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