1 /* $OpenBSD: ssl_rsa.c,v 1.48 2022/08/31 20:49:37 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
61 #include <openssl/bio.h>
62 #include <openssl/evp.h>
63 #include <openssl/objects.h>
64 #include <openssl/pem.h>
65 #include <openssl/x509.h>
69 static int ssl_get_password_cb_and_arg(SSL_CTX
*ctx
, SSL
*ssl
,
70 pem_password_cb
**passwd_cb
, void **passwd_arg
);
71 static int ssl_set_cert(SSL_CTX
*ctx
, SSL
*ssl
, X509
*x509
);
72 static int ssl_set_pkey(SSL_CTX
*ctx
, SSL
*ssl
, EVP_PKEY
*pkey
);
73 static int ssl_use_certificate_chain_bio(SSL_CTX
*ctx
, SSL
*ssl
, BIO
*in
);
74 static int ssl_use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
,
78 SSL_use_certificate(SSL
*ssl
, X509
*x
)
81 SSLerror(ssl
, ERR_R_PASSED_NULL_PARAMETER
);
84 return ssl_set_cert(NULL
, ssl
, x
);
88 SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
95 in
= BIO_new(BIO_s_file());
97 SSLerror(ssl
, ERR_R_BUF_LIB
);
101 if (BIO_read_filename(in
, file
) <= 0) {
102 SSLerror(ssl
, ERR_R_SYS_LIB
);
105 if (type
== SSL_FILETYPE_ASN1
) {
107 x
= d2i_X509_bio(in
, NULL
);
108 } else if (type
== SSL_FILETYPE_PEM
) {
110 x
= PEM_read_bio_X509(in
, NULL
,
111 ssl
->ctx
->default_passwd_callback
,
112 ssl
->ctx
->default_passwd_callback_userdata
);
114 SSLerror(ssl
, SSL_R_BAD_SSL_FILETYPE
);
123 ret
= SSL_use_certificate(ssl
, x
);
131 SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
136 x
= d2i_X509(NULL
, &d
, (long)len
);
138 SSLerror(ssl
, ERR_R_ASN1_LIB
);
142 ret
= SSL_use_certificate(ssl
, x
);
148 SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
154 SSLerror(ssl
, ERR_R_PASSED_NULL_PARAMETER
);
157 if ((pkey
= EVP_PKEY_new()) == NULL
) {
158 SSLerror(ssl
, ERR_R_EVP_LIB
);
163 EVP_PKEY_assign_RSA(pkey
, rsa
);
165 ret
= ssl_set_pkey(NULL
, ssl
, pkey
);
171 ssl_set_pkey(SSL_CTX
*ctx
, SSL
*ssl
, EVP_PKEY
*pkey
)
176 i
= ssl_cert_type(pkey
);
178 SSLerrorx(SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
182 if ((c
= ssl_get0_cert(ctx
, ssl
)) == NULL
)
185 if (c
->pkeys
[i
].x509
!= NULL
) {
188 if ((pktmp
= X509_get0_pubkey(c
->pkeys
[i
].x509
)) == NULL
)
192 * Callers of EVP_PKEY_copy_parameters() can't distinguish
193 * errors from the absence of a param_copy() method. So
194 * pretend it can never fail.
196 EVP_PKEY_copy_parameters(pktmp
, pkey
);
201 * Don't check the public/private key, this is mostly
204 if (EVP_PKEY_id(pkey
) != EVP_PKEY_RSA
||
205 !(RSA_flags(EVP_PKEY_get0_RSA(pkey
)) & RSA_METHOD_FLAG_NO_CHECK
)) {
206 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
207 X509_free(c
->pkeys
[i
].x509
);
208 c
->pkeys
[i
].x509
= NULL
;
214 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
215 EVP_PKEY_up_ref(pkey
);
216 c
->pkeys
[i
].privatekey
= pkey
;
217 c
->key
= &(c
->pkeys
[i
]);
224 SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
230 in
= BIO_new(BIO_s_file());
232 SSLerror(ssl
, ERR_R_BUF_LIB
);
236 if (BIO_read_filename(in
, file
) <= 0) {
237 SSLerror(ssl
, ERR_R_SYS_LIB
);
240 if (type
== SSL_FILETYPE_ASN1
) {
242 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
243 } else if (type
== SSL_FILETYPE_PEM
) {
245 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
246 ssl
->ctx
->default_passwd_callback
,
247 ssl
->ctx
->default_passwd_callback_userdata
);
249 SSLerror(ssl
, SSL_R_BAD_SSL_FILETYPE
);
256 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
264 SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
269 if ((rsa
= d2i_RSAPrivateKey(NULL
, &d
, (long)len
)) == NULL
) {
270 SSLerror(ssl
, ERR_R_ASN1_LIB
);
274 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
280 SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
285 SSLerror(ssl
, ERR_R_PASSED_NULL_PARAMETER
);
288 ret
= ssl_set_pkey(NULL
, ssl
, pkey
);
293 SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
297 EVP_PKEY
*pkey
= NULL
;
299 in
= BIO_new(BIO_s_file());
301 SSLerror(ssl
, ERR_R_BUF_LIB
);
305 if (BIO_read_filename(in
, file
) <= 0) {
306 SSLerror(ssl
, ERR_R_SYS_LIB
);
309 if (type
== SSL_FILETYPE_PEM
) {
311 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
312 ssl
->ctx
->default_passwd_callback
,
313 ssl
->ctx
->default_passwd_callback_userdata
);
314 } else if (type
== SSL_FILETYPE_ASN1
) {
316 pkey
= d2i_PrivateKey_bio(in
, NULL
);
318 SSLerror(ssl
, SSL_R_BAD_SSL_FILETYPE
);
325 ret
= SSL_use_PrivateKey(ssl
, pkey
);
333 SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
, long len
)
338 if ((pkey
= d2i_PrivateKey(type
, NULL
, &d
, (long)len
)) == NULL
) {
339 SSLerror(ssl
, ERR_R_ASN1_LIB
);
343 ret
= SSL_use_PrivateKey(ssl
, pkey
);
349 SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
352 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
355 return ssl_set_cert(ctx
, NULL
, x
);
359 ssl_get_password_cb_and_arg(SSL_CTX
*ctx
, SSL
*ssl
,
360 pem_password_cb
**passwd_cb
, void **passwd_arg
)
365 *passwd_cb
= ctx
->default_passwd_callback
;
366 *passwd_arg
= ctx
->default_passwd_callback_userdata
;
372 ssl_set_cert(SSL_CTX
*ctx
, SSL
*ssl
, X509
*x
)
379 if (!ssl_security_cert(ctx
, ssl
, x
, 1, &ssl_err
)) {
384 if ((c
= ssl_get0_cert(ctx
, ssl
)) == NULL
)
387 pkey
= X509_get_pubkey(x
);
389 SSLerrorx(SSL_R_X509_LIB
);
393 i
= ssl_cert_type(pkey
);
395 SSLerrorx(SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
400 if (c
->pkeys
[i
].privatekey
!= NULL
) {
401 EVP_PKEY
*priv_key
= c
->pkeys
[i
].privatekey
;
403 EVP_PKEY_copy_parameters(pkey
, priv_key
);
407 * Don't check the public/private key, this is mostly
410 if (EVP_PKEY_id(priv_key
) != EVP_PKEY_RSA
||
411 !(RSA_flags(EVP_PKEY_get0_RSA(priv_key
)) & RSA_METHOD_FLAG_NO_CHECK
)) {
412 if (!X509_check_private_key(x
, priv_key
)) {
414 * don't fail for a cert/key mismatch, just free
415 * current private key (when switching to a
416 * different cert & key, first this function
417 * should be used, then ssl_set_pkey.
419 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
420 c
->pkeys
[i
].privatekey
= NULL
;
428 X509_free(c
->pkeys
[i
].x509
);
430 c
->pkeys
[i
].x509
= x
;
431 c
->key
= &(c
->pkeys
[i
]);
438 SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
445 in
= BIO_new(BIO_s_file());
447 SSLerrorx(ERR_R_BUF_LIB
);
451 if (BIO_read_filename(in
, file
) <= 0) {
452 SSLerrorx(ERR_R_SYS_LIB
);
455 if (type
== SSL_FILETYPE_ASN1
) {
457 x
= d2i_X509_bio(in
, NULL
);
458 } else if (type
== SSL_FILETYPE_PEM
) {
460 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
461 ctx
->default_passwd_callback_userdata
);
463 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
472 ret
= SSL_CTX_use_certificate(ctx
, x
);
480 SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
485 x
= d2i_X509(NULL
, &d
, (long)len
);
487 SSLerrorx(ERR_R_ASN1_LIB
);
491 ret
= SSL_CTX_use_certificate(ctx
, x
);
497 SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
503 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
506 if ((pkey
= EVP_PKEY_new()) == NULL
) {
507 SSLerrorx(ERR_R_EVP_LIB
);
512 EVP_PKEY_assign_RSA(pkey
, rsa
);
514 ret
= ssl_set_pkey(ctx
, NULL
, pkey
);
520 SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
526 in
= BIO_new(BIO_s_file());
528 SSLerrorx(ERR_R_BUF_LIB
);
532 if (BIO_read_filename(in
, file
) <= 0) {
533 SSLerrorx(ERR_R_SYS_LIB
);
536 if (type
== SSL_FILETYPE_ASN1
) {
538 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
539 } else if (type
== SSL_FILETYPE_PEM
) {
541 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
542 ctx
->default_passwd_callback
,
543 ctx
->default_passwd_callback_userdata
);
545 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
552 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
560 SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
, long len
)
565 if ((rsa
= d2i_RSAPrivateKey(NULL
, &d
, (long)len
)) == NULL
) {
566 SSLerrorx(ERR_R_ASN1_LIB
);
570 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
576 SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
579 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
582 return ssl_set_pkey(ctx
, NULL
, pkey
);
586 SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
590 EVP_PKEY
*pkey
= NULL
;
592 in
= BIO_new(BIO_s_file());
594 SSLerrorx(ERR_R_BUF_LIB
);
598 if (BIO_read_filename(in
, file
) <= 0) {
599 SSLerrorx(ERR_R_SYS_LIB
);
602 if (type
== SSL_FILETYPE_PEM
) {
604 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
605 ctx
->default_passwd_callback
,
606 ctx
->default_passwd_callback_userdata
);
607 } else if (type
== SSL_FILETYPE_ASN1
) {
609 pkey
= d2i_PrivateKey_bio(in
, NULL
);
611 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
618 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
626 SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
, const unsigned char *d
,
632 if ((pkey
= d2i_PrivateKey(type
, NULL
, &d
, (long)len
)) == NULL
) {
633 SSLerrorx(ERR_R_ASN1_LIB
);
637 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
644 * Read a bio that contains our certificate in "PEM" format,
645 * possibly followed by a sequence of CA certificates that should be
646 * sent to the peer in the Certificate message.
649 ssl_use_certificate_chain_bio(SSL_CTX
*ctx
, SSL
*ssl
, BIO
*in
)
651 pem_password_cb
*passwd_cb
;
657 if (!ssl_get_password_cb_and_arg(ctx
, ssl
, &passwd_cb
, &passwd_arg
))
660 if ((x
= PEM_read_bio_X509_AUX(in
, NULL
, passwd_cb
, passwd_arg
)) ==
662 SSLerrorx(ERR_R_PEM_LIB
);
666 if (!ssl_set_cert(ctx
, ssl
, x
))
669 if (!ssl_cert_set0_chain(ctx
, ssl
, NULL
))
672 /* Process any additional CA certificates. */
673 while ((ca
= PEM_read_bio_X509(in
, NULL
, passwd_cb
, passwd_arg
)) !=
675 if (!ssl_cert_add0_chain_cert(ctx
, ssl
, ca
)) {
681 /* When the while loop ends, it's usually just EOF. */
682 err
= ERR_peek_last_error();
683 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
&&
684 ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
) {
696 ssl_use_certificate_chain_file(SSL_CTX
*ctx
, SSL
*ssl
, const char *file
)
701 in
= BIO_new(BIO_s_file());
703 SSLerrorx(ERR_R_BUF_LIB
);
707 if (BIO_read_filename(in
, file
) <= 0) {
708 SSLerrorx(ERR_R_SYS_LIB
);
712 ret
= ssl_use_certificate_chain_bio(ctx
, ssl
, in
);
720 SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
722 return ssl_use_certificate_chain_file(ctx
, NULL
, file
);
726 SSL_use_certificate_chain_file(SSL
*ssl
, const char *file
)
728 return ssl_use_certificate_chain_file(NULL
, ssl
, file
);
732 SSL_CTX_use_certificate_chain_mem(SSL_CTX
*ctx
, void *buf
, int len
)
737 in
= BIO_new_mem_buf(buf
, len
);
739 SSLerrorx(ERR_R_BUF_LIB
);
743 ret
= ssl_use_certificate_chain_bio(ctx
, NULL
, in
);