Coverage Report

Created: 2025-12-31 06:58

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