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