ssl_rsa.c revision 1.1.1.4 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.4 spz EVP_PKEY_assign_RSA(pkey, rsa);
164 1.1.1.4 spz
165 1.1.1.4 spz ret = ssl_set_pkey(ssl->cert, pkey);
166 1.1.1.4 spz EVP_PKEY_free(pkey);
167 1.1.1.4 spz return (ret);
168 1.1.1.4 spz }
169 1.1 christos #endif
170 1.1 christos
171 1.1 christos static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
172 1.1.1.4 spz {
173 1.1.1.4 spz int i;
174 1.1 christos
175 1.1.1.4 spz i = ssl_cert_type(NULL, pkey);
176 1.1.1.4 spz if (i < 0) {
177 1.1.1.4 spz SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
178 1.1.1.4 spz return (0);
179 1.1.1.4 spz }
180 1.1.1.4 spz
181 1.1.1.4 spz if (c->pkeys[i].x509 != NULL) {
182 1.1.1.4 spz EVP_PKEY *pktmp;
183 1.1.1.4 spz pktmp = X509_get_pubkey(c->pkeys[i].x509);
184 1.1.1.4 spz EVP_PKEY_copy_parameters(pktmp, pkey);
185 1.1.1.4 spz EVP_PKEY_free(pktmp);
186 1.1.1.4 spz ERR_clear_error();
187 1.1 christos
188 1.1 christos #ifndef OPENSSL_NO_RSA
189 1.1.1.4 spz /*
190 1.1.1.4 spz * Don't check the public/private key, this is mostly for smart
191 1.1.1.4 spz * cards.
192 1.1.1.4 spz */
193 1.1.1.4 spz if ((pkey->type == EVP_PKEY_RSA) &&
194 1.1.1.4 spz (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
195 1.1.1.4 spz else
196 1.1 christos #endif
197 1.1.1.4 spz if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
198 1.1.1.4 spz X509_free(c->pkeys[i].x509);
199 1.1.1.4 spz c->pkeys[i].x509 = NULL;
200 1.1.1.4 spz return 0;
201 1.1.1.4 spz }
202 1.1.1.4 spz }
203 1.1.1.4 spz
204 1.1.1.4 spz if (c->pkeys[i].privatekey != NULL)
205 1.1.1.4 spz EVP_PKEY_free(c->pkeys[i].privatekey);
206 1.1.1.4 spz CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
207 1.1.1.4 spz c->pkeys[i].privatekey = pkey;
208 1.1.1.4 spz c->key = &(c->pkeys[i]);
209 1.1.1.4 spz
210 1.1.1.4 spz c->valid = 0;
211 1.1.1.4 spz return (1);
212 1.1.1.4 spz }
213 1.1 christos
214 1.1 christos #ifndef OPENSSL_NO_RSA
215 1.1.1.4 spz # ifndef OPENSSL_NO_STDIO
216 1.1 christos int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
217 1.1.1.4 spz {
218 1.1.1.4 spz int j, ret = 0;
219 1.1.1.4 spz BIO *in;
220 1.1.1.4 spz RSA *rsa = NULL;
221 1.1.1.4 spz
222 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
223 1.1.1.4 spz if (in == NULL) {
224 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
225 1.1.1.4 spz goto end;
226 1.1.1.4 spz }
227 1.1.1.4 spz
228 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
229 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
230 1.1.1.4 spz goto end;
231 1.1.1.4 spz }
232 1.1.1.4 spz if (type == SSL_FILETYPE_ASN1) {
233 1.1.1.4 spz j = ERR_R_ASN1_LIB;
234 1.1.1.4 spz rsa = d2i_RSAPrivateKey_bio(in, NULL);
235 1.1.1.4 spz } else if (type == SSL_FILETYPE_PEM) {
236 1.1.1.4 spz j = ERR_R_PEM_LIB;
237 1.1.1.4 spz rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
238 1.1.1.4 spz ssl->ctx->default_passwd_callback,
239 1.1.1.4 spz ssl->
240 1.1.1.4 spz ctx->default_passwd_callback_userdata);
241 1.1.1.4 spz } else {
242 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
243 1.1.1.4 spz goto end;
244 1.1.1.4 spz }
245 1.1.1.4 spz if (rsa == NULL) {
246 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
247 1.1.1.4 spz goto end;
248 1.1.1.4 spz }
249 1.1.1.4 spz ret = SSL_use_RSAPrivateKey(ssl, rsa);
250 1.1.1.4 spz RSA_free(rsa);
251 1.1.1.4 spz end:
252 1.1.1.4 spz if (in != NULL)
253 1.1.1.4 spz BIO_free(in);
254 1.1.1.4 spz return (ret);
255 1.1.1.4 spz }
256 1.1.1.4 spz # endif
257 1.1 christos
258 1.1 christos int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, unsigned char *d, long len)
259 1.1.1.4 spz {
260 1.1.1.4 spz int ret;
261 1.1.1.4 spz const unsigned char *p;
262 1.1.1.4 spz RSA *rsa;
263 1.1.1.4 spz
264 1.1.1.4 spz p = d;
265 1.1.1.4 spz if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
266 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
267 1.1.1.4 spz return (0);
268 1.1.1.4 spz }
269 1.1.1.4 spz
270 1.1.1.4 spz ret = SSL_use_RSAPrivateKey(ssl, rsa);
271 1.1.1.4 spz RSA_free(rsa);
272 1.1.1.4 spz return (ret);
273 1.1.1.4 spz }
274 1.1.1.4 spz #endif /* !OPENSSL_NO_RSA */
275 1.1 christos
276 1.1 christos int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
277 1.1.1.4 spz {
278 1.1.1.4 spz int ret;
279 1.1 christos
280 1.1.1.4 spz if (pkey == NULL) {
281 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
282 1.1.1.4 spz return (0);
283 1.1.1.4 spz }
284 1.1.1.4 spz if (!ssl_cert_inst(&ssl->cert)) {
285 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
286 1.1.1.4 spz return (0);
287 1.1.1.4 spz }
288 1.1.1.4 spz ret = ssl_set_pkey(ssl->cert, pkey);
289 1.1.1.4 spz return (ret);
290 1.1.1.4 spz }
291 1.1 christos
292 1.1 christos #ifndef OPENSSL_NO_STDIO
293 1.1 christos int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
294 1.1.1.4 spz {
295 1.1.1.4 spz int j, ret = 0;
296 1.1.1.4 spz BIO *in;
297 1.1.1.4 spz EVP_PKEY *pkey = NULL;
298 1.1.1.4 spz
299 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
300 1.1.1.4 spz if (in == NULL) {
301 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
302 1.1.1.4 spz goto end;
303 1.1.1.4 spz }
304 1.1.1.4 spz
305 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
306 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
307 1.1.1.4 spz goto end;
308 1.1.1.4 spz }
309 1.1.1.4 spz if (type == SSL_FILETYPE_PEM) {
310 1.1.1.4 spz j = ERR_R_PEM_LIB;
311 1.1.1.4 spz pkey = PEM_read_bio_PrivateKey(in, NULL,
312 1.1.1.4 spz ssl->ctx->default_passwd_callback,
313 1.1.1.4 spz ssl->
314 1.1.1.4 spz ctx->default_passwd_callback_userdata);
315 1.1.1.4 spz } else if (type == SSL_FILETYPE_ASN1) {
316 1.1.1.4 spz j = ERR_R_ASN1_LIB;
317 1.1.1.4 spz pkey = d2i_PrivateKey_bio(in, NULL);
318 1.1.1.4 spz } else {
319 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
320 1.1.1.4 spz goto end;
321 1.1.1.4 spz }
322 1.1.1.4 spz if (pkey == NULL) {
323 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
324 1.1.1.4 spz goto end;
325 1.1.1.4 spz }
326 1.1.1.4 spz ret = SSL_use_PrivateKey(ssl, pkey);
327 1.1.1.4 spz EVP_PKEY_free(pkey);
328 1.1.1.4 spz end:
329 1.1.1.4 spz if (in != NULL)
330 1.1.1.4 spz BIO_free(in);
331 1.1.1.4 spz return (ret);
332 1.1.1.4 spz }
333 1.1 christos #endif
334 1.1 christos
335 1.1.1.4 spz int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
336 1.1.1.4 spz long len)
337 1.1.1.4 spz {
338 1.1.1.4 spz int ret;
339 1.1.1.4 spz const unsigned char *p;
340 1.1.1.4 spz EVP_PKEY *pkey;
341 1.1.1.4 spz
342 1.1.1.4 spz p = d;
343 1.1.1.4 spz if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
344 1.1.1.4 spz SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
345 1.1.1.4 spz return (0);
346 1.1.1.4 spz }
347 1.1.1.4 spz
348 1.1.1.4 spz ret = SSL_use_PrivateKey(ssl, pkey);
349 1.1.1.4 spz EVP_PKEY_free(pkey);
350 1.1.1.4 spz return (ret);
351 1.1.1.4 spz }
352 1.1 christos
353 1.1 christos int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
354 1.1.1.4 spz {
355 1.1.1.4 spz if (x == NULL) {
356 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
357 1.1.1.4 spz return (0);
358 1.1.1.4 spz }
359 1.1.1.4 spz if (!ssl_cert_inst(&ctx->cert)) {
360 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
361 1.1.1.4 spz return (0);
362 1.1.1.4 spz }
363 1.1.1.4 spz return (ssl_set_cert(ctx->cert, x));
364 1.1.1.4 spz }
365 1.1 christos
366 1.1 christos static int ssl_set_cert(CERT *c, X509 *x)
367 1.1.1.4 spz {
368 1.1.1.4 spz EVP_PKEY *pkey;
369 1.1.1.4 spz int i;
370 1.1.1.4 spz
371 1.1.1.4 spz pkey = X509_get_pubkey(x);
372 1.1.1.4 spz if (pkey == NULL) {
373 1.1.1.4 spz SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
374 1.1.1.4 spz return (0);
375 1.1.1.4 spz }
376 1.1.1.4 spz
377 1.1.1.4 spz i = ssl_cert_type(x, pkey);
378 1.1.1.4 spz if (i < 0) {
379 1.1.1.4 spz SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
380 1.1.1.4 spz EVP_PKEY_free(pkey);
381 1.1.1.4 spz return (0);
382 1.1.1.4 spz }
383 1.1.1.4 spz
384 1.1.1.4 spz if (c->pkeys[i].privatekey != NULL) {
385 1.1.1.4 spz EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
386 1.1.1.4 spz ERR_clear_error();
387 1.1 christos
388 1.1 christos #ifndef OPENSSL_NO_RSA
389 1.1.1.4 spz /*
390 1.1.1.4 spz * Don't check the public/private key, this is mostly for smart
391 1.1.1.4 spz * cards.
392 1.1.1.4 spz */
393 1.1.1.4 spz if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
394 1.1.1.4 spz (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
395 1.1.1.4 spz RSA_METHOD_FLAG_NO_CHECK)) ;
396 1.1.1.4 spz else
397 1.1.1.4 spz #endif /* OPENSSL_NO_RSA */
398 1.1.1.4 spz if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
399 1.1.1.4 spz /*
400 1.1.1.4 spz * don't fail for a cert/key mismatch, just free current private
401 1.1.1.4 spz * key (when switching to a different cert & key, first this
402 1.1.1.4 spz * function should be used, then ssl_set_pkey
403 1.1.1.4 spz */
404 1.1.1.4 spz EVP_PKEY_free(c->pkeys[i].privatekey);
405 1.1.1.4 spz c->pkeys[i].privatekey = NULL;
406 1.1.1.4 spz /* clear error queue */
407 1.1.1.4 spz ERR_clear_error();
408 1.1.1.4 spz }
409 1.1.1.4 spz }
410 1.1.1.4 spz
411 1.1.1.4 spz EVP_PKEY_free(pkey);
412 1.1.1.4 spz
413 1.1.1.4 spz if (c->pkeys[i].x509 != NULL)
414 1.1.1.4 spz X509_free(c->pkeys[i].x509);
415 1.1.1.4 spz CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
416 1.1.1.4 spz c->pkeys[i].x509 = x;
417 1.1.1.4 spz c->key = &(c->pkeys[i]);
418 1.1.1.4 spz
419 1.1.1.4 spz c->valid = 0;
420 1.1.1.4 spz return (1);
421 1.1.1.4 spz }
422 1.1 christos
423 1.1 christos #ifndef OPENSSL_NO_STDIO
424 1.1 christos int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
425 1.1.1.4 spz {
426 1.1.1.4 spz int j;
427 1.1.1.4 spz BIO *in;
428 1.1.1.4 spz int ret = 0;
429 1.1.1.4 spz X509 *x = NULL;
430 1.1.1.4 spz
431 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
432 1.1.1.4 spz if (in == NULL) {
433 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
434 1.1.1.4 spz goto end;
435 1.1.1.4 spz }
436 1.1.1.4 spz
437 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
438 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
439 1.1.1.4 spz goto end;
440 1.1.1.4 spz }
441 1.1.1.4 spz if (type == SSL_FILETYPE_ASN1) {
442 1.1.1.4 spz j = ERR_R_ASN1_LIB;
443 1.1.1.4 spz x = d2i_X509_bio(in, NULL);
444 1.1.1.4 spz } else if (type == SSL_FILETYPE_PEM) {
445 1.1.1.4 spz j = ERR_R_PEM_LIB;
446 1.1.1.4 spz x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
447 1.1.1.4 spz ctx->default_passwd_callback_userdata);
448 1.1.1.4 spz } else {
449 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
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 (x == NULL) {
454 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
455 1.1.1.4 spz goto end;
456 1.1.1.4 spz }
457 1.1.1.4 spz
458 1.1.1.4 spz ret = SSL_CTX_use_certificate(ctx, x);
459 1.1.1.4 spz end:
460 1.1.1.4 spz if (x != NULL)
461 1.1.1.4 spz X509_free(x);
462 1.1.1.4 spz if (in != NULL)
463 1.1.1.4 spz BIO_free(in);
464 1.1.1.4 spz return (ret);
465 1.1.1.4 spz }
466 1.1 christos #endif
467 1.1 christos
468 1.1.1.4 spz int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
469 1.1.1.4 spz const unsigned char *d)
470 1.1.1.4 spz {
471 1.1.1.4 spz X509 *x;
472 1.1.1.4 spz int ret;
473 1.1.1.4 spz
474 1.1.1.4 spz x = d2i_X509(NULL, &d, (long)len);
475 1.1.1.4 spz if (x == NULL) {
476 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
477 1.1.1.4 spz return (0);
478 1.1.1.4 spz }
479 1.1.1.4 spz
480 1.1.1.4 spz ret = SSL_CTX_use_certificate(ctx, x);
481 1.1.1.4 spz X509_free(x);
482 1.1.1.4 spz return (ret);
483 1.1.1.4 spz }
484 1.1 christos
485 1.1 christos #ifndef OPENSSL_NO_RSA
486 1.1 christos int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
487 1.1.1.4 spz {
488 1.1.1.4 spz int ret;
489 1.1.1.4 spz EVP_PKEY *pkey;
490 1.1.1.4 spz
491 1.1.1.4 spz if (rsa == NULL) {
492 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
493 1.1.1.4 spz return (0);
494 1.1.1.4 spz }
495 1.1.1.4 spz if (!ssl_cert_inst(&ctx->cert)) {
496 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_MALLOC_FAILURE);
497 1.1.1.4 spz return (0);
498 1.1.1.4 spz }
499 1.1.1.4 spz if ((pkey = EVP_PKEY_new()) == NULL) {
500 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
501 1.1.1.4 spz return (0);
502 1.1.1.4 spz }
503 1.1.1.4 spz
504 1.1.1.4 spz RSA_up_ref(rsa);
505 1.1.1.4 spz EVP_PKEY_assign_RSA(pkey, rsa);
506 1.1.1.4 spz
507 1.1.1.4 spz ret = ssl_set_pkey(ctx->cert, pkey);
508 1.1.1.4 spz EVP_PKEY_free(pkey);
509 1.1.1.4 spz return (ret);
510 1.1.1.4 spz }
511 1.1 christos
512 1.1.1.4 spz # ifndef OPENSSL_NO_STDIO
513 1.1 christos int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
514 1.1.1.4 spz {
515 1.1.1.4 spz int j, ret = 0;
516 1.1.1.4 spz BIO *in;
517 1.1.1.4 spz RSA *rsa = NULL;
518 1.1.1.4 spz
519 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
520 1.1.1.4 spz if (in == NULL) {
521 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
522 1.1.1.4 spz goto end;
523 1.1.1.4 spz }
524 1.1.1.4 spz
525 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
526 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
527 1.1.1.4 spz goto end;
528 1.1.1.4 spz }
529 1.1.1.4 spz if (type == SSL_FILETYPE_ASN1) {
530 1.1.1.4 spz j = ERR_R_ASN1_LIB;
531 1.1.1.4 spz rsa = d2i_RSAPrivateKey_bio(in, NULL);
532 1.1.1.4 spz } else if (type == SSL_FILETYPE_PEM) {
533 1.1.1.4 spz j = ERR_R_PEM_LIB;
534 1.1.1.4 spz rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
535 1.1.1.4 spz ctx->default_passwd_callback,
536 1.1.1.4 spz ctx->default_passwd_callback_userdata);
537 1.1.1.4 spz } else {
538 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
539 1.1.1.4 spz goto end;
540 1.1.1.4 spz }
541 1.1.1.4 spz if (rsa == NULL) {
542 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
543 1.1.1.4 spz goto end;
544 1.1.1.4 spz }
545 1.1.1.4 spz ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
546 1.1.1.4 spz RSA_free(rsa);
547 1.1.1.4 spz end:
548 1.1.1.4 spz if (in != NULL)
549 1.1.1.4 spz BIO_free(in);
550 1.1.1.4 spz return (ret);
551 1.1.1.4 spz }
552 1.1.1.4 spz # endif
553 1.1.1.4 spz
554 1.1.1.4 spz int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
555 1.1.1.4 spz long len)
556 1.1.1.4 spz {
557 1.1.1.4 spz int ret;
558 1.1.1.4 spz const unsigned char *p;
559 1.1.1.4 spz RSA *rsa;
560 1.1.1.4 spz
561 1.1.1.4 spz p = d;
562 1.1.1.4 spz if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
563 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
564 1.1.1.4 spz return (0);
565 1.1.1.4 spz }
566 1.1.1.4 spz
567 1.1.1.4 spz ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
568 1.1.1.4 spz RSA_free(rsa);
569 1.1.1.4 spz return (ret);
570 1.1.1.4 spz }
571 1.1.1.4 spz #endif /* !OPENSSL_NO_RSA */
572 1.1 christos
573 1.1 christos int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
574 1.1.1.4 spz {
575 1.1.1.4 spz if (pkey == NULL) {
576 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
577 1.1.1.4 spz return (0);
578 1.1.1.4 spz }
579 1.1.1.4 spz if (!ssl_cert_inst(&ctx->cert)) {
580 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
581 1.1.1.4 spz return (0);
582 1.1.1.4 spz }
583 1.1.1.4 spz return (ssl_set_pkey(ctx->cert, pkey));
584 1.1.1.4 spz }
585 1.1 christos
586 1.1 christos #ifndef OPENSSL_NO_STDIO
587 1.1 christos int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
588 1.1.1.4 spz {
589 1.1.1.4 spz int j, ret = 0;
590 1.1.1.4 spz BIO *in;
591 1.1.1.4 spz EVP_PKEY *pkey = NULL;
592 1.1.1.4 spz
593 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
594 1.1.1.4 spz if (in == NULL) {
595 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
596 1.1.1.4 spz goto end;
597 1.1.1.4 spz }
598 1.1.1.4 spz
599 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
600 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
601 1.1.1.4 spz goto end;
602 1.1.1.4 spz }
603 1.1.1.4 spz if (type == SSL_FILETYPE_PEM) {
604 1.1.1.4 spz j = ERR_R_PEM_LIB;
605 1.1.1.4 spz pkey = PEM_read_bio_PrivateKey(in, NULL,
606 1.1.1.4 spz ctx->default_passwd_callback,
607 1.1.1.4 spz ctx->default_passwd_callback_userdata);
608 1.1.1.4 spz } else if (type == SSL_FILETYPE_ASN1) {
609 1.1.1.4 spz j = ERR_R_ASN1_LIB;
610 1.1.1.4 spz pkey = d2i_PrivateKey_bio(in, NULL);
611 1.1.1.4 spz } else {
612 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
613 1.1.1.4 spz goto end;
614 1.1.1.4 spz }
615 1.1.1.4 spz if (pkey == NULL) {
616 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
617 1.1.1.4 spz goto end;
618 1.1.1.4 spz }
619 1.1.1.4 spz ret = SSL_CTX_use_PrivateKey(ctx, pkey);
620 1.1.1.4 spz EVP_PKEY_free(pkey);
621 1.1.1.4 spz end:
622 1.1.1.4 spz if (in != NULL)
623 1.1.1.4 spz BIO_free(in);
624 1.1.1.4 spz return (ret);
625 1.1.1.4 spz }
626 1.1 christos #endif
627 1.1 christos
628 1.1.1.4 spz int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
629 1.1.1.4 spz const unsigned char *d, long len)
630 1.1.1.4 spz {
631 1.1.1.4 spz int ret;
632 1.1.1.4 spz const unsigned char *p;
633 1.1.1.4 spz EVP_PKEY *pkey;
634 1.1.1.4 spz
635 1.1.1.4 spz p = d;
636 1.1.1.4 spz if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
637 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
638 1.1.1.4 spz return (0);
639 1.1.1.4 spz }
640 1.1.1.4 spz
641 1.1.1.4 spz ret = SSL_CTX_use_PrivateKey(ctx, pkey);
642 1.1.1.4 spz EVP_PKEY_free(pkey);
643 1.1.1.4 spz return (ret);
644 1.1.1.4 spz }
645 1.1 christos
646 1.1 christos #ifndef OPENSSL_NO_STDIO
647 1.1.1.4 spz /*
648 1.1.1.4 spz * Read a file that contains our certificate in "PEM" format, possibly
649 1.1.1.4 spz * followed by a sequence of CA certificates that should be sent to the peer
650 1.1.1.4 spz * in the Certificate message.
651 1.1 christos */
652 1.1 christos int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
653 1.1.1.4 spz {
654 1.1.1.4 spz BIO *in;
655 1.1.1.4 spz int ret = 0;
656 1.1.1.4 spz X509 *x = NULL;
657 1.1.1.4 spz
658 1.1.1.4 spz ERR_clear_error(); /* clear error stack for
659 1.1.1.4 spz * SSL_CTX_use_certificate() */
660 1.1.1.4 spz
661 1.1.1.4 spz in = BIO_new(BIO_s_file_internal());
662 1.1.1.4 spz if (in == NULL) {
663 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
664 1.1.1.4 spz goto end;
665 1.1.1.4 spz }
666 1.1.1.4 spz
667 1.1.1.4 spz if (BIO_read_filename(in, file) <= 0) {
668 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
669 1.1.1.4 spz goto end;
670 1.1.1.4 spz }
671 1.1.1.4 spz
672 1.1.1.4 spz x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
673 1.1.1.4 spz ctx->default_passwd_callback_userdata);
674 1.1.1.4 spz if (x == NULL) {
675 1.1.1.4 spz SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
676 1.1.1.4 spz goto end;
677 1.1.1.4 spz }
678 1.1.1.4 spz
679 1.1.1.4 spz ret = SSL_CTX_use_certificate(ctx, x);
680 1.1.1.4 spz
681 1.1.1.4 spz if (ERR_peek_error() != 0)
682 1.1.1.4 spz ret = 0; /* Key/certificate mismatch doesn't imply
683 1.1.1.4 spz * ret==0 ... */
684 1.1.1.4 spz if (ret) {
685 1.1.1.4 spz /*
686 1.1.1.4 spz * If we could set up our certificate, now proceed to the CA
687 1.1.1.4 spz * certificates.
688 1.1.1.4 spz */
689 1.1.1.4 spz X509 *ca;
690 1.1.1.4 spz int r;
691 1.1.1.4 spz unsigned long err;
692 1.1.1.4 spz
693 1.1.1.4 spz if (ctx->extra_certs != NULL) {
694 1.1.1.4 spz sk_X509_pop_free(ctx->extra_certs, X509_free);
695 1.1.1.4 spz ctx->extra_certs = NULL;
696 1.1.1.4 spz }
697 1.1.1.4 spz
698 1.1.1.4 spz while ((ca = PEM_read_bio_X509(in, NULL,
699 1.1.1.4 spz ctx->default_passwd_callback,
700 1.1.1.4 spz ctx->default_passwd_callback_userdata))
701 1.1.1.4 spz != NULL) {
702 1.1.1.4 spz r = SSL_CTX_add_extra_chain_cert(ctx, ca);
703 1.1.1.4 spz if (!r) {
704 1.1.1.4 spz X509_free(ca);
705 1.1.1.4 spz ret = 0;
706 1.1.1.4 spz goto end;
707 1.1.1.4 spz }
708 1.1.1.4 spz /*
709 1.1.1.4 spz * Note that we must not free r if it was successfully added to
710 1.1.1.4 spz * the chain (while we must free the main certificate, since its
711 1.1.1.4 spz * reference count is increased by SSL_CTX_use_certificate).
712 1.1.1.4 spz */
713 1.1.1.4 spz }
714 1.1.1.4 spz /* When the while loop ends, it's usually just EOF. */
715 1.1.1.4 spz err = ERR_peek_last_error();
716 1.1.1.4 spz if (ERR_GET_LIB(err) == ERR_LIB_PEM
717 1.1.1.4 spz && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
718 1.1.1.4 spz ERR_clear_error();
719 1.1.1.4 spz else
720 1.1.1.4 spz ret = 0; /* some real error */
721 1.1.1.4 spz }
722 1.1.1.4 spz
723 1.1.1.4 spz end:
724 1.1.1.4 spz if (x != NULL)
725 1.1.1.4 spz X509_free(x);
726 1.1.1.4 spz if (in != NULL)
727 1.1.1.4 spz BIO_free(in);
728 1.1.1.4 spz return (ret);
729 1.1.1.4 spz }
730 1.1 christos #endif
731