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