1ebfedea0SLionel Sambuc /* ssl/ssl_rsa.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include "ssl_locl.h"
61ebfedea0SLionel Sambuc #include <openssl/bio.h>
62ebfedea0SLionel Sambuc #include <openssl/objects.h>
63ebfedea0SLionel Sambuc #include <openssl/evp.h>
64ebfedea0SLionel Sambuc #include <openssl/x509.h>
65ebfedea0SLionel Sambuc #include <openssl/pem.h>
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc static int ssl_set_cert(CERT *c, X509 *x509);
68ebfedea0SLionel Sambuc static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
SSL_use_certificate(SSL * ssl,X509 * x)69ebfedea0SLionel Sambuc int SSL_use_certificate(SSL *ssl, X509 *x)
70ebfedea0SLionel Sambuc {
71*0a6a1f1dSLionel Sambuc if (x == NULL) {
72ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
73ebfedea0SLionel Sambuc return (0);
74ebfedea0SLionel Sambuc }
75*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ssl->cert)) {
76ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
77ebfedea0SLionel Sambuc return (0);
78ebfedea0SLionel Sambuc }
79ebfedea0SLionel Sambuc return (ssl_set_cert(ssl->cert, x));
80ebfedea0SLionel Sambuc }
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
SSL_use_certificate_file(SSL * ssl,const char * file,int type)83ebfedea0SLionel Sambuc int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
84ebfedea0SLionel Sambuc {
85ebfedea0SLionel Sambuc int j;
86ebfedea0SLionel Sambuc BIO *in;
87ebfedea0SLionel Sambuc int ret = 0;
88ebfedea0SLionel Sambuc X509 *x = NULL;
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
91*0a6a1f1dSLionel Sambuc if (in == NULL) {
92ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
93ebfedea0SLionel Sambuc goto end;
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc
96*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
97ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
98ebfedea0SLionel Sambuc goto end;
99ebfedea0SLionel Sambuc }
100*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_ASN1) {
101ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
102ebfedea0SLionel Sambuc x = d2i_X509_bio(in, NULL);
103*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_PEM) {
104ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
105*0a6a1f1dSLionel Sambuc x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
106*0a6a1f1dSLionel Sambuc ssl->ctx->default_passwd_callback_userdata);
107*0a6a1f1dSLionel Sambuc } else {
108ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
109ebfedea0SLionel Sambuc goto end;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc
112*0a6a1f1dSLionel Sambuc if (x == NULL) {
113ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
114ebfedea0SLionel Sambuc goto end;
115ebfedea0SLionel Sambuc }
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc ret = SSL_use_certificate(ssl, x);
118ebfedea0SLionel Sambuc end:
119*0a6a1f1dSLionel Sambuc if (x != NULL)
120*0a6a1f1dSLionel Sambuc X509_free(x);
121*0a6a1f1dSLionel Sambuc if (in != NULL)
122*0a6a1f1dSLionel Sambuc BIO_free(in);
123ebfedea0SLionel Sambuc return (ret);
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc #endif
126ebfedea0SLionel Sambuc
SSL_use_certificate_ASN1(SSL * ssl,const unsigned char * d,int len)127ebfedea0SLionel Sambuc int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
128ebfedea0SLionel Sambuc {
129ebfedea0SLionel Sambuc X509 *x;
130ebfedea0SLionel Sambuc int ret;
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc x = d2i_X509(NULL, &d, (long)len);
133*0a6a1f1dSLionel Sambuc if (x == NULL) {
134ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
135ebfedea0SLionel Sambuc return (0);
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc ret = SSL_use_certificate(ssl, x);
139ebfedea0SLionel Sambuc X509_free(x);
140ebfedea0SLionel Sambuc return (ret);
141ebfedea0SLionel Sambuc }
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
SSL_use_RSAPrivateKey(SSL * ssl,RSA * rsa)144ebfedea0SLionel Sambuc int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc EVP_PKEY *pkey;
147ebfedea0SLionel Sambuc int ret;
148ebfedea0SLionel Sambuc
149*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
150ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
151ebfedea0SLionel Sambuc return (0);
152ebfedea0SLionel Sambuc }
153*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ssl->cert)) {
154ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
155ebfedea0SLionel Sambuc return (0);
156ebfedea0SLionel Sambuc }
157*0a6a1f1dSLionel Sambuc if ((pkey = EVP_PKEY_new()) == NULL) {
158ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
159ebfedea0SLionel Sambuc return (0);
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc
162ebfedea0SLionel Sambuc RSA_up_ref(rsa);
163ebfedea0SLionel Sambuc EVP_PKEY_assign_RSA(pkey, rsa);
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc ret = ssl_set_pkey(ssl->cert, pkey);
166ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
167ebfedea0SLionel Sambuc return (ret);
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc #endif
170ebfedea0SLionel Sambuc
ssl_set_pkey(CERT * c,EVP_PKEY * pkey)171ebfedea0SLionel Sambuc static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
172ebfedea0SLionel Sambuc {
173ebfedea0SLionel Sambuc int i;
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc i = ssl_cert_type(NULL, pkey);
176*0a6a1f1dSLionel Sambuc if (i < 0) {
177ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
178ebfedea0SLionel Sambuc return (0);
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc
181*0a6a1f1dSLionel Sambuc if (c->pkeys[i].x509 != NULL) {
182ebfedea0SLionel Sambuc EVP_PKEY *pktmp;
183ebfedea0SLionel Sambuc pktmp = X509_get_pubkey(c->pkeys[i].x509);
184ebfedea0SLionel Sambuc EVP_PKEY_copy_parameters(pktmp, pkey);
185ebfedea0SLionel Sambuc EVP_PKEY_free(pktmp);
186ebfedea0SLionel Sambuc ERR_clear_error();
187ebfedea0SLionel Sambuc
188ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
189*0a6a1f1dSLionel Sambuc /*
190*0a6a1f1dSLionel Sambuc * Don't check the public/private key, this is mostly for smart
191*0a6a1f1dSLionel Sambuc * cards.
192*0a6a1f1dSLionel Sambuc */
193ebfedea0SLionel Sambuc if ((pkey->type == EVP_PKEY_RSA) &&
194*0a6a1f1dSLionel Sambuc (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
195ebfedea0SLionel Sambuc else
196ebfedea0SLionel Sambuc #endif
197*0a6a1f1dSLionel Sambuc if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
198ebfedea0SLionel Sambuc X509_free(c->pkeys[i].x509);
199ebfedea0SLionel Sambuc c->pkeys[i].x509 = NULL;
200ebfedea0SLionel Sambuc return 0;
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc
204ebfedea0SLionel Sambuc if (c->pkeys[i].privatekey != NULL)
205ebfedea0SLionel Sambuc EVP_PKEY_free(c->pkeys[i].privatekey);
206ebfedea0SLionel Sambuc CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
207ebfedea0SLionel Sambuc c->pkeys[i].privatekey = pkey;
208ebfedea0SLionel Sambuc c->key = &(c->pkeys[i]);
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc c->valid = 0;
211ebfedea0SLionel Sambuc return (1);
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
215ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_STDIO
SSL_use_RSAPrivateKey_file(SSL * ssl,const char * file,int type)216ebfedea0SLionel Sambuc int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
217ebfedea0SLionel Sambuc {
218ebfedea0SLionel Sambuc int j, ret = 0;
219ebfedea0SLionel Sambuc BIO *in;
220ebfedea0SLionel Sambuc RSA *rsa = NULL;
221ebfedea0SLionel Sambuc
222ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
223*0a6a1f1dSLionel Sambuc if (in == NULL) {
224ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
225ebfedea0SLionel Sambuc goto end;
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc
228*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
229ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
230ebfedea0SLionel Sambuc goto end;
231ebfedea0SLionel Sambuc }
232*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_ASN1) {
233ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
234ebfedea0SLionel Sambuc rsa = d2i_RSAPrivateKey_bio(in, NULL);
235*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_PEM) {
236ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
237ebfedea0SLionel Sambuc rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
238*0a6a1f1dSLionel Sambuc ssl->ctx->default_passwd_callback,
239*0a6a1f1dSLionel Sambuc ssl->
240*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback_userdata);
241*0a6a1f1dSLionel Sambuc } else {
242ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
243ebfedea0SLionel Sambuc goto end;
244ebfedea0SLionel Sambuc }
245*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
246ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
247ebfedea0SLionel Sambuc goto end;
248ebfedea0SLionel Sambuc }
249ebfedea0SLionel Sambuc ret = SSL_use_RSAPrivateKey(ssl, rsa);
250ebfedea0SLionel Sambuc RSA_free(rsa);
251ebfedea0SLionel Sambuc end:
252*0a6a1f1dSLionel Sambuc if (in != NULL)
253*0a6a1f1dSLionel Sambuc BIO_free(in);
254ebfedea0SLionel Sambuc return (ret);
255ebfedea0SLionel Sambuc }
256ebfedea0SLionel Sambuc # endif
257ebfedea0SLionel Sambuc
SSL_use_RSAPrivateKey_ASN1(SSL * ssl,unsigned char * d,long len)258ebfedea0SLionel Sambuc int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
259ebfedea0SLionel Sambuc {
260ebfedea0SLionel Sambuc int ret;
261ebfedea0SLionel Sambuc const unsigned char *p;
262ebfedea0SLionel Sambuc RSA *rsa;
263ebfedea0SLionel Sambuc
264ebfedea0SLionel Sambuc p = d;
265*0a6a1f1dSLionel Sambuc if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
266ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
267ebfedea0SLionel Sambuc return (0);
268ebfedea0SLionel Sambuc }
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc ret = SSL_use_RSAPrivateKey(ssl, rsa);
271ebfedea0SLionel Sambuc RSA_free(rsa);
272ebfedea0SLionel Sambuc return (ret);
273ebfedea0SLionel Sambuc }
274ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_RSA */
275ebfedea0SLionel Sambuc
SSL_use_PrivateKey(SSL * ssl,EVP_PKEY * pkey)276ebfedea0SLionel Sambuc int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
277ebfedea0SLionel Sambuc {
278ebfedea0SLionel Sambuc int ret;
279ebfedea0SLionel Sambuc
280*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
281ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
282ebfedea0SLionel Sambuc return (0);
283ebfedea0SLionel Sambuc }
284*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ssl->cert)) {
285ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
286ebfedea0SLionel Sambuc return (0);
287ebfedea0SLionel Sambuc }
288ebfedea0SLionel Sambuc ret = ssl_set_pkey(ssl->cert, pkey);
289ebfedea0SLionel Sambuc return (ret);
290ebfedea0SLionel Sambuc }
291ebfedea0SLionel Sambuc
292ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
SSL_use_PrivateKey_file(SSL * ssl,const char * file,int type)293ebfedea0SLionel Sambuc int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
294ebfedea0SLionel Sambuc {
295ebfedea0SLionel Sambuc int j, ret = 0;
296ebfedea0SLionel Sambuc BIO *in;
297ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
298ebfedea0SLionel Sambuc
299ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
300*0a6a1f1dSLionel Sambuc if (in == NULL) {
301ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
302ebfedea0SLionel Sambuc goto end;
303ebfedea0SLionel Sambuc }
304ebfedea0SLionel Sambuc
305*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
306ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
307ebfedea0SLionel Sambuc goto end;
308ebfedea0SLionel Sambuc }
309*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_PEM) {
310ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
311ebfedea0SLionel Sambuc pkey = PEM_read_bio_PrivateKey(in, NULL,
312*0a6a1f1dSLionel Sambuc ssl->ctx->default_passwd_callback,
313*0a6a1f1dSLionel Sambuc ssl->
314*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback_userdata);
315*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_ASN1) {
316ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
317ebfedea0SLionel Sambuc pkey = d2i_PrivateKey_bio(in, NULL);
318*0a6a1f1dSLionel Sambuc } else {
319ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
320ebfedea0SLionel Sambuc goto end;
321ebfedea0SLionel Sambuc }
322*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
323ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
324ebfedea0SLionel Sambuc goto end;
325ebfedea0SLionel Sambuc }
326ebfedea0SLionel Sambuc ret = SSL_use_PrivateKey(ssl, pkey);
327ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
328ebfedea0SLionel Sambuc end:
329*0a6a1f1dSLionel Sambuc if (in != NULL)
330*0a6a1f1dSLionel Sambuc BIO_free(in);
331ebfedea0SLionel Sambuc return (ret);
332ebfedea0SLionel Sambuc }
333ebfedea0SLionel Sambuc #endif
334ebfedea0SLionel Sambuc
SSL_use_PrivateKey_ASN1(int type,SSL * ssl,const unsigned char * d,long len)335*0a6a1f1dSLionel Sambuc int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
336*0a6a1f1dSLionel Sambuc long len)
337ebfedea0SLionel Sambuc {
338ebfedea0SLionel Sambuc int ret;
339ebfedea0SLionel Sambuc const unsigned char *p;
340ebfedea0SLionel Sambuc EVP_PKEY *pkey;
341ebfedea0SLionel Sambuc
342ebfedea0SLionel Sambuc p = d;
343*0a6a1f1dSLionel Sambuc if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
344ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
345ebfedea0SLionel Sambuc return (0);
346ebfedea0SLionel Sambuc }
347ebfedea0SLionel Sambuc
348ebfedea0SLionel Sambuc ret = SSL_use_PrivateKey(ssl, pkey);
349ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
350ebfedea0SLionel Sambuc return (ret);
351ebfedea0SLionel Sambuc }
352ebfedea0SLionel Sambuc
SSL_CTX_use_certificate(SSL_CTX * ctx,X509 * x)353ebfedea0SLionel Sambuc int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
354ebfedea0SLionel Sambuc {
355*0a6a1f1dSLionel Sambuc if (x == NULL) {
356ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
357ebfedea0SLionel Sambuc return (0);
358ebfedea0SLionel Sambuc }
359*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ctx->cert)) {
360ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
361ebfedea0SLionel Sambuc return (0);
362ebfedea0SLionel Sambuc }
363ebfedea0SLionel Sambuc return (ssl_set_cert(ctx->cert, x));
364ebfedea0SLionel Sambuc }
365ebfedea0SLionel Sambuc
ssl_set_cert(CERT * c,X509 * x)366ebfedea0SLionel Sambuc static int ssl_set_cert(CERT *c, X509 *x)
367ebfedea0SLionel Sambuc {
368ebfedea0SLionel Sambuc EVP_PKEY *pkey;
369ebfedea0SLionel Sambuc int i;
370ebfedea0SLionel Sambuc
371ebfedea0SLionel Sambuc pkey = X509_get_pubkey(x);
372*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
373ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
374ebfedea0SLionel Sambuc return (0);
375ebfedea0SLionel Sambuc }
376ebfedea0SLionel Sambuc
377ebfedea0SLionel Sambuc i = ssl_cert_type(x, pkey);
378*0a6a1f1dSLionel Sambuc if (i < 0) {
379ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
380ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
381ebfedea0SLionel Sambuc return (0);
382ebfedea0SLionel Sambuc }
383ebfedea0SLionel Sambuc
384*0a6a1f1dSLionel Sambuc if (c->pkeys[i].privatekey != NULL) {
385ebfedea0SLionel Sambuc EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
386ebfedea0SLionel Sambuc ERR_clear_error();
387ebfedea0SLionel Sambuc
388ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
389*0a6a1f1dSLionel Sambuc /*
390*0a6a1f1dSLionel Sambuc * Don't check the public/private key, this is mostly for smart
391*0a6a1f1dSLionel Sambuc * cards.
392*0a6a1f1dSLionel Sambuc */
393ebfedea0SLionel Sambuc if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
394ebfedea0SLionel Sambuc (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
395*0a6a1f1dSLionel Sambuc RSA_METHOD_FLAG_NO_CHECK)) ;
396ebfedea0SLionel Sambuc else
397ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_RSA */
398*0a6a1f1dSLionel Sambuc if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
399*0a6a1f1dSLionel Sambuc /*
400*0a6a1f1dSLionel Sambuc * don't fail for a cert/key mismatch, just free current private
401*0a6a1f1dSLionel Sambuc * key (when switching to a different cert & key, first this
402*0a6a1f1dSLionel Sambuc * function should be used, then ssl_set_pkey
403*0a6a1f1dSLionel Sambuc */
404ebfedea0SLionel Sambuc EVP_PKEY_free(c->pkeys[i].privatekey);
405ebfedea0SLionel Sambuc c->pkeys[i].privatekey = NULL;
406ebfedea0SLionel Sambuc /* clear error queue */
407ebfedea0SLionel Sambuc ERR_clear_error();
408ebfedea0SLionel Sambuc }
409ebfedea0SLionel Sambuc }
410ebfedea0SLionel Sambuc
411ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
412ebfedea0SLionel Sambuc
413ebfedea0SLionel Sambuc if (c->pkeys[i].x509 != NULL)
414ebfedea0SLionel Sambuc X509_free(c->pkeys[i].x509);
415ebfedea0SLionel Sambuc CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
416ebfedea0SLionel Sambuc c->pkeys[i].x509 = x;
417ebfedea0SLionel Sambuc c->key = &(c->pkeys[i]);
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc c->valid = 0;
420ebfedea0SLionel Sambuc return (1);
421ebfedea0SLionel Sambuc }
422ebfedea0SLionel Sambuc
423ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
SSL_CTX_use_certificate_file(SSL_CTX * ctx,const char * file,int type)424ebfedea0SLionel Sambuc int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
425ebfedea0SLionel Sambuc {
426ebfedea0SLionel Sambuc int j;
427ebfedea0SLionel Sambuc BIO *in;
428ebfedea0SLionel Sambuc int ret = 0;
429ebfedea0SLionel Sambuc X509 *x = NULL;
430ebfedea0SLionel Sambuc
431ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
432*0a6a1f1dSLionel Sambuc if (in == NULL) {
433ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
434ebfedea0SLionel Sambuc goto end;
435ebfedea0SLionel Sambuc }
436ebfedea0SLionel Sambuc
437*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
438ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
439ebfedea0SLionel Sambuc goto end;
440ebfedea0SLionel Sambuc }
441*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_ASN1) {
442ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
443ebfedea0SLionel Sambuc x = d2i_X509_bio(in, NULL);
444*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_PEM) {
445ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
446*0a6a1f1dSLionel Sambuc x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
447*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback_userdata);
448*0a6a1f1dSLionel Sambuc } else {
449ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
450ebfedea0SLionel Sambuc goto end;
451ebfedea0SLionel Sambuc }
452ebfedea0SLionel Sambuc
453*0a6a1f1dSLionel Sambuc if (x == NULL) {
454ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
455ebfedea0SLionel Sambuc goto end;
456ebfedea0SLionel Sambuc }
457ebfedea0SLionel Sambuc
458ebfedea0SLionel Sambuc ret = SSL_CTX_use_certificate(ctx, x);
459ebfedea0SLionel Sambuc end:
460*0a6a1f1dSLionel Sambuc if (x != NULL)
461*0a6a1f1dSLionel Sambuc X509_free(x);
462*0a6a1f1dSLionel Sambuc if (in != NULL)
463*0a6a1f1dSLionel Sambuc BIO_free(in);
464ebfedea0SLionel Sambuc return (ret);
465ebfedea0SLionel Sambuc }
466ebfedea0SLionel Sambuc #endif
467ebfedea0SLionel Sambuc
SSL_CTX_use_certificate_ASN1(SSL_CTX * ctx,int len,const unsigned char * d)468*0a6a1f1dSLionel Sambuc int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
469*0a6a1f1dSLionel Sambuc const unsigned char *d)
470ebfedea0SLionel Sambuc {
471ebfedea0SLionel Sambuc X509 *x;
472ebfedea0SLionel Sambuc int ret;
473ebfedea0SLionel Sambuc
474ebfedea0SLionel Sambuc x = d2i_X509(NULL, &d, (long)len);
475*0a6a1f1dSLionel Sambuc if (x == NULL) {
476ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
477ebfedea0SLionel Sambuc return (0);
478ebfedea0SLionel Sambuc }
479ebfedea0SLionel Sambuc
480ebfedea0SLionel Sambuc ret = SSL_CTX_use_certificate(ctx, x);
481ebfedea0SLionel Sambuc X509_free(x);
482ebfedea0SLionel Sambuc return (ret);
483ebfedea0SLionel Sambuc }
484ebfedea0SLionel Sambuc
485ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
SSL_CTX_use_RSAPrivateKey(SSL_CTX * ctx,RSA * rsa)486ebfedea0SLionel Sambuc int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
487ebfedea0SLionel Sambuc {
488ebfedea0SLionel Sambuc int ret;
489ebfedea0SLionel Sambuc EVP_PKEY *pkey;
490ebfedea0SLionel Sambuc
491*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
492ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
493ebfedea0SLionel Sambuc return (0);
494ebfedea0SLionel Sambuc }
495*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ctx->cert)) {
496ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
497ebfedea0SLionel Sambuc return (0);
498ebfedea0SLionel Sambuc }
499*0a6a1f1dSLionel Sambuc if ((pkey = EVP_PKEY_new()) == NULL) {
500ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
501ebfedea0SLionel Sambuc return (0);
502ebfedea0SLionel Sambuc }
503ebfedea0SLionel Sambuc
504ebfedea0SLionel Sambuc RSA_up_ref(rsa);
505ebfedea0SLionel Sambuc EVP_PKEY_assign_RSA(pkey, rsa);
506ebfedea0SLionel Sambuc
507ebfedea0SLionel Sambuc ret = ssl_set_pkey(ctx->cert, pkey);
508ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
509ebfedea0SLionel Sambuc return (ret);
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc
512ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_STDIO
SSL_CTX_use_RSAPrivateKey_file(SSL_CTX * ctx,const char * file,int type)513ebfedea0SLionel Sambuc int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
514ebfedea0SLionel Sambuc {
515ebfedea0SLionel Sambuc int j, ret = 0;
516ebfedea0SLionel Sambuc BIO *in;
517ebfedea0SLionel Sambuc RSA *rsa = NULL;
518ebfedea0SLionel Sambuc
519ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
520*0a6a1f1dSLionel Sambuc if (in == NULL) {
521ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
522ebfedea0SLionel Sambuc goto end;
523ebfedea0SLionel Sambuc }
524ebfedea0SLionel Sambuc
525*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
526ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
527ebfedea0SLionel Sambuc goto end;
528ebfedea0SLionel Sambuc }
529*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_ASN1) {
530ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
531ebfedea0SLionel Sambuc rsa = d2i_RSAPrivateKey_bio(in, NULL);
532*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_PEM) {
533ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
534ebfedea0SLionel Sambuc rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
535*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback,
536*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback_userdata);
537*0a6a1f1dSLionel Sambuc } else {
538ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
539ebfedea0SLionel Sambuc goto end;
540ebfedea0SLionel Sambuc }
541*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
542ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
543ebfedea0SLionel Sambuc goto end;
544ebfedea0SLionel Sambuc }
545ebfedea0SLionel Sambuc ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
546ebfedea0SLionel Sambuc RSA_free(rsa);
547ebfedea0SLionel Sambuc end:
548*0a6a1f1dSLionel Sambuc if (in != NULL)
549*0a6a1f1dSLionel Sambuc BIO_free(in);
550ebfedea0SLionel Sambuc return (ret);
551ebfedea0SLionel Sambuc }
552ebfedea0SLionel Sambuc # endif
553ebfedea0SLionel Sambuc
SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX * ctx,const unsigned char * d,long len)554*0a6a1f1dSLionel Sambuc int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
555*0a6a1f1dSLionel Sambuc long len)
556ebfedea0SLionel Sambuc {
557ebfedea0SLionel Sambuc int ret;
558ebfedea0SLionel Sambuc const unsigned char *p;
559ebfedea0SLionel Sambuc RSA *rsa;
560ebfedea0SLionel Sambuc
561ebfedea0SLionel Sambuc p = d;
562*0a6a1f1dSLionel Sambuc if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
563ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
564ebfedea0SLionel Sambuc return (0);
565ebfedea0SLionel Sambuc }
566ebfedea0SLionel Sambuc
567ebfedea0SLionel Sambuc ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
568ebfedea0SLionel Sambuc RSA_free(rsa);
569ebfedea0SLionel Sambuc return (ret);
570ebfedea0SLionel Sambuc }
571ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_RSA */
572ebfedea0SLionel Sambuc
SSL_CTX_use_PrivateKey(SSL_CTX * ctx,EVP_PKEY * pkey)573ebfedea0SLionel Sambuc int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
574ebfedea0SLionel Sambuc {
575*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
576ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
577ebfedea0SLionel Sambuc return (0);
578ebfedea0SLionel Sambuc }
579*0a6a1f1dSLionel Sambuc if (!ssl_cert_inst(&ctx->cert)) {
580ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
581ebfedea0SLionel Sambuc return (0);
582ebfedea0SLionel Sambuc }
583ebfedea0SLionel Sambuc return (ssl_set_pkey(ctx->cert, pkey));
584ebfedea0SLionel Sambuc }
585ebfedea0SLionel Sambuc
586ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
SSL_CTX_use_PrivateKey_file(SSL_CTX * ctx,const char * file,int type)587ebfedea0SLionel Sambuc int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
588ebfedea0SLionel Sambuc {
589ebfedea0SLionel Sambuc int j, ret = 0;
590ebfedea0SLionel Sambuc BIO *in;
591ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
592ebfedea0SLionel Sambuc
593ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
594*0a6a1f1dSLionel Sambuc if (in == NULL) {
595ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
596ebfedea0SLionel Sambuc goto end;
597ebfedea0SLionel Sambuc }
598ebfedea0SLionel Sambuc
599*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
600ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
601ebfedea0SLionel Sambuc goto end;
602ebfedea0SLionel Sambuc }
603*0a6a1f1dSLionel Sambuc if (type == SSL_FILETYPE_PEM) {
604ebfedea0SLionel Sambuc j = ERR_R_PEM_LIB;
605ebfedea0SLionel Sambuc pkey = PEM_read_bio_PrivateKey(in, NULL,
606*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback,
607*0a6a1f1dSLionel Sambuc ctx->default_passwd_callback_userdata);
608*0a6a1f1dSLionel Sambuc } else if (type == SSL_FILETYPE_ASN1) {
609ebfedea0SLionel Sambuc j = ERR_R_ASN1_LIB;
610ebfedea0SLionel Sambuc pkey = d2i_PrivateKey_bio(in, NULL);
611*0a6a1f1dSLionel Sambuc } else {
612ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
613ebfedea0SLionel Sambuc goto end;
614ebfedea0SLionel Sambuc }
615*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
616ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
617ebfedea0SLionel Sambuc goto end;
618ebfedea0SLionel Sambuc }
619ebfedea0SLionel Sambuc ret = SSL_CTX_use_PrivateKey(ctx, pkey);
620ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
621ebfedea0SLionel Sambuc end:
622*0a6a1f1dSLionel Sambuc if (in != NULL)
623*0a6a1f1dSLionel Sambuc BIO_free(in);
624ebfedea0SLionel Sambuc return (ret);
625ebfedea0SLionel Sambuc }
626ebfedea0SLionel Sambuc #endif
627ebfedea0SLionel Sambuc
SSL_CTX_use_PrivateKey_ASN1(int type,SSL_CTX * ctx,const unsigned char * d,long len)628*0a6a1f1dSLionel Sambuc int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
629*0a6a1f1dSLionel Sambuc const unsigned char *d, long len)
630ebfedea0SLionel Sambuc {
631ebfedea0SLionel Sambuc int ret;
632ebfedea0SLionel Sambuc const unsigned char *p;
633ebfedea0SLionel Sambuc EVP_PKEY *pkey;
634ebfedea0SLionel Sambuc
635ebfedea0SLionel Sambuc p = d;
636*0a6a1f1dSLionel Sambuc if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
637ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
638ebfedea0SLionel Sambuc return (0);
639ebfedea0SLionel Sambuc }
640ebfedea0SLionel Sambuc
641ebfedea0SLionel Sambuc ret = SSL_CTX_use_PrivateKey(ctx, pkey);
642ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
643ebfedea0SLionel Sambuc return (ret);
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc
646ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
647*0a6a1f1dSLionel Sambuc /*
648*0a6a1f1dSLionel Sambuc * Read a file that contains our certificate in "PEM" format, possibly
649*0a6a1f1dSLionel Sambuc * followed by a sequence of CA certificates that should be sent to the peer
650*0a6a1f1dSLionel Sambuc * in the Certificate message.
651ebfedea0SLionel Sambuc */
SSL_CTX_use_certificate_chain_file(SSL_CTX * ctx,const char * file)652ebfedea0SLionel Sambuc int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
653ebfedea0SLionel Sambuc {
654ebfedea0SLionel Sambuc BIO *in;
655ebfedea0SLionel Sambuc int ret = 0;
656ebfedea0SLionel Sambuc X509 *x = NULL;
657ebfedea0SLionel Sambuc
658*0a6a1f1dSLionel Sambuc ERR_clear_error(); /* clear error stack for
659*0a6a1f1dSLionel Sambuc * SSL_CTX_use_certificate() */
660ebfedea0SLionel Sambuc
661ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file_internal());
662*0a6a1f1dSLionel Sambuc if (in == NULL) {
663ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
664ebfedea0SLionel Sambuc goto end;
665ebfedea0SLionel Sambuc }
666ebfedea0SLionel Sambuc
667*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, file) <= 0) {
668ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
669ebfedea0SLionel Sambuc goto end;
670ebfedea0SLionel Sambuc }
671ebfedea0SLionel Sambuc
672ebfedea0SLionel Sambuc x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
673ebfedea0SLionel Sambuc ctx->default_passwd_callback_userdata);
674*0a6a1f1dSLionel Sambuc if (x == NULL) {
675ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
676ebfedea0SLionel Sambuc goto end;
677ebfedea0SLionel Sambuc }
678ebfedea0SLionel Sambuc
679ebfedea0SLionel Sambuc ret = SSL_CTX_use_certificate(ctx, x);
680ebfedea0SLionel Sambuc
681ebfedea0SLionel Sambuc if (ERR_peek_error() != 0)
682*0a6a1f1dSLionel Sambuc ret = 0; /* Key/certificate mismatch doesn't imply
683*0a6a1f1dSLionel Sambuc * ret==0 ... */
684*0a6a1f1dSLionel Sambuc if (ret) {
685*0a6a1f1dSLionel Sambuc /*
686*0a6a1f1dSLionel Sambuc * If we could set up our certificate, now proceed to the CA
687*0a6a1f1dSLionel Sambuc * certificates.
688ebfedea0SLionel Sambuc */
689ebfedea0SLionel Sambuc X509 *ca;
690ebfedea0SLionel Sambuc int r;
691ebfedea0SLionel Sambuc unsigned long err;
692ebfedea0SLionel Sambuc
693*0a6a1f1dSLionel Sambuc if (ctx->extra_certs != NULL) {
694ebfedea0SLionel Sambuc sk_X509_pop_free(ctx->extra_certs, X509_free);
695ebfedea0SLionel Sambuc ctx->extra_certs = NULL;
696ebfedea0SLionel Sambuc }
697ebfedea0SLionel Sambuc
698ebfedea0SLionel Sambuc while ((ca = PEM_read_bio_X509(in, NULL,
699ebfedea0SLionel Sambuc ctx->default_passwd_callback,
700ebfedea0SLionel Sambuc ctx->default_passwd_callback_userdata))
701*0a6a1f1dSLionel Sambuc != NULL) {
702ebfedea0SLionel Sambuc r = SSL_CTX_add_extra_chain_cert(ctx, ca);
703*0a6a1f1dSLionel Sambuc if (!r) {
704ebfedea0SLionel Sambuc X509_free(ca);
705ebfedea0SLionel Sambuc ret = 0;
706ebfedea0SLionel Sambuc goto end;
707ebfedea0SLionel Sambuc }
708*0a6a1f1dSLionel Sambuc /*
709*0a6a1f1dSLionel Sambuc * Note that we must not free r if it was successfully added to
710*0a6a1f1dSLionel Sambuc * the chain (while we must free the main certificate, since its
711*0a6a1f1dSLionel Sambuc * reference count is increased by SSL_CTX_use_certificate).
712*0a6a1f1dSLionel Sambuc */
713ebfedea0SLionel Sambuc }
714ebfedea0SLionel Sambuc /* When the while loop ends, it's usually just EOF. */
715ebfedea0SLionel Sambuc err = ERR_peek_last_error();
716*0a6a1f1dSLionel Sambuc if (ERR_GET_LIB(err) == ERR_LIB_PEM
717*0a6a1f1dSLionel Sambuc && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
718ebfedea0SLionel Sambuc ERR_clear_error();
719ebfedea0SLionel Sambuc else
720ebfedea0SLionel Sambuc ret = 0; /* some real error */
721ebfedea0SLionel Sambuc }
722ebfedea0SLionel Sambuc
723ebfedea0SLionel Sambuc end:
724*0a6a1f1dSLionel Sambuc if (x != NULL)
725*0a6a1f1dSLionel Sambuc X509_free(x);
726*0a6a1f1dSLionel Sambuc if (in != NULL)
727*0a6a1f1dSLionel Sambuc BIO_free(in);
728ebfedea0SLionel Sambuc return (ret);
729ebfedea0SLionel Sambuc }
730ebfedea0SLionel Sambuc #endif
731