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