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