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)
131 i = ssl_cert_type(NULL, pkey);
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 i = ssl_cert_type(x, pkey);
329 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
332 #ifndef OPENSSL_NO_EC
333 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
334 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
338 if (c->pkeys[i].privatekey != NULL) {
340 * The return code from EVP_PKEY_copy_parameters is deliberately
341 * ignored. Some EVP_PKEY types cannot do this.
343 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
346 #ifndef OPENSSL_NO_RSA
348 * Don't check the public/private key, this is mostly for smart
351 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
352 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
353 RSA_METHOD_FLAG_NO_CHECK) ;
355 #endif /* OPENSSL_NO_RSA */
356 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
358 * don't fail for a cert/key mismatch, just free current private
359 * key (when switching to a different cert & key, first this
360 * function should be used, then ssl_set_pkey
362 EVP_PKEY_free(c->pkeys[i].privatekey);
363 c->pkeys[i].privatekey = NULL;
364 /* clear error queue */
369 X509_free(c->pkeys[i].x509);
371 c->pkeys[i].x509 = x;
372 c->key = &(c->pkeys[i]);
377 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
384 in = BIO_new(BIO_s_file());
386 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
390 if (BIO_read_filename(in, file) <= 0) {
391 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
394 if (type == SSL_FILETYPE_ASN1) {
396 x = d2i_X509_bio(in, NULL);
397 } else if (type == SSL_FILETYPE_PEM) {
399 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
400 ctx->default_passwd_callback_userdata);
402 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
407 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
411 ret = SSL_CTX_use_certificate(ctx, x);
418 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
423 x = d2i_X509(NULL, &d, (long)len);
425 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
429 ret = SSL_CTX_use_certificate(ctx, x);
434 #ifndef OPENSSL_NO_RSA
435 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
441 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
444 if ((pkey = EVP_PKEY_new()) == NULL) {
445 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
450 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
456 ret = ssl_set_pkey(ctx->cert, pkey);
461 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
467 in = BIO_new(BIO_s_file());
469 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
473 if (BIO_read_filename(in, file) <= 0) {
474 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
477 if (type == SSL_FILETYPE_ASN1) {
479 rsa = d2i_RSAPrivateKey_bio(in, NULL);
480 } else if (type == SSL_FILETYPE_PEM) {
482 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
483 ctx->default_passwd_callback,
484 ctx->default_passwd_callback_userdata);
486 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
490 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
493 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
500 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
504 const unsigned char *p;
508 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
509 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
513 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
517 #endif /* !OPENSSL_NO_RSA */
519 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
522 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
525 return (ssl_set_pkey(ctx->cert, pkey));
528 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
532 EVP_PKEY *pkey = NULL;
534 in = BIO_new(BIO_s_file());
536 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
540 if (BIO_read_filename(in, file) <= 0) {
541 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
544 if (type == SSL_FILETYPE_PEM) {
546 pkey = PEM_read_bio_PrivateKey(in, NULL,
547 ctx->default_passwd_callback,
548 ctx->default_passwd_callback_userdata);
549 } else if (type == SSL_FILETYPE_ASN1) {
551 pkey = d2i_PrivateKey_bio(in, NULL);
553 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
557 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
560 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
567 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
568 const unsigned char *d, long len)
571 const unsigned char *p;
575 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
576 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
580 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
586 * Read a file that contains our certificate in "PEM" format, possibly
587 * followed by a sequence of CA certificates that should be sent to the peer
588 * in the Certificate message.
590 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
595 pem_password_cb *passwd_callback;
596 void *passwd_callback_userdata;
598 ERR_clear_error(); /* clear error stack for
599 * SSL_CTX_use_certificate() */
602 passwd_callback = ctx->default_passwd_callback;
603 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
605 passwd_callback = ssl->default_passwd_callback;
606 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
609 in = BIO_new(BIO_s_file());
611 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
615 if (BIO_read_filename(in, file) <= 0) {
616 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
620 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
621 passwd_callback_userdata);
623 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
628 ret = SSL_CTX_use_certificate(ctx, x);
630 ret = SSL_use_certificate(ssl, x);
632 if (ERR_peek_error() != 0)
633 ret = 0; /* Key/certificate mismatch doesn't imply
637 * If we could set up our certificate, now proceed to the CA
645 r = SSL_CTX_clear_chain_certs(ctx);
647 r = SSL_clear_chain_certs(ssl);
654 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
655 passwd_callback_userdata))
658 r = SSL_CTX_add0_chain_cert(ctx, ca);
660 r = SSL_add0_chain_cert(ssl, ca);
662 * Note that we must not free ca if it was successfully added to
663 * the chain (while we must free the main certificate, since its
664 * reference count is increased by SSL_CTX_use_certificate).
672 /* When the while loop ends, it's usually just EOF. */
673 err = ERR_peek_last_error();
674 if (ERR_GET_LIB(err) == ERR_LIB_PEM
675 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
678 ret = 0; /* some real error */
687 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
689 return use_certificate_chain_file(ctx, NULL, file);
692 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
694 return use_certificate_chain_file(NULL, ssl, file);
697 static int serverinfo_find_extension(const unsigned char *serverinfo,
698 size_t serverinfo_length,
699 unsigned int extension_type,
700 const unsigned char **extension_data,
701 size_t *extension_length)
705 *extension_data = NULL;
706 *extension_length = 0;
707 if (serverinfo == NULL || serverinfo_length == 0)
710 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
714 unsigned int type = 0;
715 unsigned long context = 0;
717 /* end of serverinfo */
718 if (PACKET_remaining(&pkt) == 0)
719 return 0; /* Extension not found */
721 if (!PACKET_get_net_4(&pkt, &context)
722 || !PACKET_get_net_2(&pkt, &type)
723 || !PACKET_get_length_prefixed_2(&pkt, &data))
726 if (type == extension_type) {
727 *extension_data = PACKET_data(&data);
728 *extension_length = PACKET_remaining(&data);;
729 return 1; /* Success */
735 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
736 unsigned int context,
737 const unsigned char *in,
738 size_t inlen, X509 *x, size_t chainidx,
743 *al = SSL_AD_DECODE_ERROR;
750 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
751 const unsigned char *in,
752 size_t inlen, int *al, void *arg)
754 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
758 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
759 unsigned int context,
760 const unsigned char **out,
761 size_t *outlen, X509 *x, size_t chainidx,
764 const unsigned char *serverinfo = NULL;
765 size_t serverinfo_length = 0;
767 /* We only support extensions for the first Certificate */
768 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
771 /* Is there serverinfo data for the chosen server cert? */
772 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
773 &serverinfo_length)) != 0) {
774 /* Find the relevant extension from the serverinfo */
775 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
776 ext_type, out, outlen);
778 *al = SSL_AD_DECODE_ERROR;
779 return -1; /* Error */
782 return 0; /* No extension found, don't send extension */
783 return 1; /* Send extension */
785 return 0; /* No serverinfo data found, don't send
789 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
790 const unsigned char **out, size_t *outlen,
793 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
798 * With a NULL context, this function just checks that the serverinfo data
799 * parses correctly. With a non-NULL context, it registers callbacks for
800 * the included extensions.
802 static int serverinfo_process_buffer(unsigned int version,
803 const unsigned char *serverinfo,
804 size_t serverinfo_length, SSL_CTX *ctx)
808 if (serverinfo == NULL || serverinfo_length == 0)
811 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
814 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
817 while (PACKET_remaining(&pkt)) {
818 unsigned long context = 0;
819 unsigned int ext_type = 0;
822 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
823 || !PACKET_get_net_2(&pkt, &ext_type)
824 || !PACKET_get_length_prefixed_2(&pkt, &data))
831 * The old style custom extensions API could be set separately for
832 * server/client, i.e. you could set one custom extension for a client,
833 * and *for the same extension in the same SSL_CTX* you could set a
834 * custom extension for the server as well. It seems quite weird to be
835 * setting a custom extension for both client and server in a single
836 * SSL_CTX - but theoretically possible. This isn't possible in the
837 * new API. Therefore, if we have V1 serverinfo we use the old API. We
838 * also use the old API even if we have V2 serverinfo but the context
839 * looks like an old style <= TLSv1.2 extension.
841 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
842 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
843 serverinfo_srv_add_cb,
845 serverinfo_srv_parse_cb,
849 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
850 serverinfoex_srv_add_cb,
852 serverinfoex_srv_parse_cb,
861 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
862 const unsigned char *serverinfo,
863 size_t serverinfo_length)
865 unsigned char *new_serverinfo;
867 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
868 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
871 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
873 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
876 if (ctx->cert->key == NULL) {
877 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
880 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
882 if (new_serverinfo == NULL) {
883 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
886 ctx->cert->key->serverinfo = new_serverinfo;
887 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
888 ctx->cert->key->serverinfo_length = serverinfo_length;
891 * Now that the serverinfo is validated and stored, go ahead and
892 * register callbacks.
894 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
896 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
902 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
903 size_t serverinfo_length)
905 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
909 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
911 unsigned char *serverinfo = NULL;
913 size_t serverinfo_length = 0;
914 unsigned char *extension = 0;
915 long extension_length = 0;
918 char namePrefix1[] = "SERVERINFO FOR ";
919 char namePrefix2[] = "SERVERINFOV2 FOR ";
922 size_t num_extensions = 0, contextoff = 0;
924 if (ctx == NULL || file == NULL) {
925 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
929 bin = BIO_new(BIO_s_file());
931 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
934 if (BIO_read_filename(bin, file) <= 0) {
935 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
939 for (num_extensions = 0;; num_extensions++) {
940 unsigned int version;
942 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
945 * There must be at least one extension in this file
947 if (num_extensions == 0) {
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
949 SSL_R_NO_PEM_EXTENSIONS);
951 } else /* End of file, we're done */
954 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
955 if (strlen(name) < strlen(namePrefix1)) {
956 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
959 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
960 version = SSL_SERVERINFOV1;
962 if (strlen(name) < strlen(namePrefix2)) {
963 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
964 SSL_R_PEM_NAME_TOO_SHORT);
967 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
968 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
969 SSL_R_PEM_NAME_BAD_PREFIX);
972 version = SSL_SERVERINFOV2;
975 * Check that the decoded PEM data is plausible (valid length field)
977 if (version == SSL_SERVERINFOV1) {
978 /* 4 byte header: 2 bytes type, 2 bytes len */
979 if (extension_length < 4
980 || (extension[2] << 8) + extension[3]
981 != extension_length - 4) {
982 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
986 * File does not have a context value so we must take account of
991 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
992 if (extension_length < 8
993 || (extension[6] << 8) + extension[7]
994 != extension_length - 8) {
995 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
999 /* Append the decoded extension to the serverinfo buffer */
1000 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1003 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1007 if (contextoff > 0) {
1008 unsigned char *sinfo = serverinfo + serverinfo_length;
1010 /* We know this only uses the last 2 bytes */
1013 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1014 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1016 memcpy(serverinfo + serverinfo_length + contextoff,
1017 extension, extension_length);
1018 serverinfo_length += extension_length + contextoff;
1022 OPENSSL_free(header);
1024 OPENSSL_free(extension);
1028 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1031 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1033 OPENSSL_free(header);
1034 OPENSSL_free(extension);
1035 OPENSSL_free(serverinfo);