2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "ssl_local.h"
12 #include "internal/packet.h"
13 #include <openssl/bio.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pem.h>
22 static int ssl_set_cert(CERT *c, X509 *x509);
23 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
25 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
26 | SSL_EXT_CLIENT_HELLO \
27 | SSL_EXT_TLS1_2_SERVER_HELLO \
28 | SSL_EXT_IGNORE_ON_RESUMPTION)
30 int SSL_use_certificate(SSL *ssl, X509 *x)
34 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
37 if (!X509v3_cache_extensions(x, ssl->ctx->libctx, ssl->ctx->propq)) {
38 SSLerr(0, ERR_LIB_X509);
41 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
43 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
47 return ssl_set_cert(ssl->cert, x);
50 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
57 in = BIO_new(BIO_s_file());
59 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
63 if (BIO_read_filename(in, file) <= 0) {
64 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
67 if (type == SSL_FILETYPE_ASN1) {
69 x = d2i_X509_bio(in, NULL);
70 } else if (type == SSL_FILETYPE_PEM) {
72 x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
73 ssl->default_passwd_callback_userdata);
75 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
80 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
84 ret = SSL_use_certificate(ssl, x);
91 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
96 x = d2i_X509(NULL, &d, (long)len);
98 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
102 ret = SSL_use_certificate(ssl, x);
107 #ifndef OPENSSL_NO_RSA
108 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
114 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
117 if ((pkey = EVP_PKEY_new()) == NULL) {
118 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
123 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
129 ret = ssl_set_pkey(ssl->cert, pkey);
135 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
139 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
140 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
144 if (c->pkeys[i].x509 != NULL) {
146 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
148 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
152 * The return code from EVP_PKEY_copy_parameters is deliberately
153 * ignored. Some EVP_PKEY types cannot do this.
155 EVP_PKEY_copy_parameters(pktmp, pkey);
158 #ifndef OPENSSL_NO_RSA
160 * Don't check the public/private key, this is mostly for smart
163 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
164 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
167 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
168 X509_free(c->pkeys[i].x509);
169 c->pkeys[i].x509 = NULL;
174 EVP_PKEY_free(c->pkeys[i].privatekey);
175 EVP_PKEY_up_ref(pkey);
176 c->pkeys[i].privatekey = pkey;
177 c->key = &c->pkeys[i];
181 #ifndef OPENSSL_NO_RSA
182 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
188 in = BIO_new(BIO_s_file());
190 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
194 if (BIO_read_filename(in, file) <= 0) {
195 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
198 if (type == SSL_FILETYPE_ASN1) {
200 rsa = d2i_RSAPrivateKey_bio(in, NULL);
201 } else if (type == SSL_FILETYPE_PEM) {
203 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
204 ssl->default_passwd_callback,
205 ssl->default_passwd_callback_userdata);
207 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
211 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
214 ret = SSL_use_RSAPrivateKey(ssl, rsa);
221 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
224 const unsigned char *p;
228 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
229 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
233 ret = SSL_use_RSAPrivateKey(ssl, rsa);
237 #endif /* !OPENSSL_NO_RSA */
239 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
244 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
247 ret = ssl_set_pkey(ssl->cert, pkey);
251 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
255 EVP_PKEY *pkey = NULL;
257 in = BIO_new(BIO_s_file());
259 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
263 if (BIO_read_filename(in, file) <= 0) {
264 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
267 if (type == SSL_FILETYPE_PEM) {
269 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
270 ssl->default_passwd_callback,
271 ssl->default_passwd_callback_userdata,
274 } else if (type == SSL_FILETYPE_ASN1) {
276 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
279 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
283 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
286 ret = SSL_use_PrivateKey(ssl, pkey);
293 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
297 const unsigned char *p;
301 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
302 ssl->ctx->propq)) == NULL) {
303 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
307 ret = SSL_use_PrivateKey(ssl, pkey);
312 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
316 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
319 if (!X509v3_cache_extensions(x, ctx->libctx, ctx->propq)) {
320 SSLerr(0, ERR_LIB_X509);
323 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
325 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
328 return ssl_set_cert(ctx->cert, x);
331 static int ssl_set_cert(CERT *c, X509 *x)
336 pkey = X509_get0_pubkey(x);
338 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
342 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
343 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
346 #ifndef OPENSSL_NO_EC
347 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
348 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
352 if (c->pkeys[i].privatekey != NULL) {
354 * The return code from EVP_PKEY_copy_parameters is deliberately
355 * ignored. Some EVP_PKEY types cannot do this.
357 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
360 #ifndef OPENSSL_NO_RSA
362 * Don't check the public/private key, this is mostly for smart
365 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
366 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
367 RSA_METHOD_FLAG_NO_CHECK) ;
369 #endif /* OPENSSL_NO_RSA */
370 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
372 * don't fail for a cert/key mismatch, just free current private
373 * key (when switching to a different cert & key, first this
374 * function should be used, then ssl_set_pkey
376 EVP_PKEY_free(c->pkeys[i].privatekey);
377 c->pkeys[i].privatekey = NULL;
378 /* clear error queue */
383 X509_free(c->pkeys[i].x509);
385 c->pkeys[i].x509 = x;
386 c->key = &(c->pkeys[i]);
391 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
398 in = BIO_new(BIO_s_file());
400 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
404 if (BIO_read_filename(in, file) <= 0) {
405 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
408 if (type == SSL_FILETYPE_ASN1) {
410 x = d2i_X509_bio(in, NULL);
411 } else if (type == SSL_FILETYPE_PEM) {
413 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
414 ctx->default_passwd_callback_userdata);
416 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
421 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
425 ret = SSL_CTX_use_certificate(ctx, x);
432 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
437 x = d2i_X509(NULL, &d, (long)len);
439 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
443 ret = SSL_CTX_use_certificate(ctx, x);
448 #ifndef OPENSSL_NO_RSA
449 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
455 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
458 if ((pkey = EVP_PKEY_new()) == NULL) {
459 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
464 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
470 ret = ssl_set_pkey(ctx->cert, pkey);
475 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
481 in = BIO_new(BIO_s_file());
483 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
487 if (BIO_read_filename(in, file) <= 0) {
488 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
491 if (type == SSL_FILETYPE_ASN1) {
493 rsa = d2i_RSAPrivateKey_bio(in, NULL);
494 } else if (type == SSL_FILETYPE_PEM) {
496 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
497 ctx->default_passwd_callback,
498 ctx->default_passwd_callback_userdata);
500 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
504 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
507 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
514 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
518 const unsigned char *p;
522 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
523 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
527 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
531 #endif /* !OPENSSL_NO_RSA */
533 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
536 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
539 return ssl_set_pkey(ctx->cert, pkey);
542 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
546 EVP_PKEY *pkey = NULL;
548 in = BIO_new(BIO_s_file());
550 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
554 if (BIO_read_filename(in, file) <= 0) {
555 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
558 if (type == SSL_FILETYPE_PEM) {
560 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
561 ctx->default_passwd_callback,
562 ctx->default_passwd_callback_userdata,
563 ctx->libctx, ctx->propq);
564 } else if (type == SSL_FILETYPE_ASN1) {
566 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
568 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
572 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
575 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
582 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
583 const unsigned char *d, long len)
586 const unsigned char *p;
590 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
591 ctx->propq)) == NULL) {
592 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
596 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
602 * Read a file that contains our certificate in "PEM" format, possibly
603 * followed by a sequence of CA certificates that should be sent to the peer
604 * in the Certificate message.
606 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
611 pem_password_cb *passwd_callback;
612 void *passwd_callback_userdata;
614 ERR_clear_error(); /* clear error stack for
615 * SSL_CTX_use_certificate() */
618 passwd_callback = ctx->default_passwd_callback;
619 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
621 passwd_callback = ssl->default_passwd_callback;
622 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
625 in = BIO_new(BIO_s_file());
627 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
631 if (BIO_read_filename(in, file) <= 0) {
632 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
636 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
637 passwd_callback_userdata);
639 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
644 ret = SSL_CTX_use_certificate(ctx, x);
646 ret = SSL_use_certificate(ssl, x);
648 if (ERR_peek_error() != 0)
649 ret = 0; /* Key/certificate mismatch doesn't imply
653 * If we could set up our certificate, now proceed to the CA
661 r = SSL_CTX_clear_chain_certs(ctx);
663 r = SSL_clear_chain_certs(ssl);
670 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
671 passwd_callback_userdata))
674 r = SSL_CTX_add0_chain_cert(ctx, ca);
676 r = SSL_add0_chain_cert(ssl, ca);
678 * Note that we must not free ca if it was successfully added to
679 * the chain (while we must free the main certificate, since its
680 * reference count is increased by SSL_CTX_use_certificate).
688 /* When the while loop ends, it's usually just EOF. */
689 err = ERR_peek_last_error();
690 if (ERR_GET_LIB(err) == ERR_LIB_PEM
691 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
694 ret = 0; /* some real error */
703 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
705 return use_certificate_chain_file(ctx, NULL, file);
708 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
710 return use_certificate_chain_file(NULL, ssl, file);
713 static int serverinfo_find_extension(const unsigned char *serverinfo,
714 size_t serverinfo_length,
715 unsigned int extension_type,
716 const unsigned char **extension_data,
717 size_t *extension_length)
721 *extension_data = NULL;
722 *extension_length = 0;
723 if (serverinfo == NULL || serverinfo_length == 0)
726 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
730 unsigned int type = 0;
731 unsigned long context = 0;
733 /* end of serverinfo */
734 if (PACKET_remaining(&pkt) == 0)
735 return 0; /* Extension not found */
737 if (!PACKET_get_net_4(&pkt, &context)
738 || !PACKET_get_net_2(&pkt, &type)
739 || !PACKET_get_length_prefixed_2(&pkt, &data))
742 if (type == extension_type) {
743 *extension_data = PACKET_data(&data);
744 *extension_length = PACKET_remaining(&data);;
745 return 1; /* Success */
751 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
752 unsigned int context,
753 const unsigned char *in,
754 size_t inlen, X509 *x, size_t chainidx,
759 *al = SSL_AD_DECODE_ERROR;
766 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
767 const unsigned char *in,
768 size_t inlen, int *al, void *arg)
770 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
774 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
775 unsigned int context,
776 const unsigned char **out,
777 size_t *outlen, X509 *x, size_t chainidx,
780 const unsigned char *serverinfo = NULL;
781 size_t serverinfo_length = 0;
783 /* We only support extensions for the first Certificate */
784 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
787 /* Is there serverinfo data for the chosen server cert? */
788 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
789 &serverinfo_length)) != 0) {
790 /* Find the relevant extension from the serverinfo */
791 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
792 ext_type, out, outlen);
794 *al = SSL_AD_INTERNAL_ERROR;
795 return -1; /* Error */
798 return 0; /* No extension found, don't send extension */
799 return 1; /* Send extension */
801 return 0; /* No serverinfo data found, don't send
805 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
806 const unsigned char **out, size_t *outlen,
809 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
814 * With a NULL context, this function just checks that the serverinfo data
815 * parses correctly. With a non-NULL context, it registers callbacks for
816 * the included extensions.
818 static int serverinfo_process_buffer(unsigned int version,
819 const unsigned char *serverinfo,
820 size_t serverinfo_length, SSL_CTX *ctx)
824 if (serverinfo == NULL || serverinfo_length == 0)
827 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
830 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
833 while (PACKET_remaining(&pkt)) {
834 unsigned long context = 0;
835 unsigned int ext_type = 0;
838 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
839 || !PACKET_get_net_2(&pkt, &ext_type)
840 || !PACKET_get_length_prefixed_2(&pkt, &data))
847 * The old style custom extensions API could be set separately for
848 * server/client, i.e. you could set one custom extension for a client,
849 * and *for the same extension in the same SSL_CTX* you could set a
850 * custom extension for the server as well. It seems quite weird to be
851 * setting a custom extension for both client and server in a single
852 * SSL_CTX - but theoretically possible. This isn't possible in the
853 * new API. Therefore, if we have V1 serverinfo we use the old API. We
854 * also use the old API even if we have V2 serverinfo but the context
855 * looks like an old style <= TLSv1.2 extension.
857 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
858 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
859 serverinfo_srv_add_cb,
861 serverinfo_srv_parse_cb,
865 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
866 serverinfoex_srv_add_cb,
868 serverinfoex_srv_parse_cb,
877 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
878 const unsigned char *serverinfo,
879 size_t serverinfo_length)
881 unsigned char *new_serverinfo;
883 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
884 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
887 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
889 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
892 if (ctx->cert->key == NULL) {
893 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
896 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
898 if (new_serverinfo == NULL) {
899 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
902 ctx->cert->key->serverinfo = new_serverinfo;
903 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
904 ctx->cert->key->serverinfo_length = serverinfo_length;
907 * Now that the serverinfo is validated and stored, go ahead and
908 * register callbacks.
910 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
912 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
918 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
919 size_t serverinfo_length)
921 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
925 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
927 unsigned char *serverinfo = NULL;
929 size_t serverinfo_length = 0;
930 unsigned char *extension = 0;
931 long extension_length = 0;
934 static const char namePrefix1[] = "SERVERINFO FOR ";
935 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
936 unsigned int name_len;
939 size_t num_extensions = 0, contextoff = 0;
941 if (ctx == NULL || file == NULL) {
942 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
946 bin = BIO_new(BIO_s_file());
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
951 if (BIO_read_filename(bin, file) <= 0) {
952 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
956 for (num_extensions = 0;; num_extensions++) {
957 unsigned int version;
959 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
962 * There must be at least one extension in this file
964 if (num_extensions == 0) {
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
966 SSL_R_NO_PEM_EXTENSIONS);
968 } else /* End of file, we're done */
971 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
972 name_len = strlen(name);
973 if (name_len < sizeof(namePrefix1) - 1) {
974 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
977 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
978 version = SSL_SERVERINFOV1;
980 if (name_len < sizeof(namePrefix2) - 1) {
981 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
982 SSL_R_PEM_NAME_TOO_SHORT);
985 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
986 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
987 SSL_R_PEM_NAME_BAD_PREFIX);
990 version = SSL_SERVERINFOV2;
993 * Check that the decoded PEM data is plausible (valid length field)
995 if (version == SSL_SERVERINFOV1) {
996 /* 4 byte header: 2 bytes type, 2 bytes len */
997 if (extension_length < 4
998 || (extension[2] << 8) + extension[3]
999 != extension_length - 4) {
1000 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1004 * File does not have a context value so we must take account of
1009 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
1010 if (extension_length < 8
1011 || (extension[6] << 8) + extension[7]
1012 != extension_length - 8) {
1013 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1017 /* Append the decoded extension to the serverinfo buffer */
1018 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1021 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1025 if (contextoff > 0) {
1026 unsigned char *sinfo = serverinfo + serverinfo_length;
1028 /* We know this only uses the last 2 bytes */
1031 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1032 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1034 memcpy(serverinfo + serverinfo_length + contextoff,
1035 extension, extension_length);
1036 serverinfo_length += extension_length + contextoff;
1040 OPENSSL_free(header);
1042 OPENSSL_free(extension);
1046 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1049 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1051 OPENSSL_free(header);
1052 OPENSSL_free(extension);
1053 OPENSSL_free(serverinfo);
1058 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1059 STACK_OF(X509) *chain, int override)
1065 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1066 SSL_CTX *actualctx = ssl == NULL ? ctx : ssl->ctx;
1067 STACK_OF(X509) *dup_chain = NULL;
1068 EVP_PKEY *pubkey = NULL;
1070 if (!X509v3_cache_extensions(x509, actualctx->libctx, actualctx->propq)) {
1071 SSLerr(0, ERR_R_X509_LIB);
1075 /* Do all security checks before anything else */
1076 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1078 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1081 for (j = 0; j < sk_X509_num(chain); j++) {
1082 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1084 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1089 pubkey = X509_get_pubkey(x509); /* bumps reference */
1092 if (privatekey == NULL) {
1093 privatekey = pubkey;
1095 /* For RSA, which has no parameters, missing returns 0 */
1096 if (EVP_PKEY_missing_parameters(privatekey)) {
1097 if (EVP_PKEY_missing_parameters(pubkey)) {
1098 /* nobody has parameters? - error */
1099 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
1102 /* copy to privatekey from pubkey */
1103 EVP_PKEY_copy_parameters(privatekey, pubkey);
1105 } else if (EVP_PKEY_missing_parameters(pubkey)) {
1106 /* copy to pubkey from privatekey */
1107 EVP_PKEY_copy_parameters(pubkey, privatekey);
1108 } /* else both have parameters */
1110 /* Copied from ssl_set_cert/pkey */
1111 #ifndef OPENSSL_NO_RSA
1112 if ((EVP_PKEY_id(privatekey) == EVP_PKEY_RSA) &&
1113 ((RSA_flags(EVP_PKEY_get0_RSA(privatekey)) & RSA_METHOD_FLAG_NO_CHECK)))
1117 /* check that key <-> cert match */
1118 if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
1119 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
1123 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1124 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1128 if (!override && (c->pkeys[i].x509 != NULL
1129 || c->pkeys[i].privatekey != NULL
1130 || c->pkeys[i].chain != NULL)) {
1131 /* No override, and something already there */
1132 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
1136 if (chain != NULL) {
1137 dup_chain = X509_chain_up_ref(chain);
1138 if (dup_chain == NULL) {
1139 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
1144 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1145 c->pkeys[i].chain = dup_chain;
1147 X509_free(c->pkeys[i].x509);
1149 c->pkeys[i].x509 = x509;
1151 EVP_PKEY_free(c->pkeys[i].privatekey);
1152 EVP_PKEY_up_ref(privatekey);
1153 c->pkeys[i].privatekey = privatekey;
1155 c->key = &(c->pkeys[i]);
1159 EVP_PKEY_free(pubkey);
1163 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1164 STACK_OF(X509) *chain, int override)
1166 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1169 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1170 STACK_OF(X509) *chain, int override)
1172 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);