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