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
13 #include "packet_locl.h"
14 #include <openssl/bio.h>
15 #include <openssl/objects.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
20 static int ssl_set_cert(CERT *c, X509 *x509);
21 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
22 int SSL_use_certificate(SSL *ssl, X509 *x)
26 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
29 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
31 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
35 return (ssl_set_cert(ssl->cert, x));
38 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
45 in = BIO_new(BIO_s_file());
47 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
51 if (BIO_read_filename(in, file) <= 0) {
52 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
55 if (type == SSL_FILETYPE_ASN1) {
57 x = d2i_X509_bio(in, NULL);
58 } else if (type == SSL_FILETYPE_PEM) {
60 x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
61 ssl->default_passwd_callback_userdata);
63 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
68 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
72 ret = SSL_use_certificate(ssl, x);
79 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
84 x = d2i_X509(NULL, &d, (long)len);
86 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
90 ret = SSL_use_certificate(ssl, x);
95 #ifndef OPENSSL_NO_RSA
96 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
102 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
105 if ((pkey = EVP_PKEY_new()) == NULL) {
106 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
111 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
117 ret = ssl_set_pkey(ssl->cert, pkey);
123 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
126 i = ssl_cert_type(NULL, pkey);
128 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
132 if (c->pkeys[i].x509 != NULL) {
134 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
136 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
140 * The return code from EVP_PKEY_copy_parameters is deliberately
141 * ignored. Some EVP_PKEY types cannot do this.
143 EVP_PKEY_copy_parameters(pktmp, pkey);
146 #ifndef OPENSSL_NO_RSA
148 * Don't check the public/private key, this is mostly for smart
151 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
152 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
155 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
156 X509_free(c->pkeys[i].x509);
157 c->pkeys[i].x509 = NULL;
162 EVP_PKEY_free(c->pkeys[i].privatekey);
163 EVP_PKEY_up_ref(pkey);
164 c->pkeys[i].privatekey = pkey;
165 c->key = &(c->pkeys[i]);
169 #ifndef OPENSSL_NO_RSA
170 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
176 in = BIO_new(BIO_s_file());
178 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
182 if (BIO_read_filename(in, file) <= 0) {
183 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
186 if (type == SSL_FILETYPE_ASN1) {
188 rsa = d2i_RSAPrivateKey_bio(in, NULL);
189 } else if (type == SSL_FILETYPE_PEM) {
191 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
192 ssl->default_passwd_callback,
193 ssl->default_passwd_callback_userdata);
195 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
199 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
202 ret = SSL_use_RSAPrivateKey(ssl, rsa);
209 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
212 const unsigned char *p;
216 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
217 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
221 ret = SSL_use_RSAPrivateKey(ssl, rsa);
225 #endif /* !OPENSSL_NO_RSA */
227 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
232 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
235 ret = ssl_set_pkey(ssl->cert, pkey);
239 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
243 EVP_PKEY *pkey = NULL;
245 in = BIO_new(BIO_s_file());
247 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
251 if (BIO_read_filename(in, file) <= 0) {
252 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
255 if (type == SSL_FILETYPE_PEM) {
257 pkey = PEM_read_bio_PrivateKey(in, NULL,
258 ssl->default_passwd_callback,
259 ssl->default_passwd_callback_userdata);
260 } else if (type == SSL_FILETYPE_ASN1) {
262 pkey = d2i_PrivateKey_bio(in, NULL);
264 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
268 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
271 ret = SSL_use_PrivateKey(ssl, pkey);
278 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
282 const unsigned char *p;
286 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
287 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
291 ret = SSL_use_PrivateKey(ssl, pkey);
296 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
300 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
303 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
305 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
308 return (ssl_set_cert(ctx->cert, x));
311 static int ssl_set_cert(CERT *c, X509 *x)
316 pkey = X509_get0_pubkey(x);
318 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
322 i = ssl_cert_type(x, pkey);
324 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
327 #ifndef OPENSSL_NO_EC
328 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
329 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
333 if (c->pkeys[i].privatekey != NULL) {
335 * The return code from EVP_PKEY_copy_parameters is deliberately
336 * ignored. Some EVP_PKEY types cannot do this.
338 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
341 #ifndef OPENSSL_NO_RSA
343 * Don't check the public/private key, this is mostly for smart
346 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
347 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
348 RSA_METHOD_FLAG_NO_CHECK) ;
350 #endif /* OPENSSL_NO_RSA */
351 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
353 * don't fail for a cert/key mismatch, just free current private
354 * key (when switching to a different cert & key, first this
355 * function should be used, then ssl_set_pkey
357 EVP_PKEY_free(c->pkeys[i].privatekey);
358 c->pkeys[i].privatekey = NULL;
359 /* clear error queue */
364 X509_free(c->pkeys[i].x509);
366 c->pkeys[i].x509 = x;
367 c->key = &(c->pkeys[i]);
372 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
379 in = BIO_new(BIO_s_file());
381 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
385 if (BIO_read_filename(in, file) <= 0) {
386 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
389 if (type == SSL_FILETYPE_ASN1) {
391 x = d2i_X509_bio(in, NULL);
392 } else if (type == SSL_FILETYPE_PEM) {
394 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
395 ctx->default_passwd_callback_userdata);
397 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
402 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
406 ret = SSL_CTX_use_certificate(ctx, x);
413 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
418 x = d2i_X509(NULL, &d, (long)len);
420 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
424 ret = SSL_CTX_use_certificate(ctx, x);
429 #ifndef OPENSSL_NO_RSA
430 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
436 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
439 if ((pkey = EVP_PKEY_new()) == NULL) {
440 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
445 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
451 ret = ssl_set_pkey(ctx->cert, pkey);
456 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
462 in = BIO_new(BIO_s_file());
464 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
468 if (BIO_read_filename(in, file) <= 0) {
469 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
472 if (type == SSL_FILETYPE_ASN1) {
474 rsa = d2i_RSAPrivateKey_bio(in, NULL);
475 } else if (type == SSL_FILETYPE_PEM) {
477 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
478 ctx->default_passwd_callback,
479 ctx->default_passwd_callback_userdata);
481 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
485 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
488 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
495 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
499 const unsigned char *p;
503 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
504 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
508 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
512 #endif /* !OPENSSL_NO_RSA */
514 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
517 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
520 return (ssl_set_pkey(ctx->cert, pkey));
523 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
527 EVP_PKEY *pkey = NULL;
529 in = BIO_new(BIO_s_file());
531 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
535 if (BIO_read_filename(in, file) <= 0) {
536 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
539 if (type == SSL_FILETYPE_PEM) {
541 pkey = PEM_read_bio_PrivateKey(in, NULL,
542 ctx->default_passwd_callback,
543 ctx->default_passwd_callback_userdata);
544 } else if (type == SSL_FILETYPE_ASN1) {
546 pkey = d2i_PrivateKey_bio(in, NULL);
548 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
552 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
555 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
562 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
563 const unsigned char *d, long len)
566 const unsigned char *p;
570 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
571 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
575 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
581 * Read a file that contains our certificate in "PEM" format, possibly
582 * followed by a sequence of CA certificates that should be sent to the peer
583 * in the Certificate message.
585 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
590 pem_password_cb *passwd_callback;
591 void *passwd_callback_userdata;
593 ERR_clear_error(); /* clear error stack for
594 * SSL_CTX_use_certificate() */
597 passwd_callback = ctx->default_passwd_callback;
598 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
600 passwd_callback = ssl->default_passwd_callback;
601 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
604 in = BIO_new(BIO_s_file());
606 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
610 if (BIO_read_filename(in, file) <= 0) {
611 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
615 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
616 passwd_callback_userdata);
618 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
623 ret = SSL_CTX_use_certificate(ctx, x);
625 ret = SSL_use_certificate(ssl, x);
627 if (ERR_peek_error() != 0)
628 ret = 0; /* Key/certificate mismatch doesn't imply
632 * If we could set up our certificate, now proceed to the CA
640 r = SSL_CTX_clear_chain_certs(ctx);
642 r = SSL_clear_chain_certs(ssl);
649 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
650 passwd_callback_userdata))
653 r = SSL_CTX_add0_chain_cert(ctx, ca);
655 r = SSL_add0_chain_cert(ssl, ca);
657 * Note that we must not free ca if it was successfully added to
658 * the chain (while we must free the main certificate, since its
659 * reference count is increased by SSL_CTX_use_certificate).
667 /* When the while loop ends, it's usually just EOF. */
668 err = ERR_peek_last_error();
669 if (ERR_GET_LIB(err) == ERR_LIB_PEM
670 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
673 ret = 0; /* some real error */
682 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
684 return use_certificate_chain_file(ctx, NULL, file);
687 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
689 return use_certificate_chain_file(NULL, ssl, file);
692 static int serverinfo_find_extension(const unsigned char *serverinfo,
693 size_t serverinfo_length,
694 unsigned int extension_type,
695 const unsigned char **extension_data,
696 size_t *extension_length)
700 *extension_data = NULL;
701 *extension_length = 0;
702 if (serverinfo == NULL || serverinfo_length == 0)
705 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
709 unsigned int type = 0;
710 unsigned long context = 0;
712 /* end of serverinfo */
713 if (PACKET_remaining(&pkt) == 0)
714 return 0; /* Extension not found */
716 if (!PACKET_get_net_4(&pkt, &context)
717 || !PACKET_get_net_2(&pkt, &type)
718 || !PACKET_get_length_prefixed_2(&pkt, &data))
721 if (type == extension_type) {
722 *extension_data = PACKET_data(&data);
723 *extension_length = PACKET_remaining(&data);;
724 return 1; /* Success */
730 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
731 unsigned int context,
732 const unsigned char *in,
733 size_t inlen, X509 *x, size_t chainidx,
738 *al = SSL_AD_DECODE_ERROR;
745 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
746 const unsigned char *in,
747 size_t inlen, int *al, void *arg)
749 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
753 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
754 unsigned int context,
755 const unsigned char **out,
756 size_t *outlen, X509 *x, size_t chainidx,
759 const unsigned char *serverinfo = NULL;
760 size_t serverinfo_length = 0;
762 /* We only support extensions for the first Certificate */
763 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
766 /* Is there serverinfo data for the chosen server cert? */
767 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
768 &serverinfo_length)) != 0) {
769 /* Find the relevant extension from the serverinfo */
770 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
771 ext_type, out, outlen);
773 *al = SSL_AD_DECODE_ERROR;
774 return -1; /* Error */
777 return 0; /* No extension found, don't send extension */
778 return 1; /* Send extension */
780 return 0; /* No serverinfo data found, don't send
784 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
785 const unsigned char **out, size_t *outlen,
788 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
793 * With a NULL context, this function just checks that the serverinfo data
794 * parses correctly. With a non-NULL context, it registers callbacks for
795 * the included extensions.
797 static int serverinfo_process_buffer(unsigned int version,
798 const unsigned char *serverinfo,
799 size_t serverinfo_length, SSL_CTX *ctx)
803 if (serverinfo == NULL || serverinfo_length == 0)
806 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
809 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
812 while (PACKET_remaining(&pkt)) {
813 unsigned long context = 0;
814 unsigned int ext_type = 0;
817 if (!PACKET_get_net_4(&pkt, &context)
818 || !PACKET_get_net_2(&pkt, &ext_type)
819 || !PACKET_get_length_prefixed_2(&pkt, &data))
825 if (version == SSL_SERVERINFOV1) {
826 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
827 serverinfo_srv_add_cb,
829 serverinfo_srv_parse_cb,
833 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
834 serverinfoex_srv_add_cb,
836 serverinfoex_srv_parse_cb,
845 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
846 const unsigned char *serverinfo,
847 size_t serverinfo_length)
849 unsigned char *new_serverinfo;
851 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
852 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
855 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
857 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
860 if (ctx->cert->key == NULL) {
861 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
864 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
866 if (new_serverinfo == NULL) {
867 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
870 ctx->cert->key->serverinfo = new_serverinfo;
871 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
872 ctx->cert->key->serverinfo_length = serverinfo_length;
875 * Now that the serverinfo is validated and stored, go ahead and
876 * register callbacks.
878 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
880 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
886 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
887 size_t serverinfo_length)
889 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
893 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
895 unsigned char *serverinfo = NULL;
897 size_t serverinfo_length = 0;
898 unsigned char *extension = 0;
899 long extension_length = 0;
902 char namePrefix1[] = "SERVERINFO FOR ";
903 char namePrefix2[] = "SERVERINFOV2 FOR ";
906 size_t num_extensions = 0, contextoff = 0;
907 unsigned int version = 0;
909 if (ctx == NULL || file == NULL) {
910 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
914 bin = BIO_new(BIO_s_file());
916 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
919 if (BIO_read_filename(bin, file) <= 0) {
920 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
924 for (num_extensions = 0;; num_extensions++) {
925 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
928 * There must be at least one extension in this file
930 if (num_extensions == 0) {
931 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
932 SSL_R_NO_PEM_EXTENSIONS);
934 } else /* End of file, we're done */
937 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
938 if (strlen(name) < strlen(namePrefix1)) {
939 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
942 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
943 version = SSL_SERVERINFOV1;
945 if (strlen(name) < strlen(namePrefix2)) {
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
947 SSL_R_PEM_NAME_TOO_SHORT);
950 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
951 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
952 SSL_R_PEM_NAME_BAD_PREFIX);
955 version = SSL_SERVERINFOV2;
958 * Check that the decoded PEM data is plausible (valid length field)
960 if (version == SSL_SERVERINFOV1) {
961 /* 4 byte header: 2 bytes type, 2 bytes len */
962 if (extension_length < 4
963 || (extension[2] << 8) + extension[3]
964 != extension_length - 4) {
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
969 * File does not have a context value so we must take account of
974 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
975 if (extension_length < 8
976 || (extension[6] << 8) + extension[7]
977 != extension_length - 8) {
978 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
982 /* Append the decoded extension to the serverinfo buffer */
983 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
986 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
990 if (contextoff > 0) {
991 unsigned int synthcontext = SSL_EXT_CLIENT_HELLO
992 | SSL_EXT_TLS1_2_SERVER_HELLO;
993 unsigned char *sinfo = serverinfo + serverinfo_length;
995 /* We know this only uses the last 2 bytes */
998 sinfo[2] = (synthcontext >> 8) & 0xff;
999 sinfo[3] = synthcontext & 0xff;
1001 memcpy(serverinfo + serverinfo_length + contextoff,
1002 extension, extension_length);
1003 serverinfo_length += extension_length + contextoff;
1007 OPENSSL_free(header);
1009 OPENSSL_free(extension);
1013 assert(version != 0);
1015 ret = SSL_CTX_use_serverinfo_ex(ctx, version, serverinfo,
1018 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1020 OPENSSL_free(header);
1021 OPENSSL_free(extension);
1022 OPENSSL_free(serverinfo);