]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/ssl_rsa.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / ssl / ssl_rsa.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58 #include <stdio.h>
59 #include "ssl_locl.h"
60 #include <openssl/bio.h>
61 #include <openssl/objects.h>
62 #include <openssl/evp.h>
63 #include <openssl/x509.h>
64 #include <openssl/pem.h>
65
66 static int ssl_set_cert(CERT *c, X509 *x509);
67 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
68 int SSL_use_certificate(SSL *ssl, X509 *x)
69 {
70 int rv;
71 if (x == NULL) {
72 SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
73 return (0);
74 }
75 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
76 if (rv != 1) {
77 SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
78 return 0;
79 }
80
81 return (ssl_set_cert(ssl->cert, x));
82 }
83
84 #ifndef OPENSSL_NO_STDIO
85 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
86 {
87 int j;
88 BIO *in;
89 int ret = 0;
90 X509 *x = NULL;
91
92 in = BIO_new(BIO_s_file());
93 if (in == NULL) {
94 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
95 goto end;
96 }
97
98 if (BIO_read_filename(in, file) <= 0) {
99 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
100 goto end;
101 }
102 if (type == SSL_FILETYPE_ASN1) {
103 j = ERR_R_ASN1_LIB;
104 x = d2i_X509_bio(in, NULL);
105 } else if (type == SSL_FILETYPE_PEM) {
106 j = ERR_R_PEM_LIB;
107 x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
108 ssl->ctx->default_passwd_callback_userdata);
109 } else {
110 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
111 goto end;
112 }
113
114 if (x == NULL) {
115 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
116 goto end;
117 }
118
119 ret = SSL_use_certificate(ssl, x);
120 end:
121 X509_free(x);
122 BIO_free(in);
123 return (ret);
124 }
125 #endif
126
127 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
128 {
129 X509 *x;
130 int ret;
131
132 x = d2i_X509(NULL, &d, (long)len);
133 if (x == NULL) {
134 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
135 return (0);
136 }
137
138 ret = SSL_use_certificate(ssl, x);
139 X509_free(x);
140 return (ret);
141 }
142
143 #ifndef OPENSSL_NO_RSA
144 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
145 {
146 EVP_PKEY *pkey;
147 int ret;
148
149 if (rsa == NULL) {
150 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
151 return (0);
152 }
153 if ((pkey = EVP_PKEY_new()) == NULL) {
154 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
155 return (0);
156 }
157
158 RSA_up_ref(rsa);
159 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
160 RSA_free(rsa);
161 return 0;
162 }
163
164 ret = ssl_set_pkey(ssl->cert, pkey);
165 EVP_PKEY_free(pkey);
166 return (ret);
167 }
168 #endif
169
170 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
171 {
172 int i;
173 i = ssl_cert_type(NULL, pkey);
174 if (i < 0) {
175 SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
176 return (0);
177 }
178
179 if (c->pkeys[i].x509 != NULL) {
180 EVP_PKEY *pktmp;
181 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
182 if (pktmp == NULL) {
183 SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
184 return 0;
185 }
186 /*
187 * The return code from EVP_PKEY_copy_parameters is deliberately
188 * ignored. Some EVP_PKEY types cannot do this.
189 */
190 EVP_PKEY_copy_parameters(pktmp, pkey);
191 ERR_clear_error();
192
193 #ifndef OPENSSL_NO_RSA
194 /*
195 * Don't check the public/private key, this is mostly for smart
196 * cards.
197 */
198 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
199 && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK);
200 else
201 #endif
202 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
203 X509_free(c->pkeys[i].x509);
204 c->pkeys[i].x509 = NULL;
205 return 0;
206 }
207 }
208
209 EVP_PKEY_free(c->pkeys[i].privatekey);
210 EVP_PKEY_up_ref(pkey);
211 c->pkeys[i].privatekey = pkey;
212 c->key = &(c->pkeys[i]);
213 return (1);
214 }
215
216 #ifndef OPENSSL_NO_RSA
217 # ifndef OPENSSL_NO_STDIO
218 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
219 {
220 int j, ret = 0;
221 BIO *in;
222 RSA *rsa = NULL;
223
224 in = BIO_new(BIO_s_file());
225 if (in == NULL) {
226 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
227 goto end;
228 }
229
230 if (BIO_read_filename(in, file) <= 0) {
231 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
232 goto end;
233 }
234 if (type == SSL_FILETYPE_ASN1) {
235 j = ERR_R_ASN1_LIB;
236 rsa = d2i_RSAPrivateKey_bio(in, NULL);
237 } else if (type == SSL_FILETYPE_PEM) {
238 j = ERR_R_PEM_LIB;
239 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
240 ssl->ctx->default_passwd_callback,
241 ssl->
242 ctx->default_passwd_callback_userdata);
243 } else {
244 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
245 goto end;
246 }
247 if (rsa == NULL) {
248 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
249 goto end;
250 }
251 ret = SSL_use_RSAPrivateKey(ssl, rsa);
252 RSA_free(rsa);
253 end:
254 BIO_free(in);
255 return (ret);
256 }
257 # endif
258
259 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
260 {
261 int ret;
262 const unsigned char *p;
263 RSA *rsa;
264
265 p = d;
266 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
267 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
268 return (0);
269 }
270
271 ret = SSL_use_RSAPrivateKey(ssl, rsa);
272 RSA_free(rsa);
273 return (ret);
274 }
275 #endif /* !OPENSSL_NO_RSA */
276
277 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
278 {
279 int ret;
280
281 if (pkey == NULL) {
282 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
283 return (0);
284 }
285 ret = ssl_set_pkey(ssl->cert, pkey);
286 return (ret);
287 }
288
289 #ifndef OPENSSL_NO_STDIO
290 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
291 {
292 int j, ret = 0;
293 BIO *in;
294 EVP_PKEY *pkey = NULL;
295
296 in = BIO_new(BIO_s_file());
297 if (in == NULL) {
298 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
299 goto end;
300 }
301
302 if (BIO_read_filename(in, file) <= 0) {
303 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
304 goto end;
305 }
306 if (type == SSL_FILETYPE_PEM) {
307 j = ERR_R_PEM_LIB;
308 pkey = PEM_read_bio_PrivateKey(in, NULL,
309 ssl->ctx->default_passwd_callback,
310 ssl->
311 ctx->default_passwd_callback_userdata);
312 } else if (type == SSL_FILETYPE_ASN1) {
313 j = ERR_R_ASN1_LIB;
314 pkey = d2i_PrivateKey_bio(in, NULL);
315 } else {
316 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
317 goto end;
318 }
319 if (pkey == NULL) {
320 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
321 goto end;
322 }
323 ret = SSL_use_PrivateKey(ssl, pkey);
324 EVP_PKEY_free(pkey);
325 end:
326 BIO_free(in);
327 return (ret);
328 }
329 #endif
330
331 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
332 long len)
333 {
334 int ret;
335 const unsigned char *p;
336 EVP_PKEY *pkey;
337
338 p = d;
339 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
340 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
341 return (0);
342 }
343
344 ret = SSL_use_PrivateKey(ssl, pkey);
345 EVP_PKEY_free(pkey);
346 return (ret);
347 }
348
349 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
350 {
351 int rv;
352 if (x == NULL) {
353 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
354 return (0);
355 }
356 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
357 if (rv != 1) {
358 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
359 return 0;
360 }
361 return (ssl_set_cert(ctx->cert, x));
362 }
363
364 static int ssl_set_cert(CERT *c, X509 *x)
365 {
366 EVP_PKEY *pkey;
367 int i;
368
369 pkey = X509_get0_pubkey(x);
370 if (pkey == NULL) {
371 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
372 return (0);
373 }
374
375 i = ssl_cert_type(x, pkey);
376 if (i < 0) {
377 SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
378 return 0;
379 }
380
381 if (c->pkeys[i].privatekey != NULL) {
382 /*
383 * The return code from EVP_PKEY_copy_parameters is deliberately
384 * ignored. Some EVP_PKEY types cannot do this.
385 */
386 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
387 ERR_clear_error();
388
389 #ifndef OPENSSL_NO_RSA
390 /*
391 * Don't check the public/private key, this is mostly for smart
392 * cards.
393 */
394 if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
395 && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
396 RSA_METHOD_FLAG_NO_CHECK) ;
397 else
398 #endif /* OPENSSL_NO_RSA */
399 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
400 /*
401 * don't fail for a cert/key mismatch, just free current private
402 * key (when switching to a different cert & key, first this
403 * function should be used, then ssl_set_pkey
404 */
405 EVP_PKEY_free(c->pkeys[i].privatekey);
406 c->pkeys[i].privatekey = NULL;
407 /* clear error queue */
408 ERR_clear_error();
409 }
410 }
411
412 X509_free(c->pkeys[i].x509);
413 X509_up_ref(x);
414 c->pkeys[i].x509 = x;
415 c->key = &(c->pkeys[i]);
416
417 return 1;
418 }
419
420 #ifndef OPENSSL_NO_STDIO
421 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
422 {
423 int j;
424 BIO *in;
425 int ret = 0;
426 X509 *x = NULL;
427
428 in = BIO_new(BIO_s_file());
429 if (in == NULL) {
430 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
431 goto end;
432 }
433
434 if (BIO_read_filename(in, file) <= 0) {
435 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
436 goto end;
437 }
438 if (type == SSL_FILETYPE_ASN1) {
439 j = ERR_R_ASN1_LIB;
440 x = d2i_X509_bio(in, NULL);
441 } else if (type == SSL_FILETYPE_PEM) {
442 j = ERR_R_PEM_LIB;
443 x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
444 ctx->default_passwd_callback_userdata);
445 } else {
446 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
447 goto end;
448 }
449
450 if (x == NULL) {
451 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
452 goto end;
453 }
454
455 ret = SSL_CTX_use_certificate(ctx, x);
456 end:
457 X509_free(x);
458 BIO_free(in);
459 return (ret);
460 }
461 #endif
462
463 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
464 const unsigned char *d)
465 {
466 X509 *x;
467 int ret;
468
469 x = d2i_X509(NULL, &d, (long)len);
470 if (x == NULL) {
471 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
472 return (0);
473 }
474
475 ret = SSL_CTX_use_certificate(ctx, x);
476 X509_free(x);
477 return (ret);
478 }
479
480 #ifndef OPENSSL_NO_RSA
481 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
482 {
483 int ret;
484 EVP_PKEY *pkey;
485
486 if (rsa == NULL) {
487 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
488 return (0);
489 }
490 if ((pkey = EVP_PKEY_new()) == NULL) {
491 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
492 return (0);
493 }
494
495 RSA_up_ref(rsa);
496 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
497 RSA_free(rsa);
498 return 0;
499 }
500
501 ret = ssl_set_pkey(ctx->cert, pkey);
502 EVP_PKEY_free(pkey);
503 return (ret);
504 }
505
506 # ifndef OPENSSL_NO_STDIO
507 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
508 {
509 int j, ret = 0;
510 BIO *in;
511 RSA *rsa = NULL;
512
513 in = BIO_new(BIO_s_file());
514 if (in == NULL) {
515 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
516 goto end;
517 }
518
519 if (BIO_read_filename(in, file) <= 0) {
520 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
521 goto end;
522 }
523 if (type == SSL_FILETYPE_ASN1) {
524 j = ERR_R_ASN1_LIB;
525 rsa = d2i_RSAPrivateKey_bio(in, NULL);
526 } else if (type == SSL_FILETYPE_PEM) {
527 j = ERR_R_PEM_LIB;
528 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
529 ctx->default_passwd_callback,
530 ctx->default_passwd_callback_userdata);
531 } else {
532 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
533 goto end;
534 }
535 if (rsa == NULL) {
536 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
537 goto end;
538 }
539 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
540 RSA_free(rsa);
541 end:
542 BIO_free(in);
543 return (ret);
544 }
545 # endif
546
547 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
548 long len)
549 {
550 int ret;
551 const unsigned char *p;
552 RSA *rsa;
553
554 p = d;
555 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
556 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
557 return (0);
558 }
559
560 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
561 RSA_free(rsa);
562 return (ret);
563 }
564 #endif /* !OPENSSL_NO_RSA */
565
566 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
567 {
568 if (pkey == NULL) {
569 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
570 return (0);
571 }
572 return (ssl_set_pkey(ctx->cert, pkey));
573 }
574
575 #ifndef OPENSSL_NO_STDIO
576 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
577 {
578 int j, ret = 0;
579 BIO *in;
580 EVP_PKEY *pkey = NULL;
581
582 in = BIO_new(BIO_s_file());
583 if (in == NULL) {
584 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
585 goto end;
586 }
587
588 if (BIO_read_filename(in, file) <= 0) {
589 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
590 goto end;
591 }
592 if (type == SSL_FILETYPE_PEM) {
593 j = ERR_R_PEM_LIB;
594 pkey = PEM_read_bio_PrivateKey(in, NULL,
595 ctx->default_passwd_callback,
596 ctx->default_passwd_callback_userdata);
597 } else if (type == SSL_FILETYPE_ASN1) {
598 j = ERR_R_ASN1_LIB;
599 pkey = d2i_PrivateKey_bio(in, NULL);
600 } else {
601 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
602 goto end;
603 }
604 if (pkey == NULL) {
605 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
606 goto end;
607 }
608 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
609 EVP_PKEY_free(pkey);
610 end:
611 BIO_free(in);
612 return (ret);
613 }
614 #endif
615
616 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
617 const unsigned char *d, long len)
618 {
619 int ret;
620 const unsigned char *p;
621 EVP_PKEY *pkey;
622
623 p = d;
624 if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
625 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
626 return (0);
627 }
628
629 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
630 EVP_PKEY_free(pkey);
631 return (ret);
632 }
633
634 #ifndef OPENSSL_NO_STDIO
635 /*
636 * Read a file that contains our certificate in "PEM" format, possibly
637 * followed by a sequence of CA certificates that should be sent to the peer
638 * in the Certificate message.
639 */
640 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
641 {
642 BIO *in;
643 int ret = 0;
644 X509 *x = NULL;
645 pem_password_cb *passwd_callback;
646 void *passwd_callback_userdata;
647
648 ERR_clear_error(); /* clear error stack for
649 * SSL_CTX_use_certificate() */
650
651 if (ctx != NULL) {
652 passwd_callback = ctx->default_passwd_callback;
653 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
654 } else {
655 passwd_callback = ssl->default_passwd_callback;
656 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
657 }
658
659 in = BIO_new(BIO_s_file());
660 if (in == NULL) {
661 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
662 goto end;
663 }
664
665 if (BIO_read_filename(in, file) <= 0) {
666 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
667 goto end;
668 }
669
670 x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
671 passwd_callback_userdata);
672 if (x == NULL) {
673 SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
674 goto end;
675 }
676
677 if (ctx)
678 ret = SSL_CTX_use_certificate(ctx, x);
679 else
680 ret = SSL_use_certificate(ssl, x);
681
682 if (ERR_peek_error() != 0)
683 ret = 0; /* Key/certificate mismatch doesn't imply
684 * ret==0 ... */
685 if (ret) {
686 /*
687 * If we could set up our certificate, now proceed to the CA
688 * certificates.
689 */
690 X509 *ca;
691 int r;
692 unsigned long err;
693
694 if (ctx)
695 r = SSL_CTX_clear_chain_certs(ctx);
696 else
697 r = SSL_clear_chain_certs(ssl);
698
699 if (r == 0) {
700 ret = 0;
701 goto end;
702 }
703
704 while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
705 passwd_callback_userdata))
706 != NULL) {
707 if (ctx)
708 r = SSL_CTX_add0_chain_cert(ctx, ca);
709 else
710 r = SSL_add0_chain_cert(ssl, ca);
711 /*
712 * Note that we must not free ca if it was successfully added to
713 * the chain (while we must free the main certificate, since its
714 * reference count is increased by SSL_CTX_use_certificate).
715 */
716 if (!r) {
717 X509_free(ca);
718 ret = 0;
719 goto end;
720 }
721 }
722 /* When the while loop ends, it's usually just EOF. */
723 err = ERR_peek_last_error();
724 if (ERR_GET_LIB(err) == ERR_LIB_PEM
725 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
726 ERR_clear_error();
727 else
728 ret = 0; /* some real error */
729 }
730
731 end:
732 X509_free(x);
733 BIO_free(in);
734 return (ret);
735 }
736
737 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
738 {
739 return use_certificate_chain_file(ctx, NULL, file);
740 }
741
742 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
743 {
744 return use_certificate_chain_file(NULL, ssl, file);
745 }
746 #endif
747
748 static int serverinfo_find_extension(const unsigned char *serverinfo,
749 size_t serverinfo_length,
750 unsigned int extension_type,
751 const unsigned char **extension_data,
752 size_t *extension_length)
753 {
754 *extension_data = NULL;
755 *extension_length = 0;
756 if (serverinfo == NULL || serverinfo_length == 0)
757 return -1;
758 for (;;) {
759 unsigned int type = 0;
760 size_t len = 0;
761
762 /* end of serverinfo */
763 if (serverinfo_length == 0)
764 return 0; /* Extension not found */
765
766 /* read 2-byte type field */
767 if (serverinfo_length < 2)
768 return -1; /* Error */
769 type = (serverinfo[0] << 8) + serverinfo[1];
770 serverinfo += 2;
771 serverinfo_length -= 2;
772
773 /* read 2-byte len field */
774 if (serverinfo_length < 2)
775 return -1; /* Error */
776 len = (serverinfo[0] << 8) + serverinfo[1];
777 serverinfo += 2;
778 serverinfo_length -= 2;
779
780 if (len > serverinfo_length)
781 return -1; /* Error */
782
783 if (type == extension_type) {
784 *extension_data = serverinfo;
785 *extension_length = len;
786 return 1; /* Success */
787 }
788
789 serverinfo += len;
790 serverinfo_length -= len;
791 }
792 /* Unreachable */
793 }
794
795 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
796 const unsigned char *in,
797 size_t inlen, int *al, void *arg)
798 {
799
800 if (inlen != 0) {
801 *al = SSL_AD_DECODE_ERROR;
802 return 0;
803 }
804
805 return 1;
806 }
807
808 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
809 const unsigned char **out, size_t *outlen,
810 int *al, void *arg)
811 {
812 const unsigned char *serverinfo = NULL;
813 size_t serverinfo_length = 0;
814
815 /* Is there serverinfo data for the chosen server cert? */
816 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
817 &serverinfo_length)) != 0) {
818 /* Find the relevant extension from the serverinfo */
819 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
820 ext_type, out, outlen);
821 if (retval == -1) {
822 *al = SSL_AD_DECODE_ERROR;
823 return -1; /* Error */
824 }
825 if (retval == 0)
826 return 0; /* No extension found, don't send extension */
827 return 1; /* Send extension */
828 }
829 return -1; /* No serverinfo data found, don't send
830 * extension */
831 }
832
833 /*
834 * With a NULL context, this function just checks that the serverinfo data
835 * parses correctly. With a non-NULL context, it registers callbacks for
836 * the included extensions.
837 */
838 static int serverinfo_process_buffer(const unsigned char *serverinfo,
839 size_t serverinfo_length, SSL_CTX *ctx)
840 {
841 if (serverinfo == NULL || serverinfo_length == 0)
842 return 0;
843 for (;;) {
844 unsigned int ext_type = 0;
845 size_t len = 0;
846
847 /* end of serverinfo */
848 if (serverinfo_length == 0)
849 return 1;
850
851 /* read 2-byte type field */
852 if (serverinfo_length < 2)
853 return 0;
854 /* FIXME: check for types we understand explicitly? */
855
856 /* Register callbacks for extensions */
857 ext_type = (serverinfo[0] << 8) + serverinfo[1];
858 if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
859 serverinfo_srv_add_cb,
860 NULL, NULL,
861 serverinfo_srv_parse_cb,
862 NULL))
863 return 0;
864
865 serverinfo += 2;
866 serverinfo_length -= 2;
867
868 /* read 2-byte len field */
869 if (serverinfo_length < 2)
870 return 0;
871 len = (serverinfo[0] << 8) + serverinfo[1];
872 serverinfo += 2;
873 serverinfo_length -= 2;
874
875 if (len > serverinfo_length)
876 return 0;
877
878 serverinfo += len;
879 serverinfo_length -= len;
880 }
881 }
882
883 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
884 size_t serverinfo_length)
885 {
886 unsigned char *new_serverinfo;
887
888 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
889 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
890 return 0;
891 }
892 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
893 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
894 return 0;
895 }
896 if (ctx->cert->key == NULL) {
897 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
898 return 0;
899 }
900 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
901 serverinfo_length);
902 if (new_serverinfo == NULL) {
903 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
904 return 0;
905 }
906 ctx->cert->key->serverinfo = new_serverinfo;
907 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
908 ctx->cert->key->serverinfo_length = serverinfo_length;
909
910 /*
911 * Now that the serverinfo is validated and stored, go ahead and
912 * register callbacks.
913 */
914 if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
915 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
916 return 0;
917 }
918 return 1;
919 }
920
921 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
922 {
923 unsigned char *serverinfo = NULL;
924 size_t serverinfo_length = 0;
925 unsigned char *extension = 0;
926 long extension_length = 0;
927 char *name = NULL;
928 char *header = NULL;
929 char namePrefix[] = "SERVERINFO FOR ";
930 int ret = 0;
931 BIO *bin = NULL;
932 size_t num_extensions = 0;
933
934 if (ctx == NULL || file == NULL) {
935 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
936 ERR_R_PASSED_NULL_PARAMETER);
937 goto end;
938 }
939
940 bin = BIO_new(BIO_s_file());
941 if (bin == NULL) {
942 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
943 goto end;
944 }
945 if (BIO_read_filename(bin, file) <= 0) {
946 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
947 goto end;
948 }
949
950 for (num_extensions = 0;; num_extensions++) {
951 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
952 == 0) {
953 /*
954 * There must be at least one extension in this file
955 */
956 if (num_extensions == 0) {
957 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
958 SSL_R_NO_PEM_EXTENSIONS);
959 goto end;
960 } else /* End of file, we're done */
961 break;
962 }
963 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
964 if (strlen(name) < strlen(namePrefix)) {
965 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
966 SSL_R_PEM_NAME_TOO_SHORT);
967 goto end;
968 }
969 if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
970 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
971 SSL_R_PEM_NAME_BAD_PREFIX);
972 goto end;
973 }
974 /*
975 * Check that the decoded PEM data is plausible (valid length field)
976 */
977 if (extension_length < 4
978 || (extension[2] << 8) + extension[3] != extension_length - 4) {
979 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
980 goto end;
981 }
982 /* Append the decoded extension to the serverinfo buffer */
983 serverinfo =
984 OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
985 if (serverinfo == NULL) {
986 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
987 goto end;
988 }
989 memcpy(serverinfo + serverinfo_length, extension, extension_length);
990 serverinfo_length += extension_length;
991
992 OPENSSL_free(name);
993 name = NULL;
994 OPENSSL_free(header);
995 header = NULL;
996 OPENSSL_free(extension);
997 extension = NULL;
998 }
999
1000 ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1001 end:
1002 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1003 OPENSSL_free(name);
1004 OPENSSL_free(header);
1005 OPENSSL_free(extension);
1006 OPENSSL_free(serverinfo);
1007 BIO_free(bin);
1008 return ret;
1009 }