Remove SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER and SSL_OP_TLS_D5_BUG support.
[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());
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     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
161         RSA_free(rsa);
162         return 0;
163     }
164
165     ret = ssl_set_pkey(ssl->cert, pkey);
166     EVP_PKEY_free(pkey);
167     return (ret);
168 }
169 #endif
170
171 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
172 {
173     int i;
174     i = ssl_cert_type(NULL, pkey);
175     if (i < 0) {
176         SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
177         return (0);
178     }
179
180     if (c->pkeys[i].x509 != NULL) {
181         EVP_PKEY *pktmp;
182         pktmp = X509_get_pubkey(c->pkeys[i].x509);
183         if (pktmp == NULL) {
184             SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
185             EVP_PKEY_free(pktmp);
186             return 0;
187         }
188         /*
189          * The return code from EVP_PKEY_copy_parameters is deliberately
190          * ignored. Some EVP_PKEY types cannot do this.
191          */
192         EVP_PKEY_copy_parameters(pktmp, pkey);
193         EVP_PKEY_free(pktmp);
194         ERR_clear_error();
195
196 #ifndef OPENSSL_NO_RSA
197         /*
198          * Don't check the public/private key, this is mostly for smart
199          * cards.
200          */
201         if ((pkey->type == EVP_PKEY_RSA) &&
202             (RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) ;
203         else
204 #endif
205         if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
206             X509_free(c->pkeys[i].x509);
207             c->pkeys[i].x509 = NULL;
208             return 0;
209         }
210     }
211
212     EVP_PKEY_free(c->pkeys[i].privatekey);
213     CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
214     c->pkeys[i].privatekey = pkey;
215     c->key = &(c->pkeys[i]);
216     return (1);
217 }
218
219 #ifndef OPENSSL_NO_RSA
220 # ifndef OPENSSL_NO_STDIO
221 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
222 {
223     int j, ret = 0;
224     BIO *in;
225     RSA *rsa = NULL;
226
227     in = BIO_new(BIO_s_file());
228     if (in == NULL) {
229         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
230         goto end;
231     }
232
233     if (BIO_read_filename(in, file) <= 0) {
234         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
235         goto end;
236     }
237     if (type == SSL_FILETYPE_ASN1) {
238         j = ERR_R_ASN1_LIB;
239         rsa = d2i_RSAPrivateKey_bio(in, NULL);
240     } else if (type == SSL_FILETYPE_PEM) {
241         j = ERR_R_PEM_LIB;
242         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
243                                          ssl->ctx->default_passwd_callback,
244                                          ssl->
245                                          ctx->default_passwd_callback_userdata);
246     } else {
247         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
248         goto end;
249     }
250     if (rsa == NULL) {
251         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
252         goto end;
253     }
254     ret = SSL_use_RSAPrivateKey(ssl, rsa);
255     RSA_free(rsa);
256  end:
257     BIO_free(in);
258     return (ret);
259 }
260 # endif
261
262 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
263 {
264     int ret;
265     const unsigned char *p;
266     RSA *rsa;
267
268     p = d;
269     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
270         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
271         return (0);
272     }
273
274     ret = SSL_use_RSAPrivateKey(ssl, rsa);
275     RSA_free(rsa);
276     return (ret);
277 }
278 #endif                          /* !OPENSSL_NO_RSA */
279
280 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
281 {
282     int ret;
283
284     if (pkey == NULL) {
285         SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
286         return (0);
287     }
288     ret = ssl_set_pkey(ssl->cert, pkey);
289     return (ret);
290 }
291
292 #ifndef OPENSSL_NO_STDIO
293 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
294 {
295     int j, ret = 0;
296     BIO *in;
297     EVP_PKEY *pkey = NULL;
298
299     in = BIO_new(BIO_s_file());
300     if (in == NULL) {
301         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
302         goto end;
303     }
304
305     if (BIO_read_filename(in, file) <= 0) {
306         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
307         goto end;
308     }
309     if (type == SSL_FILETYPE_PEM) {
310         j = ERR_R_PEM_LIB;
311         pkey = PEM_read_bio_PrivateKey(in, NULL,
312                                        ssl->ctx->default_passwd_callback,
313                                        ssl->
314                                        ctx->default_passwd_callback_userdata);
315     } else if (type == SSL_FILETYPE_ASN1) {
316         j = ERR_R_ASN1_LIB;
317         pkey = d2i_PrivateKey_bio(in, NULL);
318     } else {
319         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
320         goto end;
321     }
322     if (pkey == NULL) {
323         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
324         goto end;
325     }
326     ret = SSL_use_PrivateKey(ssl, pkey);
327     EVP_PKEY_free(pkey);
328  end:
329     BIO_free(in);
330     return (ret);
331 }
332 #endif
333
334 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
335                             long len)
336 {
337     int ret;
338     const unsigned char *p;
339     EVP_PKEY *pkey;
340
341     p = d;
342     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
343         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
344         return (0);
345     }
346
347     ret = SSL_use_PrivateKey(ssl, pkey);
348     EVP_PKEY_free(pkey);
349     return (ret);
350 }
351
352 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
353 {
354     int rv;
355     if (x == NULL) {
356         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
357         return (0);
358     }
359     rv = ssl_security_cert(NULL, ctx, x, 0, 1);
360     if (rv != 1) {
361         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
362         return 0;
363     }
364     return (ssl_set_cert(ctx->cert, x));
365 }
366
367 static int ssl_set_cert(CERT *c, X509 *x)
368 {
369     EVP_PKEY *pkey;
370     int i;
371
372     pkey = X509_get_pubkey(x);
373     if (pkey == NULL) {
374         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
375         return (0);
376     }
377
378     i = ssl_cert_type(x, pkey);
379     if (i < 0) {
380         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
381         EVP_PKEY_free(pkey);
382         return (0);
383     }
384
385     if (c->pkeys[i].privatekey != NULL) {
386         /*
387          * The return code from EVP_PKEY_copy_parameters is deliberately
388          * ignored. Some EVP_PKEY types cannot do this.
389          */
390         EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
391         ERR_clear_error();
392
393 #ifndef OPENSSL_NO_RSA
394         /*
395          * Don't check the public/private key, this is mostly for smart
396          * cards.
397          */
398         if ((c->pkeys[i].privatekey->type == EVP_PKEY_RSA) &&
399             (RSA_flags(c->pkeys[i].privatekey->pkey.rsa) &
400              RSA_METHOD_FLAG_NO_CHECK)) ;
401         else
402 #endif                          /* OPENSSL_NO_RSA */
403         if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
404             /*
405              * don't fail for a cert/key mismatch, just free current private
406              * key (when switching to a different cert & key, first this
407              * function should be used, then ssl_set_pkey
408              */
409             EVP_PKEY_free(c->pkeys[i].privatekey);
410             c->pkeys[i].privatekey = NULL;
411             /* clear error queue */
412             ERR_clear_error();
413         }
414     }
415
416     EVP_PKEY_free(pkey);
417
418     X509_free(c->pkeys[i].x509);
419     X509_up_ref(x);
420     c->pkeys[i].x509 = x;
421     c->key = &(c->pkeys[i]);
422
423     return (1);
424 }
425
426 #ifndef OPENSSL_NO_STDIO
427 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
428 {
429     int j;
430     BIO *in;
431     int ret = 0;
432     X509 *x = NULL;
433
434     in = BIO_new(BIO_s_file());
435     if (in == NULL) {
436         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
437         goto end;
438     }
439
440     if (BIO_read_filename(in, file) <= 0) {
441         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
442         goto end;
443     }
444     if (type == SSL_FILETYPE_ASN1) {
445         j = ERR_R_ASN1_LIB;
446         x = d2i_X509_bio(in, NULL);
447     } else if (type == SSL_FILETYPE_PEM) {
448         j = ERR_R_PEM_LIB;
449         x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
450                               ctx->default_passwd_callback_userdata);
451     } else {
452         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
453         goto end;
454     }
455
456     if (x == NULL) {
457         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
458         goto end;
459     }
460
461     ret = SSL_CTX_use_certificate(ctx, x);
462  end:
463     X509_free(x);
464     BIO_free(in);
465     return (ret);
466 }
467 #endif
468
469 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
470                                  const unsigned char *d)
471 {
472     X509 *x;
473     int ret;
474
475     x = d2i_X509(NULL, &d, (long)len);
476     if (x == NULL) {
477         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
478         return (0);
479     }
480
481     ret = SSL_CTX_use_certificate(ctx, x);
482     X509_free(x);
483     return (ret);
484 }
485
486 #ifndef OPENSSL_NO_RSA
487 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
488 {
489     int ret;
490     EVP_PKEY *pkey;
491
492     if (rsa == NULL) {
493         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
494         return (0);
495     }
496     if ((pkey = EVP_PKEY_new()) == NULL) {
497         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
498         return (0);
499     }
500
501     RSA_up_ref(rsa);
502     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
503         RSA_free(rsa);
504         return 0;
505     }
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());
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());
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 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
647 {
648     BIO *in;
649     int ret = 0;
650     X509 *x = NULL;
651     pem_password_cb *passwd_callback;
652     void *passwd_callback_userdata;
653
654     ERR_clear_error();          /* clear error stack for
655                                  * SSL_CTX_use_certificate() */
656
657     if (ctx != NULL) {
658         passwd_callback = ctx->default_passwd_callback;
659         passwd_callback_userdata = ctx->default_passwd_callback_userdata;
660     } else {
661         passwd_callback = ssl->default_passwd_callback;
662         passwd_callback_userdata = ssl->default_passwd_callback_userdata;
663     }
664
665     in = BIO_new(BIO_s_file());
666     if (in == NULL) {
667         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
668         goto end;
669     }
670
671     if (BIO_read_filename(in, file) <= 0) {
672         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
673         goto end;
674     }
675
676     x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
677                               passwd_callback_userdata);
678     if (x == NULL) {
679         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
680         goto end;
681     }
682
683     if (ctx)
684         ret = SSL_CTX_use_certificate(ctx, x);
685     else
686         ret = SSL_use_certificate(ssl, x);
687
688     if (ERR_peek_error() != 0)
689         ret = 0;                /* Key/certificate mismatch doesn't imply
690                                  * ret==0 ... */
691     if (ret) {
692         /*
693          * If we could set up our certificate, now proceed to the CA
694          * certificates.
695          */
696         X509 *ca;
697         int r;
698         unsigned long err;
699
700         if (ctx)
701             r = SSL_CTX_clear_chain_certs(ctx);
702         else
703             r = SSL_clear_chain_certs(ssl);
704
705         if (r == 0) {
706             ret = 0;
707             goto end;
708         }
709
710         while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
711                                        passwd_callback_userdata))
712                 != NULL) {
713             if (ctx)
714                 r = SSL_CTX_add0_chain_cert(ctx, ca);
715             else
716                 r = SSL_add0_chain_cert(ssl, ca);
717             /*
718              * Note that we must not free ca if it was successfully added to
719              * the chain (while we must free the main certificate, since its
720              * reference count is increased by SSL_CTX_use_certificate).
721              */
722             if (!r) {
723                 X509_free(ca);
724                 ret = 0;
725                 goto end;
726             }
727         }
728         /* When the while loop ends, it's usually just EOF. */
729         err = ERR_peek_last_error();
730         if (ERR_GET_LIB(err) == ERR_LIB_PEM
731             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
732             ERR_clear_error();
733         else
734             ret = 0;            /* some real error */
735     }
736
737  end:
738     X509_free(x);
739     BIO_free(in);
740     return (ret);
741 }
742
743 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
744 {
745     return use_certificate_chain_file(ctx, NULL, file);
746 }
747
748 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
749 {
750     return use_certificate_chain_file(NULL, ssl, file);
751 }
752 #endif
753
754 static int serverinfo_find_extension(const unsigned char *serverinfo,
755                                      size_t serverinfo_length,
756                                      unsigned int extension_type,
757                                      const unsigned char **extension_data,
758                                      size_t *extension_length)
759 {
760     *extension_data = NULL;
761     *extension_length = 0;
762     if (serverinfo == NULL || serverinfo_length == 0)
763         return -1;
764     for (;;) {
765         unsigned int type = 0;
766         size_t len = 0;
767
768         /* end of serverinfo */
769         if (serverinfo_length == 0)
770             return 0;           /* Extension not found */
771
772         /* read 2-byte type field */
773         if (serverinfo_length < 2)
774             return -1;          /* Error */
775         type = (serverinfo[0] << 8) + serverinfo[1];
776         serverinfo += 2;
777         serverinfo_length -= 2;
778
779         /* read 2-byte len field */
780         if (serverinfo_length < 2)
781             return -1;          /* Error */
782         len = (serverinfo[0] << 8) + serverinfo[1];
783         serverinfo += 2;
784         serverinfo_length -= 2;
785
786         if (len > serverinfo_length)
787             return -1;          /* Error */
788
789         if (type == extension_type) {
790             *extension_data = serverinfo;
791             *extension_length = len;
792             return 1;           /* Success */
793         }
794
795         serverinfo += len;
796         serverinfo_length -= len;
797     }
798     /* Unreachable */
799 }
800
801 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
802                                    const unsigned char *in,
803                                    size_t inlen, int *al, void *arg)
804 {
805
806     if (inlen != 0) {
807         *al = SSL_AD_DECODE_ERROR;
808         return 0;
809     }
810
811     return 1;
812 }
813
814 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
815                                  const unsigned char **out, size_t *outlen,
816                                  int *al, void *arg)
817 {
818     const unsigned char *serverinfo = NULL;
819     size_t serverinfo_length = 0;
820
821     /* Is there serverinfo data for the chosen server cert? */
822     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
823                                         &serverinfo_length)) != 0) {
824         /* Find the relevant extension from the serverinfo */
825         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
826                                                ext_type, out, outlen);
827         if (retval == -1) {
828             *al = SSL_AD_DECODE_ERROR;
829             return -1;          /* Error */
830         }
831         if (retval == 0)
832             return 0;           /* No extension found, don't send extension */
833         return 1;               /* Send extension */
834     }
835     return -1;                  /* No serverinfo data found, don't send
836                                  * extension */
837 }
838
839 /*
840  * With a NULL context, this function just checks that the serverinfo data
841  * parses correctly.  With a non-NULL context, it registers callbacks for
842  * the included extensions.
843  */
844 static int serverinfo_process_buffer(const unsigned char *serverinfo,
845                                      size_t serverinfo_length, SSL_CTX *ctx)
846 {
847     if (serverinfo == NULL || serverinfo_length == 0)
848         return 0;
849     for (;;) {
850         unsigned int ext_type = 0;
851         size_t len = 0;
852
853         /* end of serverinfo */
854         if (serverinfo_length == 0)
855             return 1;
856
857         /* read 2-byte type field */
858         if (serverinfo_length < 2)
859             return 0;
860         /* FIXME: check for types we understand explicitly? */
861
862         /* Register callbacks for extensions */
863         ext_type = (serverinfo[0] << 8) + serverinfo[1];
864         if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
865                                                   serverinfo_srv_add_cb,
866                                                   NULL, NULL,
867                                                   serverinfo_srv_parse_cb,
868                                                   NULL))
869             return 0;
870
871         serverinfo += 2;
872         serverinfo_length -= 2;
873
874         /* read 2-byte len field */
875         if (serverinfo_length < 2)
876             return 0;
877         len = (serverinfo[0] << 8) + serverinfo[1];
878         serverinfo += 2;
879         serverinfo_length -= 2;
880
881         if (len > serverinfo_length)
882             return 0;
883
884         serverinfo += len;
885         serverinfo_length -= len;
886     }
887 }
888
889 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
890                            size_t serverinfo_length)
891 {
892     unsigned char *new_serverinfo;
893
894     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
895         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
896         return 0;
897     }
898     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
899         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
900         return 0;
901     }
902     if (ctx->cert->key == NULL) {
903         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
904         return 0;
905     }
906     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
907                                      serverinfo_length);
908     if (new_serverinfo == NULL) {
909         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
910         return 0;
911     }
912     ctx->cert->key->serverinfo = new_serverinfo;
913     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
914     ctx->cert->key->serverinfo_length = serverinfo_length;
915
916     /*
917      * Now that the serverinfo is validated and stored, go ahead and
918      * register callbacks.
919      */
920     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
921         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
922         return 0;
923     }
924     return 1;
925 }
926
927 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
928 {
929     unsigned char *serverinfo = NULL;
930     size_t serverinfo_length = 0;
931     unsigned char *extension = 0;
932     long extension_length = 0;
933     char *name = NULL;
934     char *header = NULL;
935     char namePrefix[] = "SERVERINFO FOR ";
936     int ret = 0;
937     BIO *bin = NULL;
938     size_t num_extensions = 0;
939
940     if (ctx == NULL || file == NULL) {
941         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
942                ERR_R_PASSED_NULL_PARAMETER);
943         goto end;
944     }
945
946     bin = BIO_new(BIO_s_file());
947     if (bin == NULL) {
948         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
949         goto end;
950     }
951     if (BIO_read_filename(bin, file) <= 0) {
952         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
953         goto end;
954     }
955
956     for (num_extensions = 0;; num_extensions++) {
957         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
958             == 0) {
959             /*
960              * There must be at least one extension in this file
961              */
962             if (num_extensions == 0) {
963                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
964                        SSL_R_NO_PEM_EXTENSIONS);
965                 goto end;
966             } else              /* End of file, we're done */
967                 break;
968         }
969         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
970         if (strlen(name) < strlen(namePrefix)) {
971             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
972                    SSL_R_PEM_NAME_TOO_SHORT);
973             goto end;
974         }
975         if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
976             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
977                    SSL_R_PEM_NAME_BAD_PREFIX);
978             goto end;
979         }
980         /*
981          * Check that the decoded PEM data is plausible (valid length field)
982          */
983         if (extension_length < 4
984             || (extension[2] << 8) + extension[3] != extension_length - 4) {
985             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
986             goto end;
987         }
988         /* Append the decoded extension to the serverinfo buffer */
989         serverinfo =
990             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
991         if (serverinfo == NULL) {
992             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
993             goto end;
994         }
995         memcpy(serverinfo + serverinfo_length, extension, extension_length);
996         serverinfo_length += extension_length;
997
998         OPENSSL_free(name);
999         name = NULL;
1000         OPENSSL_free(header);
1001         header = NULL;
1002         OPENSSL_free(extension);
1003         extension = NULL;
1004     }
1005
1006     ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1007  end:
1008     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1009     OPENSSL_free(name);
1010     OPENSSL_free(header);
1011     OPENSSL_free(extension);
1012     OPENSSL_free(serverinfo);
1013     BIO_free(bin);
1014     return ret;
1015 }