/src/nss/lib/ssl/tls13con.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * TLS 1.3 Protocol |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
8 | | |
9 | | #include "sslt.h" |
10 | | #include "stdarg.h" |
11 | | #include "cert.h" |
12 | | #include "ssl.h" |
13 | | #include "keyhi.h" |
14 | | #include "pk11func.h" |
15 | | #include "prerr.h" |
16 | | #include "secitem.h" |
17 | | #include "secmod.h" |
18 | | #include "sslimpl.h" |
19 | | #include "sslproto.h" |
20 | | #include "sslerr.h" |
21 | | #include "ssl3exthandle.h" |
22 | | #include "tls13hkdf.h" |
23 | | #include "tls13con.h" |
24 | | #include "tls13err.h" |
25 | | #include "tls13ech.h" |
26 | | #include "tls13exthandle.h" |
27 | | #include "tls13hashstate.h" |
28 | | #include "tls13subcerts.h" |
29 | | #include "tls13psk.h" |
30 | | |
31 | | static SECStatus tls13_SetCipherSpec(sslSocket *ss, PRUint16 epoch, |
32 | | SSLSecretDirection install, |
33 | | PRBool deleteSecret); |
34 | | static SECStatus tls13_SendServerHelloSequence(sslSocket *ss); |
35 | | static SECStatus tls13_SendEncryptedExtensions(sslSocket *ss); |
36 | | static void tls13_SetKeyExchangeType(sslSocket *ss, const sslNamedGroupDef *group); |
37 | | static SECStatus tls13_HandleClientKeyShare(sslSocket *ss, |
38 | | TLS13KeyShareEntry *peerShare); |
39 | | static SECStatus tls13_SendHelloRetryRequest( |
40 | | sslSocket *ss, const sslNamedGroupDef *selectedGroup, |
41 | | const PRUint8 *token, unsigned int tokenLen); |
42 | | |
43 | | static SECStatus tls13_HandleServerKeyShare(sslSocket *ss); |
44 | | static SECStatus tls13_HandleEncryptedExtensions(sslSocket *ss, PRUint8 *b, |
45 | | PRUint32 length); |
46 | | static SECStatus tls13_SendCertificate(sslSocket *ss); |
47 | | static SECStatus tls13_HandleCertificateDecode( |
48 | | sslSocket *ss, PRUint8 *b, PRUint32 length); |
49 | | static SECStatus tls13_HandleCertificate( |
50 | | sslSocket *ss, PRUint8 *b, PRUint32 length, PRBool alreadyHashed); |
51 | | static SECStatus tls13_ReinjectHandshakeTranscript(sslSocket *ss); |
52 | | static SECStatus tls13_SendCertificateRequest(sslSocket *ss); |
53 | | static SECStatus tls13_HandleCertificateRequest(sslSocket *ss, PRUint8 *b, |
54 | | PRUint32 length); |
55 | | static SECStatus |
56 | | tls13_SendCertificateVerify(sslSocket *ss, SECKEYPrivateKey *privKey); |
57 | | static SECStatus tls13_HandleCertificateVerify( |
58 | | sslSocket *ss, PRUint8 *b, PRUint32 length); |
59 | | static SECStatus tls13_RecoverWrappedSharedSecret(sslSocket *ss, |
60 | | sslSessionID *sid); |
61 | | static SECStatus |
62 | | tls13_DeriveSecretWrap(sslSocket *ss, PK11SymKey *key, |
63 | | const char *prefix, |
64 | | const char *suffix, |
65 | | const char *keylogLabel, |
66 | | PK11SymKey **dest); |
67 | | SECStatus |
68 | | tls13_DeriveSecret(sslSocket *ss, PK11SymKey *key, |
69 | | const char *label, |
70 | | unsigned int labelLen, |
71 | | const SSL3Hashes *hashes, |
72 | | PK11SymKey **dest, |
73 | | SSLHashType hash); |
74 | | static SECStatus tls13_SendEndOfEarlyData(sslSocket *ss); |
75 | | static SECStatus tls13_HandleEndOfEarlyData(sslSocket *ss, const PRUint8 *b, |
76 | | PRUint32 length); |
77 | | static SECStatus tls13_MaybeHandleSuppressedEndOfEarlyData(sslSocket *ss); |
78 | | static SECStatus tls13_SendFinished(sslSocket *ss, PK11SymKey *baseKey); |
79 | | static SECStatus tls13_ComputePskBinderHash(sslSocket *ss, PRUint8 *b, size_t length, |
80 | | SSL3Hashes *hashes, SSLHashType type); |
81 | | static SECStatus tls13_VerifyFinished(sslSocket *ss, SSLHandshakeType message, |
82 | | PK11SymKey *secret, |
83 | | PRUint8 *b, PRUint32 length, |
84 | | const SSL3Hashes *hashes); |
85 | | static SECStatus tls13_ClientHandleFinished(sslSocket *ss, |
86 | | PRUint8 *b, PRUint32 length); |
87 | | static SECStatus tls13_ServerHandleFinished(sslSocket *ss, |
88 | | PRUint8 *b, PRUint32 length); |
89 | | static SECStatus tls13_SendNewSessionTicket(sslSocket *ss, |
90 | | const PRUint8 *appToken, |
91 | | unsigned int appTokenLen); |
92 | | static SECStatus tls13_HandleNewSessionTicket(sslSocket *ss, PRUint8 *b, |
93 | | PRUint32 length); |
94 | | static SECStatus tls13_ComputeEarlySecretsWithPsk(sslSocket *ss); |
95 | | static SECStatus tls13_ComputeHandshakeSecrets(sslSocket *ss); |
96 | | static SECStatus tls13_ComputeApplicationSecrets(sslSocket *ss); |
97 | | static SECStatus tls13_ComputeFinalSecrets(sslSocket *ss); |
98 | | static SECStatus tls13_ComputeFinished( |
99 | | sslSocket *ss, PK11SymKey *baseKey, SSLHashType hashType, |
100 | | const SSL3Hashes *hashes, PRBool sending, PRUint8 *output, |
101 | | unsigned int *outputLen, unsigned int maxOutputLen); |
102 | | static SECStatus tls13_SendClientSecondRound(sslSocket *ss); |
103 | | static SECStatus tls13_SendClientSecondFlight(sslSocket *ss); |
104 | | static SECStatus tls13_FinishHandshake(sslSocket *ss); |
105 | | |
106 | | const char kHkdfLabelClient[] = "c"; |
107 | | const char kHkdfLabelServer[] = "s"; |
108 | | const char kHkdfLabelDerivedSecret[] = "derived"; |
109 | | const char kHkdfLabelResPskBinderKey[] = "res binder"; |
110 | | const char kHkdfLabelExtPskBinderKey[] = "ext binder"; |
111 | | const char kHkdfLabelEarlyTrafficSecret[] = "e traffic"; |
112 | | const char kHkdfLabelEarlyExporterSecret[] = "e exp master"; |
113 | | const char kHkdfLabelHandshakeTrafficSecret[] = "hs traffic"; |
114 | | const char kHkdfLabelApplicationTrafficSecret[] = "ap traffic"; |
115 | | const char kHkdfLabelFinishedSecret[] = "finished"; |
116 | | const char kHkdfLabelResumptionMasterSecret[] = "res master"; |
117 | | const char kHkdfLabelExporterMasterSecret[] = "exp master"; |
118 | | const char kHkdfLabelResumption[] = "resumption"; |
119 | | const char kHkdfLabelTrafficUpdate[] = "traffic upd"; |
120 | | const char kHkdfPurposeKey[] = "key"; |
121 | | const char kHkdfPurposeSn[] = "sn"; |
122 | | const char kHkdfPurposeIv[] = "iv"; |
123 | | |
124 | | const char keylogLabelClientEarlyTrafficSecret[] = "CLIENT_EARLY_TRAFFIC_SECRET"; |
125 | | const char keylogLabelClientHsTrafficSecret[] = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; |
126 | | const char keylogLabelServerHsTrafficSecret[] = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; |
127 | | const char keylogLabelClientTrafficSecret[] = "CLIENT_TRAFFIC_SECRET_0"; |
128 | | const char keylogLabelServerTrafficSecret[] = "SERVER_TRAFFIC_SECRET_0"; |
129 | | const char keylogLabelEarlyExporterSecret[] = "EARLY_EXPORTER_SECRET"; |
130 | | const char keylogLabelExporterSecret[] = "EXPORTER_SECRET"; |
131 | | |
132 | | /* Belt and suspenders in case we ever add a TLS 1.4. */ |
133 | | PR_STATIC_ASSERT(SSL_LIBRARY_VERSION_MAX_SUPPORTED <= |
134 | | SSL_LIBRARY_VERSION_TLS_1_3); |
135 | | |
136 | | void |
137 | | tls13_FatalError(sslSocket *ss, PRErrorCode prError, SSL3AlertDescription desc) |
138 | 2.18k | { |
139 | 2.18k | PORT_Assert(desc != internal_error); /* These should never happen */ |
140 | 2.18k | (void)SSL3_SendAlert(ss, alert_fatal, desc); |
141 | 2.18k | PORT_SetError(prError); |
142 | 2.18k | } |
143 | | |
144 | | #ifdef TRACE |
145 | | #define STATE_CASE(a) \ |
146 | 5.68k | case a: \ |
147 | 5.68k | return #a |
148 | | static char * |
149 | | tls13_HandshakeState(SSL3WaitState st) |
150 | 5.68k | { |
151 | 5.68k | switch (st) { |
152 | 555 | STATE_CASE(idle_handshake); |
153 | 0 | STATE_CASE(wait_client_hello); |
154 | 0 | STATE_CASE(wait_end_of_early_data); |
155 | 1.23k | STATE_CASE(wait_client_cert); |
156 | 0 | STATE_CASE(wait_client_key); |
157 | 0 | STATE_CASE(wait_cert_verify); |
158 | 0 | STATE_CASE(wait_change_cipher); |
159 | 1.52k | STATE_CASE(wait_finished); |
160 | 0 | STATE_CASE(wait_server_hello); |
161 | 0 | STATE_CASE(wait_certificate_status); |
162 | 0 | STATE_CASE(wait_server_cert); |
163 | 0 | STATE_CASE(wait_server_key); |
164 | 0 | STATE_CASE(wait_cert_request); |
165 | 0 | STATE_CASE(wait_hello_done); |
166 | 0 | STATE_CASE(wait_new_session_ticket); |
167 | 2.37k | STATE_CASE(wait_encrypted_extensions); |
168 | 0 | default: |
169 | 0 | break; |
170 | 5.68k | } |
171 | 0 | PORT_Assert(0); |
172 | 0 | return "unknown"; |
173 | 5.68k | } |
174 | | #endif |
175 | | |
176 | 135k | #define TLS13_WAIT_STATE_MASK 0x80 |
177 | | |
178 | | #define TLS13_BASE_WAIT_STATE(ws) (ws & ~TLS13_WAIT_STATE_MASK) |
179 | | /* We don't mask idle_handshake because other parts of the code use it*/ |
180 | 129k | #define TLS13_WAIT_STATE(ws) (((ws == idle_handshake) || (ws == wait_server_hello)) ? ws : ws | TLS13_WAIT_STATE_MASK) |
181 | | #define TLS13_CHECK_HS_STATE(ss, err, ...) \ |
182 | 123k | tls13_CheckHsState(ss, err, #err, __func__, __FILE__, __LINE__, \ |
183 | 123k | __VA_ARGS__, \ |
184 | 123k | wait_invalid) |
185 | | void |
186 | | tls13_SetHsState(sslSocket *ss, SSL3WaitState ws, |
187 | | const char *func, const char *file, int line) |
188 | 5.68k | { |
189 | 5.68k | #ifdef TRACE |
190 | 5.68k | const char *new_state_name = |
191 | 5.68k | tls13_HandshakeState(ws); |
192 | | |
193 | 5.68k | SSL_TRC(3, ("%d: TLS13[%d]: %s state change from %s->%s in %s (%s:%d)", |
194 | 5.68k | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
195 | 5.68k | tls13_HandshakeState(TLS13_BASE_WAIT_STATE(ss->ssl3.hs.ws)), |
196 | 5.68k | new_state_name, |
197 | 5.68k | func, file, line)); |
198 | 5.68k | #endif |
199 | | |
200 | 5.68k | ss->ssl3.hs.ws = TLS13_WAIT_STATE(ws); |
201 | 5.68k | } |
202 | | |
203 | | static PRBool |
204 | | tls13_InHsStateV(sslSocket *ss, va_list ap) |
205 | 123k | { |
206 | 123k | SSL3WaitState ws; |
207 | | |
208 | 124k | while ((ws = va_arg(ap, SSL3WaitState)) != wait_invalid) { |
209 | 123k | if (TLS13_WAIT_STATE(ws) == ss->ssl3.hs.ws) { |
210 | 123k | return PR_TRUE; |
211 | 123k | } |
212 | 123k | } |
213 | 348 | return PR_FALSE; |
214 | 123k | } |
215 | | |
216 | | PRBool |
217 | | tls13_InHsState(sslSocket *ss, ...) |
218 | 872 | { |
219 | 872 | PRBool found; |
220 | 872 | va_list ap; |
221 | | |
222 | 872 | va_start(ap, ss); |
223 | 872 | found = tls13_InHsStateV(ss, ap); |
224 | 872 | va_end(ap); |
225 | | |
226 | 872 | return found; |
227 | 872 | } |
228 | | |
229 | | static SECStatus |
230 | | tls13_CheckHsState(sslSocket *ss, int err, const char *error_name, |
231 | | const char *func, const char *file, int line, |
232 | | ...) |
233 | 123k | { |
234 | 123k | va_list ap; |
235 | 123k | va_start(ap, line); |
236 | 123k | if (tls13_InHsStateV(ss, ap)) { |
237 | 122k | va_end(ap); |
238 | 122k | return SECSuccess; |
239 | 122k | } |
240 | 123k | va_end(ap); |
241 | | |
242 | 114 | SSL_TRC(3, ("%d: TLS13[%d]: error %s state is (%s) at %s (%s:%d)", |
243 | 114 | SSL_GETPID(), ss->fd, |
244 | 114 | error_name, |
245 | 114 | tls13_HandshakeState(TLS13_BASE_WAIT_STATE(ss->ssl3.hs.ws)), |
246 | 114 | func, file, line)); |
247 | 114 | tls13_FatalError(ss, err, unexpected_message); |
248 | 114 | return SECFailure; |
249 | 123k | } |
250 | | |
251 | | PRBool |
252 | | tls13_IsPostHandshake(const sslSocket *ss) |
253 | 405k | { |
254 | 405k | return ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && ss->firstHsDone; |
255 | 405k | } |
256 | | |
257 | | SSLHashType |
258 | | tls13_GetHashForCipherSuite(ssl3CipherSuite suite) |
259 | 14 | { |
260 | 14 | const ssl3CipherSuiteDef *cipherDef = |
261 | 14 | ssl_LookupCipherSuiteDef(suite); |
262 | 14 | PORT_Assert(cipherDef); |
263 | 14 | if (!cipherDef) { |
264 | 0 | return ssl_hash_none; |
265 | 0 | } |
266 | 14 | return cipherDef->prf_hash; |
267 | 14 | } |
268 | | |
269 | | SSLHashType |
270 | | tls13_GetHash(const sslSocket *ss) |
271 | 723k | { |
272 | | /* suite_def may not be set yet when doing EPSK 0-Rtt. */ |
273 | 723k | if (!ss->ssl3.hs.suite_def) { |
274 | 0 | if (ss->xtnData.selectedPsk) { |
275 | 0 | return ss->xtnData.selectedPsk->hash; |
276 | 0 | } |
277 | | /* This should never happen. */ |
278 | 0 | PORT_Assert(0); |
279 | 0 | return ssl_hash_none; |
280 | 0 | } |
281 | | |
282 | | /* All TLS 1.3 cipher suites must have an explict PRF hash. */ |
283 | 723k | PORT_Assert(ss->ssl3.hs.suite_def->prf_hash != ssl_hash_none); |
284 | 723k | return ss->ssl3.hs.suite_def->prf_hash; |
285 | 723k | } |
286 | | |
287 | | SECStatus |
288 | | tls13_GetHashAndCipher(PRUint16 version, PRUint16 cipherSuite, |
289 | | SSLHashType *hash, const ssl3BulkCipherDef **cipher) |
290 | 5.45k | { |
291 | 5.45k | if (version < SSL_LIBRARY_VERSION_TLS_1_3) { |
292 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
293 | 0 | return SECFailure; |
294 | 0 | } |
295 | | |
296 | | // Lookup and check the suite. |
297 | 5.45k | SSLVersionRange vrange = { version, version }; |
298 | 5.45k | if (!ssl3_CipherSuiteAllowedForVersionRange(cipherSuite, &vrange)) { |
299 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
300 | 0 | return SECFailure; |
301 | 0 | } |
302 | 5.45k | const ssl3CipherSuiteDef *suiteDef = ssl_LookupCipherSuiteDef(cipherSuite); |
303 | 5.45k | const ssl3BulkCipherDef *cipherDef = ssl_GetBulkCipherDef(suiteDef); |
304 | 5.45k | if (cipherDef->type != type_aead) { |
305 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
306 | 0 | return SECFailure; |
307 | 0 | } |
308 | 5.45k | *hash = suiteDef->prf_hash; |
309 | 5.45k | if (cipher != NULL) { |
310 | 5.45k | *cipher = cipherDef; |
311 | 5.45k | } |
312 | 5.45k | return SECSuccess; |
313 | 5.45k | } |
314 | | |
315 | | unsigned int |
316 | | tls13_GetHashSizeForHash(SSLHashType hash) |
317 | 259k | { |
318 | 259k | switch (hash) { |
319 | 188k | case ssl_hash_sha256: |
320 | 188k | return 32; |
321 | 71.2k | case ssl_hash_sha384: |
322 | 71.2k | return 48; |
323 | 0 | default: |
324 | 0 | PORT_Assert(0); |
325 | 259k | } |
326 | 0 | return 32; |
327 | 259k | } |
328 | | |
329 | | unsigned int |
330 | | tls13_GetHashSize(const sslSocket *ss) |
331 | 151k | { |
332 | 151k | return tls13_GetHashSizeForHash(tls13_GetHash(ss)); |
333 | 151k | } |
334 | | |
335 | | static CK_MECHANISM_TYPE |
336 | | tls13_GetHmacMechanismFromHash(SSLHashType hashType) |
337 | 144k | { |
338 | 144k | switch (hashType) { |
339 | 122k | case ssl_hash_sha256: |
340 | 122k | return CKM_SHA256_HMAC; |
341 | 22.9k | case ssl_hash_sha384: |
342 | 22.9k | return CKM_SHA384_HMAC; |
343 | 0 | default: |
344 | 0 | PORT_Assert(0); |
345 | 144k | } |
346 | 0 | return CKM_SHA256_HMAC; |
347 | 144k | } |
348 | | |
349 | | static CK_MECHANISM_TYPE |
350 | | tls13_GetHmacMechanism(const sslSocket *ss) |
351 | 122k | { |
352 | 122k | return tls13_GetHmacMechanismFromHash(tls13_GetHash(ss)); |
353 | 122k | } |
354 | | |
355 | | SECStatus |
356 | | tls13_ComputeHash(sslSocket *ss, SSL3Hashes *hashes, |
357 | | const PRUint8 *buf, unsigned int len, |
358 | | SSLHashType hash) |
359 | 21.3k | { |
360 | 21.3k | SECStatus rv; |
361 | | |
362 | 21.3k | rv = PK11_HashBuf(ssl3_HashTypeToOID(hash), hashes->u.raw, buf, len); |
363 | 21.3k | if (rv != SECSuccess) { |
364 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
365 | 0 | return SECFailure; |
366 | 0 | } |
367 | 21.3k | hashes->len = tls13_GetHashSizeForHash(hash); |
368 | | |
369 | 21.3k | return SECSuccess; |
370 | 21.3k | } |
371 | | |
372 | | static SECStatus |
373 | | tls13_CreateKEMKeyPair(sslSocket *ss, const sslNamedGroupDef *groupDef, |
374 | | sslKeyPair **outKeyPair) |
375 | 13.7k | { |
376 | 13.7k | PORT_Assert(groupDef); |
377 | | |
378 | 13.7k | sslKeyPair *keyPair = NULL; |
379 | 13.7k | SECKEYPrivateKey *privKey = NULL; |
380 | 13.7k | SECKEYPublicKey *pubKey = NULL; |
381 | 13.7k | CK_MECHANISM_TYPE mechanism; |
382 | 13.7k | CK_NSS_KEM_PARAMETER_SET_TYPE paramSet; |
383 | | |
384 | 13.7k | switch (groupDef->name) { |
385 | 0 | case ssl_grp_kem_xyber768d00: |
386 | 0 | mechanism = CKM_NSS_KYBER_KEY_PAIR_GEN; |
387 | 0 | paramSet = CKP_NSS_KYBER_768_ROUND3; |
388 | 0 | break; |
389 | 13.7k | case ssl_grp_kem_mlkem768x25519: |
390 | 13.7k | case ssl_grp_kem_secp256r1mlkem768: |
391 | 13.7k | mechanism = CKM_ML_KEM_KEY_PAIR_GEN; |
392 | 13.7k | paramSet = CKP_ML_KEM_768; |
393 | 13.7k | break; |
394 | 49 | case ssl_grp_kem_secp384r1mlkem1024: |
395 | 49 | mechanism = CKM_ML_KEM_KEY_PAIR_GEN; |
396 | 49 | paramSet = CKP_ML_KEM_1024; |
397 | 49 | break; |
398 | 0 | default: |
399 | 0 | PORT_Assert(0); |
400 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
401 | 0 | return SECFailure; |
402 | 13.7k | } |
403 | | |
404 | 13.7k | PK11SlotInfo *slot = PK11_GetBestSlot(mechanism, ss->pkcs11PinArg); |
405 | 13.7k | if (!slot) { |
406 | 0 | goto loser; |
407 | 0 | } |
408 | | |
409 | | /* avoid pairwise check in non-FIPS mode */ |
410 | | /* the only difference between CKM_ML_KEM_KEY_PAIR_GEN and |
411 | | * CKM_NSS_ML_KEM_KEY_PAIR_GEN is the latter skips the pairwise consistency |
412 | | * check and is only supported by softoken */ |
413 | 13.7k | if ((mechanism == CKM_ML_KEM_KEY_PAIR_GEN) && !PK11_IsFIPS() && |
414 | 13.7k | PK11_DoesMechanism(slot, CKM_NSS_ML_KEM_KEY_PAIR_GEN)) { |
415 | 13.7k | mechanism = CKM_NSS_ML_KEM_KEY_PAIR_GEN; |
416 | 13.7k | } |
417 | | |
418 | 13.7k | privKey = PK11_GenerateKeyPairWithOpFlags(slot, mechanism, |
419 | 13.7k | ¶mSet, &pubKey, |
420 | 13.7k | PK11_ATTR_SESSION | PK11_ATTR_INSENSITIVE | PK11_ATTR_PUBLIC, |
421 | 13.7k | CKF_ENCAPSULATE | CKF_DECAPSULATE, |
422 | 13.7k | CKF_ENCAPSULATE | CKF_DECAPSULATE, |
423 | 13.7k | ss->pkcs11PinArg); |
424 | | |
425 | 13.7k | if (!privKey) { |
426 | 0 | privKey = PK11_GenerateKeyPairWithOpFlags(slot, mechanism, |
427 | 0 | ¶mSet, &pubKey, |
428 | 0 | PK11_ATTR_SESSION | PK11_ATTR_SENSITIVE | PK11_ATTR_PRIVATE, |
429 | 0 | CKF_ENCAPSULATE | CKF_DECAPSULATE, |
430 | 0 | CKF_ENCAPSULATE | CKF_DECAPSULATE, |
431 | 0 | ss->pkcs11PinArg); |
432 | 0 | } |
433 | | |
434 | 13.7k | PK11_FreeSlot(slot); |
435 | 13.7k | if (!privKey || !pubKey) { |
436 | 0 | goto loser; |
437 | 0 | } |
438 | | |
439 | 13.7k | keyPair = ssl_NewKeyPair(privKey, pubKey); |
440 | 13.7k | if (!keyPair) { |
441 | 0 | goto loser; |
442 | 0 | } |
443 | | |
444 | 13.7k | SSL_TRC(50, ("%d: SSL[%d]: Create Kyber ephemeral key %d", |
445 | 13.7k | SSL_GETPID(), ss ? ss->fd : NULL, groupDef->name)); |
446 | 13.7k | PRINT_BUF(50, (ss, "Public Key", pubKey->u.kyber.publicValue.data, |
447 | 13.7k | pubKey->u.kyber.publicValue.len)); |
448 | 13.7k | #ifdef TRACE |
449 | 13.7k | if (ssl_trace >= 50) { |
450 | 0 | SECItem d = { siBuffer, NULL, 0 }; |
451 | 0 | SECStatus rv = PK11_ReadRawAttribute(PK11_TypePrivKey, privKey, CKA_VALUE, &d); |
452 | 0 | if (rv == SECSuccess) { |
453 | 0 | PRINT_BUF(50, (ss, "Private Key", d.data, d.len)); |
454 | 0 | SECITEM_FreeItem(&d, PR_FALSE); |
455 | 0 | } else { |
456 | 0 | SSL_TRC(50, ("Error extracting private key")); |
457 | 0 | } |
458 | 0 | } |
459 | 13.7k | #endif |
460 | | |
461 | 13.7k | *outKeyPair = keyPair; |
462 | 13.7k | return SECSuccess; |
463 | | |
464 | 0 | loser: |
465 | 0 | SECKEY_DestroyPrivateKey(privKey); |
466 | 0 | SECKEY_DestroyPublicKey(pubKey); |
467 | 0 | ssl_MapLowLevelError(SEC_ERROR_KEYGEN_FAIL); |
468 | 0 | return SECFailure; |
469 | 13.7k | } |
470 | | |
471 | | /* only copy the ECDH component of an ephemeral KeyPair */ |
472 | | sslEphemeralKeyPair * |
473 | | tls13_CopyECDHKeyFromHybrid(sslEphemeralKeyPair *copyKeyPair, |
474 | | const sslNamedGroupDef *groupDef) |
475 | 0 | { |
476 | | /* We could use ssl_CopyEphemeralKeyPair here, but we would need to free |
477 | | * the KEM components. So we only copy the ECDH keys */ |
478 | 0 | sslEphemeralKeyPair *keyPair = PORT_ZNew(sslEphemeralKeyPair); |
479 | 0 | if (!keyPair) { |
480 | 0 | return NULL; |
481 | 0 | } |
482 | 0 | PR_INIT_CLIST(&keyPair->link); |
483 | 0 | keyPair->group = groupDef; |
484 | 0 | keyPair->keys = ssl_GetKeyPairRef(copyKeyPair->keys); |
485 | 0 | return keyPair; |
486 | 0 | } |
487 | | |
488 | | /* |
489 | | * find a hybrid key Pair they might contain the same ecdh key so we |
490 | | * can reuse them. Each ec group can map to more than one hybrid Pair |
491 | | */ |
492 | | sslEphemeralKeyPair * |
493 | | tls13_FindHybridKeyPair(sslSocket *ss, const sslNamedGroupDef *groupDef) |
494 | 2.36k | { |
495 | 2.36k | sslEphemeralKeyPair *hybridPair = NULL; |
496 | 2.36k | switch (groupDef->name) { |
497 | 734 | case ssl_grp_ec_secp256r1: |
498 | | /* future, this may be a loop to check multiple named groups */ |
499 | 734 | hybridPair = ssl_LookupEphemeralKeyPair(ss, |
500 | 734 | ssl_LookupNamedGroup(ssl_grp_kem_secp256r1mlkem768)); |
501 | 734 | break; |
502 | 609 | case ssl_grp_ec_secp384r1: |
503 | 609 | hybridPair = ssl_LookupEphemeralKeyPair(ss, |
504 | 609 | ssl_LookupNamedGroup(ssl_grp_kem_secp384r1mlkem1024)); |
505 | 609 | break; |
506 | 937 | case ssl_grp_ec_curve25519: { |
507 | | /* a loop to check multiple named groups */ |
508 | 937 | SSLNamedGroup gnames[] = { ssl_grp_kem_xyber768d00, |
509 | 937 | ssl_grp_kem_mlkem768x25519 }; |
510 | 2.81k | for (int i = 0; i < PR_ARRAY_SIZE(gnames); i++) { |
511 | 1.87k | hybridPair = ssl_LookupEphemeralKeyPair(ss, |
512 | 1.87k | ssl_LookupNamedGroup(gnames[i])); |
513 | 1.87k | if (hybridPair != NULL) { |
514 | 0 | break; |
515 | 0 | } |
516 | 1.87k | } |
517 | 937 | break; |
518 | 0 | } |
519 | 86 | default: |
520 | 86 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
521 | 86 | return NULL; |
522 | 2.36k | } |
523 | 2.28k | return hybridPair; |
524 | 2.36k | } |
525 | | |
526 | | SECStatus |
527 | | tls13_CreateKeyShare(sslSocket *ss, const sslNamedGroupDef *groupDef, |
528 | | sslEphemeralKeyPair **outKeyPair) |
529 | 19.6k | { |
530 | 19.6k | SECStatus rv; |
531 | 19.6k | const ssl3DHParams *params; |
532 | 19.6k | sslEphemeralKeyPair *keyPair = NULL; |
533 | 19.6k | const sslNamedGroupDef *ecGroup = NULL; |
534 | | |
535 | 19.6k | PORT_Assert(groupDef); |
536 | 19.6k | switch (groupDef->keaType) { |
537 | 14.5k | case ssl_kea_ecdh_hybrid: |
538 | 14.5k | switch (groupDef->name) { |
539 | 206 | case ssl_grp_kem_secp256r1mlkem768: |
540 | 206 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_secp256r1); |
541 | 206 | break; |
542 | 465 | case ssl_grp_kem_secp384r1mlkem1024: |
543 | 465 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_secp384r1); |
544 | 465 | break; |
545 | 0 | case ssl_grp_kem_xyber768d00: |
546 | 13.9k | case ssl_grp_kem_mlkem768x25519: |
547 | 13.9k | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_curve25519); |
548 | 13.9k | break; |
549 | 0 | default: |
550 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
551 | 0 | return SECFailure; |
552 | 14.5k | } |
553 | 14.5k | if (ecGroup == NULL) { |
554 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
555 | 0 | return SECFailure; |
556 | 0 | } |
557 | 14.5k | keyPair = ssl_LookupEphemeralKeyPair(ss, ecGroup); |
558 | 14.5k | if (keyPair) { |
559 | 0 | keyPair = ssl_CopyEphemeralKeyPair(keyPair); |
560 | 0 | } |
561 | 14.5k | if (!keyPair) { |
562 | 14.5k | rv = ssl_CreateECDHEphemeralKeyPair(ss, ecGroup, &keyPair); |
563 | 14.5k | if (rv != SECSuccess) { |
564 | 73 | return SECFailure; |
565 | 73 | } |
566 | 14.5k | } |
567 | 14.5k | keyPair->group = groupDef; |
568 | 14.5k | break; |
569 | 2.36k | case ssl_kea_ecdh: |
570 | 2.36k | keyPair = tls13_FindHybridKeyPair(ss, groupDef); |
571 | 2.36k | if (keyPair) { |
572 | 0 | keyPair = tls13_CopyECDHKeyFromHybrid(keyPair, groupDef); |
573 | 0 | } |
574 | 2.36k | if (!keyPair) { |
575 | 2.36k | rv = ssl_CreateECDHEphemeralKeyPair(ss, groupDef, &keyPair); |
576 | 2.36k | if (rv != SECSuccess) { |
577 | 26 | return SECFailure; |
578 | 26 | } |
579 | 2.36k | } |
580 | 2.34k | break; |
581 | 2.70k | case ssl_kea_dh: |
582 | 2.70k | params = ssl_GetDHEParams(groupDef); |
583 | 2.70k | PORT_Assert(params->name != ssl_grp_ffdhe_custom); |
584 | 2.70k | rv = ssl_CreateDHEKeyPair(groupDef, params, &keyPair); |
585 | 2.70k | if (rv != SECSuccess) { |
586 | 5 | return SECFailure; |
587 | 5 | } |
588 | 2.70k | break; |
589 | 2.70k | default: |
590 | 0 | PORT_Assert(0); |
591 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
592 | 0 | return SECFailure; |
593 | 19.6k | } |
594 | | |
595 | | // If we're creating an ECDH + KEM hybrid share and we're the client, then |
596 | | // we still need to generate the KEM key pair. Otherwise we're done. |
597 | 19.5k | if (groupDef->keaType == ssl_kea_ecdh_hybrid && !ss->sec.isServer) { |
598 | 13.7k | rv = tls13_CreateKEMKeyPair(ss, groupDef, &keyPair->kemKeys); |
599 | 13.7k | if (rv != SECSuccess) { |
600 | 0 | ssl_FreeEphemeralKeyPair(keyPair); |
601 | 0 | return SECFailure; |
602 | 0 | } |
603 | 13.7k | } |
604 | | |
605 | 19.5k | *outKeyPair = keyPair; |
606 | 19.5k | return SECSuccess; |
607 | 19.5k | } |
608 | | |
609 | | SECStatus |
610 | | tls13_AddKeyShare(sslSocket *ss, const sslNamedGroupDef *groupDef) |
611 | 19.6k | { |
612 | 19.6k | sslEphemeralKeyPair *keyPair = NULL; |
613 | 19.6k | SECStatus rv; |
614 | | |
615 | 19.6k | rv = tls13_CreateKeyShare(ss, groupDef, &keyPair); |
616 | 19.6k | if (rv != SECSuccess) { |
617 | 104 | return SECFailure; |
618 | 104 | } |
619 | 19.5k | PR_APPEND_LINK(&keyPair->link, &ss->ephemeralKeyPairs); |
620 | 19.5k | return SECSuccess; |
621 | 19.6k | } |
622 | | |
623 | | SECStatus |
624 | | SSL_SendAdditionalKeyShares(PRFileDesc *fd, unsigned int count) |
625 | 0 | { |
626 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
627 | 0 | if (!ss) { |
628 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
629 | 0 | return SECFailure; |
630 | 0 | } |
631 | | |
632 | 0 | ss->additionalShares = count; |
633 | 0 | return SECSuccess; |
634 | 0 | } |
635 | | |
636 | | /* |
637 | | * Generate shares for ECDHE and FFDHE. This picks the first enabled group of |
638 | | * the requisite type and creates a share for that. |
639 | | * |
640 | | * Called from ssl3_SendClientHello. |
641 | | */ |
642 | | SECStatus |
643 | | tls13_SetupClientHello(sslSocket *ss, sslClientHelloType chType) |
644 | 17.1k | { |
645 | 17.1k | unsigned int i; |
646 | 17.1k | SSL3Statistics *ssl3stats = SSL_GetStatistics(); |
647 | 17.1k | NewSessionTicket *session_ticket = NULL; |
648 | 17.1k | sslSessionID *sid = ss->sec.ci.sid; |
649 | 17.1k | unsigned int numShares = 0; |
650 | 17.1k | SECStatus rv; |
651 | | |
652 | 17.1k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
653 | 17.1k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
654 | | |
655 | 17.1k | rv = tls13_ClientSetupEch(ss, chType); |
656 | 17.1k | if (rv != SECSuccess) { |
657 | 17 | return SECFailure; |
658 | 17 | } |
659 | | |
660 | | /* Everything below here is only run on the first CH. */ |
661 | 17.1k | if (chType != client_hello_initial) { |
662 | 3.40k | return SECSuccess; |
663 | 3.40k | } |
664 | | |
665 | 13.7k | rv = tls13_ClientGreaseSetup(ss); |
666 | 13.7k | if (rv != SECSuccess) { |
667 | 0 | return SECFailure; |
668 | 0 | } |
669 | | |
670 | | /* Select the first enabled group. |
671 | | * TODO(ekr@rtfm.com): be smarter about offering the group |
672 | | * that the other side negotiated if we are resuming. */ |
673 | 13.7k | PORT_Assert(PR_CLIST_IS_EMPTY(&ss->ephemeralKeyPairs)); |
674 | 13.7k | for (i = 0; i < SSL_NAMED_GROUP_COUNT; ++i) { |
675 | 13.7k | if (!ss->namedGroupPreferences[i]) { |
676 | 0 | continue; |
677 | 0 | } |
678 | 13.7k | rv = tls13_AddKeyShare(ss, ss->namedGroupPreferences[i]); |
679 | 13.7k | if (rv != SECSuccess) { |
680 | 69 | return SECFailure; |
681 | 69 | } |
682 | 13.7k | if (++numShares > ss->additionalShares) { |
683 | 13.7k | break; |
684 | 13.7k | } |
685 | 13.7k | } |
686 | | |
687 | 13.7k | if (PR_CLIST_IS_EMPTY(&ss->ephemeralKeyPairs)) { |
688 | 0 | PORT_SetError(SSL_ERROR_NO_CIPHERS_SUPPORTED); |
689 | 0 | return SECFailure; |
690 | 0 | } |
691 | | |
692 | | /* Try to do stateless resumption, if we can. */ |
693 | 13.7k | if (sid->cached != never_cached && |
694 | 0 | sid->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
695 | | /* The caller must be holding sid->u.ssl3.lock for reading. */ |
696 | 0 | session_ticket = &sid->u.ssl3.locked.sessionTicket; |
697 | 0 | PORT_Assert(session_ticket && session_ticket->ticket.data); |
698 | |
|
699 | 0 | if (ssl_TicketTimeValid(ss, session_ticket)) { |
700 | 0 | ss->statelessResume = PR_TRUE; |
701 | 0 | } |
702 | |
|
703 | 0 | if (ss->statelessResume) { |
704 | 0 | PORT_Assert(ss->sec.ci.sid); |
705 | 0 | rv = tls13_RecoverWrappedSharedSecret(ss, ss->sec.ci.sid); |
706 | 0 | if (rv != SECSuccess) { |
707 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
708 | 0 | SSL_AtomicIncrementLong(&ssl3stats->sch_sid_cache_not_ok); |
709 | 0 | ssl_UncacheSessionID(ss); |
710 | 0 | ssl_FreeSID(ss->sec.ci.sid); |
711 | 0 | ss->sec.ci.sid = NULL; |
712 | 0 | return SECFailure; |
713 | 0 | } |
714 | | |
715 | 0 | ss->ssl3.hs.cipher_suite = ss->sec.ci.sid->u.ssl3.cipherSuite; |
716 | 0 | rv = ssl3_SetupCipherSuite(ss, PR_FALSE); |
717 | 0 | if (rv != SECSuccess) { |
718 | 0 | FATAL_ERROR(ss, PORT_GetError(), internal_error); |
719 | 0 | return SECFailure; |
720 | 0 | } |
721 | 0 | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)); |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | | /* Derive the binder keys if any PSKs. */ |
726 | 13.7k | if (!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)) { |
727 | | /* If an External PSK specified a suite, use that. */ |
728 | 6.88k | sslPsk *psk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks); |
729 | 6.88k | if (!ss->statelessResume && |
730 | 6.88k | psk->type == ssl_psk_external && |
731 | 6.88k | psk->zeroRttSuite != TLS_NULL_WITH_NULL_NULL) { |
732 | 0 | ss->ssl3.hs.cipher_suite = psk->zeroRttSuite; |
733 | 0 | } |
734 | | |
735 | 6.88k | rv = tls13_ComputeEarlySecretsWithPsk(ss); |
736 | 6.88k | if (rv != SECSuccess) { |
737 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
738 | 0 | return SECFailure; |
739 | 0 | } |
740 | 6.88k | } |
741 | | |
742 | 13.7k | tls13_EchKeyLog(ss); |
743 | 13.7k | return SECSuccess; |
744 | 13.7k | } |
745 | | |
746 | | static SECStatus |
747 | | tls13_ImportDHEKeyShare(SECKEYPublicKey *peerKey, |
748 | | PRUint8 *b, PRUint32 length, |
749 | | SECKEYPublicKey *pubKey) |
750 | 2.34k | { |
751 | 2.34k | SECStatus rv; |
752 | 2.34k | SECItem publicValue = { siBuffer, NULL, 0 }; |
753 | | |
754 | 2.34k | publicValue.data = b; |
755 | 2.34k | publicValue.len = length; |
756 | 2.34k | if (!ssl_IsValidDHEShare(&pubKey->u.dh.prime, &publicValue)) { |
757 | 10 | PORT_SetError(SSL_ERROR_RX_MALFORMED_DHE_KEY_SHARE); |
758 | 10 | return SECFailure; |
759 | 10 | } |
760 | | |
761 | 2.33k | peerKey->keyType = dhKey; |
762 | 2.33k | rv = SECITEM_CopyItem(peerKey->arena, &peerKey->u.dh.prime, |
763 | 2.33k | &pubKey->u.dh.prime); |
764 | 2.33k | if (rv != SECSuccess) |
765 | 0 | return SECFailure; |
766 | 2.33k | rv = SECITEM_CopyItem(peerKey->arena, &peerKey->u.dh.base, |
767 | 2.33k | &pubKey->u.dh.base); |
768 | 2.33k | if (rv != SECSuccess) |
769 | 0 | return SECFailure; |
770 | 2.33k | rv = SECITEM_CopyItem(peerKey->arena, &peerKey->u.dh.publicValue, |
771 | 2.33k | &publicValue); |
772 | 2.33k | if (rv != SECSuccess) |
773 | 0 | return SECFailure; |
774 | | |
775 | 2.33k | return SECSuccess; |
776 | 2.33k | } |
777 | | |
778 | | static SECStatus |
779 | | tls13_ImportKEMKeyShare(SECKEYPublicKey *peerKey, TLS13KeyShareEntry *entry) |
780 | 725 | { |
781 | 725 | SECItem pk = { siBuffer, NULL, 0 }; |
782 | 725 | SECStatus rv; |
783 | 725 | size_t expected_len; |
784 | | |
785 | 725 | switch (entry->group->name) { |
786 | 0 | case ssl_grp_kem_xyber768d00: |
787 | 0 | expected_len = X25519_PUBLIC_KEY_BYTES + KYBER768_PUBLIC_KEY_BYTES; |
788 | 0 | break; |
789 | 149 | case ssl_grp_kem_mlkem768x25519: |
790 | 149 | expected_len = X25519_PUBLIC_KEY_BYTES + KYBER768_PUBLIC_KEY_BYTES; |
791 | 149 | break; |
792 | 167 | case ssl_grp_kem_secp256r1mlkem768: |
793 | 167 | expected_len = SECP256_PUBLIC_KEY_BYTES + KYBER768_PUBLIC_KEY_BYTES; |
794 | 167 | break; |
795 | 409 | case ssl_grp_kem_secp384r1mlkem1024: |
796 | 409 | expected_len = SECP384_PUBLIC_KEY_BYTES + MLKEM1024_PUBLIC_KEY_BYTES; |
797 | 409 | break; |
798 | 0 | default: |
799 | 0 | PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); |
800 | 0 | return SECFailure; |
801 | 725 | } |
802 | | |
803 | 725 | if (entry->key_exchange.len != expected_len) { |
804 | 6 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
805 | 6 | return SECFailure; |
806 | 6 | } |
807 | | |
808 | 719 | switch (entry->group->name) { |
809 | 0 | case ssl_grp_kem_xyber768d00: |
810 | 0 | peerKey->keyType = kyberKey; |
811 | 0 | peerKey->u.kyber.params = params_kyber768_round3; |
812 | | // key_exchange.data is `x25519 || kyber768` |
813 | 0 | pk.data = entry->key_exchange.data + X25519_PUBLIC_KEY_BYTES; |
814 | 0 | pk.len = KYBER768_PUBLIC_KEY_BYTES; |
815 | 0 | break; |
816 | 146 | case ssl_grp_kem_mlkem768x25519: |
817 | 146 | peerKey->keyType = kyberKey; |
818 | 146 | peerKey->u.kyber.params = params_ml_kem768; |
819 | | // key_exchange.data is `mlkem768 || x25519` |
820 | 146 | pk.data = entry->key_exchange.data; |
821 | 146 | pk.len = KYBER768_PUBLIC_KEY_BYTES; |
822 | 146 | break; |
823 | 166 | case ssl_grp_kem_secp256r1mlkem768: |
824 | 166 | peerKey->keyType = kyberKey; |
825 | 166 | peerKey->u.kyber.params = params_ml_kem768; |
826 | | /* key_exchange.data is `secp256 || mlkem768` */ |
827 | 166 | pk.data = entry->key_exchange.data + SECP256_PUBLIC_KEY_BYTES; |
828 | 166 | pk.len = KYBER768_PUBLIC_KEY_BYTES; |
829 | 166 | break; |
830 | 407 | case ssl_grp_kem_secp384r1mlkem1024: |
831 | 407 | peerKey->keyType = kyberKey; |
832 | 407 | peerKey->u.kyber.params = params_ml_kem1024; |
833 | | /* key_exchange.data is `secp384 || mlkem1024` */ |
834 | 407 | pk.data = entry->key_exchange.data + SECP384_PUBLIC_KEY_BYTES; |
835 | 407 | pk.len = MLKEM1024_PUBLIC_KEY_BYTES; |
836 | 407 | break; |
837 | 0 | default: |
838 | 0 | PORT_Assert(0); |
839 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
840 | 0 | return SECFailure; |
841 | 719 | } |
842 | | |
843 | 719 | rv = SECITEM_CopyItem(peerKey->arena, &peerKey->u.kyber.publicValue, &pk); |
844 | 719 | if (rv != SECSuccess) { |
845 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
846 | 0 | return SECFailure; |
847 | 0 | } |
848 | | |
849 | 719 | return SECSuccess; |
850 | 719 | } |
851 | | |
852 | | static SECStatus |
853 | | tls13_HandleKEMCiphertext(sslSocket *ss, TLS13KeyShareEntry *entry, sslKeyPair *keyPair, PK11SymKey **outKey) |
854 | 123 | { |
855 | 123 | SECItem ct = { siBuffer, NULL, 0 }; |
856 | 123 | SECStatus rv; |
857 | | |
858 | 123 | switch (entry->group->name) { |
859 | 0 | case ssl_grp_kem_xyber768d00: |
860 | 0 | if (entry->key_exchange.len != X25519_PUBLIC_KEY_BYTES + KYBER768_CIPHERTEXT_BYTES) { |
861 | 0 | ssl_MapLowLevelError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
862 | 0 | return SECFailure; |
863 | 0 | } |
864 | 0 | ct.data = entry->key_exchange.data + X25519_PUBLIC_KEY_BYTES; |
865 | 0 | ct.len = KYBER768_CIPHERTEXT_BYTES; |
866 | 0 | break; |
867 | 123 | case ssl_grp_kem_mlkem768x25519: |
868 | 123 | if (entry->key_exchange.len != X25519_PUBLIC_KEY_BYTES + KYBER768_CIPHERTEXT_BYTES) { |
869 | 21 | ssl_MapLowLevelError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
870 | 21 | return SECFailure; |
871 | 21 | } |
872 | 102 | ct.data = entry->key_exchange.data; |
873 | 102 | ct.len = KYBER768_CIPHERTEXT_BYTES; |
874 | 102 | break; |
875 | 0 | case ssl_grp_kem_secp256r1mlkem768: |
876 | 0 | if (entry->key_exchange.len != SECP256_PUBLIC_KEY_BYTES + KYBER768_CIPHERTEXT_BYTES) { |
877 | 0 | ssl_MapLowLevelError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
878 | 0 | return SECFailure; |
879 | 0 | } |
880 | 0 | ct.data = entry->key_exchange.data + SECP256_PUBLIC_KEY_BYTES; |
881 | 0 | ct.len = KYBER768_CIPHERTEXT_BYTES; |
882 | 0 | break; |
883 | 0 | case ssl_grp_kem_secp384r1mlkem1024: |
884 | 0 | if (entry->key_exchange.len != SECP384_PUBLIC_KEY_BYTES + MLKEM1024_CIPHERTEXT_BYTES) { |
885 | 0 | ssl_MapLowLevelError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
886 | 0 | return SECFailure; |
887 | 0 | } |
888 | 0 | ct.data = entry->key_exchange.data + SECP384_PUBLIC_KEY_BYTES; |
889 | 0 | ct.len = MLKEM1024_CIPHERTEXT_BYTES; |
890 | 0 | break; |
891 | 0 | default: |
892 | 0 | PORT_Assert(0); |
893 | 0 | ssl_MapLowLevelError(SEC_ERROR_LIBRARY_FAILURE); |
894 | 0 | return SECFailure; |
895 | 123 | } |
896 | | |
897 | 102 | rv = PK11_Decapsulate(keyPair->privKey, &ct, CKM_HKDF_DERIVE, |
898 | 102 | PK11_ATTR_SESSION | PK11_ATTR_INSENSITIVE, |
899 | 102 | CKF_DERIVE, outKey); |
900 | 102 | if (rv != SECSuccess) { |
901 | 0 | rv = PK11_Decapsulate(keyPair->privKey, &ct, CKM_HKDF_DERIVE, |
902 | 0 | PK11_ATTR_SESSION | PK11_ATTR_SENSITIVE, |
903 | 0 | CKF_DERIVE, outKey); |
904 | 0 | if (rv != SECSuccess) { |
905 | 0 | ssl_MapLowLevelError(SSL_ERROR_KEY_EXCHANGE_FAILURE); |
906 | 0 | } |
907 | 0 | } |
908 | 102 | return rv; |
909 | 123 | } |
910 | | |
911 | | static SECStatus |
912 | | tls13_HandleKEMKey(sslSocket *ss, |
913 | | TLS13KeyShareEntry *entry, |
914 | | PK11SymKey **key, |
915 | | SECItem **ciphertext) |
916 | 725 | { |
917 | 725 | PORTCheapArenaPool arena; |
918 | 725 | SECKEYPublicKey *peerKey; |
919 | 725 | CK_OBJECT_HANDLE handle; |
920 | 725 | SECStatus rv; |
921 | | |
922 | 725 | PORT_InitCheapArena(&arena, DER_DEFAULT_CHUNKSIZE); |
923 | 725 | peerKey = PORT_ArenaZNew(&arena.arena, SECKEYPublicKey); |
924 | 725 | if (peerKey == NULL) { |
925 | 0 | goto loser; |
926 | 0 | } |
927 | 725 | peerKey->arena = &arena.arena; |
928 | 725 | peerKey->pkcs11Slot = NULL; |
929 | 725 | peerKey->pkcs11ID = CK_INVALID_HANDLE; |
930 | | |
931 | 725 | rv = tls13_ImportKEMKeyShare(peerKey, entry); |
932 | 725 | if (rv != SECSuccess) { |
933 | 6 | goto loser; |
934 | 6 | } |
935 | | |
936 | 719 | PK11SlotInfo *slot = PK11_GetBestSlot(CKM_ML_KEM, ss->pkcs11PinArg); |
937 | 719 | if (!slot) { |
938 | 0 | goto loser; |
939 | 0 | } |
940 | | |
941 | 719 | handle = PK11_ImportPublicKey(slot, peerKey, PR_FALSE); |
942 | 719 | PK11_FreeSlot(slot); /* peerKey holds a slot reference on success. */ |
943 | 719 | if (handle == CK_INVALID_HANDLE) { |
944 | 0 | goto loser; |
945 | 0 | } |
946 | | |
947 | 719 | rv = PK11_Encapsulate(peerKey, CKM_HKDF_DERIVE, |
948 | 719 | PK11_ATTR_SESSION | PK11_ATTR_INSENSITIVE, |
949 | 719 | CKF_DERIVE, key, ciphertext); |
950 | 719 | if (rv != SECSuccess) { |
951 | 84 | rv = PK11_Encapsulate(peerKey, CKM_HKDF_DERIVE, |
952 | 84 | PK11_ATTR_SESSION | PK11_ATTR_SENSITIVE, |
953 | 84 | CKF_DERIVE, key, ciphertext); |
954 | 84 | } |
955 | | |
956 | | /* Destroy the imported public key */ |
957 | 719 | PORT_Assert(peerKey->pkcs11Slot); |
958 | 719 | PK11_DestroyObject(peerKey->pkcs11Slot, peerKey->pkcs11ID); |
959 | 719 | PK11_FreeSlot(peerKey->pkcs11Slot); |
960 | | |
961 | 719 | PORT_DestroyCheapArena(&arena); |
962 | 719 | return rv; |
963 | | |
964 | 6 | loser: |
965 | 6 | PORT_DestroyCheapArena(&arena); |
966 | 6 | return SECFailure; |
967 | 719 | } |
968 | | |
969 | | SECStatus |
970 | | tls13_HandleKeyShare(sslSocket *ss, |
971 | | TLS13KeyShareEntry *entry, |
972 | | sslKeyPair *keyPair, |
973 | | SSLHashType hash, |
974 | | PK11SymKey **out) |
975 | 5.41k | { |
976 | 5.41k | PORTCheapArenaPool arena; |
977 | 5.41k | SECKEYPublicKey *peerKey; |
978 | 5.41k | CK_MECHANISM_TYPE mechanism; |
979 | 5.41k | PK11SymKey *key; |
980 | 5.41k | unsigned char *ec_data; |
981 | 5.41k | SECStatus rv; |
982 | 5.41k | int keySize = 0; |
983 | 5.41k | const sslNamedGroupDef *ecGroup = NULL; |
984 | 5.41k | int ec_len = 0; |
985 | | |
986 | 5.41k | PORT_InitCheapArena(&arena, DER_DEFAULT_CHUNKSIZE); |
987 | 5.41k | peerKey = PORT_ArenaZNew(&arena.arena, SECKEYPublicKey); |
988 | 5.41k | if (peerKey == NULL) { |
989 | 0 | goto loser; |
990 | 0 | } |
991 | 5.41k | peerKey->arena = &arena.arena; |
992 | 5.41k | peerKey->pkcs11Slot = NULL; |
993 | 5.41k | peerKey->pkcs11ID = CK_INVALID_HANDLE; |
994 | | |
995 | 5.41k | switch (entry->group->keaType) { |
996 | 863 | case ssl_kea_ecdh_hybrid: |
997 | 863 | switch (entry->group->name) { |
998 | 0 | case ssl_grp_kem_xyber768d00: |
999 | 0 | ec_len = X25519_PUBLIC_KEY_BYTES; |
1000 | | // x25519 share is at the beginning |
1001 | 0 | ec_data = entry->key_exchange.len < ec_len |
1002 | 0 | ? NULL |
1003 | 0 | : entry->key_exchange.data; |
1004 | 0 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_curve25519); |
1005 | 0 | break; |
1006 | 279 | case ssl_grp_kem_mlkem768x25519: |
1007 | 279 | ec_len = X25519_PUBLIC_KEY_BYTES; |
1008 | | // x25519 share is at the end |
1009 | 279 | ec_data = entry->key_exchange.len < ec_len |
1010 | 279 | ? NULL |
1011 | 279 | : entry->key_exchange.data + entry->key_exchange.len - ec_len; |
1012 | 279 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_curve25519); |
1013 | 279 | break; |
1014 | 168 | case ssl_grp_kem_secp256r1mlkem768: |
1015 | 168 | ec_len = SECP256_PUBLIC_KEY_BYTES; |
1016 | | /* secp256 share is at the beginning */ |
1017 | 168 | ec_data = entry->key_exchange.len < ec_len |
1018 | 168 | ? NULL |
1019 | 168 | : entry->key_exchange.data; |
1020 | 168 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_secp256r1); |
1021 | 168 | break; |
1022 | 416 | case ssl_grp_kem_secp384r1mlkem1024: |
1023 | 416 | ec_len = SECP384_PUBLIC_KEY_BYTES; |
1024 | | /* secp384 share is at the beginning */ |
1025 | 416 | ec_data = entry->key_exchange.len < ec_len |
1026 | 416 | ? NULL |
1027 | 416 | : entry->key_exchange.data; |
1028 | 416 | ecGroup = ssl_LookupNamedGroup(ssl_grp_ec_secp384r1); |
1029 | 416 | break; |
1030 | 0 | default: |
1031 | 0 | ec_data = NULL; |
1032 | 0 | break; |
1033 | 863 | } |
1034 | 863 | if (!ec_data) { |
1035 | 4 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HYBRID_KEY_SHARE); |
1036 | 4 | goto loser; |
1037 | 4 | } |
1038 | 859 | rv = ssl_ImportECDHKeyShare(peerKey, ec_data, ec_len, ecGroup); |
1039 | 859 | mechanism = CKM_ECDH1_DERIVE; |
1040 | 859 | break; |
1041 | 2.20k | case ssl_kea_ecdh: |
1042 | 2.20k | rv = ssl_ImportECDHKeyShare(peerKey, |
1043 | 2.20k | entry->key_exchange.data, |
1044 | 2.20k | entry->key_exchange.len, |
1045 | 2.20k | entry->group); |
1046 | 2.20k | mechanism = CKM_ECDH1_DERIVE; |
1047 | 2.20k | break; |
1048 | 2.34k | case ssl_kea_dh: |
1049 | 2.34k | rv = tls13_ImportDHEKeyShare(peerKey, |
1050 | 2.34k | entry->key_exchange.data, |
1051 | 2.34k | entry->key_exchange.len, |
1052 | 2.34k | keyPair->pubKey); |
1053 | 2.34k | mechanism = CKM_DH_PKCS_DERIVE; |
1054 | 2.34k | keySize = peerKey->u.dh.publicValue.len; |
1055 | 2.34k | break; |
1056 | 0 | default: |
1057 | 0 | PORT_Assert(0); |
1058 | 0 | goto loser; |
1059 | 5.41k | } |
1060 | 5.41k | if (rv != SECSuccess) { |
1061 | 37 | goto loser; |
1062 | 37 | } |
1063 | | |
1064 | 5.37k | key = PK11_PubDeriveWithKDF( |
1065 | 5.37k | keyPair->privKey, peerKey, PR_FALSE, NULL, NULL, mechanism, |
1066 | 5.37k | CKM_HKDF_DERIVE, CKA_DERIVE, keySize, CKD_NULL, NULL, NULL); |
1067 | 5.37k | if (!key) { |
1068 | 142 | ssl_MapLowLevelError(SSL_ERROR_KEY_EXCHANGE_FAILURE); |
1069 | 142 | goto loser; |
1070 | 142 | } |
1071 | | |
1072 | 5.23k | *out = key; |
1073 | 5.23k | PORT_DestroyCheapArena(&arena); |
1074 | 5.23k | return SECSuccess; |
1075 | | |
1076 | 183 | loser: |
1077 | 183 | PORT_DestroyCheapArena(&arena); |
1078 | 183 | return SECFailure; |
1079 | 5.37k | } |
1080 | | |
1081 | | static PRBool |
1082 | | tls13_UseServerSecret(sslSocket *ss, SSLSecretDirection direction) |
1083 | 255k | { |
1084 | 255k | return ss->sec.isServer == (direction == ssl_secret_write); |
1085 | 255k | } |
1086 | | |
1087 | | static PK11SymKey ** |
1088 | | tls13_TrafficSecretRef(sslSocket *ss, SSLSecretDirection direction) |
1089 | 122k | { |
1090 | 122k | if (tls13_UseServerSecret(ss, direction)) { |
1091 | 56.5k | return &ss->ssl3.hs.serverTrafficSecret; |
1092 | 56.5k | } |
1093 | 65.6k | return &ss->ssl3.hs.clientTrafficSecret; |
1094 | 122k | } |
1095 | | |
1096 | | SECStatus |
1097 | | tls13_UpdateTrafficKeys(sslSocket *ss, SSLSecretDirection direction) |
1098 | 122k | { |
1099 | 122k | PK11SymKey **secret; |
1100 | 122k | PK11SymKey *updatedSecret; |
1101 | 122k | PRUint16 epoch; |
1102 | 122k | SECStatus rv; |
1103 | | |
1104 | 122k | secret = tls13_TrafficSecretRef(ss, direction); |
1105 | 122k | rv = tls13_HkdfExpandLabel(*secret, tls13_GetHash(ss), |
1106 | 122k | NULL, 0, |
1107 | 122k | kHkdfLabelTrafficUpdate, |
1108 | 122k | strlen(kHkdfLabelTrafficUpdate), |
1109 | 122k | tls13_GetHmacMechanism(ss), |
1110 | 122k | tls13_GetHashSize(ss), |
1111 | 122k | ss->protocolVariant, |
1112 | 122k | &updatedSecret); |
1113 | 122k | if (rv != SECSuccess) { |
1114 | 0 | return SECFailure; |
1115 | 0 | } |
1116 | | |
1117 | 122k | PK11_FreeSymKey(*secret); |
1118 | 122k | *secret = updatedSecret; |
1119 | | |
1120 | 122k | ssl_GetSpecReadLock(ss); |
1121 | 122k | if (direction == ssl_secret_read) { |
1122 | 65.6k | epoch = ss->ssl3.crSpec->epoch; |
1123 | 65.6k | } else { |
1124 | 56.5k | epoch = ss->ssl3.cwSpec->epoch; |
1125 | 56.5k | } |
1126 | 122k | ssl_ReleaseSpecReadLock(ss); |
1127 | | |
1128 | 122k | if (epoch == PR_UINT16_MAX) { |
1129 | | /* Good chance that this is an overflow from too many updates. */ |
1130 | 0 | FATAL_ERROR(ss, SSL_ERROR_TOO_MANY_KEY_UPDATES, internal_error); |
1131 | 0 | return SECFailure; |
1132 | 0 | } |
1133 | 122k | ++epoch; |
1134 | | |
1135 | 122k | if (ss->secretCallback) { |
1136 | 0 | ss->secretCallback(ss->fd, epoch, direction, updatedSecret, |
1137 | 0 | ss->secretCallbackArg); |
1138 | 0 | } |
1139 | 122k | rv = tls13_SetCipherSpec(ss, epoch, direction, PR_FALSE); |
1140 | 122k | if (rv != SECSuccess) { |
1141 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
1142 | 0 | return SECFailure; |
1143 | 0 | } |
1144 | 122k | return SECSuccess; |
1145 | 122k | } |
1146 | | |
1147 | | SECStatus |
1148 | | tls13_SendKeyUpdate(sslSocket *ss, tls13KeyUpdateRequest request, PRBool buffer) |
1149 | 56.6k | { |
1150 | 56.6k | SECStatus rv; |
1151 | | |
1152 | 56.6k | SSL_TRC(3, ("%d: TLS13[%d]: %s send key update, response %s", |
1153 | 56.6k | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
1154 | 56.6k | (request == update_requested) ? "requested" |
1155 | 56.6k | : "not requested")); |
1156 | | |
1157 | 56.6k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
1158 | 56.6k | PORT_Assert(!ss->sec.isServer || !ss->ssl3.clientCertRequested); |
1159 | | |
1160 | 56.6k | if (!tls13_IsPostHandshake(ss)) { |
1161 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1162 | 0 | return SECFailure; |
1163 | 0 | } |
1164 | | |
1165 | 56.6k | rv = TLS13_CHECK_HS_STATE(ss, SEC_ERROR_LIBRARY_FAILURE, |
1166 | 56.6k | idle_handshake); |
1167 | 56.6k | if (rv != SECSuccess) { |
1168 | 0 | return SECFailure; |
1169 | 0 | } |
1170 | | |
1171 | 56.6k | if (IS_DTLS(ss)) { |
1172 | 60 | rv = dtls13_MaybeSendKeyUpdate(ss, request, buffer); |
1173 | 60 | if (rv != SECSuccess) { |
1174 | | /* Error code set already. */ |
1175 | 0 | return SECFailure; |
1176 | 0 | } |
1177 | 60 | return rv; |
1178 | 60 | } |
1179 | | |
1180 | 56.5k | ssl_GetXmitBufLock(ss); |
1181 | 56.5k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_key_update, 1); |
1182 | 56.5k | if (rv != SECSuccess) { |
1183 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
1184 | 0 | goto loser; |
1185 | 0 | } |
1186 | 56.5k | rv = ssl3_AppendHandshakeNumber(ss, request, 1); |
1187 | 56.5k | if (rv != SECSuccess) { |
1188 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
1189 | 0 | goto loser; |
1190 | 0 | } |
1191 | | |
1192 | | /* If we have been asked to buffer, then do so. This allows us to coalesce |
1193 | | * a KeyUpdate with a pending write. */ |
1194 | 56.5k | rv = ssl3_FlushHandshake(ss, buffer ? ssl_SEND_FLAG_FORCE_INTO_BUFFER : 0); |
1195 | 56.5k | if (rv != SECSuccess) { |
1196 | 0 | goto loser; /* error code set by ssl3_FlushHandshake */ |
1197 | 0 | } |
1198 | 56.5k | ssl_ReleaseXmitBufLock(ss); |
1199 | | |
1200 | 56.5k | rv = tls13_UpdateTrafficKeys(ss, ssl_secret_write); |
1201 | 56.5k | if (rv != SECSuccess) { |
1202 | 0 | goto loser; /* error code set by tls13_UpdateTrafficKeys */ |
1203 | 0 | } |
1204 | | |
1205 | 56.5k | return SECSuccess; |
1206 | | |
1207 | 0 | loser: |
1208 | 0 | ssl_ReleaseXmitBufLock(ss); |
1209 | 0 | return SECFailure; |
1210 | 56.5k | } |
1211 | | |
1212 | | SECStatus |
1213 | | SSLExp_KeyUpdate(PRFileDesc *fd, PRBool requestUpdate) |
1214 | 0 | { |
1215 | 0 | SECStatus rv; |
1216 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1217 | 0 | if (!ss) { |
1218 | 0 | return SECFailure; |
1219 | 0 | } |
1220 | | |
1221 | 0 | if (!tls13_IsPostHandshake(ss)) { |
1222 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1223 | 0 | return SECFailure; |
1224 | 0 | } |
1225 | | |
1226 | 0 | if (ss->ssl3.clientCertRequested) { |
1227 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
1228 | 0 | return SECFailure; |
1229 | 0 | } |
1230 | | |
1231 | 0 | rv = TLS13_CHECK_HS_STATE(ss, SEC_ERROR_INVALID_ARGS, |
1232 | 0 | idle_handshake); |
1233 | 0 | if (rv != SECSuccess) { |
1234 | 0 | return SECFailure; |
1235 | 0 | } |
1236 | | |
1237 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1238 | 0 | rv = tls13_SendKeyUpdate(ss, requestUpdate ? update_requested : update_not_requested, |
1239 | 0 | PR_FALSE /* don't buffer */); |
1240 | | |
1241 | | /* Remember that we are the ones that initiated this KeyUpdate. */ |
1242 | 0 | if (rv == SECSuccess) { |
1243 | 0 | ss->ssl3.peerRequestedKeyUpdate = PR_FALSE; |
1244 | 0 | } |
1245 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1246 | 0 | return rv; |
1247 | 0 | } |
1248 | | |
1249 | | SECStatus |
1250 | | SSLExp_SetCertificateCompressionAlgorithm(PRFileDesc *fd, SSLCertificateCompressionAlgorithm alg) |
1251 | 32.0k | { |
1252 | 32.0k | sslSocket *ss = ssl_FindSocket(fd); |
1253 | 32.0k | if (!ss) { |
1254 | 0 | return SECFailure; /* Code already set. */ |
1255 | 0 | } |
1256 | | |
1257 | 32.0k | ssl_GetSSL3HandshakeLock(ss); |
1258 | 32.0k | if (ss->ssl3.supportedCertCompressionAlgorithmsCount == MAX_SUPPORTED_CERTIFICATE_COMPRESSION_ALGS) { |
1259 | 0 | goto loser; |
1260 | 0 | } |
1261 | | |
1262 | | /* Reserved ID */ |
1263 | 32.0k | if (alg.id == 0) { |
1264 | 0 | goto loser; |
1265 | 0 | } |
1266 | | |
1267 | 32.0k | if (alg.encode == NULL && alg.decode == NULL) { |
1268 | 0 | goto loser; |
1269 | 0 | } |
1270 | | |
1271 | | /* Checking that we have not yet registed an algorithm with the same ID. */ |
1272 | 32.0k | for (int i = 0; i < ss->ssl3.supportedCertCompressionAlgorithmsCount; i++) { |
1273 | 0 | if (ss->ssl3.supportedCertCompressionAlgorithms[i].id == alg.id) { |
1274 | 0 | goto loser; |
1275 | 0 | } |
1276 | 0 | } |
1277 | | |
1278 | 32.0k | PORT_Memcpy(&ss->ssl3.supportedCertCompressionAlgorithms |
1279 | 32.0k | [ss->ssl3.supportedCertCompressionAlgorithmsCount], |
1280 | 32.0k | &alg, sizeof(alg)); |
1281 | 32.0k | ss->ssl3.supportedCertCompressionAlgorithmsCount += 1; |
1282 | 32.0k | ssl_ReleaseSSL3HandshakeLock(ss); |
1283 | 32.0k | return SECSuccess; |
1284 | | |
1285 | 0 | loser: |
1286 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1287 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1288 | 0 | return SECFailure; |
1289 | 32.0k | } |
1290 | | |
1291 | | /* |
1292 | | * enum { |
1293 | | * update_not_requested(0), update_requested(1), (255) |
1294 | | * } KeyUpdateRequest; |
1295 | | * |
1296 | | * struct { |
1297 | | * KeyUpdateRequest request_update; |
1298 | | * } KeyUpdate; |
1299 | | */ |
1300 | | |
1301 | | /* If we're handing the DTLS1.3 message, we silently fail if there is a parsing problem. */ |
1302 | | static SECStatus |
1303 | | tls13_HandleKeyUpdate(sslSocket *ss, PRUint8 *b, unsigned int length) |
1304 | 65.6k | { |
1305 | 65.6k | SECStatus rv; |
1306 | 65.6k | PRUint32 update; |
1307 | | |
1308 | 65.6k | SSL_TRC(3, ("%d: TLS13[%d]: %s handle key update", |
1309 | 65.6k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1310 | | |
1311 | 65.6k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
1312 | 65.6k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
1313 | | |
1314 | 65.6k | if (!tls13_IsPostHandshake(ss)) { |
1315 | 14 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE, unexpected_message); |
1316 | 14 | return SECFailure; |
1317 | 14 | } |
1318 | | |
1319 | 65.6k | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE, |
1320 | 65.6k | idle_handshake); |
1321 | 65.6k | if (rv != SECSuccess) { |
1322 | | /* We should never be idle_handshake prior to firstHsDone. */ |
1323 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
1324 | 0 | return SECFailure; |
1325 | 0 | } |
1326 | | |
1327 | 65.6k | rv = ssl3_ConsumeHandshakeNumber(ss, &update, 1, &b, &length); |
1328 | 65.6k | if (rv != SECSuccess) { |
1329 | 3 | return SECFailure; /* Error code set already. */ |
1330 | 3 | } |
1331 | 65.6k | if (length != 0) { |
1332 | 11 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_KEY_UPDATE, decode_error); |
1333 | 11 | return SECFailure; |
1334 | 11 | } |
1335 | 65.6k | if (!(update == update_requested || |
1336 | 115 | update == update_not_requested)) { |
1337 | 9 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_KEY_UPDATE, decode_error); |
1338 | 9 | return SECFailure; |
1339 | 9 | } |
1340 | | |
1341 | 65.6k | if (IS_DTLS(ss)) { |
1342 | 0 | return dtls13_HandleKeyUpdate(ss, b, length, update); |
1343 | 0 | } |
1344 | | |
1345 | 65.6k | rv = tls13_UpdateTrafficKeys(ss, ssl_secret_read); |
1346 | 65.6k | if (rv != SECSuccess) { |
1347 | 0 | return SECFailure; /* Error code set by tls13_UpdateTrafficKeys. */ |
1348 | 0 | } |
1349 | | |
1350 | 65.6k | if (update == update_requested) { |
1351 | 65.5k | PRBool sendUpdate; |
1352 | 65.5k | if (ss->ssl3.clientCertRequested) { |
1353 | | /* Post-handshake auth is in progress; defer sending a key update. */ |
1354 | 0 | ss->ssl3.hs.keyUpdateDeferred = PR_TRUE; |
1355 | 0 | ss->ssl3.hs.deferredKeyUpdateRequest = update_not_requested; |
1356 | 0 | sendUpdate = PR_FALSE; |
1357 | 65.5k | } else if (ss->ssl3.peerRequestedKeyUpdate) { |
1358 | | /* Only send an update if we have sent with the current spec. This |
1359 | | * prevents us from being forced to crank forward pointlessly. */ |
1360 | 65.1k | ssl_GetSpecReadLock(ss); |
1361 | 65.1k | sendUpdate = ss->ssl3.cwSpec->nextSeqNum > 0; |
1362 | 65.1k | ssl_ReleaseSpecReadLock(ss); |
1363 | 65.1k | } else { |
1364 | 353 | sendUpdate = PR_TRUE; |
1365 | 353 | } |
1366 | 65.5k | if (sendUpdate) { |
1367 | | /* Respond immediately (don't buffer). */ |
1368 | 56.5k | rv = tls13_SendKeyUpdate(ss, update_not_requested, PR_FALSE); |
1369 | 56.5k | if (rv != SECSuccess) { |
1370 | 0 | return SECFailure; /* Error already set. */ |
1371 | 0 | } |
1372 | 56.5k | } |
1373 | 65.5k | ss->ssl3.peerRequestedKeyUpdate = PR_TRUE; |
1374 | 65.5k | } |
1375 | | |
1376 | 65.6k | return SECSuccess; |
1377 | 65.6k | } |
1378 | | |
1379 | | SECStatus |
1380 | | SSLExp_SendCertificateRequest(PRFileDesc *fd) |
1381 | 0 | { |
1382 | 0 | SECStatus rv; |
1383 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
1384 | 0 | if (!ss) { |
1385 | 0 | return SECFailure; |
1386 | 0 | } |
1387 | | |
1388 | | /* Not supported. */ |
1389 | 0 | if (IS_DTLS(ss)) { |
1390 | 0 | PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_VERSION); |
1391 | 0 | return SECFailure; |
1392 | 0 | } |
1393 | | |
1394 | 0 | if (!tls13_IsPostHandshake(ss)) { |
1395 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1396 | 0 | return SECFailure; |
1397 | 0 | } |
1398 | | |
1399 | 0 | if (ss->ssl3.clientCertRequested) { |
1400 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
1401 | 0 | return SECFailure; |
1402 | 0 | } |
1403 | | |
1404 | | /* Disallow a CertificateRequest if this connection uses an external PSK. */ |
1405 | 0 | if (ss->sec.authType == ssl_auth_psk) { |
1406 | 0 | PORT_SetError(SSL_ERROR_FEATURE_DISABLED); |
1407 | 0 | return SECFailure; |
1408 | 0 | } |
1409 | | |
1410 | 0 | rv = TLS13_CHECK_HS_STATE(ss, SEC_ERROR_INVALID_ARGS, |
1411 | 0 | idle_handshake); |
1412 | 0 | if (rv != SECSuccess) { |
1413 | 0 | return SECFailure; |
1414 | 0 | } |
1415 | | |
1416 | 0 | if (!ssl3_ExtensionNegotiated(ss, ssl_tls13_post_handshake_auth_xtn)) { |
1417 | 0 | PORT_SetError(SSL_ERROR_MISSING_POST_HANDSHAKE_AUTH_EXTENSION); |
1418 | 0 | return SECFailure; |
1419 | 0 | } |
1420 | | |
1421 | 0 | ssl_GetSSL3HandshakeLock(ss); |
1422 | |
|
1423 | 0 | rv = tls13_SendCertificateRequest(ss); |
1424 | 0 | if (rv == SECSuccess) { |
1425 | 0 | ssl_GetXmitBufLock(ss); |
1426 | 0 | rv = ssl3_FlushHandshake(ss, 0); |
1427 | 0 | ssl_ReleaseXmitBufLock(ss); |
1428 | 0 | ss->ssl3.clientCertRequested = PR_TRUE; |
1429 | 0 | } |
1430 | |
|
1431 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
1432 | 0 | return rv; |
1433 | 0 | } |
1434 | | |
1435 | | SECStatus |
1436 | | tls13_HandlePostHelloHandshakeMessage(sslSocket *ss, PRUint8 *b, PRUint32 length) |
1437 | 66.4k | { |
1438 | 66.4k | if (ss->sec.isServer && ss->ssl3.hs.zeroRttIgnore != ssl_0rtt_ignore_none) { |
1439 | 26 | SSL_TRC(3, ("%d: TLS13[%d]: successfully decrypted handshake after " |
1440 | 26 | "failed 0-RTT", |
1441 | 26 | SSL_GETPID(), ss->fd)); |
1442 | 26 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_none; |
1443 | 26 | } |
1444 | | |
1445 | | /* TODO(ekr@rtfm.com): Would it be better to check all the states here? */ |
1446 | 66.4k | switch (ss->ssl3.hs.msg_type) { |
1447 | 82 | case ssl_hs_certificate: |
1448 | 82 | return tls13_HandleCertificate(ss, b, length, PR_FALSE); |
1449 | 60 | case ssl_hs_compressed_certificate: |
1450 | 60 | return tls13_HandleCertificateDecode(ss, b, length); |
1451 | 15 | case ssl_hs_certificate_request: |
1452 | 15 | return tls13_HandleCertificateRequest(ss, b, length); |
1453 | | |
1454 | 12 | case ssl_hs_certificate_verify: |
1455 | 12 | return tls13_HandleCertificateVerify(ss, b, length); |
1456 | | |
1457 | 12 | case ssl_hs_encrypted_extensions: |
1458 | 12 | return tls13_HandleEncryptedExtensions(ss, b, length); |
1459 | | |
1460 | 11 | case ssl_hs_new_session_ticket: |
1461 | 11 | return tls13_HandleNewSessionTicket(ss, b, length); |
1462 | | |
1463 | 584 | case ssl_hs_finished: |
1464 | 584 | if (ss->sec.isServer) { |
1465 | 580 | return tls13_ServerHandleFinished(ss, b, length); |
1466 | 580 | } else { |
1467 | 4 | return tls13_ClientHandleFinished(ss, b, length); |
1468 | 4 | } |
1469 | | |
1470 | 10 | case ssl_hs_end_of_early_data: |
1471 | 10 | return tls13_HandleEndOfEarlyData(ss, b, length); |
1472 | | |
1473 | 65.6k | case ssl_hs_key_update: |
1474 | 65.6k | return tls13_HandleKeyUpdate(ss, b, length); |
1475 | | |
1476 | 25 | default: |
1477 | 25 | FATAL_ERROR(ss, SSL_ERROR_RX_UNKNOWN_HANDSHAKE, unexpected_message); |
1478 | 25 | return SECFailure; |
1479 | 66.4k | } |
1480 | | |
1481 | 0 | PORT_Assert(0); /* Unreached */ |
1482 | 0 | return SECFailure; |
1483 | 66.4k | } |
1484 | | |
1485 | | static SECStatus |
1486 | | tls13_RecoverWrappedSharedSecret(sslSocket *ss, sslSessionID *sid) |
1487 | 0 | { |
1488 | 0 | PK11SymKey *wrapKey; /* wrapping key */ |
1489 | 0 | SECItem wrappedMS = { siBuffer, NULL, 0 }; |
1490 | 0 | SSLHashType hashType; |
1491 | |
|
1492 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: recovering static secret (%s)", |
1493 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1494 | | |
1495 | | /* Now find the hash used as the PRF for the previous handshake. */ |
1496 | 0 | hashType = tls13_GetHashForCipherSuite(sid->u.ssl3.cipherSuite); |
1497 | | |
1498 | | /* If we are the server, we compute the wrapping key, but if we |
1499 | | * are the client, its coordinates are stored with the ticket. */ |
1500 | 0 | if (ss->sec.isServer) { |
1501 | 0 | wrapKey = ssl3_GetWrappingKey(ss, NULL, |
1502 | 0 | sid->u.ssl3.masterWrapMech, |
1503 | 0 | ss->pkcs11PinArg); |
1504 | 0 | } else { |
1505 | 0 | PK11SlotInfo *slot = SECMOD_LookupSlot(sid->u.ssl3.masterModuleID, |
1506 | 0 | sid->u.ssl3.masterSlotID); |
1507 | 0 | if (!slot) |
1508 | 0 | return SECFailure; |
1509 | | |
1510 | 0 | wrapKey = PK11_GetWrapKey(slot, |
1511 | 0 | sid->u.ssl3.masterWrapIndex, |
1512 | 0 | sid->u.ssl3.masterWrapMech, |
1513 | 0 | sid->u.ssl3.masterWrapSeries, |
1514 | 0 | ss->pkcs11PinArg); |
1515 | 0 | PK11_FreeSlot(slot); |
1516 | 0 | } |
1517 | 0 | if (!wrapKey) { |
1518 | 0 | return SECFailure; |
1519 | 0 | } |
1520 | | |
1521 | 0 | wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret; |
1522 | 0 | wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len; |
1523 | |
|
1524 | 0 | PK11SymKey *unwrappedPsk = ssl_unwrapSymKey(wrapKey, sid->u.ssl3.masterWrapMech, |
1525 | 0 | NULL, &wrappedMS, CKM_SSL3_MASTER_KEY_DERIVE, |
1526 | 0 | CKA_DERIVE, tls13_GetHashSizeForHash(hashType), |
1527 | 0 | CKF_SIGN | CKF_VERIFY, ss->pkcs11PinArg); |
1528 | 0 | PK11_FreeSymKey(wrapKey); |
1529 | 0 | if (!unwrappedPsk) { |
1530 | 0 | return SECFailure; |
1531 | 0 | } |
1532 | 0 | sslPsk *rpsk = tls13_MakePsk(unwrappedPsk, ssl_psk_resume, hashType, NULL); |
1533 | 0 | if (!rpsk) { |
1534 | 0 | PK11_FreeSymKey(unwrappedPsk); |
1535 | 0 | return SECFailure; |
1536 | 0 | } |
1537 | 0 | if (sid->u.ssl3.locked.sessionTicket.flags & ticket_allow_early_data) { |
1538 | 0 | rpsk->maxEarlyData = sid->u.ssl3.locked.sessionTicket.max_early_data_size; |
1539 | 0 | rpsk->zeroRttSuite = sid->u.ssl3.cipherSuite; |
1540 | 0 | } |
1541 | 0 | PRINT_KEY(50, (ss, "Recovered RMS", rpsk->key)); |
1542 | 0 | PORT_Assert(PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks) || |
1543 | 0 | ((sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks))->type != ssl_psk_resume); |
1544 | |
|
1545 | 0 | if (ss->sec.isServer) { |
1546 | | /* In server, we couldn't select the RPSK in the extension handler |
1547 | | * since it was not unwrapped yet. We're committed now, so select |
1548 | | * it and add it to the list (to ensure it is freed). */ |
1549 | 0 | ss->xtnData.selectedPsk = rpsk; |
1550 | 0 | } |
1551 | 0 | PR_APPEND_LINK(&rpsk->link, &ss->ssl3.hs.psks); |
1552 | |
|
1553 | 0 | return SECSuccess; |
1554 | 0 | } |
1555 | | |
1556 | | /* Key Derivation Functions. |
1557 | | * |
1558 | | * 0 |
1559 | | * | |
1560 | | * v |
1561 | | * PSK -> HKDF-Extract = Early Secret |
1562 | | * | |
1563 | | * +-----> Derive-Secret(., "ext binder" | "res binder", "") |
1564 | | * | = binder_key |
1565 | | * | |
1566 | | * +-----> Derive-Secret(., "c e traffic", |
1567 | | * | ClientHello) |
1568 | | * | = client_early_traffic_secret |
1569 | | * | |
1570 | | * +-----> Derive-Secret(., "e exp master", |
1571 | | * | ClientHello) |
1572 | | * | = early_exporter_secret |
1573 | | * v |
1574 | | * Derive-Secret(., "derived", "") |
1575 | | * | |
1576 | | * v |
1577 | | *(EC)DHE -> HKDF-Extract = Handshake Secret |
1578 | | * | |
1579 | | * +-----> Derive-Secret(., "c hs traffic", |
1580 | | * | ClientHello...ServerHello) |
1581 | | * | = client_handshake_traffic_secret |
1582 | | * | |
1583 | | * +-----> Derive-Secret(., "s hs traffic", |
1584 | | * | ClientHello...ServerHello) |
1585 | | * | = server_handshake_traffic_secret |
1586 | | * v |
1587 | | * Derive-Secret(., "derived", "") |
1588 | | * | |
1589 | | * v |
1590 | | * 0 -> HKDF-Extract = Master Secret |
1591 | | * | |
1592 | | * +-----> Derive-Secret(., "c ap traffic", |
1593 | | * | ClientHello...Server Finished) |
1594 | | * | = client_traffic_secret_0 |
1595 | | * | |
1596 | | * +-----> Derive-Secret(., "s ap traffic", |
1597 | | * | ClientHello...Server Finished) |
1598 | | * | = server_traffic_secret_0 |
1599 | | * | |
1600 | | * +-----> Derive-Secret(., "exp master", |
1601 | | * | ClientHello...Server Finished) |
1602 | | * | = exporter_secret |
1603 | | * | |
1604 | | * +-----> Derive-Secret(., "res master", |
1605 | | * ClientHello...Client Finished) |
1606 | | * = resumption_master_secret |
1607 | | * |
1608 | | */ |
1609 | | static SECStatus |
1610 | | tls13_ComputeEarlySecretsWithPsk(sslSocket *ss) |
1611 | 6.88k | { |
1612 | 6.88k | SECStatus rv; |
1613 | | |
1614 | 6.88k | SSL_TRC(5, ("%d: TLS13[%d]: compute early secrets (%s)", |
1615 | 6.88k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1616 | | |
1617 | 6.88k | PORT_Assert(!ss->ssl3.hs.currentSecret); |
1618 | 6.88k | sslPsk *psk = NULL; |
1619 | | |
1620 | 6.88k | if (ss->sec.isServer) { |
1621 | 0 | psk = ss->xtnData.selectedPsk; |
1622 | 6.88k | } else { |
1623 | | /* Client to use the first PSK for early secrets. */ |
1624 | 6.88k | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)); |
1625 | 6.88k | psk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks); |
1626 | 6.88k | } |
1627 | 6.88k | PORT_Assert(psk && psk->key); |
1628 | 6.88k | PORT_Assert(psk->hash != ssl_hash_none); |
1629 | | |
1630 | 6.88k | PK11SymKey *earlySecret = NULL; |
1631 | 6.88k | rv = tls13_HkdfExtract(NULL, psk->key, psk->hash, &earlySecret); |
1632 | 6.88k | if (rv != SECSuccess) { |
1633 | 0 | return SECFailure; |
1634 | 0 | } |
1635 | | |
1636 | | /* No longer need the raw input key */ |
1637 | 6.88k | PK11_FreeSymKey(psk->key); |
1638 | 6.88k | psk->key = NULL; |
1639 | 6.88k | const char *label = (psk->type == ssl_psk_resume) ? kHkdfLabelResPskBinderKey : kHkdfLabelExtPskBinderKey; |
1640 | 6.88k | rv = tls13_DeriveSecretNullHash(ss, earlySecret, |
1641 | 6.88k | label, strlen(label), |
1642 | 6.88k | &psk->binderKey, psk->hash); |
1643 | 6.88k | if (rv != SECSuccess) { |
1644 | 0 | PK11_FreeSymKey(earlySecret); |
1645 | 0 | return SECFailure; |
1646 | 0 | } |
1647 | 6.88k | ss->ssl3.hs.currentSecret = earlySecret; |
1648 | | |
1649 | 6.88k | return SECSuccess; |
1650 | 6.88k | } |
1651 | | |
1652 | | /* This derives the early traffic and early exporter secrets. */ |
1653 | | static SECStatus |
1654 | | tls13_DeriveEarlySecrets(sslSocket *ss) |
1655 | 0 | { |
1656 | 0 | SECStatus rv; |
1657 | 0 | PORT_Assert(ss->ssl3.hs.currentSecret); |
1658 | 0 | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1659 | 0 | kHkdfLabelClient, |
1660 | 0 | kHkdfLabelEarlyTrafficSecret, |
1661 | 0 | keylogLabelClientEarlyTrafficSecret, |
1662 | 0 | &ss->ssl3.hs.clientEarlyTrafficSecret); |
1663 | 0 | if (rv != SECSuccess) { |
1664 | 0 | return SECFailure; |
1665 | 0 | } |
1666 | | |
1667 | 0 | if (ss->secretCallback) { |
1668 | 0 | ss->secretCallback(ss->fd, (PRUint16)TrafficKeyEarlyApplicationData, |
1669 | 0 | ss->sec.isServer ? ssl_secret_read : ssl_secret_write, |
1670 | 0 | ss->ssl3.hs.clientEarlyTrafficSecret, |
1671 | 0 | ss->secretCallbackArg); |
1672 | 0 | } |
1673 | |
|
1674 | 0 | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1675 | 0 | NULL, kHkdfLabelEarlyExporterSecret, |
1676 | 0 | keylogLabelEarlyExporterSecret, |
1677 | 0 | &ss->ssl3.hs.earlyExporterSecret); |
1678 | 0 | if (rv != SECSuccess) { |
1679 | 0 | return SECFailure; |
1680 | 0 | } |
1681 | | |
1682 | 0 | return SECSuccess; |
1683 | 0 | } |
1684 | | |
1685 | | static SECStatus |
1686 | | tls13_ComputeHandshakeSecret(sslSocket *ss) |
1687 | 5.12k | { |
1688 | 5.12k | SECStatus rv; |
1689 | 5.12k | PK11SymKey *derivedSecret = NULL; |
1690 | 5.12k | PK11SymKey *newSecret = NULL; |
1691 | 5.12k | SSL_TRC(5, ("%d: TLS13[%d]: compute handshake secret (%s)", |
1692 | 5.12k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1693 | | |
1694 | | /* If no PSK, generate the default early secret. */ |
1695 | 5.12k | if (!ss->ssl3.hs.currentSecret) { |
1696 | 5.11k | PORT_Assert(!ss->xtnData.selectedPsk); |
1697 | 5.11k | rv = tls13_HkdfExtract(NULL, NULL, |
1698 | 5.11k | tls13_GetHash(ss), &ss->ssl3.hs.currentSecret); |
1699 | 5.11k | if (rv != SECSuccess) { |
1700 | 0 | return SECFailure; |
1701 | 0 | } |
1702 | 5.11k | } |
1703 | 5.12k | PORT_Assert(ss->ssl3.hs.currentSecret); |
1704 | 5.12k | PORT_Assert(ss->ssl3.hs.dheSecret); |
1705 | | |
1706 | | /* Derive-Secret(., "derived", "") */ |
1707 | 5.12k | rv = tls13_DeriveSecretNullHash(ss, ss->ssl3.hs.currentSecret, |
1708 | 5.12k | kHkdfLabelDerivedSecret, |
1709 | 5.12k | strlen(kHkdfLabelDerivedSecret), |
1710 | 5.12k | &derivedSecret, tls13_GetHash(ss)); |
1711 | 5.12k | if (rv != SECSuccess) { |
1712 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1713 | 0 | return rv; |
1714 | 0 | } |
1715 | | |
1716 | | /* HKDF-Extract(ECDHE, .) = Handshake Secret */ |
1717 | 5.12k | rv = tls13_HkdfExtract(derivedSecret, ss->ssl3.hs.dheSecret, |
1718 | 5.12k | tls13_GetHash(ss), &newSecret); |
1719 | 5.12k | PK11_FreeSymKey(derivedSecret); |
1720 | 5.12k | if (rv != SECSuccess) { |
1721 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1722 | 0 | return rv; |
1723 | 0 | } |
1724 | | |
1725 | 5.12k | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
1726 | 5.12k | ss->ssl3.hs.currentSecret = newSecret; |
1727 | 5.12k | return SECSuccess; |
1728 | 5.12k | } |
1729 | | |
1730 | | static SECStatus |
1731 | | tls13_ComputeHandshakeSecrets(sslSocket *ss) |
1732 | 5.12k | { |
1733 | 5.12k | SECStatus rv; |
1734 | 5.12k | PK11SymKey *derivedSecret = NULL; |
1735 | 5.12k | PK11SymKey *newSecret = NULL; |
1736 | | |
1737 | 5.12k | PK11_FreeSymKey(ss->ssl3.hs.dheSecret); |
1738 | 5.12k | ss->ssl3.hs.dheSecret = NULL; |
1739 | | |
1740 | 5.12k | SSL_TRC(5, ("%d: TLS13[%d]: compute handshake secrets (%s)", |
1741 | 5.12k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1742 | | |
1743 | | /* Now compute |*HsTrafficSecret| */ |
1744 | 5.12k | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1745 | 5.12k | kHkdfLabelClient, |
1746 | 5.12k | kHkdfLabelHandshakeTrafficSecret, |
1747 | 5.12k | keylogLabelClientHsTrafficSecret, |
1748 | 5.12k | &ss->ssl3.hs.clientHsTrafficSecret); |
1749 | 5.12k | if (rv != SECSuccess) { |
1750 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1751 | 0 | return rv; |
1752 | 0 | } |
1753 | 5.12k | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1754 | 5.12k | kHkdfLabelServer, |
1755 | 5.12k | kHkdfLabelHandshakeTrafficSecret, |
1756 | 5.12k | keylogLabelServerHsTrafficSecret, |
1757 | 5.12k | &ss->ssl3.hs.serverHsTrafficSecret); |
1758 | 5.12k | if (rv != SECSuccess) { |
1759 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1760 | 0 | return rv; |
1761 | 0 | } |
1762 | | |
1763 | 5.12k | if (ss->secretCallback) { |
1764 | 0 | SSLSecretDirection dir = |
1765 | 0 | ss->sec.isServer ? ssl_secret_read : ssl_secret_write; |
1766 | 0 | ss->secretCallback(ss->fd, (PRUint16)TrafficKeyHandshake, dir, |
1767 | 0 | ss->ssl3.hs.clientHsTrafficSecret, |
1768 | 0 | ss->secretCallbackArg); |
1769 | 0 | dir = ss->sec.isServer ? ssl_secret_write : ssl_secret_read; |
1770 | 0 | ss->secretCallback(ss->fd, (PRUint16)TrafficKeyHandshake, dir, |
1771 | 0 | ss->ssl3.hs.serverHsTrafficSecret, |
1772 | 0 | ss->secretCallbackArg); |
1773 | 0 | } |
1774 | | |
1775 | 5.12k | SSL_TRC(5, ("%d: TLS13[%d]: compute master secret (%s)", |
1776 | 5.12k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
1777 | | |
1778 | | /* Crank HKDF forward to make master secret, which we |
1779 | | * stuff in current secret. */ |
1780 | 5.12k | rv = tls13_DeriveSecretNullHash(ss, ss->ssl3.hs.currentSecret, |
1781 | 5.12k | kHkdfLabelDerivedSecret, |
1782 | 5.12k | strlen(kHkdfLabelDerivedSecret), |
1783 | 5.12k | &derivedSecret, tls13_GetHash(ss)); |
1784 | 5.12k | if (rv != SECSuccess) { |
1785 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1786 | 0 | return rv; |
1787 | 0 | } |
1788 | 5.12k | rv = tls13_HkdfExtract(derivedSecret, |
1789 | 5.12k | NULL, |
1790 | 5.12k | tls13_GetHash(ss), |
1791 | 5.12k | &newSecret); |
1792 | 5.12k | PK11_FreeSymKey(derivedSecret); |
1793 | 5.12k | if (rv != SECSuccess) { |
1794 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
1795 | 0 | return SECFailure; |
1796 | 0 | } |
1797 | 5.12k | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
1798 | 5.12k | ss->ssl3.hs.currentSecret = newSecret; |
1799 | | |
1800 | 5.12k | return SECSuccess; |
1801 | 5.12k | } |
1802 | | |
1803 | | static SECStatus |
1804 | | tls13_ComputeApplicationSecrets(sslSocket *ss) |
1805 | 2.74k | { |
1806 | 2.74k | SECStatus rv; |
1807 | | |
1808 | 2.74k | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1809 | 2.74k | kHkdfLabelClient, |
1810 | 2.74k | kHkdfLabelApplicationTrafficSecret, |
1811 | 2.74k | keylogLabelClientTrafficSecret, |
1812 | 2.74k | &ss->ssl3.hs.clientTrafficSecret); |
1813 | 2.74k | if (rv != SECSuccess) { |
1814 | 0 | return SECFailure; |
1815 | 0 | } |
1816 | 2.74k | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1817 | 2.74k | kHkdfLabelServer, |
1818 | 2.74k | kHkdfLabelApplicationTrafficSecret, |
1819 | 2.74k | keylogLabelServerTrafficSecret, |
1820 | 2.74k | &ss->ssl3.hs.serverTrafficSecret); |
1821 | 2.74k | if (rv != SECSuccess) { |
1822 | 0 | return SECFailure; |
1823 | 0 | } |
1824 | | |
1825 | 2.74k | if (ss->secretCallback) { |
1826 | 0 | SSLSecretDirection dir = |
1827 | 0 | ss->sec.isServer ? ssl_secret_read : ssl_secret_write; |
1828 | 0 | ss->secretCallback(ss->fd, (PRUint16)TrafficKeyApplicationData, |
1829 | 0 | dir, ss->ssl3.hs.clientTrafficSecret, |
1830 | 0 | ss->secretCallbackArg); |
1831 | 0 | dir = ss->sec.isServer ? ssl_secret_write : ssl_secret_read; |
1832 | 0 | ss->secretCallback(ss->fd, (PRUint16)TrafficKeyApplicationData, |
1833 | 0 | dir, ss->ssl3.hs.serverTrafficSecret, |
1834 | 0 | ss->secretCallbackArg); |
1835 | 0 | } |
1836 | | |
1837 | 2.74k | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1838 | 2.74k | NULL, kHkdfLabelExporterMasterSecret, |
1839 | 2.74k | keylogLabelExporterSecret, |
1840 | 2.74k | &ss->ssl3.hs.exporterSecret); |
1841 | 2.74k | if (rv != SECSuccess) { |
1842 | 0 | return SECFailure; |
1843 | 0 | } |
1844 | | |
1845 | 2.74k | return SECSuccess; |
1846 | 2.74k | } |
1847 | | |
1848 | | static SECStatus |
1849 | | tls13_ComputeFinalSecrets(sslSocket *ss) |
1850 | 555 | { |
1851 | 555 | SECStatus rv; |
1852 | | |
1853 | 555 | PORT_Assert(!ss->ssl3.crSpec->masterSecret); |
1854 | 555 | PORT_Assert(!ss->ssl3.cwSpec->masterSecret); |
1855 | 555 | PORT_Assert(ss->ssl3.hs.currentSecret); |
1856 | 555 | rv = tls13_DeriveSecretWrap(ss, ss->ssl3.hs.currentSecret, |
1857 | 555 | NULL, kHkdfLabelResumptionMasterSecret, |
1858 | 555 | NULL, |
1859 | 555 | &ss->ssl3.hs.resumptionMasterSecret); |
1860 | 555 | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
1861 | 555 | ss->ssl3.hs.currentSecret = NULL; |
1862 | 555 | if (rv != SECSuccess) { |
1863 | 0 | return SECFailure; |
1864 | 0 | } |
1865 | | |
1866 | 555 | return SECSuccess; |
1867 | 555 | } |
1868 | | |
1869 | | static void |
1870 | | tls13_RestoreCipherInfo(sslSocket *ss, sslSessionID *sid) |
1871 | 0 | { |
1872 | | /* Set these to match the cached value. |
1873 | | * TODO(ekr@rtfm.com): Make a version with the "true" values. |
1874 | | * Bug 1256137. |
1875 | | */ |
1876 | 0 | ss->sec.authType = sid->authType; |
1877 | 0 | ss->sec.authKeyBits = sid->authKeyBits; |
1878 | 0 | ss->sec.originalKeaGroup = ssl_LookupNamedGroup(sid->keaGroup); |
1879 | 0 | ss->sec.signatureScheme = sid->sigScheme; |
1880 | 0 | } |
1881 | | |
1882 | | /* Check whether resumption-PSK is allowed. */ |
1883 | | static PRBool |
1884 | | tls13_CanResume(sslSocket *ss, const sslSessionID *sid) |
1885 | 52 | { |
1886 | 52 | const sslServerCert *sc; |
1887 | | |
1888 | 52 | if (!sid) { |
1889 | 0 | return PR_FALSE; |
1890 | 0 | } |
1891 | | |
1892 | 52 | if (sid->version != ss->version) { |
1893 | 30 | return PR_FALSE; |
1894 | 30 | } |
1895 | | |
1896 | 22 | #ifdef UNSAFE_FUZZER_MODE |
1897 | | /* When fuzzing, sid could contain garbage that will crash tls13_GetHashForCipherSuite. |
1898 | | * Do a direct comparison of cipher suites. This makes us refuse to resume when the |
1899 | | * protocol allows it, but resumption is discretionary anyway. */ |
1900 | 22 | if (sid->u.ssl3.cipherSuite != ss->ssl3.hs.cipher_suite) { |
1901 | | #else |
1902 | | if (tls13_GetHashForCipherSuite(sid->u.ssl3.cipherSuite) != tls13_GetHashForCipherSuite(ss->ssl3.hs.cipher_suite)) { |
1903 | | #endif |
1904 | 16 | return PR_FALSE; |
1905 | 16 | } |
1906 | | |
1907 | | /* Server sids don't remember the server cert we previously sent, but they |
1908 | | * do remember the type of certificate we originally used, so we can locate |
1909 | | * it again, provided that the current ssl socket has had its server certs |
1910 | | * configured the same as the previous one. */ |
1911 | 6 | sc = ssl_FindServerCert(ss, sid->authType, sid->namedCurve); |
1912 | 6 | if (!sc || !sc->serverCert) { |
1913 | 3 | return PR_FALSE; |
1914 | 3 | } |
1915 | | |
1916 | 3 | return PR_TRUE; |
1917 | 6 | } |
1918 | | |
1919 | | static PRBool |
1920 | | tls13_CanNegotiateZeroRtt(sslSocket *ss, const sslSessionID *sid) |
1921 | 325 | { |
1922 | 325 | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_sent); |
1923 | 325 | sslPsk *psk = ss->xtnData.selectedPsk; |
1924 | | |
1925 | 325 | if (!ss->opt.enable0RttData) { |
1926 | 166 | return PR_FALSE; |
1927 | 166 | } |
1928 | 159 | if (!psk) { |
1929 | 159 | return PR_FALSE; |
1930 | 159 | } |
1931 | 0 | if (psk->zeroRttSuite == TLS_NULL_WITH_NULL_NULL) { |
1932 | 0 | return PR_FALSE; |
1933 | 0 | } |
1934 | 0 | if (!psk->maxEarlyData) { |
1935 | 0 | return PR_FALSE; |
1936 | 0 | } |
1937 | 0 | if (ss->ssl3.hs.cipher_suite != psk->zeroRttSuite) { |
1938 | 0 | return PR_FALSE; |
1939 | 0 | } |
1940 | 0 | if (psk->type == ssl_psk_resume) { |
1941 | 0 | if (!sid) { |
1942 | 0 | return PR_FALSE; |
1943 | 0 | } |
1944 | 0 | PORT_Assert(sid->u.ssl3.locked.sessionTicket.flags & ticket_allow_early_data); |
1945 | 0 | PORT_Assert(ss->statelessResume); |
1946 | 0 | if (!ss->statelessResume) { |
1947 | 0 | return PR_FALSE; |
1948 | 0 | } |
1949 | 0 | if (SECITEM_CompareItem(&ss->xtnData.nextProto, |
1950 | 0 | &sid->u.ssl3.alpnSelection) != 0) { |
1951 | 0 | return PR_FALSE; |
1952 | 0 | } |
1953 | 0 | } else if (psk->type != ssl_psk_external) { |
1954 | 0 | PORT_Assert(0); |
1955 | 0 | return PR_FALSE; |
1956 | 0 | } |
1957 | | |
1958 | 0 | if (tls13_IsReplay(ss, sid)) { |
1959 | 0 | return PR_FALSE; |
1960 | 0 | } |
1961 | | |
1962 | 0 | return PR_TRUE; |
1963 | 0 | } |
1964 | | |
1965 | | /* Called from tls13_HandleClientHelloPart2 to update the state of 0-RTT handling. |
1966 | | * |
1967 | | * 0-RTT is only permitted if: |
1968 | | * 1. The early data extension was present. |
1969 | | * 2. We are resuming a session. |
1970 | | * 3. The 0-RTT option is set. |
1971 | | * 4. The ticket allowed 0-RTT. |
1972 | | * 5. We negotiated the same ALPN value as in the ticket. |
1973 | | */ |
1974 | | static void |
1975 | | tls13_NegotiateZeroRtt(sslSocket *ss, const sslSessionID *sid) |
1976 | 2.92k | { |
1977 | 2.92k | SSL_TRC(3, ("%d: TLS13[%d]: negotiate 0-RTT %p", |
1978 | 2.92k | SSL_GETPID(), ss->fd, sid)); |
1979 | | |
1980 | | /* tls13_ServerHandleEarlyDataXtn sets this to ssl_0rtt_sent, so this will |
1981 | | * be ssl_0rtt_none unless early_data is present. */ |
1982 | 2.92k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_none) { |
1983 | 2.60k | return; |
1984 | 2.60k | } |
1985 | | |
1986 | 325 | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_ignored) { |
1987 | | /* HelloRetryRequest causes 0-RTT to be ignored. On the second |
1988 | | * ClientHello, reset the ignore state so that decryption failure is |
1989 | | * handled normally. */ |
1990 | 0 | if (ss->ssl3.hs.zeroRttIgnore == ssl_0rtt_ignore_hrr) { |
1991 | 0 | PORT_Assert(ss->ssl3.hs.helloRetry); |
1992 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_none; |
1993 | 0 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_none; |
1994 | 0 | } else { |
1995 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: application ignored 0-RTT", |
1996 | 0 | SSL_GETPID(), ss->fd)); |
1997 | 0 | } |
1998 | 0 | return; |
1999 | 0 | } |
2000 | | |
2001 | 325 | if (!tls13_CanNegotiateZeroRtt(ss, sid)) { |
2002 | 325 | SSL_TRC(3, ("%d: TLS13[%d]: ignore 0-RTT", SSL_GETPID(), ss->fd)); |
2003 | 325 | ss->ssl3.hs.zeroRttState = ssl_0rtt_ignored; |
2004 | 325 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_trial; |
2005 | 325 | return; |
2006 | 325 | } |
2007 | | |
2008 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: enable 0-RTT", SSL_GETPID(), ss->fd)); |
2009 | 0 | PORT_Assert(ss->xtnData.selectedPsk); |
2010 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_accepted; |
2011 | 0 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_none; |
2012 | 0 | ss->ssl3.hs.zeroRttSuite = ss->ssl3.hs.cipher_suite; |
2013 | 0 | ss->ssl3.hs.preliminaryInfo |= ssl_preinfo_0rtt_cipher_suite; |
2014 | 0 | } |
2015 | | |
2016 | | /* Check if the offered group is acceptable. */ |
2017 | | static PRBool |
2018 | | tls13_isGroupAcceptable(const sslNamedGroupDef *offered, |
2019 | | const sslNamedGroupDef *preferredGroup) |
2020 | 115 | { |
2021 | | /* We accept epsilon (e) bits around the offered group size. */ |
2022 | 115 | const unsigned int e = 2; |
2023 | | |
2024 | 115 | PORT_Assert(offered); |
2025 | 115 | PORT_Assert(preferredGroup); |
2026 | | |
2027 | 115 | if (offered->bits >= preferredGroup->bits - e && |
2028 | 114 | offered->bits <= preferredGroup->bits + e) { |
2029 | 95 | return PR_TRUE; |
2030 | 95 | } |
2031 | | |
2032 | 20 | return PR_FALSE; |
2033 | 115 | } |
2034 | | |
2035 | | /* Find remote key share for given group and return it. |
2036 | | * Returns NULL if no key share is found. */ |
2037 | | static TLS13KeyShareEntry * |
2038 | | tls13_FindKeyShareEntry(sslSocket *ss, const sslNamedGroupDef *group) |
2039 | 4.14k | { |
2040 | 4.14k | PRCList *cur_p = PR_NEXT_LINK(&ss->xtnData.remoteKeyShares); |
2041 | 4.86k | while (cur_p != &ss->xtnData.remoteKeyShares) { |
2042 | 3.68k | TLS13KeyShareEntry *offer = (TLS13KeyShareEntry *)cur_p; |
2043 | 3.68k | if (offer->group == group) { |
2044 | 2.96k | return offer; |
2045 | 2.96k | } |
2046 | 721 | cur_p = PR_NEXT_LINK(cur_p); |
2047 | 721 | } |
2048 | 1.18k | return NULL; |
2049 | 4.14k | } |
2050 | | |
2051 | | static SECStatus |
2052 | | tls13_NegotiateKeyExchange(sslSocket *ss, |
2053 | | const sslNamedGroupDef **requestedGroup, |
2054 | | TLS13KeyShareEntry **clientShare) |
2055 | 3.99k | { |
2056 | 3.99k | unsigned int index; |
2057 | 3.99k | TLS13KeyShareEntry *entry = NULL; |
2058 | 3.99k | const sslNamedGroupDef *preferredGroup = NULL; |
2059 | | |
2060 | | /* We insist on DHE. */ |
2061 | 3.99k | if (ssl3_ExtensionNegotiated(ss, ssl_tls13_pre_shared_key_xtn)) { |
2062 | 72 | if (!ssl3_ExtensionNegotiated(ss, ssl_tls13_psk_key_exchange_modes_xtn)) { |
2063 | 22 | FATAL_ERROR(ss, SSL_ERROR_MISSING_PSK_KEY_EXCHANGE_MODES, |
2064 | 22 | missing_extension); |
2065 | 22 | return SECFailure; |
2066 | 22 | } |
2067 | | /* Since the server insists on DHE to provide forward secracy, for |
2068 | | * every other PskKem value but DHE stateless resumption is disabled, |
2069 | | * this includes other specified and GREASE values. */ |
2070 | 50 | if (!memchr(ss->xtnData.psk_ke_modes.data, tls13_psk_dh_ke, |
2071 | 50 | ss->xtnData.psk_ke_modes.len)) { |
2072 | 45 | SSL_TRC(3, ("%d: TLS13[%d]: client offered PSK without DH", |
2073 | 45 | SSL_GETPID(), ss->fd)); |
2074 | 45 | ss->statelessResume = PR_FALSE; |
2075 | 45 | } |
2076 | 50 | } |
2077 | | |
2078 | | /* Now figure out which key share we like the best out of the |
2079 | | * mutually supported groups, regardless of what the client offered |
2080 | | * for key shares. |
2081 | | */ |
2082 | 3.97k | if (!ssl3_ExtensionNegotiated(ss, ssl_supported_groups_xtn)) { |
2083 | 42 | FATAL_ERROR(ss, SSL_ERROR_MISSING_SUPPORTED_GROUPS_EXTENSION, |
2084 | 42 | missing_extension); |
2085 | 42 | return SECFailure; |
2086 | 42 | } |
2087 | | |
2088 | 3.93k | SSL_TRC(3, ("%d: TLS13[%d]: selected KE = %s", SSL_GETPID(), |
2089 | 3.93k | ss->fd, ss->statelessResume || ss->xtnData.selectedPsk ? "PSK + (EC)DHE" : "(EC)DHE")); |
2090 | | |
2091 | | /* Find the preferred group and an according client key share available. */ |
2092 | 17.5k | for (index = 0; index < SSL_NAMED_GROUP_COUNT; ++index) { |
2093 | | /* Continue to the next group if this one is not enabled. */ |
2094 | 17.5k | if (!ss->namedGroupPreferences[index]) { |
2095 | | /* There's a gap in the preferred groups list. Assume this is a group |
2096 | | * that's not supported by the client but preferred by the server. */ |
2097 | 13.3k | if (preferredGroup) { |
2098 | 851 | entry = NULL; |
2099 | 851 | break; |
2100 | 851 | } |
2101 | 12.5k | continue; |
2102 | 13.3k | } |
2103 | | |
2104 | | /* Check if the client sent a key share for this group. */ |
2105 | 4.14k | entry = tls13_FindKeyShareEntry(ss, ss->namedGroupPreferences[index]); |
2106 | | |
2107 | 4.14k | if (preferredGroup) { |
2108 | | /* We already found our preferred group but the group didn't have a share. */ |
2109 | 222 | if (entry) { |
2110 | | /* The client sent a key share with group ss->namedGroupPreferences[index] */ |
2111 | 115 | if (tls13_isGroupAcceptable(ss->namedGroupPreferences[index], |
2112 | 115 | preferredGroup)) { |
2113 | | /* This is not the preferred group, but it's acceptable */ |
2114 | 95 | preferredGroup = ss->namedGroupPreferences[index]; |
2115 | 95 | } else { |
2116 | | /* The proposed group is not acceptable. */ |
2117 | 20 | entry = NULL; |
2118 | 20 | } |
2119 | 115 | } |
2120 | 222 | break; |
2121 | 3.92k | } else { |
2122 | | /* The first enabled group is the preferred group. */ |
2123 | 3.92k | preferredGroup = ss->namedGroupPreferences[index]; |
2124 | 3.92k | if (entry) { |
2125 | 2.85k | break; |
2126 | 2.85k | } |
2127 | 3.92k | } |
2128 | 4.14k | } |
2129 | | |
2130 | 3.93k | if (!preferredGroup) { |
2131 | 11 | FATAL_ERROR(ss, SSL_ERROR_NO_CYPHER_OVERLAP, handshake_failure); |
2132 | 11 | return SECFailure; |
2133 | 11 | } |
2134 | 3.92k | SSL_TRC(3, ("%d: TLS13[%d]: group = %d", SSL_GETPID(), ss->fd, |
2135 | 3.92k | preferredGroup->name)); |
2136 | | |
2137 | | /* Either provide a share, or provide a group that should be requested in a |
2138 | | * HelloRetryRequest, but not both. */ |
2139 | 3.92k | if (entry) { |
2140 | 2.94k | PORT_Assert(preferredGroup == entry->group); |
2141 | 2.94k | *clientShare = entry; |
2142 | 2.94k | *requestedGroup = NULL; |
2143 | 2.94k | } else { |
2144 | 978 | *clientShare = NULL; |
2145 | 978 | *requestedGroup = preferredGroup; |
2146 | 978 | } |
2147 | 3.92k | return SECSuccess; |
2148 | 3.93k | } |
2149 | | |
2150 | | SECStatus |
2151 | | tls13_SelectServerCert(sslSocket *ss) |
2152 | 2.94k | { |
2153 | 2.94k | PRCList *cursor; |
2154 | 2.94k | SECStatus rv; |
2155 | | |
2156 | 2.94k | if (!ssl3_ExtensionNegotiated(ss, ssl_signature_algorithms_xtn)) { |
2157 | 7 | FATAL_ERROR(ss, SSL_ERROR_MISSING_SIGNATURE_ALGORITHMS_EXTENSION, |
2158 | 7 | missing_extension); |
2159 | 7 | return SECFailure; |
2160 | 7 | } |
2161 | | |
2162 | | /* This picks the first certificate that has: |
2163 | | * a) the right authentication method, and |
2164 | | * b) the right named curve (EC only) |
2165 | | * |
2166 | | * We might want to do some sort of ranking here later. For now, it's all |
2167 | | * based on what order they are configured in. */ |
2168 | 2.93k | for (cursor = PR_NEXT_LINK(&ss->serverCerts); |
2169 | 4.99k | cursor != &ss->serverCerts; |
2170 | 4.98k | cursor = PR_NEXT_LINK(cursor)) { |
2171 | 4.98k | sslServerCert *cert = (sslServerCert *)cursor; |
2172 | | |
2173 | 4.98k | if (SSL_CERT_IS_ONLY(cert, ssl_auth_rsa_decrypt)) { |
2174 | 0 | continue; |
2175 | 0 | } |
2176 | | |
2177 | 4.98k | rv = ssl_PickSignatureScheme(ss, |
2178 | 4.98k | cert->serverCert, |
2179 | 4.98k | cert->serverKeyPair->pubKey, |
2180 | 4.98k | cert->serverKeyPair->privKey, |
2181 | 4.98k | ss->xtnData.sigSchemes, |
2182 | 4.98k | ss->xtnData.numSigSchemes, |
2183 | 4.98k | PR_FALSE, |
2184 | 4.98k | &ss->ssl3.hs.signatureScheme); |
2185 | 4.98k | if (rv == SECSuccess) { |
2186 | | /* Found one. */ |
2187 | 2.92k | ss->sec.serverCert = cert; |
2188 | | |
2189 | | /* If we can use a delegated credential (DC) for authentication in |
2190 | | * the current handshake, then commit to using it now. We'll send a |
2191 | | * DC as an extension and use the DC private key to sign the |
2192 | | * handshake. |
2193 | | * |
2194 | | * This sets the signature scheme to be the signature scheme |
2195 | | * indicated by the DC. |
2196 | | */ |
2197 | 2.92k | rv = tls13_MaybeSetDelegatedCredential(ss); |
2198 | 2.92k | if (rv != SECSuccess) { |
2199 | 0 | return SECFailure; /* Failure indicates an internal error. */ |
2200 | 0 | } |
2201 | | |
2202 | 2.92k | ss->sec.authType = ss->ssl3.hs.kea_def_mutable.authKeyType = |
2203 | 2.92k | ssl_SignatureSchemeToAuthType(ss->ssl3.hs.signatureScheme); |
2204 | 2.92k | ss->sec.authKeyBits = cert->serverKeyBits; |
2205 | 2.92k | return SECSuccess; |
2206 | 2.92k | } |
2207 | 4.98k | } |
2208 | | |
2209 | 12 | FATAL_ERROR(ss, SSL_ERROR_UNSUPPORTED_SIGNATURE_ALGORITHM, |
2210 | 12 | handshake_failure); |
2211 | 12 | return SECFailure; |
2212 | 2.93k | } |
2213 | | |
2214 | | /* Note: |requestedGroup| is non-NULL when we send a key_share extension. */ |
2215 | | static SECStatus |
2216 | | tls13_MaybeSendHelloRetry(sslSocket *ss, const sslNamedGroupDef *requestedGroup, |
2217 | | PRBool *hrrSent) |
2218 | 3.92k | { |
2219 | 3.92k | SSLHelloRetryRequestAction action = ssl_hello_retry_accept; |
2220 | 3.92k | PRUint8 token[256] = { 0 }; |
2221 | 3.92k | unsigned int tokenLen = 0; |
2222 | 3.92k | SECStatus rv; |
2223 | | |
2224 | 3.92k | if (ss->hrrCallback) { |
2225 | 0 | action = ss->hrrCallback(!ss->ssl3.hs.helloRetry, |
2226 | 0 | ss->xtnData.applicationToken.data, |
2227 | 0 | ss->xtnData.applicationToken.len, |
2228 | 0 | token, &tokenLen, sizeof(token), |
2229 | 0 | ss->hrrCallbackArg); |
2230 | 0 | } |
2231 | | |
2232 | | /* These use SSL3_SendAlert directly to avoid an assertion in |
2233 | | * tls13_FatalError(), which is ordinarily OK. */ |
2234 | 3.92k | if (action == ssl_hello_retry_request && ss->ssl3.hs.helloRetry) { |
2235 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, internal_error); |
2236 | 0 | PORT_SetError(SSL_ERROR_APP_CALLBACK_ERROR); |
2237 | 0 | return SECFailure; |
2238 | 0 | } |
2239 | | |
2240 | 3.92k | if (action != ssl_hello_retry_request && tokenLen) { |
2241 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, internal_error); |
2242 | 0 | PORT_SetError(SSL_ERROR_APP_CALLBACK_ERROR); |
2243 | 0 | return SECFailure; |
2244 | 0 | } |
2245 | | |
2246 | 3.92k | if (tokenLen > sizeof(token)) { |
2247 | 0 | (void)SSL3_SendAlert(ss, alert_fatal, internal_error); |
2248 | 0 | PORT_SetError(SSL_ERROR_APP_CALLBACK_ERROR); |
2249 | 0 | return SECFailure; |
2250 | 0 | } |
2251 | | |
2252 | 3.92k | if (action == ssl_hello_retry_fail) { |
2253 | 0 | FATAL_ERROR(ss, SSL_ERROR_APPLICATION_ABORT, handshake_failure); |
2254 | 0 | return SECFailure; |
2255 | 0 | } |
2256 | | |
2257 | 3.92k | if (action == ssl_hello_retry_reject_0rtt) { |
2258 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_ignored; |
2259 | 0 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_trial; |
2260 | 0 | } |
2261 | | |
2262 | 3.92k | if (!requestedGroup && action != ssl_hello_retry_request) { |
2263 | 2.94k | return SECSuccess; |
2264 | 2.94k | } |
2265 | | |
2266 | 978 | rv = tls13_SendHelloRetryRequest(ss, requestedGroup, token, tokenLen); |
2267 | 978 | if (rv != SECSuccess) { |
2268 | 0 | return SECFailure; /* Code already set. */ |
2269 | 0 | } |
2270 | | |
2271 | | /* We may have received ECH, but have to start over with CH2. */ |
2272 | 978 | ss->ssl3.hs.echAccepted = PR_FALSE; |
2273 | 978 | PK11_HPKE_DestroyContext(ss->ssl3.hs.echHpkeCtx, PR_TRUE); |
2274 | 978 | ss->ssl3.hs.echHpkeCtx = NULL; |
2275 | | |
2276 | 978 | *hrrSent = PR_TRUE; |
2277 | 978 | return SECSuccess; |
2278 | 978 | } |
2279 | | |
2280 | | static SECStatus |
2281 | | tls13_NegotiateAuthentication(sslSocket *ss) |
2282 | 2.94k | { |
2283 | 2.94k | if (ss->statelessResume) { |
2284 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: selected resumption PSK authentication", |
2285 | 0 | SSL_GETPID(), ss->fd)); |
2286 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_none; |
2287 | 0 | ss->ssl3.hs.kea_def_mutable.authKeyType = ssl_auth_psk; |
2288 | | /* Overwritten by tls13_RestoreCipherInfo. */ |
2289 | 0 | ss->sec.authType = ssl_auth_psk; |
2290 | 0 | return SECSuccess; |
2291 | 2.94k | } else if (ss->xtnData.selectedPsk) { |
2292 | | /* If the EPSK doesn't specify a suite, use what was negotiated. |
2293 | | * Else, only use the EPSK if we negotiated that suite. */ |
2294 | 0 | if (ss->xtnData.selectedPsk->zeroRttSuite == TLS_NULL_WITH_NULL_NULL || |
2295 | 0 | ss->ssl3.hs.cipher_suite == ss->xtnData.selectedPsk->zeroRttSuite) { |
2296 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: selected external PSK authentication", |
2297 | 0 | SSL_GETPID(), ss->fd)); |
2298 | 0 | ss->ssl3.hs.signatureScheme = ssl_sig_none; |
2299 | 0 | ss->ssl3.hs.kea_def_mutable.authKeyType = ssl_auth_psk; |
2300 | 0 | ss->sec.authType = ssl_auth_psk; |
2301 | 0 | return SECSuccess; |
2302 | 0 | } |
2303 | 0 | } |
2304 | | |
2305 | | /* If there were PSKs, they are no longer needed. */ |
2306 | 2.94k | if (ss->xtnData.selectedPsk) { |
2307 | 0 | tls13_DestroyPskList(&ss->ssl3.hs.psks); |
2308 | 0 | ss->xtnData.selectedPsk = NULL; |
2309 | 0 | } |
2310 | | |
2311 | 2.94k | SSL_TRC(3, ("%d: TLS13[%d]: selected certificate authentication", |
2312 | 2.94k | SSL_GETPID(), ss->fd)); |
2313 | 2.94k | SECStatus rv = tls13_SelectServerCert(ss); |
2314 | 2.94k | if (rv != SECSuccess) { |
2315 | 19 | return SECFailure; |
2316 | 19 | } |
2317 | 2.92k | return SECSuccess; |
2318 | 2.94k | } |
2319 | | /* Called from ssl3_HandleClientHello after we have parsed the |
2320 | | * ClientHello and are sure that we are going to do TLS 1.3 |
2321 | | * or fail. */ |
2322 | | SECStatus |
2323 | | tls13_HandleClientHelloPart2(sslSocket *ss, |
2324 | | const SECItem *suites, |
2325 | | sslSessionID *sid, |
2326 | | const PRUint8 *msg, |
2327 | | unsigned int len) |
2328 | 4.40k | { |
2329 | 4.40k | SECStatus rv; |
2330 | 4.40k | SSL3Statistics *ssl3stats = SSL_GetStatistics(); |
2331 | 4.40k | const sslNamedGroupDef *requestedGroup = NULL; |
2332 | 4.40k | TLS13KeyShareEntry *clientShare = NULL; |
2333 | 4.40k | ssl3CipherSuite previousCipherSuite = 0; |
2334 | 4.40k | const sslNamedGroupDef *previousGroup = NULL; |
2335 | 4.40k | PRBool hrr = PR_FALSE; |
2336 | 4.40k | PRBool previousOfferedEch; |
2337 | | |
2338 | | /* If the legacy_version field is set to 0x300 or smaller, |
2339 | | * reject the connection with protocol_version alert. */ |
2340 | 4.40k | if (ss->clientHelloVersion <= SSL_LIBRARY_VERSION_3_0) { |
2341 | 43 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, protocol_version); |
2342 | 43 | goto loser; |
2343 | 43 | } |
2344 | | |
2345 | 4.36k | ss->ssl3.hs.endOfFlight = PR_TRUE; |
2346 | | |
2347 | 4.36k | if (ssl3_ExtensionNegotiated(ss, ssl_tls13_early_data_xtn)) { |
2348 | 517 | ss->ssl3.hs.zeroRttState = ssl_0rtt_sent; |
2349 | 517 | } |
2350 | | |
2351 | | /* Negotiate cipher suite. */ |
2352 | 4.36k | rv = ssl3_NegotiateCipherSuite(ss, suites, PR_FALSE); |
2353 | 4.36k | if (rv != SECSuccess) { |
2354 | 94 | FATAL_ERROR(ss, PORT_GetError(), handshake_failure); |
2355 | 94 | goto loser; |
2356 | 94 | } |
2357 | | |
2358 | | /* If we are going around again, then we should make sure that the cipher |
2359 | | * suite selection doesn't change. That's a sign of client shennanigans. */ |
2360 | 4.26k | if (ss->ssl3.hs.helloRetry) { |
2361 | | |
2362 | | /* Update sequence numbers before checking the cookie so that any alerts |
2363 | | * we generate are sent with the right sequence numbers. */ |
2364 | 268 | if (IS_DTLS(ss)) { |
2365 | | /* Count the first ClientHello and the HelloRetryRequest. */ |
2366 | 125 | ss->ssl3.hs.sendMessageSeq = 1; |
2367 | 125 | ss->ssl3.hs.recvMessageSeq = 1; |
2368 | 125 | ssl_GetSpecWriteLock(ss); |
2369 | | /* Increase the write sequence number. The read sequence number |
2370 | | * will be reset after this to early data or handshake. */ |
2371 | 125 | ss->ssl3.cwSpec->nextSeqNum = 1; |
2372 | 125 | ssl_ReleaseSpecWriteLock(ss); |
2373 | 125 | } |
2374 | | |
2375 | 268 | if (!ssl3_ExtensionNegotiated(ss, ssl_tls13_cookie_xtn) || |
2376 | 136 | !ss->xtnData.cookie.len) { |
2377 | 136 | FATAL_ERROR(ss, SSL_ERROR_MISSING_COOKIE_EXTENSION, |
2378 | 136 | missing_extension); |
2379 | 136 | goto loser; |
2380 | 136 | } |
2381 | 132 | PRINT_BUF(50, (ss, "Client sent cookie", |
2382 | 132 | ss->xtnData.cookie.data, ss->xtnData.cookie.len)); |
2383 | | |
2384 | 132 | rv = tls13_HandleHrrCookie(ss, ss->xtnData.cookie.data, |
2385 | 132 | ss->xtnData.cookie.len, |
2386 | 132 | &previousCipherSuite, |
2387 | 132 | &previousGroup, |
2388 | 132 | &previousOfferedEch, NULL, PR_TRUE); |
2389 | | |
2390 | 132 | if (rv != SECSuccess) { |
2391 | 132 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, illegal_parameter); |
2392 | 132 | goto loser; |
2393 | 132 | } |
2394 | 132 | } |
2395 | | |
2396 | | /* Now merge the ClientHello into the hash state. */ |
2397 | 3.99k | rv = ssl_HashHandshakeMessage(ss, ssl_hs_client_hello, msg, len); |
2398 | 3.99k | if (rv != SECSuccess) { |
2399 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2400 | 0 | goto loser; |
2401 | 0 | } |
2402 | | |
2403 | | /* Now create a synthetic kea_def that we can tweak. */ |
2404 | 3.99k | ss->ssl3.hs.kea_def_mutable = *ss->ssl3.hs.kea_def; |
2405 | 3.99k | ss->ssl3.hs.kea_def = &ss->ssl3.hs.kea_def_mutable; |
2406 | | |
2407 | | /* Note: We call this quite a bit earlier than with TLS 1.2 and |
2408 | | * before. */ |
2409 | 3.99k | rv = ssl3_ServerCallSNICallback(ss); |
2410 | 3.99k | if (rv != SECSuccess) { |
2411 | 0 | goto loser; /* An alert has already been sent. */ |
2412 | 0 | } |
2413 | | |
2414 | | /* Check if we could in principle resume. */ |
2415 | 3.99k | if (ss->statelessResume) { |
2416 | 52 | PORT_Assert(sid); |
2417 | 52 | if (!sid) { |
2418 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2419 | 0 | return SECFailure; |
2420 | 0 | } |
2421 | 52 | if (!tls13_CanResume(ss, sid)) { |
2422 | 49 | ss->statelessResume = PR_FALSE; |
2423 | 49 | } |
2424 | 52 | } |
2425 | | |
2426 | | /* Select key exchange. */ |
2427 | 3.99k | rv = tls13_NegotiateKeyExchange(ss, &requestedGroup, &clientShare); |
2428 | 3.99k | if (rv != SECSuccess) { |
2429 | 75 | goto loser; |
2430 | 75 | } |
2431 | | /* We should get either one of these, but not both. */ |
2432 | 3.92k | PORT_Assert((requestedGroup && !clientShare) || |
2433 | 3.92k | (!requestedGroup && clientShare)); |
2434 | | |
2435 | | /* After HelloRetryRequest, check consistency of cipher and group. */ |
2436 | 3.92k | if (ss->ssl3.hs.helloRetry) { |
2437 | 0 | PORT_Assert(previousCipherSuite); |
2438 | 0 | if (ss->ssl3.hs.cipher_suite != previousCipherSuite) { |
2439 | 0 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, |
2440 | 0 | illegal_parameter); |
2441 | 0 | goto loser; |
2442 | 0 | } |
2443 | 0 | if (!clientShare) { |
2444 | 0 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, |
2445 | 0 | illegal_parameter); |
2446 | 0 | goto loser; |
2447 | 0 | } |
2448 | | |
2449 | | /* CH1/CH2 must either both include ECH, or both exclude it. */ |
2450 | 0 | if (previousOfferedEch != (ss->xtnData.ech != NULL)) { |
2451 | 0 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, |
2452 | 0 | previousOfferedEch ? missing_extension : illegal_parameter); |
2453 | 0 | goto loser; |
2454 | 0 | } |
2455 | | |
2456 | | /* If we requested a new key share, check that the client provided just |
2457 | | * one of the right type. */ |
2458 | 0 | if (previousGroup) { |
2459 | 0 | if (PR_PREV_LINK(&ss->xtnData.remoteKeyShares) != |
2460 | 0 | PR_NEXT_LINK(&ss->xtnData.remoteKeyShares)) { |
2461 | 0 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, |
2462 | 0 | illegal_parameter); |
2463 | 0 | goto loser; |
2464 | 0 | } |
2465 | 0 | if (clientShare->group != previousGroup) { |
2466 | 0 | FATAL_ERROR(ss, SSL_ERROR_BAD_2ND_CLIENT_HELLO, |
2467 | 0 | illegal_parameter); |
2468 | 0 | goto loser; |
2469 | 0 | } |
2470 | 0 | } |
2471 | 0 | } |
2472 | | |
2473 | 3.92k | rv = tls13_MaybeSendHelloRetry(ss, requestedGroup, &hrr); |
2474 | 3.92k | if (rv != SECSuccess) { |
2475 | 0 | goto loser; |
2476 | 0 | } |
2477 | 3.92k | if (hrr) { |
2478 | 978 | if (sid) { /* Free the sid. */ |
2479 | 24 | ssl_UncacheSessionID(ss); |
2480 | 24 | ssl_FreeSID(sid); |
2481 | 24 | } |
2482 | 978 | PORT_Assert(ss->ssl3.hs.helloRetry); |
2483 | 978 | return SECSuccess; |
2484 | 978 | } |
2485 | | |
2486 | | /* Select the authentication (this is also handshake shape). */ |
2487 | 2.94k | rv = tls13_NegotiateAuthentication(ss); |
2488 | 2.94k | if (rv != SECSuccess) { |
2489 | 19 | goto loser; |
2490 | 19 | } |
2491 | | |
2492 | 2.92k | if (ss->sec.authType == ssl_auth_psk) { |
2493 | 0 | if (ss->statelessResume) { |
2494 | | /* We are now committed to trying to resume. */ |
2495 | 0 | PORT_Assert(sid); |
2496 | | /* Check that the negotiated SNI and the cached SNI match. */ |
2497 | 0 | if (SECITEM_CompareItem(&sid->u.ssl3.srvName, |
2498 | 0 | &ss->ssl3.hs.srvVirtName) != SECEqual) { |
2499 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, |
2500 | 0 | handshake_failure); |
2501 | 0 | goto loser; |
2502 | 0 | } |
2503 | | |
2504 | 0 | ss->sec.serverCert = ssl_FindServerCert(ss, sid->authType, |
2505 | 0 | sid->namedCurve); |
2506 | 0 | PORT_Assert(ss->sec.serverCert); |
2507 | |
|
2508 | 0 | rv = tls13_RecoverWrappedSharedSecret(ss, sid); |
2509 | 0 | if (rv != SECSuccess) { |
2510 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_cache_not_ok); |
2511 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2512 | 0 | goto loser; |
2513 | 0 | } |
2514 | 0 | tls13_RestoreCipherInfo(ss, sid); |
2515 | |
|
2516 | 0 | PORT_Assert(!ss->sec.localCert); |
2517 | 0 | ss->sec.localCert = CERT_DupCertificate(ss->sec.serverCert->serverCert); |
2518 | 0 | if (sid->peerCert != NULL) { |
2519 | 0 | ss->sec.peerCert = CERT_DupCertificate(sid->peerCert); |
2520 | 0 | } |
2521 | 0 | } else if (sid) { |
2522 | | /* We should never have a SID in the non-resumption case. */ |
2523 | 0 | PORT_Assert(0); |
2524 | 0 | ssl_UncacheSessionID(ss); |
2525 | 0 | ssl_FreeSID(sid); |
2526 | 0 | sid = NULL; |
2527 | 0 | } |
2528 | 0 | ssl3_RegisterExtensionSender( |
2529 | 0 | ss, &ss->xtnData, |
2530 | 0 | ssl_tls13_pre_shared_key_xtn, tls13_ServerSendPreSharedKeyXtn); |
2531 | 0 | tls13_NegotiateZeroRtt(ss, sid); |
2532 | |
|
2533 | 0 | rv = tls13_ComputeEarlySecretsWithPsk(ss); |
2534 | 0 | if (rv != SECSuccess) { |
2535 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2536 | 0 | return SECFailure; |
2537 | 0 | } |
2538 | 2.92k | } else { |
2539 | 2.92k | if (sid) { /* we had a sid, but it's no longer valid, free it */ |
2540 | 2 | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_cache_not_ok); |
2541 | 2 | ssl_UncacheSessionID(ss); |
2542 | 2 | ssl_FreeSID(sid); |
2543 | 2 | sid = NULL; |
2544 | 2 | } |
2545 | 2.92k | tls13_NegotiateZeroRtt(ss, NULL); |
2546 | 2.92k | } |
2547 | | |
2548 | 2.92k | if (ss->statelessResume) { |
2549 | 0 | PORT_Assert(ss->xtnData.selectedPsk); |
2550 | 0 | PORT_Assert(ss->ssl3.hs.kea_def_mutable.authKeyType == ssl_auth_psk); |
2551 | 0 | } |
2552 | | |
2553 | | /* Now that we have the binder key, check the binder. */ |
2554 | 2.92k | if (ss->xtnData.selectedPsk) { |
2555 | 0 | SSL3Hashes hashes; |
2556 | 0 | PORT_Assert(ss->ssl3.hs.messages.len > ss->xtnData.pskBindersLen); |
2557 | 0 | rv = tls13_ComputePskBinderHash( |
2558 | 0 | ss, |
2559 | 0 | ss->ssl3.hs.messages.buf, |
2560 | 0 | ss->ssl3.hs.messages.len - ss->xtnData.pskBindersLen, |
2561 | 0 | &hashes, tls13_GetHash(ss)); |
2562 | 0 | if (rv != SECSuccess) { |
2563 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2564 | 0 | goto loser; |
2565 | 0 | } |
2566 | | |
2567 | 0 | PORT_Assert(ss->xtnData.selectedPsk->hash == tls13_GetHash(ss)); |
2568 | 0 | PORT_Assert(ss->ssl3.hs.suite_def); |
2569 | 0 | rv = tls13_VerifyFinished(ss, ssl_hs_client_hello, |
2570 | 0 | ss->xtnData.selectedPsk->binderKey, |
2571 | 0 | ss->xtnData.pskBinder.data, |
2572 | 0 | ss->xtnData.pskBinder.len, |
2573 | 0 | &hashes); |
2574 | 0 | } |
2575 | 2.92k | if (rv != SECSuccess) { |
2576 | 0 | goto loser; |
2577 | 0 | } |
2578 | | |
2579 | | /* This needs to go after we verify the psk binder. */ |
2580 | 2.92k | rv = ssl3_InitHandshakeHashes(ss); |
2581 | 2.92k | if (rv != SECSuccess) { |
2582 | 0 | goto loser; |
2583 | 0 | } |
2584 | | |
2585 | | /* If this is TLS 1.3 we are expecting a ClientKeyShare |
2586 | | * extension. Missing/absent extension cause failure |
2587 | | * below. */ |
2588 | 2.92k | rv = tls13_HandleClientKeyShare(ss, clientShare); |
2589 | 2.92k | if (rv != SECSuccess) { |
2590 | 178 | goto loser; /* An alert was sent already. */ |
2591 | 178 | } |
2592 | | |
2593 | | /* From this point we are either committed to resumption, or not. */ |
2594 | 2.74k | if (ss->statelessResume) { |
2595 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_cache_hits); |
2596 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_stateless_resumes); |
2597 | 2.74k | } else { |
2598 | 2.74k | if (sid) { |
2599 | | /* We had a sid, but it's no longer valid, free it. */ |
2600 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_cache_not_ok); |
2601 | 0 | ssl_UncacheSessionID(ss); |
2602 | 0 | ssl_FreeSID(sid); |
2603 | 2.74k | } else if (!ss->xtnData.selectedPsk) { |
2604 | 2.74k | SSL_AtomicIncrementLong(&ssl3stats->hch_sid_cache_misses); |
2605 | 2.74k | } |
2606 | | |
2607 | 2.74k | sid = ssl3_NewSessionID(ss, PR_TRUE); |
2608 | 2.74k | if (!sid) { |
2609 | 0 | FATAL_ERROR(ss, PORT_GetError(), internal_error); |
2610 | 0 | return SECFailure; |
2611 | 0 | } |
2612 | 2.74k | } |
2613 | | /* Take ownership of the session. */ |
2614 | 2.74k | ss->sec.ci.sid = sid; |
2615 | 2.74k | sid = NULL; |
2616 | | |
2617 | 2.74k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted) { |
2618 | 0 | rv = tls13_DeriveEarlySecrets(ss); |
2619 | 0 | if (rv != SECSuccess) { |
2620 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2621 | 0 | return SECFailure; |
2622 | 0 | } |
2623 | 0 | } |
2624 | | |
2625 | 2.74k | ssl_GetXmitBufLock(ss); |
2626 | 2.74k | rv = tls13_SendServerHelloSequence(ss); |
2627 | 2.74k | ssl_ReleaseXmitBufLock(ss); |
2628 | 2.74k | if (rv != SECSuccess) { |
2629 | 0 | FATAL_ERROR(ss, PORT_GetError(), handshake_failure); |
2630 | 0 | return SECFailure; |
2631 | 0 | } |
2632 | | |
2633 | | /* We're done with PSKs */ |
2634 | 2.74k | tls13_DestroyPskList(&ss->ssl3.hs.psks); |
2635 | 2.74k | ss->xtnData.selectedPsk = NULL; |
2636 | | |
2637 | 2.74k | return SECSuccess; |
2638 | | |
2639 | 677 | loser: |
2640 | 677 | if (sid) { |
2641 | 31 | ssl_UncacheSessionID(ss); |
2642 | 31 | ssl_FreeSID(sid); |
2643 | 31 | } |
2644 | 677 | return SECFailure; |
2645 | 2.74k | } |
2646 | | |
2647 | | SECStatus |
2648 | | SSLExp_HelloRetryRequestCallback(PRFileDesc *fd, |
2649 | | SSLHelloRetryRequestCallback cb, void *arg) |
2650 | 0 | { |
2651 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
2652 | 0 | if (!ss) { |
2653 | 0 | return SECFailure; /* Code already set. */ |
2654 | 0 | } |
2655 | | |
2656 | 0 | ss->hrrCallback = cb; |
2657 | 0 | ss->hrrCallbackArg = arg; |
2658 | 0 | return SECSuccess; |
2659 | 0 | } |
2660 | | |
2661 | | /* |
2662 | | * struct { |
2663 | | * ProtocolVersion server_version; |
2664 | | * CipherSuite cipher_suite; |
2665 | | * Extension extensions<2..2^16-1>; |
2666 | | * } HelloRetryRequest; |
2667 | | * |
2668 | | * Note: this function takes an empty buffer and returns |
2669 | | * a non-empty one on success, in which case the caller must |
2670 | | * eventually clean up. |
2671 | | */ |
2672 | | SECStatus |
2673 | | tls13_ConstructHelloRetryRequest(sslSocket *ss, |
2674 | | ssl3CipherSuite cipherSuite, |
2675 | | const sslNamedGroupDef *selectedGroup, |
2676 | | PRUint8 *cookie, unsigned int cookieLen, |
2677 | | const PRUint8 *cookieGreaseEchSignal, |
2678 | | sslBuffer *buffer) |
2679 | 978 | { |
2680 | 978 | SECStatus rv; |
2681 | 978 | sslBuffer extensionsBuf = SSL_BUFFER_EMPTY; |
2682 | 978 | PORT_Assert(buffer->len == 0); |
2683 | | |
2684 | | /* Note: cookie is pointing to a stack variable, so is only valid |
2685 | | * now. */ |
2686 | 978 | ss->xtnData.selectedGroup = selectedGroup; |
2687 | 978 | ss->xtnData.cookie.data = cookie; |
2688 | 978 | ss->xtnData.cookie.len = cookieLen; |
2689 | | |
2690 | | /* Set restored ss->ssl3.hs.greaseEchBuf value for ECH HRR extension |
2691 | | * reconstruction. */ |
2692 | 978 | if (cookieGreaseEchSignal) { |
2693 | 0 | PORT_Assert(!ss->ssl3.hs.greaseEchBuf.len); |
2694 | 0 | rv = sslBuffer_Append(&ss->ssl3.hs.greaseEchBuf, |
2695 | 0 | cookieGreaseEchSignal, |
2696 | 0 | TLS13_ECH_SIGNAL_LEN); |
2697 | 0 | if (rv != SECSuccess) { |
2698 | 0 | goto loser; |
2699 | 0 | } |
2700 | 0 | } |
2701 | 978 | rv = ssl_ConstructExtensions(ss, &extensionsBuf, |
2702 | 978 | ssl_hs_hello_retry_request); |
2703 | | /* Reset ss->ssl3.hs.greaseEchBuf if it was changed. */ |
2704 | 978 | if (cookieGreaseEchSignal) { |
2705 | 0 | sslBuffer_Clear(&ss->ssl3.hs.greaseEchBuf); |
2706 | 0 | } |
2707 | 978 | if (rv != SECSuccess) { |
2708 | 0 | goto loser; |
2709 | 0 | } |
2710 | | /* These extensions can't be empty. */ |
2711 | 978 | PORT_Assert(SSL_BUFFER_LEN(&extensionsBuf) > 0); |
2712 | | |
2713 | | /* Clean up cookie so we're not pointing at random memory. */ |
2714 | 978 | ss->xtnData.cookie.data = NULL; |
2715 | 978 | ss->xtnData.cookie.len = 0; |
2716 | | |
2717 | 978 | rv = ssl_ConstructServerHello(ss, PR_TRUE, &extensionsBuf, buffer); |
2718 | 978 | if (rv != SECSuccess) { |
2719 | 0 | goto loser; |
2720 | 0 | } |
2721 | 978 | sslBuffer_Clear(&extensionsBuf); |
2722 | 978 | return SECSuccess; |
2723 | | |
2724 | 0 | loser: |
2725 | 0 | sslBuffer_Clear(&extensionsBuf); |
2726 | 0 | sslBuffer_Clear(buffer); |
2727 | 0 | return SECFailure; |
2728 | 978 | } |
2729 | | |
2730 | | static SECStatus |
2731 | | tls13_SendHelloRetryRequest(sslSocket *ss, |
2732 | | const sslNamedGroupDef *requestedGroup, |
2733 | | const PRUint8 *appToken, unsigned int appTokenLen) |
2734 | 978 | { |
2735 | 978 | SECStatus rv; |
2736 | 978 | unsigned int cookieLen; |
2737 | 978 | PRUint8 cookie[1024]; |
2738 | 978 | sslBuffer messageBuf = SSL_BUFFER_EMPTY; |
2739 | | |
2740 | 978 | SSL_TRC(3, ("%d: TLS13[%d]: send hello retry request handshake", |
2741 | 978 | SSL_GETPID(), ss->fd)); |
2742 | | |
2743 | 978 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
2744 | | |
2745 | | /* If an ECH backend or shared-mode server accepted ECH when offered, |
2746 | | * the HRR extension's payload must be set to 8 zero bytes, these are |
2747 | | * overwritten with the accept_confirmation value after the handshake |
2748 | | * transcript calculation. |
2749 | | * If a client-facing or shared-mode server did not accept ECH when offered |
2750 | | * OR if ECH GREASE is enabled on the server and a ECH extension was |
2751 | | * received, a 8 byte random value is set as the extension's payload |
2752 | | * [draft-ietf-tls-esni-14, Section 7]. |
2753 | | * |
2754 | | * The (temporary) payload is written to the extension in tls13exthandle.c/ |
2755 | | * tls13_ServerSendHrrEchXtn(). */ |
2756 | 978 | if (ss->xtnData.ech) { |
2757 | 42 | PRUint8 echGreaseRaw[TLS13_ECH_SIGNAL_LEN] = { 0 }; |
2758 | 42 | if (!(ss->ssl3.hs.echAccepted || |
2759 | 42 | (ss->opt.enableTls13BackendEch && |
2760 | 33 | ss->xtnData.ech && |
2761 | 33 | ss->xtnData.ech->receivedInnerXtn))) { |
2762 | 20 | rv = PK11_GenerateRandom(echGreaseRaw, TLS13_ECH_SIGNAL_LEN); |
2763 | 20 | if (rv != SECSuccess) { |
2764 | 0 | return SECFailure; |
2765 | 0 | } |
2766 | 20 | SSL_TRC(100, ("Generated random value for ECH HRR GREASE.")); |
2767 | 20 | } |
2768 | 42 | sslBuffer echGreaseBuffer = SSL_BUFFER_EMPTY; |
2769 | 42 | rv = sslBuffer_Append(&echGreaseBuffer, echGreaseRaw, sizeof(echGreaseRaw)); |
2770 | 42 | if (rv != SECSuccess) { |
2771 | 0 | return SECFailure; |
2772 | 0 | } |
2773 | | /* HRR GREASE/accept_confirmation zero bytes placeholder buffer. */ |
2774 | 42 | ss->ssl3.hs.greaseEchBuf = echGreaseBuffer; |
2775 | 42 | } |
2776 | | |
2777 | | /* Compute the cookie we are going to need. */ |
2778 | 978 | rv = tls13_MakeHrrCookie(ss, requestedGroup, |
2779 | 978 | appToken, appTokenLen, |
2780 | 978 | cookie, &cookieLen, sizeof(cookie)); |
2781 | 978 | if (rv != SECSuccess) { |
2782 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2783 | 0 | return SECFailure; |
2784 | 0 | } |
2785 | | |
2786 | | /* Now build the body of the message. */ |
2787 | 978 | rv = tls13_ConstructHelloRetryRequest(ss, ss->ssl3.hs.cipher_suite, |
2788 | 978 | requestedGroup, |
2789 | 978 | cookie, cookieLen, |
2790 | 978 | NULL, &messageBuf); |
2791 | 978 | if (rv != SECSuccess) { |
2792 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
2793 | 0 | return SECFailure; |
2794 | 0 | } |
2795 | | |
2796 | | /* And send it. */ |
2797 | 978 | ssl_GetXmitBufLock(ss); |
2798 | 978 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_server_hello, |
2799 | 978 | SSL_BUFFER_LEN(&messageBuf)); |
2800 | 978 | if (rv != SECSuccess) { |
2801 | 0 | goto loser; |
2802 | 0 | } |
2803 | 978 | rv = ssl3_AppendBufferToHandshake(ss, &messageBuf); |
2804 | 978 | if (rv != SECSuccess) { |
2805 | 0 | goto loser; |
2806 | 0 | } |
2807 | 978 | sslBuffer_Clear(&messageBuf); /* Done with messageBuf */ |
2808 | | |
2809 | 978 | if (ss->ssl3.hs.fakeSid.len) { |
2810 | 28 | PRInt32 sent; |
2811 | | |
2812 | 28 | PORT_Assert(!IS_DTLS(ss)); |
2813 | 28 | rv = ssl3_SendChangeCipherSpecsInt(ss); |
2814 | 28 | if (rv != SECSuccess) { |
2815 | 0 | goto loser; |
2816 | 0 | } |
2817 | | /* ssl3_SendChangeCipherSpecsInt() only flushes to the output buffer, so we |
2818 | | * have to force a send. */ |
2819 | 28 | sent = ssl_SendSavedWriteData(ss); |
2820 | 28 | if (sent < 0 && PORT_GetError() != PR_WOULD_BLOCK_ERROR) { |
2821 | 0 | PORT_SetError(SSL_ERROR_SOCKET_WRITE_FAILURE); |
2822 | 0 | goto loser; |
2823 | 0 | } |
2824 | 950 | } else { |
2825 | 950 | rv = ssl3_FlushHandshake(ss, 0); |
2826 | 950 | if (rv != SECSuccess) { |
2827 | 0 | goto loser; /* error code set by ssl3_FlushHandshake */ |
2828 | 0 | } |
2829 | 950 | } |
2830 | | |
2831 | | /* We depend on this being exactly one record and one message. */ |
2832 | 978 | PORT_Assert(!IS_DTLS(ss) || (ss->ssl3.hs.sendMessageSeq == 1 && |
2833 | 978 | ss->ssl3.cwSpec->nextSeqNum == 1)); |
2834 | 978 | ssl_ReleaseXmitBufLock(ss); |
2835 | | |
2836 | 978 | ss->ssl3.hs.helloRetry = PR_TRUE; |
2837 | | |
2838 | | /* We received early data but have to ignore it because we sent a retry. */ |
2839 | 978 | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent) { |
2840 | 183 | ss->ssl3.hs.zeroRttState = ssl_0rtt_ignored; |
2841 | 183 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_hrr; |
2842 | 183 | } |
2843 | | |
2844 | 978 | return SECSuccess; |
2845 | | |
2846 | 0 | loser: |
2847 | 0 | sslBuffer_Clear(&messageBuf); |
2848 | 0 | ssl_ReleaseXmitBufLock(ss); |
2849 | 0 | return SECFailure; |
2850 | 978 | } |
2851 | | |
2852 | | /* Called from tls13_HandleClientHello. |
2853 | | * |
2854 | | * Caller must hold Handshake and RecvBuf locks. |
2855 | | */ |
2856 | | |
2857 | | static SECStatus |
2858 | | tls13_HandleClientKeyShare(sslSocket *ss, TLS13KeyShareEntry *peerShare) |
2859 | 2.92k | { |
2860 | 2.92k | SECStatus rv; |
2861 | 2.92k | sslEphemeralKeyPair *keyPair; /* ours */ |
2862 | 2.92k | SECItem *ciphertext = NULL; |
2863 | 2.92k | PK11SymKey *dheSecret = NULL; |
2864 | 2.92k | PK11SymKey *kemSecret = NULL; |
2865 | | |
2866 | 2.92k | SSL_TRC(3, ("%d: TLS13[%d]: handle client_key_share handshake", |
2867 | 2.92k | SSL_GETPID(), ss->fd)); |
2868 | | |
2869 | 2.92k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
2870 | 2.92k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
2871 | 2.92k | PORT_Assert(peerShare); |
2872 | | |
2873 | 2.92k | tls13_SetKeyExchangeType(ss, peerShare->group); |
2874 | | |
2875 | | /* Generate our key */ |
2876 | 2.92k | rv = tls13_AddKeyShare(ss, peerShare->group); |
2877 | 2.92k | if (rv != SECSuccess) { |
2878 | 35 | return rv; |
2879 | 35 | } |
2880 | | |
2881 | | /* We should have exactly one key share. */ |
2882 | 2.89k | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ephemeralKeyPairs)); |
2883 | 2.89k | PORT_Assert(PR_PREV_LINK(&ss->ephemeralKeyPairs) == |
2884 | 2.89k | PR_NEXT_LINK(&ss->ephemeralKeyPairs)); |
2885 | | |
2886 | 2.89k | keyPair = ((sslEphemeralKeyPair *)PR_NEXT_LINK(&ss->ephemeralKeyPairs)); |
2887 | 2.89k | ss->sec.keaKeyBits = SECKEY_PublicKeyStrengthInBits(keyPair->keys->pubKey); |
2888 | | |
2889 | | /* Register the sender */ |
2890 | 2.89k | rv = ssl3_RegisterExtensionSender(ss, &ss->xtnData, ssl_tls13_key_share_xtn, |
2891 | 2.89k | tls13_ServerSendKeyShareXtn); |
2892 | 2.89k | if (rv != SECSuccess) { |
2893 | 0 | return SECFailure; /* Error code set already. */ |
2894 | 0 | } |
2895 | | |
2896 | 2.89k | rv = tls13_HandleKeyShare(ss, peerShare, keyPair->keys, |
2897 | 2.89k | tls13_GetHash(ss), |
2898 | 2.89k | &dheSecret); |
2899 | 2.89k | if (rv != SECSuccess) { |
2900 | 53 | goto loser; /* Error code already set. */ |
2901 | 53 | } |
2902 | | |
2903 | 2.83k | if (peerShare->group->keaType == ssl_kea_ecdh_hybrid) { |
2904 | 725 | rv = tls13_HandleKEMKey(ss, peerShare, &kemSecret, &ciphertext); |
2905 | 725 | if (rv != SECSuccess) { |
2906 | 90 | goto loser; /* Error set by tls13_HandleKEMKey */ |
2907 | 90 | } |
2908 | 635 | switch (peerShare->group->name) { |
2909 | 366 | case ssl_grp_kem_secp384r1mlkem1024: |
2910 | 511 | case ssl_grp_kem_secp256r1mlkem768: |
2911 | 511 | case ssl_grp_kem_xyber768d00: |
2912 | 511 | ss->ssl3.hs.dheSecret = PK11_ConcatSymKeys(dheSecret, kemSecret, CKM_HKDF_DERIVE, CKA_DERIVE); |
2913 | 511 | break; |
2914 | 124 | case ssl_grp_kem_mlkem768x25519: |
2915 | 124 | ss->ssl3.hs.dheSecret = PK11_ConcatSymKeys(kemSecret, dheSecret, CKM_HKDF_DERIVE, CKA_DERIVE); |
2916 | 124 | break; |
2917 | 0 | default: |
2918 | 0 | PORT_Assert(0); |
2919 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
2920 | 0 | ss->ssl3.hs.dheSecret = NULL; |
2921 | 0 | break; |
2922 | 635 | } |
2923 | 635 | if (!ss->ssl3.hs.dheSecret) { |
2924 | 0 | goto loser; /* Error set by PK11_ConcatSymKeys */ |
2925 | 0 | } |
2926 | 635 | keyPair->kemCt = ciphertext; |
2927 | 635 | PK11_FreeSymKey(dheSecret); |
2928 | 635 | PK11_FreeSymKey(kemSecret); |
2929 | 2.11k | } else { |
2930 | 2.11k | ss->ssl3.hs.dheSecret = dheSecret; |
2931 | 2.11k | } |
2932 | | |
2933 | 2.74k | return SECSuccess; |
2934 | | |
2935 | 143 | loser: |
2936 | 143 | SECITEM_FreeItem(ciphertext, PR_TRUE); |
2937 | 143 | PK11_FreeSymKey(dheSecret); |
2938 | 143 | PK11_FreeSymKey(kemSecret); |
2939 | 143 | FATAL_ERROR(ss, PORT_GetError(), illegal_parameter); |
2940 | 143 | return SECFailure; |
2941 | 2.83k | } |
2942 | | |
2943 | | /* |
2944 | | * [draft-ietf-tls-tls13-11] Section 6.3.3.2 |
2945 | | * |
2946 | | * opaque DistinguishedName<1..2^16-1>; |
2947 | | * |
2948 | | * struct { |
2949 | | * opaque certificate_extension_oid<1..2^8-1>; |
2950 | | * opaque certificate_extension_values<0..2^16-1>; |
2951 | | * } CertificateExtension; |
2952 | | * |
2953 | | * struct { |
2954 | | * opaque certificate_request_context<0..2^8-1>; |
2955 | | * SignatureAndHashAlgorithm |
2956 | | * supported_signature_algorithms<2..2^16-2>; |
2957 | | * DistinguishedName certificate_authorities<0..2^16-1>; |
2958 | | * CertificateExtension certificate_extensions<0..2^16-1>; |
2959 | | * } CertificateRequest; |
2960 | | */ |
2961 | | static SECStatus |
2962 | | tls13_SendCertificateRequest(sslSocket *ss) |
2963 | 1.23k | { |
2964 | 1.23k | SECStatus rv; |
2965 | 1.23k | sslBuffer extensionBuf = SSL_BUFFER_EMPTY; |
2966 | 1.23k | unsigned int offset = 0; |
2967 | | |
2968 | 1.23k | SSL_TRC(3, ("%d: TLS13[%d]: begin send certificate_request", |
2969 | 1.23k | SSL_GETPID(), ss->fd)); |
2970 | | |
2971 | 1.23k | if (ss->firstHsDone) { |
2972 | 0 | PORT_Assert(ss->ssl3.hs.shaPostHandshake == NULL); |
2973 | 0 | ss->ssl3.hs.shaPostHandshake = PK11_CloneContext(ss->ssl3.hs.sha); |
2974 | 0 | if (ss->ssl3.hs.shaPostHandshake == NULL) { |
2975 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
2976 | 0 | return SECFailure; |
2977 | 0 | } |
2978 | 0 | } |
2979 | | |
2980 | 1.23k | rv = ssl_ConstructExtensions(ss, &extensionBuf, ssl_hs_certificate_request); |
2981 | 1.23k | if (rv != SECSuccess) { |
2982 | 0 | return SECFailure; /* Code already set. */ |
2983 | 0 | } |
2984 | | /* We should always have at least one of these. */ |
2985 | 1.23k | PORT_Assert(SSL_BUFFER_LEN(&extensionBuf) > 0); |
2986 | | |
2987 | | /* Create a new request context for post-handshake authentication */ |
2988 | 1.23k | if (ss->firstHsDone) { |
2989 | 0 | PRUint8 context[16]; |
2990 | 0 | SECItem contextItem = { siBuffer, context, sizeof(context) }; |
2991 | |
|
2992 | 0 | rv = PK11_GenerateRandom(context, sizeof(context)); |
2993 | 0 | if (rv != SECSuccess) { |
2994 | 0 | goto loser; |
2995 | 0 | } |
2996 | | |
2997 | 0 | SECITEM_FreeItem(&ss->xtnData.certReqContext, PR_FALSE); |
2998 | 0 | rv = SECITEM_CopyItem(NULL, &ss->xtnData.certReqContext, &contextItem); |
2999 | 0 | if (rv != SECSuccess) { |
3000 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
3001 | 0 | goto loser; |
3002 | 0 | } |
3003 | | |
3004 | 0 | offset = SSL_BUFFER_LEN(&ss->sec.ci.sendBuf); |
3005 | 0 | } |
3006 | | |
3007 | 1.23k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate_request, |
3008 | 1.23k | 1 + /* request context length */ |
3009 | 1.23k | ss->xtnData.certReqContext.len + |
3010 | 1.23k | 2 + /* extension length */ |
3011 | 1.23k | SSL_BUFFER_LEN(&extensionBuf)); |
3012 | 1.23k | if (rv != SECSuccess) { |
3013 | 0 | goto loser; /* err set by AppendHandshake. */ |
3014 | 0 | } |
3015 | | |
3016 | | /* Context. */ |
3017 | 1.23k | rv = ssl3_AppendHandshakeVariable(ss, ss->xtnData.certReqContext.data, |
3018 | 1.23k | ss->xtnData.certReqContext.len, 1); |
3019 | 1.23k | if (rv != SECSuccess) { |
3020 | 0 | goto loser; /* err set by AppendHandshake. */ |
3021 | 0 | } |
3022 | | /* Extensions. */ |
3023 | 1.23k | rv = ssl3_AppendBufferToHandshakeVariable(ss, &extensionBuf, 2); |
3024 | 1.23k | if (rv != SECSuccess) { |
3025 | 0 | goto loser; /* err set by AppendHandshake. */ |
3026 | 0 | } |
3027 | | |
3028 | 1.23k | if (ss->firstHsDone) { |
3029 | 0 | rv = ssl3_UpdatePostHandshakeHashes(ss, |
3030 | 0 | SSL_BUFFER_BASE(&ss->sec.ci.sendBuf) + offset, |
3031 | 0 | SSL_BUFFER_LEN(&ss->sec.ci.sendBuf) - offset); |
3032 | 0 | if (rv != SECSuccess) { |
3033 | 0 | goto loser; |
3034 | 0 | } |
3035 | 0 | } |
3036 | | |
3037 | 1.23k | sslBuffer_Clear(&extensionBuf); |
3038 | 1.23k | return SECSuccess; |
3039 | | |
3040 | 0 | loser: |
3041 | 0 | sslBuffer_Clear(&extensionBuf); |
3042 | 0 | return SECFailure; |
3043 | 1.23k | } |
3044 | | |
3045 | | /* [draft-ietf-tls-tls13; S 4.4.1] says: |
3046 | | * |
3047 | | * Transcript-Hash(ClientHello1, HelloRetryRequest, ... MN) = |
3048 | | * Hash(message_hash || // Handshake type |
3049 | | * 00 00 Hash.length || // Handshake message length |
3050 | | * Hash(ClientHello1) || // Hash of ClientHello1 |
3051 | | * HelloRetryRequest ... MN) |
3052 | | * |
3053 | | * For an ECH handshake, the process occurs for the outer |
3054 | | * transcript in |ss->ssl3.hs.messages| and the inner |
3055 | | * transcript in |ss->ssl3.hs.echInnerMessages|. |
3056 | | */ |
3057 | | static SECStatus |
3058 | | tls13_ReinjectHandshakeTranscript(sslSocket *ss) |
3059 | 2.98k | { |
3060 | 2.98k | SSL3Hashes hashes = { 0 }; |
3061 | 2.98k | SSL3Hashes echInnerHashes = { 0 }; |
3062 | 2.98k | SECStatus rv; |
3063 | | |
3064 | | /* First compute the hash. */ |
3065 | 2.98k | rv = tls13_ComputeHash(ss, &hashes, |
3066 | 2.98k | ss->ssl3.hs.messages.buf, |
3067 | 2.98k | ss->ssl3.hs.messages.len, |
3068 | 2.98k | tls13_GetHash(ss)); |
3069 | 2.98k | if (rv != SECSuccess) { |
3070 | 0 | return SECFailure; |
3071 | 0 | } |
3072 | | |
3073 | 2.98k | if (ss->ssl3.hs.echHpkeCtx) { |
3074 | 656 | rv = tls13_ComputeHash(ss, &echInnerHashes, |
3075 | 656 | ss->ssl3.hs.echInnerMessages.buf, |
3076 | 656 | ss->ssl3.hs.echInnerMessages.len, |
3077 | 656 | tls13_GetHash(ss)); |
3078 | 656 | if (rv != SECSuccess) { |
3079 | 0 | return SECFailure; |
3080 | 0 | } |
3081 | 656 | } |
3082 | | |
3083 | 2.98k | ssl3_RestartHandshakeHashes(ss); |
3084 | | |
3085 | | /* Reinject the message. The Default context variant updates |
3086 | | * the default hash state. Use it for both non-ECH and ECH Outer. */ |
3087 | 2.98k | rv = ssl_HashHandshakeMessageDefault(ss, ssl_hs_message_hash, |
3088 | 2.98k | hashes.u.raw, hashes.len); |
3089 | 2.98k | if (rv != SECSuccess) { |
3090 | 0 | return SECFailure; |
3091 | 0 | } |
3092 | | |
3093 | 2.98k | if (ss->ssl3.hs.echHpkeCtx) { |
3094 | 656 | rv = ssl_HashHandshakeMessageEchInner(ss, ssl_hs_message_hash, |
3095 | 656 | echInnerHashes.u.raw, |
3096 | 656 | echInnerHashes.len); |
3097 | 656 | if (rv != SECSuccess) { |
3098 | 0 | return SECFailure; |
3099 | 0 | } |
3100 | 656 | } |
3101 | | |
3102 | 2.98k | return SECSuccess; |
3103 | 2.98k | } |
3104 | | static unsigned int |
3105 | | ssl_ListCount(PRCList *list) |
3106 | 3.02k | { |
3107 | 3.02k | unsigned int c = 0; |
3108 | 3.02k | PRCList *cur; |
3109 | 9.28k | for (cur = PR_NEXT_LINK(list); cur != list; cur = PR_NEXT_LINK(cur)) { |
3110 | 6.26k | ++c; |
3111 | 6.26k | } |
3112 | 3.02k | return c; |
3113 | 3.02k | } |
3114 | | |
3115 | | /* |
3116 | | * savedMsg contains the HelloRetryRequest message. When its extensions are parsed |
3117 | | * in ssl3_HandleParsedExtensions, the handler for ECH HRR extensions (tls13_ClientHandleHrrEchXtn) |
3118 | | * will take a reference into the message buffer. |
3119 | | * |
3120 | | * This reference is then used in tls13_MaybeHandleEchSignal in order to compute |
3121 | | * the transcript for the ECH signal calculation. This was felt to be preferable |
3122 | | * to re-parsing the HelloRetryRequest message in order to create the transcript. |
3123 | | * |
3124 | | * Consequently, savedMsg should not be moved or mutated between these |
3125 | | * function calls. |
3126 | | */ |
3127 | | SECStatus |
3128 | | tls13_HandleHelloRetryRequest(sslSocket *ss, const PRUint8 *savedMsg, |
3129 | | PRUint32 savedLength) |
3130 | 3.02k | { |
3131 | 3.02k | SECStatus rv; |
3132 | | |
3133 | 3.02k | SSL_TRC(3, ("%d: TLS13[%d]: handle hello retry request", |
3134 | 3.02k | SSL_GETPID(), ss->fd)); |
3135 | | |
3136 | 3.02k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
3137 | 3.02k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3138 | | |
3139 | 3.02k | if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) { |
3140 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_HELLO_RETRY_REQUEST, |
3141 | 0 | unexpected_message); |
3142 | 0 | return SECFailure; |
3143 | 0 | } |
3144 | 3.02k | PORT_Assert(ss->ssl3.hs.ws == wait_server_hello); |
3145 | | |
3146 | 3.02k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent) { |
3147 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_ignored; |
3148 | | /* Restore the null cipher spec for writing. */ |
3149 | 0 | ssl_GetSpecWriteLock(ss); |
3150 | 0 | ssl_CipherSpecRelease(ss->ssl3.cwSpec); |
3151 | 0 | ss->ssl3.cwSpec = ssl_FindCipherSpecByEpoch(ss, ssl_secret_write, |
3152 | 0 | TrafficKeyClearText); |
3153 | 0 | PORT_Assert(ss->ssl3.cwSpec); |
3154 | 0 | ssl_ReleaseSpecWriteLock(ss); |
3155 | 3.02k | } else { |
3156 | 3.02k | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_none); |
3157 | 3.02k | } |
3158 | | /* Set the spec version, because we want to send CH now with 0303 */ |
3159 | 3.02k | tls13_SetSpecRecordVersion(ss, ss->ssl3.cwSpec); |
3160 | | |
3161 | | /* Extensions must contain more than just supported_versions. This will |
3162 | | * ensure that a HelloRetryRequest isn't a no-op: we must have at least two |
3163 | | * extensions, supported_versions plus one other. That other must be one |
3164 | | * that we understand and recognize as being valid for HelloRetryRequest, |
3165 | | * and should alter our next Client Hello. */ |
3166 | 3.02k | unsigned int requiredExtensions = 1; |
3167 | | /* The ECH HRR extension is a no-op from the client's perspective. */ |
3168 | 3.02k | if (ss->xtnData.ech) { |
3169 | 0 | requiredExtensions++; |
3170 | 0 | } |
3171 | 3.02k | if (ssl_ListCount(&ss->ssl3.hs.remoteExtensions) <= requiredExtensions) { |
3172 | 3 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_HELLO_RETRY_REQUEST, |
3173 | 3 | decode_error); |
3174 | 3 | return SECFailure; |
3175 | 3 | } |
3176 | | |
3177 | 3.02k | rv = ssl3_HandleParsedExtensions(ss, ssl_hs_hello_retry_request); |
3178 | 3.02k | ssl3_DestroyRemoteExtensions(&ss->ssl3.hs.remoteExtensions); |
3179 | 3.02k | if (rv != SECSuccess) { |
3180 | 36 | SECITEM_FreeItem(&ss->ssl3.hs.cookie, PR_FALSE); |
3181 | 36 | return SECFailure; /* Error code set below */ |
3182 | 36 | } |
3183 | 2.98k | rv = tls13_MaybeHandleEchSignal(ss, savedMsg, savedLength, PR_TRUE); |
3184 | 2.98k | if (rv != SECSuccess) { |
3185 | 0 | return SECFailure; |
3186 | 0 | } |
3187 | 2.98k | ss->ssl3.hs.helloRetry = PR_TRUE; |
3188 | 2.98k | rv = tls13_ReinjectHandshakeTranscript(ss); |
3189 | 2.98k | if (rv != SECSuccess) { |
3190 | 0 | return rv; |
3191 | 0 | } |
3192 | | |
3193 | 2.98k | rv = ssl_HashHandshakeMessage(ss, ssl_hs_server_hello, |
3194 | 2.98k | savedMsg, savedLength); |
3195 | 2.98k | if (rv != SECSuccess) { |
3196 | 0 | return SECFailure; |
3197 | 0 | } |
3198 | | |
3199 | 2.98k | ssl_GetXmitBufLock(ss); |
3200 | 2.98k | if (ss->opt.enableTls13CompatMode && !IS_DTLS(ss) && |
3201 | 11 | ss->ssl3.hs.zeroRttState == ssl_0rtt_none) { |
3202 | 11 | rv = ssl3_SendChangeCipherSpecsInt(ss); |
3203 | 11 | if (rv != SECSuccess) { |
3204 | 0 | goto loser; |
3205 | 0 | } |
3206 | 11 | } |
3207 | | |
3208 | 2.98k | rv = ssl3_SendClientHello(ss, client_hello_retry); |
3209 | 2.98k | if (rv != SECSuccess) { |
3210 | 0 | goto loser; |
3211 | 0 | } |
3212 | | |
3213 | 2.98k | ssl_ReleaseXmitBufLock(ss); |
3214 | 2.98k | SECITEM_FreeItem(&ss->ssl3.hs.cookie, PR_FALSE); |
3215 | 2.98k | return SECSuccess; |
3216 | | |
3217 | 0 | loser: |
3218 | 0 | ssl_ReleaseXmitBufLock(ss); |
3219 | 0 | SECITEM_FreeItem(&ss->ssl3.hs.cookie, PR_FALSE); |
3220 | 0 | return SECFailure; |
3221 | 2.98k | } |
3222 | | |
3223 | | static SECStatus |
3224 | | tls13_SendPostHandshakeCertificate(sslSocket *ss) |
3225 | 0 | { |
3226 | 0 | SECStatus rv; |
3227 | 0 | if (ss->ssl3.hs.restartTarget) { |
3228 | 0 | PR_NOT_REACHED("unexpected ss->ssl3.hs.restartTarget"); |
3229 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
3230 | 0 | return SECFailure; |
3231 | 0 | } |
3232 | | |
3233 | 0 | if (ss->ssl3.hs.clientCertificatePending) { |
3234 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: deferring tls13_SendClientSecondFlight because" |
3235 | 0 | " certificate authentication is still pending.", |
3236 | 0 | SSL_GETPID(), ss->fd)); |
3237 | 0 | ss->ssl3.hs.restartTarget = tls13_SendPostHandshakeCertificate; |
3238 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
3239 | 0 | return SECFailure; |
3240 | 0 | } |
3241 | | |
3242 | 0 | ssl_GetXmitBufLock(ss); |
3243 | 0 | rv = tls13_SendClientSecondFlight(ss); |
3244 | 0 | ssl_ReleaseXmitBufLock(ss); |
3245 | 0 | PORT_Assert(ss->ssl3.hs.ws == idle_handshake); |
3246 | 0 | PORT_Assert(ss->ssl3.hs.shaPostHandshake != NULL); |
3247 | 0 | PK11_DestroyContext(ss->ssl3.hs.shaPostHandshake, PR_TRUE); |
3248 | 0 | ss->ssl3.hs.shaPostHandshake = NULL; |
3249 | 0 | if (rv != SECSuccess) { |
3250 | 0 | return SECFailure; |
3251 | 0 | } |
3252 | 0 | return rv; |
3253 | 0 | } |
3254 | | |
3255 | | static SECStatus |
3256 | | tls13_HandleCertificateRequest(sslSocket *ss, PRUint8 *b, PRUint32 length) |
3257 | 15 | { |
3258 | 15 | SECStatus rv; |
3259 | 15 | SECItem context = { siBuffer, NULL, 0 }; |
3260 | 15 | SECItem extensionsData = { siBuffer, NULL, 0 }; |
3261 | | |
3262 | 15 | SSL_TRC(3, ("%d: TLS13[%d]: handle certificate_request sequence", |
3263 | 15 | SSL_GETPID(), ss->fd)); |
3264 | | |
3265 | 15 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
3266 | 15 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3267 | | |
3268 | | /* Client */ |
3269 | 15 | if (ss->opt.enablePostHandshakeAuth) { |
3270 | 3 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST, |
3271 | 3 | wait_cert_request, idle_handshake); |
3272 | 12 | } else { |
3273 | 12 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST, |
3274 | 12 | wait_cert_request); |
3275 | 12 | } |
3276 | 15 | if (rv != SECSuccess) { |
3277 | 15 | return SECFailure; |
3278 | 15 | } |
3279 | | |
3280 | | /* MUST NOT combine external PSKs with certificate authentication. */ |
3281 | 0 | if (ss->sec.authType == ssl_auth_psk) { |
3282 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_CERT_REQUEST, unexpected_message); |
3283 | 0 | return SECFailure; |
3284 | 0 | } |
3285 | | |
3286 | 0 | if (tls13_IsPostHandshake(ss)) { |
3287 | 0 | PORT_Assert(ss->ssl3.hs.shaPostHandshake == NULL); |
3288 | 0 | ss->ssl3.hs.shaPostHandshake = PK11_CloneContext(ss->ssl3.hs.sha); |
3289 | 0 | if (ss->ssl3.hs.shaPostHandshake == NULL) { |
3290 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
3291 | 0 | return SECFailure; |
3292 | 0 | } |
3293 | 0 | rv = ssl_HashPostHandshakeMessage(ss, ssl_hs_certificate_request, b, length); |
3294 | 0 | if (rv != SECSuccess) { |
3295 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
3296 | 0 | return SECFailure; |
3297 | 0 | } |
3298 | | |
3299 | | /* clean up anything left from previous handshake. */ |
3300 | 0 | if (ss->ssl3.clientCertChain != NULL) { |
3301 | 0 | CERT_DestroyCertificateList(ss->ssl3.clientCertChain); |
3302 | 0 | ss->ssl3.clientCertChain = NULL; |
3303 | 0 | } |
3304 | 0 | if (ss->ssl3.clientCertificate != NULL) { |
3305 | 0 | CERT_DestroyCertificate(ss->ssl3.clientCertificate); |
3306 | 0 | ss->ssl3.clientCertificate = NULL; |
3307 | 0 | } |
3308 | 0 | if (ss->ssl3.clientPrivateKey != NULL) { |
3309 | 0 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
3310 | 0 | ss->ssl3.clientPrivateKey = NULL; |
3311 | 0 | } |
3312 | 0 | if (ss->ssl3.hs.clientAuthSignatureSchemes != NULL) { |
3313 | 0 | PORT_Free(ss->ssl3.hs.clientAuthSignatureSchemes); |
3314 | 0 | ss->ssl3.hs.clientAuthSignatureSchemes = NULL; |
3315 | 0 | ss->ssl3.hs.clientAuthSignatureSchemesLen = 0; |
3316 | 0 | } |
3317 | 0 | SECITEM_FreeItem(&ss->xtnData.certReqContext, PR_FALSE); |
3318 | 0 | ss->xtnData.certReqContext.data = NULL; |
3319 | 0 | } else { |
3320 | 0 | PORT_Assert(ss->ssl3.clientCertChain == NULL); |
3321 | 0 | PORT_Assert(ss->ssl3.clientCertificate == NULL); |
3322 | 0 | PORT_Assert(ss->ssl3.clientPrivateKey == NULL); |
3323 | 0 | PORT_Assert(ss->ssl3.hs.clientAuthSignatureSchemes == NULL); |
3324 | 0 | PORT_Assert(ss->ssl3.hs.clientAuthSignatureSchemesLen == 0); |
3325 | 0 | PORT_Assert(!ss->ssl3.hs.clientCertRequested); |
3326 | 0 | PORT_Assert(ss->xtnData.certReqContext.data == NULL); |
3327 | 0 | } |
3328 | | |
3329 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &context, 1, &b, &length); |
3330 | 0 | if (rv != SECSuccess) { |
3331 | 0 | return SECFailure; |
3332 | 0 | } |
3333 | | |
3334 | | /* Unless it is a post-handshake client auth, the certificate |
3335 | | * request context must be empty. */ |
3336 | 0 | if (!tls13_IsPostHandshake(ss) && context.len > 0) { |
3337 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERT_REQUEST, illegal_parameter); |
3338 | 0 | return SECFailure; |
3339 | 0 | } |
3340 | | |
3341 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &extensionsData, 2, &b, &length); |
3342 | 0 | if (rv != SECSuccess) { |
3343 | 0 | return SECFailure; |
3344 | 0 | } |
3345 | | |
3346 | 0 | if (length) { |
3347 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERT_REQUEST, decode_error); |
3348 | 0 | return SECFailure; |
3349 | 0 | } |
3350 | | |
3351 | | /* Process all the extensions. */ |
3352 | 0 | rv = ssl3_HandleExtensions(ss, &extensionsData.data, &extensionsData.len, |
3353 | 0 | ssl_hs_certificate_request); |
3354 | 0 | if (rv != SECSuccess) { |
3355 | 0 | return SECFailure; |
3356 | 0 | } |
3357 | | |
3358 | 0 | if (!ss->xtnData.numSigSchemes) { |
3359 | 0 | FATAL_ERROR(ss, SSL_ERROR_MISSING_SIGNATURE_ALGORITHMS_EXTENSION, |
3360 | 0 | missing_extension); |
3361 | 0 | return SECFailure; |
3362 | 0 | } |
3363 | | |
3364 | 0 | rv = SECITEM_CopyItem(NULL, &ss->xtnData.certReqContext, &context); |
3365 | 0 | if (rv != SECSuccess) { |
3366 | 0 | return SECFailure; |
3367 | 0 | } |
3368 | | |
3369 | 0 | ss->ssl3.hs.clientCertRequested = PR_TRUE; |
3370 | |
|
3371 | 0 | if (ss->firstHsDone) { |
3372 | | |
3373 | | /* Request a client certificate. */ |
3374 | 0 | rv = ssl3_BeginHandleCertificateRequest( |
3375 | 0 | ss, ss->xtnData.sigSchemes, ss->xtnData.numSigSchemes, |
3376 | 0 | &ss->xtnData.certReqAuthorities); |
3377 | 0 | if (rv != SECSuccess) { |
3378 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
3379 | 0 | return rv; |
3380 | 0 | } |
3381 | 0 | rv = tls13_SendPostHandshakeCertificate(ss); |
3382 | 0 | if (rv != SECSuccess) { |
3383 | 0 | return rv; /* error code is set. */ |
3384 | 0 | } |
3385 | 0 | } else { |
3386 | 0 | TLS13_SET_HS_STATE(ss, wait_server_cert); |
3387 | 0 | } |
3388 | 0 | return SECSuccess; |
3389 | 0 | } |
3390 | | |
3391 | | PRBool |
3392 | | tls13_ShouldRequestClientAuth(sslSocket *ss) |
3393 | 6.63k | { |
3394 | | /* Even if we are configured to request a certificate, we can't |
3395 | | * if this handshake used a PSK, even when we are resuming. */ |
3396 | 6.63k | return ss->opt.requestCertificate && |
3397 | 2.48k | ss->ssl3.hs.kea_def->authKeyType != ssl_auth_psk; |
3398 | 6.63k | } |
3399 | | |
3400 | | static SECStatus |
3401 | | tls13_SendEncryptedServerSequence(sslSocket *ss) |
3402 | 2.74k | { |
3403 | 2.74k | SECStatus rv; |
3404 | | |
3405 | 2.74k | rv = tls13_ComputeHandshakeSecrets(ss); |
3406 | 2.74k | if (rv != SECSuccess) { |
3407 | 0 | return SECFailure; /* error code is set. */ |
3408 | 0 | } |
3409 | | |
3410 | 2.74k | rv = tls13_SetCipherSpec(ss, TrafficKeyHandshake, |
3411 | 2.74k | ssl_secret_write, PR_FALSE); |
3412 | 2.74k | if (rv != SECSuccess) { |
3413 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
3414 | 0 | return SECFailure; |
3415 | 0 | } |
3416 | | |
3417 | 2.74k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted) { |
3418 | 0 | rv = ssl3_RegisterExtensionSender(ss, &ss->xtnData, |
3419 | 0 | ssl_tls13_early_data_xtn, |
3420 | 0 | ssl_SendEmptyExtension); |
3421 | 0 | if (rv != SECSuccess) { |
3422 | 0 | return SECFailure; /* Error code set already. */ |
3423 | 0 | } |
3424 | 0 | } |
3425 | | |
3426 | 2.74k | rv = tls13_SendEncryptedExtensions(ss); |
3427 | 2.74k | if (rv != SECSuccess) { |
3428 | 0 | return SECFailure; /* error code is set. */ |
3429 | 0 | } |
3430 | | |
3431 | 2.74k | if (tls13_ShouldRequestClientAuth(ss)) { |
3432 | 1.23k | rv = tls13_SendCertificateRequest(ss); |
3433 | 1.23k | if (rv != SECSuccess) { |
3434 | 0 | return SECFailure; /* error code is set. */ |
3435 | 0 | } |
3436 | 1.23k | } |
3437 | 2.74k | if (ss->ssl3.hs.signatureScheme != ssl_sig_none) { |
3438 | 2.74k | SECKEYPrivateKey *svrPrivKey; |
3439 | | |
3440 | 2.74k | rv = tls13_SendCertificate(ss); |
3441 | 2.74k | if (rv != SECSuccess) { |
3442 | 0 | return SECFailure; /* error code is set. */ |
3443 | 0 | } |
3444 | | |
3445 | 2.74k | if (tls13_IsSigningWithDelegatedCredential(ss)) { |
3446 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: Signing with delegated credential", |
3447 | 0 | SSL_GETPID(), ss->fd)); |
3448 | 0 | svrPrivKey = ss->sec.serverCert->delegCredKeyPair->privKey; |
3449 | 2.74k | } else { |
3450 | 2.74k | svrPrivKey = ss->sec.serverCert->serverKeyPair->privKey; |
3451 | 2.74k | } |
3452 | | |
3453 | 2.74k | rv = tls13_SendCertificateVerify(ss, svrPrivKey); |
3454 | 2.74k | if (rv != SECSuccess) { |
3455 | 0 | return SECFailure; /* err code is set. */ |
3456 | 0 | } |
3457 | 2.74k | } |
3458 | | |
3459 | 2.74k | rv = tls13_SendFinished(ss, ss->ssl3.hs.serverHsTrafficSecret); |
3460 | 2.74k | if (rv != SECSuccess) { |
3461 | 0 | return SECFailure; /* error code is set. */ |
3462 | 0 | } |
3463 | | |
3464 | 2.74k | return SECSuccess; |
3465 | 2.74k | } |
3466 | | |
3467 | | /* Called from: ssl3_HandleClientHello */ |
3468 | | static SECStatus |
3469 | | tls13_SendServerHelloSequence(sslSocket *ss) |
3470 | 2.74k | { |
3471 | 2.74k | SECStatus rv; |
3472 | 2.74k | PRErrorCode err = 0; |
3473 | | |
3474 | 2.74k | SSL_TRC(3, ("%d: TLS13[%d]: begin send server_hello sequence", |
3475 | 2.74k | SSL_GETPID(), ss->fd)); |
3476 | | |
3477 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3478 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3479 | | |
3480 | 2.74k | rv = ssl3_RegisterExtensionSender(ss, &ss->xtnData, |
3481 | 2.74k | ssl_tls13_supported_versions_xtn, |
3482 | 2.74k | tls13_ServerSendSupportedVersionsXtn); |
3483 | 2.74k | if (rv != SECSuccess) { |
3484 | 0 | return SECFailure; |
3485 | 0 | } |
3486 | | |
3487 | 2.74k | rv = tls13_ComputeHandshakeSecret(ss); |
3488 | 2.74k | if (rv != SECSuccess) { |
3489 | 0 | return SECFailure; /* error code is set. */ |
3490 | 0 | } |
3491 | | |
3492 | 2.74k | rv = ssl3_SendServerHello(ss); |
3493 | 2.74k | if (rv != SECSuccess) { |
3494 | 0 | return rv; /* err code is set. */ |
3495 | 0 | } |
3496 | | |
3497 | 2.74k | if (ss->ssl3.hs.fakeSid.len) { |
3498 | 68 | PORT_Assert(!IS_DTLS(ss)); |
3499 | 68 | SECITEM_FreeItem(&ss->ssl3.hs.fakeSid, PR_FALSE); |
3500 | 68 | if (!ss->ssl3.hs.helloRetry) { |
3501 | 68 | rv = ssl3_SendChangeCipherSpecsInt(ss); |
3502 | 68 | if (rv != SECSuccess) { |
3503 | 0 | return rv; |
3504 | 0 | } |
3505 | 68 | } |
3506 | 68 | } |
3507 | | |
3508 | 2.74k | rv = tls13_SendEncryptedServerSequence(ss); |
3509 | 2.74k | if (rv != SECSuccess) { |
3510 | 0 | err = PORT_GetError(); |
3511 | 0 | } |
3512 | | /* Even if we get an error, since the ServerHello was successfully |
3513 | | * serialized, we should give it a chance to reach the network. This gives |
3514 | | * the client a chance to perform the key exchange and decrypt the alert |
3515 | | * we're about to send. */ |
3516 | 2.74k | rv |= ssl3_FlushHandshake(ss, 0); |
3517 | 2.74k | if (rv != SECSuccess) { |
3518 | 0 | if (err) { |
3519 | 0 | PORT_SetError(err); |
3520 | 0 | } |
3521 | 0 | return SECFailure; |
3522 | 0 | } |
3523 | | |
3524 | | /* Compute the rest of the secrets except for the resumption |
3525 | | * and exporter secret. */ |
3526 | 2.74k | rv = tls13_ComputeApplicationSecrets(ss); |
3527 | 2.74k | if (rv != SECSuccess) { |
3528 | 0 | LOG_ERROR(ss, PORT_GetError()); |
3529 | 0 | return SECFailure; |
3530 | 0 | } |
3531 | | |
3532 | 2.74k | rv = tls13_SetCipherSpec(ss, TrafficKeyApplicationData, |
3533 | 2.74k | ssl_secret_write, PR_FALSE); |
3534 | 2.74k | if (rv != SECSuccess) { |
3535 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
3536 | 0 | return SECFailure; |
3537 | 0 | } |
3538 | | |
3539 | 2.74k | if (IS_DTLS(ss)) { |
3540 | | /* We need this for reading ACKs. */ |
3541 | 1.29k | ssl_CipherSpecAddRef(ss->ssl3.crSpec); |
3542 | 1.29k | } |
3543 | 2.74k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted) { |
3544 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyEarlyApplicationData, |
3545 | 0 | ssl_secret_read, PR_TRUE); |
3546 | 0 | if (rv != SECSuccess) { |
3547 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
3548 | 0 | return SECFailure; |
3549 | 0 | } |
3550 | 0 | TLS13_SET_HS_STATE(ss, wait_end_of_early_data); |
3551 | 2.74k | } else { |
3552 | 2.74k | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_none || |
3553 | 2.74k | ss->ssl3.hs.zeroRttState == ssl_0rtt_ignored); |
3554 | | |
3555 | 2.74k | rv = tls13_SetCipherSpec(ss, |
3556 | 2.74k | TrafficKeyHandshake, |
3557 | 2.74k | ssl_secret_read, PR_FALSE); |
3558 | 2.74k | if (rv != SECSuccess) { |
3559 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
3560 | 0 | return SECFailure; |
3561 | 0 | } |
3562 | 2.74k | if (tls13_ShouldRequestClientAuth(ss)) { |
3563 | 1.23k | TLS13_SET_HS_STATE(ss, wait_client_cert); |
3564 | 1.51k | } else { |
3565 | 1.51k | TLS13_SET_HS_STATE(ss, wait_finished); |
3566 | 1.51k | } |
3567 | 2.74k | } |
3568 | | |
3569 | | /* Here we set a baseline value for our RTT estimation. |
3570 | | * This value is updated when we get a response from the client. */ |
3571 | 2.74k | ss->ssl3.hs.rttEstimate = ssl_Time(ss); |
3572 | 2.74k | return SECSuccess; |
3573 | 2.74k | } |
3574 | | |
3575 | | SECStatus |
3576 | | tls13_HandleServerHelloPart2(sslSocket *ss, const PRUint8 *savedMsg, PRUint32 savedLength) |
3577 | 2.58k | { |
3578 | 2.58k | SECStatus rv; |
3579 | 2.58k | sslSessionID *sid = ss->sec.ci.sid; |
3580 | 2.58k | SSL3Statistics *ssl3stats = SSL_GetStatistics(); |
3581 | | |
3582 | 2.58k | if (ssl3_ExtensionNegotiated(ss, ssl_tls13_pre_shared_key_xtn)) { |
3583 | 12 | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)); |
3584 | 12 | PORT_Assert(ss->xtnData.selectedPsk); |
3585 | | |
3586 | 12 | if (ss->xtnData.selectedPsk->type != ssl_psk_resume) { |
3587 | 12 | ss->statelessResume = PR_FALSE; |
3588 | 12 | } |
3589 | 2.57k | } else { |
3590 | | /* We may have offered a PSK. If the server didn't negotiate |
3591 | | * it, clear this state to re-extract the Early Secret. */ |
3592 | 2.57k | if (ss->ssl3.hs.currentSecret) { |
3593 | | /* We might have dropped incompatible PSKs on HRR |
3594 | | * (see RFC8466, Section 4.1.4). */ |
3595 | 1.36k | PORT_Assert(ss->ssl3.hs.helloRetry || |
3596 | 1.36k | ssl3_ExtensionAdvertised(ss, ssl_tls13_pre_shared_key_xtn)); |
3597 | 1.36k | PK11_FreeSymKey(ss->ssl3.hs.currentSecret); |
3598 | 1.36k | ss->ssl3.hs.currentSecret = NULL; |
3599 | 1.36k | } |
3600 | 2.57k | ss->statelessResume = PR_FALSE; |
3601 | 2.57k | ss->xtnData.selectedPsk = NULL; |
3602 | 2.57k | } |
3603 | | |
3604 | 2.58k | if (ss->statelessResume) { |
3605 | 0 | PORT_Assert(sid->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
3606 | 0 | if (tls13_GetHash(ss) != |
3607 | 0 | tls13_GetHashForCipherSuite(sid->u.ssl3.cipherSuite)) { |
3608 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_SERVER_HELLO, |
3609 | 0 | illegal_parameter); |
3610 | 0 | return SECFailure; |
3611 | 0 | } |
3612 | 0 | } |
3613 | | |
3614 | | /* Now create a synthetic kea_def that we can tweak. */ |
3615 | 2.58k | ss->ssl3.hs.kea_def_mutable = *ss->ssl3.hs.kea_def; |
3616 | 2.58k | ss->ssl3.hs.kea_def = &ss->ssl3.hs.kea_def_mutable; |
3617 | | |
3618 | 2.58k | if (ss->xtnData.selectedPsk) { |
3619 | 12 | ss->ssl3.hs.kea_def_mutable.authKeyType = ssl_auth_psk; |
3620 | 12 | if (ss->statelessResume) { |
3621 | 0 | tls13_RestoreCipherInfo(ss, sid); |
3622 | 0 | if (sid->peerCert) { |
3623 | 0 | ss->sec.peerCert = CERT_DupCertificate(sid->peerCert); |
3624 | 0 | } |
3625 | |
|
3626 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hsh_sid_cache_hits); |
3627 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hsh_sid_stateless_resumes); |
3628 | 12 | } else { |
3629 | 12 | ss->sec.authType = ssl_auth_psk; |
3630 | 12 | } |
3631 | 2.57k | } else { |
3632 | 2.57k | if (ss->statelessResume && |
3633 | 0 | ssl3_ExtensionAdvertised(ss, ssl_tls13_pre_shared_key_xtn)) { |
3634 | 0 | SSL_AtomicIncrementLong(&ssl3stats->hsh_sid_cache_misses); |
3635 | 0 | } |
3636 | 2.57k | if (sid->cached == in_client_cache) { |
3637 | | /* If we tried to resume and failed, let's not try again. */ |
3638 | 0 | ssl_UncacheSessionID(ss); |
3639 | 0 | } |
3640 | 2.57k | } |
3641 | | |
3642 | | /* Discard current SID and make a new one, though it may eventually |
3643 | | * end up looking a lot like the old one. |
3644 | | */ |
3645 | 2.58k | ssl_FreeSID(sid); |
3646 | 2.58k | ss->sec.ci.sid = sid = ssl3_NewSessionID(ss, PR_FALSE); |
3647 | 2.58k | if (sid == NULL) { |
3648 | 0 | FATAL_ERROR(ss, PORT_GetError(), internal_error); |
3649 | 0 | return SECFailure; |
3650 | 0 | } |
3651 | 2.58k | if (ss->statelessResume) { |
3652 | 0 | PORT_Assert(ss->sec.peerCert); |
3653 | 0 | sid->peerCert = CERT_DupCertificate(ss->sec.peerCert); |
3654 | 0 | } |
3655 | 2.58k | sid->version = ss->version; |
3656 | | |
3657 | 2.58k | rv = tls13_HandleServerKeyShare(ss); |
3658 | 2.58k | if (rv != SECSuccess) { |
3659 | 216 | return SECFailure; |
3660 | 216 | } |
3661 | | |
3662 | 2.37k | rv = tls13_ComputeHandshakeSecret(ss); |
3663 | 2.37k | if (rv != SECSuccess) { |
3664 | 0 | return SECFailure; /* error code is set. */ |
3665 | 0 | } |
3666 | | |
3667 | 2.37k | rv = tls13_MaybeHandleEchSignal(ss, savedMsg, savedLength, PR_FALSE); |
3668 | 2.37k | if (rv != SECSuccess) { |
3669 | 0 | return SECFailure; /* error code is set. */ |
3670 | 0 | } |
3671 | | |
3672 | 2.37k | rv = tls13_ComputeHandshakeSecrets(ss); |
3673 | 2.37k | if (rv != SECSuccess) { |
3674 | 0 | return SECFailure; /* error code is set. */ |
3675 | 0 | } |
3676 | | |
3677 | 2.37k | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent) { |
3678 | | /* When we send 0-RTT, we saved the null spec in case we needed it to |
3679 | | * send another ClientHello in response to a HelloRetryRequest. Now |
3680 | | * that we won't be receiving a HelloRetryRequest, release the spec. */ |
3681 | 0 | ssl_CipherSpecReleaseByEpoch(ss, ssl_secret_write, TrafficKeyClearText); |
3682 | 0 | } |
3683 | | |
3684 | 2.37k | rv = tls13_SetCipherSpec(ss, TrafficKeyHandshake, |
3685 | 2.37k | ssl_secret_read, PR_FALSE); |
3686 | 2.37k | if (rv != SECSuccess) { |
3687 | 0 | FATAL_ERROR(ss, SSL_ERROR_INIT_CIPHER_SUITE_FAILURE, internal_error); |
3688 | 0 | return SECFailure; |
3689 | 0 | } |
3690 | 2.37k | TLS13_SET_HS_STATE(ss, wait_encrypted_extensions); |
3691 | | |
3692 | 2.37k | return SECSuccess; |
3693 | 2.37k | } |
3694 | | |
3695 | | static void |
3696 | | tls13_SetKeyExchangeType(sslSocket *ss, const sslNamedGroupDef *group) |
3697 | 5.29k | { |
3698 | 5.29k | ss->sec.keaGroup = group; |
3699 | 5.29k | switch (group->keaType) { |
3700 | | /* Note: These overwrite on resumption.... so if you start with ECDH |
3701 | | * and resume with DH, we report DH. That's fine, since no answer |
3702 | | * is really right. */ |
3703 | 2.11k | case ssl_kea_ecdh: |
3704 | 2.11k | ss->ssl3.hs.kea_def_mutable.exchKeyType = |
3705 | 2.11k | ss->statelessResume ? ssl_kea_ecdh_psk : ssl_kea_ecdh; |
3706 | 2.11k | ss->sec.keaType = ssl_kea_ecdh; |
3707 | 2.11k | break; |
3708 | 841 | case ssl_kea_ecdh_hybrid: |
3709 | 841 | ss->ssl3.hs.kea_def_mutable.exchKeyType = |
3710 | 841 | ss->statelessResume ? ssl_kea_ecdh_hybrid_psk : ssl_kea_ecdh_hybrid; |
3711 | 841 | ss->sec.keaType = ssl_kea_ecdh_hybrid; |
3712 | 841 | break; |
3713 | 2.34k | case ssl_kea_dh: |
3714 | 2.34k | ss->ssl3.hs.kea_def_mutable.exchKeyType = |
3715 | 2.34k | ss->statelessResume ? ssl_kea_dh_psk : ssl_kea_dh; |
3716 | 2.34k | ss->sec.keaType = ssl_kea_dh; |
3717 | 2.34k | break; |
3718 | 0 | default: |
3719 | 0 | PORT_Assert(0); |
3720 | 5.29k | } |
3721 | 5.29k | } |
3722 | | |
3723 | | /* |
3724 | | * Called from ssl3_HandleServerHello. |
3725 | | * |
3726 | | * Caller must hold Handshake and RecvBuf locks. |
3727 | | */ |
3728 | | static SECStatus |
3729 | | tls13_HandleServerKeyShare(sslSocket *ss) |
3730 | 2.58k | { |
3731 | 2.58k | SECStatus rv; |
3732 | 2.58k | TLS13KeyShareEntry *entry; |
3733 | 2.58k | sslEphemeralKeyPair *keyPair; |
3734 | 2.58k | PK11SymKey *dheSecret = NULL; |
3735 | 2.58k | PK11SymKey *kemSecret = NULL; |
3736 | | |
3737 | 2.58k | SSL_TRC(3, ("%d: TLS13[%d]: handle server_key_share handshake", |
3738 | 2.58k | SSL_GETPID(), ss->fd)); |
3739 | 2.58k | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
3740 | 2.58k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3741 | | |
3742 | | /* This list should have one entry. */ |
3743 | 2.58k | if (PR_CLIST_IS_EMPTY(&ss->xtnData.remoteKeyShares)) { |
3744 | 38 | FATAL_ERROR(ss, SSL_ERROR_MISSING_KEY_SHARE, missing_extension); |
3745 | 38 | return SECFailure; |
3746 | 38 | } |
3747 | | |
3748 | 2.55k | entry = (TLS13KeyShareEntry *)PR_NEXT_LINK(&ss->xtnData.remoteKeyShares); |
3749 | 2.55k | PORT_Assert(PR_NEXT_LINK(&entry->link) == &ss->xtnData.remoteKeyShares); |
3750 | | |
3751 | | /* Now get our matching key. */ |
3752 | 2.55k | keyPair = ssl_LookupEphemeralKeyPair(ss, entry->group); |
3753 | 2.55k | if (!keyPair) { |
3754 | 27 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_KEY_SHARE, illegal_parameter); |
3755 | 27 | return SECFailure; |
3756 | 27 | } |
3757 | | |
3758 | 2.52k | PORT_Assert(ssl_NamedGroupEnabled(ss, entry->group)); |
3759 | | |
3760 | 2.52k | rv = tls13_HandleKeyShare(ss, entry, keyPair->keys, |
3761 | 2.52k | tls13_GetHash(ss), |
3762 | 2.52k | &dheSecret); |
3763 | 2.52k | if (rv != SECSuccess) { |
3764 | 130 | goto loser; /* Error code already set. */ |
3765 | 130 | } |
3766 | | |
3767 | 2.39k | if (entry->group->keaType == ssl_kea_ecdh_hybrid) { |
3768 | 123 | rv = tls13_HandleKEMCiphertext(ss, entry, keyPair->kemKeys, &kemSecret); |
3769 | 123 | if (rv != SECSuccess) { |
3770 | 21 | goto loser; /* Error set by tls13_HandleKEMCiphertext */ |
3771 | 21 | } |
3772 | 102 | switch (entry->group->name) { |
3773 | 0 | case ssl_grp_kem_secp384r1mlkem1024: |
3774 | 0 | case ssl_grp_kem_secp256r1mlkem768: |
3775 | 0 | case ssl_grp_kem_xyber768d00: |
3776 | 0 | ss->ssl3.hs.dheSecret = PK11_ConcatSymKeys(dheSecret, kemSecret, CKM_HKDF_DERIVE, CKA_DERIVE); |
3777 | 0 | break; |
3778 | 102 | case ssl_grp_kem_mlkem768x25519: |
3779 | 102 | ss->ssl3.hs.dheSecret = PK11_ConcatSymKeys(kemSecret, dheSecret, CKM_HKDF_DERIVE, CKA_DERIVE); |
3780 | 102 | break; |
3781 | 0 | default: |
3782 | 0 | PORT_Assert(0); |
3783 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
3784 | 0 | ss->ssl3.hs.dheSecret = NULL; |
3785 | 0 | break; |
3786 | 102 | } |
3787 | 102 | if (!ss->ssl3.hs.dheSecret) { |
3788 | 0 | goto loser; /* Error set by PK11_ConcatSymKeys */ |
3789 | 0 | } |
3790 | 102 | PK11_FreeSymKey(dheSecret); |
3791 | 102 | PK11_FreeSymKey(kemSecret); |
3792 | 2.27k | } else { |
3793 | 2.27k | ss->ssl3.hs.dheSecret = dheSecret; |
3794 | 2.27k | } |
3795 | | |
3796 | 2.37k | tls13_SetKeyExchangeType(ss, entry->group); |
3797 | 2.37k | ss->sec.keaKeyBits = SECKEY_PublicKeyStrengthInBits(keyPair->keys->pubKey); |
3798 | | |
3799 | 2.37k | return SECSuccess; |
3800 | | |
3801 | 151 | loser: |
3802 | 151 | PK11_FreeSymKey(dheSecret); |
3803 | 151 | PK11_FreeSymKey(kemSecret); |
3804 | 151 | FATAL_ERROR(ss, PORT_GetError(), illegal_parameter); |
3805 | 151 | return SECFailure; |
3806 | 2.39k | } |
3807 | | |
3808 | | static PRBool |
3809 | | tls13_FindCompressionAlgAndCheckIfSupportsEncoding(sslSocket *ss) |
3810 | 4 | { |
3811 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3812 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3813 | | |
3814 | 4 | for (int j = 0; j < ss->ssl3.supportedCertCompressionAlgorithmsCount; j++) { |
3815 | 4 | if (ss->ssl3.supportedCertCompressionAlgorithms[j].id == ss->xtnData.compressionAlg) { |
3816 | 4 | if (ss->ssl3.supportedCertCompressionAlgorithms[j].encode != NULL) { |
3817 | 4 | return PR_TRUE; |
3818 | 4 | } |
3819 | 0 | return PR_FALSE; |
3820 | 4 | } |
3821 | 4 | } |
3822 | | |
3823 | 0 | return PR_FALSE; |
3824 | 4 | } |
3825 | | |
3826 | | static SECStatus |
3827 | | tls13_FindCompressionAlgAndEncodeCertificate( |
3828 | | sslSocket *ss, SECItem *certificateToEncode, SECItem *encodedCertificate) |
3829 | 4 | { |
3830 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3831 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3832 | | |
3833 | 4 | SECStatus rv = SECFailure; |
3834 | 4 | for (int j = 0; j < ss->ssl3.supportedCertCompressionAlgorithmsCount; j++) { |
3835 | 4 | if (ss->ssl3.supportedCertCompressionAlgorithms[j].id == ss->xtnData.compressionAlg && |
3836 | 4 | ss->ssl3.supportedCertCompressionAlgorithms[j].encode != NULL) { |
3837 | 4 | rv = ss->ssl3.supportedCertCompressionAlgorithms[j].encode( |
3838 | 4 | certificateToEncode, encodedCertificate); |
3839 | 4 | return rv; |
3840 | 4 | } |
3841 | 4 | } |
3842 | | |
3843 | 0 | PORT_SetError(SEC_ERROR_CERTIFICATE_COMPRESSION_ALGORITHM_NOT_SUPPORTED); |
3844 | 0 | return SECFailure; |
3845 | 4 | } |
3846 | | |
3847 | | static SECStatus |
3848 | | tls13_SendCompressedCertificate(sslSocket *ss, sslBuffer *bufferCertificate) |
3849 | 4 | { |
3850 | | /* TLS Certificate Compression. RFC 8879 */ |
3851 | | /* As the encoding function takes as input a SECItem, |
3852 | | * we convert bufferCertificate to certificateToEncode. |
3853 | | * |
3854 | | * encodedCertificate is used to store the certificate |
3855 | | * after encoding. |
3856 | | */ |
3857 | 4 | SECItem encodedCertificate = { siBuffer, NULL, 0 }; |
3858 | 4 | SECItem certificateToEncode = { siBuffer, NULL, 0 }; |
3859 | 4 | SECStatus rv = SECFailure; |
3860 | | |
3861 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3862 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3863 | | |
3864 | 4 | SSL_TRC(30, ("%d: TLS13[%d]: %s is encoding the certificate using the %s compression algorithm", |
3865 | 4 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
3866 | 4 | ssl3_mapCertificateCompressionAlgorithmToName(ss, ss->xtnData.compressionAlg))); |
3867 | | |
3868 | 4 | PRINT_BUF(50, (NULL, "The certificate before encoding:", |
3869 | 4 | bufferCertificate->buf, bufferCertificate->len)); |
3870 | | |
3871 | 4 | PRUint32 lengthUnencodedMessage = bufferCertificate->len; |
3872 | 4 | rv = ssl3_CopyToSECItem(bufferCertificate, &certificateToEncode); |
3873 | 4 | if (rv != SECSuccess) { |
3874 | 0 | SSL_TRC(50, ("%d: TLS13[%d]: %s has failed encoding the certificate.", |
3875 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
3876 | 0 | goto loser; /* Code already set. */ |
3877 | 0 | } |
3878 | | |
3879 | 4 | rv = tls13_FindCompressionAlgAndEncodeCertificate(ss, &certificateToEncode, |
3880 | 4 | &encodedCertificate); |
3881 | 4 | if (rv != SECSuccess) { |
3882 | 0 | SSL_TRC(50, ("%d: TLS13[%d]: %s has failed encoding the certificate.", |
3883 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
3884 | 0 | PORT_SetError(SEC_ERROR_NO_MEMORY); |
3885 | 0 | goto loser; /* Code already set. */ |
3886 | 0 | } |
3887 | | |
3888 | | /* The CompressedCertificate message is formed as follows: |
3889 | | * struct { |
3890 | | * CertificateCompressionAlgorithm algorithm; |
3891 | | * uint24 uncompressed_length; |
3892 | | * opaque compressed_certificate_message<1..2^24-1>; |
3893 | | * } CompressedCertificate; |
3894 | | */ |
3895 | | |
3896 | 4 | if (encodedCertificate.len < 1) { |
3897 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
3898 | 0 | goto loser; |
3899 | 0 | } |
3900 | | |
3901 | 4 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_compressed_certificate, |
3902 | 4 | encodedCertificate.len + 2 + 3 + 3); |
3903 | 4 | if (rv != SECSuccess) { |
3904 | 0 | goto loser; /* err set by AppendHandshake. */ |
3905 | 0 | } |
3906 | | |
3907 | 4 | rv = ssl3_AppendHandshakeNumber(ss, ss->xtnData.compressionAlg, 2); |
3908 | 4 | if (rv != SECSuccess) { |
3909 | 0 | goto loser; /* err set by AppendHandshake. */ |
3910 | 0 | } |
3911 | | |
3912 | 4 | rv = ssl3_AppendHandshakeNumber(ss, lengthUnencodedMessage, 3); |
3913 | 4 | if (rv != SECSuccess) { |
3914 | 0 | goto loser; /* err set by AppendHandshake. */ |
3915 | 0 | } |
3916 | | |
3917 | 4 | PRINT_BUF(30, (NULL, "The encoded certificate: ", |
3918 | 4 | encodedCertificate.data, encodedCertificate.len)); |
3919 | | |
3920 | 4 | rv = ssl3_AppendHandshakeVariable(ss, encodedCertificate.data, encodedCertificate.len, 3); |
3921 | 4 | if (rv != SECSuccess) { |
3922 | 0 | goto loser; /* err set by AppendHandshake. */ |
3923 | 0 | } |
3924 | | |
3925 | 4 | SECITEM_FreeItem(&certificateToEncode, PR_FALSE); |
3926 | 4 | SECITEM_FreeItem(&encodedCertificate, PR_FALSE); |
3927 | 4 | return SECSuccess; |
3928 | | |
3929 | 0 | loser: |
3930 | 0 | SECITEM_FreeItem(&certificateToEncode, PR_FALSE); |
3931 | 0 | SECITEM_FreeItem(&encodedCertificate, PR_FALSE); |
3932 | 0 | return SECFailure; |
3933 | 4 | } |
3934 | | |
3935 | | /* |
3936 | | * opaque ASN1Cert<1..2^24-1>; |
3937 | | * |
3938 | | * struct { |
3939 | | * ASN1Cert cert_data; |
3940 | | * Extension extensions<0..2^16-1>; |
3941 | | * } CertificateEntry; |
3942 | | * |
3943 | | * struct { |
3944 | | * opaque certificate_request_context<0..2^8-1>; |
3945 | | * CertificateEntry certificate_list<0..2^24-1>; |
3946 | | * } Certificate; |
3947 | | */ |
3948 | | static SECStatus |
3949 | | tls13_SendCertificate(sslSocket *ss) |
3950 | 2.74k | { |
3951 | 2.74k | SECStatus rv; |
3952 | 2.74k | CERTCertificateList *certChain; |
3953 | 2.74k | int certChainLen = 0; |
3954 | 2.74k | int i; |
3955 | 2.74k | SECItem context = { siBuffer, NULL, 0 }; |
3956 | 2.74k | sslBuffer extensionBuf = SSL_BUFFER_EMPTY; |
3957 | 2.74k | sslBuffer bufferCertificate = SSL_BUFFER_EMPTY; |
3958 | | |
3959 | 2.74k | SSL_TRC(3, ("%d: TLS1.3[%d]: send certificate handshake", |
3960 | 2.74k | SSL_GETPID(), ss->fd)); |
3961 | | |
3962 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
3963 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
3964 | | |
3965 | 2.74k | if (ss->sec.isServer) { |
3966 | 2.74k | PORT_Assert(!ss->sec.localCert); |
3967 | | /* A server certificate is selected in tls13_SelectServerCert(). */ |
3968 | 2.74k | PORT_Assert(ss->sec.serverCert); |
3969 | | |
3970 | 2.74k | certChain = ss->sec.serverCert->serverCertChain; |
3971 | 2.74k | ss->sec.localCert = CERT_DupCertificate(ss->sec.serverCert->serverCert); |
3972 | 2.74k | } else { |
3973 | 0 | if (ss->sec.localCert) |
3974 | 0 | CERT_DestroyCertificate(ss->sec.localCert); |
3975 | |
|
3976 | 0 | certChain = ss->ssl3.clientCertChain; |
3977 | 0 | ss->sec.localCert = CERT_DupCertificate(ss->ssl3.clientCertificate); |
3978 | 0 | } |
3979 | | |
3980 | 2.74k | if (!ss->sec.isServer) { |
3981 | 0 | PORT_Assert(ss->ssl3.hs.clientCertRequested); |
3982 | 0 | context = ss->xtnData.certReqContext; |
3983 | 0 | } |
3984 | | |
3985 | 2.74k | if (certChain) { |
3986 | 5.49k | for (i = 0; i < certChain->len; i++) { |
3987 | | /* Each cert is 3 octet length, cert, and extensions */ |
3988 | 2.74k | certChainLen += 3 + certChain->certs[i].len + 2; |
3989 | 2.74k | } |
3990 | | |
3991 | | /* Build the extensions. This only applies to the leaf cert, because we |
3992 | | * don't yet send extensions for non-leaf certs. */ |
3993 | 2.74k | rv = ssl_ConstructExtensions(ss, &extensionBuf, ssl_hs_certificate); |
3994 | 2.74k | if (rv != SECSuccess) { |
3995 | 0 | return SECFailure; /* code already set */ |
3996 | 0 | } |
3997 | | /* extensionBuf.len is only added once, for the leaf cert. */ |
3998 | 2.74k | certChainLen += SSL_BUFFER_LEN(&extensionBuf); |
3999 | 2.74k | } |
4000 | | |
4001 | 2.74k | rv = sslBuffer_AppendVariable(&bufferCertificate, context.data, context.len, 1); |
4002 | 2.74k | if (rv != SECSuccess) { |
4003 | 0 | goto loser; /* Code already set. */ |
4004 | 0 | } |
4005 | | |
4006 | 2.74k | rv = sslBuffer_AppendNumber(&bufferCertificate, certChainLen, 3); |
4007 | 2.74k | if (rv != SECSuccess) { |
4008 | 0 | goto loser; /* Code already set. */ |
4009 | 0 | } |
4010 | | |
4011 | 2.74k | if (certChain) { |
4012 | 5.49k | for (i = 0; i < certChain->len; i++) { |
4013 | 2.74k | rv = sslBuffer_AppendVariable(&bufferCertificate, certChain->certs[i].data, |
4014 | 2.74k | certChain->certs[i].len, 3); |
4015 | 2.74k | if (rv != SECSuccess) { |
4016 | 0 | goto loser; /* Code already set. */ |
4017 | 0 | } |
4018 | | |
4019 | 2.74k | if (i) { |
4020 | | /* Not end-entity. */ |
4021 | 0 | rv = sslBuffer_AppendNumber(&bufferCertificate, 0, 2); |
4022 | 0 | if (rv != SECSuccess) { |
4023 | 0 | goto loser; /* Code already set. */ |
4024 | 0 | } |
4025 | 0 | continue; |
4026 | 0 | } |
4027 | | |
4028 | 2.74k | rv = sslBuffer_AppendBufferVariable(&bufferCertificate, &extensionBuf, 2); |
4029 | 2.74k | if (rv != SECSuccess) { |
4030 | 0 | goto loser; /* Code already set. */ |
4031 | 0 | } |
4032 | 2.74k | } |
4033 | 2.74k | } |
4034 | | |
4035 | | /* If no compression mechanism was established or |
4036 | | * the compression mechanism supports only decoding, |
4037 | | * we continue as before. */ |
4038 | 2.74k | if (ss->xtnData.compressionAlg == 0 || !tls13_FindCompressionAlgAndCheckIfSupportsEncoding(ss)) { |
4039 | 2.74k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate, |
4040 | 2.74k | 1 + context.len + 3 + certChainLen); |
4041 | 2.74k | if (rv != SECSuccess) { |
4042 | 0 | goto loser; /* err set by AppendHandshake. */ |
4043 | 0 | } |
4044 | 2.74k | rv = ssl3_AppendBufferToHandshake(ss, &bufferCertificate); |
4045 | 2.74k | if (rv != SECSuccess) { |
4046 | 0 | goto loser; /* err set by AppendHandshake. */ |
4047 | 0 | } |
4048 | 2.74k | } else { |
4049 | 4 | rv = tls13_SendCompressedCertificate(ss, &bufferCertificate); |
4050 | 4 | if (rv != SECSuccess) { |
4051 | 0 | goto loser; /* err set by tls13_SendCompressedCertificate. */ |
4052 | 0 | } |
4053 | 4 | } |
4054 | | |
4055 | 2.74k | sslBuffer_Clear(&bufferCertificate); |
4056 | 2.74k | sslBuffer_Clear(&extensionBuf); |
4057 | 2.74k | return SECSuccess; |
4058 | | |
4059 | 0 | loser: |
4060 | 0 | sslBuffer_Clear(&bufferCertificate); |
4061 | 0 | sslBuffer_Clear(&extensionBuf); |
4062 | 0 | return SECFailure; |
4063 | 2.74k | } |
4064 | | |
4065 | | static SECStatus |
4066 | | tls13_HandleCertificateEntry(sslSocket *ss, SECItem *data, PRBool first, |
4067 | | SECItem *certData) |
4068 | 11 | { |
4069 | 11 | SECStatus rv; |
4070 | 11 | SECItem extensionsData; |
4071 | | |
4072 | 11 | rv = ssl3_ConsumeHandshakeVariable(ss, certData, |
4073 | 11 | 3, &data->data, &data->len); |
4074 | 11 | if (rv != SECSuccess) { |
4075 | 5 | return SECFailure; |
4076 | 5 | } |
4077 | | |
4078 | 6 | rv = ssl3_ConsumeHandshakeVariable(ss, &extensionsData, |
4079 | 6 | 2, &data->data, &data->len); |
4080 | 6 | if (rv != SECSuccess) { |
4081 | 2 | return SECFailure; |
4082 | 2 | } |
4083 | | |
4084 | | /* Parse all the extensions. */ |
4085 | 4 | if (first && !ss->sec.isServer) { |
4086 | 0 | rv = ssl3_HandleExtensions(ss, &extensionsData.data, |
4087 | 0 | &extensionsData.len, |
4088 | 0 | ssl_hs_certificate); |
4089 | 0 | if (rv != SECSuccess) { |
4090 | 0 | return SECFailure; |
4091 | 0 | } |
4092 | | /* TODO(ekr@rtfm.com): Copy out SCTs. Bug 1315727. */ |
4093 | 0 | } |
4094 | | |
4095 | 4 | return SECSuccess; |
4096 | 4 | } |
4097 | | |
4098 | | static SECStatus |
4099 | | tls13_EnsureCerticateExpected(sslSocket *ss) |
4100 | 128 | { |
4101 | 128 | SECStatus rv = SECFailure; |
4102 | 128 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
4103 | 128 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4104 | | |
4105 | 128 | if (ss->sec.isServer) { |
4106 | | /* Receiving this message might be the first sign we have that |
4107 | | * early data is over, so pretend we received EOED. */ |
4108 | 120 | rv = tls13_MaybeHandleSuppressedEndOfEarlyData(ss); |
4109 | 120 | if (rv != SECSuccess) { |
4110 | 0 | return SECFailure; /* Code already set. */ |
4111 | 0 | } |
4112 | | |
4113 | 120 | if (ss->ssl3.clientCertRequested) { |
4114 | 0 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERTIFICATE, |
4115 | 0 | idle_handshake); |
4116 | 120 | } else { |
4117 | 120 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERTIFICATE, |
4118 | 120 | wait_client_cert); |
4119 | 120 | } |
4120 | 120 | } else { |
4121 | 8 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERTIFICATE, |
4122 | 8 | wait_cert_request, wait_server_cert); |
4123 | 8 | } |
4124 | 128 | return rv; |
4125 | 128 | } |
4126 | | |
4127 | | /* RFC 8879 TLS Certificate Compression |
4128 | | * struct { |
4129 | | * CertificateCompressionAlgorithm algorithm; |
4130 | | * uint24 uncompressed_length; |
4131 | | * opaque compressed_certificate_message<1..2^24-1>; |
4132 | | * } CompressedCertificate; |
4133 | | */ |
4134 | | static SECStatus |
4135 | | tls13_HandleCertificateDecode(sslSocket *ss, PRUint8 *b, PRUint32 length) |
4136 | 60 | { |
4137 | 60 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
4138 | 60 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4139 | | |
4140 | 60 | SECStatus rv = SECFailure; |
4141 | | |
4142 | 60 | if (!ss->xtnData.certificateCompressionAdvertised) { |
4143 | 14 | FATAL_ERROR(ss, SEC_ERROR_UNEXPECTED_COMPRESSED_CERTIFICATE, decode_error); |
4144 | 14 | return SECFailure; |
4145 | 14 | } |
4146 | | |
4147 | 46 | rv = tls13_EnsureCerticateExpected(ss); |
4148 | 46 | if (rv != SECSuccess) { |
4149 | 3 | return SECFailure; /* Code already set. */ |
4150 | 3 | } |
4151 | | |
4152 | 43 | if (ss->firstHsDone) { |
4153 | 0 | rv = ssl_HashPostHandshakeMessage(ss, ssl_hs_compressed_certificate, b, length); |
4154 | 0 | if (rv != SECSuccess) { |
4155 | 0 | return rv; |
4156 | 0 | } |
4157 | 0 | } |
4158 | | |
4159 | 43 | SSL_TRC(30, ("%d: TLS1.3[%d]: %s handles certificate compression handshake", |
4160 | 43 | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
4161 | | |
4162 | 43 | PRINT_BUF(50, (NULL, "The certificate before decoding:", b, length)); |
4163 | | /* Reading CertificateCompressionAlgorithm. */ |
4164 | 43 | PRUint32 compressionAlg = 0; |
4165 | 43 | rv = ssl3_ConsumeHandshakeNumber(ss, &compressionAlg, 2, &b, &length); |
4166 | 43 | if (rv != SECSuccess) { |
4167 | 2 | return SECFailure; /* Alert already sent. */ |
4168 | 2 | } |
4169 | | |
4170 | 41 | PRBool compressionAlgorithmIsSupported = PR_FALSE; |
4171 | 41 | SECStatus (*certificateDecodingFunc)(const SECItem *, |
4172 | 41 | unsigned char *output, size_t outputLen, size_t *usedLen) = NULL; |
4173 | 82 | for (int i = 0; i < ss->ssl3.supportedCertCompressionAlgorithmsCount; i++) { |
4174 | 41 | if (ss->ssl3.supportedCertCompressionAlgorithms[i].id == compressionAlg) { |
4175 | 24 | compressionAlgorithmIsSupported = PR_TRUE; |
4176 | 24 | certificateDecodingFunc = ss->ssl3.supportedCertCompressionAlgorithms[i].decode; |
4177 | 24 | } |
4178 | 41 | } |
4179 | | |
4180 | | /* Peer selected a compression algorithm we do not support (and did not advertise). */ |
4181 | 41 | if (!compressionAlgorithmIsSupported) { |
4182 | 17 | PORT_SetError(SEC_ERROR_CERTIFICATE_COMPRESSION_ALGORITHM_NOT_SUPPORTED); |
4183 | 17 | FATAL_ERROR(ss, PORT_GetError(), illegal_parameter); |
4184 | 17 | return SECFailure; |
4185 | 17 | } |
4186 | | |
4187 | | /* The algorithm does not support decoding. */ |
4188 | 24 | if (certificateDecodingFunc == NULL) { |
4189 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4190 | 0 | FATAL_ERROR(ss, PORT_GetError(), illegal_parameter); |
4191 | 0 | return SECFailure; |
4192 | 0 | } |
4193 | | |
4194 | 24 | SSL_TRC(30, ("%d: TLS13[%d]: %s is decoding the certificate using the %s compression algorithm", |
4195 | 24 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
4196 | 24 | ssl3_mapCertificateCompressionAlgorithmToName(ss, compressionAlg))); |
4197 | 24 | PRUint32 decodedCertLen = 0; |
4198 | 24 | rv = ssl3_ConsumeHandshakeNumber(ss, &decodedCertLen, 3, &b, &length); |
4199 | 24 | if (rv != SECSuccess) { |
4200 | 1 | return SECFailure; /* alert has been sent */ |
4201 | 1 | } |
4202 | | |
4203 | | /* If the received CompressedCertificate message cannot be decompressed, |
4204 | | * he connection MUST be terminated with the "bad_certificate" alert. |
4205 | | */ |
4206 | 23 | if (decodedCertLen == 0) { |
4207 | 1 | SSL_TRC(50, ("%d: TLS13[%d]: %s decoded certificate length is incorrect", |
4208 | 1 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
4209 | 1 | ssl3_mapCertificateCompressionAlgorithmToName(ss, compressionAlg))); |
4210 | 1 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4211 | 1 | return SECFailure; |
4212 | 1 | } |
4213 | | |
4214 | | /* Cap the decompressed size to prevent memory exhaustion. The wire field |
4215 | | * is a uint24 (max 16MB) but the CompressedCertificate path bypasses the |
4216 | | * 128KB cap applied to regular handshake messages. 100KB matches the limit |
4217 | | * enforced by OpenSSL and BoringSSL. */ |
4218 | 22 | #define MAX_CERT_UNCOMPRESSED_LEN (100 * 1024) |
4219 | 22 | if (decodedCertLen > MAX_CERT_UNCOMPRESSED_LEN) { |
4220 | 3 | SSL_TRC(50, ("%d: TLS13[%d]: %s uncompressed_length %u exceeds limit %u", |
4221 | 3 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
4222 | 3 | decodedCertLen, MAX_CERT_UNCOMPRESSED_LEN)); |
4223 | 3 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4224 | 3 | return SECFailure; |
4225 | 3 | } |
4226 | 19 | #undef MAX_CERT_UNCOMPRESSED_LEN |
4227 | | |
4228 | | /* opaque compressed_certificate_message<1..2^24-1>; */ |
4229 | 19 | PRUint32 compressedCertLen = 0; |
4230 | 19 | rv = ssl3_ConsumeHandshakeNumber(ss, &compressedCertLen, 3, &b, &length); |
4231 | 19 | if (rv != SECSuccess) { |
4232 | 2 | return SECFailure; /* alert has been sent */ |
4233 | 2 | } |
4234 | | |
4235 | 17 | if (compressedCertLen == 0 || compressedCertLen != length) { |
4236 | 17 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4237 | 17 | return SECFailure; |
4238 | 17 | } |
4239 | | |
4240 | | /* Decoding received certificate. */ |
4241 | 0 | PRUint8 *decodedCert = PORT_ZAlloc(decodedCertLen); |
4242 | 0 | if (!decodedCert) { |
4243 | 0 | return SECFailure; |
4244 | 0 | } |
4245 | | |
4246 | 0 | size_t actualCertLen = 0; |
4247 | |
|
4248 | 0 | SECItem encodedCertAsSecItem = { siBuffer, b, compressedCertLen }; |
4249 | 0 | rv = certificateDecodingFunc(&encodedCertAsSecItem, |
4250 | 0 | decodedCert, decodedCertLen, &actualCertLen); |
4251 | |
|
4252 | 0 | if (rv != SECSuccess) { |
4253 | 0 | SSL_TRC(50, ("%d: TLS13[%d]: %s decoding of the certificate has failed", |
4254 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
4255 | 0 | ssl3_mapCertificateCompressionAlgorithmToName(ss, compressionAlg))); |
4256 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4257 | 0 | goto loser; |
4258 | 0 | } |
4259 | 0 | PRINT_BUF(60, (ss, "consume bytes:", b, compressedCertLen)); |
4260 | 0 | *b += compressedCertLen; |
4261 | 0 | length -= compressedCertLen; |
4262 | | |
4263 | | /* If, after decompression, the specified length does not match the actual length, |
4264 | | * the party receiving the invalid message MUST abort the connection |
4265 | | * with the "bad_certificate" alert. |
4266 | | */ |
4267 | 0 | if (actualCertLen != decodedCertLen) { |
4268 | 0 | SSL_TRC(50, ("%d: TLS13[%d]: %s certificate length does not correspond to extension length", |
4269 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), |
4270 | 0 | ssl3_mapCertificateCompressionAlgorithmToName(ss, compressionAlg))); |
4271 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4272 | 0 | goto loser; |
4273 | 0 | } |
4274 | | |
4275 | 0 | PRINT_BUF(50, (NULL, "Decoded certificate", |
4276 | 0 | decodedCert, decodedCertLen)); |
4277 | | |
4278 | | /* compressed_certificate_message: The result of applying the indicated |
4279 | | * compression algorithm to the encoded Certificate message that |
4280 | | * would have been sent if certificate compression was not in use. |
4281 | | * |
4282 | | * After decompression, the Certificate message MUST be processed as if |
4283 | | * it were encoded without being compressed. This way, the parsing and |
4284 | | * the verification have the same security properties as they would have |
4285 | | * in TLS normally. |
4286 | | */ |
4287 | 0 | rv = tls13_HandleCertificate(ss, decodedCert, decodedCertLen, PR_TRUE); |
4288 | 0 | if (rv != SECSuccess) { |
4289 | 0 | goto loser; |
4290 | 0 | } |
4291 | | /* We allow only one compressed certificate to be handled after each |
4292 | | certificate compression advertisement. |
4293 | | See test CertificateCompression_TwoEncodedCertificateRequests. */ |
4294 | 0 | ss->xtnData.certificateCompressionAdvertised = PR_FALSE; |
4295 | 0 | PORT_Free(decodedCert); |
4296 | 0 | return SECSuccess; |
4297 | | |
4298 | 0 | loser: |
4299 | 0 | PORT_Free(decodedCert); |
4300 | 0 | return SECFailure; |
4301 | 0 | } |
4302 | | |
4303 | | /* Called from tls13_CompleteHandleHandshakeMessage() when it has deciphered a complete |
4304 | | * tls13 Certificate message. |
4305 | | * Caller must hold Handshake and RecvBuf locks. |
4306 | | */ |
4307 | | static SECStatus |
4308 | | tls13_HandleCertificate(sslSocket *ss, PRUint8 *b, PRUint32 length, PRBool alreadyHashed) |
4309 | 82 | { |
4310 | 82 | SECStatus rv; |
4311 | 82 | SECItem context = { siBuffer, NULL, 0 }; |
4312 | 82 | SECItem certList; |
4313 | 82 | PRBool first = PR_TRUE; |
4314 | 82 | ssl3CertNode *lastCert = NULL; |
4315 | | |
4316 | 82 | SSL_TRC(3, ("%d: TLS13[%d]: handle certificate handshake", |
4317 | 82 | SSL_GETPID(), ss->fd)); |
4318 | 82 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
4319 | 82 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4320 | | |
4321 | 82 | rv = tls13_EnsureCerticateExpected(ss); |
4322 | 82 | if (rv != SECSuccess) { |
4323 | 24 | return SECFailure; /* Code already set. */ |
4324 | 24 | } |
4325 | | |
4326 | | /* We can ignore any other cleartext from the client. */ |
4327 | 58 | if (ss->sec.isServer && IS_DTLS(ss)) { |
4328 | 22 | ssl_CipherSpecReleaseByEpoch(ss, ssl_secret_read, TrafficKeyClearText); |
4329 | 22 | dtls_ReceivedFirstMessageInFlight(ss); |
4330 | 22 | } |
4331 | | |
4332 | | /* AlreadyHashed is true only when Certificate Compression is used. */ |
4333 | 58 | if (ss->firstHsDone && !alreadyHashed) { |
4334 | 0 | rv = ssl_HashPostHandshakeMessage(ss, ssl_hs_certificate, b, length); |
4335 | 0 | if (rv != SECSuccess) { |
4336 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4337 | 0 | return SECFailure; |
4338 | 0 | } |
4339 | 0 | } |
4340 | | |
4341 | 58 | if (!ss->firstHsDone && ss->sec.isServer) { |
4342 | | /* Our first shot an getting an RTT estimate. If the client took extra |
4343 | | * time to fetch a certificate, this will be bad, but we can't do much |
4344 | | * about that. */ |
4345 | 58 | ss->ssl3.hs.rttEstimate = ssl_Time(ss) - ss->ssl3.hs.rttEstimate; |
4346 | 58 | } |
4347 | | |
4348 | | /* Process the context string */ |
4349 | 58 | rv = ssl3_ConsumeHandshakeVariable(ss, &context, 1, &b, &length); |
4350 | 58 | if (rv != SECSuccess) |
4351 | 3 | return SECFailure; |
4352 | | |
4353 | 55 | if (ss->ssl3.clientCertRequested) { |
4354 | 0 | PORT_Assert(ss->sec.isServer); |
4355 | 0 | if (SECITEM_CompareItem(&context, &ss->xtnData.certReqContext) != 0) { |
4356 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, illegal_parameter); |
4357 | 0 | return SECFailure; |
4358 | 0 | } |
4359 | 0 | } |
4360 | 55 | rv = ssl3_ConsumeHandshakeVariable(ss, &certList, 3, &b, &length); |
4361 | 55 | if (rv != SECSuccess) { |
4362 | 31 | return SECFailure; |
4363 | 31 | } |
4364 | 24 | if (length) { |
4365 | 4 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, illegal_parameter); |
4366 | 4 | return SECFailure; |
4367 | 4 | } |
4368 | | |
4369 | 20 | if (!certList.len) { |
4370 | 9 | if (!ss->sec.isServer) { |
4371 | | /* Servers always need to send some cert. */ |
4372 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERTIFICATE, bad_certificate); |
4373 | 0 | return SECFailure; |
4374 | 9 | } else { |
4375 | | /* This is TLS's version of a no_certificate alert. */ |
4376 | | /* I'm a server. I've requested a client cert. He hasn't got one. */ |
4377 | 9 | rv = ssl3_HandleNoCertificate(ss); |
4378 | 9 | if (rv != SECSuccess) { |
4379 | 1 | return SECFailure; |
4380 | 1 | } |
4381 | | |
4382 | 8 | TLS13_SET_HS_STATE(ss, wait_finished); |
4383 | 8 | return SECSuccess; |
4384 | 9 | } |
4385 | 9 | } |
4386 | | |
4387 | | /* Now clean up. */ |
4388 | 11 | ssl3_CleanupPeerCerts(ss); |
4389 | 11 | ss->ssl3.peerCertArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
4390 | 11 | if (ss->ssl3.peerCertArena == NULL) { |
4391 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
4392 | 0 | return SECFailure; |
4393 | 0 | } |
4394 | | |
4395 | 11 | while (certList.len) { |
4396 | 11 | SECItem derCert; // will hold a weak reference into certList |
4397 | 11 | rv = tls13_HandleCertificateEntry(ss, &certList, first, |
4398 | 11 | &derCert); |
4399 | 11 | if (rv != SECSuccess) { |
4400 | 7 | ss->xtnData.signedCertTimestamps.len = 0; |
4401 | 7 | return SECFailure; |
4402 | 7 | } |
4403 | | |
4404 | 4 | if (first) { |
4405 | 4 | ss->sec.peerCert = CERT_NewTempCertificate(ss->dbHandle, &derCert, |
4406 | 4 | NULL, PR_FALSE, PR_TRUE); |
4407 | 4 | if (!ss->sec.peerCert) { |
4408 | 4 | PRErrorCode errCode = PORT_GetError(); |
4409 | 4 | switch (errCode) { |
4410 | 0 | case PR_OUT_OF_MEMORY_ERROR: |
4411 | 0 | case SEC_ERROR_BAD_DATABASE: |
4412 | 0 | case SEC_ERROR_NO_MEMORY: |
4413 | 0 | FATAL_ERROR(ss, errCode, internal_error); |
4414 | 0 | return SECFailure; |
4415 | 4 | default: |
4416 | 4 | ssl3_SendAlertForCertError(ss, errCode); |
4417 | 4 | return SECFailure; |
4418 | 4 | } |
4419 | 4 | } |
4420 | | |
4421 | 0 | if (ss->xtnData.signedCertTimestamps.len) { |
4422 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
4423 | 0 | rv = SECITEM_CopyItem(NULL, &sid->u.ssl3.signedCertTimestamps, |
4424 | 0 | &ss->xtnData.signedCertTimestamps); |
4425 | 0 | ss->xtnData.signedCertTimestamps.len = 0; |
4426 | 0 | if (rv != SECSuccess) { |
4427 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
4428 | 0 | return SECFailure; |
4429 | 0 | } |
4430 | 0 | } |
4431 | 0 | } else { |
4432 | 0 | ssl3CertNode *c = PORT_ArenaNew(ss->ssl3.peerCertArena, |
4433 | 0 | ssl3CertNode); |
4434 | 0 | if (!c) { |
4435 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
4436 | 0 | return SECFailure; |
4437 | 0 | } |
4438 | 0 | c->derCert = SECITEM_ArenaDupItem(ss->ssl3.peerCertArena, |
4439 | 0 | &derCert); |
4440 | 0 | c->next = NULL; |
4441 | |
|
4442 | 0 | if (lastCert) { |
4443 | 0 | lastCert->next = c; |
4444 | 0 | } else { |
4445 | 0 | ss->ssl3.peerCertChain = c; |
4446 | 0 | } |
4447 | 0 | lastCert = c; |
4448 | 0 | } |
4449 | | |
4450 | 0 | first = PR_FALSE; |
4451 | 0 | } |
4452 | 0 | SECKEY_UpdateCertPQG(ss->sec.peerCert); |
4453 | |
|
4454 | 0 | return ssl3_AuthCertificate(ss); /* sets ss->ssl3.hs.ws */ |
4455 | 11 | } |
4456 | | |
4457 | | /* Add context when signing hashes as described in |
4458 | | [draft-ietf-tls-tls13; Section 4.9.1] */ |
4459 | | SECStatus |
4460 | | tls13_SignOrVerifyHashWithContext(sslSocket *ss, const SSL3Hashes *hashes, |
4461 | | SECKEYPrivateKey *privKey, SECKEYPublicKey *pubKey, |
4462 | | SSLSignatureScheme scheme, sslSignOrVerify direction, |
4463 | | SECItem *signature) |
4464 | 2.74k | { |
4465 | 2.74k | SECStatus rv = SECSuccess; |
4466 | 2.74k | tlsSignOrVerifyContext ctx = { sig_verify, { NULL } }; |
4467 | 2.74k | void *pwArg = ss->pkcs11PinArg; |
4468 | 2.74k | const unsigned char context_padding[] = { |
4469 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4470 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4471 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4472 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4473 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4474 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4475 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, |
4476 | 2.74k | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 |
4477 | 2.74k | }; |
4478 | | |
4479 | 2.74k | const char *client_cert_verify_string = "TLS 1.3, client CertificateVerify"; |
4480 | 2.74k | const char *server_cert_verify_string = "TLS 1.3, server CertificateVerify"; |
4481 | 2.74k | const char *context_string = ((direction == sig_sign && ss->sec.isServer) || |
4482 | 0 | (direction == sig_verify && !ss->sec.isServer)) |
4483 | 2.74k | ? server_cert_verify_string |
4484 | 2.74k | : client_cert_verify_string; |
4485 | | |
4486 | | /* Double check that we are doing the same hash.*/ |
4487 | 2.74k | PORT_Assert(hashes->len == tls13_GetHashSize(ss)); |
4488 | | |
4489 | 2.74k | PRINT_BUF(50, (ss, "TLS 1.3 hash without context", hashes->u.raw, hashes->len)); |
4490 | 2.74k | PRINT_BUF(50, (ss, "Context string", context_string, strlen(context_string))); |
4491 | | |
4492 | 2.74k | ctx = tls_CreateSignOrVerifyContext(privKey, pubKey, scheme, |
4493 | 2.74k | direction, signature, pwArg); |
4494 | 2.74k | if (ctx.u.ptr == NULL) { |
4495 | 0 | goto loser; |
4496 | 0 | } |
4497 | 2.74k | rv = tls_SignOrVerifyUpdate(ctx, context_padding, sizeof(context_padding)); |
4498 | 2.74k | if (rv != SECSuccess) { |
4499 | 0 | goto loser; |
4500 | 0 | } |
4501 | 2.74k | rv = tls_SignOrVerifyUpdate(ctx, (const unsigned char *)context_string, |
4502 | | /* +1 includes the terminating 0 */ |
4503 | 2.74k | strlen(context_string) + 1); |
4504 | 2.74k | if (rv != SECSuccess) { |
4505 | 0 | goto loser; |
4506 | 0 | } |
4507 | 2.74k | rv = tls_SignOrVerifyUpdate(ctx, hashes->u.raw, hashes->len); |
4508 | 2.74k | if (rv != SECSuccess) { |
4509 | 0 | goto loser; |
4510 | 0 | } |
4511 | 2.74k | rv = tls_SignOrVerifyEnd(ctx, signature); |
4512 | 2.74k | if (rv) { |
4513 | 0 | goto loser; |
4514 | 0 | } |
4515 | | |
4516 | | /* if we are server & sending or !server & !sending, update the scheme */ |
4517 | | /* only update on server cert verify */ |
4518 | 2.74k | if ((direction == sig_sign && ss->sec.isServer) || |
4519 | 2.74k | (direction == sig_verify && !ss->sec.isServer)) { |
4520 | 2.74k | ss->sec.signatureScheme = scheme; |
4521 | 2.74k | ss->sec.authType = ssl_SignatureSchemeToAuthType(scheme); |
4522 | 2.74k | } |
4523 | 2.74k | return SECSuccess; |
4524 | | |
4525 | 0 | loser: |
4526 | 0 | tls_DestroySignOrVerifyContext(&ctx); |
4527 | 0 | ssl_MapLowLevelError(SSL_ERROR_SIGN_HASHES_FAILURE); |
4528 | 0 | return SECFailure; |
4529 | 2.74k | } |
4530 | | |
4531 | | /* |
4532 | | * Derive-Secret(Secret, Label, Messages) = |
4533 | | * HKDF-Expand-Label(Secret, Label, |
4534 | | * Hash(Messages) + Hash(resumption_context), L)) |
4535 | | */ |
4536 | | SECStatus |
4537 | | tls13_DeriveSecret(sslSocket *ss, PK11SymKey *key, |
4538 | | const char *label, |
4539 | | unsigned int labelLen, |
4540 | | const SSL3Hashes *hashes, |
4541 | | PK11SymKey **dest, |
4542 | | SSLHashType hash) |
4543 | 36.1k | { |
4544 | 36.1k | SECStatus rv; |
4545 | | |
4546 | 36.1k | rv = tls13_HkdfExpandLabel(key, hash, hashes->u.raw, hashes->len, |
4547 | 36.1k | label, labelLen, CKM_HKDF_DERIVE, |
4548 | 36.1k | tls13_GetHashSizeForHash(hash), |
4549 | 36.1k | ss->protocolVariant, dest); |
4550 | 36.1k | if (rv != SECSuccess) { |
4551 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
4552 | 0 | return SECFailure; |
4553 | 0 | } |
4554 | 36.1k | return SECSuccess; |
4555 | 36.1k | } |
4556 | | |
4557 | | /* Convenience wrapper for the empty hash. */ |
4558 | | SECStatus |
4559 | | tls13_DeriveSecretNullHash(sslSocket *ss, PK11SymKey *key, |
4560 | | const char *label, |
4561 | | unsigned int labelLen, |
4562 | | PK11SymKey **dest, |
4563 | | SSLHashType hash) |
4564 | 17.1k | { |
4565 | 17.1k | SSL3Hashes hashes; |
4566 | 17.1k | SECStatus rv; |
4567 | 17.1k | PRUint8 buf[] = { 0 }; |
4568 | | |
4569 | 17.1k | rv = tls13_ComputeHash(ss, &hashes, buf, 0, hash); |
4570 | 17.1k | if (rv != SECSuccess) { |
4571 | 0 | return SECFailure; |
4572 | 0 | } |
4573 | | |
4574 | 17.1k | return tls13_DeriveSecret(ss, key, label, labelLen, &hashes, dest, hash); |
4575 | 17.1k | } |
4576 | | |
4577 | | /* Convenience wrapper that lets us supply a separate prefix and suffix. */ |
4578 | | static SECStatus |
4579 | | tls13_DeriveSecretWrap(sslSocket *ss, PK11SymKey *key, |
4580 | | const char *prefix, |
4581 | | const char *suffix, |
4582 | | const char *keylogLabel, |
4583 | | PK11SymKey **dest) |
4584 | 19.0k | { |
4585 | 19.0k | SECStatus rv; |
4586 | 19.0k | SSL3Hashes hashes; |
4587 | 19.0k | char buf[100]; |
4588 | 19.0k | const char *label; |
4589 | | |
4590 | 19.0k | if (prefix) { |
4591 | 15.7k | if ((strlen(prefix) + strlen(suffix) + 2) > sizeof(buf)) { |
4592 | 0 | PORT_Assert(0); |
4593 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4594 | 0 | return SECFailure; |
4595 | 0 | } |
4596 | 15.7k | (void)PR_snprintf(buf, sizeof(buf), "%s %s", |
4597 | 15.7k | prefix, suffix); |
4598 | 15.7k | label = buf; |
4599 | 15.7k | } else { |
4600 | 3.30k | label = suffix; |
4601 | 3.30k | } |
4602 | | |
4603 | 19.0k | SSL_TRC(3, ("%d: TLS13[%d]: deriving secret '%s'", |
4604 | 19.0k | SSL_GETPID(), ss->fd, label)); |
4605 | 19.0k | rv = tls13_ComputeHandshakeHashes(ss, &hashes); |
4606 | 19.0k | if (rv != SECSuccess) { |
4607 | 0 | PORT_Assert(0); /* Should never fail */ |
4608 | 0 | ssl_MapLowLevelError(SEC_ERROR_LIBRARY_FAILURE); |
4609 | 0 | return SECFailure; |
4610 | 0 | } |
4611 | | |
4612 | 19.0k | rv = tls13_DeriveSecret(ss, key, label, strlen(label), |
4613 | 19.0k | &hashes, dest, tls13_GetHash(ss)); |
4614 | 19.0k | if (rv != SECSuccess) { |
4615 | 0 | return SECFailure; |
4616 | 0 | } |
4617 | | |
4618 | 19.0k | if (keylogLabel) { |
4619 | 18.4k | ssl3_RecordKeyLog(ss, keylogLabel, *dest); |
4620 | 18.4k | } |
4621 | 19.0k | return SECSuccess; |
4622 | 19.0k | } |
4623 | | |
4624 | | SECStatus |
4625 | | SSLExp_SecretCallback(PRFileDesc *fd, SSLSecretCallback cb, void *arg) |
4626 | 0 | { |
4627 | 0 | sslSocket *ss = ssl_FindSocket(fd); |
4628 | 0 | if (!ss) { |
4629 | 0 | SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SecretCallback", |
4630 | 0 | SSL_GETPID(), fd)); |
4631 | 0 | return SECFailure; |
4632 | 0 | } |
4633 | | |
4634 | 0 | ssl_Get1stHandshakeLock(ss); |
4635 | 0 | ssl_GetSSL3HandshakeLock(ss); |
4636 | 0 | ss->secretCallback = cb; |
4637 | 0 | ss->secretCallbackArg = arg; |
4638 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
4639 | 0 | ssl_Release1stHandshakeLock(ss); |
4640 | 0 | return SECSuccess; |
4641 | 0 | } |
4642 | | |
4643 | | /* Derive traffic keys for the next cipher spec in the queue. */ |
4644 | | static SECStatus |
4645 | | tls13_DeriveTrafficKeys(sslSocket *ss, ssl3CipherSpec *spec, |
4646 | | TrafficKeyType type, |
4647 | | PRBool deleteSecret) |
4648 | 133k | { |
4649 | 133k | size_t keySize = spec->cipherDef->key_size; |
4650 | 133k | size_t ivSize = spec->cipherDef->iv_size + |
4651 | 133k | spec->cipherDef->explicit_nonce_size; /* This isn't always going to |
4652 | | * work, but it does for |
4653 | | * AES-GCM */ |
4654 | 133k | CK_MECHANISM_TYPE bulkAlgorithm = ssl3_Alg2Mech(spec->cipherDef->calg); |
4655 | 133k | PK11SymKey **prkp = NULL; |
4656 | 133k | PK11SymKey *prk = NULL; |
4657 | 133k | PRBool clientSecret; |
4658 | 133k | SECStatus rv; |
4659 | | /* These labels are just used for debugging. */ |
4660 | 133k | static const char kHkdfPhaseEarlyApplicationDataKeys[] = "early application data"; |
4661 | 133k | static const char kHkdfPhaseHandshakeKeys[] = "handshake data"; |
4662 | 133k | static const char kHkdfPhaseApplicationDataKeys[] = "application data"; |
4663 | | |
4664 | 133k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4665 | | |
4666 | 133k | clientSecret = !tls13_UseServerSecret(ss, spec->direction); |
4667 | 133k | switch (type) { |
4668 | 0 | case TrafficKeyEarlyApplicationData: |
4669 | 0 | PORT_Assert(clientSecret); |
4670 | 0 | prkp = &ss->ssl3.hs.clientEarlyTrafficSecret; |
4671 | 0 | spec->phase = kHkdfPhaseEarlyApplicationDataKeys; |
4672 | 0 | break; |
4673 | 8.10k | case TrafficKeyHandshake: |
4674 | 8.10k | prkp = clientSecret ? &ss->ssl3.hs.clientHsTrafficSecret |
4675 | 8.10k | : &ss->ssl3.hs.serverHsTrafficSecret; |
4676 | 8.10k | spec->phase = kHkdfPhaseHandshakeKeys; |
4677 | 8.10k | break; |
4678 | 125k | case TrafficKeyApplicationData: |
4679 | 125k | prkp = clientSecret ? &ss->ssl3.hs.clientTrafficSecret |
4680 | 125k | : &ss->ssl3.hs.serverTrafficSecret; |
4681 | 125k | spec->phase = kHkdfPhaseApplicationDataKeys; |
4682 | 125k | break; |
4683 | 0 | default: |
4684 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
4685 | 0 | PORT_Assert(0); |
4686 | 0 | return SECFailure; |
4687 | 133k | } |
4688 | 133k | PORT_Assert(prkp != NULL); |
4689 | 133k | prk = *prkp; |
4690 | | |
4691 | 133k | SSL_TRC(3, ("%d: TLS13[%d]: deriving %s traffic keys epoch=%d (%s)", |
4692 | 133k | SSL_GETPID(), ss->fd, SPEC_DIR(spec), |
4693 | 133k | spec->epoch, spec->phase)); |
4694 | | |
4695 | 133k | rv = tls13_HkdfExpandLabel(prk, tls13_GetHash(ss), |
4696 | 133k | NULL, 0, |
4697 | 133k | kHkdfPurposeKey, strlen(kHkdfPurposeKey), |
4698 | 133k | bulkAlgorithm, keySize, |
4699 | 133k | ss->protocolVariant, |
4700 | 133k | &spec->keyMaterial.key); |
4701 | 133k | if (rv != SECSuccess) { |
4702 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
4703 | 0 | PORT_Assert(0); |
4704 | 0 | goto loser; |
4705 | 0 | } |
4706 | | |
4707 | 133k | if (IS_DTLS(ss) && spec->epoch > 0) { |
4708 | 5.45k | rv = ssl_CreateMaskingContextInner(spec->version, ss->ssl3.hs.cipher_suite, |
4709 | 5.45k | ss->protocolVariant, prk, kHkdfPurposeSn, |
4710 | 5.45k | strlen(kHkdfPurposeSn), &spec->maskContext); |
4711 | 5.45k | if (rv != SECSuccess) { |
4712 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
4713 | 0 | PORT_Assert(0); |
4714 | 0 | goto loser; |
4715 | 0 | } |
4716 | 5.45k | } |
4717 | | |
4718 | 133k | rv = tls13_HkdfExpandLabelRaw(prk, tls13_GetHash(ss), |
4719 | 133k | NULL, 0, |
4720 | 133k | kHkdfPurposeIv, strlen(kHkdfPurposeIv), |
4721 | 133k | ss->protocolVariant, |
4722 | 133k | spec->keyMaterial.iv, ivSize); |
4723 | 133k | if (rv != SECSuccess) { |
4724 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
4725 | 0 | PORT_Assert(0); |
4726 | 0 | goto loser; |
4727 | 0 | } |
4728 | | |
4729 | 133k | if (deleteSecret) { |
4730 | 0 | PK11_FreeSymKey(prk); |
4731 | 0 | *prkp = NULL; |
4732 | 0 | } |
4733 | 133k | return SECSuccess; |
4734 | | |
4735 | 0 | loser: |
4736 | 0 | return SECFailure; |
4737 | 133k | } |
4738 | | |
4739 | | void |
4740 | | tls13_SetSpecRecordVersion(sslSocket *ss, ssl3CipherSpec *spec) |
4741 | 144k | { |
4742 | | /* Set the record version to pretend to be (D)TLS 1.2. */ |
4743 | 144k | if (IS_DTLS(ss)) { |
4744 | 11.2k | spec->recordVersion = SSL_LIBRARY_VERSION_DTLS_1_2_WIRE; |
4745 | 133k | } else { |
4746 | 133k | spec->recordVersion = SSL_LIBRARY_VERSION_TLS_1_2; |
4747 | 133k | } |
4748 | 144k | SSL_TRC(10, ("%d: TLS13[%d]: set spec=%d record version to 0x%04x", |
4749 | 144k | SSL_GETPID(), ss->fd, spec, spec->recordVersion)); |
4750 | 144k | } |
4751 | | |
4752 | | static SECStatus |
4753 | | tls13_SetupPendingCipherSpec(sslSocket *ss, ssl3CipherSpec *spec) |
4754 | 133k | { |
4755 | 133k | ssl3CipherSuite suite = ss->ssl3.hs.cipher_suite; |
4756 | | |
4757 | 133k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4758 | 133k | PORT_Assert(spec->epoch); |
4759 | | |
4760 | | /* Version isn't set when we send 0-RTT data. */ |
4761 | 133k | spec->version = PR_MAX(SSL_LIBRARY_VERSION_TLS_1_3, ss->version); |
4762 | | |
4763 | 133k | ssl_SaveCipherSpec(ss, spec); |
4764 | | /* We want to keep read cipher specs around longer because |
4765 | | * there are cases where we might get either epoch N or |
4766 | | * epoch N+1. */ |
4767 | 133k | if (IS_DTLS(ss) && spec->direction == ssl_secret_read) { |
4768 | 2.73k | ssl_CipherSpecAddRef(spec); |
4769 | 2.73k | } |
4770 | | |
4771 | 133k | SSL_TRC(3, ("%d: TLS13[%d]: Set Pending Cipher Suite to 0x%04x", |
4772 | 133k | SSL_GETPID(), ss->fd, suite)); |
4773 | | |
4774 | 133k | spec->cipherDef = ssl_GetBulkCipherDef(ssl_LookupCipherSuiteDef(suite)); |
4775 | | |
4776 | 133k | if (spec->epoch == TrafficKeyEarlyApplicationData) { |
4777 | 0 | if (ss->xtnData.selectedPsk && |
4778 | 0 | ss->xtnData.selectedPsk->zeroRttSuite != TLS_NULL_WITH_NULL_NULL) { |
4779 | 0 | spec->earlyDataRemaining = ss->xtnData.selectedPsk->maxEarlyData; |
4780 | 0 | } |
4781 | 0 | } |
4782 | | |
4783 | 133k | tls13_SetSpecRecordVersion(ss, spec); |
4784 | | |
4785 | | /* The record size limit is reduced by one so that the remainder of the |
4786 | | * record handling code can use the same checks for all versions. */ |
4787 | 133k | if (ssl3_ExtensionNegotiated(ss, ssl_record_size_limit_xtn)) { |
4788 | 124k | spec->recordSizeLimit = ((spec->direction == ssl_secret_read) |
4789 | 124k | ? ss->opt.recordSizeLimit |
4790 | 124k | : ss->xtnData.recordSizeLimit) - |
4791 | 124k | 1; |
4792 | 124k | } else { |
4793 | 8.94k | spec->recordSizeLimit = MAX_FRAGMENT_LENGTH; |
4794 | 8.94k | } |
4795 | 133k | return SECSuccess; |
4796 | 133k | } |
4797 | | |
4798 | | /* |
4799 | | * Initialize the cipher context. All TLS 1.3 operations are AEAD, |
4800 | | * so they are all message contexts. |
4801 | | */ |
4802 | | static SECStatus |
4803 | | tls13_InitPendingContext(sslSocket *ss, ssl3CipherSpec *spec) |
4804 | 133k | { |
4805 | 133k | CK_MECHANISM_TYPE encMechanism; |
4806 | 133k | CK_ATTRIBUTE_TYPE encMode; |
4807 | 133k | SECItem iv; |
4808 | 133k | SSLCipherAlgorithm calg; |
4809 | | |
4810 | 133k | calg = spec->cipherDef->calg; |
4811 | | |
4812 | 133k | encMechanism = ssl3_Alg2Mech(calg); |
4813 | 133k | encMode = CKA_NSS_MESSAGE | ((spec->direction == ssl_secret_write) ? CKA_ENCRYPT : CKA_DECRYPT); |
4814 | 133k | iv.data = NULL; |
4815 | 133k | iv.len = 0; |
4816 | | |
4817 | | /* |
4818 | | * build the context |
4819 | | */ |
4820 | 133k | spec->cipherContext = PK11_CreateContextBySymKey(encMechanism, encMode, |
4821 | 133k | spec->keyMaterial.key, |
4822 | 133k | &iv); |
4823 | 133k | if (!spec->cipherContext) { |
4824 | 0 | ssl_MapLowLevelError(SSL_ERROR_SYM_KEY_CONTEXT_FAILURE); |
4825 | 0 | return SECFailure; |
4826 | 0 | } |
4827 | 133k | return SECSuccess; |
4828 | 133k | } |
4829 | | |
4830 | | /* |
4831 | | * Called before sending alerts to set up the right key on the client. |
4832 | | * We might encounter errors during the handshake where the current |
4833 | | * key is ClearText or EarlyApplicationData. This |
4834 | | * function switches to the Handshake key if possible. |
4835 | | */ |
4836 | | SECStatus |
4837 | | tls13_SetAlertCipherSpec(sslSocket *ss) |
4838 | 32.4k | { |
4839 | 32.4k | SECStatus rv; |
4840 | | |
4841 | 32.4k | if (ss->sec.isServer) { |
4842 | 17.4k | return SECSuccess; |
4843 | 17.4k | } |
4844 | 15.0k | if (ss->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
4845 | 14.1k | return SECSuccess; |
4846 | 14.1k | } |
4847 | 872 | if (TLS13_IN_HS_STATE(ss, wait_server_hello)) { |
4848 | 638 | return SECSuccess; |
4849 | 638 | } |
4850 | 234 | if ((ss->ssl3.cwSpec->epoch != TrafficKeyClearText) && |
4851 | 0 | (ss->ssl3.cwSpec->epoch != TrafficKeyEarlyApplicationData)) { |
4852 | 0 | return SECSuccess; |
4853 | 0 | } |
4854 | | |
4855 | 234 | rv = tls13_SetCipherSpec(ss, TrafficKeyHandshake, |
4856 | 234 | ssl_secret_write, PR_FALSE); |
4857 | 234 | if (rv != SECSuccess) { |
4858 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
4859 | 0 | return SECFailure; |
4860 | 0 | } |
4861 | 234 | return SECSuccess; |
4862 | 234 | } |
4863 | | |
4864 | | /* Install a new cipher spec for this direction. |
4865 | | * |
4866 | | * During the handshake, the values for |epoch| take values from the |
4867 | | * TrafficKeyType enum. Afterwards, key update increments them. |
4868 | | */ |
4869 | | static SECStatus |
4870 | | tls13_SetCipherSpec(sslSocket *ss, PRUint16 epoch, |
4871 | | SSLSecretDirection direction, PRBool deleteSecret) |
4872 | 133k | { |
4873 | 133k | TrafficKeyType type; |
4874 | 133k | SECStatus rv; |
4875 | 133k | ssl3CipherSpec *spec = NULL; |
4876 | 133k | ssl3CipherSpec **specp; |
4877 | | |
4878 | | /* Flush out old handshake data. */ |
4879 | 133k | ssl_GetXmitBufLock(ss); |
4880 | 133k | rv = ssl3_FlushHandshake(ss, ssl_SEND_FLAG_FORCE_INTO_BUFFER); |
4881 | 133k | ssl_ReleaseXmitBufLock(ss); |
4882 | 133k | if (rv != SECSuccess) { |
4883 | 0 | return SECFailure; |
4884 | 0 | } |
4885 | | |
4886 | | /* Create the new spec. */ |
4887 | 133k | spec = ssl_CreateCipherSpec(ss, direction); |
4888 | 133k | if (!spec) { |
4889 | 0 | return SECFailure; |
4890 | 0 | } |
4891 | 133k | spec->epoch = epoch; |
4892 | 133k | spec->nextSeqNum = 0; |
4893 | 133k | if (IS_DTLS(ss)) { |
4894 | 5.45k | dtls_InitRecvdRecords(&spec->recvdRecords); |
4895 | 5.45k | } |
4896 | | |
4897 | | /* This depends on spec having a valid direction and epoch. */ |
4898 | 133k | rv = tls13_SetupPendingCipherSpec(ss, spec); |
4899 | 133k | if (rv != SECSuccess) { |
4900 | 0 | goto loser; |
4901 | 0 | } |
4902 | | |
4903 | 133k | type = (TrafficKeyType)PR_MIN(TrafficKeyApplicationData, epoch); |
4904 | 133k | rv = tls13_DeriveTrafficKeys(ss, spec, type, deleteSecret); |
4905 | 133k | if (rv != SECSuccess) { |
4906 | 0 | goto loser; |
4907 | 0 | } |
4908 | | |
4909 | 133k | rv = tls13_InitPendingContext(ss, spec); |
4910 | 133k | if (rv != SECSuccess) { |
4911 | 0 | goto loser; |
4912 | 0 | } |
4913 | | |
4914 | | /* Now that we've set almost everything up, finally cut over. */ |
4915 | 133k | specp = (direction == ssl_secret_read) ? &ss->ssl3.crSpec : &ss->ssl3.cwSpec; |
4916 | 133k | ssl_GetSpecWriteLock(ss); |
4917 | 133k | ssl_CipherSpecRelease(*specp); /* May delete old cipher. */ |
4918 | 133k | *specp = spec; /* Overwrite. */ |
4919 | 133k | ssl_ReleaseSpecWriteLock(ss); |
4920 | | |
4921 | 133k | SSL_TRC(3, ("%d: TLS13[%d]: %s installed key for epoch=%d (%s) dir=%s", |
4922 | 133k | SSL_GETPID(), ss->fd, SSL_ROLE(ss), spec->epoch, |
4923 | 133k | spec->phase, SPEC_DIR(spec))); |
4924 | 133k | return SECSuccess; |
4925 | | |
4926 | 0 | loser: |
4927 | 0 | ssl_CipherSpecRelease(spec); |
4928 | 0 | return SECFailure; |
4929 | 133k | } |
4930 | | |
4931 | | SECStatus |
4932 | | tls13_ComputeHandshakeHashes(sslSocket *ss, SSL3Hashes *hashes) |
4933 | 26.0k | { |
4934 | 26.0k | SECStatus rv; |
4935 | 26.0k | PK11Context *ctx = NULL; |
4936 | 26.0k | PRBool useEchInner; |
4937 | 26.0k | sslBuffer *transcript; |
4938 | | |
4939 | 26.0k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
4940 | 26.0k | if (ss->ssl3.hs.hashType == handshake_hash_unknown) { |
4941 | | /* Backup: if we haven't done any hashing, then hash now. |
4942 | | * This happens when we are doing 0-RTT on the client. */ |
4943 | 978 | ctx = PK11_CreateDigestContext(ssl3_HashTypeToOID(tls13_GetHash(ss))); |
4944 | 978 | if (!ctx) { |
4945 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4946 | 0 | return SECFailure; |
4947 | 0 | } |
4948 | | |
4949 | 978 | if (PK11_DigestBegin(ctx) != SECSuccess) { |
4950 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4951 | 0 | goto loser; |
4952 | 0 | } |
4953 | | |
4954 | | /* One might expect this to use ss->ssl3.hs.echAccepted, |
4955 | | * but with 0-RTT we don't know that yet. */ |
4956 | 978 | useEchInner = ss->sec.isServer ? PR_FALSE : !!ss->ssl3.hs.echHpkeCtx; |
4957 | 978 | transcript = useEchInner ? &ss->ssl3.hs.echInnerMessages : &ss->ssl3.hs.messages; |
4958 | | |
4959 | 978 | PRINT_BUF(10, (ss, "Handshake hash computed over saved messages", |
4960 | 978 | transcript->buf, |
4961 | 978 | transcript->len)); |
4962 | | |
4963 | 978 | if (PK11_DigestOp(ctx, |
4964 | 978 | transcript->buf, |
4965 | 978 | transcript->len) != SECSuccess) { |
4966 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4967 | 0 | goto loser; |
4968 | 0 | } |
4969 | 25.0k | } else { |
4970 | 25.0k | if (ss->firstHsDone) { |
4971 | 0 | ctx = PK11_CloneContext(ss->ssl3.hs.shaPostHandshake); |
4972 | 25.0k | } else { |
4973 | 25.0k | ctx = PK11_CloneContext(ss->ssl3.hs.sha); |
4974 | 25.0k | } |
4975 | 25.0k | if (!ctx) { |
4976 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
4977 | 0 | return SECFailure; |
4978 | 0 | } |
4979 | 25.0k | } |
4980 | | |
4981 | 26.0k | rv = PK11_DigestFinal(ctx, hashes->u.raw, |
4982 | 26.0k | &hashes->len, |
4983 | 26.0k | sizeof(hashes->u.raw)); |
4984 | 26.0k | if (rv != SECSuccess) { |
4985 | 0 | ssl_MapLowLevelError(SSL_ERROR_DIGEST_FAILURE); |
4986 | 0 | goto loser; |
4987 | 0 | } |
4988 | | |
4989 | 26.0k | PRINT_BUF(10, (ss, "Handshake hash", hashes->u.raw, hashes->len)); |
4990 | 26.0k | PORT_Assert(hashes->len == tls13_GetHashSize(ss)); |
4991 | 26.0k | PK11_DestroyContext(ctx, PR_TRUE); |
4992 | | |
4993 | 26.0k | return SECSuccess; |
4994 | | |
4995 | 0 | loser: |
4996 | 0 | PK11_DestroyContext(ctx, PR_TRUE); |
4997 | 0 | return SECFailure; |
4998 | 26.0k | } |
4999 | | |
5000 | | TLS13KeyShareEntry * |
5001 | | tls13_CopyKeyShareEntry(TLS13KeyShareEntry *o) |
5002 | 0 | { |
5003 | 0 | TLS13KeyShareEntry *n; |
5004 | |
|
5005 | 0 | PORT_Assert(o); |
5006 | 0 | n = PORT_ZNew(TLS13KeyShareEntry); |
5007 | 0 | if (!n) { |
5008 | 0 | return NULL; |
5009 | 0 | } |
5010 | | |
5011 | 0 | if (SECSuccess != SECITEM_CopyItem(NULL, &n->key_exchange, &o->key_exchange)) { |
5012 | 0 | PORT_Free(n); |
5013 | 0 | return NULL; |
5014 | 0 | } |
5015 | 0 | n->group = o->group; |
5016 | 0 | return n; |
5017 | 0 | } |
5018 | | |
5019 | | void |
5020 | | tls13_DestroyKeyShareEntry(TLS13KeyShareEntry *offer) |
5021 | 7.11k | { |
5022 | 7.11k | if (!offer) { |
5023 | 197 | return; |
5024 | 197 | } |
5025 | 6.91k | SECITEM_ZfreeItem(&offer->key_exchange, PR_FALSE); |
5026 | 6.91k | PORT_ZFree(offer, sizeof(*offer)); |
5027 | 6.91k | } |
5028 | | |
5029 | | void |
5030 | | tls13_DestroyKeyShares(PRCList *list) |
5031 | 356k | { |
5032 | 356k | PRCList *cur_p; |
5033 | | |
5034 | | /* The list must be initialized. */ |
5035 | 356k | PORT_Assert(PR_LIST_HEAD(list)); |
5036 | | |
5037 | 363k | while (!PR_CLIST_IS_EMPTY(list)) { |
5038 | 6.89k | cur_p = PR_LIST_TAIL(list); |
5039 | 6.89k | PR_REMOVE_LINK(cur_p); |
5040 | 6.89k | tls13_DestroyKeyShareEntry((TLS13KeyShareEntry *)cur_p); |
5041 | 6.89k | } |
5042 | 356k | } |
5043 | | |
5044 | | void |
5045 | | tls13_DestroyEarlyData(PRCList *list) |
5046 | 64.0k | { |
5047 | 64.0k | PRCList *cur_p; |
5048 | | |
5049 | 64.0k | while (!PR_CLIST_IS_EMPTY(list)) { |
5050 | 0 | TLS13EarlyData *msg; |
5051 | |
|
5052 | 0 | cur_p = PR_LIST_TAIL(list); |
5053 | 0 | msg = (TLS13EarlyData *)cur_p; |
5054 | |
|
5055 | 0 | PR_REMOVE_LINK(cur_p); |
5056 | 0 | SECITEM_ZfreeItem(&msg->data, PR_FALSE); |
5057 | 0 | PORT_ZFree(msg, sizeof(*msg)); |
5058 | 0 | } |
5059 | 64.0k | } |
5060 | | |
5061 | | /* draft-ietf-tls-tls13 Section 5.2.2 specifies the following |
5062 | | * nonce algorithm: |
5063 | | * |
5064 | | * The length of the per-record nonce (iv_length) is set to max(8 bytes, |
5065 | | * N_MIN) for the AEAD algorithm (see [RFC5116] Section 4). An AEAD |
5066 | | * algorithm where N_MAX is less than 8 bytes MUST NOT be used with TLS. |
5067 | | * The per-record nonce for the AEAD construction is formed as follows: |
5068 | | * |
5069 | | * 1. The 64-bit record sequence number is padded to the left with |
5070 | | * zeroes to iv_length. |
5071 | | * |
5072 | | * 2. The padded sequence number is XORed with the static |
5073 | | * client_write_iv or server_write_iv, depending on the role. |
5074 | | * |
5075 | | * The resulting quantity (of length iv_length) is used as the per- |
5076 | | * record nonce. |
5077 | | * |
5078 | | * Existing suites have the same nonce size: N_MIN = N_MAX = 12 bytes |
5079 | | * |
5080 | | * See RFC 5288 and https://tools.ietf.org/html/draft-ietf-tls-chacha20-poly1305-04#section-2 |
5081 | | */ |
5082 | | static void |
5083 | | tls13_WriteNonce(const unsigned char *ivIn, unsigned int ivInLen, |
5084 | | const unsigned char *nonce, unsigned int nonceLen, |
5085 | | unsigned char *ivOut, unsigned int ivOutLen) |
5086 | 15.7k | { |
5087 | 15.7k | size_t i; |
5088 | 15.7k | unsigned int offset = ivOutLen - nonceLen; |
5089 | | |
5090 | 15.7k | PORT_Assert(ivInLen <= ivOutLen); |
5091 | 15.7k | PORT_Assert(nonceLen <= ivOutLen); |
5092 | 15.7k | PORT_Memset(ivOut, 0, ivOutLen); |
5093 | 15.7k | PORT_Memcpy(ivOut, ivIn, ivInLen); |
5094 | | |
5095 | | /* XOR the last n bytes of the IV with the nonce (should be a counter). */ |
5096 | 141k | for (i = 0; i < nonceLen; ++i) { |
5097 | 126k | ivOut[offset + i] ^= nonce[i]; |
5098 | 126k | } |
5099 | 15.7k | PRINT_BUF(50, (NULL, "Nonce", ivOut, ivOutLen)); |
5100 | 15.7k | } |
5101 | | |
5102 | | /* Setup the IV for AEAD encrypt. The PKCS #11 module will add the |
5103 | | * counter, but it doesn't know about the DTLS epic, so we add it here. |
5104 | | */ |
5105 | | unsigned int |
5106 | | tls13_SetupAeadIv(PRBool isDTLS, SSL3ProtocolVersion v, unsigned char *ivOut, unsigned char *ivIn, |
5107 | | unsigned int offset, unsigned int ivLen, DTLSEpoch epoch) |
5108 | 26.3k | { |
5109 | 26.3k | PORT_Memcpy(ivOut, ivIn, ivLen); |
5110 | 26.3k | if (isDTLS && v < SSL_LIBRARY_VERSION_TLS_1_3) { |
5111 | | /* handle the tls 1.2 counter mode case, the epoc is copied |
5112 | | * instead of xored. We accomplish this by clearing ivOut |
5113 | | * before running xor. */ |
5114 | 58 | if (offset >= ivLen) { |
5115 | 58 | ivOut[offset] = ivOut[offset + 1] = 0; |
5116 | 58 | } |
5117 | 58 | ivOut[offset] ^= (unsigned char)(epoch >> BPB) & 0xff; |
5118 | 58 | ivOut[offset + 1] ^= (unsigned char)(epoch)&0xff; |
5119 | 58 | offset += 2; |
5120 | 58 | } |
5121 | | |
5122 | 26.3k | return offset; |
5123 | 26.3k | } |
5124 | | |
5125 | | /* |
5126 | | * Do a single AEAD for TLS. This differs from PK11_AEADOp in the following |
5127 | | * ways. |
5128 | | * 1) If context is not supplied, it treats the operation as a single shot |
5129 | | * and creates a context from symKey and mech. |
5130 | | * 2) It always assumes the tag will be at the end of the buffer |
5131 | | * (in on decrypt, out on encrypt) just like the old single shot. |
5132 | | * 3) If we aren't generating an IV, it uses tls13_WriteNonce to create the |
5133 | | * nonce. |
5134 | | * NOTE is context is supplied, symKey and mech are ignored |
5135 | | */ |
5136 | | SECStatus |
5137 | | tls13_AEAD(PK11Context *context, PRBool decrypt, |
5138 | | CK_GENERATOR_FUNCTION ivGen, unsigned int fixedbits, |
5139 | | const unsigned char *ivIn, unsigned char *ivOut, unsigned int ivLen, |
5140 | | const unsigned char *nonceIn, unsigned int nonceLen, |
5141 | | const unsigned char *aad, unsigned int aadLen, |
5142 | | unsigned char *out, unsigned int *outLen, unsigned int maxout, |
5143 | | unsigned int tagLen, const unsigned char *in, unsigned int inLen) |
5144 | 42.1k | { |
5145 | 42.1k | unsigned char *tag; |
5146 | 42.1k | unsigned char iv[MAX_IV_LENGTH]; |
5147 | 42.1k | unsigned char tagbuf[HASH_LENGTH_MAX]; |
5148 | 42.1k | SECStatus rv; |
5149 | | |
5150 | | /* must have either context or the symKey set */ |
5151 | 42.1k | if (!context) { |
5152 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
5153 | 0 | return SECFailure; |
5154 | 0 | } |
5155 | | |
5156 | 42.1k | PORT_Assert(ivLen <= MAX_IV_LENGTH); |
5157 | 42.1k | PORT_Assert(tagLen <= HASH_LENGTH_MAX); |
5158 | 42.1k | if (!ivOut) { |
5159 | 15.7k | ivOut = iv; /* caller doesn't need a returned, iv */ |
5160 | 15.7k | } |
5161 | | |
5162 | 42.1k | if (ivGen == CKG_NO_GENERATE) { |
5163 | 15.7k | tls13_WriteNonce(ivIn, ivLen, nonceIn, nonceLen, ivOut, ivLen); |
5164 | 26.3k | } else if (ivIn != ivOut) { |
5165 | 0 | PORT_Memcpy(ivOut, ivIn, ivLen); |
5166 | 0 | } |
5167 | 42.1k | if (decrypt) { |
5168 | 15.7k | if (inLen < tagLen) { |
5169 | 0 | PORT_SetError(SEC_ERROR_INPUT_LEN); |
5170 | 0 | return SECFailure; |
5171 | 0 | } |
5172 | 15.7k | inLen = inLen - tagLen; |
5173 | 15.7k | tag = (unsigned char *)in + inLen; |
5174 | | /* tag is const on decrypt, but returned on encrypt */ |
5175 | 26.3k | } else { |
5176 | | /* tag is written to a separate buffer, then added to the end |
5177 | | * of the actual output buffer. This allows output buffer to be larger |
5178 | | * than the input buffer and everything still work */ |
5179 | 26.3k | tag = tagbuf; |
5180 | 26.3k | } |
5181 | 42.1k | rv = PK11_AEADOp(context, ivGen, fixedbits, ivOut, ivLen, aad, aadLen, |
5182 | 42.1k | out, (int *)outLen, maxout, tag, tagLen, in, inLen); |
5183 | | /* on encrypt SSL always puts the tag at the end of the buffer */ |
5184 | 42.1k | if ((rv == SECSuccess) && !(decrypt)) { |
5185 | 26.3k | unsigned int len = *outLen; |
5186 | | /* make sure there is still space */ |
5187 | 26.3k | if (len + tagLen > maxout) { |
5188 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
5189 | 0 | return SECFailure; |
5190 | 0 | } |
5191 | 26.3k | PORT_Memcpy(out + len, tag, tagLen); |
5192 | 26.3k | *outLen += tagLen; |
5193 | 26.3k | } |
5194 | 42.1k | return rv; |
5195 | 42.1k | } |
5196 | | |
5197 | | static SECStatus |
5198 | | tls13_HandleEncryptedExtensions(sslSocket *ss, PRUint8 *b, PRUint32 length) |
5199 | 12 | { |
5200 | 12 | SECStatus rv; |
5201 | 12 | PRUint32 innerLength; |
5202 | 12 | SECItem oldAlpn = { siBuffer, NULL, 0 }; |
5203 | | |
5204 | 12 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
5205 | 12 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5206 | | |
5207 | 12 | SSL_TRC(3, ("%d: TLS13[%d]: handle encrypted extensions", |
5208 | 12 | SSL_GETPID(), ss->fd)); |
5209 | | |
5210 | 12 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_ENCRYPTED_EXTENSIONS, |
5211 | 12 | wait_encrypted_extensions); |
5212 | 12 | if (rv != SECSuccess) { |
5213 | 12 | return SECFailure; |
5214 | 12 | } |
5215 | | |
5216 | 0 | rv = ssl3_ConsumeHandshakeNumber(ss, &innerLength, 2, &b, &length); |
5217 | 0 | if (rv != SECSuccess) { |
5218 | 0 | return SECFailure; /* Alert already sent. */ |
5219 | 0 | } |
5220 | 0 | if (innerLength != length) { |
5221 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_ENCRYPTED_EXTENSIONS, |
5222 | 0 | illegal_parameter); |
5223 | 0 | return SECFailure; |
5224 | 0 | } |
5225 | | |
5226 | | /* If we are doing 0-RTT, then we already have an ALPN value. Stash |
5227 | | * it for comparison. */ |
5228 | 0 | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent && |
5229 | 0 | ss->xtnData.nextProtoState == SSL_NEXT_PROTO_EARLY_VALUE) { |
5230 | 0 | oldAlpn = ss->xtnData.nextProto; |
5231 | 0 | ss->xtnData.nextProto.data = NULL; |
5232 | 0 | ss->xtnData.nextProtoState = SSL_NEXT_PROTO_NO_SUPPORT; |
5233 | 0 | } |
5234 | |
|
5235 | 0 | rv = ssl3_ParseExtensions(ss, &b, &length); |
5236 | 0 | if (rv != SECSuccess) { |
5237 | 0 | return SECFailure; /* Error code set below */ |
5238 | 0 | } |
5239 | | |
5240 | | /* Handle the rest of the extensions. */ |
5241 | 0 | rv = ssl3_HandleParsedExtensions(ss, ssl_hs_encrypted_extensions); |
5242 | 0 | if (rv != SECSuccess) { |
5243 | 0 | return SECFailure; /* Error code set below */ |
5244 | 0 | } |
5245 | | |
5246 | | /* We can only get here if we offered 0-RTT. */ |
5247 | 0 | if (ssl3_ExtensionNegotiated(ss, ssl_tls13_early_data_xtn)) { |
5248 | 0 | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_sent); |
5249 | 0 | if (!ss->xtnData.selectedPsk) { |
5250 | | /* Illegal to accept 0-RTT without also accepting PSK. */ |
5251 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_ENCRYPTED_EXTENSIONS, |
5252 | 0 | illegal_parameter); |
5253 | 0 | return SECFailure; |
5254 | 0 | } |
5255 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_accepted; |
5256 | | |
5257 | | /* Check that the server negotiated the same ALPN (if any). */ |
5258 | 0 | if (SECITEM_CompareItem(&oldAlpn, &ss->xtnData.nextProto)) { |
5259 | 0 | SECITEM_FreeItem(&oldAlpn, PR_FALSE); |
5260 | 0 | FATAL_ERROR(ss, SSL_ERROR_NEXT_PROTOCOL_DATA_INVALID, |
5261 | 0 | illegal_parameter); |
5262 | 0 | return SECFailure; |
5263 | 0 | } |
5264 | | /* Check that the server negotiated the same cipher suite. */ |
5265 | 0 | if (ss->ssl3.hs.cipher_suite != ss->ssl3.hs.zeroRttSuite) { |
5266 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_ENCRYPTED_EXTENSIONS, |
5267 | 0 | illegal_parameter); |
5268 | 0 | return SECFailure; |
5269 | 0 | } |
5270 | 0 | } else if (ss->ssl3.hs.zeroRttState == ssl_0rtt_sent) { |
5271 | | /* Though we sent 0-RTT, the early_data extension wasn't present so the |
5272 | | * state is unmodified; the server must have rejected 0-RTT. */ |
5273 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_ignored; |
5274 | 0 | ss->ssl3.hs.zeroRttIgnore = ssl_0rtt_ignore_trial; |
5275 | 0 | } else { |
5276 | 0 | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_none || |
5277 | 0 | (ss->ssl3.hs.helloRetry && |
5278 | 0 | ss->ssl3.hs.zeroRttState == ssl_0rtt_ignored)); |
5279 | 0 | } |
5280 | | |
5281 | 0 | SECITEM_FreeItem(&oldAlpn, PR_FALSE); |
5282 | 0 | if (ss->ssl3.hs.kea_def->authKeyType == ssl_auth_psk) { |
5283 | 0 | TLS13_SET_HS_STATE(ss, wait_finished); |
5284 | 0 | } else { |
5285 | 0 | TLS13_SET_HS_STATE(ss, wait_cert_request); |
5286 | 0 | } |
5287 | | |
5288 | | /* Client is done with any PSKs */ |
5289 | 0 | tls13_DestroyPskList(&ss->ssl3.hs.psks); |
5290 | 0 | ss->xtnData.selectedPsk = NULL; |
5291 | |
|
5292 | 0 | return SECSuccess; |
5293 | 0 | } |
5294 | | |
5295 | | static SECStatus |
5296 | | tls13_SendEncryptedExtensions(sslSocket *ss) |
5297 | 2.74k | { |
5298 | 2.74k | sslBuffer extensions = SSL_BUFFER_EMPTY; |
5299 | 2.74k | SECStatus rv; |
5300 | | |
5301 | 2.74k | SSL_TRC(3, ("%d: TLS13[%d]: send encrypted extensions handshake", |
5302 | 2.74k | SSL_GETPID(), ss->fd)); |
5303 | | |
5304 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5305 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
5306 | | |
5307 | 2.74k | rv = ssl_ConstructExtensions(ss, &extensions, ssl_hs_encrypted_extensions); |
5308 | 2.74k | if (rv != SECSuccess) { |
5309 | 0 | return SECFailure; |
5310 | 0 | } |
5311 | | |
5312 | 2.74k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_encrypted_extensions, |
5313 | 2.74k | SSL_BUFFER_LEN(&extensions) + 2); |
5314 | 2.74k | if (rv != SECSuccess) { |
5315 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
5316 | 0 | goto loser; |
5317 | 0 | } |
5318 | 2.74k | rv = ssl3_AppendBufferToHandshakeVariable(ss, &extensions, 2); |
5319 | 2.74k | if (rv != SECSuccess) { |
5320 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
5321 | 0 | goto loser; |
5322 | 0 | } |
5323 | 2.74k | sslBuffer_Clear(&extensions); |
5324 | 2.74k | return SECSuccess; |
5325 | | |
5326 | 0 | loser: |
5327 | 0 | sslBuffer_Clear(&extensions); |
5328 | 0 | return SECFailure; |
5329 | 2.74k | } |
5330 | | |
5331 | | SECStatus |
5332 | | tls13_SendCertificateVerify(sslSocket *ss, SECKEYPrivateKey *privKey) |
5333 | 2.74k | { |
5334 | 2.74k | SECStatus rv = SECFailure; |
5335 | 2.74k | SECItem buf = { siBuffer, NULL, 0 }; |
5336 | 2.74k | unsigned int len; |
5337 | 2.74k | SSL3Hashes hash; |
5338 | | |
5339 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
5340 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5341 | | |
5342 | 2.74k | SSL_TRC(3, ("%d: TLS13[%d]: send certificate_verify handshake", |
5343 | 2.74k | SSL_GETPID(), ss->fd)); |
5344 | | |
5345 | 2.74k | PORT_Assert(ss->ssl3.hs.hashType == handshake_hash_single); |
5346 | 2.74k | rv = tls13_ComputeHandshakeHashes(ss, &hash); |
5347 | 2.74k | if (rv != SECSuccess) { |
5348 | 0 | return SECFailure; |
5349 | 0 | } |
5350 | | |
5351 | | /* We should have picked a signature scheme when we received a |
5352 | | * CertificateRequest, or when we picked a server certificate. */ |
5353 | 2.74k | PORT_Assert(ss->ssl3.hs.signatureScheme != ssl_sig_none); |
5354 | 2.74k | if (ss->ssl3.hs.signatureScheme == ssl_sig_none) { |
5355 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5356 | 0 | return SECFailure; |
5357 | 0 | } |
5358 | | |
5359 | 2.74k | rv = tls13_SignOrVerifyHashWithContext(ss, &hash, privKey, NULL, |
5360 | 2.74k | ss->ssl3.hs.signatureScheme, |
5361 | 2.74k | sig_sign, &buf); |
5362 | 2.74k | if (rv == SECSuccess && !ss->sec.isServer) { |
5363 | | /* Remember the info about the slot that did the signing. |
5364 | | * Later, when doing an SSL restart handshake, verify this. |
5365 | | * These calls are mere accessors, and can't fail. |
5366 | | */ |
5367 | 0 | PK11SlotInfo *slot; |
5368 | 0 | sslSessionID *sid = ss->sec.ci.sid; |
5369 | |
|
5370 | 0 | slot = PK11_GetSlotFromPrivateKey(privKey); |
5371 | 0 | sid->u.ssl3.clAuthSeries = PK11_GetSlotSeries(slot); |
5372 | 0 | sid->u.ssl3.clAuthSlotID = PK11_GetSlotID(slot); |
5373 | 0 | sid->u.ssl3.clAuthModuleID = PK11_GetModuleID(slot); |
5374 | 0 | sid->u.ssl3.clAuthValid = PR_TRUE; |
5375 | 0 | PK11_FreeSlot(slot); |
5376 | 0 | } |
5377 | 2.74k | if (rv != SECSuccess) { |
5378 | 0 | goto done; /* err code was set by tls13_SignOrVerifyHashWithContext */ |
5379 | 0 | } |
5380 | | |
5381 | 2.74k | len = buf.len + 2 + 2; |
5382 | | |
5383 | 2.74k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_certificate_verify, len); |
5384 | 2.74k | if (rv != SECSuccess) { |
5385 | 0 | goto done; /* error code set by AppendHandshake */ |
5386 | 0 | } |
5387 | | |
5388 | 2.74k | rv = ssl3_AppendHandshakeNumber(ss, ss->ssl3.hs.signatureScheme, 2); |
5389 | 2.74k | if (rv != SECSuccess) { |
5390 | 0 | goto done; /* err set by AppendHandshakeNumber */ |
5391 | 0 | } |
5392 | | |
5393 | 2.74k | rv = ssl3_AppendHandshakeVariable(ss, buf.data, buf.len, 2); |
5394 | 2.74k | if (rv != SECSuccess) { |
5395 | 0 | goto done; /* error code set by AppendHandshake */ |
5396 | 0 | } |
5397 | | |
5398 | 2.74k | done: |
5399 | | /* For parity with the allocation functions, which don't use |
5400 | | * SECITEM_AllocItem(). */ |
5401 | 2.74k | if (buf.data) |
5402 | 2.74k | PORT_Free(buf.data); |
5403 | 2.74k | return rv; |
5404 | 2.74k | } |
5405 | | |
5406 | | /* Called from tls13_CompleteHandleHandshakeMessage() when it has deciphered a complete |
5407 | | * tls13 CertificateVerify message |
5408 | | * Caller must hold Handshake and RecvBuf locks. |
5409 | | */ |
5410 | | SECStatus |
5411 | | tls13_HandleCertificateVerify(sslSocket *ss, PRUint8 *b, PRUint32 length) |
5412 | 12 | { |
5413 | 12 | sslDelegatedCredential *dc = ss->xtnData.peerDelegCred; |
5414 | 12 | CERTSubjectPublicKeyInfo *spki; |
5415 | 12 | SECKEYPublicKey *pubKey = NULL; |
5416 | 12 | SECItem signed_hash = { siBuffer, NULL, 0 }; |
5417 | 12 | SECStatus rv; |
5418 | 12 | SSLSignatureScheme sigScheme; |
5419 | 12 | SSL3Hashes hashes; |
5420 | | |
5421 | 12 | SSL_TRC(3, ("%d: TLS13[%d]: handle certificate_verify handshake", |
5422 | 12 | SSL_GETPID(), ss->fd)); |
5423 | 12 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
5424 | 12 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5425 | | |
5426 | 12 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_CERT_VERIFY, |
5427 | 12 | wait_cert_verify); |
5428 | 12 | if (rv != SECSuccess) { |
5429 | 12 | return SECFailure; |
5430 | 12 | } |
5431 | | |
5432 | 0 | rv = tls13_ComputeHandshakeHashes(ss, &hashes); |
5433 | 0 | if (rv != SECSuccess) { |
5434 | 0 | return SECFailure; |
5435 | 0 | } |
5436 | | |
5437 | 0 | if (ss->firstHsDone) { |
5438 | 0 | rv = ssl_HashPostHandshakeMessage(ss, ssl_hs_certificate_verify, b, length); |
5439 | 0 | } else { |
5440 | 0 | rv = ssl_HashHandshakeMessage(ss, ssl_hs_certificate_verify, b, length); |
5441 | 0 | } |
5442 | 0 | if (rv != SECSuccess) { |
5443 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5444 | 0 | return SECFailure; |
5445 | 0 | } |
5446 | | |
5447 | 0 | rv = ssl_ConsumeSignatureScheme(ss, &b, &length, &sigScheme); |
5448 | 0 | if (rv != SECSuccess) { |
5449 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERT_VERIFY, illegal_parameter); |
5450 | 0 | return SECFailure; |
5451 | 0 | } |
5452 | | |
5453 | | /* Set the |spki| used to verify the handshake. When verifying with a |
5454 | | * delegated credential (DC), this corresponds to the DC public key; |
5455 | | * otherwise it correspond to the public key of the peer's end-entity |
5456 | | * certificate. |
5457 | | */ |
5458 | 0 | if (tls13_IsVerifyingWithDelegatedCredential(ss)) { |
5459 | | /* DelegatedCredential.cred.expected_cert_verify_algorithm is expected |
5460 | | * to match CertificateVerify.scheme. |
5461 | | * DelegatedCredential.cred.expected_cert_verify_algorithm must also be |
5462 | | * the same as was reported in ssl3_AuthCertificate. |
5463 | | */ |
5464 | 0 | if (sigScheme != dc->expectedCertVerifyAlg || sigScheme != ss->sec.signatureScheme) { |
5465 | 0 | FATAL_ERROR(ss, SSL_ERROR_DC_CERT_VERIFY_ALG_MISMATCH, illegal_parameter); |
5466 | 0 | return SECFailure; |
5467 | 0 | } |
5468 | | |
5469 | | /* Verify the DC has three steps: (1) use the peer's end-entity |
5470 | | * certificate to verify DelegatedCredential.signature, (2) check that |
5471 | | * the certificate has the correct key usage, and (3) check that the DC |
5472 | | * hasn't expired. |
5473 | | */ |
5474 | 0 | rv = tls13_VerifyDelegatedCredential(ss, dc); |
5475 | 0 | if (rv != SECSuccess) { /* Calls FATAL_ERROR() */ |
5476 | 0 | return SECFailure; |
5477 | 0 | } |
5478 | | |
5479 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: Verifying with delegated credential", |
5480 | 0 | SSL_GETPID(), ss->fd)); |
5481 | 0 | spki = dc->spki; |
5482 | 0 | } else { |
5483 | 0 | spki = &ss->sec.peerCert->subjectPublicKeyInfo; |
5484 | 0 | } |
5485 | | |
5486 | 0 | rv = ssl_CheckSignatureSchemeConsistency(ss, sigScheme, spki); |
5487 | 0 | if (rv != SECSuccess) { |
5488 | | /* Error set already */ |
5489 | 0 | FATAL_ERROR(ss, PORT_GetError(), illegal_parameter); |
5490 | 0 | return SECFailure; |
5491 | 0 | } |
5492 | | |
5493 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &signed_hash, 2, &b, &length); |
5494 | 0 | if (rv != SECSuccess) { |
5495 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_CERT_VERIFY); |
5496 | 0 | return SECFailure; |
5497 | 0 | } |
5498 | | |
5499 | 0 | if (length != 0) { |
5500 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CERT_VERIFY, decode_error); |
5501 | 0 | return SECFailure; |
5502 | 0 | } |
5503 | | |
5504 | 0 | pubKey = SECKEY_ExtractPublicKey(spki); |
5505 | 0 | if (pubKey == NULL) { |
5506 | 0 | ssl_MapLowLevelError(SSL_ERROR_EXTRACT_PUBLIC_KEY_FAILURE); |
5507 | 0 | return SECFailure; |
5508 | 0 | } |
5509 | | |
5510 | 0 | rv = tls13_SignOrVerifyHashWithContext(ss, &hashes, NULL, pubKey, |
5511 | 0 | sigScheme, sig_verify, &signed_hash); |
5512 | 0 | if (rv != SECSuccess) { |
5513 | 0 | FATAL_ERROR(ss, PORT_GetError(), decrypt_error); |
5514 | 0 | goto loser; |
5515 | 0 | } |
5516 | | |
5517 | | /* Set the auth type and verify it is what we captured in ssl3_AuthCertificate */ |
5518 | 0 | if (!ss->sec.isServer) { |
5519 | 0 | ss->sec.authType = ssl_SignatureSchemeToAuthType(sigScheme); |
5520 | |
|
5521 | 0 | uint32_t prelimAuthKeyBits = ss->sec.authKeyBits; |
5522 | 0 | rv = ssl_SetAuthKeyBits(ss, pubKey); |
5523 | 0 | if (rv != SECSuccess) { |
5524 | 0 | goto loser; /* Alert sent and code set. */ |
5525 | 0 | } |
5526 | | |
5527 | 0 | if (prelimAuthKeyBits != ss->sec.authKeyBits) { |
5528 | 0 | FATAL_ERROR(ss, SSL_ERROR_DC_CERT_VERIFY_ALG_MISMATCH, illegal_parameter); |
5529 | 0 | goto loser; |
5530 | 0 | } |
5531 | 0 | } |
5532 | | |
5533 | | /* Request a client certificate now if one was requested. */ |
5534 | 0 | if (ss->ssl3.hs.clientCertRequested) { |
5535 | 0 | PORT_Assert(!ss->sec.isServer); |
5536 | 0 | rv = ssl3_BeginHandleCertificateRequest( |
5537 | 0 | ss, ss->xtnData.sigSchemes, ss->xtnData.numSigSchemes, |
5538 | 0 | &ss->xtnData.certReqAuthorities); |
5539 | 0 | if (rv != SECSuccess) { |
5540 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
5541 | 0 | goto loser; |
5542 | 0 | } |
5543 | 0 | } |
5544 | | |
5545 | 0 | SECKEY_DestroyPublicKey(pubKey); |
5546 | 0 | TLS13_SET_HS_STATE(ss, wait_finished); |
5547 | 0 | return SECSuccess; |
5548 | | |
5549 | 0 | loser: |
5550 | 0 | SECKEY_DestroyPublicKey(pubKey); |
5551 | 0 | return SECFailure; |
5552 | 0 | } |
5553 | | |
5554 | | /* Compute the PSK binder hash over: |
5555 | | * Client HRR prefix, if present in ss->ssl3.hs.messages or ss->ssl3.hs.echInnerMessages, |
5556 | | * |len| bytes of |buf| */ |
5557 | | static SECStatus |
5558 | | tls13_ComputePskBinderHash(sslSocket *ss, PRUint8 *b, size_t length, |
5559 | | SSL3Hashes *hashes, SSLHashType hashType) |
5560 | 8.10k | { |
5561 | 8.10k | SECStatus rv; |
5562 | 8.10k | PK11Context *ctx = NULL; |
5563 | 8.10k | sslBuffer *clientResidual = NULL; |
5564 | 8.10k | if (!ss->sec.isServer) { |
5565 | | /* On the server, HRR residual is already buffered. */ |
5566 | 8.10k | clientResidual = ss->ssl3.hs.echHpkeCtx ? &ss->ssl3.hs.echInnerMessages : &ss->ssl3.hs.messages; |
5567 | 8.10k | } |
5568 | 8.10k | PORT_Assert(ss->ssl3.hs.hashType == handshake_hash_unknown); |
5569 | 8.10k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5570 | | |
5571 | 8.10k | PRINT_BUF(10, (NULL, "Binder computed over ClientHello", |
5572 | 8.10k | b, length)); |
5573 | | |
5574 | 8.10k | ctx = PK11_CreateDigestContext(ssl3_HashTypeToOID(hashType)); |
5575 | 8.10k | if (!ctx) { |
5576 | 0 | goto loser; |
5577 | 0 | } |
5578 | 8.10k | rv = PK11_DigestBegin(ctx); |
5579 | 8.10k | if (rv != SECSuccess) { |
5580 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5581 | 0 | goto loser; |
5582 | 0 | } |
5583 | | |
5584 | 8.10k | if (clientResidual && clientResidual->len) { |
5585 | 909 | PRINT_BUF(10, (NULL, " with HRR prefix", clientResidual->buf, |
5586 | 909 | clientResidual->len)); |
5587 | 909 | rv = PK11_DigestOp(ctx, clientResidual->buf, clientResidual->len); |
5588 | 909 | if (rv != SECSuccess) { |
5589 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5590 | 0 | goto loser; |
5591 | 0 | } |
5592 | 909 | } |
5593 | | |
5594 | 8.10k | if (IS_DTLS(ss) && !ss->sec.isServer) { |
5595 | | /* Removing the unnecessary header fields. |
5596 | | * See ssl3_AppendHandshakeHeader.*/ |
5597 | 4.30k | PORT_Assert(length >= 12); |
5598 | 4.30k | rv = PK11_DigestOp(ctx, b, 4); |
5599 | 4.30k | if (rv != SECSuccess) { |
5600 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5601 | 0 | goto loser; |
5602 | 0 | } |
5603 | 4.30k | rv = PK11_DigestOp(ctx, b + 12, length - 12); |
5604 | 4.30k | } else { |
5605 | 3.79k | rv = PK11_DigestOp(ctx, b, length); |
5606 | 3.79k | } |
5607 | 8.10k | if (rv != SECSuccess) { |
5608 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5609 | 0 | goto loser; |
5610 | 0 | } |
5611 | 8.10k | rv = PK11_DigestFinal(ctx, hashes->u.raw, &hashes->len, sizeof(hashes->u.raw)); |
5612 | 8.10k | if (rv != SECSuccess) { |
5613 | 0 | ssl_MapLowLevelError(SSL_ERROR_SHA_DIGEST_FAILURE); |
5614 | 0 | goto loser; |
5615 | 0 | } |
5616 | | |
5617 | 8.10k | PK11_DestroyContext(ctx, PR_TRUE); |
5618 | 8.10k | PRINT_BUF(10, (NULL, "PSK Binder hash", hashes->u.raw, hashes->len)); |
5619 | 8.10k | return SECSuccess; |
5620 | | |
5621 | 0 | loser: |
5622 | 0 | if (ctx) { |
5623 | 0 | PK11_DestroyContext(ctx, PR_TRUE); |
5624 | 0 | } |
5625 | 0 | return SECFailure; |
5626 | 8.10k | } |
5627 | | |
5628 | | /* Compute and inject the PSK Binder for sending. |
5629 | | * |
5630 | | * When sending a ClientHello, we construct all the extensions with a dummy |
5631 | | * value for the binder. To construct the binder, we commit the entire message |
5632 | | * up to the point where the binders start. Then we calculate the hash using |
5633 | | * the saved message (in ss->ssl3.hs.messages). This is written over the dummy |
5634 | | * binder, after which we write the remainder of the binder extension. */ |
5635 | | SECStatus |
5636 | | tls13_WriteExtensionsWithBinder(sslSocket *ss, sslBuffer *extensions, sslBuffer *chBuf) |
5637 | 8.10k | { |
5638 | 8.10k | SSL3Hashes hashes; |
5639 | 8.10k | SECStatus rv; |
5640 | | |
5641 | 8.10k | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)); |
5642 | 8.10k | sslPsk *psk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks); |
5643 | 8.10k | unsigned int size = tls13_GetHashSizeForHash(psk->hash); |
5644 | 8.10k | unsigned int prefixLen = extensions->len - size - 3; |
5645 | 8.10k | unsigned int finishedLen; |
5646 | | |
5647 | 8.10k | PORT_Assert(extensions->len >= size + 3); |
5648 | | |
5649 | 8.10k | rv = sslBuffer_AppendNumber(chBuf, extensions->len, 2); |
5650 | 8.10k | if (rv != SECSuccess) { |
5651 | 0 | return SECFailure; |
5652 | 0 | } |
5653 | | |
5654 | | /* Only write the extension up to the point before the binders. Assume that |
5655 | | * the pre_shared_key extension is at the end of the buffer. Don't write |
5656 | | * the binder, or the lengths that precede it (a 2 octet length for the list |
5657 | | * of all binders, plus a 1 octet length for the binder length). */ |
5658 | 8.10k | rv = sslBuffer_Append(chBuf, extensions->buf, prefixLen); |
5659 | 8.10k | if (rv != SECSuccess) { |
5660 | 0 | return SECFailure; |
5661 | 0 | } |
5662 | | |
5663 | | /* Calculate the binder based on what has been written out. */ |
5664 | 8.10k | rv = tls13_ComputePskBinderHash(ss, chBuf->buf, chBuf->len, &hashes, psk->hash); |
5665 | 8.10k | if (rv != SECSuccess) { |
5666 | 0 | return SECFailure; |
5667 | 0 | } |
5668 | | |
5669 | | /* Write the binder into the extensions buffer, over the zeros we reserved |
5670 | | * previously. This avoids an allocation and means that we don't need a |
5671 | | * separate write for the extra bits that precede the binder. */ |
5672 | 8.10k | PORT_Assert(psk->binderKey); |
5673 | 8.10k | rv = tls13_ComputeFinished(ss, psk->binderKey, |
5674 | 8.10k | psk->hash, &hashes, PR_TRUE, |
5675 | 8.10k | extensions->buf + extensions->len - size, |
5676 | 8.10k | &finishedLen, size); |
5677 | 8.10k | if (rv != SECSuccess) { |
5678 | 0 | return SECFailure; |
5679 | 0 | } |
5680 | 8.10k | PORT_Assert(finishedLen == size); |
5681 | | |
5682 | | /* Write out the remainder of the extension. */ |
5683 | 8.10k | rv = sslBuffer_Append(chBuf, extensions->buf + prefixLen, |
5684 | 8.10k | extensions->len - prefixLen); |
5685 | 8.10k | if (rv != SECSuccess) { |
5686 | 0 | return SECFailure; |
5687 | 0 | } |
5688 | | |
5689 | 8.10k | return SECSuccess; |
5690 | 8.10k | } |
5691 | | |
5692 | | static SECStatus |
5693 | | tls13_ComputeFinished(sslSocket *ss, PK11SymKey *baseKey, |
5694 | | SSLHashType hashType, const SSL3Hashes *hashes, |
5695 | | PRBool sending, PRUint8 *output, unsigned int *outputLen, |
5696 | | unsigned int maxOutputLen) |
5697 | 11.4k | { |
5698 | 11.4k | SECStatus rv; |
5699 | 11.4k | PK11Context *hmacCtx = NULL; |
5700 | 11.4k | CK_MECHANISM_TYPE macAlg = tls13_GetHmacMechanismFromHash(hashType); |
5701 | 11.4k | SECItem param = { siBuffer, NULL, 0 }; |
5702 | 11.4k | unsigned int outputLenUint; |
5703 | 11.4k | const char *label = kHkdfLabelFinishedSecret; |
5704 | 11.4k | PK11SymKey *secret = NULL; |
5705 | | |
5706 | 11.4k | PORT_Assert(baseKey); |
5707 | 11.4k | SSL_TRC(3, ("%d: TLS13[%d]: %s calculate finished", |
5708 | 11.4k | SSL_GETPID(), ss->fd, SSL_ROLE(ss))); |
5709 | 11.4k | PRINT_BUF(50, (ss, "Handshake hash", hashes->u.raw, hashes->len)); |
5710 | | |
5711 | | /* Now derive the appropriate finished secret from the base secret. */ |
5712 | 11.4k | rv = tls13_HkdfExpandLabel(baseKey, hashType, |
5713 | 11.4k | NULL, 0, label, strlen(label), |
5714 | 11.4k | tls13_GetHmacMechanismFromHash(hashType), |
5715 | 11.4k | tls13_GetHashSizeForHash(hashType), |
5716 | 11.4k | ss->protocolVariant, &secret); |
5717 | 11.4k | if (rv != SECSuccess) { |
5718 | 0 | goto abort; |
5719 | 0 | } |
5720 | | |
5721 | 11.4k | PORT_Assert(hashes->len == tls13_GetHashSizeForHash(hashType)); |
5722 | 11.4k | hmacCtx = PK11_CreateContextBySymKey(macAlg, CKA_SIGN, |
5723 | 11.4k | secret, ¶m); |
5724 | 11.4k | if (!hmacCtx) { |
5725 | 0 | goto abort; |
5726 | 0 | } |
5727 | | |
5728 | 11.4k | rv = PK11_DigestBegin(hmacCtx); |
5729 | 11.4k | if (rv != SECSuccess) |
5730 | 0 | goto abort; |
5731 | | |
5732 | 11.4k | rv = PK11_DigestOp(hmacCtx, hashes->u.raw, hashes->len); |
5733 | 11.4k | if (rv != SECSuccess) |
5734 | 0 | goto abort; |
5735 | | |
5736 | 11.4k | PORT_Assert(maxOutputLen >= tls13_GetHashSizeForHash(hashType)); |
5737 | 11.4k | rv = PK11_DigestFinal(hmacCtx, output, &outputLenUint, maxOutputLen); |
5738 | 11.4k | if (rv != SECSuccess) |
5739 | 0 | goto abort; |
5740 | 11.4k | *outputLen = outputLenUint; |
5741 | | |
5742 | 11.4k | PK11_FreeSymKey(secret); |
5743 | 11.4k | PK11_DestroyContext(hmacCtx, PR_TRUE); |
5744 | 11.4k | PRINT_BUF(50, (ss, "finished value", output, outputLenUint)); |
5745 | 11.4k | return SECSuccess; |
5746 | | |
5747 | 0 | abort: |
5748 | 0 | if (secret) { |
5749 | 0 | PK11_FreeSymKey(secret); |
5750 | 0 | } |
5751 | |
|
5752 | 0 | if (hmacCtx) { |
5753 | 0 | PK11_DestroyContext(hmacCtx, PR_TRUE); |
5754 | 0 | } |
5755 | |
|
5756 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5757 | 0 | return SECFailure; |
5758 | 11.4k | } |
5759 | | |
5760 | | static SECStatus |
5761 | | tls13_SendFinished(sslSocket *ss, PK11SymKey *baseKey) |
5762 | 2.74k | { |
5763 | 2.74k | SECStatus rv; |
5764 | 2.74k | PRUint8 finishedBuf[TLS13_MAX_FINISHED_SIZE]; |
5765 | 2.74k | unsigned int finishedLen; |
5766 | 2.74k | SSL3Hashes hashes; |
5767 | | |
5768 | 2.74k | SSL_TRC(3, ("%d: TLS13[%d]: send finished handshake", SSL_GETPID(), ss->fd)); |
5769 | | |
5770 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
5771 | 2.74k | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5772 | | |
5773 | 2.74k | rv = tls13_ComputeHandshakeHashes(ss, &hashes); |
5774 | 2.74k | if (rv != SECSuccess) { |
5775 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
5776 | 0 | return SECFailure; |
5777 | 0 | } |
5778 | | |
5779 | 2.74k | ssl_GetSpecReadLock(ss); |
5780 | 2.74k | rv = tls13_ComputeFinished(ss, baseKey, tls13_GetHash(ss), &hashes, PR_TRUE, |
5781 | 2.74k | finishedBuf, &finishedLen, sizeof(finishedBuf)); |
5782 | 2.74k | ssl_ReleaseSpecReadLock(ss); |
5783 | 2.74k | if (rv != SECSuccess) { |
5784 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
5785 | 0 | return SECFailure; |
5786 | 0 | } |
5787 | | |
5788 | 2.74k | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_finished, finishedLen); |
5789 | 2.74k | if (rv != SECSuccess) { |
5790 | 0 | return SECFailure; /* Error code already set. */ |
5791 | 0 | } |
5792 | | |
5793 | 2.74k | rv = ssl3_AppendHandshake(ss, finishedBuf, finishedLen); |
5794 | 2.74k | if (rv != SECSuccess) { |
5795 | 0 | return SECFailure; /* Error code already set. */ |
5796 | 0 | } |
5797 | | |
5798 | | /* TODO(ekr@rtfm.com): Record key log */ |
5799 | 2.74k | return SECSuccess; |
5800 | 2.74k | } |
5801 | | |
5802 | | static SECStatus |
5803 | | tls13_VerifyFinished(sslSocket *ss, SSLHandshakeType message, |
5804 | | PK11SymKey *secret, |
5805 | | PRUint8 *b, PRUint32 length, |
5806 | | const SSL3Hashes *hashes) |
5807 | 555 | { |
5808 | 555 | SECStatus rv; |
5809 | 555 | PRUint8 finishedBuf[TLS13_MAX_FINISHED_SIZE]; |
5810 | 555 | unsigned int finishedLen; |
5811 | | |
5812 | 555 | if (!hashes) { |
5813 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
5814 | 0 | return SECFailure; |
5815 | 0 | } |
5816 | | |
5817 | 555 | rv = tls13_ComputeFinished(ss, secret, tls13_GetHash(ss), hashes, PR_FALSE, |
5818 | 555 | finishedBuf, &finishedLen, sizeof(finishedBuf)); |
5819 | 555 | if (rv != SECSuccess) { |
5820 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
5821 | 0 | return SECFailure; |
5822 | 0 | } |
5823 | | |
5824 | 555 | if (length != finishedLen) { |
5825 | | #ifndef UNSAFE_FUZZER_MODE |
5826 | | FATAL_ERROR(ss, message == ssl_hs_finished ? SSL_ERROR_RX_MALFORMED_FINISHED : SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, decode_error); |
5827 | | return SECFailure; |
5828 | | #endif |
5829 | 555 | } |
5830 | | |
5831 | 555 | if (NSS_SecureMemcmp(b, finishedBuf, finishedLen) != 0) { |
5832 | | #ifndef UNSAFE_FUZZER_MODE |
5833 | | FATAL_ERROR(ss, SSL_ERROR_BAD_HANDSHAKE_HASH_VALUE, |
5834 | | decrypt_error); |
5835 | | return SECFailure; |
5836 | | #endif |
5837 | 555 | } |
5838 | | |
5839 | 555 | return SECSuccess; |
5840 | 555 | } |
5841 | | |
5842 | | static SECStatus |
5843 | | tls13_CommonHandleFinished(sslSocket *ss, PK11SymKey *key, |
5844 | | PRUint8 *b, PRUint32 length) |
5845 | 584 | { |
5846 | 584 | SECStatus rv; |
5847 | 584 | SSL3Hashes hashes; |
5848 | | |
5849 | 584 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_FINISHED, |
5850 | 584 | wait_finished); |
5851 | 584 | if (rv != SECSuccess) { |
5852 | 29 | return SECFailure; |
5853 | 29 | } |
5854 | 555 | ss->ssl3.hs.endOfFlight = PR_TRUE; |
5855 | | |
5856 | 555 | rv = tls13_ComputeHandshakeHashes(ss, &hashes); |
5857 | 555 | if (rv != SECSuccess) { |
5858 | 0 | LOG_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE); |
5859 | 0 | return SECFailure; |
5860 | 0 | } |
5861 | | |
5862 | 555 | if (ss->firstHsDone) { |
5863 | 0 | rv = ssl_HashPostHandshakeMessage(ss, ssl_hs_finished, b, length); |
5864 | 555 | } else { |
5865 | 555 | rv = ssl_HashHandshakeMessage(ss, ssl_hs_finished, b, length); |
5866 | 555 | } |
5867 | 555 | if (rv != SECSuccess) { |
5868 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
5869 | 0 | return SECFailure; |
5870 | 0 | } |
5871 | | |
5872 | 555 | return tls13_VerifyFinished(ss, ssl_hs_finished, |
5873 | 555 | key, b, length, &hashes); |
5874 | 555 | } |
5875 | | |
5876 | | static SECStatus |
5877 | | tls13_ClientHandleFinished(sslSocket *ss, PRUint8 *b, PRUint32 length) |
5878 | 4 | { |
5879 | 4 | SECStatus rv; |
5880 | | |
5881 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
5882 | 4 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5883 | | |
5884 | 4 | SSL_TRC(3, ("%d: TLS13[%d]: client handle finished handshake", |
5885 | 4 | SSL_GETPID(), ss->fd)); |
5886 | | |
5887 | 4 | rv = tls13_CommonHandleFinished(ss, ss->ssl3.hs.serverHsTrafficSecret, |
5888 | 4 | b, length); |
5889 | 4 | if (rv != SECSuccess) { |
5890 | 4 | return SECFailure; |
5891 | 4 | } |
5892 | | |
5893 | 0 | return tls13_SendClientSecondRound(ss); |
5894 | 4 | } |
5895 | | |
5896 | | static SECStatus |
5897 | | tls13_ServerHandleFinished(sslSocket *ss, PRUint8 *b, PRUint32 length) |
5898 | 580 | { |
5899 | 580 | SECStatus rv; |
5900 | | |
5901 | 580 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
5902 | 580 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
5903 | | |
5904 | 580 | SSL_TRC(3, ("%d: TLS13[%d]: server handle finished handshake", |
5905 | 580 | SSL_GETPID(), ss->fd)); |
5906 | | |
5907 | 580 | if (!tls13_ShouldRequestClientAuth(ss)) { |
5908 | | /* Receiving this message might be the first sign we have that |
5909 | | * early data is over, so pretend we received EOED. */ |
5910 | 566 | rv = tls13_MaybeHandleSuppressedEndOfEarlyData(ss); |
5911 | 566 | if (rv != SECSuccess) { |
5912 | 0 | return SECFailure; /* Code already set. */ |
5913 | 0 | } |
5914 | | |
5915 | 566 | if (!tls13_IsPostHandshake(ss)) { |
5916 | | /* Finalize the RTT estimate. */ |
5917 | 557 | ss->ssl3.hs.rttEstimate = ssl_Time(ss) - ss->ssl3.hs.rttEstimate; |
5918 | 557 | } |
5919 | 566 | } |
5920 | | |
5921 | 580 | rv = tls13_CommonHandleFinished(ss, |
5922 | 580 | ss->firstHsDone ? ss->ssl3.hs.clientTrafficSecret : ss->ssl3.hs.clientHsTrafficSecret, |
5923 | 580 | b, length); |
5924 | 580 | if (rv != SECSuccess) { |
5925 | 25 | return SECFailure; |
5926 | 25 | } |
5927 | | |
5928 | 555 | if (ss->firstHsDone) { |
5929 | 0 | TLS13_SET_HS_STATE(ss, idle_handshake); |
5930 | |
|
5931 | 0 | PORT_Assert(ss->ssl3.hs.shaPostHandshake != NULL); |
5932 | 0 | PK11_DestroyContext(ss->ssl3.hs.shaPostHandshake, PR_TRUE); |
5933 | 0 | ss->ssl3.hs.shaPostHandshake = NULL; |
5934 | |
|
5935 | 0 | ss->ssl3.clientCertRequested = PR_FALSE; |
5936 | |
|
5937 | 0 | if (ss->ssl3.hs.keyUpdateDeferred) { |
5938 | 0 | rv = tls13_SendKeyUpdate(ss, ss->ssl3.hs.deferredKeyUpdateRequest, |
5939 | 0 | PR_FALSE); |
5940 | 0 | if (rv != SECSuccess) { |
5941 | 0 | return SECFailure; /* error is set. */ |
5942 | 0 | } |
5943 | 0 | ss->ssl3.hs.keyUpdateDeferred = PR_FALSE; |
5944 | 0 | } |
5945 | | |
5946 | 0 | return SECSuccess; |
5947 | 0 | } |
5948 | | |
5949 | 555 | if (!tls13_ShouldRequestClientAuth(ss) && |
5950 | 551 | (ss->ssl3.hs.zeroRttState != ssl_0rtt_done)) { |
5951 | 551 | dtls_ReceivedFirstMessageInFlight(ss); |
5952 | 551 | } |
5953 | | |
5954 | 555 | rv = tls13_SetCipherSpec(ss, TrafficKeyApplicationData, |
5955 | 555 | ssl_secret_read, PR_FALSE); |
5956 | 555 | if (rv != SECSuccess) { |
5957 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
5958 | 0 | return SECFailure; |
5959 | 0 | } |
5960 | | |
5961 | 555 | if (IS_DTLS(ss)) { |
5962 | 102 | ssl_CipherSpecReleaseByEpoch(ss, ssl_secret_read, TrafficKeyClearText); |
5963 | | /* We need to keep the handshake cipher spec so we can |
5964 | | * read re-transmitted client Finished. */ |
5965 | 102 | rv = dtls_StartTimer(ss, ss->ssl3.hs.hdTimer, |
5966 | 102 | DTLS_RETRANSMIT_FINISHED_MS, |
5967 | 102 | dtls13_HolddownTimerCb); |
5968 | 102 | if (rv != SECSuccess) { |
5969 | 0 | return SECFailure; |
5970 | 0 | } |
5971 | 102 | } |
5972 | | |
5973 | 555 | rv = tls13_ComputeFinalSecrets(ss); |
5974 | 555 | if (rv != SECSuccess) { |
5975 | 0 | return SECFailure; |
5976 | 0 | } |
5977 | | |
5978 | 555 | rv = tls13_FinishHandshake(ss); |
5979 | 555 | if (rv != SECSuccess) { |
5980 | 0 | return SECFailure; |
5981 | 0 | } |
5982 | | |
5983 | 555 | ssl_GetXmitBufLock(ss); |
5984 | | /* If resumption, authType is the original value and not ssl_auth_psk. */ |
5985 | 555 | if (ss->opt.enableSessionTickets && ss->sec.authType != ssl_auth_psk) { |
5986 | 364 | rv = tls13_SendNewSessionTicket(ss, NULL, 0); |
5987 | 364 | if (rv != SECSuccess) { |
5988 | 0 | goto loser; |
5989 | 0 | } |
5990 | 364 | rv = ssl3_FlushHandshake(ss, 0); |
5991 | 364 | if (rv != SECSuccess) { |
5992 | 0 | goto loser; |
5993 | 0 | } |
5994 | 364 | } |
5995 | 555 | ssl_ReleaseXmitBufLock(ss); |
5996 | 555 | return SECSuccess; |
5997 | | |
5998 | 0 | loser: |
5999 | 0 | ssl_ReleaseXmitBufLock(ss); |
6000 | 0 | return SECFailure; |
6001 | 555 | } |
6002 | | |
6003 | | static SECStatus |
6004 | | tls13_FinishHandshake(sslSocket *ss) |
6005 | 555 | { |
6006 | 555 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
6007 | 555 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6008 | 555 | PORT_Assert(ss->ssl3.hs.restartTarget == NULL); |
6009 | | |
6010 | | /* The first handshake is now completed. */ |
6011 | 555 | ss->handshake = NULL; |
6012 | | |
6013 | | /* Don't need this. */ |
6014 | 555 | PK11_FreeSymKey(ss->ssl3.hs.clientHsTrafficSecret); |
6015 | 555 | ss->ssl3.hs.clientHsTrafficSecret = NULL; |
6016 | 555 | PK11_FreeSymKey(ss->ssl3.hs.serverHsTrafficSecret); |
6017 | 555 | ss->ssl3.hs.serverHsTrafficSecret = NULL; |
6018 | | |
6019 | 555 | TLS13_SET_HS_STATE(ss, idle_handshake); |
6020 | | |
6021 | 555 | return ssl_FinishHandshake(ss); |
6022 | 555 | } |
6023 | | |
6024 | | /* Do the parts of sending the client's second round that require |
6025 | | * the XmitBuf lock. */ |
6026 | | static SECStatus |
6027 | | tls13_SendClientSecondFlight(sslSocket *ss) |
6028 | 0 | { |
6029 | 0 | SECStatus rv; |
6030 | 0 | unsigned int offset = 0; |
6031 | |
|
6032 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
6033 | 0 | PORT_Assert(!ss->ssl3.hs.clientCertificatePending); |
6034 | |
|
6035 | 0 | PRBool sendClientCert = !ss->ssl3.sendEmptyCert && |
6036 | 0 | ss->ssl3.clientCertChain != NULL && |
6037 | 0 | ss->ssl3.clientPrivateKey != NULL; |
6038 | |
|
6039 | 0 | if (ss->firstHsDone) { |
6040 | 0 | offset = SSL_BUFFER_LEN(&ss->sec.ci.sendBuf); |
6041 | 0 | } |
6042 | |
|
6043 | 0 | if (ss->ssl3.sendEmptyCert) { |
6044 | 0 | ss->ssl3.sendEmptyCert = PR_FALSE; |
6045 | 0 | rv = ssl3_SendEmptyCertificate(ss); |
6046 | | /* Don't send verify */ |
6047 | 0 | if (rv != SECSuccess) { |
6048 | 0 | goto alert_error; /* error code is set. */ |
6049 | 0 | } |
6050 | 0 | } else if (sendClientCert) { |
6051 | 0 | rv = tls13_SendCertificate(ss); |
6052 | 0 | if (rv != SECSuccess) { |
6053 | 0 | goto alert_error; /* err code was set. */ |
6054 | 0 | } |
6055 | 0 | } |
6056 | | |
6057 | 0 | if (ss->firstHsDone) { |
6058 | 0 | rv = ssl3_UpdatePostHandshakeHashes(ss, |
6059 | 0 | SSL_BUFFER_BASE(&ss->sec.ci.sendBuf) + offset, |
6060 | 0 | SSL_BUFFER_LEN(&ss->sec.ci.sendBuf) - offset); |
6061 | 0 | if (rv != SECSuccess) { |
6062 | 0 | goto alert_error; /* err code was set. */ |
6063 | 0 | } |
6064 | 0 | } |
6065 | | |
6066 | 0 | if (ss->ssl3.hs.clientCertRequested) { |
6067 | 0 | SECITEM_FreeItem(&ss->xtnData.certReqContext, PR_FALSE); |
6068 | 0 | if (ss->xtnData.certReqAuthorities.arena) { |
6069 | 0 | PORT_FreeArena(ss->xtnData.certReqAuthorities.arena, PR_FALSE); |
6070 | 0 | ss->xtnData.certReqAuthorities.arena = NULL; |
6071 | 0 | } |
6072 | 0 | PORT_Memset(&ss->xtnData.certReqAuthorities, 0, |
6073 | 0 | sizeof(ss->xtnData.certReqAuthorities)); |
6074 | 0 | ss->ssl3.hs.clientCertRequested = PR_FALSE; |
6075 | 0 | } |
6076 | |
|
6077 | 0 | if (sendClientCert) { |
6078 | 0 | if (ss->firstHsDone) { |
6079 | 0 | offset = SSL_BUFFER_LEN(&ss->sec.ci.sendBuf); |
6080 | 0 | } |
6081 | |
|
6082 | 0 | rv = tls13_SendCertificateVerify(ss, ss->ssl3.clientPrivateKey); |
6083 | 0 | SECKEY_DestroyPrivateKey(ss->ssl3.clientPrivateKey); |
6084 | 0 | ss->ssl3.clientPrivateKey = NULL; |
6085 | 0 | if (rv != SECSuccess) { |
6086 | 0 | goto alert_error; /* err code was set. */ |
6087 | 0 | } |
6088 | | |
6089 | 0 | if (ss->firstHsDone) { |
6090 | 0 | rv = ssl3_UpdatePostHandshakeHashes(ss, |
6091 | 0 | SSL_BUFFER_BASE(&ss->sec.ci.sendBuf) + offset, |
6092 | 0 | SSL_BUFFER_LEN(&ss->sec.ci.sendBuf) - offset); |
6093 | 0 | if (rv != SECSuccess) { |
6094 | 0 | goto alert_error; /* err code was set. */ |
6095 | 0 | } |
6096 | 0 | } |
6097 | 0 | } |
6098 | | |
6099 | 0 | rv = tls13_SendFinished(ss, ss->firstHsDone ? ss->ssl3.hs.clientTrafficSecret : ss->ssl3.hs.clientHsTrafficSecret); |
6100 | 0 | if (rv != SECSuccess) { |
6101 | 0 | goto alert_error; /* err code was set. */ |
6102 | 0 | } |
6103 | 0 | rv = ssl3_FlushHandshake(ss, 0); |
6104 | 0 | if (rv != SECSuccess) { |
6105 | | /* No point in sending an alert here because we're not going to |
6106 | | * be able to send it if we couldn't flush the handshake. */ |
6107 | 0 | goto error; |
6108 | 0 | } |
6109 | | |
6110 | 0 | return SECSuccess; |
6111 | | |
6112 | 0 | alert_error: |
6113 | 0 | FATAL_ERROR(ss, PORT_GetError(), internal_error); |
6114 | 0 | return SECFailure; |
6115 | 0 | error: |
6116 | 0 | LOG_ERROR(ss, PORT_GetError()); |
6117 | 0 | return SECFailure; |
6118 | 0 | } |
6119 | | |
6120 | | static SECStatus |
6121 | | tls13_SendClientSecondRound(sslSocket *ss) |
6122 | 0 | { |
6123 | 0 | SECStatus rv; |
6124 | |
|
6125 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveRecvBufLock(ss)); |
6126 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss)); |
6127 | | |
6128 | | /* Defer client authentication sending if we are still waiting for server |
6129 | | * authentication. This avoids unnecessary disclosure of client credentials |
6130 | | * to an unauthenticated server. |
6131 | | */ |
6132 | 0 | if (ss->ssl3.hs.restartTarget) { |
6133 | 0 | PR_NOT_REACHED("unexpected ss->ssl3.hs.restartTarget"); |
6134 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6135 | 0 | return SECFailure; |
6136 | 0 | } |
6137 | 0 | if (ss->ssl3.hs.authCertificatePending || ss->ssl3.hs.clientCertificatePending) { |
6138 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: deferring tls13_SendClientSecondRound because" |
6139 | 0 | " certificate authentication is still pending.", |
6140 | 0 | SSL_GETPID(), ss->fd)); |
6141 | 0 | ss->ssl3.hs.restartTarget = tls13_SendClientSecondRound; |
6142 | 0 | PORT_SetError(PR_WOULD_BLOCK_ERROR); |
6143 | 0 | return SECFailure; |
6144 | 0 | } |
6145 | | |
6146 | 0 | rv = tls13_ComputeApplicationSecrets(ss); |
6147 | 0 | if (rv != SECSuccess) { |
6148 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
6149 | 0 | return SECFailure; |
6150 | 0 | } |
6151 | | |
6152 | 0 | if (ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted) { |
6153 | 0 | ssl_GetXmitBufLock(ss); /*******************************/ |
6154 | 0 | rv = tls13_SendEndOfEarlyData(ss); |
6155 | 0 | ssl_ReleaseXmitBufLock(ss); /*******************************/ |
6156 | 0 | if (rv != SECSuccess) { |
6157 | 0 | return SECFailure; /* Error code already set. */ |
6158 | 0 | } |
6159 | 0 | } else if (ss->opt.enableTls13CompatMode && !IS_DTLS(ss) && |
6160 | 0 | ss->ssl3.hs.zeroRttState == ssl_0rtt_none && |
6161 | 0 | !ss->ssl3.hs.helloRetry) { |
6162 | 0 | ssl_GetXmitBufLock(ss); /*******************************/ |
6163 | 0 | rv = ssl3_SendChangeCipherSpecsInt(ss); |
6164 | 0 | ssl_ReleaseXmitBufLock(ss); /*******************************/ |
6165 | 0 | if (rv != SECSuccess) { |
6166 | 0 | return rv; |
6167 | 0 | } |
6168 | 0 | } |
6169 | | |
6170 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyHandshake, |
6171 | 0 | ssl_secret_write, PR_FALSE); |
6172 | 0 | if (rv != SECSuccess) { |
6173 | 0 | FATAL_ERROR(ss, SSL_ERROR_INIT_CIPHER_SUITE_FAILURE, internal_error); |
6174 | 0 | return SECFailure; |
6175 | 0 | } |
6176 | | |
6177 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyApplicationData, |
6178 | 0 | ssl_secret_read, PR_FALSE); |
6179 | 0 | if (rv != SECSuccess) { |
6180 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
6181 | 0 | return SECFailure; |
6182 | 0 | } |
6183 | | |
6184 | 0 | ssl_GetXmitBufLock(ss); /*******************************/ |
6185 | | /* This call can't block, as clientAuthCertificatePending is checked above */ |
6186 | 0 | rv = tls13_SendClientSecondFlight(ss); |
6187 | 0 | ssl_ReleaseXmitBufLock(ss); /*******************************/ |
6188 | 0 | if (rv != SECSuccess) { |
6189 | 0 | return SECFailure; |
6190 | 0 | } |
6191 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyApplicationData, |
6192 | 0 | ssl_secret_write, PR_FALSE); |
6193 | 0 | if (rv != SECSuccess) { |
6194 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6195 | 0 | return SECFailure; |
6196 | 0 | } |
6197 | | |
6198 | 0 | rv = tls13_ComputeFinalSecrets(ss); |
6199 | 0 | if (rv != SECSuccess) { |
6200 | 0 | return SECFailure; |
6201 | 0 | } |
6202 | | |
6203 | | /* The handshake is now finished */ |
6204 | 0 | return tls13_FinishHandshake(ss); |
6205 | 0 | } |
6206 | | |
6207 | | /* |
6208 | | * enum { (65535) } TicketExtensionType; |
6209 | | * |
6210 | | * struct { |
6211 | | * TicketExtensionType extension_type; |
6212 | | * opaque extension_data<0..2^16-1>; |
6213 | | * } TicketExtension; |
6214 | | * |
6215 | | * struct { |
6216 | | * uint32 ticket_lifetime; |
6217 | | * uint32 ticket_age_add; |
6218 | | * opaque ticket_nonce<1..255>; |
6219 | | * opaque ticket<1..2^16-1>; |
6220 | | * TicketExtension extensions<0..2^16-2>; |
6221 | | * } NewSessionTicket; |
6222 | | */ |
6223 | | |
6224 | | static SECStatus |
6225 | | tls13_SendNewSessionTicket(sslSocket *ss, const PRUint8 *appToken, |
6226 | | unsigned int appTokenLen) |
6227 | 364 | { |
6228 | 364 | PRUint16 message_length; |
6229 | 364 | PK11SymKey *secret; |
6230 | 364 | SECItem ticket_data = { 0, NULL, 0 }; |
6231 | 364 | SECStatus rv; |
6232 | 364 | NewSessionTicket ticket = { 0 }; |
6233 | 364 | PRUint32 max_early_data_size_len = 0; |
6234 | 364 | PRUint32 greaseLen = 0; |
6235 | 364 | PRUint8 ticketNonce[sizeof(ss->ssl3.hs.ticketNonce)]; |
6236 | 364 | sslBuffer ticketNonceBuf = SSL_BUFFER(ticketNonce); |
6237 | | |
6238 | 364 | SSL_TRC(3, ("%d: TLS13[%d]: send new session ticket message %d", |
6239 | 364 | SSL_GETPID(), ss->fd, ss->ssl3.hs.ticketNonce)); |
6240 | | |
6241 | 364 | ticket.flags = 0; |
6242 | 364 | if (ss->opt.enable0RttData) { |
6243 | 154 | ticket.flags |= ticket_allow_early_data; |
6244 | 154 | max_early_data_size_len = 8; /* type + len + value. */ |
6245 | 154 | } |
6246 | 364 | ticket.ticket_lifetime_hint = ssl_ticket_lifetime; |
6247 | | |
6248 | 364 | if (ss->opt.enableGrease) { |
6249 | 209 | greaseLen = 4; /* type + len + 0 (empty) */ |
6250 | 209 | } |
6251 | | |
6252 | | /* The ticket age obfuscator. */ |
6253 | 364 | rv = PK11_GenerateRandom((PRUint8 *)&ticket.ticket_age_add, |
6254 | 364 | sizeof(ticket.ticket_age_add)); |
6255 | 364 | if (rv != SECSuccess) |
6256 | 0 | goto loser; |
6257 | | |
6258 | 364 | rv = sslBuffer_AppendNumber(&ticketNonceBuf, ss->ssl3.hs.ticketNonce, |
6259 | 364 | sizeof(ticketNonce)); |
6260 | 364 | if (rv != SECSuccess) { |
6261 | 0 | goto loser; |
6262 | 0 | } |
6263 | 364 | ++ss->ssl3.hs.ticketNonce; |
6264 | 364 | rv = tls13_HkdfExpandLabel(ss->ssl3.hs.resumptionMasterSecret, |
6265 | 364 | tls13_GetHash(ss), |
6266 | 364 | ticketNonce, sizeof(ticketNonce), |
6267 | 364 | kHkdfLabelResumption, |
6268 | 364 | strlen(kHkdfLabelResumption), |
6269 | 364 | CKM_HKDF_DERIVE, |
6270 | 364 | tls13_GetHashSize(ss), |
6271 | 364 | ss->protocolVariant, &secret); |
6272 | 364 | if (rv != SECSuccess) { |
6273 | 0 | goto loser; |
6274 | 0 | } |
6275 | | |
6276 | 364 | rv = ssl3_EncodeSessionTicket(ss, &ticket, appToken, appTokenLen, |
6277 | 364 | secret, &ticket_data); |
6278 | 364 | PK11_FreeSymKey(secret); |
6279 | 364 | if (rv != SECSuccess) |
6280 | 0 | goto loser; |
6281 | | |
6282 | 364 | message_length = |
6283 | 364 | 4 + /* lifetime */ |
6284 | 364 | 4 + /* ticket_age_add */ |
6285 | 364 | 1 + sizeof(ticketNonce) + /* ticket_nonce */ |
6286 | 364 | 2 + /* extensions lentgh */ |
6287 | 364 | max_early_data_size_len + /* max_early_data_size extension length */ |
6288 | 364 | greaseLen + /* GREASE extension length */ |
6289 | 364 | 2 + /* ticket length */ |
6290 | 364 | ticket_data.len; |
6291 | | |
6292 | 364 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_new_session_ticket, |
6293 | 364 | message_length); |
6294 | 364 | if (rv != SECSuccess) |
6295 | 0 | goto loser; |
6296 | | |
6297 | | /* This is a fixed value. */ |
6298 | 364 | rv = ssl3_AppendHandshakeNumber(ss, ssl_ticket_lifetime, 4); |
6299 | 364 | if (rv != SECSuccess) |
6300 | 0 | goto loser; |
6301 | | |
6302 | 364 | rv = ssl3_AppendHandshakeNumber(ss, ticket.ticket_age_add, 4); |
6303 | 364 | if (rv != SECSuccess) |
6304 | 0 | goto loser; |
6305 | | |
6306 | | /* The ticket nonce. */ |
6307 | 364 | rv = ssl3_AppendHandshakeVariable(ss, ticketNonce, sizeof(ticketNonce), 1); |
6308 | 364 | if (rv != SECSuccess) |
6309 | 0 | goto loser; |
6310 | | |
6311 | | /* Encode the ticket. */ |
6312 | 364 | rv = ssl3_AppendHandshakeVariable( |
6313 | 364 | ss, ticket_data.data, ticket_data.len, 2); |
6314 | 364 | if (rv != SECSuccess) |
6315 | 0 | goto loser; |
6316 | | |
6317 | | /* Extensions */ |
6318 | 364 | rv = ssl3_AppendHandshakeNumber(ss, max_early_data_size_len + greaseLen, 2); |
6319 | 364 | if (rv != SECSuccess) |
6320 | 0 | goto loser; |
6321 | | |
6322 | | /* GREASE NewSessionTicket: |
6323 | | * When sending a NewSessionTicket message in TLS 1.3, a server MAY select |
6324 | | * one or more GREASE extension values and advertise them as extensions |
6325 | | * with varying length and contents [RFC8701, SEction 4.1]. */ |
6326 | 364 | if (ss->opt.enableGrease) { |
6327 | 209 | PR_ASSERT(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
6328 | | |
6329 | 209 | PRUint16 grease; |
6330 | 209 | rv = tls13_RandomGreaseValue(&grease); |
6331 | 209 | if (rv != SECSuccess) |
6332 | 0 | goto loser; |
6333 | | /* Extension type */ |
6334 | 209 | rv = ssl3_AppendHandshakeNumber(ss, grease, 2); |
6335 | 209 | if (rv != SECSuccess) |
6336 | 0 | goto loser; |
6337 | | /* Extension length */ |
6338 | 209 | rv = ssl3_AppendHandshakeNumber(ss, 0, 2); |
6339 | 209 | if (rv != SECSuccess) |
6340 | 0 | goto loser; |
6341 | 209 | } |
6342 | | |
6343 | | /* Max early data size extension. */ |
6344 | 364 | if (max_early_data_size_len) { |
6345 | 154 | rv = ssl3_AppendHandshakeNumber( |
6346 | 154 | ss, ssl_tls13_early_data_xtn, 2); |
6347 | 154 | if (rv != SECSuccess) |
6348 | 0 | goto loser; |
6349 | | |
6350 | | /* Length */ |
6351 | 154 | rv = ssl3_AppendHandshakeNumber(ss, 4, 2); |
6352 | 154 | if (rv != SECSuccess) |
6353 | 0 | goto loser; |
6354 | | |
6355 | 154 | rv = ssl3_AppendHandshakeNumber(ss, ss->opt.maxEarlyDataSize, 4); |
6356 | 154 | if (rv != SECSuccess) |
6357 | 0 | goto loser; |
6358 | 154 | } |
6359 | | |
6360 | 364 | SECITEM_FreeItem(&ticket_data, PR_FALSE); |
6361 | 364 | return SECSuccess; |
6362 | | |
6363 | 0 | loser: |
6364 | 0 | if (ticket_data.data) { |
6365 | 0 | SECITEM_FreeItem(&ticket_data, PR_FALSE); |
6366 | 0 | } |
6367 | 0 | return SECFailure; |
6368 | 364 | } |
6369 | | |
6370 | | SECStatus |
6371 | | SSLExp_SendSessionTicket(PRFileDesc *fd, const PRUint8 *token, |
6372 | | unsigned int tokenLen) |
6373 | 0 | { |
6374 | 0 | sslSocket *ss; |
6375 | 0 | SECStatus rv; |
6376 | |
|
6377 | 0 | ss = ssl_FindSocket(fd); |
6378 | 0 | if (!ss) { |
6379 | 0 | return SECFailure; |
6380 | 0 | } |
6381 | | |
6382 | 0 | if (IS_DTLS(ss)) { |
6383 | 0 | PORT_SetError(SSL_ERROR_FEATURE_NOT_SUPPORTED_FOR_VERSION); |
6384 | 0 | return SECFailure; |
6385 | 0 | } |
6386 | | |
6387 | 0 | if (!ss->sec.isServer || !tls13_IsPostHandshake(ss) || |
6388 | 0 | tokenLen > 0xffff) { |
6389 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
6390 | 0 | return SECFailure; |
6391 | 0 | } |
6392 | | |
6393 | | /* Disable tickets if we can trace this connection back to a PSK. |
6394 | | * We aren't able to issue tickets (currently) without a certificate. |
6395 | | * As PSK =~ resumption, there is no reason to do this. */ |
6396 | 0 | if (ss->sec.authType == ssl_auth_psk) { |
6397 | 0 | PORT_SetError(SSL_ERROR_FEATURE_DISABLED); |
6398 | 0 | return SECFailure; |
6399 | 0 | } |
6400 | | |
6401 | 0 | ssl_GetSSL3HandshakeLock(ss); |
6402 | 0 | ssl_GetXmitBufLock(ss); |
6403 | 0 | rv = tls13_SendNewSessionTicket(ss, token, tokenLen); |
6404 | 0 | if (rv == SECSuccess) { |
6405 | 0 | rv = ssl3_FlushHandshake(ss, 0); |
6406 | 0 | } |
6407 | 0 | ssl_ReleaseXmitBufLock(ss); |
6408 | 0 | ssl_ReleaseSSL3HandshakeLock(ss); |
6409 | |
|
6410 | 0 | return rv; |
6411 | 0 | } |
6412 | | |
6413 | | static SECStatus |
6414 | | tls13_HandleNewSessionTicket(sslSocket *ss, PRUint8 *b, PRUint32 length) |
6415 | 11 | { |
6416 | 11 | SECStatus rv; |
6417 | 11 | PRUint32 utmp; |
6418 | 11 | NewSessionTicket ticket = { 0 }; |
6419 | 11 | SECItem data; |
6420 | 11 | SECItem ticket_nonce; |
6421 | 11 | SECItem ticket_data; |
6422 | | |
6423 | 11 | SSL_TRC(3, ("%d: TLS13[%d]: handle new session ticket message", |
6424 | 11 | SSL_GETPID(), ss->fd)); |
6425 | | |
6426 | 11 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_NEW_SESSION_TICKET, |
6427 | 11 | idle_handshake); |
6428 | 11 | if (rv != SECSuccess) { |
6429 | 9 | return SECFailure; |
6430 | 9 | } |
6431 | 2 | if (!tls13_IsPostHandshake(ss) || ss->sec.isServer) { |
6432 | 2 | FATAL_ERROR(ss, SSL_ERROR_RX_UNEXPECTED_NEW_SESSION_TICKET, |
6433 | 2 | unexpected_message); |
6434 | 2 | return SECFailure; |
6435 | 2 | } |
6436 | | |
6437 | 0 | ticket.received_timestamp = ssl_Time(ss); |
6438 | 0 | rv = ssl3_ConsumeHandshakeNumber(ss, &ticket.ticket_lifetime_hint, 4, &b, |
6439 | 0 | &length); |
6440 | 0 | if (rv != SECSuccess) { |
6441 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET, |
6442 | 0 | decode_error); |
6443 | 0 | return SECFailure; |
6444 | 0 | } |
6445 | 0 | ticket.ticket.type = siBuffer; |
6446 | |
|
6447 | 0 | rv = ssl3_ConsumeHandshake(ss, &utmp, sizeof(utmp), |
6448 | 0 | &b, &length); |
6449 | 0 | if (rv != SECSuccess) { |
6450 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET); |
6451 | 0 | return SECFailure; |
6452 | 0 | } |
6453 | 0 | ticket.ticket_age_add = PR_ntohl(utmp); |
6454 | | |
6455 | | /* The nonce. */ |
6456 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &ticket_nonce, 1, &b, &length); |
6457 | 0 | if (rv != SECSuccess) { |
6458 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET, |
6459 | 0 | decode_error); |
6460 | 0 | return SECFailure; |
6461 | 0 | } |
6462 | | |
6463 | | /* Get the ticket value. */ |
6464 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &ticket_data, 2, &b, &length); |
6465 | 0 | if (rv != SECSuccess || !ticket_data.len) { |
6466 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET, |
6467 | 0 | decode_error); |
6468 | 0 | return SECFailure; |
6469 | 0 | } |
6470 | | |
6471 | | /* Parse extensions. */ |
6472 | 0 | rv = ssl3_ConsumeHandshakeVariable(ss, &data, 2, &b, &length); |
6473 | 0 | if (rv != SECSuccess || length) { |
6474 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET, |
6475 | 0 | decode_error); |
6476 | 0 | return SECFailure; |
6477 | 0 | } |
6478 | | |
6479 | 0 | rv = ssl3_HandleExtensions(ss, &data.data, |
6480 | 0 | &data.len, ssl_hs_new_session_ticket); |
6481 | 0 | if (rv != SECSuccess) { |
6482 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_NEW_SESSION_TICKET, |
6483 | 0 | decode_error); |
6484 | 0 | return SECFailure; |
6485 | 0 | } |
6486 | 0 | if (ss->xtnData.max_early_data_size) { |
6487 | 0 | ticket.flags |= ticket_allow_early_data; |
6488 | 0 | ticket.max_early_data_size = ss->xtnData.max_early_data_size; |
6489 | 0 | } |
6490 | |
|
6491 | 0 | if (!ss->opt.noCache) { |
6492 | 0 | PK11SymKey *secret; |
6493 | |
|
6494 | 0 | PORT_Assert(ss->sec.ci.sid); |
6495 | 0 | rv = SECITEM_CopyItem(NULL, &ticket.ticket, &ticket_data); |
6496 | 0 | if (rv != SECSuccess) { |
6497 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
6498 | 0 | return SECFailure; |
6499 | 0 | } |
6500 | 0 | PRINT_BUF(50, (ss, "Caching session ticket", |
6501 | 0 | ticket.ticket.data, |
6502 | 0 | ticket.ticket.len)); |
6503 | | |
6504 | | /* Replace a previous session ticket when |
6505 | | * we receive a second NewSessionTicket message. */ |
6506 | 0 | if (ss->sec.ci.sid->cached == in_client_cache || |
6507 | 0 | ss->sec.ci.sid->cached == in_external_cache) { |
6508 | | /* Create a new session ID. */ |
6509 | 0 | sslSessionID *sid = ssl3_NewSessionID(ss, PR_FALSE); |
6510 | 0 | if (!sid) { |
6511 | 0 | return SECFailure; |
6512 | 0 | } |
6513 | | |
6514 | | /* Copy over the peerCert. */ |
6515 | 0 | PORT_Assert(ss->sec.ci.sid->peerCert); |
6516 | 0 | sid->peerCert = CERT_DupCertificate(ss->sec.ci.sid->peerCert); |
6517 | 0 | if (!sid->peerCert) { |
6518 | 0 | ssl_FreeSID(sid); |
6519 | 0 | return SECFailure; |
6520 | 0 | } |
6521 | | |
6522 | | /* Destroy the old SID. */ |
6523 | 0 | ssl_UncacheSessionID(ss); |
6524 | 0 | ssl_FreeSID(ss->sec.ci.sid); |
6525 | 0 | ss->sec.ci.sid = sid; |
6526 | 0 | } |
6527 | | |
6528 | 0 | ssl3_SetSIDSessionTicket(ss->sec.ci.sid, &ticket); |
6529 | 0 | PORT_Assert(!ticket.ticket.data); |
6530 | |
|
6531 | 0 | rv = tls13_HkdfExpandLabel(ss->ssl3.hs.resumptionMasterSecret, |
6532 | 0 | tls13_GetHash(ss), |
6533 | 0 | ticket_nonce.data, ticket_nonce.len, |
6534 | 0 | kHkdfLabelResumption, |
6535 | 0 | strlen(kHkdfLabelResumption), |
6536 | 0 | CKM_HKDF_DERIVE, |
6537 | 0 | tls13_GetHashSize(ss), |
6538 | 0 | ss->protocolVariant, &secret); |
6539 | 0 | if (rv != SECSuccess) { |
6540 | 0 | return SECFailure; |
6541 | 0 | } |
6542 | | |
6543 | 0 | rv = ssl3_FillInCachedSID(ss, ss->sec.ci.sid, secret); |
6544 | 0 | PK11_FreeSymKey(secret); |
6545 | 0 | if (rv != SECSuccess) { |
6546 | 0 | return SECFailure; |
6547 | 0 | } |
6548 | | |
6549 | | /* Cache the session. */ |
6550 | 0 | ssl_CacheSessionID(ss); |
6551 | 0 | } |
6552 | | |
6553 | 0 | return SECSuccess; |
6554 | 0 | } |
6555 | | |
6556 | | #define _M_NONE 0 |
6557 | 139k | #define _M(a) (1 << PR_MIN(a, 31)) |
6558 | | #define _M1(a) (_M(ssl_hs_##a)) |
6559 | | #define _M2(a, b) (_M1(a) | _M1(b)) |
6560 | | #define _M3(a, b, c) (_M1(a) | _M2(b, c)) |
6561 | | |
6562 | | static const struct { |
6563 | | PRUint16 ex_value; |
6564 | | PRUint32 messages; |
6565 | | } KnownExtensions[] = { |
6566 | | { ssl_server_name_xtn, _M2(client_hello, encrypted_extensions) }, |
6567 | | { ssl_supported_groups_xtn, _M2(client_hello, encrypted_extensions) }, |
6568 | | { ssl_signature_algorithms_xtn, _M2(client_hello, certificate_request) }, |
6569 | | { ssl_signature_algorithms_cert_xtn, _M2(client_hello, |
6570 | | certificate_request) }, |
6571 | | { ssl_use_srtp_xtn, _M2(client_hello, encrypted_extensions) }, |
6572 | | { ssl_app_layer_protocol_xtn, _M2(client_hello, encrypted_extensions) }, |
6573 | | { ssl_padding_xtn, _M1(client_hello) }, |
6574 | | { ssl_tls13_key_share_xtn, _M3(client_hello, server_hello, |
6575 | | hello_retry_request) }, |
6576 | | { ssl_tls13_pre_shared_key_xtn, _M2(client_hello, server_hello) }, |
6577 | | { ssl_tls13_psk_key_exchange_modes_xtn, _M1(client_hello) }, |
6578 | | { ssl_tls13_early_data_xtn, _M3(client_hello, encrypted_extensions, |
6579 | | new_session_ticket) }, |
6580 | | { ssl_signed_cert_timestamp_xtn, _M3(client_hello, certificate_request, |
6581 | | certificate) }, |
6582 | | { ssl_cert_status_xtn, _M3(client_hello, certificate_request, |
6583 | | certificate) }, |
6584 | | { ssl_delegated_credentials_xtn, _M2(client_hello, certificate) }, |
6585 | | { ssl_tls13_cookie_xtn, _M2(client_hello, hello_retry_request) }, |
6586 | | { ssl_tls13_certificate_authorities_xtn, _M2(client_hello, certificate_request) }, |
6587 | | { ssl_tls13_supported_versions_xtn, _M3(client_hello, server_hello, |
6588 | | hello_retry_request) }, |
6589 | | { ssl_record_size_limit_xtn, _M2(client_hello, encrypted_extensions) }, |
6590 | | { ssl_tls13_encrypted_client_hello_xtn, _M3(client_hello, encrypted_extensions, hello_retry_request) }, |
6591 | | { ssl_tls13_outer_extensions_xtn, _M_NONE /* Encoding/decoding only */ }, |
6592 | | { ssl_tls13_post_handshake_auth_xtn, _M1(client_hello) }, |
6593 | | { ssl_certificate_compression_xtn, _M2(client_hello, certificate_request) } |
6594 | | }; |
6595 | | |
6596 | | tls13ExtensionStatus |
6597 | | tls13_ExtensionStatus(PRUint16 extension, SSLHandshakeType message) |
6598 | 178k | { |
6599 | 178k | unsigned int i; |
6600 | | |
6601 | 178k | PORT_Assert((message == ssl_hs_client_hello) || |
6602 | 178k | (message == ssl_hs_server_hello) || |
6603 | 178k | (message == ssl_hs_hello_retry_request) || |
6604 | 178k | (message == ssl_hs_encrypted_extensions) || |
6605 | 178k | (message == ssl_hs_new_session_ticket) || |
6606 | 178k | (message == ssl_hs_certificate) || |
6607 | 178k | (message == ssl_hs_certificate_request)); |
6608 | | |
6609 | 2.21M | for (i = 0; i < PR_ARRAY_SIZE(KnownExtensions); i++) { |
6610 | | /* Hacky check for message numbers > 30. */ |
6611 | 2.17M | PORT_Assert(!(KnownExtensions[i].messages & (1U << 31))); |
6612 | 2.17M | if (KnownExtensions[i].ex_value == extension) { |
6613 | 139k | break; |
6614 | 139k | } |
6615 | 2.17M | } |
6616 | 178k | if (i >= PR_ARRAY_SIZE(KnownExtensions)) { |
6617 | 39.6k | return tls13_extension_unknown; |
6618 | 39.6k | } |
6619 | | |
6620 | | /* Return "disallowed" if the message mask bit isn't set. */ |
6621 | 139k | if (!(_M(message) & KnownExtensions[i].messages)) { |
6622 | 14.4k | return tls13_extension_disallowed; |
6623 | 14.4k | } |
6624 | | |
6625 | 124k | return tls13_extension_allowed; |
6626 | 139k | } |
6627 | | |
6628 | | #undef _M |
6629 | | #undef _M1 |
6630 | | #undef _M2 |
6631 | | #undef _M3 |
6632 | | |
6633 | | /* We cheat a bit on additional data because the AEAD interface |
6634 | | * which doesn't have room for the record number. The AAD we |
6635 | | * format is serialized record number followed by the true AD |
6636 | | * (i.e., the record header) plus the serialized record number. */ |
6637 | | static SECStatus |
6638 | | tls13_FormatAdditionalData( |
6639 | | sslSocket *ss, |
6640 | | const PRUint8 *header, unsigned int headerLen, |
6641 | | DTLSEpoch epoch, sslSequenceNumber seqNum, |
6642 | | PRUint8 *aad, unsigned int *aadLength, unsigned int maxLength) |
6643 | 40.4k | { |
6644 | 40.4k | SECStatus rv; |
6645 | 40.4k | sslBuffer buf = SSL_BUFFER_FIXED(aad, maxLength); |
6646 | | |
6647 | 40.4k | if (IS_DTLS_1_OR_12(ss)) { |
6648 | 0 | rv = sslBuffer_AppendNumber(&buf, epoch, 2); |
6649 | 0 | if (rv != SECSuccess) { |
6650 | 0 | return SECFailure; |
6651 | 0 | } |
6652 | 0 | } |
6653 | 40.4k | rv = sslBuffer_AppendNumber(&buf, seqNum, IS_DTLS_1_OR_12(ss) ? 6 : 8); |
6654 | 40.4k | if (rv != SECSuccess) { |
6655 | 0 | return SECFailure; |
6656 | 0 | } |
6657 | | |
6658 | 40.4k | rv = sslBuffer_Append(&buf, header, headerLen); |
6659 | 40.4k | if (rv != SECSuccess) { |
6660 | 0 | return SECFailure; |
6661 | 0 | } |
6662 | | |
6663 | 40.4k | *aadLength = buf.len; |
6664 | | |
6665 | 40.4k | return SECSuccess; |
6666 | 40.4k | } |
6667 | | |
6668 | | PRInt32 |
6669 | | tls13_LimitEarlyData(sslSocket *ss, SSLContentType type, PRInt32 toSend) |
6670 | 0 | { |
6671 | 0 | PRInt32 reduced; |
6672 | |
|
6673 | 0 | PORT_Assert(type == ssl_ct_application_data); |
6674 | 0 | PORT_Assert(ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3); |
6675 | 0 | PORT_Assert(!ss->firstHsDone); |
6676 | 0 | if (ss->ssl3.cwSpec->epoch != TrafficKeyEarlyApplicationData) { |
6677 | 0 | return toSend; |
6678 | 0 | } |
6679 | | |
6680 | 0 | if (IS_DTLS(ss) && toSend > ss->ssl3.cwSpec->earlyDataRemaining) { |
6681 | | /* Don't split application data records in DTLS. */ |
6682 | 0 | return 0; |
6683 | 0 | } |
6684 | | |
6685 | 0 | reduced = PR_MIN(toSend, ss->ssl3.cwSpec->earlyDataRemaining); |
6686 | 0 | ss->ssl3.cwSpec->earlyDataRemaining -= reduced; |
6687 | 0 | return reduced; |
6688 | 0 | } |
6689 | | |
6690 | | SECStatus |
6691 | | tls13_ProtectRecord(sslSocket *ss, |
6692 | | ssl3CipherSpec *cwSpec, |
6693 | | SSLContentType type, |
6694 | | const PRUint8 *pIn, |
6695 | | PRUint32 contentLen, |
6696 | | sslBuffer *wrBuf) |
6697 | 56.9k | { |
6698 | 56.9k | const ssl3BulkCipherDef *cipher_def = cwSpec->cipherDef; |
6699 | 56.9k | const int tagLen = cipher_def->tag_size; |
6700 | 56.9k | SECStatus rv; |
6701 | | |
6702 | 56.9k | PORT_Assert(cwSpec->direction == ssl_secret_write); |
6703 | 56.9k | SSL_TRC(3, ("%d: TLS13[%d]: spec=%d epoch=%d (%s) protect 0x%0llx len=%u", |
6704 | 56.9k | SSL_GETPID(), ss->fd, cwSpec, cwSpec->epoch, cwSpec->phase, |
6705 | 56.9k | cwSpec->nextSeqNum, contentLen)); |
6706 | | |
6707 | 56.9k | if (contentLen + 1 + tagLen > SSL_BUFFER_SPACE(wrBuf)) { |
6708 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
6709 | 0 | return SECFailure; |
6710 | 0 | } |
6711 | | |
6712 | | /* Copy the data into the wrBuf. We're going to encrypt in-place |
6713 | | * in the AEAD branch anyway */ |
6714 | 56.9k | PORT_Memcpy(SSL_BUFFER_NEXT(wrBuf), pIn, contentLen); |
6715 | | |
6716 | 56.9k | if (cipher_def->calg == ssl_calg_null) { |
6717 | | /* Shortcut for plaintext */ |
6718 | 30.7k | rv = sslBuffer_Skip(wrBuf, contentLen, NULL); |
6719 | 30.7k | PORT_Assert(rv == SECSuccess); |
6720 | 30.7k | } else { |
6721 | 26.2k | PRUint8 hdr[13]; |
6722 | 26.2k | sslBuffer buf = SSL_BUFFER_FIXED(hdr, sizeof(hdr)); |
6723 | 26.2k | PRBool needsLength; |
6724 | 26.2k | PRUint8 aad[21]; |
6725 | 26.2k | const int ivLen = cipher_def->iv_size + cipher_def->explicit_nonce_size; |
6726 | 26.2k | unsigned int ivOffset = ivLen - sizeof(sslSequenceNumber); |
6727 | 26.2k | unsigned char ivOut[MAX_IV_LENGTH]; |
6728 | | |
6729 | 26.2k | unsigned int aadLen; |
6730 | 26.2k | unsigned int len; |
6731 | | |
6732 | 26.2k | PORT_Assert(cipher_def->type == type_aead); |
6733 | | |
6734 | | /* If the following condition holds, we can skip the padding logic for |
6735 | | * DTLS 1.3 (4.2.3). This will be the case until we support a cipher |
6736 | | * with tag length < 15B. */ |
6737 | 26.2k | PORT_Assert(tagLen + 1 /* cType */ >= 16); |
6738 | | |
6739 | | /* Add the content type at the end. */ |
6740 | 26.2k | *(SSL_BUFFER_NEXT(wrBuf) + contentLen) = type; |
6741 | | |
6742 | | /* Create the header (ugly that we have to do it twice). */ |
6743 | 26.2k | rv = ssl_InsertRecordHeader(ss, cwSpec, ssl_ct_application_data, |
6744 | 26.2k | &buf, &needsLength); |
6745 | 26.2k | if (rv != SECSuccess) { |
6746 | 0 | return SECFailure; |
6747 | 0 | } |
6748 | 26.2k | if (needsLength) { |
6749 | 26.2k | rv = sslBuffer_AppendNumber(&buf, contentLen + 1 + tagLen, 2); |
6750 | 26.2k | if (rv != SECSuccess) { |
6751 | 0 | return SECFailure; |
6752 | 0 | } |
6753 | 26.2k | } |
6754 | 26.2k | rv = tls13_FormatAdditionalData(ss, SSL_BUFFER_BASE(&buf), SSL_BUFFER_LEN(&buf), |
6755 | 26.2k | cwSpec->epoch, cwSpec->nextSeqNum, |
6756 | 26.2k | aad, &aadLen, sizeof(aad)); |
6757 | 26.2k | if (rv != SECSuccess) { |
6758 | 0 | return SECFailure; |
6759 | 0 | } |
6760 | | /* set up initial IV value */ |
6761 | 26.2k | ivOffset = tls13_SetupAeadIv(IS_DTLS(ss), cwSpec->version, ivOut, cwSpec->keyMaterial.iv, |
6762 | 26.2k | ivOffset, ivLen, cwSpec->epoch); |
6763 | 26.2k | rv = tls13_AEAD(cwSpec->cipherContext, PR_FALSE, |
6764 | 26.2k | CKG_GENERATE_COUNTER_XOR, ivOffset * BPB, |
6765 | 26.2k | ivOut, ivOut, ivLen, /* iv */ |
6766 | 26.2k | NULL, 0, /* nonce */ |
6767 | 26.2k | aad + sizeof(sslSequenceNumber), /* aad */ |
6768 | 26.2k | aadLen - sizeof(sslSequenceNumber), |
6769 | 26.2k | SSL_BUFFER_NEXT(wrBuf), /* output */ |
6770 | 26.2k | &len, /* out len */ |
6771 | 26.2k | SSL_BUFFER_SPACE(wrBuf), /* max out */ |
6772 | 26.2k | tagLen, |
6773 | 26.2k | SSL_BUFFER_NEXT(wrBuf), /* input */ |
6774 | 26.2k | contentLen + 1); /* input len */ |
6775 | 26.2k | if (rv != SECSuccess) { |
6776 | 0 | PORT_SetError(SSL_ERROR_ENCRYPTION_FAILURE); |
6777 | 0 | return SECFailure; |
6778 | 0 | } |
6779 | 26.2k | rv = sslBuffer_Skip(wrBuf, len, NULL); |
6780 | 26.2k | PORT_Assert(rv == SECSuccess); |
6781 | 26.2k | } |
6782 | | |
6783 | 56.9k | return SECSuccess; |
6784 | 56.9k | } |
6785 | | |
6786 | | /* Unprotect a TLS 1.3 record and leave the result in plaintext. |
6787 | | * |
6788 | | * Called by ssl3_HandleRecord. Caller must hold the spec read lock. |
6789 | | * Therefore, we MUST not call SSL3_SendAlert(). |
6790 | | * |
6791 | | * If SECFailure is returned, we: |
6792 | | * 1. Set |*alert| to the alert to be sent. |
6793 | | * 2. Call PORT_SetError() with an appropriate code. |
6794 | | */ |
6795 | | SECStatus |
6796 | | tls13_UnprotectRecord(sslSocket *ss, |
6797 | | ssl3CipherSpec *spec, |
6798 | | SSL3Ciphertext *cText, |
6799 | | sslBuffer *plaintext, |
6800 | | SSLContentType *innerType, |
6801 | | SSL3AlertDescription *alert) |
6802 | 18.0k | { |
6803 | 18.0k | const ssl3BulkCipherDef *cipher_def = spec->cipherDef; |
6804 | 18.0k | const int ivLen = cipher_def->iv_size + cipher_def->explicit_nonce_size; |
6805 | 18.0k | const int tagLen = cipher_def->tag_size; |
6806 | 18.0k | const int innerTypeLen = 1; |
6807 | | |
6808 | 18.0k | PRUint8 aad[21]; |
6809 | 18.0k | unsigned int aadLen; |
6810 | 18.0k | SECStatus rv; |
6811 | | |
6812 | 18.0k | *alert = bad_record_mac; /* Default alert for most issues. */ |
6813 | | |
6814 | 18.0k | PORT_Assert(spec->direction == ssl_secret_read); |
6815 | 18.0k | SSL_TRC(3, ("%d: TLS13[%d]: spec=%d epoch=%d (%s) unprotect 0x%0llx len=%u", |
6816 | 18.0k | SSL_GETPID(), ss->fd, spec, spec->epoch, spec->phase, |
6817 | 18.0k | cText->seqNum, cText->buf->len)); |
6818 | | |
6819 | | /* Verify that the outer content type is right. |
6820 | | * |
6821 | | * For the inner content type as well as lower TLS versions this is checked |
6822 | | * in ssl3con.c/ssl3_HandleNonApllicationData(). |
6823 | | * |
6824 | | * For DTLS 1.3 this is checked in ssl3gthr.c/dtls_GatherData(). DTLS drops |
6825 | | * invalid records silently [RFC6347, Section 4.1.2.7]. |
6826 | | * |
6827 | | * Also allow the DTLS short header in TLS 1.3. */ |
6828 | 18.0k | if (!(cText->hdr[0] == ssl_ct_application_data || |
6829 | 13.2k | (IS_DTLS(ss) && |
6830 | 11.4k | ss->version >= SSL_LIBRARY_VERSION_TLS_1_3 && |
6831 | 11.4k | (cText->hdr[0] & 0xe0) == 0x20))) { |
6832 | 3.15k | SSL_TRC(3, |
6833 | 3.15k | ("%d: TLS13[%d]: record has invalid exterior type=%2.2x", |
6834 | 3.15k | SSL_GETPID(), ss->fd, cText->hdr[0])); |
6835 | 3.15k | PORT_SetError(SSL_ERROR_RX_UNEXPECTED_RECORD_TYPE); |
6836 | 3.15k | *alert = unexpected_message; |
6837 | 3.15k | return SECFailure; |
6838 | 3.15k | } |
6839 | | |
6840 | | /* We can perform this test in variable time because the record's total |
6841 | | * length and the ciphersuite are both public knowledge. */ |
6842 | 14.8k | if (cText->buf->len < tagLen) { |
6843 | 272 | SSL_TRC(3, |
6844 | 272 | ("%d: TLS13[%d]: record too short to contain valid AEAD data", |
6845 | 272 | SSL_GETPID(), ss->fd)); |
6846 | 272 | PORT_SetError(SSL_ERROR_BAD_MAC_READ); |
6847 | 272 | return SECFailure; |
6848 | 272 | } |
6849 | | |
6850 | | /* Check if the ciphertext can be valid if we assume maximum plaintext and |
6851 | | * add the specific ciphersuite expansion. |
6852 | | * This way we detect overlong plaintexts/padding before decryption. |
6853 | | * This check enforces size limitations more strict than the RFC. |
6854 | | * (see RFC8446, Section 5.2) */ |
6855 | 14.5k | if (cText->buf->len > (spec->recordSizeLimit + innerTypeLen + tagLen)) { |
6856 | 155 | *alert = record_overflow; |
6857 | 155 | PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG); |
6858 | 155 | return SECFailure; |
6859 | 155 | } |
6860 | | |
6861 | | /* Check the version number in the record. Stream only. */ |
6862 | 14.4k | if (!IS_DTLS(ss)) { |
6863 | 4.40k | SSL3ProtocolVersion version = |
6864 | 4.40k | ((SSL3ProtocolVersion)cText->hdr[1] << 8) | |
6865 | 4.40k | (SSL3ProtocolVersion)cText->hdr[2]; |
6866 | 4.40k | if (version != spec->recordVersion) { |
6867 | | /* Do we need a better error here? */ |
6868 | 235 | SSL_TRC(3, ("%d: TLS13[%d]: record has bogus version", |
6869 | 235 | SSL_GETPID(), ss->fd)); |
6870 | 235 | return SECFailure; |
6871 | 235 | } |
6872 | 4.40k | } |
6873 | | |
6874 | | /* Decrypt */ |
6875 | 14.2k | PORT_Assert(cipher_def->type == type_aead); |
6876 | 14.2k | rv = tls13_FormatAdditionalData(ss, cText->hdr, cText->hdrLen, |
6877 | 14.2k | spec->epoch, cText->seqNum, |
6878 | 14.2k | aad, &aadLen, sizeof(aad)); |
6879 | 14.2k | if (rv != SECSuccess) { |
6880 | |
|
6881 | 0 | return SECFailure; |
6882 | 0 | } |
6883 | 14.2k | rv = tls13_AEAD(spec->cipherContext, PR_TRUE, |
6884 | 14.2k | CKG_NO_GENERATE, 0, /* ignored for decrypt */ |
6885 | 14.2k | spec->keyMaterial.iv, NULL, ivLen, /* iv */ |
6886 | 14.2k | aad, sizeof(sslSequenceNumber), /* nonce */ |
6887 | 14.2k | aad + sizeof(sslSequenceNumber), /* aad */ |
6888 | 14.2k | aadLen - sizeof(sslSequenceNumber), |
6889 | 14.2k | plaintext->buf, /* output */ |
6890 | 14.2k | &plaintext->len, /* outlen */ |
6891 | 14.2k | plaintext->space, /* maxout */ |
6892 | 14.2k | tagLen, |
6893 | 14.2k | cText->buf->buf, /* in */ |
6894 | 14.2k | cText->buf->len); /* inlen */ |
6895 | 14.2k | if (rv != SECSuccess) { |
6896 | 14.2k | if (IS_DTLS(ss)) { |
6897 | 10.0k | spec->deprotectionFailures++; |
6898 | 10.0k | } |
6899 | | |
6900 | 14.2k | SSL_TRC(3, |
6901 | 14.2k | ("%d: TLS13[%d]: record has bogus MAC", |
6902 | 14.2k | SSL_GETPID(), ss->fd)); |
6903 | 14.2k | PORT_SetError(SSL_ERROR_BAD_MAC_READ); |
6904 | 14.2k | return SECFailure; |
6905 | 14.2k | } |
6906 | | |
6907 | | /* There is a similar test in ssl3_HandleRecord, but this test is needed to |
6908 | | * account for padding. */ |
6909 | 0 | if (plaintext->len > spec->recordSizeLimit + innerTypeLen) { |
6910 | 0 | *alert = record_overflow; |
6911 | 0 | PORT_SetError(SSL_ERROR_RX_RECORD_TOO_LONG); |
6912 | 0 | return SECFailure; |
6913 | 0 | } |
6914 | | |
6915 | | /* The record is right-padded with 0s, followed by the true |
6916 | | * content type, so read from the right until we receive a |
6917 | | * nonzero byte. */ |
6918 | 0 | while (plaintext->len > 0 && !(plaintext->buf[plaintext->len - 1])) { |
6919 | 0 | --plaintext->len; |
6920 | 0 | } |
6921 | | |
6922 | | /* Bogus padding. */ |
6923 | 0 | if (plaintext->len < 1) { |
6924 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: empty record", SSL_GETPID(), ss->fd)); |
6925 | | /* It's safe to report this specifically because it happened |
6926 | | * after the MAC has been verified. */ |
6927 | 0 | *alert = unexpected_message; |
6928 | 0 | PORT_SetError(SSL_ERROR_BAD_BLOCK_PADDING); |
6929 | 0 | return SECFailure; |
6930 | 0 | } |
6931 | | |
6932 | | /* Record the type. */ |
6933 | 0 | *innerType = (SSLContentType)plaintext->buf[plaintext->len - 1]; |
6934 | 0 | --plaintext->len; |
6935 | | |
6936 | | /* Check for zero-length encrypted Alert and Handshake fragments |
6937 | | * (zero-length + inner content type byte). |
6938 | | * |
6939 | | * Implementations MUST NOT send Handshake and Alert records that have a |
6940 | | * zero-length TLSInnerPlaintext.content; if such a message is received, |
6941 | | * the receiving implementation MUST terminate the connection with an |
6942 | | * "unexpected_message" alert [RFC8446, Section 5.4]. */ |
6943 | 0 | if (!plaintext->len && ((!IS_DTLS(ss) && cText->hdr[0] == ssl_ct_application_data) || |
6944 | 0 | (IS_DTLS(ss) && dtls_IsDtls13Ciphertext(spec->version, cText->hdr[0])))) { |
6945 | 0 | switch (*innerType) { |
6946 | 0 | case ssl_ct_alert: |
6947 | 0 | *alert = unexpected_message; |
6948 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_ALERT); |
6949 | 0 | return SECFailure; |
6950 | 0 | case ssl_ct_handshake: |
6951 | 0 | *alert = unexpected_message; |
6952 | 0 | PORT_SetError(SSL_ERROR_RX_MALFORMED_HANDSHAKE); |
6953 | 0 | return SECFailure; |
6954 | 0 | default: |
6955 | 0 | break; |
6956 | 0 | } |
6957 | 0 | } |
6958 | | |
6959 | | /* Check that we haven't received too much 0-RTT data. */ |
6960 | 0 | if (spec->epoch == TrafficKeyEarlyApplicationData && |
6961 | 0 | *innerType == ssl_ct_application_data) { |
6962 | 0 | if (plaintext->len > spec->earlyDataRemaining) { |
6963 | 0 | *alert = unexpected_message; |
6964 | 0 | PORT_SetError(SSL_ERROR_TOO_MUCH_EARLY_DATA); |
6965 | 0 | return SECFailure; |
6966 | 0 | } |
6967 | 0 | spec->earlyDataRemaining -= plaintext->len; |
6968 | 0 | } |
6969 | | |
6970 | 0 | SSL_TRC(10, |
6971 | 0 | ("%d: TLS13[%d]: %s received record of length=%d, type=%d", |
6972 | 0 | SSL_GETPID(), ss->fd, SSL_ROLE(ss), plaintext->len, *innerType)); |
6973 | |
|
6974 | 0 | return SECSuccess; |
6975 | 0 | } |
6976 | | |
6977 | | /* 0-RTT is only permitted if: |
6978 | | * |
6979 | | * 1. We are doing TLS 1.3 |
6980 | | * 2. This isn't a second ClientHello (in response to HelloRetryRequest) |
6981 | | * 3. The 0-RTT option is set. |
6982 | | * 4. We have a valid ticket or an External PSK. |
6983 | | * 5. If resuming: |
6984 | | * 5a. The server is willing to accept 0-RTT. |
6985 | | * 5b. We have not changed our ALPN settings to disallow the ALPN tag |
6986 | | * in the ticket. |
6987 | | * |
6988 | | * Called from tls13_ClientSendEarlyDataXtn(). |
6989 | | */ |
6990 | | PRBool |
6991 | | tls13_ClientAllow0Rtt(const sslSocket *ss, const sslSessionID *sid) |
6992 | 77.1k | { |
6993 | | /* We checked that the cipher suite was still allowed back in |
6994 | | * ssl3_SendClientHello. */ |
6995 | 77.1k | if (sid->version < SSL_LIBRARY_VERSION_TLS_1_3) { |
6996 | 60.0k | return PR_FALSE; |
6997 | 60.0k | } |
6998 | 17.1k | if (ss->ssl3.hs.helloRetry) { |
6999 | 2.98k | return PR_FALSE; |
7000 | 2.98k | } |
7001 | 14.1k | if (!ss->opt.enable0RttData) { |
7002 | 7.39k | return PR_FALSE; |
7003 | 7.39k | } |
7004 | 6.73k | if (PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks)) { |
7005 | 3.11k | return PR_FALSE; |
7006 | 3.11k | } |
7007 | 3.61k | sslPsk *psk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks); |
7008 | | |
7009 | 3.61k | if (psk->zeroRttSuite == TLS_NULL_WITH_NULL_NULL) { |
7010 | 3.61k | return PR_FALSE; |
7011 | 3.61k | } |
7012 | 0 | if (!psk->maxEarlyData) { |
7013 | 0 | return PR_FALSE; |
7014 | 0 | } |
7015 | | |
7016 | 0 | if (psk->type == ssl_psk_external) { |
7017 | 0 | return psk->hash == tls13_GetHashForCipherSuite(psk->zeroRttSuite); |
7018 | 0 | } |
7019 | 0 | if (psk->type == ssl_psk_resume) { |
7020 | 0 | if (!ss->statelessResume) |
7021 | 0 | return PR_FALSE; |
7022 | 0 | if ((sid->u.ssl3.locked.sessionTicket.flags & ticket_allow_early_data) == 0) |
7023 | 0 | return PR_FALSE; |
7024 | 0 | return ssl_AlpnTagAllowed(ss, &sid->u.ssl3.alpnSelection); |
7025 | 0 | } |
7026 | 0 | PORT_Assert(0); |
7027 | 0 | return PR_FALSE; |
7028 | 0 | } |
7029 | | |
7030 | | SECStatus |
7031 | | tls13_MaybeDo0RTTHandshake(sslSocket *ss) |
7032 | 17.1k | { |
7033 | 17.1k | SECStatus rv; |
7034 | | |
7035 | | /* Don't do anything if there is no early_data xtn, which means we're |
7036 | | * not doing early data. */ |
7037 | 17.1k | if (!ssl3_ExtensionAdvertised(ss, ssl_tls13_early_data_xtn)) { |
7038 | 17.1k | return SECSuccess; |
7039 | 17.1k | } |
7040 | | |
7041 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_sent; |
7042 | 0 | ss->ssl3.hs.zeroRttSuite = ss->ssl3.hs.cipher_suite; |
7043 | | /* Note: Reset the preliminary info here rather than just add 0-RTT. We are |
7044 | | * only guessing what might happen at this point.*/ |
7045 | 0 | ss->ssl3.hs.preliminaryInfo = ssl_preinfo_0rtt_cipher_suite; |
7046 | |
|
7047 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: in 0-RTT mode", SSL_GETPID(), ss->fd)); |
7048 | | |
7049 | | /* Set the ALPN data as if it was negotiated. We check in the ServerHello |
7050 | | * handler that the server negotiates the same value. */ |
7051 | 0 | if (ss->sec.ci.sid->u.ssl3.alpnSelection.len) { |
7052 | 0 | ss->xtnData.nextProtoState = SSL_NEXT_PROTO_EARLY_VALUE; |
7053 | 0 | rv = SECITEM_CopyItem(NULL, &ss->xtnData.nextProto, |
7054 | 0 | &ss->sec.ci.sid->u.ssl3.alpnSelection); |
7055 | 0 | if (rv != SECSuccess) { |
7056 | 0 | return SECFailure; |
7057 | 0 | } |
7058 | 0 | } |
7059 | | |
7060 | 0 | if (ss->opt.enableTls13CompatMode && !IS_DTLS(ss)) { |
7061 | | /* Pretend that this is a proper ChangeCipherSpec even though it is sent |
7062 | | * before receiving the ServerHello. */ |
7063 | 0 | ssl_GetSpecWriteLock(ss); |
7064 | 0 | tls13_SetSpecRecordVersion(ss, ss->ssl3.cwSpec); |
7065 | 0 | ssl_ReleaseSpecWriteLock(ss); |
7066 | 0 | ssl_GetXmitBufLock(ss); |
7067 | 0 | rv = ssl3_SendChangeCipherSpecsInt(ss); |
7068 | 0 | ssl_ReleaseXmitBufLock(ss); |
7069 | 0 | if (rv != SECSuccess) { |
7070 | 0 | return SECFailure; |
7071 | 0 | } |
7072 | 0 | } |
7073 | | |
7074 | | /* If we have any message that was saved for later hashing. |
7075 | | * The updated hash is then used in tls13_DeriveEarlySecrets. */ |
7076 | 0 | rv = ssl3_MaybeUpdateHashWithSavedRecord(ss); |
7077 | 0 | if (rv != SECSuccess) { |
7078 | 0 | return SECFailure; |
7079 | 0 | } |
7080 | | |
7081 | | /* If we're trying 0-RTT, derive from the first PSK */ |
7082 | 0 | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.psks) && !ss->xtnData.selectedPsk); |
7083 | 0 | ss->xtnData.selectedPsk = (sslPsk *)PR_LIST_HEAD(&ss->ssl3.hs.psks); |
7084 | 0 | rv = tls13_DeriveEarlySecrets(ss); |
7085 | 0 | if (rv != SECSuccess) { |
7086 | 0 | return SECFailure; |
7087 | 0 | } |
7088 | | |
7089 | | /* Save cwSpec in case we get a HelloRetryRequest and have to send another |
7090 | | * ClientHello. */ |
7091 | 0 | ssl_CipherSpecAddRef(ss->ssl3.cwSpec); |
7092 | |
|
7093 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyEarlyApplicationData, |
7094 | 0 | ssl_secret_write, PR_TRUE); |
7095 | 0 | ss->xtnData.selectedPsk = NULL; |
7096 | 0 | if (rv != SECSuccess) { |
7097 | 0 | return SECFailure; |
7098 | 0 | } |
7099 | | |
7100 | 0 | return SECSuccess; |
7101 | 0 | } |
7102 | | |
7103 | | PRInt32 |
7104 | | tls13_Read0RttData(sslSocket *ss, PRUint8 *buf, PRInt32 len) |
7105 | 0 | { |
7106 | 0 | PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.bufferedEarlyData)); |
7107 | 0 | PRInt32 offset = 0; |
7108 | 0 | while (!PR_CLIST_IS_EMPTY(&ss->ssl3.hs.bufferedEarlyData)) { |
7109 | 0 | TLS13EarlyData *msg = |
7110 | 0 | (TLS13EarlyData *)PR_NEXT_LINK(&ss->ssl3.hs.bufferedEarlyData); |
7111 | 0 | unsigned int tocpy = msg->data.len - msg->consumed; |
7112 | |
|
7113 | 0 | if (tocpy > (len - offset)) { |
7114 | 0 | if (IS_DTLS(ss)) { |
7115 | | /* In DTLS, we only return entire records. |
7116 | | * So offset and consumed are always zero. */ |
7117 | 0 | PORT_Assert(offset == 0); |
7118 | 0 | PORT_Assert(msg->consumed == 0); |
7119 | 0 | PORT_SetError(SSL_ERROR_RX_SHORT_DTLS_READ); |
7120 | 0 | return -1; |
7121 | 0 | } |
7122 | | |
7123 | 0 | tocpy = len - offset; |
7124 | 0 | } |
7125 | | |
7126 | 0 | PORT_Memcpy(buf + offset, msg->data.data + msg->consumed, tocpy); |
7127 | 0 | offset += tocpy; |
7128 | 0 | msg->consumed += tocpy; |
7129 | |
|
7130 | 0 | if (msg->consumed == msg->data.len) { |
7131 | 0 | PR_REMOVE_LINK(&msg->link); |
7132 | 0 | SECITEM_ZfreeItem(&msg->data, PR_FALSE); |
7133 | 0 | PORT_ZFree(msg, sizeof(*msg)); |
7134 | 0 | } |
7135 | | |
7136 | | /* We are done after one record for DTLS; otherwise, when the buffer fills up. */ |
7137 | 0 | if (IS_DTLS(ss) || offset == len) { |
7138 | 0 | break; |
7139 | 0 | } |
7140 | 0 | } |
7141 | | |
7142 | 0 | return offset; |
7143 | 0 | } |
7144 | | |
7145 | | static SECStatus |
7146 | | tls13_SendEndOfEarlyData(sslSocket *ss) |
7147 | 0 | { |
7148 | 0 | SECStatus rv; |
7149 | |
|
7150 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveXmitBufLock(ss)); |
7151 | |
|
7152 | 0 | if (!ss->opt.suppressEndOfEarlyData) { |
7153 | 0 | SSL_TRC(3, ("%d: TLS13[%d]: send EndOfEarlyData", SSL_GETPID(), ss->fd)); |
7154 | 0 | rv = ssl3_AppendHandshakeHeader(ss, ssl_hs_end_of_early_data, 0); |
7155 | 0 | if (rv != SECSuccess) { |
7156 | 0 | return rv; /* err set by AppendHandshake. */ |
7157 | 0 | } |
7158 | 0 | } |
7159 | | |
7160 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_done; |
7161 | 0 | return SECSuccess; |
7162 | 0 | } |
7163 | | |
7164 | | static SECStatus |
7165 | | tls13_HandleEndOfEarlyData(sslSocket *ss, const PRUint8 *b, PRUint32 length) |
7166 | 10 | { |
7167 | 10 | SECStatus rv; |
7168 | | |
7169 | 10 | PORT_Assert(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
7170 | | |
7171 | 10 | rv = TLS13_CHECK_HS_STATE(ss, SSL_ERROR_RX_UNEXPECTED_END_OF_EARLY_DATA, |
7172 | 10 | wait_end_of_early_data); |
7173 | 10 | if (rv != SECSuccess) { |
7174 | 10 | return SECFailure; |
7175 | 10 | } |
7176 | | |
7177 | | /* We shouldn't be getting any more early data, and if we do, |
7178 | | * it is because of reordering and we drop it. */ |
7179 | 0 | if (IS_DTLS(ss)) { |
7180 | 0 | ssl_CipherSpecReleaseByEpoch(ss, ssl_secret_read, |
7181 | 0 | TrafficKeyEarlyApplicationData); |
7182 | 0 | dtls_ReceivedFirstMessageInFlight(ss); |
7183 | 0 | } |
7184 | |
|
7185 | 0 | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted); |
7186 | |
|
7187 | 0 | if (length) { |
7188 | 0 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_END_OF_EARLY_DATA, decode_error); |
7189 | 0 | return SECFailure; |
7190 | 0 | } |
7191 | | |
7192 | 0 | rv = tls13_SetCipherSpec(ss, TrafficKeyHandshake, |
7193 | 0 | ssl_secret_read, PR_FALSE); |
7194 | 0 | if (rv != SECSuccess) { |
7195 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
7196 | 0 | return SECFailure; |
7197 | 0 | } |
7198 | | |
7199 | 0 | ss->ssl3.hs.zeroRttState = ssl_0rtt_done; |
7200 | 0 | if (tls13_ShouldRequestClientAuth(ss)) { |
7201 | 0 | TLS13_SET_HS_STATE(ss, wait_client_cert); |
7202 | 0 | } else { |
7203 | 0 | TLS13_SET_HS_STATE(ss, wait_finished); |
7204 | 0 | } |
7205 | 0 | return SECSuccess; |
7206 | 0 | } |
7207 | | |
7208 | | static SECStatus |
7209 | | tls13_MaybeHandleSuppressedEndOfEarlyData(sslSocket *ss) |
7210 | 686 | { |
7211 | 686 | PORT_Assert(ss->sec.isServer); |
7212 | 686 | if (!ss->opt.suppressEndOfEarlyData || |
7213 | 686 | ss->ssl3.hs.zeroRttState != ssl_0rtt_accepted) { |
7214 | 686 | return SECSuccess; |
7215 | 686 | } |
7216 | | |
7217 | 0 | return tls13_HandleEndOfEarlyData(ss, NULL, 0); |
7218 | 686 | } |
7219 | | |
7220 | | SECStatus |
7221 | | tls13_HandleEarlyApplicationData(sslSocket *ss, sslBuffer *origBuf) |
7222 | 0 | { |
7223 | 0 | TLS13EarlyData *ed; |
7224 | 0 | SECItem it = { siBuffer, NULL, 0 }; |
7225 | |
|
7226 | 0 | PORT_Assert(ss->sec.isServer); |
7227 | 0 | PORT_Assert(ss->ssl3.hs.zeroRttState == ssl_0rtt_accepted); |
7228 | 0 | if (ss->ssl3.hs.zeroRttState != ssl_0rtt_accepted) { |
7229 | | /* Belt and suspenders. */ |
7230 | 0 | FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error); |
7231 | 0 | return SECFailure; |
7232 | 0 | } |
7233 | | |
7234 | 0 | PRINT_BUF(3, (NULL, "Received early application data", |
7235 | 0 | origBuf->buf, origBuf->len)); |
7236 | 0 | ed = PORT_ZNew(TLS13EarlyData); |
7237 | 0 | if (!ed) { |
7238 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
7239 | 0 | return SECFailure; |
7240 | 0 | } |
7241 | 0 | it.data = origBuf->buf; |
7242 | 0 | it.len = origBuf->len; |
7243 | 0 | if (SECITEM_CopyItem(NULL, &ed->data, &it) != SECSuccess) { |
7244 | 0 | FATAL_ERROR(ss, SEC_ERROR_NO_MEMORY, internal_error); |
7245 | 0 | return SECFailure; |
7246 | 0 | } |
7247 | 0 | PR_APPEND_LINK(&ed->link, &ss->ssl3.hs.bufferedEarlyData); |
7248 | |
|
7249 | 0 | origBuf->len = 0; /* So ssl3_GatherAppDataRecord will keep looping. */ |
7250 | |
|
7251 | 0 | return SECSuccess; |
7252 | 0 | } |
7253 | | |
7254 | | PRUint16 |
7255 | | tls13_EncodeVersion(SSL3ProtocolVersion version, SSLProtocolVariant variant) |
7256 | 68.7k | { |
7257 | 68.7k | if (variant == ssl_variant_datagram) { |
7258 | 31.1k | return dtls_TLSVersionToDTLSVersion(version); |
7259 | 31.1k | } |
7260 | | /* Stream-variant encodings do not change. */ |
7261 | 37.5k | return (PRUint16)version; |
7262 | 68.7k | } |
7263 | | |
7264 | | SECStatus |
7265 | | tls13_ClientReadSupportedVersion(sslSocket *ss) |
7266 | 70.0k | { |
7267 | 70.0k | PRUint32 temp; |
7268 | 70.0k | TLSExtension *versionExtension; |
7269 | 70.0k | SECItem it; |
7270 | 70.0k | SECStatus rv; |
7271 | | |
7272 | | /* Update the version based on the extension, as necessary. */ |
7273 | 70.0k | versionExtension = ssl3_FindExtension(ss, ssl_tls13_supported_versions_xtn); |
7274 | 70.0k | if (!versionExtension) { |
7275 | 64.1k | return SECSuccess; |
7276 | 64.1k | } |
7277 | | |
7278 | | /* Struct copy so we don't damage the extension. */ |
7279 | 5.86k | it = versionExtension->data; |
7280 | | |
7281 | 5.86k | rv = ssl3_ConsumeHandshakeNumber(ss, &temp, 2, &it.data, &it.len); |
7282 | 5.86k | if (rv != SECSuccess) { |
7283 | 8 | return SECFailure; |
7284 | 8 | } |
7285 | 5.85k | if (it.len) { |
7286 | 6 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_SERVER_HELLO, illegal_parameter); |
7287 | 6 | return SECFailure; |
7288 | 6 | } |
7289 | | |
7290 | 5.85k | if (temp != tls13_EncodeVersion(SSL_LIBRARY_VERSION_TLS_1_3, |
7291 | 5.85k | ss->protocolVariant)) { |
7292 | | /* You cannot negotiate < TLS 1.3 with supported_versions. */ |
7293 | 72 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_SERVER_HELLO, illegal_parameter); |
7294 | 72 | return SECFailure; |
7295 | 72 | } |
7296 | | |
7297 | | /* Any endpoint receiving a Hello message with...ServerHello.legacy_version |
7298 | | * set to 0x0300 (SSL3) MUST abort the handshake with a "protocol_version" |
7299 | | * alert. [RFC8446, Section D.5] |
7300 | | * |
7301 | | * The ServerHello.legacy_version is read into the ss->version field by |
7302 | | * ssl_ClientReadVersion(). */ |
7303 | 5.77k | if (ss->version == SSL_LIBRARY_VERSION_3_0) { |
7304 | 2 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_SERVER_HELLO, protocol_version); |
7305 | 2 | return SECFailure; |
7306 | 2 | } |
7307 | | |
7308 | 5.77k | ss->version = SSL_LIBRARY_VERSION_TLS_1_3; |
7309 | 5.77k | return SECSuccess; |
7310 | 5.77k | } |
7311 | | |
7312 | | /* Pick the highest version we support that is also advertised. */ |
7313 | | SECStatus |
7314 | | tls13_NegotiateVersion(sslSocket *ss, const TLSExtension *supportedVersions) |
7315 | 6.08k | { |
7316 | 6.08k | PRUint16 version; |
7317 | | /* Make a copy so we're nondestructive. */ |
7318 | 6.08k | SECItem data = supportedVersions->data; |
7319 | 6.08k | SECItem versions; |
7320 | 6.08k | SECStatus rv; |
7321 | | |
7322 | 6.08k | rv = ssl3_ConsumeHandshakeVariable(ss, &versions, 1, |
7323 | 6.08k | &data.data, &data.len); |
7324 | 6.08k | if (rv != SECSuccess) { |
7325 | 7 | return SECFailure; |
7326 | 7 | } |
7327 | 6.07k | if (data.len || !versions.len || (versions.len & 1)) { |
7328 | 18 | FATAL_ERROR(ss, SSL_ERROR_RX_MALFORMED_CLIENT_HELLO, illegal_parameter); |
7329 | 18 | return SECFailure; |
7330 | 18 | } |
7331 | 6.58k | for (version = ss->vrange.max; version >= ss->vrange.min; --version) { |
7332 | 6.53k | if (version < SSL_LIBRARY_VERSION_TLS_1_3 && |
7333 | 675 | (ss->ssl3.hs.helloRetry || ss->ssl3.hs.echAccepted)) { |
7334 | | /* Prevent negotiating to a lower version after 1.3 HRR or ECH |
7335 | | * When accepting ECH, a different alert is generated. |
7336 | | */ |
7337 | 5 | SSL3AlertDescription alert = ss->ssl3.hs.echAccepted ? illegal_parameter : protocol_version; |
7338 | 5 | PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION); |
7339 | 5 | FATAL_ERROR(ss, SSL_ERROR_UNSUPPORTED_VERSION, alert); |
7340 | 5 | return SECFailure; |
7341 | 5 | } |
7342 | | |
7343 | 6.53k | PRUint16 wire = tls13_EncodeVersion(version, ss->protocolVariant); |
7344 | 6.53k | unsigned long offset; |
7345 | | |
7346 | 16.2k | for (offset = 0; offset < versions.len; offset += 2) { |
7347 | 15.7k | PRUint16 supported = |
7348 | 15.7k | (versions.data[offset] << 8) | versions.data[offset + 1]; |
7349 | 15.7k | if (supported == wire) { |
7350 | 6.00k | ss->version = version; |
7351 | 6.00k | return SECSuccess; |
7352 | 6.00k | } |
7353 | 15.7k | } |
7354 | 6.53k | } |
7355 | | |
7356 | 50 | FATAL_ERROR(ss, SSL_ERROR_UNSUPPORTED_VERSION, protocol_version); |
7357 | 50 | return SECFailure; |
7358 | 6.05k | } |
7359 | | |
7360 | | /* This is TLS 1.3 or might negotiate to it. */ |
7361 | | PRBool |
7362 | | tls13_MaybeTls13(sslSocket *ss) |
7363 | 326k | { |
7364 | 326k | if (ss->version >= SSL_LIBRARY_VERSION_TLS_1_3) { |
7365 | 108k | return PR_TRUE; |
7366 | 108k | } |
7367 | | |
7368 | 217k | if (ss->vrange.max < SSL_LIBRARY_VERSION_TLS_1_3) { |
7369 | 140k | return PR_FALSE; |
7370 | 140k | } |
7371 | | |
7372 | 77.3k | if (!(ss->ssl3.hs.preliminaryInfo & ssl_preinfo_version)) { |
7373 | 46.3k | return PR_TRUE; |
7374 | 46.3k | } |
7375 | | |
7376 | 30.9k | return PR_FALSE; |
7377 | 77.3k | } |
7378 | | |
7379 | | /* Setup random client GREASE values according to RFC8701. State must be kept |
7380 | | * so an equal ClientHello might be send on HelloRetryRequest. */ |
7381 | | SECStatus |
7382 | | tls13_ClientGreaseSetup(sslSocket *ss) |
7383 | 13.7k | { |
7384 | 13.7k | if (!ss->opt.enableGrease) { |
7385 | 6.77k | return SECSuccess; |
7386 | 6.77k | } |
7387 | | |
7388 | 6.99k | PORT_Assert(ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3); |
7389 | | |
7390 | 6.99k | if (ss->ssl3.hs.grease) { |
7391 | 0 | return SECFailure; |
7392 | 0 | } |
7393 | 6.99k | ss->ssl3.hs.grease = PORT_Alloc(sizeof(tls13ClientGrease)); |
7394 | 6.99k | if (!ss->ssl3.hs.grease) { |
7395 | 0 | return SECFailure; |
7396 | 0 | } |
7397 | | |
7398 | 6.99k | tls13ClientGrease *grease = ss->ssl3.hs.grease; |
7399 | | /* We require eight GREASE values and randoms. */ |
7400 | 6.99k | PRUint8 random[8]; |
7401 | | |
7402 | | /* Generate random GREASE values. */ |
7403 | 6.99k | if (PK11_GenerateRandom(random, sizeof(random)) != SECSuccess) { |
7404 | 0 | return SECFailure; |
7405 | 0 | } |
7406 | 55.9k | for (size_t i = 0; i < PR_ARRAY_SIZE(grease->idx); i++) { |
7407 | 48.9k | random[i] = ((random[i] & 0xf0) | 0x0a); |
7408 | 48.9k | grease->idx[i] = ((random[i] << 8) | random[i]); |
7409 | 48.9k | } |
7410 | | /* Specific PskKeyExchangeMode GREASE value. */ |
7411 | 6.99k | grease->pskKem = 0x0b + ((random[8 - 1] >> 5) * 0x1f); |
7412 | | |
7413 | | /* Duplicate extensions are not allowed. */ |
7414 | 6.99k | if (grease->idx[grease_extension1] == grease->idx[grease_extension2]) { |
7415 | 0 | grease->idx[grease_extension2] ^= 0x1010; |
7416 | 0 | } |
7417 | | |
7418 | 6.99k | return SECSuccess; |
7419 | 6.99k | } |
7420 | | |
7421 | | /* Destroy client GREASE state. */ |
7422 | | void |
7423 | | tls13_ClientGreaseDestroy(sslSocket *ss) |
7424 | 128k | { |
7425 | 128k | if (ss->ssl3.hs.grease) { |
7426 | 6.99k | PORT_Free(ss->ssl3.hs.grease); |
7427 | 6.99k | ss->ssl3.hs.grease = NULL; |
7428 | 6.99k | } |
7429 | 128k | } |
7430 | | |
7431 | | /* Generate a random GREASE value according to RFC8701. |
7432 | | * This function does not provide valid PskKeyExchangeMode GREASE values! */ |
7433 | | SECStatus |
7434 | | tls13_RandomGreaseValue(PRUint16 *out) |
7435 | 1.51k | { |
7436 | 1.51k | PRUint8 random; |
7437 | | |
7438 | 1.51k | if (PK11_GenerateRandom(&random, sizeof(random)) != SECSuccess) { |
7439 | 0 | return SECFailure; |
7440 | 0 | } |
7441 | | |
7442 | 1.51k | random = ((random & 0xf0) | 0x0a); |
7443 | 1.51k | *out = ((random << 8) | random); |
7444 | | |
7445 | 1.51k | return SECSuccess; |
7446 | 1.51k | } |
7447 | | |
7448 | | /* Set TLS 1.3 GREASE Extension random GREASE type. */ |
7449 | | SECStatus |
7450 | | tls13_MaybeGreaseExtensionType(const sslSocket *ss, |
7451 | | const SSLHandshakeType message, |
7452 | | PRUint16 *exType) |
7453 | 712k | { |
7454 | 712k | if (*exType != ssl_tls13_grease_xtn) { |
7455 | 694k | return SECSuccess; |
7456 | 694k | } |
7457 | | |
7458 | 18.1k | PR_ASSERT(ss->opt.enableGrease); |
7459 | 18.1k | PR_ASSERT(message == ssl_hs_client_hello || |
7460 | 18.1k | message == ssl_hs_certificate_request); |
7461 | | |
7462 | | /* GREASE ClientHello: |
7463 | | * A client MAY select one or more GREASE extension values and |
7464 | | * advertise them as extensions with varying length and contents |
7465 | | * [RFC8701, Section 3.1]. */ |
7466 | 18.1k | if (message == ssl_hs_client_hello) { |
7467 | 17.4k | PR_ASSERT(ss->vrange.max >= SSL_LIBRARY_VERSION_TLS_1_3); |
7468 | | /* Check if the first GREASE extension was already added. */ |
7469 | 17.4k | if (!ssl3_ExtensionAdvertised(ss, ss->ssl3.hs.grease->idx[grease_extension1])) { |
7470 | 8.72k | *exType = ss->ssl3.hs.grease->idx[grease_extension1]; |
7471 | 8.72k | } else { |
7472 | 8.72k | *exType = ss->ssl3.hs.grease->idx[grease_extension2]; |
7473 | 8.72k | } |
7474 | 17.4k | } |
7475 | | /* GREASE CertificateRequest: |
7476 | | * When sending a CertificateRequest in TLS 1.3, a server MAY behave as |
7477 | | * follows: A server MAY select one or more GREASE extension values and |
7478 | | * advertise them as extensions with varying length and contents |
7479 | | * [RFC8701, Section 4.1]. */ |
7480 | 651 | else if (message == ssl_hs_certificate_request) { |
7481 | 651 | PR_ASSERT(ss->version >= SSL_LIBRARY_VERSION_TLS_1_3); |
7482 | | /* Get random grease extension type. */ |
7483 | 651 | SECStatus rv = tls13_RandomGreaseValue(exType); |
7484 | 651 | if (rv != SECSuccess) { |
7485 | 0 | return SECFailure; |
7486 | 0 | } |
7487 | 651 | } |
7488 | | |
7489 | 18.1k | return SECSuccess; |
7490 | 18.1k | } |