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