2 * Copyright 1995-2020 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
11 #include "ssl_local.h"
12 #include "packet_local.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 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
152 X509_free(c->pkeys[i].x509);
153 c->pkeys[i].x509 = NULL;
158 EVP_PKEY_free(c->pkeys[i].privatekey);
159 EVP_PKEY_up_ref(pkey);
160 c->pkeys[i].privatekey = pkey;
161 c->key = &c->pkeys[i];
165 #ifndef OPENSSL_NO_RSA
166 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
172 in = BIO_new(BIO_s_file());
174 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
178 if (BIO_read_filename(in, file) <= 0) {
179 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
182 if (type == SSL_FILETYPE_ASN1) {
184 rsa = d2i_RSAPrivateKey_bio(in, NULL);
185 } else if (type == SSL_FILETYPE_PEM) {
187 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
188 ssl->default_passwd_callback,
189 ssl->default_passwd_callback_userdata);
191 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
195 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
198 ret = SSL_use_RSAPrivateKey(ssl, rsa);
205 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
208 const unsigned char *p;
212 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
213 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
217 ret = SSL_use_RSAPrivateKey(ssl, rsa);
221 #endif /* !OPENSSL_NO_RSA */
223 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
228 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
231 ret = ssl_set_pkey(ssl->cert, pkey);
235 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
239 EVP_PKEY *pkey = NULL;
241 in = BIO_new(BIO_s_file());
243 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
247 if (BIO_read_filename(in, file) <= 0) {
248 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
251 if (type == SSL_FILETYPE_PEM) {
253 pkey = PEM_read_bio_PrivateKey(in, NULL,
254 ssl->default_passwd_callback,
255 ssl->default_passwd_callback_userdata);
256 } else if (type == SSL_FILETYPE_ASN1) {
258 pkey = d2i_PrivateKey_bio(in, NULL);
260 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
264 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
267 ret = SSL_use_PrivateKey(ssl, pkey);
274 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
278 const unsigned char *p;
282 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
283 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
287 ret = SSL_use_PrivateKey(ssl, pkey);
292 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
296 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
299 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
301 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
304 return ssl_set_cert(ctx->cert, x);
307 static int ssl_set_cert(CERT *c, X509 *x)
312 pkey = X509_get0_pubkey(x);
314 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
318 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
319 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
322 #ifndef OPENSSL_NO_EC
323 if (i == SSL_PKEY_ECC && !EC_KEY_can_sign(EVP_PKEY_get0_EC_KEY(pkey))) {
324 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
328 if (c->pkeys[i].privatekey != NULL) {
330 * The return code from EVP_PKEY_copy_parameters is deliberately
331 * ignored. Some EVP_PKEY types cannot do this.
333 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
336 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
338 * don't fail for a cert/key mismatch, just free current private
339 * key (when switching to a different cert & key, first this
340 * function should be used, then ssl_set_pkey
342 EVP_PKEY_free(c->pkeys[i].privatekey);
343 c->pkeys[i].privatekey = NULL;
344 /* clear error queue */
349 X509_free(c->pkeys[i].x509);
351 c->pkeys[i].x509 = x;
352 c->key = &(c->pkeys[i]);
357 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
364 in = BIO_new(BIO_s_file());
366 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
370 if (BIO_read_filename(in, file) <= 0) {
371 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
374 if (type == SSL_FILETYPE_ASN1) {
376 x = d2i_X509_bio(in, NULL);
377 } else if (type == SSL_FILETYPE_PEM) {
379 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
380 ctx->default_passwd_callback_userdata);
382 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
387 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
391 ret = SSL_CTX_use_certificate(ctx, x);
398 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
403 x = d2i_X509(NULL, &d, (long)len);
405 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
409 ret = SSL_CTX_use_certificate(ctx, x);
414 #ifndef OPENSSL_NO_RSA
415 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
421 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
424 if ((pkey = EVP_PKEY_new()) == NULL) {
425 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
430 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
436 ret = ssl_set_pkey(ctx->cert, pkey);
441 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
447 in = BIO_new(BIO_s_file());
449 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
453 if (BIO_read_filename(in, file) <= 0) {
454 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
457 if (type == SSL_FILETYPE_ASN1) {
459 rsa = d2i_RSAPrivateKey_bio(in, NULL);
460 } else if (type == SSL_FILETYPE_PEM) {
462 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
463 ctx->default_passwd_callback,
464 ctx->default_passwd_callback_userdata);
466 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
470 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
473 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
480 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
484 const unsigned char *p;
488 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
489 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
493 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
497 #endif /* !OPENSSL_NO_RSA */
499 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
502 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
505 return ssl_set_pkey(ctx->cert, pkey);
508 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
512 EVP_PKEY *pkey = NULL;
514 in = BIO_new(BIO_s_file());
516 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
520 if (BIO_read_filename(in, file) <= 0) {
521 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
524 if (type == SSL_FILETYPE_PEM) {
526 pkey = PEM_read_bio_PrivateKey(in, NULL,
527 ctx->default_passwd_callback,
528 ctx->default_passwd_callback_userdata);
529 } else if (type == SSL_FILETYPE_ASN1) {
531 pkey = d2i_PrivateKey_bio(in, NULL);
533 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
537 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
540 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
547 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
548 const unsigned char *d, long len)
551 const unsigned char *p;
555 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
556 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
560 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
566 * Read a file that contains our certificate in "PEM" format, possibly
567 * followed by a sequence of CA certificates that should be sent to the peer
568 * in the Certificate message.
570 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
575 pem_password_cb *passwd_callback;
576 void *passwd_callback_userdata;
578 ERR_clear_error(); /* clear error stack for
579 * SSL_CTX_use_certificate() */
582 passwd_callback = ctx->default_passwd_callback;
583 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
585 passwd_callback = ssl->default_passwd_callback;
586 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
589 in = BIO_new(BIO_s_file());
591 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
595 if (BIO_read_filename(in, file) <= 0) {
596 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
600 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
601 passwd_callback_userdata);
603 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
608 ret = SSL_CTX_use_certificate(ctx, x);
610 ret = SSL_use_certificate(ssl, x);
612 if (ERR_peek_error() != 0)
613 ret = 0; /* Key/certificate mismatch doesn't imply
617 * If we could set up our certificate, now proceed to the CA
625 r = SSL_CTX_clear_chain_certs(ctx);
627 r = SSL_clear_chain_certs(ssl);
634 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
635 passwd_callback_userdata))
638 r = SSL_CTX_add0_chain_cert(ctx, ca);
640 r = SSL_add0_chain_cert(ssl, ca);
642 * Note that we must not free ca if it was successfully added to
643 * the chain (while we must free the main certificate, since its
644 * reference count is increased by SSL_CTX_use_certificate).
652 /* When the while loop ends, it's usually just EOF. */
653 err = ERR_peek_last_error();
654 if (ERR_GET_LIB(err) == ERR_LIB_PEM
655 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
658 ret = 0; /* some real error */
667 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
669 return use_certificate_chain_file(ctx, NULL, file);
672 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
674 return use_certificate_chain_file(NULL, ssl, file);
677 static int serverinfo_find_extension(const unsigned char *serverinfo,
678 size_t serverinfo_length,
679 unsigned int extension_type,
680 const unsigned char **extension_data,
681 size_t *extension_length)
685 *extension_data = NULL;
686 *extension_length = 0;
687 if (serverinfo == NULL || serverinfo_length == 0)
690 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
694 unsigned int type = 0;
695 unsigned long context = 0;
697 /* end of serverinfo */
698 if (PACKET_remaining(&pkt) == 0)
699 return 0; /* Extension not found */
701 if (!PACKET_get_net_4(&pkt, &context)
702 || !PACKET_get_net_2(&pkt, &type)
703 || !PACKET_get_length_prefixed_2(&pkt, &data))
706 if (type == extension_type) {
707 *extension_data = PACKET_data(&data);
708 *extension_length = PACKET_remaining(&data);;
709 return 1; /* Success */
715 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
716 unsigned int context,
717 const unsigned char *in,
718 size_t inlen, X509 *x, size_t chainidx,
723 *al = SSL_AD_DECODE_ERROR;
730 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
731 const unsigned char *in,
732 size_t inlen, int *al, void *arg)
734 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
738 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
739 unsigned int context,
740 const unsigned char **out,
741 size_t *outlen, X509 *x, size_t chainidx,
744 const unsigned char *serverinfo = NULL;
745 size_t serverinfo_length = 0;
747 /* We only support extensions for the first Certificate */
748 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
751 /* Is there serverinfo data for the chosen server cert? */
752 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
753 &serverinfo_length)) != 0) {
754 /* Find the relevant extension from the serverinfo */
755 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
756 ext_type, out, outlen);
758 *al = SSL_AD_INTERNAL_ERROR;
759 return -1; /* Error */
762 return 0; /* No extension found, don't send extension */
763 return 1; /* Send extension */
765 return 0; /* No serverinfo data found, don't send
769 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
770 const unsigned char **out, size_t *outlen,
773 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
778 * With a NULL context, this function just checks that the serverinfo data
779 * parses correctly. With a non-NULL context, it registers callbacks for
780 * the included extensions.
782 static int serverinfo_process_buffer(unsigned int version,
783 const unsigned char *serverinfo,
784 size_t serverinfo_length, SSL_CTX *ctx)
788 if (serverinfo == NULL || serverinfo_length == 0)
791 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
794 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
797 while (PACKET_remaining(&pkt)) {
798 unsigned long context = 0;
799 unsigned int ext_type = 0;
802 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
803 || !PACKET_get_net_2(&pkt, &ext_type)
804 || !PACKET_get_length_prefixed_2(&pkt, &data))
811 * The old style custom extensions API could be set separately for
812 * server/client, i.e. you could set one custom extension for a client,
813 * and *for the same extension in the same SSL_CTX* you could set a
814 * custom extension for the server as well. It seems quite weird to be
815 * setting a custom extension for both client and server in a single
816 * SSL_CTX - but theoretically possible. This isn't possible in the
817 * new API. Therefore, if we have V1 serverinfo we use the old API. We
818 * also use the old API even if we have V2 serverinfo but the context
819 * looks like an old style <= TLSv1.2 extension.
821 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
822 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
823 serverinfo_srv_add_cb,
825 serverinfo_srv_parse_cb,
829 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
830 serverinfoex_srv_add_cb,
832 serverinfoex_srv_parse_cb,
841 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
842 const unsigned char *serverinfo,
843 size_t serverinfo_length)
845 unsigned char *new_serverinfo;
847 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
848 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
851 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
853 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
856 if (ctx->cert->key == NULL) {
857 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
860 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
862 if (new_serverinfo == NULL) {
863 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
866 ctx->cert->key->serverinfo = new_serverinfo;
867 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
868 ctx->cert->key->serverinfo_length = serverinfo_length;
871 * Now that the serverinfo is validated and stored, go ahead and
872 * register callbacks.
874 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
876 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
882 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
883 size_t serverinfo_length)
885 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
889 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
891 unsigned char *serverinfo = NULL;
893 size_t serverinfo_length = 0;
894 unsigned char *extension = 0;
895 long extension_length = 0;
898 char namePrefix1[] = "SERVERINFO FOR ";
899 char namePrefix2[] = "SERVERINFOV2 FOR ";
902 size_t num_extensions = 0, contextoff = 0;
904 if (ctx == NULL || file == NULL) {
905 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
909 bin = BIO_new(BIO_s_file());
911 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
914 if (BIO_read_filename(bin, file) <= 0) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
919 for (num_extensions = 0;; num_extensions++) {
920 unsigned int version;
922 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
925 * There must be at least one extension in this file
927 if (num_extensions == 0) {
928 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
929 SSL_R_NO_PEM_EXTENSIONS);
931 } else /* End of file, we're done */
934 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
935 if (strlen(name) < strlen(namePrefix1)) {
936 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
939 if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
940 version = SSL_SERVERINFOV1;
942 if (strlen(name) < strlen(namePrefix2)) {
943 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
944 SSL_R_PEM_NAME_TOO_SHORT);
947 if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
948 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
949 SSL_R_PEM_NAME_BAD_PREFIX);
952 version = SSL_SERVERINFOV2;
955 * Check that the decoded PEM data is plausible (valid length field)
957 if (version == SSL_SERVERINFOV1) {
958 /* 4 byte header: 2 bytes type, 2 bytes len */
959 if (extension_length < 4
960 || (extension[2] << 8) + extension[3]
961 != extension_length - 4) {
962 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
966 * File does not have a context value so we must take account of
971 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
972 if (extension_length < 8
973 || (extension[6] << 8) + extension[7]
974 != extension_length - 8) {
975 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
979 /* Append the decoded extension to the serverinfo buffer */
980 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
983 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
987 if (contextoff > 0) {
988 unsigned char *sinfo = serverinfo + serverinfo_length;
990 /* We know this only uses the last 2 bytes */
993 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
994 sinfo[3] = SYNTHV1CONTEXT & 0xff;
996 memcpy(serverinfo + serverinfo_length + contextoff,
997 extension, extension_length);
998 serverinfo_length += extension_length + contextoff;
1002 OPENSSL_free(header);
1004 OPENSSL_free(extension);
1008 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1011 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1013 OPENSSL_free(header);
1014 OPENSSL_free(extension);
1015 OPENSSL_free(serverinfo);
1020 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1021 STACK_OF(X509) *chain, int override)
1027 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1028 STACK_OF(X509) *dup_chain = NULL;
1029 EVP_PKEY *pubkey = NULL;
1031 /* Do all security checks before anything else */
1032 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1034 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1037 for (j = 0; j < sk_X509_num(chain); j++) {
1038 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1040 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1045 pubkey = X509_get_pubkey(x509); /* bumps reference */
1048 if (privatekey == NULL) {
1049 privatekey = pubkey;
1051 /* For RSA, which has no parameters, missing returns 0 */
1052 if (EVP_PKEY_missing_parameters(privatekey)) {
1053 if (EVP_PKEY_missing_parameters(pubkey)) {
1054 /* nobody has parameters? - error */
1055 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
1058 /* copy to privatekey from pubkey */
1059 EVP_PKEY_copy_parameters(privatekey, pubkey);
1061 } else if (EVP_PKEY_missing_parameters(pubkey)) {
1062 /* copy to pubkey from privatekey */
1063 EVP_PKEY_copy_parameters(pubkey, privatekey);
1064 } /* else both have parameters */
1066 /* check that key <-> cert match */
1067 if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
1068 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
1072 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1073 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1077 if (!override && (c->pkeys[i].x509 != NULL
1078 || c->pkeys[i].privatekey != NULL
1079 || c->pkeys[i].chain != NULL)) {
1080 /* No override, and something already there */
1081 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
1085 if (chain != NULL) {
1086 dup_chain = X509_chain_up_ref(chain);
1087 if (dup_chain == NULL) {
1088 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
1093 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1094 c->pkeys[i].chain = dup_chain;
1096 X509_free(c->pkeys[i].x509);
1098 c->pkeys[i].x509 = x509;
1100 EVP_PKEY_free(c->pkeys[i].privatekey);
1101 EVP_PKEY_up_ref(privatekey);
1102 c->pkeys[i].privatekey = privatekey;
1104 c->key = &(c->pkeys[i]);
1108 EVP_PKEY_free(pubkey);
1112 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1113 STACK_OF(X509) *chain, int override)
1115 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1118 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1119 STACK_OF(X509) *chain, int override)
1121 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);