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