2 * Copyright 1995-2023 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, SSL_CTX *ctx);
21 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey, SSL_CTX *ctx);
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)
34 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
40 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
44 rv = ssl_security_cert(sc, NULL, x, 0, 1);
46 ERR_raise(ERR_LIB_SSL, rv);
50 return ssl_set_cert(sc->cert, x, SSL_CONNECTION_GET_CTX(sc));
53 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
58 X509 *cert = NULL, *x = NULL;
60 in = BIO_new(BIO_s_file());
62 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
66 if (BIO_read_filename(in, file) <= 0) {
67 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
71 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
73 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
76 if (type == SSL_FILETYPE_ASN1) {
78 cert = d2i_X509_bio(in, &x);
79 } else if (type == SSL_FILETYPE_PEM) {
80 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
86 cert = PEM_read_bio_X509(in, &x, sc->default_passwd_callback,
87 sc->default_passwd_callback_userdata);
89 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
94 ERR_raise(ERR_LIB_SSL, j);
98 ret = SSL_use_certificate(ssl, x);
105 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
110 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
112 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
116 if (d2i_X509(&x, &d, (long)len)== NULL) {
118 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
122 ret = SSL_use_certificate(ssl, x);
127 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey, SSL_CTX *ctx)
131 if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) {
132 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
136 if (c->pkeys[i].x509 != NULL
137 && !X509_check_private_key(c->pkeys[i].x509, pkey))
140 EVP_PKEY_free(c->pkeys[i].privatekey);
141 EVP_PKEY_up_ref(pkey);
142 c->pkeys[i].privatekey = pkey;
143 c->key = &c->pkeys[i];
147 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
150 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
156 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
159 ret = ssl_set_pkey(sc->cert, pkey, SSL_CONNECTION_GET_CTX(sc));
163 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
167 EVP_PKEY *pkey = NULL;
169 in = BIO_new(BIO_s_file());
171 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
175 if (BIO_read_filename(in, file) <= 0) {
176 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
179 if (type == SSL_FILETYPE_PEM) {
180 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
186 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
187 sc->default_passwd_callback,
188 sc->default_passwd_callback_userdata,
191 } else if (type == SSL_FILETYPE_ASN1) {
193 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
196 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
200 ERR_raise(ERR_LIB_SSL, j);
203 ret = SSL_use_PrivateKey(ssl, pkey);
210 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
214 const unsigned char *p;
218 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
219 ssl->ctx->propq)) == NULL) {
220 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
224 ret = SSL_use_PrivateKey(ssl, pkey);
229 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
233 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
237 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
239 ERR_raise(ERR_LIB_SSL, rv);
242 return ssl_set_cert(ctx->cert, x, ctx);
245 static int ssl_set_cert(CERT *c, X509 *x, SSL_CTX *ctx)
250 pkey = X509_get0_pubkey(x);
252 ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
256 if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) {
257 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
261 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
262 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
266 if (c->pkeys[i].privatekey != NULL) {
268 * The return code from EVP_PKEY_copy_parameters is deliberately
269 * ignored. Some EVP_PKEY types cannot do this.
270 * coverity[check_return]
272 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
275 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
277 * don't fail for a cert/key mismatch, just free current private
278 * key (when switching to a different cert & key, first this
279 * function should be used, then ssl_set_pkey
281 EVP_PKEY_free(c->pkeys[i].privatekey);
282 c->pkeys[i].privatekey = NULL;
283 /* clear error queue */
288 X509_free(c->pkeys[i].x509);
290 c->pkeys[i].x509 = x;
291 c->key = &(c->pkeys[i]);
296 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
298 int j = SSL_R_BAD_VALUE;
301 X509 *x = NULL, *cert = NULL;
303 in = BIO_new(BIO_s_file());
305 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
309 if (BIO_read_filename(in, file) <= 0) {
310 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
314 x = X509_new_ex(ctx->libctx, ctx->propq);
316 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
319 if (type == SSL_FILETYPE_ASN1) {
321 cert = d2i_X509_bio(in, &x);
322 } else if (type == SSL_FILETYPE_PEM) {
324 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
325 ctx->default_passwd_callback_userdata);
327 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
331 ERR_raise(ERR_LIB_SSL, j);
335 ret = SSL_CTX_use_certificate(ctx, x);
342 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
347 x = X509_new_ex(ctx->libctx, ctx->propq);
349 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
353 if (d2i_X509(&x, &d, (long)len) == NULL) {
355 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
359 ret = SSL_CTX_use_certificate(ctx, x);
364 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
367 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
370 return ssl_set_pkey(ctx->cert, pkey, ctx);
373 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
377 EVP_PKEY *pkey = NULL;
379 in = BIO_new(BIO_s_file());
381 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
385 if (BIO_read_filename(in, file) <= 0) {
386 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
389 if (type == SSL_FILETYPE_PEM) {
391 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
392 ctx->default_passwd_callback,
393 ctx->default_passwd_callback_userdata,
394 ctx->libctx, ctx->propq);
395 } else if (type == SSL_FILETYPE_ASN1) {
397 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
399 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
403 ERR_raise(ERR_LIB_SSL, j);
406 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
413 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
414 const unsigned char *d, long len)
417 const unsigned char *p;
421 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
422 ctx->propq)) == NULL) {
423 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
427 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
433 * Read a file that contains our certificate in "PEM" format, possibly
434 * followed by a sequence of CA certificates that should be sent to the peer
435 * in the Certificate message.
437 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
442 pem_password_cb *passwd_callback;
443 void *passwd_callback_userdata;
444 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
446 if (ctx == NULL && ssl == NULL)
449 ERR_clear_error(); /* clear error stack for
450 * SSL_CTX_use_certificate() */
453 passwd_callback = ctx->default_passwd_callback;
454 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
456 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
461 passwd_callback = sc->default_passwd_callback;
462 passwd_callback_userdata = sc->default_passwd_callback_userdata;
465 in = BIO_new(BIO_s_file());
467 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
471 if (BIO_read_filename(in, file) <= 0) {
472 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
476 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
478 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
481 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
482 passwd_callback_userdata) == NULL) {
483 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
488 ret = SSL_CTX_use_certificate(ctx, x);
490 ret = SSL_use_certificate(ssl, x);
492 if (ERR_peek_error() != 0)
493 ret = 0; /* Key/certificate mismatch doesn't imply
497 * If we could set up our certificate, now proceed to the CA
505 r = SSL_CTX_clear_chain_certs(ctx);
507 r = SSL_clear_chain_certs(ssl);
515 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
517 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
520 if (PEM_read_bio_X509(in, &ca, passwd_callback,
521 passwd_callback_userdata) != NULL) {
523 r = SSL_CTX_add0_chain_cert(ctx, ca);
525 r = SSL_add0_chain_cert(ssl, ca);
527 * Note that we must not free ca if it was successfully added to
528 * the chain (while we must free the main certificate, since its
529 * reference count is increased by SSL_CTX_use_certificate).
541 /* When the while loop ends, it's usually just EOF. */
542 err = ERR_peek_last_error();
543 if (ERR_GET_LIB(err) == ERR_LIB_PEM
544 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
547 ret = 0; /* some real error */
556 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
558 return use_certificate_chain_file(ctx, NULL, file);
561 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
563 return use_certificate_chain_file(NULL, ssl, file);
566 static int serverinfo_find_extension(const unsigned char *serverinfo,
567 size_t serverinfo_length,
568 unsigned int extension_type,
569 const unsigned char **extension_data,
570 size_t *extension_length)
574 *extension_data = NULL;
575 *extension_length = 0;
576 if (serverinfo == NULL || serverinfo_length == 0)
579 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
583 unsigned int type = 0;
584 unsigned long context = 0;
586 /* end of serverinfo */
587 if (PACKET_remaining(&pkt) == 0)
588 return 0; /* Extension not found */
590 if (!PACKET_get_net_4(&pkt, &context)
591 || !PACKET_get_net_2(&pkt, &type)
592 || !PACKET_get_length_prefixed_2(&pkt, &data))
595 if (type == extension_type) {
596 *extension_data = PACKET_data(&data);
597 *extension_length = PACKET_remaining(&data);
598 return 1; /* Success */
604 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
605 unsigned int context,
606 const unsigned char *in,
607 size_t inlen, X509 *x, size_t chainidx,
612 *al = SSL_AD_DECODE_ERROR;
619 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
620 const unsigned char *in,
621 size_t inlen, int *al, void *arg)
623 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
627 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
628 unsigned int context,
629 const unsigned char **out,
630 size_t *outlen, X509 *x, size_t chainidx,
633 const unsigned char *serverinfo = NULL;
634 size_t serverinfo_length = 0;
635 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
638 *al = SSL_AD_INTERNAL_ERROR;
642 /* We only support extensions for the first Certificate */
643 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
646 /* Is there serverinfo data for the chosen server cert? */
647 if ((ssl_get_server_cert_serverinfo(sc, &serverinfo,
648 &serverinfo_length)) != 0) {
649 /* Find the relevant extension from the serverinfo */
650 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
651 ext_type, out, outlen);
653 *al = SSL_AD_INTERNAL_ERROR;
654 return -1; /* Error */
657 return 0; /* No extension found, don't send extension */
658 return 1; /* Send extension */
660 return 0; /* No serverinfo data found, don't send
664 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
665 const unsigned char **out, size_t *outlen,
668 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
673 * With a NULL context, this function just checks that the serverinfo data
674 * parses correctly. With a non-NULL context, it registers callbacks for
675 * the included extensions.
677 static int serverinfo_process_buffer(unsigned int version,
678 const unsigned char *serverinfo,
679 size_t serverinfo_length, SSL_CTX *ctx)
683 if (serverinfo == NULL || serverinfo_length == 0)
686 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
689 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
692 while (PACKET_remaining(&pkt)) {
693 unsigned long context = 0;
694 unsigned int ext_type = 0;
697 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
698 || !PACKET_get_net_2(&pkt, &ext_type)
699 || !PACKET_get_length_prefixed_2(&pkt, &data))
706 * The old style custom extensions API could be set separately for
707 * server/client, i.e. you could set one custom extension for a client,
708 * and *for the same extension in the same SSL_CTX* you could set a
709 * custom extension for the server as well. It seems quite weird to be
710 * setting a custom extension for both client and server in a single
711 * SSL_CTX - but theoretically possible. This isn't possible in the
712 * new API. Therefore, if we have V1 serverinfo we use the old API. We
713 * also use the old API even if we have V2 serverinfo but the context
714 * looks like an old style <= TLSv1.2 extension.
716 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
717 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
718 serverinfo_srv_add_cb,
720 serverinfo_srv_parse_cb,
724 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
725 serverinfoex_srv_add_cb,
727 serverinfoex_srv_parse_cb,
736 static size_t extension_contextoff(unsigned int version)
738 return version == SSL_SERVERINFOV1 ? 4 : 0;
741 static size_t extension_append_length(unsigned int version, size_t extension_length)
743 return extension_length + extension_contextoff(version);
746 static void extension_append(unsigned int version,
747 const unsigned char *extension,
748 const size_t extension_length,
749 unsigned char *serverinfo)
751 const size_t contextoff = extension_contextoff(version);
753 if (contextoff > 0) {
754 /* We know this only uses the last 2 bytes */
757 serverinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
758 serverinfo[3] = SYNTHV1CONTEXT & 0xff;
761 memcpy(serverinfo + contextoff, extension, extension_length);
764 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
765 const unsigned char *serverinfo,
766 size_t serverinfo_length)
768 unsigned char *new_serverinfo = NULL;
770 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
771 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
774 if (version == SSL_SERVERINFOV1) {
776 * Convert serverinfo version v1 to v2 and call yourself recursively
777 * over the converted serverinfo.
779 const size_t sinfo_length = extension_append_length(SSL_SERVERINFOV1,
781 unsigned char *sinfo;
784 sinfo = OPENSSL_malloc(sinfo_length);
788 extension_append(SSL_SERVERINFOV1, serverinfo, serverinfo_length, sinfo);
790 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, sinfo,
796 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
798 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
801 if (ctx->cert->key == NULL) {
802 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
805 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
807 if (new_serverinfo == NULL)
809 ctx->cert->key->serverinfo = new_serverinfo;
810 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
811 ctx->cert->key->serverinfo_length = serverinfo_length;
814 * Now that the serverinfo is validated and stored, go ahead and
815 * register callbacks.
817 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
819 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
825 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
826 size_t serverinfo_length)
828 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
832 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
834 unsigned char *serverinfo = NULL;
836 size_t serverinfo_length = 0;
837 unsigned char *extension = 0;
838 long extension_length = 0;
841 unsigned int name_len;
844 size_t num_extensions = 0;
846 if (ctx == NULL || file == NULL) {
847 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
851 bin = BIO_new(BIO_s_file());
853 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
856 if (BIO_read_filename(bin, file) <= 0) {
857 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
861 for (num_extensions = 0;; num_extensions++) {
862 unsigned int version;
863 size_t append_length;
865 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
868 * There must be at least one extension in this file
870 if (num_extensions == 0) {
871 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
873 } else /* End of file, we're done */
876 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
877 name_len = strlen(name);
878 if (name_len < sizeof(NAME_PREFIX1) - 1) {
879 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
882 if (HAS_PREFIX(name, NAME_PREFIX1)) {
883 version = SSL_SERVERINFOV1;
885 if (name_len < sizeof(NAME_PREFIX2) - 1) {
886 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
889 if (!HAS_PREFIX(name, NAME_PREFIX2)) {
890 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
893 version = SSL_SERVERINFOV2;
896 * Check that the decoded PEM data is plausible (valid length field)
898 if (version == SSL_SERVERINFOV1) {
899 /* 4 byte header: 2 bytes type, 2 bytes len */
900 if (extension_length < 4
901 || (extension[2] << 8) + extension[3]
902 != extension_length - 4) {
903 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
907 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
908 if (extension_length < 8
909 || (extension[6] << 8) + extension[7]
910 != extension_length - 8) {
911 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
915 /* Append the decoded extension to the serverinfo buffer */
916 append_length = extension_append_length(version, extension_length);
917 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + append_length);
921 extension_append(version, extension, extension_length,
922 serverinfo + serverinfo_length);
923 serverinfo_length += append_length;
927 OPENSSL_free(header);
929 OPENSSL_free(extension);
933 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
936 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
938 OPENSSL_free(header);
939 OPENSSL_free(extension);
940 OPENSSL_free(serverinfo);
945 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
946 STACK_OF(X509) *chain, int override)
953 STACK_OF(X509) *dup_chain = NULL;
954 EVP_PKEY *pubkey = NULL;
955 SSL_CONNECTION *sc = NULL;
958 (sc = SSL_CONNECTION_FROM_SSL(ssl)) == NULL)
961 c = sc != NULL ? sc->cert : ctx->cert;
962 /* Do all security checks before anything else */
963 rv = ssl_security_cert(sc, ctx, x509, 0, 1);
965 ERR_raise(ERR_LIB_SSL, rv);
968 for (j = 0; j < sk_X509_num(chain); j++) {
969 rv = ssl_security_cert(sc, ctx, sk_X509_value(chain, j), 0, 0);
971 ERR_raise(ERR_LIB_SSL, rv);
976 pubkey = X509_get_pubkey(x509); /* bumps reference */
979 if (privatekey == NULL) {
982 /* For RSA, which has no parameters, missing returns 0 */
983 if (EVP_PKEY_missing_parameters(privatekey)) {
984 if (EVP_PKEY_missing_parameters(pubkey)) {
985 /* nobody has parameters? - error */
986 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
989 /* copy to privatekey from pubkey */
990 if (!EVP_PKEY_copy_parameters(privatekey, pubkey)) {
991 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
995 } else if (EVP_PKEY_missing_parameters(pubkey)) {
996 /* copy to pubkey from privatekey */
997 if (!EVP_PKEY_copy_parameters(pubkey, privatekey)) {
998 ERR_raise(ERR_LIB_SSL, SSL_R_COPY_PARAMETERS_FAILED);
1001 } /* else both have parameters */
1003 /* check that key <-> cert match */
1004 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
1005 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
1009 if (ssl_cert_lookup_by_pkey(pubkey, &i, ctx) == NULL) {
1010 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1014 if (!override && (c->pkeys[i].x509 != NULL
1015 || c->pkeys[i].privatekey != NULL
1016 || c->pkeys[i].chain != NULL)) {
1017 /* No override, and something already there */
1018 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
1022 if (chain != NULL) {
1023 dup_chain = X509_chain_up_ref(chain);
1024 if (dup_chain == NULL) {
1025 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
1030 OSSL_STACK_OF_X509_free(c->pkeys[i].chain);
1031 c->pkeys[i].chain = dup_chain;
1033 X509_free(c->pkeys[i].x509);
1035 c->pkeys[i].x509 = x509;
1037 EVP_PKEY_free(c->pkeys[i].privatekey);
1038 EVP_PKEY_up_ref(privatekey);
1039 c->pkeys[i].privatekey = privatekey;
1041 c->key = &(c->pkeys[i]);
1045 EVP_PKEY_free(pubkey);
1049 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1050 STACK_OF(X509) *chain, int override)
1052 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1055 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1056 STACK_OF(X509) *chain, int override)
1058 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);