2 * Copyright 1995-2018 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);
35 if (!X509v3_cache_extensions(x, ssl->ctx->libctx, ssl->ctx->propq)) {
36 SSLerr(0, ERR_LIB_X509);
39 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
41 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
45 return ssl_set_cert(ssl->cert, x);
48 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
55 in = BIO_new(BIO_s_file());
57 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
61 if (BIO_read_filename(in, file) <= 0) {
62 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
65 if (type == SSL_FILETYPE_ASN1) {
67 x = d2i_X509_bio(in, NULL);
68 } else if (type == SSL_FILETYPE_PEM) {
70 x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
71 ssl->default_passwd_callback_userdata);
73 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
78 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
82 ret = SSL_use_certificate(ssl, x);
89 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
94 x = d2i_X509(NULL, &d, (long)len);
96 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
100 ret = SSL_use_certificate(ssl, x);
105 #ifndef OPENSSL_NO_RSA
106 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
112 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
115 if ((pkey = EVP_PKEY_new()) == NULL) {
116 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
121 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
127 ret = ssl_set_pkey(ssl->cert, pkey);
133 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
137 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
138 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
142 if (c->pkeys[i].x509 != NULL) {
144 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
146 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
150 * The return code from EVP_PKEY_copy_parameters is deliberately
151 * ignored. Some EVP_PKEY types cannot do this.
153 EVP_PKEY_copy_parameters(pktmp, pkey);
156 #ifndef OPENSSL_NO_RSA
158 * Don't check the public/private key, this is mostly for smart
161 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
162 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
165 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
166 X509_free(c->pkeys[i].x509);
167 c->pkeys[i].x509 = NULL;
172 EVP_PKEY_free(c->pkeys[i].privatekey);
173 EVP_PKEY_up_ref(pkey);
174 c->pkeys[i].privatekey = pkey;
175 c->key = &c->pkeys[i];
179 #ifndef OPENSSL_NO_RSA
180 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
186 in = BIO_new(BIO_s_file());
188 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
192 if (BIO_read_filename(in, file) <= 0) {
193 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
196 if (type == SSL_FILETYPE_ASN1) {
198 rsa = d2i_RSAPrivateKey_bio(in, NULL);
199 } else if (type == SSL_FILETYPE_PEM) {
201 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
202 ssl->default_passwd_callback,
203 ssl->default_passwd_callback_userdata);
205 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
209 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
212 ret = SSL_use_RSAPrivateKey(ssl, rsa);
219 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
222 const unsigned char *p;
226 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
227 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
231 ret = SSL_use_RSAPrivateKey(ssl, rsa);
235 #endif /* !OPENSSL_NO_RSA */
237 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
242 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
245 ret = ssl_set_pkey(ssl->cert, pkey);
249 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
253 EVP_PKEY *pkey = NULL;
255 in = BIO_new(BIO_s_file());
257 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
261 if (BIO_read_filename(in, file) <= 0) {
262 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
265 if (type == SSL_FILETYPE_PEM) {
267 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
268 ssl->default_passwd_callback,
269 ssl->default_passwd_callback_userdata,
272 } else if (type == SSL_FILETYPE_ASN1) {
274 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
277 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
281 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
284 ret = SSL_use_PrivateKey(ssl, pkey);
291 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
295 const unsigned char *p;
299 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
300 ssl->ctx->propq)) == NULL) {
301 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
305 ret = SSL_use_PrivateKey(ssl, pkey);
310 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
314 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
317 if (!X509v3_cache_extensions(x, ctx->libctx, ctx->propq)) {
318 SSLerr(0, ERR_LIB_X509);
321 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
323 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
326 return ssl_set_cert(ctx->cert, x);
329 static int ssl_set_cert(CERT *c, X509 *x)
334 pkey = X509_get0_pubkey(x);
336 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
340 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
341 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
344 #ifndef OPENSSL_NO_EC
345 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
346 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
350 if (c->pkeys[i].privatekey != NULL) {
352 * The return code from EVP_PKEY_copy_parameters is deliberately
353 * ignored. Some EVP_PKEY types cannot do this.
355 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
358 #ifndef OPENSSL_NO_RSA
360 * Don't check the public/private key, this is mostly for smart
363 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
364 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
365 RSA_METHOD_FLAG_NO_CHECK) ;
367 #endif /* OPENSSL_NO_RSA */
368 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
370 * don't fail for a cert/key mismatch, just free current private
371 * key (when switching to a different cert & key, first this
372 * function should be used, then ssl_set_pkey
374 EVP_PKEY_free(c->pkeys[i].privatekey);
375 c->pkeys[i].privatekey = NULL;
376 /* clear error queue */
381 X509_free(c->pkeys[i].x509);
383 c->pkeys[i].x509 = x;
384 c->key = &(c->pkeys[i]);
389 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
396 in = BIO_new(BIO_s_file());
398 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
402 if (BIO_read_filename(in, file) <= 0) {
403 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
406 if (type == SSL_FILETYPE_ASN1) {
408 x = d2i_X509_bio(in, NULL);
409 } else if (type == SSL_FILETYPE_PEM) {
411 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
412 ctx->default_passwd_callback_userdata);
414 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
419 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
423 ret = SSL_CTX_use_certificate(ctx, x);
430 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
435 x = d2i_X509(NULL, &d, (long)len);
437 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
441 ret = SSL_CTX_use_certificate(ctx, x);
446 #ifndef OPENSSL_NO_RSA
447 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
453 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
456 if ((pkey = EVP_PKEY_new()) == NULL) {
457 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
462 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
468 ret = ssl_set_pkey(ctx->cert, pkey);
473 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
479 in = BIO_new(BIO_s_file());
481 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
485 if (BIO_read_filename(in, file) <= 0) {
486 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
489 if (type == SSL_FILETYPE_ASN1) {
491 rsa = d2i_RSAPrivateKey_bio(in, NULL);
492 } else if (type == SSL_FILETYPE_PEM) {
494 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
495 ctx->default_passwd_callback,
496 ctx->default_passwd_callback_userdata);
498 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
502 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
505 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
512 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
516 const unsigned char *p;
520 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
521 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
525 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
529 #endif /* !OPENSSL_NO_RSA */
531 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
534 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
537 return ssl_set_pkey(ctx->cert, pkey);
540 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
544 EVP_PKEY *pkey = NULL;
546 in = BIO_new(BIO_s_file());
548 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
552 if (BIO_read_filename(in, file) <= 0) {
553 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
556 if (type == SSL_FILETYPE_PEM) {
558 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
559 ctx->default_passwd_callback,
560 ctx->default_passwd_callback_userdata,
561 ctx->libctx, ctx->propq);
562 } else if (type == SSL_FILETYPE_ASN1) {
564 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
566 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
570 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
573 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
580 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
581 const unsigned char *d, long len)
584 const unsigned char *p;
588 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
589 ctx->propq)) == NULL) {
590 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
594 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
600 * Read a file that contains our certificate in "PEM" format, possibly
601 * followed by a sequence of CA certificates that should be sent to the peer
602 * in the Certificate message.
604 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
609 pem_password_cb *passwd_callback;
610 void *passwd_callback_userdata;
612 ERR_clear_error(); /* clear error stack for
613 * SSL_CTX_use_certificate() */
616 passwd_callback = ctx->default_passwd_callback;
617 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
619 passwd_callback = ssl->default_passwd_callback;
620 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
623 in = BIO_new(BIO_s_file());
625 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
629 if (BIO_read_filename(in, file) <= 0) {
630 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
634 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
635 passwd_callback_userdata);
637 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
642 ret = SSL_CTX_use_certificate(ctx, x);
644 ret = SSL_use_certificate(ssl, x);
646 if (ERR_peek_error() != 0)
647 ret = 0; /* Key/certificate mismatch doesn't imply
651 * If we could set up our certificate, now proceed to the CA
659 r = SSL_CTX_clear_chain_certs(ctx);
661 r = SSL_clear_chain_certs(ssl);
668 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
669 passwd_callback_userdata))
672 r = SSL_CTX_add0_chain_cert(ctx, ca);
674 r = SSL_add0_chain_cert(ssl, ca);
676 * Note that we must not free ca if it was successfully added to
677 * the chain (while we must free the main certificate, since its
678 * reference count is increased by SSL_CTX_use_certificate).
686 /* When the while loop ends, it's usually just EOF. */
687 err = ERR_peek_last_error();
688 if (ERR_GET_LIB(err) == ERR_LIB_PEM
689 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
692 ret = 0; /* some real error */
701 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
703 return use_certificate_chain_file(ctx, NULL, file);
706 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
708 return use_certificate_chain_file(NULL, ssl, file);
711 static int serverinfo_find_extension(const unsigned char *serverinfo,
712 size_t serverinfo_length,
713 unsigned int extension_type,
714 const unsigned char **extension_data,
715 size_t *extension_length)
719 *extension_data = NULL;
720 *extension_length = 0;
721 if (serverinfo == NULL || serverinfo_length == 0)
724 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
728 unsigned int type = 0;
729 unsigned long context = 0;
731 /* end of serverinfo */
732 if (PACKET_remaining(&pkt) == 0)
733 return 0; /* Extension not found */
735 if (!PACKET_get_net_4(&pkt, &context)
736 || !PACKET_get_net_2(&pkt, &type)
737 || !PACKET_get_length_prefixed_2(&pkt, &data))
740 if (type == extension_type) {
741 *extension_data = PACKET_data(&data);
742 *extension_length = PACKET_remaining(&data);;
743 return 1; /* Success */
749 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
750 unsigned int context,
751 const unsigned char *in,
752 size_t inlen, X509 *x, size_t chainidx,
757 *al = SSL_AD_DECODE_ERROR;
764 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
765 const unsigned char *in,
766 size_t inlen, int *al, void *arg)
768 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
772 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
773 unsigned int context,
774 const unsigned char **out,
775 size_t *outlen, X509 *x, size_t chainidx,
778 const unsigned char *serverinfo = NULL;
779 size_t serverinfo_length = 0;
781 /* We only support extensions for the first Certificate */
782 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
785 /* Is there serverinfo data for the chosen server cert? */
786 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
787 &serverinfo_length)) != 0) {
788 /* Find the relevant extension from the serverinfo */
789 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
790 ext_type, out, outlen);
792 *al = SSL_AD_INTERNAL_ERROR;
793 return -1; /* Error */
796 return 0; /* No extension found, don't send extension */
797 return 1; /* Send extension */
799 return 0; /* No serverinfo data found, don't send
803 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
804 const unsigned char **out, size_t *outlen,
807 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
812 * With a NULL context, this function just checks that the serverinfo data
813 * parses correctly. With a non-NULL context, it registers callbacks for
814 * the included extensions.
816 static int serverinfo_process_buffer(unsigned int version,
817 const unsigned char *serverinfo,
818 size_t serverinfo_length, SSL_CTX *ctx)
822 if (serverinfo == NULL || serverinfo_length == 0)
825 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
828 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
831 while (PACKET_remaining(&pkt)) {
832 unsigned long context = 0;
833 unsigned int ext_type = 0;
836 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
837 || !PACKET_get_net_2(&pkt, &ext_type)
838 || !PACKET_get_length_prefixed_2(&pkt, &data))
845 * The old style custom extensions API could be set separately for
846 * server/client, i.e. you could set one custom extension for a client,
847 * and *for the same extension in the same SSL_CTX* you could set a
848 * custom extension for the server as well. It seems quite weird to be
849 * setting a custom extension for both client and server in a single
850 * SSL_CTX - but theoretically possible. This isn't possible in the
851 * new API. Therefore, if we have V1 serverinfo we use the old API. We
852 * also use the old API even if we have V2 serverinfo but the context
853 * looks like an old style <= TLSv1.2 extension.
855 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
856 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
857 serverinfo_srv_add_cb,
859 serverinfo_srv_parse_cb,
863 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
864 serverinfoex_srv_add_cb,
866 serverinfoex_srv_parse_cb,
875 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
876 const unsigned char *serverinfo,
877 size_t serverinfo_length)
879 unsigned char *new_serverinfo;
881 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
882 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
885 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
887 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
890 if (ctx->cert->key == NULL) {
891 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
894 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
896 if (new_serverinfo == NULL) {
897 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
900 ctx->cert->key->serverinfo = new_serverinfo;
901 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
902 ctx->cert->key->serverinfo_length = serverinfo_length;
905 * Now that the serverinfo is validated and stored, go ahead and
906 * register callbacks.
908 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
910 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
916 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
917 size_t serverinfo_length)
919 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
923 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
925 unsigned char *serverinfo = NULL;
927 size_t serverinfo_length = 0;
928 unsigned char *extension = 0;
929 long extension_length = 0;
932 static const char namePrefix1[] = "SERVERINFO FOR ";
933 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
934 unsigned int name_len;
937 size_t num_extensions = 0, contextoff = 0;
939 if (ctx == NULL || file == NULL) {
940 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
944 bin = BIO_new(BIO_s_file());
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
949 if (BIO_read_filename(bin, file) <= 0) {
950 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
954 for (num_extensions = 0;; num_extensions++) {
955 unsigned int version;
957 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
960 * There must be at least one extension in this file
962 if (num_extensions == 0) {
963 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
964 SSL_R_NO_PEM_EXTENSIONS);
966 } else /* End of file, we're done */
969 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
970 name_len = strlen(name);
971 if (name_len < sizeof(namePrefix1) - 1) {
972 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
975 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
976 version = SSL_SERVERINFOV1;
978 if (name_len < sizeof(namePrefix2) - 1) {
979 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
980 SSL_R_PEM_NAME_TOO_SHORT);
983 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
984 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
985 SSL_R_PEM_NAME_BAD_PREFIX);
988 version = SSL_SERVERINFOV2;
991 * Check that the decoded PEM data is plausible (valid length field)
993 if (version == SSL_SERVERINFOV1) {
994 /* 4 byte header: 2 bytes type, 2 bytes len */
995 if (extension_length < 4
996 || (extension[2] << 8) + extension[3]
997 != extension_length - 4) {
998 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1002 * File does not have a context value so we must take account of
1007 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
1008 if (extension_length < 8
1009 || (extension[6] << 8) + extension[7]
1010 != extension_length - 8) {
1011 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
1015 /* Append the decoded extension to the serverinfo buffer */
1016 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1019 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
1023 if (contextoff > 0) {
1024 unsigned char *sinfo = serverinfo + serverinfo_length;
1026 /* We know this only uses the last 2 bytes */
1029 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1030 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1032 memcpy(serverinfo + serverinfo_length + contextoff,
1033 extension, extension_length);
1034 serverinfo_length += extension_length + contextoff;
1038 OPENSSL_free(header);
1040 OPENSSL_free(extension);
1044 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1047 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1049 OPENSSL_free(header);
1050 OPENSSL_free(extension);
1051 OPENSSL_free(serverinfo);
1056 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1057 STACK_OF(X509) *chain, int override)
1063 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1064 SSL_CTX *actualctx = ssl == NULL ? ctx : ssl->ctx;
1065 STACK_OF(X509) *dup_chain = NULL;
1066 EVP_PKEY *pubkey = NULL;
1068 if (!X509v3_cache_extensions(x509, actualctx->libctx, actualctx->propq)) {
1069 SSLerr(0, ERR_R_X509_LIB);
1073 /* Do all security checks before anything else */
1074 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1076 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1079 for (j = 0; j < sk_X509_num(chain); j++) {
1080 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1082 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
1087 pubkey = X509_get_pubkey(x509); /* bumps reference */
1090 if (privatekey == NULL) {
1091 privatekey = pubkey;
1093 /* For RSA, which has no parameters, missing returns 0 */
1094 if (EVP_PKEY_missing_parameters(privatekey)) {
1095 if (EVP_PKEY_missing_parameters(pubkey)) {
1096 /* nobody has parameters? - error */
1097 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
1100 /* copy to privatekey from pubkey */
1101 EVP_PKEY_copy_parameters(privatekey, pubkey);
1103 } else if (EVP_PKEY_missing_parameters(pubkey)) {
1104 /* copy to pubkey from privatekey */
1105 EVP_PKEY_copy_parameters(pubkey, privatekey);
1106 } /* else both have parameters */
1108 /* Copied from ssl_set_cert/pkey */
1109 #ifndef OPENSSL_NO_RSA
1110 if ((EVP_PKEY_id(privatekey) == EVP_PKEY_RSA) &&
1111 ((RSA_flags(EVP_PKEY_get0_RSA(privatekey)) & RSA_METHOD_FLAG_NO_CHECK)))
1115 /* check that key <-> cert match */
1116 if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
1117 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
1121 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1122 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1126 if (!override && (c->pkeys[i].x509 != NULL
1127 || c->pkeys[i].privatekey != NULL
1128 || c->pkeys[i].chain != NULL)) {
1129 /* No override, and something already there */
1130 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
1134 if (chain != NULL) {
1135 dup_chain = X509_chain_up_ref(chain);
1136 if (dup_chain == NULL) {
1137 SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
1142 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1143 c->pkeys[i].chain = dup_chain;
1145 X509_free(c->pkeys[i].x509);
1147 c->pkeys[i].x509 = x509;
1149 EVP_PKEY_free(c->pkeys[i].privatekey);
1150 EVP_PKEY_up_ref(privatekey);
1151 c->pkeys[i].privatekey = privatekey;
1153 c->key = &(c->pkeys[i]);
1157 EVP_PKEY_free(pubkey);
1161 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1162 STACK_OF(X509) *chain, int override)
1164 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1167 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1168 STACK_OF(X509) *chain, int override)
1170 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);