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