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 EVP_PKEY_assign_RSA(pkey, rsa);
165 ret = ssl_set_pkey(ssl->cert, pkey);
171 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
175 i = ssl_cert_type(NULL, pkey);
177 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
181 if (c->pkeys[i].x509 != NULL) {
183 pktmp = X509_get_pubkey(c->pkeys[i].x509);
184 EVP_PKEY_copy_parameters(pktmp, pkey);
185 EVP_PKEY_free(pktmp);
188 #ifndef OPENSSL_NO_RSA
190 * Don't check the public/private key, this is mostly for smart
193 if ((pkey->type == EVP_PKEY_RSA) &&
194 (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
197 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
198 X509_free(c->pkeys[i].x509);
199 c->pkeys[i].x509 = NULL;
204 if (c->pkeys[i].privatekey != NULL)
205 EVP_PKEY_free(c->pkeys[i].privatekey);
206 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
207 c->pkeys[i].privatekey = pkey;
208 c->key = &(c->pkeys[i]);
214 #ifndef OPENSSL_NO_RSA
215 # ifndef OPENSSL_NO_STDIO
216 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
222 in = BIO_new(BIO_s_file_internal());
224 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
228 if (BIO_read_filename(in, file) <= 0) {
229 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
232 if (type == SSL_FILETYPE_ASN1) {
234 rsa = d2i_RSAPrivateKey_bio(in, NULL);
235 } else if (type == SSL_FILETYPE_PEM) {
237 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
238 ssl->ctx->default_passwd_callback,
240 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);
258 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
261 const unsigned char *p;
265 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
266 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
270 ret = SSL_use_RSAPrivateKey(ssl, rsa);
274 #endif /* !OPENSSL_NO_RSA */
276 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
281 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
284 if (!ssl_cert_inst(&ssl->cert)) {
285 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
288 ret = ssl_set_pkey(ssl->cert, pkey);
292 #ifndef OPENSSL_NO_STDIO
293 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
297 EVP_PKEY *pkey = NULL;
299 in = BIO_new(BIO_s_file_internal());
301 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
305 if (BIO_read_filename(in, file) <= 0) {
306 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
309 if (type == SSL_FILETYPE_PEM) {
311 pkey = PEM_read_bio_PrivateKey(in, NULL,
312 ssl->ctx->default_passwd_callback,
314 ctx->default_passwd_callback_userdata);
315 } else if (type == SSL_FILETYPE_ASN1) {
317 pkey = d2i_PrivateKey_bio(in, NULL);
319 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
323 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
326 ret = SSL_use_PrivateKey(ssl, pkey);
335 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
339 const unsigned char *p;
343 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
344 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
348 ret = SSL_use_PrivateKey(ssl, pkey);
353 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
356 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
359 if (!ssl_cert_inst(&ctx->cert)) {
360 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
363 return (ssl_set_cert(ctx->cert, x));
366 static int ssl_set_cert(CERT *c, X509 *x)
371 pkey = X509_get_pubkey(x);
373 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
377 i = ssl_cert_type(x, pkey);
379 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
384 if (c->pkeys[i].privatekey != NULL) {
385 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
388 #ifndef OPENSSL_NO_RSA
390 * Don't check the public/private key, this is mostly for smart
393 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
394 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
395 RSA_METHOD_FLAG_NO_CHECK)) ;
397 #endif /* OPENSSL_NO_RSA */
398 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
400 * don't fail for a cert/key mismatch, just free current private
401 * key (when switching to a different cert & key, first this
402 * function should be used, then ssl_set_pkey
404 EVP_PKEY_free(c->pkeys[i].privatekey);
405 c->pkeys[i].privatekey = NULL;
406 /* clear error queue */
413 if (c->pkeys[i].x509 != NULL)
414 X509_free(c->pkeys[i].x509);
415 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
416 c->pkeys[i].x509 = x;
417 c->key = &(c->pkeys[i]);
423 #ifndef OPENSSL_NO_STDIO
424 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
431 in = BIO_new(BIO_s_file_internal());
433 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
437 if (BIO_read_filename(in, file) <= 0) {
438 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
441 if (type == SSL_FILETYPE_ASN1) {
443 x = d2i_X509_bio(in, NULL);
444 } else if (type == SSL_FILETYPE_PEM) {
446 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
447 ctx->default_passwd_callback_userdata);
449 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
454 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
458 ret = SSL_CTX_use_certificate(ctx, x);
468 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
469 const unsigned char *d)
474 x = d2i_X509(NULL, &d, (long)len);
476 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
480 ret = SSL_CTX_use_certificate(ctx, x);
485 #ifndef OPENSSL_NO_RSA
486 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
492 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
495 if (!ssl_cert_inst(&ctx->cert)) {
496 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
499 if ((pkey = EVP_PKEY_new()) == NULL) {
500 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
505 EVP_PKEY_assign_RSA(pkey, rsa);
507 ret = ssl_set_pkey(ctx->cert, pkey);
512 # ifndef OPENSSL_NO_STDIO
513 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
519 in = BIO_new(BIO_s_file_internal());
521 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
525 if (BIO_read_filename(in, file) <= 0) {
526 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
529 if (type == SSL_FILETYPE_ASN1) {
531 rsa = d2i_RSAPrivateKey_bio(in, NULL);
532 } else if (type == SSL_FILETYPE_PEM) {
534 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
535 ctx->default_passwd_callback,
536 ctx->default_passwd_callback_userdata);
538 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
542 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
545 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
554 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
558 const unsigned char *p;
562 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
563 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
567 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
571 #endif /* !OPENSSL_NO_RSA */
573 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
576 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
579 if (!ssl_cert_inst(&ctx->cert)) {
580 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
583 return (ssl_set_pkey(ctx->cert, pkey));
586 #ifndef OPENSSL_NO_STDIO
587 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
591 EVP_PKEY *pkey = NULL;
593 in = BIO_new(BIO_s_file_internal());
595 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
599 if (BIO_read_filename(in, file) <= 0) {
600 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
603 if (type == SSL_FILETYPE_PEM) {
605 pkey = PEM_read_bio_PrivateKey(in, NULL,
606 ctx->default_passwd_callback,
607 ctx->default_passwd_callback_userdata);
608 } else if (type == SSL_FILETYPE_ASN1) {
610 pkey = d2i_PrivateKey_bio(in, NULL);
612 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
616 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
619 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
628 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
629 const unsigned char *d, long len)
632 const unsigned char *p;
636 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
637 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
641 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
646 #ifndef OPENSSL_NO_STDIO
648 * Read a file that contains our certificate in "PEM" format, possibly
649 * followed by a sequence of CA certificates that should be sent to the peer
650 * in the Certificate message.
652 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
658 ERR_clear_error(); /* clear error stack for
659 * SSL_CTX_use_certificate() */
661 in = BIO_new(BIO_s_file_internal());
663 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
667 if (BIO_read_filename(in, file) <= 0) {
668 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
672 x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
673 ctx->default_passwd_callback_userdata);
675 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
679 ret = SSL_CTX_use_certificate(ctx, x);
680 if (ERR_peek_error() != 0)
681 ret = 0; /* Key/certificate mismatch doesn't imply
685 * If we could set up our certificate, now proceed to the CA
692 if (ctx->extra_certs != NULL) {
693 sk_X509_pop_free(ctx->extra_certs, X509_free);
694 ctx->extra_certs = NULL;
698 PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
699 ctx->default_passwd_callback_userdata))
701 r = SSL_CTX_add_extra_chain_cert(ctx, ca);
708 * Note that we must not free r if it was successfully added to
709 * the chain (while we must free the main certificate, since its
710 * reference count is increased by SSL_CTX_use_certificate).
713 /* When the while loop ends, it's usually just EOF. */
714 err = ERR_peek_last_error();
715 if (ERR_GET_LIB(err) == ERR_LIB_PEM
716 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
719 ret = 0; /* some real error */