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.]
68 static int ssl_set_cert(CERT *c, X509 *x509);
69 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
71 static int ssl_set_cert();
72 static int ssl_set_pkey();
75 int SSL_use_certificate(SSL *ssl, X509 *x)
79 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
82 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
84 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
87 return(ssl_set_cert(ssl->cert,x));
91 int SSL_use_certificate_file(SSL *ssl, char *file, int type)
98 in=BIO_new(BIO_s_file_internal());
101 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
105 if (BIO_read_filename(in,file) <= 0)
107 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
110 if (type == SSL_FILETYPE_ASN1)
113 x=d2i_X509_bio(in,NULL);
115 else if (type == SSL_FILETYPE_PEM)
118 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback);
122 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
128 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
132 ret=SSL_use_certificate(ssl,x);
134 if (x != NULL) X509_free(x);
135 if (in != NULL) BIO_free(in);
140 int SSL_use_certificate_ASN1(SSL *ssl, unsigned char *d, int len)
145 x=d2i_X509(NULL,&d,(long)len);
148 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
152 ret=SSL_use_certificate(ssl,x);
158 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
165 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
168 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
170 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
173 if ((pkey=EVP_PKEY_new()) == NULL)
175 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
179 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
180 EVP_PKEY_assign_RSA(pkey,rsa);
182 ret=ssl_set_pkey(ssl->cert,pkey);
188 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
192 i=ssl_cert_type(NULL,pkey);
195 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
199 if (c->pkeys[i].x509 != NULL)
202 pktmp = X509_get_pubkey(c->pkeys[i].x509);
203 EVP_PKEY_copy_parameters(pktmp,pkey);
204 EVP_PKEY_free(pktmp);
208 /* Don't check the public/private key, this is mostly
209 * for smart cards. */
210 if ((pkey->type == EVP_PKEY_RSA) &&
211 (RSA_flags(pkey->pkey.rsa) &
212 RSA_METHOD_FLAG_NO_CHECK))
216 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
218 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
220 i=(i == SSL_PKEY_DH_RSA)?
221 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
223 if (c->pkeys[i].x509 == NULL)
227 if (!X509_check_private_key(
228 c->pkeys[i].x509,pkey))
245 X509_free(c->pkeys[i].x509);
246 c->pkeys[i].x509=NULL;
250 if (c->pkeys[i].privatekey != NULL)
251 EVP_PKEY_free(c->pkeys[i].privatekey);
252 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
253 c->pkeys[i].privatekey=pkey;
254 c->key= &(c->pkeys[i]);
262 int SSL_use_RSAPrivateKey_file(SSL *ssl, char *file, int type)
268 in=BIO_new(BIO_s_file_internal());
271 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
275 if (BIO_read_filename(in,file) <= 0)
277 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
280 if (type == SSL_FILETYPE_ASN1)
283 rsa=d2i_RSAPrivateKey_bio(in,NULL);
285 else if (type == SSL_FILETYPE_PEM)
288 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
289 ssl->ctx->default_passwd_callback);
293 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
298 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
301 ret=SSL_use_RSAPrivateKey(ssl,rsa);
304 if (in != NULL) BIO_free(in);
309 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
316 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
318 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
322 ret=SSL_use_RSAPrivateKey(ssl,rsa);
328 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
334 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
337 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
339 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
342 ret=ssl_set_pkey(ssl->cert,pkey);
347 int SSL_use_PrivateKey_file(SSL *ssl, char *file, int type)
353 in=BIO_new(BIO_s_file_internal());
356 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
360 if (BIO_read_filename(in,file) <= 0)
362 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
365 if (type == SSL_FILETYPE_PEM)
368 pkey=PEM_read_bio_PrivateKey(in,NULL,
369 ssl->ctx->default_passwd_callback);
373 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
378 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
381 ret=SSL_use_PrivateKey(ssl,pkey);
384 if (in != NULL) BIO_free(in);
389 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, unsigned char *d, long len)
396 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
398 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
402 ret=SSL_use_PrivateKey(ssl,pkey);
407 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
411 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
414 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
416 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
419 return(ssl_set_cert(ctx->default_cert,x));
422 static int ssl_set_cert(CERT *c, X509 *x)
427 pkey=X509_get_pubkey(x);
430 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
434 i=ssl_cert_type(x,pkey);
437 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
442 if (c->pkeys[i].privatekey != NULL)
444 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
448 /* Don't check the public/private key, this is mostly
449 * for smart cards. */
450 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
451 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
452 RSA_METHOD_FLAG_NO_CHECK))
457 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
459 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
461 i=(i == SSL_PKEY_DH_RSA)?
462 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
464 if (c->pkeys[i].privatekey == NULL)
468 if (!X509_check_private_key(x,
469 c->pkeys[i].privatekey))
488 EVP_PKEY_free(c->pkeys[i].privatekey);
489 c->pkeys[i].privatekey=NULL;
492 if (c->pkeys[i].x509 != NULL)
493 X509_free(c->pkeys[i].x509);
494 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
496 c->key= &(c->pkeys[i]);
503 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, char *file, int type)
510 in=BIO_new(BIO_s_file_internal());
513 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
517 if (BIO_read_filename(in,file) <= 0)
519 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
522 if (type == SSL_FILETYPE_ASN1)
525 x=d2i_X509_bio(in,NULL);
527 else if (type == SSL_FILETYPE_PEM)
530 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
534 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
540 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
544 ret=SSL_CTX_use_certificate(ctx,x);
546 if (x != NULL) X509_free(x);
547 if (in != NULL) BIO_free(in);
552 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, unsigned char *d)
557 x=d2i_X509(NULL,&d,(long)len);
560 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
564 ret=SSL_CTX_use_certificate(ctx,x);
570 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
577 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
580 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
582 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
585 if ((pkey=EVP_PKEY_new()) == NULL)
587 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
591 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
592 EVP_PKEY_assign_RSA(pkey,rsa);
594 ret=ssl_set_pkey(ctx->default_cert,pkey);
600 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, char *file, int type)
606 in=BIO_new(BIO_s_file_internal());
609 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
613 if (BIO_read_filename(in,file) <= 0)
615 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
618 if (type == SSL_FILETYPE_ASN1)
621 rsa=d2i_RSAPrivateKey_bio(in,NULL);
623 else if (type == SSL_FILETYPE_PEM)
626 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
627 ctx->default_passwd_callback);
631 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
636 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
639 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
642 if (in != NULL) BIO_free(in);
647 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, unsigned char *d, long len)
654 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
656 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
660 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
666 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
670 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
673 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
675 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
678 return(ssl_set_pkey(ctx->default_cert,pkey));
682 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, char *file, int type)
688 in=BIO_new(BIO_s_file_internal());
691 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
695 if (BIO_read_filename(in,file) <= 0)
697 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
700 if (type == SSL_FILETYPE_PEM)
703 pkey=PEM_read_bio_PrivateKey(in,NULL,
704 ctx->default_passwd_callback);
708 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
713 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
716 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
719 if (in != NULL) BIO_free(in);
724 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, unsigned char *d,
732 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
734 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
738 ret=SSL_CTX_use_PrivateKey(ctx,pkey);