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