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