Revert "Fix clang compile time error"
[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     /* We only support extensions for the first Certificate */
762     if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
763         return 0;
764
765     /* Is there serverinfo data for the chosen server cert? */
766     if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
767                                         &serverinfo_length)) != 0) {
768         /* Find the relevant extension from the serverinfo */
769         int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
770                                                ext_type, out, outlen);
771         if (retval == -1) {
772             *al = SSL_AD_DECODE_ERROR;
773             return -1;          /* Error */
774         }
775         if (retval == 0)
776             return 0;           /* No extension found, don't send extension */
777         return 1;               /* Send extension */
778     }
779     return 0;                   /* No serverinfo data found, don't send
780                                  * extension */
781 }
782
783 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
784                                  const unsigned char **out, size_t *outlen,
785                                  int *al, void *arg)
786 {
787     return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
788                                    arg);
789 }
790
791 /*
792  * With a NULL context, this function just checks that the serverinfo data
793  * parses correctly.  With a non-NULL context, it registers callbacks for
794  * the included extensions.
795  */
796 static int serverinfo_process_buffer(unsigned int version,
797                                      const unsigned char *serverinfo,
798                                      size_t serverinfo_length, SSL_CTX *ctx)
799 {
800     PACKET pkt;
801
802     if (serverinfo == NULL || serverinfo_length == 0)
803         return 0;
804
805     if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
806         return 0;
807
808     if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
809         return 0;
810
811     while (PACKET_remaining(&pkt)) {
812         unsigned long context = 0;
813         unsigned int ext_type = 0;
814         PACKET data;
815
816         if (!PACKET_get_net_4(&pkt, &context)
817                 || !PACKET_get_net_2(&pkt, &ext_type)
818                 || !PACKET_get_length_prefixed_2(&pkt, &data))
819             return 0;
820
821         if (ctx == NULL)
822             continue;
823
824         if (version == SSL_SERVERINFOV1) {
825             if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
826                                                serverinfo_srv_add_cb,
827                                                NULL, NULL,
828                                                serverinfo_srv_parse_cb,
829                                                NULL))
830                 return 0;
831         } else {
832             if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
833                                         serverinfoex_srv_add_cb,
834                                         NULL, NULL,
835                                         serverinfoex_srv_parse_cb,
836                                         NULL))
837                 return 0;
838         }
839     }
840
841     return 1;
842 }
843
844 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
845                               const unsigned char *serverinfo,
846                               size_t serverinfo_length)
847 {
848     unsigned char *new_serverinfo;
849
850     if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
851         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
852         return 0;
853     }
854     if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
855                                    NULL)) {
856         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
857         return 0;
858     }
859     if (ctx->cert->key == NULL) {
860         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
861         return 0;
862     }
863     new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
864                                      serverinfo_length);
865     if (new_serverinfo == NULL) {
866         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
867         return 0;
868     }
869     ctx->cert->key->serverinfo = new_serverinfo;
870     memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
871     ctx->cert->key->serverinfo_length = serverinfo_length;
872
873     /*
874      * Now that the serverinfo is validated and stored, go ahead and
875      * register callbacks.
876      */
877     if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
878                                    ctx)) {
879         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
880         return 0;
881     }
882     return 1;
883 }
884
885 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
886                            size_t serverinfo_length)
887 {
888     return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
889                                      serverinfo_length);
890 }
891
892 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
893 {
894     unsigned char *serverinfo = NULL;
895     unsigned char *tmp;
896     size_t serverinfo_length = 0;
897     unsigned char *extension = 0;
898     long extension_length = 0;
899     char *name = NULL;
900     char *header = NULL;
901     char namePrefix1[] = "SERVERINFO FOR ";
902     char namePrefix2[] = "SERVERINFOV2 FOR ";
903     int ret = 0;
904     BIO *bin = NULL;
905     size_t num_extensions = 0, contextoff = 0;
906     unsigned int version;
907
908     if (ctx == NULL || file == NULL) {
909         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
910         goto end;
911     }
912
913     bin = BIO_new(BIO_s_file());
914     if (bin == NULL) {
915         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
916         goto end;
917     }
918     if (BIO_read_filename(bin, file) <= 0) {
919         SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
920         goto end;
921     }
922
923     for (num_extensions = 0;; num_extensions++) {
924         if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
925             == 0) {
926             /*
927              * There must be at least one extension in this file
928              */
929             if (num_extensions == 0) {
930                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
931                        SSL_R_NO_PEM_EXTENSIONS);
932                 goto end;
933             } else              /* End of file, we're done */
934                 break;
935         }
936         /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
937         if (strlen(name) < strlen(namePrefix1)) {
938             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
939             goto end;
940         }
941         if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
942             version = SSL_SERVERINFOV1;
943         } else {
944             if (strlen(name) < strlen(namePrefix2)) {
945                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
946                        SSL_R_PEM_NAME_TOO_SHORT);
947                 goto end;
948             }
949             if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
950                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
951                        SSL_R_PEM_NAME_BAD_PREFIX);
952                 goto end;
953             }
954             version = SSL_SERVERINFOV2;
955         }
956         /*
957          * Check that the decoded PEM data is plausible (valid length field)
958          */
959         if (version == SSL_SERVERINFOV1) {
960             /* 4 byte header: 2 bytes type, 2 bytes len */
961             if (extension_length < 4
962                     || (extension[2] << 8) + extension[3]
963                        != extension_length - 4) {
964                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
965                 goto end;
966             }
967             /*
968              * File does not have a context value so we must take account of
969              * this later.
970              */
971             contextoff = 4;
972         } else {
973             /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
974             if (extension_length < 8
975                     || (extension[6] << 8) + extension[7]
976                        != extension_length - 8) {
977                 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
978                 goto end;
979             }
980         }
981         /* Append the decoded extension to the serverinfo buffer */
982         tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
983                                           + contextoff);
984         if (tmp == NULL) {
985             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
986             goto end;
987         }
988         serverinfo = tmp;
989         if (contextoff > 0) {
990             unsigned int synthcontext = SSL_EXT_CLIENT_HELLO
991                                         | SSL_EXT_TLS1_2_SERVER_HELLO;
992             unsigned char *sinfo = serverinfo + serverinfo_length;
993
994             /* We know this only uses the last 2 bytes */
995             sinfo[0] = 0;
996             sinfo[1] = 0;
997             sinfo[2] = (synthcontext >> 8) & 0xff;
998             sinfo[3] = synthcontext & 0xff;
999         }
1000         memcpy(serverinfo + serverinfo_length + contextoff,
1001                extension, extension_length);
1002         serverinfo_length += extension_length + contextoff;
1003
1004         OPENSSL_free(name);
1005         name = NULL;
1006         OPENSSL_free(header);
1007         header = NULL;
1008         OPENSSL_free(extension);
1009         extension = NULL;
1010     }
1011
1012     ret = SSL_CTX_use_serverinfo_ex(ctx, version, serverinfo,
1013                                     serverinfo_length);
1014  end:
1015     /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1016     OPENSSL_free(name);
1017     OPENSSL_free(header);
1018     OPENSSL_free(extension);
1019     OPENSSL_free(serverinfo);
1020     BIO_free(bin);
1021     return ret;
1022 }