4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 /* TLS extension code moved here from ssl3ecc.c */
16 #ifdef NO_PKCS11_BYPASS
23 static unsigned char key_name
[SESS_TICKET_KEY_NAME_LEN
];
24 static PK11SymKey
*session_ticket_enc_key_pkcs11
= NULL
;
25 static PK11SymKey
*session_ticket_mac_key_pkcs11
= NULL
;
27 #ifndef NO_PKCS11_BYPASS
28 static unsigned char session_ticket_enc_key
[AES_256_KEY_LENGTH
];
29 static unsigned char session_ticket_mac_key
[SHA256_LENGTH
];
31 static PRBool session_ticket_keys_initialized
= PR_FALSE
;
33 static PRCallOnceType generate_session_keys_once
;
35 /* forward static function declarations */
36 static SECStatus
ssl3_ParseEncryptedSessionTicket(sslSocket
*ss
,
37 SECItem
*data
, EncryptedSessionTicket
*enc_session_ticket
);
38 static SECStatus
ssl3_AppendToItem(SECItem
*item
, const unsigned char *buf
,
40 static SECStatus
ssl3_AppendNumberToItem(SECItem
*item
, PRUint32 num
,
42 static SECStatus
ssl3_GetSessionTicketKeysPKCS11(sslSocket
*ss
,
43 PK11SymKey
**aes_key
, PK11SymKey
**mac_key
);
44 #ifndef NO_PKCS11_BYPASS
45 static SECStatus
ssl3_GetSessionTicketKeys(const unsigned char **aes_key
,
46 PRUint32
*aes_key_length
, const unsigned char **mac_key
,
47 PRUint32
*mac_key_length
);
49 static PRInt32
ssl3_SendRenegotiationInfoXtn(sslSocket
* ss
,
50 PRBool append
, PRUint32 maxBytes
);
51 static SECStatus
ssl3_HandleRenegotiationInfoXtn(sslSocket
*ss
,
52 PRUint16 ex_type
, SECItem
*data
);
53 static SECStatus
ssl3_ClientHandleNextProtoNegoXtn(sslSocket
*ss
,
54 PRUint16 ex_type
, SECItem
*data
);
55 static SECStatus
ssl3_ClientHandleAppProtoXtn(sslSocket
*ss
,
56 PRUint16 ex_type
, SECItem
*data
);
57 static SECStatus
ssl3_ServerHandleNextProtoNegoXtn(sslSocket
*ss
,
58 PRUint16 ex_type
, SECItem
*data
);
59 static PRInt32
ssl3_ClientSendAppProtoXtn(sslSocket
*ss
, PRBool append
,
61 static PRInt32
ssl3_ClientSendNextProtoNegoXtn(sslSocket
*ss
, PRBool append
,
63 static PRInt32
ssl3_SendUseSRTPXtn(sslSocket
*ss
, PRBool append
,
65 static SECStatus
ssl3_HandleUseSRTPXtn(sslSocket
* ss
, PRUint16 ex_type
,
67 static SECStatus
ssl3_ClientHandleChannelIDXtn(sslSocket
*ss
,
68 PRUint16 ex_type
, SECItem
*data
);
69 static PRInt32
ssl3_ClientSendChannelIDXtn(sslSocket
*ss
, PRBool append
,
71 static SECStatus
ssl3_ServerSendStatusRequestXtn(sslSocket
* ss
,
72 PRBool append
, PRUint32 maxBytes
);
73 static SECStatus
ssl3_ServerHandleStatusRequestXtn(sslSocket
*ss
,
74 PRUint16 ex_type
, SECItem
*data
);
75 static SECStatus
ssl3_ClientHandleStatusRequestXtn(sslSocket
*ss
,
78 static PRInt32
ssl3_ClientSendStatusRequestXtn(sslSocket
* ss
, PRBool append
,
80 static PRInt32
ssl3_ClientSendSigAlgsXtn(sslSocket
*ss
, PRBool append
,
82 static SECStatus
ssl3_ServerHandleSigAlgsXtn(sslSocket
*ss
, PRUint16 ex_type
,
84 static PRInt32
ssl3_ClientSendSignedCertTimestampXtn(sslSocket
*ss
,
87 static SECStatus
ssl3_ClientHandleSignedCertTimestampXtn(sslSocket
*ss
,
92 * Write bytes. Using this function means the SECItem structure
93 * cannot be freed. The caller is expected to call this function
94 * on a shallow copy of the structure.
97 ssl3_AppendToItem(SECItem
*item
, const unsigned char *buf
, PRUint32 bytes
)
99 if (bytes
> item
->len
)
102 PORT_Memcpy(item
->data
, buf
, bytes
);
109 * Write a number in network byte order. Using this function means the
110 * SECItem structure cannot be freed. The caller is expected to call
111 * this function on a shallow copy of the structure.
114 ssl3_AppendNumberToItem(SECItem
*item
, PRUint32 num
, PRInt32 lenSize
)
122 *p
++ = (PRUint8
) (num
>> 24);
124 *p
++ = (PRUint8
) (num
>> 16);
126 *p
++ = (PRUint8
) (num
>> 8);
130 rv
= ssl3_AppendToItem(item
, &b
[0], lenSize
);
134 static SECStatus
ssl3_SessionTicketShutdown(void* appData
, void* nssData
)
136 if (session_ticket_enc_key_pkcs11
) {
137 PK11_FreeSymKey(session_ticket_enc_key_pkcs11
);
138 session_ticket_enc_key_pkcs11
= NULL
;
140 if (session_ticket_mac_key_pkcs11
) {
141 PK11_FreeSymKey(session_ticket_mac_key_pkcs11
);
142 session_ticket_mac_key_pkcs11
= NULL
;
144 PORT_Memset(&generate_session_keys_once
, 0,
145 sizeof(generate_session_keys_once
));
151 ssl3_GenerateSessionTicketKeysPKCS11(void *data
)
154 sslSocket
*ss
= (sslSocket
*)data
;
155 SECKEYPrivateKey
*svrPrivKey
= ss
->serverCerts
[kt_rsa
].SERVERKEY
;
156 SECKEYPublicKey
*svrPubKey
= ss
->serverCerts
[kt_rsa
].serverKeyPair
->pubKey
;
158 if (svrPrivKey
== NULL
|| svrPubKey
== NULL
) {
159 SSL_DBG(("%d: SSL[%d]: Pub or priv key(s) is NULL.",
160 SSL_GETPID(), ss
->fd
));
164 /* Get a copy of the session keys from shared memory. */
165 PORT_Memcpy(key_name
, SESS_TICKET_KEY_NAME_PREFIX
,
166 sizeof(SESS_TICKET_KEY_NAME_PREFIX
));
167 if (!ssl_GetSessionTicketKeysPKCS11(svrPrivKey
, svrPubKey
,
168 ss
->pkcs11PinArg
, &key_name
[SESS_TICKET_KEY_NAME_PREFIX_LEN
],
169 &session_ticket_enc_key_pkcs11
, &session_ticket_mac_key_pkcs11
))
172 rv
= NSS_RegisterShutdown(ssl3_SessionTicketShutdown
, NULL
);
173 if (rv
!= SECSuccess
)
179 ssl3_SessionTicketShutdown(NULL
, NULL
);
184 ssl3_GetSessionTicketKeysPKCS11(sslSocket
*ss
, PK11SymKey
**aes_key
,
185 PK11SymKey
**mac_key
)
187 if (PR_CallOnceWithArg(&generate_session_keys_once
,
188 ssl3_GenerateSessionTicketKeysPKCS11
, ss
) != PR_SUCCESS
)
191 if (session_ticket_enc_key_pkcs11
== NULL
||
192 session_ticket_mac_key_pkcs11
== NULL
)
195 *aes_key
= session_ticket_enc_key_pkcs11
;
196 *mac_key
= session_ticket_mac_key_pkcs11
;
200 #ifndef NO_PKCS11_BYPASS
202 ssl3_GenerateSessionTicketKeys(void)
204 PORT_Memcpy(key_name
, SESS_TICKET_KEY_NAME_PREFIX
,
205 sizeof(SESS_TICKET_KEY_NAME_PREFIX
));
207 if (!ssl_GetSessionTicketKeys(&key_name
[SESS_TICKET_KEY_NAME_PREFIX_LEN
],
208 session_ticket_enc_key
, session_ticket_mac_key
))
211 session_ticket_keys_initialized
= PR_TRUE
;
216 ssl3_GetSessionTicketKeys(const unsigned char **aes_key
,
217 PRUint32
*aes_key_length
, const unsigned char **mac_key
,
218 PRUint32
*mac_key_length
)
220 if (PR_CallOnce(&generate_session_keys_once
,
221 ssl3_GenerateSessionTicketKeys
) != PR_SUCCESS
)
224 if (!session_ticket_keys_initialized
)
227 *aes_key
= session_ticket_enc_key
;
228 *aes_key_length
= sizeof(session_ticket_enc_key
);
229 *mac_key
= session_ticket_mac_key
;
230 *mac_key_length
= sizeof(session_ticket_mac_key
);
236 /* Table of handlers for received TLS hello extensions, one per extension.
237 * In the second generation, this table will be dynamic, and functions
238 * will be registered here.
240 /* This table is used by the server, to handle client hello extensions. */
241 static const ssl3HelloExtensionHandler clientHelloHandlers
[] = {
242 { ssl_server_name_xtn
, &ssl3_HandleServerNameXtn
},
243 #ifdef NSS_ENABLE_ECC
244 { ssl_elliptic_curves_xtn
, &ssl3_HandleSupportedCurvesXtn
},
245 { ssl_ec_point_formats_xtn
, &ssl3_HandleSupportedPointFormatsXtn
},
247 { ssl_session_ticket_xtn
, &ssl3_ServerHandleSessionTicketXtn
},
248 { ssl_renegotiation_info_xtn
, &ssl3_HandleRenegotiationInfoXtn
},
249 { ssl_next_proto_nego_xtn
, &ssl3_ServerHandleNextProtoNegoXtn
},
250 { ssl_use_srtp_xtn
, &ssl3_HandleUseSRTPXtn
},
251 { ssl_cert_status_xtn
, &ssl3_ServerHandleStatusRequestXtn
},
252 { ssl_signature_algorithms_xtn
, &ssl3_ServerHandleSigAlgsXtn
},
256 /* These two tables are used by the client, to handle server hello
258 static const ssl3HelloExtensionHandler serverHelloHandlersTLS
[] = {
259 { ssl_server_name_xtn
, &ssl3_HandleServerNameXtn
},
260 /* TODO: add a handler for ssl_ec_point_formats_xtn */
261 { ssl_session_ticket_xtn
, &ssl3_ClientHandleSessionTicketXtn
},
262 { ssl_renegotiation_info_xtn
, &ssl3_HandleRenegotiationInfoXtn
},
263 { ssl_next_proto_nego_xtn
, &ssl3_ClientHandleNextProtoNegoXtn
},
264 { ssl_app_layer_protocol_xtn
, &ssl3_ClientHandleAppProtoXtn
},
265 { ssl_use_srtp_xtn
, &ssl3_HandleUseSRTPXtn
},
266 { ssl_channel_id_xtn
, &ssl3_ClientHandleChannelIDXtn
},
267 { ssl_cert_status_xtn
, &ssl3_ClientHandleStatusRequestXtn
},
268 { ssl_signed_certificate_timestamp_xtn
,
269 &ssl3_ClientHandleSignedCertTimestampXtn
},
273 static const ssl3HelloExtensionHandler serverHelloHandlersSSL3
[] = {
274 { ssl_renegotiation_info_xtn
, &ssl3_HandleRenegotiationInfoXtn
},
278 /* Tables of functions to format TLS hello extensions, one function per
280 * These static tables are for the formatting of client hello extensions.
281 * The server's table of hello senders is dynamic, in the socket struct,
282 * and sender functions are registered there.
285 ssl3HelloExtensionSender clientHelloSendersTLS
[SSL_MAX_EXTENSIONS
] = {
286 { ssl_server_name_xtn
, &ssl3_SendServerNameXtn
},
287 { ssl_renegotiation_info_xtn
, &ssl3_SendRenegotiationInfoXtn
},
288 #ifdef NSS_ENABLE_ECC
289 { ssl_elliptic_curves_xtn
, &ssl3_SendSupportedCurvesXtn
},
290 { ssl_ec_point_formats_xtn
, &ssl3_SendSupportedPointFormatsXtn
},
292 { ssl_session_ticket_xtn
, &ssl3_SendSessionTicketXtn
},
293 { ssl_next_proto_nego_xtn
, &ssl3_ClientSendNextProtoNegoXtn
},
294 { ssl_app_layer_protocol_xtn
, &ssl3_ClientSendAppProtoXtn
},
295 { ssl_use_srtp_xtn
, &ssl3_SendUseSRTPXtn
},
296 { ssl_channel_id_xtn
, &ssl3_ClientSendChannelIDXtn
},
297 { ssl_cert_status_xtn
, &ssl3_ClientSendStatusRequestXtn
},
298 { ssl_signed_certificate_timestamp_xtn
,
299 &ssl3_ClientSendSignedCertTimestampXtn
},
300 /* WebSphere Application Server 7.0 is intolerant to the last extension
301 * being zero-length. It is not intolerant of TLS 1.2, so move
302 * signature_algorithms to the end. */
303 { ssl_signature_algorithms_xtn
, &ssl3_ClientSendSigAlgsXtn
}
304 /* any extra entries will appear as { 0, NULL } */
308 ssl3HelloExtensionSender clientHelloSendersSSL3
[SSL_MAX_EXTENSIONS
] = {
309 { ssl_renegotiation_info_xtn
, &ssl3_SendRenegotiationInfoXtn
}
310 /* any extra entries will appear as { 0, NULL } */
314 arrayContainsExtension(const PRUint16
*array
, PRUint32 len
, PRUint16 ex_type
)
317 for (i
= 0; i
< len
; i
++) {
318 if (ex_type
== array
[i
])
325 ssl3_ExtensionNegotiated(sslSocket
*ss
, PRUint16 ex_type
) {
326 TLSExtensionData
*xtnData
= &ss
->xtnData
;
327 return arrayContainsExtension(xtnData
->negotiated
,
328 xtnData
->numNegotiated
, ex_type
);
332 ssl3_ClientExtensionAdvertised(sslSocket
*ss
, PRUint16 ex_type
) {
333 TLSExtensionData
*xtnData
= &ss
->xtnData
;
334 return arrayContainsExtension(xtnData
->advertised
,
335 xtnData
->numAdvertised
, ex_type
);
338 /* Format an SNI extension, using the name from the socket's URL,
339 * unless that name is a dotted decimal string.
340 * Used by client and server.
343 ssl3_SendServerNameXtn(sslSocket
* ss
, PRBool append
,
349 if (!ss
->sec
.isServer
) {
353 /* must have a hostname */
354 if (!ss
->url
|| !ss
->url
[0])
356 /* must not be an IPv4 or IPv6 address */
357 if (PR_SUCCESS
== PR_StringToNetAddr(ss
->url
, &netAddr
)) {
358 /* is an IP address (v4 or v6) */
361 len
= PORT_Strlen(ss
->url
);
362 if (append
&& maxBytes
>= len
+ 9) {
364 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_server_name_xtn
, 2);
365 if (rv
!= SECSuccess
) return -1;
366 /* length of extension_data */
367 rv
= ssl3_AppendHandshakeNumber(ss
, len
+ 5, 2);
368 if (rv
!= SECSuccess
) return -1;
369 /* length of server_name_list */
370 rv
= ssl3_AppendHandshakeNumber(ss
, len
+ 3, 2);
371 if (rv
!= SECSuccess
) return -1;
372 /* Name Type (sni_host_name) */
373 rv
= ssl3_AppendHandshake(ss
, "\0", 1);
374 if (rv
!= SECSuccess
) return -1;
375 /* HostName (length and value) */
376 rv
= ssl3_AppendHandshakeVariable(ss
, (PRUint8
*)ss
->url
, len
, 2);
377 if (rv
!= SECSuccess
) return -1;
378 if (!ss
->sec
.isServer
) {
379 TLSExtensionData
*xtnData
= &ss
->xtnData
;
380 xtnData
->advertised
[xtnData
->numAdvertised
++] =
387 if (append
&& maxBytes
>= 4) {
388 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_server_name_xtn
, 2);
389 if (rv
!= SECSuccess
) return -1;
390 /* length of extension_data */
391 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
392 if (rv
!= SECSuccess
) return -1;
397 /* handle an incoming SNI extension, by ignoring it. */
399 ssl3_HandleServerNameXtn(sslSocket
* ss
, PRUint16 ex_type
, SECItem
*data
)
401 SECItem
*names
= NULL
;
402 PRUint32 listCount
= 0, namesPos
= 0, i
;
403 TLSExtensionData
*xtnData
= &ss
->xtnData
;
405 PRInt32 listLenBytes
= 0;
407 if (!ss
->sec
.isServer
) {
408 /* Verify extension_data is empty. */
409 if (data
->data
|| data
->len
||
410 !ssl3_ExtensionNegotiated(ss
, ssl_server_name_xtn
)) {
411 /* malformed or was not initiated by the client.*/
417 /* Server side - consume client data and register server sender. */
418 /* do not parse the data if don't have user extension handling function. */
419 if (!ss
->sniSocketConfig
) {
422 /* length of server_name_list */
423 listLenBytes
= ssl3_ConsumeHandshakeNumber(ss
, 2, &data
->data
, &data
->len
);
424 if (listLenBytes
== 0 || listLenBytes
!= data
->len
) {
428 /* Calculate the size of the array.*/
429 while (listLenBytes
> 0) {
433 /* Name Type (sni_host_name) */
434 type
= ssl3_ConsumeHandshakeNumber(ss
, 1, &ldata
.data
, &ldata
.len
);
438 rv
= ssl3_ConsumeHandshakeVariable(ss
, &litem
, 2, &ldata
.data
, &ldata
.len
);
439 if (rv
!= SECSuccess
) {
442 /* Adjust total length for cunsumed item, item len and type.*/
443 listLenBytes
-= litem
.len
+ 3;
444 if (listLenBytes
> 0 && !ldata
.len
) {
452 names
= PORT_ZNewArray(SECItem
, listCount
);
456 for (i
= 0;i
< listCount
;i
++) {
460 PRBool nametypePresent
= PR_FALSE
;
461 /* Name Type (sni_host_name) */
462 type
= ssl3_ConsumeHandshakeNumber(ss
, 1, &data
->data
, &data
->len
);
463 /* Check if we have such type in the list */
464 for (j
= 0;j
< listCount
&& names
[j
].data
;j
++) {
465 if (names
[j
].type
== type
) {
466 nametypePresent
= PR_TRUE
;
470 /* HostName (length and value) */
471 rv
= ssl3_ConsumeHandshakeVariable(ss
, &names
[namesPos
], 2,
472 &data
->data
, &data
->len
);
473 if (rv
!= SECSuccess
) {
476 if (nametypePresent
== PR_FALSE
) {
480 /* Free old and set the new data. */
481 if (xtnData
->sniNameArr
) {
482 PORT_Free(ss
->xtnData
.sniNameArr
);
484 xtnData
->sniNameArr
= names
;
485 xtnData
->sniNameArrSize
= namesPos
;
486 xtnData
->negotiated
[xtnData
->numNegotiated
++] = ssl_server_name_xtn
;
495 /* Called by both clients and servers.
496 * Clients sends a filled in session ticket if one is available, and otherwise
497 * sends an empty ticket. Servers always send empty tickets.
500 ssl3_SendSessionTicketXtn(
505 PRInt32 extension_length
;
506 NewSessionTicket
*session_ticket
= NULL
;
507 sslSessionID
*sid
= ss
->sec
.ci
.sid
;
509 /* Ignore the SessionTicket extension if processing is disabled. */
510 if (!ss
->opt
.enableSessionTickets
)
513 /* Empty extension length = extension_type (2-bytes) +
514 * length(extension_data) (2-bytes)
516 extension_length
= 4;
518 /* If we are a client then send a session ticket if one is availble.
519 * Servers that support the extension and are willing to negotiate the
520 * the extension always respond with an empty extension.
522 if (!ss
->sec
.isServer
) {
523 /* The caller must be holding sid->u.ssl3.lock for reading. We cannot
524 * just acquire and release the lock within this function because the
525 * caller will call this function twice, and we need the inputs to be
526 * consistent between the two calls. Note that currently the caller
527 * will only be holding the lock when we are the client and when we're
528 * attempting to resume an existing session.
531 session_ticket
= &sid
->u
.ssl3
.locked
.sessionTicket
;
532 if (session_ticket
->ticket
.data
) {
533 if (ss
->xtnData
.ticketTimestampVerified
) {
534 extension_length
+= session_ticket
->ticket
.len
;
535 } else if (!append
&&
536 (session_ticket
->ticket_lifetime_hint
== 0 ||
537 (session_ticket
->ticket_lifetime_hint
+
538 session_ticket
->received_timestamp
> ssl_Time()))) {
539 extension_length
+= session_ticket
->ticket
.len
;
540 ss
->xtnData
.ticketTimestampVerified
= PR_TRUE
;
545 if (append
&& maxBytes
>= extension_length
) {
548 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_session_ticket_xtn
, 2);
549 if (rv
!= SECSuccess
)
551 if (session_ticket
&& session_ticket
->ticket
.data
&&
552 ss
->xtnData
.ticketTimestampVerified
) {
553 rv
= ssl3_AppendHandshakeVariable(ss
, session_ticket
->ticket
.data
,
554 session_ticket
->ticket
.len
, 2);
555 ss
->xtnData
.ticketTimestampVerified
= PR_FALSE
;
556 ss
->xtnData
.sentSessionTicketInClientHello
= PR_TRUE
;
558 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
560 if (rv
!= SECSuccess
)
563 if (!ss
->sec
.isServer
) {
564 TLSExtensionData
*xtnData
= &ss
->xtnData
;
565 xtnData
->advertised
[xtnData
->numAdvertised
++] =
566 ssl_session_ticket_xtn
;
568 } else if (maxBytes
< extension_length
) {
572 return extension_length
;
575 ss
->xtnData
.ticketTimestampVerified
= PR_FALSE
;
579 /* handle an incoming Next Protocol Negotiation extension. */
581 ssl3_ServerHandleNextProtoNegoXtn(sslSocket
* ss
, PRUint16 ex_type
, SECItem
*data
)
583 if (ss
->firstHsDone
|| data
->len
!= 0) {
584 /* Clients MUST send an empty NPN extension, if any. */
585 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID
);
589 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
591 /* TODO: server side NPN support would require calling
592 * ssl3_RegisterServerHelloExtensionSender here in order to echo the
593 * extension back to the client. */
598 /* ssl3_ValidateNextProtoNego checks that the given block of data is valid: none
599 * of the lengths may be 0 and the sum of the lengths must equal the length of
602 ssl3_ValidateNextProtoNego(const unsigned char* data
, unsigned int length
)
604 unsigned int offset
= 0;
606 while (offset
< length
) {
607 unsigned int newOffset
= offset
+ 1 + (unsigned int) data
[offset
];
608 /* Reject embedded nulls to protect against buggy applications that
609 * store protocol identifiers in null-terminated strings.
611 if (newOffset
> length
|| data
[offset
] == 0) {
612 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID
);
618 if (offset
> length
) {
619 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID
);
627 ssl3_ClientHandleNextProtoNegoXtn(sslSocket
*ss
, PRUint16 ex_type
,
631 unsigned char resultBuffer
[255];
632 SECItem result
= { siBuffer
, resultBuffer
, 0 };
634 PORT_Assert(!ss
->firstHsDone
);
636 if (ssl3_ExtensionNegotiated(ss
, ssl_app_layer_protocol_xtn
)) {
637 /* If the server negotiated ALPN then it has already told us what protocol
638 * to use, so it doesn't make sense for us to try to negotiate a different
639 * one by sending the NPN handshake message. However, if we've negotiated
640 * NPN then we're required to send the NPN handshake message. Thus, these
641 * two extensions cannot both be negotiated on the same connection. */
642 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE
);
646 rv
= ssl3_ValidateNextProtoNego(data
->data
, data
->len
);
647 if (rv
!= SECSuccess
)
650 /* ss->nextProtoCallback cannot normally be NULL if we negotiated the
651 * extension. However, It is possible that an application erroneously
652 * cleared the callback between the time we sent the ClientHello and now.
654 PORT_Assert(ss
->nextProtoCallback
!= NULL
);
655 if (!ss
->nextProtoCallback
) {
656 /* XXX Use a better error code. This is an application error, not an
658 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE
);
662 rv
= ss
->nextProtoCallback(ss
->nextProtoArg
, ss
->fd
, data
->data
, data
->len
,
663 result
.data
, &result
.len
, sizeof resultBuffer
);
664 if (rv
!= SECSuccess
)
666 /* If the callback wrote more than allowed to |result| it has corrupted our
668 if (result
.len
> sizeof resultBuffer
) {
669 PORT_SetError(SEC_ERROR_OUTPUT_LEN
);
673 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
675 SECITEM_FreeItem(&ss
->ssl3
.nextProto
, PR_FALSE
);
676 return SECITEM_CopyItem(NULL
, &ss
->ssl3
.nextProto
, &result
);
680 ssl3_ClientHandleAppProtoXtn(sslSocket
*ss
, PRUint16 ex_type
, SECItem
*data
)
682 const unsigned char* d
= data
->data
;
683 PRUint16 name_list_len
;
684 SECItem protocol_name
;
686 if (ssl3_ExtensionNegotiated(ss
, ssl_next_proto_nego_xtn
)) {
687 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE
);
691 /* The extension data from the server has the following format:
692 * uint16 name_list_len;
694 * uint8 protocol_name[len]; */
695 if (data
->len
< 4 || data
->len
> 2 + 1 + 255) {
696 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID
);
700 name_list_len
= ((PRUint16
) d
[0]) << 8 |
702 if (name_list_len
!= data
->len
- 2 || d
[2] != data
->len
- 3) {
703 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID
);
707 protocol_name
.data
= data
->data
+ 3;
708 protocol_name
.len
= data
->len
- 3;
710 SECITEM_FreeItem(&ss
->ssl3
.nextProto
, PR_FALSE
);
711 ss
->ssl3
.nextProtoState
= SSL_NEXT_PROTO_SELECTED
;
712 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
713 return SECITEM_CopyItem(NULL
, &ss
->ssl3
.nextProto
, &protocol_name
);
717 ssl3_ClientSendNextProtoNegoXtn(sslSocket
* ss
, PRBool append
,
720 PRInt32 extension_length
;
722 /* Renegotiations do not send this extension. */
723 if (!ss
->opt
.enableNPN
|| !ss
->nextProtoCallback
|| ss
->firstHsDone
) {
727 extension_length
= 4;
729 if (append
&& maxBytes
>= extension_length
) {
731 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_next_proto_nego_xtn
, 2);
732 if (rv
!= SECSuccess
)
734 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
735 if (rv
!= SECSuccess
)
737 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
738 ssl_next_proto_nego_xtn
;
739 } else if (maxBytes
< extension_length
) {
743 return extension_length
;
750 ssl3_ClientSendAppProtoXtn(sslSocket
* ss
, PRBool append
, PRUint32 maxBytes
)
752 PRInt32 extension_length
;
753 unsigned char *alpn_protos
= NULL
;
755 /* Renegotiations do not send this extension. */
756 if (!ss
->opt
.enableALPN
|| !ss
->opt
.nextProtoNego
.data
|| ss
->firstHsDone
) {
760 extension_length
= 2 /* extension type */ + 2 /* extension length */ +
761 2 /* protocol name list length */ +
762 ss
->opt
.nextProtoNego
.len
;
764 if (append
&& maxBytes
>= extension_length
) {
765 /* NPN requires that the client's fallback protocol is first in the
766 * list. However, ALPN sends protocols in preference order. So we
767 * allocate a buffer and move the first protocol to the end of the
770 const unsigned int len
= ss
->opt
.nextProtoNego
.len
;
772 alpn_protos
= PORT_Alloc(len
);
773 if (alpn_protos
== NULL
) {
777 /* Each protocol string is prefixed with a single byte length. */
778 unsigned int i
= ss
->opt
.nextProtoNego
.data
[0] + 1;
780 memcpy(alpn_protos
, &ss
->opt
.nextProtoNego
.data
[i
], len
- i
);
781 memcpy(alpn_protos
+ len
- i
, ss
->opt
.nextProtoNego
.data
, i
);
783 /* This seems to be invalid data so we'll send as-is. */
784 memcpy(alpn_protos
, ss
->opt
.nextProtoNego
.data
, len
);
788 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_app_layer_protocol_xtn
, 2);
789 if (rv
!= SECSuccess
) {
792 rv
= ssl3_AppendHandshakeNumber(ss
, extension_length
- 4, 2);
793 if (rv
!= SECSuccess
) {
796 rv
= ssl3_AppendHandshakeVariable(ss
, alpn_protos
, len
, 2);
797 PORT_Free(alpn_protos
);
799 if (rv
!= SECSuccess
) {
802 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
803 ssl_app_layer_protocol_xtn
;
804 } else if (maxBytes
< extension_length
) {
808 return extension_length
;
812 PORT_Free(alpn_protos
);
818 ssl3_ClientHandleChannelIDXtn(sslSocket
*ss
, PRUint16 ex_type
,
821 PORT_Assert(ss
->getChannelID
!= NULL
);
824 PORT_SetError(SSL_ERROR_BAD_CHANNEL_ID_DATA
);
827 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
832 ssl3_ClientSendChannelIDXtn(sslSocket
* ss
, PRBool append
,
835 PRInt32 extension_length
= 4;
837 if (!ss
->getChannelID
)
840 if (maxBytes
< extension_length
) {
845 if (ss
->sec
.ci
.sid
->cached
!= never_cached
&&
846 ss
->sec
.ci
.sid
->u
.ssl3
.originalHandshakeHash
.len
== 0) {
847 /* We can't do ChannelID on a connection if we're resuming and didn't
848 * do ChannelID on the original connection: without ChannelID on the
849 * original connection we didn't record the handshake hashes needed for
856 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_channel_id_xtn
, 2);
857 if (rv
!= SECSuccess
)
859 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
860 if (rv
!= SECSuccess
)
862 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
866 return extension_length
;
873 ssl3_ClientHandleStatusRequestXtn(sslSocket
*ss
, PRUint16 ex_type
,
876 /* The echoed extension must be empty. */
880 /* Keep track of negotiated extensions. */
881 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
887 ssl3_ServerSendStatusRequestXtn(
892 PRInt32 extension_length
;
895 PRBool haveStatus
= PR_FALSE
;
897 for (i
= kt_null
; i
< kt_kea_size
; i
++) {
898 /* TODO: This is a temporary workaround.
899 * The correct code needs to see if we have an OCSP response for
900 * the server certificate being used, rather than if we have any
901 * OCSP response. See also ssl3_SendCertificateStatus.
903 if (ss
->certStatusArray
[i
] && ss
->certStatusArray
[i
]->len
) {
904 haveStatus
= PR_TRUE
;
911 extension_length
= 2 + 2;
912 if (append
&& maxBytes
>= extension_length
) {
914 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_cert_status_xtn
, 2);
915 if (rv
!= SECSuccess
)
917 /* length of extension_data */
918 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
919 if (rv
!= SECSuccess
)
923 return extension_length
;
926 /* ssl3_ClientSendStatusRequestXtn builds the status_request extension on the
927 * client side. See RFC 4366 section 3.6. */
929 ssl3_ClientSendStatusRequestXtn(sslSocket
* ss
, PRBool append
,
932 PRInt32 extension_length
;
934 if (!ss
->opt
.enableOCSPStapling
)
937 /* extension_type (2-bytes) +
938 * length(extension_data) (2-bytes) +
940 * responder_id_list length (2) +
941 * request_extensions length (2)
943 extension_length
= 9;
945 if (append
&& maxBytes
>= extension_length
) {
947 TLSExtensionData
*xtnData
;
950 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_cert_status_xtn
, 2);
951 if (rv
!= SECSuccess
)
953 rv
= ssl3_AppendHandshakeNumber(ss
, extension_length
- 4, 2);
954 if (rv
!= SECSuccess
)
956 rv
= ssl3_AppendHandshakeNumber(ss
, 1 /* status_type ocsp */, 1);
957 if (rv
!= SECSuccess
)
959 /* A zero length responder_id_list means that the responders are
960 * implicitly known to the server. */
961 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
962 if (rv
!= SECSuccess
)
964 /* A zero length request_extensions means that there are no extensions.
965 * Specifically, we don't set the id-pkix-ocsp-nonce extension. This
966 * means that the server can replay a cached OCSP response to us. */
967 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
968 if (rv
!= SECSuccess
)
971 xtnData
= &ss
->xtnData
;
972 xtnData
->advertised
[xtnData
->numAdvertised
++] = ssl_cert_status_xtn
;
973 } else if (maxBytes
< extension_length
) {
977 return extension_length
;
982 * Called from ssl3_HandleFinished
985 ssl3_SendNewSessionTicket(sslSocket
*ss
)
989 NewSessionTicket ticket
;
991 SECItem plaintext_item
= {0, NULL
, 0};
992 SECItem ciphertext
= {0, NULL
, 0};
993 PRUint32 ciphertext_length
;
994 PRBool ms_is_wrapped
;
995 unsigned char wrapped_ms
[SSL3_MASTER_SECRET_LENGTH
];
996 SECItem ms_item
= {0, NULL
, 0};
997 SSL3KEAType effectiveExchKeyType
= ssl_kea_null
;
998 PRUint32 padding_length
;
999 PRUint32 message_length
;
1000 PRUint32 cert_length
;
1001 PRUint8 length_buf
[4];
1003 PK11SymKey
*aes_key_pkcs11
;
1004 PK11SymKey
*mac_key_pkcs11
;
1005 #ifndef NO_PKCS11_BYPASS
1006 const unsigned char *aes_key
;
1007 const unsigned char *mac_key
;
1008 PRUint32 aes_key_length
;
1009 PRUint32 mac_key_length
;
1010 PRUint64 aes_ctx_buf
[MAX_CIPHER_CONTEXT_LLONGS
];
1011 AESContext
*aes_ctx
;
1012 const SECHashObject
*hashObj
= NULL
;
1013 PRUint64 hmac_ctx_buf
[MAX_MAC_CONTEXT_LLONGS
];
1014 HMACContext
*hmac_ctx
;
1016 CK_MECHANISM_TYPE cipherMech
= CKM_AES_CBC
;
1017 PK11Context
*aes_ctx_pkcs11
;
1018 CK_MECHANISM_TYPE macMech
= CKM_SHA256_HMAC
;
1019 PK11Context
*hmac_ctx_pkcs11
;
1020 unsigned char computed_mac
[TLS_EX_SESS_TICKET_MAC_LENGTH
];
1021 unsigned int computed_mac_length
;
1022 unsigned char iv
[AES_BLOCK_SIZE
];
1024 SECItem
*srvName
= NULL
;
1025 PRUint32 srvNameLen
= 0;
1026 CK_MECHANISM_TYPE msWrapMech
= 0; /* dummy default value,
1029 SSL_TRC(3, ("%d: SSL3[%d]: send session_ticket handshake",
1030 SSL_GETPID(), ss
->fd
));
1032 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveXmitBufLock(ss
));
1033 PORT_Assert( ss
->opt
.noLocks
|| ssl_HaveSSL3HandshakeLock(ss
));
1035 ticket
.ticket_lifetime_hint
= TLS_EX_SESS_TICKET_LIFETIME_HINT
;
1036 cert_length
= (ss
->opt
.requestCertificate
&& ss
->sec
.ci
.sid
->peerCert
) ?
1037 3 + ss
->sec
.ci
.sid
->peerCert
->derCert
.len
: 0;
1039 /* Get IV and encryption keys */
1041 ivItem
.len
= sizeof(iv
);
1042 rv
= PK11_GenerateRandom(iv
, sizeof(iv
));
1043 if (rv
!= SECSuccess
) goto loser
;
1045 #ifndef NO_PKCS11_BYPASS
1046 if (ss
->opt
.bypassPKCS11
) {
1047 rv
= ssl3_GetSessionTicketKeys(&aes_key
, &aes_key_length
,
1048 &mac_key
, &mac_key_length
);
1052 rv
= ssl3_GetSessionTicketKeysPKCS11(ss
, &aes_key_pkcs11
,
1055 if (rv
!= SECSuccess
) goto loser
;
1057 if (ss
->ssl3
.pwSpec
->msItem
.len
&& ss
->ssl3
.pwSpec
->msItem
.data
) {
1058 /* The master secret is available unwrapped. */
1059 ms_item
.data
= ss
->ssl3
.pwSpec
->msItem
.data
;
1060 ms_item
.len
= ss
->ssl3
.pwSpec
->msItem
.len
;
1061 ms_is_wrapped
= PR_FALSE
;
1063 /* Extract the master secret wrapped. */
1065 PORT_Memset(&sid
, 0, sizeof(sslSessionID
));
1067 if (ss
->ssl3
.hs
.kea_def
->kea
== kea_ecdhe_rsa
) {
1068 effectiveExchKeyType
= kt_rsa
;
1070 effectiveExchKeyType
= ss
->ssl3
.hs
.kea_def
->exchKeyType
;
1073 rv
= ssl3_CacheWrappedMasterSecret(ss
, &sid
, ss
->ssl3
.pwSpec
,
1074 effectiveExchKeyType
);
1075 if (rv
== SECSuccess
) {
1076 if (sid
.u
.ssl3
.keys
.wrapped_master_secret_len
> sizeof(wrapped_ms
))
1078 memcpy(wrapped_ms
, sid
.u
.ssl3
.keys
.wrapped_master_secret
,
1079 sid
.u
.ssl3
.keys
.wrapped_master_secret_len
);
1080 ms_item
.data
= wrapped_ms
;
1081 ms_item
.len
= sid
.u
.ssl3
.keys
.wrapped_master_secret_len
;
1082 msWrapMech
= sid
.u
.ssl3
.masterWrapMech
;
1084 /* TODO: else send an empty ticket. */
1087 ms_is_wrapped
= PR_TRUE
;
1089 /* Prep to send negotiated name */
1090 srvName
= &ss
->ssl3
.pwSpec
->srvVirtName
;
1091 if (srvName
->data
&& srvName
->len
) {
1092 srvNameLen
= 2 + srvName
->len
; /* len bytes + name len */
1096 sizeof(PRUint16
) /* ticket_version */
1097 + sizeof(SSL3ProtocolVersion
) /* ssl_version */
1098 + sizeof(ssl3CipherSuite
) /* ciphersuite */
1099 + 1 /* compression */
1100 + 10 /* cipher spec parameters */
1101 + 1 /* SessionTicket.ms_is_wrapped */
1102 + 1 /* effectiveExchKeyType */
1103 + 4 /* msWrapMech */
1104 + 2 /* master_secret.length */
1105 + ms_item
.len
/* master_secret */
1106 + 1 /* client_auth_type */
1107 + cert_length
/* cert */
1108 + 1 /* server name type */
1109 + srvNameLen
/* name len + length field */
1110 + sizeof(ticket
.ticket_lifetime_hint
);
1111 padding_length
= AES_BLOCK_SIZE
-
1112 (ciphertext_length
% AES_BLOCK_SIZE
);
1113 ciphertext_length
+= padding_length
;
1116 sizeof(ticket
.ticket_lifetime_hint
) /* ticket_lifetime_hint */
1117 + 2 /* length field for NewSessionTicket.ticket */
1118 + SESS_TICKET_KEY_NAME_LEN
/* key_name */
1119 + AES_BLOCK_SIZE
/* iv */
1120 + 2 /* length field for NewSessionTicket.ticket.encrypted_state */
1121 + ciphertext_length
/* encrypted_state */
1122 + TLS_EX_SESS_TICKET_MAC_LENGTH
; /* mac */
1124 if (SECITEM_AllocItem(NULL
, &plaintext_item
, ciphertext_length
) == NULL
)
1127 plaintext
= plaintext_item
;
1129 /* ticket_version */
1130 rv
= ssl3_AppendNumberToItem(&plaintext
, TLS_EX_SESS_TICKET_VERSION
,
1132 if (rv
!= SECSuccess
) goto loser
;
1135 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->version
,
1136 sizeof(SSL3ProtocolVersion
));
1137 if (rv
!= SECSuccess
) goto loser
;
1140 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->ssl3
.hs
.cipher_suite
,
1141 sizeof(ssl3CipherSuite
));
1142 if (rv
!= SECSuccess
) goto loser
;
1145 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->ssl3
.hs
.compression
, 1);
1146 if (rv
!= SECSuccess
) goto loser
;
1148 /* cipher spec parameters */
1149 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->sec
.authAlgorithm
, 1);
1150 if (rv
!= SECSuccess
) goto loser
;
1151 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->sec
.authKeyBits
, 4);
1152 if (rv
!= SECSuccess
) goto loser
;
1153 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->sec
.keaType
, 1);
1154 if (rv
!= SECSuccess
) goto loser
;
1155 rv
= ssl3_AppendNumberToItem(&plaintext
, ss
->sec
.keaKeyBits
, 4);
1156 if (rv
!= SECSuccess
) goto loser
;
1159 rv
= ssl3_AppendNumberToItem(&plaintext
, ms_is_wrapped
, 1);
1160 if (rv
!= SECSuccess
) goto loser
;
1161 rv
= ssl3_AppendNumberToItem(&plaintext
, effectiveExchKeyType
, 1);
1162 if (rv
!= SECSuccess
) goto loser
;
1163 rv
= ssl3_AppendNumberToItem(&plaintext
, msWrapMech
, 4);
1164 if (rv
!= SECSuccess
) goto loser
;
1165 rv
= ssl3_AppendNumberToItem(&plaintext
, ms_item
.len
, 2);
1166 if (rv
!= SECSuccess
) goto loser
;
1167 rv
= ssl3_AppendToItem(&plaintext
, ms_item
.data
, ms_item
.len
);
1168 if (rv
!= SECSuccess
) goto loser
;
1170 /* client_identity */
1171 if (ss
->opt
.requestCertificate
&& ss
->sec
.ci
.sid
->peerCert
) {
1172 rv
= ssl3_AppendNumberToItem(&plaintext
, CLIENT_AUTH_CERTIFICATE
, 1);
1173 if (rv
!= SECSuccess
) goto loser
;
1174 rv
= ssl3_AppendNumberToItem(&plaintext
,
1175 ss
->sec
.ci
.sid
->peerCert
->derCert
.len
, 3);
1176 if (rv
!= SECSuccess
) goto loser
;
1177 rv
= ssl3_AppendToItem(&plaintext
,
1178 ss
->sec
.ci
.sid
->peerCert
->derCert
.data
,
1179 ss
->sec
.ci
.sid
->peerCert
->derCert
.len
);
1180 if (rv
!= SECSuccess
) goto loser
;
1182 rv
= ssl3_AppendNumberToItem(&plaintext
, 0, 1);
1183 if (rv
!= SECSuccess
) goto loser
;
1188 rv
= ssl3_AppendNumberToItem(&plaintext
, now
,
1189 sizeof(ticket
.ticket_lifetime_hint
));
1190 if (rv
!= SECSuccess
) goto loser
;
1193 /* Name Type (sni_host_name) */
1194 rv
= ssl3_AppendNumberToItem(&plaintext
, srvName
->type
, 1);
1195 if (rv
!= SECSuccess
) goto loser
;
1196 /* HostName (length and value) */
1197 rv
= ssl3_AppendNumberToItem(&plaintext
, srvName
->len
, 2);
1198 if (rv
!= SECSuccess
) goto loser
;
1199 rv
= ssl3_AppendToItem(&plaintext
, srvName
->data
, srvName
->len
);
1200 if (rv
!= SECSuccess
) goto loser
;
1203 rv
= ssl3_AppendNumberToItem(&plaintext
, (char)TLS_STE_NO_SERVER_NAME
,
1205 if (rv
!= SECSuccess
) goto loser
;
1208 PORT_Assert(plaintext
.len
== padding_length
);
1209 for (i
= 0; i
< padding_length
; i
++)
1210 plaintext
.data
[i
] = (unsigned char)padding_length
;
1212 if (SECITEM_AllocItem(NULL
, &ciphertext
, ciphertext_length
) == NULL
) {
1217 /* Generate encrypted portion of ticket. */
1218 #ifndef NO_PKCS11_BYPASS
1219 if (ss
->opt
.bypassPKCS11
) {
1220 aes_ctx
= (AESContext
*)aes_ctx_buf
;
1221 rv
= AES_InitContext(aes_ctx
, aes_key
, aes_key_length
, iv
,
1222 NSS_AES_CBC
, 1, AES_BLOCK_SIZE
);
1223 if (rv
!= SECSuccess
) goto loser
;
1225 rv
= AES_Encrypt(aes_ctx
, ciphertext
.data
, &ciphertext
.len
,
1226 ciphertext
.len
, plaintext_item
.data
,
1227 plaintext_item
.len
);
1228 if (rv
!= SECSuccess
) goto loser
;
1232 aes_ctx_pkcs11
= PK11_CreateContextBySymKey(cipherMech
,
1233 CKA_ENCRYPT
, aes_key_pkcs11
, &ivItem
);
1234 if (!aes_ctx_pkcs11
)
1237 rv
= PK11_CipherOp(aes_ctx_pkcs11
, ciphertext
.data
,
1238 (int *)&ciphertext
.len
, ciphertext
.len
,
1239 plaintext_item
.data
, plaintext_item
.len
);
1240 PK11_Finalize(aes_ctx_pkcs11
);
1241 PK11_DestroyContext(aes_ctx_pkcs11
, PR_TRUE
);
1242 if (rv
!= SECSuccess
) goto loser
;
1245 /* Convert ciphertext length to network order. */
1246 length_buf
[0] = (ciphertext
.len
>> 8) & 0xff;
1247 length_buf
[1] = (ciphertext
.len
) & 0xff;
1250 #ifndef NO_PKCS11_BYPASS
1251 if (ss
->opt
.bypassPKCS11
) {
1252 hmac_ctx
= (HMACContext
*)hmac_ctx_buf
;
1253 hashObj
= HASH_GetRawHashObject(HASH_AlgSHA256
);
1254 if (HMAC_Init(hmac_ctx
, hashObj
, mac_key
,
1255 mac_key_length
, PR_FALSE
) != SECSuccess
)
1258 HMAC_Begin(hmac_ctx
);
1259 HMAC_Update(hmac_ctx
, key_name
, SESS_TICKET_KEY_NAME_LEN
);
1260 HMAC_Update(hmac_ctx
, iv
, sizeof(iv
));
1261 HMAC_Update(hmac_ctx
, (unsigned char *)length_buf
, 2);
1262 HMAC_Update(hmac_ctx
, ciphertext
.data
, ciphertext
.len
);
1263 HMAC_Finish(hmac_ctx
, computed_mac
, &computed_mac_length
,
1264 sizeof(computed_mac
));
1269 macParam
.data
= NULL
;
1271 hmac_ctx_pkcs11
= PK11_CreateContextBySymKey(macMech
,
1272 CKA_SIGN
, mac_key_pkcs11
, &macParam
);
1273 if (!hmac_ctx_pkcs11
)
1276 rv
= PK11_DigestBegin(hmac_ctx_pkcs11
);
1277 rv
= PK11_DigestOp(hmac_ctx_pkcs11
, key_name
,
1278 SESS_TICKET_KEY_NAME_LEN
);
1279 rv
= PK11_DigestOp(hmac_ctx_pkcs11
, iv
, sizeof(iv
));
1280 rv
= PK11_DigestOp(hmac_ctx_pkcs11
, (unsigned char *)length_buf
, 2);
1281 rv
= PK11_DigestOp(hmac_ctx_pkcs11
, ciphertext
.data
, ciphertext
.len
);
1282 rv
= PK11_DigestFinal(hmac_ctx_pkcs11
, computed_mac
,
1283 &computed_mac_length
, sizeof(computed_mac
));
1284 PK11_DestroyContext(hmac_ctx_pkcs11
, PR_TRUE
);
1285 if (rv
!= SECSuccess
) goto loser
;
1288 /* Serialize the handshake message. */
1289 rv
= ssl3_AppendHandshakeHeader(ss
, new_session_ticket
, message_length
);
1290 if (rv
!= SECSuccess
) goto loser
;
1292 rv
= ssl3_AppendHandshakeNumber(ss
, ticket
.ticket_lifetime_hint
,
1293 sizeof(ticket
.ticket_lifetime_hint
));
1294 if (rv
!= SECSuccess
) goto loser
;
1296 rv
= ssl3_AppendHandshakeNumber(ss
,
1297 message_length
- sizeof(ticket
.ticket_lifetime_hint
) - 2, 2);
1298 if (rv
!= SECSuccess
) goto loser
;
1300 rv
= ssl3_AppendHandshake(ss
, key_name
, SESS_TICKET_KEY_NAME_LEN
);
1301 if (rv
!= SECSuccess
) goto loser
;
1303 rv
= ssl3_AppendHandshake(ss
, iv
, sizeof(iv
));
1304 if (rv
!= SECSuccess
) goto loser
;
1306 rv
= ssl3_AppendHandshakeVariable(ss
, ciphertext
.data
, ciphertext
.len
, 2);
1307 if (rv
!= SECSuccess
) goto loser
;
1309 rv
= ssl3_AppendHandshake(ss
, computed_mac
, computed_mac_length
);
1310 if (rv
!= SECSuccess
) goto loser
;
1313 if (plaintext_item
.data
)
1314 SECITEM_FreeItem(&plaintext_item
, PR_FALSE
);
1315 if (ciphertext
.data
)
1316 SECITEM_FreeItem(&ciphertext
, PR_FALSE
);
1321 /* When a client receives a SessionTicket extension a NewSessionTicket
1322 * message is expected during the handshake.
1325 ssl3_ClientHandleSessionTicketXtn(sslSocket
*ss
, PRUint16 ex_type
,
1331 /* Keep track of negotiated extensions. */
1332 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
1337 ssl3_ServerHandleSessionTicketXtn(sslSocket
*ss
, PRUint16 ex_type
,
1341 SECItem
*decrypted_state
= NULL
;
1342 SessionTicket
*parsed_session_ticket
= NULL
;
1343 sslSessionID
*sid
= NULL
;
1344 SSL3Statistics
*ssl3stats
;
1346 /* Ignore the SessionTicket extension if processing is disabled. */
1347 if (!ss
->opt
.enableSessionTickets
)
1350 /* Keep track of negotiated extensions. */
1351 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
1353 /* Parse the received ticket sent in by the client. We are
1354 * lenient about some parse errors, falling back to a fullshake
1355 * instead of terminating the current connection.
1357 if (data
->len
== 0) {
1358 ss
->xtnData
.emptySessionTicket
= PR_TRUE
;
1361 SECItem extension_data
;
1362 EncryptedSessionTicket enc_session_ticket
;
1363 unsigned char computed_mac
[TLS_EX_SESS_TICKET_MAC_LENGTH
];
1364 unsigned int computed_mac_length
;
1365 #ifndef NO_PKCS11_BYPASS
1366 const SECHashObject
*hashObj
;
1367 const unsigned char *aes_key
;
1368 const unsigned char *mac_key
;
1369 PRUint32 aes_key_length
;
1370 PRUint32 mac_key_length
;
1371 PRUint64 hmac_ctx_buf
[MAX_MAC_CONTEXT_LLONGS
];
1372 HMACContext
*hmac_ctx
;
1373 PRUint64 aes_ctx_buf
[MAX_CIPHER_CONTEXT_LLONGS
];
1374 AESContext
*aes_ctx
;
1376 PK11SymKey
*aes_key_pkcs11
;
1377 PK11SymKey
*mac_key_pkcs11
;
1378 PK11Context
*hmac_ctx_pkcs11
;
1379 CK_MECHANISM_TYPE macMech
= CKM_SHA256_HMAC
;
1380 PK11Context
*aes_ctx_pkcs11
;
1381 CK_MECHANISM_TYPE cipherMech
= CKM_AES_CBC
;
1382 unsigned char * padding
;
1383 PRUint32 padding_length
;
1384 unsigned char *buffer
;
1385 unsigned int buffer_len
;
1388 PRInt8 nameType
= TLS_STE_NO_SERVER_NAME
;
1390 /* Turn off stateless session resumption if the client sends a
1391 * SessionTicket extension, even if the extension turns out to be
1392 * malformed (ss->sec.ci.sid is non-NULL when doing session
1395 if (ss
->sec
.ci
.sid
!= NULL
) {
1396 if (ss
->sec
.uncache
)
1397 ss
->sec
.uncache(ss
->sec
.ci
.sid
);
1398 ssl_FreeSID(ss
->sec
.ci
.sid
);
1399 ss
->sec
.ci
.sid
= NULL
;
1402 extension_data
.data
= data
->data
; /* Keep a copy for future use. */
1403 extension_data
.len
= data
->len
;
1405 if (ssl3_ParseEncryptedSessionTicket(ss
, data
, &enc_session_ticket
)
1409 /* Get session ticket keys. */
1410 #ifndef NO_PKCS11_BYPASS
1411 if (ss
->opt
.bypassPKCS11
) {
1412 rv
= ssl3_GetSessionTicketKeys(&aes_key
, &aes_key_length
,
1413 &mac_key
, &mac_key_length
);
1417 rv
= ssl3_GetSessionTicketKeysPKCS11(ss
, &aes_key_pkcs11
,
1420 if (rv
!= SECSuccess
) {
1421 SSL_DBG(("%d: SSL[%d]: Unable to get/generate session ticket keys.",
1422 SSL_GETPID(), ss
->fd
));
1426 /* If the ticket sent by the client was generated under a key different
1427 * from the one we have, bypass ticket processing.
1429 if (PORT_Memcmp(enc_session_ticket
.key_name
, key_name
,
1430 SESS_TICKET_KEY_NAME_LEN
) != 0) {
1431 SSL_DBG(("%d: SSL[%d]: Session ticket key_name sent mismatch.",
1432 SSL_GETPID(), ss
->fd
));
1436 /* Verify the MAC on the ticket. MAC verification may also
1437 * fail if the MAC key has been recently refreshed.
1439 #ifndef NO_PKCS11_BYPASS
1440 if (ss
->opt
.bypassPKCS11
) {
1441 hmac_ctx
= (HMACContext
*)hmac_ctx_buf
;
1442 hashObj
= HASH_GetRawHashObject(HASH_AlgSHA256
);
1443 if (HMAC_Init(hmac_ctx
, hashObj
, mac_key
,
1444 sizeof(session_ticket_mac_key
), PR_FALSE
) != SECSuccess
)
1446 HMAC_Begin(hmac_ctx
);
1447 HMAC_Update(hmac_ctx
, extension_data
.data
,
1448 extension_data
.len
- TLS_EX_SESS_TICKET_MAC_LENGTH
);
1449 if (HMAC_Finish(hmac_ctx
, computed_mac
, &computed_mac_length
,
1450 sizeof(computed_mac
)) != SECSuccess
)
1456 macParam
.data
= NULL
;
1458 hmac_ctx_pkcs11
= PK11_CreateContextBySymKey(macMech
,
1459 CKA_SIGN
, mac_key_pkcs11
, &macParam
);
1460 if (!hmac_ctx_pkcs11
) {
1461 SSL_DBG(("%d: SSL[%d]: Unable to create HMAC context: %d.",
1462 SSL_GETPID(), ss
->fd
, PORT_GetError()));
1465 SSL_DBG(("%d: SSL[%d]: Successfully created HMAC context.",
1466 SSL_GETPID(), ss
->fd
));
1468 rv
= PK11_DigestBegin(hmac_ctx_pkcs11
);
1469 rv
= PK11_DigestOp(hmac_ctx_pkcs11
, extension_data
.data
,
1470 extension_data
.len
- TLS_EX_SESS_TICKET_MAC_LENGTH
);
1471 if (rv
!= SECSuccess
) {
1472 PK11_DestroyContext(hmac_ctx_pkcs11
, PR_TRUE
);
1475 rv
= PK11_DigestFinal(hmac_ctx_pkcs11
, computed_mac
,
1476 &computed_mac_length
, sizeof(computed_mac
));
1477 PK11_DestroyContext(hmac_ctx_pkcs11
, PR_TRUE
);
1478 if (rv
!= SECSuccess
)
1481 if (NSS_SecureMemcmp(computed_mac
, enc_session_ticket
.mac
,
1482 computed_mac_length
) != 0) {
1483 SSL_DBG(("%d: SSL[%d]: Session ticket MAC mismatch.",
1484 SSL_GETPID(), ss
->fd
));
1488 /* We ignore key_name for now.
1489 * This is ok as MAC verification succeeded.
1492 /* Decrypt the ticket. */
1494 /* Plaintext is shorter than the ciphertext due to padding. */
1495 decrypted_state
= SECITEM_AllocItem(NULL
, NULL
,
1496 enc_session_ticket
.encrypted_state
.len
);
1498 #ifndef NO_PKCS11_BYPASS
1499 if (ss
->opt
.bypassPKCS11
) {
1500 aes_ctx
= (AESContext
*)aes_ctx_buf
;
1501 rv
= AES_InitContext(aes_ctx
, aes_key
,
1502 sizeof(session_ticket_enc_key
), enc_session_ticket
.iv
,
1503 NSS_AES_CBC
, 0,AES_BLOCK_SIZE
);
1504 if (rv
!= SECSuccess
) {
1505 SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
1506 SSL_GETPID(), ss
->fd
));
1510 rv
= AES_Decrypt(aes_ctx
, decrypted_state
->data
,
1511 &decrypted_state
->len
, decrypted_state
->len
,
1512 enc_session_ticket
.encrypted_state
.data
,
1513 enc_session_ticket
.encrypted_state
.len
);
1514 if (rv
!= SECSuccess
)
1520 ivItem
.data
= enc_session_ticket
.iv
;
1521 ivItem
.len
= AES_BLOCK_SIZE
;
1522 aes_ctx_pkcs11
= PK11_CreateContextBySymKey(cipherMech
,
1523 CKA_DECRYPT
, aes_key_pkcs11
, &ivItem
);
1524 if (!aes_ctx_pkcs11
) {
1525 SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
1526 SSL_GETPID(), ss
->fd
));
1530 rv
= PK11_CipherOp(aes_ctx_pkcs11
, decrypted_state
->data
,
1531 (int *)&decrypted_state
->len
, decrypted_state
->len
,
1532 enc_session_ticket
.encrypted_state
.data
,
1533 enc_session_ticket
.encrypted_state
.len
);
1534 PK11_Finalize(aes_ctx_pkcs11
);
1535 PK11_DestroyContext(aes_ctx_pkcs11
, PR_TRUE
);
1536 if (rv
!= SECSuccess
)
1540 /* Check padding. */
1542 (PRUint32
)decrypted_state
->data
[decrypted_state
->len
- 1];
1543 if (padding_length
== 0 || padding_length
> AES_BLOCK_SIZE
)
1546 padding
= &decrypted_state
->data
[decrypted_state
->len
- padding_length
];
1547 for (i
= 0; i
< padding_length
; i
++, padding
++) {
1548 if (padding_length
!= (PRUint32
)*padding
)
1552 /* Deserialize session state. */
1553 buffer
= decrypted_state
->data
;
1554 buffer_len
= decrypted_state
->len
;
1556 parsed_session_ticket
= PORT_ZAlloc(sizeof(SessionTicket
));
1557 if (parsed_session_ticket
== NULL
) {
1562 /* Read ticket_version (which is ignored for now.) */
1563 temp
= ssl3_ConsumeHandshakeNumber(ss
, 2, &buffer
, &buffer_len
);
1564 if (temp
< 0) goto no_ticket
;
1565 parsed_session_ticket
->ticket_version
= (SSL3ProtocolVersion
)temp
;
1567 /* Read SSLVersion. */
1568 temp
= ssl3_ConsumeHandshakeNumber(ss
, 2, &buffer
, &buffer_len
);
1569 if (temp
< 0) goto no_ticket
;
1570 parsed_session_ticket
->ssl_version
= (SSL3ProtocolVersion
)temp
;
1572 /* Read cipher_suite. */
1573 temp
= ssl3_ConsumeHandshakeNumber(ss
, 2, &buffer
, &buffer_len
);
1574 if (temp
< 0) goto no_ticket
;
1575 parsed_session_ticket
->cipher_suite
= (ssl3CipherSuite
)temp
;
1577 /* Read compression_method. */
1578 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1579 if (temp
< 0) goto no_ticket
;
1580 parsed_session_ticket
->compression_method
= (SSLCompressionMethod
)temp
;
1582 /* Read cipher spec parameters. */
1583 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1584 if (temp
< 0) goto no_ticket
;
1585 parsed_session_ticket
->authAlgorithm
= (SSLSignType
)temp
;
1586 temp
= ssl3_ConsumeHandshakeNumber(ss
, 4, &buffer
, &buffer_len
);
1587 if (temp
< 0) goto no_ticket
;
1588 parsed_session_ticket
->authKeyBits
= (PRUint32
)temp
;
1589 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1590 if (temp
< 0) goto no_ticket
;
1591 parsed_session_ticket
->keaType
= (SSLKEAType
)temp
;
1592 temp
= ssl3_ConsumeHandshakeNumber(ss
, 4, &buffer
, &buffer_len
);
1593 if (temp
< 0) goto no_ticket
;
1594 parsed_session_ticket
->keaKeyBits
= (PRUint32
)temp
;
1596 /* Read wrapped master_secret. */
1597 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1598 if (temp
< 0) goto no_ticket
;
1599 parsed_session_ticket
->ms_is_wrapped
= (PRBool
)temp
;
1601 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1602 if (temp
< 0) goto no_ticket
;
1603 parsed_session_ticket
->exchKeyType
= (SSL3KEAType
)temp
;
1605 temp
= ssl3_ConsumeHandshakeNumber(ss
, 4, &buffer
, &buffer_len
);
1606 if (temp
< 0) goto no_ticket
;
1607 parsed_session_ticket
->msWrapMech
= (CK_MECHANISM_TYPE
)temp
;
1609 temp
= ssl3_ConsumeHandshakeNumber(ss
, 2, &buffer
, &buffer_len
);
1610 if (temp
< 0) goto no_ticket
;
1611 parsed_session_ticket
->ms_length
= (PRUint16
)temp
;
1612 if (parsed_session_ticket
->ms_length
== 0 || /* sanity check MS. */
1613 parsed_session_ticket
->ms_length
>
1614 sizeof(parsed_session_ticket
->master_secret
))
1617 /* Allow for the wrapped master secret to be longer. */
1618 if (buffer_len
< parsed_session_ticket
->ms_length
)
1620 PORT_Memcpy(parsed_session_ticket
->master_secret
, buffer
,
1621 parsed_session_ticket
->ms_length
);
1622 buffer
+= parsed_session_ticket
->ms_length
;
1623 buffer_len
-= parsed_session_ticket
->ms_length
;
1625 /* Read client_identity */
1626 temp
= ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1629 parsed_session_ticket
->client_identity
.client_auth_type
=
1630 (ClientAuthenticationType
)temp
;
1631 switch(parsed_session_ticket
->client_identity
.client_auth_type
) {
1632 case CLIENT_AUTH_ANONYMOUS
:
1634 case CLIENT_AUTH_CERTIFICATE
:
1635 rv
= ssl3_ConsumeHandshakeVariable(ss
, &cert_item
, 3,
1636 &buffer
, &buffer_len
);
1637 if (rv
!= SECSuccess
) goto no_ticket
;
1638 rv
= SECITEM_CopyItem(NULL
, &parsed_session_ticket
->peer_cert
,
1640 if (rv
!= SECSuccess
) goto no_ticket
;
1645 /* Read timestamp. */
1646 temp
= ssl3_ConsumeHandshakeNumber(ss
, 4, &buffer
, &buffer_len
);
1649 parsed_session_ticket
->timestamp
= (PRUint32
)temp
;
1651 /* Read server name */
1653 ssl3_ConsumeHandshakeNumber(ss
, 1, &buffer
, &buffer_len
);
1654 if (nameType
!= TLS_STE_NO_SERVER_NAME
) {
1656 rv
= ssl3_ConsumeHandshakeVariable(ss
, &name_item
, 2, &buffer
,
1658 if (rv
!= SECSuccess
) goto no_ticket
;
1659 rv
= SECITEM_CopyItem(NULL
, &parsed_session_ticket
->srvName
,
1661 if (rv
!= SECSuccess
) goto no_ticket
;
1662 parsed_session_ticket
->srvName
.type
= nameType
;
1665 /* Done parsing. Check that all bytes have been consumed. */
1666 if (buffer_len
!= padding_length
)
1669 /* Use the ticket if it has not expired, otherwise free the allocated
1670 * memory since the ticket is of no use.
1672 if (parsed_session_ticket
->timestamp
!= 0 &&
1673 parsed_session_ticket
->timestamp
+
1674 TLS_EX_SESS_TICKET_LIFETIME_HINT
> ssl_Time()) {
1676 sid
= ssl3_NewSessionID(ss
, PR_TRUE
);
1682 /* Copy over parameters. */
1683 sid
->version
= parsed_session_ticket
->ssl_version
;
1684 sid
->u
.ssl3
.cipherSuite
= parsed_session_ticket
->cipher_suite
;
1685 sid
->u
.ssl3
.compression
= parsed_session_ticket
->compression_method
;
1686 sid
->authAlgorithm
= parsed_session_ticket
->authAlgorithm
;
1687 sid
->authKeyBits
= parsed_session_ticket
->authKeyBits
;
1688 sid
->keaType
= parsed_session_ticket
->keaType
;
1689 sid
->keaKeyBits
= parsed_session_ticket
->keaKeyBits
;
1691 /* Copy master secret. */
1692 #ifndef NO_PKCS11_BYPASS
1693 if (ss
->opt
.bypassPKCS11
&&
1694 parsed_session_ticket
->ms_is_wrapped
)
1697 if (parsed_session_ticket
->ms_length
>
1698 sizeof(sid
->u
.ssl3
.keys
.wrapped_master_secret
))
1700 PORT_Memcpy(sid
->u
.ssl3
.keys
.wrapped_master_secret
,
1701 parsed_session_ticket
->master_secret
,
1702 parsed_session_ticket
->ms_length
);
1703 sid
->u
.ssl3
.keys
.wrapped_master_secret_len
=
1704 parsed_session_ticket
->ms_length
;
1705 sid
->u
.ssl3
.exchKeyType
= parsed_session_ticket
->exchKeyType
;
1706 sid
->u
.ssl3
.masterWrapMech
= parsed_session_ticket
->msWrapMech
;
1707 sid
->u
.ssl3
.keys
.msIsWrapped
=
1708 parsed_session_ticket
->ms_is_wrapped
;
1709 sid
->u
.ssl3
.masterValid
= PR_TRUE
;
1710 sid
->u
.ssl3
.keys
.resumable
= PR_TRUE
;
1712 /* Copy over client cert from session ticket if there is one. */
1713 if (parsed_session_ticket
->peer_cert
.data
!= NULL
) {
1714 if (sid
->peerCert
!= NULL
)
1715 CERT_DestroyCertificate(sid
->peerCert
);
1716 sid
->peerCert
= CERT_NewTempCertificate(ss
->dbHandle
,
1717 &parsed_session_ticket
->peer_cert
, NULL
, PR_FALSE
, PR_TRUE
);
1718 if (sid
->peerCert
== NULL
) {
1723 if (parsed_session_ticket
->srvName
.data
!= NULL
) {
1724 sid
->u
.ssl3
.srvName
= parsed_session_ticket
->srvName
;
1726 ss
->statelessResume
= PR_TRUE
;
1727 ss
->sec
.ci
.sid
= sid
;
1733 SSL_DBG(("%d: SSL[%d]: Session ticket parsing failed.",
1734 SSL_GETPID(), ss
->fd
));
1735 ssl3stats
= SSL_GetStatistics();
1736 SSL_AtomicIncrementLong(& ssl3stats
->hch_sid_ticket_parse_failures
);
1741 /* ss->sec.ci.sid == sid if it did NOT come here via goto statement
1742 * in that case do not free sid
1744 if (sid
&& (ss
->sec
.ci
.sid
!= sid
)) {
1748 if (decrypted_state
!= NULL
) {
1749 SECITEM_FreeItem(decrypted_state
, PR_TRUE
);
1750 decrypted_state
= NULL
;
1753 if (parsed_session_ticket
!= NULL
) {
1754 if (parsed_session_ticket
->peer_cert
.data
) {
1755 SECITEM_FreeItem(&parsed_session_ticket
->peer_cert
, PR_FALSE
);
1757 PORT_ZFree(parsed_session_ticket
, sizeof(SessionTicket
));
1764 * Read bytes. Using this function means the SECItem structure
1765 * cannot be freed. The caller is expected to call this function
1766 * on a shallow copy of the structure.
1769 ssl3_ConsumeFromItem(SECItem
*item
, unsigned char **buf
, PRUint32 bytes
)
1771 if (bytes
> item
->len
)
1775 item
->data
+= bytes
;
1781 ssl3_ParseEncryptedSessionTicket(sslSocket
*ss
, SECItem
*data
,
1782 EncryptedSessionTicket
*enc_session_ticket
)
1784 if (ssl3_ConsumeFromItem(data
, &enc_session_ticket
->key_name
,
1785 SESS_TICKET_KEY_NAME_LEN
) != SECSuccess
)
1787 if (ssl3_ConsumeFromItem(data
, &enc_session_ticket
->iv
,
1788 AES_BLOCK_SIZE
) != SECSuccess
)
1790 if (ssl3_ConsumeHandshakeVariable(ss
, &enc_session_ticket
->encrypted_state
,
1791 2, &data
->data
, &data
->len
) != SECSuccess
)
1793 if (ssl3_ConsumeFromItem(data
, &enc_session_ticket
->mac
,
1794 TLS_EX_SESS_TICKET_MAC_LENGTH
) != SECSuccess
)
1796 if (data
->len
!= 0) /* Make sure that we have consumed all bytes. */
1802 /* go through hello extensions in buffer "b".
1803 * For each one, find the extension handler in the table, and
1804 * if present, invoke that handler.
1805 * Servers ignore any extensions with unknown extension types.
1806 * Clients reject any extensions with unadvertised extension types.
1809 ssl3_HandleHelloExtensions(sslSocket
*ss
, SSL3Opaque
**b
, PRUint32
*length
)
1811 const ssl3HelloExtensionHandler
* handlers
;
1813 if (ss
->sec
.isServer
) {
1814 handlers
= clientHelloHandlers
;
1815 } else if (ss
->version
> SSL_LIBRARY_VERSION_3_0
) {
1816 handlers
= serverHelloHandlersTLS
;
1818 handlers
= serverHelloHandlersSSL3
;
1822 const ssl3HelloExtensionHandler
* handler
;
1824 PRInt32 extension_type
;
1825 SECItem extension_data
;
1827 /* Get the extension's type field */
1828 extension_type
= ssl3_ConsumeHandshakeNumber(ss
, 2, b
, length
);
1829 if (extension_type
< 0) /* failure to decode extension_type */
1830 return SECFailure
; /* alert already sent */
1832 /* get the data for this extension, so we can pass it or skip it. */
1833 rv
= ssl3_ConsumeHandshakeVariable(ss
, &extension_data
, 2, b
, length
);
1834 if (rv
!= SECSuccess
)
1837 /* Check whether the server sent an extension which was not advertised
1838 * in the ClientHello.
1840 if (!ss
->sec
.isServer
&&
1841 !ssl3_ClientExtensionAdvertised(ss
, extension_type
))
1842 return SECFailure
; /* TODO: send unsupported_extension alert */
1844 /* Check whether an extension has been sent multiple times. */
1845 if (ssl3_ExtensionNegotiated(ss
, extension_type
))
1848 /* find extension_type in table of Hello Extension Handlers */
1849 for (handler
= handlers
; handler
->ex_type
>= 0; handler
++) {
1850 /* if found, call this handler */
1851 if (handler
->ex_type
== extension_type
) {
1852 rv
= (*handler
->ex_handler
)(ss
, (PRUint16
)extension_type
,
1854 /* Ignore this result */
1855 /* Treat all bad extensions as unrecognized types. */
1863 /* Add a callback function to the table of senders of server hello extensions.
1866 ssl3_RegisterServerHelloExtensionSender(sslSocket
*ss
, PRUint16 ex_type
,
1867 ssl3HelloExtensionSenderFunc cb
)
1870 ssl3HelloExtensionSender
*sender
= &ss
->xtnData
.serverSenders
[0];
1872 for (i
= 0; i
< SSL_MAX_EXTENSIONS
; ++i
, ++sender
) {
1873 if (!sender
->ex_sender
) {
1874 sender
->ex_type
= ex_type
;
1875 sender
->ex_sender
= cb
;
1878 /* detect duplicate senders */
1879 PORT_Assert(sender
->ex_type
!= ex_type
);
1880 if (sender
->ex_type
== ex_type
) {
1885 PORT_Assert(i
< SSL_MAX_EXTENSIONS
); /* table needs to grow */
1886 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE
);
1890 /* call each of the extension senders and return the accumulated length */
1892 ssl3_CallHelloExtensionSenders(sslSocket
*ss
, PRBool append
, PRUint32 maxBytes
,
1893 const ssl3HelloExtensionSender
*sender
)
1895 PRInt32 total_exten_len
= 0;
1899 sender
= ss
->version
> SSL_LIBRARY_VERSION_3_0
?
1900 &clientHelloSendersTLS
[0] : &clientHelloSendersSSL3
[0];
1903 for (i
= 0; i
< SSL_MAX_EXTENSIONS
; ++i
, ++sender
) {
1904 if (sender
->ex_sender
) {
1905 PRInt32 extLen
= (*sender
->ex_sender
)(ss
, append
, maxBytes
);
1909 total_exten_len
+= extLen
;
1912 return total_exten_len
;
1916 /* Extension format:
1917 * Extension number: 2 bytes
1918 * Extension length: 2 bytes
1919 * Verify Data Length: 1 byte
1920 * Verify Data (TLS): 12 bytes (client) or 24 bytes (server)
1921 * Verify Data (SSL): 36 bytes (client) or 72 bytes (server)
1924 ssl3_SendRenegotiationInfoXtn(
1929 PRInt32 len
, needed
;
1931 /* In draft-ietf-tls-renegotiation-03, it is NOT RECOMMENDED to send
1932 * both the SCSV and the empty RI, so when we send SCSV in
1933 * the initial handshake, we don't also send RI.
1935 if (!ss
|| ss
->ssl3
.hs
.sendingSCSV
)
1937 len
= !ss
->firstHsDone
? 0 :
1938 (ss
->sec
.isServer
? ss
->ssl3
.hs
.finishedBytes
* 2
1939 : ss
->ssl3
.hs
.finishedBytes
);
1941 if (append
&& maxBytes
>= needed
) {
1943 /* extension_type */
1944 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_renegotiation_info_xtn
, 2);
1945 if (rv
!= SECSuccess
) return -1;
1946 /* length of extension_data */
1947 rv
= ssl3_AppendHandshakeNumber(ss
, len
+ 1, 2);
1948 if (rv
!= SECSuccess
) return -1;
1949 /* verify_Data from previous Finished message(s) */
1950 rv
= ssl3_AppendHandshakeVariable(ss
,
1951 ss
->ssl3
.hs
.finishedMsgs
.data
, len
, 1);
1952 if (rv
!= SECSuccess
) return -1;
1953 if (!ss
->sec
.isServer
) {
1954 TLSExtensionData
*xtnData
= &ss
->xtnData
;
1955 xtnData
->advertised
[xtnData
->numAdvertised
++] =
1956 ssl_renegotiation_info_xtn
;
1963 ssl3_ServerHandleStatusRequestXtn(sslSocket
*ss
, PRUint16 ex_type
,
1966 SECStatus rv
= SECSuccess
;
1968 /* remember that we got this extension. */
1969 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
1970 PORT_Assert(ss
->sec
.isServer
);
1971 /* prepare to send back the appropriate response */
1972 rv
= ssl3_RegisterServerHelloExtensionSender(ss
, ex_type
,
1973 ssl3_ServerSendStatusRequestXtn
);
1977 /* This function runs in both the client and server. */
1979 ssl3_HandleRenegotiationInfoXtn(sslSocket
*ss
, PRUint16 ex_type
, SECItem
*data
)
1981 SECStatus rv
= SECSuccess
;
1984 if (ss
->firstHsDone
) {
1985 len
= ss
->sec
.isServer
? ss
->ssl3
.hs
.finishedBytes
1986 : ss
->ssl3
.hs
.finishedBytes
* 2;
1988 if (data
->len
!= 1 + len
||
1989 data
->data
[0] != len
|| (len
&&
1990 NSS_SecureMemcmp(ss
->ssl3
.hs
.finishedMsgs
.data
,
1991 data
->data
+ 1, len
))) {
1992 /* Can we do this here? Or, must we arrange for the caller to do it? */
1993 (void)SSL3_SendAlert(ss
, alert_fatal
, handshake_failure
);
1994 PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE
);
1997 /* remember that we got this extension and it was correct. */
1998 ss
->peerRequestedProtection
= 1;
1999 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
2000 if (ss
->sec
.isServer
) {
2001 /* prepare to send back the appropriate response */
2002 rv
= ssl3_RegisterServerHelloExtensionSender(ss
, ex_type
,
2003 ssl3_SendRenegotiationInfoXtn
);
2009 ssl3_SendUseSRTPXtn(sslSocket
*ss
, PRBool append
, PRUint32 maxBytes
)
2011 PRUint32 ext_data_len
;
2018 if (!ss
->sec
.isServer
) {
2021 if (!IS_DTLS(ss
) || !ss
->ssl3
.dtlsSRTPCipherCount
)
2022 return 0; /* Not relevant */
2024 ext_data_len
= 2 + 2 * ss
->ssl3
.dtlsSRTPCipherCount
+ 1;
2026 if (append
&& maxBytes
>= 4 + ext_data_len
) {
2027 /* Extension type */
2028 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_use_srtp_xtn
, 2);
2029 if (rv
!= SECSuccess
) return -1;
2030 /* Length of extension data */
2031 rv
= ssl3_AppendHandshakeNumber(ss
, ext_data_len
, 2);
2032 if (rv
!= SECSuccess
) return -1;
2033 /* Length of the SRTP cipher list */
2034 rv
= ssl3_AppendHandshakeNumber(ss
,
2035 2 * ss
->ssl3
.dtlsSRTPCipherCount
,
2037 if (rv
!= SECSuccess
) return -1;
2038 /* The SRTP ciphers */
2039 for (i
= 0; i
< ss
->ssl3
.dtlsSRTPCipherCount
; i
++) {
2040 rv
= ssl3_AppendHandshakeNumber(ss
,
2041 ss
->ssl3
.dtlsSRTPCiphers
[i
],
2044 /* Empty MKI value */
2045 ssl3_AppendHandshakeVariable(ss
, NULL
, 0, 1);
2047 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
2051 return 4 + ext_data_len
;
2055 if (append
&& maxBytes
>= 9) {
2056 /* Extension type */
2057 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_use_srtp_xtn
, 2);
2058 if (rv
!= SECSuccess
) return -1;
2059 /* Length of extension data */
2060 rv
= ssl3_AppendHandshakeNumber(ss
, 5, 2);
2061 if (rv
!= SECSuccess
) return -1;
2062 /* Length of the SRTP cipher list */
2063 rv
= ssl3_AppendHandshakeNumber(ss
, 2, 2);
2064 if (rv
!= SECSuccess
) return -1;
2065 /* The selected cipher */
2066 rv
= ssl3_AppendHandshakeNumber(ss
, ss
->ssl3
.dtlsSRTPCipherSuite
, 2);
2067 if (rv
!= SECSuccess
) return -1;
2068 /* Empty MKI value */
2069 ssl3_AppendHandshakeVariable(ss
, NULL
, 0, 1);
2076 ssl3_HandleUseSRTPXtn(sslSocket
* ss
, PRUint16 ex_type
, SECItem
*data
)
2079 SECItem ciphers
= {siBuffer
, NULL
, 0};
2082 PRUint16 cipher
= 0;
2083 PRBool found
= PR_FALSE
;
2086 if (!ss
->sec
.isServer
) {
2088 if (!data
->data
|| !data
->len
) {
2093 /* Get the cipher list */
2094 rv
= ssl3_ConsumeHandshakeVariable(ss
, &ciphers
, 2,
2095 &data
->data
, &data
->len
);
2096 if (rv
!= SECSuccess
) {
2099 /* Now check that the number of ciphers listed is 1 (len = 2) */
2100 if (ciphers
.len
!= 2) {
2104 /* Get the selected cipher */
2105 cipher
= (ciphers
.data
[0] << 8) | ciphers
.data
[1];
2107 /* Now check that this is one of the ciphers we offered */
2108 for (i
= 0; i
< ss
->ssl3
.dtlsSRTPCipherCount
; i
++) {
2109 if (cipher
== ss
->ssl3
.dtlsSRTPCiphers
[i
]) {
2119 /* Get the srtp_mki value */
2120 rv
= ssl3_ConsumeHandshakeVariable(ss
, &litem
, 1,
2121 &data
->data
, &data
->len
);
2122 if (rv
!= SECSuccess
) {
2126 /* We didn't offer an MKI, so this must be 0 length */
2127 /* XXX RFC 5764 Section 4.1.3 says:
2128 * If the client detects a nonzero-length MKI in the server's
2129 * response that is different than the one the client offered,
2130 * then the client MUST abort the handshake and SHOULD send an
2131 * invalid_parameter alert.
2133 * Due to a limitation of the ssl3_HandleHelloExtensions function,
2134 * returning SECFailure here won't abort the handshake. It will
2135 * merely cause the use_srtp extension to be not negotiated. We
2136 * should fix this. See NSS bug 753136.
2138 if (litem
.len
!= 0) {
2142 if (data
->len
!= 0) {
2147 /* OK, this looks fine. */
2148 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ssl_use_srtp_xtn
;
2149 ss
->ssl3
.dtlsSRTPCipherSuite
= cipher
;
2154 if (!IS_DTLS(ss
) || !ss
->ssl3
.dtlsSRTPCipherCount
) {
2155 /* Ignore the extension if we aren't doing DTLS or no DTLS-SRTP
2156 * preferences have been set. */
2160 if (!data
->data
|| data
->len
< 5) {
2165 /* Get the cipher list */
2166 rv
= ssl3_ConsumeHandshakeVariable(ss
, &ciphers
, 2,
2167 &data
->data
, &data
->len
);
2168 if (rv
!= SECSuccess
) {
2171 /* Check that the list is even length */
2172 if (ciphers
.len
% 2) {
2176 /* Walk through the offered list and pick the most preferred of our
2177 * ciphers, if any */
2178 for (i
= 0; !found
&& i
< ss
->ssl3
.dtlsSRTPCipherCount
; i
++) {
2179 for (j
= 0; j
+ 1 < ciphers
.len
; j
+= 2) {
2180 cipher
= (ciphers
.data
[j
] << 8) | ciphers
.data
[j
+ 1];
2181 if (cipher
== ss
->ssl3
.dtlsSRTPCiphers
[i
]) {
2188 /* Get the srtp_mki value */
2189 rv
= ssl3_ConsumeHandshakeVariable(ss
, &litem
, 1, &data
->data
, &data
->len
);
2190 if (rv
!= SECSuccess
) {
2194 if (data
->len
!= 0) {
2195 return SECFailure
; /* Malformed */
2198 /* Now figure out what to do */
2200 /* No matching ciphers */
2204 /* OK, we have a valid cipher and we've selected it */
2205 ss
->ssl3
.dtlsSRTPCipherSuite
= cipher
;
2206 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ssl_use_srtp_xtn
;
2208 return ssl3_RegisterServerHelloExtensionSender(ss
, ssl_use_srtp_xtn
,
2209 ssl3_SendUseSRTPXtn
);
2212 /* ssl3_ServerHandleSigAlgsXtn handles the signature_algorithms extension
2214 * See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2216 ssl3_ServerHandleSigAlgsXtn(sslSocket
* ss
, PRUint16 ex_type
, SECItem
*data
)
2220 const unsigned char *b
;
2221 unsigned int numAlgorithms
, i
;
2223 /* Ignore this extension if we aren't doing TLS 1.2 or greater. */
2224 if (ss
->version
< SSL_LIBRARY_VERSION_TLS_1_2
) {
2228 /* Keep track of negotiated extensions. */
2229 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;
2231 rv
= ssl3_ConsumeHandshakeVariable(ss
, &algorithms
, 2, &data
->data
,
2233 if (rv
!= SECSuccess
) {
2236 /* Trailing data, empty value, or odd-length value is invalid. */
2237 if (data
->len
!= 0 || algorithms
.len
== 0 || (algorithms
.len
& 1) != 0) {
2238 PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO
);
2242 numAlgorithms
= algorithms
.len
/2;
2244 /* We don't care to process excessive numbers of algorithms. */
2245 if (numAlgorithms
> 512) {
2246 numAlgorithms
= 512;
2249 ss
->ssl3
.hs
.clientSigAndHash
=
2250 PORT_NewArray(SSL3SignatureAndHashAlgorithm
, numAlgorithms
);
2251 if (!ss
->ssl3
.hs
.clientSigAndHash
) {
2254 ss
->ssl3
.hs
.numClientSigAndHash
= 0;
2256 b
= algorithms
.data
;
2257 for (i
= 0; i
< numAlgorithms
; i
++) {
2258 unsigned char tls_hash
= *(b
++);
2259 unsigned char tls_sig
= *(b
++);
2260 SECOidTag hash
= ssl3_TLSHashAlgorithmToOID(tls_hash
);
2262 if (hash
== SEC_OID_UNKNOWN
) {
2263 /* We ignore formats that we don't understand. */
2266 /* tls_sig support will be checked later in
2267 * ssl3_PickSignatureHashAlgorithm. */
2268 ss
->ssl3
.hs
.clientSigAndHash
[i
].hashAlg
= hash
;
2269 ss
->ssl3
.hs
.clientSigAndHash
[i
].sigAlg
= tls_sig
;
2270 ss
->ssl3
.hs
.numClientSigAndHash
++;
2273 if (!ss
->ssl3
.hs
.numClientSigAndHash
) {
2274 /* We didn't understand any of the client's requested signature
2275 * formats. We'll use the defaults. */
2276 PORT_Free(ss
->ssl3
.hs
.clientSigAndHash
);
2277 ss
->ssl3
.hs
.clientSigAndHash
= NULL
;
2283 /* ssl3_ClientSendSigAlgsXtn sends the signature_algorithm extension for TLS
2284 * 1.2 ClientHellos. */
2286 ssl3_ClientSendSigAlgsXtn(sslSocket
* ss
, PRBool append
, PRUint32 maxBytes
)
2288 static const unsigned char signatureAlgorithms
[] = {
2289 /* This block is the contents of our signature_algorithms extension, in
2291 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2292 tls_hash_sha256
, tls_sig_rsa
,
2293 tls_hash_sha384
, tls_sig_rsa
,
2294 tls_hash_sha1
, tls_sig_rsa
,
2295 #ifdef NSS_ENABLE_ECC
2296 tls_hash_sha256
, tls_sig_ecdsa
,
2297 tls_hash_sha384
, tls_sig_ecdsa
,
2298 tls_hash_sha1
, tls_sig_ecdsa
,
2300 tls_hash_sha256
, tls_sig_dsa
,
2301 tls_hash_sha1
, tls_sig_dsa
,
2303 PRInt32 extension_length
;
2305 if (ss
->version
< SSL_LIBRARY_VERSION_TLS_1_2
) {
2310 2 /* extension type */ +
2311 2 /* extension length */ +
2312 2 /* supported_signature_algorithms length */ +
2313 sizeof(signatureAlgorithms
);
2315 if (append
&& maxBytes
>= extension_length
) {
2317 rv
= ssl3_AppendHandshakeNumber(ss
, ssl_signature_algorithms_xtn
, 2);
2318 if (rv
!= SECSuccess
)
2320 rv
= ssl3_AppendHandshakeNumber(ss
, extension_length
- 4, 2);
2321 if (rv
!= SECSuccess
)
2323 rv
= ssl3_AppendHandshakeVariable(ss
, signatureAlgorithms
,
2324 sizeof(signatureAlgorithms
), 2);
2325 if (rv
!= SECSuccess
)
2327 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
2328 ssl_signature_algorithms_xtn
;
2329 } else if (maxBytes
< extension_length
) {
2334 return extension_length
;
2341 ssl3_CalculatePaddingExtensionLength(unsigned int clientHelloLength
)
2343 unsigned int recordLength
= 1 /* handshake message type */ +
2344 3 /* handshake message length */ +
2346 unsigned int extensionLength
;
2348 if (recordLength
< 256 || recordLength
>= 512) {
2352 extensionLength
= 512 - recordLength
;
2353 /* Extensions take at least four bytes to encode. Always include at least
2354 * one byte of data if including the extension. WebSphere Application Server
2355 * 7.0 is intolerant to the last extension being zero-length. */
2356 if (extensionLength
< 4 + 1) {
2357 extensionLength
= 4 + 1;
2360 return extensionLength
;
2363 /* ssl3_AppendPaddingExtension possibly adds an extension which ensures that a
2364 * ClientHello record is either < 256 bytes or is >= 512 bytes. This ensures
2365 * that we don't trigger bugs in F5 products. */
2367 ssl3_AppendPaddingExtension(sslSocket
*ss
, unsigned int extensionLen
,
2370 unsigned int paddingLen
= extensionLen
- 4;
2371 static unsigned char padding
[256];
2373 if (extensionLen
== 0) {
2377 if (extensionLen
< 4 ||
2378 extensionLen
> maxBytes
||
2379 paddingLen
> sizeof(padding
)) {
2384 if (SECSuccess
!= ssl3_AppendHandshakeNumber(ss
, ssl_padding_xtn
, 2))
2386 if (SECSuccess
!= ssl3_AppendHandshakeNumber(ss
, paddingLen
, 2))
2388 if (SECSuccess
!= ssl3_AppendHandshake(ss
, padding
, paddingLen
))
2391 return extensionLen
;
2394 /* ssl3_ClientSendSignedCertTimestampXtn sends the signed_certificate_timestamp
2395 * extension for TLS ClientHellos. */
2397 ssl3_ClientSendSignedCertTimestampXtn(sslSocket
*ss
, PRBool append
,
2400 PRInt32 extension_length
= 2 /* extension_type */ +
2401 2 /* length(extension_data) */;
2403 /* Only send the extension if processing is enabled. */
2404 if (!ss
->opt
.enableSignedCertTimestamps
)
2407 if (append
&& maxBytes
>= extension_length
) {
2409 /* extension_type */
2410 rv
= ssl3_AppendHandshakeNumber(ss
,
2411 ssl_signed_certificate_timestamp_xtn
,
2413 if (rv
!= SECSuccess
)
2416 rv
= ssl3_AppendHandshakeNumber(ss
, 0, 2);
2417 if (rv
!= SECSuccess
)
2419 ss
->xtnData
.advertised
[ss
->xtnData
.numAdvertised
++] =
2420 ssl_signed_certificate_timestamp_xtn
;
2421 } else if (maxBytes
< extension_length
) {
2426 return extension_length
;
2432 ssl3_ClientHandleSignedCertTimestampXtn(sslSocket
*ss
, PRUint16 ex_type
,
2435 /* We do not yet know whether we'll be resuming a session or creating
2436 * a new one, so we keep a pointer to the data in the TLSExtensionData
2437 * structure. This pointer is only valid in the scope of
2438 * ssl3_HandleServerHello, and, if not resuming a session, the data is
2439 * copied once a new session structure has been set up.
2440 * All parsing is currently left to the application and we accept
2441 * everything, including empty data.
2443 SECItem
*scts
= &ss
->xtnData
.signedCertTimestamps
;
2444 PORT_Assert(!scts
->data
&& !scts
->len
);
2447 /* Empty extension data: RFC 6962 mandates non-empty contents. */
2451 /* Keep track of negotiated extensions. */
2452 ss
->xtnData
.negotiated
[ss
->xtnData
.numNegotiated
++] = ex_type
;