Coverage Report

Created: 2018-09-25 14:53

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