Coverage Report

Created: 2024-07-24 06:31

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