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 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
36 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
38 SSLerr(SSL_F_SSL_USE_CERTIFICATE, 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 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
58 if (BIO_read_filename(in, file) <= 0) {
59 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
63 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
64 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
67 x = X509_new_with_libctx(ssl->ctx->libctx, ssl->ctx->propq);
69 SSLerr(0, 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 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
85 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, 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_with_libctx(ssl->ctx->libctx, ssl->ctx->propq);
103 SSLerr(0, ERR_R_MALLOC_FAILURE);
107 if (d2i_X509(&x, &d, (long)len)== NULL) {
109 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
113 ret = SSL_use_certificate(ssl, x);
118 #ifndef OPENSSL_NO_RSA
119 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
125 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
128 if ((pkey = EVP_PKEY_new()) == NULL) {
129 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
134 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
140 ret = ssl_set_pkey(ssl->cert, pkey);
146 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
150 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
151 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
155 if (c->pkeys[i].x509 != NULL) {
157 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
159 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
163 * The return code from EVP_PKEY_copy_parameters is deliberately
164 * ignored. Some EVP_PKEY types cannot do this.
166 EVP_PKEY_copy_parameters(pktmp, pkey);
169 #ifndef OPENSSL_NO_RSA
171 * Don't check the public/private key, this is mostly for smart
174 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
175 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
178 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
179 X509_free(c->pkeys[i].x509);
180 c->pkeys[i].x509 = NULL;
185 EVP_PKEY_free(c->pkeys[i].privatekey);
186 EVP_PKEY_up_ref(pkey);
187 c->pkeys[i].privatekey = pkey;
188 c->key = &c->pkeys[i];
192 #ifndef OPENSSL_NO_RSA
193 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
199 in = BIO_new(BIO_s_file());
201 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
205 if (BIO_read_filename(in, file) <= 0) {
206 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
209 if (type == SSL_FILETYPE_ASN1) {
211 rsa = d2i_RSAPrivateKey_bio(in, NULL);
212 } else if (type == SSL_FILETYPE_PEM) {
214 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
215 ssl->default_passwd_callback,
216 ssl->default_passwd_callback_userdata);
218 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
222 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
225 ret = SSL_use_RSAPrivateKey(ssl, rsa);
232 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
235 const unsigned char *p;
239 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
240 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
244 ret = SSL_use_RSAPrivateKey(ssl, rsa);
248 #endif /* !OPENSSL_NO_RSA */
250 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
255 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
258 ret = ssl_set_pkey(ssl->cert, pkey);
262 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
266 EVP_PKEY *pkey = NULL;
268 in = BIO_new(BIO_s_file());
270 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
274 if (BIO_read_filename(in, file) <= 0) {
275 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
278 if (type == SSL_FILETYPE_PEM) {
280 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
281 ssl->default_passwd_callback,
282 ssl->default_passwd_callback_userdata,
285 } else if (type == SSL_FILETYPE_ASN1) {
287 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
290 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
294 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
297 ret = SSL_use_PrivateKey(ssl, pkey);
304 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
308 const unsigned char *p;
312 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
313 ssl->ctx->propq)) == NULL) {
314 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
318 ret = SSL_use_PrivateKey(ssl, pkey);
323 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
327 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
331 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
333 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
336 return ssl_set_cert(ctx->cert, x);
339 static int ssl_set_cert(CERT *c, X509 *x)
344 pkey = X509_get0_pubkey(x);
346 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
350 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
351 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
354 #ifndef OPENSSL_NO_EC
355 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
356 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
360 if (c->pkeys[i].privatekey != NULL) {
362 * The return code from EVP_PKEY_copy_parameters is deliberately
363 * ignored. Some EVP_PKEY types cannot do this.
365 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
368 #ifndef OPENSSL_NO_RSA
370 * Don't check the public/private key, this is mostly for smart
373 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
374 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
375 RSA_METHOD_FLAG_NO_CHECK) ;
377 #endif /* OPENSSL_NO_RSA */
378 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
380 * don't fail for a cert/key mismatch, just free current private
381 * key (when switching to a different cert & key, first this
382 * function should be used, then ssl_set_pkey
384 EVP_PKEY_free(c->pkeys[i].privatekey);
385 c->pkeys[i].privatekey = NULL;
386 /* clear error queue */
391 X509_free(c->pkeys[i].x509);
393 c->pkeys[i].x509 = x;
394 c->key = &(c->pkeys[i]);
399 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
401 int j = SSL_R_BAD_VALUE;
404 X509 *x = NULL, *cert = NULL;
406 in = BIO_new(BIO_s_file());
408 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
412 if (BIO_read_filename(in, file) <= 0) {
413 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
416 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
417 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
420 x = X509_new_with_libctx(ctx->libctx, ctx->propq);
422 SSLerr(0, ERR_R_MALLOC_FAILURE);
425 if (type == SSL_FILETYPE_ASN1) {
427 cert = d2i_X509_bio(in, &x);
428 } else if (type == SSL_FILETYPE_PEM) {
430 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
431 ctx->default_passwd_callback_userdata);
434 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
438 ret = SSL_CTX_use_certificate(ctx, x);
445 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
450 x = X509_new_with_libctx(ctx->libctx, ctx->propq);
452 SSLerr(0, ERR_R_MALLOC_FAILURE);
456 if (d2i_X509(&x, &d, (long)len) == NULL) {
458 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
462 ret = SSL_CTX_use_certificate(ctx, x);
467 #ifndef OPENSSL_NO_RSA
468 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
474 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
477 if ((pkey = EVP_PKEY_new()) == NULL) {
478 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
483 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
489 ret = ssl_set_pkey(ctx->cert, pkey);
494 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
500 in = BIO_new(BIO_s_file());
502 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
506 if (BIO_read_filename(in, file) <= 0) {
507 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
510 if (type == SSL_FILETYPE_ASN1) {
512 rsa = d2i_RSAPrivateKey_bio(in, NULL);
513 } else if (type == SSL_FILETYPE_PEM) {
515 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
516 ctx->default_passwd_callback,
517 ctx->default_passwd_callback_userdata);
519 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
523 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
526 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
533 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
537 const unsigned char *p;
541 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
542 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
546 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
550 #endif /* !OPENSSL_NO_RSA */
552 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
555 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
558 return ssl_set_pkey(ctx->cert, pkey);
561 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
565 EVP_PKEY *pkey = NULL;
567 in = BIO_new(BIO_s_file());
569 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
573 if (BIO_read_filename(in, file) <= 0) {
574 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
577 if (type == SSL_FILETYPE_PEM) {
579 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
580 ctx->default_passwd_callback,
581 ctx->default_passwd_callback_userdata,
582 ctx->libctx, ctx->propq);
583 } else if (type == SSL_FILETYPE_ASN1) {
585 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
587 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
591 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
594 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
601 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
602 const unsigned char *d, long len)
605 const unsigned char *p;
609 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
610 ctx->propq)) == NULL) {
611 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
615 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
621 * Read a file that contains our certificate in "PEM" format, possibly
622 * followed by a sequence of CA certificates that should be sent to the peer
623 * in the Certificate message.
625 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
630 pem_password_cb *passwd_callback;
631 void *passwd_callback_userdata;
632 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
634 ERR_clear_error(); /* clear error stack for
635 * SSL_CTX_use_certificate() */
638 passwd_callback = ctx->default_passwd_callback;
639 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
641 passwd_callback = ssl->default_passwd_callback;
642 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
645 in = BIO_new(BIO_s_file());
647 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
651 if (BIO_read_filename(in, file) <= 0) {
652 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
656 x = X509_new_with_libctx(real_ctx->libctx, real_ctx->propq);
658 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_MALLOC_FAILURE);
661 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
662 passwd_callback_userdata) == NULL) {
663 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
668 ret = SSL_CTX_use_certificate(ctx, x);
670 ret = SSL_use_certificate(ssl, x);
672 if (ERR_peek_error() != 0)
673 ret = 0; /* Key/certificate mismatch doesn't imply
677 * If we could set up our certificate, now proceed to the CA
685 r = SSL_CTX_clear_chain_certs(ctx);
687 r = SSL_clear_chain_certs(ssl);
695 ca = X509_new_with_libctx(real_ctx->libctx, real_ctx->propq);
697 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_MALLOC_FAILURE);
700 if (PEM_read_bio_X509(in, &ca, passwd_callback,
701 passwd_callback_userdata) != NULL) {
703 r = SSL_CTX_add0_chain_cert(ctx, ca);
705 r = SSL_add0_chain_cert(ssl, ca);
707 * Note that we must not free ca if it was successfully added to
708 * the chain (while we must free the main certificate, since its
709 * reference count is increased by SSL_CTX_use_certificate).
721 /* When the while loop ends, it's usually just EOF. */
722 err = ERR_peek_last_error();
723 if (ERR_GET_LIB(err) == ERR_LIB_PEM
724 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
727 ret = 0; /* some real error */
736 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
738 return use_certificate_chain_file(ctx, NULL, file);
741 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
743 return use_certificate_chain_file(NULL, ssl, file);
746 static int serverinfo_find_extension(const unsigned char *serverinfo,
747 size_t serverinfo_length,
748 unsigned int extension_type,
749 const unsigned char **extension_data,
750 size_t *extension_length)
754 *extension_data = NULL;
755 *extension_length = 0;
756 if (serverinfo == NULL || serverinfo_length == 0)
759 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
763 unsigned int type = 0;
764 unsigned long context = 0;
766 /* end of serverinfo */
767 if (PACKET_remaining(&pkt) == 0)
768 return 0; /* Extension not found */
770 if (!PACKET_get_net_4(&pkt, &context)
771 || !PACKET_get_net_2(&pkt, &type)
772 || !PACKET_get_length_prefixed_2(&pkt, &data))
775 if (type == extension_type) {
776 *extension_data = PACKET_data(&data);
777 *extension_length = PACKET_remaining(&data);;
778 return 1; /* Success */
784 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
785 unsigned int context,
786 const unsigned char *in,
787 size_t inlen, X509 *x, size_t chainidx,
792 *al = SSL_AD_DECODE_ERROR;
799 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
800 const unsigned char *in,
801 size_t inlen, int *al, void *arg)
803 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
807 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
808 unsigned int context,
809 const unsigned char **out,
810 size_t *outlen, X509 *x, size_t chainidx,
813 const unsigned char *serverinfo = NULL;
814 size_t serverinfo_length = 0;
816 /* We only support extensions for the first Certificate */
817 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
820 /* Is there serverinfo data for the chosen server cert? */
821 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
822 &serverinfo_length)) != 0) {
823 /* Find the relevant extension from the serverinfo */
824 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
825 ext_type, out, outlen);
827 *al = SSL_AD_INTERNAL_ERROR;
828 return -1; /* Error */
831 return 0; /* No extension found, don't send extension */
832 return 1; /* Send extension */
834 return 0; /* No serverinfo data found, don't send
838 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
839 const unsigned char **out, size_t *outlen,
842 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
847 * With a NULL context, this function just checks that the serverinfo data
848 * parses correctly. With a non-NULL context, it registers callbacks for
849 * the included extensions.
851 static int serverinfo_process_buffer(unsigned int version,
852 const unsigned char *serverinfo,
853 size_t serverinfo_length, SSL_CTX *ctx)
857 if (serverinfo == NULL || serverinfo_length == 0)
860 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
863 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
866 while (PACKET_remaining(&pkt)) {
867 unsigned long context = 0;
868 unsigned int ext_type = 0;
871 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
872 || !PACKET_get_net_2(&pkt, &ext_type)
873 || !PACKET_get_length_prefixed_2(&pkt, &data))
880 * The old style custom extensions API could be set separately for
881 * server/client, i.e. you could set one custom extension for a client,
882 * and *for the same extension in the same SSL_CTX* you could set a
883 * custom extension for the server as well. It seems quite weird to be
884 * setting a custom extension for both client and server in a single
885 * SSL_CTX - but theoretically possible. This isn't possible in the
886 * new API. Therefore, if we have V1 serverinfo we use the old API. We
887 * also use the old API even if we have V2 serverinfo but the context
888 * looks like an old style <= TLSv1.2 extension.
890 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
891 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
892 serverinfo_srv_add_cb,
894 serverinfo_srv_parse_cb,
898 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
899 serverinfoex_srv_add_cb,
901 serverinfoex_srv_parse_cb,
910 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
911 const unsigned char *serverinfo,
912 size_t serverinfo_length)
914 unsigned char *new_serverinfo;
916 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
917 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
920 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
922 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
925 if (ctx->cert->key == NULL) {
926 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
929 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
931 if (new_serverinfo == NULL) {
932 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
935 ctx->cert->key->serverinfo = new_serverinfo;
936 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
937 ctx->cert->key->serverinfo_length = serverinfo_length;
940 * Now that the serverinfo is validated and stored, go ahead and
941 * register callbacks.
943 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
945 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
951 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
952 size_t serverinfo_length)
954 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
958 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
960 unsigned char *serverinfo = NULL;
962 size_t serverinfo_length = 0;
963 unsigned char *extension = 0;
964 long extension_length = 0;
967 static const char namePrefix1[] = "SERVERINFO FOR ";
968 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
969 unsigned int name_len;
972 size_t num_extensions = 0, contextoff = 0;
974 if (ctx == NULL || file == NULL) {
975 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
979 bin = BIO_new(BIO_s_file());
981 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
984 if (BIO_read_filename(bin, file) <= 0) {
985 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
989 for (num_extensions = 0;; num_extensions++) {
990 unsigned int version;
992 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
995 * There must be at least one extension in this file
997 if (num_extensions == 0) {
998 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
999 SSL_R_NO_PEM_EXTENSIONS);
1001 } else /* End of file, we're done */
1004 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
1005 name_len = strlen(name);
1006 if (name_len < sizeof(namePrefix1) - 1) {
1007 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
1010 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
1011 version = SSL_SERVERINFOV1;
1013 if (name_len < sizeof(namePrefix2) - 1) {
1014 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
1015 SSL_R_PEM_NAME_TOO_SHORT);
1018 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
1019 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
1020 SSL_R_PEM_NAME_BAD_PREFIX);
1023 version = SSL_SERVERINFOV2;
1026 * Check that the decoded PEM data is plausible (valid length field)
1028 if (version == SSL_SERVERINFOV1) {
1029 /* 4 byte header: 2 bytes type, 2 bytes len */
1030 if (extension_length < 4
1031 || (extension[2] << 8) + extension[3]
1032 != extension_length - 4) {
1033 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1037 * File does not have a context value so we must take account of
1042 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
1043 if (extension_length < 8
1044 || (extension[6] << 8) + extension[7]
1045 != extension_length - 8) {
1046 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1050 /* Append the decoded extension to the serverinfo buffer */
1051 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1054 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1058 if (contextoff > 0) {
1059 unsigned char *sinfo = serverinfo + serverinfo_length;
1061 /* We know this only uses the last 2 bytes */
1064 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1065 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1067 memcpy(serverinfo + serverinfo_length + contextoff,
1068 extension, extension_length);
1069 serverinfo_length += extension_length + contextoff;
1073 OPENSSL_free(header);
1075 OPENSSL_free(extension);
1079 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1082 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1084 OPENSSL_free(header);
1085 OPENSSL_free(extension);
1086 OPENSSL_free(serverinfo);
1091 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1092 STACK_OF(X509) *chain, int override)
1098 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1099 STACK_OF(X509) *dup_chain = NULL;
1100 EVP_PKEY *pubkey = NULL;
1102 /* Do all security checks before anything else */
1103 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1105 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1108 for (j = 0; j < sk_X509_num(chain); j++) {
1109 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1111 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1116 pubkey = X509_get_pubkey(x509); /* bumps reference */
1119 if (privatekey == NULL) {
1120 privatekey = pubkey;
1122 /* For RSA, which has no parameters, missing returns 0 */
1123 if (EVP_PKEY_missing_parameters(privatekey)) {
1124 if (EVP_PKEY_missing_parameters(pubkey)) {
1125 /* nobody has parameters? - error */
1126 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
1129 /* copy to privatekey from pubkey */
1130 EVP_PKEY_copy_parameters(privatekey, pubkey);
1132 } else if (EVP_PKEY_missing_parameters(pubkey)) {
1133 /* copy to pubkey from privatekey */
1134 EVP_PKEY_copy_parameters(pubkey, privatekey);
1135 } /* else both have parameters */
1137 /* Copied from ssl_set_cert/pkey */
1138 #ifndef OPENSSL_NO_RSA
1139 if ((EVP_PKEY_id(privatekey) == EVP_PKEY_RSA) &&
1140 ((RSA_flags(EVP_PKEY_get0_RSA(privatekey)) & RSA_METHOD_FLAG_NO_CHECK)))
1144 /* check that key <-> cert match */
1145 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
1146 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
1150 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1151 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1155 if (!override && (c->pkeys[i].x509 != NULL
1156 || c->pkeys[i].privatekey != NULL
1157 || c->pkeys[i].chain != NULL)) {
1158 /* No override, and something already there */
1159 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
1163 if (chain != NULL) {
1164 dup_chain = X509_chain_up_ref(chain);
1165 if (dup_chain == NULL) {
1166 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
1171 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1172 c->pkeys[i].chain = dup_chain;
1174 X509_free(c->pkeys[i].x509);
1176 c->pkeys[i].x509 = x509;
1178 EVP_PKEY_free(c->pkeys[i].privatekey);
1179 EVP_PKEY_up_ref(privatekey);
1180 c->pkeys[i].privatekey = privatekey;
1182 c->key = &(c->pkeys[i]);
1186 EVP_PKEY_free(pubkey);
1190 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1191 STACK_OF(X509) *chain, int override)
1193 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1196 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1197 STACK_OF(X509) *chain, int override)
1199 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);