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