2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "packet_locl.h"
13 #include <openssl/bio.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/pem.h>
19 static int ssl_set_cert(CERT *c, X509 *x509);
20 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
21 int SSL_use_certificate(SSL *ssl, X509 *x)
25 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
28 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
30 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
34 return (ssl_set_cert(ssl->cert, x));
37 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
44 in = BIO_new(BIO_s_file());
46 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
50 if (BIO_read_filename(in, file) <= 0) {
51 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
54 if (type == SSL_FILETYPE_ASN1) {
56 x = d2i_X509_bio(in, NULL);
57 } else if (type == SSL_FILETYPE_PEM) {
59 x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
60 ssl->default_passwd_callback_userdata);
62 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
67 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
71 ret = SSL_use_certificate(ssl, x);
78 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
83 x = d2i_X509(NULL, &d, (long)len);
85 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
89 ret = SSL_use_certificate(ssl, x);
94 #ifndef OPENSSL_NO_RSA
95 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
101 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
104 if ((pkey = EVP_PKEY_new()) == NULL) {
105 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
110 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
116 ret = ssl_set_pkey(ssl->cert, pkey);
122 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
125 i = ssl_cert_type(NULL, pkey);
127 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
131 if (c->pkeys[i].x509 != NULL) {
133 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
135 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
139 * The return code from EVP_PKEY_copy_parameters is deliberately
140 * ignored. Some EVP_PKEY types cannot do this.
142 EVP_PKEY_copy_parameters(pktmp, pkey);
145 #ifndef OPENSSL_NO_RSA
147 * Don't check the public/private key, this is mostly for smart
150 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
151 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
154 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
155 X509_free(c->pkeys[i].x509);
156 c->pkeys[i].x509 = NULL;
161 EVP_PKEY_free(c->pkeys[i].privatekey);
162 EVP_PKEY_up_ref(pkey);
163 c->pkeys[i].privatekey = pkey;
164 c->key = &(c->pkeys[i]);
168 #ifndef OPENSSL_NO_RSA
169 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
175 in = BIO_new(BIO_s_file());
177 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
181 if (BIO_read_filename(in, file) <= 0) {
182 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
185 if (type == SSL_FILETYPE_ASN1) {
187 rsa = d2i_RSAPrivateKey_bio(in, NULL);
188 } else if (type == SSL_FILETYPE_PEM) {
190 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
191 ssl->default_passwd_callback,
192 ssl->default_passwd_callback_userdata);
194 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
198 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
201 ret = SSL_use_RSAPrivateKey(ssl, rsa);
208 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
211 const unsigned char *p;
215 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
216 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
220 ret = SSL_use_RSAPrivateKey(ssl, rsa);
224 #endif /* !OPENSSL_NO_RSA */
226 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
231 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
234 ret = ssl_set_pkey(ssl->cert, pkey);
238 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
242 EVP_PKEY *pkey = NULL;
244 in = BIO_new(BIO_s_file());
246 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
250 if (BIO_read_filename(in, file) <= 0) {
251 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
254 if (type == SSL_FILETYPE_PEM) {
256 pkey = PEM_read_bio_PrivateKey(in, NULL,
257 ssl->default_passwd_callback,
258 ssl->default_passwd_callback_userdata);
259 } else if (type == SSL_FILETYPE_ASN1) {
261 pkey = d2i_PrivateKey_bio(in, NULL);
263 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
267 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
270 ret = SSL_use_PrivateKey(ssl, pkey);
277 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
281 const unsigned char *p;
285 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
286 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
290 ret = SSL_use_PrivateKey(ssl, pkey);
295 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
299 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
302 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
304 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
307 return (ssl_set_cert(ctx->cert, x));
310 static int ssl_set_cert(CERT *c, X509 *x)
315 pkey = X509_get0_pubkey(x);
317 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
321 i = ssl_cert_type(x, pkey);
323 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
326 #ifndef OPENSSL_NO_EC
327 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
328 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
332 if (c->pkeys[i].privatekey != NULL) {
334 * The return code from EVP_PKEY_copy_parameters is deliberately
335 * ignored. Some EVP_PKEY types cannot do this.
337 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
340 #ifndef OPENSSL_NO_RSA
342 * Don't check the public/private key, this is mostly for smart
345 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
346 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
347 RSA_METHOD_FLAG_NO_CHECK) ;
349 #endif /* OPENSSL_NO_RSA */
350 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
352 * don't fail for a cert/key mismatch, just free current private
353 * key (when switching to a different cert & key, first this
354 * function should be used, then ssl_set_pkey
356 EVP_PKEY_free(c->pkeys[i].privatekey);
357 c->pkeys[i].privatekey = NULL;
358 /* clear error queue */
363 X509_free(c->pkeys[i].x509);
365 c->pkeys[i].x509 = x;
366 c->key = &(c->pkeys[i]);
371 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
378 in = BIO_new(BIO_s_file());
380 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
384 if (BIO_read_filename(in, file) <= 0) {
385 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
388 if (type == SSL_FILETYPE_ASN1) {
390 x = d2i_X509_bio(in, NULL);
391 } else if (type == SSL_FILETYPE_PEM) {
393 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
394 ctx->default_passwd_callback_userdata);
396 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
401 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
405 ret = SSL_CTX_use_certificate(ctx, x);
412 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
417 x = d2i_X509(NULL, &d, (long)len);
419 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
423 ret = SSL_CTX_use_certificate(ctx, x);
428 #ifndef OPENSSL_NO_RSA
429 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
435 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
438 if ((pkey = EVP_PKEY_new()) == NULL) {
439 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
444 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
450 ret = ssl_set_pkey(ctx->cert, pkey);
455 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
461 in = BIO_new(BIO_s_file());
463 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
467 if (BIO_read_filename(in, file) <= 0) {
468 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
471 if (type == SSL_FILETYPE_ASN1) {
473 rsa = d2i_RSAPrivateKey_bio(in, NULL);
474 } else if (type == SSL_FILETYPE_PEM) {
476 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
477 ctx->default_passwd_callback,
478 ctx->default_passwd_callback_userdata);
480 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
484 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
487 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
494 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
498 const unsigned char *p;
502 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
503 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
507 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
511 #endif /* !OPENSSL_NO_RSA */
513 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
516 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
519 return (ssl_set_pkey(ctx->cert, pkey));
522 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
526 EVP_PKEY *pkey = NULL;
528 in = BIO_new(BIO_s_file());
530 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
534 if (BIO_read_filename(in, file) <= 0) {
535 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
538 if (type == SSL_FILETYPE_PEM) {
540 pkey = PEM_read_bio_PrivateKey(in, NULL,
541 ctx->default_passwd_callback,
542 ctx->default_passwd_callback_userdata);
543 } else if (type == SSL_FILETYPE_ASN1) {
545 pkey = d2i_PrivateKey_bio(in, NULL);
547 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
551 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
554 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
561 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
562 const unsigned char *d, long len)
565 const unsigned char *p;
569 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
570 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
574 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
580 * Read a file that contains our certificate in "PEM" format, possibly
581 * followed by a sequence of CA certificates that should be sent to the peer
582 * in the Certificate message.
584 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
589 pem_password_cb *passwd_callback;
590 void *passwd_callback_userdata;
592 ERR_clear_error(); /* clear error stack for
593 * SSL_CTX_use_certificate() */
596 passwd_callback = ctx->default_passwd_callback;
597 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
599 passwd_callback = ssl->default_passwd_callback;
600 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
603 in = BIO_new(BIO_s_file());
605 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
609 if (BIO_read_filename(in, file) <= 0) {
610 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
614 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
615 passwd_callback_userdata);
617 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
622 ret = SSL_CTX_use_certificate(ctx, x);
624 ret = SSL_use_certificate(ssl, x);
626 if (ERR_peek_error() != 0)
627 ret = 0; /* Key/certificate mismatch doesn't imply
631 * If we could set up our certificate, now proceed to the CA
639 r = SSL_CTX_clear_chain_certs(ctx);
641 r = SSL_clear_chain_certs(ssl);
648 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
649 passwd_callback_userdata))
652 r = SSL_CTX_add0_chain_cert(ctx, ca);
654 r = SSL_add0_chain_cert(ssl, ca);
656 * Note that we must not free ca if it was successfully added to
657 * the chain (while we must free the main certificate, since its
658 * reference count is increased by SSL_CTX_use_certificate).
666 /* When the while loop ends, it's usually just EOF. */
667 err = ERR_peek_last_error();
668 if (ERR_GET_LIB(err) == ERR_LIB_PEM
669 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
672 ret = 0; /* some real error */
681 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
683 return use_certificate_chain_file(ctx, NULL, file);
686 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
688 return use_certificate_chain_file(NULL, ssl, file);
691 static int serverinfo_find_extension(const unsigned char *serverinfo,
692 size_t serverinfo_length,
693 unsigned int extension_type,
694 const unsigned char **extension_data,
695 size_t *extension_length)
699 *extension_data = NULL;
700 *extension_length = 0;
701 if (serverinfo == NULL || serverinfo_length == 0)
704 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
708 unsigned int type = 0;
709 unsigned long context = 0;
711 /* end of serverinfo */
712 if (PACKET_remaining(&pkt) == 0)
713 return 0; /* Extension not found */
715 if (!PACKET_get_net_4(&pkt, &context)
716 || !PACKET_get_net_2(&pkt, &type)
717 || !PACKET_get_length_prefixed_2(&pkt, &data))
720 if (type == extension_type) {
721 *extension_data = PACKET_data(&data);
722 *extension_length = PACKET_remaining(&data);;
723 return 1; /* Success */
729 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
730 unsigned int context,
731 const unsigned char *in,
732 size_t inlen, X509 *x, size_t chainidx,
737 *al = SSL_AD_DECODE_ERROR;
744 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
745 const unsigned char *in,
746 size_t inlen, int *al, void *arg)
748 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
752 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
753 unsigned int context,
754 const unsigned char **out,
755 size_t *outlen, X509 *x, size_t chainidx,
758 const unsigned char *serverinfo = NULL;
759 size_t serverinfo_length = 0;
761 /* Is there serverinfo data for the chosen server cert? */
762 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
763 &serverinfo_length)) != 0) {
764 /* Find the relevant extension from the serverinfo */
765 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
766 ext_type, out, outlen);
768 *al = SSL_AD_DECODE_ERROR;
769 return -1; /* Error */
772 return 0; /* No extension found, don't send extension */
773 return 1; /* Send extension */
775 return 0; /* No serverinfo data found, don't send
779 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
780 const unsigned char **out, size_t *outlen,
783 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
788 * With a NULL context, this function just checks that the serverinfo data
789 * parses correctly. With a non-NULL context, it registers callbacks for
790 * the included extensions.
792 static int serverinfo_process_buffer(unsigned int version,
793 const unsigned char *serverinfo,
794 size_t serverinfo_length, SSL_CTX *ctx)
798 if (serverinfo == NULL || serverinfo_length == 0)
801 if (version != SERVERINFOV1 && version != SERVERINFOV2)
804 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
807 while (PACKET_remaining(&pkt)) {
808 unsigned long context = 0;
809 unsigned int ext_type = 0;
812 if (!PACKET_get_net_4(&pkt, &context)
813 || !PACKET_get_net_2(&pkt, &ext_type)
814 || !PACKET_get_length_prefixed_2(&pkt, &data))
820 if (version == SERVERINFOV1) {
821 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
822 serverinfo_srv_add_cb,
824 serverinfo_srv_parse_cb,
828 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
829 serverinfoex_srv_add_cb,
831 serverinfoex_srv_parse_cb,
840 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
841 const unsigned char *serverinfo,
842 size_t serverinfo_length)
844 unsigned char *new_serverinfo;
846 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
847 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
850 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
852 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
855 if (ctx->cert->key == NULL) {
856 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
859 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
861 if (new_serverinfo == NULL) {
862 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
865 ctx->cert->key->serverinfo = new_serverinfo;
866 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
867 ctx->cert->key->serverinfo_length = serverinfo_length;
870 * Now that the serverinfo is validated and stored, go ahead and
871 * register callbacks.
873 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
875 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
881 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
882 size_t serverinfo_length)
884 return SSL_CTX_use_serverinfo_ex(ctx, SERVERINFOV1, serverinfo,
888 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
890 unsigned char *serverinfo = NULL;
892 size_t serverinfo_length = 0;
893 unsigned char *extension = 0;
894 long extension_length = 0;
897 char namePrefix1[] = "SERVERINFO FOR ";
898 char namePrefix2[] = "SERVERINFOV2 FOR ";
901 size_t num_extensions = 0, contextoff = 0;
902 unsigned int version;
904 if (ctx == NULL || file == NULL) {
905 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
909 bin = BIO_new(BIO_s_file());
911 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
914 if (BIO_read_filename(bin, file) <= 0) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
919 for (num_extensions = 0;; num_extensions++) {
920 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
923 * There must be at least one extension in this file
925 if (num_extensions == 0) {
926 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
927 SSL_R_NO_PEM_EXTENSIONS);
929 } else /* End of file, we're done */
932 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
933 if (strlen(name) < strlen(namePrefix1)) {
934 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
937 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
938 version = SERVERINFOV1;
940 if (strlen(name) < strlen(namePrefix2)) {
941 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
942 SSL_R_PEM_NAME_TOO_SHORT);
945 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
947 SSL_R_PEM_NAME_BAD_PREFIX);
950 version = SERVERINFOV2;
953 * Check that the decoded PEM data is plausible (valid length field)
955 if (version == SERVERINFOV1) {
956 /* 4 byte header: 2 bytes type, 2 bytes len */
957 if (extension_length < 4
958 || (extension[2] << 8) + extension[3]
959 != extension_length - 4) {
960 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
964 * File does not have a context value so we must take account of
969 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
970 if (extension_length < 8
971 || (extension[6] << 8) + extension[7]
972 != extension_length - 8) {
973 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
977 /* Append the decoded extension to the serverinfo buffer */
978 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
981 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
985 if (contextoff > 0) {
986 unsigned int synthcontext = SSL_EXT_CLIENT_HELLO
987 | SSL_EXT_TLS1_2_SERVER_HELLO;
988 unsigned char *sinfo = serverinfo + serverinfo_length;
990 /* We know this only uses the last 2 bytes */
993 sinfo[2] = (synthcontext >> 8) & 0xff;
994 sinfo[3] = synthcontext & 0xff;
996 memcpy(serverinfo + serverinfo_length + contextoff,
997 extension, extension_length);
998 serverinfo_length += extension_length + contextoff;
1002 OPENSSL_free(header);
1004 OPENSSL_free(extension);
1008 ret = SSL_CTX_use_serverinfo_ex(ctx, version, serverinfo,
1011 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1013 OPENSSL_free(header);
1014 OPENSSL_free(extension);
1015 OPENSSL_free(serverinfo);