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, x)
81 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
84 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
86 SSLerr(SSL_F_SSL_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
89 return(ssl_set_cert(ssl->cert,x));
93 int SSL_use_certificate_file(ssl, file, type)
103 in=BIO_new(BIO_s_file_internal());
106 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
110 if (BIO_read_filename(in,file) <= 0)
112 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
115 if (type == SSL_FILETYPE_ASN1)
118 x=d2i_X509_bio(in,NULL);
120 else if (type == SSL_FILETYPE_PEM)
123 x=PEM_read_bio_X509(in,NULL,ssl->ctx->default_passwd_callback);
127 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
133 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE,j);
137 ret=SSL_use_certificate(ssl,x);
139 if (x != NULL) X509_free(x);
140 if (in != NULL) BIO_free(in);
145 int SSL_use_certificate_ASN1(ssl, d,len)
153 x=d2i_X509(NULL,&d,(long)len);
156 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
160 ret=SSL_use_certificate(ssl,x);
166 int SSL_use_RSAPrivateKey(ssl, rsa)
175 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
178 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
180 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
183 if ((pkey=EVP_PKEY_new()) == NULL)
185 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
189 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
190 EVP_PKEY_assign_RSA(pkey,rsa);
192 ret=ssl_set_pkey(ssl->cert,pkey);
198 static int ssl_set_pkey(c,pkey)
204 i=ssl_cert_type(NULL,pkey);
207 SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
211 if (c->pkeys[i].x509 != NULL)
214 pktmp = X509_get_pubkey(c->pkeys[i].x509);
215 EVP_PKEY_copy_parameters(pktmp,pkey);
216 EVP_PKEY_free(pktmp);
220 /* Don't check the public/private key, this is mostly
221 * for smart cards. */
222 if ((pkey->type == EVP_PKEY_RSA) &&
223 (RSA_flags(pkey->pkey.rsa) &
224 RSA_METHOD_FLAG_NO_CHECK))
228 if (!X509_check_private_key(c->pkeys[i].x509,pkey))
230 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
232 i=(i == SSL_PKEY_DH_RSA)?
233 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
235 if (c->pkeys[i].x509 == NULL)
239 if (!X509_check_private_key(
240 c->pkeys[i].x509,pkey))
257 X509_free(c->pkeys[i].x509);
258 c->pkeys[i].x509=NULL;
262 if (c->pkeys[i].privatekey != NULL)
263 EVP_PKEY_free(c->pkeys[i].privatekey);
264 CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
265 c->pkeys[i].privatekey=pkey;
266 c->key= &(c->pkeys[i]);
274 int SSL_use_RSAPrivateKey_file(ssl, file, type)
283 in=BIO_new(BIO_s_file_internal());
286 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
290 if (BIO_read_filename(in,file) <= 0)
292 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
295 if (type == SSL_FILETYPE_ASN1)
298 rsa=d2i_RSAPrivateKey_bio(in,NULL);
300 else if (type == SSL_FILETYPE_PEM)
303 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
304 ssl->ctx->default_passwd_callback);
308 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
313 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE,j);
316 ret=SSL_use_RSAPrivateKey(ssl,rsa);
319 if (in != NULL) BIO_free(in);
324 int SSL_use_RSAPrivateKey_ASN1(ssl,d,len)
334 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
336 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
340 ret=SSL_use_RSAPrivateKey(ssl,rsa);
346 int SSL_use_PrivateKey(ssl, pkey)
354 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
357 if (!ssl_cert_instantiate(&ssl->cert, ssl->ctx->default_cert))
359 SSLerr(SSL_F_SSL_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
362 ret=ssl_set_pkey(ssl->cert,pkey);
367 int SSL_use_PrivateKey_file(ssl, file, type)
376 in=BIO_new(BIO_s_file_internal());
379 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
383 if (BIO_read_filename(in,file) <= 0)
385 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
388 if (type == SSL_FILETYPE_PEM)
391 pkey=PEM_read_bio_PrivateKey(in,NULL,
392 ssl->ctx->default_passwd_callback);
396 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
401 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE,j);
404 ret=SSL_use_PrivateKey(ssl,pkey);
407 if (in != NULL) BIO_free(in);
412 int SSL_use_PrivateKey_ASN1(type,ssl,d,len)
423 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
425 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
429 ret=SSL_use_PrivateKey(ssl,pkey);
434 int SSL_CTX_use_certificate(ctx, x)
440 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_PASSED_NULL_PARAMETER);
443 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
445 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE,ERR_R_MALLOC_FAILURE);
448 return(ssl_set_cert(ctx->default_cert,x));
451 static int ssl_set_cert(c,x)
458 pkey=X509_get_pubkey(x);
461 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_X509_LIB);
465 i=ssl_cert_type(x,pkey);
468 SSLerr(SSL_F_SSL_SET_CERT,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
473 if (c->pkeys[i].privatekey != NULL)
475 EVP_PKEY_copy_parameters(pkey,c->pkeys[i].privatekey);
479 /* Don't check the public/private key, this is mostly
480 * for smart cards. */
481 if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
482 (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
483 RSA_METHOD_FLAG_NO_CHECK))
488 if (!X509_check_private_key(x,c->pkeys[i].privatekey))
490 if ((i == SSL_PKEY_DH_RSA) || (i == SSL_PKEY_DH_DSA))
492 i=(i == SSL_PKEY_DH_RSA)?
493 SSL_PKEY_DH_DSA:SSL_PKEY_DH_RSA;
495 if (c->pkeys[i].privatekey == NULL)
499 if (!X509_check_private_key(x,
500 c->pkeys[i].privatekey))
519 EVP_PKEY_free(c->pkeys[i].privatekey);
520 c->pkeys[i].privatekey=NULL;
523 if (c->pkeys[i].x509 != NULL)
524 X509_free(c->pkeys[i].x509);
525 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
527 c->key= &(c->pkeys[i]);
534 int SSL_CTX_use_certificate_file(ctx, file, type)
544 in=BIO_new(BIO_s_file_internal());
547 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_BUF_LIB);
551 if (BIO_read_filename(in,file) <= 0)
553 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,ERR_R_SYS_LIB);
556 if (type == SSL_FILETYPE_ASN1)
559 x=d2i_X509_bio(in,NULL);
561 else if (type == SSL_FILETYPE_PEM)
564 x=PEM_read_bio_X509(in,NULL,ctx->default_passwd_callback);
568 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,SSL_R_BAD_SSL_FILETYPE);
574 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,j);
578 ret=SSL_CTX_use_certificate(ctx,x);
580 if (x != NULL) X509_free(x);
581 if (in != NULL) BIO_free(in);
586 int SSL_CTX_use_certificate_ASN1(ctx, len, d)
594 x=d2i_X509(NULL,&d,(long)len);
597 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1,ERR_R_ASN1_LIB);
601 ret=SSL_CTX_use_certificate(ctx,x);
607 int SSL_CTX_use_RSAPrivateKey(ctx, rsa)
616 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
619 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
621 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_MALLOC_FAILURE);
624 if ((pkey=EVP_PKEY_new()) == NULL)
626 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY,ERR_R_EVP_LIB);
630 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
631 EVP_PKEY_assign_RSA(pkey,rsa);
633 ret=ssl_set_pkey(ctx->default_cert,pkey);
639 int SSL_CTX_use_RSAPrivateKey_file(ctx, file, type)
648 in=BIO_new(BIO_s_file_internal());
651 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_BUF_LIB);
655 if (BIO_read_filename(in,file) <= 0)
657 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,ERR_R_SYS_LIB);
660 if (type == SSL_FILETYPE_ASN1)
663 rsa=d2i_RSAPrivateKey_bio(in,NULL);
665 else if (type == SSL_FILETYPE_PEM)
668 rsa=PEM_read_bio_RSAPrivateKey(in,NULL,
669 ctx->default_passwd_callback);
673 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
678 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE,j);
681 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
684 if (in != NULL) BIO_free(in);
689 int SSL_CTX_use_RSAPrivateKey_ASN1(ctx,d,len)
699 if ((rsa=d2i_RSAPrivateKey(NULL,&p,(long)len)) == NULL)
701 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
705 ret=SSL_CTX_use_RSAPrivateKey(ctx,rsa);
711 int SSL_CTX_use_PrivateKey(ctx, pkey)
717 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_PASSED_NULL_PARAMETER);
720 if (!ssl_cert_instantiate(&ctx->default_cert, NULL))
722 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY,ERR_R_MALLOC_FAILURE);
725 return(ssl_set_pkey(ctx->default_cert,pkey));
729 int SSL_CTX_use_PrivateKey_file(ctx, file, type)
738 in=BIO_new(BIO_s_file_internal());
741 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_BUF_LIB);
745 if (BIO_read_filename(in,file) <= 0)
747 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,ERR_R_SYS_LIB);
750 if (type == SSL_FILETYPE_PEM)
753 pkey=PEM_read_bio_PrivateKey(in,NULL,
754 ctx->default_passwd_callback);
758 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,SSL_R_BAD_SSL_FILETYPE);
763 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE,j);
766 ret=SSL_CTX_use_PrivateKey(ctx,pkey);
769 if (in != NULL) BIO_free(in);
774 int SSL_CTX_use_PrivateKey_ASN1(type,ctx,d,len)
785 if ((pkey=d2i_PrivateKey(type,NULL,&p,(long)len)) == NULL)
787 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1,ERR_R_ASN1_LIB);
791 ret=SSL_CTX_use_PrivateKey(ctx,pkey);