Coverage Report

Created: 2024-05-20 06:23

/src/nss/lib/ssl/tls13exthandle.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nssrenam.h"
8
#include "nss.h"
9
#include "ssl.h"
10
#include "sslproto.h"
11
#include "sslimpl.h"
12
#include "pk11pub.h"
13
#include "ssl3ext.h"
14
#include "ssl3exthandle.h"
15
#include "tls13ech.h"
16
#include "tls13exthandle.h"
17
#include "tls13psk.h"
18
#include "tls13subcerts.h"
19
20
SECStatus
21
tls13_ServerSendStatusRequestXtn(const sslSocket *ss, TLSExtensionData *xtnData,
22
                                 sslBuffer *buf, PRBool *added)
23
16
{
24
16
    const sslServerCert *serverCert = ss->sec.serverCert;
25
16
    const SECItem *item;
26
16
    SECStatus rv;
27
28
16
    if (!serverCert->certStatusArray ||
29
16
        !serverCert->certStatusArray->len) {
30
16
        return SECSuccess;
31
16
    }
32
33
0
    item = &serverCert->certStatusArray->items[0];
34
35
    /* Only send the first entry. */
36
    /* status_type == ocsp */
37
0
    rv = sslBuffer_AppendNumber(buf, 1 /*ocsp*/, 1);
38
0
    if (rv != SECSuccess) {
39
0
        return SECFailure;
40
0
    }
41
    /* opaque OCSPResponse<1..2^24-1> */
42
0
    rv = sslBuffer_AppendVariable(buf, item->data, item->len, 3);
43
0
    if (rv != SECSuccess) {
44
0
        return SECFailure;
45
0
    }
46
47
0
    *added = PR_TRUE;
48
0
    return SECSuccess;
49
0
}
50
51
/*
52
 *     [RFC 8446] Section 4.2.8.
53
 *
54
 *     struct {
55
 *         NamedGroup group;
56
 *         opaque key_exchange<1..2^16-1>;
57
 *     } KeyShareEntry;
58
 *
59
 */
60
PRUint32
61
tls13_SizeOfKeyShareEntry(const sslEphemeralKeyPair *keyPair)
62
423
{
63
    /* Size = NamedGroup(2) + length(2) + opaque<?> share */
64
423
    PRUint32 size = 2 + 2;
65
66
423
    const SECKEYPublicKey *pubKey = keyPair->keys->pubKey;
67
423
    switch (pubKey->keyType) {
68
355
        case ecKey:
69
355
            size += pubKey->u.ec.publicValue.len;
70
355
            break;
71
68
        case dhKey:
72
68
            size += pubKey->u.dh.prime.len;
73
68
            break;
74
0
        default:
75
0
            PORT_Assert(0);
76
0
            return 0;
77
423
    }
78
79
423
    if (keyPair->kemKeys) {
80
0
        PORT_Assert(!keyPair->kemCt);
81
0
        PORT_Assert(keyPair->group->name == ssl_grp_kem_xyber768d00);
82
0
        pubKey = keyPair->kemKeys->pubKey;
83
0
        size += pubKey->u.kyber.publicValue.len;
84
0
    }
85
423
    if (keyPair->kemCt) {
86
0
        PORT_Assert(!keyPair->kemKeys);
87
0
        PORT_Assert(keyPair->group->name == ssl_grp_kem_xyber768d00);
88
0
        size += keyPair->kemCt->len;
89
0
    }
90
91
423
    return size;
92
423
}
93
94
SECStatus
95
tls13_EncodeKeyShareEntry(sslBuffer *buf, sslEphemeralKeyPair *keyPair)
96
423
{
97
423
    SECStatus rv;
98
423
    unsigned int size = tls13_SizeOfKeyShareEntry(keyPair);
99
100
423
    rv = sslBuffer_AppendNumber(buf, keyPair->group->name, 2);
101
423
    if (rv != SECSuccess)
102
0
        return rv;
103
423
    rv = sslBuffer_AppendNumber(buf, size - 4, 2);
104
423
    if (rv != SECSuccess)
105
0
        return rv;
106
107
423
    const SECKEYPublicKey *pubKey = keyPair->keys->pubKey;
108
423
    switch (pubKey->keyType) {
109
355
        case ecKey:
110
355
            rv = sslBuffer_Append(buf, pubKey->u.ec.publicValue.data,
111
355
                                  pubKey->u.ec.publicValue.len);
112
355
            break;
113
68
        case dhKey:
114
68
            rv = ssl_AppendPaddedDHKeyShare(buf, pubKey, PR_FALSE);
115
68
            break;
116
0
        default:
117
0
            PORT_Assert(0);
118
0
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
119
0
            break;
120
423
    }
121
122
423
    if (rv != SECSuccess) {
123
0
        return rv;
124
0
    }
125
126
423
    if (keyPair->kemKeys) {
127
0
        PORT_Assert(!keyPair->kemCt);
128
0
        PORT_Assert(keyPair->group->name == ssl_grp_kem_xyber768d00);
129
0
        pubKey = keyPair->kemKeys->pubKey;
130
0
        rv = sslBuffer_Append(buf, pubKey->u.kyber.publicValue.data, pubKey->u.kyber.publicValue.len);
131
0
    }
132
423
    if (keyPair->kemCt) {
133
0
        PORT_Assert(!keyPair->kemKeys);
134
0
        PORT_Assert(keyPair->group->name == ssl_grp_kem_xyber768d00);
135
0
        rv = sslBuffer_Append(buf, keyPair->kemCt->data, keyPair->kemCt->len);
136
0
    }
137
138
423
    return rv;
139
423
}
140
141
SECStatus
142
tls13_ClientSendKeyShareXtn(const sslSocket *ss, TLSExtensionData *xtnData,
143
                            sslBuffer *buf, PRBool *added)
144
0
{
145
0
    SECStatus rv;
146
0
    PRCList *cursor;
147
0
    unsigned int lengthOffset;
148
149
0
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) {
150
0
        return SECSuccess;
151
0
    }
152
153
    /* Optimistically try to send an ECDHE key using the
154
     * preexisting key (in future will be keys) */
155
0
    SSL_TRC(3, ("%d: TLS13[%d]: send client key share xtn",
156
0
                SSL_GETPID(), ss->fd));
157
158
    /* Save the offset to the length. */
159
0
    rv = sslBuffer_Skip(buf, 2, &lengthOffset);
160
0
    if (rv != SECSuccess) {
161
0
        return SECFailure;
162
0
    }
163
164
0
    for (cursor = PR_NEXT_LINK(&ss->ephemeralKeyPairs);
165
0
         cursor != &ss->ephemeralKeyPairs;
166
0
         cursor = PR_NEXT_LINK(cursor)) {
167
0
        sslEphemeralKeyPair *keyPair = (sslEphemeralKeyPair *)cursor;
168
0
        rv = tls13_EncodeKeyShareEntry(buf, keyPair);
169
0
        if (rv != SECSuccess) {
170
0
            return SECFailure;
171
0
        }
172
0
    }
173
174
    /* GREASE KeyShareEntry:
175
     * [The client] MAY also send KeyShareEntry values for a subset of those
176
     * selected in the "key_share" extension.  For each of these, the
177
     * "key_exchange" field MAY be any value [RFC8701, Section 3.1].
178
     *
179
     * By default we do not send KeyShares for every NamedGroup so the
180
     * ServerKeyShare handshake message / additional round-trip is not
181
     * triggered by sending GREASE KeyShareEntries. */
182
0
    if (ss->opt.enableGrease) {
183
0
        rv = sslBuffer_AppendNumber(buf, ss->ssl3.hs.grease->idx[grease_group], 2);
184
0
        if (rv != SECSuccess)
185
0
            return rv;
186
        /* Entry length */
187
0
        rv = sslBuffer_AppendNumber(buf, 2, 2);
188
0
        if (rv != SECSuccess)
189
0
            return rv;
190
        /* Entry value */
191
0
        rv = sslBuffer_AppendNumber(buf, 0xCD, 2);
192
0
        if (rv != SECSuccess)
193
0
            return rv;
194
0
    }
195
196
0
    rv = sslBuffer_InsertLength(buf, lengthOffset, 2);
197
0
    if (rv != SECSuccess) {
198
0
        return SECFailure;
199
0
    }
200
201
0
    *added = PR_TRUE;
202
0
    return SECSuccess;
203
0
}
204
205
SECStatus
206
tls13_DecodeKeyShareEntry(sslReader *rdr, TLS13KeyShareEntry **ksp)
207
1.03k
{
208
1.03k
    SECStatus rv;
209
1.03k
    PRUint64 group;
210
1.03k
    const sslNamedGroupDef *groupDef;
211
1.03k
    TLS13KeyShareEntry *ks = NULL;
212
1.03k
    sslReadBuffer share;
213
214
1.03k
    rv = sslRead_ReadNumber(rdr, 2, &group);
215
1.03k
    if (rv != SECSuccess) {
216
3
        goto loser;
217
3
    }
218
1.03k
    groupDef = ssl_LookupNamedGroup(group);
219
1.03k
    rv = sslRead_ReadVariable(rdr, 2, &share);
220
1.03k
    if (rv != SECSuccess) {
221
19
        goto loser;
222
19
    }
223
224
    /* This has to happen here because we want to consume
225
     * the entire entry even if the group is unknown
226
     * or disabled. */
227
    /* If the group is disabled, continue. */
228
1.01k
    if (!groupDef) {
229
350
        return SECSuccess;
230
350
    }
231
232
664
    ks = PORT_ZNew(TLS13KeyShareEntry);
233
664
    if (!ks) {
234
0
        goto loser;
235
0
    }
236
664
    ks->group = groupDef;
237
238
664
    rv = SECITEM_MakeItem(NULL, &ks->key_exchange,
239
664
                          share.buf, share.len);
240
664
    if (rv != SECSuccess) {
241
0
        goto loser;
242
0
    }
243
244
664
    *ksp = ks;
245
664
    return SECSuccess;
246
247
22
loser:
248
22
    tls13_DestroyKeyShareEntry(ks);
249
250
22
    return SECFailure;
251
664
}
252
/* Handle an incoming KeyShare extension at the client and copy to
253
 * |xtnData->remoteKeyShares| for future use. The key
254
 * share is processed in tls13_HandleServerKeyShare(). */
255
SECStatus
256
tls13_ClientHandleKeyShareXtn(const sslSocket *ss, TLSExtensionData *xtnData,
257
                              SECItem *data)
258
0
{
259
0
    SECStatus rv;
260
0
    PORT_Assert(PR_CLIST_IS_EMPTY(&xtnData->remoteKeyShares));
261
0
    TLS13KeyShareEntry *ks = NULL;
262
263
0
    PORT_Assert(!ss->sec.isServer);
264
265
    /* The server must not send this extension when negotiating < TLS 1.3. */
266
0
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
267
0
        PORT_SetError(SSL_ERROR_EXTENSION_DISALLOWED_FOR_VERSION);
268
0
        return SECFailure;
269
0
    }
270
271
0
    SSL_TRC(3, ("%d: SSL3[%d]: handle key_share extension",
272
0
                SSL_GETPID(), ss->fd));
273
274
0
    sslReader rdr = SSL_READER(data->data, data->len);
275
0
    rv = tls13_DecodeKeyShareEntry(&rdr, &ks);
276
0
    if ((rv != SECSuccess) || !ks) {
277
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
278
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_KEY_SHARE);
279
0
        return SECFailure;
280
0
    }
281
282
0
    if (SSL_READER_REMAINING(&rdr)) {
283
0
        tls13_DestroyKeyShareEntry(ks);
284
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_KEY_SHARE);
285
0
        return SECFailure;
286
0
    }
287
0
    PR_APPEND_LINK(&ks->link, &xtnData->remoteKeyShares);
288
289
0
    return SECSuccess;
290
0
}
291
292
SECStatus
293
tls13_ClientHandleKeyShareXtnHrr(const sslSocket *ss, TLSExtensionData *xtnData,
294
                                 SECItem *data)
295
0
{
296
0
    SECStatus rv;
297
0
    PRUint32 tmp;
298
0
    const sslNamedGroupDef *group;
299
300
0
    PORT_Assert(!ss->sec.isServer);
301
0
    PORT_Assert(ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3);
302
303
0
    SSL_TRC(3, ("%d: SSL3[%d]: handle key_share extension in HRR",
304
0
                SSL_GETPID(), ss->fd));
305
306
0
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &tmp, 2, &data->data, &data->len);
307
0
    if (rv != SECSuccess) {
308
0
        return SECFailure; /* error code already set */
309
0
    }
310
0
    if (data->len) {
311
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
312
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_RETRY_REQUEST);
313
0
        return SECFailure;
314
0
    }
315
316
0
    group = ssl_LookupNamedGroup((SSLNamedGroup)tmp);
317
    /* If the group is not enabled, or we already have a share for the
318
     * requested group, abort. */
319
0
    if (!ssl_NamedGroupEnabled(ss, group) ||
320
0
        ssl_HaveEphemeralKeyPair(ss, group)) {
321
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
322
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_RETRY_REQUEST);
323
0
        return SECFailure;
324
0
    }
325
326
    /* Now delete all the key shares per [draft-ietf-tls-tls13 S 4.1.2] */
327
0
    ssl_FreeEphemeralKeyPairs(CONST_CAST(sslSocket, ss));
328
329
    /* And replace with our new share. */
330
0
    rv = tls13_AddKeyShare(CONST_CAST(sslSocket, ss), group);
331
0
    if (rv != SECSuccess) {
332
0
        ssl3_ExtSendAlert(ss, alert_fatal, internal_error);
333
0
        PORT_SetError(SEC_ERROR_KEYGEN_FAIL);
334
0
        return SECFailure;
335
0
    }
336
337
0
    return SECSuccess;
338
0
}
339
340
/* Handle an incoming KeyShare extension at the server and copy to
341
 * |xtnData->remoteKeyShares| for future use. The key
342
 * share is processed in tls13_HandleClientKeyShare(). */
343
SECStatus
344
tls13_ServerHandleKeyShareXtn(const sslSocket *ss, TLSExtensionData *xtnData,
345
                              SECItem *data)
346
544
{
347
544
    SECStatus rv;
348
544
    PRUint32 length;
349
350
544
    PORT_Assert(ss->sec.isServer);
351
544
    PORT_Assert(PR_CLIST_IS_EMPTY(&xtnData->remoteKeyShares));
352
353
544
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
354
24
        return SECSuccess;
355
24
    }
356
357
520
    SSL_TRC(3, ("%d: SSL3[%d]: handle key_share extension",
358
520
                SSL_GETPID(), ss->fd));
359
360
    /* Redundant length because of TLS encoding (this vector consumes
361
     * the entire extension.) */
362
520
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &length, 2, &data->data,
363
520
                                        &data->len);
364
520
    if (rv != SECSuccess)
365
1
        goto loser;
366
519
    if (length != data->len) {
367
        /* Check for consistency */
368
14
        PORT_SetError(SSL_ERROR_RX_MALFORMED_KEY_SHARE);
369
14
        goto loser;
370
14
    }
371
372
505
    sslReader rdr = SSL_READER(data->data, data->len);
373
1.51k
    while (SSL_READER_REMAINING(&rdr)) {
374
1.03k
        TLS13KeyShareEntry *ks = NULL;
375
1.03k
        rv = tls13_DecodeKeyShareEntry(&rdr, &ks);
376
1.03k
        if (rv != SECSuccess) {
377
22
            PORT_SetError(SSL_ERROR_RX_MALFORMED_KEY_SHARE);
378
22
            goto loser;
379
22
        }
380
1.01k
        if (ks) {
381
            /* |ks| == NULL if this is an unknown group. */
382
664
            PR_APPEND_LINK(&ks->link, &xtnData->remoteKeyShares);
383
664
        }
384
1.01k
    }
385
386
    /* Keep track of negotiated extensions. */
387
483
    xtnData->negotiated[xtnData->numNegotiated++] =
388
483
        ssl_tls13_key_share_xtn;
389
390
483
    return SECSuccess;
391
392
37
loser:
393
37
    tls13_DestroyKeyShares(&xtnData->remoteKeyShares);
394
37
    return SECFailure;
395
505
}
396
397
SECStatus
398
tls13_ServerSendKeyShareXtn(const sslSocket *ss, TLSExtensionData *xtnData,
399
                            sslBuffer *buf, PRBool *added)
400
423
{
401
423
    SECStatus rv;
402
423
    sslEphemeralKeyPair *keyPair;
403
404
    /* There should be exactly one key share. */
405
423
    PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ephemeralKeyPairs));
406
423
    PORT_Assert(PR_PREV_LINK(&ss->ephemeralKeyPairs) ==
407
423
                PR_NEXT_LINK(&ss->ephemeralKeyPairs));
408
409
423
    keyPair = (sslEphemeralKeyPair *)PR_NEXT_LINK(&ss->ephemeralKeyPairs);
410
411
423
    rv = tls13_EncodeKeyShareEntry(buf, keyPair);
412
423
    if (rv != SECSuccess) {
413
0
        return SECFailure;
414
0
    }
415
416
423
    *added = PR_TRUE;
417
423
    return SECSuccess;
418
423
}
419
420
/* Called by clients.
421
 *
422
 *      struct {
423
 *         opaque identity<0..2^16-1>;
424
 *         uint32 obfuscated_ticket_age;
425
 *     } PskIdentity;
426
 *
427
 *     opaque PskBinderEntry<32..255>;
428
 *
429
 *     struct {
430
 *         select (Handshake.msg_type) {
431
 *             case client_hello:
432
 *                 PskIdentity identities<6..2^16-1>;
433
 *                 PskBinderEntry binders<33..2^16-1>;
434
 *
435
 *             case server_hello:
436
 *                 uint16 selected_identity;
437
 *         };
438
 *
439
 *     } PreSharedKeyExtension;
440
 */
441
SECStatus
442
tls13_ClientSendPreSharedKeyXtn(const sslSocket *ss, TLSExtensionData *xtnData,
443
                                sslBuffer *buf, PRBool *added)
444
0
{
445
0
    const static PRUint8 binder[TLS13_MAX_FINISHED_SIZE] = { 0 };
446
0
    unsigned int binderLen;
447
0
    unsigned int identityLen = 0;
448
0
    const PRUint8 *identity = NULL;
449
0
    PRTime age;
450
0
    SECStatus rv;
451
452
    /* Exit early if no PSKs or max version < 1.3. */
453
0
    if (PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks) ||
454
0
        ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) {
455
0
        return SECSuccess;
456
0
    }
457
458
    /* ...or if PSK type is resumption, but we're not resuming. */
459
0
    sslPsk *psk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks);
460
0
    if (psk->type == ssl_psk_resume && !ss->statelessResume) {
461
0
        return SECSuccess;
462
0
    }
463
464
    /* ...or if PSKs are incompatible with negotiated ciphersuites
465
     * (different hash algorithms) on HRR.
466
     *
467
     * In addition, in its updated ClientHello, the client SHOULD NOT offer any
468
     * pre-shared keys associated with a hash other than that of the selected
469
     * cipher suite.  This allows the client to avoid having to compute partial
470
     * hash transcripts for multiple hashes in the second ClientHello
471
     * [RFC8446, Section 4.1.4]. */
472
0
    if (ss->ssl3.hs.helloRetry &&
473
0
        (psk->hash != ss->ssl3.hs.suite_def->prf_hash)) {
474
0
        return SECSuccess;
475
0
    }
476
477
    /* Save where this extension starts so that if we have to add padding, it
478
    * can be inserted before this extension. */
479
0
    PORT_Assert(buf->len >= 4);
480
0
    xtnData->lastXtnOffset = buf->len - 4;
481
0
    PORT_Assert(psk->type == ssl_psk_resume || psk->type == ssl_psk_external);
482
0
    binderLen = tls13_GetHashSizeForHash(psk->hash);
483
0
    if (psk->type == ssl_psk_resume) {
484
        /* Send a single ticket identity. */
485
0
        NewSessionTicket *session_ticket = &ss->sec.ci.sid->u.ssl3.locked.sessionTicket;
486
0
        identityLen = session_ticket->ticket.len;
487
0
        identity = session_ticket->ticket.data;
488
489
        /* Obfuscated age. */
490
0
        age = ssl_Time(ss) - session_ticket->received_timestamp;
491
0
        age /= PR_USEC_PER_MSEC;
492
0
        age += session_ticket->ticket_age_add;
493
0
        PRINT_BUF(50, (ss, "Sending Resumption PSK with identity", identity, identityLen));
494
0
    } else if (psk->type == ssl_psk_external) {
495
0
        identityLen = psk->label.len;
496
0
        identity = psk->label.data;
497
0
        age = 0;
498
0
        PRINT_BUF(50, (ss, "Sending External PSK with label", identity, identityLen));
499
0
    } else {
500
0
        PORT_Assert(0);
501
0
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
502
0
        return SECFailure;
503
0
    }
504
505
    /* Length is len(identityLen) + identityLen + len(age) */
506
0
    rv = sslBuffer_AppendNumber(buf, 2 + identityLen + 4, 2);
507
0
    if (rv != SECSuccess) {
508
0
        goto loser;
509
0
    }
510
511
0
    rv = sslBuffer_AppendVariable(buf, identity,
512
0
                                  identityLen, 2);
513
0
    if (rv != SECSuccess) {
514
0
        goto loser;
515
0
    }
516
517
0
    rv = sslBuffer_AppendNumber(buf, age, 4);
518
0
    if (rv != SECSuccess) {
519
0
        goto loser;
520
0
    }
521
522
    /* Write out the binder list length. */
523
0
    rv = sslBuffer_AppendNumber(buf, binderLen + 1, 2);
524
0
    if (rv != SECSuccess) {
525
0
        goto loser;
526
0
    }
527
528
    /* Write zeroes for the binder for the moment. These
529
     * are overwritten in tls13_WriteExtensionsWithBinder. */
530
0
    rv = sslBuffer_AppendVariable(buf, binder, binderLen, 1);
531
0
    if (rv != SECSuccess) {
532
0
        goto loser;
533
0
    }
534
535
0
    if (psk->type == ssl_psk_resume) {
536
0
        xtnData->sentSessionTicketInClientHello = PR_TRUE;
537
0
    }
538
539
0
    *added = PR_TRUE;
540
0
    return SECSuccess;
541
542
0
loser:
543
0
    xtnData->ticketTimestampVerified = PR_FALSE;
544
0
    return SECFailure;
545
0
}
546
547
/* Handle a TLS 1.3 PreSharedKey Extension. */
548
SECStatus
549
tls13_ServerHandlePreSharedKeyXtn(const sslSocket *ss, TLSExtensionData *xtnData,
550
                                  SECItem *data)
551
151
{
552
151
    SECItem inner;
553
151
    SECStatus rv;
554
151
    unsigned int numIdentities = 0;
555
151
    unsigned int numBinders = 0;
556
151
    SECItem *appToken;
557
558
151
    SSL_TRC(3, ("%d: SSL3[%d]: handle pre_shared_key extension",
559
151
                SSL_GETPID(), ss->fd));
560
561
    /* If we are doing < TLS 1.3, then ignore this. */
562
151
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
563
16
        return SECSuccess;
564
16
    }
565
566
    /* The application token is set via the cookie extension if this is the
567
     * second ClientHello.  Don't set it twice.  The cookie extension handler
568
     * sets |helloRetry| and that will have been called already because this
569
     * extension always comes last. */
570
135
    if (!ss->ssl3.hs.helloRetry) {
571
123
        appToken = &xtnData->applicationToken;
572
123
    } else {
573
12
        appToken = NULL;
574
12
    }
575
576
    /* Parse the identities list. */
577
135
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &inner, 2,
578
135
                                          &data->data, &data->len);
579
135
    if (rv != SECSuccess) {
580
5
        return SECFailure;
581
5
    }
582
583
238
    while (inner.len) {
584
163
        SECItem label;
585
163
        PRUint32 obfuscatedAge;
586
587
163
        rv = ssl3_ExtConsumeHandshakeVariable(ss, &label, 2,
588
163
                                              &inner.data, &inner.len);
589
163
        if (rv != SECSuccess)
590
10
            return rv;
591
153
        if (!label.len) {
592
1
            goto alert_loser;
593
1
        }
594
595
152
        rv = ssl3_ExtConsumeHandshakeNumber(ss, &obfuscatedAge, 4,
596
152
                                            &inner.data, &inner.len);
597
152
        if (rv != SECSuccess)
598
1
            return rv;
599
600
151
        if (!numIdentities) {
601
            /* Check any configured external PSK for a matching label.
602
             * If none exists, try to parse it as a ticket. */
603
125
            PORT_Assert(!xtnData->selectedPsk);
604
125
            for (PRCList *cur_p = PR_LIST_HEAD(&ss->ssl3.hs.psks);
605
125
                 cur_p != &ss->ssl3.hs.psks;
606
125
                 cur_p = PR_NEXT_LINK(cur_p)) {
607
0
                sslPsk *psk = (sslPsk *)cur_p;
608
0
                if (psk->type != ssl_psk_external ||
609
0
                    SECITEM_CompareItem(&psk->label, &label) != SECEqual) {
610
0
                    continue;
611
0
                }
612
0
                PRINT_BUF(50, (ss, "Using External PSK with label",
613
0
                               psk->label.data, psk->label.len));
614
0
                xtnData->selectedPsk = psk;
615
0
            }
616
617
125
            if (!xtnData->selectedPsk) {
618
125
                PRINT_BUF(50, (ss, "Handling PreSharedKey value",
619
125
                               label.data, label.len));
620
125
                rv = ssl3_ProcessSessionTicketCommon(
621
125
                    CONST_CAST(sslSocket, ss), &label, appToken);
622
                /* This only happens if we have an internal error, not
623
                * a malformed ticket. Bogus tickets just don't resume
624
                * and return SECSuccess. */
625
125
                if (rv != SECSuccess) {
626
43
                    return SECFailure;
627
43
                }
628
629
82
                if (ss->sec.ci.sid) {
630
                    /* xtnData->ticketAge contains the baseline we use for
631
                    * calculating the ticket age (i.e., our RTT estimate less the
632
                    * value of ticket_age_add).
633
                    *
634
                    * Add that to the obfuscated ticket age to recover the client's
635
                    * view of the ticket age plus the estimated RTT.
636
                    *
637
                    * See ssl3_EncodeSessionTicket() for details. */
638
65
                    xtnData->ticketAge += obfuscatedAge;
639
640
                    /* We are not committed to resumption until after unwrapping the
641
                    * RMS in tls13_HandleClientHelloPart2. The RPSK will be stored
642
                    * in ss->xtnData.selectedPsk at that point, so continue. */
643
65
                }
644
82
            }
645
125
        }
646
647
108
        ++numIdentities;
648
108
    }
649
650
75
    xtnData->pskBindersLen = data->len;
651
652
    /* Parse the binders list. */
653
75
    rv = ssl3_ExtConsumeHandshakeVariable(ss,
654
75
                                          &inner, 2, &data->data, &data->len);
655
75
    if (rv != SECSuccess)
656
21
        return SECFailure;
657
54
    if (data->len) {
658
4
        goto alert_loser;
659
4
    }
660
661
104
    while (inner.len) {
662
63
        SECItem binder;
663
63
        rv = ssl3_ExtConsumeHandshakeVariable(ss, &binder, 1,
664
63
                                              &inner.data, &inner.len);
665
63
        if (rv != SECSuccess)
666
3
            return rv;
667
60
        if (binder.len < 32) {
668
6
            goto alert_loser;
669
6
        }
670
671
54
        if (!numBinders) {
672
44
            xtnData->pskBinder = binder;
673
44
        }
674
54
        ++numBinders;
675
54
    }
676
677
41
    if (numBinders != numIdentities)
678
1
        goto alert_loser;
679
680
40
    if (ss->statelessResume) {
681
34
        PORT_Assert(!ss->xtnData.selectedPsk);
682
34
    } else if (!xtnData->selectedPsk) {
683
        /* No matching EPSK. */
684
6
        return SECSuccess;
685
6
    }
686
687
34
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_pre_shared_key_xtn;
688
34
    return SECSuccess;
689
690
12
alert_loser:
691
12
    ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
692
12
    PORT_SetError(SSL_ERROR_MALFORMED_PRE_SHARED_KEY);
693
12
    return SECFailure;
694
40
}
695
696
SECStatus
697
tls13_ServerSendPreSharedKeyXtn(const sslSocket *ss, TLSExtensionData *xtnData,
698
                                sslBuffer *buf, PRBool *added)
699
0
{
700
0
    SECStatus rv;
701
702
    /* We only process the first session ticket the client sends,
703
     * so the index is always 0. */
704
0
    rv = sslBuffer_AppendNumber(buf, 0, 2);
705
0
    if (rv != SECSuccess) {
706
0
        return SECFailure;
707
0
    }
708
709
0
    *added = PR_TRUE;
710
0
    return SECSuccess;
711
0
}
712
713
/* Handle a TLS 1.3 PreSharedKey Extension. */
714
SECStatus
715
tls13_ClientHandlePreSharedKeyXtn(const sslSocket *ss, TLSExtensionData *xtnData,
716
                                  SECItem *data)
717
0
{
718
0
    PRUint32 index;
719
0
    SECStatus rv;
720
721
0
    SSL_TRC(3, ("%d: SSL3[%d]: handle pre_shared_key extension",
722
0
                SSL_GETPID(), ss->fd));
723
724
    /* The server must not send this extension when negotiating < TLS 1.3. */
725
0
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
726
0
        PORT_SetError(SSL_ERROR_EXTENSION_DISALLOWED_FOR_VERSION);
727
0
        return SECFailure;
728
0
    }
729
730
0
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &index, 2, &data->data, &data->len);
731
0
    if (rv != SECSuccess)
732
0
        return SECFailure;
733
734
    /* This should be the end of the extension. */
735
0
    if (data->len) {
736
0
        PORT_SetError(SSL_ERROR_MALFORMED_PRE_SHARED_KEY);
737
0
        return SECFailure;
738
0
    }
739
740
    /* We only sent one PSK label so index must be equal to 0 */
741
0
    if (index) {
742
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
743
0
        PORT_SetError(SSL_ERROR_MALFORMED_PRE_SHARED_KEY);
744
0
        return SECFailure;
745
0
    }
746
747
0
    PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks));
748
0
    sslPsk *candidate = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks);
749
750
    /* Check that the server-selected ciphersuite hash and PSK hash match. */
751
0
    if (candidate->hash != tls13_GetHashForCipherSuite(ss->ssl3.hs.cipher_suite)) {
752
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
753
0
        return SECFailure;
754
0
    }
755
756
    /* Keep track of negotiated extensions. */
757
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_pre_shared_key_xtn;
758
0
    xtnData->selectedPsk = candidate;
759
760
0
    return SECSuccess;
761
0
}
762
763
/*
764
 *  struct { } EarlyDataIndication;
765
 */
766
SECStatus
767
tls13_ClientSendEarlyDataXtn(const sslSocket *ss, TLSExtensionData *xtnData,
768
                             sslBuffer *buf, PRBool *added)
769
0
{
770
0
    if (!tls13_ClientAllow0Rtt(ss, ss->sec.ci.sid)) {
771
0
        return SECSuccess;
772
0
    }
773
774
0
    *added = PR_TRUE;
775
0
    return SECSuccess;
776
0
}
777
778
SECStatus
779
tls13_ServerHandleEarlyDataXtn(const sslSocket *ss, TLSExtensionData *xtnData,
780
                               SECItem *data)
781
84
{
782
84
    SSL_TRC(3, ("%d: TLS13[%d]: handle early_data extension",
783
84
                SSL_GETPID(), ss->fd));
784
785
    /* If we are doing < TLS 1.3, then ignore this. */
786
84
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
787
9
        return SECSuccess;
788
9
    }
789
790
75
    if (ss->ssl3.hs.helloRetry) {
791
6
        ssl3_ExtSendAlert(ss, alert_fatal, unsupported_extension);
792
6
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
793
6
        return SECFailure;
794
6
    }
795
796
69
    if (data->len) {
797
2
        PORT_SetError(SSL_ERROR_MALFORMED_EARLY_DATA);
798
2
        return SECFailure;
799
2
    }
800
801
67
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_early_data_xtn;
802
803
67
    return SECSuccess;
804
69
}
805
806
/* This will only be called if we also offered the extension. */
807
SECStatus
808
tls13_ClientHandleEarlyDataXtn(const sslSocket *ss, TLSExtensionData *xtnData,
809
                               SECItem *data)
810
0
{
811
0
    SSL_TRC(3, ("%d: TLS13[%d]: handle early_data extension",
812
0
                SSL_GETPID(), ss->fd));
813
814
    /* The server must not send this extension when negotiating < TLS 1.3. */
815
0
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
816
0
        PORT_SetError(SSL_ERROR_EXTENSION_DISALLOWED_FOR_VERSION);
817
0
        return SECFailure;
818
0
    }
819
820
0
    if (data->len) {
821
0
        PORT_SetError(SSL_ERROR_MALFORMED_EARLY_DATA);
822
0
        return SECFailure;
823
0
    }
824
825
    /* Keep track of negotiated extensions. */
826
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_early_data_xtn;
827
828
0
    return SECSuccess;
829
0
}
830
831
SECStatus
832
tls13_ClientHandleTicketEarlyDataXtn(const sslSocket *ss, TLSExtensionData *xtnData,
833
                                     SECItem *data)
834
0
{
835
0
    PRUint32 utmp;
836
0
    SECStatus rv;
837
838
0
    SSL_TRC(3, ("%d: TLS13[%d]: handle ticket early_data extension",
839
0
                SSL_GETPID(), ss->fd));
840
841
    /* The server must not send this extension when negotiating < TLS 1.3. */
842
0
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
843
0
        PORT_SetError(SSL_ERROR_EXTENSION_DISALLOWED_FOR_VERSION);
844
0
        return SECFailure;
845
0
    }
846
847
0
    rv = ssl3_ExtConsumeHandshake(ss, &utmp, sizeof(utmp),
848
0
                                  &data->data, &data->len);
849
0
    if (rv != SECSuccess) {
850
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET);
851
0
        return SECFailure;
852
0
    }
853
0
    if (data->len) {
854
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET);
855
0
        return SECFailure;
856
0
    }
857
858
0
    xtnData->max_early_data_size = PR_ntohl(utmp);
859
860
0
    return SECSuccess;
861
0
}
862
863
/*
864
 *     struct {
865
 *       select (Handshake.msg_type) {
866
 *       case client_hello:
867
 *          ProtocolVersion versions<2..254>;
868
 *       case server_hello:
869
 *          ProtocolVersion version;
870
 *       };
871
 *     } SupportedVersions;
872
 */
873
SECStatus
874
tls13_ClientSendSupportedVersionsXtn(const sslSocket *ss, TLSExtensionData *xtnData,
875
                                     sslBuffer *buf, PRBool *added)
876
0
{
877
0
    PRUint16 version;
878
0
    unsigned int lengthOffset;
879
0
    SECStatus rv;
880
881
0
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) {
882
0
        return SECSuccess;
883
0
    }
884
885
0
    SSL_TRC(3, ("%d: TLS13[%d]: client send supported_versions extension",
886
0
                SSL_GETPID(), ss->fd));
887
888
0
    rv = sslBuffer_Skip(buf, 1, &lengthOffset);
889
0
    if (rv != SECSuccess) {
890
0
        return SECFailure;
891
0
    }
892
893
0
    PORT_Assert(!ss->ssl3.hs.echHpkeCtx || ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3);
894
0
    for (version = ss->vrange.max; version >= ss->vrange.min; --version) {
895
0
        PRUint16 wire = tls13_EncodeVersion(version,
896
0
                                            ss->protocolVariant);
897
0
        rv = sslBuffer_AppendNumber(buf, wire, 2);
898
0
        if (rv != SECSuccess) {
899
0
            return SECFailure;
900
0
        }
901
902
0
        if (ss->opt.enableDtls13VersionCompat &&
903
0
            ss->protocolVariant == ssl_variant_datagram) {
904
0
            switch (version) {
905
0
                case SSL_LIBRARY_VERSION_TLS_1_2:
906
0
                case SSL_LIBRARY_VERSION_TLS_1_1:
907
0
                    rv = sslBuffer_AppendNumber(buf, (PRUint16)version, 2);
908
0
                    break;
909
0
                default:
910
0
                    continue;
911
0
            }
912
0
            if (rv != SECSuccess) {
913
0
                return SECFailure;
914
0
            }
915
0
        }
916
0
    }
917
918
    /* GREASE SupportedVersions:
919
     * A client MAY select one or more GREASE version values and advertise them
920
     * in the "supported_versions" extension, if sent [RFC8701, Section 3.1]. */
921
0
    if (ss->opt.enableGrease) {
922
0
        rv = sslBuffer_AppendNumber(buf, ss->ssl3.hs.grease->idx[grease_version], 2);
923
0
        if (rv != SECSuccess) {
924
0
            return SECFailure;
925
0
        }
926
0
    }
927
928
0
    rv = sslBuffer_InsertLength(buf, lengthOffset, 1);
929
0
    if (rv != SECSuccess) {
930
0
        return SECFailure;
931
0
    }
932
933
0
    *added = PR_TRUE;
934
0
    return SECSuccess;
935
0
}
936
937
SECStatus
938
tls13_ServerSendSupportedVersionsXtn(const sslSocket *ss, TLSExtensionData *xtnData,
939
                                     sslBuffer *buf, PRBool *added)
940
545
{
941
545
    SECStatus rv;
942
943
545
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
944
0
        return SECSuccess;
945
0
    }
946
947
545
    SSL_TRC(3, ("%d: TLS13[%d]: server send supported_versions extension",
948
545
                SSL_GETPID(), ss->fd));
949
950
545
    PRUint16 ver = tls13_EncodeVersion(SSL_LIBRARY_VERSION_TLS_1_3,
951
545
                                       ss->protocolVariant);
952
545
    rv = sslBuffer_AppendNumber(buf, ver, 2);
953
545
    if (rv != SECSuccess) {
954
0
        return SECFailure;
955
0
    }
956
957
545
    *added = PR_TRUE;
958
545
    return SECSuccess;
959
545
}
960
961
/*
962
 *    struct {
963
 *        opaque cookie<1..2^16-1>;
964
 *    } Cookie;
965
 */
966
SECStatus
967
tls13_ClientHandleHrrCookie(const sslSocket *ss, TLSExtensionData *xtnData,
968
                            SECItem *data)
969
0
{
970
0
    SECStatus rv;
971
972
0
    SSL_TRC(3, ("%d: TLS13[%d]: handle cookie extension",
973
0
                SSL_GETPID(), ss->fd));
974
975
0
    PORT_Assert(ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3);
976
977
    /* IMPORTANT: this is only valid while the HelloRetryRequest is still valid. */
978
0
    rv = ssl3_ExtConsumeHandshakeVariable(
979
0
        ss, &CONST_CAST(sslSocket, ss)->ssl3.hs.cookie, 2,
980
0
        &data->data, &data->len);
981
0
    if (rv != SECSuccess) {
982
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_RETRY_REQUEST);
983
0
        return SECFailure;
984
0
    }
985
0
    if (!ss->ssl3.hs.cookie.len || data->len) {
986
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
987
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HELLO_RETRY_REQUEST);
988
0
        return SECFailure;
989
0
    }
990
991
0
    return SECSuccess;
992
0
}
993
994
SECStatus
995
tls13_ClientSendHrrCookieXtn(const sslSocket *ss, TLSExtensionData *xtnData,
996
                             sslBuffer *buf, PRBool *added)
997
0
{
998
0
    SECStatus rv;
999
1000
0
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3 ||
1001
0
        !ss->ssl3.hs.cookie.len) {
1002
0
        return SECSuccess;
1003
0
    }
1004
1005
0
    SSL_TRC(3, ("%d: TLS13[%d]: send cookie extension", SSL_GETPID(), ss->fd));
1006
0
    rv = sslBuffer_AppendVariable(buf, ss->ssl3.hs.cookie.data,
1007
0
                                  ss->ssl3.hs.cookie.len, 2);
1008
0
    if (rv != SECSuccess) {
1009
0
        return SECFailure;
1010
0
    }
1011
1012
0
    *added = PR_TRUE;
1013
0
    return SECSuccess;
1014
0
}
1015
1016
SECStatus
1017
tls13_ServerHandleCookieXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1018
                            SECItem *data)
1019
43
{
1020
43
    SECStatus rv;
1021
1022
43
    SSL_TRC(3, ("%d: TLS13[%d]: handle cookie extension",
1023
43
                SSL_GETPID(), ss->fd));
1024
1025
43
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &xtnData->cookie, 2,
1026
43
                                          &data->data, &data->len);
1027
43
    if (rv != SECSuccess) {
1028
3
        return SECFailure;
1029
3
    }
1030
1031
40
    if (xtnData->cookie.len == 0) {
1032
1
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1033
1
        return SECFailure;
1034
1
    }
1035
1036
39
    if (data->len) {
1037
5
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1038
5
        return SECFailure;
1039
5
    }
1040
1041
    /* Keep track of negotiated extensions. */
1042
34
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_cookie_xtn;
1043
1044
34
    return SECSuccess;
1045
39
}
1046
1047
SECStatus
1048
tls13_ClientSendPostHandshakeAuthXtn(const sslSocket *ss,
1049
                                     TLSExtensionData *xtnData,
1050
                                     sslBuffer *buf, PRBool *added)
1051
0
{
1052
    /* Only one post-handshake message is supported: a single
1053
     * NST immediately following the client Finished. */
1054
0
    if (!IS_DTLS(ss)) {
1055
0
        SSL_TRC(3, ("%d: TLS13[%d]: send post_handshake_auth extension",
1056
0
                    SSL_GETPID(), ss->fd));
1057
0
        *added = ss->opt.enablePostHandshakeAuth;
1058
0
    }
1059
0
    return SECSuccess;
1060
0
}
1061
1062
SECStatus
1063
tls13_ServerHandlePostHandshakeAuthXtn(const sslSocket *ss,
1064
                                       TLSExtensionData *xtnData,
1065
                                       SECItem *data)
1066
78
{
1067
78
    SSL_TRC(3, ("%d: TLS13[%d]: handle post_handshake_auth extension",
1068
78
                SSL_GETPID(), ss->fd));
1069
1070
78
    if (data->len) {
1071
4
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1072
4
        return SECFailure;
1073
4
    }
1074
1075
    /* Only one post-handshake message is supported: a single
1076
     * NST immediately following the client Finished. */
1077
74
    if (!IS_DTLS(ss)) {
1078
        /* Keep track of negotiated extensions. */
1079
74
        xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_post_handshake_auth_xtn;
1080
74
    }
1081
1082
74
    return SECSuccess;
1083
78
}
1084
1085
/*
1086
 *     enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
1087
 *
1088
 *     struct {
1089
 *         PskKeyExchangeMode ke_modes<1..255>;
1090
 *     } PskKeyExchangeModes;
1091
 */
1092
SECStatus
1093
tls13_ClientSendPskModesXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1094
                            sslBuffer *buf, PRBool *added)
1095
0
{
1096
0
    SECStatus rv;
1097
1098
0
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3 ||
1099
0
        ss->opt.noCache) {
1100
0
        return SECSuccess;
1101
0
    }
1102
1103
0
    SSL_TRC(3, ("%d: TLS13[%d]: send psk key exchange modes extension",
1104
0
                SSL_GETPID(), ss->fd));
1105
1106
    /* GREASE PskKeyExchangeMode:
1107
     * A client MAY select one or more GREASE PskKeyExchangeMode values and
1108
     * advertise them in the "psk_key_exchange_modes" extension, if sent
1109
     * [RFC8701, Section 3.1]. */
1110
0
    if (ss->opt.enableGrease) {
1111
0
        rv = sslBuffer_AppendVariable(buf, (PRUint8[]){ tls13_psk_dh_ke, ss->ssl3.hs.grease->pskKem }, 2, 1);
1112
0
    } else {
1113
0
        rv = sslBuffer_AppendVariable(buf, (PRUint8[]){ tls13_psk_dh_ke }, 1, 1);
1114
0
    }
1115
0
    if (rv != SECSuccess) {
1116
0
        return SECFailure;
1117
0
    }
1118
1119
0
    *added = PR_TRUE;
1120
0
    return SECSuccess;
1121
0
}
1122
1123
SECStatus
1124
tls13_ServerHandlePskModesXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1125
                              SECItem *data)
1126
115
{
1127
115
    SECStatus rv;
1128
1129
    /* If we are doing < TLS 1.3, then ignore this. */
1130
115
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
1131
26
        return SECSuccess;
1132
26
    }
1133
1134
89
    SSL_TRC(3, ("%d: TLS13[%d]: handle PSK key exchange modes extension",
1135
89
                SSL_GETPID(), ss->fd));
1136
1137
    /* IMPORTANT: We aren't copying these values, just setting pointers.
1138
     * They will only be valid as long as the ClientHello is in memory. */
1139
89
    rv = ssl3_ExtConsumeHandshakeVariable(ss,
1140
89
                                          &xtnData->psk_ke_modes, 1,
1141
89
                                          &data->data, &data->len);
1142
89
    if (rv != SECSuccess)
1143
1
        return rv;
1144
88
    if (!xtnData->psk_ke_modes.len || data->len) {
1145
2
        PORT_SetError(SSL_ERROR_MALFORMED_PSK_KEY_EXCHANGE_MODES);
1146
2
        return SECFailure;
1147
2
    }
1148
1149
    /* Keep track of negotiated extensions. */
1150
86
    xtnData->negotiated[xtnData->numNegotiated++] =
1151
86
        ssl_tls13_psk_key_exchange_modes_xtn;
1152
1153
86
    return SECSuccess;
1154
88
}
1155
1156
SECStatus
1157
tls13_SendCertAuthoritiesXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1158
                             sslBuffer *buf, PRBool *added)
1159
336
{
1160
336
    unsigned int calen;
1161
336
    const SECItem *name;
1162
336
    unsigned int nnames;
1163
336
    SECStatus rv;
1164
1165
336
    PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3);
1166
1167
336
    rv = ssl_GetCertificateRequestCAs(ss, &calen, &name, &nnames);
1168
336
    if (rv != SECSuccess) {
1169
0
        return SECFailure;
1170
0
    }
1171
1172
336
    if (!calen) {
1173
336
        return SECSuccess;
1174
336
    }
1175
1176
0
    rv = sslBuffer_AppendNumber(buf, calen, 2);
1177
0
    if (rv != SECSuccess) {
1178
0
        return SECFailure;
1179
0
    }
1180
1181
0
    while (nnames) {
1182
0
        rv = sslBuffer_AppendVariable(buf, name->data, name->len, 2);
1183
0
        if (rv != SECSuccess) {
1184
0
            return SECFailure;
1185
0
        }
1186
0
        ++name;
1187
0
        --nnames;
1188
0
    }
1189
1190
0
    *added = PR_TRUE;
1191
0
    return SECSuccess;
1192
0
}
1193
1194
SECStatus
1195
tls13_ClientHandleCertAuthoritiesXtn(const sslSocket *ss,
1196
                                     TLSExtensionData *xtnData,
1197
                                     SECItem *data)
1198
0
{
1199
0
    SECStatus rv;
1200
0
    PLArenaPool *arena;
1201
1202
0
    if (!data->len) {
1203
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1204
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CERT_REQUEST);
1205
0
        return SECFailure;
1206
0
    }
1207
1208
0
    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
1209
0
    if (!arena) {
1210
0
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1211
0
        return SECFailure;
1212
0
    }
1213
1214
0
    xtnData->certReqAuthorities.arena = arena;
1215
0
    rv = ssl3_ParseCertificateRequestCAs((sslSocket *)ss,
1216
0
                                         &data->data, &data->len,
1217
0
                                         &xtnData->certReqAuthorities);
1218
0
    if (rv != SECSuccess) {
1219
0
        goto loser;
1220
0
    }
1221
0
    if (data->len) {
1222
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1223
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CERT_REQUEST);
1224
0
        goto loser;
1225
0
    }
1226
0
    return SECSuccess;
1227
1228
0
loser:
1229
0
    PORT_FreeArena(arena, PR_FALSE);
1230
0
    xtnData->certReqAuthorities.arena = NULL;
1231
0
    return SECFailure;
1232
0
}
1233
1234
SECStatus
1235
tls13_ServerHandleCertAuthoritiesXtn(const sslSocket *ss, TLSExtensionData *xtnData, SECItem *data)
1236
74
{
1237
74
    SSL_TRC(3, ("%d: TLS13[%d]: ignore certificate_authorities extension",
1238
74
                SSL_GETPID(), ss->fd));
1239
    /* NSS ignores certificate_authorities in the ClientHello */
1240
74
    return SECSuccess;
1241
74
}
1242
1243
SECStatus
1244
tls13_ServerSendHrrKeyShareXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1245
                               sslBuffer *buf, PRBool *added)
1246
122
{
1247
122
    SECStatus rv;
1248
1249
122
    PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3);
1250
1251
122
    if (!xtnData->selectedGroup) {
1252
0
        return SECSuccess;
1253
0
    }
1254
1255
122
    rv = sslBuffer_AppendNumber(buf, xtnData->selectedGroup->name, 2);
1256
122
    if (rv != SECSuccess) {
1257
0
        return SECFailure;
1258
0
    }
1259
1260
122
    *added = PR_TRUE;
1261
122
    return SECSuccess;
1262
122
}
1263
1264
SECStatus
1265
tls13_ServerSendHrrCookieXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1266
                             sslBuffer *buf, PRBool *added)
1267
122
{
1268
122
    SECStatus rv;
1269
1270
122
    PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3);
1271
122
    PORT_Assert(xtnData->cookie.len > 0);
1272
1273
122
    rv = sslBuffer_AppendVariable(buf,
1274
122
                                  xtnData->cookie.data, xtnData->cookie.len, 2);
1275
122
    if (rv != SECSuccess) {
1276
0
        return SECFailure;
1277
0
    }
1278
1279
122
    *added = PR_TRUE;
1280
122
    return SECSuccess;
1281
122
}
1282
1283
SECStatus
1284
tls13_ClientHandleHrrEchXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1285
                            SECItem *data)
1286
0
{
1287
0
    if (data->len != TLS13_ECH_SIGNAL_LEN) {
1288
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1289
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_EXTENSION);
1290
0
        return SECFailure;
1291
0
    }
1292
0
    if (!ssl3_ExtensionAdvertised(ss, ssl_tls13_encrypted_client_hello_xtn)) {
1293
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1294
0
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1295
0
        return SECFailure;
1296
0
    }
1297
0
    if (!ss->ssl3.hs.echHpkeCtx) {
1298
0
        SSL_TRC(50, ("%d: TLS13[%d]: client received GREASEd ECH confirmation",
1299
0
                     SSL_GETPID(), ss->fd));
1300
0
        return SECSuccess;
1301
0
    }
1302
0
    SSL_TRC(50, ("%d: TLS13[%d]: client received HRR ECH confirmation",
1303
0
                 SSL_GETPID(), ss->fd));
1304
0
    PORT_Assert(!xtnData->ech);
1305
0
    xtnData->ech = PORT_ZNew(sslEchXtnState);
1306
0
    if (!xtnData->ech) {
1307
0
        return SECFailure;
1308
0
    }
1309
0
    xtnData->ech->hrrConfirmation = data->data;
1310
0
    return SECSuccess;
1311
0
}
1312
1313
SECStatus
1314
tls13_ClientHandleEchXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1315
                         SECItem *data)
1316
0
{
1317
0
    SECStatus rv;
1318
0
    PRCList parsedConfigs;
1319
0
    PR_INIT_CLIST(&parsedConfigs);
1320
1321
    /* The [retry config] response is valid only when the server used the
1322
     * ClientHelloOuter. If the server sent this extension in response to the
1323
     * inner variant [ECH was accepted], then the client MUST abort with an
1324
     * "unsupported_extension" alert [draft-ietf-tls-esni-14, Section 5]. */
1325
0
    if (ss->ssl3.hs.echAccepted) {
1326
0
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1327
0
        ssl3_ExtSendAlert(ss, alert_fatal, unsupported_extension);
1328
0
        return SECFailure;
1329
0
    }
1330
1331
    /* If the server is configured with any ECHConfigs, it MUST include the
1332
     * "encrypted_client_hello" extension in its EncryptedExtensions with the
1333
     * "retry_configs" field set to one or more ECHConfig structures with
1334
     * up-to-date keys [draft-ietf-tls-esni-14, Section 7.1]. */
1335
0
    if (ss->ssl3.hs.msg_type != ssl_hs_encrypted_extensions) {
1336
0
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1337
0
        if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
1338
            /* For TLS < 1.3 the extension is unkown/unsupported. */
1339
0
            ssl3_ExtSendAlert(ss, alert_fatal, unsupported_extension);
1340
0
        } else {
1341
            /* For TLS 1.3 the extension is known but prohibited outside EE
1342
             * (see RFC8446, Section 4.2 for alert rationale). */
1343
0
            ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1344
0
        }
1345
0
        return SECFailure;
1346
0
    }
1347
1348
0
    PORT_Assert(!xtnData->ech);
1349
0
    xtnData->ech = PORT_ZNew(sslEchXtnState);
1350
0
    if (!xtnData->ech) {
1351
0
        return SECFailure;
1352
0
    }
1353
1354
    /* Parse the list to determine 1) That the configs are valid
1355
     * and properly encoded, and 2) If any are compatible. */
1356
0
    rv = tls13_DecodeEchConfigs(data, &parsedConfigs);
1357
0
    if (rv == SECFailure) {
1358
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1359
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_CONFIG);
1360
0
        return SECFailure;
1361
0
    }
1362
    /* Don't mark ECH negotiated on rejection with retry_config.
1363
     * Save the the raw configs so the application can retry. If
1364
     * we sent GREASE ECH (no echHpkeCtx), don't apply retry_configs. */
1365
0
    if (ss->ssl3.hs.echHpkeCtx && !PR_CLIST_IS_EMPTY(&parsedConfigs)) {
1366
0
        rv = SECITEM_CopyItem(NULL, &xtnData->ech->retryConfigs, data);
1367
0
    }
1368
0
    tls13_DestroyEchConfigs(&parsedConfigs);
1369
1370
0
    return rv;
1371
0
}
1372
1373
/* Indicates support for the delegated credentials extension. This should be
1374
 * hooked while processing the ClientHello. */
1375
SECStatus
1376
tls13_ClientSendDelegatedCredentialsXtn(const sslSocket *ss,
1377
                                        TLSExtensionData *xtnData,
1378
                                        sslBuffer *buf, PRBool *added)
1379
0
{
1380
    /* Only send the extension if support is enabled and the client can
1381
     * negotiate TLS 1.3. */
1382
0
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3 ||
1383
0
        !ss->opt.enableDelegatedCredentials) {
1384
0
        return SECSuccess;
1385
0
    }
1386
1387
    /* Filter the schemes that are enabled and acceptable. Save these in
1388
     * the "advertised" list, then encode them to be sent. If we receive
1389
     * a DC in response, validate that it matches one of the advertised
1390
     * schemes. */
1391
0
    SSLSignatureScheme filtered[MAX_SIGNATURE_SCHEMES] = { 0 };
1392
0
    unsigned int filteredCount = 0;
1393
0
    SECStatus rv = ssl3_FilterSigAlgs(ss, ss->vrange.max,
1394
0
                                      PR_TRUE /* disableRsae */,
1395
0
                                      PR_FALSE /* forCert */,
1396
0
                                      MAX_SIGNATURE_SCHEMES,
1397
0
                                      filtered,
1398
0
                                      &filteredCount);
1399
0
    if (rv != SECSuccess) {
1400
0
        return SECFailure;
1401
0
    }
1402
1403
    /* If no schemes available for the DC extension, don't send it. */
1404
0
    if (!filteredCount) {
1405
0
        return SECSuccess;
1406
0
    }
1407
1408
0
    rv = ssl3_EncodeFilteredSigAlgs(ss, filtered, filteredCount,
1409
0
                                    PR_FALSE /* GREASE */, buf);
1410
0
    if (rv != SECSuccess) {
1411
0
        return SECFailure;
1412
0
    }
1413
1414
0
    SSLSignatureScheme *dcSchemesAdvertised = PORT_ZNewArray(SSLSignatureScheme,
1415
0
                                                             filteredCount);
1416
0
    if (!dcSchemesAdvertised) {
1417
0
        return SECFailure;
1418
0
    }
1419
0
    for (unsigned int i = 0; i < filteredCount; i++) {
1420
0
        dcSchemesAdvertised[i] = filtered[i];
1421
0
    }
1422
1423
0
    if (xtnData->delegCredSigSchemesAdvertised) {
1424
0
        PORT_Free(xtnData->delegCredSigSchemesAdvertised);
1425
0
    }
1426
0
    xtnData->delegCredSigSchemesAdvertised = dcSchemesAdvertised;
1427
0
    xtnData->numDelegCredSigSchemesAdvertised = filteredCount;
1428
0
    *added = PR_TRUE;
1429
0
    return SECSuccess;
1430
0
}
1431
1432
/* Parses the delegated credential (DC) offered by the server. This should be
1433
 * hooked while processing the server's CertificateVerify.
1434
 *
1435
 * Only the DC sent with the end-entity certificate is to be parsed. This is
1436
 * ensured by |tls13_HandleCertificateEntry|, which only processes extensions
1437
 * for the first certificate in the chain.
1438
 */
1439
SECStatus
1440
tls13_ClientHandleDelegatedCredentialsXtn(const sslSocket *ss,
1441
                                          TLSExtensionData *xtnData,
1442
                                          SECItem *data)
1443
0
{
1444
0
    if (!ss->opt.enableDelegatedCredentials ||
1445
0
        ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
1446
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1447
0
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1448
0
        return SECFailure;
1449
0
    }
1450
1451
0
    sslDelegatedCredential *dc = NULL;
1452
0
    SECStatus rv = tls13_ReadDelegatedCredential(data->data, data->len, &dc);
1453
0
    if (rv != SECSuccess) {
1454
0
        goto loser; /* code already set */
1455
0
    }
1456
1457
    /* When using RSA, the public key MUST NOT use the rsaEncryption OID. */
1458
0
    if (dc->expectedCertVerifyAlg == ssl_sig_rsa_pss_rsae_sha256 ||
1459
0
        dc->expectedCertVerifyAlg == ssl_sig_rsa_pss_rsae_sha384 ||
1460
0
        dc->expectedCertVerifyAlg == ssl_sig_rsa_pss_rsae_sha512) {
1461
0
        goto alert_loser;
1462
0
    }
1463
1464
    /* The algorithm and expected_cert_verify_algorithm fields MUST be of a
1465
     * type advertised by the client in the SignatureSchemeList and are
1466
     * considered invalid otherwise.  Clients that receive invalid delegated
1467
     * credentials MUST terminate the connection with an "illegal_parameter"
1468
     * alert. */
1469
0
    PRBool found = PR_FALSE;
1470
0
    for (unsigned int i = 0; i < ss->xtnData.numDelegCredSigSchemesAdvertised; ++i) {
1471
0
        if (dc->expectedCertVerifyAlg == ss->xtnData.delegCredSigSchemesAdvertised[i]) {
1472
0
            found = PR_TRUE;
1473
0
            break;
1474
0
        }
1475
0
    }
1476
0
    if (found == PR_FALSE) {
1477
0
        goto alert_loser;
1478
0
    }
1479
1480
    // Check the dc->alg, if necessary.
1481
0
    if (dc->alg != dc->expectedCertVerifyAlg) {
1482
0
        found = PR_FALSE;
1483
0
        for (unsigned int i = 0; i < ss->xtnData.numDelegCredSigSchemesAdvertised; ++i) {
1484
0
            if (dc->alg == ss->xtnData.delegCredSigSchemesAdvertised[i]) {
1485
0
                found = PR_TRUE;
1486
0
                break;
1487
0
            }
1488
0
        }
1489
0
        if (found == PR_FALSE) {
1490
0
            goto alert_loser;
1491
0
        }
1492
0
    }
1493
1494
0
    xtnData->peerDelegCred = dc;
1495
0
    xtnData->negotiated[xtnData->numNegotiated++] =
1496
0
        ssl_delegated_credentials_xtn;
1497
0
    return SECSuccess;
1498
0
alert_loser:
1499
0
    ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1500
0
    PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM);
1501
0
loser:
1502
0
    tls13_DestroyDelegatedCredential(dc);
1503
0
    return SECFailure;
1504
0
}
1505
1506
/* Adds the DC extension if we're committed to authenticating with a DC. */
1507
static SECStatus
1508
tls13_ServerSendDelegatedCredentialsXtn(const sslSocket *ss,
1509
                                        TLSExtensionData *xtnData,
1510
                                        sslBuffer *buf, PRBool *added)
1511
2
{
1512
2
    if (tls13_IsSigningWithDelegatedCredential(ss)) {
1513
0
        const SECItem *dc = &ss->sec.serverCert->delegCred;
1514
0
        SECStatus rv;
1515
0
        rv = sslBuffer_Append(buf, dc->data, dc->len);
1516
0
        if (rv != SECSuccess) {
1517
0
            return SECFailure;
1518
0
        }
1519
0
        *added = PR_TRUE;
1520
0
    }
1521
2
    return SECSuccess;
1522
2
}
1523
1524
/* The client has indicated support of DCs. We can't act on this information
1525
 * until we've committed to signing with a DC, so just set a callback for
1526
 * sending the DC extension later. */
1527
SECStatus
1528
tls13_ServerHandleDelegatedCredentialsXtn(const sslSocket *ss,
1529
                                          TLSExtensionData *xtnData,
1530
                                          SECItem *data)
1531
72
{
1532
72
    if (xtnData->delegCredSigSchemes) {
1533
0
        PORT_Free(xtnData->delegCredSigSchemes);
1534
0
        xtnData->delegCredSigSchemes = NULL;
1535
0
        xtnData->numDelegCredSigSchemes = 0;
1536
0
    }
1537
72
    SECStatus rv = ssl_ParseSignatureSchemes(ss, NULL,
1538
72
                                             &xtnData->delegCredSigSchemes,
1539
72
                                             &xtnData->numDelegCredSigSchemes,
1540
72
                                             &data->data, &data->len);
1541
72
    if (rv != SECSuccess) {
1542
2
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1543
2
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1544
2
        return SECFailure;
1545
2
    }
1546
70
    if (xtnData->numDelegCredSigSchemes == 0) {
1547
23
        ssl3_ExtSendAlert(ss, alert_fatal, handshake_failure);
1548
23
        PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM);
1549
23
        return SECFailure;
1550
23
    }
1551
    /* Check for trailing data. */
1552
47
    if (data->len != 0) {
1553
22
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1554
22
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1555
22
        return SECFailure;
1556
22
    }
1557
1558
    /* Keep track of negotiated extensions. */
1559
25
    xtnData->peerRequestedDelegCred = PR_TRUE;
1560
25
    xtnData->negotiated[xtnData->numNegotiated++] =
1561
25
        ssl_delegated_credentials_xtn;
1562
1563
25
    return ssl3_RegisterExtensionSender(
1564
25
        ss, xtnData, ssl_delegated_credentials_xtn,
1565
25
        tls13_ServerSendDelegatedCredentialsXtn);
1566
47
}
1567
1568
/* Adds the ECH extension containing server retry_configs */
1569
SECStatus
1570
tls13_ServerSendEchXtn(const sslSocket *ss,
1571
                       TLSExtensionData *xtnData,
1572
                       sslBuffer *buf, PRBool *added)
1573
0
{
1574
0
    SECStatus rv;
1575
0
    PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3);
1576
0
    if (PR_CLIST_IS_EMPTY(&ss->echConfigs)) {
1577
0
        return SECSuccess;
1578
0
    }
1579
1580
0
    const sslEchConfig *cfg = (sslEchConfig *)PR_LIST_HEAD(&ss->echConfigs);
1581
0
    rv = sslBuffer_AppendVariable(buf, cfg->raw.data, cfg->raw.len, 2);
1582
0
    if (rv != SECSuccess) {
1583
0
        return SECFailure;
1584
0
    }
1585
1586
0
    *added = PR_TRUE;
1587
0
    return SECSuccess;
1588
0
}
1589
1590
/* If an ECH server sends the HRR ECH extension after it accepted ECH, the
1591
 * extension's payload must be set to 8 zero bytes, these are overwritten with
1592
 * the accept_confirmation value after the required transcript calculation.
1593
 * If a client-facing/shared-mode server did not accept ECH when offered in CH
1594
 * or if ECH GREASE is enabled on the server and a ECH extension was received,
1595
 * a 8 byte random value is set as the extension's payload
1596
 * [draft-ietf-tls-esni-14, Section 7].
1597
 *
1598
 * Depending on the acceptance of ECH, zero or random bytes are written to
1599
 * ss->ssl3.hs.greaseEchBuf.buf in tls13con.c/tls13_SendHelloRetryRequest(). */
1600
SECStatus
1601
tls13_ServerSendHrrEchXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1602
                          sslBuffer *buf, PRBool *added)
1603
122
{
1604
122
    SECStatus rv;
1605
    /* Do not send HRR ECH extension if TLS < 1.3 was negotiated OR no ECH
1606
     * extension was received OR the server is NOT in any ECH server mode AND
1607
     * ECH GREASE is NOT enabled. */
1608
122
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3 ||
1609
122
        !xtnData->ech ||
1610
122
        (!ss->echPubKey && !ss->opt.enableTls13BackendEch && !ss->opt.enableTls13GreaseEch)) {
1611
122
        SSL_TRC(100, ("%d: TLS13[%d]: server not sending HRR ECH Xtn",
1612
122
                      SSL_GETPID(), ss->fd));
1613
122
        return SECSuccess;
1614
122
    }
1615
0
    SSL_TRC(100, ("%d: TLS13[%d]: server sending HRR ECH Xtn",
1616
0
                  SSL_GETPID(), ss->fd));
1617
0
    PR_ASSERT(SSL_BUFFER_LEN(&ss->ssl3.hs.greaseEchBuf) == TLS13_ECH_SIGNAL_LEN);
1618
0
    PRINT_BUF(100, (ss, "grease_ech_confirmation", ss->ssl3.hs.greaseEchBuf.buf, TLS13_ECH_SIGNAL_LEN));
1619
0
    rv = sslBuffer_AppendBuffer(buf, &ss->ssl3.hs.greaseEchBuf);
1620
0
    if (rv != SECSuccess) {
1621
0
        return SECFailure;
1622
0
    }
1623
0
    *added = PR_TRUE;
1624
0
    return SECSuccess;
1625
0
}
1626
1627
SECStatus
1628
tls13_ServerHandleInnerEchXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1629
                              SECItem *data)
1630
0
{
1631
0
    PRUint64 xtn_type;
1632
0
    sslReader xtnReader = SSL_READER(data->data, data->len);
1633
1634
0
    PR_ASSERT(ss->ssl3.hs.echAccepted || ss->opt.enableTls13BackendEch);
1635
0
    PR_ASSERT(!xtnData->ech->receivedInnerXtn);
1636
1637
0
    SECStatus rv = sslRead_ReadNumber(&xtnReader, 1, &xtn_type);
1638
0
    if (rv != SECSuccess) {
1639
0
        goto alert_loser;
1640
0
    }
1641
0
    if (xtn_type != ech_xtn_type_inner) {
1642
0
        goto alert_loser;
1643
0
    }
1644
0
    if (SSL_READER_REMAINING(&xtnReader)) {
1645
        /* Inner ECH Extension must contain only type enum */
1646
0
        goto alert_loser;
1647
0
    }
1648
1649
0
    xtnData->ech->receivedInnerXtn = PR_TRUE;
1650
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_tls13_encrypted_client_hello_xtn;
1651
0
    return SECSuccess;
1652
1653
0
alert_loser:
1654
0
    ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1655
0
    PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_EXTENSION);
1656
0
    return SECFailure;
1657
0
}
1658
1659
SECStatus
1660
tls13_ServerHandleOuterEchXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1661
                              SECItem *data)
1662
62
{
1663
62
    SECStatus rv;
1664
62
    HpkeKdfId kdf;
1665
62
    HpkeAeadId aead;
1666
62
    PRUint32 tmp;
1667
62
    PRUint8 configId;
1668
62
    SECItem senderPubKey;
1669
62
    SECItem encryptedCh;
1670
1671
62
    PRUint32 xtn_type;
1672
62
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &xtn_type, 1, &data->data, &data->len);
1673
62
    if (rv != SECSuccess) {
1674
1
        goto alert_loser;
1675
1
    }
1676
61
    if (xtn_type != ech_xtn_type_outer && xtn_type != ech_xtn_type_inner) {
1677
3
        SSL_TRC(3, ("%d: TLS13[%d]: unexpected ECH extension type in client hello outer, alert",
1678
3
                    SSL_GETPID(), ss->fd));
1679
3
        goto alert_loser;
1680
3
    }
1681
    /* If we are operating in shared mode, we can accept an inner xtn in the ClientHelloOuter */
1682
58
    if (xtn_type == ech_xtn_type_inner) {
1683
1
        if (!ss->opt.enableTls13BackendEch) {
1684
1
            ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1685
1
            PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1686
1
            return SECFailure;
1687
1
        }
1688
0
        PORT_Assert(!xtnData->ech);
1689
0
        xtnData->ech = PORT_ZNew(sslEchXtnState);
1690
0
        if (!xtnData->ech) {
1691
0
            return SECFailure;
1692
0
        }
1693
        /* We have to rewind the buffer advanced by ssl3_ExtConsumeHandshakeNumber */
1694
0
        data->data--;
1695
0
        data->len++;
1696
0
        return tls13_ServerHandleInnerEchXtn(ss, xtnData, data);
1697
0
    }
1698
57
    if (ss->ssl3.hs.echAccepted) {
1699
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1700
0
        PORT_SetError(SSL_ERROR_RX_UNEXPECTED_EXTENSION);
1701
0
        return SECFailure;
1702
0
    }
1703
1704
57
    SSL_TRC(3, ("%d: TLS13[%d]: handle outer ECH extension",
1705
57
                SSL_GETPID(), ss->fd));
1706
1707
57
    PORT_Assert(!xtnData->ech);
1708
57
    xtnData->ech = PORT_ZNew(sslEchXtnState);
1709
57
    if (!xtnData->ech) {
1710
0
        return SECFailure;
1711
0
    }
1712
1713
    /* Parse the KDF and AEAD. */
1714
57
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &tmp, 2,
1715
57
                                        &data->data, &data->len);
1716
57
    if (rv != SECSuccess) {
1717
1
        goto alert_loser;
1718
1
    }
1719
56
    kdf = (HpkeKdfId)tmp;
1720
56
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &tmp, 2,
1721
56
                                        &data->data, &data->len);
1722
56
    if (rv != SECSuccess) {
1723
1
        goto alert_loser;
1724
1
    }
1725
55
    aead = (HpkeAeadId)tmp;
1726
1727
    /* config_id */
1728
55
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &tmp, 1,
1729
55
                                        &data->data, &data->len);
1730
55
    if (rv != SECSuccess) {
1731
0
        goto alert_loser;
1732
0
    }
1733
55
    configId = tmp;
1734
1735
    /* enc */
1736
55
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &senderPubKey, 2,
1737
55
                                          &data->data, &data->len);
1738
55
    if (rv != SECSuccess) {
1739
2
        goto alert_loser;
1740
2
    }
1741
1742
    /* payload, which must be final and non-empty. */
1743
53
    xtnData->ech->payloadStart = data->data + 2; /* Move past length */
1744
53
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &encryptedCh, 2,
1745
53
                                          &data->data, &data->len);
1746
53
    if (rv != SECSuccess) {
1747
1
        goto alert_loser;
1748
1
    }
1749
52
    if (data->len || !encryptedCh.len) {
1750
2
        goto alert_loser;
1751
2
    }
1752
1753
50
    if (!ss->ssl3.hs.helloRetry) {
1754
        /* In the real ECH HRR case, config_id and enc should be empty. This
1755
         * is checked after acceptance, because it might be GREASE ECH. */
1756
8
        if (!senderPubKey.len) {
1757
1
            goto alert_loser;
1758
1
        }
1759
1760
7
        rv = SECITEM_CopyItem(NULL, &xtnData->ech->senderPubKey, &senderPubKey);
1761
7
        if (rv == SECFailure) {
1762
0
            return SECFailure;
1763
0
        }
1764
7
    }
1765
1766
49
    rv = SECITEM_CopyItem(NULL, &xtnData->ech->innerCh, &encryptedCh);
1767
49
    PRINT_BUF(100, (ss, "CT for ECH Decryption", encryptedCh.data, encryptedCh.len));
1768
49
    if (rv == SECFailure) {
1769
0
        return SECFailure;
1770
0
    }
1771
49
    xtnData->ech->configId = configId;
1772
49
    xtnData->ech->kdfId = kdf;
1773
49
    xtnData->ech->aeadId = aead;
1774
1775
    /* Not negotiated until tls13_MaybeAcceptEch. */
1776
49
    return SECSuccess;
1777
1778
12
alert_loser:
1779
12
    ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1780
12
    PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_EXTENSION);
1781
12
    return SECFailure;
1782
49
}
1783
1784
SECStatus
1785
tls13_SendEmptyGreaseXtn(const sslSocket *ss,
1786
                         TLSExtensionData *xtnData,
1787
                         sslBuffer *buf, PRBool *added)
1788
336
{
1789
336
    if (!ss->opt.enableGrease ||
1790
336
        (!ss->sec.isServer && ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) ||
1791
336
        (ss->sec.isServer && ss->version < SSL_LIBRARY_VERSION_TLS_1_3)) {
1792
336
        return SECSuccess;
1793
336
    }
1794
1795
0
    *added = PR_TRUE;
1796
0
    return SECSuccess;
1797
336
}
1798
1799
SECStatus
1800
tls13_SendGreaseXtn(const sslSocket *ss,
1801
                    TLSExtensionData *xtnData,
1802
                    sslBuffer *buf, PRBool *added)
1803
0
{
1804
0
    if (!ss->opt.enableGrease ||
1805
0
        (!ss->sec.isServer && ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) ||
1806
0
        (ss->sec.isServer && ss->version < SSL_LIBRARY_VERSION_TLS_1_3)) {
1807
0
        return SECSuccess;
1808
0
    }
1809
1810
0
    SECStatus rv = sslBuffer_AppendVariable(buf, (PRUint8[]){ 0x00 }, 1, 2);
1811
0
    if (rv != SECSuccess) {
1812
0
        return SECFailure;
1813
0
    }
1814
1815
0
    *added = PR_TRUE;
1816
0
    return SECSuccess;
1817
0
}
1818
1819
SECStatus
1820
ssl3_SendCertificateCompressionXtn(const sslSocket *ss,
1821
                                   TLSExtensionData *xtnData,
1822
                                   sslBuffer *buf, PRBool *added)
1823
336
{
1824
    /* enum {
1825
     *  zlib(1),
1826
     *  brotli(2),
1827
     *  zstd(3),
1828
     *  (65535)
1829
     * } CertificateCompressionAlgorithm;
1830
     *
1831
     * struct {
1832
     *      CertificateCompressionAlgorithm algorithms<2..2^8-2>;
1833
     *  } CertificateCompressionAlgorithms;
1834
     */
1835
1836
336
    SECStatus rv = SECFailure;
1837
336
    if (ss->ssl3.cwSpec->version < SSL_LIBRARY_VERSION_TLS_1_3) {
1838
0
        SSL_TRC(50, ("%d: TLS13[%d]: certificate_compression_algorithm extension requires TLS1.3 and above",
1839
0
                     SSL_GETPID(), ss->fd));
1840
0
        return SECSuccess;
1841
0
    }
1842
1843
336
    size_t certificateCompressionAlgorithmsLen = ss->ssl3.supportedCertCompressionAlgorithmsCount;
1844
336
    if (certificateCompressionAlgorithmsLen == 0) {
1845
336
        SSL_TRC(30, ("%d: TLS13[%d]: %s does not support any certificate compression algorithm",
1846
336
                     SSL_GETPID(), ss->fd, SSL_ROLE(ss)));
1847
336
        return SECSuccess;
1848
336
    }
1849
1850
0
    SSL_TRC(30, ("%d: TLS13[%d]: %s sends certificate_compression_algorithm extension",
1851
0
                 SSL_GETPID(), ss->fd, SSL_ROLE(ss)));
1852
0
    PORT_Assert(certificateCompressionAlgorithmsLen < (0x1u << 8) - 1);
1853
1854
0
    rv = sslBuffer_AppendNumber(buf, certificateCompressionAlgorithmsLen << 1, 1);
1855
0
    if (rv != SECSuccess) {
1856
0
        return SECFailure;
1857
0
    }
1858
1859
0
    for (size_t i = 0; i < certificateCompressionAlgorithmsLen; i++) {
1860
0
        rv = sslBuffer_AppendNumber(buf, ss->ssl3.supportedCertCompressionAlgorithms[i].id, 2);
1861
0
        if (rv != SECSuccess) {
1862
0
            return SECFailure;
1863
0
        }
1864
0
    }
1865
1866
0
    xtnData->certificateCompressionAdvertised = PR_TRUE;
1867
0
    *added = PR_TRUE;
1868
0
    return SECSuccess;
1869
0
}
1870
1871
const char *
1872
ssl3_mapCertificateCompressionAlgorithmToName(const sslSocket *ss, SSLCertificateCompressionAlgorithmID alg)
1873
0
{
1874
0
    for (int i = 0; i < ss->ssl3.supportedCertCompressionAlgorithmsCount; i++) {
1875
0
        if (ss->ssl3.supportedCertCompressionAlgorithms[i].id == alg) {
1876
0
            return ss->ssl3.supportedCertCompressionAlgorithms[i].name;
1877
0
        }
1878
0
    }
1879
0
    return "unknown";
1880
0
}
1881
1882
SECStatus
1883
ssl3_HandleCertificateCompressionXtn(const sslSocket *ss,
1884
                                     TLSExtensionData *xtnData,
1885
                                     SECItem *data)
1886
27
{
1887
    /* This extension is only supported with TLS 1.3 [RFC8446] and newer; 
1888
     * if TLS 1.2 [RFC5246] or earlier is negotiated, the peers MUST ignore this extension.
1889
     */
1890
27
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
1891
4
        SSL_TRC(50, ("%d: TLS13[%d]: ignore certificate_compression extension",
1892
4
                     SSL_GETPID(), ss->fd));
1893
4
        return SECSuccess;
1894
4
    }
1895
1896
23
    SECStatus rv = SECFailure;
1897
23
    PRUint32 lengthSupportedAlgorithms = 0;
1898
23
    PRUint32 certComprAlgId = 0;
1899
1900
23
    SSL_TRC(30, ("%d: TLS13[%d]: %s handles certificate_compression_algorithm extension",
1901
23
                 SSL_GETPID(), ss->fd, SSL_ROLE(ss)));
1902
1903
23
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &lengthSupportedAlgorithms, 1, &data->data, &data->len);
1904
23
    if (rv != SECSuccess) {
1905
1
        goto alert_loser;
1906
1
    }
1907
1908
    /* Each of the algorithm is 2 bytes. */
1909
22
    if (lengthSupportedAlgorithms % 2 != 0) {
1910
1
        goto alert_loser;
1911
1
    }
1912
1913
21
    if (data->len != lengthSupportedAlgorithms) {
1914
4
        goto alert_loser;
1915
4
    }
1916
1917
17
    SECStatus algFound = SECFailure;
1918
1919
    /* We use the first common algorithm we found. */
1920
187
    for (int i = 0; i < lengthSupportedAlgorithms / 2; i++) {
1921
170
        rv = ssl3_ExtConsumeHandshakeNumber(ss, &certComprAlgId, 2, &data->data, &data->len);
1922
170
        if (rv != SECSuccess) {
1923
0
            goto alert_loser;
1924
0
        }
1925
1926
170
        SSLCertificateCompressionAlgorithmID alg = (SSLCertificateCompressionAlgorithmID)certComprAlgId;
1927
170
        if (alg == 0) {
1928
54
            SSL_TRC(50, ("%d: TLS13[%d]: certificate compression ignores reserved algorithm %02x",
1929
54
                         SSL_GETPID(), ss->fd, alg));
1930
54
            continue;
1931
54
        }
1932
1933
116
        for (int j = 0; j < ss->ssl3.supportedCertCompressionAlgorithmsCount; j++) {
1934
0
            if (ss->ssl3.supportedCertCompressionAlgorithms[j].id == alg) {
1935
0
                xtnData->compressionAlg = alg;
1936
0
                xtnData->negotiated[xtnData->numNegotiated++] = ssl_certificate_compression_xtn;
1937
0
                algFound = SECSuccess;
1938
0
                break;
1939
0
            }
1940
0
        }
1941
1942
116
        if (algFound == SECSuccess) {
1943
0
            break;
1944
0
        }
1945
116
    }
1946
1947
17
    if (algFound == SECSuccess) {
1948
0
        SSL_TRC(30, ("%d: TLS13[%d]: %s established certificate compression algorithm %s",
1949
0
                     SSL_GETPID(), ss->fd, SSL_ROLE(ss),
1950
0
                     ssl3_mapCertificateCompressionAlgorithmToName(ss, xtnData->compressionAlg)));
1951
17
    } else {
1952
17
        SSL_TRC(30, ("%d: TLS13[%d]: no common certificate compression algorithms found on the %s side",
1953
17
                     SSL_GETPID(), ss->fd, SSL_ROLE(ss)));
1954
17
    }
1955
1956
17
    return SECSuccess;
1957
1958
6
alert_loser:
1959
6
    ssl3_ExtDecodeError(ss);
1960
6
    return SECFailure;
1961
17
}