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