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 /* We only support extensions for the first Certificate */
762 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
765 /* Is there serverinfo data for the chosen server cert? */
766 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
767 &serverinfo_length)) != 0) {
768 /* Find the relevant extension from the serverinfo */
769 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
770 ext_type, out, outlen);
772 *al = SSL_AD_DECODE_ERROR;
773 return -1; /* Error */
776 return 0; /* No extension found, don't send extension */
777 return 1; /* Send extension */
779 return 0; /* No serverinfo data found, don't send
783 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
784 const unsigned char **out, size_t *outlen,
787 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
792 * With a NULL context, this function just checks that the serverinfo data
793 * parses correctly. With a non-NULL context, it registers callbacks for
794 * the included extensions.
796 static int serverinfo_process_buffer(unsigned int version,
797 const unsigned char *serverinfo,
798 size_t serverinfo_length, SSL_CTX *ctx)
802 if (serverinfo == NULL || serverinfo_length == 0)
805 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
808 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
811 while (PACKET_remaining(&pkt)) {
812 unsigned long context = 0;
813 unsigned int ext_type = 0;
816 if (!PACKET_get_net_4(&pkt, &context)
817 || !PACKET_get_net_2(&pkt, &ext_type)
818 || !PACKET_get_length_prefixed_2(&pkt, &data))
824 if (version == SSL_SERVERINFOV1) {
825 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
826 serverinfo_srv_add_cb,
828 serverinfo_srv_parse_cb,
832 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
833 serverinfoex_srv_add_cb,
835 serverinfoex_srv_parse_cb,
844 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
845 const unsigned char *serverinfo,
846 size_t serverinfo_length)
848 unsigned char *new_serverinfo;
850 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
851 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
854 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
856 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
859 if (ctx->cert->key == NULL) {
860 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
863 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
865 if (new_serverinfo == NULL) {
866 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
869 ctx->cert->key->serverinfo = new_serverinfo;
870 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
871 ctx->cert->key->serverinfo_length = serverinfo_length;
874 * Now that the serverinfo is validated and stored, go ahead and
875 * register callbacks.
877 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
879 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
885 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
886 size_t serverinfo_length)
888 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
892 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
894 unsigned char *serverinfo = NULL;
896 size_t serverinfo_length = 0;
897 unsigned char *extension = 0;
898 long extension_length = 0;
901 char namePrefix1[] = "SERVERINFO FOR ";
902 char namePrefix2[] = "SERVERINFOV2 FOR ";
905 size_t num_extensions = 0, contextoff = 0;
906 unsigned int version;
908 if (ctx == NULL || file == NULL) {
909 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
913 bin = BIO_new(BIO_s_file());
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
918 if (BIO_read_filename(bin, file) <= 0) {
919 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
923 for (num_extensions = 0;; num_extensions++) {
924 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
927 * There must be at least one extension in this file
929 if (num_extensions == 0) {
930 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
931 SSL_R_NO_PEM_EXTENSIONS);
933 } else /* End of file, we're done */
936 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
937 if (strlen(name) < strlen(namePrefix1)) {
938 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
941 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
942 version = SSL_SERVERINFOV1;
944 if (strlen(name) < strlen(namePrefix2)) {
945 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
946 SSL_R_PEM_NAME_TOO_SHORT);
949 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
950 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
951 SSL_R_PEM_NAME_BAD_PREFIX);
954 version = SSL_SERVERINFOV2;
957 * Check that the decoded PEM data is plausible (valid length field)
959 if (version == SSL_SERVERINFOV1) {
960 /* 4 byte header: 2 bytes type, 2 bytes len */
961 if (extension_length < 4
962 || (extension[2] << 8) + extension[3]
963 != extension_length - 4) {
964 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
968 * File does not have a context value so we must take account of
973 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
974 if (extension_length < 8
975 || (extension[6] << 8) + extension[7]
976 != extension_length - 8) {
977 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
981 /* Append the decoded extension to the serverinfo buffer */
982 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
985 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
989 if (contextoff > 0) {
990 unsigned int synthcontext = SSL_EXT_CLIENT_HELLO
991 | SSL_EXT_TLS1_2_SERVER_HELLO;
992 unsigned char *sinfo = serverinfo + serverinfo_length;
994 /* We know this only uses the last 2 bytes */
997 sinfo[2] = (synthcontext >> 8) & 0xff;
998 sinfo[3] = synthcontext & 0xff;
1000 memcpy(serverinfo + serverinfo_length + contextoff,
1001 extension, extension_length);
1002 serverinfo_length += extension_length + contextoff;
1006 OPENSSL_free(header);
1008 OPENSSL_free(extension);
1012 ret = SSL_CTX_use_serverinfo_ex(ctx, version, serverinfo,
1015 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1017 OPENSSL_free(header);
1018 OPENSSL_free(extension);
1019 OPENSSL_free(serverinfo);