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 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);
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 if (ctx == NULL && ssl == NULL)
430 ERR_clear_error(); /* clear error stack for
431 * SSL_CTX_use_certificate() */
434 passwd_callback = ctx->default_passwd_callback;
435 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
437 passwd_callback = ssl->default_passwd_callback;
438 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
441 in = BIO_new(BIO_s_file());
443 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
447 if (BIO_read_filename(in, file) <= 0) {
448 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
452 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
454 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
457 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
458 passwd_callback_userdata) == NULL) {
459 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
464 ret = SSL_CTX_use_certificate(ctx, x);
466 ret = SSL_use_certificate(ssl, x);
468 if (ERR_peek_error() != 0)
469 ret = 0; /* Key/certificate mismatch doesn't imply
473 * If we could set up our certificate, now proceed to the CA
481 r = SSL_CTX_clear_chain_certs(ctx);
483 r = SSL_clear_chain_certs(ssl);
491 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
493 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
496 if (PEM_read_bio_X509(in, &ca, passwd_callback,
497 passwd_callback_userdata) != NULL) {
499 r = SSL_CTX_add0_chain_cert(ctx, ca);
501 r = SSL_add0_chain_cert(ssl, ca);
503 * Note that we must not free ca if it was successfully added to
504 * the chain (while we must free the main certificate, since its
505 * reference count is increased by SSL_CTX_use_certificate).
517 /* When the while loop ends, it's usually just EOF. */
518 err = ERR_peek_last_error();
519 if (ERR_GET_LIB(err) == ERR_LIB_PEM
520 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
523 ret = 0; /* some real error */
532 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
534 return use_certificate_chain_file(ctx, NULL, file);
537 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
539 return use_certificate_chain_file(NULL, ssl, file);
542 static int serverinfo_find_extension(const unsigned char *serverinfo,
543 size_t serverinfo_length,
544 unsigned int extension_type,
545 const unsigned char **extension_data,
546 size_t *extension_length)
550 *extension_data = NULL;
551 *extension_length = 0;
552 if (serverinfo == NULL || serverinfo_length == 0)
555 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
559 unsigned int type = 0;
560 unsigned long context = 0;
562 /* end of serverinfo */
563 if (PACKET_remaining(&pkt) == 0)
564 return 0; /* Extension not found */
566 if (!PACKET_get_net_4(&pkt, &context)
567 || !PACKET_get_net_2(&pkt, &type)
568 || !PACKET_get_length_prefixed_2(&pkt, &data))
571 if (type == extension_type) {
572 *extension_data = PACKET_data(&data);
573 *extension_length = PACKET_remaining(&data);;
574 return 1; /* Success */
580 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
581 unsigned int context,
582 const unsigned char *in,
583 size_t inlen, X509 *x, size_t chainidx,
588 *al = SSL_AD_DECODE_ERROR;
595 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
596 const unsigned char *in,
597 size_t inlen, int *al, void *arg)
599 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
603 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
604 unsigned int context,
605 const unsigned char **out,
606 size_t *outlen, X509 *x, size_t chainidx,
609 const unsigned char *serverinfo = NULL;
610 size_t serverinfo_length = 0;
612 /* We only support extensions for the first Certificate */
613 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
616 /* Is there serverinfo data for the chosen server cert? */
617 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
618 &serverinfo_length)) != 0) {
619 /* Find the relevant extension from the serverinfo */
620 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
621 ext_type, out, outlen);
623 *al = SSL_AD_INTERNAL_ERROR;
624 return -1; /* Error */
627 return 0; /* No extension found, don't send extension */
628 return 1; /* Send extension */
630 return 0; /* No serverinfo data found, don't send
634 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
635 const unsigned char **out, size_t *outlen,
638 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
643 * With a NULL context, this function just checks that the serverinfo data
644 * parses correctly. With a non-NULL context, it registers callbacks for
645 * the included extensions.
647 static int serverinfo_process_buffer(unsigned int version,
648 const unsigned char *serverinfo,
649 size_t serverinfo_length, SSL_CTX *ctx)
653 if (serverinfo == NULL || serverinfo_length == 0)
656 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
659 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
662 while (PACKET_remaining(&pkt)) {
663 unsigned long context = 0;
664 unsigned int ext_type = 0;
667 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
668 || !PACKET_get_net_2(&pkt, &ext_type)
669 || !PACKET_get_length_prefixed_2(&pkt, &data))
676 * The old style custom extensions API could be set separately for
677 * server/client, i.e. you could set one custom extension for a client,
678 * and *for the same extension in the same SSL_CTX* you could set a
679 * custom extension for the server as well. It seems quite weird to be
680 * setting a custom extension for both client and server in a single
681 * SSL_CTX - but theoretically possible. This isn't possible in the
682 * new API. Therefore, if we have V1 serverinfo we use the old API. We
683 * also use the old API even if we have V2 serverinfo but the context
684 * looks like an old style <= TLSv1.2 extension.
686 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
687 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
688 serverinfo_srv_add_cb,
690 serverinfo_srv_parse_cb,
694 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
695 serverinfoex_srv_add_cb,
697 serverinfoex_srv_parse_cb,
706 static size_t extension_contextoff(unsigned int version)
708 return version == SSL_SERVERINFOV1 ? 4 : 0;
711 static size_t extension_append_length(unsigned int version, size_t extension_length)
713 return extension_length + extension_contextoff(version);
716 static void extension_append(unsigned int version,
717 const unsigned char *extension,
718 const size_t extension_length,
719 unsigned char *serverinfo)
721 const size_t contextoff = extension_contextoff(version);
723 if (contextoff > 0) {
724 /* We know this only uses the last 2 bytes */
727 serverinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
728 serverinfo[3] = SYNTHV1CONTEXT & 0xff;
731 memcpy(serverinfo + contextoff, extension, extension_length);
734 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
735 const unsigned char *serverinfo,
736 size_t serverinfo_length)
738 unsigned char *new_serverinfo = NULL;
740 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
741 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
744 if (version == SSL_SERVERINFOV1) {
746 * Convert serverinfo version v1 to v2 and call yourself recursively
747 * over the converted serverinfo.
749 const size_t sinfo_length = extension_append_length(SSL_SERVERINFOV1,
751 unsigned char *sinfo;
754 sinfo = OPENSSL_malloc(sinfo_length);
756 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
760 extension_append(SSL_SERVERINFOV1, serverinfo, serverinfo_length, sinfo);
762 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, sinfo,
768 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
770 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
773 if (ctx->cert->key == NULL) {
774 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
777 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
779 if (new_serverinfo == NULL) {
780 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
783 ctx->cert->key->serverinfo = new_serverinfo;
784 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
785 ctx->cert->key->serverinfo_length = serverinfo_length;
788 * Now that the serverinfo is validated and stored, go ahead and
789 * register callbacks.
791 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
793 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
799 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
800 size_t serverinfo_length)
802 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
806 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
808 unsigned char *serverinfo = NULL;
810 size_t serverinfo_length = 0;
811 unsigned char *extension = 0;
812 long extension_length = 0;
815 static const char namePrefix1[] = "SERVERINFO FOR ";
816 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
817 unsigned int name_len;
820 size_t num_extensions = 0;
822 if (ctx == NULL || file == NULL) {
823 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
827 bin = BIO_new(BIO_s_file());
829 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
832 if (BIO_read_filename(bin, file) <= 0) {
833 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
837 for (num_extensions = 0;; num_extensions++) {
838 unsigned int version;
839 size_t append_length;
841 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
844 * There must be at least one extension in this file
846 if (num_extensions == 0) {
847 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
849 } else /* End of file, we're done */
852 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
853 name_len = strlen(name);
854 if (name_len < sizeof(namePrefix1) - 1) {
855 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
858 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
859 version = SSL_SERVERINFOV1;
861 if (name_len < sizeof(namePrefix2) - 1) {
862 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
865 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
866 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
869 version = SSL_SERVERINFOV2;
872 * Check that the decoded PEM data is plausible (valid length field)
874 if (version == SSL_SERVERINFOV1) {
875 /* 4 byte header: 2 bytes type, 2 bytes len */
876 if (extension_length < 4
877 || (extension[2] << 8) + extension[3]
878 != extension_length - 4) {
879 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
883 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
884 if (extension_length < 8
885 || (extension[6] << 8) + extension[7]
886 != extension_length - 8) {
887 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
891 /* Append the decoded extension to the serverinfo buffer */
892 append_length = extension_append_length(version, extension_length);
893 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + append_length);
895 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
899 extension_append(version, extension, extension_length,
900 serverinfo + serverinfo_length);
901 serverinfo_length += append_length;
905 OPENSSL_free(header);
907 OPENSSL_free(extension);
911 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
914 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
916 OPENSSL_free(header);
917 OPENSSL_free(extension);
918 OPENSSL_free(serverinfo);
923 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
924 STACK_OF(X509) *chain, int override)
930 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
931 STACK_OF(X509) *dup_chain = NULL;
932 EVP_PKEY *pubkey = NULL;
934 /* Do all security checks before anything else */
935 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
937 ERR_raise(ERR_LIB_SSL, rv);
940 for (j = 0; j < sk_X509_num(chain); j++) {
941 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
943 ERR_raise(ERR_LIB_SSL, rv);
948 pubkey = X509_get_pubkey(x509); /* bumps reference */
951 if (privatekey == NULL) {
954 /* For RSA, which has no parameters, missing returns 0 */
955 if (EVP_PKEY_missing_parameters(privatekey)) {
956 if (EVP_PKEY_missing_parameters(pubkey)) {
957 /* nobody has parameters? - error */
958 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
961 /* copy to privatekey from pubkey */
962 if (!EVP_PKEY_copy_parameters(privatekey, pubkey)) {
963 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
967 } else if (EVP_PKEY_missing_parameters(pubkey)) {
968 /* copy to pubkey from privatekey */
969 if (!EVP_PKEY_copy_parameters(pubkey, privatekey)) {
970 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
973 } /* else both have parameters */
975 /* check that key <-> cert match */
976 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
977 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
981 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
982 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
986 if (!override && (c->pkeys[i].x509 != NULL
987 || c->pkeys[i].privatekey != NULL
988 || c->pkeys[i].chain != NULL)) {
989 /* No override, and something already there */
990 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
995 dup_chain = X509_chain_up_ref(chain);
996 if (dup_chain == NULL) {
997 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1002 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1003 c->pkeys[i].chain = dup_chain;
1005 X509_free(c->pkeys[i].x509);
1007 c->pkeys[i].x509 = x509;
1009 EVP_PKEY_free(c->pkeys[i].privatekey);
1010 EVP_PKEY_up_ref(privatekey);
1011 c->pkeys[i].privatekey = privatekey;
1013 c->key = &(c->pkeys[i]);
1017 EVP_PKEY_free(pubkey);
1021 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1022 STACK_OF(X509) *chain, int override)
1024 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1027 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1028 STACK_OF(X509) *chain, int override)
1030 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);