Coverage Report

Created: 2025-06-13 06:57

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