Coverage Report

Created: 2018-08-29 13:53

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