Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: net/third_party/nss/ssl/ssl3ext.c

Issue 1053903002: Update libssl to NSS 3.18 RTM (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * SSL3 Protocol 2 * SSL3 Protocol
3 * 3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public 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 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/. */ 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 7
8 /* TLS extension code moved here from ssl3ecc.c */ 8 /* TLS extension code moved here from ssl3ecc.c */
9 9
10 #include "nssrenam.h" 10 #include "nssrenam.h"
(...skipping 30 matching lines...) Expand all
41 PRInt32 lenSize); 41 PRInt32 lenSize);
42 static SECStatus ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss, 42 static SECStatus ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss,
43 PK11SymKey **aes_key, PK11SymKey **mac_key); 43 PK11SymKey **aes_key, PK11SymKey **mac_key);
44 #ifndef NO_PKCS11_BYPASS 44 #ifndef NO_PKCS11_BYPASS
45 static SECStatus ssl3_GetSessionTicketKeys(const unsigned char **aes_key, 45 static SECStatus ssl3_GetSessionTicketKeys(const unsigned char **aes_key,
46 PRUint32 *aes_key_length, const unsigned char **mac_key, 46 PRUint32 *aes_key_length, const unsigned char **mac_key,
47 PRUint32 *mac_key_length); 47 PRUint32 *mac_key_length);
48 #endif 48 #endif
49 static PRInt32 ssl3_SendRenegotiationInfoXtn(sslSocket * ss, 49 static PRInt32 ssl3_SendRenegotiationInfoXtn(sslSocket * ss,
50 PRBool append, PRUint32 maxBytes); 50 PRBool append, PRUint32 maxBytes);
51 static SECStatus ssl3_HandleRenegotiationInfoXtn(sslSocket *ss, 51 static SECStatus ssl3_HandleRenegotiationInfoXtn(sslSocket *ss,
52 PRUint16 ex_type, SECItem *data); 52 PRUint16 ex_type, SECItem *data);
53 static SECStatus ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss, 53 static SECStatus ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss,
54 » » » PRUint16 ex_type, SECItem *data); 54 PRUint16 ex_type, SECItem *data);
55 static SECStatus ssl3_ClientHandleAppProtoXtn(sslSocket *ss, 55 static SECStatus ssl3_ClientHandleAppProtoXtn(sslSocket *ss,
56 » » » PRUint16 ex_type, SECItem *data); 56 PRUint16 ex_type, SECItem *data);
57 static SECStatus ssl3_ServerHandleNextProtoNegoXtn(sslSocket *ss, 57 static SECStatus ssl3_ServerHandleNextProtoNegoXtn(sslSocket *ss,
58 » » » PRUint16 ex_type, SECItem *data); 58 PRUint16 ex_type, SECItem *data);
59 static SECStatus ssl3_ServerHandleAppProtoXtn(sslSocket *ss, PRUint16 ex_type, 59 static SECStatus ssl3_ServerHandleAppProtoXtn(sslSocket *ss, PRUint16 ex_type,
60 SECItem *data); 60 SECItem *data);
61 static PRInt32 ssl3_ClientSendNextProtoNegoXtn(sslSocket *ss, PRBool append, 61 static PRInt32 ssl3_ClientSendNextProtoNegoXtn(sslSocket *ss, PRBool append,
62 PRUint32 maxBytes); 62 PRUint32 maxBytes);
63 static PRInt32 ssl3_ClientSendAppProtoXtn(sslSocket *ss, PRBool append, 63 static PRInt32 ssl3_ClientSendAppProtoXtn(sslSocket *ss, PRBool append,
64 » » » » » PRUint32 maxBytes); 64 PRUint32 maxBytes);
65 static PRInt32 ssl3_ServerSendAppProtoXtn(sslSocket *ss, PRBool append, 65 static PRInt32 ssl3_ServerSendAppProtoXtn(sslSocket *ss, PRBool append,
66 PRUint32 maxBytes); 66 PRUint32 maxBytes);
67 static PRInt32 ssl3_SendUseSRTPXtn(sslSocket *ss, PRBool append, 67 static PRInt32 ssl3_SendUseSRTPXtn(sslSocket *ss, PRBool append,
68 PRUint32 maxBytes); 68 PRUint32 maxBytes);
69 static SECStatus ssl3_HandleUseSRTPXtn(sslSocket * ss, PRUint16 ex_type, 69 static SECStatus ssl3_HandleUseSRTPXtn(sslSocket * ss, PRUint16 ex_type,
70 SECItem *data); 70 SECItem *data);
71 static SECStatus ssl3_ClientHandleChannelIDXtn(sslSocket *ss, 71 static SECStatus ssl3_ClientHandleChannelIDXtn(sslSocket *ss,
72 PRUint16 ex_type, SECItem *data); 72 PRUint16 ex_type, SECItem *data);
73 static PRInt32 ssl3_ClientSendChannelIDXtn(sslSocket *ss, PRBool append, 73 static PRInt32 ssl3_ClientSendChannelIDXtn(sslSocket *ss, PRBool append,
74 PRUint32 maxBytes); 74 PRUint32 maxBytes);
75 static SECStatus ssl3_ServerSendStatusRequestXtn(sslSocket * ss, 75 static PRInt32 ssl3_ServerSendStatusRequestXtn(sslSocket * ss,
76 PRBool append, PRUint32 maxBytes); 76 PRBool append, PRUint32 maxBytes);
77 static SECStatus ssl3_ServerHandleStatusRequestXtn(sslSocket *ss, 77 static SECStatus ssl3_ServerHandleStatusRequestXtn(sslSocket *ss,
78 PRUint16 ex_type, SECItem *data); 78 PRUint16 ex_type, SECItem *data);
79 static SECStatus ssl3_ClientHandleStatusRequestXtn(sslSocket *ss, 79 static SECStatus ssl3_ClientHandleStatusRequestXtn(sslSocket *ss,
80 PRUint16 ex_type, 80 PRUint16 ex_type,
81 SECItem *data); 81 SECItem *data);
82 static PRInt32 ssl3_ClientSendStatusRequestXtn(sslSocket * ss, PRBool append, 82 static PRInt32 ssl3_ClientSendStatusRequestXtn(sslSocket * ss, PRBool append,
83 PRUint32 maxBytes); 83 PRUint32 maxBytes);
84 static PRInt32 ssl3_ClientSendSigAlgsXtn(sslSocket *ss, PRBool append, 84 static PRInt32 ssl3_ClientSendSigAlgsXtn(sslSocket *ss, PRBool append,
85 PRUint32 maxBytes); 85 PRUint32 maxBytes);
86 static SECStatus ssl3_ServerHandleSigAlgsXtn(sslSocket *ss, PRUint16 ex_type, 86 static SECStatus ssl3_ServerHandleSigAlgsXtn(sslSocket *ss, PRUint16 ex_type,
87 SECItem *data); 87 SECItem *data);
88 static PRInt32 ssl3_ClientSendSignedCertTimestampXtn(sslSocket *ss, 88 static PRInt32 ssl3_ClientSendSignedCertTimestampXtn(sslSocket *ss,
89 » » » » » » PRBool append, 89 PRBool append,
90 » » » » » » PRUint32 maxBytes); 90 PRUint32 maxBytes);
91 static SECStatus ssl3_ClientHandleSignedCertTimestampXtn(sslSocket *ss, 91 static SECStatus ssl3_ClientHandleSignedCertTimestampXtn(sslSocket *ss,
92 » » » » » » » PRUint16 ex_type, 92 PRUint16 ex_type,
93 » » » » » » » SECItem *data); 93 SECItem *data);
94
95 static PRInt32 ssl3_ClientSendDraftVersionXtn(sslSocket *ss, PRBool append,
96 PRUint32 maxBytes);
97 static SECStatus ssl3_ServerHandleDraftVersionXtn(sslSocket *ss, PRUint16 ex_typ e,
98 SECItem *data);
94 99
95 /* 100 /*
96 * Write bytes. Using this function means the SECItem structure 101 * Write bytes. Using this function means the SECItem structure
97 * cannot be freed. The caller is expected to call this function 102 * cannot be freed. The caller is expected to call this function
98 * on a shallow copy of the structure. 103 * on a shallow copy of the structure.
99 */ 104 */
100 static SECStatus 105 static SECStatus
101 ssl3_AppendToItem(SECItem *item, const unsigned char *buf, PRUint32 bytes) 106 ssl3_AppendToItem(SECItem *item, const unsigned char *buf, PRUint32 bytes)
102 { 107 {
103 if (bytes > item->len) 108 if (bytes > item->len)
104 » return SECFailure; 109 return SECFailure;
105 110
106 PORT_Memcpy(item->data, buf, bytes); 111 PORT_Memcpy(item->data, buf, bytes);
107 item->data += bytes; 112 item->data += bytes;
108 item->len -= bytes; 113 item->len -= bytes;
109 return SECSuccess; 114 return SECSuccess;
110 } 115 }
111 116
112 /* 117 /*
113 * Write a number in network byte order. Using this function means the 118 * Write a number in network byte order. Using this function means the
114 * SECItem structure cannot be freed. The caller is expected to call 119 * SECItem structure cannot be freed. The caller is expected to call
115 * this function on a shallow copy of the structure. 120 * this function on a shallow copy of the structure.
116 */ 121 */
117 static SECStatus 122 static SECStatus
118 ssl3_AppendNumberToItem(SECItem *item, PRUint32 num, PRInt32 lenSize) 123 ssl3_AppendNumberToItem(SECItem *item, PRUint32 num, PRInt32 lenSize)
119 { 124 {
120 SECStatus rv; 125 SECStatus rv;
121 PRUint8 b[4]; 126 PRUint8 b[4];
122 PRUint8 * p = b; 127 PRUint8 * p = b;
123 128
124 switch (lenSize) { 129 switch (lenSize) {
125 case 4: 130 case 4:
126 » *p++ = (PRUint8) (num >> 24); 131 *p++ = (PRUint8) (num >> 24);
127 case 3: 132 case 3:
128 » *p++ = (PRUint8) (num >> 16); 133 *p++ = (PRUint8) (num >> 16);
129 case 2: 134 case 2:
130 » *p++ = (PRUint8) (num >> 8); 135 *p++ = (PRUint8) (num >> 8);
131 case 1: 136 case 1:
132 » *p = (PRUint8) num; 137 *p = (PRUint8) num;
133 } 138 }
134 rv = ssl3_AppendToItem(item, &b[0], lenSize); 139 rv = ssl3_AppendToItem(item, &b[0], lenSize);
135 return rv; 140 return rv;
136 } 141 }
137 142
138 static SECStatus ssl3_SessionTicketShutdown(void* appData, void* nssData) 143 static SECStatus ssl3_SessionTicketShutdown(void* appData, void* nssData)
139 { 144 {
140 if (session_ticket_enc_key_pkcs11) { 145 if (session_ticket_enc_key_pkcs11) {
141 » PK11_FreeSymKey(session_ticket_enc_key_pkcs11); 146 PK11_FreeSymKey(session_ticket_enc_key_pkcs11);
142 » session_ticket_enc_key_pkcs11 = NULL; 147 session_ticket_enc_key_pkcs11 = NULL;
143 } 148 }
144 if (session_ticket_mac_key_pkcs11) { 149 if (session_ticket_mac_key_pkcs11) {
145 » PK11_FreeSymKey(session_ticket_mac_key_pkcs11); 150 PK11_FreeSymKey(session_ticket_mac_key_pkcs11);
146 » session_ticket_mac_key_pkcs11 = NULL; 151 session_ticket_mac_key_pkcs11 = NULL;
147 } 152 }
148 PORT_Memset(&generate_session_keys_once, 0, 153 PORT_Memset(&generate_session_keys_once, 0,
149 » sizeof(generate_session_keys_once)); 154 sizeof(generate_session_keys_once));
150 return SECSuccess; 155 return SECSuccess;
151 } 156 }
152 157
153 158
154 static PRStatus 159 static PRStatus
155 ssl3_GenerateSessionTicketKeysPKCS11(void *data) 160 ssl3_GenerateSessionTicketKeysPKCS11(void *data)
156 { 161 {
157 SECStatus rv; 162 SECStatus rv;
158 sslSocket *ss = (sslSocket *)data; 163 sslSocket *ss = (sslSocket *)data;
159 SECKEYPrivateKey *svrPrivKey = ss->serverCerts[kt_rsa].SERVERKEY; 164 SECKEYPrivateKey *svrPrivKey = ss->serverCerts[kt_rsa].SERVERKEY;
160 SECKEYPublicKey *svrPubKey = ss->serverCerts[kt_rsa].serverKeyPair->pubKey; 165 SECKEYPublicKey *svrPubKey = ss->serverCerts[kt_rsa].serverKeyPair->pubKey;
161 166
162 if (svrPrivKey == NULL || svrPubKey == NULL) { 167 if (svrPrivKey == NULL || svrPubKey == NULL) {
163 » SSL_DBG(("%d: SSL[%d]: Pub or priv key(s) is NULL.", 168 SSL_DBG(("%d: SSL[%d]: Pub or priv key(s) is NULL.",
164 » » » SSL_GETPID(), ss->fd)); 169 SSL_GETPID(), ss->fd));
165 » goto loser; 170 goto loser;
166 } 171 }
167 172
168 /* Get a copy of the session keys from shared memory. */ 173 /* Get a copy of the session keys from shared memory. */
169 PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX, 174 PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX,
170 » sizeof(SESS_TICKET_KEY_NAME_PREFIX)); 175 sizeof(SESS_TICKET_KEY_NAME_PREFIX));
171 if (!ssl_GetSessionTicketKeysPKCS11(svrPrivKey, svrPubKey, 176 if (!ssl_GetSessionTicketKeysPKCS11(svrPrivKey, svrPubKey,
172 » ss->pkcs11PinArg, &key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN], 177 ss->pkcs11PinArg, &key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN],
173 » &session_ticket_enc_key_pkcs11, &session_ticket_mac_key_pkcs11)) 178 &session_ticket_enc_key_pkcs11, &session_ticket_mac_key_pkcs11))
174 » return PR_FAILURE; 179 return PR_FAILURE;
175 180
176 rv = NSS_RegisterShutdown(ssl3_SessionTicketShutdown, NULL); 181 rv = NSS_RegisterShutdown(ssl3_SessionTicketShutdown, NULL);
177 if (rv != SECSuccess) 182 if (rv != SECSuccess)
178 » goto loser; 183 goto loser;
179 184
180 return PR_SUCCESS; 185 return PR_SUCCESS;
181 186
182 loser: 187 loser:
183 ssl3_SessionTicketShutdown(NULL, NULL); 188 ssl3_SessionTicketShutdown(NULL, NULL);
184 return PR_FAILURE; 189 return PR_FAILURE;
185 } 190 }
186 191
187 static SECStatus 192 static SECStatus
188 ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss, PK11SymKey **aes_key, 193 ssl3_GetSessionTicketKeysPKCS11(sslSocket *ss, PK11SymKey **aes_key,
189 PK11SymKey **mac_key) 194 PK11SymKey **mac_key)
190 { 195 {
191 if (PR_CallOnceWithArg(&generate_session_keys_once, 196 if (PR_CallOnceWithArg(&generate_session_keys_once,
192 » ssl3_GenerateSessionTicketKeysPKCS11, ss) != PR_SUCCESS) 197 ssl3_GenerateSessionTicketKeysPKCS11, ss) != PR_SUCCESS)
193 » return SECFailure; 198 return SECFailure;
194 199
195 if (session_ticket_enc_key_pkcs11 == NULL || 200 if (session_ticket_enc_key_pkcs11 == NULL ||
196 » session_ticket_mac_key_pkcs11 == NULL) 201 session_ticket_mac_key_pkcs11 == NULL)
197 » return SECFailure; 202 return SECFailure;
198 203
199 *aes_key = session_ticket_enc_key_pkcs11; 204 *aes_key = session_ticket_enc_key_pkcs11;
200 *mac_key = session_ticket_mac_key_pkcs11; 205 *mac_key = session_ticket_mac_key_pkcs11;
201 return SECSuccess; 206 return SECSuccess;
202 } 207 }
203 208
204 #ifndef NO_PKCS11_BYPASS 209 #ifndef NO_PKCS11_BYPASS
205 static PRStatus 210 static PRStatus
206 ssl3_GenerateSessionTicketKeys(void) 211 ssl3_GenerateSessionTicketKeys(void)
207 { 212 {
208 PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX, 213 PORT_Memcpy(key_name, SESS_TICKET_KEY_NAME_PREFIX,
209 » sizeof(SESS_TICKET_KEY_NAME_PREFIX)); 214 sizeof(SESS_TICKET_KEY_NAME_PREFIX));
210 215
211 if (!ssl_GetSessionTicketKeys(&key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN], 216 if (!ssl_GetSessionTicketKeys(&key_name[SESS_TICKET_KEY_NAME_PREFIX_LEN],
212 » session_ticket_enc_key, session_ticket_mac_key)) 217 session_ticket_enc_key, session_ticket_mac_key))
213 » return PR_FAILURE; 218 return PR_FAILURE;
214 219
215 session_ticket_keys_initialized = PR_TRUE; 220 session_ticket_keys_initialized = PR_TRUE;
216 return PR_SUCCESS; 221 return PR_SUCCESS;
217 } 222 }
218 223
219 static SECStatus 224 static SECStatus
220 ssl3_GetSessionTicketKeys(const unsigned char **aes_key, 225 ssl3_GetSessionTicketKeys(const unsigned char **aes_key,
221 PRUint32 *aes_key_length, const unsigned char **mac_key, 226 PRUint32 *aes_key_length, const unsigned char **mac_key,
222 PRUint32 *mac_key_length) 227 PRUint32 *mac_key_length)
223 { 228 {
224 if (PR_CallOnce(&generate_session_keys_once, 229 if (PR_CallOnce(&generate_session_keys_once,
225 » ssl3_GenerateSessionTicketKeys) != PR_SUCCESS) 230 ssl3_GenerateSessionTicketKeys) != PR_SUCCESS)
226 » return SECFailure; 231 return SECFailure;
227 232
228 if (!session_ticket_keys_initialized) 233 if (!session_ticket_keys_initialized)
229 » return SECFailure; 234 return SECFailure;
230 235
231 *aes_key = session_ticket_enc_key; 236 *aes_key = session_ticket_enc_key;
232 *aes_key_length = sizeof(session_ticket_enc_key); 237 *aes_key_length = sizeof(session_ticket_enc_key);
233 *mac_key = session_ticket_mac_key; 238 *mac_key = session_ticket_mac_key;
234 *mac_key_length = sizeof(session_ticket_mac_key); 239 *mac_key_length = sizeof(session_ticket_mac_key);
235 240
236 return SECSuccess; 241 return SECSuccess;
237 } 242 }
238 #endif 243 #endif
239 244
240 /* Table of handlers for received TLS hello extensions, one per extension. 245 /* Table of handlers for received TLS hello extensions, one per extension.
241 * In the second generation, this table will be dynamic, and functions 246 * In the second generation, this table will be dynamic, and functions
242 * will be registered here. 247 * will be registered here.
243 */ 248 */
244 /* This table is used by the server, to handle client hello extensions. */ 249 /* This table is used by the server, to handle client hello extensions. */
245 static const ssl3HelloExtensionHandler clientHelloHandlers[] = { 250 static const ssl3HelloExtensionHandler clientHelloHandlers[] = {
246 { ssl_server_name_xtn, &ssl3_HandleServerNameXtn }, 251 { ssl_server_name_xtn, &ssl3_HandleServerNameXtn },
247 #ifdef NSS_ENABLE_ECC 252 #ifndef NSS_DISABLE_ECC
248 { ssl_elliptic_curves_xtn, &ssl3_HandleSupportedCurvesXtn }, 253 { ssl_elliptic_curves_xtn, &ssl3_HandleSupportedCurvesXtn },
249 { ssl_ec_point_formats_xtn, &ssl3_HandleSupportedPointFormatsXtn }, 254 { ssl_ec_point_formats_xtn, &ssl3_HandleSupportedPointFormatsXtn },
250 #endif 255 #endif
251 { ssl_session_ticket_xtn, &ssl3_ServerHandleSessionTicketXtn }, 256 { ssl_session_ticket_xtn, &ssl3_ServerHandleSessionTicketXtn },
252 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn }, 257 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
253 { ssl_next_proto_nego_xtn, &ssl3_ServerHandleNextProtoNegoXtn }, 258 { ssl_next_proto_nego_xtn, &ssl3_ServerHandleNextProtoNegoXtn },
254 { ssl_app_layer_protocol_xtn, &ssl3_ServerHandleAppProtoXtn }, 259 { ssl_app_layer_protocol_xtn, &ssl3_ServerHandleAppProtoXtn },
255 { ssl_use_srtp_xtn, &ssl3_HandleUseSRTPXtn }, 260 { ssl_use_srtp_xtn, &ssl3_HandleUseSRTPXtn },
261 { ssl_channel_id_xtn, &ssl3_ClientSendChannelIDXtn },
256 { ssl_cert_status_xtn, &ssl3_ServerHandleStatusRequestXtn }, 262 { ssl_cert_status_xtn, &ssl3_ServerHandleStatusRequestXtn },
257 { ssl_signature_algorithms_xtn, &ssl3_ServerHandleSigAlgsXtn }, 263 { ssl_signature_algorithms_xtn, &ssl3_ServerHandleSigAlgsXtn },
264 { ssl_tls13_draft_version_xtn, &ssl3_ServerHandleDraftVersionXtn },
258 { -1, NULL } 265 { -1, NULL }
259 }; 266 };
260 267
261 /* These two tables are used by the client, to handle server hello 268 /* These two tables are used by the client, to handle server hello
262 * extensions. */ 269 * extensions. */
263 static const ssl3HelloExtensionHandler serverHelloHandlersTLS[] = { 270 static const ssl3HelloExtensionHandler serverHelloHandlersTLS[] = {
264 { ssl_server_name_xtn, &ssl3_HandleServerNameXtn }, 271 { ssl_server_name_xtn, &ssl3_HandleServerNameXtn },
265 /* TODO: add a handler for ssl_ec_point_formats_xtn */ 272 /* TODO: add a handler for ssl_ec_point_formats_xtn */
266 { ssl_session_ticket_xtn, &ssl3_ClientHandleSessionTicketXtn }, 273 { ssl_session_ticket_xtn, &ssl3_ClientHandleSessionTicketXtn },
267 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn }, 274 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
(...skipping 11 matching lines...) Expand all
279 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn }, 286 { ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
280 { -1, NULL } 287 { -1, NULL }
281 }; 288 };
282 289
283 /* Tables of functions to format TLS hello extensions, one function per 290 /* Tables of functions to format TLS hello extensions, one function per
284 * extension. 291 * extension.
285 * These static tables are for the formatting of client hello extensions. 292 * These static tables are for the formatting of client hello extensions.
286 * The server's table of hello senders is dynamic, in the socket struct, 293 * The server's table of hello senders is dynamic, in the socket struct,
287 * and sender functions are registered there. 294 * and sender functions are registered there.
288 */ 295 */
289 static const 296 static const
290 ssl3HelloExtensionSender clientHelloSendersTLS[SSL_MAX_EXTENSIONS] = { 297 ssl3HelloExtensionSender clientHelloSendersTLS[SSL_MAX_EXTENSIONS] = {
291 { ssl_server_name_xtn, &ssl3_SendServerNameXtn }, 298 { ssl_server_name_xtn, &ssl3_SendServerNameXtn },
292 { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn }, 299 { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn },
293 #ifdef NSS_ENABLE_ECC 300 #ifndef NSS_DISABLE_ECC
294 { ssl_elliptic_curves_xtn, &ssl3_SendSupportedCurvesXtn }, 301 { ssl_elliptic_curves_xtn, &ssl3_SendSupportedCurvesXtn },
295 { ssl_ec_point_formats_xtn, &ssl3_SendSupportedPointFormatsXtn }, 302 { ssl_ec_point_formats_xtn, &ssl3_SendSupportedPointFormatsXtn },
296 #endif 303 #endif
297 { ssl_session_ticket_xtn, &ssl3_SendSessionTicketXtn }, 304 { ssl_session_ticket_xtn, &ssl3_SendSessionTicketXtn },
298 { ssl_next_proto_nego_xtn, &ssl3_ClientSendNextProtoNegoXtn }, 305 { ssl_next_proto_nego_xtn, &ssl3_ClientSendNextProtoNegoXtn },
299 { ssl_app_layer_protocol_xtn, &ssl3_ClientSendAppProtoXtn }, 306 { ssl_app_layer_protocol_xtn, &ssl3_ClientSendAppProtoXtn },
300 { ssl_use_srtp_xtn, &ssl3_SendUseSRTPXtn }, 307 { ssl_use_srtp_xtn, &ssl3_SendUseSRTPXtn },
301 { ssl_channel_id_xtn, &ssl3_ClientSendChannelIDXtn },
302 { ssl_cert_status_xtn, &ssl3_ClientSendStatusRequestXtn }, 308 { ssl_cert_status_xtn, &ssl3_ClientSendStatusRequestXtn },
309 { ssl_signature_algorithms_xtn, &ssl3_ClientSendSigAlgsXtn },
310 { ssl_tls13_draft_version_xtn, &ssl3_ClientSendDraftVersionXtn },
303 { ssl_signed_certificate_timestamp_xtn, 311 { ssl_signed_certificate_timestamp_xtn,
304 &ssl3_ClientSendSignedCertTimestampXtn }, 312 &ssl3_ClientSendSignedCertTimestampXtn },
305 /* WebSphere Application Server 7.0 is intolerant to the last extension
306 * being zero-length. It is not intolerant of TLS 1.2, so move
307 * signature_algorithms to the end. */
308 { ssl_signature_algorithms_xtn, &ssl3_ClientSendSigAlgsXtn }
Ryan Sleevi 2015/04/02 23:35:40 this is the other bug; I'm not sure how my patch m
309 /* any extra entries will appear as { 0, NULL } */ 313 /* any extra entries will appear as { 0, NULL } */
310 }; 314 };
311 315
312 static const 316 static const
313 ssl3HelloExtensionSender clientHelloSendersSSL3[SSL_MAX_EXTENSIONS] = { 317 ssl3HelloExtensionSender clientHelloSendersSSL3[SSL_MAX_EXTENSIONS] = {
314 { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn } 318 { ssl_renegotiation_info_xtn, &ssl3_SendRenegotiationInfoXtn }
315 /* any extra entries will appear as { 0, NULL } */ 319 /* any extra entries will appear as { 0, NULL } */
316 }; 320 };
317 321
318 static PRBool 322 static PRBool
319 arrayContainsExtension(const PRUint16 *array, PRUint32 len, PRUint16 ex_type) 323 arrayContainsExtension(const PRUint16 *array, PRUint32 len, PRUint16 ex_type)
320 { 324 {
321 int i; 325 int i;
322 for (i = 0; i < len; i++) { 326 for (i = 0; i < len; i++) {
323 » if (ex_type == array[i]) 327 if (ex_type == array[i])
324 » return PR_TRUE; 328 return PR_TRUE;
325 } 329 }
326 return PR_FALSE; 330 return PR_FALSE;
327 } 331 }
328 332
329 PRBool 333 PRBool
330 ssl3_ExtensionNegotiated(sslSocket *ss, PRUint16 ex_type) { 334 ssl3_ExtensionNegotiated(sslSocket *ss, PRUint16 ex_type) {
331 TLSExtensionData *xtnData = &ss->xtnData; 335 TLSExtensionData *xtnData = &ss->xtnData;
332 return arrayContainsExtension(xtnData->negotiated, 336 return arrayContainsExtension(xtnData->negotiated,
333 » xtnData->numNegotiated, ex_type); 337 xtnData->numNegotiated, ex_type);
334 } 338 }
335 339
336 static PRBool 340 static PRBool
337 ssl3_ClientExtensionAdvertised(sslSocket *ss, PRUint16 ex_type) { 341 ssl3_ClientExtensionAdvertised(sslSocket *ss, PRUint16 ex_type) {
338 TLSExtensionData *xtnData = &ss->xtnData; 342 TLSExtensionData *xtnData = &ss->xtnData;
339 return arrayContainsExtension(xtnData->advertised, 343 return arrayContainsExtension(xtnData->advertised,
340 » xtnData->numAdvertised, ex_type); 344 xtnData->numAdvertised, ex_type);
341 } 345 }
342 346
343 /* Format an SNI extension, using the name from the socket's URL, 347 /* Format an SNI extension, using the name from the socket's URL,
344 * unless that name is a dotted decimal string. 348 * unless that name is a dotted decimal string.
345 * Used by client and server. 349 * Used by client and server.
346 */ 350 */
347 PRInt32 351 PRInt32
348 ssl3_SendServerNameXtn(sslSocket * ss, PRBool append, 352 ssl3_SendServerNameXtn(sslSocket * ss, PRBool append,
349 PRUint32 maxBytes) 353 PRUint32 maxBytes)
350 { 354 {
351 SECStatus rv; 355 SECStatus rv;
352 if (!ss) 356 if (!ss)
353 » return 0; 357 return 0;
354 if (!ss->sec.isServer) { 358 if (!ss->sec.isServer) {
355 PRUint32 len; 359 PRUint32 len;
356 PRNetAddr netAddr; 360 PRNetAddr netAddr;
357 361
358 /* must have a hostname */ 362 /* must have a hostname */
359 if (!ss->url || !ss->url[0]) 363 if (!ss->url || !ss->url[0])
360 return 0; 364 return 0;
361 /* must not be an IPv4 or IPv6 address */ 365 /* must not be an IPv4 or IPv6 address */
362 if (PR_SUCCESS == PR_StringToNetAddr(ss->url, &netAddr)) { 366 if (PR_SUCCESS == PR_StringToNetAddr(ss->url, &netAddr)) {
363 /* is an IP address (v4 or v6) */ 367 /* is an IP address (v4 or v6) */
364 return 0; 368 return 0;
365 } 369 }
366 len = PORT_Strlen(ss->url); 370 len = PORT_Strlen(ss->url);
367 if (append && maxBytes >= len + 9) { 371 if (append && maxBytes >= len + 9) {
368 /* extension_type */ 372 /* extension_type */
369 rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2); 373 rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2);
370 if (rv != SECSuccess) return -1; 374 if (rv != SECSuccess) return -1;
371 /* length of extension_data */ 375 /* length of extension_data */
372 rv = ssl3_AppendHandshakeNumber(ss, len + 5, 2); 376 rv = ssl3_AppendHandshakeNumber(ss, len + 5, 2);
373 if (rv != SECSuccess) return -1; 377 if (rv != SECSuccess) return -1;
374 /* length of server_name_list */ 378 /* length of server_name_list */
375 rv = ssl3_AppendHandshakeNumber(ss, len + 3, 2); 379 rv = ssl3_AppendHandshakeNumber(ss, len + 3, 2);
376 if (rv != SECSuccess) return -1; 380 if (rv != SECSuccess) return -1;
377 /* Name Type (sni_host_name) */ 381 /* Name Type (sni_host_name) */
378 rv = ssl3_AppendHandshake(ss, "\0", 1); 382 rv = ssl3_AppendHandshake(ss, "\0", 1);
379 if (rv != SECSuccess) return -1; 383 if (rv != SECSuccess) return -1;
380 /* HostName (length and value) */ 384 /* HostName (length and value) */
381 rv = ssl3_AppendHandshakeVariable(ss, (PRUint8 *)ss->url, len, 2); 385 rv = ssl3_AppendHandshakeVariable(ss, (PRUint8 *)ss->url, len, 2);
382 if (rv != SECSuccess) return -1; 386 if (rv != SECSuccess) return -1;
383 if (!ss->sec.isServer) { 387 if (!ss->sec.isServer) {
384 TLSExtensionData *xtnData = &ss->xtnData; 388 TLSExtensionData *xtnData = &ss->xtnData;
385 xtnData->advertised[xtnData->numAdvertised++] = 389 xtnData->advertised[xtnData->numAdvertised++] =
386 » » ssl_server_name_xtn; 390 ssl_server_name_xtn;
387 } 391 }
388 } 392 }
389 return len + 9; 393 return len + 9;
390 } 394 }
391 /* Server side */ 395 /* Server side */
392 if (append && maxBytes >= 4) { 396 if (append && maxBytes >= 4) {
393 rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2); 397 rv = ssl3_AppendHandshakeNumber(ss, ssl_server_name_xtn, 2);
394 if (rv != SECSuccess) return -1; 398 if (rv != SECSuccess) return -1;
395 /* length of extension_data */ 399 /* length of extension_data */
396 rv = ssl3_AppendHandshakeNumber(ss, 0, 2); 400 rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
(...skipping 21 matching lines...) Expand all
418 } 422 }
419 return SECSuccess; 423 return SECSuccess;
420 } 424 }
421 425
422 /* Server side - consume client data and register server sender. */ 426 /* Server side - consume client data and register server sender. */
423 /* do not parse the data if don't have user extension handling function. */ 427 /* do not parse the data if don't have user extension handling function. */
424 if (!ss->sniSocketConfig) { 428 if (!ss->sniSocketConfig) {
425 return SECSuccess; 429 return SECSuccess;
426 } 430 }
427 /* length of server_name_list */ 431 /* length of server_name_list */
428 listLenBytes = ssl3_ConsumeHandshakeNumber(ss, 2, &data->data, &data->len); 432 listLenBytes = ssl3_ConsumeHandshakeNumber(ss, 2, &data->data, &data->len);
429 if (listLenBytes == 0 || listLenBytes != data->len) { 433 if (listLenBytes == 0 || listLenBytes != data->len) {
430 return SECFailure; 434 return SECFailure;
431 } 435 }
432 ldata = *data; 436 ldata = *data;
433 /* Calculate the size of the array.*/ 437 /* Calculate the size of the array.*/
434 while (listLenBytes > 0) { 438 while (listLenBytes > 0) {
435 SECItem litem; 439 SECItem litem;
436 SECStatus rv; 440 SECStatus rv;
437 PRInt32 type; 441 PRInt32 type;
438 /* Name Type (sni_host_name) */ 442 /* Name Type (sni_host_name) */
439 type = ssl3_ConsumeHandshakeNumber(ss, 1, &ldata.data, &ldata.len); 443 type = ssl3_ConsumeHandshakeNumber(ss, 1, &ldata.data, &ldata.len);
440 if (!ldata.len) { 444 if (!ldata.len) {
441 return SECFailure; 445 return SECFailure;
442 } 446 }
443 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 2, &ldata.data, &ldata.le n); 447 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 2, &ldata.data, &ldata.le n);
444 if (rv != SECSuccess) { 448 if (rv != SECSuccess) {
445 return SECFailure; 449 return SECFailure;
446 } 450 }
447 /* Adjust total length for cunsumed item, item len and type.*/ 451 /* Adjust total length for cunsumed item, item len and type.*/
448 listLenBytes -= litem.len + 3; 452 listLenBytes -= litem.len + 3;
449 if (listLenBytes > 0 && !ldata.len) { 453 if (listLenBytes > 0 && !ldata.len) {
450 return SECFailure; 454 return SECFailure;
451 } 455 }
452 listCount += 1; 456 listCount += 1;
453 } 457 }
454 if (!listCount) { 458 if (!listCount) {
455 return SECFailure; 459 return SECFailure;
456 } 460 }
457 names = PORT_ZNewArray(SECItem, listCount); 461 names = PORT_ZNewArray(SECItem, listCount);
458 if (!names) { 462 if (!names) {
459 return SECFailure; 463 return SECFailure;
460 } 464 }
461 for (i = 0;i < listCount;i++) { 465 for (i = 0;i < listCount;i++) {
462 int j; 466 int j;
463 PRInt32 type; 467 PRInt32 type;
464 SECStatus rv; 468 SECStatus rv;
465 PRBool nametypePresent = PR_FALSE; 469 PRBool nametypePresent = PR_FALSE;
466 /* Name Type (sni_host_name) */ 470 /* Name Type (sni_host_name) */
467 type = ssl3_ConsumeHandshakeNumber(ss, 1, &data->data, &data->len); 471 type = ssl3_ConsumeHandshakeNumber(ss, 1, &data->data, &data->len);
468 /* Check if we have such type in the list */ 472 /* Check if we have such type in the list */
469 for (j = 0;j < listCount && names[j].data;j++) { 473 for (j = 0;j < listCount && names[j].data;j++) {
470 if (names[j].type == type) { 474 if (names[j].type == type) {
471 nametypePresent = PR_TRUE; 475 nametypePresent = PR_TRUE;
472 break; 476 break;
473 } 477 }
474 } 478 }
475 /* HostName (length and value) */ 479 /* HostName (length and value) */
476 rv = ssl3_ConsumeHandshakeVariable(ss, &names[namesPos], 2, 480 rv = ssl3_ConsumeHandshakeVariable(ss, &names[namesPos], 2,
477 &data->data, &data->len); 481 &data->data, &data->len);
(...skipping 11 matching lines...) Expand all
489 xtnData->sniNameArr = names; 493 xtnData->sniNameArr = names;
490 xtnData->sniNameArrSize = namesPos; 494 xtnData->sniNameArrSize = namesPos;
491 xtnData->negotiated[xtnData->numNegotiated++] = ssl_server_name_xtn; 495 xtnData->negotiated[xtnData->numNegotiated++] = ssl_server_name_xtn;
492 496
493 return SECSuccess; 497 return SECSuccess;
494 498
495 loser: 499 loser:
496 PORT_Free(names); 500 PORT_Free(names);
497 return SECFailure; 501 return SECFailure;
498 } 502 }
499 503
500 /* Called by both clients and servers. 504 /* Called by both clients and servers.
501 * Clients sends a filled in session ticket if one is available, and otherwise 505 * Clients sends a filled in session ticket if one is available, and otherwise
502 * sends an empty ticket. Servers always send empty tickets. 506 * sends an empty ticket. Servers always send empty tickets.
503 */ 507 */
504 PRInt32 508 PRInt32
505 ssl3_SendSessionTicketXtn( 509 ssl3_SendSessionTicketXtn(
506 » » » sslSocket * ss, 510 sslSocket * ss,
507 » » » PRBool append, 511 PRBool append,
508 » » » PRUint32 maxBytes) 512 PRUint32 maxBytes)
509 { 513 {
510 PRInt32 extension_length; 514 PRInt32 extension_length;
511 NewSessionTicket *session_ticket = NULL; 515 NewSessionTicket *session_ticket = NULL;
512 sslSessionID *sid = ss->sec.ci.sid; 516 sslSessionID *sid = ss->sec.ci.sid;
513 517
514 /* Ignore the SessionTicket extension if processing is disabled. */ 518 /* Ignore the SessionTicket extension if processing is disabled. */
515 if (!ss->opt.enableSessionTickets) 519 if (!ss->opt.enableSessionTickets)
516 » return 0; 520 return 0;
517 521
518 /* Empty extension length = extension_type (2-bytes) + 522 /* Empty extension length = extension_type (2-bytes) +
519 * length(extension_data) (2-bytes) 523 * length(extension_data) (2-bytes)
520 */ 524 */
521 extension_length = 4; 525 extension_length = 4;
522 526
523 /* If we are a client then send a session ticket if one is availble. 527 /* If we are a client then send a session ticket if one is availble.
524 * Servers that support the extension and are willing to negotiate the 528 * Servers that support the extension and are willing to negotiate the
525 * the extension always respond with an empty extension. 529 * the extension always respond with an empty extension.
526 */ 530 */
527 if (!ss->sec.isServer) { 531 if (!ss->sec.isServer) {
528 » /* The caller must be holding sid->u.ssl3.lock for reading. We cannot 532 /* The caller must be holding sid->u.ssl3.lock for reading. We cannot
529 » * just acquire and release the lock within this function because the 533 * just acquire and release the lock within this function because the
530 » * caller will call this function twice, and we need the inputs to be 534 * caller will call this function twice, and we need the inputs to be
531 » * consistent between the two calls. Note that currently the caller 535 * consistent between the two calls. Note that currently the caller
532 » * will only be holding the lock when we are the client and when we're 536 * will only be holding the lock when we are the client and when we're
533 » * attempting to resume an existing session. 537 * attempting to resume an existing session.
534 » */ 538 */
535 539
536 » session_ticket = &sid->u.ssl3.locked.sessionTicket; 540 session_ticket = &sid->u.ssl3.locked.sessionTicket;
537 » if (session_ticket->ticket.data) { 541 if (session_ticket->ticket.data) {
538 » if (ss->xtnData.ticketTimestampVerified) { 542 if (ss->xtnData.ticketTimestampVerified) {
539 » » extension_length += session_ticket->ticket.len; 543 extension_length += session_ticket->ticket.len;
540 » } else if (!append && 544 } else if (!append &&
541 » » (session_ticket->ticket_lifetime_hint == 0 || 545 (session_ticket->ticket_lifetime_hint == 0 ||
542 » » (session_ticket->ticket_lifetime_hint + 546 (session_ticket->ticket_lifetime_hint +
543 » » session_ticket->received_timestamp > ssl_Time()))) { 547 session_ticket->received_timestamp > ssl_Time()))) {
544 » » extension_length += session_ticket->ticket.len; 548 extension_length += session_ticket->ticket.len;
545 » » ss->xtnData.ticketTimestampVerified = PR_TRUE; 549 ss->xtnData.ticketTimestampVerified = PR_TRUE;
546 » } 550 }
547 » } 551 }
548 } 552 }
549 553
550 if (append && maxBytes >= extension_length) { 554 if (append && maxBytes >= extension_length) {
551 » SECStatus rv; 555 SECStatus rv;
552 » /* extension_type */ 556 /* extension_type */
553 rv = ssl3_AppendHandshakeNumber(ss, ssl_session_ticket_xtn, 2); 557 rv = ssl3_AppendHandshakeNumber(ss, ssl_session_ticket_xtn, 2);
554 if (rv != SECSuccess) 558 if (rv != SECSuccess)
555 » goto loser; 559 goto loser;
556 » if (session_ticket && session_ticket->ticket.data && 560 if (session_ticket && session_ticket->ticket.data &&
557 » ss->xtnData.ticketTimestampVerified) { 561 ss->xtnData.ticketTimestampVerified) {
558 » rv = ssl3_AppendHandshakeVariable(ss, session_ticket->ticket.data, 562 rv = ssl3_AppendHandshakeVariable(ss, session_ticket->ticket.data,
559 » » session_ticket->ticket.len, 2); 563 session_ticket->ticket.len, 2);
560 » ss->xtnData.ticketTimestampVerified = PR_FALSE; 564 ss->xtnData.ticketTimestampVerified = PR_FALSE;
561 » ss->xtnData.sentSessionTicketInClientHello = PR_TRUE; 565 ss->xtnData.sentSessionTicketInClientHello = PR_TRUE;
562 » } else { 566 } else {
563 » rv = ssl3_AppendHandshakeNumber(ss, 0, 2); 567 rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
564 » } 568 }
565 if (rv != SECSuccess) 569 if (rv != SECSuccess)
566 » goto loser; 570 goto loser;
567 571
568 » if (!ss->sec.isServer) { 572 if (!ss->sec.isServer) {
569 » TLSExtensionData *xtnData = &ss->xtnData; 573 TLSExtensionData *xtnData = &ss->xtnData;
570 » xtnData->advertised[xtnData->numAdvertised++] = 574 xtnData->advertised[xtnData->numAdvertised++] =
571 » » ssl_session_ticket_xtn; 575 ssl_session_ticket_xtn;
572 » } 576 }
573 } else if (maxBytes < extension_length) { 577 } else if (maxBytes < extension_length) {
574 » PORT_Assert(0); 578 PORT_Assert(0);
575 return 0; 579 return 0;
576 } 580 }
577 return extension_length; 581 return extension_length;
578 582
579 loser: 583 loser:
580 ss->xtnData.ticketTimestampVerified = PR_FALSE; 584 ss->xtnData.ticketTimestampVerified = PR_FALSE;
581 return -1; 585 return -1;
582 } 586 }
583 587
584 /* handle an incoming Next Protocol Negotiation extension. */ 588 /* handle an incoming Next Protocol Negotiation extension. */
585 static SECStatus 589 static SECStatus
586 ssl3_ServerHandleNextProtoNegoXtn(sslSocket * ss, PRUint16 ex_type, 590 ssl3_ServerHandleNextProtoNegoXtn(sslSocket * ss, PRUint16 ex_type,
587 SECItem *data) 591 SECItem *data)
588 { 592 {
589 if (ss->firstHsDone || data->len != 0) { 593 if (ss->firstHsDone || data->len != 0) {
590 » /* Clients MUST send an empty NPN extension, if any. */ 594 /* Clients MUST send an empty NPN extension, if any. */
591 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID); 595 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
592 » return SECFailure; 596 return SECFailure;
593 } 597 }
594 598
595 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 599 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
596 600
597 /* TODO: server side NPN support would require calling 601 /* TODO: server side NPN support would require calling
598 * ssl3_RegisterServerHelloExtensionSender here in order to echo the 602 * ssl3_RegisterServerHelloExtensionSender here in order to echo the
599 * extension back to the client. */ 603 * extension back to the client. */
600 604
601 return SECSuccess; 605 return SECSuccess;
602 } 606 }
603 607
604 /* ssl3_ValidateNextProtoNego checks that the given block of data is valid: none 608 /* ssl3_ValidateNextProtoNego checks that the given block of data is valid: none
605 * of the lengths may be 0 and the sum of the lengths must equal the length of 609 * of the lengths may be 0 and the sum of the lengths must equal the length of
606 * the block. */ 610 * the block. */
607 SECStatus 611 SECStatus
608 ssl3_ValidateNextProtoNego(const unsigned char* data, unsigned int length) 612 ssl3_ValidateNextProtoNego(const unsigned char* data, unsigned int length)
609 { 613 {
610 unsigned int offset = 0; 614 unsigned int offset = 0;
611 615
612 while (offset < length) { 616 while (offset < length) {
613 » unsigned int newOffset = offset + 1 + (unsigned int) data[offset]; 617 unsigned int newOffset = offset + 1 + (unsigned int) data[offset];
614 » /* Reject embedded nulls to protect against buggy applications that 618 /* Reject embedded nulls to protect against buggy applications that
615 » * store protocol identifiers in null-terminated strings. 619 * store protocol identifiers in null-terminated strings.
616 » */ 620 */
617 » if (newOffset > length || data[offset] == 0) { 621 if (newOffset > length || data[offset] == 0) {
618 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID); 622 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
619 » return SECFailure; 623 return SECFailure;
620 » } 624 }
621 » offset = newOffset; 625 offset = newOffset;
622 } 626 }
623 627
624 if (offset > length) { 628 if (offset > length) {
625 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID); 629 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
626 » return SECFailure; 630 return SECFailure;
627 } 631 }
628 632
629 return SECSuccess; 633 return SECSuccess;
630 } 634 }
631 635
632 /* protocol selection handler for ALPN (server side) and NPN (client side) */ 636 /* protocol selection handler for ALPN (server side) and NPN (client side) */
633 static SECStatus 637 static SECStatus
634 ssl3_SelectAppProtocol(sslSocket *ss, PRUint16 ex_type, SECItem *data) 638 ssl3_SelectAppProtocol(sslSocket *ss, PRUint16 ex_type, SECItem *data)
635 { 639 {
636 SECStatus rv; 640 SECStatus rv;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 return SECSuccess; 716 return SECSuccess;
713 } 717 }
714 718
715 static SECStatus 719 static SECStatus
716 ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss, PRUint16 ex_type, 720 ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss, PRUint16 ex_type,
717 SECItem *data) 721 SECItem *data)
718 { 722 {
719 PORT_Assert(!ss->firstHsDone); 723 PORT_Assert(!ss->firstHsDone);
720 724
721 if (ssl3_ExtensionNegotiated(ss, ssl_app_layer_protocol_xtn)) { 725 if (ssl3_ExtensionNegotiated(ss, ssl_app_layer_protocol_xtn)) {
722 » /* If the server negotiated ALPN then it has already told us what protoc ol 726 /* If the server negotiated ALPN then it has already told us what
723 » * to use, so it doesn't make sense for us to try to negotiate a differe nt 727 * protocol to use, so it doesn't make sense for us to try to negotiate
724 » * one by sending the NPN handshake message. However, if we've negotiate d 728 * a different one by sending the NPN handshake message. However, if
725 » * NPN then we're required to send the NPN handshake message. Thus, thes e 729 * we've negotiated NPN then we're required to send the NPN handshake
726 » * two extensions cannot both be negotiated on the same connection. */ 730 * message. Thus, these two extensions cannot both be negotiated on the
727 » PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 731 * same connection. */
728 » return SECFailure; 732 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
733 return SECFailure;
729 } 734 }
730 735
731 /* We should only get this call if we sent the extension, so 736 /* We should only get this call if we sent the extension, so
732 * ss->nextProtoCallback needs to be non-NULL. However, it is possible 737 * ss->nextProtoCallback needs to be non-NULL. However, it is possible
733 * that an application erroneously cleared the callback between the time 738 * that an application erroneously cleared the callback between the time
734 * we sent the ClientHello and now. */ 739 * we sent the ClientHello and now. */
735 if (!ss->nextProtoCallback) { 740 if (!ss->nextProtoCallback) {
736 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_NO_CALLBACK); 741 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_NO_CALLBACK);
737 » return SECFailure; 742 return SECFailure;
738 } 743 }
739 744
740 return ssl3_SelectAppProtocol(ss, ex_type, data); 745 return ssl3_SelectAppProtocol(ss, ex_type, data);
741 } 746 }
742 747
743 static SECStatus 748 static SECStatus
744 ssl3_ClientHandleAppProtoXtn(sslSocket *ss, PRUint16 ex_type, SECItem *data) 749 ssl3_ClientHandleAppProtoXtn(sslSocket *ss, PRUint16 ex_type, SECItem *data)
745 { 750 {
746 const unsigned char* d = data->data; 751 const unsigned char* d = data->data;
747 PRUint16 name_list_len; 752 PRUint16 name_list_len;
748 SECItem protocol_name; 753 SECItem protocol_name;
749 754
750 if (ssl3_ExtensionNegotiated(ss, ssl_next_proto_nego_xtn)) { 755 if (ssl3_ExtensionNegotiated(ss, ssl_next_proto_nego_xtn)) {
751 » PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 756 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
752 » return SECFailure; 757 return SECFailure;
753 } 758 }
754 759
755 /* The extension data from the server has the following format: 760 /* The extension data from the server has the following format:
756 * uint16 name_list_len; 761 * uint16 name_list_len;
757 * uint8 len; 762 * uint8 len;
758 * uint8 protocol_name[len]; */ 763 * uint8 protocol_name[len]; */
759 if (data->len < 4 || data->len > 2 + 1 + 255) { 764 if (data->len < 4 || data->len > 2 + 1 + 255) {
760 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID); 765 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
761 » return SECFailure; 766 return SECFailure;
762 } 767 }
763 768
764 name_list_len = ((PRUint16) d[0]) << 8 | 769 name_list_len = ((PRUint16) d[0]) << 8 |
765 » ((PRUint16) d[1]); 770 ((PRUint16) d[1]);
766 if (name_list_len != data->len - 2 || d[2] != data->len - 3) { 771 if (name_list_len != data->len - 2 || d[2] != data->len - 3) {
767 » PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID); 772 PORT_SetError(SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID);
768 » return SECFailure; 773 return SECFailure;
769 } 774 }
770 775
771 protocol_name.data = data->data + 3; 776 protocol_name.data = data->data + 3;
772 protocol_name.len = data->len - 3; 777 protocol_name.len = data->len - 3;
773 778
774 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE); 779 SECITEM_FreeItem(&ss->ssl3.nextProto, PR_FALSE);
775 ss->ssl3.nextProtoState = SSL_NEXT_PROTO_SELECTED; 780 ss->ssl3.nextProtoState = SSL_NEXT_PROTO_SELECTED;
776 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 781 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
777 return SECITEM_CopyItem(NULL, &ss->ssl3.nextProto, &protocol_name); 782 return SECITEM_CopyItem(NULL, &ss->ssl3.nextProto, &protocol_name);
778 } 783 }
779 784
780 static PRInt32 785 static PRInt32
781 ssl3_ClientSendNextProtoNegoXtn(sslSocket * ss, PRBool append, 786 ssl3_ClientSendNextProtoNegoXtn(sslSocket * ss, PRBool append,
782 » » » » PRUint32 maxBytes) 787 PRUint32 maxBytes)
783 { 788 {
784 PRInt32 extension_length; 789 PRInt32 extension_length;
785 790
786 /* Renegotiations do not send this extension. */ 791 /* Renegotiations do not send this extension. */
787 if (!ss->opt.enableNPN || !ss->nextProtoCallback || ss->firstHsDone) { 792 if (!ss->opt.enableNPN || !ss->nextProtoCallback || ss->firstHsDone) {
788 » return 0; 793 return 0;
789 } 794 }
790 795
791 extension_length = 4; 796 extension_length = 4;
792 797
793 if (append && maxBytes >= extension_length) { 798 if (append && maxBytes >= extension_length) {
794 » SECStatus rv; 799 SECStatus rv;
795 » rv = ssl3_AppendHandshakeNumber(ss, ssl_next_proto_nego_xtn, 2); 800 rv = ssl3_AppendHandshakeNumber(ss, ssl_next_proto_nego_xtn, 2);
796 » if (rv != SECSuccess) 801 if (rv != SECSuccess)
797 » goto loser; 802 goto loser;
798 » rv = ssl3_AppendHandshakeNumber(ss, 0, 2); 803 rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
799 » if (rv != SECSuccess) 804 if (rv != SECSuccess)
800 » goto loser; 805 goto loser;
801 » ss->xtnData.advertised[ss->xtnData.numAdvertised++] = 806 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
802 » » ssl_next_proto_nego_xtn; 807 ssl_next_proto_nego_xtn;
803 } else if (maxBytes < extension_length) { 808 } else if (maxBytes < extension_length) {
804 » return 0; 809 return 0;
805 } 810 }
806 811
807 return extension_length; 812 return extension_length;
808 813
809 loser: 814 loser:
810 return -1; 815 return -1;
811 } 816 }
812 817
813 static PRInt32 818 static PRInt32
814 ssl3_ClientSendAppProtoXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes) 819 ssl3_ClientSendAppProtoXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes)
815 { 820 {
816 PRInt32 extension_length; 821 PRInt32 extension_length;
817 unsigned char *alpn_protos = NULL; 822 unsigned char *alpn_protos = NULL;
818 823
819 /* Renegotiations do not send this extension. */ 824 /* Renegotiations do not send this extension. */
820 if (!ss->opt.enableALPN || !ss->opt.nextProtoNego.data || ss->firstHsDone) { 825 if (!ss->opt.enableALPN || !ss->opt.nextProtoNego.data || ss->firstHsDone) {
821 » return 0; 826 return 0;
822 } 827 }
823 828
824 extension_length = 2 /* extension type */ + 2 /* extension length */ + 829 extension_length = 2 /* extension type */ + 2 /* extension length */ +
825 » » 2 /* protocol name list length */ + 830 2 /* protocol name list length */ +
826 » » ss->opt.nextProtoNego.len; 831 ss->opt.nextProtoNego.len;
827 832
828 if (append && maxBytes >= extension_length) { 833 if (append && maxBytes >= extension_length) {
829 » /* NPN requires that the client's fallback protocol is first in the 834 /* NPN requires that the client's fallback protocol is first in the
830 » * list. However, ALPN sends protocols in preference order. So we 835 * list. However, ALPN sends protocols in preference order. So we
831 » * allocate a buffer and move the first protocol to the end of the 836 * allocate a buffer and move the first protocol to the end of the
832 » * list. */ 837 * list. */
833 » SECStatus rv; 838 SECStatus rv;
834 » const unsigned int len = ss->opt.nextProtoNego.len; 839 const unsigned int len = ss->opt.nextProtoNego.len;
835 840
836 » alpn_protos = PORT_Alloc(len); 841 alpn_protos = PORT_Alloc(len);
837 » if (alpn_protos == NULL) { 842 if (alpn_protos == NULL) {
838 » return SECFailure; 843 return SECFailure;
839 » } 844 }
840 » if (len > 0) { 845 if (len > 0) {
841 » /* Each protocol string is prefixed with a single byte length. */ 846 /* Each protocol string is prefixed with a single byte length. */
842 » unsigned int i = ss->opt.nextProtoNego.data[0] + 1; 847 unsigned int i = ss->opt.nextProtoNego.data[0] + 1;
843 » if (i <= len) { 848 if (i <= len) {
844 » » memcpy(alpn_protos, &ss->opt.nextProtoNego.data[i], len - i); 849 memcpy(alpn_protos, &ss->opt.nextProtoNego.data[i], len - i);
845 » » memcpy(alpn_protos + len - i, ss->opt.nextProtoNego.data, i); 850 memcpy(alpn_protos + len - i, ss->opt.nextProtoNego.data, i);
846 » } else { 851 } else {
847 » » /* This seems to be invalid data so we'll send as-is. */ 852 /* This seems to be invalid data so we'll send as-is. */
848 » » memcpy(alpn_protos, ss->opt.nextProtoNego.data, len); 853 memcpy(alpn_protos, ss->opt.nextProtoNego.data, len);
849 » } 854 }
850 » } 855 }
851 856
852 » rv = ssl3_AppendHandshakeNumber(ss, ssl_app_layer_protocol_xtn, 2); 857 rv = ssl3_AppendHandshakeNumber(ss, ssl_app_layer_protocol_xtn, 2);
853 » if (rv != SECSuccess) { 858 if (rv != SECSuccess) {
854 » goto loser; 859 goto loser;
855 » } 860 }
856 » rv = ssl3_AppendHandshakeNumber(ss, extension_length - 4, 2); 861 rv = ssl3_AppendHandshakeNumber(ss, extension_length - 4, 2);
857 » if (rv != SECSuccess) { 862 if (rv != SECSuccess) {
858 » goto loser; 863 goto loser;
859 » } 864 }
860 » rv = ssl3_AppendHandshakeVariable(ss, alpn_protos, len, 2); 865 rv = ssl3_AppendHandshakeVariable(ss, alpn_protos, len, 2);
861 » PORT_Free(alpn_protos); 866 PORT_Free(alpn_protos);
862 » alpn_protos = NULL; 867 alpn_protos = NULL;
863 » if (rv != SECSuccess) { 868 if (rv != SECSuccess) {
864 » goto loser; 869 goto loser;
865 » } 870 }
866 » ss->xtnData.advertised[ss->xtnData.numAdvertised++] = 871 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
867 » » ssl_app_layer_protocol_xtn; 872 ssl_app_layer_protocol_xtn;
868 } else if (maxBytes < extension_length) { 873 } else if (maxBytes < extension_length) {
869 » return 0; 874 return 0;
870 } 875 }
871 876
872 return extension_length; 877 return extension_length;
873 878
874 loser: 879 loser:
875 if (alpn_protos) { 880 if (alpn_protos) {
876 » PORT_Free(alpn_protos); 881 PORT_Free(alpn_protos);
877 } 882 }
878 return -1; 883 return -1;
879 } 884 }
880 885
881 static PRInt32 886 static PRInt32
882 ssl3_ServerSendAppProtoXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes) 887 ssl3_ServerSendAppProtoXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes)
883 { 888 {
884 PRInt32 extension_length; 889 PRInt32 extension_length;
885 890
891 /* we're in over our heads if any of these fail */
886 PORT_Assert(ss->opt.enableALPN); 892 PORT_Assert(ss->opt.enableALPN);
887 PORT_Assert(ss->ssl3.nextProto.data); 893 PORT_Assert(ss->ssl3.nextProto.data);
888 PORT_Assert(ss->ssl3.nextProto.len > 0); 894 PORT_Assert(ss->ssl3.nextProto.len > 0);
889 PORT_Assert(ss->ssl3.nextProtoState == SSL_NEXT_PROTO_NEGOTIATED); 895 PORT_Assert(ss->ssl3.nextProtoState == SSL_NEXT_PROTO_NEGOTIATED);
890 PORT_Assert(!ss->firstHsDone); 896 PORT_Assert(!ss->firstHsDone);
891 897
892 extension_length = 2 /* extension type */ + 2 /* extension length */ + 898 extension_length = 2 /* extension type */ + 2 /* extension length */ +
893 2 /* protocol name list */ + 1 /* name length */ + 899 2 /* protocol name list */ + 1 /* name length */ +
894 ss->ssl3.nextProto.len; 900 ss->ssl3.nextProto.len;
895 901
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 978
973 loser: 979 loser:
974 return -1; 980 return -1;
975 } 981 }
976 982
977 static SECStatus 983 static SECStatus
978 ssl3_ClientHandleStatusRequestXtn(sslSocket *ss, PRUint16 ex_type, 984 ssl3_ClientHandleStatusRequestXtn(sslSocket *ss, PRUint16 ex_type,
979 SECItem *data) 985 SECItem *data)
980 { 986 {
981 /* The echoed extension must be empty. */ 987 /* The echoed extension must be empty. */
982 if (data->len != 0) 988 if (data->len != 0) {
983 return SECFailure; 989 return SECSuccess; /* Ignore the extension. */
990 }
984 991
985 /* Keep track of negotiated extensions. */ 992 /* Keep track of negotiated extensions. */
986 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 993 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
987 994
988 return SECSuccess; 995 return SECSuccess;
989 } 996 }
990 997
991 static PRInt32 998 static PRInt32
992 ssl3_ServerSendStatusRequestXtn( 999 ssl3_ServerSendStatusRequestXtn(
993 » » » sslSocket * ss, 1000 sslSocket * ss,
994 » » » PRBool append, 1001 PRBool append,
995 » » » PRUint32 maxBytes) 1002 PRUint32 maxBytes)
996 { 1003 {
997 PRInt32 extension_length; 1004 PRInt32 extension_length;
998 SECStatus rv; 1005 SECStatus rv;
999 int i; 1006 int i;
1000 PRBool haveStatus = PR_FALSE; 1007 PRBool haveStatus = PR_FALSE;
1001 1008
1002 for (i = kt_null; i < kt_kea_size; i++) { 1009 for (i = kt_null; i < kt_kea_size; i++) {
1003 » /* TODO: This is a temporary workaround. 1010 /* TODO: This is a temporary workaround.
1004 » * The correct code needs to see if we have an OCSP response for 1011 * The correct code needs to see if we have an OCSP response for
1005 » * the server certificate being used, rather than if we have any 1012 * the server certificate being used, rather than if we have any
1006 » * OCSP response. See also ssl3_SendCertificateStatus. 1013 * OCSP response. See also ssl3_SendCertificateStatus.
1007 » */ 1014 */
1008 » if (ss->certStatusArray[i] && ss->certStatusArray[i]->len) { 1015 if (ss->certStatusArray[i] && ss->certStatusArray[i]->len) {
1009 » haveStatus = PR_TRUE; 1016 haveStatus = PR_TRUE;
1010 » break; 1017 break;
1011 » } 1018 }
1012 } 1019 }
1013 if (!haveStatus) 1020 if (!haveStatus)
1014 » return 0; 1021 return 0;
1015 1022
1016 extension_length = 2 + 2; 1023 extension_length = 2 + 2;
1017 if (append && maxBytes >= extension_length) { 1024 if (append && maxBytes >= extension_length) {
1018 » /* extension_type */ 1025 /* extension_type */
1019 » rv = ssl3_AppendHandshakeNumber(ss, ssl_cert_status_xtn, 2); 1026 rv = ssl3_AppendHandshakeNumber(ss, ssl_cert_status_xtn, 2);
1020 » if (rv != SECSuccess) 1027 if (rv != SECSuccess)
1021 » return -1; 1028 return -1;
1022 » /* length of extension_data */ 1029 /* length of extension_data */
1023 » rv = ssl3_AppendHandshakeNumber(ss, 0, 2); 1030 rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
1024 » if (rv != SECSuccess) 1031 if (rv != SECSuccess)
1025 » return -1; 1032 return -1;
1026 } 1033 }
1027 1034
1028 return extension_length; 1035 return extension_length;
1029 } 1036 }
1030 1037
1031 /* ssl3_ClientSendStatusRequestXtn builds the status_request extension on the 1038 /* ssl3_ClientSendStatusRequestXtn builds the status_request extension on the
1032 * client side. See RFC 4366 section 3.6. */ 1039 * client side. See RFC 4366 section 3.6. */
1033 static PRInt32 1040 static PRInt32
1034 ssl3_ClientSendStatusRequestXtn(sslSocket * ss, PRBool append, 1041 ssl3_ClientSendStatusRequestXtn(sslSocket * ss, PRBool append,
1035 PRUint32 maxBytes) 1042 PRUint32 maxBytes)
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1125 unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH]; 1132 unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH];
1126 unsigned int computed_mac_length; 1133 unsigned int computed_mac_length;
1127 unsigned char iv[AES_BLOCK_SIZE]; 1134 unsigned char iv[AES_BLOCK_SIZE];
1128 SECItem ivItem; 1135 SECItem ivItem;
1129 SECItem *srvName = NULL; 1136 SECItem *srvName = NULL;
1130 PRUint32 srvNameLen = 0; 1137 PRUint32 srvNameLen = 0;
1131 CK_MECHANISM_TYPE msWrapMech = 0; /* dummy default value, 1138 CK_MECHANISM_TYPE msWrapMech = 0; /* dummy default value,
1132 * must be >= 0 */ 1139 * must be >= 0 */
1133 1140
1134 SSL_TRC(3, ("%d: SSL3[%d]: send session_ticket handshake", 1141 SSL_TRC(3, ("%d: SSL3[%d]: send session_ticket handshake",
1135 » » SSL_GETPID(), ss->fd)); 1142 SSL_GETPID(), ss->fd));
1136 1143
1137 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); 1144 PORT_Assert( ss->opt.noLocks || ssl_HaveXmitBufLock(ss));
1138 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); 1145 PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss));
1139 1146
1140 ticket.ticket_lifetime_hint = TLS_EX_SESS_TICKET_LIFETIME_HINT; 1147 ticket.ticket_lifetime_hint = TLS_EX_SESS_TICKET_LIFETIME_HINT;
1141 cert_length = (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) ? 1148 cert_length = (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) ?
1142 » 3 + ss->sec.ci.sid->peerCert->derCert.len : 0; 1149 3 + ss->sec.ci.sid->peerCert->derCert.len : 0;
1143 1150
1144 /* Get IV and encryption keys */ 1151 /* Get IV and encryption keys */
1145 ivItem.data = iv; 1152 ivItem.data = iv;
1146 ivItem.len = sizeof(iv); 1153 ivItem.len = sizeof(iv);
1147 rv = PK11_GenerateRandom(iv, sizeof(iv)); 1154 rv = PK11_GenerateRandom(iv, sizeof(iv));
1148 if (rv != SECSuccess) goto loser; 1155 if (rv != SECSuccess) goto loser;
1149 1156
1150 #ifndef NO_PKCS11_BYPASS 1157 #ifndef NO_PKCS11_BYPASS
1151 if (ss->opt.bypassPKCS11) { 1158 if (ss->opt.bypassPKCS11) {
1152 » rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length, 1159 rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length,
1153 » &mac_key, &mac_key_length); 1160 &mac_key, &mac_key_length);
1154 } else 1161 } else
1155 #endif 1162 #endif
1156 { 1163 {
1157 » rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11, 1164 rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11,
1158 » &mac_key_pkcs11); 1165 &mac_key_pkcs11);
1159 } 1166 }
1160 if (rv != SECSuccess) goto loser; 1167 if (rv != SECSuccess) goto loser;
1161 1168
1162 if (ss->ssl3.pwSpec->msItem.len && ss->ssl3.pwSpec->msItem.data) { 1169 if (ss->ssl3.pwSpec->msItem.len && ss->ssl3.pwSpec->msItem.data) {
1163 » /* The master secret is available unwrapped. */ 1170 /* The master secret is available unwrapped. */
1164 » ms_item.data = ss->ssl3.pwSpec->msItem.data; 1171 ms_item.data = ss->ssl3.pwSpec->msItem.data;
1165 » ms_item.len = ss->ssl3.pwSpec->msItem.len; 1172 ms_item.len = ss->ssl3.pwSpec->msItem.len;
1166 » ms_is_wrapped = PR_FALSE; 1173 ms_is_wrapped = PR_FALSE;
1167 } else { 1174 } else {
1168 » /* Extract the master secret wrapped. */ 1175 /* Extract the master secret wrapped. */
1169 » sslSessionID sid; 1176 sslSessionID sid;
1170 » PORT_Memset(&sid, 0, sizeof(sslSessionID)); 1177 PORT_Memset(&sid, 0, sizeof(sslSessionID));
1171 1178
1172 » if (ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa) { 1179 if (ss->ssl3.hs.kea_def->kea == kea_ecdhe_rsa) {
1173 » effectiveExchKeyType = kt_rsa; 1180 effectiveExchKeyType = kt_rsa;
1174 » } else { 1181 } else {
1175 » effectiveExchKeyType = ss->ssl3.hs.kea_def->exchKeyType; 1182 effectiveExchKeyType = ss->ssl3.hs.kea_def->exchKeyType;
1176 » } 1183 }
1177 1184
1178 » rv = ssl3_CacheWrappedMasterSecret(ss, &sid, ss->ssl3.pwSpec, 1185 rv = ssl3_CacheWrappedMasterSecret(ss, &sid, ss->ssl3.pwSpec,
1179 » effectiveExchKeyType); 1186 effectiveExchKeyType);
1180 » if (rv == SECSuccess) { 1187 if (rv == SECSuccess) {
1181 » if (sid.u.ssl3.keys.wrapped_master_secret_len > sizeof(wrapped_ms)) 1188 if (sid.u.ssl3.keys.wrapped_master_secret_len > sizeof(wrapped_ms))
1182 » » goto loser; 1189 goto loser;
1183 » memcpy(wrapped_ms, sid.u.ssl3.keys.wrapped_master_secret, 1190 memcpy(wrapped_ms, sid.u.ssl3.keys.wrapped_master_secret,
1184 » » sid.u.ssl3.keys.wrapped_master_secret_len); 1191 sid.u.ssl3.keys.wrapped_master_secret_len);
1185 » ms_item.data = wrapped_ms; 1192 ms_item.data = wrapped_ms;
1186 » ms_item.len = sid.u.ssl3.keys.wrapped_master_secret_len; 1193 ms_item.len = sid.u.ssl3.keys.wrapped_master_secret_len;
1187 » msWrapMech = sid.u.ssl3.masterWrapMech; 1194 msWrapMech = sid.u.ssl3.masterWrapMech;
1188 » } else { 1195 } else {
1189 » /* TODO: else send an empty ticket. */ 1196 /* TODO: else send an empty ticket. */
1190 » goto loser; 1197 goto loser;
1191 » } 1198 }
1192 » ms_is_wrapped = PR_TRUE; 1199 ms_is_wrapped = PR_TRUE;
1193 } 1200 }
1194 /* Prep to send negotiated name */ 1201 /* Prep to send negotiated name */
1195 srvName = &ss->ssl3.pwSpec->srvVirtName; 1202 srvName = &ss->ssl3.pwSpec->srvVirtName;
1196 if (srvName->data && srvName->len) { 1203 if (srvName->data && srvName->len) {
1197 srvNameLen = 2 + srvName->len; /* len bytes + name len */ 1204 srvNameLen = 2 + srvName->len; /* len bytes + name len */
1198 } 1205 }
1199 1206
1200 ciphertext_length = 1207 ciphertext_length =
1201 » sizeof(PRUint16) /* ticket_version */ 1208 sizeof(PRUint16) /* ticket_version */
1202 » + sizeof(SSL3ProtocolVersion) /* ssl_version */ 1209 + sizeof(SSL3ProtocolVersion) /* ssl_version */
1203 » + sizeof(ssl3CipherSuite) /* ciphersuite */ 1210 + sizeof(ssl3CipherSuite) /* ciphersuite */
1204 » + 1 /* compression */ 1211 + 1 /* compression */
1205 » + 10 /* cipher spec parameters */ 1212 + 10 /* cipher spec parameters */
1206 » + 1 /* SessionTicket.ms_is_wrapped */ 1213 + 1 /* SessionTicket.ms_is_wrapped */
1207 » + 1 /* effectiveExchKeyType */ 1214 + 1 /* effectiveExchKeyType */
1208 » + 4 /* msWrapMech */ 1215 + 4 /* msWrapMech */
1209 » + 2 /* master_secret.length */ 1216 + 2 /* master_secret.length */
1210 » + ms_item.len /* master_secret */ 1217 + ms_item.len /* master_secret */
1211 » + 1 /* client_auth_type */ 1218 + 1 /* client_auth_type */
1212 » + cert_length /* cert */ 1219 + cert_length /* cert */
1213 + 1 /* server name type */ 1220 + 1 /* server name type */
1214 + srvNameLen /* name len + length field */ 1221 + srvNameLen /* name len + length field */
1215 » + sizeof(ticket.ticket_lifetime_hint); 1222 + sizeof(ticket.ticket_lifetime_hint);
1216 padding_length = AES_BLOCK_SIZE - 1223 padding_length = AES_BLOCK_SIZE -
1217 » (ciphertext_length % AES_BLOCK_SIZE); 1224 (ciphertext_length % AES_BLOCK_SIZE);
1218 ciphertext_length += padding_length; 1225 ciphertext_length += padding_length;
1219 1226
1220 message_length = 1227 message_length =
1221 » sizeof(ticket.ticket_lifetime_hint) /* ticket_lifetime_hint */ 1228 sizeof(ticket.ticket_lifetime_hint) /* ticket_lifetime_hint */
1222 » + 2 /* length field for NewSessionTicket.ticket */ 1229 + 2 /* length field for NewSessionTicket.ticket */
1223 » + SESS_TICKET_KEY_NAME_LEN /* key_name */ 1230 + SESS_TICKET_KEY_NAME_LEN /* key_name */
1224 » + AES_BLOCK_SIZE /* iv */ 1231 + AES_BLOCK_SIZE /* iv */
1225 » + 2 /* length field for NewSessionTicket.ticket.encrypted_state */ 1232 + 2 /* length field for NewSessionTicket.ticket.encrypted_state */
1226 » + ciphertext_length /* encrypted_state */ 1233 + ciphertext_length /* encrypted_state */
1227 » + TLS_EX_SESS_TICKET_MAC_LENGTH; /* mac */ 1234 + TLS_EX_SESS_TICKET_MAC_LENGTH; /* mac */
1228 1235
1229 if (SECITEM_AllocItem(NULL, &plaintext_item, ciphertext_length) == NULL) 1236 if (SECITEM_AllocItem(NULL, &plaintext_item, ciphertext_length) == NULL)
1230 » goto loser; 1237 goto loser;
1231 1238
1232 plaintext = plaintext_item; 1239 plaintext = plaintext_item;
1233 1240
1234 /* ticket_version */ 1241 /* ticket_version */
1235 rv = ssl3_AppendNumberToItem(&plaintext, TLS_EX_SESS_TICKET_VERSION, 1242 rv = ssl3_AppendNumberToItem(&plaintext, TLS_EX_SESS_TICKET_VERSION,
1236 » sizeof(PRUint16)); 1243 sizeof(PRUint16));
1237 if (rv != SECSuccess) goto loser; 1244 if (rv != SECSuccess) goto loser;
1238 1245
1239 /* ssl_version */ 1246 /* ssl_version */
1240 rv = ssl3_AppendNumberToItem(&plaintext, ss->version, 1247 rv = ssl3_AppendNumberToItem(&plaintext, ss->version,
1241 » sizeof(SSL3ProtocolVersion)); 1248 sizeof(SSL3ProtocolVersion));
1242 if (rv != SECSuccess) goto loser; 1249 if (rv != SECSuccess) goto loser;
1243 1250
1244 /* ciphersuite */ 1251 /* ciphersuite */
1245 rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.cipher_suite, 1252 rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.cipher_suite,
1246 » sizeof(ssl3CipherSuite)); 1253 sizeof(ssl3CipherSuite));
1247 if (rv != SECSuccess) goto loser; 1254 if (rv != SECSuccess) goto loser;
1248 1255
1249 /* compression */ 1256 /* compression */
1250 rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.compression, 1); 1257 rv = ssl3_AppendNumberToItem(&plaintext, ss->ssl3.hs.compression, 1);
1251 if (rv != SECSuccess) goto loser; 1258 if (rv != SECSuccess) goto loser;
1252 1259
1253 /* cipher spec parameters */ 1260 /* cipher spec parameters */
1254 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authAlgorithm, 1); 1261 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authAlgorithm, 1);
1255 if (rv != SECSuccess) goto loser; 1262 if (rv != SECSuccess) goto loser;
1256 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authKeyBits, 4); 1263 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.authKeyBits, 4);
1257 if (rv != SECSuccess) goto loser; 1264 if (rv != SECSuccess) goto loser;
1258 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaType, 1); 1265 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaType, 1);
1259 if (rv != SECSuccess) goto loser; 1266 if (rv != SECSuccess) goto loser;
1260 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaKeyBits, 4); 1267 rv = ssl3_AppendNumberToItem(&plaintext, ss->sec.keaKeyBits, 4);
1261 if (rv != SECSuccess) goto loser; 1268 if (rv != SECSuccess) goto loser;
1262 1269
1263 /* master_secret */ 1270 /* master_secret */
1264 rv = ssl3_AppendNumberToItem(&plaintext, ms_is_wrapped, 1); 1271 rv = ssl3_AppendNumberToItem(&plaintext, ms_is_wrapped, 1);
1265 if (rv != SECSuccess) goto loser; 1272 if (rv != SECSuccess) goto loser;
1266 rv = ssl3_AppendNumberToItem(&plaintext, effectiveExchKeyType, 1); 1273 rv = ssl3_AppendNumberToItem(&plaintext, effectiveExchKeyType, 1);
1267 if (rv != SECSuccess) goto loser; 1274 if (rv != SECSuccess) goto loser;
1268 rv = ssl3_AppendNumberToItem(&plaintext, msWrapMech, 4); 1275 rv = ssl3_AppendNumberToItem(&plaintext, msWrapMech, 4);
1269 if (rv != SECSuccess) goto loser; 1276 if (rv != SECSuccess) goto loser;
1270 rv = ssl3_AppendNumberToItem(&plaintext, ms_item.len, 2); 1277 rv = ssl3_AppendNumberToItem(&plaintext, ms_item.len, 2);
1271 if (rv != SECSuccess) goto loser; 1278 if (rv != SECSuccess) goto loser;
1272 rv = ssl3_AppendToItem(&plaintext, ms_item.data, ms_item.len); 1279 rv = ssl3_AppendToItem(&plaintext, ms_item.data, ms_item.len);
1273 if (rv != SECSuccess) goto loser; 1280 if (rv != SECSuccess) goto loser;
1274 1281
1275 /* client_identity */ 1282 /* client_identity */
1276 if (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) { 1283 if (ss->opt.requestCertificate && ss->sec.ci.sid->peerCert) {
1277 » rv = ssl3_AppendNumberToItem(&plaintext, CLIENT_AUTH_CERTIFICATE, 1); 1284 rv = ssl3_AppendNumberToItem(&plaintext, CLIENT_AUTH_CERTIFICATE, 1);
1278 » if (rv != SECSuccess) goto loser; 1285 if (rv != SECSuccess) goto loser;
1279 » rv = ssl3_AppendNumberToItem(&plaintext, 1286 rv = ssl3_AppendNumberToItem(&plaintext,
1280 » ss->sec.ci.sid->peerCert->derCert.len, 3); 1287 ss->sec.ci.sid->peerCert->derCert.len, 3);
1281 » if (rv != SECSuccess) goto loser; 1288 if (rv != SECSuccess) goto loser;
1282 » rv = ssl3_AppendToItem(&plaintext, 1289 rv = ssl3_AppendToItem(&plaintext,
1283 » ss->sec.ci.sid->peerCert->derCert.data, 1290 ss->sec.ci.sid->peerCert->derCert.data,
1284 » ss->sec.ci.sid->peerCert->derCert.len); 1291 ss->sec.ci.sid->peerCert->derCert.len);
1285 » if (rv != SECSuccess) goto loser; 1292 if (rv != SECSuccess) goto loser;
1286 } else { 1293 } else {
1287 » rv = ssl3_AppendNumberToItem(&plaintext, 0, 1); 1294 rv = ssl3_AppendNumberToItem(&plaintext, 0, 1);
1288 » if (rv != SECSuccess) goto loser; 1295 if (rv != SECSuccess) goto loser;
1289 } 1296 }
1290 1297
1291 /* timestamp */ 1298 /* timestamp */
1292 now = ssl_Time(); 1299 now = ssl_Time();
1293 rv = ssl3_AppendNumberToItem(&plaintext, now, 1300 rv = ssl3_AppendNumberToItem(&plaintext, now,
1294 » sizeof(ticket.ticket_lifetime_hint)); 1301 sizeof(ticket.ticket_lifetime_hint));
1295 if (rv != SECSuccess) goto loser; 1302 if (rv != SECSuccess) goto loser;
1296 1303
1297 if (srvNameLen) { 1304 if (srvNameLen) {
1298 /* Name Type (sni_host_name) */ 1305 /* Name Type (sni_host_name) */
1299 rv = ssl3_AppendNumberToItem(&plaintext, srvName->type, 1); 1306 rv = ssl3_AppendNumberToItem(&plaintext, srvName->type, 1);
1300 if (rv != SECSuccess) goto loser; 1307 if (rv != SECSuccess) goto loser;
1301 /* HostName (length and value) */ 1308 /* HostName (length and value) */
1302 rv = ssl3_AppendNumberToItem(&plaintext, srvName->len, 2); 1309 rv = ssl3_AppendNumberToItem(&plaintext, srvName->len, 2);
1303 if (rv != SECSuccess) goto loser; 1310 if (rv != SECSuccess) goto loser;
1304 rv = ssl3_AppendToItem(&plaintext, srvName->data, srvName->len); 1311 rv = ssl3_AppendToItem(&plaintext, srvName->data, srvName->len);
1305 if (rv != SECSuccess) goto loser; 1312 if (rv != SECSuccess) goto loser;
1306 } else { 1313 } else {
1307 /* No Name */ 1314 /* No Name */
1308 rv = ssl3_AppendNumberToItem(&plaintext, (char)TLS_STE_NO_SERVER_NAME, 1315 rv = ssl3_AppendNumberToItem(&plaintext, (char)TLS_STE_NO_SERVER_NAME,
1309 1); 1316 1);
1310 if (rv != SECSuccess) goto loser; 1317 if (rv != SECSuccess) goto loser;
1311 } 1318 }
1312 1319
1313 PORT_Assert(plaintext.len == padding_length); 1320 PORT_Assert(plaintext.len == padding_length);
1314 for (i = 0; i < padding_length; i++) 1321 for (i = 0; i < padding_length; i++)
1315 » plaintext.data[i] = (unsigned char)padding_length; 1322 plaintext.data[i] = (unsigned char)padding_length;
1316 1323
1317 if (SECITEM_AllocItem(NULL, &ciphertext, ciphertext_length) == NULL) { 1324 if (SECITEM_AllocItem(NULL, &ciphertext, ciphertext_length) == NULL) {
1318 » rv = SECFailure; 1325 rv = SECFailure;
1319 » goto loser; 1326 goto loser;
1320 } 1327 }
1321 1328
1322 /* Generate encrypted portion of ticket. */ 1329 /* Generate encrypted portion of ticket. */
1323 #ifndef NO_PKCS11_BYPASS 1330 #ifndef NO_PKCS11_BYPASS
1324 if (ss->opt.bypassPKCS11) { 1331 if (ss->opt.bypassPKCS11) {
1325 » aes_ctx = (AESContext *)aes_ctx_buf; 1332 aes_ctx = (AESContext *)aes_ctx_buf;
1326 » rv = AES_InitContext(aes_ctx, aes_key, aes_key_length, iv, 1333 rv = AES_InitContext(aes_ctx, aes_key, aes_key_length, iv,
1327 » NSS_AES_CBC, 1, AES_BLOCK_SIZE); 1334 NSS_AES_CBC, 1, AES_BLOCK_SIZE);
1328 » if (rv != SECSuccess) goto loser; 1335 if (rv != SECSuccess) goto loser;
1329 1336
1330 » rv = AES_Encrypt(aes_ctx, ciphertext.data, &ciphertext.len, 1337 rv = AES_Encrypt(aes_ctx, ciphertext.data, &ciphertext.len,
1331 » ciphertext.len, plaintext_item.data, 1338 ciphertext.len, plaintext_item.data,
1332 » plaintext_item.len); 1339 plaintext_item.len);
1333 » if (rv != SECSuccess) goto loser; 1340 if (rv != SECSuccess) goto loser;
1334 } else 1341 } else
1335 #endif 1342 #endif
1336 { 1343 {
1337 » aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech, 1344 aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech,
1338 » CKA_ENCRYPT, aes_key_pkcs11, &ivItem); 1345 CKA_ENCRYPT, aes_key_pkcs11, &ivItem);
1339 » if (!aes_ctx_pkcs11) 1346 if (!aes_ctx_pkcs11)
1340 » goto loser; 1347 goto loser;
1341 1348
1342 » rv = PK11_CipherOp(aes_ctx_pkcs11, ciphertext.data, 1349 rv = PK11_CipherOp(aes_ctx_pkcs11, ciphertext.data,
1343 » (int *)&ciphertext.len, ciphertext.len, 1350 (int *)&ciphertext.len, ciphertext.len,
1344 » plaintext_item.data, plaintext_item.len); 1351 plaintext_item.data, plaintext_item.len);
1345 » PK11_Finalize(aes_ctx_pkcs11); 1352 PK11_Finalize(aes_ctx_pkcs11);
1346 » PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE); 1353 PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE);
1347 » if (rv != SECSuccess) goto loser; 1354 if (rv != SECSuccess) goto loser;
1348 } 1355 }
1349 1356
1350 /* Convert ciphertext length to network order. */ 1357 /* Convert ciphertext length to network order. */
1351 length_buf[0] = (ciphertext.len >> 8) & 0xff; 1358 length_buf[0] = (ciphertext.len >> 8) & 0xff;
1352 length_buf[1] = (ciphertext.len ) & 0xff; 1359 length_buf[1] = (ciphertext.len ) & 0xff;
1353 1360
1354 /* Compute MAC. */ 1361 /* Compute MAC. */
1355 #ifndef NO_PKCS11_BYPASS 1362 #ifndef NO_PKCS11_BYPASS
1356 if (ss->opt.bypassPKCS11) { 1363 if (ss->opt.bypassPKCS11) {
1357 » hmac_ctx = (HMACContext *)hmac_ctx_buf; 1364 hmac_ctx = (HMACContext *)hmac_ctx_buf;
1358 » hashObj = HASH_GetRawHashObject(HASH_AlgSHA256); 1365 hashObj = HASH_GetRawHashObject(HASH_AlgSHA256);
1359 » if (HMAC_Init(hmac_ctx, hashObj, mac_key, 1366 if (HMAC_Init(hmac_ctx, hashObj, mac_key,
1360 » » mac_key_length, PR_FALSE) != SECSuccess) 1367 mac_key_length, PR_FALSE) != SECSuccess)
1361 » goto loser; 1368 goto loser;
1362 1369
1363 » HMAC_Begin(hmac_ctx); 1370 HMAC_Begin(hmac_ctx);
1364 » HMAC_Update(hmac_ctx, key_name, SESS_TICKET_KEY_NAME_LEN); 1371 HMAC_Update(hmac_ctx, key_name, SESS_TICKET_KEY_NAME_LEN);
1365 » HMAC_Update(hmac_ctx, iv, sizeof(iv)); 1372 HMAC_Update(hmac_ctx, iv, sizeof(iv));
1366 » HMAC_Update(hmac_ctx, (unsigned char *)length_buf, 2); 1373 HMAC_Update(hmac_ctx, (unsigned char *)length_buf, 2);
1367 » HMAC_Update(hmac_ctx, ciphertext.data, ciphertext.len); 1374 HMAC_Update(hmac_ctx, ciphertext.data, ciphertext.len);
1368 » HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length, 1375 HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length,
1369 » sizeof(computed_mac)); 1376 sizeof(computed_mac));
1370 } else 1377 } else
1371 #endif 1378 #endif
1372 { 1379 {
1373 » SECItem macParam; 1380 SECItem macParam;
1374 » macParam.data = NULL; 1381 macParam.data = NULL;
1375 » macParam.len = 0; 1382 macParam.len = 0;
1376 » hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech, 1383 hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech,
1377 » CKA_SIGN, mac_key_pkcs11, &macParam); 1384 CKA_SIGN, mac_key_pkcs11, &macParam);
1378 » if (!hmac_ctx_pkcs11) 1385 if (!hmac_ctx_pkcs11)
1379 » goto loser; 1386 goto loser;
1380 1387
1381 » rv = PK11_DigestBegin(hmac_ctx_pkcs11); 1388 rv = PK11_DigestBegin(hmac_ctx_pkcs11);
1382 » rv = PK11_DigestOp(hmac_ctx_pkcs11, key_name, 1389 rv = PK11_DigestOp(hmac_ctx_pkcs11, key_name,
1383 » SESS_TICKET_KEY_NAME_LEN); 1390 SESS_TICKET_KEY_NAME_LEN);
1384 » rv = PK11_DigestOp(hmac_ctx_pkcs11, iv, sizeof(iv)); 1391 rv = PK11_DigestOp(hmac_ctx_pkcs11, iv, sizeof(iv));
1385 » rv = PK11_DigestOp(hmac_ctx_pkcs11, (unsigned char *)length_buf, 2); 1392 rv = PK11_DigestOp(hmac_ctx_pkcs11, (unsigned char *)length_buf, 2);
1386 » rv = PK11_DigestOp(hmac_ctx_pkcs11, ciphertext.data, ciphertext.len); 1393 rv = PK11_DigestOp(hmac_ctx_pkcs11, ciphertext.data, ciphertext.len);
1387 » rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac, 1394 rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac,
1388 » &computed_mac_length, sizeof(computed_mac)); 1395 &computed_mac_length, sizeof(computed_mac));
1389 » PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE); 1396 PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
1390 » if (rv != SECSuccess) goto loser; 1397 if (rv != SECSuccess) goto loser;
1391 } 1398 }
1392 1399
1393 /* Serialize the handshake message. */ 1400 /* Serialize the handshake message. */
1394 rv = ssl3_AppendHandshakeHeader(ss, new_session_ticket, message_length); 1401 rv = ssl3_AppendHandshakeHeader(ss, new_session_ticket, message_length);
1395 if (rv != SECSuccess) goto loser; 1402 if (rv != SECSuccess) goto loser;
1396 1403
1397 rv = ssl3_AppendHandshakeNumber(ss, ticket.ticket_lifetime_hint, 1404 rv = ssl3_AppendHandshakeNumber(ss, ticket.ticket_lifetime_hint,
1398 » sizeof(ticket.ticket_lifetime_hint)); 1405 sizeof(ticket.ticket_lifetime_hint));
1399 if (rv != SECSuccess) goto loser; 1406 if (rv != SECSuccess) goto loser;
1400 1407
1401 rv = ssl3_AppendHandshakeNumber(ss, 1408 rv = ssl3_AppendHandshakeNumber(ss,
1402 » message_length - sizeof(ticket.ticket_lifetime_hint) - 2, 2); 1409 message_length - sizeof(ticket.ticket_lifetime_hint) - 2, 2);
1403 if (rv != SECSuccess) goto loser; 1410 if (rv != SECSuccess) goto loser;
1404 1411
1405 rv = ssl3_AppendHandshake(ss, key_name, SESS_TICKET_KEY_NAME_LEN); 1412 rv = ssl3_AppendHandshake(ss, key_name, SESS_TICKET_KEY_NAME_LEN);
1406 if (rv != SECSuccess) goto loser; 1413 if (rv != SECSuccess) goto loser;
1407 1414
1408 rv = ssl3_AppendHandshake(ss, iv, sizeof(iv)); 1415 rv = ssl3_AppendHandshake(ss, iv, sizeof(iv));
1409 if (rv != SECSuccess) goto loser; 1416 if (rv != SECSuccess) goto loser;
1410 1417
1411 rv = ssl3_AppendHandshakeVariable(ss, ciphertext.data, ciphertext.len, 2); 1418 rv = ssl3_AppendHandshakeVariable(ss, ciphertext.data, ciphertext.len, 2);
1412 if (rv != SECSuccess) goto loser; 1419 if (rv != SECSuccess) goto loser;
1413 1420
1414 rv = ssl3_AppendHandshake(ss, computed_mac, computed_mac_length); 1421 rv = ssl3_AppendHandshake(ss, computed_mac, computed_mac_length);
1415 if (rv != SECSuccess) goto loser; 1422 if (rv != SECSuccess) goto loser;
1416 1423
1417 loser: 1424 loser:
1418 if (plaintext_item.data) 1425 if (plaintext_item.data)
1419 » SECITEM_FreeItem(&plaintext_item, PR_FALSE); 1426 SECITEM_FreeItem(&plaintext_item, PR_FALSE);
1420 if (ciphertext.data) 1427 if (ciphertext.data)
1421 » SECITEM_FreeItem(&ciphertext, PR_FALSE); 1428 SECITEM_FreeItem(&ciphertext, PR_FALSE);
1422 1429
1423 return rv; 1430 return rv;
1424 } 1431 }
1425 1432
1426 /* When a client receives a SessionTicket extension a NewSessionTicket 1433 /* When a client receives a SessionTicket extension a NewSessionTicket
1427 * message is expected during the handshake. 1434 * message is expected during the handshake.
1428 */ 1435 */
1429 SECStatus 1436 SECStatus
1430 ssl3_ClientHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type, 1437 ssl3_ClientHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type,
1431 SECItem *data) 1438 SECItem *data)
1432 { 1439 {
1433 if (data->len != 0) 1440 if (data->len != 0) {
1434 » return SECFailure; 1441 return SECSuccess; /* Ignore the extension. */
1442 }
1435 1443
1436 /* Keep track of negotiated extensions. */ 1444 /* Keep track of negotiated extensions. */
1437 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 1445 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
1438 return SECSuccess; 1446 return SECSuccess;
1439 } 1447 }
1440 1448
1441 SECStatus 1449 SECStatus
1442 ssl3_ServerHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type, 1450 ssl3_ServerHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type,
1443 SECItem *data) 1451 SECItem *data)
1444 { 1452 {
1445 SECStatus rv; 1453 SECStatus rv;
1446 SECItem *decrypted_state = NULL; 1454 SECItem *decrypted_state = NULL;
1447 SessionTicket *parsed_session_ticket = NULL; 1455 SessionTicket *parsed_session_ticket = NULL;
1448 sslSessionID *sid = NULL; 1456 sslSessionID *sid = NULL;
1449 SSL3Statistics *ssl3stats; 1457 SSL3Statistics *ssl3stats;
1450 1458
1451 /* Ignore the SessionTicket extension if processing is disabled. */ 1459 /* Ignore the SessionTicket extension if processing is disabled. */
1452 if (!ss->opt.enableSessionTickets) 1460 if (!ss->opt.enableSessionTickets)
1453 » return SECSuccess; 1461 return SECSuccess;
1454 1462
1455 /* Keep track of negotiated extensions. */ 1463 /* Keep track of negotiated extensions. */
1456 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 1464 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
1457 1465
1458 /* Parse the received ticket sent in by the client. We are 1466 /* Parse the received ticket sent in by the client. We are
1459 * lenient about some parse errors, falling back to a fullshake 1467 * lenient about some parse errors, falling back to a fullshake
1460 * instead of terminating the current connection. 1468 * instead of terminating the current connection.
1461 */ 1469 */
1462 if (data->len == 0) { 1470 if (data->len == 0) {
1463 » ss->xtnData.emptySessionTicket = PR_TRUE; 1471 ss->xtnData.emptySessionTicket = PR_TRUE;
1464 } else { 1472 } else {
1465 » int i; 1473 int i;
1466 » SECItem extension_data; 1474 SECItem extension_data;
1467 » EncryptedSessionTicket enc_session_ticket; 1475 EncryptedSessionTicket enc_session_ticket;
1468 » unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH]; 1476 unsigned char computed_mac[TLS_EX_SESS_TICKET_MAC_LENGTH];
1469 » unsigned int computed_mac_length; 1477 unsigned int computed_mac_length;
1470 #ifndef NO_PKCS11_BYPASS 1478 #ifndef NO_PKCS11_BYPASS
1471 » const SECHashObject *hashObj; 1479 const SECHashObject *hashObj;
1472 » const unsigned char *aes_key; 1480 const unsigned char *aes_key;
1473 » const unsigned char *mac_key; 1481 const unsigned char *mac_key;
1474 » PRUint32 aes_key_length; 1482 PRUint32 aes_key_length;
1475 » PRUint32 mac_key_length; 1483 PRUint32 mac_key_length;
1476 » PRUint64 hmac_ctx_buf[MAX_MAC_CONTEXT_LLONGS]; 1484 PRUint64 hmac_ctx_buf[MAX_MAC_CONTEXT_LLONGS];
1477 » HMACContext *hmac_ctx; 1485 HMACContext *hmac_ctx;
1478 » PRUint64 aes_ctx_buf[MAX_CIPHER_CONTEXT_LLONGS]; 1486 PRUint64 aes_ctx_buf[MAX_CIPHER_CONTEXT_LLONGS];
1479 » AESContext *aes_ctx; 1487 AESContext *aes_ctx;
1480 #endif 1488 #endif
1481 » PK11SymKey *aes_key_pkcs11; 1489 PK11SymKey *aes_key_pkcs11;
1482 » PK11SymKey *mac_key_pkcs11; 1490 PK11SymKey *mac_key_pkcs11;
1483 » PK11Context *hmac_ctx_pkcs11; 1491 PK11Context *hmac_ctx_pkcs11;
1484 » CK_MECHANISM_TYPE macMech = CKM_SHA256_HMAC; 1492 CK_MECHANISM_TYPE macMech = CKM_SHA256_HMAC;
1485 » PK11Context *aes_ctx_pkcs11; 1493 PK11Context *aes_ctx_pkcs11;
1486 » CK_MECHANISM_TYPE cipherMech = CKM_AES_CBC; 1494 CK_MECHANISM_TYPE cipherMech = CKM_AES_CBC;
1487 » unsigned char * padding; 1495 unsigned char * padding;
1488 » PRUint32 padding_length; 1496 PRUint32 padding_length;
1489 » unsigned char *buffer; 1497 unsigned char *buffer;
1490 » unsigned int buffer_len; 1498 unsigned int buffer_len;
1491 » PRInt32 temp; 1499 PRInt32 temp;
1492 » SECItem cert_item; 1500 SECItem cert_item;
1493 PRInt8 nameType = TLS_STE_NO_SERVER_NAME; 1501 PRInt8 nameType = TLS_STE_NO_SERVER_NAME;
1494 1502
1495 » /* Turn off stateless session resumption if the client sends a 1503 /* Turn off stateless session resumption if the client sends a
1496 » * SessionTicket extension, even if the extension turns out to be 1504 * SessionTicket extension, even if the extension turns out to be
1497 » * malformed (ss->sec.ci.sid is non-NULL when doing session 1505 * malformed (ss->sec.ci.sid is non-NULL when doing session
1498 » * renegotiation.) 1506 * renegotiation.)
1499 » */ 1507 */
1500 » if (ss->sec.ci.sid != NULL) { 1508 if (ss->sec.ci.sid != NULL) {
1501 » if (ss->sec.uncache) 1509 if (ss->sec.uncache)
1502 » » ss->sec.uncache(ss->sec.ci.sid); 1510 ss->sec.uncache(ss->sec.ci.sid);
1503 » ssl_FreeSID(ss->sec.ci.sid); 1511 ssl_FreeSID(ss->sec.ci.sid);
1504 » ss->sec.ci.sid = NULL; 1512 ss->sec.ci.sid = NULL;
1505 » } 1513 }
1506 1514
1507 » extension_data.data = data->data; /* Keep a copy for future use. */ 1515 extension_data.data = data->data; /* Keep a copy for future use. */
1508 » extension_data.len = data->len; 1516 extension_data.len = data->len;
1509 1517
1510 » if (ssl3_ParseEncryptedSessionTicket(ss, data, &enc_session_ticket) 1518 if (ssl3_ParseEncryptedSessionTicket(ss, data, &enc_session_ticket)
1511 » != SECSuccess) 1519 != SECSuccess)
1512 » return SECFailure; 1520 return SECFailure;
1513 1521
1514 » /* Get session ticket keys. */ 1522 /* Get session ticket keys. */
1515 #ifndef NO_PKCS11_BYPASS 1523 #ifndef NO_PKCS11_BYPASS
1516 » if (ss->opt.bypassPKCS11) { 1524 if (ss->opt.bypassPKCS11) {
1517 » rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length, 1525 rv = ssl3_GetSessionTicketKeys(&aes_key, &aes_key_length,
1518 » » &mac_key, &mac_key_length); 1526 &mac_key, &mac_key_length);
1519 » } else 1527 } else
1520 #endif 1528 #endif
1521 » { 1529 {
1522 » rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11, 1530 rv = ssl3_GetSessionTicketKeysPKCS11(ss, &aes_key_pkcs11,
1523 » » &mac_key_pkcs11); 1531 &mac_key_pkcs11);
1524 » } 1532 }
1525 » if (rv != SECSuccess) { 1533 if (rv != SECSuccess) {
1526 » SSL_DBG(("%d: SSL[%d]: Unable to get/generate session ticket keys.", 1534 SSL_DBG(("%d: SSL[%d]: Unable to get/generate session ticket keys.",
1527 » » » SSL_GETPID(), ss->fd)); 1535 SSL_GETPID(), ss->fd));
1528 » goto loser; 1536 goto loser;
1529 » } 1537 }
1530 1538
1531 » /* If the ticket sent by the client was generated under a key different 1539 /* If the ticket sent by the client was generated under a key different
1532 » * from the one we have, bypass ticket processing. 1540 * from the one we have, bypass ticket processing.
1533 » */ 1541 */
1534 » if (PORT_Memcmp(enc_session_ticket.key_name, key_name, 1542 if (PORT_Memcmp(enc_session_ticket.key_name, key_name,
1535 » » SESS_TICKET_KEY_NAME_LEN) != 0) { 1543 SESS_TICKET_KEY_NAME_LEN) != 0) {
1536 » SSL_DBG(("%d: SSL[%d]: Session ticket key_name sent mismatch.", 1544 SSL_DBG(("%d: SSL[%d]: Session ticket key_name sent mismatch.",
1537 » » » SSL_GETPID(), ss->fd)); 1545 SSL_GETPID(), ss->fd));
1538 » goto no_ticket; 1546 goto no_ticket;
1539 » } 1547 }
1540 1548
1541 » /* Verify the MAC on the ticket. MAC verification may also 1549 /* Verify the MAC on the ticket. MAC verification may also
1542 » * fail if the MAC key has been recently refreshed. 1550 * fail if the MAC key has been recently refreshed.
1543 » */ 1551 */
1544 #ifndef NO_PKCS11_BYPASS 1552 #ifndef NO_PKCS11_BYPASS
1545 » if (ss->opt.bypassPKCS11) { 1553 if (ss->opt.bypassPKCS11) {
1546 » hmac_ctx = (HMACContext *)hmac_ctx_buf; 1554 hmac_ctx = (HMACContext *)hmac_ctx_buf;
1547 » hashObj = HASH_GetRawHashObject(HASH_AlgSHA256); 1555 hashObj = HASH_GetRawHashObject(HASH_AlgSHA256);
1548 » if (HMAC_Init(hmac_ctx, hashObj, mac_key, 1556 if (HMAC_Init(hmac_ctx, hashObj, mac_key,
1549 » » sizeof(session_ticket_mac_key), PR_FALSE) != SECSuccess) 1557 sizeof(session_ticket_mac_key), PR_FALSE) != SECSuccess)
1550 » » goto no_ticket; 1558 goto no_ticket;
1551 » HMAC_Begin(hmac_ctx); 1559 HMAC_Begin(hmac_ctx);
1552 » HMAC_Update(hmac_ctx, extension_data.data, 1560 HMAC_Update(hmac_ctx, extension_data.data,
1553 » » extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH); 1561 extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH);
1554 » if (HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length, 1562 if (HMAC_Finish(hmac_ctx, computed_mac, &computed_mac_length,
1555 » » sizeof(computed_mac)) != SECSuccess) 1563 sizeof(computed_mac)) != SECSuccess)
1556 » » goto no_ticket; 1564 goto no_ticket;
1557 » } else 1565 } else
1558 #endif 1566 #endif
1559 » { 1567 {
1560 » SECItem macParam; 1568 SECItem macParam;
1561 » macParam.data = NULL; 1569 macParam.data = NULL;
1562 » macParam.len = 0; 1570 macParam.len = 0;
1563 » hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech, 1571 hmac_ctx_pkcs11 = PK11_CreateContextBySymKey(macMech,
1564 » » CKA_SIGN, mac_key_pkcs11, &macParam); 1572 CKA_SIGN, mac_key_pkcs11, &macParam);
1565 » if (!hmac_ctx_pkcs11) { 1573 if (!hmac_ctx_pkcs11) {
1566 » » SSL_DBG(("%d: SSL[%d]: Unable to create HMAC context: %d.", 1574 SSL_DBG(("%d: SSL[%d]: Unable to create HMAC context: %d.",
1567 » » » SSL_GETPID(), ss->fd, PORT_GetError())); 1575 SSL_GETPID(), ss->fd, PORT_GetError()));
1568 » » goto no_ticket; 1576 goto no_ticket;
1569 » } else { 1577 } else {
1570 » » SSL_DBG(("%d: SSL[%d]: Successfully created HMAC context.", 1578 SSL_DBG(("%d: SSL[%d]: Successfully created HMAC context.",
1571 » » » SSL_GETPID(), ss->fd)); 1579 SSL_GETPID(), ss->fd));
1572 » } 1580 }
1573 » rv = PK11_DigestBegin(hmac_ctx_pkcs11); 1581 rv = PK11_DigestBegin(hmac_ctx_pkcs11);
1574 » rv = PK11_DigestOp(hmac_ctx_pkcs11, extension_data.data, 1582 rv = PK11_DigestOp(hmac_ctx_pkcs11, extension_data.data,
1575 » » extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH); 1583 extension_data.len - TLS_EX_SESS_TICKET_MAC_LENGTH);
1576 » if (rv != SECSuccess) { 1584 if (rv != SECSuccess) {
1577 » » PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE); 1585 PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
1578 » » goto no_ticket; 1586 goto no_ticket;
1579 » } 1587 }
1580 » rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac, 1588 rv = PK11_DigestFinal(hmac_ctx_pkcs11, computed_mac,
1581 » » &computed_mac_length, sizeof(computed_mac)); 1589 &computed_mac_length, sizeof(computed_mac));
1582 » PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE); 1590 PK11_DestroyContext(hmac_ctx_pkcs11, PR_TRUE);
1583 » if (rv != SECSuccess) 1591 if (rv != SECSuccess)
1584 » » goto no_ticket; 1592 goto no_ticket;
1585 » } 1593 }
1586 » if (NSS_SecureMemcmp(computed_mac, enc_session_ticket.mac, 1594 if (NSS_SecureMemcmp(computed_mac, enc_session_ticket.mac,
1587 » » computed_mac_length) != 0) { 1595 computed_mac_length) != 0) {
1588 » SSL_DBG(("%d: SSL[%d]: Session ticket MAC mismatch.", 1596 SSL_DBG(("%d: SSL[%d]: Session ticket MAC mismatch.",
1589 » » » SSL_GETPID(), ss->fd)); 1597 SSL_GETPID(), ss->fd));
1590 » goto no_ticket; 1598 goto no_ticket;
1591 » } 1599 }
1592 1600
1593 » /* We ignore key_name for now. 1601 /* We ignore key_name for now.
1594 » * This is ok as MAC verification succeeded. 1602 * This is ok as MAC verification succeeded.
1595 » */ 1603 */
1596 1604
1597 » /* Decrypt the ticket. */ 1605 /* Decrypt the ticket. */
1598 1606
1599 » /* Plaintext is shorter than the ciphertext due to padding. */ 1607 /* Plaintext is shorter than the ciphertext due to padding. */
1600 » decrypted_state = SECITEM_AllocItem(NULL, NULL, 1608 decrypted_state = SECITEM_AllocItem(NULL, NULL,
1601 » enc_session_ticket.encrypted_state.len); 1609 enc_session_ticket.encrypted_state.len);
1602 1610
1603 #ifndef NO_PKCS11_BYPASS 1611 #ifndef NO_PKCS11_BYPASS
1604 » if (ss->opt.bypassPKCS11) { 1612 if (ss->opt.bypassPKCS11) {
1605 » aes_ctx = (AESContext *)aes_ctx_buf; 1613 aes_ctx = (AESContext *)aes_ctx_buf;
1606 » rv = AES_InitContext(aes_ctx, aes_key, 1614 rv = AES_InitContext(aes_ctx, aes_key,
1607 » » sizeof(session_ticket_enc_key), enc_session_ticket.iv, 1615 sizeof(session_ticket_enc_key), enc_session_ticket.iv,
1608 » » NSS_AES_CBC, 0,AES_BLOCK_SIZE); 1616 NSS_AES_CBC, 0,AES_BLOCK_SIZE);
1609 » if (rv != SECSuccess) { 1617 if (rv != SECSuccess) {
1610 » » SSL_DBG(("%d: SSL[%d]: Unable to create AES context.", 1618 SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
1611 » » » SSL_GETPID(), ss->fd)); 1619 SSL_GETPID(), ss->fd));
1612 » » goto no_ticket; 1620 goto no_ticket;
1613 » } 1621 }
1614 1622
1615 » rv = AES_Decrypt(aes_ctx, decrypted_state->data, 1623 rv = AES_Decrypt(aes_ctx, decrypted_state->data,
1616 » » &decrypted_state->len, decrypted_state->len, 1624 &decrypted_state->len, decrypted_state->len,
1617 » » enc_session_ticket.encrypted_state.data, 1625 enc_session_ticket.encrypted_state.data,
1618 » » enc_session_ticket.encrypted_state.len); 1626 enc_session_ticket.encrypted_state.len);
1619 » if (rv != SECSuccess) 1627 if (rv != SECSuccess)
1620 » » goto no_ticket; 1628 goto no_ticket;
1621 » } else 1629 } else
1622 #endif 1630 #endif
1623 » { 1631 {
1624 » SECItem ivItem; 1632 SECItem ivItem;
1625 » ivItem.data = enc_session_ticket.iv; 1633 ivItem.data = enc_session_ticket.iv;
1626 » ivItem.len = AES_BLOCK_SIZE; 1634 ivItem.len = AES_BLOCK_SIZE;
1627 » aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech, 1635 aes_ctx_pkcs11 = PK11_CreateContextBySymKey(cipherMech,
1628 » » CKA_DECRYPT, aes_key_pkcs11, &ivItem); 1636 CKA_DECRYPT, aes_key_pkcs11, &ivItem);
1629 » if (!aes_ctx_pkcs11) { 1637 if (!aes_ctx_pkcs11) {
1630 » » SSL_DBG(("%d: SSL[%d]: Unable to create AES context.", 1638 SSL_DBG(("%d: SSL[%d]: Unable to create AES context.",
1631 » » » SSL_GETPID(), ss->fd)); 1639 SSL_GETPID(), ss->fd));
1632 » » goto no_ticket; 1640 goto no_ticket;
1633 » } 1641 }
1634 1642
1635 » rv = PK11_CipherOp(aes_ctx_pkcs11, decrypted_state->data, 1643 rv = PK11_CipherOp(aes_ctx_pkcs11, decrypted_state->data,
1636 » » (int *)&decrypted_state->len, decrypted_state->len, 1644 (int *)&decrypted_state->len, decrypted_state->len,
1637 » » enc_session_ticket.encrypted_state.data, 1645 enc_session_ticket.encrypted_state.data,
1638 » » enc_session_ticket.encrypted_state.len); 1646 enc_session_ticket.encrypted_state.len);
1639 » PK11_Finalize(aes_ctx_pkcs11); 1647 PK11_Finalize(aes_ctx_pkcs11);
1640 » PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE); 1648 PK11_DestroyContext(aes_ctx_pkcs11, PR_TRUE);
1641 » if (rv != SECSuccess) 1649 if (rv != SECSuccess)
1642 » » goto no_ticket; 1650 goto no_ticket;
1643 » } 1651 }
1644 1652
1645 » /* Check padding. */ 1653 /* Check padding. */
1646 » padding_length = 1654 padding_length =
1647 » (PRUint32)decrypted_state->data[decrypted_state->len - 1]; 1655 (PRUint32)decrypted_state->data[decrypted_state->len - 1];
1648 » if (padding_length == 0 || padding_length > AES_BLOCK_SIZE) 1656 if (padding_length == 0 || padding_length > AES_BLOCK_SIZE)
1649 » goto no_ticket; 1657 goto no_ticket;
1650 1658
1651 » padding = &decrypted_state->data[decrypted_state->len - padding_length]; 1659 padding = &decrypted_state->data[decrypted_state->len - padding_length];
1652 » for (i = 0; i < padding_length; i++, padding++) { 1660 for (i = 0; i < padding_length; i++, padding++) {
1653 » if (padding_length != (PRUint32)*padding) 1661 if (padding_length != (PRUint32)*padding)
1654 » » goto no_ticket; 1662 goto no_ticket;
1655 » } 1663 }
1656 1664
1657 » /* Deserialize session state. */ 1665 /* Deserialize session state. */
1658 » buffer = decrypted_state->data; 1666 buffer = decrypted_state->data;
1659 » buffer_len = decrypted_state->len; 1667 buffer_len = decrypted_state->len;
1660 1668
1661 » parsed_session_ticket = PORT_ZAlloc(sizeof(SessionTicket)); 1669 parsed_session_ticket = PORT_ZAlloc(sizeof(SessionTicket));
1662 » if (parsed_session_ticket == NULL) { 1670 if (parsed_session_ticket == NULL) {
1663 » rv = SECFailure; 1671 rv = SECFailure;
1664 » goto loser; 1672 goto loser;
1665 » } 1673 }
1666 1674
1667 » /* Read ticket_version (which is ignored for now.) */ 1675 /* Read ticket_version (which is ignored for now.) */
1668 » temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len); 1676 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
1669 » if (temp < 0) goto no_ticket; 1677 if (temp < 0) goto no_ticket;
1670 » parsed_session_ticket->ticket_version = (SSL3ProtocolVersion)temp; 1678 parsed_session_ticket->ticket_version = (SSL3ProtocolVersion)temp;
1671 1679
1672 » /* Read SSLVersion. */ 1680 /* Read SSLVersion. */
1673 » temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len); 1681 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
1674 » if (temp < 0) goto no_ticket; 1682 if (temp < 0) goto no_ticket;
1675 » parsed_session_ticket->ssl_version = (SSL3ProtocolVersion)temp; 1683 parsed_session_ticket->ssl_version = (SSL3ProtocolVersion)temp;
1676 1684
1677 » /* Read cipher_suite. */ 1685 /* Read cipher_suite. */
1678 » temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len); 1686 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
1679 » if (temp < 0) goto no_ticket; 1687 if (temp < 0) goto no_ticket;
1680 » parsed_session_ticket->cipher_suite = (ssl3CipherSuite)temp; 1688 parsed_session_ticket->cipher_suite = (ssl3CipherSuite)temp;
1681 1689
1682 » /* Read compression_method. */ 1690 /* Read compression_method. */
1683 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1691 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1684 » if (temp < 0) goto no_ticket; 1692 if (temp < 0) goto no_ticket;
1685 » parsed_session_ticket->compression_method = (SSLCompressionMethod)temp; 1693 parsed_session_ticket->compression_method = (SSLCompressionMethod)temp;
1686 1694
1687 » /* Read cipher spec parameters. */ 1695 /* Read cipher spec parameters. */
1688 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1696 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1689 » if (temp < 0) goto no_ticket; 1697 if (temp < 0) goto no_ticket;
1690 » parsed_session_ticket->authAlgorithm = (SSLSignType)temp; 1698 parsed_session_ticket->authAlgorithm = (SSLSignType)temp;
1691 » temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len); 1699 temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
1692 » if (temp < 0) goto no_ticket; 1700 if (temp < 0) goto no_ticket;
1693 » parsed_session_ticket->authKeyBits = (PRUint32)temp; 1701 parsed_session_ticket->authKeyBits = (PRUint32)temp;
1694 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1702 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1695 » if (temp < 0) goto no_ticket; 1703 if (temp < 0) goto no_ticket;
1696 » parsed_session_ticket->keaType = (SSLKEAType)temp; 1704 parsed_session_ticket->keaType = (SSLKEAType)temp;
1697 » temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len); 1705 temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
1698 » if (temp < 0) goto no_ticket; 1706 if (temp < 0) goto no_ticket;
1699 » parsed_session_ticket->keaKeyBits = (PRUint32)temp; 1707 parsed_session_ticket->keaKeyBits = (PRUint32)temp;
1700 1708
1701 » /* Read wrapped master_secret. */ 1709 /* Read wrapped master_secret. */
1702 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1710 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1703 » if (temp < 0) goto no_ticket; 1711 if (temp < 0) goto no_ticket;
1704 » parsed_session_ticket->ms_is_wrapped = (PRBool)temp; 1712 parsed_session_ticket->ms_is_wrapped = (PRBool)temp;
1705 1713
1706 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1714 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1707 » if (temp < 0) goto no_ticket; 1715 if (temp < 0) goto no_ticket;
1708 » parsed_session_ticket->exchKeyType = (SSL3KEAType)temp; 1716 parsed_session_ticket->exchKeyType = (SSL3KEAType)temp;
1709 1717
1710 » temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len); 1718 temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
1711 » if (temp < 0) goto no_ticket; 1719 if (temp < 0) goto no_ticket;
1712 » parsed_session_ticket->msWrapMech = (CK_MECHANISM_TYPE)temp; 1720 parsed_session_ticket->msWrapMech = (CK_MECHANISM_TYPE)temp;
1713 1721
1714 » temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len); 1722 temp = ssl3_ConsumeHandshakeNumber(ss, 2, &buffer, &buffer_len);
1715 » if (temp < 0) goto no_ticket; 1723 if (temp < 0) goto no_ticket;
1716 » parsed_session_ticket->ms_length = (PRUint16)temp; 1724 parsed_session_ticket->ms_length = (PRUint16)temp;
1717 » if (parsed_session_ticket->ms_length == 0 || /* sanity check MS. */ 1725 if (parsed_session_ticket->ms_length == 0 || /* sanity check MS. */
1718 » parsed_session_ticket->ms_length > 1726 parsed_session_ticket->ms_length >
1719 » sizeof(parsed_session_ticket->master_secret)) 1727 sizeof(parsed_session_ticket->master_secret))
1720 » goto no_ticket; 1728 goto no_ticket;
1721 » 1729
1722 » /* Allow for the wrapped master secret to be longer. */ 1730 /* Allow for the wrapped master secret to be longer. */
1723 » if (buffer_len < parsed_session_ticket->ms_length) 1731 if (buffer_len < parsed_session_ticket->ms_length)
1724 » goto no_ticket; 1732 goto no_ticket;
1725 » PORT_Memcpy(parsed_session_ticket->master_secret, buffer, 1733 PORT_Memcpy(parsed_session_ticket->master_secret, buffer,
1726 » parsed_session_ticket->ms_length); 1734 parsed_session_ticket->ms_length);
1727 » buffer += parsed_session_ticket->ms_length; 1735 buffer += parsed_session_ticket->ms_length;
1728 » buffer_len -= parsed_session_ticket->ms_length; 1736 buffer_len -= parsed_session_ticket->ms_length;
1729 1737
1730 » /* Read client_identity */ 1738 /* Read client_identity */
1731 » temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1739 temp = ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1732 » if (temp < 0) 1740 if (temp < 0)
1733 » goto no_ticket; 1741 goto no_ticket;
1734 » parsed_session_ticket->client_identity.client_auth_type = 1742 parsed_session_ticket->client_identity.client_auth_type =
1735 » (ClientAuthenticationType)temp; 1743 (ClientAuthenticationType)temp;
1736 » switch(parsed_session_ticket->client_identity.client_auth_type) { 1744 switch(parsed_session_ticket->client_identity.client_auth_type) {
1737 case CLIENT_AUTH_ANONYMOUS: 1745 case CLIENT_AUTH_ANONYMOUS:
1738 » » break; 1746 break;
1739 case CLIENT_AUTH_CERTIFICATE: 1747 case CLIENT_AUTH_CERTIFICATE:
1740 » » rv = ssl3_ConsumeHandshakeVariable(ss, &cert_item, 3, 1748 rv = ssl3_ConsumeHandshakeVariable(ss, &cert_item, 3,
1741 » » &buffer, &buffer_len); 1749 &buffer, &buffer_len);
1742 » » if (rv != SECSuccess) goto no_ticket; 1750 if (rv != SECSuccess) goto no_ticket;
1743 » » rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->peer_cert, 1751 rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->peer_cert,
1744 » » &cert_item); 1752 &cert_item);
1745 » » if (rv != SECSuccess) goto no_ticket; 1753 if (rv != SECSuccess) goto no_ticket;
1746 » » break; 1754 break;
1747 default: 1755 default:
1748 » » goto no_ticket; 1756 goto no_ticket;
1749 » } 1757 }
1750 » /* Read timestamp. */ 1758 /* Read timestamp. */
1751 » temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len); 1759 temp = ssl3_ConsumeHandshakeNumber(ss, 4, &buffer, &buffer_len);
1752 » if (temp < 0) 1760 if (temp < 0)
1753 » goto no_ticket; 1761 goto no_ticket;
1754 » parsed_session_ticket->timestamp = (PRUint32)temp; 1762 parsed_session_ticket->timestamp = (PRUint32)temp;
1755 1763
1756 /* Read server name */ 1764 /* Read server name */
1757 nameType = 1765 nameType =
1758 ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len); 1766 ssl3_ConsumeHandshakeNumber(ss, 1, &buffer, &buffer_len);
1759 if (nameType != TLS_STE_NO_SERVER_NAME) { 1767 if (nameType != TLS_STE_NO_SERVER_NAME) {
1760 SECItem name_item; 1768 SECItem name_item;
1761 rv = ssl3_ConsumeHandshakeVariable(ss, &name_item, 2, &buffer, 1769 rv = ssl3_ConsumeHandshakeVariable(ss, &name_item, 2, &buffer,
1762 &buffer_len); 1770 &buffer_len);
1763 if (rv != SECSuccess) goto no_ticket; 1771 if (rv != SECSuccess) goto no_ticket;
1764 rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->srvName, 1772 rv = SECITEM_CopyItem(NULL, &parsed_session_ticket->srvName,
1765 &name_item); 1773 &name_item);
1766 if (rv != SECSuccess) goto no_ticket; 1774 if (rv != SECSuccess) goto no_ticket;
1767 parsed_session_ticket->srvName.type = nameType; 1775 parsed_session_ticket->srvName.type = nameType;
1768 } 1776 }
1769 1777
1770 » /* Done parsing. Check that all bytes have been consumed. */ 1778 /* Done parsing. Check that all bytes have been consumed. */
1771 » if (buffer_len != padding_length) 1779 if (buffer_len != padding_length)
1772 » goto no_ticket; 1780 goto no_ticket;
1773 1781
1774 » /* Use the ticket if it has not expired, otherwise free the allocated 1782 /* Use the ticket if it has not expired, otherwise free the allocated
1775 » * memory since the ticket is of no use. 1783 * memory since the ticket is of no use.
1776 » */ 1784 */
1777 » if (parsed_session_ticket->timestamp != 0 && 1785 if (parsed_session_ticket->timestamp != 0 &&
1778 » parsed_session_ticket->timestamp + 1786 parsed_session_ticket->timestamp +
1779 » TLS_EX_SESS_TICKET_LIFETIME_HINT > ssl_Time()) { 1787 TLS_EX_SESS_TICKET_LIFETIME_HINT > ssl_Time()) {
1780 1788
1781 » sid = ssl3_NewSessionID(ss, PR_TRUE); 1789 sid = ssl3_NewSessionID(ss, PR_TRUE);
1782 » if (sid == NULL) { 1790 if (sid == NULL) {
1783 » » rv = SECFailure; 1791 rv = SECFailure;
1784 » » goto loser; 1792 goto loser;
1785 » } 1793 }
1786 1794
1787 » /* Copy over parameters. */ 1795 /* Copy over parameters. */
1788 » sid->version = parsed_session_ticket->ssl_version; 1796 sid->version = parsed_session_ticket->ssl_version;
1789 » sid->u.ssl3.cipherSuite = parsed_session_ticket->cipher_suite; 1797 sid->u.ssl3.cipherSuite = parsed_session_ticket->cipher_suite;
1790 » sid->u.ssl3.compression = parsed_session_ticket->compression_method; 1798 sid->u.ssl3.compression = parsed_session_ticket->compression_method;
1791 » sid->authAlgorithm = parsed_session_ticket->authAlgorithm; 1799 sid->authAlgorithm = parsed_session_ticket->authAlgorithm;
1792 » sid->authKeyBits = parsed_session_ticket->authKeyBits; 1800 sid->authKeyBits = parsed_session_ticket->authKeyBits;
1793 » sid->keaType = parsed_session_ticket->keaType; 1801 sid->keaType = parsed_session_ticket->keaType;
1794 » sid->keaKeyBits = parsed_session_ticket->keaKeyBits; 1802 sid->keaKeyBits = parsed_session_ticket->keaKeyBits;
1795 1803
1796 » /* Copy master secret. */ 1804 /* Copy master secret. */
1797 #ifndef NO_PKCS11_BYPASS 1805 #ifndef NO_PKCS11_BYPASS
1798 » if (ss->opt.bypassPKCS11 && 1806 if (ss->opt.bypassPKCS11 &&
1799 » » parsed_session_ticket->ms_is_wrapped) 1807 parsed_session_ticket->ms_is_wrapped)
1800 » » goto no_ticket; 1808 goto no_ticket;
1801 #endif 1809 #endif
1802 » if (parsed_session_ticket->ms_length > 1810 if (parsed_session_ticket->ms_length >
1803 » » sizeof(sid->u.ssl3.keys.wrapped_master_secret)) 1811 sizeof(sid->u.ssl3.keys.wrapped_master_secret))
1804 » » goto no_ticket; 1812 goto no_ticket;
1805 » PORT_Memcpy(sid->u.ssl3.keys.wrapped_master_secret, 1813 PORT_Memcpy(sid->u.ssl3.keys.wrapped_master_secret,
1806 » » parsed_session_ticket->master_secret, 1814 parsed_session_ticket->master_secret,
1807 » » parsed_session_ticket->ms_length); 1815 parsed_session_ticket->ms_length);
1808 » sid->u.ssl3.keys.wrapped_master_secret_len = 1816 sid->u.ssl3.keys.wrapped_master_secret_len =
1809 » » parsed_session_ticket->ms_length; 1817 parsed_session_ticket->ms_length;
1810 » sid->u.ssl3.exchKeyType = parsed_session_ticket->exchKeyType; 1818 sid->u.ssl3.exchKeyType = parsed_session_ticket->exchKeyType;
1811 » sid->u.ssl3.masterWrapMech = parsed_session_ticket->msWrapMech; 1819 sid->u.ssl3.masterWrapMech = parsed_session_ticket->msWrapMech;
1812 » sid->u.ssl3.keys.msIsWrapped = 1820 sid->u.ssl3.keys.msIsWrapped =
1813 » » parsed_session_ticket->ms_is_wrapped; 1821 parsed_session_ticket->ms_is_wrapped;
1814 » sid->u.ssl3.masterValid = PR_TRUE; 1822 sid->u.ssl3.masterValid = PR_TRUE;
1815 » sid->u.ssl3.keys.resumable = PR_TRUE; 1823 sid->u.ssl3.keys.resumable = PR_TRUE;
1816 1824
1817 » /* Copy over client cert from session ticket if there is one. */ 1825 /* Copy over client cert from session ticket if there is one. */
1818 » if (parsed_session_ticket->peer_cert.data != NULL) { 1826 if (parsed_session_ticket->peer_cert.data != NULL) {
1819 » » if (sid->peerCert != NULL) 1827 if (sid->peerCert != NULL)
1820 » » CERT_DestroyCertificate(sid->peerCert); 1828 CERT_DestroyCertificate(sid->peerCert);
1821 » » sid->peerCert = CERT_NewTempCertificate(ss->dbHandle, 1829 sid->peerCert = CERT_NewTempCertificate(ss->dbHandle,
1822 » » &parsed_session_ticket->peer_cert, NULL, PR_FALSE, PR_TRUE); 1830 &parsed_session_ticket->peer_cert, NULL, PR_FALSE, PR_TRUE);
1823 » » if (sid->peerCert == NULL) { 1831 if (sid->peerCert == NULL) {
1824 » » rv = SECFailure; 1832 rv = SECFailure;
1825 » » goto loser; 1833 goto loser;
1826 » » } 1834 }
1827 » } 1835 }
1828 » if (parsed_session_ticket->srvName.data != NULL) { 1836 if (parsed_session_ticket->srvName.data != NULL) {
1829 sid->u.ssl3.srvName = parsed_session_ticket->srvName; 1837 sid->u.ssl3.srvName = parsed_session_ticket->srvName;
1830 } 1838 }
1831 » ss->statelessResume = PR_TRUE; 1839 ss->statelessResume = PR_TRUE;
1832 » ss->sec.ci.sid = sid; 1840 ss->sec.ci.sid = sid;
1833 » } 1841 }
1834 } 1842 }
1835 1843
1836 if (0) { 1844 if (0) {
1837 no_ticket: 1845 no_ticket:
1838 » SSL_DBG(("%d: SSL[%d]: Session ticket parsing failed.", 1846 SSL_DBG(("%d: SSL[%d]: Session ticket parsing failed.",
1839 » » » SSL_GETPID(), ss->fd)); 1847 SSL_GETPID(), ss->fd));
1840 » ssl3stats = SSL_GetStatistics(); 1848 ssl3stats = SSL_GetStatistics();
1841 » SSL_AtomicIncrementLong(& ssl3stats->hch_sid_ticket_parse_failures ); 1849 SSL_AtomicIncrementLong(& ssl3stats->hch_sid_ticket_parse_failures );
1842 } 1850 }
1843 rv = SECSuccess; 1851 rv = SECSuccess;
1844 1852
1845 loser: 1853 loser:
1846 » /* ss->sec.ci.sid == sid if it did NOT come here via goto statement 1854 /* ss->sec.ci.sid == sid if it did NOT come here via goto statement
1847 » * in that case do not free sid 1855 * in that case do not free sid
1848 » */ 1856 */
1849 » if (sid && (ss->sec.ci.sid != sid)) { 1857 if (sid && (ss->sec.ci.sid != sid)) {
1850 » ssl_FreeSID(sid); 1858 ssl_FreeSID(sid);
1851 » sid = NULL; 1859 sid = NULL;
1852 » } 1860 }
1853 if (decrypted_state != NULL) { 1861 if (decrypted_state != NULL) {
1854 » SECITEM_FreeItem(decrypted_state, PR_TRUE); 1862 SECITEM_FreeItem(decrypted_state, PR_TRUE);
1855 » decrypted_state = NULL; 1863 decrypted_state = NULL;
1856 } 1864 }
1857 1865
1858 if (parsed_session_ticket != NULL) { 1866 if (parsed_session_ticket != NULL) {
1859 » if (parsed_session_ticket->peer_cert.data) { 1867 if (parsed_session_ticket->peer_cert.data) {
1860 » SECITEM_FreeItem(&parsed_session_ticket->peer_cert, PR_FALSE); 1868 SECITEM_FreeItem(&parsed_session_ticket->peer_cert, PR_FALSE);
1861 » } 1869 }
1862 » PORT_ZFree(parsed_session_ticket, sizeof(SessionTicket)); 1870 PORT_ZFree(parsed_session_ticket, sizeof(SessionTicket));
1863 } 1871 }
1864 1872
1865 return rv; 1873 return rv;
1866 } 1874 }
1867 1875
1868 /* 1876 /*
1869 * Read bytes. Using this function means the SECItem structure 1877 * Read bytes. Using this function means the SECItem structure
1870 * cannot be freed. The caller is expected to call this function 1878 * cannot be freed. The caller is expected to call this function
1871 * on a shallow copy of the structure. 1879 * on a shallow copy of the structure.
1872 */ 1880 */
1873 static SECStatus 1881 static SECStatus
1874 ssl3_ConsumeFromItem(SECItem *item, unsigned char **buf, PRUint32 bytes) 1882 ssl3_ConsumeFromItem(SECItem *item, unsigned char **buf, PRUint32 bytes)
1875 { 1883 {
1876 if (bytes > item->len) 1884 if (bytes > item->len)
1877 » return SECFailure; 1885 return SECFailure;
1878 1886
1879 *buf = item->data; 1887 *buf = item->data;
1880 item->data += bytes; 1888 item->data += bytes;
1881 item->len -= bytes; 1889 item->len -= bytes;
1882 return SECSuccess; 1890 return SECSuccess;
1883 } 1891 }
1884 1892
1885 static SECStatus 1893 static SECStatus
1886 ssl3_ParseEncryptedSessionTicket(sslSocket *ss, SECItem *data, 1894 ssl3_ParseEncryptedSessionTicket(sslSocket *ss, SECItem *data,
1887 EncryptedSessionTicket *enc_session_ticket) 1895 EncryptedSessionTicket *enc_session_ticket)
1888 { 1896 {
1889 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->key_name, 1897 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->key_name,
1890 » SESS_TICKET_KEY_NAME_LEN) != SECSuccess) 1898 SESS_TICKET_KEY_NAME_LEN) != SECSuccess)
1891 » return SECFailure; 1899 return SECFailure;
1892 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->iv, 1900 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->iv,
1893 » AES_BLOCK_SIZE) != SECSuccess) 1901 AES_BLOCK_SIZE) != SECSuccess)
1894 » return SECFailure; 1902 return SECFailure;
1895 if (ssl3_ConsumeHandshakeVariable(ss, &enc_session_ticket->encrypted_state, 1903 if (ssl3_ConsumeHandshakeVariable(ss, &enc_session_ticket->encrypted_state,
1896 » 2, &data->data, &data->len) != SECSuccess) 1904 2, &data->data, &data->len) != SECSuccess)
1897 » return SECFailure; 1905 return SECFailure;
1898 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->mac, 1906 if (ssl3_ConsumeFromItem(data, &enc_session_ticket->mac,
1899 » TLS_EX_SESS_TICKET_MAC_LENGTH) != SECSuccess) 1907 TLS_EX_SESS_TICKET_MAC_LENGTH) != SECSuccess)
1900 » return SECFailure; 1908 return SECFailure;
1901 if (data->len != 0) /* Make sure that we have consumed all bytes. */ 1909 if (data->len != 0) /* Make sure that we have consumed all bytes. */
1902 » return SECFailure; 1910 return SECFailure;
1903 1911
1904 return SECSuccess; 1912 return SECSuccess;
1905 } 1913 }
1906 1914
1907 /* go through hello extensions in buffer "b". 1915 /* go through hello extensions in buffer "b".
1908 * For each one, find the extension handler in the table, and 1916 * For each one, find the extension handler in the table, and
1909 * if present, invoke that handler. 1917 * if present, invoke that handler.
1910 * Servers ignore any extensions with unknown extension types. 1918 * Servers ignore any extensions with unknown extension types.
1911 * Clients reject any extensions with unadvertised extension types. 1919 * Clients reject any extensions with unadvertised extension types.
1912 */ 1920 */
1913 SECStatus 1921 SECStatus
1914 ssl3_HandleHelloExtensions(sslSocket *ss, SSL3Opaque **b, PRUint32 *length) 1922 ssl3_HandleHelloExtensions(sslSocket *ss, SSL3Opaque **b, PRUint32 *length)
1915 { 1923 {
1916 const ssl3HelloExtensionHandler * handlers; 1924 const ssl3HelloExtensionHandler * handlers;
1917 1925
1918 if (ss->sec.isServer) { 1926 if (ss->sec.isServer) {
1919 handlers = clientHelloHandlers; 1927 handlers = clientHelloHandlers;
1920 } else if (ss->version > SSL_LIBRARY_VERSION_3_0) { 1928 } else if (ss->version > SSL_LIBRARY_VERSION_3_0) {
1921 handlers = serverHelloHandlersTLS; 1929 handlers = serverHelloHandlersTLS;
1922 } else { 1930 } else {
1923 handlers = serverHelloHandlersSSL3; 1931 handlers = serverHelloHandlersSSL3;
1924 } 1932 }
1925 1933
1926 while (*length) { 1934 while (*length) {
1927 » const ssl3HelloExtensionHandler * handler; 1935 const ssl3HelloExtensionHandler * handler;
1928 » SECStatus rv; 1936 SECStatus rv;
1929 » PRInt32 extension_type; 1937 PRInt32 extension_type;
1930 » SECItem extension_data; 1938 SECItem extension_data;
1931 1939
1932 » /* Get the extension's type field */ 1940 /* Get the extension's type field */
1933 » extension_type = ssl3_ConsumeHandshakeNumber(ss, 2, b, length); 1941 extension_type = ssl3_ConsumeHandshakeNumber(ss, 2, b, length);
1934 » if (extension_type < 0) /* failure to decode extension_type */ 1942 if (extension_type < 0) /* failure to decode extension_type */
1935 » return SECFailure; /* alert already sent */ 1943 return SECFailure; /* alert already sent */
1936 1944
1937 » /* get the data for this extension, so we can pass it or skip it. */ 1945 /* get the data for this extension, so we can pass it or skip it. */
1938 » rv = ssl3_ConsumeHandshakeVariable(ss, &extension_data, 2, b, length); 1946 rv = ssl3_ConsumeHandshakeVariable(ss, &extension_data, 2, b, length);
1939 » if (rv != SECSuccess) 1947 if (rv != SECSuccess)
1940 » return rv; 1948 return rv;
1941 1949
1942 » /* Check whether the server sent an extension which was not advertised 1950 /* Check whether the server sent an extension which was not advertised
1943 » * in the ClientHello. 1951 * in the ClientHello.
1944 » */ 1952 */
1945 » if (!ss->sec.isServer && 1953 if (!ss->sec.isServer &&
1946 » !ssl3_ClientExtensionAdvertised(ss, extension_type)) 1954 !ssl3_ClientExtensionAdvertised(ss, extension_type))
1947 » return SECFailure; /* TODO: send unsupported_extension alert */ 1955 return SECFailure; /* TODO: send unsupported_extension alert */
1948 1956
1949 » /* Check whether an extension has been sent multiple times. */ 1957 /* Check whether an extension has been sent multiple times. */
1950 » if (ssl3_ExtensionNegotiated(ss, extension_type)) 1958 if (ssl3_ExtensionNegotiated(ss, extension_type))
1951 » return SECFailure; 1959 return SECFailure;
1952 1960
1953 » /* find extension_type in table of Hello Extension Handlers */ 1961 /* find extension_type in table of Hello Extension Handlers */
1954 » for (handler = handlers; handler->ex_type >= 0; handler++) { 1962 for (handler = handlers; handler->ex_type >= 0; handler++) {
1955 » /* if found, call this handler */ 1963 /* if found, call this handler */
1956 » if (handler->ex_type == extension_type) { 1964 if (handler->ex_type == extension_type) {
1957 » » rv = (*handler->ex_handler)(ss, (PRUint16)extension_type, 1965 rv = (*handler->ex_handler)(ss, (PRUint16)extension_type,
1958 » » &extension_data); 1966 &extension_data);
1959 » » /* Ignore this result */ 1967 /* Ignore this result */
1960 » » /* Treat all bad extensions as unrecognized types. */ 1968 /* Treat all bad extensions as unrecognized types. */
1961 » break; 1969 break;
1962 » } 1970 }
1963 » } 1971 }
1964 } 1972 }
1965 return SECSuccess; 1973 return SECSuccess;
1966 } 1974 }
1967 1975
1968 /* Add a callback function to the table of senders of server hello extensions. 1976 /* Add a callback function to the table of senders of server hello extensions.
1969 */ 1977 */
1970 SECStatus 1978 SECStatus
1971 ssl3_RegisterServerHelloExtensionSender(sslSocket *ss, PRUint16 ex_type, 1979 ssl3_RegisterServerHelloExtensionSender(sslSocket *ss, PRUint16 ex_type,
1972 » » » » ssl3HelloExtensionSenderFunc cb) 1980 ssl3HelloExtensionSenderFunc cb)
1973 { 1981 {
1974 int i; 1982 int i;
1975 ssl3HelloExtensionSender *sender = &ss->xtnData.serverSenders[0]; 1983 ssl3HelloExtensionSender *sender = &ss->xtnData.serverSenders[0];
1976 1984
1977 for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) { 1985 for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) {
1978 if (!sender->ex_sender) { 1986 if (!sender->ex_sender) {
1979 » sender->ex_type = ex_type; 1987 sender->ex_type = ex_type;
1980 » sender->ex_sender = cb; 1988 sender->ex_sender = cb;
1981 » return SECSuccess; 1989 return SECSuccess;
1982 » } 1990 }
1983 » /* detect duplicate senders */ 1991 /* detect duplicate senders */
1984 » PORT_Assert(sender->ex_type != ex_type); 1992 PORT_Assert(sender->ex_type != ex_type);
1985 » if (sender->ex_type == ex_type) { 1993 if (sender->ex_type == ex_type) {
1986 » /* duplicate */ 1994 /* duplicate */
1987 » break; 1995 break;
1988 » } 1996 }
1989 } 1997 }
1990 PORT_Assert(i < SSL_MAX_EXTENSIONS); /* table needs to grow */ 1998 PORT_Assert(i < SSL_MAX_EXTENSIONS); /* table needs to grow */
1991 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); 1999 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1992 return SECFailure; 2000 return SECFailure;
1993 } 2001 }
1994 2002
1995 /* call each of the extension senders and return the accumulated length */ 2003 /* call each of the extension senders and return the accumulated length */
1996 PRInt32 2004 PRInt32
1997 ssl3_CallHelloExtensionSenders(sslSocket *ss, PRBool append, PRUint32 maxBytes, 2005 ssl3_CallHelloExtensionSenders(sslSocket *ss, PRBool append, PRUint32 maxBytes,
1998 const ssl3HelloExtensionSender *sender) 2006 const ssl3HelloExtensionSender *sender)
1999 { 2007 {
2000 PRInt32 total_exten_len = 0; 2008 PRInt32 total_exten_len = 0;
2001 int i; 2009 int i;
2002 2010
2003 if (!sender) { 2011 if (!sender) {
2004 » sender = ss->version > SSL_LIBRARY_VERSION_3_0 ? 2012 sender = ss->version > SSL_LIBRARY_VERSION_3_0 ?
2005 &clientHelloSendersTLS[0] : &clientHelloSendersSSL3[0]; 2013 &clientHelloSendersTLS[0] : &clientHelloSendersSSL3[0];
2006 } 2014 }
2007 2015
2008 for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) { 2016 for (i = 0; i < SSL_MAX_EXTENSIONS; ++i, ++sender) {
2009 » if (sender->ex_sender) { 2017 if (sender->ex_sender) {
2010 » PRInt32 extLen = (*sender->ex_sender)(ss, append, maxBytes); 2018 PRInt32 extLen = (*sender->ex_sender)(ss, append, maxBytes);
2011 » if (extLen < 0) 2019 if (extLen < 0)
2012 » » return -1; 2020 return -1;
2013 » maxBytes -= extLen; 2021 maxBytes -= extLen;
2014 » total_exten_len += extLen; 2022 total_exten_len += extLen;
2015 » } 2023 }
2016 } 2024 }
2017 return total_exten_len; 2025 return total_exten_len;
2018 } 2026 }
2019 2027
2020 2028
2021 /* Extension format: 2029 /* Extension format:
2022 * Extension number: 2 bytes 2030 * Extension number: 2 bytes
2023 * Extension length: 2 bytes 2031 * Extension length: 2 bytes
2024 * Verify Data Length: 1 byte 2032 * Verify Data Length: 1 byte
2025 * Verify Data (TLS): 12 bytes (client) or 24 bytes (server) 2033 * Verify Data (TLS): 12 bytes (client) or 24 bytes (server)
2026 * Verify Data (SSL): 36 bytes (client) or 72 bytes (server) 2034 * Verify Data (SSL): 36 bytes (client) or 72 bytes (server)
2027 */ 2035 */
2028 static PRInt32 2036 static PRInt32
2029 ssl3_SendRenegotiationInfoXtn( 2037 ssl3_SendRenegotiationInfoXtn(
2030 » » » sslSocket * ss, 2038 sslSocket * ss,
2031 » » » PRBool append, 2039 PRBool append,
2032 » » » PRUint32 maxBytes) 2040 PRUint32 maxBytes)
2033 { 2041 {
2034 PRInt32 len, needed; 2042 PRInt32 len, needed;
2035 2043
2036 /* In draft-ietf-tls-renegotiation-03, it is NOT RECOMMENDED to send 2044 /* In draft-ietf-tls-renegotiation-03, it is NOT RECOMMENDED to send
2037 * both the SCSV and the empty RI, so when we send SCSV in 2045 * both the SCSV and the empty RI, so when we send SCSV in
2038 * the initial handshake, we don't also send RI. 2046 * the initial handshake, we don't also send RI.
2039 */ 2047 */
2040 if (!ss || ss->ssl3.hs.sendingSCSV) 2048 if (!ss || ss->ssl3.hs.sendingSCSV)
2041 » return 0; 2049 return 0;
2042 len = !ss->firstHsDone ? 0 : 2050 len = !ss->firstHsDone ? 0 :
2043 » (ss->sec.isServer ? ss->ssl3.hs.finishedBytes * 2 2051 (ss->sec.isServer ? ss->ssl3.hs.finishedBytes * 2
2044 » » » : ss->ssl3.hs.finishedBytes); 2052 : ss->ssl3.hs.finishedBytes);
2045 needed = 5 + len; 2053 needed = 5 + len;
2046 if (append && maxBytes >= needed) { 2054 if (append && maxBytes >= needed) {
2047 » SECStatus rv; 2055 SECStatus rv;
2048 » /* extension_type */ 2056 /* extension_type */
2049 » rv = ssl3_AppendHandshakeNumber(ss, ssl_renegotiation_info_xtn, 2); 2057 rv = ssl3_AppendHandshakeNumber(ss, ssl_renegotiation_info_xtn, 2);
2050 » if (rv != SECSuccess) return -1; 2058 if (rv != SECSuccess) return -1;
2051 » /* length of extension_data */ 2059 /* length of extension_data */
2052 » rv = ssl3_AppendHandshakeNumber(ss, len + 1, 2); 2060 rv = ssl3_AppendHandshakeNumber(ss, len + 1, 2);
2053 » if (rv != SECSuccess) return -1; 2061 if (rv != SECSuccess) return -1;
2054 » /* verify_Data from previous Finished message(s) */ 2062 /* verify_Data from previous Finished message(s) */
2055 » rv = ssl3_AppendHandshakeVariable(ss, 2063 rv = ssl3_AppendHandshakeVariable(ss,
2056 » » ss->ssl3.hs.finishedMsgs.data, len, 1); 2064 ss->ssl3.hs.finishedMsgs.data, len, 1);
2057 » if (rv != SECSuccess) return -1; 2065 if (rv != SECSuccess) return -1;
2058 » if (!ss->sec.isServer) { 2066 if (!ss->sec.isServer) {
2059 » TLSExtensionData *xtnData = &ss->xtnData; 2067 TLSExtensionData *xtnData = &ss->xtnData;
2060 » xtnData->advertised[xtnData->numAdvertised++] = 2068 xtnData->advertised[xtnData->numAdvertised++] =
2061 » ssl_renegotiation_info_xtn; 2069 ssl_renegotiation_info_xtn;
2062 » } 2070 }
2063 } 2071 }
2064 return needed; 2072 return needed;
2065 } 2073 }
2066 2074
2067 static SECStatus 2075 static SECStatus
2068 ssl3_ServerHandleStatusRequestXtn(sslSocket *ss, PRUint16 ex_type, 2076 ssl3_ServerHandleStatusRequestXtn(sslSocket *ss, PRUint16 ex_type,
2069 » » » » SECItem *data) 2077 SECItem *data)
2070 { 2078 {
2071 SECStatus rv = SECSuccess; 2079 SECStatus rv = SECSuccess;
2072 2080
2073 /* remember that we got this extension. */ 2081 /* remember that we got this extension. */
2074 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 2082 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
2075 PORT_Assert(ss->sec.isServer); 2083 PORT_Assert(ss->sec.isServer);
2076 /* prepare to send back the appropriate response */ 2084 /* prepare to send back the appropriate response */
2077 rv = ssl3_RegisterServerHelloExtensionSender(ss, ex_type, 2085 rv = ssl3_RegisterServerHelloExtensionSender(ss, ex_type,
2078 » » » » » ssl3_ServerSendStatusRequestXtn); 2086 ssl3_ServerSendStatusRequestXtn);
2079 return rv; 2087 return rv;
2080 } 2088 }
2081 2089
2082 /* This function runs in both the client and server. */ 2090 /* This function runs in both the client and server. */
2083 static SECStatus 2091 static SECStatus
2084 ssl3_HandleRenegotiationInfoXtn(sslSocket *ss, PRUint16 ex_type, SECItem *data) 2092 ssl3_HandleRenegotiationInfoXtn(sslSocket *ss, PRUint16 ex_type, SECItem *data)
2085 { 2093 {
2086 SECStatus rv = SECSuccess; 2094 SECStatus rv = SECSuccess;
2087 PRUint32 len = 0; 2095 PRUint32 len = 0;
2088 2096
2089 if (ss->firstHsDone) { 2097 if (ss->firstHsDone) {
2090 » len = ss->sec.isServer ? ss->ssl3.hs.finishedBytes 2098 len = ss->sec.isServer ? ss->ssl3.hs.finishedBytes
2091 » : ss->ssl3.hs.finishedBytes * 2; 2099 : ss->ssl3.hs.finishedBytes * 2;
2092 } 2100 }
2093 if (data->len != 1 + len || 2101 if (data->len != 1 + len ||
2094 » data->data[0] != len || (len && 2102 data->data[0] != len || (len &&
2095 » NSS_SecureMemcmp(ss->ssl3.hs.finishedMsgs.data, 2103 NSS_SecureMemcmp(ss->ssl3.hs.finishedMsgs.data,
2096 » data->data + 1, len))) { 2104 data->data + 1, len))) {
2097 » /* Can we do this here? Or, must we arrange for the caller to do it? */ 2105 /* Can we do this here? Or, must we arrange for the caller to do it? */
2098 » (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure); 2106 (void)SSL3_SendAlert(ss, alert_fatal, handshake_failure);
2099 » PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE); 2107 PORT_SetError(SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE);
2100 » return SECFailure; 2108 return SECFailure;
2101 } 2109 }
2102 /* remember that we got this extension and it was correct. */ 2110 /* remember that we got this extension and it was correct. */
2103 ss->peerRequestedProtection = 1; 2111 ss->peerRequestedProtection = 1;
2104 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 2112 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
2105 if (ss->sec.isServer) { 2113 if (ss->sec.isServer) {
2106 » /* prepare to send back the appropriate response */ 2114 /* prepare to send back the appropriate response */
2107 » rv = ssl3_RegisterServerHelloExtensionSender(ss, ex_type, 2115 rv = ssl3_RegisterServerHelloExtensionSender(ss, ex_type,
2108 » » » » » ssl3_SendRenegotiationInfoXtn); 2116 ssl3_SendRenegotiationInfoXtn);
2109 } 2117 }
2110 return rv; 2118 return rv;
2111 } 2119 }
2112 2120
2113 static PRInt32 2121 static PRInt32
2114 ssl3_SendUseSRTPXtn(sslSocket *ss, PRBool append, PRUint32 maxBytes) 2122 ssl3_SendUseSRTPXtn(sslSocket *ss, PRBool append, PRUint32 maxBytes)
2115 { 2123 {
2116 PRUint32 ext_data_len; 2124 PRUint32 ext_data_len;
2117 PRInt16 i; 2125 PRInt16 i;
2118 SECStatus rv; 2126 SECStatus rv;
2119 2127
2120 if (!ss) 2128 if (!ss)
2121 » return 0; 2129 return 0;
2122 2130
2123 if (!ss->sec.isServer) { 2131 if (!ss->sec.isServer) {
2124 » /* Client side */ 2132 /* Client side */
2125 2133
2126 » if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount) 2134 if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount)
2127 » return 0; /* Not relevant */ 2135 return 0; /* Not relevant */
2128 2136
2129 » ext_data_len = 2 + 2 * ss->ssl3.dtlsSRTPCipherCount + 1; 2137 ext_data_len = 2 + 2 * ss->ssl3.dtlsSRTPCipherCount + 1;
2130 2138
2131 » if (append && maxBytes >= 4 + ext_data_len) { 2139 if (append && maxBytes >= 4 + ext_data_len) {
2132 » /* Extension type */ 2140 /* Extension type */
2133 » rv = ssl3_AppendHandshakeNumber(ss, ssl_use_srtp_xtn, 2); 2141 rv = ssl3_AppendHandshakeNumber(ss, ssl_use_srtp_xtn, 2);
2134 » if (rv != SECSuccess) return -1; 2142 if (rv != SECSuccess) return -1;
2135 » /* Length of extension data */ 2143 /* Length of extension data */
2136 » rv = ssl3_AppendHandshakeNumber(ss, ext_data_len, 2); 2144 rv = ssl3_AppendHandshakeNumber(ss, ext_data_len, 2);
2137 » if (rv != SECSuccess) return -1; 2145 if (rv != SECSuccess) return -1;
2138 » /* Length of the SRTP cipher list */ 2146 /* Length of the SRTP cipher list */
2139 » rv = ssl3_AppendHandshakeNumber(ss, 2147 rv = ssl3_AppendHandshakeNumber(ss,
2140 » » » » » 2 * ss->ssl3.dtlsSRTPCipherCount, 2148 2 * ss->ssl3.dtlsSRTPCipherCount,
2141 » » » » » 2); 2149 2);
2142 » if (rv != SECSuccess) return -1; 2150 if (rv != SECSuccess) return -1;
2143 » /* The SRTP ciphers */ 2151 /* The SRTP ciphers */
2144 » for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) { 2152 for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) {
2145 » » rv = ssl3_AppendHandshakeNumber(ss, 2153 rv = ssl3_AppendHandshakeNumber(ss,
2146 » » » » » » ss->ssl3.dtlsSRTPCiphers[i], 2154 ss->ssl3.dtlsSRTPCiphers[i],
2147 » » » » » » 2); 2155 2);
2148 » } 2156 }
2149 » /* Empty MKI value */ 2157 /* Empty MKI value */
2150 » ssl3_AppendHandshakeVariable(ss, NULL, 0, 1); 2158 ssl3_AppendHandshakeVariable(ss, NULL, 0, 1);
2151 2159
2152 » ss->xtnData.advertised[ss->xtnData.numAdvertised++] = 2160 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
2153 » » ssl_use_srtp_xtn; 2161 ssl_use_srtp_xtn;
2154 » } 2162 }
2155 2163
2156 » return 4 + ext_data_len; 2164 return 4 + ext_data_len;
2157 } 2165 }
2158 2166
2159 /* Server side */ 2167 /* Server side */
2160 if (append && maxBytes >= 9) { 2168 if (append && maxBytes >= 9) {
2161 » /* Extension type */ 2169 /* Extension type */
2162 » rv = ssl3_AppendHandshakeNumber(ss, ssl_use_srtp_xtn, 2); 2170 rv = ssl3_AppendHandshakeNumber(ss, ssl_use_srtp_xtn, 2);
2163 » if (rv != SECSuccess) return -1; 2171 if (rv != SECSuccess) return -1;
2164 » /* Length of extension data */ 2172 /* Length of extension data */
2165 » rv = ssl3_AppendHandshakeNumber(ss, 5, 2); 2173 rv = ssl3_AppendHandshakeNumber(ss, 5, 2);
2166 » if (rv != SECSuccess) return -1; 2174 if (rv != SECSuccess) return -1;
2167 » /* Length of the SRTP cipher list */ 2175 /* Length of the SRTP cipher list */
2168 » rv = ssl3_AppendHandshakeNumber(ss, 2, 2); 2176 rv = ssl3_AppendHandshakeNumber(ss, 2, 2);
2169 » if (rv != SECSuccess) return -1; 2177 if (rv != SECSuccess) return -1;
2170 » /* The selected cipher */ 2178 /* The selected cipher */
2171 » rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.dtlsSRTPCipherSuite, 2); 2179 rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.dtlsSRTPCipherSuite, 2);
2172 » if (rv != SECSuccess) return -1; 2180 if (rv != SECSuccess) return -1;
2173 » /* Empty MKI value */ 2181 /* Empty MKI value */
2174 » ssl3_AppendHandshakeVariable(ss, NULL, 0, 1); 2182 ssl3_AppendHandshakeVariable(ss, NULL, 0, 1);
2175 } 2183 }
2176 2184
2177 return 9; 2185 return 9;
2178 } 2186 }
2179 2187
2180 static SECStatus 2188 static SECStatus
2181 ssl3_HandleUseSRTPXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data) 2189 ssl3_HandleUseSRTPXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data)
2182 { 2190 {
2183 SECStatus rv; 2191 SECStatus rv;
2184 SECItem ciphers = {siBuffer, NULL, 0}; 2192 SECItem ciphers = {siBuffer, NULL, 0};
2185 PRUint16 i; 2193 PRUint16 i;
2186 unsigned int j; 2194 unsigned int j;
2187 PRUint16 cipher = 0; 2195 PRUint16 cipher = 0;
2188 PRBool found = PR_FALSE; 2196 PRBool found = PR_FALSE;
2189 SECItem litem; 2197 SECItem litem;
2190 2198
2191 if (!ss->sec.isServer) { 2199 if (!ss->sec.isServer) {
2192 » /* Client side */ 2200 /* Client side */
2193 » if (!data->data || !data->len) { 2201 if (!data->data || !data->len) {
2194 /* malformed */ 2202 /* malformed */
2195 return SECFailure; 2203 return SECFailure;
2196 » } 2204 }
2197 2205
2198 » /* Get the cipher list */ 2206 /* Get the cipher list */
2199 » rv = ssl3_ConsumeHandshakeVariable(ss, &ciphers, 2, 2207 rv = ssl3_ConsumeHandshakeVariable(ss, &ciphers, 2,
2200 » » » » » &data->data, &data->len); 2208 &data->data, &data->len);
2201 » if (rv != SECSuccess) { 2209 if (rv != SECSuccess) {
2202 » return SECFailure; 2210 return SECFailure;
2203 » } 2211 }
2204 » /* Now check that the number of ciphers listed is 1 (len = 2) */ 2212 /* Now check that the number of ciphers listed is 1 (len = 2) */
2205 » if (ciphers.len != 2) { 2213 if (ciphers.len != 2) {
2206 » return SECFailure; 2214 return SECFailure;
2207 » } 2215 }
2208 2216
2209 » /* Get the selected cipher */ 2217 /* Get the selected cipher */
2210 » cipher = (ciphers.data[0] << 8) | ciphers.data[1]; 2218 cipher = (ciphers.data[0] << 8) | ciphers.data[1];
2211 2219
2212 » /* Now check that this is one of the ciphers we offered */ 2220 /* Now check that this is one of the ciphers we offered */
2213 » for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) { 2221 for (i = 0; i < ss->ssl3.dtlsSRTPCipherCount; i++) {
2214 » if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) { 2222 if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) {
2215 » » found = PR_TRUE; 2223 found = PR_TRUE;
2216 » » break; 2224 break;
2217 » } 2225 }
2218 » } 2226 }
2219 2227
2220 » if (!found) { 2228 if (!found) {
2221 » return SECFailure; 2229 return SECFailure;
2222 » } 2230 }
2223 2231
2224 » /* Get the srtp_mki value */ 2232 /* Get the srtp_mki value */
2225 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 1, 2233 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 1,
2226 » » » » » &data->data, &data->len); 2234 &data->data, &data->len);
2227 if (rv != SECSuccess) { 2235 if (rv != SECSuccess) {
2228 return SECFailure; 2236 return SECFailure;
2229 } 2237 }
2230 2238
2231 » /* We didn't offer an MKI, so this must be 0 length */ 2239 /* We didn't offer an MKI, so this must be 0 length */
2232 » /* XXX RFC 5764 Section 4.1.3 says: 2240 /* XXX RFC 5764 Section 4.1.3 says:
2233 » * If the client detects a nonzero-length MKI in the server's 2241 * If the client detects a nonzero-length MKI in the server's
2234 » * response that is different than the one the client offered, 2242 * response that is different than the one the client offered,
2235 » * then the client MUST abort the handshake and SHOULD send an 2243 * then the client MUST abort the handshake and SHOULD send an
2236 » * invalid_parameter alert. 2244 * invalid_parameter alert.
2237 » * 2245 *
2238 » * Due to a limitation of the ssl3_HandleHelloExtensions function, 2246 * Due to a limitation of the ssl3_HandleHelloExtensions function,
2239 » * returning SECFailure here won't abort the handshake. It will 2247 * returning SECFailure here won't abort the handshake. It will
2240 » * merely cause the use_srtp extension to be not negotiated. We 2248 * merely cause the use_srtp extension to be not negotiated. We
2241 » * should fix this. See NSS bug 753136. 2249 * should fix this. See NSS bug 753136.
2242 » */ 2250 */
2243 » if (litem.len != 0) { 2251 if (litem.len != 0) {
2244 » return SECFailure; 2252 return SECFailure;
2245 » } 2253 }
2246 2254
2247 » if (data->len != 0) { 2255 if (data->len != 0) {
2248 /* malformed */ 2256 /* malformed */
2249 return SECFailure; 2257 return SECFailure;
2250 » } 2258 }
2251 2259
2252 » /* OK, this looks fine. */ 2260 /* OK, this looks fine. */
2253 » ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ssl_use_srtp_xtn; 2261 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ssl_use_srtp_xtn;
2254 » ss->ssl3.dtlsSRTPCipherSuite = cipher; 2262 ss->ssl3.dtlsSRTPCipherSuite = cipher;
2255 » return SECSuccess; 2263 return SECSuccess;
2256 } 2264 }
2257 2265
2258 /* Server side */ 2266 /* Server side */
2259 if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount) { 2267 if (!IS_DTLS(ss) || !ss->ssl3.dtlsSRTPCipherCount) {
2260 » /* Ignore the extension if we aren't doing DTLS or no DTLS-SRTP 2268 /* Ignore the extension if we aren't doing DTLS or no DTLS-SRTP
2261 » * preferences have been set. */ 2269 * preferences have been set. */
2262 » return SECSuccess; 2270 return SECSuccess;
2263 } 2271 }
2264 2272
2265 if (!data->data || data->len < 5) { 2273 if (!data->data || data->len < 5) {
2266 » /* malformed */ 2274 /* malformed */
2267 » return SECFailure; 2275 return SECFailure;
2268 } 2276 }
2269 2277
2270 /* Get the cipher list */ 2278 /* Get the cipher list */
2271 rv = ssl3_ConsumeHandshakeVariable(ss, &ciphers, 2, 2279 rv = ssl3_ConsumeHandshakeVariable(ss, &ciphers, 2,
2272 » » » » &data->data, &data->len); 2280 &data->data, &data->len);
2273 if (rv != SECSuccess) { 2281 if (rv != SECSuccess) {
2274 » return SECFailure; 2282 return SECFailure;
2275 } 2283 }
2276 /* Check that the list is even length */ 2284 /* Check that the list is even length */
2277 if (ciphers.len % 2) { 2285 if (ciphers.len % 2) {
2278 » return SECFailure; 2286 return SECFailure;
2279 } 2287 }
2280 2288
2281 /* Walk through the offered list and pick the most preferred of our 2289 /* Walk through the offered list and pick the most preferred of our
2282 * ciphers, if any */ 2290 * ciphers, if any */
2283 for (i = 0; !found && i < ss->ssl3.dtlsSRTPCipherCount; i++) { 2291 for (i = 0; !found && i < ss->ssl3.dtlsSRTPCipherCount; i++) {
2284 » for (j = 0; j + 1 < ciphers.len; j += 2) { 2292 for (j = 0; j + 1 < ciphers.len; j += 2) {
2285 » cipher = (ciphers.data[j] << 8) | ciphers.data[j + 1]; 2293 cipher = (ciphers.data[j] << 8) | ciphers.data[j + 1];
2286 » if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) { 2294 if (cipher == ss->ssl3.dtlsSRTPCiphers[i]) {
2287 » » found = PR_TRUE; 2295 found = PR_TRUE;
2288 » » break; 2296 break;
2289 » } 2297 }
2290 » } 2298 }
2291 } 2299 }
2292 2300
2293 /* Get the srtp_mki value */ 2301 /* Get the srtp_mki value */
2294 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 1, &data->data, &data->len); 2302 rv = ssl3_ConsumeHandshakeVariable(ss, &litem, 1, &data->data, &data->len);
2295 if (rv != SECSuccess) { 2303 if (rv != SECSuccess) {
2296 » return SECFailure; 2304 return SECFailure;
2297 } 2305 }
2298 2306
2299 if (data->len != 0) { 2307 if (data->len != 0) {
2300 » return SECFailure; /* Malformed */ 2308 return SECFailure; /* Malformed */
2301 } 2309 }
2302 2310
2303 /* Now figure out what to do */ 2311 /* Now figure out what to do */
2304 if (!found) { 2312 if (!found) {
2305 » /* No matching ciphers */ 2313 /* No matching ciphers */
2306 » return SECSuccess; 2314 return SECSuccess;
2307 } 2315 }
2308 2316
2309 /* OK, we have a valid cipher and we've selected it */ 2317 /* OK, we have a valid cipher and we've selected it */
2310 ss->ssl3.dtlsSRTPCipherSuite = cipher; 2318 ss->ssl3.dtlsSRTPCipherSuite = cipher;
2311 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ssl_use_srtp_xtn; 2319 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ssl_use_srtp_xtn;
2312 2320
2313 return ssl3_RegisterServerHelloExtensionSender(ss, ssl_use_srtp_xtn, 2321 return ssl3_RegisterServerHelloExtensionSender(ss, ssl_use_srtp_xtn,
2314 » » » » » » ssl3_SendUseSRTPXtn); 2322 ssl3_SendUseSRTPXtn);
2315 } 2323 }
2316 2324
2317 /* ssl3_ServerHandleSigAlgsXtn handles the signature_algorithms extension 2325 /* ssl3_ServerHandleSigAlgsXtn handles the signature_algorithms extension
2318 * from a client. 2326 * from a client.
2319 * See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ 2327 * See https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2320 static SECStatus 2328 static SECStatus
2321 ssl3_ServerHandleSigAlgsXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data) 2329 ssl3_ServerHandleSigAlgsXtn(sslSocket * ss, PRUint16 ex_type, SECItem *data)
2322 { 2330 {
2323 SECStatus rv; 2331 SECStatus rv;
2324 SECItem algorithms; 2332 SECItem algorithms;
2325 const unsigned char *b; 2333 const unsigned char *b;
2326 unsigned int numAlgorithms, i; 2334 unsigned int numAlgorithms, i, j;
2327 2335
2328 /* Ignore this extension if we aren't doing TLS 1.2 or greater. */ 2336 /* Ignore this extension if we aren't doing TLS 1.2 or greater. */
2329 if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2) { 2337 if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2) {
2330 » return SECSuccess; 2338 return SECSuccess;
2331 } 2339 }
2332 2340
2333 /* Keep track of negotiated extensions. */ 2341 /* Keep track of negotiated extensions. */
2334 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 2342 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
2335 2343
2336 rv = ssl3_ConsumeHandshakeVariable(ss, &algorithms, 2, &data->data, 2344 rv = ssl3_ConsumeHandshakeVariable(ss, &algorithms, 2, &data->data,
2337 » » » » &data->len); 2345 &data->len);
2338 if (rv != SECSuccess) { 2346 if (rv != SECSuccess) {
2339 » return SECFailure; 2347 return SECFailure;
2340 } 2348 }
2341 /* Trailing data, empty value, or odd-length value is invalid. */ 2349 /* Trailing data, empty value, or odd-length value is invalid. */
2342 if (data->len != 0 || algorithms.len == 0 || (algorithms.len & 1) != 0) { 2350 if (data->len != 0 || algorithms.len == 0 || (algorithms.len & 1) != 0) {
2343 » PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO); 2351 PORT_SetError(SSL_ERROR_RX_MALFORMED_CLIENT_HELLO);
2344 » return SECFailure; 2352 return SECFailure;
2345 } 2353 }
2346 2354
2347 numAlgorithms = algorithms.len/2; 2355 numAlgorithms = algorithms.len/2;
2348 2356
2349 /* We don't care to process excessive numbers of algorithms. */ 2357 /* We don't care to process excessive numbers of algorithms. */
2350 if (numAlgorithms > 512) { 2358 if (numAlgorithms > 512) {
2351 » numAlgorithms = 512; 2359 numAlgorithms = 512;
2352 } 2360 }
2353 2361
2354 ss->ssl3.hs.clientSigAndHash = 2362 ss->ssl3.hs.clientSigAndHash =
2355 » PORT_NewArray(SSL3SignatureAndHashAlgorithm, numAlgorithms); 2363 PORT_NewArray(SSL3SignatureAndHashAlgorithm, numAlgorithms);
2356 if (!ss->ssl3.hs.clientSigAndHash) { 2364 if (!ss->ssl3.hs.clientSigAndHash) {
2357 » return SECFailure; 2365 return SECFailure;
2358 } 2366 }
2359 ss->ssl3.hs.numClientSigAndHash = 0; 2367 ss->ssl3.hs.numClientSigAndHash = 0;
2360 2368
2361 b = algorithms.data; 2369 b = algorithms.data;
2362 for (i = 0; i < numAlgorithms; i++) { 2370 for (i = j = 0; i < numAlgorithms; i++) {
2363 » unsigned char tls_hash = *(b++); 2371 unsigned char tls_hash = *(b++);
2364 » unsigned char tls_sig = *(b++); 2372 unsigned char tls_sig = *(b++);
2365 » SECOidTag hash = ssl3_TLSHashAlgorithmToOID(tls_hash); 2373 SECOidTag hash = ssl3_TLSHashAlgorithmToOID(tls_hash);
2366 2374
2367 » if (hash == SEC_OID_UNKNOWN) { 2375 if (hash == SEC_OID_UNKNOWN) {
2368 » /* We ignore formats that we don't understand. */ 2376 /* We ignore formats that we don't understand. */
2369 » continue; 2377 continue;
2370 » } 2378 }
2371 » /* tls_sig support will be checked later in 2379 /* tls_sig support will be checked later in
2372 » * ssl3_PickSignatureHashAlgorithm. */ 2380 * ssl3_PickSignatureHashAlgorithm. */
2373 » ss->ssl3.hs.clientSigAndHash[i].hashAlg = hash; 2381 ss->ssl3.hs.clientSigAndHash[j].hashAlg = hash;
2374 » ss->ssl3.hs.clientSigAndHash[i].sigAlg = tls_sig; 2382 ss->ssl3.hs.clientSigAndHash[j].sigAlg = tls_sig;
2375 » ss->ssl3.hs.numClientSigAndHash++; 2383 ++j;
2384 ++ss->ssl3.hs.numClientSigAndHash;
2376 } 2385 }
2377 2386
2378 if (!ss->ssl3.hs.numClientSigAndHash) { 2387 if (!ss->ssl3.hs.numClientSigAndHash) {
2379 » /* We didn't understand any of the client's requested signature 2388 /* We didn't understand any of the client's requested signature
2380 » * formats. We'll use the defaults. */ 2389 * formats. We'll use the defaults. */
2381 » PORT_Free(ss->ssl3.hs.clientSigAndHash); 2390 PORT_Free(ss->ssl3.hs.clientSigAndHash);
2382 » ss->ssl3.hs.clientSigAndHash = NULL; 2391 ss->ssl3.hs.clientSigAndHash = NULL;
2383 } 2392 }
2384 2393
2385 return SECSuccess; 2394 return SECSuccess;
2386 } 2395 }
2387 2396
2388 /* ssl3_ClientSendSigAlgsXtn sends the signature_algorithm extension for TLS 2397 /* ssl3_ClientSendSigAlgsXtn sends the signature_algorithm extension for TLS
2389 * 1.2 ClientHellos. */ 2398 * 1.2 ClientHellos. */
2390 static PRInt32 2399 static PRInt32
2391 ssl3_ClientSendSigAlgsXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes) 2400 ssl3_ClientSendSigAlgsXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes)
2392 { 2401 {
2393 static const unsigned char signatureAlgorithms[] = { 2402 static const unsigned char signatureAlgorithms[] = {
2394 » /* This block is the contents of our signature_algorithms extension, in 2403 /* This block is the contents of our signature_algorithms extension, in
2395 » * wire format. See 2404 * wire format. See
2396 » * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ 2405 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2397 » tls_hash_sha256, tls_sig_rsa, 2406 tls_hash_sha256, tls_sig_rsa,
2398 » tls_hash_sha384, tls_sig_rsa, 2407 tls_hash_sha384, tls_sig_rsa,
2399 » tls_hash_sha1, tls_sig_rsa, 2408 tls_hash_sha1, tls_sig_rsa,
2400 #ifdef NSS_ENABLE_ECC 2409 #ifndef NSS_DISABLE_ECC
2401 » tls_hash_sha256, tls_sig_ecdsa, 2410 tls_hash_sha256, tls_sig_ecdsa,
2402 » tls_hash_sha384, tls_sig_ecdsa, 2411 tls_hash_sha384, tls_sig_ecdsa,
2403 » tls_hash_sha1, tls_sig_ecdsa, 2412 tls_hash_sha1, tls_sig_ecdsa,
2404 #endif 2413 #endif
2405 » tls_hash_sha256, tls_sig_dsa, 2414 tls_hash_sha256, tls_sig_dsa,
2406 » tls_hash_sha1, tls_sig_dsa, 2415 tls_hash_sha1, tls_sig_dsa,
2407 }; 2416 };
2408 PRInt32 extension_length; 2417 PRInt32 extension_length;
2409 2418
2410 if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2) { 2419 if (ss->version < SSL_LIBRARY_VERSION_TLS_1_2) {
2411 » return 0; 2420 return 0;
2412 } 2421 }
2413 2422
2414 extension_length = 2423 extension_length =
2415 » 2 /* extension type */ + 2424 2 /* extension type */ +
2416 » 2 /* extension length */ + 2425 2 /* extension length */ +
2417 » 2 /* supported_signature_algorithms length */ + 2426 2 /* supported_signature_algorithms length */ +
2418 » sizeof(signatureAlgorithms); 2427 sizeof(signatureAlgorithms);
2419 2428
2420 if (append && maxBytes >= extension_length) { 2429 if (append && maxBytes >= extension_length) {
2421 » SECStatus rv; 2430 SECStatus rv;
2422 » rv = ssl3_AppendHandshakeNumber(ss, ssl_signature_algorithms_xtn, 2); 2431 rv = ssl3_AppendHandshakeNumber(ss, ssl_signature_algorithms_xtn, 2);
2423 » if (rv != SECSuccess) 2432 if (rv != SECSuccess)
2424 » goto loser; 2433 goto loser;
2425 » rv = ssl3_AppendHandshakeNumber(ss, extension_length - 4, 2); 2434 rv = ssl3_AppendHandshakeNumber(ss, extension_length - 4, 2);
2426 » if (rv != SECSuccess) 2435 if (rv != SECSuccess)
2427 » goto loser; 2436 goto loser;
2428 » rv = ssl3_AppendHandshakeVariable(ss, signatureAlgorithms, 2437 rv = ssl3_AppendHandshakeVariable(ss, signatureAlgorithms,
2429 » » » » » sizeof(signatureAlgorithms), 2); 2438 sizeof(signatureAlgorithms), 2);
2430 » if (rv != SECSuccess) 2439 if (rv != SECSuccess)
2431 » goto loser; 2440 goto loser;
2432 » ss->xtnData.advertised[ss->xtnData.numAdvertised++] = 2441 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
2433 » » ssl_signature_algorithms_xtn; 2442 ssl_signature_algorithms_xtn;
2434 } else if (maxBytes < extension_length) { 2443 } else if (maxBytes < extension_length) {
2435 » PORT_Assert(0); 2444 PORT_Assert(0);
2436 » return 0; 2445 return 0;
2437 } 2446 }
2438 2447
2439 return extension_length; 2448 return extension_length;
2440 2449
2441 loser: 2450 loser:
2442 return -1; 2451 return -1;
2443 } 2452 }
2444 2453
2445 unsigned int 2454 unsigned int
2446 ssl3_CalculatePaddingExtensionLength(unsigned int clientHelloLength) 2455 ssl3_CalculatePaddingExtensionLength(unsigned int clientHelloLength)
2447 { 2456 {
2448 unsigned int recordLength = 1 /* handshake message type */ + 2457 unsigned int recordLength = 1 /* handshake message type */ +
2449 » » » » 3 /* handshake message length */ + 2458 3 /* handshake message length */ +
2450 » » » » clientHelloLength; 2459 clientHelloLength;
2451 unsigned int extensionLength; 2460 unsigned int extensionLength;
2452 2461
2453 if (recordLength < 256 || recordLength >= 512) { 2462 if (recordLength < 256 || recordLength >= 512) {
2454 » return 0; 2463 return 0;
2455 } 2464 }
2456 2465
2457 extensionLength = 512 - recordLength; 2466 extensionLength = 512 - recordLength;
2458 /* Extensions take at least four bytes to encode. Always include at least 2467 /* Extensions take at least four bytes to encode. */
2459 * one byte of data if including the extension. WebSphere Application Server 2468 if (extensionLength < 4) {
2460 * 7.0 is intolerant to the last extension being zero-length. */ 2469 extensionLength = 4;
2461 if (extensionLength < 4 + 1) {
2462 » extensionLength = 4 + 1;
2463 } 2470 }
2464 2471
2465 return extensionLength; 2472 return extensionLength;
2466 } 2473 }
2467 2474
2468 /* ssl3_AppendPaddingExtension possibly adds an extension which ensures that a 2475 /* ssl3_AppendPaddingExtension possibly adds an extension which ensures that a
2469 * ClientHello record is either < 256 bytes or is >= 512 bytes. This ensures 2476 * ClientHello record is either < 256 bytes or is >= 512 bytes. This ensures
2470 * that we don't trigger bugs in F5 products. */ 2477 * that we don't trigger bugs in F5 products. */
2471 PRInt32 2478 PRInt32
2472 ssl3_AppendPaddingExtension(sslSocket *ss, unsigned int extensionLen, 2479 ssl3_AppendPaddingExtension(sslSocket *ss, unsigned int extensionLen,
2473 » » » PRUint32 maxBytes) 2480 PRUint32 maxBytes)
2474 { 2481 {
2475 unsigned int paddingLen = extensionLen - 4; 2482 unsigned int paddingLen = extensionLen - 4;
2476 static unsigned char padding[256]; 2483 static unsigned char padding[256];
2477 2484
2478 if (extensionLen == 0) { 2485 if (extensionLen == 0) {
2479 » return 0; 2486 return 0;
2480 } 2487 }
2481 2488
2482 if (extensionLen < 4 || 2489 if (extensionLen < 4 ||
2483 » extensionLen > maxBytes || 2490 extensionLen > maxBytes ||
2484 » paddingLen > sizeof(padding)) { 2491 paddingLen > sizeof(padding)) {
2485 » PORT_Assert(0); 2492 PORT_Assert(0);
2486 » return -1; 2493 return -1;
2487 } 2494 }
2488 2495
2489 if (SECSuccess != ssl3_AppendHandshakeNumber(ss, ssl_padding_xtn, 2)) 2496 if (SECSuccess != ssl3_AppendHandshakeNumber(ss, ssl_padding_xtn, 2))
2490 » return -1; 2497 return -1;
2491 if (SECSuccess != ssl3_AppendHandshakeNumber(ss, paddingLen, 2)) 2498 if (SECSuccess != ssl3_AppendHandshakeNumber(ss, paddingLen, 2))
2492 » return -1; 2499 return -1;
2493 if (SECSuccess != ssl3_AppendHandshake(ss, padding, paddingLen)) 2500 if (SECSuccess != ssl3_AppendHandshake(ss, padding, paddingLen))
2494 » return -1; 2501 return -1;
2495 2502
2496 return extensionLen; 2503 return extensionLen;
2497 } 2504 }
2498 2505
2506 /* ssl3_ClientSendDraftVersionXtn sends the TLS 1.3 temporary draft
2507 * version extension.
2508 * TODO(ekr@rtfm.com): Remove when TLS 1.3 is published. */
2509 static PRInt32
2510 ssl3_ClientSendDraftVersionXtn(sslSocket * ss, PRBool append, PRUint32 maxBytes)
2511 {
2512 PRInt32 extension_length;
2513
2514 if (ss->version != SSL_LIBRARY_VERSION_TLS_1_3) {
2515 return 0;
2516 }
2517
2518 extension_length = 6; /* Type + length + number */
2519 if (append && maxBytes >= extension_length) {
2520 SECStatus rv;
2521 rv = ssl3_AppendHandshakeNumber(ss, ssl_tls13_draft_version_xtn, 2);
2522 if (rv != SECSuccess)
2523 goto loser;
2524 rv = ssl3_AppendHandshakeNumber(ss, extension_length - 4, 2);
2525 if (rv != SECSuccess)
2526 goto loser;
2527 rv = ssl3_AppendHandshakeNumber(ss, TLS_1_3_DRAFT_VERSION, 2);
2528 if (rv != SECSuccess)
2529 goto loser;
2530 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
2531 ssl_tls13_draft_version_xtn;
2532 } else if (maxBytes < extension_length) {
2533 PORT_Assert(0);
2534 return 0;
2535 }
2536
2537 return extension_length;
2538
2539 loser:
2540 return -1;
2541 }
2542
2543 /* ssl3_ServerHandleDraftVersionXtn handles the TLS 1.3 temporary draft
2544 * version extension.
2545 * TODO(ekr@rtfm.com): Remove when TLS 1.3 is published. */
2546 static SECStatus
2547 ssl3_ServerHandleDraftVersionXtn(sslSocket * ss, PRUint16 ex_type,
2548 SECItem *data)
2549 {
2550 PRInt32 draft_version;
2551
2552 /* Ignore this extension if we aren't doing TLS 1.3 */
2553 if (ss->version != SSL_LIBRARY_VERSION_TLS_1_3) {
2554 return SECSuccess;
2555 }
2556
2557 if (data->len != 2)
2558 goto loser;
2559
2560 /* Get the draft version out of the handshake */
2561 draft_version = ssl3_ConsumeHandshakeNumber(ss, 2,
2562 &data->data, &data->len);
2563 if (draft_version < 0) {
2564 goto loser;
2565 }
2566
2567 /* Keep track of negotiated extensions. */
2568 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
2569
2570 /* Compare the version */
2571 if (draft_version != TLS_1_3_DRAFT_VERSION) {
2572 SSL_TRC(30, ("%d: SSL3[%d]: Incompatible version of TLS 1.3 (%d), "
2573 "expected %d",
2574 SSL_GETPID(), ss->fd, draft_version, TLS_1_3_DRAFT_VERSION) );
2575 goto loser;
2576 }
2577
2578 return SECSuccess;
2579
2580 loser:
2581 /*
2582 * Incompatible/broken TLS 1.3 implementation. Fall back to TLS 1.2.
2583 * TODO(ekr@rtfm.com): It's not entirely clear it's safe to roll back
2584 * here. Need to double-check.
2585 * TODO(ekr@rtfm.com): Currently we fall back even on broken extensions.
2586 * because SECFailure does not cause handshake failures. See bug
2587 * 753136.
2588 */
2589 SSL_TRC(30, ("%d: SSL3[%d]: Rolling back to TLS 1.2", SSL_GETPID(), ss->fd)) ;
2590 ss->version = SSL_LIBRARY_VERSION_TLS_1_2;
2591
2592 return SECSuccess;
2593 }
2594
2499 /* ssl3_ClientSendSignedCertTimestampXtn sends the signed_certificate_timestamp 2595 /* ssl3_ClientSendSignedCertTimestampXtn sends the signed_certificate_timestamp
2500 * extension for TLS ClientHellos. */ 2596 * extension for TLS ClientHellos. */
2501 static PRInt32 2597 static PRInt32
2502 ssl3_ClientSendSignedCertTimestampXtn(sslSocket *ss, PRBool append, 2598 ssl3_ClientSendSignedCertTimestampXtn(sslSocket *ss, PRBool append,
2503 » » » » PRUint32 maxBytes) 2599 PRUint32 maxBytes)
2504 { 2600 {
2505 PRInt32 extension_length = 2 /* extension_type */ + 2601 PRInt32 extension_length = 2 /* extension_type */ +
2506 » 2 /* length(extension_data) */; 2602 2 /* length(extension_data) */;
2507 2603
2508 /* Only send the extension if processing is enabled. */ 2604 /* Only send the extension if processing is enabled. */
2509 if (!ss->opt.enableSignedCertTimestamps) 2605 if (!ss->opt.enableSignedCertTimestamps)
2510 » return 0; 2606 return 0;
2511 2607
2512 if (append && maxBytes >= extension_length) { 2608 if (append && maxBytes >= extension_length) {
2513 » SECStatus rv; 2609 SECStatus rv;
2514 » /* extension_type */ 2610 /* extension_type */
2515 » rv = ssl3_AppendHandshakeNumber(ss, 2611 rv = ssl3_AppendHandshakeNumber(ss,
2516 » » » » » ssl_signed_certificate_timestamp_xtn, 2612 ssl_signed_certificate_timestamp_xtn,
2517 » » » » » 2); 2613 2);
2518 » if (rv != SECSuccess) 2614 if (rv != SECSuccess)
2519 » goto loser; 2615 goto loser;
2520 » /* zero length */ 2616 /* zero length */
2521 » rv = ssl3_AppendHandshakeNumber(ss, 0, 2); 2617 rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
2522 » if (rv != SECSuccess) 2618 if (rv != SECSuccess)
2523 » goto loser; 2619 goto loser;
2524 » ss->xtnData.advertised[ss->xtnData.numAdvertised++] = 2620 ss->xtnData.advertised[ss->xtnData.numAdvertised++] =
2525 » » ssl_signed_certificate_timestamp_xtn; 2621 ssl_signed_certificate_timestamp_xtn;
2526 } else if (maxBytes < extension_length) { 2622 } else if (maxBytes < extension_length) {
2527 » PORT_Assert(0); 2623 PORT_Assert(0);
2528 » return 0; 2624 return 0;
2529 } 2625 }
2530 2626
2531 return extension_length; 2627 return extension_length;
2532 loser: 2628 loser:
2533 return -1; 2629 return -1;
2534 } 2630 }
2535 2631
2536 static SECStatus 2632 static SECStatus
2537 ssl3_ClientHandleSignedCertTimestampXtn(sslSocket *ss, PRUint16 ex_type, 2633 ssl3_ClientHandleSignedCertTimestampXtn(sslSocket *ss, PRUint16 ex_type,
2538 » » » » » SECItem *data) 2634 SECItem *data)
2539 { 2635 {
2540 /* We do not yet know whether we'll be resuming a session or creating 2636 /* We do not yet know whether we'll be resuming a session or creating
2541 * a new one, so we keep a pointer to the data in the TLSExtensionData 2637 * a new one, so we keep a pointer to the data in the TLSExtensionData
2542 * structure. This pointer is only valid in the scope of 2638 * structure. This pointer is only valid in the scope of
2543 * ssl3_HandleServerHello, and, if not resuming a session, the data is 2639 * ssl3_HandleServerHello, and, if not resuming a session, the data is
2544 * copied once a new session structure has been set up. 2640 * copied once a new session structure has been set up.
2545 * All parsing is currently left to the application and we accept 2641 * All parsing is currently left to the application and we accept
2546 * everything, including empty data. 2642 * everything, including empty data.
2547 */ 2643 */
2548 SECItem *scts = &ss->xtnData.signedCertTimestamps; 2644 SECItem *scts = &ss->xtnData.signedCertTimestamps;
2549 PORT_Assert(!scts->data && !scts->len); 2645 PORT_Assert(!scts->data && !scts->len);
2550 2646
2551 if (!data->len) { 2647 if (!data->len) {
2552 » /* Empty extension data: RFC 6962 mandates non-empty contents. */ 2648 /* Empty extension data: RFC 6962 mandates non-empty contents. */
2553 » return SECFailure; 2649 return SECFailure;
2554 } 2650 }
2555 *scts = *data; 2651 *scts = *data;
2556 /* Keep track of negotiated extensions. */ 2652 /* Keep track of negotiated extensions. */
2557 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type; 2653 ss->xtnData.negotiated[ss->xtnData.numNegotiated++] = ex_type;
2558 return SECSuccess; 2654 return SECSuccess;
2559 } 2655 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698