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/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
67 static int ssl_set_cert(CERT *c, X509 *x509);
68 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69 int SSL_use_certificate(SSL *ssl, X509 *x)
72 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
75 if (!ssl_cert_inst(&ssl->cert)) {
76 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
79 return (ssl_set_cert(ssl->cert, x));
82 #ifndef OPENSSL_NO_STDIO
83 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
90 in = BIO_new(BIO_s_file_internal());
92 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
96 if (BIO_read_filename(in, file) <= 0) {
97 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
100 if (type == SSL_FILETYPE_ASN1) {
102 x = d2i_X509_bio(in, NULL);
103 } else if (type == SSL_FILETYPE_PEM) {
105 x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
106 ssl->ctx->default_passwd_callback_userdata);
108 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
113 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
117 ret = SSL_use_certificate(ssl, x);
127 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
132 x = d2i_X509(NULL, &d, (long)len);
134 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
138 ret = SSL_use_certificate(ssl, x);
143 #ifndef OPENSSL_NO_RSA
144 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
150 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
153 if (!ssl_cert_inst(&ssl->cert)) {
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
157 if ((pkey = EVP_PKEY_new()) == NULL) {
158 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
163 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
168 ret = ssl_set_pkey(ssl->cert, pkey);
174 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
178 i = ssl_cert_type(NULL, pkey);
180 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
184 if (c->pkeys[i].x509 != NULL) {
186 pktmp = X509_get_pubkey(c->pkeys[i].x509);
188 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
189 EVP_PKEY_free(pktmp);
193 * The return code from EVP_PKEY_copy_parameters is deliberately
194 * ignored. Some EVP_PKEY types cannot do this.
196 EVP_PKEY_copy_parameters(pktmp, pkey);
197 EVP_PKEY_free(pktmp);
200 #ifndef OPENSSL_NO_RSA
202 * Don't check the public/private key, this is mostly for smart
205 if ((pkey->type == EVP_PKEY_RSA) &&
206 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
209 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
210 X509_free(c->pkeys[i].x509);
211 c->pkeys[i].x509 = NULL;
216 if (c->pkeys[i].privatekey != NULL)
217 EVP_PKEY_free(c->pkeys[i].privatekey);
218 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
219 c->pkeys[i].privatekey = pkey;
220 c->key = &(c->pkeys[i]);
226 #ifndef OPENSSL_NO_RSA
227 # ifndef OPENSSL_NO_STDIO
228 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
234 in = BIO_new(BIO_s_file_internal());
236 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
240 if (BIO_read_filename(in, file) <= 0) {
241 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
244 if (type == SSL_FILETYPE_ASN1) {
246 rsa = d2i_RSAPrivateKey_bio(in, NULL);
247 } else if (type == SSL_FILETYPE_PEM) {
249 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
250 ssl->ctx->default_passwd_callback,
252 ctx->default_passwd_callback_userdata);
254 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
258 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
261 ret = SSL_use_RSAPrivateKey(ssl, rsa);
270 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
273 const unsigned char *p;
277 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
278 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
282 ret = SSL_use_RSAPrivateKey(ssl, rsa);
286 #endif /* !OPENSSL_NO_RSA */
288 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
293 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
296 if (!ssl_cert_inst(&ssl->cert)) {
297 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
300 ret = ssl_set_pkey(ssl->cert, pkey);
304 #ifndef OPENSSL_NO_STDIO
305 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
309 EVP_PKEY *pkey = NULL;
311 in = BIO_new(BIO_s_file_internal());
313 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
317 if (BIO_read_filename(in, file) <= 0) {
318 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
321 if (type == SSL_FILETYPE_PEM) {
323 pkey = PEM_read_bio_PrivateKey(in, NULL,
324 ssl->ctx->default_passwd_callback,
326 ctx->default_passwd_callback_userdata);
327 } else if (type == SSL_FILETYPE_ASN1) {
329 pkey = d2i_PrivateKey_bio(in, NULL);
331 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
335 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
338 ret = SSL_use_PrivateKey(ssl, pkey);
347 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
351 const unsigned char *p;
355 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
356 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
360 ret = SSL_use_PrivateKey(ssl, pkey);
365 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
368 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
371 if (!ssl_cert_inst(&ctx->cert)) {
372 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
375 return (ssl_set_cert(ctx->cert, x));
378 static int ssl_set_cert(CERT *c, X509 *x)
383 pkey = X509_get_pubkey(x);
385 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
389 i = ssl_cert_type(x, pkey);
391 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
396 if (c->pkeys[i].privatekey != NULL) {
398 * The return code from EVP_PKEY_copy_parameters is deliberately
399 * ignored. Some EVP_PKEY types cannot do this.
401 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
404 #ifndef OPENSSL_NO_RSA
406 * Don't check the public/private key, this is mostly for smart
409 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
410 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
411 RSA_METHOD_FLAG_NO_CHECK)) ;
413 #endif /* OPENSSL_NO_RSA */
414 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
416 * don't fail for a cert/key mismatch, just free current private
417 * key (when switching to a different cert & key, first this
418 * function should be used, then ssl_set_pkey
420 EVP_PKEY_free(c->pkeys[i].privatekey);
421 c->pkeys[i].privatekey = NULL;
422 /* clear error queue */
429 if (c->pkeys[i].x509 != NULL)
430 X509_free(c->pkeys[i].x509);
431 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
432 c->pkeys[i].x509 = x;
433 c->key = &(c->pkeys[i]);
439 #ifndef OPENSSL_NO_STDIO
440 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
447 in = BIO_new(BIO_s_file_internal());
449 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
453 if (BIO_read_filename(in, file) <= 0) {
454 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
457 if (type == SSL_FILETYPE_ASN1) {
459 x = d2i_X509_bio(in, NULL);
460 } else if (type == SSL_FILETYPE_PEM) {
462 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
463 ctx->default_passwd_callback_userdata);
465 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
470 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
474 ret = SSL_CTX_use_certificate(ctx, x);
484 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
485 const unsigned char *d)
490 x = d2i_X509(NULL, &d, (long)len);
492 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
496 ret = SSL_CTX_use_certificate(ctx, x);
501 #ifndef OPENSSL_NO_RSA
502 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
508 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
511 if (!ssl_cert_inst(&ctx->cert)) {
512 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
515 if ((pkey = EVP_PKEY_new()) == NULL) {
516 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
521 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
526 ret = ssl_set_pkey(ctx->cert, pkey);
531 # ifndef OPENSSL_NO_STDIO
532 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
538 in = BIO_new(BIO_s_file_internal());
540 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
544 if (BIO_read_filename(in, file) <= 0) {
545 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
548 if (type == SSL_FILETYPE_ASN1) {
550 rsa = d2i_RSAPrivateKey_bio(in, NULL);
551 } else if (type == SSL_FILETYPE_PEM) {
553 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
554 ctx->default_passwd_callback,
555 ctx->default_passwd_callback_userdata);
557 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
561 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
564 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
573 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
577 const unsigned char *p;
581 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
582 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
586 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
590 #endif /* !OPENSSL_NO_RSA */
592 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
595 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
598 if (!ssl_cert_inst(&ctx->cert)) {
599 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
602 return (ssl_set_pkey(ctx->cert, pkey));
605 #ifndef OPENSSL_NO_STDIO
606 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
610 EVP_PKEY *pkey = NULL;
612 in = BIO_new(BIO_s_file_internal());
614 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
618 if (BIO_read_filename(in, file) <= 0) {
619 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
622 if (type == SSL_FILETYPE_PEM) {
624 pkey = PEM_read_bio_PrivateKey(in, NULL,
625 ctx->default_passwd_callback,
626 ctx->default_passwd_callback_userdata);
627 } else if (type == SSL_FILETYPE_ASN1) {
629 pkey = d2i_PrivateKey_bio(in, NULL);
631 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
635 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
638 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
647 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
648 const unsigned char *d, long len)
651 const unsigned char *p;
655 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
656 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
660 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
665 #ifndef OPENSSL_NO_STDIO
667 * Read a file that contains our certificate in "PEM" format, possibly
668 * followed by a sequence of CA certificates that should be sent to the peer
669 * in the Certificate message.
671 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
677 ERR_clear_error(); /* clear error stack for
678 * SSL_CTX_use_certificate() */
680 in = BIO_new(BIO_s_file_internal());
682 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
686 if (BIO_read_filename(in, file) <= 0) {
687 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
691 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
692 ctx->default_passwd_callback_userdata);
694 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
698 ret = SSL_CTX_use_certificate(ctx, x);
700 if (ERR_peek_error() != 0)
701 ret = 0; /* Key/certificate mismatch doesn't imply
705 * If we could set up our certificate, now proceed to the CA
712 if (ctx->extra_certs != NULL) {
713 sk_X509_pop_free(ctx->extra_certs, X509_free);
714 ctx->extra_certs = NULL;
717 while ((ca = PEM_read_bio_X509(in, NULL,
718 ctx->default_passwd_callback,
719 ctx->default_passwd_callback_userdata))
721 r = SSL_CTX_add_extra_chain_cert(ctx, ca);
728 * Note that we must not free r if it was successfully added to
729 * the chain (while we must free the main certificate, since its
730 * reference count is increased by SSL_CTX_use_certificate).
733 /* When the while loop ends, it's usually just EOF. */
734 err = ERR_peek_last_error();
735 if (ERR_GET_LIB(err) == ERR_LIB_PEM
736 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
739 ret = 0; /* some real error */