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