Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/ssl/statem/extensions_cust.c
Line
Count
Source
1
/*
2
 * Copyright 2014-2021 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
/* Custom extension utility functions */
11
12
#include <openssl/ct.h>
13
#include "../ssl_local.h"
14
#include "internal/cryptlib.h"
15
#include "statem_local.h"
16
17
typedef struct {
18
    void *add_arg;
19
    custom_ext_add_cb add_cb;
20
    custom_ext_free_cb free_cb;
21
} custom_ext_add_cb_wrap;
22
23
typedef struct {
24
    void *parse_arg;
25
    custom_ext_parse_cb parse_cb;
26
} custom_ext_parse_cb_wrap;
27
28
/*
29
 * Provide thin wrapper callbacks which convert new style arguments to old style
30
 */
31
static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,
32
    unsigned int context,
33
    const unsigned char **out,
34
    size_t *outlen, X509 *x, size_t chainidx,
35
    int *al, void *add_arg)
36
0
{
37
0
    custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
38
39
0
    if (add_cb_wrap->add_cb == NULL)
40
0
        return 1;
41
42
0
    return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,
43
0
        add_cb_wrap->add_arg);
44
0
}
45
46
static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,
47
    unsigned int context,
48
    const unsigned char *out, void *add_arg)
49
0
{
50
0
    custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
51
52
0
    if (add_cb_wrap->free_cb == NULL)
53
0
        return;
54
55
0
    add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);
56
0
}
57
58
static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,
59
    unsigned int context,
60
    const unsigned char *in,
61
    size_t inlen, X509 *x, size_t chainidx,
62
    int *al, void *parse_arg)
63
0
{
64
0
    custom_ext_parse_cb_wrap *parse_cb_wrap = (custom_ext_parse_cb_wrap *)parse_arg;
65
66
0
    if (parse_cb_wrap->parse_cb == NULL)
67
0
        return 1;
68
69
0
    return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,
70
0
        parse_cb_wrap->parse_arg);
71
0
}
72
73
/*
74
 * Find a custom extension from the list. The |role| param is there to
75
 * support the legacy API where custom extensions for client and server could
76
 * be set independently on the same SSL_CTX. It is set to ENDPOINT_SERVER if we
77
 * are trying to find a method relevant to the server, ENDPOINT_CLIENT for the
78
 * client, or ENDPOINT_BOTH for either
79
 */
80
custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
81
    ENDPOINT role, unsigned int ext_type,
82
    size_t *idx)
83
201k
{
84
201k
    size_t i;
85
201k
    custom_ext_method *meth = exts->meths;
86
87
205k
    for (i = 0; i < exts->meths_count; i++, meth++) {
88
43.8k
        if (ext_type == meth->ext_type
89
40.2k
            && (role == ENDPOINT_BOTH || role == meth->role
90
40.2k
                || meth->role == ENDPOINT_BOTH)) {
91
40.2k
            if (idx != NULL)
92
20.1k
                *idx = i;
93
40.2k
            return meth;
94
40.2k
        }
95
43.8k
    }
96
161k
    return NULL;
97
201k
}
98
99
/*
100
 * Initialise custom extensions flags to indicate neither sent nor received.
101
 */
102
void custom_ext_init(custom_ext_methods *exts)
103
160k
{
104
160k
    size_t i;
105
160k
    custom_ext_method *meth = exts->meths;
106
107
211k
    for (i = 0; i < exts->meths_count; i++, meth++)
108
50.4k
        meth->ext_flags = 0;
109
160k
}
110
111
/* Pass received custom extension data to the application for parsing. */
112
int custom_ext_parse(SSL *s, unsigned int context, unsigned int ext_type,
113
    const unsigned char *ext_data, size_t ext_size, X509 *x,
114
    size_t chainidx)
115
483
{
116
483
    int al;
117
483
    custom_ext_methods *exts = &s->cert->custext;
118
483
    custom_ext_method *meth;
119
483
    ENDPOINT role = ENDPOINT_BOTH;
120
121
483
    if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
122
483
        role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
123
124
483
    meth = custom_ext_find(exts, role, ext_type, NULL);
125
    /* If not found return success */
126
483
    if (!meth)
127
483
        return 1;
128
129
    /* Check if extension is defined for our protocol. If not, skip */
130
0
    if (!extension_is_relevant(s, meth->context, context))
131
0
        return 1;
132
133
0
    if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
134
        /*
135
         * If it's ServerHello or EncryptedExtensions we can't have any
136
         * extensions not sent in ClientHello.
137
         */
138
0
        if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
139
0
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
140
0
            return 0;
141
0
        }
142
0
    }
143
144
    /*
145
     * Extensions received in the ClientHello or CertificateRequest are marked
146
     * with the SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
147
     * extensions in the response messages
148
     */
149
0
    if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST))
150
0
        != 0)
151
0
        meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
152
153
    /* If no parse function set return success */
154
0
    if (!meth->parse_cb)
155
0
        return 1;
156
157
0
    if (meth->parse_cb(s, ext_type, context, ext_data, ext_size, x, chainidx,
158
0
            &al, meth->parse_arg)
159
0
        <= 0) {
160
0
        SSLfatal(s, al, SSL_R_BAD_EXTENSION);
161
0
        return 0;
162
0
    }
163
164
0
    return 1;
165
0
}
166
167
/*
168
 * Request custom extension data from the application and add to the return
169
 * buffer.
170
 */
171
int custom_ext_add(SSL *s, int context, WPACKET *pkt, X509 *x, size_t chainidx,
172
    int maxversion)
173
10.4k
{
174
10.4k
    custom_ext_methods *exts = &s->cert->custext;
175
10.4k
    custom_ext_method *meth;
176
10.4k
    size_t i;
177
10.4k
    int al;
178
179
10.4k
    for (i = 0; i < exts->meths_count; i++) {
180
0
        const unsigned char *out = NULL;
181
0
        size_t outlen = 0;
182
183
0
        meth = exts->meths + i;
184
185
0
        if (!should_add_extension(s, meth->context, context, maxversion))
186
0
            continue;
187
188
0
        if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) {
189
            /* Only send extensions present in ClientHello/CertificateRequest */
190
0
            if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
191
0
                continue;
192
0
        }
193
        /*
194
         * We skip it if the callback is absent - except for a ClientHello where
195
         * we add an empty extension.
196
         */
197
0
        if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
198
0
            continue;
199
200
0
        if (meth->add_cb != NULL) {
201
0
            int cb_retval = meth->add_cb(s, meth->ext_type, context, &out,
202
0
                &outlen, x, chainidx, &al,
203
0
                meth->add_arg);
204
205
0
            if (cb_retval < 0) {
206
0
                SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
207
0
                return 0; /* error */
208
0
            }
209
0
            if (cb_retval == 0)
210
0
                continue; /* skip this extension */
211
0
        }
212
213
0
        if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
214
0
            || !WPACKET_start_sub_packet_u16(pkt)
215
0
            || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
216
0
            || !WPACKET_close(pkt)) {
217
0
            if (meth->free_cb != NULL)
218
0
                meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
219
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
220
0
            return 0;
221
0
        }
222
0
        if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
223
            /*
224
             * We can't send duplicates: code logic should prevent this.
225
             */
226
0
            if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {
227
0
                if (meth->free_cb != NULL)
228
0
                    meth->free_cb(s, meth->ext_type, context, out,
229
0
                        meth->add_arg);
230
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
231
0
                return 0;
232
0
            }
233
            /*
234
             * Indicate extension has been sent: this is both a sanity check to
235
             * ensure we don't send duplicate extensions and indicates that it
236
             * is not an error if the extension is present in ServerHello.
237
             */
238
0
            meth->ext_flags |= SSL_EXT_FLAG_SENT;
239
0
        }
240
0
        if (meth->free_cb != NULL)
241
0
            meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
242
0
    }
243
10.4k
    return 1;
244
10.4k
}
245
246
/* Copy the flags from src to dst for any extensions that exist in both */
247
int custom_exts_copy_flags(custom_ext_methods *dst,
248
    const custom_ext_methods *src)
249
0
{
250
0
    size_t i;
251
0
    custom_ext_method *methsrc = src->meths;
252
253
0
    for (i = 0; i < src->meths_count; i++, methsrc++) {
254
0
        custom_ext_method *methdst = custom_ext_find(dst, methsrc->role,
255
0
            methsrc->ext_type, NULL);
256
257
0
        if (methdst == NULL)
258
0
            continue;
259
260
0
        methdst->ext_flags = methsrc->ext_flags;
261
0
    }
262
263
0
    return 1;
264
0
}
265
266
/* Copy table of custom extensions */
267
int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
268
72.8k
{
269
72.8k
    size_t i;
270
72.8k
    int err = 0;
271
272
72.8k
    if (src->meths_count > 0) {
273
0
        dst->meths = OPENSSL_memdup(src->meths,
274
0
            sizeof(*src->meths) * src->meths_count);
275
0
        if (dst->meths == NULL)
276
0
            return 0;
277
0
        dst->meths_count = src->meths_count;
278
279
0
        for (i = 0; i < src->meths_count; i++) {
280
0
            custom_ext_method *methsrc = src->meths + i;
281
0
            custom_ext_method *methdst = dst->meths + i;
282
283
0
            if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
284
0
                continue;
285
286
            /*
287
             * We have found an old style API wrapper. We need to copy the
288
             * arguments too.
289
             */
290
291
0
            if (err) {
292
0
                methdst->add_arg = NULL;
293
0
                methdst->parse_arg = NULL;
294
0
                continue;
295
0
            }
296
297
0
            methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
298
0
                sizeof(custom_ext_add_cb_wrap));
299
0
            methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
300
0
                sizeof(custom_ext_parse_cb_wrap));
301
302
0
            if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
303
0
                err = 1;
304
0
        }
305
0
    }
306
307
72.8k
    if (err) {
308
0
        custom_exts_free(dst);
309
0
        return 0;
310
0
    }
311
312
72.8k
    return 1;
313
72.8k
}
314
315
void custom_exts_free(custom_ext_methods *exts)
316
326k
{
317
326k
    size_t i;
318
326k
    custom_ext_method *meth;
319
320
377k
    for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
321
50.4k
        if (meth->add_cb != custom_ext_add_old_cb_wrap)
322
50.4k
            continue;
323
324
        /* Old style API wrapper. Need to free the arguments too */
325
0
        OPENSSL_free(meth->add_arg);
326
0
        OPENSSL_free(meth->parse_arg);
327
0
    }
328
326k
    OPENSSL_free(exts->meths);
329
326k
    exts->meths = NULL;
330
326k
    exts->meths_count = 0;
331
326k
}
332
333
/* Return true if a client custom extension exists, false otherwise */
334
int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
335
0
{
336
0
    return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
337
0
               NULL)
338
0
        != NULL;
339
0
}
340
341
static int add_custom_ext_intern(SSL_CTX *ctx, ENDPOINT role,
342
    unsigned int ext_type,
343
    unsigned int context,
344
    SSL_custom_ext_add_cb_ex add_cb,
345
    SSL_custom_ext_free_cb_ex free_cb,
346
    void *add_arg,
347
    SSL_custom_ext_parse_cb_ex parse_cb,
348
    void *parse_arg)
349
0
{
350
0
    custom_ext_methods *exts = &ctx->cert->custext;
351
0
    custom_ext_method *meth, *tmp;
352
353
    /*
354
     * Check application error: if add_cb is not set free_cb will never be
355
     * called.
356
     */
357
0
    if (add_cb == NULL && free_cb != NULL)
358
0
        return 0;
359
360
0
#ifndef OPENSSL_NO_CT
361
    /*
362
     * We don't want applications registering callbacks for SCT extensions
363
     * whilst simultaneously using the built-in SCT validation features, as
364
     * these two things may not play well together.
365
     */
366
0
    if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
367
0
        && (context & SSL_EXT_CLIENT_HELLO) != 0
368
0
        && SSL_CTX_ct_is_enabled(ctx))
369
0
        return 0;
370
0
#endif
371
372
    /*
373
     * Don't add if extension supported internally, but make exception
374
     * for extension types that previously were not supported, but now are.
375
     */
376
0
    if (SSL_extension_supported(ext_type)
377
0
        && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
378
0
        return 0;
379
380
    /* Extension type must fit in 16 bits */
381
0
    if (ext_type > 0xffff)
382
0
        return 0;
383
    /* Search for duplicate */
384
0
    if (custom_ext_find(exts, role, ext_type, NULL))
385
0
        return 0;
386
0
    tmp = OPENSSL_realloc(exts->meths,
387
0
        (exts->meths_count + 1) * sizeof(custom_ext_method));
388
0
    if (tmp == NULL)
389
0
        return 0;
390
391
0
    exts->meths = tmp;
392
0
    meth = exts->meths + exts->meths_count;
393
0
    memset(meth, 0, sizeof(*meth));
394
0
    meth->role = role;
395
0
    meth->context = context;
396
0
    meth->parse_cb = parse_cb;
397
0
    meth->add_cb = add_cb;
398
0
    meth->free_cb = free_cb;
399
0
    meth->ext_type = ext_type;
400
0
    meth->add_arg = add_arg;
401
0
    meth->parse_arg = parse_arg;
402
0
    exts->meths_count++;
403
0
    return 1;
404
0
}
405
406
static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,
407
    unsigned int ext_type,
408
    unsigned int context,
409
    custom_ext_add_cb add_cb,
410
    custom_ext_free_cb free_cb,
411
    void *add_arg,
412
    custom_ext_parse_cb parse_cb, void *parse_arg)
413
0
{
414
0
    custom_ext_add_cb_wrap *add_cb_wrap
415
0
        = OPENSSL_malloc(sizeof(*add_cb_wrap));
416
0
    custom_ext_parse_cb_wrap *parse_cb_wrap
417
0
        = OPENSSL_malloc(sizeof(*parse_cb_wrap));
418
0
    int ret;
419
420
0
    if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
421
0
        OPENSSL_free(add_cb_wrap);
422
0
        OPENSSL_free(parse_cb_wrap);
423
0
        return 0;
424
0
    }
425
426
0
    add_cb_wrap->add_arg = add_arg;
427
0
    add_cb_wrap->add_cb = add_cb;
428
0
    add_cb_wrap->free_cb = free_cb;
429
0
    parse_cb_wrap->parse_arg = parse_arg;
430
0
    parse_cb_wrap->parse_cb = parse_cb;
431
432
0
    ret = add_custom_ext_intern(ctx, role, ext_type,
433
0
        context,
434
0
        custom_ext_add_old_cb_wrap,
435
0
        custom_ext_free_old_cb_wrap,
436
0
        add_cb_wrap,
437
0
        custom_ext_parse_old_cb_wrap,
438
0
        parse_cb_wrap);
439
440
0
    if (!ret) {
441
0
        OPENSSL_free(add_cb_wrap);
442
0
        OPENSSL_free(parse_cb_wrap);
443
0
    }
444
445
0
    return ret;
446
0
}
447
448
/* Application level functions to add the old custom extension callbacks */
449
int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
450
    custom_ext_add_cb add_cb,
451
    custom_ext_free_cb free_cb,
452
    void *add_arg,
453
    custom_ext_parse_cb parse_cb, void *parse_arg)
454
0
{
455
0
    return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,
456
0
        SSL_EXT_TLS1_2_AND_BELOW_ONLY
457
0
            | SSL_EXT_CLIENT_HELLO
458
0
            | SSL_EXT_TLS1_2_SERVER_HELLO
459
0
            | SSL_EXT_IGNORE_ON_RESUMPTION,
460
0
        add_cb, free_cb, add_arg, parse_cb, parse_arg);
461
0
}
462
463
int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
464
    custom_ext_add_cb add_cb,
465
    custom_ext_free_cb free_cb,
466
    void *add_arg,
467
    custom_ext_parse_cb parse_cb, void *parse_arg)
468
0
{
469
0
    return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,
470
0
        SSL_EXT_TLS1_2_AND_BELOW_ONLY
471
0
            | SSL_EXT_CLIENT_HELLO
472
0
            | SSL_EXT_TLS1_2_SERVER_HELLO
473
0
            | SSL_EXT_IGNORE_ON_RESUMPTION,
474
0
        add_cb, free_cb, add_arg, parse_cb, parse_arg);
475
0
}
476
477
int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
478
    unsigned int context,
479
    SSL_custom_ext_add_cb_ex add_cb,
480
    SSL_custom_ext_free_cb_ex free_cb,
481
    void *add_arg,
482
    SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
483
0
{
484
0
    return add_custom_ext_intern(ctx, ENDPOINT_BOTH, ext_type, context, add_cb,
485
0
        free_cb, add_arg, parse_cb, parse_arg);
486
0
}
487
488
int SSL_extension_supported(unsigned int ext_type)
489
{
490
    switch (ext_type) {
491
        /* Internally supported extensions. */
492
    case TLSEXT_TYPE_application_layer_protocol_negotiation:
493
    case TLSEXT_TYPE_ec_point_formats:
494
    case TLSEXT_TYPE_supported_groups:
495
    case TLSEXT_TYPE_key_share:
496
#ifndef OPENSSL_NO_NEXTPROTONEG
497
    case TLSEXT_TYPE_next_proto_neg:
498
#endif
499
    case TLSEXT_TYPE_padding:
500
    case TLSEXT_TYPE_renegotiate:
501
    case TLSEXT_TYPE_max_fragment_length:
502
    case TLSEXT_TYPE_server_name:
503
    case TLSEXT_TYPE_session_ticket:
504
    case TLSEXT_TYPE_signature_algorithms:
505
#ifndef OPENSSL_NO_SRP
506
    case TLSEXT_TYPE_srp:
507
#endif
508
#ifndef OPENSSL_NO_OCSP
509
    case TLSEXT_TYPE_status_request:
510
#endif
511
#ifndef OPENSSL_NO_CT
512
    case TLSEXT_TYPE_signed_certificate_timestamp:
513
#endif
514
#ifndef OPENSSL_NO_SRTP
515
    case TLSEXT_TYPE_use_srtp:
516
#endif
517
    case TLSEXT_TYPE_encrypt_then_mac:
518
    case TLSEXT_TYPE_supported_versions:
519
    case TLSEXT_TYPE_extended_master_secret:
520
    case TLSEXT_TYPE_psk_kex_modes:
521
    case TLSEXT_TYPE_cookie:
522
    case TLSEXT_TYPE_early_data:
523
    case TLSEXT_TYPE_certificate_authorities:
524
    case TLSEXT_TYPE_psk:
525
    case TLSEXT_TYPE_post_handshake_auth:
526
        return 1;
527
    default:
528
        return 0;
529
    }
530
}