2 * Copyright 1995-2022 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>
20 static int ssl_set_cert(CERT *c, X509 *x509);
21 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
23 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
24 | SSL_EXT_CLIENT_HELLO \
25 | SSL_EXT_TLS1_2_SERVER_HELLO \
26 | SSL_EXT_IGNORE_ON_RESUMPTION)
28 #define NAME_PREFIX1 "SERVERINFO FOR "
29 #define NAME_PREFIX2 "SERVERINFOV2 FOR "
31 int SSL_use_certificate(SSL *ssl, X509 *x)
35 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
39 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
41 ERR_raise(ERR_LIB_SSL, rv);
45 return ssl_set_cert(ssl->cert, x);
48 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
53 X509 *cert = NULL, *x = NULL;
55 in = BIO_new(BIO_s_file());
57 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
61 if (BIO_read_filename(in, file) <= 0) {
62 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
66 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
67 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
70 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
72 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
75 if (type == SSL_FILETYPE_ASN1) {
77 cert = d2i_X509_bio(in, &x);
78 } else if (type == SSL_FILETYPE_PEM) {
80 cert = PEM_read_bio_X509(in, &x, ssl->default_passwd_callback,
81 ssl->default_passwd_callback_userdata);
83 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
88 ERR_raise(ERR_LIB_SSL, j);
92 ret = SSL_use_certificate(ssl, x);
99 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
104 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
106 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
110 if (d2i_X509(&x, &d, (long)len)== NULL) {
112 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
116 ret = SSL_use_certificate(ssl, x);
121 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
125 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
126 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
130 if (c->pkeys[i].x509 != NULL
131 && !X509_check_private_key(c->pkeys[i].x509, pkey))
134 EVP_PKEY_free(c->pkeys[i].privatekey);
135 EVP_PKEY_up_ref(pkey);
136 c->pkeys[i].privatekey = pkey;
137 c->key = &c->pkeys[i];
141 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
146 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
149 ret = ssl_set_pkey(ssl->cert, pkey);
153 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
157 EVP_PKEY *pkey = NULL;
159 in = BIO_new(BIO_s_file());
161 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
165 if (BIO_read_filename(in, file) <= 0) {
166 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
169 if (type == SSL_FILETYPE_PEM) {
171 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
172 ssl->default_passwd_callback,
173 ssl->default_passwd_callback_userdata,
176 } else if (type == SSL_FILETYPE_ASN1) {
178 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
181 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
185 ERR_raise(ERR_LIB_SSL, j);
188 ret = SSL_use_PrivateKey(ssl, pkey);
195 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
199 const unsigned char *p;
203 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
204 ssl->ctx->propq)) == NULL) {
205 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
209 ret = SSL_use_PrivateKey(ssl, pkey);
214 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
218 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
222 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
224 ERR_raise(ERR_LIB_SSL, rv);
227 return ssl_set_cert(ctx->cert, x);
230 static int ssl_set_cert(CERT *c, X509 *x)
235 pkey = X509_get0_pubkey(x);
237 ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
241 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
242 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
246 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
247 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
251 if (c->pkeys[i].privatekey != NULL) {
253 * The return code from EVP_PKEY_copy_parameters is deliberately
254 * ignored. Some EVP_PKEY types cannot do this.
255 * coverity[check_return]
257 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
260 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
262 * don't fail for a cert/key mismatch, just free current private
263 * key (when switching to a different cert & key, first this
264 * function should be used, then ssl_set_pkey
266 EVP_PKEY_free(c->pkeys[i].privatekey);
267 c->pkeys[i].privatekey = NULL;
268 /* clear error queue */
273 X509_free(c->pkeys[i].x509);
275 c->pkeys[i].x509 = x;
276 c->key = &(c->pkeys[i]);
281 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
283 int j = SSL_R_BAD_VALUE;
286 X509 *x = NULL, *cert = NULL;
288 in = BIO_new(BIO_s_file());
290 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
294 if (BIO_read_filename(in, file) <= 0) {
295 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
298 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
299 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
302 x = X509_new_ex(ctx->libctx, ctx->propq);
304 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
307 if (type == SSL_FILETYPE_ASN1) {
309 cert = d2i_X509_bio(in, &x);
310 } else if (type == SSL_FILETYPE_PEM) {
312 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
313 ctx->default_passwd_callback_userdata);
316 ERR_raise(ERR_LIB_SSL, j);
320 ret = SSL_CTX_use_certificate(ctx, x);
327 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
332 x = X509_new_ex(ctx->libctx, ctx->propq);
334 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
338 if (d2i_X509(&x, &d, (long)len) == NULL) {
340 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
344 ret = SSL_CTX_use_certificate(ctx, x);
349 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
352 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
355 return ssl_set_pkey(ctx->cert, pkey);
358 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
362 EVP_PKEY *pkey = NULL;
364 in = BIO_new(BIO_s_file());
366 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
370 if (BIO_read_filename(in, file) <= 0) {
371 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
374 if (type == SSL_FILETYPE_PEM) {
376 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
377 ctx->default_passwd_callback,
378 ctx->default_passwd_callback_userdata,
379 ctx->libctx, ctx->propq);
380 } else if (type == SSL_FILETYPE_ASN1) {
382 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
384 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
388 ERR_raise(ERR_LIB_SSL, j);
391 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
398 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
399 const unsigned char *d, long len)
402 const unsigned char *p;
406 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
407 ctx->propq)) == NULL) {
408 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
412 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
418 * Read a file that contains our certificate in "PEM" format, possibly
419 * followed by a sequence of CA certificates that should be sent to the peer
420 * in the Certificate message.
422 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
427 pem_password_cb *passwd_callback;
428 void *passwd_callback_userdata;
429 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
431 if (ctx == NULL && ssl == NULL)
434 ERR_clear_error(); /* clear error stack for
435 * SSL_CTX_use_certificate() */
438 passwd_callback = ctx->default_passwd_callback;
439 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
441 passwd_callback = ssl->default_passwd_callback;
442 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
445 in = BIO_new(BIO_s_file());
447 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
451 if (BIO_read_filename(in, file) <= 0) {
452 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
456 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
458 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
461 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
462 passwd_callback_userdata) == NULL) {
463 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
468 ret = SSL_CTX_use_certificate(ctx, x);
470 ret = SSL_use_certificate(ssl, x);
472 if (ERR_peek_error() != 0)
473 ret = 0; /* Key/certificate mismatch doesn't imply
477 * If we could set up our certificate, now proceed to the CA
485 r = SSL_CTX_clear_chain_certs(ctx);
487 r = SSL_clear_chain_certs(ssl);
495 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
497 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
500 if (PEM_read_bio_X509(in, &ca, passwd_callback,
501 passwd_callback_userdata) != NULL) {
503 r = SSL_CTX_add0_chain_cert(ctx, ca);
505 r = SSL_add0_chain_cert(ssl, ca);
507 * Note that we must not free ca if it was successfully added to
508 * the chain (while we must free the main certificate, since its
509 * reference count is increased by SSL_CTX_use_certificate).
521 /* When the while loop ends, it's usually just EOF. */
522 err = ERR_peek_last_error();
523 if (ERR_GET_LIB(err) == ERR_LIB_PEM
524 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
527 ret = 0; /* some real error */
536 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
538 return use_certificate_chain_file(ctx, NULL, file);
541 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
543 return use_certificate_chain_file(NULL, ssl, file);
546 static int serverinfo_find_extension(const unsigned char *serverinfo,
547 size_t serverinfo_length,
548 unsigned int extension_type,
549 const unsigned char **extension_data,
550 size_t *extension_length)
554 *extension_data = NULL;
555 *extension_length = 0;
556 if (serverinfo == NULL || serverinfo_length == 0)
559 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
563 unsigned int type = 0;
564 unsigned long context = 0;
566 /* end of serverinfo */
567 if (PACKET_remaining(&pkt) == 0)
568 return 0; /* Extension not found */
570 if (!PACKET_get_net_4(&pkt, &context)
571 || !PACKET_get_net_2(&pkt, &type)
572 || !PACKET_get_length_prefixed_2(&pkt, &data))
575 if (type == extension_type) {
576 *extension_data = PACKET_data(&data);
577 *extension_length = PACKET_remaining(&data);
578 return 1; /* Success */
584 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
585 unsigned int context,
586 const unsigned char *in,
587 size_t inlen, X509 *x, size_t chainidx,
592 *al = SSL_AD_DECODE_ERROR;
599 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
600 const unsigned char *in,
601 size_t inlen, int *al, void *arg)
603 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
607 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
608 unsigned int context,
609 const unsigned char **out,
610 size_t *outlen, X509 *x, size_t chainidx,
613 const unsigned char *serverinfo = NULL;
614 size_t serverinfo_length = 0;
616 /* We only support extensions for the first Certificate */
617 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
620 /* Is there serverinfo data for the chosen server cert? */
621 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
622 &serverinfo_length)) != 0) {
623 /* Find the relevant extension from the serverinfo */
624 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
625 ext_type, out, outlen);
627 *al = SSL_AD_INTERNAL_ERROR;
628 return -1; /* Error */
631 return 0; /* No extension found, don't send extension */
632 return 1; /* Send extension */
634 return 0; /* No serverinfo data found, don't send
638 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
639 const unsigned char **out, size_t *outlen,
642 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
647 * With a NULL context, this function just checks that the serverinfo data
648 * parses correctly. With a non-NULL context, it registers callbacks for
649 * the included extensions.
651 static int serverinfo_process_buffer(unsigned int version,
652 const unsigned char *serverinfo,
653 size_t serverinfo_length, SSL_CTX *ctx)
657 if (serverinfo == NULL || serverinfo_length == 0)
660 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
663 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
666 while (PACKET_remaining(&pkt)) {
667 unsigned long context = 0;
668 unsigned int ext_type = 0;
671 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
672 || !PACKET_get_net_2(&pkt, &ext_type)
673 || !PACKET_get_length_prefixed_2(&pkt, &data))
680 * The old style custom extensions API could be set separately for
681 * server/client, i.e. you could set one custom extension for a client,
682 * and *for the same extension in the same SSL_CTX* you could set a
683 * custom extension for the server as well. It seems quite weird to be
684 * setting a custom extension for both client and server in a single
685 * SSL_CTX - but theoretically possible. This isn't possible in the
686 * new API. Therefore, if we have V1 serverinfo we use the old API. We
687 * also use the old API even if we have V2 serverinfo but the context
688 * looks like an old style <= TLSv1.2 extension.
690 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
691 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
692 serverinfo_srv_add_cb,
694 serverinfo_srv_parse_cb,
698 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
699 serverinfoex_srv_add_cb,
701 serverinfoex_srv_parse_cb,
710 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
711 const unsigned char *serverinfo,
712 size_t serverinfo_length)
714 unsigned char *new_serverinfo;
716 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
717 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
720 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
722 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
725 if (ctx->cert->key == NULL) {
726 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
729 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
731 if (new_serverinfo == NULL) {
732 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
735 ctx->cert->key->serverinfo = new_serverinfo;
736 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
737 ctx->cert->key->serverinfo_length = serverinfo_length;
740 * Now that the serverinfo is validated and stored, go ahead and
741 * register callbacks.
743 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
745 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
751 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
752 size_t serverinfo_length)
754 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
758 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
760 unsigned char *serverinfo = NULL;
762 size_t serverinfo_length = 0;
763 unsigned char *extension = 0;
764 long extension_length = 0;
767 unsigned int name_len;
770 size_t num_extensions = 0, contextoff = 0;
772 if (ctx == NULL || file == NULL) {
773 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
777 bin = BIO_new(BIO_s_file());
779 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
782 if (BIO_read_filename(bin, file) <= 0) {
783 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
787 for (num_extensions = 0;; num_extensions++) {
788 unsigned int version;
790 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
793 * There must be at least one extension in this file
795 if (num_extensions == 0) {
796 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
798 } else /* End of file, we're done */
801 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
802 name_len = strlen(name);
803 if (name_len < sizeof(NAME_PREFIX1) - 1) {
804 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
807 if (HAS_PREFIX(name, NAME_PREFIX1)) {
808 version = SSL_SERVERINFOV1;
810 if (name_len < sizeof(NAME_PREFIX2) - 1) {
811 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
814 if (!HAS_PREFIX(name, NAME_PREFIX2)) {
815 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
818 version = SSL_SERVERINFOV2;
821 * Check that the decoded PEM data is plausible (valid length field)
823 if (version == SSL_SERVERINFOV1) {
824 /* 4 byte header: 2 bytes type, 2 bytes len */
825 if (extension_length < 4
826 || (extension[2] << 8) + extension[3]
827 != extension_length - 4) {
828 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
832 * File does not have a context value so we must take account of
837 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
838 if (extension_length < 8
839 || (extension[6] << 8) + extension[7]
840 != extension_length - 8) {
841 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
845 /* Append the decoded extension to the serverinfo buffer */
846 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
849 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
853 if (contextoff > 0) {
854 unsigned char *sinfo = serverinfo + serverinfo_length;
856 /* We know this only uses the last 2 bytes */
859 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
860 sinfo[3] = SYNTHV1CONTEXT & 0xff;
862 memcpy(serverinfo + serverinfo_length + contextoff,
863 extension, extension_length);
864 serverinfo_length += extension_length + contextoff;
868 OPENSSL_free(header);
870 OPENSSL_free(extension);
874 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
877 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
879 OPENSSL_free(header);
880 OPENSSL_free(extension);
881 OPENSSL_free(serverinfo);
886 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
887 STACK_OF(X509) *chain, int override)
893 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
894 STACK_OF(X509) *dup_chain = NULL;
895 EVP_PKEY *pubkey = NULL;
897 /* Do all security checks before anything else */
898 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
900 ERR_raise(ERR_LIB_SSL, rv);
903 for (j = 0; j < sk_X509_num(chain); j++) {
904 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
906 ERR_raise(ERR_LIB_SSL, rv);
911 pubkey = X509_get_pubkey(x509); /* bumps reference */
914 if (privatekey == NULL) {
917 /* For RSA, which has no parameters, missing returns 0 */
918 if (EVP_PKEY_missing_parameters(privatekey)) {
919 if (EVP_PKEY_missing_parameters(pubkey)) {
920 /* nobody has parameters? - error */
921 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
924 /* copy to privatekey from pubkey */
925 if (!EVP_PKEY_copy_parameters(privatekey, pubkey)) {
926 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
930 } else if (EVP_PKEY_missing_parameters(pubkey)) {
931 /* copy to pubkey from privatekey */
932 if (!EVP_PKEY_copy_parameters(pubkey, privatekey)) {
933 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
936 } /* else both have parameters */
938 /* check that key <-> cert match */
939 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
940 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
944 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
945 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
949 if (!override && (c->pkeys[i].x509 != NULL
950 || c->pkeys[i].privatekey != NULL
951 || c->pkeys[i].chain != NULL)) {
952 /* No override, and something already there */
953 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
958 dup_chain = X509_chain_up_ref(chain);
959 if (dup_chain == NULL) {
960 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
965 OSSL_STACK_OF_X509_free(c->pkeys[i].chain);
966 c->pkeys[i].chain = dup_chain;
968 X509_free(c->pkeys[i].x509);
970 c->pkeys[i].x509 = x509;
972 EVP_PKEY_free(c->pkeys[i].privatekey);
973 EVP_PKEY_up_ref(privatekey);
974 c->pkeys[i].privatekey = privatekey;
976 c->key = &(c->pkeys[i]);
980 EVP_PKEY_free(pubkey);
984 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
985 STACK_OF(X509) *chain, int override)
987 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
990 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
991 STACK_OF(X509) *chain, int override)
993 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);