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>
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 int SSL_use_certificate(SSL *ssl, X509 *x)
32 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
36 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
38 ERR_raise(ERR_LIB_SSL, rv);
42 return ssl_set_cert(ssl->cert, x);
45 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
50 X509 *cert = NULL, *x = NULL;
52 in = BIO_new(BIO_s_file());
54 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
58 if (BIO_read_filename(in, file) <= 0) {
59 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
63 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
64 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
67 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
69 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
72 if (type == SSL_FILETYPE_ASN1) {
74 cert = d2i_X509_bio(in, &x);
75 } else if (type == SSL_FILETYPE_PEM) {
77 cert = PEM_read_bio_X509(in, &x, ssl->default_passwd_callback,
78 ssl->default_passwd_callback_userdata);
80 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
85 ERR_raise(ERR_LIB_SSL, j);
89 ret = SSL_use_certificate(ssl, x);
96 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
101 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
103 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
107 if (d2i_X509(&x, &d, (long)len)== NULL) {
109 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
113 ret = SSL_use_certificate(ssl, x);
118 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
122 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
123 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
127 if (c->pkeys[i].x509 != NULL
128 && !X509_check_private_key(c->pkeys[i].x509, pkey))
131 EVP_PKEY_free(c->pkeys[i].privatekey);
132 EVP_PKEY_up_ref(pkey);
133 c->pkeys[i].privatekey = pkey;
134 c->key = &c->pkeys[i];
138 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
143 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
146 ret = ssl_set_pkey(ssl->cert, pkey);
150 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
154 EVP_PKEY *pkey = NULL;
156 in = BIO_new(BIO_s_file());
158 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
162 if (BIO_read_filename(in, file) <= 0) {
163 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
166 if (type == SSL_FILETYPE_PEM) {
168 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
169 ssl->default_passwd_callback,
170 ssl->default_passwd_callback_userdata,
173 } else if (type == SSL_FILETYPE_ASN1) {
175 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
178 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
182 ERR_raise(ERR_LIB_SSL, j);
185 ret = SSL_use_PrivateKey(ssl, pkey);
192 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
196 const unsigned char *p;
200 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
201 ssl->ctx->propq)) == NULL) {
202 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
206 ret = SSL_use_PrivateKey(ssl, pkey);
211 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
215 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
219 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
221 ERR_raise(ERR_LIB_SSL, rv);
224 return ssl_set_cert(ctx->cert, x);
227 static int ssl_set_cert(CERT *c, X509 *x)
232 pkey = X509_get0_pubkey(x);
234 ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
238 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
239 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
242 #ifndef OPENSSL_NO_EC
243 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
244 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
248 if (c->pkeys[i].privatekey != NULL) {
250 * The return code from EVP_PKEY_copy_parameters is deliberately
251 * ignored. Some EVP_PKEY types cannot do this.
253 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
256 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
258 * don't fail for a cert/key mismatch, just free current private
259 * key (when switching to a different cert & key, first this
260 * function should be used, then ssl_set_pkey
262 EVP_PKEY_free(c->pkeys[i].privatekey);
263 c->pkeys[i].privatekey = NULL;
264 /* clear error queue */
269 X509_free(c->pkeys[i].x509);
271 c->pkeys[i].x509 = x;
272 c->key = &(c->pkeys[i]);
277 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
279 int j = SSL_R_BAD_VALUE;
282 X509 *x = NULL, *cert = NULL;
284 in = BIO_new(BIO_s_file());
286 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
290 if (BIO_read_filename(in, file) <= 0) {
291 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
294 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
295 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
298 x = X509_new_ex(ctx->libctx, ctx->propq);
300 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
303 if (type == SSL_FILETYPE_ASN1) {
305 cert = d2i_X509_bio(in, &x);
306 } else if (type == SSL_FILETYPE_PEM) {
308 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
309 ctx->default_passwd_callback_userdata);
312 ERR_raise(ERR_LIB_SSL, j);
316 ret = SSL_CTX_use_certificate(ctx, x);
323 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
328 x = X509_new_ex(ctx->libctx, ctx->propq);
330 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
334 if (d2i_X509(&x, &d, (long)len) == NULL) {
336 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
340 ret = SSL_CTX_use_certificate(ctx, x);
345 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
348 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
351 return ssl_set_pkey(ctx->cert, pkey);
354 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
358 EVP_PKEY *pkey = NULL;
360 in = BIO_new(BIO_s_file());
362 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
366 if (BIO_read_filename(in, file) <= 0) {
367 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
370 if (type == SSL_FILETYPE_PEM) {
372 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
373 ctx->default_passwd_callback,
374 ctx->default_passwd_callback_userdata,
375 ctx->libctx, ctx->propq);
376 } else if (type == SSL_FILETYPE_ASN1) {
378 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
380 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
384 ERR_raise(ERR_LIB_SSL, j);
387 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
394 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
395 const unsigned char *d, long len)
398 const unsigned char *p;
402 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
403 ctx->propq)) == NULL) {
404 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
408 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
414 * Read a file that contains our certificate in "PEM" format, possibly
415 * followed by a sequence of CA certificates that should be sent to the peer
416 * in the Certificate message.
418 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
423 pem_password_cb *passwd_callback;
424 void *passwd_callback_userdata;
425 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
427 ERR_clear_error(); /* clear error stack for
428 * SSL_CTX_use_certificate() */
431 passwd_callback = ctx->default_passwd_callback;
432 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
434 passwd_callback = ssl->default_passwd_callback;
435 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
438 in = BIO_new(BIO_s_file());
440 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
444 if (BIO_read_filename(in, file) <= 0) {
445 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
449 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
451 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
454 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
455 passwd_callback_userdata) == NULL) {
456 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
461 ret = SSL_CTX_use_certificate(ctx, x);
463 ret = SSL_use_certificate(ssl, x);
465 if (ERR_peek_error() != 0)
466 ret = 0; /* Key/certificate mismatch doesn't imply
470 * If we could set up our certificate, now proceed to the CA
478 r = SSL_CTX_clear_chain_certs(ctx);
480 r = SSL_clear_chain_certs(ssl);
488 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
490 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
493 if (PEM_read_bio_X509(in, &ca, passwd_callback,
494 passwd_callback_userdata) != NULL) {
496 r = SSL_CTX_add0_chain_cert(ctx, ca);
498 r = SSL_add0_chain_cert(ssl, ca);
500 * Note that we must not free ca if it was successfully added to
501 * the chain (while we must free the main certificate, since its
502 * reference count is increased by SSL_CTX_use_certificate).
514 /* When the while loop ends, it's usually just EOF. */
515 err = ERR_peek_last_error();
516 if (ERR_GET_LIB(err) == ERR_LIB_PEM
517 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
520 ret = 0; /* some real error */
529 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
531 return use_certificate_chain_file(ctx, NULL, file);
534 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
536 return use_certificate_chain_file(NULL, ssl, file);
539 static int serverinfo_find_extension(const unsigned char *serverinfo,
540 size_t serverinfo_length,
541 unsigned int extension_type,
542 const unsigned char **extension_data,
543 size_t *extension_length)
547 *extension_data = NULL;
548 *extension_length = 0;
549 if (serverinfo == NULL || serverinfo_length == 0)
552 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
556 unsigned int type = 0;
557 unsigned long context = 0;
559 /* end of serverinfo */
560 if (PACKET_remaining(&pkt) == 0)
561 return 0; /* Extension not found */
563 if (!PACKET_get_net_4(&pkt, &context)
564 || !PACKET_get_net_2(&pkt, &type)
565 || !PACKET_get_length_prefixed_2(&pkt, &data))
568 if (type == extension_type) {
569 *extension_data = PACKET_data(&data);
570 *extension_length = PACKET_remaining(&data);;
571 return 1; /* Success */
577 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
578 unsigned int context,
579 const unsigned char *in,
580 size_t inlen, X509 *x, size_t chainidx,
585 *al = SSL_AD_DECODE_ERROR;
592 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
593 const unsigned char *in,
594 size_t inlen, int *al, void *arg)
596 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
600 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
601 unsigned int context,
602 const unsigned char **out,
603 size_t *outlen, X509 *x, size_t chainidx,
606 const unsigned char *serverinfo = NULL;
607 size_t serverinfo_length = 0;
609 /* We only support extensions for the first Certificate */
610 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
613 /* Is there serverinfo data for the chosen server cert? */
614 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
615 &serverinfo_length)) != 0) {
616 /* Find the relevant extension from the serverinfo */
617 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
618 ext_type, out, outlen);
620 *al = SSL_AD_INTERNAL_ERROR;
621 return -1; /* Error */
624 return 0; /* No extension found, don't send extension */
625 return 1; /* Send extension */
627 return 0; /* No serverinfo data found, don't send
631 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
632 const unsigned char **out, size_t *outlen,
635 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
640 * With a NULL context, this function just checks that the serverinfo data
641 * parses correctly. With a non-NULL context, it registers callbacks for
642 * the included extensions.
644 static int serverinfo_process_buffer(unsigned int version,
645 const unsigned char *serverinfo,
646 size_t serverinfo_length, SSL_CTX *ctx)
650 if (serverinfo == NULL || serverinfo_length == 0)
653 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
656 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
659 while (PACKET_remaining(&pkt)) {
660 unsigned long context = 0;
661 unsigned int ext_type = 0;
664 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
665 || !PACKET_get_net_2(&pkt, &ext_type)
666 || !PACKET_get_length_prefixed_2(&pkt, &data))
673 * The old style custom extensions API could be set separately for
674 * server/client, i.e. you could set one custom extension for a client,
675 * and *for the same extension in the same SSL_CTX* you could set a
676 * custom extension for the server as well. It seems quite weird to be
677 * setting a custom extension for both client and server in a single
678 * SSL_CTX - but theoretically possible. This isn't possible in the
679 * new API. Therefore, if we have V1 serverinfo we use the old API. We
680 * also use the old API even if we have V2 serverinfo but the context
681 * looks like an old style <= TLSv1.2 extension.
683 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
684 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
685 serverinfo_srv_add_cb,
687 serverinfo_srv_parse_cb,
691 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
692 serverinfoex_srv_add_cb,
694 serverinfoex_srv_parse_cb,
703 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
704 const unsigned char *serverinfo,
705 size_t serverinfo_length)
707 unsigned char *new_serverinfo;
709 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
710 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
713 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
715 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
718 if (ctx->cert->key == NULL) {
719 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
722 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
724 if (new_serverinfo == NULL) {
725 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
728 ctx->cert->key->serverinfo = new_serverinfo;
729 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
730 ctx->cert->key->serverinfo_length = serverinfo_length;
733 * Now that the serverinfo is validated and stored, go ahead and
734 * register callbacks.
736 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
738 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
744 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
745 size_t serverinfo_length)
747 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
751 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
753 unsigned char *serverinfo = NULL;
755 size_t serverinfo_length = 0;
756 unsigned char *extension = 0;
757 long extension_length = 0;
760 static const char namePrefix1[] = "SERVERINFO FOR ";
761 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
762 unsigned int name_len;
765 size_t num_extensions = 0, contextoff = 0;
767 if (ctx == NULL || file == NULL) {
768 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
772 bin = BIO_new(BIO_s_file());
774 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
777 if (BIO_read_filename(bin, file) <= 0) {
778 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
782 for (num_extensions = 0;; num_extensions++) {
783 unsigned int version;
785 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
788 * There must be at least one extension in this file
790 if (num_extensions == 0) {
791 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
793 } else /* End of file, we're done */
796 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
797 name_len = strlen(name);
798 if (name_len < sizeof(namePrefix1) - 1) {
799 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
802 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
803 version = SSL_SERVERINFOV1;
805 if (name_len < sizeof(namePrefix2) - 1) {
806 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
809 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
810 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
813 version = SSL_SERVERINFOV2;
816 * Check that the decoded PEM data is plausible (valid length field)
818 if (version == SSL_SERVERINFOV1) {
819 /* 4 byte header: 2 bytes type, 2 bytes len */
820 if (extension_length < 4
821 || (extension[2] << 8) + extension[3]
822 != extension_length - 4) {
823 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
827 * File does not have a context value so we must take account of
832 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
833 if (extension_length < 8
834 || (extension[6] << 8) + extension[7]
835 != extension_length - 8) {
836 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
840 /* Append the decoded extension to the serverinfo buffer */
841 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
844 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
848 if (contextoff > 0) {
849 unsigned char *sinfo = serverinfo + serverinfo_length;
851 /* We know this only uses the last 2 bytes */
854 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
855 sinfo[3] = SYNTHV1CONTEXT & 0xff;
857 memcpy(serverinfo + serverinfo_length + contextoff,
858 extension, extension_length);
859 serverinfo_length += extension_length + contextoff;
863 OPENSSL_free(header);
865 OPENSSL_free(extension);
869 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
872 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
874 OPENSSL_free(header);
875 OPENSSL_free(extension);
876 OPENSSL_free(serverinfo);
881 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
882 STACK_OF(X509) *chain, int override)
888 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
889 STACK_OF(X509) *dup_chain = NULL;
890 EVP_PKEY *pubkey = NULL;
892 /* Do all security checks before anything else */
893 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
895 ERR_raise(ERR_LIB_SSL, rv);
898 for (j = 0; j < sk_X509_num(chain); j++) {
899 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
901 ERR_raise(ERR_LIB_SSL, rv);
906 pubkey = X509_get_pubkey(x509); /* bumps reference */
909 if (privatekey == NULL) {
912 /* For RSA, which has no parameters, missing returns 0 */
913 if (EVP_PKEY_missing_parameters(privatekey)) {
914 if (EVP_PKEY_missing_parameters(pubkey)) {
915 /* nobody has parameters? - error */
916 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
919 /* copy to privatekey from pubkey */
920 EVP_PKEY_copy_parameters(privatekey, pubkey);
922 } else if (EVP_PKEY_missing_parameters(pubkey)) {
923 /* copy to pubkey from privatekey */
924 EVP_PKEY_copy_parameters(pubkey, privatekey);
925 } /* else both have parameters */
927 /* check that key <-> cert match */
928 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
929 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
933 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
934 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
938 if (!override && (c->pkeys[i].x509 != NULL
939 || c->pkeys[i].privatekey != NULL
940 || c->pkeys[i].chain != NULL)) {
941 /* No override, and something already there */
942 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
947 dup_chain = X509_chain_up_ref(chain);
948 if (dup_chain == NULL) {
949 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
954 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
955 c->pkeys[i].chain = dup_chain;
957 X509_free(c->pkeys[i].x509);
959 c->pkeys[i].x509 = x509;
961 EVP_PKEY_free(c->pkeys[i].privatekey);
962 EVP_PKEY_up_ref(privatekey);
963 c->pkeys[i].privatekey = privatekey;
965 c->key = &(c->pkeys[i]);
969 EVP_PKEY_free(pubkey);
973 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
974 STACK_OF(X509) *chain, int override)
976 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
979 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
980 STACK_OF(X509) *chain, int override)
982 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);