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