Coverage Report

Created: 2025-06-24 06:49

/src/nss/lib/ssl/ssl3exthandle.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 "blapit.h"
14
#include "prinit.h"
15
#include "selfencrypt.h"
16
#include "ssl3ext.h"
17
#include "ssl3exthandle.h"
18
#include "tls13ech.h"
19
#include "tls13exthandle.h" /* For tls13_ServerSendStatusRequestXtn. */
20
21
PRBool
22
ssl_ShouldSendSNIExtension(const sslSocket *ss, const char *url)
23
78.2k
{
24
78.2k
    PRNetAddr netAddr;
25
26
    /* must have a hostname */
27
78.2k
    if (!url || !url[0]) {
28
0
        return PR_FALSE;
29
0
    }
30
    /* must not be an IPv4 or IPv6 address */
31
78.2k
    if (PR_SUCCESS == PR_StringToNetAddr(url, &netAddr)) {
32
        /* is an IP address (v4 or v6) */
33
0
        return PR_FALSE;
34
0
    }
35
36
78.2k
    return PR_TRUE;
37
78.2k
}
38
39
/* Format an SNI extension, using the name from the socket's URL,
40
 * unless that name is a dotted decimal string.
41
 * Used by client and server.
42
 */
43
SECStatus
44
ssl3_ClientFormatServerNameXtn(const sslSocket *ss, const char *url,
45
                               unsigned int len, TLSExtensionData *xtnData,
46
                               sslBuffer *buf)
47
84.5k
{
48
84.5k
    SECStatus rv;
49
50
    /* length of server_name_list */
51
84.5k
    rv = sslBuffer_AppendNumber(buf, len + 3, 2);
52
84.5k
    if (rv != SECSuccess) {
53
0
        return SECFailure;
54
0
    }
55
    /* Name Type (sni_host_name) */
56
84.5k
    rv = sslBuffer_AppendNumber(buf, 0, 1);
57
84.5k
    if (rv != SECSuccess) {
58
0
        return SECFailure;
59
0
    }
60
    /* HostName (length and value) */
61
84.5k
    rv = sslBuffer_AppendVariable(buf, (const PRUint8 *)url, len, 2);
62
84.5k
    if (rv != SECSuccess) {
63
0
        return SECFailure;
64
0
    }
65
66
84.5k
    return SECSuccess;
67
84.5k
}
68
69
SECStatus
70
ssl3_ClientSendServerNameXtn(const sslSocket *ss, TLSExtensionData *xtnData,
71
                             sslBuffer *buf, PRBool *added)
72
73.7k
{
73
73.7k
    SECStatus rv;
74
75
73.7k
    const char *url = ss->url;
76
77
73.7k
    if (!ssl_ShouldSendSNIExtension(ss, url)) {
78
0
        return SECSuccess;
79
0
    }
80
81
    /* If ECH, write the public name. The real server name
82
     * is emplaced while constructing CHInner extensions. */
83
73.7k
    sslEchConfig *cfg = (sslEchConfig *)PR_LIST_HEAD(&ss->echConfigs);
84
73.7k
    const char *sniContents = PR_CLIST_IS_EMPTY(&ss->echConfigs) ? url : cfg->contents.publicName;
85
73.7k
    rv = ssl3_ClientFormatServerNameXtn(ss, sniContents, strlen(sniContents), xtnData, buf);
86
73.7k
    if (rv != SECSuccess) {
87
0
        return SECFailure;
88
0
    }
89
90
73.7k
    *added = PR_TRUE;
91
73.7k
    return SECSuccess;
92
73.7k
}
93
94
SECStatus
95
ssl3_HandleServerNameXtn(const sslSocket *ss, TLSExtensionData *xtnData,
96
                         SECItem *data)
97
21.4k
{
98
21.4k
    SECItem *names = NULL;
99
21.4k
    PRUint32 listLenBytes = 0;
100
21.4k
    SECStatus rv;
101
102
21.4k
    if (!ss->sec.isServer) {
103
7.81k
        return SECSuccess; /* ignore extension */
104
7.81k
    }
105
106
    /* Server side - consume client data and register server sender. */
107
    /* do not parse the data if don't have user extension handling function. */
108
13.5k
    if (!ss->sniSocketConfig) {
109
13.5k
        return SECSuccess;
110
13.5k
    }
111
112
    /* length of server_name_list */
113
0
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &listLenBytes, 2, &data->data, &data->len);
114
0
    if (rv != SECSuccess) {
115
0
        goto loser; /* alert already sent */
116
0
    }
117
0
    if (listLenBytes == 0 || listLenBytes != data->len) {
118
0
        goto alert_loser;
119
0
    }
120
121
    /* Read ServerNameList. */
122
0
    while (data->len > 0) {
123
0
        SECItem tmp;
124
0
        PRUint32 type;
125
126
        /* Read Name Type. */
127
0
        rv = ssl3_ExtConsumeHandshakeNumber(ss, &type, 1, &data->data, &data->len);
128
0
        if (rv != SECSuccess) {
129
            /* alert sent in ConsumeHandshakeNumber */
130
0
            goto loser;
131
0
        }
132
133
        /* Read ServerName (length and value). */
134
0
        rv = ssl3_ExtConsumeHandshakeVariable(ss, &tmp, 2, &data->data, &data->len);
135
0
        if (rv != SECSuccess) {
136
0
            goto loser;
137
0
        }
138
139
        /* Record the value for host_name(0). */
140
0
        if (type == sni_nametype_hostname) {
141
            /* Fail if we encounter a second host_name entry. */
142
0
            if (names) {
143
0
                goto alert_loser;
144
0
            }
145
146
            /* Create an array for the only supported NameType. */
147
0
            names = PORT_ZNewArray(SECItem, 1);
148
0
            if (!names) {
149
0
                goto loser;
150
0
            }
151
152
            /* Copy ServerName into the array. */
153
0
            if (SECITEM_CopyItem(NULL, &names[0], &tmp) != SECSuccess) {
154
0
                goto loser;
155
0
            }
156
0
        }
157
158
        /* Even if we don't support NameTypes other than host_name at the
159
         * moment, we continue parsing the whole list to check its validity.
160
         * We do not check for duplicate entries with NameType != host_name(0).
161
         */
162
0
    }
163
0
    if (names) {
164
        /* Free old and set the new data. */
165
0
        ssl3_FreeSniNameArray(xtnData);
166
0
        xtnData->sniNameArr = names;
167
0
        xtnData->sniNameArrSize = 1;
168
0
        xtnData->negotiated[xtnData->numNegotiated++] = ssl_server_name_xtn;
169
0
    }
170
0
    return SECSuccess;
171
172
0
alert_loser:
173
0
    ssl3_ExtDecodeError(ss);
174
0
loser:
175
0
    if (names) {
176
0
        PORT_Free(names);
177
0
    }
178
0
    return SECFailure;
179
0
}
180
181
/* Frees a given xtnData->sniNameArr and its elements. */
182
void
183
ssl3_FreeSniNameArray(TLSExtensionData *xtnData)
184
423k
{
185
423k
    PRUint32 i;
186
187
423k
    if (!xtnData->sniNameArr) {
188
423k
        return;
189
423k
    }
190
191
0
    for (i = 0; i < xtnData->sniNameArrSize; i++) {
192
0
        SECITEM_FreeItem(&xtnData->sniNameArr[i], PR_FALSE);
193
0
    }
194
195
0
    PORT_Free(xtnData->sniNameArr);
196
0
    xtnData->sniNameArr = NULL;
197
0
    xtnData->sniNameArrSize = 0;
198
0
}
199
200
/* Called by both clients and servers.
201
 * Clients sends a filled in session ticket if one is available, and otherwise
202
 * sends an empty ticket.  Servers always send empty tickets.
203
 */
204
SECStatus
205
ssl3_ClientSendSessionTicketXtn(const sslSocket *ss, TLSExtensionData *xtnData,
206
                                sslBuffer *buf, PRBool *added)
207
73.7k
{
208
73.7k
    NewSessionTicket *session_ticket = NULL;
209
73.7k
    sslSessionID *sid = ss->sec.ci.sid;
210
73.7k
    SECStatus rv;
211
212
73.7k
    PORT_Assert(!ss->sec.isServer);
213
214
    /* Never send an extension with a ticket for TLS 1.3, but
215
     * OK to send the empty one in case the server does 1.2. */
216
73.7k
    if ((sid->cached == in_client_cache || sid->cached == in_external_cache) &&
217
73.7k
        sid->version >= SSL_LIBRARY_VERSION_TLS_1_3) {
218
0
        return SECSuccess;
219
0
    }
220
221
    /* Ignore the SessionTicket extension if processing is disabled. */
222
73.7k
    if (!ss->opt.enableSessionTickets) {
223
35.5k
        return SECSuccess;
224
35.5k
    }
225
226
    /* Send a session ticket if one is available.
227
     *
228
     * The caller must be holding sid->u.ssl3.lock for reading. We cannot
229
     * just acquire and release the lock within this function because the
230
     * caller will call this function twice, and we need the inputs to be
231
     * consistent between the two calls. Note that currently the caller
232
     * will only be holding the lock when we are the client and when we're
233
     * attempting to resume an existing session.
234
     */
235
38.1k
    session_ticket = &sid->u.ssl3.locked.sessionTicket;
236
38.1k
    if (session_ticket->ticket.data &&
237
38.1k
        (xtnData->ticketTimestampVerified ||
238
0
         ssl_TicketTimeValid(ss, session_ticket))) {
239
240
0
        xtnData->ticketTimestampVerified = PR_FALSE;
241
242
0
        rv = sslBuffer_Append(buf, session_ticket->ticket.data,
243
0
                              session_ticket->ticket.len);
244
0
        if (rv != SECSuccess) {
245
0
            return SECFailure;
246
0
        }
247
248
0
        xtnData->sentSessionTicketInClientHello = PR_TRUE;
249
0
    }
250
251
38.1k
    *added = PR_TRUE;
252
38.1k
    return SECSuccess;
253
38.1k
}
254
255
PRBool
256
ssl_AlpnTagAllowed(const sslSocket *ss, const SECItem *tag)
257
0
{
258
0
    const unsigned char *data = ss->opt.nextProtoNego.data;
259
0
    unsigned int length = ss->opt.nextProtoNego.len;
260
0
    unsigned int offset = 0;
261
262
0
    if (!tag->len)
263
0
        return PR_TRUE;
264
265
0
    while (offset < length) {
266
0
        unsigned int taglen = (unsigned int)data[offset];
267
0
        if ((taglen == tag->len) &&
268
0
            !PORT_Memcmp(data + offset + 1, tag->data, tag->len))
269
0
            return PR_TRUE;
270
0
        offset += 1 + taglen;
271
0
    }
272
273
0
    return PR_FALSE;
274
0
}
275
276
/* ssl3_ValidateAppProtocol checks that the given block of data is valid: none
277
 * of the lengths may be 0 and the sum of the lengths must equal the length of
278
 * the block. */
279
SECStatus
280
ssl3_ValidateAppProtocol(const unsigned char *data, unsigned int length)
281
0
{
282
0
    unsigned int offset = 0;
283
284
0
    while (offset < length) {
285
0
        unsigned int newOffset = offset + 1 + (unsigned int)data[offset];
286
        /* Reject embedded nulls to protect against buggy applications that
287
         * store protocol identifiers in null-terminated strings.
288
         */
289
0
        if (newOffset > length || data[offset] == 0) {
290
0
            return SECFailure;
291
0
        }
292
0
        offset = newOffset;
293
0
    }
294
295
0
    return SECSuccess;
296
0
}
297
298
/* Protocol selection handler for ALPN. */
299
static SECStatus
300
ssl3_SelectAppProtocol(const sslSocket *ss, TLSExtensionData *xtnData,
301
                       PRUint16 extension, SECItem *data)
302
0
{
303
0
    SECStatus rv;
304
0
    unsigned char resultBuffer[255];
305
0
    SECItem result = { siBuffer, resultBuffer, 0 };
306
307
0
    rv = ssl3_ValidateAppProtocol(data->data, data->len);
308
0
    if (rv != SECSuccess) {
309
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
310
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
311
0
        return SECFailure;
312
0
    }
313
314
0
    PORT_Assert(ss->nextProtoCallback);
315
    /* Neither the cipher suite nor ECH are selected yet Note that extensions
316
     * sometimes affect what cipher suite is selected, e.g., for ECC. */
317
0
    PORT_Assert((ss->ssl3.hs.preliminaryInfo &
318
0
                 ssl_preinfo_all & ~ssl_preinfo_cipher_suite & ~ssl_preinfo_ech) ==
319
0
                (ssl_preinfo_all & ~ssl_preinfo_cipher_suite & ~ssl_preinfo_ech));
320
    /* The callback has to make sure that either rv != SECSuccess or that result
321
     * is not set if there is no common protocol. */
322
0
    rv = ss->nextProtoCallback(ss->nextProtoArg, ss->fd, data->data, data->len,
323
0
                               result.data, &result.len, sizeof(resultBuffer));
324
0
    if (rv != SECSuccess) {
325
        /* Expect callback to call PORT_SetError() */
326
0
        ssl3_ExtSendAlert(ss, alert_fatal, internal_error);
327
0
        return SECFailure;
328
0
    }
329
330
    /* If the callback wrote more than allowed to |result| it has corrupted our
331
     * stack. */
332
0
    if (result.len > sizeof(resultBuffer)) {
333
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
334
0
        PORT_Assert(PR_FALSE);
335
0
        return SECFailure;
336
0
    }
337
338
0
    SECITEM_FreeItem(&xtnData->nextProto, PR_FALSE);
339
340
0
    if (result.len < 1 || !result.data) {
341
        /* Check that we actually got a result. */
342
0
        ssl3_ExtSendAlert(ss, alert_fatal, no_application_protocol);
343
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_NO_PROTOCOL);
344
0
        return SECFailure;
345
0
    }
346
347
0
    xtnData->nextProtoState = SSL_NEXT_PROTO_NEGOTIATED;
348
0
    xtnData->negotiated[xtnData->numNegotiated++] = extension;
349
0
    return SECITEM_CopyItem(NULL, &xtnData->nextProto, &result);
350
0
}
351
352
/* handle an incoming ALPN extension at the server */
353
SECStatus
354
ssl3_ServerHandleAppProtoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
355
                             SECItem *data)
356
613
{
357
613
    PRUint32 count;
358
613
    SECStatus rv;
359
360
    /* We expressly don't want to allow ALPN on renegotiation,
361
     * despite it being permitted by the spec. */
362
613
    if (ss->firstHsDone || data->len == 0) {
363
        /* Clients MUST send a non-empty ALPN extension. */
364
6
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
365
6
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
366
6
        return SECFailure;
367
6
    }
368
369
    /* ALPN has extra redundant length information so that
370
     * the extension is the same in both ClientHello and ServerHello. */
371
607
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &count, 2, &data->data, &data->len);
372
607
    if (rv != SECSuccess || count != data->len) {
373
62
        ssl3_ExtDecodeError(ss);
374
62
        return SECFailure;
375
62
    }
376
377
545
    if (!ss->nextProtoCallback) {
378
        /* we're not configured for it */
379
545
        return SECSuccess;
380
545
    }
381
382
0
    rv = ssl3_SelectAppProtocol(ss, xtnData, ssl_app_layer_protocol_xtn, data);
383
0
    if (rv != SECSuccess) {
384
0
        return rv;
385
0
    }
386
387
    /* prepare to send back a response, if we negotiated */
388
0
    if (xtnData->nextProtoState == SSL_NEXT_PROTO_NEGOTIATED) {
389
0
        rv = ssl3_RegisterExtensionSender(ss, xtnData,
390
0
                                          ssl_app_layer_protocol_xtn,
391
0
                                          ssl3_ServerSendAppProtoXtn);
392
0
        if (rv != SECSuccess) {
393
0
            ssl3_ExtSendAlert(ss, alert_fatal, internal_error);
394
0
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
395
0
            return rv;
396
0
        }
397
0
    }
398
0
    return SECSuccess;
399
0
}
400
401
SECStatus
402
ssl3_ClientHandleAppProtoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
403
                             SECItem *data)
404
0
{
405
0
    SECStatus rv;
406
0
    PRUint32 list_len;
407
0
    SECItem protocol_name;
408
409
0
    if (ssl3_ExtensionNegotiated(ss, ssl_next_proto_nego_xtn)) {
410
0
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
411
0
        return SECFailure;
412
0
    }
413
414
    /* The extension data from the server has the following format:
415
     *   uint16 name_list_len;
416
     *   uint8 len;  // where len >= 1
417
     *   uint8 protocol_name[len]; */
418
0
    if (data->len < 4 || data->len > 2 + 1 + 255) {
419
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
420
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
421
0
        return SECFailure;
422
0
    }
423
424
0
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &list_len, 2, &data->data,
425
0
                                        &data->len);
426
    /* The list has to be the entire extension. */
427
0
    if (rv != SECSuccess || list_len != data->len) {
428
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
429
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
430
0
        return SECFailure;
431
0
    }
432
433
0
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &protocol_name, 1,
434
0
                                          &data->data, &data->len);
435
    /* The list must have exactly one value. */
436
0
    if (rv != SECSuccess || data->len != 0) {
437
0
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
438
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
439
0
        return SECFailure;
440
0
    }
441
442
0
    if (!ssl_AlpnTagAllowed(ss, &protocol_name)) {
443
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
444
0
        PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
445
0
        return SECFailure;
446
0
    }
447
448
0
    SECITEM_FreeItem(&xtnData->nextProto, PR_FALSE);
449
0
    xtnData->nextProtoState = SSL_NEXT_PROTO_SELECTED;
450
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_app_layer_protocol_xtn;
451
0
    return SECITEM_CopyItem(NULL, &xtnData->nextProto, &protocol_name);
452
0
}
453
454
SECStatus
455
ssl3_ClientSendAppProtoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
456
                           sslBuffer *buf, PRBool *added)
457
73.7k
{
458
73.7k
    SECStatus rv;
459
460
    /* Renegotiations do not send this extension. */
461
73.7k
    if (!ss->opt.enableALPN || !ss->opt.nextProtoNego.len || ss->firstHsDone) {
462
73.7k
        PR_ASSERT(!ss->opt.nextProtoNego.data);
463
73.7k
        return SECSuccess;
464
73.7k
    }
465
0
    PRBool addGrease = ss->opt.enableGrease && ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3;
466
467
    /* The list of protocol strings is prefixed with a 2-byte length */
468
0
    rv = sslBuffer_AppendNumber(buf, ss->opt.nextProtoNego.len + (addGrease ? 3 : 0), 2);
469
0
    if (rv != SECSuccess) {
470
0
        return SECFailure;
471
0
    }
472
    /* The list of protocol strings */
473
0
    rv = sslBuffer_Append(buf, ss->opt.nextProtoNego.data, ss->opt.nextProtoNego.len);
474
0
    if (rv != SECSuccess) {
475
0
        return SECFailure;
476
0
    }
477
    /* A client MAY select one or more GREASE ALPN identifiers and advertise
478
     * them in the "application_layer_protocol_negotiation" extension, if sent
479
     * [RFC8701, Section 3.1]. */
480
0
    if (addGrease) {
481
0
        rv = sslBuffer_AppendNumber(buf, 2, 1);
482
0
        if (rv != SECSuccess) {
483
0
            return SECFailure;
484
0
        }
485
0
        rv = sslBuffer_AppendNumber(buf, ss->ssl3.hs.grease->idx[grease_alpn], 2);
486
0
        if (rv != SECSuccess) {
487
0
            return SECFailure;
488
0
        }
489
0
    }
490
491
0
    *added = PR_TRUE;
492
0
    return SECSuccess;
493
0
}
494
495
SECStatus
496
ssl3_ServerSendAppProtoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
497
                           sslBuffer *buf, PRBool *added)
498
0
{
499
0
    SECStatus rv;
500
501
    /* We're in over our heads if any of these fail */
502
0
    PORT_Assert(ss->opt.enableALPN);
503
0
    PORT_Assert(xtnData->nextProto.data);
504
0
    PORT_Assert(xtnData->nextProto.len > 0);
505
0
    PORT_Assert(xtnData->nextProtoState == SSL_NEXT_PROTO_NEGOTIATED);
506
0
    PORT_Assert(!ss->firstHsDone);
507
508
0
    rv = sslBuffer_AppendNumber(buf, xtnData->nextProto.len + 1, 2);
509
0
    if (rv != SECSuccess) {
510
0
        return SECFailure;
511
0
    }
512
0
    rv = sslBuffer_AppendVariable(buf, xtnData->nextProto.data,
513
0
                                  xtnData->nextProto.len, 1);
514
0
    if (rv != SECSuccess) {
515
0
        return SECFailure;
516
0
    }
517
518
0
    *added = PR_TRUE;
519
0
    return SECSuccess;
520
0
}
521
522
SECStatus
523
ssl3_ServerHandleStatusRequestXtn(const sslSocket *ss, TLSExtensionData *xtnData,
524
                                  SECItem *data)
525
9.50k
{
526
9.50k
    sslExtensionBuilderFunc sender;
527
528
9.50k
    PORT_Assert(ss->sec.isServer);
529
530
    /* remember that we got this extension. */
531
9.50k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_cert_status_xtn;
532
533
9.50k
    if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) {
534
395
        sender = tls13_ServerSendStatusRequestXtn;
535
9.10k
    } else {
536
9.10k
        sender = ssl3_ServerSendStatusRequestXtn;
537
9.10k
    }
538
9.50k
    return ssl3_RegisterExtensionSender(ss, xtnData, ssl_cert_status_xtn, sender);
539
9.50k
}
540
541
SECStatus
542
ssl3_ServerSendStatusRequestXtn(const sslSocket *ss, TLSExtensionData *xtnData,
543
                                sslBuffer *buf, PRBool *added)
544
9.07k
{
545
9.07k
    const sslServerCert *serverCert = ss->sec.serverCert;
546
547
9.07k
    if (!serverCert->certStatusArray ||
548
9.07k
        !serverCert->certStatusArray->len) {
549
9.07k
        return SECSuccess;
550
9.07k
    }
551
552
0
    *added = PR_TRUE;
553
0
    return SECSuccess;
554
9.07k
}
555
556
/* ssl3_ClientSendStatusRequestXtn builds the status_request extension on the
557
 * client side. See RFC 6066 section 8. */
558
SECStatus
559
ssl3_ClientSendStatusRequestXtn(const sslSocket *ss, TLSExtensionData *xtnData,
560
                                sslBuffer *buf, PRBool *added)
561
73.7k
{
562
73.7k
    SECStatus rv;
563
564
73.7k
    if (!ss->opt.enableOCSPStapling) {
565
36.6k
        return SECSuccess;
566
36.6k
    }
567
568
37.0k
    rv = sslBuffer_AppendNumber(buf, 1 /* status_type ocsp */, 1);
569
37.0k
    if (rv != SECSuccess) {
570
0
        return SECFailure;
571
0
    }
572
    /* A zero length responder_id_list means that the responders are
573
     * implicitly known to the server. */
574
37.0k
    rv = sslBuffer_AppendNumber(buf, 0, 2);
575
37.0k
    if (rv != SECSuccess) {
576
0
        return SECFailure;
577
0
    }
578
    /* A zero length request_extensions means that there are no extensions.
579
     * Specifically, we don't set the id-pkix-ocsp-nonce extension. This
580
     * means that the server can replay a cached OCSP response to us. */
581
37.0k
    rv = sslBuffer_AppendNumber(buf, 0, 2);
582
37.0k
    if (rv != SECSuccess) {
583
0
        return SECFailure;
584
0
    }
585
586
37.0k
    *added = PR_TRUE;
587
37.0k
    return SECSuccess;
588
37.0k
}
589
590
SECStatus
591
ssl3_ClientHandleStatusRequestXtn(const sslSocket *ss, TLSExtensionData *xtnData,
592
                                  SECItem *data)
593
183
{
594
    /* In TLS 1.3, the extension carries the OCSP response. */
595
183
    if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) {
596
0
        SECStatus rv;
597
0
        rv = ssl_ReadCertificateStatus(CONST_CAST(sslSocket, ss),
598
0
                                       data->data, data->len);
599
0
        if (rv != SECSuccess) {
600
0
            return SECFailure; /* code already set */
601
0
        }
602
183
    } else if (data->len != 0) {
603
6
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
604
6
        PORT_SetError(SSL_ERROR_RX_MALFORMED_SERVER_HELLO);
605
6
        return SECFailure;
606
6
    }
607
608
    /* Keep track of negotiated extensions. */
609
177
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_cert_status_xtn;
610
177
    return SECSuccess;
611
183
}
612
613
1.70k
#define TLS_EX_SESS_TICKET_VERSION (0x010a)
614
615
/*
616
 * Called from ssl3_SendNewSessionTicket, tls13_SendNewSessionTicket
617
 */
618
SECStatus
619
ssl3_EncodeSessionTicket(sslSocket *ss, const NewSessionTicket *ticket,
620
                         const PRUint8 *appToken, unsigned int appTokenLen,
621
                         PK11SymKey *secret, SECItem *ticket_data)
622
1.05k
{
623
1.05k
    SECStatus rv;
624
1.05k
    sslBuffer plaintext = SSL_BUFFER_EMPTY;
625
1.05k
    SECItem ticket_buf = { 0, NULL, 0 };
626
1.05k
    sslSessionID sid;
627
1.05k
    unsigned char wrapped_ms[SSL3_MASTER_SECRET_LENGTH];
628
1.05k
    SECItem ms_item = { 0, NULL, 0 };
629
1.05k
    PRTime now;
630
1.05k
    SECItem *srvName = NULL;
631
1.05k
    CK_MECHANISM_TYPE msWrapMech;
632
1.05k
    SECItem *alpnSelection = NULL;
633
1.05k
    PRUint32 ticketAgeBaseline;
634
635
1.05k
    SSL_TRC(3, ("%d: SSL3[%d]: send session_ticket handshake",
636
1.05k
                SSL_GETPID(), ss->fd));
637
638
1.05k
    PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss));
639
1.05k
    PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
640
641
    /* Extract the master secret wrapped. */
642
643
1.05k
    PORT_Memset(&sid, 0, sizeof(sslSessionID));
644
645
1.05k
    PORT_Assert(secret);
646
1.05k
    rv = ssl3_CacheWrappedSecret(ss, &sid, secret);
647
1.05k
    if (rv == SECSuccess) {
648
1.05k
        if (sid.u.ssl3.keys.wrapped_master_secret_len > sizeof(wrapped_ms))
649
0
            goto loser;
650
1.05k
        memcpy(wrapped_ms, sid.u.ssl3.keys.wrapped_master_secret,
651
1.05k
               sid.u.ssl3.keys.wrapped_master_secret_len);
652
1.05k
        ms_item.data = wrapped_ms;
653
1.05k
        ms_item.len = sid.u.ssl3.keys.wrapped_master_secret_len;
654
1.05k
        msWrapMech = sid.u.ssl3.masterWrapMech;
655
1.05k
    } else {
656
        /* TODO: else send an empty ticket. */
657
0
        goto loser;
658
0
    }
659
    /* Prep to send negotiated name */
660
1.05k
    srvName = &ss->sec.ci.sid->u.ssl3.srvName;
661
662
    /* ticket version */
663
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, TLS_EX_SESS_TICKET_VERSION,
664
1.05k
                                sizeof(PRUint16));
665
1.05k
    if (rv != SECSuccess)
666
0
        goto loser;
667
668
    /* ssl_version */
669
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->version,
670
1.05k
                                sizeof(SSL3ProtocolVersion));
671
1.05k
    if (rv != SECSuccess)
672
0
        goto loser;
673
674
    /* ciphersuite */
675
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->ssl3.hs.cipher_suite,
676
1.05k
                                sizeof(ssl3CipherSuite));
677
1.05k
    if (rv != SECSuccess)
678
0
        goto loser;
679
680
    /* cipher spec parameters */
681
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->sec.authType, 1);
682
1.05k
    if (rv != SECSuccess)
683
0
        goto loser;
684
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->sec.authKeyBits, 4);
685
1.05k
    if (rv != SECSuccess)
686
0
        goto loser;
687
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->sec.keaType, 1);
688
1.05k
    if (rv != SECSuccess)
689
0
        goto loser;
690
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->sec.keaKeyBits, 4);
691
1.05k
    if (rv != SECSuccess)
692
0
        goto loser;
693
1.05k
    if (ss->sec.keaGroup) {
694
444
        rv = sslBuffer_AppendNumber(&plaintext, ss->sec.keaGroup->name, 4);
695
444
        if (rv != SECSuccess)
696
0
            goto loser;
697
609
    } else {
698
        /* No kea group. Write 0 as invalid value. */
699
609
        rv = sslBuffer_AppendNumber(&plaintext, 0, 4);
700
609
        if (rv != SECSuccess)
701
0
            goto loser;
702
609
    }
703
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->sec.signatureScheme, 4);
704
1.05k
    if (rv != SECSuccess)
705
0
        goto loser;
706
707
    /* certificate type */
708
1.05k
    PORT_Assert(SSL_CERT_IS(ss->sec.serverCert, ss->sec.authType));
709
1.05k
    if (SSL_CERT_IS_EC(ss->sec.serverCert)) {
710
94
        const sslServerCert *cert = ss->sec.serverCert;
711
94
        PORT_Assert(cert->namedCurve);
712
        /* EC curves only use the second of the two bytes. */
713
94
        PORT_Assert(cert->namedCurve->name < 256);
714
94
        rv = sslBuffer_AppendNumber(&plaintext, cert->namedCurve->name, 1);
715
959
    } else {
716
959
        rv = sslBuffer_AppendNumber(&plaintext, 0, 1);
717
959
    }
718
1.05k
    if (rv != SECSuccess)
719
0
        goto loser;
720
721
    /* master_secret */
722
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, msWrapMech, 4);
723
1.05k
    if (rv != SECSuccess)
724
0
        goto loser;
725
1.05k
    rv = sslBuffer_AppendVariable(&plaintext, ms_item.data, ms_item.len, 2);
726
1.05k
    if (rv != SECSuccess)
727
0
        goto loser;
728
729
    /* client identity */
730
1.05k
    if (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) {
731
4
        rv = sslBuffer_AppendNumber(&plaintext, CLIENT_AUTH_CERTIFICATE, 1);
732
4
        if (rv != SECSuccess)
733
0
            goto loser;
734
4
        rv = sslBuffer_AppendVariable(&plaintext,
735
4
                                      ss->sec.ci.sid->peerCert->derCert.data,
736
4
                                      ss->sec.ci.sid->peerCert->derCert.len, 2);
737
4
        if (rv != SECSuccess)
738
0
            goto loser;
739
1.04k
    } else {
740
1.04k
        rv = sslBuffer_AppendNumber(&plaintext, 0, 1);
741
1.04k
        if (rv != SECSuccess)
742
0
            goto loser;
743
1.04k
    }
744
745
    /* timestamp */
746
1.05k
    now = ssl_Time(ss);
747
1.05k
    PORT_Assert(sizeof(now) == 8);
748
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, now, 8);
749
1.05k
    if (rv != SECSuccess)
750
0
        goto loser;
751
752
    /* HostName (length and value) */
753
1.05k
    rv = sslBuffer_AppendVariable(&plaintext, srvName->data, srvName->len, 2);
754
1.05k
    if (rv != SECSuccess)
755
0
        goto loser;
756
757
    /* extendedMasterSecretUsed */
758
1.05k
    rv = sslBuffer_AppendNumber(
759
1.05k
        &plaintext, ss->sec.ci.sid->u.ssl3.keys.extendedMasterSecretUsed, 1);
760
1.05k
    if (rv != SECSuccess)
761
0
        goto loser;
762
763
    /* Flags */
764
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ticket->flags,
765
1.05k
                                sizeof(ticket->flags));
766
1.05k
    if (rv != SECSuccess)
767
0
        goto loser;
768
769
    /* ALPN value. */
770
1.05k
    PORT_Assert(ss->xtnData.nextProtoState == SSL_NEXT_PROTO_SELECTED ||
771
1.05k
                ss->xtnData.nextProtoState == SSL_NEXT_PROTO_NEGOTIATED ||
772
1.05k
                ss->xtnData.nextProto.len == 0);
773
1.05k
    alpnSelection = &ss->xtnData.nextProto;
774
1.05k
    PORT_Assert(alpnSelection->len < 256);
775
1.05k
    rv = sslBuffer_AppendVariable(&plaintext, alpnSelection->data,
776
1.05k
                                  alpnSelection->len, 1);
777
1.05k
    if (rv != SECSuccess)
778
0
        goto loser;
779
780
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ss->opt.maxEarlyDataSize, 4);
781
1.05k
    if (rv != SECSuccess)
782
0
        goto loser;
783
784
    /*
785
     * We store this in the ticket:
786
     *    ticket_age_baseline = 1rtt - ticket_age_add
787
     *
788
     * When the client resumes, it will provide:
789
     *    obfuscated_age = ticket_age_client + ticket_age_add
790
     *
791
     * We expect to receive the ticket at:
792
     *    ticket_create + 1rtt + ticket_age_server
793
     *
794
     * We calculate the client's estimate of this as:
795
     *    ticket_create + ticket_age_baseline + obfuscated_age
796
     *    = ticket_create + 1rtt + ticket_age_client
797
     *
798
     * This is compared to the expected time, which should differ only as a
799
     * result of clock errors or errors in the RTT estimate.
800
     */
801
1.05k
    ticketAgeBaseline = ss->ssl3.hs.rttEstimate / PR_USEC_PER_MSEC;
802
1.05k
    ticketAgeBaseline -= ticket->ticket_age_add;
803
1.05k
    rv = sslBuffer_AppendNumber(&plaintext, ticketAgeBaseline, 4);
804
1.05k
    if (rv != SECSuccess)
805
0
        goto loser;
806
807
    /* Application token */
808
1.05k
    rv = sslBuffer_AppendVariable(&plaintext, appToken, appTokenLen, 2);
809
1.05k
    if (rv != SECSuccess)
810
0
        goto loser;
811
812
    /* This really only happens if appTokenLen is too much, and that always
813
     * comes from the using application. */
814
1.05k
    if (SSL_BUFFER_LEN(&plaintext) > 0xffff) {
815
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
816
0
        goto loser;
817
0
    }
818
819
1.05k
    ticket_buf.len = ssl_SelfEncryptGetProtectedSize(SSL_BUFFER_LEN(&plaintext));
820
1.05k
    PORT_Assert(ticket_buf.len > 0);
821
1.05k
    if (SECITEM_AllocItem(NULL, &ticket_buf, ticket_buf.len) == NULL) {
822
0
        goto loser;
823
0
    }
824
825
    /* Finally, encrypt the ticket. */
826
1.05k
    rv = ssl_SelfEncryptProtect(ss, SSL_BUFFER_BASE(&plaintext),
827
1.05k
                                SSL_BUFFER_LEN(&plaintext),
828
1.05k
                                ticket_buf.data, &ticket_buf.len, ticket_buf.len);
829
1.05k
    if (rv != SECSuccess) {
830
0
        goto loser;
831
0
    }
832
833
    /* Give ownership of memory to caller. */
834
1.05k
    *ticket_data = ticket_buf;
835
836
1.05k
    sslBuffer_Clear(&plaintext);
837
1.05k
    return SECSuccess;
838
839
0
loser:
840
0
    sslBuffer_Clear(&plaintext);
841
0
    if (ticket_buf.data) {
842
0
        SECITEM_FreeItem(&ticket_buf, PR_FALSE);
843
0
    }
844
845
0
    return SECFailure;
846
1.05k
}
847
848
/* When a client receives a SessionTicket extension a NewSessionTicket
849
 * message is expected during the handshake.
850
 */
851
SECStatus
852
ssl3_ClientHandleSessionTicketXtn(const sslSocket *ss, TLSExtensionData *xtnData,
853
                                  SECItem *data)
854
149
{
855
149
    PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3);
856
857
149
    if (data->len != 0) {
858
9
        return SECSuccess; /* Ignore the extension. */
859
9
    }
860
861
    /* Keep track of negotiated extensions. */
862
140
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_session_ticket_xtn;
863
140
    return SECSuccess;
864
149
}
865
866
PR_STATIC_ASSERT((TLS_EX_SESS_TICKET_VERSION >> 8) == 1);
867
868
static SECStatus
869
ssl_ParseSessionTicket(sslSocket *ss, const SECItem *decryptedTicket,
870
                       SessionTicket *parsedTicket)
871
696
{
872
696
    PRUint32 temp;
873
696
    SECStatus rv;
874
875
696
    PRUint8 *buffer = decryptedTicket->data;
876
696
    unsigned int len = decryptedTicket->len;
877
878
696
    PORT_Memset(parsedTicket, 0, sizeof(*parsedTicket));
879
696
    parsedTicket->valid = PR_FALSE;
880
881
    /* If the decrypted ticket is empty, then report success, but leave the
882
     * ticket marked as invalid. */
883
696
    if (decryptedTicket->len == 0) {
884
0
        return SECSuccess;
885
0
    }
886
887
    /* Read ticket version. */
888
696
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 2, &buffer, &len);
889
696
    if (rv != SECSuccess) {
890
7
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
891
7
        return SECFailure;
892
7
    }
893
894
    /* All ticket versions start with 0x01, so check to see if this
895
     * is a ticket or some other self-encrypted thing. */
896
689
    if ((temp >> 8) != 1) {
897
35
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
898
35
        return SECFailure;
899
35
    }
900
    /* Skip the ticket if the version is wrong.  This won't result in a
901
     * handshake failure, just a failure to resume. */
902
654
    if (temp != TLS_EX_SESS_TICKET_VERSION) {
903
441
        return SECSuccess;
904
441
    }
905
906
    /* Read SSLVersion. */
907
213
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 2, &buffer, &len);
908
213
    if (rv != SECSuccess) {
909
3
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
910
3
        return SECFailure;
911
3
    }
912
210
    parsedTicket->ssl_version = (SSL3ProtocolVersion)temp;
913
210
    if (!ssl3_VersionIsSupported(ss->protocolVariant,
914
210
                                 parsedTicket->ssl_version)) {
915
        /* This socket doesn't support the version from the ticket. */
916
64
        return SECSuccess;
917
64
    }
918
919
    /* Read cipher_suite. */
920
146
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 2, &buffer, &len);
921
146
    if (rv != SECSuccess) {
922
3
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
923
3
        return SECFailure;
924
3
    }
925
143
    parsedTicket->cipher_suite = (ssl3CipherSuite)temp;
926
927
    /* Read cipher spec parameters. */
928
143
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 1, &buffer, &len);
929
143
    if (rv != SECSuccess) {
930
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
931
1
        return SECFailure;
932
1
    }
933
934
#ifndef UNSAFE_FUZZER_MODE
935
    PORT_Assert(temp < ssl_auth_size);
936
#else
937
142
    temp %= (8 * sizeof(SSLAuthType)) - 1;
938
142
#endif
939
940
142
    parsedTicket->authType = (SSLAuthType)temp;
941
142
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
942
142
    if (rv != SECSuccess) {
943
2
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
944
2
        return SECFailure;
945
2
    }
946
140
    parsedTicket->authKeyBits = temp;
947
140
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 1, &buffer, &len);
948
140
    if (rv != SECSuccess) {
949
2
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
950
2
        return SECFailure;
951
2
    }
952
138
    parsedTicket->keaType = (SSLKEAType)temp;
953
138
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
954
138
    if (rv != SECSuccess) {
955
2
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
956
2
        return SECFailure;
957
2
    }
958
136
    parsedTicket->keaKeyBits = temp;
959
136
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
960
136
    if (rv != SECSuccess) {
961
2
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
962
2
        return SECFailure;
963
2
    }
964
134
    parsedTicket->originalKeaGroup = temp;
965
134
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
966
134
    if (rv != SECSuccess) {
967
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
968
1
        return SECFailure;
969
1
    }
970
133
    parsedTicket->signatureScheme = (SSLSignatureScheme)temp;
971
972
    /* Read the optional named curve. */
973
133
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 1, &buffer, &len);
974
133
    if (rv != SECSuccess) {
975
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
976
1
        return SECFailure;
977
1
    }
978
132
    if (parsedTicket->authType == ssl_auth_ecdsa ||
979
132
        parsedTicket->authType == ssl_auth_ecdh_rsa ||
980
132
        parsedTicket->authType == ssl_auth_ecdh_ecdsa) {
981
54
        const sslNamedGroupDef *group =
982
54
            ssl_LookupNamedGroup((SSLNamedGroup)temp);
983
54
        if (!group || group->keaType != ssl_kea_ecdh) {
984
1
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
985
1
            return SECFailure;
986
1
        }
987
53
        parsedTicket->namedCurve = group;
988
53
    }
989
990
    /* Read the master secret (and how it is wrapped). */
991
131
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
992
131
    if (rv != SECSuccess) {
993
3
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
994
3
        return SECFailure;
995
3
    }
996
128
    parsedTicket->msWrapMech = (CK_MECHANISM_TYPE)temp;
997
998
128
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 2, &buffer, &len);
999
128
    if (rv != SECSuccess) {
1000
7
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1001
7
        return SECFailure;
1002
7
    }
1003
121
    if (temp == 0 || temp > sizeof(parsedTicket->master_secret)) {
1004
13
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1005
13
        return SECFailure;
1006
13
    }
1007
108
    parsedTicket->ms_length = (PRUint16)temp;
1008
1009
    /* Read the master secret. */
1010
108
    rv = ssl3_ExtConsumeHandshake(ss, parsedTicket->master_secret,
1011
108
                                  parsedTicket->ms_length, &buffer, &len);
1012
108
    if (rv != SECSuccess) {
1013
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1014
1
        return SECFailure;
1015
1
    }
1016
    /* Read client identity */
1017
107
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 1, &buffer, &len);
1018
107
    if (rv != SECSuccess) {
1019
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1020
1
        return SECFailure;
1021
1
    }
1022
106
    parsedTicket->client_auth_type = (ClientAuthenticationType)temp;
1023
106
    switch (parsedTicket->client_auth_type) {
1024
73
        case CLIENT_AUTH_ANONYMOUS:
1025
73
            break;
1026
32
        case CLIENT_AUTH_CERTIFICATE:
1027
32
            rv = ssl3_ExtConsumeHandshakeVariable(ss, &parsedTicket->peer_cert, 2,
1028
32
                                                  &buffer, &len);
1029
32
            if (rv != SECSuccess) {
1030
2
                PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1031
2
                return SECFailure;
1032
2
            }
1033
30
            break;
1034
30
        default:
1035
1
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1036
1
            return SECFailure;
1037
106
    }
1038
1039
    /* Read timestamp.  This is a 64-bit value and
1040
     * ssl3_ExtConsumeHandshakeNumber only reads 32-bits at a time. */
1041
103
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
1042
103
    if (rv != SECSuccess) {
1043
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1044
1
        return SECFailure;
1045
1
    }
1046
1047
    /* Cast to avoid undefined behavior if the top bit is set. */
1048
102
    parsedTicket->timestamp = (PRTime)((PRUint64)temp << 32);
1049
102
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
1050
102
    if (rv != SECSuccess) {
1051
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1052
1
        return SECFailure;
1053
1
    }
1054
101
    parsedTicket->timestamp |= (PRTime)temp;
1055
1056
    /* Read server name */
1057
101
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &parsedTicket->srvName, 2,
1058
101
                                          &buffer, &len);
1059
101
    if (rv != SECSuccess) {
1060
2
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1061
2
        return SECFailure;
1062
2
    }
1063
1064
    /* Read extendedMasterSecretUsed */
1065
99
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 1, &buffer, &len);
1066
99
    if (rv != SECSuccess) {
1067
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1068
1
        return SECFailure;
1069
1
    }
1070
#ifndef UNSAFE_FUZZER_MODE
1071
    /* A well-behaving server should only write 0 or 1. */
1072
    PORT_Assert(temp == PR_TRUE || temp == PR_FALSE);
1073
#endif
1074
98
    parsedTicket->extendedMasterSecretUsed = temp ? PR_TRUE : PR_FALSE;
1075
1076
98
    rv = ssl3_ExtConsumeHandshake(ss, &temp, 4, &buffer, &len);
1077
98
    if (rv != SECSuccess) {
1078
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1079
1
        return SECFailure;
1080
1
    }
1081
97
    parsedTicket->flags = PR_ntohl(temp);
1082
1083
97
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &parsedTicket->alpnSelection, 1,
1084
97
                                          &buffer, &len);
1085
97
    PORT_Assert(parsedTicket->alpnSelection.len < 256);
1086
97
    if (rv != SECSuccess) {
1087
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1088
1
        return SECFailure;
1089
1
    }
1090
1091
96
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
1092
96
    if (rv != SECSuccess) {
1093
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1094
1
        return SECFailure;
1095
1
    }
1096
95
    parsedTicket->maxEarlyData = temp;
1097
1098
95
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &temp, 4, &buffer, &len);
1099
95
    if (rv != SECSuccess) {
1100
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1101
1
        return SECFailure;
1102
1
    }
1103
94
    parsedTicket->ticketAgeBaseline = temp;
1104
1105
94
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &parsedTicket->applicationToken,
1106
94
                                          2, &buffer, &len);
1107
94
    if (rv != SECSuccess) {
1108
1
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1109
1
        return SECFailure;
1110
1
    }
1111
1112
#ifndef UNSAFE_FUZZER_MODE
1113
    /* Done parsing.  Check that all bytes have been consumed. */
1114
    if (len != 0) {
1115
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1116
        return SECFailure;
1117
    }
1118
#endif
1119
1120
93
    parsedTicket->valid = PR_TRUE;
1121
93
    return SECSuccess;
1122
94
}
1123
1124
static SECStatus
1125
ssl_CreateSIDFromTicket(sslSocket *ss, const SECItem *rawTicket,
1126
                        SessionTicket *parsedTicket, sslSessionID **out)
1127
579
{
1128
579
    sslSessionID *sid;
1129
579
    SECStatus rv;
1130
1131
579
    sid = ssl3_NewSessionID(ss, PR_TRUE);
1132
579
    if (sid == NULL) {
1133
0
        return SECFailure;
1134
0
    }
1135
1136
    /* Copy over parameters. */
1137
579
    sid->version = parsedTicket->ssl_version;
1138
579
    sid->creationTime = parsedTicket->timestamp;
1139
579
    sid->u.ssl3.cipherSuite = parsedTicket->cipher_suite;
1140
579
    sid->authType = parsedTicket->authType;
1141
579
    sid->authKeyBits = parsedTicket->authKeyBits;
1142
579
    sid->keaType = parsedTicket->keaType;
1143
579
    sid->keaKeyBits = parsedTicket->keaKeyBits;
1144
579
    sid->keaGroup = parsedTicket->originalKeaGroup;
1145
579
    sid->namedCurve = parsedTicket->namedCurve;
1146
579
    sid->sigScheme = parsedTicket->signatureScheme;
1147
1148
579
    rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.locked.sessionTicket.ticket,
1149
579
                          rawTicket);
1150
579
    if (rv != SECSuccess) {
1151
0
        goto loser;
1152
0
    }
1153
579
    sid->u.ssl3.locked.sessionTicket.flags = parsedTicket->flags;
1154
579
    sid->u.ssl3.locked.sessionTicket.max_early_data_size =
1155
579
        parsedTicket->maxEarlyData;
1156
1157
579
    if (parsedTicket->ms_length >
1158
579
        sizeof(sid->u.ssl3.keys.wrapped_master_secret)) {
1159
0
        goto loser;
1160
0
    }
1161
579
    PORT_Memcpy(sid->u.ssl3.keys.wrapped_master_secret,
1162
579
                parsedTicket->master_secret, parsedTicket->ms_length);
1163
579
    sid->u.ssl3.keys.wrapped_master_secret_len = parsedTicket->ms_length;
1164
579
    sid->u.ssl3.masterWrapMech = parsedTicket->msWrapMech;
1165
579
    sid->u.ssl3.masterValid = PR_TRUE;
1166
579
    sid->u.ssl3.keys.resumable = PR_TRUE;
1167
579
    sid->u.ssl3.keys.extendedMasterSecretUsed = parsedTicket->extendedMasterSecretUsed;
1168
1169
    /* Copy over client cert from session ticket if there is one. */
1170
579
    if (parsedTicket->peer_cert.data != NULL) {
1171
4
        PORT_Assert(!sid->peerCert);
1172
4
        sid->peerCert = CERT_NewTempCertificate(ss->dbHandle,
1173
4
                                                &parsedTicket->peer_cert,
1174
4
                                                NULL, PR_FALSE, PR_TRUE);
1175
4
        if (!sid->peerCert) {
1176
4
            goto loser;
1177
4
        }
1178
4
    }
1179
1180
    /* Transfer ownership of the remaining items. */
1181
575
    if (parsedTicket->srvName.data != NULL) {
1182
38
        SECITEM_FreeItem(&sid->u.ssl3.srvName, PR_FALSE);
1183
38
        rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.srvName,
1184
38
                              &parsedTicket->srvName);
1185
38
        if (rv != SECSuccess) {
1186
0
            goto loser;
1187
0
        }
1188
38
    }
1189
575
    if (parsedTicket->alpnSelection.data != NULL) {
1190
54
        SECITEM_FreeItem(&sid->u.ssl3.alpnSelection, PR_FALSE);
1191
54
        rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.alpnSelection,
1192
54
                              &parsedTicket->alpnSelection);
1193
54
        if (rv != SECSuccess) {
1194
0
            goto loser;
1195
0
        }
1196
54
    }
1197
1198
575
    *out = sid;
1199
575
    return SECSuccess;
1200
1201
4
loser:
1202
4
    ssl_FreeSID(sid);
1203
4
    return SECFailure;
1204
575
}
1205
1206
/* Generic ticket processing code, common to all TLS versions. */
1207
SECStatus
1208
ssl3_ProcessSessionTicketCommon(sslSocket *ss, const SECItem *ticket,
1209
                                SECItem *appToken)
1210
955
{
1211
955
    SECItem decryptedTicket = { siBuffer, NULL, 0 };
1212
955
    SessionTicket parsedTicket;
1213
955
    sslSessionID *sid = NULL;
1214
955
    SECStatus rv;
1215
1216
955
    if (ss->sec.ci.sid != NULL) {
1217
40
        ssl_UncacheSessionID(ss);
1218
40
        ssl_FreeSID(ss->sec.ci.sid);
1219
40
        ss->sec.ci.sid = NULL;
1220
40
    }
1221
1222
955
    if (!SECITEM_AllocItem(NULL, &decryptedTicket, ticket->len)) {
1223
0
        return SECFailure;
1224
0
    }
1225
1226
    /* Decrypt the ticket. */
1227
955
    rv = ssl_SelfEncryptUnprotect(ss, ticket->data, ticket->len,
1228
955
                                  decryptedTicket.data,
1229
955
                                  &decryptedTicket.len,
1230
955
                                  decryptedTicket.len);
1231
955
    if (rv != SECSuccess) {
1232
        /* Ignore decryption failure if we are doing TLS 1.3; that
1233
         * means the server rejects the client's resumption
1234
         * attempt. In TLS 1.2, however, it's a hard failure, unless
1235
         * it's just because we're not the recipient of the ticket. */
1236
259
        if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 ||
1237
259
            PORT_GetError() == SEC_ERROR_NOT_A_RECIPIENT) {
1238
225
            SECITEM_ZfreeItem(&decryptedTicket, PR_FALSE);
1239
225
            return SECSuccess;
1240
225
        }
1241
1242
34
        SSL3_SendAlert(ss, alert_fatal, illegal_parameter);
1243
34
        goto loser;
1244
259
    }
1245
1246
696
    rv = ssl_ParseSessionTicket(ss, &decryptedTicket, &parsedTicket);
1247
696
    if (rv != SECSuccess) {
1248
98
        SSL3Statistics *ssl3stats;
1249
1250
98
        SSL_DBG(("%d: SSL[%d]: Session ticket parsing failed.",
1251
98
                 SSL_GETPID(), ss->fd));
1252
98
        ssl3stats = SSL_GetStatistics();
1253
98
        SSL_AtomicIncrementLong(&ssl3stats->hch_sid_ticket_parse_failures);
1254
98
        goto loser; /* code already set */
1255
98
    }
1256
1257
    /* Use the ticket if it is valid and unexpired. */
1258
598
    PRTime end = parsedTicket.timestamp + (ssl_ticket_lifetime * PR_USEC_PER_SEC);
1259
598
    if (end > ssl_Time(ss)) {
1260
1261
579
        rv = ssl_CreateSIDFromTicket(ss, ticket, &parsedTicket, &sid);
1262
579
        if (rv != SECSuccess) {
1263
4
            goto loser; /* code already set */
1264
4
        }
1265
575
        if (appToken && parsedTicket.applicationToken.len) {
1266
36
            rv = SECITEM_CopyItem(NULL, appToken,
1267
36
                                  &parsedTicket.applicationToken);
1268
36
            if (rv != SECSuccess) {
1269
0
                goto loser; /* code already set */
1270
0
            }
1271
36
        }
1272
1273
575
        ss->statelessResume = PR_TRUE;
1274
575
        ss->sec.ci.sid = sid;
1275
1276
        /* We have the baseline value for the obfuscated ticket age here.  Save
1277
         * that in xtnData temporarily.  This value is updated in
1278
         * tls13_ServerHandlePreSharedKeyXtn with the final estimate. */
1279
575
        ss->xtnData.ticketAge = parsedTicket.ticketAgeBaseline;
1280
575
    }
1281
1282
594
    SECITEM_ZfreeItem(&decryptedTicket, PR_FALSE);
1283
594
    PORT_Memset(&parsedTicket, 0, sizeof(parsedTicket));
1284
594
    return SECSuccess;
1285
1286
136
loser:
1287
136
    if (sid) {
1288
0
        ssl_FreeSID(sid);
1289
0
    }
1290
136
    SECITEM_ZfreeItem(&decryptedTicket, PR_FALSE);
1291
136
    PORT_Memset(&parsedTicket, 0, sizeof(parsedTicket));
1292
136
    return SECFailure;
1293
598
}
1294
1295
SECStatus
1296
ssl3_ServerHandleSessionTicketXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1297
                                  SECItem *data)
1298
2.38k
{
1299
2.38k
    PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3);
1300
1301
    /* Ignore the SessionTicket extension if processing is disabled. */
1302
2.38k
    if (!ss->opt.enableSessionTickets) {
1303
932
        return SECSuccess;
1304
932
    }
1305
1306
    /* If we are doing TLS 1.3, then ignore this. */
1307
1.44k
    if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) {
1308
0
        return SECSuccess;
1309
0
    }
1310
1311
    /* Keep track of negotiated extensions. */
1312
1.44k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_session_ticket_xtn;
1313
1314
    /* Parse the received ticket sent in by the client.  We are
1315
     * lenient about some parse errors, falling back to a fullshake
1316
     * instead of terminating the current connection.
1317
     */
1318
1.44k
    if (data->len == 0) {
1319
942
        xtnData->emptySessionTicket = PR_TRUE;
1320
942
        return SECSuccess;
1321
942
    }
1322
1323
506
    return ssl3_ProcessSessionTicketCommon(CONST_CAST(sslSocket, ss), data,
1324
506
                                           NULL);
1325
1.44k
}
1326
1327
/* Extension format:
1328
 * Extension number:   2 bytes
1329
 * Extension length:   2 bytes
1330
 * Verify Data Length: 1 byte
1331
 * Verify Data (TLS): 12 bytes (client) or 24 bytes (server)
1332
 * Verify Data (SSL): 36 bytes (client) or 72 bytes (server)
1333
 */
1334
SECStatus
1335
ssl3_SendRenegotiationInfoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1336
                              sslBuffer *buf, PRBool *added)
1337
84.3k
{
1338
84.3k
    PRInt32 len = 0;
1339
84.3k
    SECStatus rv;
1340
1341
    /* In RFC 5746, it is NOT RECOMMENDED to send both the SCSV and the empty
1342
     * RI, so when we send SCSV in the initial handshake, we don't also send RI.
1343
     */
1344
84.3k
    if (ss->ssl3.hs.sendingSCSV) {
1345
0
        return 0;
1346
0
    }
1347
84.3k
    if (ss->firstHsDone) {
1348
36.0k
        len = ss->sec.isServer ? ss->ssl3.hs.finishedBytes * 2
1349
36.0k
                               : ss->ssl3.hs.finishedBytes;
1350
36.0k
    }
1351
1352
    /* verify_Data from previous Finished message(s) */
1353
84.3k
    rv = sslBuffer_AppendVariable(buf,
1354
84.3k
                                  ss->ssl3.hs.finishedMsgs.data, len, 1);
1355
84.3k
    if (rv != SECSuccess) {
1356
0
        return SECFailure;
1357
0
    }
1358
1359
84.3k
    *added = PR_TRUE;
1360
84.3k
    return SECSuccess;
1361
84.3k
}
1362
1363
/* This function runs in both the client and server.  */
1364
SECStatus
1365
ssl3_HandleRenegotiationInfoXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1366
                                SECItem *data)
1367
20.0k
{
1368
20.0k
    SECStatus rv = SECSuccess;
1369
20.0k
    PRUint32 len = 0;
1370
1371
20.0k
    PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3);
1372
1373
20.0k
    if (ss->firstHsDone) {
1374
20
        len = ss->sec.isServer ? ss->ssl3.hs.finishedBytes
1375
20
                               : ss->ssl3.hs.finishedBytes * 2;
1376
20
    }
1377
20.0k
    if (data->len != 1 + len || data->data[0] != len) {
1378
87
        ssl3_ExtDecodeError(ss);
1379
87
        return SECFailure;
1380
87
    }
1381
20.0k
    if (len && NSS_SecureMemcmp(ss->ssl3.hs.finishedMsgs.data,
1382
0
                                data->data + 1, len)) {
1383
0
        ssl3_ExtSendAlert(ss, alert_fatal, handshake_failure);
1384
0
        PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE);
1385
0
        return SECFailure;
1386
0
    }
1387
    /* remember that we got this extension and it was correct. */
1388
20.0k
    CONST_CAST(sslSocket, ss)
1389
20.0k
        ->peerRequestedProtection = 1;
1390
20.0k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_renegotiation_info_xtn;
1391
20.0k
    if (ss->sec.isServer) {
1392
        /* prepare to send back the appropriate response */
1393
10.7k
        rv = ssl3_RegisterExtensionSender(ss, xtnData,
1394
10.7k
                                          ssl_renegotiation_info_xtn,
1395
10.7k
                                          ssl3_SendRenegotiationInfoXtn);
1396
10.7k
    }
1397
20.0k
    return rv;
1398
20.0k
}
1399
1400
SECStatus
1401
ssl3_ClientSendUseSRTPXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1402
                          sslBuffer *buf, PRBool *added)
1403
73.7k
{
1404
73.7k
    unsigned int i;
1405
73.7k
    SECStatus rv;
1406
1407
73.7k
    if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount) {
1408
73.7k
        return SECSuccess; /* Not relevant */
1409
73.7k
    }
1410
1411
    /* Length of the SRTP cipher list */
1412
0
    rv = sslBuffer_AppendNumber(buf, 2 * ss->ssl3.dtlsSRTPCipherCount, 2);
1413
0
    if (rv != SECSuccess) {
1414
0
        return SECFailure;
1415
0
    }
1416
    /* The SRTP ciphers */
1417
0
    for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) {
1418
0
        rv = sslBuffer_AppendNumber(buf, ss->ssl3.dtlsSRTPCiphers[i], 2);
1419
0
        if (rv != SECSuccess) {
1420
0
            return SECFailure;
1421
0
        }
1422
0
    }
1423
    /* Empty MKI value */
1424
0
    rv = sslBuffer_AppendNumber(buf, 0, 1);
1425
0
    if (rv != SECSuccess) {
1426
0
        return SECFailure;
1427
0
    }
1428
1429
0
    *added = PR_TRUE;
1430
0
    return SECSuccess;
1431
0
}
1432
1433
SECStatus
1434
ssl3_ServerSendUseSRTPXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1435
                          sslBuffer *buf, PRBool *added)
1436
0
{
1437
0
    SECStatus rv;
1438
1439
    /* Length of the SRTP cipher list */
1440
0
    rv = sslBuffer_AppendNumber(buf, 2, 2);
1441
0
    if (rv != SECSuccess) {
1442
0
        return SECFailure;
1443
0
    }
1444
    /* The selected cipher */
1445
0
    rv = sslBuffer_AppendNumber(buf, xtnData->dtlsSRTPCipherSuite, 2);
1446
0
    if (rv != SECSuccess) {
1447
0
        return SECFailure;
1448
0
    }
1449
    /* Empty MKI value */
1450
0
    rv = sslBuffer_AppendNumber(buf, 0, 1);
1451
0
    if (rv != SECSuccess) {
1452
0
        return SECFailure;
1453
0
    }
1454
1455
0
    *added = PR_TRUE;
1456
0
    return SECSuccess;
1457
0
}
1458
1459
SECStatus
1460
ssl3_ClientHandleUseSRTPXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1461
                            SECItem *data)
1462
0
{
1463
0
    SECStatus rv;
1464
0
    SECItem ciphers = { siBuffer, NULL, 0 };
1465
0
    PRUint16 i;
1466
0
    PRUint16 cipher = 0;
1467
0
    PRBool found = PR_FALSE;
1468
0
    SECItem litem;
1469
1470
0
    if (!data->data || !data->len) {
1471
0
        ssl3_ExtDecodeError(ss);
1472
0
        return SECFailure;
1473
0
    }
1474
1475
    /* Get the cipher list */
1476
0
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &ciphers, 2,
1477
0
                                          &data->data, &data->len);
1478
0
    if (rv != SECSuccess) {
1479
0
        return SECFailure; /* fatal alert already sent */
1480
0
    }
1481
    /* Now check that the server has picked just 1 (i.e., len = 2) */
1482
0
    if (ciphers.len != 2) {
1483
0
        ssl3_ExtDecodeError(ss);
1484
0
        return SECFailure;
1485
0
    }
1486
1487
    /* Get the selected cipher */
1488
0
    cipher = (ciphers.data[0] << 8) | ciphers.data[1];
1489
1490
    /* Now check that this is one of the ciphers we offered */
1491
0
    for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) {
1492
0
        if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) {
1493
0
            found = PR_TRUE;
1494
0
            break;
1495
0
        }
1496
0
    }
1497
1498
0
    if (!found) {
1499
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1500
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_SERVER_HELLO);
1501
0
        return SECFailure;
1502
0
    }
1503
1504
    /* Get the srtp_mki value */
1505
0
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &litem, 1,
1506
0
                                          &data->data, &data->len);
1507
0
    if (rv != SECSuccess) {
1508
0
        return SECFailure; /* alert already sent */
1509
0
    }
1510
1511
    /* We didn't offer an MKI, so this must be 0 length */
1512
0
    if (litem.len != 0) {
1513
0
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1514
0
        PORT_SetError(SSL_ERROR_RX_MALFORMED_SERVER_HELLO);
1515
0
        return SECFailure;
1516
0
    }
1517
1518
    /* extra trailing bytes */
1519
0
    if (data->len != 0) {
1520
0
        ssl3_ExtDecodeError(ss);
1521
0
        return SECFailure;
1522
0
    }
1523
1524
    /* OK, this looks fine. */
1525
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_use_srtp_xtn;
1526
0
    xtnData->dtlsSRTPCipherSuite = cipher;
1527
0
    return SECSuccess;
1528
0
}
1529
1530
SECStatus
1531
ssl3_ServerHandleUseSRTPXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1532
                            SECItem *data)
1533
70
{
1534
70
    SECStatus rv;
1535
70
    SECItem ciphers = { siBuffer, NULL, 0 };
1536
70
    PRUint16 i;
1537
70
    unsigned int j;
1538
70
    PRUint16 cipher = 0;
1539
70
    PRBool found = PR_FALSE;
1540
70
    SECItem litem;
1541
1542
70
    if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount) {
1543
        /* Ignore the extension if we aren't doing DTLS or no DTLS-SRTP
1544
         * preferences have been set. */
1545
70
        return SECSuccess;
1546
70
    }
1547
1548
0
    if (!data->data || data->len < 5) {
1549
0
        ssl3_ExtDecodeError(ss);
1550
0
        return SECFailure;
1551
0
    }
1552
1553
    /* Get the cipher list */
1554
0
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &ciphers, 2,
1555
0
                                          &data->data, &data->len);
1556
0
    if (rv != SECSuccess) {
1557
0
        return SECFailure; /* alert already sent */
1558
0
    }
1559
    /* Check that the list is even length */
1560
0
    if (ciphers.len % 2) {
1561
0
        ssl3_ExtDecodeError(ss);
1562
0
        return SECFailure;
1563
0
    }
1564
1565
    /* Walk through the offered list and pick the most preferred of our
1566
     * ciphers, if any */
1567
0
    for (i = 0; !found && i < ss->ssl3.dtlsSRTPCipherCount; i++) {
1568
0
        for (j = 0; j + 1 < ciphers.len; j += 2) {
1569
0
            cipher = (ciphers.data[j] << 8) | ciphers.data[j + 1];
1570
0
            if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) {
1571
0
                found = PR_TRUE;
1572
0
                break;
1573
0
            }
1574
0
        }
1575
0
    }
1576
1577
    /* Get the srtp_mki value */
1578
0
    rv = ssl3_ExtConsumeHandshakeVariable(ss, &litem, 1, &data->data, &data->len);
1579
0
    if (rv != SECSuccess) {
1580
0
        return SECFailure;
1581
0
    }
1582
1583
0
    if (data->len != 0) {
1584
0
        ssl3_ExtDecodeError(ss); /* trailing bytes */
1585
0
        return SECFailure;
1586
0
    }
1587
1588
    /* Now figure out what to do */
1589
0
    if (!found) {
1590
        /* No matching ciphers, pretend we don't support use_srtp */
1591
0
        return SECSuccess;
1592
0
    }
1593
1594
    /* OK, we have a valid cipher and we've selected it */
1595
0
    xtnData->dtlsSRTPCipherSuite = cipher;
1596
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_use_srtp_xtn;
1597
1598
0
    return ssl3_RegisterExtensionSender(ss, xtnData,
1599
0
                                        ssl_use_srtp_xtn,
1600
0
                                        ssl3_ServerSendUseSRTPXtn);
1601
0
}
1602
1603
/* ssl3_HandleSigAlgsXtn handles the signature_algorithms extension from a
1604
 * client.  In TLS 1.3, the client uses this to parse CertificateRequest
1605
 * extensions.  See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1606
SECStatus
1607
ssl3_HandleSigAlgsXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1608
                      SECItem *data)
1609
6.85k
{
1610
6.85k
    SECStatus rv;
1611
1612
    /* Ignore this extension if we aren't doing TLS 1.2 or greater. */
1613
6.85k
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2) {
1614
562
        return SECSuccess;
1615
562
    }
1616
1617
6.28k
    if (xtnData->sigSchemes) {
1618
0
        PORT_Free(xtnData->sigSchemes);
1619
0
        xtnData->sigSchemes = NULL;
1620
0
    }
1621
6.28k
    rv = ssl_ParseSignatureSchemes(ss, NULL,
1622
6.28k
                                   &xtnData->sigSchemes,
1623
6.28k
                                   &xtnData->numSigSchemes,
1624
6.28k
                                   &data->data, &data->len);
1625
6.28k
    if (rv != SECSuccess) {
1626
26
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1627
26
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1628
26
        return SECFailure;
1629
26
    }
1630
6.26k
    if (xtnData->numSigSchemes == 0) {
1631
137
        ssl3_ExtSendAlert(ss, alert_fatal, handshake_failure);
1632
137
        PORT_SetError(SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM);
1633
137
        return SECFailure;
1634
137
    }
1635
    /* Check for trailing data. */
1636
6.12k
    if (data->len != 0) {
1637
40
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1638
40
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1639
40
        return SECFailure;
1640
40
    }
1641
1642
    /* Keep track of negotiated extensions. */
1643
6.08k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_signature_algorithms_xtn;
1644
6.08k
    return SECSuccess;
1645
6.12k
}
1646
1647
/* ssl3_ClientSendSigAlgsXtn sends the signature_algorithm extension for TLS
1648
 * 1.2 ClientHellos. */
1649
SECStatus
1650
ssl3_SendSigAlgsXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1651
                    sslBuffer *buf, PRBool *added)
1652
74.8k
{
1653
74.8k
    if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_2) {
1654
18.9k
        return SECSuccess;
1655
18.9k
    }
1656
1657
55.8k
    PRUint16 minVersion;
1658
55.8k
    if (ss->sec.isServer) {
1659
1.07k
        minVersion = ss->version; /* CertificateRequest */
1660
54.8k
    } else {
1661
54.8k
        minVersion = ss->vrange.min; /* ClientHello */
1662
54.8k
    }
1663
1664
55.8k
    SECStatus rv = ssl3_EncodeSigAlgs(ss, minVersion, PR_TRUE /* forCert */,
1665
55.8k
                                      ss->opt.enableGrease, buf);
1666
55.8k
    if (rv != SECSuccess) {
1667
0
        return SECFailure;
1668
0
    }
1669
1670
55.8k
    *added = PR_TRUE;
1671
55.8k
    return SECSuccess;
1672
55.8k
}
1673
1674
SECStatus
1675
ssl3_SendExtendedMasterSecretXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1676
                                 sslBuffer *buf, PRBool *added)
1677
73.7k
{
1678
73.7k
    if (!ss->opt.enableExtendedMS) {
1679
37.2k
        return SECSuccess;
1680
37.2k
    }
1681
1682
    /* Always send the extension in this function, since the
1683
     * client always sends it and this function is only called on
1684
     * the server if we negotiated the extension. */
1685
36.4k
    *added = PR_TRUE;
1686
36.4k
    return SECSuccess;
1687
73.7k
}
1688
1689
SECStatus
1690
ssl3_HandleExtendedMasterSecretXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1691
                                   SECItem *data)
1692
11.6k
{
1693
11.6k
    PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3);
1694
1695
11.6k
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_0) {
1696
0
        return SECSuccess;
1697
0
    }
1698
1699
11.6k
    if (!ss->opt.enableExtendedMS) {
1700
5.36k
        return SECSuccess;
1701
5.36k
    }
1702
1703
6.29k
    if (data->len != 0) {
1704
10
        SSL_TRC(30, ("%d: SSL3[%d]: Bogus extended master secret extension",
1705
10
                     SSL_GETPID(), ss->fd));
1706
10
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1707
10
        return SECFailure;
1708
10
    }
1709
1710
6.28k
    SSL_DBG(("%d: SSL[%d]: Negotiated extended master secret extension.",
1711
6.28k
             SSL_GETPID(), ss->fd));
1712
1713
    /* Keep track of negotiated extensions. */
1714
6.28k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_extended_master_secret_xtn;
1715
1716
6.28k
    if (ss->sec.isServer) {
1717
6.09k
        return ssl3_RegisterExtensionSender(ss, xtnData,
1718
6.09k
                                            ssl_extended_master_secret_xtn,
1719
6.09k
                                            ssl_SendEmptyExtension);
1720
6.09k
    }
1721
191
    return SECSuccess;
1722
6.28k
}
1723
1724
/* ssl3_ClientSendSignedCertTimestampXtn sends the signed_certificate_timestamp
1725
 * extension for TLS ClientHellos. */
1726
SECStatus
1727
ssl3_ClientSendSignedCertTimestampXtn(const sslSocket *ss,
1728
                                      TLSExtensionData *xtnData,
1729
                                      sslBuffer *buf, PRBool *added)
1730
73.7k
{
1731
    /* Only send the extension if processing is enabled. */
1732
73.7k
    if (!ss->opt.enableSignedCertTimestamps) {
1733
73.7k
        return SECSuccess;
1734
73.7k
    }
1735
1736
0
    *added = PR_TRUE;
1737
0
    return SECSuccess;
1738
73.7k
}
1739
1740
SECStatus
1741
ssl3_ClientHandleSignedCertTimestampXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1742
                                        SECItem *data)
1743
0
{
1744
    /* We do not yet know whether we'll be resuming a session or creating
1745
     * a new one, so we keep a pointer to the data in the TLSExtensionData
1746
     * structure. This pointer is only valid in the scope of
1747
     * ssl3_HandleServerHello, and, if not resuming a session, the data is
1748
     * copied once a new session structure has been set up.
1749
     * All parsing is currently left to the application and we accept
1750
     * everything, including empty data.
1751
     */
1752
0
    SECItem *scts = &xtnData->signedCertTimestamps;
1753
0
    PORT_Assert(!scts->data && !scts->len);
1754
1755
0
    if (!data->len) {
1756
        /* Empty extension data: RFC 6962 mandates non-empty contents. */
1757
0
        return SECFailure;
1758
0
    }
1759
0
    *scts = *data;
1760
    /* Keep track of negotiated extensions. */
1761
0
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_signed_cert_timestamp_xtn;
1762
0
    return SECSuccess;
1763
0
}
1764
1765
SECStatus
1766
ssl3_ServerSendSignedCertTimestampXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1767
                                      sslBuffer *buf, PRBool *added)
1768
10.0k
{
1769
10.0k
    const SECItem *scts = &ss->sec.serverCert->signedCertTimestamps;
1770
10.0k
    SECStatus rv;
1771
1772
10.0k
    if (!scts->len) {
1773
        /* No timestamps to send */
1774
10.0k
        return SECSuccess;
1775
10.0k
    }
1776
1777
0
    rv = sslBuffer_Append(buf, scts->data, scts->len);
1778
0
    if (rv != SECSuccess) {
1779
0
        return SECFailure;
1780
0
    }
1781
1782
0
    *added = PR_TRUE;
1783
0
    return SECSuccess;
1784
0
}
1785
1786
SECStatus
1787
ssl3_ServerHandleSignedCertTimestampXtn(const sslSocket *ss,
1788
                                        TLSExtensionData *xtnData,
1789
                                        SECItem *data)
1790
10.1k
{
1791
10.1k
    if (data->len != 0) {
1792
13
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1793
13
        PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
1794
13
        return SECFailure;
1795
13
    }
1796
1797
10.1k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_signed_cert_timestamp_xtn;
1798
10.1k
    PORT_Assert(ss->sec.isServer);
1799
10.1k
    return ssl3_RegisterExtensionSender(ss, xtnData,
1800
10.1k
                                        ssl_signed_cert_timestamp_xtn,
1801
10.1k
                                        ssl3_ServerSendSignedCertTimestampXtn);
1802
10.1k
}
1803
1804
/* Just make sure that the remote client supports uncompressed points,
1805
 * Since that is all we support.  Disable ECC cipher suites if it doesn't.
1806
 */
1807
SECStatus
1808
ssl3_HandleSupportedPointFormatsXtn(const sslSocket *ss,
1809
                                    TLSExtensionData *xtnData,
1810
                                    SECItem *data)
1811
8.93k
{
1812
8.93k
    int i;
1813
1814
8.93k
    PORT_Assert(ss->version < SSL_LIBRARY_VERSION_TLS_1_3);
1815
1816
8.93k
    if (data->len < 2 || data->len > 255 || !data->data ||
1817
8.93k
        data->len != (unsigned int)data->data[0] + 1) {
1818
33
        ssl3_ExtDecodeError(ss);
1819
33
        return SECFailure;
1820
33
    }
1821
9.60k
    for (i = data->len; --i > 0;) {
1822
9.58k
        if (data->data[i] == 0) {
1823
            /* indicate that we should send a reply */
1824
8.88k
            return ssl3_RegisterExtensionSender(
1825
8.88k
                ss, xtnData, ssl_ec_point_formats_xtn,
1826
8.88k
                &ssl3_SendSupportedPointFormatsXtn);
1827
8.88k
        }
1828
9.58k
    }
1829
1830
    /* Poor client doesn't support uncompressed points.
1831
     *
1832
     * If the client sends the extension and the extension does not contain the
1833
     * uncompressed point format, and the client has used the Supported Groups
1834
     * extension to indicate support for any of the curves defined in this
1835
     * specification, then the server MUST abort the handshake and return an
1836
     * illegal_parameter alert. [RFC8422, Section 5.1.2] */
1837
15
    ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1838
15
    PORT_SetError(SSL_ERROR_RX_MALFORMED_HANDSHAKE);
1839
1840
15
    return SECFailure;
1841
8.89k
}
1842
1843
static SECStatus
1844
ssl_UpdateSupportedGroups(sslSocket *ss, SECItem *data)
1845
15.5k
{
1846
15.5k
    SECStatus rv;
1847
15.5k
    PRUint32 list_len;
1848
15.5k
    unsigned int i;
1849
15.5k
    const sslNamedGroupDef *enabled[SSL_NAMED_GROUP_COUNT] = { 0 };
1850
15.5k
    PORT_Assert(SSL_NAMED_GROUP_COUNT == PR_ARRAY_SIZE(enabled));
1851
1852
15.5k
    if (!data->data || data->len < 4) {
1853
12
        (void)ssl3_DecodeError(ss);
1854
12
        return SECFailure;
1855
12
    }
1856
1857
    /* get the length of elliptic_curve_list */
1858
15.5k
    rv = ssl3_ConsumeHandshakeNumber(ss, &list_len, 2, &data->data, &data->len);
1859
15.5k
    if (rv != SECSuccess || data->len != list_len || (data->len % 2) != 0) {
1860
85
        (void)ssl3_DecodeError(ss);
1861
85
        return SECFailure;
1862
85
    }
1863
1864
    /* disable all groups and remember the enabled groups */
1865
524k
    for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) {
1866
509k
        enabled[i] = ss->namedGroupPreferences[i];
1867
509k
        ss->namedGroupPreferences[i] = NULL;
1868
509k
    }
1869
1870
    /* Read groups from data and enable if in |enabled| */
1871
79.8k
    while (data->len) {
1872
64.4k
        const sslNamedGroupDef *group;
1873
64.4k
        PRUint32 curve_name;
1874
64.4k
        rv = ssl3_ConsumeHandshakeNumber(ss, &curve_name, 2, &data->data,
1875
64.4k
                                         &data->len);
1876
64.4k
        if (rv != SECSuccess) {
1877
0
            return SECFailure; /* fatal alert already sent */
1878
0
        }
1879
64.4k
        group = ssl_LookupNamedGroup(curve_name);
1880
64.4k
        if (group) {
1881
416k
            for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) {
1882
407k
                if (enabled[i] && group == enabled[i]) {
1883
25.5k
                    ss->namedGroupPreferences[i] = enabled[i];
1884
25.5k
                    break;
1885
25.5k
                }
1886
407k
            }
1887
33.6k
        }
1888
1889
        /* "Codepoints in the NamedCurve registry with a high byte of 0x01 (that
1890
         * is, between 256 and 511 inclusive) are set aside for FFDHE groups,"
1891
         * -- https://tools.ietf.org/html/draft-ietf-tls-negotiated-ff-dhe-10
1892
         */
1893
64.4k
        if ((curve_name & 0xff00) == 0x0100) {
1894
14.2k
            ss->xtnData.peerSupportsFfdheGroups = PR_TRUE;
1895
14.2k
        }
1896
64.4k
    }
1897
1898
    /* Note: if ss->opt.requireDHENamedGroups is set, we disable DHE cipher
1899
     * suites, but we do that in ssl3_config_match(). */
1900
15.4k
    if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3 &&
1901
15.4k
        !ss->opt.requireDHENamedGroups && !ss->xtnData.peerSupportsFfdheGroups) {
1902
        /* If we don't require that DHE use named groups, and no FFDHE was
1903
         * included, we pretend that they support all the FFDHE groups we do. */
1904
81.7k
        for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) {
1905
79.3k
            if (enabled[i] && enabled[i]->keaType == ssl_kea_dh) {
1906
11.1k
                ss->namedGroupPreferences[i] = enabled[i];
1907
11.1k
            }
1908
79.3k
        }
1909
2.40k
    }
1910
1911
15.4k
    return SECSuccess;
1912
15.4k
}
1913
1914
/* Ensure that the curve in our server cert is one of the ones supported
1915
 * by the remote client, and disable all ECC cipher suites if not.
1916
 */
1917
SECStatus
1918
ssl_HandleSupportedGroupsXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1919
                             SECItem *data)
1920
15.5k
{
1921
15.5k
    SECStatus rv;
1922
1923
15.5k
    rv = ssl_UpdateSupportedGroups(CONST_CAST(sslSocket, ss), data);
1924
15.5k
    if (rv != SECSuccess)
1925
97
        return SECFailure;
1926
1927
    /* TLS 1.3 permits the server to send this extension so make it so. */
1928
15.4k
    if (ss->sec.isServer && ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) {
1929
3.88k
        rv = ssl3_RegisterExtensionSender(ss, xtnData, ssl_supported_groups_xtn,
1930
3.88k
                                          &ssl_SendSupportedGroupsXtn);
1931
3.88k
        if (rv != SECSuccess) {
1932
0
            return SECFailure; /* error already set. */
1933
0
        }
1934
3.88k
    }
1935
1936
    /* Remember that we negotiated this extension. */
1937
15.4k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_supported_groups_xtn;
1938
1939
15.4k
    return SECSuccess;
1940
15.4k
}
1941
1942
SECStatus
1943
ssl_HandleRecordSizeLimitXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1944
                             SECItem *data)
1945
5.11k
{
1946
5.11k
    SECStatus rv;
1947
5.11k
    PRUint32 limit;
1948
5.11k
    PRUint32 maxLimit = (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3)
1949
5.11k
                            ? (MAX_FRAGMENT_LENGTH + 1)
1950
5.11k
                            : MAX_FRAGMENT_LENGTH;
1951
1952
5.11k
    rv = ssl3_ExtConsumeHandshakeNumber(ss, &limit, 2, &data->data, &data->len);
1953
5.11k
    if (rv != SECSuccess) {
1954
12
        return SECFailure;
1955
12
    }
1956
5.09k
    if (data->len != 0 || limit < 64) {
1957
40
        ssl3_ExtSendAlert(ss, alert_fatal, decode_error);
1958
40
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HANDSHAKE);
1959
40
        return SECFailure;
1960
40
    }
1961
1962
5.05k
    if (ss->sec.isServer) {
1963
2.84k
        rv = ssl3_RegisterExtensionSender(ss, xtnData, ssl_record_size_limit_xtn,
1964
2.84k
                                          &ssl_SendRecordSizeLimitXtn);
1965
2.84k
        if (rv != SECSuccess) {
1966
0
            return SECFailure; /* error already set. */
1967
0
        }
1968
2.84k
    } else if (limit > maxLimit) {
1969
        /* The client can sensibly check the maximum. */
1970
51
        ssl3_ExtSendAlert(ss, alert_fatal, illegal_parameter);
1971
51
        PORT_SetError(SSL_ERROR_RX_MALFORMED_HANDSHAKE);
1972
51
        return SECFailure;
1973
51
    }
1974
1975
    /* We can't enforce the maximum on a server. But we do need to ensure
1976
     * that we don't apply a limit that is too large. */
1977
5.00k
    xtnData->recordSizeLimit = PR_MIN(maxLimit, limit);
1978
5.00k
    xtnData->negotiated[xtnData->numNegotiated++] = ssl_record_size_limit_xtn;
1979
5.00k
    return SECSuccess;
1980
5.05k
}
1981
1982
SECStatus
1983
ssl_SendRecordSizeLimitXtn(const sslSocket *ss, TLSExtensionData *xtnData,
1984
                           sslBuffer *buf, PRBool *added)
1985
75.8k
{
1986
75.8k
    PRUint32 maxLimit;
1987
75.8k
    if (ss->sec.isServer) {
1988
2.08k
        maxLimit = (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3)
1989
2.08k
                       ? (MAX_FRAGMENT_LENGTH + 1)
1990
2.08k
                       : MAX_FRAGMENT_LENGTH;
1991
73.7k
    } else {
1992
73.7k
        maxLimit = (ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3)
1993
73.7k
                       ? (MAX_FRAGMENT_LENGTH + 1)
1994
73.7k
                       : MAX_FRAGMENT_LENGTH;
1995
73.7k
    }
1996
75.8k
    PRUint32 limit = PR_MIN(ss->opt.recordSizeLimit, maxLimit);
1997
75.8k
    SECStatus rv = sslBuffer_AppendNumber(buf, limit, 2);
1998
75.8k
    if (rv != SECSuccess) {
1999
0
        return SECFailure;
2000
0
    }
2001
2002
75.8k
    *added = PR_TRUE;
2003
75.8k
    return SECSuccess;
2004
75.8k
}