Create RECORD_LAYER_clear function.
[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     int rv;
72     if (x == NULL) {
73         SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
74         return (0);
75     }
76     rv = ssl_security_cert(ssl, NULL, x, 0, 1);
77     if (rv != 1) {
78         SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
79         return 0;
80     }
81
82     return (ssl_set_cert(ssl->cert, x));
83 }
84
85 #ifndef OPENSSL_NO_STDIO
86 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
87 {
88     int j;
89     BIO *in;
90     int ret = 0;
91     X509 *x = NULL;
92
93     in = BIO_new(BIO_s_file_internal());
94     if (in == NULL) {
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         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
101         goto end;
102     }
103     if (type == SSL_FILETYPE_ASN1) {
104         j = ERR_R_ASN1_LIB;
105         x = d2i_X509_bio(in, NULL);
106     } else if (type == SSL_FILETYPE_PEM) {
107         j = ERR_R_PEM_LIB;
108         x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
109                               ssl->ctx->default_passwd_callback_userdata);
110     } else {
111         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
112         goto end;
113     }
114
115     if (x == NULL) {
116         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
117         goto end;
118     }
119
120     ret = SSL_use_certificate(ssl, x);
121  end:
122     if (x != NULL)
123         X509_free(x);
124     BIO_free(in);
125     return (ret);
126 }
127 #endif
128
129 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
130 {
131     X509 *x;
132     int ret;
133
134     x = d2i_X509(NULL, &d, (long)len);
135     if (x == NULL) {
136         SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
137         return (0);
138     }
139
140     ret = SSL_use_certificate(ssl, x);
141     X509_free(x);
142     return (ret);
143 }
144
145 #ifndef OPENSSL_NO_RSA
146 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
147 {
148     EVP_PKEY *pkey;
149     int ret;
150
151     if (rsa == NULL) {
152         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
153         return (0);
154     }
155     if ((pkey = EVP_PKEY_new()) == NULL) {
156         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
157         return (0);
158     }
159
160     RSA_up_ref(rsa);
161     EVP_PKEY_assign_RSA(pkey, rsa);
162
163     ret = ssl_set_pkey(ssl->cert, pkey);
164     EVP_PKEY_free(pkey);
165     return (ret);
166 }
167 #endif
168
169 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
170 {
171     int i;
172     /*
173      * Special case for DH: check two DH certificate types for a match. This
174      * means for DH certificates we must set the certificate first.
175      */
176     if (pkey->type == EVP_PKEY_DH) {
177         X509 *x;
178         i = -1;
179         x = c->pkeys[SSL_PKEY_DH_RSA].x509;
180         if (x && X509_check_private_key(x, pkey))
181             i = SSL_PKEY_DH_RSA;
182         x = c->pkeys[SSL_PKEY_DH_DSA].x509;
183         if (i == -1 && x && X509_check_private_key(x, pkey))
184             i = SSL_PKEY_DH_DSA;
185         ERR_clear_error();
186     } else
187         i = ssl_cert_type(NULL, pkey);
188     if (i < 0) {
189         SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
190         return (0);
191     }
192
193     if (c->pkeys[i].x509 != NULL) {
194         EVP_PKEY *pktmp;
195         pktmp = X509_get_pubkey(c->pkeys[i].x509);
196         EVP_PKEY_copy_parameters(pktmp, pkey);
197         EVP_PKEY_free(pktmp);
198         ERR_clear_error();
199
200 #ifndef OPENSSL_NO_RSA
201         /*
202          * Don't check the public/private key, this is mostly for smart
203          * cards.
204          */
205         if ((pkey->type == EVP_PKEY_RSA) &&
206             (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
207         else
208 #endif
209         if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
210             X509_free(c->pkeys[i].x509);
211             c->pkeys[i].x509 = NULL;
212             return 0;
213         }
214     }
215
216     if (c->pkeys[i].privatekey != NULL)
217         EVP_PKEY_free(c->pkeys[i].privatekey);
218     CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
219     c->pkeys[i].privatekey = pkey;
220     c->key = &(c->pkeys[i]);
221
222     c->valid = 0;
223     return (1);
224 }
225
226 #ifndef OPENSSL_NO_RSA
227 # ifndef OPENSSL_NO_STDIO
228 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
229 {
230     int j, ret = 0;
231     BIO *in;
232     RSA *rsa = NULL;
233
234     in = BIO_new(BIO_s_file_internal());
235     if (in == NULL) {
236         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
237         goto end;
238     }
239
240     if (BIO_read_filename(in, file) <= 0) {
241         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
242         goto end;
243     }
244     if (type == SSL_FILETYPE_ASN1) {
245         j = ERR_R_ASN1_LIB;
246         rsa = d2i_RSAPrivateKey_bio(in, NULL);
247     } else if (type == SSL_FILETYPE_PEM) {
248         j = ERR_R_PEM_LIB;
249         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
250                                          ssl->ctx->default_passwd_callback,
251                                          ssl->
252                                          ctx->default_passwd_callback_userdata);
253     } else {
254         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
255         goto end;
256     }
257     if (rsa == NULL) {
258         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
259         goto end;
260     }
261     ret = SSL_use_RSAPrivateKey(ssl, rsa);
262     RSA_free(rsa);
263  end:
264     BIO_free(in);
265     return (ret);
266 }
267 # endif
268
269 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
270 {
271     int ret;
272     const unsigned char *p;
273     RSA *rsa;
274
275     p = d;
276     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
277         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
278         return (0);
279     }
280
281     ret = SSL_use_RSAPrivateKey(ssl, rsa);
282     RSA_free(rsa);
283     return (ret);
284 }
285 #endif                          /* !OPENSSL_NO_RSA */
286
287 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
288 {
289     int ret;
290
291     if (pkey == NULL) {
292         SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
293         return (0);
294     }
295     ret = ssl_set_pkey(ssl->cert, pkey);
296     return (ret);
297 }
298
299 #ifndef OPENSSL_NO_STDIO
300 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
301 {
302     int j, ret = 0;
303     BIO *in;
304     EVP_PKEY *pkey = NULL;
305
306     in = BIO_new(BIO_s_file_internal());
307     if (in == NULL) {
308         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
309         goto end;
310     }
311
312     if (BIO_read_filename(in, file) <= 0) {
313         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
314         goto end;
315     }
316     if (type == SSL_FILETYPE_PEM) {
317         j = ERR_R_PEM_LIB;
318         pkey = PEM_read_bio_PrivateKey(in, NULL,
319                                        ssl->ctx->default_passwd_callback,
320                                        ssl->
321                                        ctx->default_passwd_callback_userdata);
322     } else if (type == SSL_FILETYPE_ASN1) {
323         j = ERR_R_ASN1_LIB;
324         pkey = d2i_PrivateKey_bio(in, NULL);
325     } else {
326         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
327         goto end;
328     }
329     if (pkey == NULL) {
330         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
331         goto end;
332     }
333     ret = SSL_use_PrivateKey(ssl, pkey);
334     EVP_PKEY_free(pkey);
335  end:
336     BIO_free(in);
337     return (ret);
338 }
339 #endif
340
341 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
342                             long len)
343 {
344     int ret;
345     const unsigned char *p;
346     EVP_PKEY *pkey;
347
348     p = d;
349     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
350         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
351         return (0);
352     }
353
354     ret = SSL_use_PrivateKey(ssl, pkey);
355     EVP_PKEY_free(pkey);
356     return (ret);
357 }
358
359 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
360 {
361     int rv;
362     if (x == NULL) {
363         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
364         return (0);
365     }
366     rv = ssl_security_cert(NULL, ctx, x, 0, 1);
367     if (rv != 1) {
368         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
369         return 0;
370     }
371     return (ssl_set_cert(ctx->cert, x));
372 }
373
374 static int ssl_set_cert(CERT *c, X509 *x)
375 {
376     EVP_PKEY *pkey;
377     int i;
378
379     pkey = X509_get_pubkey(x);
380     if (pkey == NULL) {
381         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
382         return (0);
383     }
384
385     i = ssl_cert_type(x, pkey);
386     if (i < 0) {
387         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
388         EVP_PKEY_free(pkey);
389         return (0);
390     }
391
392     if (c->pkeys[i].privatekey != NULL) {
393         EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
394         ERR_clear_error();
395
396 #ifndef OPENSSL_NO_RSA
397         /*
398          * Don't check the public/private key, this is mostly for smart
399          * cards.
400          */
401         if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
402             (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
403              RSA_METHOD_FLAG_NO_CHECK)) ;
404         else
405 #endif                          /* OPENSSL_NO_RSA */
406         if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
407             /*
408              * don't fail for a cert/key mismatch, just free current private
409              * key (when switching to a different cert & key, first this
410              * function should be used, then ssl_set_pkey
411              */
412             EVP_PKEY_free(c->pkeys[i].privatekey);
413             c->pkeys[i].privatekey = NULL;
414             /* clear error queue */
415             ERR_clear_error();
416         }
417     }
418
419     EVP_PKEY_free(pkey);
420
421     if (c->pkeys[i].x509 != NULL)
422         X509_free(c->pkeys[i].x509);
423     CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
424     c->pkeys[i].x509 = x;
425     c->key = &(c->pkeys[i]);
426
427     c->valid = 0;
428     return (1);
429 }
430
431 #ifndef OPENSSL_NO_STDIO
432 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
433 {
434     int j;
435     BIO *in;
436     int ret = 0;
437     X509 *x = NULL;
438
439     in = BIO_new(BIO_s_file_internal());
440     if (in == NULL) {
441         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
442         goto end;
443     }
444
445     if (BIO_read_filename(in, file) <= 0) {
446         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
447         goto end;
448     }
449     if (type == SSL_FILETYPE_ASN1) {
450         j = ERR_R_ASN1_LIB;
451         x = d2i_X509_bio(in, NULL);
452     } else if (type == SSL_FILETYPE_PEM) {
453         j = ERR_R_PEM_LIB;
454         x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
455                               ctx->default_passwd_callback_userdata);
456     } else {
457         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
458         goto end;
459     }
460
461     if (x == NULL) {
462         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
463         goto end;
464     }
465
466     ret = SSL_CTX_use_certificate(ctx, x);
467  end:
468     if (x != NULL)
469         X509_free(x);
470     BIO_free(in);
471     return (ret);
472 }
473 #endif
474
475 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
476                                  const unsigned char *d)
477 {
478     X509 *x;
479     int ret;
480
481     x = d2i_X509(NULL, &d, (long)len);
482     if (x == NULL) {
483         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
484         return (0);
485     }
486
487     ret = SSL_CTX_use_certificate(ctx, x);
488     X509_free(x);
489     return (ret);
490 }
491
492 #ifndef OPENSSL_NO_RSA
493 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
494 {
495     int ret;
496     EVP_PKEY *pkey;
497
498     if (rsa == NULL) {
499         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
500         return (0);
501     }
502     if ((pkey = EVP_PKEY_new()) == NULL) {
503         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
504         return (0);
505     }
506
507     RSA_up_ref(rsa);
508     EVP_PKEY_assign_RSA(pkey, rsa);
509
510     ret = ssl_set_pkey(ctx->cert, pkey);
511     EVP_PKEY_free(pkey);
512     return (ret);
513 }
514
515 # ifndef OPENSSL_NO_STDIO
516 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
517 {
518     int j, ret = 0;
519     BIO *in;
520     RSA *rsa = NULL;
521
522     in = BIO_new(BIO_s_file_internal());
523     if (in == NULL) {
524         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
525         goto end;
526     }
527
528     if (BIO_read_filename(in, file) <= 0) {
529         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
530         goto end;
531     }
532     if (type == SSL_FILETYPE_ASN1) {
533         j = ERR_R_ASN1_LIB;
534         rsa = d2i_RSAPrivateKey_bio(in, NULL);
535     } else if (type == SSL_FILETYPE_PEM) {
536         j = ERR_R_PEM_LIB;
537         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
538                                          ctx->default_passwd_callback,
539                                          ctx->default_passwd_callback_userdata);
540     } else {
541         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
542         goto end;
543     }
544     if (rsa == NULL) {
545         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
546         goto end;
547     }
548     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
549     RSA_free(rsa);
550  end:
551     BIO_free(in);
552     return (ret);
553 }
554 # endif
555
556 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
557                                    long len)
558 {
559     int ret;
560     const unsigned char *p;
561     RSA *rsa;
562
563     p = d;
564     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
565         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
566         return (0);
567     }
568
569     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
570     RSA_free(rsa);
571     return (ret);
572 }
573 #endif                          /* !OPENSSL_NO_RSA */
574
575 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
576 {
577     if (pkey == NULL) {
578         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
579         return (0);
580     }
581     return (ssl_set_pkey(ctx->cert, pkey));
582 }
583
584 #ifndef OPENSSL_NO_STDIO
585 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
586 {
587     int j, ret = 0;
588     BIO *in;
589     EVP_PKEY *pkey = NULL;
590
591     in = BIO_new(BIO_s_file_internal());
592     if (in == NULL) {
593         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
594         goto end;
595     }
596
597     if (BIO_read_filename(in, file) <= 0) {
598         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
599         goto end;
600     }
601     if (type == SSL_FILETYPE_PEM) {
602         j = ERR_R_PEM_LIB;
603         pkey = PEM_read_bio_PrivateKey(in, NULL,
604                                        ctx->default_passwd_callback,
605                                        ctx->default_passwd_callback_userdata);
606     } else if (type == SSL_FILETYPE_ASN1) {
607         j = ERR_R_ASN1_LIB;
608         pkey = d2i_PrivateKey_bio(in, NULL);
609     } else {
610         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
611         goto end;
612     }
613     if (pkey == NULL) {
614         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
615         goto end;
616     }
617     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
618     EVP_PKEY_free(pkey);
619  end:
620     BIO_free(in);
621     return (ret);
622 }
623 #endif
624
625 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
626                                 const unsigned char *d, long len)
627 {
628     int ret;
629     const unsigned char *p;
630     EVP_PKEY *pkey;
631
632     p = d;
633     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
634         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
635         return (0);
636     }
637
638     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
639     EVP_PKEY_free(pkey);
640     return (ret);
641 }
642
643 #ifndef OPENSSL_NO_STDIO
644 /*
645  * Read a file that contains our certificate in "PEM" format, possibly
646  * followed by a sequence of CA certificates that should be sent to the peer
647  * in the Certificate message.
648  */
649 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
650 {
651     BIO *in;
652     int ret = 0;
653     X509 *x = NULL;
654
655     ERR_clear_error();          /* clear error stack for
656                                  * SSL_CTX_use_certificate() */
657
658     in = BIO_new(BIO_s_file_internal());
659     if (in == NULL) {
660         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
661         goto end;
662     }
663
664     if (BIO_read_filename(in, file) <= 0) {
665         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
666         goto end;
667     }
668
669     x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
670                               ctx->default_passwd_callback_userdata);
671     if (x == NULL) {
672         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
673         goto end;
674     }
675
676     ret = SSL_CTX_use_certificate(ctx, x);
677
678     if (ERR_peek_error() != 0)
679         ret = 0;                /* Key/certificate mismatch doesn't imply
680                                  * ret==0 ... */
681     if (ret) {
682         /*
683          * If we could set up our certificate, now proceed to the CA
684          * certificates.
685          */
686         X509 *ca;
687         int r;
688         unsigned long err;
689
690         if(!SSL_CTX_clear_chain_certs(ctx)) {
691             ret = 0;
692             goto end;
693         }
694
695         while ((ca = PEM_read_bio_X509(in, NULL,
696                                        ctx->default_passwd_callback,
697                                        ctx->default_passwd_callback_userdata))
698                != NULL) {
699             r = SSL_CTX_add0_chain_cert(ctx, ca);
700             if (!r) {
701                 X509_free(ca);
702                 ret = 0;
703                 goto end;
704             }
705             /*
706              * Note that we must not free r if it was successfully added to
707              * the chain (while we must free the main certificate, since its
708              * reference count is increased by SSL_CTX_use_certificate).
709              */
710         }
711         /* When the while loop ends, it's usually just EOF. */
712         err = ERR_peek_last_error();
713         if (ERR_GET_LIB(err) == ERR_LIB_PEM
714             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
715             ERR_clear_error();
716         else
717             ret = 0;            /* some real error */
718     }
719
720  end:
721     if (x != NULL)
722         X509_free(x);
723     BIO_free(in);
724     return (ret);
725 }
726 #endif
727
728 #ifndef OPENSSL_NO_TLSEXT
729 static int serverinfo_find_extension(const unsigned char *serverinfo,
730                                      size_t serverinfo_length,
731                                      unsigned int extension_type,
732                                      const unsigned char **extension_data,
733                                      size_t *extension_length)
734 {
735     *extension_data = NULL;
736     *extension_length = 0;
737     if (serverinfo == NULL || serverinfo_length == 0)
738         return 0;
739     for (;;) {
740         unsigned int type = 0;
741         size_t len = 0;
742
743         /* end of serverinfo */
744         if (serverinfo_length == 0)
745             return -1;          /* Extension not found */
746
747         /* read 2-byte type field */
748         if (serverinfo_length < 2)
749             return 0;           /* Error */
750         type = (serverinfo[0] << 8) + serverinfo[1];
751         serverinfo += 2;
752         serverinfo_length -= 2;
753
754         /* read 2-byte len field */
755         if (serverinfo_length < 2)
756             return 0;           /* Error */
757         len = (serverinfo[0] << 8) + serverinfo[1];
758         serverinfo += 2;
759         serverinfo_length -= 2;
760
761         if (len > serverinfo_length)
762             return 0;           /* Error */
763
764         if (type == extension_type) {
765             *extension_data = serverinfo;
766             *extension_length = len;
767             return 1;           /* Success */
768         }
769
770         serverinfo += len;
771         serverinfo_length -= len;
772     }
773     /* Unreachable */
774 }
775
776 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
777                                    const unsigned char *in,
778                                    size_t inlen, int *al, void *arg)
779 {
780
781     if (inlen != 0) {
782         *al = SSL_AD_DECODE_ERROR;
783         return 0;
784     }
785
786     return 1;
787 }
788
789 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
790                                  const unsigned char **out, size_t *outlen,
791                                  int *al, void *arg)
792 {
793     const unsigned char *serverinfo = NULL;
794     size_t serverinfo_length = 0;
795
796     /* Is there serverinfo data for the chosen server cert? */
797     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
798                                         &serverinfo_length)) != 0) {
799         /* Find the relevant extension from the serverinfo */
800         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
801                                                ext_type, out, outlen);
802         if (retval == 0)
803             return 0;           /* Error */
804         if (retval == -1)
805             return -1;          /* No extension found, don't send extension */
806         return 1;               /* Send extension */
807     }
808     return -1;                  /* No serverinfo data found, don't send
809                                  * extension */
810 }
811
812 /*
813  * With a NULL context, this function just checks that the serverinfo data
814  * parses correctly.  With a non-NULL context, it registers callbacks for
815  * the included extensions.
816  */
817 static int serverinfo_process_buffer(const unsigned char *serverinfo,
818                                      size_t serverinfo_length, SSL_CTX *ctx)
819 {
820     if (serverinfo == NULL || serverinfo_length == 0)
821         return 0;
822     for (;;) {
823         unsigned int ext_type = 0;
824         size_t len = 0;
825
826         /* end of serverinfo */
827         if (serverinfo_length == 0)
828             return 1;
829
830         /* read 2-byte type field */
831         if (serverinfo_length < 2)
832             return 0;
833         /* FIXME: check for types we understand explicitly? */
834
835         /* Register callbacks for extensions */
836         ext_type = (serverinfo[0] << 8) + serverinfo[1];
837         if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
838                                                   serverinfo_srv_add_cb,
839                                                   NULL, NULL,
840                                                   serverinfo_srv_parse_cb,
841                                                   NULL))
842             return 0;
843
844         serverinfo += 2;
845         serverinfo_length -= 2;
846
847         /* read 2-byte len field */
848         if (serverinfo_length < 2)
849             return 0;
850         len = (serverinfo[0] << 8) + serverinfo[1];
851         serverinfo += 2;
852         serverinfo_length -= 2;
853
854         if (len > serverinfo_length)
855             return 0;
856
857         serverinfo += len;
858         serverinfo_length -= len;
859     }
860 }
861
862 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
863                            size_t serverinfo_length)
864 {
865     unsigned char *new_serverinfo;
866
867     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
868         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
869         return 0;
870     }
871     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
872         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
873         return 0;
874     }
875     if (ctx->cert->key == NULL) {
876         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
877         return 0;
878     }
879     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
880                                      serverinfo_length);
881     if (new_serverinfo == NULL) {
882         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
883         return 0;
884     }
885     ctx->cert->key->serverinfo = new_serverinfo;
886     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
887     ctx->cert->key->serverinfo_length = serverinfo_length;
888
889     /*
890      * Now that the serverinfo is validated and stored, go ahead and
891      * register callbacks.
892      */
893     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
894         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
895         return 0;
896     }
897     return 1;
898 }
899
900 # ifndef OPENSSL_NO_STDIO
901 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
902 {
903     unsigned char *serverinfo = NULL;
904     size_t serverinfo_length = 0;
905     unsigned char *extension = 0;
906     long extension_length = 0;
907     char *name = NULL;
908     char *header = NULL;
909     char namePrefix[] = "SERVERINFO FOR ";
910     int ret = 0;
911     BIO *bin = NULL;
912     size_t num_extensions = 0;
913
914     if (ctx == NULL || file == NULL) {
915         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
916                ERR_R_PASSED_NULL_PARAMETER);
917         goto end;
918     }
919
920     bin = BIO_new(BIO_s_file_internal());
921     if (bin == NULL) {
922         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
923         goto end;
924     }
925     if (BIO_read_filename(bin, file) <= 0) {
926         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
927         goto end;
928     }
929
930     for (num_extensions = 0;; num_extensions++) {
931         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
932             == 0) {
933             /*
934              * There must be at least one extension in this file
935              */
936             if (num_extensions == 0) {
937                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
938                        SSL_R_NO_PEM_EXTENSIONS);
939                 goto end;
940             } else              /* End of file, we're done */
941                 break;
942         }
943         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
944         if (strlen(name) < strlen(namePrefix)) {
945             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
946                    SSL_R_PEM_NAME_TOO_SHORT);
947             goto end;
948         }
949         if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
950             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
951                    SSL_R_PEM_NAME_BAD_PREFIX);
952             goto end;
953         }
954         /*
955          * Check that the decoded PEM data is plausible (valid length field)
956          */
957         if (extension_length < 4
958             || (extension[2] << 8) + extension[3] != extension_length - 4) {
959             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
960             goto end;
961         }
962         /* Append the decoded extension to the serverinfo buffer */
963         serverinfo =
964             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
965         if (serverinfo == NULL) {
966             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
967             goto end;
968         }
969         memcpy(serverinfo + serverinfo_length, extension, extension_length);
970         serverinfo_length += extension_length;
971
972         OPENSSL_free(name);
973         name = NULL;
974         OPENSSL_free(header);
975         header = NULL;
976         OPENSSL_free(extension);
977         extension = NULL;
978     }
979
980     ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
981  end:
982     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
983     OPENSSL_free(name);
984     OPENSSL_free(header);
985     OPENSSL_free(extension);
986     OPENSSL_free(serverinfo);
987     BIO_free(bin);
988     return ret;
989 }
990 # endif                         /* OPENSSL_NO_STDIO */
991 #endif                          /* OPENSSL_NO_TLSEXT */