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);
22 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
23 | SSL_EXT_CLIENT_HELLO \
24 | SSL_EXT_TLS1_2_SERVER_HELLO \
25 | SSL_EXT_IGNORE_ON_RESUMPTION)
27 int SSL_use_certificate(SSL *ssl, X509 *x)
31 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
34 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
36 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
40 return ssl_set_cert(ssl->cert, x);
43 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
50 in = BIO_new(BIO_s_file());
52 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
56 if (BIO_read_filename(in, file) <= 0) {
57 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
60 if (type == SSL_FILETYPE_ASN1) {
62 x = d2i_X509_bio(in, NULL);
63 } else if (type == SSL_FILETYPE_PEM) {
65 x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
66 ssl->default_passwd_callback_userdata);
68 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
73 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
77 ret = SSL_use_certificate(ssl, x);
84 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
89 x = d2i_X509(NULL, &d, (long)len);
91 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
95 ret = SSL_use_certificate(ssl, x);
100 #ifndef OPENSSL_NO_RSA
101 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
107 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
110 if ((pkey = EVP_PKEY_new()) == NULL) {
111 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
116 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
122 ret = ssl_set_pkey(ssl->cert, pkey);
128 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
132 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
133 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
137 if (c->pkeys[i].x509 != NULL) {
139 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
141 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
145 * The return code from EVP_PKEY_copy_parameters is deliberately
146 * ignored. Some EVP_PKEY types cannot do this.
148 EVP_PKEY_copy_parameters(pktmp, pkey);
151 #ifndef OPENSSL_NO_RSA
153 * Don't check the public/private key, this is mostly for smart
156 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
157 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
160 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
161 X509_free(c->pkeys[i].x509);
162 c->pkeys[i].x509 = NULL;
167 EVP_PKEY_free(c->pkeys[i].privatekey);
168 EVP_PKEY_up_ref(pkey);
169 c->pkeys[i].privatekey = pkey;
170 c->key = &c->pkeys[i];
174 #ifndef OPENSSL_NO_RSA
175 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
181 in = BIO_new(BIO_s_file());
183 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
187 if (BIO_read_filename(in, file) <= 0) {
188 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
191 if (type == SSL_FILETYPE_ASN1) {
193 rsa = d2i_RSAPrivateKey_bio(in, NULL);
194 } else if (type == SSL_FILETYPE_PEM) {
196 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
197 ssl->default_passwd_callback,
198 ssl->default_passwd_callback_userdata);
200 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
204 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
207 ret = SSL_use_RSAPrivateKey(ssl, rsa);
214 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
217 const unsigned char *p;
221 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
222 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
226 ret = SSL_use_RSAPrivateKey(ssl, rsa);
230 #endif /* !OPENSSL_NO_RSA */
232 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
237 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
240 ret = ssl_set_pkey(ssl->cert, pkey);
244 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
248 EVP_PKEY *pkey = NULL;
250 in = BIO_new(BIO_s_file());
252 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
256 if (BIO_read_filename(in, file) <= 0) {
257 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
260 if (type == SSL_FILETYPE_PEM) {
262 pkey = PEM_read_bio_PrivateKey(in, NULL,
263 ssl->default_passwd_callback,
264 ssl->default_passwd_callback_userdata);
265 } else if (type == SSL_FILETYPE_ASN1) {
267 pkey = d2i_PrivateKey_bio(in, NULL);
269 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
273 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
276 ret = SSL_use_PrivateKey(ssl, pkey);
283 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
287 const unsigned char *p;
291 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
292 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
296 ret = SSL_use_PrivateKey(ssl, pkey);
301 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
305 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
308 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
310 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
313 return ssl_set_cert(ctx->cert, x);
316 static int ssl_set_cert(CERT *c, X509 *x)
321 pkey = X509_get0_pubkey(x);
323 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
327 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
328 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
331 #ifndef OPENSSL_NO_EC
332 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
333 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
337 if (c->pkeys[i].privatekey != NULL) {
339 * The return code from EVP_PKEY_copy_parameters is deliberately
340 * ignored. Some EVP_PKEY types cannot do this.
342 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
345 #ifndef OPENSSL_NO_RSA
347 * Don't check the public/private key, this is mostly for smart
350 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
351 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
352 RSA_METHOD_FLAG_NO_CHECK) ;
354 #endif /* OPENSSL_NO_RSA */
355 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
357 * don't fail for a cert/key mismatch, just free current private
358 * key (when switching to a different cert & key, first this
359 * function should be used, then ssl_set_pkey
361 EVP_PKEY_free(c->pkeys[i].privatekey);
362 c->pkeys[i].privatekey = NULL;
363 /* clear error queue */
368 X509_free(c->pkeys[i].x509);
370 c->pkeys[i].x509 = x;
371 c->key = &(c->pkeys[i]);
376 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
383 in = BIO_new(BIO_s_file());
385 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
389 if (BIO_read_filename(in, file) <= 0) {
390 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
393 if (type == SSL_FILETYPE_ASN1) {
395 x = d2i_X509_bio(in, NULL);
396 } else if (type == SSL_FILETYPE_PEM) {
398 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
399 ctx->default_passwd_callback_userdata);
401 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
406 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
410 ret = SSL_CTX_use_certificate(ctx, x);
417 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
422 x = d2i_X509(NULL, &d, (long)len);
424 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
428 ret = SSL_CTX_use_certificate(ctx, x);
433 #ifndef OPENSSL_NO_RSA
434 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
440 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
443 if ((pkey = EVP_PKEY_new()) == NULL) {
444 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
449 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
455 ret = ssl_set_pkey(ctx->cert, pkey);
460 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
466 in = BIO_new(BIO_s_file());
468 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
472 if (BIO_read_filename(in, file) <= 0) {
473 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
476 if (type == SSL_FILETYPE_ASN1) {
478 rsa = d2i_RSAPrivateKey_bio(in, NULL);
479 } else if (type == SSL_FILETYPE_PEM) {
481 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
482 ctx->default_passwd_callback,
483 ctx->default_passwd_callback_userdata);
485 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
489 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
492 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
499 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
503 const unsigned char *p;
507 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
508 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
512 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
516 #endif /* !OPENSSL_NO_RSA */
518 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
521 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
524 return ssl_set_pkey(ctx->cert, pkey);
527 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
531 EVP_PKEY *pkey = NULL;
533 in = BIO_new(BIO_s_file());
535 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
539 if (BIO_read_filename(in, file) <= 0) {
540 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
543 if (type == SSL_FILETYPE_PEM) {
545 pkey = PEM_read_bio_PrivateKey(in, NULL,
546 ctx->default_passwd_callback,
547 ctx->default_passwd_callback_userdata);
548 } else if (type == SSL_FILETYPE_ASN1) {
550 pkey = d2i_PrivateKey_bio(in, NULL);
552 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
556 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
559 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
566 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
567 const unsigned char *d, long len)
570 const unsigned char *p;
574 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
575 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
579 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
585 * Read a file that contains our certificate in "PEM" format, possibly
586 * followed by a sequence of CA certificates that should be sent to the peer
587 * in the Certificate message.
589 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
594 pem_password_cb *passwd_callback;
595 void *passwd_callback_userdata;
597 ERR_clear_error(); /* clear error stack for
598 * SSL_CTX_use_certificate() */
601 passwd_callback = ctx->default_passwd_callback;
602 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
604 passwd_callback = ssl->default_passwd_callback;
605 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
608 in = BIO_new(BIO_s_file());
610 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
614 if (BIO_read_filename(in, file) <= 0) {
615 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
619 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
620 passwd_callback_userdata);
622 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
627 ret = SSL_CTX_use_certificate(ctx, x);
629 ret = SSL_use_certificate(ssl, x);
631 if (ERR_peek_error() != 0)
632 ret = 0; /* Key/certificate mismatch doesn't imply
636 * If we could set up our certificate, now proceed to the CA
644 r = SSL_CTX_clear_chain_certs(ctx);
646 r = SSL_clear_chain_certs(ssl);
653 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
654 passwd_callback_userdata))
657 r = SSL_CTX_add0_chain_cert(ctx, ca);
659 r = SSL_add0_chain_cert(ssl, ca);
661 * Note that we must not free ca if it was successfully added to
662 * the chain (while we must free the main certificate, since its
663 * reference count is increased by SSL_CTX_use_certificate).
671 /* When the while loop ends, it's usually just EOF. */
672 err = ERR_peek_last_error();
673 if (ERR_GET_LIB(err) == ERR_LIB_PEM
674 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
677 ret = 0; /* some real error */
686 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
688 return use_certificate_chain_file(ctx, NULL, file);
691 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
693 return use_certificate_chain_file(NULL, ssl, file);
696 static int serverinfo_find_extension(const unsigned char *serverinfo,
697 size_t serverinfo_length,
698 unsigned int extension_type,
699 const unsigned char **extension_data,
700 size_t *extension_length)
704 *extension_data = NULL;
705 *extension_length = 0;
706 if (serverinfo == NULL || serverinfo_length == 0)
709 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
713 unsigned int type = 0;
714 unsigned long context = 0;
716 /* end of serverinfo */
717 if (PACKET_remaining(&pkt) == 0)
718 return 0; /* Extension not found */
720 if (!PACKET_get_net_4(&pkt, &context)
721 || !PACKET_get_net_2(&pkt, &type)
722 || !PACKET_get_length_prefixed_2(&pkt, &data))
725 if (type == extension_type) {
726 *extension_data = PACKET_data(&data);
727 *extension_length = PACKET_remaining(&data);;
728 return 1; /* Success */
734 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
735 unsigned int context,
736 const unsigned char *in,
737 size_t inlen, X509 *x, size_t chainidx,
742 *al = SSL_AD_DECODE_ERROR;
749 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
750 const unsigned char *in,
751 size_t inlen, int *al, void *arg)
753 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
757 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
758 unsigned int context,
759 const unsigned char **out,
760 size_t *outlen, X509 *x, size_t chainidx,
763 const unsigned char *serverinfo = NULL;
764 size_t serverinfo_length = 0;
766 /* We only support extensions for the first Certificate */
767 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
770 /* Is there serverinfo data for the chosen server cert? */
771 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
772 &serverinfo_length)) != 0) {
773 /* Find the relevant extension from the serverinfo */
774 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
775 ext_type, out, outlen);
777 *al = SSL_AD_INTERNAL_ERROR;
778 return -1; /* Error */
781 return 0; /* No extension found, don't send extension */
782 return 1; /* Send extension */
784 return 0; /* No serverinfo data found, don't send
788 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
789 const unsigned char **out, size_t *outlen,
792 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
797 * With a NULL context, this function just checks that the serverinfo data
798 * parses correctly. With a non-NULL context, it registers callbacks for
799 * the included extensions.
801 static int serverinfo_process_buffer(unsigned int version,
802 const unsigned char *serverinfo,
803 size_t serverinfo_length, SSL_CTX *ctx)
807 if (serverinfo == NULL || serverinfo_length == 0)
810 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
813 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
816 while (PACKET_remaining(&pkt)) {
817 unsigned long context = 0;
818 unsigned int ext_type = 0;
821 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
822 || !PACKET_get_net_2(&pkt, &ext_type)
823 || !PACKET_get_length_prefixed_2(&pkt, &data))
830 * The old style custom extensions API could be set separately for
831 * server/client, i.e. you could set one custom extension for a client,
832 * and *for the same extension in the same SSL_CTX* you could set a
833 * custom extension for the server as well. It seems quite weird to be
834 * setting a custom extension for both client and server in a single
835 * SSL_CTX - but theoretically possible. This isn't possible in the
836 * new API. Therefore, if we have V1 serverinfo we use the old API. We
837 * also use the old API even if we have V2 serverinfo but the context
838 * looks like an old style <= TLSv1.2 extension.
840 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
841 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
842 serverinfo_srv_add_cb,
844 serverinfo_srv_parse_cb,
848 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
849 serverinfoex_srv_add_cb,
851 serverinfoex_srv_parse_cb,
860 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
861 const unsigned char *serverinfo,
862 size_t serverinfo_length)
864 unsigned char *new_serverinfo;
866 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
867 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
870 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
872 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
875 if (ctx->cert->key == NULL) {
876 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
879 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
881 if (new_serverinfo == NULL) {
882 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
885 ctx->cert->key->serverinfo = new_serverinfo;
886 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
887 ctx->cert->key->serverinfo_length = serverinfo_length;
890 * Now that the serverinfo is validated and stored, go ahead and
891 * register callbacks.
893 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
895 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
901 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
902 size_t serverinfo_length)
904 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
908 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
910 unsigned char *serverinfo = NULL;
912 size_t serverinfo_length = 0;
913 unsigned char *extension = 0;
914 long extension_length = 0;
917 char namePrefix1[] = "SERVERINFO FOR ";
918 char namePrefix2[] = "SERVERINFOV2 FOR ";
921 size_t num_extensions = 0, contextoff = 0;
923 if (ctx == NULL || file == NULL) {
924 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
928 bin = BIO_new(BIO_s_file());
930 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
933 if (BIO_read_filename(bin, file) <= 0) {
934 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
938 for (num_extensions = 0;; num_extensions++) {
939 unsigned int version;
941 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
944 * There must be at least one extension in this file
946 if (num_extensions == 0) {
947 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
948 SSL_R_NO_PEM_EXTENSIONS);
950 } else /* End of file, we're done */
953 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
954 if (strlen(name) < strlen(namePrefix1)) {
955 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
958 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
959 version = SSL_SERVERINFOV1;
961 if (strlen(name) < strlen(namePrefix2)) {
962 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
963 SSL_R_PEM_NAME_TOO_SHORT);
966 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
967 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
968 SSL_R_PEM_NAME_BAD_PREFIX);
971 version = SSL_SERVERINFOV2;
974 * Check that the decoded PEM data is plausible (valid length field)
976 if (version == SSL_SERVERINFOV1) {
977 /* 4 byte header: 2 bytes type, 2 bytes len */
978 if (extension_length < 4
979 || (extension[2] << 8) + extension[3]
980 != extension_length - 4) {
981 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
985 * File does not have a context value so we must take account of
990 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
991 if (extension_length < 8
992 || (extension[6] << 8) + extension[7]
993 != extension_length - 8) {
994 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
998 /* Append the decoded extension to the serverinfo buffer */
999 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1002 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1006 if (contextoff > 0) {
1007 unsigned char *sinfo = serverinfo + serverinfo_length;
1009 /* We know this only uses the last 2 bytes */
1012 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1013 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1015 memcpy(serverinfo + serverinfo_length + contextoff,
1016 extension, extension_length);
1017 serverinfo_length += extension_length + contextoff;
1021 OPENSSL_free(header);
1023 OPENSSL_free(extension);
1027 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1030 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1032 OPENSSL_free(header);
1033 OPENSSL_free(extension);
1034 OPENSSL_free(serverinfo);