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