1 /* $OpenBSD: ssl_rsa.c,v 1.28 2017/02/07 02:08:38 beck 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 SSLerror(ssl
, ERR_R_PASSED_NULL_PARAMETER
);
80 if (!ssl_cert_inst(&ssl
->cert
)) {
81 SSLerror(ssl
, 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 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 (!ssl_cert_inst(&ssl
->cert
)) {
158 SSLerror(ssl
, ERR_R_MALLOC_FAILURE
);
161 if ((pkey
= EVP_PKEY_new()) == NULL
) {
162 SSLerror(ssl
, 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 SSLerrorx(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 SSLerror(ssl
, ERR_R_BUF_LIB
);
229 if (BIO_read_filename(in
, file
) <= 0) {
230 SSLerror(ssl
, 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 SSLerror(ssl
, SSL_R_BAD_SSL_FILETYPE
);
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 SSLerror(ssl
, ERR_R_ASN1_LIB
);
269 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
275 SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
280 SSLerror(ssl
, ERR_R_PASSED_NULL_PARAMETER
);
283 if (!ssl_cert_inst(&ssl
->cert
)) {
284 SSLerror(ssl
, 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 SSLerror(ssl
, ERR_R_BUF_LIB
);
304 if (BIO_read_filename(in
, file
) <= 0) {
305 SSLerror(ssl
, 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 SSLerror(ssl
, SSL_R_BAD_SSL_FILETYPE
);
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 SSLerror(ssl
, ERR_R_ASN1_LIB
);
344 ret
= SSL_use_PrivateKey(ssl
, pkey
);
350 SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
353 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
356 if (!ssl_cert_inst(&ctx
->internal
->cert
)) {
357 SSLerrorx(ERR_R_MALLOC_FAILURE
);
360 return (ssl_set_cert(ctx
->internal
->cert
, x
));
364 ssl_set_cert(CERT
*c
, X509
*x
)
369 pkey
= X509_get_pubkey(x
);
371 SSLerrorx(SSL_R_X509_LIB
);
375 i
= ssl_cert_type(x
, pkey
);
377 SSLerrorx(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 SSLerrorx(ERR_R_BUF_LIB
);
434 if (BIO_read_filename(in
, file
) <= 0) {
435 SSLerrorx(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 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
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 SSLerrorx(ERR_R_ASN1_LIB
);
474 ret
= SSL_CTX_use_certificate(ctx
, x
);
480 SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
486 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
489 if (!ssl_cert_inst(&ctx
->internal
->cert
)) {
490 SSLerrorx(ERR_R_MALLOC_FAILURE
);
493 if ((pkey
= EVP_PKEY_new()) == NULL
) {
494 SSLerrorx(ERR_R_EVP_LIB
);
499 EVP_PKEY_assign_RSA(pkey
, rsa
);
501 ret
= ssl_set_pkey(ctx
->internal
->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 SSLerrorx(ERR_R_BUF_LIB
);
519 if (BIO_read_filename(in
, file
) <= 0) {
520 SSLerrorx(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 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
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 SSLerrorx(ERR_R_ASN1_LIB
);
559 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
565 SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
568 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER
);
571 if (!ssl_cert_inst(&ctx
->internal
->cert
)) {
572 SSLerrorx(ERR_R_MALLOC_FAILURE
);
575 return (ssl_set_pkey(ctx
->internal
->cert
, pkey
));
579 SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
583 EVP_PKEY
*pkey
= NULL
;
585 in
= BIO_new(BIO_s_file_internal());
587 SSLerrorx(ERR_R_BUF_LIB
);
591 if (BIO_read_filename(in
, file
) <= 0) {
592 SSLerrorx(ERR_R_SYS_LIB
);
595 if (type
== SSL_FILETYPE_PEM
) {
597 pkey
= PEM_read_bio_PrivateKey(in
, NULL
,
598 ctx
->default_passwd_callback
,
599 ctx
->default_passwd_callback_userdata
);
600 } else if (type
== SSL_FILETYPE_ASN1
) {
602 pkey
= d2i_PrivateKey_bio(in
, NULL
);
604 SSLerrorx(SSL_R_BAD_SSL_FILETYPE
);
611 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
619 SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
, const unsigned char *d
,
623 const unsigned char *p
;
627 if ((pkey
= d2i_PrivateKey(type
, NULL
, &p
,(long)len
)) == NULL
) {
628 SSLerrorx(ERR_R_ASN1_LIB
);
632 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
639 * Read a bio that contains our certificate in "PEM" format,
640 * possibly followed by a sequence of CA certificates that should be
641 * sent to the peer in the Certificate message.
644 ssl_ctx_use_certificate_chain_bio(SSL_CTX
*ctx
, BIO
*in
)
649 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
651 x
= PEM_read_bio_X509_AUX(in
, NULL
, ctx
->default_passwd_callback
,
652 ctx
->default_passwd_callback_userdata
);
654 SSLerrorx(ERR_R_PEM_LIB
);
658 ret
= SSL_CTX_use_certificate(ctx
, x
);
660 if (ERR_peek_error() != 0)
662 /* Key/certificate mismatch doesn't imply ret==0 ... */
665 * If we could set up our certificate, now proceed to
666 * the CA certificates.
672 sk_X509_pop_free(ctx
->extra_certs
, X509_free
);
673 ctx
->extra_certs
= NULL
;
675 while ((ca
= PEM_read_bio_X509(in
, NULL
,
676 ctx
->default_passwd_callback
,
677 ctx
->default_passwd_callback_userdata
)) != NULL
) {
678 r
= SSL_CTX_add_extra_chain_cert(ctx
, ca
);
685 * Note that we must not free r if it was successfully
686 * added to the chain (while we must free the main
687 * certificate, since its reference count is increased
688 * by SSL_CTX_use_certificate).
692 /* When the while loop ends, it's usually just EOF. */
693 err
= ERR_peek_last_error();
694 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
&&
695 ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
698 ret
= 0; /* some real error */
707 SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
712 in
= BIO_new(BIO_s_file_internal());
714 SSLerrorx(ERR_R_BUF_LIB
);
718 if (BIO_read_filename(in
, file
) <= 0) {
719 SSLerrorx(ERR_R_SYS_LIB
);
723 ret
= ssl_ctx_use_certificate_chain_bio(ctx
, in
);
731 SSL_CTX_use_certificate_chain_mem(SSL_CTX
*ctx
, void *buf
, int len
)
736 in
= BIO_new_mem_buf(buf
, len
);
738 SSLerrorx(ERR_R_BUF_LIB
);
742 ret
= ssl_ctx_use_certificate_chain_bio(ctx
, in
);