Coverage Report

Created: 2023-04-12 06:22

/src/openssl/ssl/ssl_rsa.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2022 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
    if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
72
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
73
0
        goto end;
74
0
    }
75
0
    x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
76
0
    if (x == NULL) {
77
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
78
0
        goto end;
79
0
    }
80
0
    if (type == SSL_FILETYPE_ASN1) {
81
0
        j = ERR_R_ASN1_LIB;
82
0
        cert = d2i_X509_bio(in, &x);
83
0
    } else if (type == SSL_FILETYPE_PEM) {
84
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
85
86
0
        if (sc == NULL)
87
0
            goto end;
88
89
0
        j = ERR_R_PEM_LIB;
90
0
        cert = PEM_read_bio_X509(in, &x, sc->default_passwd_callback,
91
0
                                 sc->default_passwd_callback_userdata);
92
0
    } else {
93
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
94
0
        goto end;
95
0
    }
96
97
0
    if (cert == NULL) {
98
0
        ERR_raise(ERR_LIB_SSL, j);
99
0
        goto end;
100
0
    }
101
102
0
    ret = SSL_use_certificate(ssl, x);
103
0
 end:
104
0
    X509_free(x);
105
0
    BIO_free(in);
106
0
    return ret;
107
0
}
108
109
int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
110
0
{
111
0
    X509 *x;
112
0
    int ret;
113
114
0
    x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
115
0
    if (x == NULL) {
116
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
117
0
        return 0;
118
0
    }
119
120
0
    if (d2i_X509(&x, &d, (long)len)== NULL) {
121
0
        X509_free(x);
122
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
123
0
        return 0;
124
0
    }
125
126
0
    ret = SSL_use_certificate(ssl, x);
127
0
    X509_free(x);
128
0
    return ret;
129
0
}
130
131
static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey, SSL_CTX *ctx)
132
0
{
133
0
    size_t i;
134
135
0
    if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) {
136
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
137
0
        return 0;
138
0
    }
139
140
0
    if (c->pkeys[i].x509 != NULL
141
0
            && !X509_check_private_key(c->pkeys[i].x509, pkey))
142
0
        return 0;
143
144
0
    EVP_PKEY_free(c->pkeys[i].privatekey);
145
0
    EVP_PKEY_up_ref(pkey);
146
0
    c->pkeys[i].privatekey = pkey;
147
0
    c->key = &c->pkeys[i];
148
0
    return 1;
149
0
}
150
151
int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
152
0
{
153
0
    int ret;
154
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
155
156
0
    if (sc == NULL)
157
0
        return 0;
158
159
0
    if (pkey == NULL) {
160
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
161
0
        return 0;
162
0
    }
163
0
    ret = ssl_set_pkey(sc->cert, pkey, SSL_CONNECTION_GET_CTX(sc));
164
0
    return ret;
165
0
}
166
167
int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
168
0
{
169
0
    int j, ret = 0;
170
0
    BIO *in;
171
0
    EVP_PKEY *pkey = NULL;
172
173
0
    in = BIO_new(BIO_s_file());
174
0
    if (in == NULL) {
175
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
176
0
        goto end;
177
0
    }
178
179
0
    if (BIO_read_filename(in, file) <= 0) {
180
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
181
0
        goto end;
182
0
    }
183
0
    if (type == SSL_FILETYPE_PEM) {
184
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
185
186
0
        if (sc == NULL)
187
0
            goto end;
188
189
0
        j = ERR_R_PEM_LIB;
190
0
        pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
191
0
                                          sc->default_passwd_callback,
192
0
                                          sc->default_passwd_callback_userdata,
193
0
                                          ssl->ctx->libctx,
194
0
                                          ssl->ctx->propq);
195
0
    } else if (type == SSL_FILETYPE_ASN1) {
196
0
        j = ERR_R_ASN1_LIB;
197
0
        pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
198
0
                                     ssl->ctx->propq);
199
0
    } else {
200
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
201
0
        goto end;
202
0
    }
203
0
    if (pkey == NULL) {
204
0
        ERR_raise(ERR_LIB_SSL, j);
205
0
        goto end;
206
0
    }
207
0
    ret = SSL_use_PrivateKey(ssl, pkey);
208
0
    EVP_PKEY_free(pkey);
209
0
 end:
210
0
    BIO_free(in);
211
0
    return ret;
212
0
}
213
214
int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
215
                            long len)
216
0
{
217
0
    int ret;
218
0
    const unsigned char *p;
219
0
    EVP_PKEY *pkey;
220
221
0
    p = d;
222
0
    if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
223
0
                                  ssl->ctx->propq)) == NULL) {
224
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
225
0
        return 0;
226
0
    }
227
228
0
    ret = SSL_use_PrivateKey(ssl, pkey);
229
0
    EVP_PKEY_free(pkey);
230
0
    return ret;
231
0
}
232
233
int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
234
0
{
235
0
    int rv;
236
0
    if (x == NULL) {
237
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
238
0
        return 0;
239
0
    }
240
241
0
    rv = ssl_security_cert(NULL, ctx, x, 0, 1);
242
0
    if (rv != 1) {
243
0
        ERR_raise(ERR_LIB_SSL, rv);
244
0
        return 0;
245
0
    }
246
0
    return ssl_set_cert(ctx->cert, x, ctx);
247
0
}
248
249
static int ssl_set_cert(CERT *c, X509 *x, SSL_CTX *ctx)
250
0
{
251
0
    EVP_PKEY *pkey;
252
0
    size_t i;
253
254
0
    pkey = X509_get0_pubkey(x);
255
0
    if (pkey == NULL) {
256
0
        ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
257
0
        return 0;
258
0
    }
259
260
0
    if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) {
261
0
        ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
262
0
        return 0;
263
0
    }
264
265
0
    if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
266
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
267
0
        return 0;
268
0
    }
269
270
0
    if (c->pkeys[i].privatekey != NULL) {
271
        /*
272
         * The return code from EVP_PKEY_copy_parameters is deliberately
273
         * ignored. Some EVP_PKEY types cannot do this.
274
         * coverity[check_return]
275
         */
276
0
        EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
277
0
        ERR_clear_error();
278
279
0
        if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
280
            /*
281
             * don't fail for a cert/key mismatch, just free current private
282
             * key (when switching to a different cert & key, first this
283
             * function should be used, then ssl_set_pkey
284
             */
285
0
            EVP_PKEY_free(c->pkeys[i].privatekey);
286
0
            c->pkeys[i].privatekey = NULL;
287
            /* clear error queue */
288
0
            ERR_clear_error();
289
0
        }
290
0
    }
291
292
0
    X509_free(c->pkeys[i].x509);
293
0
    X509_up_ref(x);
294
0
    c->pkeys[i].x509 = x;
295
0
    c->key = &(c->pkeys[i]);
296
297
0
    return 1;
298
0
}
299
300
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
301
0
{
302
0
    int j = SSL_R_BAD_VALUE;
303
0
    BIO *in;
304
0
    int ret = 0;
305
0
    X509 *x = NULL, *cert = NULL;
306
307
0
    in = BIO_new(BIO_s_file());
308
0
    if (in == NULL) {
309
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
310
0
        goto end;
311
0
    }
312
313
0
    if (BIO_read_filename(in, file) <= 0) {
314
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
315
0
        goto end;
316
0
    }
317
0
    if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
318
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
319
0
        goto end;
320
0
    }
321
0
    x = X509_new_ex(ctx->libctx, ctx->propq);
322
0
    if (x == NULL) {
323
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
324
0
        goto end;
325
0
    }
326
0
    if (type == SSL_FILETYPE_ASN1) {
327
0
        j = ERR_R_ASN1_LIB;
328
0
        cert = d2i_X509_bio(in, &x);
329
0
    } else if (type == SSL_FILETYPE_PEM) {
330
0
        j = ERR_R_PEM_LIB;
331
0
        cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
332
0
                                 ctx->default_passwd_callback_userdata);
333
0
    }
334
0
    if (cert == NULL) {
335
0
        ERR_raise(ERR_LIB_SSL, j);
336
0
        goto end;
337
0
    }
338
339
0
    ret = SSL_CTX_use_certificate(ctx, x);
340
0
 end:
341
0
    X509_free(x);
342
0
    BIO_free(in);
343
0
    return ret;
344
0
}
345
346
int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
347
0
{
348
0
    X509 *x;
349
0
    int ret;
350
351
0
    x = X509_new_ex(ctx->libctx, ctx->propq);
352
0
    if (x == NULL) {
353
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
354
0
        return 0;
355
0
    }
356
357
0
    if (d2i_X509(&x, &d, (long)len) == NULL) {
358
0
        X509_free(x);
359
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
360
0
        return 0;
361
0
    }
362
363
0
    ret = SSL_CTX_use_certificate(ctx, x);
364
0
    X509_free(x);
365
0
    return ret;
366
0
}
367
368
int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
369
0
{
370
0
    if (pkey == NULL) {
371
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
372
0
        return 0;
373
0
    }
374
0
    return ssl_set_pkey(ctx->cert, pkey, ctx);
375
0
}
376
377
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
378
0
{
379
0
    int j, ret = 0;
380
0
    BIO *in;
381
0
    EVP_PKEY *pkey = NULL;
382
383
0
    in = BIO_new(BIO_s_file());
384
0
    if (in == NULL) {
385
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
386
0
        goto end;
387
0
    }
388
389
0
    if (BIO_read_filename(in, file) <= 0) {
390
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
391
0
        goto end;
392
0
    }
393
0
    if (type == SSL_FILETYPE_PEM) {
394
0
        j = ERR_R_PEM_LIB;
395
0
        pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
396
0
                                       ctx->default_passwd_callback,
397
0
                                       ctx->default_passwd_callback_userdata,
398
0
                                       ctx->libctx, ctx->propq);
399
0
    } else if (type == SSL_FILETYPE_ASN1) {
400
0
        j = ERR_R_ASN1_LIB;
401
0
        pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
402
0
    } else {
403
0
        ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
404
0
        goto end;
405
0
    }
406
0
    if (pkey == NULL) {
407
0
        ERR_raise(ERR_LIB_SSL, j);
408
0
        goto end;
409
0
    }
410
0
    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
411
0
    EVP_PKEY_free(pkey);
412
0
 end:
413
0
    BIO_free(in);
414
0
    return ret;
415
0
}
416
417
int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
418
                                const unsigned char *d, long len)
419
0
{
420
0
    int ret;
421
0
    const unsigned char *p;
422
0
    EVP_PKEY *pkey;
423
424
0
    p = d;
425
0
    if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
426
0
                                  ctx->propq)) == NULL) {
427
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
428
0
        return 0;
429
0
    }
430
431
0
    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
432
0
    EVP_PKEY_free(pkey);
433
0
    return ret;
434
0
}
435
436
/*
437
 * Read a file that contains our certificate in "PEM" format, possibly
438
 * followed by a sequence of CA certificates that should be sent to the peer
439
 * in the Certificate message.
440
 */
441
static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
442
0
{
443
0
    BIO *in;
444
0
    int ret = 0;
445
0
    X509 *x = NULL;
446
0
    pem_password_cb *passwd_callback;
447
0
    void *passwd_callback_userdata;
448
0
    SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
449
450
0
    if (ctx == NULL && ssl == NULL)
451
0
        return 0;
452
453
0
    ERR_clear_error();          /* clear error stack for
454
                                 * SSL_CTX_use_certificate() */
455
456
0
    if (ctx != NULL) {
457
0
        passwd_callback = ctx->default_passwd_callback;
458
0
        passwd_callback_userdata = ctx->default_passwd_callback_userdata;
459
0
    } else {
460
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
461
462
0
        if (sc == NULL)
463
0
            return 0;
464
465
0
        passwd_callback = sc->default_passwd_callback;
466
0
        passwd_callback_userdata = sc->default_passwd_callback_userdata;
467
0
    }
468
469
0
    in = BIO_new(BIO_s_file());
470
0
    if (in == NULL) {
471
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
472
0
        goto end;
473
0
    }
474
475
0
    if (BIO_read_filename(in, file) <= 0) {
476
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
477
0
        goto end;
478
0
    }
479
480
0
    x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
481
0
    if (x == NULL) {
482
0
        ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
483
0
        goto end;
484
0
    }
485
0
    if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
486
0
                              passwd_callback_userdata) == NULL) {
487
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
488
0
        goto end;
489
0
    }
490
491
0
    if (ctx)
492
0
        ret = SSL_CTX_use_certificate(ctx, x);
493
0
    else
494
0
        ret = SSL_use_certificate(ssl, x);
495
496
0
    if (ERR_peek_error() != 0)
497
0
        ret = 0;                /* Key/certificate mismatch doesn't imply
498
                                 * ret==0 ... */
499
0
    if (ret) {
500
        /*
501
         * If we could set up our certificate, now proceed to the CA
502
         * certificates.
503
         */
504
0
        X509 *ca;
505
0
        int r;
506
0
        unsigned long err;
507
508
0
        if (ctx)
509
0
            r = SSL_CTX_clear_chain_certs(ctx);
510
0
        else
511
0
            r = SSL_clear_chain_certs(ssl);
512
513
0
        if (r == 0) {
514
0
            ret = 0;
515
0
            goto end;
516
0
        }
517
518
0
        while (1) {
519
0
            ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
520
0
            if (ca == NULL) {
521
0
                ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
522
0
                goto end;
523
0
            }
524
0
            if (PEM_read_bio_X509(in, &ca, passwd_callback,
525
0
                                  passwd_callback_userdata) != 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)) != 0) {
653
        /* Find the relevant extension from the serverinfo */
654
0
        int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
655
0
                                               ext_type, out, outlen);
656
0
        if (retval == -1) {
657
0
            *al = SSL_AD_INTERNAL_ERROR;
658
0
            return -1;          /* Error */
659
0
        }
660
0
        if (retval == 0)
661
0
            return 0;           /* No extension found, don't send extension */
662
0
        return 1;               /* Send extension */
663
0
    }
664
0
    return 0;                   /* No serverinfo data found, don't send
665
                                 * extension */
666
0
}
667
668
static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
669
                                 const unsigned char **out, size_t *outlen,
670
                                 int *al, void *arg)
671
0
{
672
0
    return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
673
0
                                   arg);
674
0
}
675
676
/*
677
 * With a NULL context, this function just checks that the serverinfo data
678
 * parses correctly.  With a non-NULL context, it registers callbacks for
679
 * the included extensions.
680
 */
681
static int serverinfo_process_buffer(unsigned int version,
682
                                     const unsigned char *serverinfo,
683
                                     size_t serverinfo_length, SSL_CTX *ctx)
684
0
{
685
0
    PACKET pkt;
686
687
0
    if (serverinfo == NULL || serverinfo_length == 0)
688
0
        return 0;
689
690
0
    if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
691
0
        return 0;
692
693
0
    if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
694
0
        return 0;
695
696
0
    while (PACKET_remaining(&pkt)) {
697
0
        unsigned long context = 0;
698
0
        unsigned int ext_type = 0;
699
0
        PACKET data;
700
701
0
        if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
702
0
                || !PACKET_get_net_2(&pkt, &ext_type)
703
0
                || !PACKET_get_length_prefixed_2(&pkt, &data))
704
0
            return 0;
705
706
0
        if (ctx == NULL)
707
0
            continue;
708
709
        /*
710
         * The old style custom extensions API could be set separately for
711
         * server/client, i.e. you could set one custom extension for a client,
712
         * and *for the same extension in the same SSL_CTX* you could set a
713
         * custom extension for the server as well. It seems quite weird to be
714
         * setting a custom extension for both client and server in a single
715
         * SSL_CTX - but theoretically possible. This isn't possible in the
716
         * new API. Therefore, if we have V1 serverinfo we use the old API. We
717
         * also use the old API even if we have V2 serverinfo but the context
718
         * looks like an old style <= TLSv1.2 extension.
719
         */
720
0
        if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
721
0
            if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
722
0
                                               serverinfo_srv_add_cb,
723
0
                                               NULL, NULL,
724
0
                                               serverinfo_srv_parse_cb,
725
0
                                               NULL))
726
0
                return 0;
727
0
        } else {
728
0
            if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
729
0
                                        serverinfoex_srv_add_cb,
730
0
                                        NULL, NULL,
731
0
                                        serverinfoex_srv_parse_cb,
732
0
                                        NULL))
733
0
                return 0;
734
0
        }
735
0
    }
736
737
0
    return 1;
738
0
}
739
740
static size_t extension_contextoff(unsigned int version)
741
0
{
742
0
    return version == SSL_SERVERINFOV1 ? 4 : 0;
743
0
}
744
745
static size_t extension_append_length(unsigned int version, size_t extension_length)
746
0
{
747
0
    return extension_length + extension_contextoff(version);
748
0
}
749
750
static void extension_append(unsigned int version,
751
                             const unsigned char *extension,
752
                             const size_t extension_length,
753
                             unsigned char *serverinfo)
754
0
{
755
0
    const size_t contextoff = extension_contextoff(version);
756
757
0
    if (contextoff > 0) {
758
        /* We know this only uses the last 2 bytes */
759
0
        serverinfo[0] = 0;
760
0
        serverinfo[1] = 0;
761
0
        serverinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
762
0
        serverinfo[3] = SYNTHV1CONTEXT & 0xff;
763
0
    }
764
765
0
    memcpy(serverinfo + contextoff, extension, extension_length);
766
0
}
767
768
int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
769
                              const unsigned char *serverinfo,
770
                              size_t serverinfo_length)
771
0
{
772
0
    unsigned char *new_serverinfo = NULL;
773
774
0
    if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
775
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
776
0
        return 0;
777
0
    }
778
0
    if (version == SSL_SERVERINFOV1) {
779
        /*
780
         * Convert serverinfo version v1 to v2 and call yourself recursively
781
         * over the converted serverinfo.
782
         */
783
0
        const size_t sinfo_length = extension_append_length(SSL_SERVERINFOV1,
784
0
                                                            serverinfo_length);
785
0
        unsigned char *sinfo;
786
0
        int ret;
787
788
0
        sinfo = OPENSSL_malloc(sinfo_length);
789
0
        if (sinfo == NULL)
790
0
            return 0;
791
792
0
        extension_append(SSL_SERVERINFOV1, serverinfo, serverinfo_length, sinfo);
793
794
0
        ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, sinfo,
795
0
                                        sinfo_length);
796
797
0
        OPENSSL_free(sinfo);
798
0
        return ret;
799
0
    }
800
0
    if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
801
0
                                   NULL)) {
802
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
803
0
        return 0;
804
0
    }
805
0
    if (ctx->cert->key == NULL) {
806
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
807
0
        return 0;
808
0
    }
809
0
    new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
810
0
                                     serverinfo_length);
811
0
    if (new_serverinfo == NULL)
812
0
        return 0;
813
0
    ctx->cert->key->serverinfo = new_serverinfo;
814
0
    memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
815
0
    ctx->cert->key->serverinfo_length = serverinfo_length;
816
817
    /*
818
     * Now that the serverinfo is validated and stored, go ahead and
819
     * register callbacks.
820
     */
821
0
    if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
822
0
                                   ctx)) {
823
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
824
0
        return 0;
825
0
    }
826
0
    return 1;
827
0
}
828
829
int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
830
                           size_t serverinfo_length)
831
0
{
832
0
    return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
833
0
                                     serverinfo_length);
834
0
}
835
836
int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
837
0
{
838
0
    unsigned char *serverinfo = NULL;
839
0
    unsigned char *tmp;
840
0
    size_t serverinfo_length = 0;
841
0
    unsigned char *extension = 0;
842
0
    long extension_length = 0;
843
0
    char *name = NULL;
844
0
    char *header = NULL;
845
0
    unsigned int name_len;
846
0
    int ret = 0;
847
0
    BIO *bin = NULL;
848
0
    size_t num_extensions = 0;
849
850
0
    if (ctx == NULL || file == NULL) {
851
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
852
0
        goto end;
853
0
    }
854
855
0
    bin = BIO_new(BIO_s_file());
856
0
    if (bin == NULL) {
857
0
        ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
858
0
        goto end;
859
0
    }
860
0
    if (BIO_read_filename(bin, file) <= 0) {
861
0
        ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
862
0
        goto end;
863
0
    }
864
865
0
    for (num_extensions = 0;; num_extensions++) {
866
0
        unsigned int version;
867
0
        size_t append_length;
868
869
0
        if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
870
0
            == 0) {
871
            /*
872
             * There must be at least one extension in this file
873
             */
874
0
            if (num_extensions == 0) {
875
0
                ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
876
0
                goto end;
877
0
            } else              /* End of file, we're done */
878
0
                break;
879
0
        }
880
        /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
881
0
        name_len = strlen(name);
882
0
        if (name_len < sizeof(NAME_PREFIX1) - 1) {
883
0
            ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
884
0
            goto end;
885
0
        }
886
0
        if (HAS_PREFIX(name, NAME_PREFIX1)) {
887
0
            version = SSL_SERVERINFOV1;
888
0
        } else {
889
0
            if (name_len < sizeof(NAME_PREFIX2) - 1) {
890
0
                ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
891
0
                goto end;
892
0
            }
893
0
            if (!HAS_PREFIX(name, NAME_PREFIX2)) {
894
0
                ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
895
0
                goto end;
896
0
            }
897
0
            version = SSL_SERVERINFOV2;
898
0
        }
899
        /*
900
         * Check that the decoded PEM data is plausible (valid length field)
901
         */
902
0
        if (version == SSL_SERVERINFOV1) {
903
            /* 4 byte header: 2 bytes type, 2 bytes len */
904
0
            if (extension_length < 4
905
0
                    || (extension[2] << 8) + extension[3]
906
0
                       != extension_length - 4) {
907
0
                ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
908
0
                goto end;
909
0
            }
910
0
        } else {
911
            /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
912
0
            if (extension_length < 8
913
0
                    || (extension[6] << 8) + extension[7]
914
0
                       != extension_length - 8) {
915
0
                ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
916
0
                goto end;
917
0
            }
918
0
        }
919
        /* Append the decoded extension to the serverinfo buffer */
920
0
        append_length = extension_append_length(version, extension_length);
921
0
        tmp = OPENSSL_realloc(serverinfo, serverinfo_length + append_length);
922
0
        if (tmp == NULL)
923
0
            goto end;
924
0
        serverinfo = tmp;
925
0
        extension_append(version, extension, extension_length,
926
0
                         serverinfo + serverinfo_length);
927
0
        serverinfo_length += append_length;
928
929
0
        OPENSSL_free(name);
930
0
        name = NULL;
931
0
        OPENSSL_free(header);
932
0
        header = NULL;
933
0
        OPENSSL_free(extension);
934
0
        extension = NULL;
935
0
    }
936
937
0
    ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
938
0
                                    serverinfo_length);
939
0
 end:
940
    /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
941
0
    OPENSSL_free(name);
942
0
    OPENSSL_free(header);
943
0
    OPENSSL_free(extension);
944
0
    OPENSSL_free(serverinfo);
945
0
    BIO_free(bin);
946
0
    return ret;
947
0
}
948
949
static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
950
                                STACK_OF(X509) *chain, int override)
951
0
{
952
0
    int ret = 0;
953
0
    size_t i;
954
0
    int j;
955
0
    int rv;
956
0
    CERT *c;
957
0
    STACK_OF(X509) *dup_chain = NULL;
958
0
    EVP_PKEY *pubkey = NULL;
959
0
    SSL_CONNECTION *sc = NULL;
960
961
0
    if (ctx == NULL &&
962
0
        (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
1019
0
                      || c->pkeys[i].privatekey != NULL
1020
0
                      || c->pkeys[i].chain != NULL)) {
1021
        /* No override, and something already there */
1022
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
1023
0
        goto out;
1024
0
    }
1025
1026
0
    if (chain != NULL) {
1027
0
        dup_chain = X509_chain_up_ref(chain);
1028
0
        if  (dup_chain == NULL) {
1029
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
1030
0
            goto out;
1031
0
        }
1032
0
    }
1033
1034
0
    OSSL_STACK_OF_X509_free(c->pkeys[i].chain);
1035
0
    c->pkeys[i].chain = dup_chain;
1036
1037
0
    X509_free(c->pkeys[i].x509);
1038
0
    X509_up_ref(x509);
1039
0
    c->pkeys[i].x509 = x509;
1040
1041
0
    EVP_PKEY_free(c->pkeys[i].privatekey);
1042
0
    EVP_PKEY_up_ref(privatekey);
1043
0
    c->pkeys[i].privatekey = privatekey;
1044
1045
0
    c->key = &(c->pkeys[i]);
1046
1047
0
    ret = 1;
1048
0
 out:
1049
0
    EVP_PKEY_free(pubkey);
1050
0
    return ret;
1051
0
}
1052
1053
int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1054
                         STACK_OF(X509) *chain, int override)
1055
0
{
1056
0
    return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1057
0
}
1058
1059
int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1060
                             STACK_OF(X509) *chain, int override)
1061
0
{
1062
0
    return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
1063
0
}