make EVP_PKEY opaque
[openssl.git] / ssl / ssl_rsa.c
1 /* ssl/ssl_rsa.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "ssl_locl.h"
61 #include <openssl/bio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
66
67 static int ssl_set_cert(CERT *c, X509 *x509);
68 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
69 int SSL_use_certificate(SSL *ssl, X509 *x)
70 {
71     int rv;
72     if (x == NULL) {
73         SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
74         return (0);
75     }
76     rv = ssl_security_cert(ssl, NULL, x, 0, 1);
77     if (rv != 1) {
78         SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
79         return 0;
80     }
81
82     return (ssl_set_cert(ssl->cert, x));
83 }
84
85 #ifndef OPENSSL_NO_STDIO
86 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
87 {
88     int j;
89     BIO *in;
90     int ret = 0;
91     X509 *x = NULL;
92
93     in = BIO_new(BIO_s_file());
94     if (in == NULL) {
95         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
96         goto end;
97     }
98
99     if (BIO_read_filename(in, file) <= 0) {
100         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
101         goto end;
102     }
103     if (type == SSL_FILETYPE_ASN1) {
104         j = ERR_R_ASN1_LIB;
105         x = d2i_X509_bio(in, NULL);
106     } else if (type == SSL_FILETYPE_PEM) {
107         j = ERR_R_PEM_LIB;
108         x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
109                               ssl->ctx->default_passwd_callback_userdata);
110     } else {
111         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
112         goto end;
113     }
114
115     if (x == NULL) {
116         SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
117         goto end;
118     }
119
120     ret = SSL_use_certificate(ssl, x);
121  end:
122     X509_free(x);
123     BIO_free(in);
124     return (ret);
125 }
126 #endif
127
128 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
129 {
130     X509 *x;
131     int ret;
132
133     x = d2i_X509(NULL, &d, (long)len);
134     if (x == NULL) {
135         SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
136         return (0);
137     }
138
139     ret = SSL_use_certificate(ssl, x);
140     X509_free(x);
141     return (ret);
142 }
143
144 #ifndef OPENSSL_NO_RSA
145 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
146 {
147     EVP_PKEY *pkey;
148     int ret;
149
150     if (rsa == NULL) {
151         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
152         return (0);
153     }
154     if ((pkey = EVP_PKEY_new()) == NULL) {
155         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
156         return (0);
157     }
158
159     RSA_up_ref(rsa);
160     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
161         RSA_free(rsa);
162         return 0;
163     }
164
165     ret = ssl_set_pkey(ssl->cert, pkey);
166     EVP_PKEY_free(pkey);
167     return (ret);
168 }
169 #endif
170
171 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
172 {
173     int i;
174     i = ssl_cert_type(NULL, pkey);
175     if (i < 0) {
176         SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
177         return (0);
178     }
179
180     if (c->pkeys[i].x509 != NULL) {
181         EVP_PKEY *pktmp;
182         pktmp = X509_get0_pubkey(c->pkeys[i].x509);
183         if (pktmp == NULL) {
184             SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
185             return 0;
186         }
187         /*
188          * The return code from EVP_PKEY_copy_parameters is deliberately
189          * ignored. Some EVP_PKEY types cannot do this.
190          */
191         EVP_PKEY_copy_parameters(pktmp, pkey);
192         ERR_clear_error();
193
194 #ifndef OPENSSL_NO_RSA
195         /*
196          * Don't check the public/private key, this is mostly for smart
197          * cards.
198          */
199         if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
200             && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK);
201         else
202 #endif
203         if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
204             X509_free(c->pkeys[i].x509);
205             c->pkeys[i].x509 = NULL;
206             return 0;
207         }
208     }
209
210     EVP_PKEY_free(c->pkeys[i].privatekey);
211     EVP_PKEY_up_ref(pkey);
212     c->pkeys[i].privatekey = pkey;
213     c->key = &(c->pkeys[i]);
214     return (1);
215 }
216
217 #ifndef OPENSSL_NO_RSA
218 # ifndef OPENSSL_NO_STDIO
219 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
220 {
221     int j, ret = 0;
222     BIO *in;
223     RSA *rsa = NULL;
224
225     in = BIO_new(BIO_s_file());
226     if (in == NULL) {
227         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
228         goto end;
229     }
230
231     if (BIO_read_filename(in, file) <= 0) {
232         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
233         goto end;
234     }
235     if (type == SSL_FILETYPE_ASN1) {
236         j = ERR_R_ASN1_LIB;
237         rsa = d2i_RSAPrivateKey_bio(in, NULL);
238     } else if (type == SSL_FILETYPE_PEM) {
239         j = ERR_R_PEM_LIB;
240         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
241                                          ssl->ctx->default_passwd_callback,
242                                          ssl->
243                                          ctx->default_passwd_callback_userdata);
244     } else {
245         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
246         goto end;
247     }
248     if (rsa == NULL) {
249         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
250         goto end;
251     }
252     ret = SSL_use_RSAPrivateKey(ssl, rsa);
253     RSA_free(rsa);
254  end:
255     BIO_free(in);
256     return (ret);
257 }
258 # endif
259
260 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
261 {
262     int ret;
263     const unsigned char *p;
264     RSA *rsa;
265
266     p = d;
267     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
268         SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
269         return (0);
270     }
271
272     ret = SSL_use_RSAPrivateKey(ssl, rsa);
273     RSA_free(rsa);
274     return (ret);
275 }
276 #endif                          /* !OPENSSL_NO_RSA */
277
278 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
279 {
280     int ret;
281
282     if (pkey == NULL) {
283         SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
284         return (0);
285     }
286     ret = ssl_set_pkey(ssl->cert, pkey);
287     return (ret);
288 }
289
290 #ifndef OPENSSL_NO_STDIO
291 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
292 {
293     int j, ret = 0;
294     BIO *in;
295     EVP_PKEY *pkey = NULL;
296
297     in = BIO_new(BIO_s_file());
298     if (in == NULL) {
299         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
300         goto end;
301     }
302
303     if (BIO_read_filename(in, file) <= 0) {
304         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
305         goto end;
306     }
307     if (type == SSL_FILETYPE_PEM) {
308         j = ERR_R_PEM_LIB;
309         pkey = PEM_read_bio_PrivateKey(in, NULL,
310                                        ssl->ctx->default_passwd_callback,
311                                        ssl->
312                                        ctx->default_passwd_callback_userdata);
313     } else if (type == SSL_FILETYPE_ASN1) {
314         j = ERR_R_ASN1_LIB;
315         pkey = d2i_PrivateKey_bio(in, NULL);
316     } else {
317         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
318         goto end;
319     }
320     if (pkey == NULL) {
321         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
322         goto end;
323     }
324     ret = SSL_use_PrivateKey(ssl, pkey);
325     EVP_PKEY_free(pkey);
326  end:
327     BIO_free(in);
328     return (ret);
329 }
330 #endif
331
332 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
333                             long len)
334 {
335     int ret;
336     const unsigned char *p;
337     EVP_PKEY *pkey;
338
339     p = d;
340     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
341         SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
342         return (0);
343     }
344
345     ret = SSL_use_PrivateKey(ssl, pkey);
346     EVP_PKEY_free(pkey);
347     return (ret);
348 }
349
350 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
351 {
352     int rv;
353     if (x == NULL) {
354         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
355         return (0);
356     }
357     rv = ssl_security_cert(NULL, ctx, x, 0, 1);
358     if (rv != 1) {
359         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
360         return 0;
361     }
362     return (ssl_set_cert(ctx->cert, x));
363 }
364
365 static int ssl_set_cert(CERT *c, X509 *x)
366 {
367     EVP_PKEY *pkey;
368     int i;
369
370     pkey = X509_get0_pubkey(x);
371     if (pkey == NULL) {
372         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
373         return (0);
374     }
375
376     i = ssl_cert_type(x, pkey);
377     if (i < 0) {
378         SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
379         return 0;
380     }
381
382     if (c->pkeys[i].privatekey != NULL) {
383         /*
384          * The return code from EVP_PKEY_copy_parameters is deliberately
385          * ignored. Some EVP_PKEY types cannot do this.
386          */
387         EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
388         ERR_clear_error();
389
390 #ifndef OPENSSL_NO_RSA
391         /*
392          * Don't check the public/private key, this is mostly for smart
393          * cards.
394          */
395         if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
396             && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
397                RSA_METHOD_FLAG_NO_CHECK) ;
398         else
399 #endif                          /* OPENSSL_NO_RSA */
400         if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
401             /*
402              * don't fail for a cert/key mismatch, just free current private
403              * key (when switching to a different cert & key, first this
404              * function should be used, then ssl_set_pkey
405              */
406             EVP_PKEY_free(c->pkeys[i].privatekey);
407             c->pkeys[i].privatekey = NULL;
408             /* clear error queue */
409             ERR_clear_error();
410         }
411     }
412
413     X509_free(c->pkeys[i].x509);
414     X509_up_ref(x);
415     c->pkeys[i].x509 = x;
416     c->key = &(c->pkeys[i]);
417
418     return 1;
419 }
420
421 #ifndef OPENSSL_NO_STDIO
422 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
423 {
424     int j;
425     BIO *in;
426     int ret = 0;
427     X509 *x = NULL;
428
429     in = BIO_new(BIO_s_file());
430     if (in == NULL) {
431         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
432         goto end;
433     }
434
435     if (BIO_read_filename(in, file) <= 0) {
436         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
437         goto end;
438     }
439     if (type == SSL_FILETYPE_ASN1) {
440         j = ERR_R_ASN1_LIB;
441         x = d2i_X509_bio(in, NULL);
442     } else if (type == SSL_FILETYPE_PEM) {
443         j = ERR_R_PEM_LIB;
444         x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
445                               ctx->default_passwd_callback_userdata);
446     } else {
447         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
448         goto end;
449     }
450
451     if (x == NULL) {
452         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
453         goto end;
454     }
455
456     ret = SSL_CTX_use_certificate(ctx, x);
457  end:
458     X509_free(x);
459     BIO_free(in);
460     return (ret);
461 }
462 #endif
463
464 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
465                                  const unsigned char *d)
466 {
467     X509 *x;
468     int ret;
469
470     x = d2i_X509(NULL, &d, (long)len);
471     if (x == NULL) {
472         SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
473         return (0);
474     }
475
476     ret = SSL_CTX_use_certificate(ctx, x);
477     X509_free(x);
478     return (ret);
479 }
480
481 #ifndef OPENSSL_NO_RSA
482 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
483 {
484     int ret;
485     EVP_PKEY *pkey;
486
487     if (rsa == NULL) {
488         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
489         return (0);
490     }
491     if ((pkey = EVP_PKEY_new()) == NULL) {
492         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
493         return (0);
494     }
495
496     RSA_up_ref(rsa);
497     if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
498         RSA_free(rsa);
499         return 0;
500     }
501
502     ret = ssl_set_pkey(ctx->cert, pkey);
503     EVP_PKEY_free(pkey);
504     return (ret);
505 }
506
507 # ifndef OPENSSL_NO_STDIO
508 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
509 {
510     int j, ret = 0;
511     BIO *in;
512     RSA *rsa = NULL;
513
514     in = BIO_new(BIO_s_file());
515     if (in == NULL) {
516         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
517         goto end;
518     }
519
520     if (BIO_read_filename(in, file) <= 0) {
521         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
522         goto end;
523     }
524     if (type == SSL_FILETYPE_ASN1) {
525         j = ERR_R_ASN1_LIB;
526         rsa = d2i_RSAPrivateKey_bio(in, NULL);
527     } else if (type == SSL_FILETYPE_PEM) {
528         j = ERR_R_PEM_LIB;
529         rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
530                                          ctx->default_passwd_callback,
531                                          ctx->default_passwd_callback_userdata);
532     } else {
533         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
534         goto end;
535     }
536     if (rsa == NULL) {
537         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
538         goto end;
539     }
540     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
541     RSA_free(rsa);
542  end:
543     BIO_free(in);
544     return (ret);
545 }
546 # endif
547
548 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
549                                    long len)
550 {
551     int ret;
552     const unsigned char *p;
553     RSA *rsa;
554
555     p = d;
556     if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
557         SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
558         return (0);
559     }
560
561     ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
562     RSA_free(rsa);
563     return (ret);
564 }
565 #endif                          /* !OPENSSL_NO_RSA */
566
567 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
568 {
569     if (pkey == NULL) {
570         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
571         return (0);
572     }
573     return (ssl_set_pkey(ctx->cert, pkey));
574 }
575
576 #ifndef OPENSSL_NO_STDIO
577 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
578 {
579     int j, ret = 0;
580     BIO *in;
581     EVP_PKEY *pkey = NULL;
582
583     in = BIO_new(BIO_s_file());
584     if (in == NULL) {
585         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
586         goto end;
587     }
588
589     if (BIO_read_filename(in, file) <= 0) {
590         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
591         goto end;
592     }
593     if (type == SSL_FILETYPE_PEM) {
594         j = ERR_R_PEM_LIB;
595         pkey = PEM_read_bio_PrivateKey(in, NULL,
596                                        ctx->default_passwd_callback,
597                                        ctx->default_passwd_callback_userdata);
598     } else if (type == SSL_FILETYPE_ASN1) {
599         j = ERR_R_ASN1_LIB;
600         pkey = d2i_PrivateKey_bio(in, NULL);
601     } else {
602         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
603         goto end;
604     }
605     if (pkey == NULL) {
606         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
607         goto end;
608     }
609     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
610     EVP_PKEY_free(pkey);
611  end:
612     BIO_free(in);
613     return (ret);
614 }
615 #endif
616
617 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
618                                 const unsigned char *d, long len)
619 {
620     int ret;
621     const unsigned char *p;
622     EVP_PKEY *pkey;
623
624     p = d;
625     if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) {
626         SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
627         return (0);
628     }
629
630     ret = SSL_CTX_use_PrivateKey(ctx, pkey);
631     EVP_PKEY_free(pkey);
632     return (ret);
633 }
634
635 #ifndef OPENSSL_NO_STDIO
636 /*
637  * Read a file that contains our certificate in "PEM" format, possibly
638  * followed by a sequence of CA certificates that should be sent to the peer
639  * in the Certificate message.
640  */
641 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
642 {
643     BIO *in;
644     int ret = 0;
645     X509 *x = NULL;
646     pem_password_cb *passwd_callback;
647     void *passwd_callback_userdata;
648
649     ERR_clear_error();          /* clear error stack for
650                                  * SSL_CTX_use_certificate() */
651
652     if (ctx != NULL) {
653         passwd_callback = ctx->default_passwd_callback;
654         passwd_callback_userdata = ctx->default_passwd_callback_userdata;
655     } else {
656         passwd_callback = ssl->default_passwd_callback;
657         passwd_callback_userdata = ssl->default_passwd_callback_userdata;
658     }
659
660     in = BIO_new(BIO_s_file());
661     if (in == NULL) {
662         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
663         goto end;
664     }
665
666     if (BIO_read_filename(in, file) <= 0) {
667         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
668         goto end;
669     }
670
671     x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
672                               passwd_callback_userdata);
673     if (x == NULL) {
674         SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
675         goto end;
676     }
677
678     if (ctx)
679         ret = SSL_CTX_use_certificate(ctx, x);
680     else
681         ret = SSL_use_certificate(ssl, x);
682
683     if (ERR_peek_error() != 0)
684         ret = 0;                /* Key/certificate mismatch doesn't imply
685                                  * ret==0 ... */
686     if (ret) {
687         /*
688          * If we could set up our certificate, now proceed to the CA
689          * certificates.
690          */
691         X509 *ca;
692         int r;
693         unsigned long err;
694
695         if (ctx)
696             r = SSL_CTX_clear_chain_certs(ctx);
697         else
698             r = SSL_clear_chain_certs(ssl);
699
700         if (r == 0) {
701             ret = 0;
702             goto end;
703         }
704
705         while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
706                                        passwd_callback_userdata))
707                 != NULL) {
708             if (ctx)
709                 r = SSL_CTX_add0_chain_cert(ctx, ca);
710             else
711                 r = SSL_add0_chain_cert(ssl, ca);
712             /*
713              * Note that we must not free ca if it was successfully added to
714              * the chain (while we must free the main certificate, since its
715              * reference count is increased by SSL_CTX_use_certificate).
716              */
717             if (!r) {
718                 X509_free(ca);
719                 ret = 0;
720                 goto end;
721             }
722         }
723         /* When the while loop ends, it's usually just EOF. */
724         err = ERR_peek_last_error();
725         if (ERR_GET_LIB(err) == ERR_LIB_PEM
726             && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
727             ERR_clear_error();
728         else
729             ret = 0;            /* some real error */
730     }
731
732  end:
733     X509_free(x);
734     BIO_free(in);
735     return (ret);
736 }
737
738 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
739 {
740     return use_certificate_chain_file(ctx, NULL, file);
741 }
742
743 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
744 {
745     return use_certificate_chain_file(NULL, ssl, file);
746 }
747 #endif
748
749 static int serverinfo_find_extension(const unsigned char *serverinfo,
750                                      size_t serverinfo_length,
751                                      unsigned int extension_type,
752                                      const unsigned char **extension_data,
753                                      size_t *extension_length)
754 {
755     *extension_data = NULL;
756     *extension_length = 0;
757     if (serverinfo == NULL || serverinfo_length == 0)
758         return -1;
759     for (;;) {
760         unsigned int type = 0;
761         size_t len = 0;
762
763         /* end of serverinfo */
764         if (serverinfo_length == 0)
765             return 0;           /* Extension not found */
766
767         /* read 2-byte type field */
768         if (serverinfo_length < 2)
769             return -1;          /* Error */
770         type = (serverinfo[0] << 8) + serverinfo[1];
771         serverinfo += 2;
772         serverinfo_length -= 2;
773
774         /* read 2-byte len field */
775         if (serverinfo_length < 2)
776             return -1;          /* Error */
777         len = (serverinfo[0] << 8) + serverinfo[1];
778         serverinfo += 2;
779         serverinfo_length -= 2;
780
781         if (len > serverinfo_length)
782             return -1;          /* Error */
783
784         if (type == extension_type) {
785             *extension_data = serverinfo;
786             *extension_length = len;
787             return 1;           /* Success */
788         }
789
790         serverinfo += len;
791         serverinfo_length -= len;
792     }
793     /* Unreachable */
794 }
795
796 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
797                                    const unsigned char *in,
798                                    size_t inlen, int *al, void *arg)
799 {
800
801     if (inlen != 0) {
802         *al = SSL_AD_DECODE_ERROR;
803         return 0;
804     }
805
806     return 1;
807 }
808
809 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
810                                  const unsigned char **out, size_t *outlen,
811                                  int *al, void *arg)
812 {
813     const unsigned char *serverinfo = NULL;
814     size_t serverinfo_length = 0;
815
816     /* Is there serverinfo data for the chosen server cert? */
817     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
818                                         &serverinfo_length)) != 0) {
819         /* Find the relevant extension from the serverinfo */
820         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
821                                                ext_type, out, outlen);
822         if (retval == -1) {
823             *al = SSL_AD_DECODE_ERROR;
824             return -1;          /* Error */
825         }
826         if (retval == 0)
827             return 0;           /* No extension found, don't send extension */
828         return 1;               /* Send extension */
829     }
830     return -1;                  /* No serverinfo data found, don't send
831                                  * extension */
832 }
833
834 /*
835  * With a NULL context, this function just checks that the serverinfo data
836  * parses correctly.  With a non-NULL context, it registers callbacks for
837  * the included extensions.
838  */
839 static int serverinfo_process_buffer(const unsigned char *serverinfo,
840                                      size_t serverinfo_length, SSL_CTX *ctx)
841 {
842     if (serverinfo == NULL || serverinfo_length == 0)
843         return 0;
844     for (;;) {
845         unsigned int ext_type = 0;
846         size_t len = 0;
847
848         /* end of serverinfo */
849         if (serverinfo_length == 0)
850             return 1;
851
852         /* read 2-byte type field */
853         if (serverinfo_length < 2)
854             return 0;
855         /* FIXME: check for types we understand explicitly? */
856
857         /* Register callbacks for extensions */
858         ext_type = (serverinfo[0] << 8) + serverinfo[1];
859         if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type,
860                                                   serverinfo_srv_add_cb,
861                                                   NULL, NULL,
862                                                   serverinfo_srv_parse_cb,
863                                                   NULL))
864             return 0;
865
866         serverinfo += 2;
867         serverinfo_length -= 2;
868
869         /* read 2-byte len field */
870         if (serverinfo_length < 2)
871             return 0;
872         len = (serverinfo[0] << 8) + serverinfo[1];
873         serverinfo += 2;
874         serverinfo_length -= 2;
875
876         if (len > serverinfo_length)
877             return 0;
878
879         serverinfo += len;
880         serverinfo_length -= len;
881     }
882 }
883
884 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
885                            size_t serverinfo_length)
886 {
887     unsigned char *new_serverinfo;
888
889     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
890         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER);
891         return 0;
892     }
893     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) {
894         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
895         return 0;
896     }
897     if (ctx->cert->key == NULL) {
898         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR);
899         return 0;
900     }
901     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
902                                      serverinfo_length);
903     if (new_serverinfo == NULL) {
904         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE);
905         return 0;
906     }
907     ctx->cert->key->serverinfo = new_serverinfo;
908     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
909     ctx->cert->key->serverinfo_length = serverinfo_length;
910
911     /*
912      * Now that the serverinfo is validated and stored, go ahead and
913      * register callbacks.
914      */
915     if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) {
916         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA);
917         return 0;
918     }
919     return 1;
920 }
921
922 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
923 {
924     unsigned char *serverinfo = NULL;
925     size_t serverinfo_length = 0;
926     unsigned char *extension = 0;
927     long extension_length = 0;
928     char *name = NULL;
929     char *header = NULL;
930     char namePrefix[] = "SERVERINFO FOR ";
931     int ret = 0;
932     BIO *bin = NULL;
933     size_t num_extensions = 0;
934
935     if (ctx == NULL || file == NULL) {
936         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
937                ERR_R_PASSED_NULL_PARAMETER);
938         goto end;
939     }
940
941     bin = BIO_new(BIO_s_file());
942     if (bin == NULL) {
943         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
944         goto end;
945     }
946     if (BIO_read_filename(bin, file) <= 0) {
947         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
948         goto end;
949     }
950
951     for (num_extensions = 0;; num_extensions++) {
952         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
953             == 0) {
954             /*
955              * There must be at least one extension in this file
956              */
957             if (num_extensions == 0) {
958                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
959                        SSL_R_NO_PEM_EXTENSIONS);
960                 goto end;
961             } else              /* End of file, we're done */
962                 break;
963         }
964         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
965         if (strlen(name) < strlen(namePrefix)) {
966             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
967                    SSL_R_PEM_NAME_TOO_SHORT);
968             goto end;
969         }
970         if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) {
971             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
972                    SSL_R_PEM_NAME_BAD_PREFIX);
973             goto end;
974         }
975         /*
976          * Check that the decoded PEM data is plausible (valid length field)
977          */
978         if (extension_length < 4
979             || (extension[2] << 8) + extension[3] != extension_length - 4) {
980             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
981             goto end;
982         }
983         /* Append the decoded extension to the serverinfo buffer */
984         serverinfo =
985             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
986         if (serverinfo == NULL) {
987             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
988             goto end;
989         }
990         memcpy(serverinfo + serverinfo_length, extension, extension_length);
991         serverinfo_length += extension_length;
992
993         OPENSSL_free(name);
994         name = NULL;
995         OPENSSL_free(header);
996         header = NULL;
997         OPENSSL_free(extension);
998         extension = NULL;
999     }
1000
1001     ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length);
1002  end:
1003     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1004     OPENSSL_free(name);
1005     OPENSSL_free(header);
1006     OPENSSL_free(extension);
1007     OPENSSL_free(serverinfo);
1008     BIO_free(bin);
1009     return ret;
1010 }