2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "ssl_local.h"
12 #include "internal/packet.h"
13 #include <openssl/bio.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pem.h>
20 static int ssl_set_cert(CERT
*c
, X509
*x509
);
21 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
23 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
24 | SSL_EXT_CLIENT_HELLO \
25 | SSL_EXT_TLS1_2_SERVER_HELLO \
26 | SSL_EXT_IGNORE_ON_RESUMPTION)
28 int SSL_use_certificate(SSL
*ssl
, X509
*x
)
32 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
36 rv
= ssl_security_cert(ssl
, NULL
, x
, 0, 1);
38 ERR_raise(ERR_LIB_SSL
, rv
);
42 return ssl_set_cert(ssl
->cert
, x
);
45 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
50 X509
*cert
= NULL
, *x
= NULL
;
52 in
= BIO_new(BIO_s_file());
54 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
58 if (BIO_read_filename(in
, file
) <= 0) {
59 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
63 if (type
!= SSL_FILETYPE_ASN1
&& type
!= SSL_FILETYPE_PEM
) {
64 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
67 x
= X509_new_ex(ssl
->ctx
->libctx
, ssl
->ctx
->propq
);
69 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
72 if (type
== SSL_FILETYPE_ASN1
) {
74 cert
= d2i_X509_bio(in
, &x
);
75 } else if (type
== SSL_FILETYPE_PEM
) {
77 cert
= PEM_read_bio_X509(in
, &x
, ssl
->default_passwd_callback
,
78 ssl
->default_passwd_callback_userdata
);
80 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
85 ERR_raise(ERR_LIB_SSL
, j
);
89 ret
= SSL_use_certificate(ssl
, x
);
96 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
101 x
= X509_new_ex(ssl
->ctx
->libctx
, ssl
->ctx
->propq
);
103 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
107 if (d2i_X509(&x
, &d
, (long)len
)== NULL
) {
109 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
113 ret
= SSL_use_certificate(ssl
, x
);
118 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
122 if (ssl_cert_lookup_by_pkey(pkey
, &i
) == NULL
) {
123 ERR_raise(ERR_LIB_SSL
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
127 if (c
->pkeys
[i
].x509
!= NULL
) {
129 pktmp
= X509_get0_pubkey(c
->pkeys
[i
].x509
);
131 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
135 * The return code from EVP_PKEY_copy_parameters is deliberately
136 * ignored. Some EVP_PKEY types cannot do this.
138 EVP_PKEY_copy_parameters(pktmp
, pkey
);
141 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
142 X509_free(c
->pkeys
[i
].x509
);
143 c
->pkeys
[i
].x509
= NULL
;
148 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
149 EVP_PKEY_up_ref(pkey
);
150 c
->pkeys
[i
].privatekey
= pkey
;
151 c
->key
= &c
->pkeys
[i
];
155 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
160 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
163 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
167 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
171 EVP_PKEY
*pkey
= NULL
;
173 in
= BIO_new(BIO_s_file());
175 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
179 if (BIO_read_filename(in
, file
) <= 0) {
180 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
183 if (type
== SSL_FILETYPE_PEM
) {
185 pkey
= PEM_read_bio_PrivateKey_ex(in
, NULL
,
186 ssl
->default_passwd_callback
,
187 ssl
->default_passwd_callback_userdata
,
190 } else if (type
== SSL_FILETYPE_ASN1
) {
192 pkey
= d2i_PrivateKey_ex_bio(in
, NULL
, ssl
->ctx
->libctx
,
195 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
199 ERR_raise(ERR_LIB_SSL
, j
);
202 ret
= SSL_use_PrivateKey(ssl
, pkey
);
209 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
,
213 const unsigned char *p
;
217 if ((pkey
= d2i_PrivateKey_ex(type
, NULL
, &p
, (long)len
, ssl
->ctx
->libctx
,
218 ssl
->ctx
->propq
)) == NULL
) {
219 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
223 ret
= SSL_use_PrivateKey(ssl
, pkey
);
228 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
232 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
236 rv
= ssl_security_cert(NULL
, ctx
, x
, 0, 1);
238 ERR_raise(ERR_LIB_SSL
, rv
);
241 return ssl_set_cert(ctx
->cert
, x
);
244 static int ssl_set_cert(CERT
*c
, X509
*x
)
249 pkey
= X509_get0_pubkey(x
);
251 ERR_raise(ERR_LIB_SSL
, SSL_R_X509_LIB
);
255 if (ssl_cert_lookup_by_pkey(pkey
, &i
) == NULL
) {
256 ERR_raise(ERR_LIB_SSL
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
259 #ifndef OPENSSL_NO_EC
260 if (i
== SSL_PKEY_ECC
&& !EVP_PKEY_can_sign(pkey
)) {
261 ERR_raise(ERR_LIB_SSL
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
265 if (c
->pkeys
[i
].privatekey
!= NULL
) {
267 * The return code from EVP_PKEY_copy_parameters is deliberately
268 * ignored. Some EVP_PKEY types cannot do this.
270 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
273 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
275 * don't fail for a cert/key mismatch, just free current private
276 * key (when switching to a different cert & key, first this
277 * function should be used, then ssl_set_pkey
279 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
280 c
->pkeys
[i
].privatekey
= NULL
;
281 /* clear error queue */
286 X509_free(c
->pkeys
[i
].x509
);
288 c
->pkeys
[i
].x509
= x
;
289 c
->key
= &(c
->pkeys
[i
]);
294 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
296 int j
= SSL_R_BAD_VALUE
;
299 X509
*x
= NULL
, *cert
= NULL
;
301 in
= BIO_new(BIO_s_file());
303 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
307 if (BIO_read_filename(in
, file
) <= 0) {
308 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
311 if (type
!= SSL_FILETYPE_ASN1
&& type
!= SSL_FILETYPE_PEM
) {
312 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
315 x
= X509_new_ex(ctx
->libctx
, ctx
->propq
);
317 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
320 if (type
== SSL_FILETYPE_ASN1
) {
322 cert
= d2i_X509_bio(in
, &x
);
323 } else if (type
== SSL_FILETYPE_PEM
) {
325 cert
= PEM_read_bio_X509(in
, &x
, ctx
->default_passwd_callback
,
326 ctx
->default_passwd_callback_userdata
);
329 ERR_raise(ERR_LIB_SSL
, j
);
333 ret
= SSL_CTX_use_certificate(ctx
, x
);
340 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
345 x
= X509_new_ex(ctx
->libctx
, ctx
->propq
);
347 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
351 if (d2i_X509(&x
, &d
, (long)len
) == NULL
) {
353 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
357 ret
= SSL_CTX_use_certificate(ctx
, x
);
362 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
365 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
368 return ssl_set_pkey(ctx
->cert
, pkey
);
371 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
375 EVP_PKEY
*pkey
= NULL
;
377 in
= BIO_new(BIO_s_file());
379 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
383 if (BIO_read_filename(in
, file
) <= 0) {
384 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
387 if (type
== SSL_FILETYPE_PEM
) {
389 pkey
= PEM_read_bio_PrivateKey_ex(in
, NULL
,
390 ctx
->default_passwd_callback
,
391 ctx
->default_passwd_callback_userdata
,
392 ctx
->libctx
, ctx
->propq
);
393 } else if (type
== SSL_FILETYPE_ASN1
) {
395 pkey
= d2i_PrivateKey_ex_bio(in
, NULL
, ctx
->libctx
, ctx
->propq
);
397 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
401 ERR_raise(ERR_LIB_SSL
, j
);
404 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
411 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
,
412 const unsigned char *d
, long len
)
415 const unsigned char *p
;
419 if ((pkey
= d2i_PrivateKey_ex(type
, NULL
, &p
, (long)len
, ctx
->libctx
,
420 ctx
->propq
)) == NULL
) {
421 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
425 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
431 * Read a file that contains our certificate in "PEM" format, possibly
432 * followed by a sequence of CA certificates that should be sent to the peer
433 * in the Certificate message.
435 static int use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
, const char *file
)
440 pem_password_cb
*passwd_callback
;
441 void *passwd_callback_userdata
;
442 SSL_CTX
*real_ctx
= (ssl
== NULL
) ? ctx
: ssl
->ctx
;
444 ERR_clear_error(); /* clear error stack for
445 * SSL_CTX_use_certificate() */
448 passwd_callback
= ctx
->default_passwd_callback
;
449 passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
451 passwd_callback
= ssl
->default_passwd_callback
;
452 passwd_callback_userdata
= ssl
->default_passwd_callback_userdata
;
455 in
= BIO_new(BIO_s_file());
457 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
461 if (BIO_read_filename(in
, file
) <= 0) {
462 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
466 x
= X509_new_ex(real_ctx
->libctx
, real_ctx
->propq
);
468 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
471 if (PEM_read_bio_X509_AUX(in
, &x
, passwd_callback
,
472 passwd_callback_userdata
) == NULL
) {
473 ERR_raise(ERR_LIB_SSL
, ERR_R_PEM_LIB
);
478 ret
= SSL_CTX_use_certificate(ctx
, x
);
480 ret
= SSL_use_certificate(ssl
, x
);
482 if (ERR_peek_error() != 0)
483 ret
= 0; /* Key/certificate mismatch doesn't imply
487 * If we could set up our certificate, now proceed to the CA
495 r
= SSL_CTX_clear_chain_certs(ctx
);
497 r
= SSL_clear_chain_certs(ssl
);
505 ca
= X509_new_ex(real_ctx
->libctx
, real_ctx
->propq
);
507 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
510 if (PEM_read_bio_X509(in
, &ca
, passwd_callback
,
511 passwd_callback_userdata
) != NULL
) {
513 r
= SSL_CTX_add0_chain_cert(ctx
, ca
);
515 r
= SSL_add0_chain_cert(ssl
, ca
);
517 * Note that we must not free ca if it was successfully added to
518 * the chain (while we must free the main certificate, since its
519 * reference count is increased by SSL_CTX_use_certificate).
531 /* When the while loop ends, it's usually just EOF. */
532 err
= ERR_peek_last_error();
533 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
534 && ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
537 ret
= 0; /* some real error */
546 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
548 return use_certificate_chain_file(ctx
, NULL
, file
);
551 int SSL_use_certificate_chain_file(SSL
*ssl
, const char *file
)
553 return use_certificate_chain_file(NULL
, ssl
, file
);
556 static int serverinfo_find_extension(const unsigned char *serverinfo
,
557 size_t serverinfo_length
,
558 unsigned int extension_type
,
559 const unsigned char **extension_data
,
560 size_t *extension_length
)
564 *extension_data
= NULL
;
565 *extension_length
= 0;
566 if (serverinfo
== NULL
|| serverinfo_length
== 0)
569 if (!PACKET_buf_init(&pkt
, serverinfo
, serverinfo_length
))
573 unsigned int type
= 0;
574 unsigned long context
= 0;
576 /* end of serverinfo */
577 if (PACKET_remaining(&pkt
) == 0)
578 return 0; /* Extension not found */
580 if (!PACKET_get_net_4(&pkt
, &context
)
581 || !PACKET_get_net_2(&pkt
, &type
)
582 || !PACKET_get_length_prefixed_2(&pkt
, &data
))
585 if (type
== extension_type
) {
586 *extension_data
= PACKET_data(&data
);
587 *extension_length
= PACKET_remaining(&data
);;
588 return 1; /* Success */
594 static int serverinfoex_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
595 unsigned int context
,
596 const unsigned char *in
,
597 size_t inlen
, X509
*x
, size_t chainidx
,
602 *al
= SSL_AD_DECODE_ERROR
;
609 static int serverinfo_srv_parse_cb(SSL
*s
, unsigned int ext_type
,
610 const unsigned char *in
,
611 size_t inlen
, int *al
, void *arg
)
613 return serverinfoex_srv_parse_cb(s
, ext_type
, 0, in
, inlen
, NULL
, 0, al
,
617 static int serverinfoex_srv_add_cb(SSL
*s
, unsigned int ext_type
,
618 unsigned int context
,
619 const unsigned char **out
,
620 size_t *outlen
, X509
*x
, size_t chainidx
,
623 const unsigned char *serverinfo
= NULL
;
624 size_t serverinfo_length
= 0;
626 /* We only support extensions for the first Certificate */
627 if ((context
& SSL_EXT_TLS1_3_CERTIFICATE
) != 0 && chainidx
> 0)
630 /* Is there serverinfo data for the chosen server cert? */
631 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
632 &serverinfo_length
)) != 0) {
633 /* Find the relevant extension from the serverinfo */
634 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
635 ext_type
, out
, outlen
);
637 *al
= SSL_AD_INTERNAL_ERROR
;
638 return -1; /* Error */
641 return 0; /* No extension found, don't send extension */
642 return 1; /* Send extension */
644 return 0; /* No serverinfo data found, don't send
648 static int serverinfo_srv_add_cb(SSL
*s
, unsigned int ext_type
,
649 const unsigned char **out
, size_t *outlen
,
652 return serverinfoex_srv_add_cb(s
, ext_type
, 0, out
, outlen
, NULL
, 0, al
,
657 * With a NULL context, this function just checks that the serverinfo data
658 * parses correctly. With a non-NULL context, it registers callbacks for
659 * the included extensions.
661 static int serverinfo_process_buffer(unsigned int version
,
662 const unsigned char *serverinfo
,
663 size_t serverinfo_length
, SSL_CTX
*ctx
)
667 if (serverinfo
== NULL
|| serverinfo_length
== 0)
670 if (version
!= SSL_SERVERINFOV1
&& version
!= SSL_SERVERINFOV2
)
673 if (!PACKET_buf_init(&pkt
, serverinfo
, serverinfo_length
))
676 while (PACKET_remaining(&pkt
)) {
677 unsigned long context
= 0;
678 unsigned int ext_type
= 0;
681 if ((version
== SSL_SERVERINFOV2
&& !PACKET_get_net_4(&pkt
, &context
))
682 || !PACKET_get_net_2(&pkt
, &ext_type
)
683 || !PACKET_get_length_prefixed_2(&pkt
, &data
))
690 * The old style custom extensions API could be set separately for
691 * server/client, i.e. you could set one custom extension for a client,
692 * and *for the same extension in the same SSL_CTX* you could set a
693 * custom extension for the server as well. It seems quite weird to be
694 * setting a custom extension for both client and server in a single
695 * SSL_CTX - but theoretically possible. This isn't possible in the
696 * new API. Therefore, if we have V1 serverinfo we use the old API. We
697 * also use the old API even if we have V2 serverinfo but the context
698 * looks like an old style <= TLSv1.2 extension.
700 if (version
== SSL_SERVERINFOV1
|| context
== SYNTHV1CONTEXT
) {
701 if (!SSL_CTX_add_server_custom_ext(ctx
, ext_type
,
702 serverinfo_srv_add_cb
,
704 serverinfo_srv_parse_cb
,
708 if (!SSL_CTX_add_custom_ext(ctx
, ext_type
, context
,
709 serverinfoex_srv_add_cb
,
711 serverinfoex_srv_parse_cb
,
720 int SSL_CTX_use_serverinfo_ex(SSL_CTX
*ctx
, unsigned int version
,
721 const unsigned char *serverinfo
,
722 size_t serverinfo_length
)
724 unsigned char *new_serverinfo
;
726 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0) {
727 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
730 if (!serverinfo_process_buffer(version
, serverinfo
, serverinfo_length
,
732 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_SERVERINFO_DATA
);
735 if (ctx
->cert
->key
== NULL
) {
736 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
739 new_serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
741 if (new_serverinfo
== NULL
) {
742 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
745 ctx
->cert
->key
->serverinfo
= new_serverinfo
;
746 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
747 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
750 * Now that the serverinfo is validated and stored, go ahead and
751 * register callbacks.
753 if (!serverinfo_process_buffer(version
, serverinfo
, serverinfo_length
,
755 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_SERVERINFO_DATA
);
761 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
762 size_t serverinfo_length
)
764 return SSL_CTX_use_serverinfo_ex(ctx
, SSL_SERVERINFOV1
, serverinfo
,
768 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
770 unsigned char *serverinfo
= NULL
;
772 size_t serverinfo_length
= 0;
773 unsigned char *extension
= 0;
774 long extension_length
= 0;
777 static const char namePrefix1
[] = "SERVERINFO FOR ";
778 static const char namePrefix2
[] = "SERVERINFOV2 FOR ";
779 unsigned int name_len
;
782 size_t num_extensions
= 0, contextoff
= 0;
784 if (ctx
== NULL
|| file
== NULL
) {
785 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
789 bin
= BIO_new(BIO_s_file());
791 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
794 if (BIO_read_filename(bin
, file
) <= 0) {
795 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
799 for (num_extensions
= 0;; num_extensions
++) {
800 unsigned int version
;
802 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
)
805 * There must be at least one extension in this file
807 if (num_extensions
== 0) {
808 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_PEM_EXTENSIONS
);
810 } else /* End of file, we're done */
813 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
814 name_len
= strlen(name
);
815 if (name_len
< sizeof(namePrefix1
) - 1) {
816 ERR_raise(ERR_LIB_SSL
, SSL_R_PEM_NAME_TOO_SHORT
);
819 if (strncmp(name
, namePrefix1
, sizeof(namePrefix1
) - 1) == 0) {
820 version
= SSL_SERVERINFOV1
;
822 if (name_len
< sizeof(namePrefix2
) - 1) {
823 ERR_raise(ERR_LIB_SSL
, SSL_R_PEM_NAME_TOO_SHORT
);
826 if (strncmp(name
, namePrefix2
, sizeof(namePrefix2
) - 1) != 0) {
827 ERR_raise(ERR_LIB_SSL
, SSL_R_PEM_NAME_BAD_PREFIX
);
830 version
= SSL_SERVERINFOV2
;
833 * Check that the decoded PEM data is plausible (valid length field)
835 if (version
== SSL_SERVERINFOV1
) {
836 /* 4 byte header: 2 bytes type, 2 bytes len */
837 if (extension_length
< 4
838 || (extension
[2] << 8) + extension
[3]
839 != extension_length
- 4) {
840 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_DATA
);
844 * File does not have a context value so we must take account of
849 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
850 if (extension_length
< 8
851 || (extension
[6] << 8) + extension
[7]
852 != extension_length
- 8) {
853 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_DATA
);
857 /* Append the decoded extension to the serverinfo buffer */
858 tmp
= OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
861 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
865 if (contextoff
> 0) {
866 unsigned char *sinfo
= serverinfo
+ serverinfo_length
;
868 /* We know this only uses the last 2 bytes */
871 sinfo
[2] = (SYNTHV1CONTEXT
>> 8) & 0xff;
872 sinfo
[3] = SYNTHV1CONTEXT
& 0xff;
874 memcpy(serverinfo
+ serverinfo_length
+ contextoff
,
875 extension
, extension_length
);
876 serverinfo_length
+= extension_length
+ contextoff
;
880 OPENSSL_free(header
);
882 OPENSSL_free(extension
);
886 ret
= SSL_CTX_use_serverinfo_ex(ctx
, SSL_SERVERINFOV2
, serverinfo
,
889 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
891 OPENSSL_free(header
);
892 OPENSSL_free(extension
);
893 OPENSSL_free(serverinfo
);
898 static int ssl_set_cert_and_key(SSL
*ssl
, SSL_CTX
*ctx
, X509
*x509
, EVP_PKEY
*privatekey
,
899 STACK_OF(X509
) *chain
, int override
)
905 CERT
*c
= ssl
!= NULL
? ssl
->cert
: ctx
->cert
;
906 STACK_OF(X509
) *dup_chain
= NULL
;
907 EVP_PKEY
*pubkey
= NULL
;
909 /* Do all security checks before anything else */
910 rv
= ssl_security_cert(ssl
, ctx
, x509
, 0, 1);
912 ERR_raise(ERR_LIB_SSL
, rv
);
915 for (j
= 0; j
< sk_X509_num(chain
); j
++) {
916 rv
= ssl_security_cert(ssl
, ctx
, sk_X509_value(chain
, j
), 0, 0);
918 ERR_raise(ERR_LIB_SSL
, rv
);
923 pubkey
= X509_get_pubkey(x509
); /* bumps reference */
926 if (privatekey
== NULL
) {
929 /* For RSA, which has no parameters, missing returns 0 */
930 if (EVP_PKEY_missing_parameters(privatekey
)) {
931 if (EVP_PKEY_missing_parameters(pubkey
)) {
932 /* nobody has parameters? - error */
933 ERR_raise(ERR_LIB_SSL
, SSL_R_MISSING_PARAMETERS
);
936 /* copy to privatekey from pubkey */
937 EVP_PKEY_copy_parameters(privatekey
, pubkey
);
939 } else if (EVP_PKEY_missing_parameters(pubkey
)) {
940 /* copy to pubkey from privatekey */
941 EVP_PKEY_copy_parameters(pubkey
, privatekey
);
942 } /* else both have parameters */
944 /* check that key <-> cert match */
945 if (EVP_PKEY_eq(pubkey
, privatekey
) != 1) {
946 ERR_raise(ERR_LIB_SSL
, SSL_R_PRIVATE_KEY_MISMATCH
);
950 if (ssl_cert_lookup_by_pkey(pubkey
, &i
) == NULL
) {
951 ERR_raise(ERR_LIB_SSL
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
955 if (!override
&& (c
->pkeys
[i
].x509
!= NULL
956 || c
->pkeys
[i
].privatekey
!= NULL
957 || c
->pkeys
[i
].chain
!= NULL
)) {
958 /* No override, and something already there */
959 ERR_raise(ERR_LIB_SSL
, SSL_R_NOT_REPLACING_CERTIFICATE
);
964 dup_chain
= X509_chain_up_ref(chain
);
965 if (dup_chain
== NULL
) {
966 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
971 sk_X509_pop_free(c
->pkeys
[i
].chain
, X509_free
);
972 c
->pkeys
[i
].chain
= dup_chain
;
974 X509_free(c
->pkeys
[i
].x509
);
976 c
->pkeys
[i
].x509
= x509
;
978 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
979 EVP_PKEY_up_ref(privatekey
);
980 c
->pkeys
[i
].privatekey
= privatekey
;
982 c
->key
= &(c
->pkeys
[i
]);
986 EVP_PKEY_free(pubkey
);
990 int SSL_use_cert_and_key(SSL
*ssl
, X509
*x509
, EVP_PKEY
*privatekey
,
991 STACK_OF(X509
) *chain
, int override
)
993 return ssl_set_cert_and_key(ssl
, NULL
, x509
, privatekey
, chain
, override
);
996 int SSL_CTX_use_cert_and_key(SSL_CTX
*ctx
, X509
*x509
, EVP_PKEY
*privatekey
,
997 STACK_OF(X509
) *chain
, int override
)
999 return ssl_set_cert_and_key(NULL
, ctx
, x509
, privatekey
, chain
, override
);