Coverage Report

Created: 2025-06-13 06:58

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