Client side version negotiation rewrite
[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 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, 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_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_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_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
668         goto end;
669     }
670
671     if (ctx)
672         ret = SSL_CTX_use_certificate(ctx, x);
673     else
674         ret = SSL_use_certificate(ssl, x);
675
676     if (ERR_peek_error() != 0)
677         ret = 0;                /* Key/certificate mismatch doesn't imply
678                                  * ret==0 ... */
679     if (ret) {
680         /*
681          * If we could set up our certificate, now proceed to the CA
682          * certificates.
683          */
684         X509 *ca;
685         int r;
686         unsigned long err;
687
688         if (ctx)
689             r = SSL_CTX_clear_chain_certs(ctx);
690         else
691             r = SSL_clear_chain_certs(ssl);
692
693         if (r == 0) {
694             ret = 0;
695             goto end;
696         }
697
698         while ((ca = PEM_read_bio_X509(in, NULL,
699                                        ctx->default_passwd_callback,
700                                        ctx->default_passwd_callback_userdata))
701                != NULL) {
702             if (ctx)
703                 r = SSL_CTX_add0_chain_cert(ctx, ca);
704             else
705                 r = SSL_add0_chain_cert(ssl, ca);
706             /*
707              * Note that we must not free ca if it was successfully added to
708              * the chain (while we must free the main certificate, since its
709              * reference count is increased by SSL_CTX_use_certificate).
710              */
711             if (!r) {
712                 X509_free(ca);
713                 ret = 0;
714                 goto end;
715             }
716         }
717         /* When the while loop ends, it's usually just EOF. */
718         err = ERR_peek_last_error();
719         if (ERR_GET_LIB(err) == ERR_LIB_PEM
720             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
721             ERR_clear_error();
722         else
723             ret = 0;            /* some real error */
724     }
725
726  end:
727     X509_free(x);
728     BIO_free(in);
729     return (ret);
730 }
731
732 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
733 {
734     return use_certificate_chain_file(ctx, NULL, file);
735 }
736
737 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
738 {
739     return use_certificate_chain_file(NULL, ssl, file);
740 }
741 #endif
742
743 #ifndef OPENSSL_NO_TLSEXT
744 static int serverinfo_find_extension(const unsigned char *serverinfo,
745                                      size_t serverinfo_length,
746                                      unsigned int extension_type,
747                                      const unsigned char **extension_data,
748                                      size_t *extension_length)
749 {
750     *extension_data = NULL;
751     *extension_length = 0;
752     if (serverinfo == NULL || serverinfo_length == 0)
753         return 0;
754     for (;;) {
755         unsigned int type = 0;
756         size_t len = 0;
757
758         /* end of serverinfo */
759         if (serverinfo_length == 0)
760             return -1;          /* Extension not found */
761
762         /* read 2-byte type field */
763         if (serverinfo_length < 2)
764             return 0;           /* Error */
765         type = (serverinfo[0] << 8) + serverinfo[1];
766         serverinfo += 2;
767         serverinfo_length -= 2;
768
769         /* read 2-byte len field */
770         if (serverinfo_length < 2)
771             return 0;           /* Error */
772         len = (serverinfo[0] << 8) + serverinfo[1];
773         serverinfo += 2;
774         serverinfo_length -= 2;
775
776         if (len > serverinfo_length)
777             return 0;           /* Error */
778
779         if (type == extension_type) {
780             *extension_data = serverinfo;
781             *extension_length = len;
782             return 1;           /* Success */
783         }
784
785         serverinfo += len;
786         serverinfo_length -= len;
787     }
788     /* Unreachable */
789 }
790
791 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
792                                    const unsigned char *in,
793                                    size_t inlen, int *al, void *arg)
794 {
795
796     if (inlen != 0) {
797         *al = SSL_AD_DECODE_ERROR;
798         return 0;
799     }
800
801     return 1;
802 }
803
804 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
805                                  const unsigned char **out, size_t *outlen,
806                                  int *al, void *arg)
807 {
808     const unsigned char *serverinfo = NULL;
809     size_t serverinfo_length = 0;
810
811     /* Is there serverinfo data for the chosen server cert? */
812     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
813                                         &serverinfo_length)) != 0) {
814         /* Find the relevant extension from the serverinfo */
815         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
816                                                ext_type, out, outlen);
817         if (retval == 0)
818             return 0;           /* Error */
819         if (retval == -1)
820             return -1;          /* No extension found, don't send extension */
821         return 1;               /* Send extension */
822     }
823     return -1;                  /* No serverinfo data found, don't send
824                                  * extension */
825 }
826
827 /*
828  * With a NULL context, this function just checks that the serverinfo data
829  * parses correctly.  With a non-NULL context, it registers callbacks for
830  * the included extensions.
831  */
832 static int serverinfo_process_buffer(const unsigned char *serverinfo,
833                                      size_t serverinfo_length, SSL_CTX *ctx)
834 {
835     if (serverinfo == NULL || serverinfo_length == 0)
836         return 0;
837     for (;;) {
838         unsigned int ext_type = 0;
839         size_t len = 0;
840
841         /* end of serverinfo */
842         if (serverinfo_length == 0)
843             return 1;
844
845         /* read 2-byte type field */
846         if (serverinfo_length < 2)
847             return 0;
848         /* FIXME: check for types we understand explicitly? */
849
850         /* Register callbacks for extensions */
851         ext_type = (serverinfo[0] << 8) + serverinfo[1];
852         if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
853                                                   serverinfo_srv_add_cb,
854                                                   NULL, NULL,
855                                                   serverinfo_srv_parse_cb,
856                                                   NULL))
857             return 0;
858
859         serverinfo += 2;
860         serverinfo_length -= 2;
861
862         /* read 2-byte len field */
863         if (serverinfo_length < 2)
864             return 0;
865         len = (serverinfo[0] << 8) + serverinfo[1];
866         serverinfo += 2;
867         serverinfo_length -= 2;
868
869         if (len > serverinfo_length)
870             return 0;
871
872         serverinfo += len;
873         serverinfo_length -= len;
874     }
875 }
876
877 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
878                            size_t serverinfo_length)
879 {
880     unsigned char *new_serverinfo;
881
882     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
883         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
884         return 0;
885     }
886     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
887         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
888         return 0;
889     }
890     if (ctx->cert->key == NULL) {
891         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
892         return 0;
893     }
894     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
895                                      serverinfo_length);
896     if (new_serverinfo == NULL) {
897         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
898         return 0;
899     }
900     ctx->cert->key->serverinfo = new_serverinfo;
901     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
902     ctx->cert->key->serverinfo_length = serverinfo_length;
903
904     /*
905      * Now that the serverinfo is validated and stored, go ahead and
906      * register callbacks.
907      */
908     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
909         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
910         return 0;
911     }
912     return 1;
913 }
914
915 # ifndef OPENSSL_NO_STDIO
916 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
917 {
918     unsigned char *serverinfo = NULL;
919     size_t serverinfo_length = 0;
920     unsigned char *extension = 0;
921     long extension_length = 0;
922     char *name = NULL;
923     char *header = NULL;
924     char namePrefix[] = "SERVERINFO FOR ";
925     int ret = 0;
926     BIO *bin = NULL;
927     size_t num_extensions = 0;
928
929     if (ctx == NULL || file == NULL) {
930         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
931                ERR_R_PASSED_NULL_PARAMETER);
932         goto end;
933     }
934
935     bin = BIO_new(BIO_s_file_internal());
936     if (bin == NULL) {
937         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
938         goto end;
939     }
940     if (BIO_read_filename(bin, file) <= 0) {
941         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
942         goto end;
943     }
944
945     for (num_extensions = 0;; num_extensions++) {
946         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
947             == 0) {
948             /*
949              * There must be at least one extension in this file
950              */
951             if (num_extensions == 0) {
952                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
953                        SSL_R_NO_PEM_EXTENSIONS);
954                 goto end;
955             } else              /* End of file, we're done */
956                 break;
957         }
958         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
959         if (strlen(name) < strlen(namePrefix)) {
960             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
961                    SSL_R_PEM_NAME_TOO_SHORT);
962             goto end;
963         }
964         if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
965             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
966                    SSL_R_PEM_NAME_BAD_PREFIX);
967             goto end;
968         }
969         /*
970          * Check that the decoded PEM data is plausible (valid length field)
971          */
972         if (extension_length < 4
973             || (extension[2] << 8) + extension[3] != extension_length - 4) {
974             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
975             goto end;
976         }
977         /* Append the decoded extension to the serverinfo buffer */
978         serverinfo =
979             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
980         if (serverinfo == NULL) {
981             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
982             goto end;
983         }
984         memcpy(serverinfo + serverinfo_length, extension, extension_length);
985         serverinfo_length += extension_length;
986
987         OPENSSL_free(name);
988         name = NULL;
989         OPENSSL_free(header);
990         header = NULL;
991         OPENSSL_free(extension);
992         extension = NULL;
993     }
994
995     ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
996  end:
997     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
998     OPENSSL_free(name);
999     OPENSSL_free(header);
1000     OPENSSL_free(extension);
1001     OPENSSL_free(serverinfo);
1002     BIO_free(bin);
1003     return ret;
1004 }
1005 # endif                         /* OPENSSL_NO_STDIO */
1006 #endif                          /* OPENSSL_NO_TLSEXT */