1 /* $OpenBSD: ssl_rsa.c,v 1.20 2015/02/06 01:37:11 reyk 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.]
63 #include <openssl/bio.h>
64 #include <openssl/evp.h>
65 #include <openssl/objects.h>
66 #include <openssl/pem.h>
67 #include <openssl/x509.h>
69 static int ssl_set_cert(CERT
*c
, X509
*x509
);
70 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
71 static int ssl_ctx_use_certificate_chain_bio(SSL_CTX
*, BIO
*);
74 SSL_use_certificate(SSL
*ssl
, X509
*x
)
77 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
80 if (!ssl_cert_inst(&ssl
->cert
)) {
81 SSLerr(SSL_F_SSL_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
84 return (ssl_set_cert(ssl
->cert
, x
));
88 SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
95 in
= BIO_new(BIO_s_file_internal());
97 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
101 if (BIO_read_filename(in
, file
) <= 0) {
102 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, 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 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
119 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
, j
);
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 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
142 ret
= SSL_use_certificate(ssl
, x
);
148 SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
157 if (!ssl_cert_inst(&ssl
->cert
)) {
158 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
161 if ((pkey
= EVP_PKEY_new()) == NULL
) {
162 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
167 EVP_PKEY_assign_RSA(pkey
, rsa
);
169 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
175 ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
179 i
= ssl_cert_type(NULL
, pkey
);
181 SSLerr(SSL_F_SSL_SET_PKEY
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
185 if (c
->pkeys
[i
].x509
!= NULL
) {
187 pktmp
= X509_get_pubkey(c
->pkeys
[i
].x509
);
188 EVP_PKEY_copy_parameters(pktmp
, pkey
);
189 EVP_PKEY_free(pktmp
);
193 * Don't check the public/private key, this is mostly
196 if ((pkey
->type
== EVP_PKEY_RSA
) &&
197 (RSA_flags(pkey
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
))
200 if (!X509_check_private_key(c
->pkeys
[i
].x509
, pkey
)) {
201 X509_free(c
->pkeys
[i
].x509
);
202 c
->pkeys
[i
].x509
= NULL
;
207 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
208 CRYPTO_add(&pkey
->references
, 1, CRYPTO_LOCK_EVP_PKEY
);
209 c
->pkeys
[i
].privatekey
= pkey
;
210 c
->key
= &(c
->pkeys
[i
]);
217 SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
223 in
= BIO_new(BIO_s_file_internal());
225 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
229 if (BIO_read_filename(in
, file
) <= 0) {
230 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
233 if (type
== SSL_FILETYPE_ASN1
) {
235 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
236 } else if (type
== SSL_FILETYPE_PEM
) {
238 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
239 ssl
->ctx
->default_passwd_callback
,
240 ssl
->ctx
->default_passwd_callback_userdata
);
242 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
246 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
, j
);
249 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
257 SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, unsigned char *d
, long len
)
260 const unsigned char *p
;
264 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
,(long)len
)) == NULL
) {
265 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
269 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
275 SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
280 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
283 if (!ssl_cert_inst(&ssl
->cert
)) {
284 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
287 ret
= ssl_set_pkey(ssl
->cert
, pkey
);
292 SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
296 EVP_PKEY
*pkey
= NULL
;
298 in
= BIO_new(BIO_s_file_internal());
300 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
304 if (BIO_read_filename(in
, file
) <= 0) {
305 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
308 if (type
== SSL_FILETYPE_PEM
) {
310 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
311 ssl
->ctx
->default_passwd_callback
,
312 ssl
->ctx
->default_passwd_callback_userdata
);
313 } else if (type
== SSL_FILETYPE_ASN1
) {
315 pkey
= d2i_PrivateKey_bio(in
, NULL
);
317 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
321 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
, j
);
324 ret
= SSL_use_PrivateKey(ssl
, pkey
);
332 SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
, long len
)
335 const unsigned char *p
;
339 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
,(long)len
)) == NULL
) {
340 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
344 ret
= SSL_use_PrivateKey(ssl
, pkey
);
350 SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
353 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_PASSED_NULL_PARAMETER
);
356 if (!ssl_cert_inst(&ctx
->cert
)) {
357 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
360 return (ssl_set_cert(ctx
->cert
, x
));
364 ssl_set_cert(CERT
*c
, X509
*x
)
369 pkey
= X509_get_pubkey(x
);
371 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_X509_LIB
);
375 i
= ssl_cert_type(x
, pkey
);
377 SSLerr(SSL_F_SSL_SET_CERT
, SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
382 if (c
->pkeys
[i
].privatekey
!= NULL
) {
383 EVP_PKEY_copy_parameters(pkey
, c
->pkeys
[i
].privatekey
);
387 * Don't check the public/private key, this is mostly
390 if ((c
->pkeys
[i
].privatekey
->type
== EVP_PKEY_RSA
) &&
391 (RSA_flags(c
->pkeys
[i
].privatekey
->pkey
.rsa
) &
392 RSA_METHOD_FLAG_NO_CHECK
))
395 if (!X509_check_private_key(x
, c
->pkeys
[i
].privatekey
)) {
397 * don't fail for a cert/key mismatch, just free
398 * current private key (when switching to a different
399 * cert & key, first this function should be used,
402 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
403 c
->pkeys
[i
].privatekey
= NULL
;
404 /* clear error queue */
411 X509_free(c
->pkeys
[i
].x509
);
412 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
413 c
->pkeys
[i
].x509
= x
;
414 c
->key
= &(c
->pkeys
[i
]);
421 SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
428 in
= BIO_new(BIO_s_file_internal());
430 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_BUF_LIB
);
434 if (BIO_read_filename(in
, file
) <= 0) {
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, ERR_R_SYS_LIB
);
438 if (type
== SSL_FILETYPE_ASN1
) {
440 x
= d2i_X509_bio(in
, NULL
);
441 } else if (type
== SSL_FILETYPE_PEM
) {
443 x
= PEM_read_bio_X509(in
, NULL
, ctx
->default_passwd_callback
,
444 ctx
->default_passwd_callback_userdata
);
446 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, SSL_R_BAD_SSL_FILETYPE
);
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
, j
);
455 ret
= SSL_CTX_use_certificate(ctx
, x
);
463 SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
468 x
= d2i_X509(NULL
, &d
,(long)len
);
470 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
, ERR_R_ASN1_LIB
);
474 ret
= SSL_CTX_use_certificate(ctx
, x
);
480 SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
486 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_PASSED_NULL_PARAMETER
);
489 if (!ssl_cert_inst(&ctx
->cert
)) {
490 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
493 if ((pkey
= EVP_PKEY_new()) == NULL
) {
494 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
, ERR_R_EVP_LIB
);
499 EVP_PKEY_assign_RSA(pkey
, rsa
);
501 ret
= ssl_set_pkey(ctx
->cert
, pkey
);
507 SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
513 in
= BIO_new(BIO_s_file_internal());
515 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
519 if (BIO_read_filename(in
, file
) <= 0) {
520 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
523 if (type
== SSL_FILETYPE_ASN1
) {
525 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
526 } else if (type
== SSL_FILETYPE_PEM
) {
528 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
529 ctx
->default_passwd_callback
,
530 ctx
->default_passwd_callback_userdata
);
532 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, SSL_R_BAD_SSL_FILETYPE
);
536 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
, j
);
539 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
547 SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
, long len
)
550 const unsigned char *p
;
554 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
,(long)len
)) == NULL
) {
555 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
559 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
565 SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
568 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
,
569 ERR_R_PASSED_NULL_PARAMETER
);
572 if (!ssl_cert_inst(&ctx
->cert
)) {
573 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
, ERR_R_MALLOC_FAILURE
);
576 return (ssl_set_pkey(ctx
->cert
, pkey
));
580 SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
584 EVP_PKEY
*pkey
= NULL
;
586 in
= BIO_new(BIO_s_file_internal());
588 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_BUF_LIB
);
592 if (BIO_read_filename(in
, file
) <= 0) {
593 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, ERR_R_SYS_LIB
);
596 if (type
== SSL_FILETYPE_PEM
) {
598 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
599 ctx
->default_passwd_callback
,
600 ctx
->default_passwd_callback_userdata
);
601 } else if (type
== SSL_FILETYPE_ASN1
) {
603 pkey
= d2i_PrivateKey_bio(in
, NULL
);
605 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,
606 SSL_R_BAD_SSL_FILETYPE
);
610 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
, j
);
613 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
621 SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
, const unsigned char *d
,
625 const unsigned char *p
;
629 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
,(long)len
)) == NULL
) {
630 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
, ERR_R_ASN1_LIB
);
634 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
641 * Read a bio that contains our certificate in "PEM" format,
642 * possibly followed by a sequence of CA certificates that should be
643 * sent to the peer in the Certificate message.
646 ssl_ctx_use_certificate_chain_bio(SSL_CTX
*ctx
, BIO
*in
)
651 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
653 x
= PEM_read_bio_X509_AUX(in
, NULL
, ctx
->default_passwd_callback
,
654 ctx
->default_passwd_callback_userdata
);
656 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_PEM_LIB
);
660 ret
= SSL_CTX_use_certificate(ctx
, x
);
662 if (ERR_peek_error() != 0)
664 /* Key/certificate mismatch doesn't imply ret==0 ... */
667 * If we could set up our certificate, now proceed to
668 * the CA certificates.
674 if (ctx
->extra_certs
!= NULL
) {
675 sk_X509_pop_free(ctx
->extra_certs
, X509_free
);
676 ctx
->extra_certs
= NULL
;
679 while ((ca
= PEM_read_bio_X509(in
, NULL
,
680 ctx
->default_passwd_callback
,
681 ctx
->default_passwd_callback_userdata
)) != NULL
) {
682 r
= SSL_CTX_add_extra_chain_cert(ctx
, ca
);
689 * Note that we must not free r if it was successfully
690 * added to the chain (while we must free the main
691 * certificate, since its reference count is increased
692 * by SSL_CTX_use_certificate).
696 /* When the while loop ends, it's usually just EOF. */
697 err
= ERR_peek_last_error();
698 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
&&
699 ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
702 ret
= 0; /* some real error */
711 SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
716 in
= BIO_new(BIO_s_file_internal());
718 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
722 if (BIO_read_filename(in
, file
) <= 0) {
723 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_SYS_LIB
);
727 ret
= ssl_ctx_use_certificate_chain_bio(ctx
, in
);
735 SSL_CTX_use_certificate_chain_mem(SSL_CTX
*ctx
, void *buf
, int len
)
740 in
= BIO_new_mem_buf(buf
, len
);
742 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
, ERR_R_BUF_LIB
);
746 ret
= ssl_ctx_use_certificate_chain_bio(ctx
, in
);