/src/wolfssl-fastmath/src/tls13.c
Line | Count | Source |
1 | | /* tls13.c |
2 | | * |
3 | | * Copyright (C) 2006-2025 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
23 | | |
24 | | /* |
25 | | * BUILD_GCM |
26 | | * Enables AES-GCM ciphersuites. |
27 | | * HAVE_AESCCM |
28 | | * Enables AES-CCM ciphersuites. |
29 | | * HAVE_SESSION_TICKET |
30 | | * Enables session tickets - required for TLS 1.3 resumption. |
31 | | * NO_PSK |
32 | | * Do not enable Pre-Shared Keys. |
33 | | * HAVE_KEYING_MATERIAL |
34 | | * Enables exporting keying material based on section 7.5 of RFC 8446. |
35 | | * WOLFSSL_ASYNC_CRYPT |
36 | | * Enables the use of asynchronous cryptographic operations. |
37 | | * This is available for ciphers and certificates. |
38 | | * HAVE_CHACHA && HAVE_POLY1305 |
39 | | * Enables use of CHACHA20-POLY1305 ciphersuites. |
40 | | * WOLFSSL_DEBUG_TLS |
41 | | * Writes out details of TLS 1.3 protocol including handshake message buffers |
42 | | * and key generation input and output. |
43 | | * WOLFSSL_EARLY_DATA |
44 | | * Allow 0-RTT Handshake using Early Data extensions and handshake message |
45 | | * WOLFSSL_EARLY_DATA_GROUP |
46 | | * Group EarlyData message with ClientHello when sending |
47 | | * WOLFSSL_NO_SERVER_GROUPS_EXT |
48 | | * Do not send the server's groups in an extension when the server's top |
49 | | * preference is not in client's list. |
50 | | * WOLFSSL_POST_HANDSHAKE_AUTH |
51 | | * Allow TLS v1.3 code to perform post-handshake authentication of the |
52 | | * client. |
53 | | * WOLFSSL_SEND_HRR_COOKIE |
54 | | * Send a cookie in hello_retry_request message to enable stateless tracking |
55 | | * of ClientHello replies. |
56 | | * WOLFSSL_TLS13 |
57 | | * Enable TLS 1.3 protocol implementation. |
58 | | * WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
59 | | * Enable middlebox compatibility in the TLS 1.3 handshake. |
60 | | * This includes sending ChangeCipherSpec before encrypted messages and |
61 | | * including a session id. |
62 | | * WOLFSSL_TLS13_SHA512 |
63 | | * Allow generation of SHA-512 digests in handshake - no ciphersuite |
64 | | * requires SHA-512 at this time. |
65 | | * WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
66 | | * Allow a NewSessionTicket message to be sent by server before Client's |
67 | | * Finished message. |
68 | | * See TLS v1.3 specification, Section 4.6.1, Paragraph 4 (Note). |
69 | | * WOLFSSL_PSK_ONE_ID |
70 | | * When only one PSK ID is used and only one call to the PSK callback can |
71 | | * be made per connect. |
72 | | * You cannot use wc_psk_client_cs_callback type callback on client. |
73 | | * WOLFSSL_PRIORITIZE_PSK |
74 | | * During a handshake, prioritize PSK order instead of ciphersuite order. |
75 | | * WOLFSSL_CHECK_ALERT_ON_ERR |
76 | | * Check for alerts during the handshake in the event of an error. |
77 | | * WOLFSSL_NO_CLIENT_CERT_ERROR |
78 | | * Requires client to set a client certificate |
79 | | * WOLFSSL_PSK_MULTI_ID_PER_CS |
80 | | * When multiple PSK identities are available for the same cipher suite. |
81 | | * Sets the first byte of the client identity to the count of identities |
82 | | * that have been seen so far for the cipher suite. |
83 | | * WOLFSSL_CHECK_SIG_FAULTS |
84 | | * Verifies the ECC signature after signing in case of faults in the |
85 | | * calculation of the signature. Useful when signature fault injection is a |
86 | | * possible attack. |
87 | | * WOLFSSL_32BIT_MILLI_TIME |
88 | | * Function TimeNowInMilliseconds() returns an unsigned 32-bit value. |
89 | | * Default behavior is to return a signed 64-bit value. |
90 | | */ |
91 | | |
92 | | #if !defined(NO_TLS) && defined(WOLFSSL_TLS13) |
93 | | |
94 | | #ifndef WOLFCRYPT_ONLY |
95 | | |
96 | | #ifdef HAVE_ERRNO_H |
97 | | #include <errno.h> |
98 | | #endif |
99 | | |
100 | | #if defined(__MACH__) || defined(__FreeBSD__) || \ |
101 | | defined(__INCLUDE_NUTTX_CONFIG_H) || defined(WOLFSSL_RIOT_OS) |
102 | | #include <sys/time.h> |
103 | | #endif /* __MACH__ || __FreeBSD__ || |
104 | | __INCLUDE_NUTTX_CONFIG_H || WOLFSSL_RIOT_OS */ |
105 | | |
106 | | |
107 | | #include <wolfssl/internal.h> |
108 | | #include <wolfssl/error-ssl.h> |
109 | | #include <wolfssl/wolfcrypt/asn.h> |
110 | | #include <wolfssl/wolfcrypt/dh.h> |
111 | | #include <wolfssl/wolfcrypt/kdf.h> |
112 | | #include <wolfssl/wolfcrypt/signature.h> |
113 | | #ifdef NO_INLINE |
114 | | #include <wolfssl/wolfcrypt/misc.h> |
115 | | #else |
116 | | #define WOLFSSL_MISC_INCLUDED |
117 | | #include <wolfcrypt/src/misc.c> |
118 | | #endif |
119 | | |
120 | | #ifdef __sun |
121 | | #include <sys/filio.h> |
122 | | #endif |
123 | | |
124 | | #ifndef TRUE |
125 | | #define TRUE 1 |
126 | | #endif |
127 | | #ifndef FALSE |
128 | | #define FALSE 0 |
129 | | #endif |
130 | | |
131 | | #ifndef HAVE_AEAD |
132 | | #ifndef _MSC_VER |
133 | | #error "The build option HAVE_AEAD is required for TLS 1.3" |
134 | | #else |
135 | | #pragma \ |
136 | | message("error: The build option HAVE_AEAD is required for TLS 1.3") |
137 | | #endif |
138 | | #endif |
139 | | |
140 | | #ifndef HAVE_HKDF |
141 | | #ifndef _MSC_VER |
142 | | #error "The build option HAVE_HKDF is required for TLS 1.3" |
143 | | #else |
144 | | #pragma message("error: The build option HAVE_HKDF is required for TLS 1.3") |
145 | | #endif |
146 | | #endif |
147 | | |
148 | | #ifndef HAVE_TLS_EXTENSIONS |
149 | | #ifndef _MSC_VER |
150 | | #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3" |
151 | | #else |
152 | | #pragma message("error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3") |
153 | | #endif |
154 | | #endif |
155 | | |
156 | | |
157 | | /* Set ret to error value and jump to label. |
158 | | * |
159 | | * err The error value to set. |
160 | | * eLabel The label to jump to. |
161 | | */ |
162 | 0 | #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } |
163 | | |
164 | | /* Size of the TLS v1.3 label use when deriving keys. */ |
165 | 0 | #define TLS13_PROTOCOL_LABEL_SZ 6 |
166 | | /* The protocol label for TLS v1.3. */ |
167 | | static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 "; |
168 | | |
169 | | #ifdef WOLFSSL_DTLS13 |
170 | | #define DTLS13_PROTOCOL_LABEL_SZ 6 |
171 | | static const byte dtls13ProtocolLabel[DTLS13_PROTOCOL_LABEL_SZ + 1] = "dtls13"; |
172 | | #endif /* WOLFSSL_DTLS13 */ |
173 | | |
174 | | #if defined(HAVE_ECH) |
175 | | #define ECH_ACCEPT_CONFIRMATION_LABEL_SZ 23 |
176 | | #define ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ 27 |
177 | | static const byte |
178 | | echAcceptConfirmationLabel[ECH_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
179 | | "ech accept confirmation"; |
180 | | static const byte |
181 | | echHrrAcceptConfirmationLabel[ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
182 | | "hrr ech accept confirmation"; |
183 | | #endif |
184 | | |
185 | | #ifndef NO_CERTS |
186 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
187 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
188 | | |
189 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash); |
190 | | |
191 | | #endif |
192 | | #endif |
193 | | |
194 | | /* Expand data using HMAC, salt and label and info. |
195 | | * TLS v1.3 defines this function. Use callback if available. */ |
196 | | static int Tls13HKDFExpandLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
197 | | const byte* prk, word32 prkLen, |
198 | | const byte* protocol, word32 protocolLen, |
199 | | const byte* label, word32 labelLen, |
200 | | const byte* info, word32 infoLen, |
201 | | int digest) |
202 | 4.05k | { |
203 | 4.05k | int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
204 | | |
205 | | #if defined(HAVE_PK_CALLBACKS) |
206 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
207 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
208 | | protocol, protocolLen, |
209 | | label, labelLen, |
210 | | info, infoLen, digest, |
211 | | WOLFSSL_CLIENT_END /* ignored */); |
212 | | } |
213 | | |
214 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
215 | | return ret; |
216 | | #endif |
217 | 4.05k | (void)ssl; |
218 | 4.05k | PRIVATE_KEY_UNLOCK(); |
219 | 4.05k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
220 | 4.05k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
221 | 4.05k | protocol, protocolLen, |
222 | 4.05k | label, labelLen, |
223 | 4.05k | info, infoLen, digest, |
224 | 4.05k | ssl->heap, ssl->devId); |
225 | | #else |
226 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
227 | | protocol, protocolLen, |
228 | | label, labelLen, |
229 | | info, infoLen, digest); |
230 | | #endif |
231 | 4.05k | PRIVATE_KEY_LOCK(); |
232 | 4.05k | return ret; |
233 | 4.05k | } |
234 | | |
235 | | /* Same as above, but pass in the side we are expanding for: |
236 | | * side: either WOLFSSL_CLIENT_END or WOLFSSL_SERVER_END. |
237 | | */ |
238 | | static int Tls13HKDFExpandKeyLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
239 | | const byte* prk, word32 prkLen, |
240 | | const byte* protocol, word32 protocolLen, |
241 | | const byte* label, word32 labelLen, |
242 | | const byte* info, word32 infoLen, |
243 | | int digest, int side) |
244 | 9.81k | { |
245 | 9.81k | int ret; |
246 | | #if defined(HAVE_PK_CALLBACKS) |
247 | | ret = NOT_COMPILED_IN; |
248 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
249 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
250 | | protocol, protocolLen, |
251 | | label, labelLen, |
252 | | info, infoLen, |
253 | | digest, side); |
254 | | } |
255 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
256 | | return ret; |
257 | | #endif |
258 | | |
259 | 9.81k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
260 | 9.81k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
261 | 9.81k | protocol, protocolLen, |
262 | 9.81k | label, labelLen, |
263 | 9.81k | info, infoLen, digest, |
264 | 9.81k | ssl->heap, ssl->devId); |
265 | | |
266 | | #elif defined(HAVE_FIPS) && defined(wc_Tls13_HKDF_Expand_Label) |
267 | | ret = wc_Tls13_HKDF_Expand_Label_fips(okm, okmLen, prk, prkLen, |
268 | | protocol, protocolLen, |
269 | | label, labelLen, |
270 | | info, infoLen, digest); |
271 | | #else |
272 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
273 | | protocol, protocolLen, |
274 | | label, labelLen, |
275 | | info, infoLen, digest); |
276 | | #endif |
277 | 9.81k | (void)ssl; |
278 | 9.81k | (void)side; |
279 | 9.81k | return ret; |
280 | 9.81k | } |
281 | | |
282 | | |
283 | | /* Derive a key from a message. |
284 | | * |
285 | | * ssl The SSL/TLS object. |
286 | | * output The buffer to hold the derived key. |
287 | | * outputLen The length of the derived key. |
288 | | * secret The secret used to derive the key (HMAC secret). |
289 | | * label The label used to distinguish the context. |
290 | | * labelLen The length of the label. |
291 | | * msg The message data to derive key from. |
292 | | * msgLen The length of the message data to derive key from. |
293 | | * hashAlgo The hash algorithm to use in the HMAC. |
294 | | * returns 0 on success, otherwise failure. |
295 | | */ |
296 | | static int DeriveKeyMsg(WOLFSSL* ssl, byte* output, int outputLen, |
297 | | const byte* secret, const byte* label, word32 labelLen, |
298 | | byte* msg, int msgLen, int hashAlgo) |
299 | | { |
300 | | byte hash[WC_MAX_DIGEST_SIZE]; |
301 | | Digest digest; |
302 | | word32 hashSz = 0; |
303 | | const byte* protocol; |
304 | | word32 protocolLen; |
305 | | int digestAlg = -1; |
306 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
307 | | |
308 | | switch (hashAlgo) { |
309 | | #ifndef NO_SHA256 |
310 | | case sha256_mac: |
311 | | ret = wc_InitSha256_ex(&digest.sha256, ssl->heap, ssl->devId); |
312 | | if (ret == 0) { |
313 | | ret = wc_Sha256Update(&digest.sha256, msg, (word32)msgLen); |
314 | | if (ret == 0) |
315 | | ret = wc_Sha256Final(&digest.sha256, hash); |
316 | | wc_Sha256Free(&digest.sha256); |
317 | | } |
318 | | hashSz = WC_SHA256_DIGEST_SIZE; |
319 | | digestAlg = WC_SHA256; |
320 | | break; |
321 | | #endif |
322 | | #ifdef WOLFSSL_SHA384 |
323 | | case sha384_mac: |
324 | | ret = wc_InitSha384_ex(&digest.sha384, ssl->heap, ssl->devId); |
325 | | if (ret == 0) { |
326 | | ret = wc_Sha384Update(&digest.sha384, msg, (word32)msgLen); |
327 | | if (ret == 0) |
328 | | ret = wc_Sha384Final(&digest.sha384, hash); |
329 | | wc_Sha384Free(&digest.sha384); |
330 | | } |
331 | | hashSz = WC_SHA384_DIGEST_SIZE; |
332 | | digestAlg = WC_SHA384; |
333 | | break; |
334 | | #endif |
335 | | #ifdef WOLFSSL_TLS13_SHA512 |
336 | | case sha512_mac: |
337 | | ret = wc_InitSha512_ex(&digest.sha512, ssl->heap, ssl->devId); |
338 | | if (ret == 0) { |
339 | | ret = wc_Sha512Update(&digest.sha512, msg, (word32)msgLen); |
340 | | if (ret == 0) |
341 | | ret = wc_Sha512Final(&digest.sha512, hash); |
342 | | wc_Sha512Free(&digest.sha512); |
343 | | } |
344 | | hashSz = WC_SHA512_DIGEST_SIZE; |
345 | | digestAlg = WC_SHA512; |
346 | | break; |
347 | | #endif |
348 | | #ifdef WOLFSSL_SM3 |
349 | | case sm3_mac: |
350 | | ret = wc_InitSm3(&digest.sm3, ssl->heap, ssl->devId); |
351 | | if (ret == 0) { |
352 | | ret = wc_Sm3Update(&digest.sm3, msg, (word32)msgLen); |
353 | | if (ret == 0) |
354 | | ret = wc_Sm3Final(&digest.sm3, hash); |
355 | | wc_Sm3Free(&digest.sm3); |
356 | | } |
357 | | hashSz = WC_SM3_DIGEST_SIZE; |
358 | | digestAlg = WC_SM3; |
359 | | break; |
360 | | #endif |
361 | | default: |
362 | | ret = BAD_FUNC_ARG; |
363 | | digestAlg = -1; |
364 | | break; |
365 | | } |
366 | | |
367 | | if (digestAlg < 0) |
368 | | return HASH_TYPE_E; |
369 | | |
370 | | if (ret != 0) |
371 | | return ret; |
372 | | |
373 | | switch (ssl->version.minor) { |
374 | | case TLSv1_3_MINOR: |
375 | | protocol = tls13ProtocolLabel; |
376 | | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
377 | | break; |
378 | | #ifdef WOLFSSL_DTLS13 |
379 | | case DTLSv1_3_MINOR: |
380 | | if (!ssl->options.dtls) |
381 | | return VERSION_ERROR; |
382 | | |
383 | | protocol = dtls13ProtocolLabel; |
384 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
385 | | break; |
386 | | #endif /* WOLFSSL_DTLS13 */ |
387 | | default: |
388 | | return VERSION_ERROR; |
389 | | } |
390 | | if (outputLen == -1) |
391 | | outputLen = (int)hashSz; |
392 | | |
393 | | ret = Tls13HKDFExpandLabel(ssl, output, (word32)outputLen, secret, hashSz, |
394 | | protocol, protocolLen, label, labelLen, |
395 | | hash, hashSz, digestAlg); |
396 | | return ret; |
397 | | } |
398 | | |
399 | | /* Derive a key. |
400 | | * |
401 | | * ssl The SSL/TLS object. |
402 | | * output The buffer to hold the derived key. |
403 | | * outputLen The length of the derived key. |
404 | | * secret The secret used to derive the key (HMAC secret). |
405 | | * label The label used to distinguish the context. |
406 | | * labelLen The length of the label. |
407 | | * hashAlgo The hash algorithm to use in the HMAC. |
408 | | * includeMsgs Whether to include a hash of the handshake messages so far. |
409 | | * side The side that we are deriving the secret for. |
410 | | * returns 0 on success, otherwise failure. |
411 | | */ |
412 | | int Tls13DeriveKey(WOLFSSL* ssl, byte* output, int outputLen, |
413 | | const byte* secret, const byte* label, word32 labelLen, |
414 | | int hashAlgo, int includeMsgs, int side) |
415 | 0 | { |
416 | 0 | int ret = 0; |
417 | 0 | byte hash[WC_MAX_DIGEST_SIZE]; |
418 | 0 | word32 hashSz = 0; |
419 | 0 | word32 hashOutSz = 0; |
420 | 0 | const byte* protocol; |
421 | 0 | word32 protocolLen; |
422 | 0 | int digestAlg = 0; |
423 | | |
424 | |
|
425 | 0 | switch (hashAlgo) { |
426 | 0 | #ifndef NO_SHA256 |
427 | 0 | case sha256_mac: |
428 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
429 | 0 | digestAlg = WC_SHA256; |
430 | 0 | if (includeMsgs) |
431 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
432 | 0 | break; |
433 | 0 | #endif |
434 | | |
435 | 0 | #ifdef WOLFSSL_SHA384 |
436 | 0 | case sha384_mac: |
437 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
438 | 0 | digestAlg = WC_SHA384; |
439 | 0 | if (includeMsgs) |
440 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
441 | 0 | break; |
442 | 0 | #endif |
443 | | |
444 | | #ifdef WOLFSSL_TLS13_SHA512 |
445 | | case sha512_mac: |
446 | | hashSz = WC_SHA512_DIGEST_SIZE; |
447 | | digestAlg = WC_SHA512; |
448 | | if (includeMsgs) |
449 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
450 | | break; |
451 | | #endif |
452 | | |
453 | 0 | #ifdef WOLFSSL_SM3 |
454 | 0 | case sm3_mac: |
455 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
456 | 0 | digestAlg = WC_SM3; |
457 | 0 | if (includeMsgs) |
458 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
459 | 0 | break; |
460 | 0 | #endif |
461 | | |
462 | 0 | default: |
463 | 0 | ret = HASH_TYPE_E; |
464 | 0 | break; |
465 | 0 | } |
466 | 0 | if (ret != 0) |
467 | 0 | return ret; |
468 | | |
469 | 0 | protocol = tls13ProtocolLabel; |
470 | 0 | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
471 | |
|
472 | | #ifdef WOLFSSL_DTLS13 |
473 | | if (ssl->options.dtls) { |
474 | | protocol = dtls13ProtocolLabel; |
475 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
476 | | } |
477 | | #endif /* WOLFSSL_DTLS13 */ |
478 | |
|
479 | 0 | if (outputLen == -1) { |
480 | 0 | outputLen = (int)hashSz; |
481 | 0 | } |
482 | 0 | if (includeMsgs) { |
483 | 0 | hashOutSz = hashSz; |
484 | 0 | } |
485 | 0 | else { |
486 | | /* Appease static analyzers by making sure hash is cleared, since it is |
487 | | * passed into expand key label where older wc_Tls13_HKDF_Expand_Label |
488 | | * will unconditionally try to call a memcpy on it, however length will |
489 | | * always be 0. */ |
490 | 0 | XMEMSET(hash, 0, sizeof(hash)); |
491 | 0 | hashOutSz = 0; |
492 | 0 | } |
493 | |
|
494 | 0 | PRIVATE_KEY_UNLOCK(); |
495 | 0 | ret = Tls13HKDFExpandKeyLabel(ssl, output, (word32)outputLen, secret, hashSz, |
496 | 0 | protocol, protocolLen, label, labelLen, |
497 | 0 | hash, hashOutSz, digestAlg, side); |
498 | 0 | PRIVATE_KEY_LOCK(); |
499 | |
|
500 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
501 | | wc_MemZero_Add("TLS 1.3 derived key", output, outputLen); |
502 | | #endif |
503 | 0 | return ret; |
504 | 0 | } |
505 | | |
506 | | /* Convert TLS mac ID to a hash algorithm ID |
507 | | * |
508 | | * mac Mac ID to convert |
509 | | * returns hash ID on success, or the NONE type. |
510 | | */ |
511 | | static WC_INLINE int mac2hash(int mac) |
512 | | { |
513 | | int hash; |
514 | | switch (mac) { |
515 | | #ifndef NO_SHA256 |
516 | | case sha256_mac: |
517 | | hash = WC_SHA256; |
518 | | break; |
519 | | #endif |
520 | | |
521 | | #ifdef WOLFSSL_SHA384 |
522 | | case sha384_mac: |
523 | | hash = WC_SHA384; |
524 | | break; |
525 | | #endif |
526 | | |
527 | | #ifdef WOLFSSL_TLS13_SHA512 |
528 | | case sha512_mac: |
529 | | hash = WC_SHA512; |
530 | | break; |
531 | | #endif |
532 | | |
533 | | #ifdef WOLFSSL_SM3 |
534 | | case sm3_mac: |
535 | | hash = WC_SM3; |
536 | | break; |
537 | | #endif |
538 | | |
539 | | default: |
540 | | hash = WC_HASH_TYPE_NONE; |
541 | | } |
542 | | return hash; |
543 | | } |
544 | | |
545 | | #ifndef NO_PSK |
546 | | /* The length of the binder key label. */ |
547 | | #define BINDER_KEY_LABEL_SZ 10 |
548 | | /* The binder key label. */ |
549 | | static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] = |
550 | | "ext binder"; |
551 | | |
552 | | /* Derive the binder key. |
553 | | * |
554 | | * ssl The SSL/TLS object. |
555 | | * key The derived key. |
556 | | * returns 0 on success, otherwise failure. |
557 | | */ |
558 | | static int DeriveBinderKey(WOLFSSL* ssl, byte* key) |
559 | | { |
560 | | WOLFSSL_MSG("Derive Binder Key"); |
561 | | if (ssl == NULL || ssl->arrays == NULL) { |
562 | | return BAD_FUNC_ARG; |
563 | | } |
564 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
565 | | binderKeyLabel, BINDER_KEY_LABEL_SZ, |
566 | | NULL, 0, ssl->specs.mac_algorithm); |
567 | | } |
568 | | #endif /* !NO_PSK */ |
569 | | |
570 | | #if defined(HAVE_SESSION_TICKET) && \ |
571 | | (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) |
572 | | /* The length of the binder key resume label. */ |
573 | | #define BINDER_KEY_RESUME_LABEL_SZ 10 |
574 | | /* The binder key resume label. */ |
575 | | static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] = |
576 | | "res binder"; |
577 | | |
578 | | /* Derive the binder resumption key. |
579 | | * |
580 | | * ssl The SSL/TLS object. |
581 | | * key The derived key. |
582 | | * returns 0 on success, otherwise failure. |
583 | | */ |
584 | | static int DeriveBinderKeyResume(WOLFSSL* ssl, byte* key) |
585 | | { |
586 | | WOLFSSL_MSG("Derive Binder Key - Resumption"); |
587 | | if (ssl == NULL || ssl->arrays == NULL) { |
588 | | return BAD_FUNC_ARG; |
589 | | } |
590 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
591 | | binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ, |
592 | | NULL, 0, ssl->specs.mac_algorithm); |
593 | | } |
594 | | #endif /* HAVE_SESSION_TICKET && (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) */ |
595 | | |
596 | | #ifdef WOLFSSL_EARLY_DATA |
597 | | |
598 | | /* The length of the early traffic label. */ |
599 | | #define EARLY_TRAFFIC_LABEL_SZ 11 |
600 | | /* The early traffic label. */ |
601 | | static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] = |
602 | | "c e traffic"; |
603 | | |
604 | | /* Derive the early traffic key. |
605 | | * |
606 | | * ssl The SSL/TLS object. |
607 | | * key The derived key. |
608 | | * side The side that we are deriving the secret for. |
609 | | * returns 0 on success, otherwise failure. |
610 | | */ |
611 | | static int DeriveEarlyTrafficSecret(WOLFSSL* ssl, byte* key, int side) |
612 | | { |
613 | | int ret; |
614 | | WOLFSSL_MSG("Derive Early Traffic Secret"); |
615 | | if (ssl == NULL || ssl->arrays == NULL) { |
616 | | return BAD_FUNC_ARG; |
617 | | } |
618 | | |
619 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
620 | | /* If this is called from a sniffer session with keylog file support, |
621 | | * obtain the appropriate secret from the callback */ |
622 | | if (ssl->snifferSecretCb != NULL) { |
623 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
624 | | SNIFFER_SECRET_CLIENT_EARLY_TRAFFIC_SECRET, |
625 | | key); |
626 | | } |
627 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
628 | | |
629 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->secret, |
630 | | earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ, |
631 | | ssl->specs.mac_algorithm, 1, side); |
632 | | #ifdef HAVE_SECRET_CALLBACK |
633 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
634 | | ret = ssl->tls13SecretCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
635 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
636 | | if (ret != 0) { |
637 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
638 | | return TLS13_SECRET_CB_E; |
639 | | } |
640 | | } |
641 | | #ifdef OPENSSL_EXTRA |
642 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
643 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
644 | | ssl->specs.hash_size, NULL); |
645 | | if (ret != 0) { |
646 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
647 | | return TLS13_SECRET_CB_E; |
648 | | } |
649 | | } |
650 | | #endif /* OPENSSL_EXTRA */ |
651 | | #endif /* HAVE_SECRET_CALLBACK */ |
652 | | return ret; |
653 | | } |
654 | | |
655 | | #endif |
656 | | |
657 | | /* The length of the client handshake label. */ |
658 | 738 | #define CLIENT_HANDSHAKE_LABEL_SZ 12 |
659 | | /* The client handshake label. */ |
660 | | static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] = |
661 | | "c hs traffic"; |
662 | | |
663 | | /* Derive the client handshake key. |
664 | | * |
665 | | * ssl The SSL/TLS object. |
666 | | * key The derived key. |
667 | | * returns 0 on success, otherwise failure. |
668 | | */ |
669 | | static int DeriveClientHandshakeSecret(WOLFSSL* ssl, byte* key) |
670 | 738 | { |
671 | 738 | int ret; |
672 | 738 | WOLFSSL_MSG("Derive Client Handshake Secret"); |
673 | 738 | if (ssl == NULL || ssl->arrays == NULL) { |
674 | 0 | return BAD_FUNC_ARG; |
675 | 0 | } |
676 | | |
677 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
678 | | /* If this is called from a sniffer session with keylog file support, |
679 | | * obtain the appropriate secret from the callback */ |
680 | | if (ssl->snifferSecretCb != NULL) { |
681 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
682 | | SNIFFER_SECRET_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
683 | | key); |
684 | | } |
685 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
686 | | |
687 | 738 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
688 | 738 | clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ, |
689 | 738 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
690 | | #ifdef HAVE_SECRET_CALLBACK |
691 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
692 | | ret = ssl->tls13SecretCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
693 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
694 | | if (ret != 0) { |
695 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
696 | | return TLS13_SECRET_CB_E; |
697 | | } |
698 | | } |
699 | | #ifdef OPENSSL_EXTRA |
700 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
701 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
702 | | ssl->specs.hash_size, NULL); |
703 | | if (ret != 0) { |
704 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
705 | | return TLS13_SECRET_CB_E; |
706 | | } |
707 | | } |
708 | | #endif /* OPENSSL_EXTRA */ |
709 | | #endif /* HAVE_SECRET_CALLBACK */ |
710 | 738 | return ret; |
711 | 738 | } |
712 | | |
713 | | /* The length of the server handshake label. */ |
714 | 730 | #define SERVER_HANDSHAKE_LABEL_SZ 12 |
715 | | /* The server handshake label. */ |
716 | | static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] = |
717 | | "s hs traffic"; |
718 | | |
719 | | /* Derive the server handshake key. |
720 | | * |
721 | | * ssl The SSL/TLS object. |
722 | | * key The derived key. |
723 | | * returns 0 on success, otherwise failure. |
724 | | */ |
725 | | static int DeriveServerHandshakeSecret(WOLFSSL* ssl, byte* key) |
726 | 730 | { |
727 | 730 | int ret; |
728 | 730 | WOLFSSL_MSG("Derive Server Handshake Secret"); |
729 | 730 | if (ssl == NULL || ssl->arrays == NULL) { |
730 | 0 | return BAD_FUNC_ARG; |
731 | 0 | } |
732 | | |
733 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
734 | | /* If this is called from a sniffer session with keylog file support, |
735 | | * obtain the appropriate secret from the callback */ |
736 | | if (ssl->snifferSecretCb != NULL) { |
737 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
738 | | SNIFFER_SECRET_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
739 | | key); |
740 | | } |
741 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
742 | | |
743 | 730 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
744 | 730 | serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ, |
745 | 730 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
746 | | |
747 | | #ifdef HAVE_SECRET_CALLBACK |
748 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
749 | | ret = ssl->tls13SecretCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
750 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
751 | | if (ret != 0) { |
752 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
753 | | return TLS13_SECRET_CB_E; |
754 | | } |
755 | | } |
756 | | #ifdef OPENSSL_EXTRA |
757 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
758 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
759 | | ssl->specs.hash_size, NULL); |
760 | | if (ret != 0) { |
761 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
762 | | return TLS13_SECRET_CB_E; |
763 | | } |
764 | | } |
765 | | #endif /* OPENSSL_EXTRA */ |
766 | | #endif /* HAVE_SECRET_CALLBACK */ |
767 | 730 | return ret; |
768 | 730 | } |
769 | | |
770 | | /* The length of the client application traffic label. */ |
771 | 391 | #define CLIENT_APP_LABEL_SZ 12 |
772 | | /* The client application traffic label. */ |
773 | | static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] = |
774 | | "c ap traffic"; |
775 | | |
776 | | /* Derive the client application traffic key. |
777 | | * |
778 | | * ssl The SSL/TLS object. |
779 | | * key The derived key. |
780 | | * returns 0 on success, otherwise failure. |
781 | | */ |
782 | | static int DeriveClientTrafficSecret(WOLFSSL* ssl, byte* key) |
783 | 391 | { |
784 | 391 | int ret; |
785 | 391 | WOLFSSL_MSG("Derive Client Traffic Secret"); |
786 | 391 | if (ssl == NULL || ssl->arrays == NULL) { |
787 | 0 | return BAD_FUNC_ARG; |
788 | 0 | } |
789 | | |
790 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
791 | | /* If this is called from a sniffer session with keylog file support, |
792 | | * obtain the appropriate secret from the callback */ |
793 | | if (ssl->snifferSecretCb != NULL) { |
794 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
795 | | SNIFFER_SECRET_CLIENT_TRAFFIC_SECRET, |
796 | | key); |
797 | | } |
798 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
799 | | |
800 | 391 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
801 | 391 | clientAppLabel, CLIENT_APP_LABEL_SZ, |
802 | 391 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
803 | | |
804 | | #ifdef HAVE_SECRET_CALLBACK |
805 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
806 | | ret = ssl->tls13SecretCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
807 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
808 | | if (ret != 0) { |
809 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
810 | | return TLS13_SECRET_CB_E; |
811 | | } |
812 | | } |
813 | | #ifdef OPENSSL_EXTRA |
814 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
815 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
816 | | ssl->specs.hash_size, NULL); |
817 | | if (ret != 0) { |
818 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
819 | | return TLS13_SECRET_CB_E; |
820 | | } |
821 | | } |
822 | | #endif /* OPENSSL_EXTRA */ |
823 | | #endif /* HAVE_SECRET_CALLBACK */ |
824 | 391 | return ret; |
825 | 391 | } |
826 | | |
827 | | /* The length of the server application traffic label. */ |
828 | 393 | #define SERVER_APP_LABEL_SZ 12 |
829 | | /* The server application traffic label. */ |
830 | | static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] = |
831 | | "s ap traffic"; |
832 | | |
833 | | /* Derive the server application traffic key. |
834 | | * |
835 | | * ssl The SSL/TLS object. |
836 | | * key The derived key. |
837 | | * returns 0 on success, otherwise failure. |
838 | | */ |
839 | | static int DeriveServerTrafficSecret(WOLFSSL* ssl, byte* key) |
840 | 393 | { |
841 | 393 | int ret; |
842 | 393 | WOLFSSL_MSG("Derive Server Traffic Secret"); |
843 | 393 | if (ssl == NULL || ssl->arrays == NULL) { |
844 | 0 | return BAD_FUNC_ARG; |
845 | 0 | } |
846 | | |
847 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
848 | | /* If this is called from a sniffer session with keylog file support, |
849 | | * obtain the appropriate secret from the callback */ |
850 | | if (ssl->snifferSecretCb != NULL) { |
851 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
852 | | SNIFFER_SECRET_SERVER_TRAFFIC_SECRET, |
853 | | key); |
854 | | } |
855 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
856 | | |
857 | 393 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
858 | 393 | serverAppLabel, SERVER_APP_LABEL_SZ, |
859 | 393 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
860 | | |
861 | | #ifdef HAVE_SECRET_CALLBACK |
862 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
863 | | ret = ssl->tls13SecretCb(ssl, SERVER_TRAFFIC_SECRET, key, |
864 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
865 | | if (ret != 0) { |
866 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
867 | | return TLS13_SECRET_CB_E; |
868 | | } |
869 | | } |
870 | | #ifdef OPENSSL_EXTRA |
871 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
872 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_TRAFFIC_SECRET, key, |
873 | | ssl->specs.hash_size, NULL); |
874 | | if (ret != 0) { |
875 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
876 | | return TLS13_SECRET_CB_E; |
877 | | } |
878 | | } |
879 | | #endif /* OPENSSL_EXTRA */ |
880 | | #endif /* HAVE_SECRET_CALLBACK */ |
881 | 393 | return ret; |
882 | 393 | } |
883 | | |
884 | | #ifdef HAVE_KEYING_MATERIAL |
885 | | /* The length of the exporter master secret label. */ |
886 | | #define EXPORTER_MASTER_LABEL_SZ 10 |
887 | | /* The exporter master secret label. */ |
888 | | static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] = |
889 | | "exp master"; |
890 | | |
891 | | /* Derive the exporter secret. |
892 | | * |
893 | | * ssl The SSL/TLS object. |
894 | | * key The derived key. |
895 | | * returns 0 on success, otherwise failure. |
896 | | */ |
897 | | static int DeriveExporterSecret(WOLFSSL* ssl, byte* key) |
898 | | { |
899 | | int ret; |
900 | | WOLFSSL_ENTER("Derive Exporter Secret"); |
901 | | if (ssl == NULL || ssl->arrays == NULL) { |
902 | | return BAD_FUNC_ARG; |
903 | | } |
904 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
905 | | exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ, |
906 | | ssl->specs.mac_algorithm, 1, 0 /* Unused */); |
907 | | #ifdef HAVE_SECRET_CALLBACK |
908 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
909 | | ret = ssl->tls13SecretCb(ssl, EXPORTER_SECRET, key, |
910 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
911 | | if (ret != 0) { |
912 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
913 | | return TLS13_SECRET_CB_E; |
914 | | } |
915 | | } |
916 | | #ifdef OPENSSL_EXTRA |
917 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
918 | | ret = ssl->tls13KeyLogCb(ssl, EXPORTER_SECRET, key, |
919 | | ssl->specs.hash_size, NULL); |
920 | | if (ret != 0) { |
921 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
922 | | return TLS13_SECRET_CB_E; |
923 | | } |
924 | | } |
925 | | #endif /* OPENSSL_EXTRA */ |
926 | | #endif /* HAVE_SECRET_CALLBACK */ |
927 | | return ret; |
928 | | } |
929 | | |
930 | | /* The length of the exporter label. */ |
931 | | #define EXPORTER_LABEL_SZ 8 |
932 | | /* The exporter label. */ |
933 | | static const byte exporterLabel[EXPORTER_LABEL_SZ + 1] = |
934 | | "exporter"; |
935 | | /* Hash("") */ |
936 | | #ifndef NO_SHA256 |
937 | | static const byte emptySHA256Hash[] = { |
938 | | 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, |
939 | | 0x99, 0x6F, 0xB9, 0x24, 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, |
940 | | 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 |
941 | | }; |
942 | | #endif |
943 | | #ifdef WOLFSSL_SHA384 |
944 | | static const byte emptySHA384Hash[] = { |
945 | | 0x38, 0xB0, 0x60, 0xA7, 0x51, 0xAC, 0x96, 0x38, 0x4C, 0xD9, 0x32, 0x7E, |
946 | | 0xB1, 0xB1, 0xE3, 0x6A, 0x21, 0xFD, 0xB7, 0x11, 0x14, 0xBE, 0x07, 0x43, |
947 | | 0x4C, 0x0C, 0xC7, 0xBF, 0x63, 0xF6, 0xE1, 0xDA, 0x27, 0x4E, 0xDE, 0xBF, |
948 | | 0xE7, 0x6F, 0x65, 0xFB, 0xD5, 0x1A, 0xD2, 0xF1, 0x48, 0x98, 0xB9, 0x5B |
949 | | }; |
950 | | #endif |
951 | | #ifdef WOLFSSL_TLS13_SHA512 |
952 | | static const byte emptySHA512Hash[] = { |
953 | | 0xCF, 0x83, 0xE1, 0x35, 0x7E, 0xEF, 0xB8, 0xBD, 0xF1, 0x54, 0x28, 0x50, |
954 | | 0xD6, 0x6D, 0x80, 0x07, 0xD6, 0x20, 0xE4, 0x05, 0x0B, 0x57, 0x15, 0xDC, |
955 | | 0x83, 0xF4, 0xA9, 0x21, 0xD3, 0x6C, 0xE9, 0xCE, 0x47, 0xD0, 0xD1, 0x3C, |
956 | | 0x5D, 0x85, 0xF2, 0xB0, 0xFF, 0x83, 0x18, 0xD2, 0x87, 0x7E, 0xEC, 0x2F, |
957 | | 0x63, 0xB9, 0x31, 0xBD, 0x47, 0x41, 0x7A, 0x81, 0xA5, 0x38, 0x32, 0x7A, |
958 | | 0xF9, 0x27, 0xDA, 0x3E |
959 | | }; |
960 | | #endif |
961 | | /** |
962 | | * Implement section 7.5 of RFC 8446 |
963 | | * @return 0 on success |
964 | | * <0 on failure |
965 | | */ |
966 | | int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen, |
967 | | const char *label, size_t labelLen, |
968 | | const unsigned char *context, size_t contextLen) |
969 | | { |
970 | | int ret; |
971 | | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
972 | | word32 hashLen = 0; |
973 | | byte hashOut[WC_MAX_DIGEST_SIZE]; |
974 | | const byte* emptyHash = NULL; |
975 | | byte firstExpand[WC_MAX_DIGEST_SIZE]; |
976 | | const byte* protocol = tls13ProtocolLabel; |
977 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
978 | | |
979 | | if (ssl->options.dtls && ssl->version.minor != DTLSv1_3_MINOR) |
980 | | return VERSION_ERROR; |
981 | | |
982 | | if (!ssl->options.dtls && ssl->version.minor != TLSv1_3_MINOR) |
983 | | return VERSION_ERROR; |
984 | | |
985 | | #ifdef WOLFSSL_DTLS13 |
986 | | if (ssl->options.dtls) { |
987 | | protocol = dtls13ProtocolLabel; |
988 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
989 | | } |
990 | | #endif /* WOLFSSL_DTLS13 */ |
991 | | |
992 | | switch (ssl->specs.mac_algorithm) { |
993 | | #ifndef NO_SHA256 |
994 | | case sha256_mac: |
995 | | hashType = WC_HASH_TYPE_SHA256; |
996 | | hashLen = WC_SHA256_DIGEST_SIZE; |
997 | | emptyHash = emptySHA256Hash; |
998 | | break; |
999 | | #endif |
1000 | | |
1001 | | #ifdef WOLFSSL_SHA384 |
1002 | | case sha384_mac: |
1003 | | hashType = WC_HASH_TYPE_SHA384; |
1004 | | hashLen = WC_SHA384_DIGEST_SIZE; |
1005 | | emptyHash = emptySHA384Hash; |
1006 | | break; |
1007 | | #endif |
1008 | | |
1009 | | #ifdef WOLFSSL_TLS13_SHA512 |
1010 | | case sha512_mac: |
1011 | | hashType = WC_HASH_TYPE_SHA512; |
1012 | | hashLen = WC_SHA512_DIGEST_SIZE; |
1013 | | emptyHash = emptySHA512Hash; |
1014 | | break; |
1015 | | #endif |
1016 | | } |
1017 | | |
1018 | | /* Derive-Secret(Secret, label, "") */ |
1019 | | ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen, |
1020 | | ssl->arrays->exporterSecret, hashLen, |
1021 | | protocol, protocolLen, (byte*)label, (word32)labelLen, |
1022 | | emptyHash, hashLen, (int)hashType); |
1023 | | if (ret != 0) |
1024 | | return ret; |
1025 | | |
1026 | | /* Hash(context_value) */ |
1027 | | ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE); |
1028 | | if (ret != 0) |
1029 | | return ret; |
1030 | | |
1031 | | ret = Tls13HKDFExpandLabel(ssl, out, (word32)outLen, firstExpand, hashLen, |
1032 | | protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ, |
1033 | | hashOut, hashLen, (int)hashType); |
1034 | | |
1035 | | return ret; |
1036 | | } |
1037 | | #endif |
1038 | | |
1039 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1040 | | /* The length of the resumption master secret label. */ |
1041 | | #define RESUME_MASTER_LABEL_SZ 10 |
1042 | | /* The resumption master secret label. */ |
1043 | | static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] = |
1044 | | "res master"; |
1045 | | |
1046 | | /* Derive the resumption secret. |
1047 | | * |
1048 | | * ssl The SSL/TLS object. |
1049 | | * key The derived key. |
1050 | | * returns 0 on success, otherwise failure. |
1051 | | */ |
1052 | | int DeriveResumptionSecret(WOLFSSL* ssl, byte* key) |
1053 | | { |
1054 | | byte* masterSecret; |
1055 | | |
1056 | | WOLFSSL_MSG("Derive Resumption Secret"); |
1057 | | if (ssl == NULL) { |
1058 | | return BAD_FUNC_ARG; |
1059 | | } |
1060 | | if (ssl->arrays != NULL) { |
1061 | | masterSecret = ssl->arrays->masterSecret; |
1062 | | } |
1063 | | else { |
1064 | | masterSecret = ssl->session->masterSecret; |
1065 | | } |
1066 | | return Tls13DeriveKey(ssl, key, -1, masterSecret, resumeMasterLabel, |
1067 | | RESUME_MASTER_LABEL_SZ, ssl->specs.mac_algorithm, 1, |
1068 | | 0 /* Unused */); |
1069 | | } |
1070 | | #endif |
1071 | | |
1072 | | /* Length of the finished label. */ |
1073 | 3.70k | #define FINISHED_LABEL_SZ 8 |
1074 | | /* Finished label for generating finished key. */ |
1075 | | static const byte finishedLabel[FINISHED_LABEL_SZ+1] = "finished"; |
1076 | | /* Derive the finished secret. |
1077 | | * |
1078 | | * ssl The SSL/TLS object. |
1079 | | * key The key to use with the HMAC. |
1080 | | * secret The derived secret. |
1081 | | * side The side that we are deriving the secret for. |
1082 | | * returns 0 on success, otherwise failure. |
1083 | | */ |
1084 | | static int DeriveFinishedSecret(WOLFSSL* ssl, byte* key, byte* secret, |
1085 | | int side) |
1086 | 3.70k | { |
1087 | 3.70k | WOLFSSL_MSG("Derive Finished Secret"); |
1088 | 3.70k | return Tls13DeriveKey(ssl, secret, -1, key, finishedLabel, |
1089 | 3.70k | FINISHED_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1090 | 3.70k | side); |
1091 | 3.70k | } |
1092 | | |
1093 | | /* The length of the application traffic label. */ |
1094 | 0 | #define APP_TRAFFIC_LABEL_SZ 11 |
1095 | | /* The application traffic label. */ |
1096 | | static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] = |
1097 | | "traffic upd"; |
1098 | | |
1099 | | /* Update the traffic secret. |
1100 | | * |
1101 | | * ssl The SSL/TLS object. |
1102 | | * secret The previous secret and derived secret. |
1103 | | * side The side that we are deriving the secret for. |
1104 | | * returns 0 on success, otherwise failure. |
1105 | | */ |
1106 | | static int DeriveTrafficSecret(WOLFSSL* ssl, byte* secret, int side) |
1107 | 0 | { |
1108 | 0 | WOLFSSL_MSG("Derive New Application Traffic Secret"); |
1109 | 0 | return Tls13DeriveKey(ssl, secret, -1, secret, |
1110 | 0 | appTrafficLabel, APP_TRAFFIC_LABEL_SZ, |
1111 | 0 | ssl->specs.mac_algorithm, 0, side); |
1112 | 0 | } |
1113 | | |
1114 | | |
1115 | | static int Tls13_HKDF_Extract(WOLFSSL *ssl, byte* prk, const byte* salt, |
1116 | | int saltLen, byte* ikm, int ikmLen, int digest) |
1117 | | { |
1118 | | int ret; |
1119 | | #ifdef HAVE_PK_CALLBACKS |
1120 | | void *cb_ctx = ssl->HkdfExtractCtx; |
1121 | | CallbackHKDFExtract cb = ssl->ctx->HkdfExtractCb; |
1122 | | if (cb != NULL) { |
1123 | | ret = cb(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, cb_ctx); |
1124 | | } |
1125 | | else |
1126 | | #endif |
1127 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1128 | | if ((int)ssl->arrays->psk_keySz < 0) { |
1129 | | ret = PSK_KEY_ERROR; |
1130 | | } |
1131 | | else |
1132 | | #endif |
1133 | | { |
1134 | | #if !defined(HAVE_FIPS) || \ |
1135 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
1136 | | ret = wc_Tls13_HKDF_Extract_ex(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, |
1137 | | ssl->heap, ssl->devId); |
1138 | | #else |
1139 | | ret = wc_Tls13_HKDF_Extract(prk, salt, saltLen, ikm, ikmLen, digest); |
1140 | | (void)ssl; |
1141 | | #endif |
1142 | | } |
1143 | | return ret; |
1144 | | } |
1145 | | |
1146 | | /* Derive the early secret using HKDF Extract. |
1147 | | * |
1148 | | * ssl The SSL/TLS object. |
1149 | | */ |
1150 | | int DeriveEarlySecret(WOLFSSL* ssl) |
1151 | 3.96k | { |
1152 | 3.96k | int ret; |
1153 | | |
1154 | 3.96k | WOLFSSL_MSG("Derive Early Secret"); |
1155 | 3.96k | if (ssl == NULL || ssl->arrays == NULL) { |
1156 | 0 | return BAD_FUNC_ARG; |
1157 | 0 | } |
1158 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1159 | | ret = tsip_Tls13DeriveEarlySecret(ssl); |
1160 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1161 | | return ret; |
1162 | | #endif |
1163 | 3.96k | PRIVATE_KEY_UNLOCK(); |
1164 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1165 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1166 | | ssl->arrays->psk_key, (int)ssl->arrays->psk_keySz, |
1167 | | mac2hash(ssl->specs.mac_algorithm)); |
1168 | | #else |
1169 | 3.96k | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1170 | 3.96k | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1171 | 3.96k | #endif |
1172 | 3.96k | PRIVATE_KEY_LOCK(); |
1173 | 3.96k | return ret; |
1174 | 3.96k | } |
1175 | | |
1176 | | /* The length of the derived label. */ |
1177 | 759 | #define DERIVED_LABEL_SZ 7 |
1178 | | /* The derived label. */ |
1179 | | static const byte derivedLabel[DERIVED_LABEL_SZ + 1] = |
1180 | | "derived"; |
1181 | | |
1182 | | /* Derive the handshake secret using HKDF Extract. |
1183 | | * |
1184 | | * ssl The SSL/TLS object. |
1185 | | */ |
1186 | | int DeriveHandshakeSecret(WOLFSSL* ssl) |
1187 | 759 | { |
1188 | 759 | byte key[WC_MAX_DIGEST_SIZE]; |
1189 | 759 | int ret; |
1190 | 759 | WOLFSSL_MSG("Derive Handshake Secret"); |
1191 | 759 | if (ssl == NULL || ssl->arrays == NULL) { |
1192 | 0 | return BAD_FUNC_ARG; |
1193 | 0 | } |
1194 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1195 | | ret = tsip_Tls13DeriveHandshakeSecret(ssl); |
1196 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1197 | | return ret; |
1198 | | #endif |
1199 | | |
1200 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1201 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1202 | 759 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
1203 | 759 | derivedLabel, DERIVED_LABEL_SZ, |
1204 | 759 | NULL, 0, ssl->specs.mac_algorithm); |
1205 | 759 | if (ret != 0) |
1206 | 11 | return ret; |
1207 | | |
1208 | 748 | PRIVATE_KEY_UNLOCK(); |
1209 | 748 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->preMasterSecret, |
1210 | 748 | key, ssl->specs.hash_size, |
1211 | 748 | ssl->arrays->preMasterSecret, (int)ssl->arrays->preMasterSz, |
1212 | 748 | mac2hash(ssl->specs.mac_algorithm)); |
1213 | 748 | PRIVATE_KEY_LOCK(); |
1214 | | |
1215 | 748 | return ret; |
1216 | 759 | } |
1217 | | |
1218 | | /* Derive the master secret using HKDF Extract. |
1219 | | * |
1220 | | * ssl The SSL/TLS object. |
1221 | | */ |
1222 | | int DeriveMasterSecret(WOLFSSL* ssl) |
1223 | | { |
1224 | | byte key[WC_MAX_DIGEST_SIZE]; |
1225 | | int ret; |
1226 | | WOLFSSL_MSG("Derive Master Secret"); |
1227 | | if (ssl == NULL || ssl->arrays == NULL) { |
1228 | | return BAD_FUNC_ARG; |
1229 | | } |
1230 | | |
1231 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1232 | | ret = tsip_Tls13DeriveMasterSecret(ssl); |
1233 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1234 | | return ret; |
1235 | | #endif |
1236 | | |
1237 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1238 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1239 | | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret, |
1240 | | derivedLabel, DERIVED_LABEL_SZ, |
1241 | | NULL, 0, ssl->specs.mac_algorithm); |
1242 | | if (ret != 0) |
1243 | | return ret; |
1244 | | |
1245 | | PRIVATE_KEY_UNLOCK(); |
1246 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->masterSecret, |
1247 | | key, ssl->specs.hash_size, |
1248 | | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1249 | | PRIVATE_KEY_LOCK(); |
1250 | | |
1251 | | #ifdef HAVE_KEYING_MATERIAL |
1252 | | if (ret != 0) |
1253 | | return ret; |
1254 | | /* Calculate exporter secret only when saving arrays */ |
1255 | | if (ssl->options.saveArrays) |
1256 | | ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret); |
1257 | | #endif |
1258 | | |
1259 | | return ret; |
1260 | | } |
1261 | | |
1262 | | #if defined(HAVE_SESSION_TICKET) |
1263 | | /* Length of the resumption label. */ |
1264 | | #define RESUMPTION_LABEL_SZ 10 |
1265 | | /* Resumption label for generating PSK associated with the ticket. */ |
1266 | | static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption"; |
1267 | | |
1268 | | /* Derive the PSK associated with the ticket. |
1269 | | * |
1270 | | * ssl The SSL/TLS object. |
1271 | | * nonce The nonce to derive with. |
1272 | | * nonceLen The length of the nonce to derive with. |
1273 | | * secret The derived secret. |
1274 | | * returns 0 on success, otherwise failure. |
1275 | | */ |
1276 | | int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret) |
1277 | | { |
1278 | | int digestAlg; |
1279 | | /* Only one protocol version defined at this time. */ |
1280 | | const byte* protocol = tls13ProtocolLabel; |
1281 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
1282 | | int ret; |
1283 | | |
1284 | | WOLFSSL_MSG("Derive Resumption PSK"); |
1285 | | |
1286 | | #ifdef WOLFSSL_DTLS13 |
1287 | | if (ssl->options.dtls) { |
1288 | | protocol = dtls13ProtocolLabel; |
1289 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1290 | | } |
1291 | | #endif /* WOLFSSL_DTLS13 */ |
1292 | | |
1293 | | switch (ssl->specs.mac_algorithm) { |
1294 | | #ifndef NO_SHA256 |
1295 | | case sha256_mac: |
1296 | | digestAlg = WC_SHA256; |
1297 | | break; |
1298 | | #endif |
1299 | | |
1300 | | #ifdef WOLFSSL_SHA384 |
1301 | | case sha384_mac: |
1302 | | digestAlg = WC_SHA384; |
1303 | | break; |
1304 | | #endif |
1305 | | |
1306 | | #ifdef WOLFSSL_TLS13_SHA512 |
1307 | | case sha512_mac: |
1308 | | digestAlg = WC_SHA512; |
1309 | | break; |
1310 | | #endif |
1311 | | |
1312 | | #ifdef WOLFSSL_SM3 |
1313 | | case sm3_mac: |
1314 | | digestAlg = WC_SM3; |
1315 | | break; |
1316 | | #endif |
1317 | | |
1318 | | default: |
1319 | | return BAD_FUNC_ARG; |
1320 | | } |
1321 | | |
1322 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
1323 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
1324 | | PRIVATE_KEY_UNLOCK(); |
1325 | | ret = wc_Tls13_HKDF_Expand_Label_Alloc(secret, ssl->specs.hash_size, |
1326 | | ssl->session->masterSecret, ssl->specs.hash_size, protocol, protocolLen, |
1327 | | resumptionLabel, RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg, |
1328 | | ssl->heap); |
1329 | | PRIVATE_KEY_LOCK(); |
1330 | | #else |
1331 | | ret = Tls13HKDFExpandLabel(ssl, secret, ssl->specs.hash_size, |
1332 | | ssl->session->masterSecret, ssl->specs.hash_size, |
1333 | | protocol, protocolLen, resumptionLabel, |
1334 | | RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg); |
1335 | | #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */ |
1336 | | return ret; |
1337 | | } |
1338 | | #endif /* HAVE_SESSION_TICKET */ |
1339 | | |
1340 | | |
1341 | | /* Calculate the HMAC of message data to this point. |
1342 | | * |
1343 | | * ssl The SSL/TLS object. |
1344 | | * key The HMAC key. |
1345 | | * hash The hash result - verify data. |
1346 | | * returns length of verify data generated. |
1347 | | */ |
1348 | | static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash, |
1349 | | word32* pHashSz) |
1350 | 0 | { |
1351 | 0 | WC_DECLARE_VAR(verifyHmac, Hmac, 1, 0); |
1352 | 0 | int hashType = WC_SHA256; |
1353 | 0 | int hashSz = WC_SHA256_DIGEST_SIZE; |
1354 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
1355 | |
|
1356 | 0 | if (ssl == NULL || key == NULL || hash == NULL) { |
1357 | 0 | return BAD_FUNC_ARG; |
1358 | 0 | } |
1359 | | |
1360 | | /* Get the hash of the previous handshake messages. */ |
1361 | 0 | switch (ssl->specs.mac_algorithm) { |
1362 | 0 | #ifndef NO_SHA256 |
1363 | 0 | case sha256_mac: |
1364 | 0 | hashType = WC_SHA256; |
1365 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
1366 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
1367 | 0 | break; |
1368 | 0 | #endif /* !NO_SHA256 */ |
1369 | 0 | #ifdef WOLFSSL_SHA384 |
1370 | 0 | case sha384_mac: |
1371 | 0 | hashType = WC_SHA384; |
1372 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
1373 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
1374 | 0 | break; |
1375 | 0 | #endif /* WOLFSSL_SHA384 */ |
1376 | | #ifdef WOLFSSL_TLS13_SHA512 |
1377 | | case sha512_mac: |
1378 | | hashType = WC_SHA512; |
1379 | | hashSz = WC_SHA512_DIGEST_SIZE; |
1380 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
1381 | | break; |
1382 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
1383 | 0 | #ifdef WOLFSSL_SM3 |
1384 | 0 | case sm3_mac: |
1385 | 0 | hashType = WC_SM3; |
1386 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
1387 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
1388 | 0 | break; |
1389 | 0 | #endif /* WOLFSSL_SM3 */ |
1390 | 0 | default: |
1391 | 0 | ret = BAD_FUNC_ARG; |
1392 | 0 | break; |
1393 | 0 | } |
1394 | 0 | if (ret != 0) |
1395 | 0 | return ret; |
1396 | | |
1397 | | #ifdef WOLFSSL_DEBUG_TLS |
1398 | | WOLFSSL_MSG(" Key"); |
1399 | | WOLFSSL_BUFFER(key, ssl->specs.hash_size); |
1400 | | WOLFSSL_MSG(" Msg Hash"); |
1401 | | WOLFSSL_BUFFER(hash, hashSz); |
1402 | | #endif |
1403 | | |
1404 | 0 | WC_ALLOC_VAR_EX(verifyHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
1405 | 0 | return MEMORY_E); |
1406 | | |
1407 | | /* Calculate the verify data. */ |
1408 | 0 | ret = wc_HmacInit(verifyHmac, ssl->heap, ssl->devId); |
1409 | 0 | if (ret == 0) { |
1410 | 0 | ret = wc_HmacSetKey(verifyHmac, hashType, key, ssl->specs.hash_size); |
1411 | 0 | if (ret == 0) |
1412 | 0 | ret = wc_HmacUpdate(verifyHmac, hash, (word32)hashSz); |
1413 | 0 | if (ret == 0) |
1414 | 0 | ret = wc_HmacFinal(verifyHmac, hash); |
1415 | 0 | wc_HmacFree(verifyHmac); |
1416 | 0 | } |
1417 | |
|
1418 | 0 | WC_FREE_VAR_EX(verifyHmac, NULL, DYNAMIC_TYPE_HMAC); |
1419 | |
|
1420 | | #ifdef WOLFSSL_DEBUG_TLS |
1421 | | WOLFSSL_MSG(" Hash"); |
1422 | | WOLFSSL_BUFFER(hash, hashSz); |
1423 | | #endif |
1424 | |
|
1425 | 0 | if (pHashSz) |
1426 | 0 | *pHashSz = (word32)hashSz; |
1427 | |
|
1428 | 0 | return ret; |
1429 | 0 | } |
1430 | | |
1431 | | /* The length of the label to use when deriving keys. */ |
1432 | | #define WRITE_KEY_LABEL_SZ 3 |
1433 | | /* The length of the label to use when deriving IVs. */ |
1434 | | #define WRITE_IV_LABEL_SZ 2 |
1435 | | /* The label to use when deriving keys. */ |
1436 | | static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key"; |
1437 | | /* The label to use when deriving IVs. */ |
1438 | | static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv"; |
1439 | | |
1440 | | /* Derive the keys and IVs for TLS v1.3. |
1441 | | * |
1442 | | * ssl The SSL/TLS object. |
1443 | | * secret early_data_key when deriving the key and IV for encrypting early |
1444 | | * data application data and end_of_early_data messages. |
1445 | | * handshake_key when deriving keys and IVs for encrypting handshake |
1446 | | * messages. |
1447 | | * traffic_key when deriving first keys and IVs for encrypting |
1448 | | * traffic messages. |
1449 | | * update_traffic_key when deriving next keys and IVs for encrypting |
1450 | | * traffic messages. |
1451 | | * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived. |
1452 | | * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived. |
1453 | | * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived. |
1454 | | * store 1 indicates to derive the keys and IVs from derived secret and |
1455 | | * store ready for provisioning. |
1456 | | * returns 0 on success, otherwise failure. |
1457 | | */ |
1458 | | int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store) |
1459 | | { |
1460 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1461 | | int i = 0; |
1462 | | WC_DECLARE_VAR(key_dig, byte, MAX_PRF_DIG, 0); |
1463 | | int provision; |
1464 | | |
1465 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1466 | | ret = tsip_Tls13DeriveKeys(ssl, secret, side); |
1467 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1468 | | return ret; |
1469 | | } |
1470 | | ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1471 | | #endif |
1472 | | |
1473 | | WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap, |
1474 | | DYNAMIC_TYPE_DIGEST, return MEMORY_E); |
1475 | | |
1476 | | if (side == ENCRYPT_AND_DECRYPT_SIDE) { |
1477 | | provision = PROVISION_CLIENT_SERVER; |
1478 | | } |
1479 | | else { |
1480 | | provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^ |
1481 | | (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT : |
1482 | | PROVISION_SERVER; |
1483 | | } |
1484 | | |
1485 | | /* Derive the appropriate secret to use in the HKDF. */ |
1486 | | switch (secret) { |
1487 | | #ifdef WOLFSSL_EARLY_DATA |
1488 | | case early_data_key: |
1489 | | ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret, |
1490 | | WOLFSSL_CLIENT_END); |
1491 | | if (ret != 0) |
1492 | | goto end; |
1493 | | break; |
1494 | | #endif |
1495 | | |
1496 | | case handshake_key: |
1497 | | if (provision & PROVISION_CLIENT) { |
1498 | | ret = DeriveClientHandshakeSecret(ssl, |
1499 | | ssl->clientSecret); |
1500 | | if (ret != 0) |
1501 | | goto end; |
1502 | | } |
1503 | | if (provision & PROVISION_SERVER) { |
1504 | | ret = DeriveServerHandshakeSecret(ssl, |
1505 | | ssl->serverSecret); |
1506 | | if (ret != 0) |
1507 | | goto end; |
1508 | | } |
1509 | | break; |
1510 | | |
1511 | | case traffic_key: |
1512 | | if (provision & PROVISION_CLIENT) { |
1513 | | ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret); |
1514 | | if (ret != 0) |
1515 | | goto end; |
1516 | | } |
1517 | | if (provision & PROVISION_SERVER) { |
1518 | | ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret); |
1519 | | if (ret != 0) |
1520 | | goto end; |
1521 | | } |
1522 | | break; |
1523 | | |
1524 | | case update_traffic_key: |
1525 | | if (provision & PROVISION_CLIENT) { |
1526 | | ret = DeriveTrafficSecret(ssl, ssl->clientSecret, |
1527 | | WOLFSSL_CLIENT_END); |
1528 | | if (ret != 0) |
1529 | | goto end; |
1530 | | } |
1531 | | if (provision & PROVISION_SERVER) { |
1532 | | ret = DeriveTrafficSecret(ssl, ssl->serverSecret, |
1533 | | WOLFSSL_SERVER_END); |
1534 | | if (ret != 0) |
1535 | | goto end; |
1536 | | } |
1537 | | break; |
1538 | | |
1539 | | default: |
1540 | | ret = BAD_FUNC_ARG; |
1541 | | break; |
1542 | | } |
1543 | | |
1544 | | #ifdef WOLFSSL_QUIC |
1545 | | if (WOLFSSL_IS_QUIC(ssl)) { |
1546 | | ret = wolfSSL_quic_forward_secrets(ssl, secret, side); |
1547 | | if (ret != 0) |
1548 | | goto end; |
1549 | | } |
1550 | | #endif /* WOLFSSL_QUIC */ |
1551 | | |
1552 | | if (!store) |
1553 | | goto end; |
1554 | | |
1555 | | /* Key data = client key | server key | client IV | server IV */ |
1556 | | |
1557 | | if (provision & PROVISION_CLIENT) { |
1558 | | /* Derive the client key. */ |
1559 | | WOLFSSL_MSG("Derive Client Key"); |
1560 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1561 | | ssl->clientSecret, writeKeyLabel, |
1562 | | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1563 | | WOLFSSL_CLIENT_END); |
1564 | | if (ret != 0) |
1565 | | goto end; |
1566 | | i += ssl->specs.key_size; |
1567 | | } |
1568 | | |
1569 | | if (provision & PROVISION_SERVER) { |
1570 | | /* Derive the server key. */ |
1571 | | WOLFSSL_MSG("Derive Server Key"); |
1572 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1573 | | ssl->serverSecret, writeKeyLabel, |
1574 | | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1575 | | WOLFSSL_SERVER_END); |
1576 | | if (ret != 0) |
1577 | | goto end; |
1578 | | i += ssl->specs.key_size; |
1579 | | } |
1580 | | |
1581 | | if (provision & PROVISION_CLIENT) { |
1582 | | /* Derive the client IV. */ |
1583 | | WOLFSSL_MSG("Derive Client IV"); |
1584 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1585 | | ssl->clientSecret, writeIVLabel, |
1586 | | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1587 | | WOLFSSL_CLIENT_END); |
1588 | | if (ret != 0) |
1589 | | goto end; |
1590 | | i += ssl->specs.iv_size; |
1591 | | } |
1592 | | |
1593 | | if (provision & PROVISION_SERVER) { |
1594 | | /* Derive the server IV. */ |
1595 | | WOLFSSL_MSG("Derive Server IV"); |
1596 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1597 | | ssl->serverSecret, writeIVLabel, |
1598 | | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1599 | | WOLFSSL_SERVER_END); |
1600 | | if (ret != 0) |
1601 | | goto end; |
1602 | | i += ssl->specs.iv_size; |
1603 | | } |
1604 | | |
1605 | | /* Store keys and IVs but don't activate them. */ |
1606 | | ret = StoreKeys(ssl, key_dig, provision); |
1607 | | |
1608 | | #ifdef WOLFSSL_DTLS13 |
1609 | | if (ret != 0) |
1610 | | goto end; |
1611 | | |
1612 | | if (ssl->options.dtls) { |
1613 | | w64wrapper epochNumber; |
1614 | | ret = Dtls13DeriveSnKeys(ssl, provision); |
1615 | | if (ret != 0) |
1616 | | goto end; |
1617 | | |
1618 | | switch (secret) { |
1619 | | case early_data_key: |
1620 | | epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA); |
1621 | | break; |
1622 | | case handshake_key: |
1623 | | epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
1624 | | break; |
1625 | | case traffic_key: |
1626 | | case no_key: |
1627 | | epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
1628 | | break; |
1629 | | case update_traffic_key: |
1630 | | if (side == ENCRYPT_SIDE_ONLY) { |
1631 | | epochNumber = ssl->dtls13Epoch; |
1632 | | } |
1633 | | else if (side == DECRYPT_SIDE_ONLY) { |
1634 | | epochNumber = ssl->dtls13PeerEpoch; |
1635 | | } |
1636 | | else { |
1637 | | ret = BAD_STATE_E; |
1638 | | goto end; |
1639 | | } |
1640 | | w64Increment(&epochNumber); |
1641 | | break; |
1642 | | default: |
1643 | | ret = BAD_STATE_E; |
1644 | | goto end; |
1645 | | } |
1646 | | ret = Dtls13NewEpoch(ssl, epochNumber, side); |
1647 | | if (ret != 0) |
1648 | | goto end; |
1649 | | } |
1650 | | |
1651 | | #endif /* WOLFSSL_DTLS13 */ |
1652 | | |
1653 | | end: |
1654 | | ForceZero(key_dig, (word32)i); |
1655 | | #ifdef WOLFSSL_SMALL_STACK |
1656 | | XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1657 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1658 | | wc_MemZero_Check(key_dig, MAX_PRF_DIG); |
1659 | | #endif |
1660 | | |
1661 | | if (ret != 0) { |
1662 | | WOLFSSL_ERROR_VERBOSE(ret); |
1663 | | } |
1664 | | |
1665 | | return ret; |
1666 | | } |
1667 | | |
1668 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
1669 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
1670 | | #ifndef NO_ASN_TIME |
1671 | | #if defined(USER_TICKS) |
1672 | | #if 0 |
1673 | | word32 TimeNowInMilliseconds(void) |
1674 | | { |
1675 | | /* |
1676 | | write your own clock tick function if don't want gettimeofday() |
1677 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1678 | | */ |
1679 | | } |
1680 | | #endif |
1681 | | |
1682 | | #elif defined(TIME_OVERRIDES) |
1683 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1684 | | word32 TimeNowInMilliseconds(void) |
1685 | | { |
1686 | | return (word32) wc_Time(0) * 1000; |
1687 | | } |
1688 | | #else |
1689 | | #ifndef HAVE_TIME_T_TYPE |
1690 | | typedef long time_t; |
1691 | | #endif |
1692 | | extern time_t XTIME(time_t * timer); |
1693 | | |
1694 | | /* The time in milliseconds. |
1695 | | * Used for tickets to represent difference between when first seen and when |
1696 | | * sending. |
1697 | | * |
1698 | | * returns the time in milliseconds as a 32-bit value. |
1699 | | */ |
1700 | | word32 TimeNowInMilliseconds(void) |
1701 | | { |
1702 | | return (word32) XTIME(0) * 1000; |
1703 | | } |
1704 | | #endif |
1705 | | |
1706 | | #elif defined(XTIME_MS) |
1707 | | word32 TimeNowInMilliseconds(void) |
1708 | | { |
1709 | | return (word32)XTIME_MS(0); |
1710 | | } |
1711 | | |
1712 | | #elif defined(USE_WINDOWS_API) |
1713 | | /* The time in milliseconds. |
1714 | | * Used for tickets to represent difference between when first seen and when |
1715 | | * sending. |
1716 | | * |
1717 | | * returns the time in milliseconds as a 32-bit value. |
1718 | | */ |
1719 | | word32 TimeNowInMilliseconds(void) |
1720 | | { |
1721 | | static int init = 0; |
1722 | | static LARGE_INTEGER freq; |
1723 | | LARGE_INTEGER count; |
1724 | | |
1725 | | if (!init) { |
1726 | | QueryPerformanceFrequency(&freq); |
1727 | | init = 1; |
1728 | | } |
1729 | | |
1730 | | QueryPerformanceCounter(&count); |
1731 | | |
1732 | | return (word32)(count.QuadPart / (freq.QuadPart / 1000)); |
1733 | | } |
1734 | | |
1735 | | #elif defined(HAVE_RTP_SYS) |
1736 | | #include "rtptime.h" |
1737 | | |
1738 | | /* The time in milliseconds. |
1739 | | * Used for tickets to represent difference between when first seen and when |
1740 | | * sending. |
1741 | | * |
1742 | | * returns the time in milliseconds as a 32-bit value. |
1743 | | */ |
1744 | | word32 TimeNowInMilliseconds(void) |
1745 | | { |
1746 | | return (word32)rtp_get_system_sec() * 1000; |
1747 | | } |
1748 | | #elif defined(WOLFSSL_DEOS) |
1749 | | word32 TimeNowInMilliseconds(void) |
1750 | | { |
1751 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
1752 | | word32 *systemTickPtr = systemTickPointer(); |
1753 | | |
1754 | | return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000; |
1755 | | } |
1756 | | #elif defined(MICRIUM) |
1757 | | /* The time in milliseconds. |
1758 | | * Used for tickets to represent difference between when first seen and when |
1759 | | * sending. |
1760 | | * |
1761 | | * returns the time in milliseconds as a 32-bit value. |
1762 | | */ |
1763 | | word32 TimeNowInMilliseconds(void) |
1764 | | { |
1765 | | OS_TICK ticks = 0; |
1766 | | OS_ERR err; |
1767 | | |
1768 | | ticks = OSTimeGet(&err); |
1769 | | |
1770 | | return (word32) (ticks / OSCfg_TickRate_Hz) * 1000; |
1771 | | } |
1772 | | #elif defined(MICROCHIP_TCPIP_V5) |
1773 | | /* The time in milliseconds. |
1774 | | * Used for tickets to represent difference between when first seen and when |
1775 | | * sending. |
1776 | | * |
1777 | | * returns the time in milliseconds as a 32-bit value. |
1778 | | */ |
1779 | | word32 TimeNowInMilliseconds(void) |
1780 | | { |
1781 | | return (word32) (TickGet() / (TICKS_PER_SECOND / 1000)); |
1782 | | } |
1783 | | #elif defined(MICROCHIP_TCPIP) |
1784 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
1785 | | #include <system/tmr/sys_tmr.h> |
1786 | | |
1787 | | /* The time in milliseconds. |
1788 | | * Used for tickets to represent difference between when first seen and when |
1789 | | * sending. |
1790 | | * |
1791 | | * returns the time in milliseconds as a 32-bit value. |
1792 | | */ |
1793 | | word32 TimeNowInMilliseconds(void) |
1794 | | { |
1795 | | return (word32)(SYS_TMR_TickCountGet() / |
1796 | | (SYS_TMR_TickCounterFrequencyGet() / 1000)); |
1797 | | } |
1798 | | #else |
1799 | | /* The time in milliseconds. |
1800 | | * Used for tickets to represent difference between when first seen and when |
1801 | | * sending. |
1802 | | * |
1803 | | * returns the time in milliseconds as a 32-bit value. |
1804 | | */ |
1805 | | word32 TimeNowInMilliseconds(void) |
1806 | | { |
1807 | | return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000)); |
1808 | | } |
1809 | | |
1810 | | #endif |
1811 | | |
1812 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1813 | | /* The time in milliseconds. |
1814 | | * Used for tickets to represent difference between when first seen and when |
1815 | | * sending. |
1816 | | * |
1817 | | * returns the time in milliseconds as a 32-bit value. |
1818 | | */ |
1819 | | word32 TimeNowInMilliseconds(void) |
1820 | | { |
1821 | | TIME_STRUCT mqxTime; |
1822 | | |
1823 | | _time_get_elapsed(&mqxTime); |
1824 | | |
1825 | | return (word32) mqxTime.SECONDS * 1000; |
1826 | | } |
1827 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
1828 | | #include "include/task.h" |
1829 | | |
1830 | | /* The time in milliseconds. |
1831 | | * Used for tickets to represent difference between when first seen and when |
1832 | | * sending. |
1833 | | * |
1834 | | * returns the time in milliseconds as a 32-bit value. |
1835 | | */ |
1836 | | word32 TimeNowInMilliseconds(void) |
1837 | | { |
1838 | | return (unsigned int)(((float)xTaskGetTickCount()) / |
1839 | | (configTICK_RATE_HZ / 1000)); |
1840 | | } |
1841 | | #elif defined(FREESCALE_KSDK_BM) |
1842 | | #include "lwip/sys.h" /* lwIP */ |
1843 | | |
1844 | | /* The time in milliseconds. |
1845 | | * Used for tickets to represent difference between when first seen and when |
1846 | | * sending. |
1847 | | * |
1848 | | * returns the time in milliseconds as a 32-bit value. |
1849 | | */ |
1850 | | word32 TimeNowInMilliseconds(void) |
1851 | | { |
1852 | | return sys_now(); |
1853 | | } |
1854 | | |
1855 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
1856 | | |
1857 | | word32 TimeNowInMilliseconds(void) |
1858 | | { |
1859 | | return (word32)osKernelGetTickCount(); |
1860 | | } |
1861 | | |
1862 | | #elif defined(WOLFSSL_TIRTOS) |
1863 | | /* The time in milliseconds. |
1864 | | * Used for tickets to represent difference between when first seen and when |
1865 | | * sending. |
1866 | | * |
1867 | | * returns the time in milliseconds as a 32-bit value. |
1868 | | */ |
1869 | | word32 TimeNowInMilliseconds(void) |
1870 | | { |
1871 | | return (word32) Seconds_get() * 1000; |
1872 | | } |
1873 | | #elif defined(WOLFSSL_UTASKER) |
1874 | | /* The time in milliseconds. |
1875 | | * Used for tickets to represent difference between when first seen and when |
1876 | | * sending. |
1877 | | * |
1878 | | * returns the time in milliseconds as a 32-bit value. |
1879 | | */ |
1880 | | word32 TimeNowInMilliseconds(void) |
1881 | | { |
1882 | | return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
1883 | | } |
1884 | | #elif defined(WOLFSSL_LINUXKM) |
1885 | | word32 TimeNowInMilliseconds(void) |
1886 | | { |
1887 | | s64 t; |
1888 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
1889 | | struct timespec ts; |
1890 | | getnstimeofday(&ts); |
1891 | | t = ts.tv_sec * (s64)1000; |
1892 | | t += ts.tv_nsec / (s64)1000000; |
1893 | | #else |
1894 | | struct timespec64 ts; |
1895 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
1896 | | ts = current_kernel_time64(); |
1897 | | #else |
1898 | | ktime_get_coarse_real_ts64(&ts); |
1899 | | #endif |
1900 | | t = ts.tv_sec * 1000L; |
1901 | | t += ts.tv_nsec / 1000000L; |
1902 | | #endif |
1903 | | return (word32)t; |
1904 | | } |
1905 | | #elif defined(WOLFSSL_QNX_CAAM) |
1906 | | word32 TimeNowInMilliseconds(void) |
1907 | | { |
1908 | | struct timespec now; |
1909 | | clock_gettime(CLOCK_REALTIME, &now); |
1910 | | return (word32)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
1911 | | } |
1912 | | #elif defined(FUSION_RTOS) |
1913 | | /* The time in milliseconds. |
1914 | | * Used for tickets to represent difference between when first seen and when |
1915 | | * sending. |
1916 | | * |
1917 | | * returns the time in milliseconds as a 32-bit value. |
1918 | | */ |
1919 | | word32 TimeNowInMilliseconds(void) |
1920 | | { |
1921 | | struct timeval now; |
1922 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
1923 | | return 0; |
1924 | | |
1925 | | /* Convert to milliseconds number. */ |
1926 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1927 | | } |
1928 | | #elif defined(WOLFSSL_ZEPHYR) |
1929 | | word32 TimeNowInMilliseconds(void) |
1930 | | { |
1931 | | int64_t t; |
1932 | | #if defined(CONFIG_ARCH_POSIX) |
1933 | | k_cpu_idle(); |
1934 | | #endif |
1935 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
1936 | | return (word32)t; |
1937 | | } |
1938 | | #elif defined(FREERTOS) |
1939 | | word32 TimeNowInMilliseconds(void) |
1940 | | { |
1941 | | return (word32)((uint64_t)(xTaskGetTickCount() * 1000) / |
1942 | | configTICK_RATE_HZ); |
1943 | | } |
1944 | | #else |
1945 | | /* The time in milliseconds. |
1946 | | * Used for tickets to represent difference between when first seen and when |
1947 | | * sending. |
1948 | | * |
1949 | | * returns the time in milliseconds as a 32-bit value. |
1950 | | */ |
1951 | | word32 TimeNowInMilliseconds(void) |
1952 | | { |
1953 | | struct timeval now; |
1954 | | |
1955 | | if (gettimeofday(&now, 0) < 0) |
1956 | | return 0; |
1957 | | |
1958 | | /* Convert to milliseconds number. */ |
1959 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1960 | | } |
1961 | | #endif |
1962 | | #else |
1963 | | /* user must supply time in milliseconds function: |
1964 | | * word32 TimeNowInMilliseconds(void); |
1965 | | * The response is milliseconds elapsed |
1966 | | */ |
1967 | | #endif /* !NO_ASN_TIME */ |
1968 | | #else |
1969 | | #ifndef NO_ASN_TIME |
1970 | | #if defined(USER_TICKS) |
1971 | | #if 0 |
1972 | | sword64 TimeNowInMilliseconds(void) |
1973 | | { |
1974 | | /* |
1975 | | write your own clock tick function if don't want gettimeofday() |
1976 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1977 | | */ |
1978 | | } |
1979 | | #endif |
1980 | | |
1981 | | #elif defined(TIME_OVERRIDES) |
1982 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1983 | | sword64 TimeNowInMilliseconds(void) |
1984 | | { |
1985 | | return (sword64) wc_Time(0) * 1000; |
1986 | | } |
1987 | | #else |
1988 | | #ifndef HAVE_TIME_T_TYPE |
1989 | | typedef long time_t; |
1990 | | #endif |
1991 | | extern time_t XTIME(time_t * timer); |
1992 | | |
1993 | | /* The time in milliseconds. |
1994 | | * Used for tickets to represent difference between when first seen and when |
1995 | | * sending. |
1996 | | * |
1997 | | * returns the time in milliseconds as a 32-bit value. |
1998 | | */ |
1999 | | sword64 TimeNowInMilliseconds(void) |
2000 | | { |
2001 | | return (sword64) XTIME(0) * 1000; |
2002 | | } |
2003 | | #endif |
2004 | | |
2005 | | #elif defined(XTIME_MS) |
2006 | | sword64 TimeNowInMilliseconds(void) |
2007 | | { |
2008 | | return (sword64)XTIME_MS(0); |
2009 | | } |
2010 | | |
2011 | | #elif defined(USE_WINDOWS_API) |
2012 | | /* The time in milliseconds. |
2013 | | * Used for tickets to represent difference between when first seen and when |
2014 | | * sending. |
2015 | | * |
2016 | | * returns the time in milliseconds as a 64-bit value. |
2017 | | */ |
2018 | | sword64 TimeNowInMilliseconds(void) |
2019 | | { |
2020 | | static int init = 0; |
2021 | | static LARGE_INTEGER freq; |
2022 | | LARGE_INTEGER count; |
2023 | | |
2024 | | if (!init) { |
2025 | | QueryPerformanceFrequency(&freq); |
2026 | | init = 1; |
2027 | | } |
2028 | | |
2029 | | QueryPerformanceCounter(&count); |
2030 | | |
2031 | | return (sword64)(count.QuadPart / (freq.QuadPart / 1000)); |
2032 | | } |
2033 | | |
2034 | | #elif defined(HAVE_RTP_SYS) |
2035 | | #include "rtptime.h" |
2036 | | |
2037 | | /* The time in milliseconds. |
2038 | | * Used for tickets to represent difference between when first seen and when |
2039 | | * sending. |
2040 | | * |
2041 | | * returns the time in milliseconds as a 64-bit value. |
2042 | | */ |
2043 | | sword64 TimeNowInMilliseconds(void) |
2044 | | { |
2045 | | return (sword64)rtp_get_system_sec() * 1000; |
2046 | | } |
2047 | | #elif defined(WOLFSSL_DEOS) |
2048 | | sword64 TimeNowInMilliseconds(void) |
2049 | | { |
2050 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2051 | | word32 *systemTickPtr = systemTickPointer(); |
2052 | | |
2053 | | return (sword64) (*systemTickPtr/systemTickTimeInHz) * 1000; |
2054 | | } |
2055 | | #elif defined(MICRIUM) |
2056 | | /* The time in milliseconds. |
2057 | | * Used for tickets to represent difference between when first seen and when |
2058 | | * sending. |
2059 | | * |
2060 | | * returns the time in milliseconds as a 64-bit value. |
2061 | | */ |
2062 | | sword64 TimeNowInMilliseconds(void) |
2063 | | { |
2064 | | OS_TICK ticks = 0; |
2065 | | OS_ERR err; |
2066 | | |
2067 | | ticks = OSTimeGet(&err); |
2068 | | |
2069 | | return (sword64) (ticks / OSCfg_TickRate_Hz) * 1000; |
2070 | | } |
2071 | | #elif defined(MICROCHIP_TCPIP_V5) |
2072 | | /* The time in milliseconds. |
2073 | | * Used for tickets to represent difference between when first seen and when |
2074 | | * sending. |
2075 | | * |
2076 | | * returns the time in milliseconds as a 64-bit value. |
2077 | | */ |
2078 | | sword64 TimeNowInMilliseconds(void) |
2079 | | { |
2080 | | return (sword64) (TickGet() / (TICKS_PER_SECOND / 1000)); |
2081 | | } |
2082 | | #elif defined(MICROCHIP_TCPIP) |
2083 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
2084 | | #include <system/tmr/sys_tmr.h> |
2085 | | |
2086 | | /* The time in milliseconds. |
2087 | | * Used for tickets to represent difference between when first seen and when |
2088 | | * sending. |
2089 | | * |
2090 | | * returns the time in milliseconds as a 64-bit value. |
2091 | | */ |
2092 | | sword64 TimeNowInMilliseconds(void) |
2093 | | { |
2094 | | return (sword64)SYS_TMR_TickCountGet() / |
2095 | | (SYS_TMR_TickCounterFrequencyGet() / 1000); |
2096 | | } |
2097 | | #else |
2098 | | /* The time in milliseconds. |
2099 | | * Used for tickets to represent difference between when first seen and when |
2100 | | * sending. |
2101 | | * |
2102 | | * returns the time in milliseconds as a 64-bit value. |
2103 | | */ |
2104 | | sword64 TimeNowInMilliseconds(void) |
2105 | | { |
2106 | | return (sword64)SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000); |
2107 | | } |
2108 | | |
2109 | | #endif |
2110 | | |
2111 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2112 | | /* The time in milliseconds. |
2113 | | * Used for tickets to represent difference between when first seen and when |
2114 | | * sending. |
2115 | | * |
2116 | | * returns the time in milliseconds as a 64-bit value. |
2117 | | */ |
2118 | | sword64 TimeNowInMilliseconds(void) |
2119 | | { |
2120 | | TIME_STRUCT mqxTime; |
2121 | | |
2122 | | _time_get_elapsed(&mqxTime); |
2123 | | |
2124 | | return (sword64) mqxTime.SECONDS * 1000; |
2125 | | } |
2126 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
2127 | | #include "include/task.h" |
2128 | | |
2129 | | /* The time in milliseconds. |
2130 | | * Used for tickets to represent difference between when first seen and when |
2131 | | * sending. |
2132 | | * |
2133 | | * returns the time in milliseconds as a 64-bit value. |
2134 | | */ |
2135 | | sword64 TimeNowInMilliseconds(void) |
2136 | | { |
2137 | | return (sword64)xTaskGetTickCount() / (configTICK_RATE_HZ / 1000); |
2138 | | } |
2139 | | #elif defined(FREESCALE_KSDK_BM) |
2140 | | #include "lwip/sys.h" /* lwIP */ |
2141 | | |
2142 | | /* The time in milliseconds. |
2143 | | * Used for tickets to represent difference between when first seen and when |
2144 | | * sending. |
2145 | | * |
2146 | | * returns the time in milliseconds as a 64-bit value. |
2147 | | */ |
2148 | | sword64 TimeNowInMilliseconds(void) |
2149 | | { |
2150 | | return sys_now(); |
2151 | | } |
2152 | | |
2153 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
2154 | | |
2155 | | sword64 TimeNowInMilliseconds(void) |
2156 | | { |
2157 | | return (sword64)osKernelGetTickCount(); |
2158 | | } |
2159 | | |
2160 | | #elif defined(WOLFSSL_TIRTOS) |
2161 | | /* The time in milliseconds. |
2162 | | * Used for tickets to represent difference between when first seen and when |
2163 | | * sending. |
2164 | | * |
2165 | | * returns the time in milliseconds as a 64-bit value. |
2166 | | */ |
2167 | | sword64 TimeNowInMilliseconds(void) |
2168 | | { |
2169 | | return (sword64) Seconds_get() * 1000; |
2170 | | } |
2171 | | #elif defined(WOLFSSL_UTASKER) |
2172 | | /* The time in milliseconds. |
2173 | | * Used for tickets to represent difference between when first seen and when |
2174 | | * sending. |
2175 | | * |
2176 | | * returns the time in milliseconds as a 64-bit value. |
2177 | | */ |
2178 | | sword64 TimeNowInMilliseconds(void) |
2179 | | { |
2180 | | return (sword64)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
2181 | | } |
2182 | | #elif defined(WOLFSSL_LINUXKM) |
2183 | | sword64 TimeNowInMilliseconds(void) |
2184 | | { |
2185 | | s64 t; |
2186 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2187 | | struct timespec ts; |
2188 | | getnstimeofday(&ts); |
2189 | | t = ts.tv_sec * (s64)1000; |
2190 | | t += ts.tv_nsec / (s64)1000000; |
2191 | | #else |
2192 | | struct timespec64 ts; |
2193 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2194 | | ts = current_kernel_time64(); |
2195 | | #else |
2196 | | ktime_get_coarse_real_ts64(&ts); |
2197 | | #endif |
2198 | | t = ts.tv_sec * 1000L; |
2199 | | t += ts.tv_nsec / 1000000L; |
2200 | | #endif |
2201 | | return (sword64)t; |
2202 | | } |
2203 | | #elif defined(WOLFSSL_QNX_CAAM) |
2204 | | sword64 TimeNowInMilliseconds(void) |
2205 | | { |
2206 | | struct timespec now; |
2207 | | clock_gettime(CLOCK_REALTIME, &now); |
2208 | | return (sword64)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
2209 | | } |
2210 | | #elif defined(FUSION_RTOS) |
2211 | | /* The time in milliseconds. |
2212 | | * Used for tickets to represent difference between when first seen and when |
2213 | | * sending. |
2214 | | * |
2215 | | * returns the time in milliseconds as a 64-bit value. |
2216 | | */ |
2217 | | sword64 TimeNowInMilliseconds(void) |
2218 | | { |
2219 | | struct timeval now; |
2220 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2221 | | return 0; |
2222 | | |
2223 | | /* Convert to milliseconds number. */ |
2224 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2225 | | } |
2226 | | #elif defined(WOLFSSL_ZEPHYR) |
2227 | | sword64 TimeNowInMilliseconds(void) |
2228 | | { |
2229 | | int64_t t; |
2230 | | #if defined(CONFIG_ARCH_POSIX) |
2231 | | k_cpu_idle(); |
2232 | | #endif |
2233 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2234 | | return (sword64)t; |
2235 | | } |
2236 | | #elif defined(FREERTOS) |
2237 | | sword64 TimeNowInMilliseconds(void) |
2238 | | { |
2239 | | return (sword64)((uint64_t)(xTaskGetTickCount() * 1000) / |
2240 | | configTICK_RATE_HZ); |
2241 | | } |
2242 | | #else |
2243 | | /* The time in milliseconds. |
2244 | | * Used for tickets to represent difference between when first seen and when |
2245 | | * sending. |
2246 | | * |
2247 | | * returns the time in milliseconds as a 64-bit value. |
2248 | | */ |
2249 | | sword64 TimeNowInMilliseconds(void) |
2250 | | { |
2251 | | struct timeval now; |
2252 | | |
2253 | | if (gettimeofday(&now, 0) < 0) |
2254 | | return 0; |
2255 | | |
2256 | | /* Convert to milliseconds number. */ |
2257 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2258 | | } |
2259 | | #endif |
2260 | | #else |
2261 | | /* user must supply time in milliseconds function: |
2262 | | * sword64 TimeNowInMilliseconds(void); |
2263 | | * The response is milliseconds elapsed |
2264 | | */ |
2265 | | #endif /* !NO_ASN_TIME */ |
2266 | | #endif /* WOLFSSL_32BIT_MILLI_TIME */ |
2267 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
2268 | | |
2269 | | /* Add record layer header to message. |
2270 | | * |
2271 | | * output The buffer to write the record layer header into. |
2272 | | * length The length of the record data. |
2273 | | * type The type of record message. |
2274 | | * ssl The SSL/TLS object. |
2275 | | */ |
2276 | | static void AddTls13RecordHeader(byte* output, word32 length, byte type, |
2277 | | WOLFSSL* ssl) |
2278 | 5.85k | { |
2279 | 5.85k | RecordLayerHeader* rl; |
2280 | | |
2281 | 5.85k | rl = (RecordLayerHeader*)output; |
2282 | 5.85k | rl->type = type; |
2283 | 5.85k | rl->pvMajor = ssl->version.major; |
2284 | | /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */ |
2285 | 5.85k | rl->pvMinor = TLSv1_2_MINOR; |
2286 | 5.85k | c16toa((word16)length, rl->length); |
2287 | 5.85k | } |
2288 | | |
2289 | | /* Add handshake header to message. |
2290 | | * |
2291 | | * output The buffer to write the handshake header into. |
2292 | | * length The length of the handshake data. |
2293 | | * fragOffset The offset of the fragment data. (DTLS) |
2294 | | * fragLength The length of the fragment data. (DTLS) |
2295 | | * type The type of handshake message. |
2296 | | * ssl The SSL/TLS object. (DTLS) |
2297 | | */ |
2298 | | static void AddTls13HandShakeHeader(byte* output, word32 length, |
2299 | | word32 fragOffset, word32 fragLength, |
2300 | | byte type, WOLFSSL* ssl) |
2301 | | { |
2302 | | HandShakeHeader* hs; |
2303 | | (void)fragOffset; |
2304 | | (void)fragLength; |
2305 | | (void)ssl; |
2306 | | |
2307 | | #ifdef WOLFSSL_DTLS13 |
2308 | | /* message_hash type is used for a synthetic message that replaces the first |
2309 | | ClientHello in the hash transcript when using HelloRetryRequest. It will |
2310 | | never be transmitted and, as the DTLS-only fields must not be considered |
2311 | | when computing the hash transcript, we can avoid to use the DTLS |
2312 | | handshake header. */ |
2313 | | if (ssl->options.dtls && type != message_hash) { |
2314 | | Dtls13HandshakeAddHeader(ssl, output, (enum HandShakeType)type, length); |
2315 | | return; |
2316 | | } |
2317 | | #endif /* WOLFSSL_DTLS13 */ |
2318 | | |
2319 | | /* handshake header */ |
2320 | | hs = (HandShakeHeader*)output; |
2321 | | hs->type = type; |
2322 | | c32to24(length, hs->length); |
2323 | | } |
2324 | | |
2325 | | |
2326 | | /* Add both record layer and handshake header to message. |
2327 | | * |
2328 | | * output The buffer to write the headers into. |
2329 | | * length The length of the handshake data. |
2330 | | * type The type of record layer message. |
2331 | | * ssl The SSL/TLS object. (DTLS) |
2332 | | */ |
2333 | | static void AddTls13Headers(byte* output, word32 length, byte type, |
2334 | | WOLFSSL* ssl) |
2335 | | { |
2336 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2337 | | word32 outputAdj = RECORD_HEADER_SZ; |
2338 | | |
2339 | | #ifdef WOLFSSL_DTLS13 |
2340 | | if (ssl->options.dtls) { |
2341 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2342 | | return; |
2343 | | } |
2344 | | #endif /* WOLFSSL_DTLS13 */ |
2345 | | |
2346 | | AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl); |
2347 | | AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl); |
2348 | | } |
2349 | | |
2350 | | #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) \ |
2351 | | && !defined(NO_CERTS) |
2352 | | /* Add both record layer and fragment handshake header to message. |
2353 | | * |
2354 | | * output The buffer to write the headers into. |
2355 | | * fragOffset The offset of the fragment data. (DTLS) |
2356 | | * fragLength The length of the fragment data. (DTLS) |
2357 | | * length The length of the handshake data. |
2358 | | * type The type of record layer message. |
2359 | | * ssl The SSL/TLS object. (DTLS) |
2360 | | */ |
2361 | | static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset, |
2362 | | word32 length, byte type, WOLFSSL* ssl) |
2363 | | { |
2364 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2365 | | word32 outputAdj = RECORD_HEADER_SZ; |
2366 | | (void)fragSz; |
2367 | | |
2368 | | #ifdef WOLFSSL_DTLS13 |
2369 | | /* we ignore fragmentation fields here because fragmentation logic for |
2370 | | DTLS1.3 is inside dtls13_handshake_send(). */ |
2371 | | if (ssl->options.dtls) { |
2372 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2373 | | return; |
2374 | | } |
2375 | | #endif /* WOLFSSL_DTLS13 */ |
2376 | | |
2377 | | AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl); |
2378 | | AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz, |
2379 | | type, ssl); |
2380 | | } |
2381 | | #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !NO_CERTS */ |
2382 | | |
2383 | | /* Write the sequence number into the buffer. |
2384 | | * No DTLS v1.3 support. |
2385 | | * |
2386 | | * ssl The SSL/TLS object. |
2387 | | * verifyOrder Which set of sequence numbers to use. |
2388 | | * out The buffer to write into. |
2389 | | */ |
2390 | | static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out) |
2391 | 7.74k | { |
2392 | 7.74k | word32 seq[2] = {0, 0}; |
2393 | | |
2394 | 7.74k | if (ssl->options.dtls) { |
2395 | | #ifdef WOLFSSL_DTLS13 |
2396 | | Dtls13GetSeq(ssl, verifyOrder, seq, 1); |
2397 | | #endif /* WOLFSSL_DTLS13 */ |
2398 | 0 | } |
2399 | 7.74k | else if (verifyOrder == PEER_ORDER) { |
2400 | 5.35k | seq[0] = ssl->keys.peer_sequence_number_hi; |
2401 | 5.35k | seq[1] = ssl->keys.peer_sequence_number_lo++; |
2402 | | /* handle rollover */ |
2403 | 5.35k | if (seq[1] > ssl->keys.peer_sequence_number_lo) |
2404 | 12 | ssl->keys.peer_sequence_number_hi++; |
2405 | 5.35k | } |
2406 | 2.39k | else { |
2407 | 2.39k | seq[0] = ssl->keys.sequence_number_hi; |
2408 | 2.39k | seq[1] = ssl->keys.sequence_number_lo++; |
2409 | | /* handle rollover */ |
2410 | 2.39k | if (seq[1] > ssl->keys.sequence_number_lo) |
2411 | 0 | ssl->keys.sequence_number_hi++; |
2412 | 2.39k | } |
2413 | | #ifdef WOLFSSL_DEBUG_TLS |
2414 | | WOLFSSL_MSG_EX("TLS 1.3 Write Sequence %d %d", seq[0], seq[1]); |
2415 | | #endif |
2416 | | |
2417 | 7.74k | c32toa(seq[0], out); |
2418 | 7.74k | c32toa(seq[1], out + OPAQUE32_LEN); |
2419 | 7.74k | } |
2420 | | |
2421 | | /* Build the nonce for TLS v1.3 encryption and decryption. |
2422 | | * |
2423 | | * ssl The SSL/TLS object. |
2424 | | * nonce The nonce data to use when encrypting or decrypting. |
2425 | | * iv The derived IV. |
2426 | | * order The side on which the message is to be or was sent. |
2427 | | */ |
2428 | | static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv, |
2429 | | int order) |
2430 | 7.74k | { |
2431 | 7.74k | int seq_offset = AEAD_NONCE_SZ - SEQ_SZ; |
2432 | | /* The nonce is the IV with the sequence XORed into the last bytes. */ |
2433 | 7.74k | WriteSEQTls13(ssl, order, nonce + seq_offset); |
2434 | 7.74k | XMEMCPY(nonce, iv, seq_offset); |
2435 | 7.74k | xorbuf(nonce + seq_offset, iv + seq_offset, SEQ_SZ); |
2436 | 7.74k | } |
2437 | | |
2438 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2439 | | /* Encrypt with ChaCha20 and create authentication tag with Poly1305. |
2440 | | * |
2441 | | * ssl The SSL/TLS object. |
2442 | | * output The buffer to write encrypted data and authentication tag into. |
2443 | | * May be the same pointer as input. |
2444 | | * input The data to encrypt. |
2445 | | * sz The number of bytes to encrypt. |
2446 | | * nonce The nonce to use with ChaCha20. |
2447 | | * aad The additional authentication data. |
2448 | | * aadSz The size of the addition authentication data. |
2449 | | * tag The authentication tag buffer. |
2450 | | * returns 0 on success, otherwise failure. |
2451 | | */ |
2452 | | static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output, |
2453 | | const byte* input, word16 sz, byte* nonce, |
2454 | | const byte* aad, word16 aadSz, byte* tag) |
2455 | 769 | { |
2456 | 769 | int ret = 0; |
2457 | 769 | byte poly[CHACHA20_256_KEY_SIZE]; |
2458 | | |
2459 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2460 | 769 | XMEMSET(poly, 0, sizeof(poly)); |
2461 | | |
2462 | | /* Set the nonce for ChaCha and get Poly1305 key. */ |
2463 | 769 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0); |
2464 | 769 | if (ret != 0) |
2465 | 0 | return ret; |
2466 | | /* Create Poly1305 key using ChaCha20 keystream. */ |
2467 | 769 | ret = wc_Chacha_Process(ssl->encrypt.chacha, poly, poly, sizeof(poly)); |
2468 | 769 | if (ret != 0) |
2469 | 0 | return ret; |
2470 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2471 | | wc_MemZero_Add("ChaCha20Poly1305_Encrypt poly", poly, sizeof(poly)); |
2472 | | #endif |
2473 | 769 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1); |
2474 | 769 | if (ret != 0) |
2475 | 0 | return ret; |
2476 | | /* Encrypt the plain text. */ |
2477 | 769 | ret = wc_Chacha_Process(ssl->encrypt.chacha, output, input, sz); |
2478 | 769 | if (ret != 0) { |
2479 | 0 | ForceZero(poly, sizeof(poly)); |
2480 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2481 | | wc_MemZero_Check(poly, sizeof(poly)); |
2482 | | #endif |
2483 | 0 | return ret; |
2484 | 0 | } |
2485 | | |
2486 | | /* Set key for Poly1305. */ |
2487 | 769 | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2488 | 769 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2489 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2490 | | wc_MemZero_Check(poly, sizeof(poly)); |
2491 | | #endif |
2492 | 769 | if (ret != 0) |
2493 | 0 | return ret; |
2494 | | /* Add authentication code of encrypted data to end. */ |
2495 | 769 | ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, output, sz, tag, |
2496 | 769 | POLY1305_AUTH_SZ); |
2497 | | |
2498 | 769 | return ret; |
2499 | 769 | } |
2500 | | #endif |
2501 | | |
2502 | | #ifdef HAVE_NULL_CIPHER |
2503 | | /* Create authentication tag and copy data over input. |
2504 | | * |
2505 | | * ssl The SSL/TLS object. |
2506 | | * output The buffer to copy data into. |
2507 | | * May be the same pointer as input. |
2508 | | * input The data. |
2509 | | * sz The number of bytes of data. |
2510 | | * nonce The nonce to use with authentication. |
2511 | | * aad The additional authentication data. |
2512 | | * aadSz The size of the addition authentication data. |
2513 | | * tag The authentication tag buffer. |
2514 | | * returns 0 on success, otherwise failure. |
2515 | | */ |
2516 | | static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output, |
2517 | | const byte* input, word16 sz, |
2518 | | const byte* nonce, |
2519 | | const byte* aad, word16 aadSz, byte* tag) |
2520 | | { |
2521 | | int ret; |
2522 | | |
2523 | | /* HMAC: nonce | aad | input */ |
2524 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, HMAC_NONCE_SZ); |
2525 | | if (ret == 0) |
2526 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz); |
2527 | | if (ret == 0) |
2528 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz); |
2529 | | if (ret == 0) |
2530 | | ret = wc_HmacFinal(ssl->encrypt.hmac, tag); |
2531 | | /* Copy the input to output if not the same buffer */ |
2532 | | if (ret == 0 && output != input) |
2533 | | XMEMCPY(output, input, sz); |
2534 | | return ret; |
2535 | | } |
2536 | | #endif |
2537 | | |
2538 | | /* Encrypt data for TLS v1.3. |
2539 | | * |
2540 | | * ssl The SSL/TLS object. |
2541 | | * output The buffer to write encrypted data and authentication tag into. |
2542 | | * May be the same pointer as input. |
2543 | | * input The record header and data to encrypt. |
2544 | | * sz The number of bytes to encrypt. |
2545 | | * aad The additional authentication data. |
2546 | | * aadSz The size of the addition authentication data. |
2547 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
2548 | | * returns 0 on success, otherwise failure. |
2549 | | */ |
2550 | | static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, |
2551 | | word16 sz, const byte* aad, word16 aadSz, int asyncOkay) |
2552 | 0 | { |
2553 | 0 | int ret = 0; |
2554 | 0 | word16 dataSz = sz - ssl->specs.aead_mac_size; |
2555 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2556 | 0 | word32 nonceSz = 0; |
2557 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2558 | | WC_ASYNC_DEV* asyncDev = NULL; |
2559 | | word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN; |
2560 | | #endif |
2561 | |
|
2562 | 0 | WOLFSSL_ENTER("EncryptTls13"); |
2563 | |
|
2564 | 0 | (void)output; |
2565 | 0 | (void)input; |
2566 | 0 | (void)sz; |
2567 | 0 | (void)dataSz; |
2568 | 0 | (void)macSz; |
2569 | 0 | (void)asyncOkay; |
2570 | 0 | (void)nonceSz; |
2571 | |
|
2572 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2573 | | if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2574 | | ssl->error = 0; /* clear async */ |
2575 | | } |
2576 | | #endif |
2577 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2578 | | ret = tsip_Tls13AesEncrypt(ssl, output, input, dataSz); |
2579 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2580 | | if (ret > 0) { |
2581 | | ret = 0; /* tsip_Tls13AesEncrypt returns output size */ |
2582 | | } |
2583 | | return ret; |
2584 | | } |
2585 | | ret = 0; |
2586 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
2587 | |
|
2588 | 0 | switch (ssl->encrypt.state) { |
2589 | 0 | case CIPHER_STATE_BEGIN: |
2590 | 0 | { |
2591 | | #ifdef WOLFSSL_DEBUG_TLS |
2592 | | WOLFSSL_MSG("Data to encrypt"); |
2593 | | WOLFSSL_BUFFER(input, dataSz); |
2594 | | WOLFSSL_MSG("Additional Authentication Data"); |
2595 | | WOLFSSL_BUFFER(aad, aadSz); |
2596 | | #endif |
2597 | |
|
2598 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2599 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) { |
2600 | | XMEMCPY(ssl->encrypt.sanityCheck, input, |
2601 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))); |
2602 | | } |
2603 | | #endif |
2604 | |
|
2605 | 0 | #ifdef CIPHER_NONCE |
2606 | 0 | if (ssl->encrypt.nonce == NULL) { |
2607 | 0 | ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ, |
2608 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
2609 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2610 | | if (ssl->encrypt.nonce != NULL) { |
2611 | | wc_MemZero_Add("EncryptTls13 nonce", ssl->encrypt.nonce, |
2612 | | AEAD_NONCE_SZ); |
2613 | | } |
2614 | | #endif |
2615 | 0 | } |
2616 | 0 | if (ssl->encrypt.nonce == NULL) |
2617 | 0 | return MEMORY_E; |
2618 | | |
2619 | 0 | BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV, |
2620 | 0 | CUR_ORDER); |
2621 | 0 | #endif |
2622 | | |
2623 | | /* Advance state and proceed */ |
2624 | 0 | ssl->encrypt.state = CIPHER_STATE_DO; |
2625 | 0 | } |
2626 | 0 | FALL_THROUGH; |
2627 | |
|
2628 | 0 | case CIPHER_STATE_DO: |
2629 | 0 | { |
2630 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
2631 | 0 | #ifdef BUILD_AESGCM |
2632 | 0 | case wolfssl_aes_gcm: |
2633 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2634 | | /* initialize event */ |
2635 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2636 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2637 | | if (ret != 0) |
2638 | | break; |
2639 | | #endif |
2640 | |
|
2641 | 0 | nonceSz = AESGCM_NONCE_SZ; |
2642 | |
|
2643 | | #if defined(HAVE_PK_CALLBACKS) |
2644 | | ret = NOT_COMPILED_IN; |
2645 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2646 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2647 | | output, input, dataSz, |
2648 | | ssl->encrypt.nonce, nonceSz, |
2649 | | output + dataSz, macSz, |
2650 | | aad, aadSz); |
2651 | | } |
2652 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2653 | | #endif |
2654 | 0 | { |
2655 | |
|
2656 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2657 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2658 | | ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, |
2659 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2660 | | output + dataSz, macSz, aad, aadSz); |
2661 | | #else |
2662 | 0 | ret = wc_AesGcmSetExtIV(ssl->encrypt.aes, |
2663 | 0 | ssl->encrypt.nonce, nonceSz); |
2664 | 0 | if (ret == 0) { |
2665 | 0 | ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output, |
2666 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2667 | 0 | output + dataSz, macSz, aad, aadSz); |
2668 | 0 | } |
2669 | 0 | #endif |
2670 | 0 | } |
2671 | 0 | break; |
2672 | 0 | #endif |
2673 | | |
2674 | 0 | #ifdef HAVE_AESCCM |
2675 | 0 | case wolfssl_aes_ccm: |
2676 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2677 | | /* initialize event */ |
2678 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2679 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2680 | | if (ret != 0) |
2681 | | break; |
2682 | | #endif |
2683 | |
|
2684 | 0 | nonceSz = AESCCM_NONCE_SZ; |
2685 | | #if defined(HAVE_PK_CALLBACKS) |
2686 | | ret = NOT_COMPILED_IN; |
2687 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2688 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2689 | | output, input, dataSz, |
2690 | | ssl->encrypt.nonce, nonceSz, |
2691 | | output + dataSz, macSz, |
2692 | | aad, aadSz); |
2693 | | } |
2694 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2695 | | #endif |
2696 | 0 | { |
2697 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2698 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2699 | | ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, |
2700 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2701 | | output + dataSz, macSz, aad, aadSz); |
2702 | | #else |
2703 | 0 | ret = wc_AesCcmSetNonce(ssl->encrypt.aes, |
2704 | 0 | ssl->encrypt.nonce, nonceSz); |
2705 | 0 | if (ret == 0) { |
2706 | 0 | ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output, |
2707 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2708 | 0 | output + dataSz, macSz, aad, aadSz); |
2709 | 0 | } |
2710 | 0 | #endif |
2711 | 0 | } |
2712 | 0 | break; |
2713 | 0 | #endif |
2714 | | |
2715 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2716 | 0 | case wolfssl_chacha: |
2717 | 0 | ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz, |
2718 | 0 | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2719 | 0 | break; |
2720 | 0 | #endif |
2721 | | |
2722 | 0 | #ifdef WOLFSSL_SM4_GCM |
2723 | 0 | case wolfssl_sm4_gcm: |
2724 | 0 | nonceSz = SM4_GCM_NONCE_SZ; |
2725 | 0 | ret = wc_Sm4GcmEncrypt(ssl->encrypt.sm4, output, input, |
2726 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2727 | 0 | macSz, aad, aadSz); |
2728 | 0 | break; |
2729 | 0 | #endif |
2730 | | |
2731 | 0 | #ifdef WOLFSSL_SM4_CCM |
2732 | 0 | case wolfssl_sm4_ccm: |
2733 | 0 | nonceSz = SM4_CCM_NONCE_SZ; |
2734 | 0 | ret = wc_Sm4CcmEncrypt(ssl->encrypt.sm4, output, input, |
2735 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2736 | 0 | macSz, aad, aadSz); |
2737 | 0 | break; |
2738 | 0 | #endif |
2739 | | |
2740 | | #ifdef HAVE_NULL_CIPHER |
2741 | | case wolfssl_cipher_null: |
2742 | | ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz, |
2743 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2744 | | break; |
2745 | | #endif |
2746 | | |
2747 | 0 | default: |
2748 | 0 | WOLFSSL_MSG("wolfSSL Encrypt programming error"); |
2749 | 0 | return ENCRYPT_ERROR; |
2750 | 0 | } |
2751 | | |
2752 | | /* Advance state */ |
2753 | 0 | ssl->encrypt.state = CIPHER_STATE_END; |
2754 | |
|
2755 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2756 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2757 | | /* if async is not okay, then block */ |
2758 | | if (!asyncOkay) { |
2759 | | ret = wc_AsyncWait(ret, asyncDev, event_flags); |
2760 | | } |
2761 | | else { |
2762 | | /* If pending, then leave and return will resume below */ |
2763 | | return wolfSSL_AsyncPush(ssl, asyncDev); |
2764 | | } |
2765 | | } |
2766 | | #endif |
2767 | 0 | } |
2768 | 0 | FALL_THROUGH; |
2769 | |
|
2770 | 0 | case CIPHER_STATE_END: |
2771 | 0 | { |
2772 | | #ifdef WOLFSSL_DEBUG_TLS |
2773 | | #ifdef CIPHER_NONCE |
2774 | | WOLFSSL_MSG("Nonce"); |
2775 | | WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size); |
2776 | | #endif |
2777 | | WOLFSSL_MSG("Encrypted data"); |
2778 | | WOLFSSL_BUFFER(output, dataSz); |
2779 | | WOLFSSL_MSG("Authentication Tag"); |
2780 | | WOLFSSL_BUFFER(output + dataSz, macSz); |
2781 | | #endif |
2782 | |
|
2783 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2784 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2785 | | XMEMCMP(output, ssl->encrypt.sanityCheck, |
2786 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))) == 0) { |
2787 | | |
2788 | | WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?"); |
2789 | | return ENCRYPT_ERROR; |
2790 | | } |
2791 | | ForceZero(ssl->encrypt.sanityCheck, |
2792 | | sizeof(ssl->encrypt.sanityCheck)); |
2793 | | #endif |
2794 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2795 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
2796 | | (output != input) && (ret == 0)) { |
2797 | | wc_MemZero_Add("TLS 1.3 Encrypt plaintext", input, sz); |
2798 | | } |
2799 | | #endif |
2800 | |
|
2801 | 0 | #ifdef CIPHER_NONCE |
2802 | 0 | ForceZero(ssl->encrypt.nonce, AEAD_NONCE_SZ); |
2803 | 0 | #endif |
2804 | |
|
2805 | 0 | break; |
2806 | 0 | } |
2807 | | |
2808 | 0 | default: |
2809 | 0 | break; |
2810 | 0 | } |
2811 | | |
2812 | | |
2813 | | /* Reset state */ |
2814 | 0 | ssl->encrypt.state = CIPHER_STATE_BEGIN; |
2815 | |
|
2816 | 0 | return ret; |
2817 | 0 | } |
2818 | | |
2819 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2820 | | /* Decrypt with ChaCha20 and check authentication tag with Poly1305. |
2821 | | * |
2822 | | * ssl The SSL/TLS object. |
2823 | | * output The buffer to write decrypted data into. |
2824 | | * May be the same pointer as input. |
2825 | | * input The data to decrypt. |
2826 | | * sz The number of bytes to decrypt. |
2827 | | * nonce The nonce to use with ChaCha20. |
2828 | | * aad The additional authentication data. |
2829 | | * aadSz The size of the addition authentication data. |
2830 | | * tagIn The authentication tag data from packet. |
2831 | | * returns 0 on success, otherwise failure. |
2832 | | */ |
2833 | | static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output, |
2834 | | const byte* input, word16 sz, byte* nonce, |
2835 | | const byte* aad, word16 aadSz, |
2836 | | const byte* tagIn) |
2837 | 2.24k | { |
2838 | 2.24k | int ret; |
2839 | 2.24k | byte tag[POLY1305_AUTH_SZ]; |
2840 | 2.24k | byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */ |
2841 | | |
2842 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2843 | 2.24k | XMEMSET(poly, 0, sizeof(poly)); |
2844 | | |
2845 | | /* Set nonce and get Poly1305 key. */ |
2846 | 2.24k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0); |
2847 | 2.24k | if (ret != 0) |
2848 | 0 | return ret; |
2849 | | /* Use ChaCha20 keystream to get Poly1305 key for tag. */ |
2850 | 2.24k | ret = wc_Chacha_Process(ssl->decrypt.chacha, poly, poly, sizeof(poly)); |
2851 | 2.24k | if (ret != 0) |
2852 | 0 | return ret; |
2853 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2854 | | wc_MemZero_Add("ChaCha20Poly1305_Decrypt poly", poly, sizeof(poly)); |
2855 | | #endif |
2856 | 2.24k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1); |
2857 | 2.24k | if (ret != 0) { |
2858 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2859 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2860 | | wc_MemZero_Check(poly, sizeof(poly)); |
2861 | | #endif |
2862 | 0 | return ret; |
2863 | 0 | } |
2864 | | |
2865 | | /* Set key for Poly1305. */ |
2866 | 2.24k | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2867 | 2.24k | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2868 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2869 | | wc_MemZero_Check(poly, sizeof(poly)); |
2870 | | #endif |
2871 | 2.24k | if (ret != 0) |
2872 | 0 | return ret; |
2873 | | /* Generate authentication tag for encrypted data. */ |
2874 | 2.24k | if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, input, sz, tag, |
2875 | 2.24k | sizeof(tag))) != 0) { |
2876 | 0 | return ret; |
2877 | 0 | } |
2878 | | |
2879 | | /* Check tag sent along with packet. */ |
2880 | 2.24k | if (ConstantCompare(tagIn, tag, POLY1305_AUTH_SZ) != 0) { |
2881 | 2.24k | WOLFSSL_MSG("MAC did not match"); |
2882 | 2.24k | return VERIFY_MAC_ERROR; |
2883 | 2.24k | } |
2884 | | |
2885 | | /* If the tag was good decrypt message. */ |
2886 | 0 | ret = wc_Chacha_Process(ssl->decrypt.chacha, output, input, sz); |
2887 | |
|
2888 | 0 | return ret; |
2889 | 2.24k | } |
2890 | | #endif |
2891 | | |
2892 | | #ifdef HAVE_NULL_CIPHER |
2893 | | /* Check HMAC tag and copy over input. |
2894 | | * |
2895 | | * ssl The SSL/TLS object. |
2896 | | * output The buffer to copy data into. |
2897 | | * May be the same pointer as input. |
2898 | | * input The data. |
2899 | | * sz The number of bytes of data. |
2900 | | * nonce The nonce to use with authentication. |
2901 | | * aad The additional authentication data. |
2902 | | * aadSz The size of the addition authentication data. |
2903 | | * tagIn The authentication tag data from packet. |
2904 | | * returns 0 on success, otherwise failure. |
2905 | | */ |
2906 | | static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output, |
2907 | | const byte* input, word16 sz, |
2908 | | const byte* nonce, |
2909 | | const byte* aad, word16 aadSz, |
2910 | | const byte* tagIn) |
2911 | | { |
2912 | | int ret; |
2913 | | byte hmac[WC_MAX_DIGEST_SIZE]; |
2914 | | |
2915 | | /* HMAC: nonce | aad | input */ |
2916 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, HMAC_NONCE_SZ); |
2917 | | if (ret == 0) |
2918 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz); |
2919 | | if (ret == 0) |
2920 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz); |
2921 | | if (ret == 0) |
2922 | | ret = wc_HmacFinal(ssl->decrypt.hmac, hmac); |
2923 | | /* Check authentication tag matches */ |
2924 | | if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0) |
2925 | | ret = DECRYPT_ERROR; |
2926 | | /* Copy the input to output if not the same buffer */ |
2927 | | if (ret == 0 && output != input) |
2928 | | XMEMCPY(output, input, sz); |
2929 | | return ret; |
2930 | | } |
2931 | | #endif |
2932 | | |
2933 | | /* Decrypt data for TLS v1.3. |
2934 | | * |
2935 | | * ssl The SSL/TLS object. |
2936 | | * output The buffer to write decrypted data into. |
2937 | | * May be the same pointer as input. |
2938 | | * input The data to decrypt and authentication tag. |
2939 | | * sz The length of the encrypted data plus authentication tag. |
2940 | | * aad The additional authentication data. |
2941 | | * aadSz The size of the addition authentication data. |
2942 | | * returns 0 on success, otherwise failure. |
2943 | | */ |
2944 | | int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz, |
2945 | | const byte* aad, word16 aadSz) |
2946 | | { |
2947 | | int ret = 0; |
2948 | | word16 dataSz = sz - ssl->specs.aead_mac_size; |
2949 | | word16 macSz = ssl->specs.aead_mac_size; |
2950 | | word32 nonceSz = 0; |
2951 | | |
2952 | | WOLFSSL_ENTER("DecryptTls13"); |
2953 | | |
2954 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2955 | | ret = tsip_Tls13AesDecrypt(ssl, output, input, sz); |
2956 | | |
2957 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2958 | | #ifndef WOLFSSL_EARLY_DATA |
2959 | | if (ret < 0) { |
2960 | | ret = VERIFY_MAC_ERROR; |
2961 | | WOLFSSL_ERROR_VERBOSE(ret); |
2962 | | } |
2963 | | #endif |
2964 | | return ret; |
2965 | | } |
2966 | | #endif |
2967 | | |
2968 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2969 | | ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state); |
2970 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
2971 | | /* check for still pending */ |
2972 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
2973 | | return ret; |
2974 | | |
2975 | | ssl->error = 0; /* clear async */ |
2976 | | |
2977 | | /* let failures through so CIPHER_STATE_END logic is run */ |
2978 | | } |
2979 | | else |
2980 | | #endif |
2981 | | { |
2982 | | /* Reset state */ |
2983 | | ret = 0; |
2984 | | ssl->decrypt.state = CIPHER_STATE_BEGIN; |
2985 | | } |
2986 | | |
2987 | | (void)output; |
2988 | | (void)input; |
2989 | | (void)sz; |
2990 | | (void)dataSz; |
2991 | | (void)macSz; |
2992 | | (void)nonceSz; |
2993 | | |
2994 | | switch (ssl->decrypt.state) { |
2995 | | case CIPHER_STATE_BEGIN: |
2996 | | { |
2997 | | #ifdef WOLFSSL_DEBUG_TLS |
2998 | | WOLFSSL_MSG("Data to decrypt"); |
2999 | | WOLFSSL_BUFFER(input, dataSz); |
3000 | | WOLFSSL_MSG("Additional Authentication Data"); |
3001 | | WOLFSSL_BUFFER(aad, aadSz); |
3002 | | WOLFSSL_MSG("Authentication tag"); |
3003 | | WOLFSSL_BUFFER(input + dataSz, macSz); |
3004 | | #endif |
3005 | | |
3006 | | #ifdef CIPHER_NONCE |
3007 | | if (ssl->decrypt.nonce == NULL) { |
3008 | | ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ, |
3009 | | ssl->heap, DYNAMIC_TYPE_CIPHER); |
3010 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3011 | | if (ssl->decrypt.nonce != NULL) { |
3012 | | wc_MemZero_Add("DecryptTls13 nonce", ssl->decrypt.nonce, |
3013 | | AEAD_NONCE_SZ); |
3014 | | } |
3015 | | #endif |
3016 | | } |
3017 | | if (ssl->decrypt.nonce == NULL) |
3018 | | return MEMORY_E; |
3019 | | |
3020 | | BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV, |
3021 | | PEER_ORDER); |
3022 | | #endif |
3023 | | |
3024 | | /* Advance state and proceed */ |
3025 | | ssl->decrypt.state = CIPHER_STATE_DO; |
3026 | | } |
3027 | | FALL_THROUGH; |
3028 | | |
3029 | | case CIPHER_STATE_DO: |
3030 | | { |
3031 | | switch (ssl->specs.bulk_cipher_algorithm) { |
3032 | | #ifdef BUILD_AESGCM |
3033 | | case wolfssl_aes_gcm: |
3034 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3035 | | /* initialize event */ |
3036 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3037 | | WC_ASYNC_FLAG_NONE); |
3038 | | if (ret != 0) |
3039 | | break; |
3040 | | #endif |
3041 | | |
3042 | | nonceSz = AESGCM_NONCE_SZ; |
3043 | | |
3044 | | #if defined(HAVE_PK_CALLBACKS) |
3045 | | ret = NOT_COMPILED_IN; |
3046 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3047 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3048 | | output, input, dataSz, |
3049 | | ssl->decrypt.nonce, nonceSz, |
3050 | | (byte *)(input + dataSz), macSz, |
3051 | | aad, aadSz); |
3052 | | } |
3053 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3054 | | #endif |
3055 | | { |
3056 | | |
3057 | | ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, |
3058 | | dataSz, ssl->decrypt.nonce, nonceSz, |
3059 | | input + dataSz, macSz, aad, aadSz); |
3060 | | |
3061 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3062 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3063 | | ret = wolfSSL_AsyncPush(ssl, |
3064 | | &ssl->decrypt.aes->asyncDev); |
3065 | | } |
3066 | | #endif |
3067 | | |
3068 | | } |
3069 | | break; |
3070 | | #endif |
3071 | | |
3072 | | #ifdef HAVE_AESCCM |
3073 | | case wolfssl_aes_ccm: |
3074 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3075 | | /* initialize event */ |
3076 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3077 | | WC_ASYNC_FLAG_NONE); |
3078 | | if (ret != 0) |
3079 | | break; |
3080 | | #endif |
3081 | | |
3082 | | nonceSz = AESCCM_NONCE_SZ; |
3083 | | #if defined(HAVE_PK_CALLBACKS) |
3084 | | ret = NOT_COMPILED_IN; |
3085 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3086 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3087 | | output, input, dataSz, |
3088 | | ssl->decrypt.nonce, nonceSz, |
3089 | | (byte *)(input + dataSz), macSz, |
3090 | | aad, aadSz); |
3091 | | } |
3092 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3093 | | #endif |
3094 | | { |
3095 | | ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, |
3096 | | dataSz, ssl->decrypt.nonce, nonceSz, |
3097 | | input + dataSz, macSz, aad, aadSz); |
3098 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3099 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3100 | | ret = wolfSSL_AsyncPush(ssl, |
3101 | | &ssl->decrypt.aes->asyncDev); |
3102 | | } |
3103 | | #endif |
3104 | | } |
3105 | | break; |
3106 | | #endif |
3107 | | |
3108 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
3109 | | case wolfssl_chacha: |
3110 | | ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz, |
3111 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3112 | | break; |
3113 | | #endif |
3114 | | |
3115 | | #ifdef WOLFSSL_SM4_GCM |
3116 | | case wolfssl_sm4_gcm: |
3117 | | nonceSz = SM4_GCM_NONCE_SZ; |
3118 | | ret = wc_Sm4GcmDecrypt(ssl->decrypt.sm4, output, input, |
3119 | | dataSz, ssl->decrypt.nonce, nonceSz, output + dataSz, |
3120 | | macSz, aad, aadSz); |
3121 | | break; |
3122 | | #endif |
3123 | | |
3124 | | #ifdef WOLFSSL_SM4_CCM |
3125 | | case wolfssl_sm4_ccm: |
3126 | | nonceSz = SM4_CCM_NONCE_SZ; |
3127 | | ret = wc_Sm4CcmDecrypt(ssl->decrypt.sm4, output, input, |
3128 | | dataSz, ssl->decrypt.nonce, nonceSz, output + dataSz, |
3129 | | macSz, aad, aadSz); |
3130 | | break; |
3131 | | #endif |
3132 | | |
3133 | | #ifdef HAVE_NULL_CIPHER |
3134 | | case wolfssl_cipher_null: |
3135 | | ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz, |
3136 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3137 | | break; |
3138 | | #endif |
3139 | | default: |
3140 | | WOLFSSL_MSG("wolfSSL Decrypt programming error"); |
3141 | | return DECRYPT_ERROR; |
3142 | | } |
3143 | | |
3144 | | /* Advance state */ |
3145 | | ssl->decrypt.state = CIPHER_STATE_END; |
3146 | | |
3147 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3148 | | /* If pending, leave now */ |
3149 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3150 | | return ret; |
3151 | | } |
3152 | | #endif |
3153 | | } |
3154 | | FALL_THROUGH; |
3155 | | |
3156 | | case CIPHER_STATE_END: |
3157 | | { |
3158 | | #ifdef WOLFSSL_DEBUG_TLS |
3159 | | #ifdef CIPHER_NONCE |
3160 | | WOLFSSL_MSG("Nonce"); |
3161 | | WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size); |
3162 | | #endif |
3163 | | WOLFSSL_MSG("Decrypted data"); |
3164 | | WOLFSSL_BUFFER(output, dataSz); |
3165 | | #endif |
3166 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3167 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
3168 | | (ret == 0)) { |
3169 | | wc_MemZero_Add("TLS 1.3 Decrypted data", output, sz); |
3170 | | } |
3171 | | #endif |
3172 | | |
3173 | | #ifdef CIPHER_NONCE |
3174 | | ForceZero(ssl->decrypt.nonce, AEAD_NONCE_SZ); |
3175 | | #endif |
3176 | | |
3177 | | break; |
3178 | | } |
3179 | | |
3180 | | default: |
3181 | | break; |
3182 | | } |
3183 | | |
3184 | | if (ret < 0) { |
3185 | | WOLFSSL_ERROR_VERBOSE(ret); |
3186 | | } |
3187 | | |
3188 | | return ret; |
3189 | | } |
3190 | | |
3191 | | /* Persistable BuildTls13Message arguments */ |
3192 | | typedef struct BuildMsg13Args { |
3193 | | word32 sz; |
3194 | | word32 idx; |
3195 | | word32 headerSz; |
3196 | | word16 size; |
3197 | | word32 paddingSz; |
3198 | | } BuildMsg13Args; |
3199 | | |
3200 | | static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs) |
3201 | 2.40k | { |
3202 | 2.40k | BuildMsg13Args* args = (BuildMsg13Args*)pArgs; |
3203 | | |
3204 | 2.40k | (void)ssl; |
3205 | 2.40k | (void)args; |
3206 | | |
3207 | | /* no allocations in BuildTls13Message */ |
3208 | 2.40k | } |
3209 | | |
3210 | | /* Build SSL Message, encrypted. |
3211 | | * TLS v1.3 encryption is AEAD only. |
3212 | | * |
3213 | | * ssl The SSL/TLS object. |
3214 | | * output The buffer to write record message to. |
3215 | | * outSz Size of the buffer being written into. |
3216 | | * input The record data to encrypt (excluding record header). |
3217 | | * inSz The size of the record data. |
3218 | | * type The recorder header content type. |
3219 | | * hashOutput Whether to hash the unencrypted record data. |
3220 | | * sizeOnly Only want the size of the record message. |
3221 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
3222 | | * returns the size of the encrypted record message or negative value on error. |
3223 | | */ |
3224 | | int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input, |
3225 | | int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay) |
3226 | | { |
3227 | | int ret; |
3228 | | BuildMsg13Args* args; |
3229 | | BuildMsg13Args lcl_args; |
3230 | | |
3231 | | WOLFSSL_ENTER("BuildTls13Message"); |
3232 | | |
3233 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3234 | | ret = WC_NO_PENDING_E; |
3235 | | if (asyncOkay) { |
3236 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
3237 | | |
3238 | | if (ssl->async == NULL) { |
3239 | | ssl->async = (struct WOLFSSL_ASYNC*) |
3240 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
3241 | | DYNAMIC_TYPE_ASYNC); |
3242 | | if (ssl->async == NULL) |
3243 | | return MEMORY_E; |
3244 | | } |
3245 | | args = (BuildMsg13Args*)ssl->async->args; |
3246 | | |
3247 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState); |
3248 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3249 | | /* Check for error */ |
3250 | | if (ret < 0) |
3251 | | goto exit_buildmsg; |
3252 | | } |
3253 | | } |
3254 | | else |
3255 | | #endif |
3256 | | { |
3257 | | args = &lcl_args; |
3258 | | } |
3259 | | |
3260 | | /* Reset state */ |
3261 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3262 | | if (ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) |
3263 | | #endif |
3264 | | { |
3265 | | ret = 0; |
3266 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3267 | | XMEMSET(args, 0, sizeof(BuildMsg13Args)); |
3268 | | |
3269 | | args->headerSz = RECORD_HEADER_SZ; |
3270 | | #ifdef WOLFSSL_DTLS13 |
3271 | | if (ssl->options.dtls) |
3272 | | args->headerSz = Dtls13GetRlHeaderLength(ssl, 1); |
3273 | | #endif /* WOLFSSL_DTLS13 */ |
3274 | | |
3275 | | args->sz = args->headerSz + (word32)inSz; |
3276 | | args->idx = args->headerSz; |
3277 | | |
3278 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3279 | | if (asyncOkay) |
3280 | | ssl->async->freeArgs = FreeBuildMsg13Args; |
3281 | | #endif |
3282 | | } |
3283 | | |
3284 | | switch (ssl->options.buildMsgState) { |
3285 | | case BUILD_MSG_BEGIN: |
3286 | | { |
3287 | | /* catch mistaken sizeOnly parameter */ |
3288 | | if (sizeOnly) { |
3289 | | if (output || input) { |
3290 | | WOLFSSL_MSG("BuildTls13Message with sizeOnly " |
3291 | | "doesn't need input or output"); |
3292 | | return BAD_FUNC_ARG; |
3293 | | } |
3294 | | } |
3295 | | else if (output == NULL || input == NULL) { |
3296 | | return BAD_FUNC_ARG; |
3297 | | } |
3298 | | |
3299 | | /* Record layer content type at the end of record data. */ |
3300 | | args->sz++; |
3301 | | /* Authentication data at the end. */ |
3302 | | args->sz += ssl->specs.aead_mac_size; |
3303 | | #ifdef WOLFSSL_DTLS13 |
3304 | | /* Pad to minimum length */ |
3305 | | if (ssl->options.dtls && |
3306 | | args->sz < (word32)Dtls13MinimumRecordLength(ssl)) { |
3307 | | args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz; |
3308 | | args->sz = Dtls13MinimumRecordLength(ssl); |
3309 | | } |
3310 | | #endif |
3311 | | if (sizeOnly) |
3312 | | return (int)args->sz; |
3313 | | |
3314 | | if (args->sz > (word32)outSz) { |
3315 | | WOLFSSL_MSG("Oops, want to write past output buffer size"); |
3316 | | return BUFFER_E; |
3317 | | } |
3318 | | |
3319 | | /* Record data length. */ |
3320 | | args->size = (word16)(args->sz - args->headerSz); |
3321 | | /* Write/update the record header with the new size. |
3322 | | * Always have the content type as application data for encrypted |
3323 | | * messages in TLS v1.3. |
3324 | | */ |
3325 | | |
3326 | | if (ssl->options.dtls) { |
3327 | | #ifdef WOLFSSL_DTLS13 |
3328 | | Dtls13RlAddCiphertextHeader(ssl, output, args->size); |
3329 | | #endif /* WOLFSSL_DTLS13 */ |
3330 | | } |
3331 | | else { |
3332 | | AddTls13RecordHeader(output, args->size, application_data, ssl); |
3333 | | } |
3334 | | |
3335 | | /* TLS v1.3 can do in place encryption. */ |
3336 | | if (input != output + args->idx) |
3337 | | XMEMCPY(output + args->idx, input, (size_t)inSz); |
3338 | | args->idx += (word32)inSz; |
3339 | | |
3340 | | ssl->options.buildMsgState = BUILD_MSG_HASH; |
3341 | | } |
3342 | | FALL_THROUGH; |
3343 | | |
3344 | | case BUILD_MSG_HASH: |
3345 | | { |
3346 | | if (hashOutput) { |
3347 | | ret = HashOutput(ssl, output, (int)args->headerSz + inSz, 0); |
3348 | | if (ret != 0) |
3349 | | goto exit_buildmsg; |
3350 | | } |
3351 | | |
3352 | | /* The real record content type goes at the end of the data. */ |
3353 | | output[args->idx++] = (byte)type; |
3354 | | /* Double check that any necessary padding is zero'd out */ |
3355 | | XMEMSET(output + args->idx, 0, args->paddingSz); |
3356 | | args->idx += args->paddingSz; |
3357 | | |
3358 | | ssl->options.buildMsgState = BUILD_MSG_ENCRYPT; |
3359 | | } |
3360 | | FALL_THROUGH; |
3361 | | |
3362 | | case BUILD_MSG_ENCRYPT: |
3363 | | { |
3364 | | #ifdef WOLFSSL_QUIC |
3365 | | if (WOLFSSL_IS_QUIC(ssl)) { |
3366 | | /* QUIC does not use encryption of the TLS Record Layer. |
3367 | | * Return the original length + added headers |
3368 | | * and restore it in the record header. */ |
3369 | | AddTls13RecordHeader(output, (word32)inSz, (byte)type, ssl); |
3370 | | ret = (int)args->headerSz + inSz; |
3371 | | goto exit_buildmsg; |
3372 | | } |
3373 | | #endif |
3374 | | #ifdef ATOMIC_USER |
3375 | | if (ssl->ctx->MacEncryptCb) { |
3376 | | /* User Record Layer Callback handling */ |
3377 | | byte* mac = output + args->idx; |
3378 | | output += args->headerSz; |
3379 | | |
3380 | | ret = ssl->ctx->MacEncryptCb(ssl, mac, output, (unsigned int)inSz, (byte)type, 0, |
3381 | | output, output, args->size, ssl->MacEncryptCtx); |
3382 | | } |
3383 | | else |
3384 | | #endif |
3385 | | { |
3386 | | const byte* aad = output; |
3387 | | output += args->headerSz; |
3388 | | ret = EncryptTls13(ssl, output, output, args->size, aad, |
3389 | | (word16)args->headerSz, asyncOkay); |
3390 | | if (ret != 0) { |
3391 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3392 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
3393 | | #endif |
3394 | | { |
3395 | | /* Zeroize plaintext. */ |
3396 | | ForceZero(output, args->size); |
3397 | | } |
3398 | | } |
3399 | | #ifdef WOLFSSL_DTLS13 |
3400 | | if (ret == 0 && ssl->options.dtls) { |
3401 | | /* AAD points to the header. Reuse the variable */ |
3402 | | ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, |
3403 | | (word16)args->sz); |
3404 | | } |
3405 | | #endif /* WOLFSSL_DTLS13 */ |
3406 | | } |
3407 | | break; |
3408 | | } |
3409 | | |
3410 | | default: |
3411 | | break; |
3412 | | } |
3413 | | |
3414 | | exit_buildmsg: |
3415 | | |
3416 | | WOLFSSL_LEAVE("BuildTls13Message", ret); |
3417 | | |
3418 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3419 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3420 | | return ret; |
3421 | | } |
3422 | | #endif |
3423 | | |
3424 | | /* make sure build message state is reset */ |
3425 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3426 | | |
3427 | | /* return sz on success */ |
3428 | | if (ret == 0) { |
3429 | | ret = (int)args->sz; |
3430 | | } |
3431 | | else { |
3432 | | WOLFSSL_ERROR_VERBOSE(ret); |
3433 | | } |
3434 | | |
3435 | | /* Final cleanup */ |
3436 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3437 | | if (asyncOkay) |
3438 | | FreeAsyncCtx(ssl, 0); |
3439 | | else |
3440 | | #endif |
3441 | | FreeBuildMsg13Args(ssl, args); |
3442 | | |
3443 | | return ret; |
3444 | | } |
3445 | | |
3446 | | #if !defined(NO_WOLFSSL_CLIENT) || (!defined(NO_WOLFSSL_SERVER) && \ |
3447 | | (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
3448 | | (defined(WOLFSSL_PSK_ONE_ID) || defined(WOLFSSL_PRIORITIZE_PSK))) |
3449 | | /* Find the cipher suite in the suites set in the SSL. |
3450 | | * |
3451 | | * ssl SSL/TLS object. |
3452 | | * suite Cipher suite to look for. |
3453 | | * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise. |
3454 | | */ |
3455 | | int FindSuiteSSL(const WOLFSSL* ssl, byte* suite) |
3456 | 159 | { |
3457 | 159 | word16 i; |
3458 | 159 | const Suites* suites = WOLFSSL_SUITES(ssl); |
3459 | | |
3460 | 807 | for (i = 0; i < suites->suiteSz; i += 2) { |
3461 | 802 | if (suites->suites[i+0] == suite[0] && |
3462 | 467 | suites->suites[i+1] == suite[1]) { |
3463 | 154 | return 1; |
3464 | 154 | } |
3465 | 802 | } |
3466 | | |
3467 | 5 | return 0; |
3468 | 159 | } |
3469 | | #endif |
3470 | | |
3471 | | #ifndef NO_PSK |
3472 | | /* Get the MAC algorithm for the TLS 1.3 cipher suite. |
3473 | | * |
3474 | | * @param [in] suite. |
3475 | | * @return A value from wc_MACAlgorithm enumeration. |
3476 | | */ |
3477 | | byte SuiteMac(const byte* suite) |
3478 | | { |
3479 | | byte mac = no_mac; |
3480 | | |
3481 | | if (suite[0] == TLS13_BYTE) { |
3482 | | switch (suite[1]) { |
3483 | | #ifdef BUILD_TLS_AES_128_GCM_SHA256 |
3484 | | case TLS_AES_128_GCM_SHA256: |
3485 | | mac = sha256_mac; |
3486 | | break; |
3487 | | #endif |
3488 | | #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256 |
3489 | | case TLS_CHACHA20_POLY1305_SHA256: |
3490 | | mac = sha256_mac; |
3491 | | break; |
3492 | | #endif |
3493 | | #ifdef BUILD_TLS_AES_128_CCM_SHA256 |
3494 | | case TLS_AES_128_CCM_SHA256: |
3495 | | mac = sha256_mac; |
3496 | | break; |
3497 | | #endif |
3498 | | #ifdef BUILD_TLS_AES_128_CCM_8_SHA256 |
3499 | | case TLS_AES_128_CCM_8_SHA256: |
3500 | | mac = sha256_mac; |
3501 | | break; |
3502 | | #endif |
3503 | | #ifdef BUILD_TLS_AES_256_GCM_SHA384 |
3504 | | case TLS_AES_256_GCM_SHA384: |
3505 | | mac = sha384_mac; |
3506 | | break; |
3507 | | #endif |
3508 | | default: |
3509 | | break; |
3510 | | } |
3511 | | } |
3512 | | #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \ |
3513 | | defined(WOLFSSL_SM3) |
3514 | | else if (suite[0] == CIPHER_BYTE) { |
3515 | | switch (suite[1]) { |
3516 | | #ifdef BUILD_TLS_SM4_GCM_SM3 |
3517 | | case TLS_SM4_GCM_SM3: |
3518 | | mac = sm3_mac; |
3519 | | break; |
3520 | | #endif |
3521 | | #ifdef BUILD_TLS_SM4_CCM_SM3 |
3522 | | case TLS_SM4_CCM_SM3: |
3523 | | mac = sm3_mac; |
3524 | | break; |
3525 | | #endif |
3526 | | default: |
3527 | | break; |
3528 | | } |
3529 | | } |
3530 | | #endif |
3531 | | #ifdef HAVE_NULL_CIPHER |
3532 | | else if (suite[0] == ECC_BYTE) { |
3533 | | switch (suite[1]) { |
3534 | | #ifdef BUILD_TLS_SHA256_SHA256 |
3535 | | case TLS_SHA256_SHA256: |
3536 | | mac = sha256_mac; |
3537 | | break; |
3538 | | #endif |
3539 | | #ifdef BUILD_TLS_SHA384_SHA384 |
3540 | | case TLS_SHA384_SHA384: |
3541 | | mac = sha384_mac; |
3542 | | break; |
3543 | | #endif |
3544 | | default: |
3545 | | break; |
3546 | | } |
3547 | | } |
3548 | | #endif |
3549 | | |
3550 | | return mac; |
3551 | | } |
3552 | | #endif |
3553 | | |
3554 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3555 | | /* Create Cookie extension using the hash of the first ClientHello. |
3556 | | * |
3557 | | * ssl SSL/TLS object. |
3558 | | * hash The hash data. |
3559 | | * hashSz The size of the hash data in bytes. |
3560 | | * returns 0 on success, otherwise failure. |
3561 | | */ |
3562 | | int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz, |
3563 | | TLSX** exts, byte cipherSuite0, byte cipherSuite) |
3564 | | { |
3565 | | int ret; |
3566 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
3567 | | Hmac cookieHmac; |
3568 | | byte cookieType = 0; |
3569 | | byte macSz = 0; |
3570 | | byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2]; |
3571 | | TLSX* ext; |
3572 | | word16 cookieSz = 0; |
3573 | | |
3574 | | if (hash == NULL || hashSz == 0) { |
3575 | | return BAD_FUNC_ARG; |
3576 | | } |
3577 | | |
3578 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
3579 | | ssl->buffers.tls13CookieSecret.length == 0) { |
3580 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
3581 | | return COOKIE_ERROR; |
3582 | | } |
3583 | | |
3584 | | /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */ |
3585 | | cookie[cookieSz++] = (byte)hashSz; |
3586 | | XMEMCPY(cookie + cookieSz, hash, hashSz); |
3587 | | cookieSz += hashSz; |
3588 | | cookie[cookieSz++] = cipherSuite0; |
3589 | | cookie[cookieSz++] = cipherSuite; |
3590 | | if ((ext = TLSX_Find(*exts, TLSX_KEY_SHARE)) != NULL) { |
3591 | | KeyShareEntry* kse = (KeyShareEntry*)ext->data; |
3592 | | if (kse == NULL) { |
3593 | | WOLFSSL_MSG("KeyShareEntry can't be empty when negotiating " |
3594 | | "parameters"); |
3595 | | return BAD_STATE_E; |
3596 | | } |
3597 | | c16toa(kse->group, cookie + cookieSz); |
3598 | | cookieSz += OPAQUE16_LEN; |
3599 | | } |
3600 | | |
3601 | | #ifndef NO_SHA256 |
3602 | | cookieType = WC_SHA256; |
3603 | | macSz = WC_SHA256_DIGEST_SIZE; |
3604 | | #elif defined(WOLFSSL_SHA384) |
3605 | | cookieType = WC_SHA384; |
3606 | | macSz = WC_SHA384_DIGEST_SIZE; |
3607 | | #elif defined(WOLFSSL_TLS13_SHA512) |
3608 | | cookieType = WC_SHA512; |
3609 | | macSz = WC_SHA512_DIGEST_SIZE; |
3610 | | #elif defined(WOLFSSL_SM3) |
3611 | | cookieType = WC_SM3; |
3612 | | macSz = WC_SM3_DIGEST_SIZE; |
3613 | | #else |
3614 | | #error "No digest to available to use with HMAC for cookies." |
3615 | | #endif /* NO_SHA */ |
3616 | | |
3617 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
3618 | | if (ret == 0) { |
3619 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
3620 | | ssl->buffers.tls13CookieSecret.buffer, |
3621 | | ssl->buffers.tls13CookieSecret.length); |
3622 | | } |
3623 | | if (ret == 0) |
3624 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
3625 | | #ifdef WOLFSSL_DTLS13 |
3626 | | /* Tie cookie to peer address */ |
3627 | | if (ret == 0) { |
3628 | | /* peerLock not necessary. Still in handshake phase. */ |
3629 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
3630 | | ret = wc_HmacUpdate(&cookieHmac, |
3631 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
3632 | | ssl->buffers.dtlsCtx.peer.sz); |
3633 | | } |
3634 | | } |
3635 | | #endif |
3636 | | if (ret == 0) |
3637 | | ret = wc_HmacFinal(&cookieHmac, mac); |
3638 | | |
3639 | | wc_HmacFree(&cookieHmac); |
3640 | | if (ret != 0) |
3641 | | return ret; |
3642 | | |
3643 | | /* The cookie data is the hash and the integrity check. */ |
3644 | | return TLSX_Cookie_Use(ssl, cookie, cookieSz, mac, macSz, 1, exts); |
3645 | | } |
3646 | | #endif |
3647 | | |
3648 | | #ifdef WOLFSSL_DTLS13 |
3649 | | #define HRR_MAX_HS_HEADER_SZ DTLS_HANDSHAKE_HEADER_SZ |
3650 | | #else |
3651 | | #define HRR_MAX_HS_HEADER_SZ HANDSHAKE_HEADER_SZ |
3652 | | #endif /* WOLFSSL_DTLS13 */ |
3653 | | |
3654 | | static int CreateCookie(const WOLFSSL* ssl, byte** hash, byte* hashSz, |
3655 | | Hashes* hashes, TLSX** exts) |
3656 | | { |
3657 | | int ret = 0; |
3658 | | |
3659 | | (void)exts; |
3660 | | |
3661 | | *hash = NULL; |
3662 | | switch (ssl->specs.mac_algorithm) { |
3663 | | #ifndef NO_SHA256 |
3664 | | case sha256_mac: |
3665 | | *hash = hashes->sha256; |
3666 | | break; |
3667 | | #endif |
3668 | | #ifdef WOLFSSL_SHA384 |
3669 | | case sha384_mac: |
3670 | | *hash = hashes->sha384; |
3671 | | break; |
3672 | | #endif |
3673 | | #ifdef WOLFSSL_TLS13_SHA512 |
3674 | | case sha512_mac: |
3675 | | *hash = hashes->sha512; |
3676 | | break; |
3677 | | #endif |
3678 | | #ifdef WOLFSSL_SM3 |
3679 | | case sm3_mac: |
3680 | | *hash = hashes->sm3; |
3681 | | break; |
3682 | | #endif |
3683 | | } |
3684 | | *hashSz = ssl->specs.hash_size; |
3685 | | |
3686 | | /* check hash */ |
3687 | | if (*hash == NULL && *hashSz > 0) |
3688 | | return BAD_FUNC_ARG; |
3689 | | |
3690 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3691 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3692 | | ret = CreateCookieExt(ssl, *hash, *hashSz, exts, |
3693 | | ssl->options.cipherSuite0, ssl->options.cipherSuite); |
3694 | | #endif |
3695 | | return ret; |
3696 | | } |
3697 | | |
3698 | | /* Restart the handshake hash with a hash of the previous messages. |
3699 | | * |
3700 | | * ssl The SSL/TLS object. |
3701 | | * returns 0 on success, otherwise failure. |
3702 | | */ |
3703 | | int RestartHandshakeHash(WOLFSSL* ssl) |
3704 | 0 | { |
3705 | 0 | int ret; |
3706 | 0 | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
3707 | 0 | Hashes hashes; |
3708 | 0 | byte* hash = NULL; |
3709 | 0 | byte hashSz = 0; |
3710 | |
|
3711 | 0 | ret = BuildCertHashes(ssl, &hashes); |
3712 | 0 | if (ret != 0) |
3713 | 0 | return ret; |
3714 | 0 | ret = CreateCookie(ssl, &hash, &hashSz, &hashes, &ssl->extensions); |
3715 | 0 | if (ret != 0) |
3716 | 0 | return ret; |
3717 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3718 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3719 | | return 0; |
3720 | | #endif |
3721 | | |
3722 | 0 | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
3723 | |
|
3724 | | #ifdef WOLFSSL_DEBUG_TLS |
3725 | | WOLFSSL_MSG("Restart Hash"); |
3726 | | WOLFSSL_BUFFER(hash, hashSz); |
3727 | | #endif |
3728 | |
|
3729 | 0 | ret = InitHandshakeHashes(ssl); |
3730 | 0 | if (ret != 0) |
3731 | 0 | return ret; |
3732 | 0 | ret = HashRaw(ssl, header, sizeof(header)); |
3733 | 0 | if (ret != 0) |
3734 | 0 | return ret; |
3735 | 0 | return HashRaw(ssl, hash, hashSz); |
3736 | 0 | } |
3737 | | |
3738 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
3739 | | /* The value in the random field of a ServerHello to indicate |
3740 | | * HelloRetryRequest. |
3741 | | */ |
3742 | | static byte helloRetryRequestRandom[] = { |
3743 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
3744 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
3745 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
3746 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
3747 | | }; |
3748 | | #endif |
3749 | | |
3750 | | #ifndef NO_WOLFSSL_CLIENT |
3751 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
3752 | | #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_PSK_ONE_ID) && \ |
3753 | | !defined(NO_PSK) |
3754 | | /** |
3755 | | * convert mac algorithm to WOLFSSL_EVP_MD |
3756 | | * @param mac_alg mac algorithm |
3757 | | * @return const WOLFSSL_EVP_MD on successful, otherwise NULL |
3758 | | */ |
3759 | | static const WOLFSSL_EVP_MD* ssl_handshake_md(const byte mac_alg) |
3760 | | { |
3761 | | switch(mac_alg) { |
3762 | | case no_mac: |
3763 | | #ifndef NO_MD5 |
3764 | | case md5_mac: |
3765 | | return wolfSSL_EVP_md5(); |
3766 | | #endif |
3767 | | #ifndef NO_SHA |
3768 | | case sha_mac: |
3769 | | return wolfSSL_EVP_sha1(); |
3770 | | #endif |
3771 | | #ifdef WOLFSSL_SHA224 |
3772 | | case sha224_mac: |
3773 | | return wolfSSL_EVP_sha224(); |
3774 | | #endif |
3775 | | case sha256_mac: |
3776 | | return wolfSSL_EVP_sha256(); |
3777 | | #ifdef WOLFSSL_SHA384 |
3778 | | case sha384_mac: |
3779 | | return wolfSSL_EVP_sha384(); |
3780 | | #endif |
3781 | | #ifdef WOLFSSL_SHA512 |
3782 | | case sha512_mac: |
3783 | | return wolfSSL_EVP_sha512(); |
3784 | | #endif |
3785 | | case rmd_mac: |
3786 | | case blake2b_mac: |
3787 | | WOLFSSL_MSG("no suitable EVP_MD"); |
3788 | | return NULL; |
3789 | | default: |
3790 | | WOLFSSL_MSG("Unknown mac algorithm"); |
3791 | | return NULL; |
3792 | | } |
3793 | | } |
3794 | | #endif |
3795 | | /* Setup pre-shared key based on the details in the extension data. |
3796 | | * |
3797 | | * ssl SSL/TLS object. |
3798 | | * psk Pre-shared key extension data. |
3799 | | * clientHello Whether called from client_hello construction. |
3800 | | * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and |
3801 | | * other negative value on failure. |
3802 | | */ |
3803 | | static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello) |
3804 | | { |
3805 | | #if defined(HAVE_SESSION_TICKET) || !defined(WOLFSSL_PSK_ONE_ID) |
3806 | | int ret; |
3807 | | #endif |
3808 | | byte suite[2]; |
3809 | | |
3810 | | if (psk == NULL) |
3811 | | return BAD_FUNC_ARG; |
3812 | | |
3813 | | if (!HaveUniqueSessionObj(ssl)) { |
3814 | | WOLFSSL_MSG("Unable to have unique session object"); |
3815 | | WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR); |
3816 | | return MEMORY_ERROR; |
3817 | | } |
3818 | | |
3819 | | suite[0] = ssl->options.cipherSuite0; |
3820 | | suite[1] = ssl->options.cipherSuite; |
3821 | | |
3822 | | #ifdef HAVE_SESSION_TICKET |
3823 | | if (psk->resumption) { |
3824 | | if (clientHello) { |
3825 | | suite[0] = psk->cipherSuite0; |
3826 | | suite[1] = psk->cipherSuite; |
3827 | | |
3828 | | /* Ensure cipher suite is supported or changed suite to one with |
3829 | | * the same MAC algorithm. */ |
3830 | | if (!FindSuiteSSL(ssl, suite)) { |
3831 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3832 | | return PSK_KEY_ERROR; |
3833 | | } |
3834 | | |
3835 | | ssl->options.cipherSuite0 = suite[0]; |
3836 | | ssl->options.cipherSuite = suite[1]; |
3837 | | |
3838 | | /* Setting mac for binder and keys for deriving EarlyData. */ |
3839 | | ret = SetCipherSpecs(ssl); |
3840 | | if (ret != 0) |
3841 | | return ret; |
3842 | | } |
3843 | | |
3844 | | #ifdef WOLFSSL_EARLY_DATA |
3845 | | if (ssl->session->maxEarlyDataSz == 0) |
3846 | | ssl->earlyData = no_early_data; |
3847 | | #endif |
3848 | | /* Resumption PSK is master secret. */ |
3849 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
3850 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
3851 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
3852 | | return ret; |
3853 | | } |
3854 | | if (!clientHello) { |
3855 | | /* CLIENT: using secret in ticket for peer authentication. */ |
3856 | | ssl->options.peerAuthGood = 1; |
3857 | | } |
3858 | | } |
3859 | | #endif |
3860 | | #ifndef NO_PSK |
3861 | | if (!psk->resumption) { |
3862 | | /* Get the pre-shared key. */ |
3863 | | #ifndef WOLFSSL_PSK_ONE_ID |
3864 | | const char* cipherName = NULL; |
3865 | | #ifdef OPENSSL_EXTRA |
3866 | | WOLFSSL_SESSION* psksession = NULL; |
3867 | | #endif |
3868 | | |
3869 | | /* Set the client identity to use. */ |
3870 | | XMEMSET(ssl->arrays->client_identity, 0, |
3871 | | sizeof(ssl->arrays->client_identity)); |
3872 | | XMEMCPY(ssl->arrays->client_identity, psk->identity, psk->identityLen); |
3873 | | |
3874 | | #ifdef WOLFSSL_DEBUG_TLS |
3875 | | WOLFSSL_MSG("PSK cipher suite:"); |
3876 | | WOLFSSL_MSG(GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
3877 | | #endif |
3878 | | |
3879 | | /* Get the pre-shared key. */ |
3880 | | #ifdef OPENSSL_EXTRA |
3881 | | if (ssl->options.session_psk_cb != NULL) { |
3882 | | const unsigned char* id = NULL; |
3883 | | size_t idlen = 0; |
3884 | | const WOLFSSL_EVP_MD* handshake_md = NULL; |
3885 | | |
3886 | | if (ssl->msgsReceived.got_hello_retry_request >= 1) { |
3887 | | handshake_md = ssl_handshake_md(ssl->specs.mac_algorithm); |
3888 | | } |
3889 | | /* OpenSSL compatible callback that gets cached session. */ |
3890 | | if (ssl->options.session_psk_cb(ssl, handshake_md, &id, &idlen, |
3891 | | &psksession) == 0) { |
3892 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3893 | | WOLFSSL_MSG("psk session callback failed"); |
3894 | | return PSK_KEY_ERROR; |
3895 | | } |
3896 | | if (psksession != NULL) { |
3897 | | if (idlen > MAX_PSK_KEY_LEN) { |
3898 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3899 | | WOLFSSL_MSG("psk key length is too long"); |
3900 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3901 | | return PSK_KEY_ERROR; |
3902 | | } |
3903 | | |
3904 | | ssl->arrays->psk_keySz = (word32)idlen; |
3905 | | XMEMCPY(ssl->arrays->psk_key, id, idlen); |
3906 | | suite[0] = psksession->cipherSuite0; |
3907 | | suite[1] = psksession->cipherSuite; |
3908 | | /* Not needed anymore. */ |
3909 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3910 | | /* Leave pointer not NULL to indicate success with callback. */ |
3911 | | } |
3912 | | } |
3913 | | if (psksession != NULL) { |
3914 | | /* Don't try other callbacks - we have an answer. */ |
3915 | | } |
3916 | | else |
3917 | | #endif /* OPENSSL_EXTRA */ |
3918 | | if (ssl->options.client_psk_cs_cb != NULL) { |
3919 | | #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
3920 | | ssl->arrays->client_identity[0] = 0; |
3921 | | #endif |
3922 | | /* Lookup key again for next identity. */ |
3923 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cs_cb( |
3924 | | ssl, ssl->arrays->server_hint, |
3925 | | ssl->arrays->client_identity, MAX_PSK_ID_LEN, |
3926 | | ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
3927 | | GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
3928 | | if (clientHello) { |
3929 | | /* Use PSK cipher suite. */ |
3930 | | ssl->options.cipherSuite0 = psk->cipherSuite0; |
3931 | | ssl->options.cipherSuite = psk->cipherSuite; |
3932 | | } |
3933 | | else { |
3934 | | byte pskCS[2]; |
3935 | | pskCS[0] = psk->cipherSuite0; |
3936 | | pskCS[1] = psk->cipherSuite; |
3937 | | |
3938 | | /* Ensure PSK and negotiated cipher suites have same hash. */ |
3939 | | if (SuiteMac(pskCS) != SuiteMac(suite)) { |
3940 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3941 | | return PSK_KEY_ERROR; |
3942 | | } |
3943 | | /* Negotiated cipher suite is to be used - update PSK. */ |
3944 | | psk->cipherSuite0 = suite[0]; |
3945 | | psk->cipherSuite = suite[1]; |
3946 | | } |
3947 | | } |
3948 | | else if (ssl->options.client_psk_tls13_cb != NULL) { |
3949 | | byte cipherSuite0; |
3950 | | byte cipherSuite; |
3951 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
3952 | | |
3953 | | ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl, |
3954 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
3955 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
3956 | | &cipherName); |
3957 | | if (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
3958 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) { |
3959 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3960 | | return PSK_KEY_ERROR; |
3961 | | } |
3962 | | ssl->options.cipherSuite0 = cipherSuite0; |
3963 | | ssl->options.cipherSuite = cipherSuite; |
3964 | | (void)cipherSuiteFlags; |
3965 | | } |
3966 | | else { |
3967 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl, |
3968 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
3969 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN); |
3970 | | ssl->options.cipherSuite0 = TLS13_BYTE; |
3971 | | ssl->options.cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
3972 | | } |
3973 | | if (ssl->arrays->psk_keySz == 0 || |
3974 | | (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN && |
3975 | | (int)ssl->arrays->psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK))) { |
3976 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3977 | | return PSK_KEY_ERROR; |
3978 | | } |
3979 | | |
3980 | | ret = SetCipherSpecs(ssl); |
3981 | | if (ret != 0) |
3982 | | return ret; |
3983 | | #else |
3984 | | /* PSK information loaded during setting of default TLS extensions. */ |
3985 | | #endif /* !WOLFSSL_PSK_ONE_ID */ |
3986 | | |
3987 | | if (!clientHello && (psk->cipherSuite0 != suite[0] || |
3988 | | psk->cipherSuite != suite[1])) { |
3989 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3990 | | return PSK_KEY_ERROR; |
3991 | | } |
3992 | | |
3993 | | if (!clientHello) { |
3994 | | /* CLIENT: using PSK for peer authentication. */ |
3995 | | ssl->options.peerAuthGood = 1; |
3996 | | } |
3997 | | } |
3998 | | #endif |
3999 | | |
4000 | | #ifdef HAVE_SUPPORTED_CURVES |
4001 | | if (!clientHello) { |
4002 | | TLSX* ext; |
4003 | | word32 modes; |
4004 | | KeyShareEntry* kse = NULL; |
4005 | | |
4006 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
4007 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
4008 | | if (ext == NULL) { |
4009 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4010 | | return PSK_KEY_ERROR; |
4011 | | } |
4012 | | modes = ext->val; |
4013 | | |
4014 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
4015 | | if (ext != NULL) { |
4016 | | kse = (KeyShareEntry*)ext->data; |
4017 | | } |
4018 | | /* Use (EC)DHE for forward-security if possible. */ |
4019 | | if (((modes & (1 << PSK_DHE_KE)) != 0) && (!ssl->options.noPskDheKe) && |
4020 | | (kse != NULL) && kse->derived) { |
4021 | | if ((kse->session != 0) && (kse->session != kse->group)) { |
4022 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4023 | | return PSK_KEY_ERROR; |
4024 | | } |
4025 | | } |
4026 | | else if (ssl->options.onlyPskDheKe) { |
4027 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4028 | | return PSK_KEY_ERROR; |
4029 | | } |
4030 | | else if (ssl->options.noPskDheKe) { |
4031 | | ssl->arrays->preMasterSz = 0; |
4032 | | } |
4033 | | } |
4034 | | else |
4035 | | #endif |
4036 | | if (ssl->options.noPskDheKe) { |
4037 | | ssl->arrays->preMasterSz = 0; |
4038 | | } |
4039 | | |
4040 | | /* Derive the early secret using the PSK. */ |
4041 | | return DeriveEarlySecret(ssl); |
4042 | | } |
4043 | | |
4044 | | /* Derive and write the binders into the ClientHello in space left when |
4045 | | * writing the Pre-Shared Key extension. |
4046 | | * |
4047 | | * ssl The SSL/TLS object. |
4048 | | * output The buffer containing the ClientHello. |
4049 | | * idx The index at the end of the completed ClientHello. |
4050 | | * returns 0 on success and otherwise failure. |
4051 | | */ |
4052 | | static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx) |
4053 | | { |
4054 | | int ret; |
4055 | | TLSX* ext; |
4056 | | PreSharedKey* current; |
4057 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
4058 | | word16 len; |
4059 | | |
4060 | | WOLFSSL_ENTER("WritePSKBinders"); |
4061 | | |
4062 | | if (idx > WOLFSSL_MAX_16BIT) { |
4063 | | return INPUT_SIZE_E; |
4064 | | } |
4065 | | |
4066 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
4067 | | if (ext == NULL) |
4068 | | return SANITY_MSG_E; |
4069 | | |
4070 | | /* Get the size of the binders to determine where to write binders. */ |
4071 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
4072 | | client_hello, &len); |
4073 | | if (ret < 0) |
4074 | | return ret; |
4075 | | idx -= len; |
4076 | | |
4077 | | /* Hash truncated ClientHello - up to binders. */ |
4078 | | #ifdef WOLFSSL_DTLS13 |
4079 | | if (ssl->options.dtls) |
4080 | | ret = Dtls13HashHandshake(ssl, output + Dtls13GetRlHeaderLength(ssl, 0), |
4081 | | (word16)idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4082 | | else |
4083 | | #endif /* WOLFSSL_DTLS13 */ |
4084 | | ret = HashOutput(ssl, output, (int)idx, 0); |
4085 | | |
4086 | | if (ret != 0) |
4087 | | return ret; |
4088 | | |
4089 | | current = (PreSharedKey*)ext->data; |
4090 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4091 | | if (current != NULL) { |
4092 | | wc_MemZero_Add("WritePSKBinders binderKey", binderKey, |
4093 | | sizeof(binderKey)); |
4094 | | } |
4095 | | #endif |
4096 | | /* Calculate the binder for each identity based on previous handshake data. |
4097 | | */ |
4098 | | while (current != NULL) { |
4099 | | if ((ret = SetupPskKey(ssl, current, 1)) != 0) |
4100 | | break; |
4101 | | |
4102 | | #ifdef HAVE_SESSION_TICKET |
4103 | | if (current->resumption) |
4104 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
4105 | | #endif |
4106 | | #ifndef NO_PSK |
4107 | | if (!current->resumption) |
4108 | | ret = DeriveBinderKey(ssl, binderKey); |
4109 | | #endif |
4110 | | if (ret != 0) |
4111 | | break; |
4112 | | |
4113 | | /* Derive the Finished message secret. */ |
4114 | | ret = DeriveFinishedSecret(ssl, binderKey, |
4115 | | ssl->keys.client_write_MAC_secret, |
4116 | | 0 /* neither end */); |
4117 | | if (ret != 0) |
4118 | | break; |
4119 | | |
4120 | | /* Build the HMAC of the handshake message data = binder. */ |
4121 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, |
4122 | | current->binder, ¤t->binderLen); |
4123 | | if (ret != 0) |
4124 | | break; |
4125 | | |
4126 | | current = current->next; |
4127 | | } |
4128 | | |
4129 | | ForceZero(binderKey, sizeof(binderKey)); |
4130 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4131 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
4132 | | #endif |
4133 | | if (ret != 0) |
4134 | | return ret; |
4135 | | |
4136 | | /* Data entered into extension, now write to message. */ |
4137 | | ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx, |
4138 | | client_hello, &len); |
4139 | | if (ret < 0) |
4140 | | return ret; |
4141 | | |
4142 | | /* Hash binders to complete the hash of the ClientHello. */ |
4143 | | ret = HashRaw(ssl, output + idx, len); |
4144 | | if (ret < 0) |
4145 | | return ret; |
4146 | | |
4147 | | #ifdef WOLFSSL_EARLY_DATA |
4148 | | if (ssl->earlyData != no_early_data) { |
4149 | | if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data, 1)) != 0) |
4150 | | return ret; |
4151 | | |
4152 | | /* Derive early data encryption key. */ |
4153 | | ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1); |
4154 | | if (ret != 0) |
4155 | | return ret; |
4156 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
4157 | | return ret; |
4158 | | |
4159 | | } |
4160 | | #endif |
4161 | | |
4162 | | WOLFSSL_LEAVE("WritePSKBinders", ret); |
4163 | | |
4164 | | return ret; |
4165 | | } |
4166 | | #endif |
4167 | | |
4168 | | #if defined(HAVE_ECH) |
4169 | | /* returns the index of the first supported cipher suite, -1 if none */ |
4170 | | int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config) |
4171 | | { |
4172 | | int i, j, supported = 0; |
4173 | | |
4174 | | for (i = 0; i < config->numCipherSuites; i++) { |
4175 | | supported = 0; |
4176 | | |
4177 | | for (j = 0; j < HPKE_SUPPORTED_KDF_LEN; j++) { |
4178 | | if (config->cipherSuites[i].kdfId == hpkeSupportedKdf[j]) |
4179 | | break; |
4180 | | } |
4181 | | |
4182 | | if (j < HPKE_SUPPORTED_KDF_LEN) |
4183 | | for (j = 0; j < HPKE_SUPPORTED_AEAD_LEN; j++) { |
4184 | | if (config->cipherSuites[i].aeadId == hpkeSupportedAead[j]) { |
4185 | | supported = 1; |
4186 | | break; |
4187 | | } |
4188 | | } |
4189 | | |
4190 | | if (supported) |
4191 | | return i; |
4192 | | } |
4193 | | |
4194 | | return WOLFSSL_FATAL_ERROR; |
4195 | | } |
4196 | | |
4197 | | /* returns status after we hash the ech inner */ |
4198 | | static int EchHashHelloInner(WOLFSSL* ssl, WOLFSSL_ECH* ech) |
4199 | | { |
4200 | | int ret = 0; |
4201 | | word32 realSz; |
4202 | | HS_Hashes* tmpHashes; |
4203 | | #ifdef WOLFSSL_DTLS13 |
4204 | | byte falseHeader[DTLS13_HANDSHAKE_HEADER_SZ]; |
4205 | | #else |
4206 | | byte falseHeader[HANDSHAKE_HEADER_SZ]; |
4207 | | #endif |
4208 | | |
4209 | | if (ssl == NULL || ech == NULL) |
4210 | | return BAD_FUNC_ARG; |
4211 | | realSz = ech->innerClientHelloLen - ech->paddingLen - ech->hpke->Nt; |
4212 | | tmpHashes = ssl->hsHashes; |
4213 | | ssl->hsHashes = NULL; |
4214 | | /* init the ech hashes */ |
4215 | | ret = InitHandshakeHashes(ssl); |
4216 | | if (ret == 0) { |
4217 | | ssl->hsHashesEch = ssl->hsHashes; |
4218 | | /* do the handshake header then the body */ |
4219 | | AddTls13HandShakeHeader(falseHeader, realSz, 0, 0, client_hello, ssl); |
4220 | | ret = HashRaw(ssl, falseHeader, HANDSHAKE_HEADER_SZ); |
4221 | | /* hash with inner */ |
4222 | | if (ret == 0) { |
4223 | | /* init hsHashesEchInner */ |
4224 | | if (ech->innerCount == 0) { |
4225 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4226 | | ret = InitHandshakeHashes(ssl); |
4227 | | if (ret == 0) { |
4228 | | ssl->hsHashesEchInner = ssl->hsHashes; |
4229 | | ech->innerCount = 1; |
4230 | | } |
4231 | | } |
4232 | | else { |
4233 | | /* switch back to hsHashes so we have hrr -> echInner2 */ |
4234 | | ssl->hsHashes = tmpHashes; |
4235 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, |
4236 | | &ssl->hsHashesEchInner); |
4237 | | } |
4238 | | |
4239 | | if (ret == 0) { |
4240 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4241 | | ret = HashRaw(ssl, falseHeader, HANDSHAKE_HEADER_SZ); |
4242 | | ssl->hsHashes = ssl->hsHashesEch; |
4243 | | } |
4244 | | } |
4245 | | } |
4246 | | /* hash the body */ |
4247 | | if (ret == 0) |
4248 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
4249 | | /* hash with inner */ |
4250 | | if (ret == 0) { |
4251 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4252 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
4253 | | } |
4254 | | /* swap hsHashes back */ |
4255 | | ssl->hsHashes = tmpHashes; |
4256 | | return ret; |
4257 | | } |
4258 | | #endif |
4259 | | |
4260 | | static void GetTls13SessionId(WOLFSSL* ssl, byte* output, word32* idx) |
4261 | 6.33k | { |
4262 | 6.33k | if (ssl->session->sessionIDSz > 0) { |
4263 | | /* Session resumption for old versions of protocol. */ |
4264 | 0 | if (ssl->session->sessionIDSz <= ID_LEN) { |
4265 | 0 | if (output != NULL) |
4266 | 0 | output[*idx] = ssl->session->sessionIDSz; |
4267 | 0 | (*idx)++; |
4268 | 0 | if (output != NULL) { |
4269 | 0 | XMEMCPY(output + *idx, ssl->session->sessionID, |
4270 | 0 | ssl->session->sessionIDSz); |
4271 | 0 | } |
4272 | 0 | *idx += ssl->session->sessionIDSz; |
4273 | 0 | } |
4274 | 0 | else { |
4275 | | /* Invalid session ID length. Reset it. */ |
4276 | 0 | ssl->session->sessionIDSz = 0; |
4277 | 0 | if (output != NULL) |
4278 | 0 | output[*idx] = 0; |
4279 | 0 | (*idx)++; |
4280 | 0 | } |
4281 | 0 | } |
4282 | 6.33k | else { |
4283 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
4284 | | if (ssl->options.tls13MiddleBoxCompat) { |
4285 | | if (output != NULL) |
4286 | | output[*idx] = ID_LEN; |
4287 | | (*idx)++; |
4288 | | if (output != NULL) |
4289 | | XMEMCPY(output + *idx, ssl->arrays->clientRandom, ID_LEN); |
4290 | | *idx += ID_LEN; |
4291 | | } |
4292 | | else |
4293 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
4294 | 6.33k | { |
4295 | | /* TLS v1.3 does not use session id - 0 length. */ |
4296 | 6.33k | if (output != NULL) |
4297 | 2.91k | output[*idx] = 0; |
4298 | 6.33k | (*idx)++; |
4299 | 6.33k | } |
4300 | 6.33k | } |
4301 | 6.33k | } |
4302 | | |
4303 | | /* handle generation of TLS 1.3 client_hello (1) */ |
4304 | | /* Send a ClientHello message to the server. |
4305 | | * Include the information required to start a handshake with servers using |
4306 | | * protocol versions less than TLS v1.3. |
4307 | | * Only a client will send this message. |
4308 | | * |
4309 | | * ssl The SSL/TLS object. |
4310 | | * returns 0 on success and otherwise failure. |
4311 | | */ |
4312 | | |
4313 | | typedef struct Sch13Args { |
4314 | | byte* output; |
4315 | | word32 idx; |
4316 | | int sendSz; |
4317 | | word32 length; |
4318 | | #if defined(HAVE_ECH) |
4319 | | int clientRandomOffset; |
4320 | | int preXLength; |
4321 | | WOLFSSL_ECH* ech; |
4322 | | #endif |
4323 | | } Sch13Args; |
4324 | | |
4325 | | int SendTls13ClientHello(WOLFSSL* ssl) |
4326 | | { |
4327 | | int ret; |
4328 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4329 | | Sch13Args* args = NULL; |
4330 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
4331 | | #else |
4332 | | Sch13Args args[1]; |
4333 | | #endif |
4334 | | byte major, tls12minor; |
4335 | | const Suites* suites; |
4336 | | |
4337 | | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND); |
4338 | | WOLFSSL_ENTER("SendTls13ClientHello"); |
4339 | | |
4340 | | if (ssl == NULL) { |
4341 | | return BAD_FUNC_ARG; |
4342 | | } |
4343 | | |
4344 | | ssl->options.buildingMsg = 1; |
4345 | | major = SSLv3_MAJOR; |
4346 | | tls12minor = TLSv1_2_MINOR; |
4347 | | |
4348 | | #ifdef WOLFSSL_DTLS13 |
4349 | | if (ssl->options.dtls) { |
4350 | | major = DTLS_MAJOR; |
4351 | | tls12minor = DTLSv1_2_MINOR; |
4352 | | } |
4353 | | #endif /* WOLFSSL_DTLS */ |
4354 | | |
4355 | | #ifdef HAVE_SESSION_TICKET |
4356 | | if (ssl->options.resuming && |
4357 | | (ssl->session->version.major != ssl->version.major || |
4358 | | ssl->session->version.minor != ssl->version.minor)) { |
4359 | | #ifndef WOLFSSL_NO_TLS12 |
4360 | | if (ssl->session->version.major == ssl->version.major && |
4361 | | ssl->session->version.minor < ssl->version.minor) { |
4362 | | /* Cannot resume with a different protocol version. */ |
4363 | | ssl->options.resuming = 0; |
4364 | | ssl->version.major = ssl->session->version.major; |
4365 | | ssl->version.minor = ssl->session->version.minor; |
4366 | | return SendClientHello(ssl); |
4367 | | } |
4368 | | else |
4369 | | #endif |
4370 | | { |
4371 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
4372 | | return VERSION_ERROR; |
4373 | | } |
4374 | | } |
4375 | | #endif |
4376 | | |
4377 | | suites = WOLFSSL_SUITES(ssl); |
4378 | | if (suites == NULL) { |
4379 | | WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello"); |
4380 | | return SUITES_ERROR; |
4381 | | } |
4382 | | |
4383 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4384 | | if (ssl->async == NULL) { |
4385 | | ssl->async = (struct WOLFSSL_ASYNC*) |
4386 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
4387 | | DYNAMIC_TYPE_ASYNC); |
4388 | | if (ssl->async == NULL) |
4389 | | return MEMORY_E; |
4390 | | ssl->async->freeArgs = NULL; |
4391 | | } |
4392 | | args = (Sch13Args*)ssl->async->args; |
4393 | | |
4394 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
4395 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
4396 | | /* Check for error */ |
4397 | | if (ret < 0) |
4398 | | return ret; |
4399 | | } |
4400 | | else |
4401 | | #endif |
4402 | | { |
4403 | | /* Reset state */ |
4404 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
4405 | | XMEMSET(args, 0, sizeof(Sch13Args)); |
4406 | | } |
4407 | | |
4408 | | switch (ssl->options.asyncState) { |
4409 | | case TLS_ASYNC_BEGIN: |
4410 | | { |
4411 | | word32 sessIdSz = 0; |
4412 | | |
4413 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
4414 | | |
4415 | | #ifdef WOLFSSL_DTLS13 |
4416 | | if (ssl->options.dtls) |
4417 | | args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4418 | | #endif /* WOLFSSL_DTLS13 */ |
4419 | | |
4420 | | /* Version | Random | Cipher Suites | Compression */ |
4421 | | args->length = VERSION_SZ + RAN_LEN + suites->suiteSz + |
4422 | | SUITE_LEN + COMP_LEN + ENUM_LEN; |
4423 | | #ifdef WOLFSSL_QUIC |
4424 | | if (WOLFSSL_IS_QUIC(ssl)) { |
4425 | | /* RFC 9001 ch. 8.4 sessionID in ClientHello MUST be 0 length */ |
4426 | | ssl->session->sessionIDSz = 0; |
4427 | | ssl->options.tls13MiddleBoxCompat = 0; |
4428 | | } |
4429 | | #endif |
4430 | | GetTls13SessionId(ssl, NULL, &sessIdSz); |
4431 | | args->length += (word16)sessIdSz; |
4432 | | |
4433 | | #ifdef WOLFSSL_DTLS13 |
4434 | | if (ssl->options.dtls) { |
4435 | | /* legacy_cookie_id len */ |
4436 | | args->length += ENUM_LEN; |
4437 | | |
4438 | | /* server sent us an HelloVerifyRequest and we allow downgrade */ |
4439 | | if (ssl->arrays->cookieSz > 0 && ssl->options.downgrade) |
4440 | | args->length += ssl->arrays->cookieSz; |
4441 | | } |
4442 | | #endif /* WOLFSSL_DTLS13 */ |
4443 | | |
4444 | | /* Advance state and proceed */ |
4445 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
4446 | | } /* case TLS_ASYNC_BEGIN */ |
4447 | | FALL_THROUGH; |
4448 | | |
4449 | | case TLS_ASYNC_BUILD: |
4450 | | case TLS_ASYNC_DO: |
4451 | | { |
4452 | | /* Auto populate extensions supported unless user defined. */ |
4453 | | if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0) |
4454 | | return ret; |
4455 | | |
4456 | | /* Advance state and proceed */ |
4457 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
4458 | | } /* case TLS_ASYNC_BUILD */ |
4459 | | FALL_THROUGH; |
4460 | | |
4461 | | case TLS_ASYNC_FINALIZE: |
4462 | | { |
4463 | | #ifdef WOLFSSL_EARLY_DATA |
4464 | | #ifndef NO_PSK |
4465 | | if (!ssl->options.resuming && |
4466 | | ssl->options.client_psk_tls13_cb == NULL && |
4467 | | ssl->options.client_psk_cb == NULL) |
4468 | | #else |
4469 | | if (!ssl->options.resuming) |
4470 | | #endif |
4471 | | ssl->earlyData = no_early_data; |
4472 | | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
4473 | | ssl->earlyData = no_early_data; |
4474 | | if (ssl->earlyData == no_early_data) |
4475 | | TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap); |
4476 | | if (ssl->earlyData != no_early_data && |
4477 | | (ret = TLSX_EarlyData_Use(ssl, 0, 0)) < 0) { |
4478 | | return ret; |
4479 | | } |
4480 | | #endif |
4481 | | #ifdef WOLFSSL_QUIC |
4482 | | if (WOLFSSL_IS_QUIC(ssl) && IsAtLeastTLSv1_3(ssl->version)) { |
4483 | | ret = wolfSSL_quic_add_transport_extensions(ssl, client_hello); |
4484 | | if (ret != 0) |
4485 | | return ret; |
4486 | | } |
4487 | | #endif |
4488 | | |
4489 | | /* find length of outer and inner */ |
4490 | | #if defined(HAVE_ECH) |
4491 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH) { |
4492 | | TLSX* echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
4493 | | if (echX == NULL) |
4494 | | return WOLFSSL_FATAL_ERROR; |
4495 | | |
4496 | | args->ech = (WOLFSSL_ECH*)echX->data; |
4497 | | if (args->ech == NULL) |
4498 | | return WOLFSSL_FATAL_ERROR; |
4499 | | |
4500 | | /* only prepare if we have a chance at acceptance */ |
4501 | | if (ssl->options.echAccepted || args->ech->innerCount == 0) { |
4502 | | /* set the type to inner */ |
4503 | | args->ech->type = ECH_TYPE_INNER; |
4504 | | args->preXLength = (int)args->length; |
4505 | | |
4506 | | /* get size for inner */ |
4507 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4508 | | if (ret != 0) |
4509 | | return ret; |
4510 | | |
4511 | | /* set the type to outer */ |
4512 | | args->ech->type = 0; |
4513 | | /* set innerClientHelloLen to ClientHelloInner + padding + tag */ |
4514 | | args->ech->paddingLen = 31 - ((args->length - 1) % 32); |
4515 | | args->ech->innerClientHelloLen = (word16)(args->length + |
4516 | | args->ech->paddingLen + args->ech->hpke->Nt); |
4517 | | /* set the length back to before we computed ClientHelloInner size */ |
4518 | | args->length = (word32)args->preXLength; |
4519 | | } |
4520 | | } |
4521 | | #endif |
4522 | | |
4523 | | { |
4524 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4525 | | word16 maxFrag = wolfssl_local_GetMaxPlaintextSize(ssl); |
4526 | | word16 lenWithoutExts = args->length; |
4527 | | #endif |
4528 | | |
4529 | | /* Include length of TLS extensions. */ |
4530 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4531 | | if (ret != 0) |
4532 | | return ret; |
4533 | | |
4534 | | /* Total message size. */ |
4535 | | args->sendSz = |
4536 | | (int)(args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ); |
4537 | | |
4538 | | #ifdef WOLFSSL_DTLS13 |
4539 | | if (ssl->options.dtls) |
4540 | | args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4541 | | #endif /* WOLFSSL_DTLS13 */ |
4542 | | |
4543 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4544 | | if (ssl->options.dtls && args->sendSz > maxFrag && |
4545 | | TLSX_Find(ssl->extensions, TLSX_COOKIE) == NULL) { |
4546 | | /* Try again with an empty key share if we would be fragmenting |
4547 | | * without a cookie */ |
4548 | | ret = TLSX_KeyShare_Empty(ssl); |
4549 | | if (ret != 0) |
4550 | | return ret; |
4551 | | args->length = lenWithoutExts; |
4552 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4553 | | if (ret != 0) |
4554 | | return ret; |
4555 | | args->sendSz = (int)(args->length + |
4556 | | DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ); |
4557 | | if (args->sendSz > maxFrag) { |
4558 | | WOLFSSL_MSG("Can't fit first CH in one fragment."); |
4559 | | return BUFFER_ERROR; |
4560 | | } |
4561 | | WOLFSSL_MSG("Sending empty key share so we don't fragment CH1"); |
4562 | | } |
4563 | | #endif |
4564 | | } |
4565 | | |
4566 | | /* Check buffers are big enough and grow if needed. */ |
4567 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) |
4568 | | return ret; |
4569 | | |
4570 | | /* Get position in output buffer to write new message to. */ |
4571 | | args->output = GetOutputBuffer(ssl); |
4572 | | |
4573 | | /* Put the record and handshake headers on. */ |
4574 | | AddTls13Headers(args->output, args->length, client_hello, ssl); |
4575 | | |
4576 | | /* Protocol version - negotiation now in extension: supported_versions. */ |
4577 | | args->output[args->idx++] = major; |
4578 | | args->output[args->idx++] = tls12minor; |
4579 | | |
4580 | | /* Keep for downgrade. */ |
4581 | | ssl->chVersion = ssl->version; |
4582 | | |
4583 | | if (ssl->arrays == NULL) { |
4584 | | return BAD_FUNC_ARG; |
4585 | | } |
4586 | | /* Client Random */ |
4587 | | if (ssl->options.connectState == CONNECT_BEGIN) { |
4588 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + args->idx, RAN_LEN); |
4589 | | if (ret != 0) |
4590 | | return ret; |
4591 | | |
4592 | | /* Store random for possible second ClientHello. */ |
4593 | | XMEMCPY(ssl->arrays->clientRandom, args->output + args->idx, RAN_LEN); |
4594 | | } |
4595 | | else |
4596 | | XMEMCPY(args->output + args->idx, ssl->arrays->clientRandom, RAN_LEN); |
4597 | | |
4598 | | #if defined(HAVE_ECH) |
4599 | | args->clientRandomOffset = (int)args->idx; |
4600 | | #endif |
4601 | | |
4602 | | args->idx += RAN_LEN; |
4603 | | |
4604 | | GetTls13SessionId(ssl, args->output, &args->idx); |
4605 | | |
4606 | | #ifdef WOLFSSL_DTLS13 |
4607 | | if (ssl->options.dtls) { |
4608 | | args->output[args->idx++] = ssl->arrays->cookieSz; |
4609 | | |
4610 | | if (ssl->arrays->cookieSz > 0) { |
4611 | | /* We have a cookie saved, so the server sent us an |
4612 | | * HelloVerifyRequest, it means it is a v1.2 server */ |
4613 | | if (!ssl->options.downgrade) |
4614 | | return VERSION_ERROR; |
4615 | | XMEMCPY(args->output + args->idx, ssl->arrays->cookie, |
4616 | | ssl->arrays->cookieSz); |
4617 | | args->idx += ssl->arrays->cookieSz; |
4618 | | } |
4619 | | } |
4620 | | #endif /* WOLFSSL_DTLS13 */ |
4621 | | |
4622 | | /* Cipher suites */ |
4623 | | c16toa(suites->suiteSz, args->output + args->idx); |
4624 | | args->idx += OPAQUE16_LEN; |
4625 | | XMEMCPY(args->output + args->idx, &suites->suites, |
4626 | | suites->suiteSz); |
4627 | | args->idx += suites->suiteSz; |
4628 | | #ifdef WOLFSSL_DEBUG_TLS |
4629 | | { |
4630 | | int ii; |
4631 | | WOLFSSL_MSG("Ciphers:"); |
4632 | | for (ii = 0 ; ii < suites->suiteSz; ii += 2) { |
4633 | | WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0], |
4634 | | suites->suites[ii+1])); |
4635 | | } |
4636 | | } |
4637 | | #endif |
4638 | | |
4639 | | /* Compression not supported in TLS v1.3. */ |
4640 | | args->output[args->idx++] = COMP_LEN; |
4641 | | args->output[args->idx++] = NO_COMPRESSION; |
4642 | | |
4643 | | #if defined(HAVE_ECH) |
4644 | | /* write inner then outer */ |
4645 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4646 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4647 | | /* set the type to inner */ |
4648 | | args->ech->type = ECH_TYPE_INNER; |
4649 | | /* innerClientHello may already exist from hrr, free if it does */ |
4650 | | if (args->ech->innerClientHello != NULL) { |
4651 | | XFREE(args->ech->innerClientHello, ssl->heap, |
4652 | | DYNAMIC_TYPE_TMP_BUFFER); |
4653 | | } |
4654 | | /* allocate the inner */ |
4655 | | args->ech->innerClientHello = |
4656 | | (byte*)XMALLOC(args->ech->innerClientHelloLen - args->ech->hpke->Nt, |
4657 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
4658 | | if (args->ech->innerClientHello == NULL) |
4659 | | return MEMORY_E; |
4660 | | /* set the padding bytes to 0 */ |
4661 | | XMEMSET(args->ech->innerClientHello + args->ech->innerClientHelloLen - |
4662 | | args->ech->hpke->Nt - args->ech->paddingLen, 0, |
4663 | | args->ech->paddingLen); |
4664 | | /* copy the client hello to the ech innerClientHello, exclude record */ |
4665 | | /* and handshake headers */ |
4666 | | XMEMCPY(args->ech->innerClientHello, |
4667 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4668 | | args->idx - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ)); |
4669 | | /* copy the client random to inner */ |
4670 | | XMEMCPY(ssl->arrays->clientRandomInner, ssl->arrays->clientRandom, |
4671 | | RAN_LEN); |
4672 | | /* change the outer client random */ |
4673 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + |
4674 | | args->clientRandomOffset, RAN_LEN); |
4675 | | if (ret != 0) |
4676 | | return ret; |
4677 | | /* copy the new client random */ |
4678 | | XMEMCPY(ssl->arrays->clientRandom, args->output + |
4679 | | args->clientRandomOffset, RAN_LEN); |
4680 | | /* write the extensions for inner */ |
4681 | | args->length = 0; |
4682 | | ret = TLSX_WriteRequest(ssl, args->ech->innerClientHello + args->idx - |
4683 | | (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ), client_hello, |
4684 | | &args->length); |
4685 | | if (ret != 0) |
4686 | | return ret; |
4687 | | /* set the type to outer */ |
4688 | | args->ech->type = 0; |
4689 | | } |
4690 | | #endif |
4691 | | |
4692 | | /* Write out extensions for a request. */ |
4693 | | args->length = 0; |
4694 | | ret = TLSX_WriteRequest(ssl, args->output + args->idx, client_hello, |
4695 | | &args->length); |
4696 | | if (ret != 0) |
4697 | | return ret; |
4698 | | |
4699 | | args->idx += args->length; |
4700 | | |
4701 | | #if defined(HAVE_ECH) |
4702 | | /* encrypt and pack the ech innerClientHello */ |
4703 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4704 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4705 | | ret = TLSX_FinalizeEch(args->ech, |
4706 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4707 | | (word32)(args->sendSz - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ))); |
4708 | | |
4709 | | if (ret != 0) |
4710 | | return ret; |
4711 | | } |
4712 | | #endif |
4713 | | |
4714 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4715 | | /* Resumption has a specific set of extensions and binder is calculated |
4716 | | * for each identity. |
4717 | | */ |
4718 | | if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY)) { |
4719 | | ret = WritePSKBinders(ssl, args->output, args->idx); |
4720 | | } |
4721 | | else |
4722 | | #endif |
4723 | | { |
4724 | | #ifdef WOLFSSL_DTLS13 |
4725 | | if (ssl->options.dtls) |
4726 | | ret = Dtls13HashHandshake(ssl, |
4727 | | args->output + Dtls13GetRlHeaderLength(ssl, 0), |
4728 | | (word16)args->idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4729 | | else |
4730 | | #endif /* WOLFSSL_DTLS13 */ |
4731 | | { |
4732 | | #if defined(HAVE_ECH) |
4733 | | /* compute the inner hash */ |
4734 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4735 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) |
4736 | | ret = EchHashHelloInner(ssl, args->ech); |
4737 | | #endif |
4738 | | /* compute the outer hash */ |
4739 | | if (ret == 0) |
4740 | | ret = HashOutput(ssl, args->output, (int)args->idx, 0); |
4741 | | } |
4742 | | } |
4743 | | if (ret != 0) |
4744 | | return ret; |
4745 | | |
4746 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
4747 | | |
4748 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
4749 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
4750 | | if (ssl->toInfoOn) { |
4751 | | ret = AddPacketInfo(ssl, "ClientHello", handshake, args->output, |
4752 | | args->sendSz, WRITE_PROTO, 0, ssl->heap); |
4753 | | if (ret != 0) |
4754 | | return ret; |
4755 | | } |
4756 | | #endif |
4757 | | |
4758 | | ssl->options.buildingMsg = 0; |
4759 | | #ifdef WOLFSSL_DTLS13 |
4760 | | if (ssl->options.dtls) { |
4761 | | ret = Dtls13HandshakeSend(ssl, args->output, (word16)args->sendSz, |
4762 | | (word16)args->idx, client_hello, 0); |
4763 | | break; |
4764 | | } |
4765 | | #endif /* WOLFSSL_DTLS13 */ |
4766 | | |
4767 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
4768 | | |
4769 | | /* Advance state and proceed */ |
4770 | | ssl->options.asyncState = TLS_ASYNC_END; |
4771 | | } |
4772 | | /* case TLS_ASYNC_BUILD */ |
4773 | | FALL_THROUGH; |
4774 | | |
4775 | | case TLS_ASYNC_END: |
4776 | | { |
4777 | | #ifdef WOLFSSL_EARLY_DATA_GROUP |
4778 | | /* QUIC needs to forward records at their encryption level |
4779 | | * and is therefore unable to group here */ |
4780 | | if (ssl->earlyData == no_early_data || WOLFSSL_IS_QUIC(ssl)) |
4781 | | #endif |
4782 | | ret = SendBuffered(ssl); |
4783 | | |
4784 | | break; |
4785 | | } |
4786 | | default: |
4787 | | ret = INPUT_CASE_ERROR; |
4788 | | } /* switch (ssl->options.asyncState) */ |
4789 | | |
4790 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4791 | | if (ret == 0) |
4792 | | FreeAsyncCtx(ssl, 0); |
4793 | | #endif |
4794 | | |
4795 | | WOLFSSL_LEAVE("SendTls13ClientHello", ret); |
4796 | | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND); |
4797 | | |
4798 | | return ret; |
4799 | | } |
4800 | | |
4801 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_CLIENT) |
4802 | | static int Dtls13ClientDoDowngrade(WOLFSSL* ssl) |
4803 | | { |
4804 | | int ret; |
4805 | | if (ssl->dtls13ClientHello == NULL) |
4806 | | return BAD_STATE_E; |
4807 | | |
4808 | | /* v1.3 and v1.2 hash messages to compute the transcript hash. When we are |
4809 | | * using DTLSv1.3 we hash the first clientHello following v1.3 but the |
4810 | | * server can negotiate a lower version. So we need to re-hash the |
4811 | | * clientHello to adhere to DTLS <= v1.2 rules. */ |
4812 | | ret = InitHandshakeHashes(ssl); |
4813 | | if (ret != 0) |
4814 | | return ret; |
4815 | | ret = HashRaw(ssl, ssl->dtls13ClientHello, ssl->dtls13ClientHelloSz); |
4816 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
4817 | | ssl->dtls13ClientHello = NULL; |
4818 | | ssl->dtls13ClientHelloSz = 0; |
4819 | | ssl->keys.dtls_sequence_number_hi = |
4820 | | (word16)w64GetHigh32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
4821 | | ssl->keys.dtls_sequence_number_lo = |
4822 | | w64GetLow32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
4823 | | return ret; |
4824 | | } |
4825 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_CLIENT*/ |
4826 | | |
4827 | | #if defined(HAVE_ECH) |
4828 | | /* check if the server accepted ech or not, must be run after an hsHashes |
4829 | | * restart */ |
4830 | | static int EchCheckAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
4831 | | const byte* input, int acceptOffset, int helloSz) |
4832 | | { |
4833 | | int ret = 0; |
4834 | | int digestType = 0; |
4835 | | int digestSize = 0; |
4836 | | HS_Hashes* tmpHashes; |
4837 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
4838 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
4839 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
4840 | | byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ]; |
4841 | | XMEMSET(zeros, 0, sizeof(zeros)); |
4842 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
4843 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
4844 | | XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation)); |
4845 | | /* store so we can restore regardless of the outcome */ |
4846 | | tmpHashes = ssl->hsHashes; |
4847 | | /* swap hsHashes to hsHashesEch */ |
4848 | | ssl->hsHashes = ssl->hsHashesEch; |
4849 | | /* hash up to the last 8 bytes */ |
4850 | | ret = HashRaw(ssl, input, acceptOffset); |
4851 | | /* hash 8 zeros */ |
4852 | | if (ret == 0) |
4853 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
4854 | | /* hash the rest of the hello */ |
4855 | | if (ret == 0) { |
4856 | | ret = HashRaw(ssl, input + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
4857 | | helloSz + HANDSHAKE_HEADER_SZ - |
4858 | | (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
4859 | | } |
4860 | | /* get the modified transcript hash */ |
4861 | | if (ret == 0) |
4862 | | ret = GetMsgHash(ssl, transcriptEchConf); |
4863 | | if (ret > 0) |
4864 | | ret = 0; |
4865 | | /* pick the right type and size based on mac_algorithm */ |
4866 | | if (ret == 0) { |
4867 | | switch (ssl->specs.mac_algorithm) { |
4868 | | #ifndef NO_SHA256 |
4869 | | case sha256_mac: |
4870 | | digestType = WC_SHA256; |
4871 | | digestSize = WC_SHA256_DIGEST_SIZE; |
4872 | | break; |
4873 | | #endif /* !NO_SHA256 */ |
4874 | | #ifdef WOLFSSL_SHA384 |
4875 | | case sha384_mac: |
4876 | | digestType = WC_SHA384; |
4877 | | digestSize = WC_SHA384_DIGEST_SIZE; |
4878 | | break; |
4879 | | #endif /* WOLFSSL_SHA384 */ |
4880 | | #ifdef WOLFSSL_TLS13_SHA512 |
4881 | | case sha512_mac: |
4882 | | digestType = WC_SHA512; |
4883 | | digestSize = WC_SHA512_DIGEST_SIZE; |
4884 | | break; |
4885 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
4886 | | #ifdef WOLFSSL_SM3 |
4887 | | case sm3_mac: |
4888 | | digestType = WC_SM3; |
4889 | | digestSize = WC_SM3_DIGEST_SIZE; |
4890 | | break; |
4891 | | #endif /* WOLFSSL_SM3 */ |
4892 | | default: |
4893 | | ret = WOLFSSL_FATAL_ERROR; |
4894 | | break; |
4895 | | } |
4896 | | } |
4897 | | /* extract clientRandomInner with a key of all zeros */ |
4898 | | if (ret == 0) { |
4899 | | PRIVATE_KEY_UNLOCK(); |
4900 | | #if !defined(HAVE_FIPS) || \ |
4901 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
4902 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
4903 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk, |
4904 | | ssl->heap, ssl->devId); |
4905 | | #else |
4906 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4907 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk); |
4908 | | #endif |
4909 | | PRIVATE_KEY_LOCK(); |
4910 | | } |
4911 | | /* tls expand with the confirmation label */ |
4912 | | if (ret == 0) { |
4913 | | PRIVATE_KEY_UNLOCK(); |
4914 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptConfirmation, |
4915 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4916 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4917 | | transcriptEchConf, (word32)digestSize, digestType, |
4918 | | WOLFSSL_SERVER_END); |
4919 | | PRIVATE_KEY_LOCK(); |
4920 | | } |
4921 | | if (ret == 0) { |
4922 | | /* last 8 bytes should match our expand output */ |
4923 | | ret = XMEMCMP(acceptConfirmation, input + acceptOffset, |
4924 | | ECH_ACCEPT_CONFIRMATION_SZ); |
4925 | | /* ech accepted */ |
4926 | | if (ret == 0) { |
4927 | | /* set echAccepted to 1 */ |
4928 | | ssl->options.echAccepted = 1; |
4929 | | /* free hsHashes and go with inner */ |
4930 | | ssl->hsHashes = tmpHashes; |
4931 | | FreeHandshakeHashes(ssl); |
4932 | | ssl->hsHashes = ssl->hsHashesEch; |
4933 | | tmpHashes = ssl->hsHashesEchInner; |
4934 | | ssl->hsHashesEchInner = NULL; |
4935 | | } |
4936 | | /* ech rejected */ |
4937 | | else { |
4938 | | /* set echAccepted to 0, needed in case HRR */ |
4939 | | ssl->options.echAccepted = 0; |
4940 | | /* free inner since we're continuing with outer */ |
4941 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4942 | | FreeHandshakeHashes(ssl); |
4943 | | ssl->hsHashesEchInner = NULL; |
4944 | | } |
4945 | | /* continue with outer if we failed to verify ech was accepted */ |
4946 | | ret = 0; |
4947 | | } |
4948 | | FreeHandshakeHashes(ssl); |
4949 | | /* set hsHashesEch to NULL to avoid double free */ |
4950 | | ssl->hsHashesEch = NULL; |
4951 | | /* swap to tmp, will be inner if accepted, hsHashes if rejected */ |
4952 | | ssl->hsHashes = tmpHashes; |
4953 | | return ret; |
4954 | | } |
4955 | | |
4956 | | /* replace the last acceptance field for either sever hello or hrr with the ech |
4957 | | * acceptance parameter, return status */ |
4958 | | static int EchWriteAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
4959 | | byte* output, int acceptOffset, int helloSz, byte msgType) |
4960 | | { |
4961 | | int ret = 0; |
4962 | | int digestType = 0; |
4963 | | int digestSize = 0; |
4964 | | HS_Hashes* tmpHashes = NULL; |
4965 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
4966 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
4967 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
4968 | | XMEMSET(zeros, 0, sizeof(zeros)); |
4969 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
4970 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
4971 | | /* store so we can restore regardless of the outcome */ |
4972 | | tmpHashes = ssl->hsHashes; |
4973 | | ssl->hsHashes = ssl->hsHashesEch; |
4974 | | /* hash up to the acceptOffset */ |
4975 | | ret = HashRaw(ssl, output, acceptOffset); |
4976 | | /* hash 8 zeros */ |
4977 | | if (ret == 0) |
4978 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
4979 | | /* hash the rest of the hello */ |
4980 | | if (ret == 0) { |
4981 | | ret = HashRaw(ssl, output + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
4982 | | helloSz - (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
4983 | | } |
4984 | | /* get the modified transcript hash */ |
4985 | | if (ret == 0) |
4986 | | ret = GetMsgHash(ssl, transcriptEchConf); |
4987 | | if (ret > 0) |
4988 | | ret = 0; |
4989 | | /* pick the right type and size based on mac_algorithm */ |
4990 | | if (ret == 0) { |
4991 | | switch (ssl->specs.mac_algorithm) { |
4992 | | #ifndef NO_SHA256 |
4993 | | case sha256_mac: |
4994 | | digestType = WC_SHA256; |
4995 | | digestSize = WC_SHA256_DIGEST_SIZE; |
4996 | | break; |
4997 | | #endif /* !NO_SHA256 */ |
4998 | | #ifdef WOLFSSL_SHA384 |
4999 | | case sha384_mac: |
5000 | | digestType = WC_SHA384; |
5001 | | digestSize = WC_SHA384_DIGEST_SIZE; |
5002 | | break; |
5003 | | #endif /* WOLFSSL_SHA384 */ |
5004 | | #ifdef WOLFSSL_TLS13_SHA512 |
5005 | | case sha512_mac: |
5006 | | digestType = WC_SHA512; |
5007 | | digestSize = WC_SHA512_DIGEST_SIZE; |
5008 | | break; |
5009 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
5010 | | #ifdef WOLFSSL_SM3 |
5011 | | case sm3_mac: |
5012 | | digestType = WC_SM3; |
5013 | | digestSize = WC_SM3_DIGEST_SIZE; |
5014 | | break; |
5015 | | #endif /* WOLFSSL_SM3 */ |
5016 | | default: |
5017 | | ret = WOLFSSL_FATAL_ERROR; |
5018 | | break; |
5019 | | } |
5020 | | } |
5021 | | /* extract clientRandom with a key of all zeros */ |
5022 | | if (ret == 0) { |
5023 | | PRIVATE_KEY_UNLOCK(); |
5024 | | #if !defined(HAVE_FIPS) || \ |
5025 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
5026 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
5027 | | ssl->arrays->clientRandom, RAN_LEN, expandLabelPrk, |
5028 | | ssl->heap, ssl->devId); |
5029 | | #else |
5030 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
5031 | | ssl->arrays->clientRandom, RAN_LEN, expandLabelPrk); |
5032 | | #endif |
5033 | | PRIVATE_KEY_LOCK(); |
5034 | | } |
5035 | | /* tls expand with the confirmation label */ |
5036 | | if (ret == 0) { |
5037 | | PRIVATE_KEY_UNLOCK(); |
5038 | | ret = Tls13HKDFExpandKeyLabel(ssl, output + acceptOffset, |
5039 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
5040 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
5041 | | transcriptEchConf, (word32)digestSize, digestType, |
5042 | | WOLFSSL_SERVER_END); |
5043 | | PRIVATE_KEY_LOCK(); |
5044 | | } |
5045 | | /* mark that ech was accepted */ |
5046 | | if (ret == 0 && msgType != hello_retry_request) |
5047 | | ssl->options.echAccepted = 1; |
5048 | | /* free hsHashesEch, if this is an HRR we will start at client hello 2*/ |
5049 | | FreeHandshakeHashes(ssl); |
5050 | | ssl->hsHashesEch = NULL; |
5051 | | ssl->hsHashes = tmpHashes; |
5052 | | return ret; |
5053 | | } |
5054 | | #endif |
5055 | | |
5056 | | /* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */ |
5057 | | /* Handle the ServerHello message from the server. |
5058 | | * Only a client will receive this message. |
5059 | | * |
5060 | | * ssl The SSL/TLS object. |
5061 | | * input The message buffer. |
5062 | | * inOutIdx On entry, the index into the message buffer of ServerHello. |
5063 | | * On exit, the index of byte after the ServerHello message. |
5064 | | * helloSz The length of the current handshake message. |
5065 | | * returns 0 on success and otherwise failure. |
5066 | | */ |
5067 | | |
5068 | | typedef struct Dsh13Args { |
5069 | | ProtocolVersion pv; |
5070 | | word32 idx; |
5071 | | word32 begin; |
5072 | | const byte* sessId; |
5073 | | word16 totalExtSz; |
5074 | | byte sessIdSz; |
5075 | | byte extMsgType; |
5076 | | #if defined(HAVE_ECH) |
5077 | | TLSX* echX; |
5078 | | byte* acceptLabel; |
5079 | | word32 acceptOffset; |
5080 | | word16 acceptLabelSz; |
5081 | | #endif |
5082 | | } Dsh13Args; |
5083 | | |
5084 | | int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
5085 | | word32 helloSz, byte* extMsgType) |
5086 | 0 | { |
5087 | 0 | int ret; |
5088 | 0 | byte suite[2]; |
5089 | 0 | byte tls12minor; |
5090 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5091 | | Dsh13Args* args = NULL; |
5092 | | #else |
5093 | 0 | Dsh13Args args[1]; |
5094 | 0 | #endif |
5095 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5096 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
5097 | | #endif |
5098 | |
|
5099 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO); |
5100 | 0 | WOLFSSL_ENTER("DoTls13ServerHello"); |
5101 | |
|
5102 | 0 | if (ssl == NULL || ssl->arrays == NULL) |
5103 | 0 | return BAD_FUNC_ARG; |
5104 | | |
5105 | 0 | tls12minor = TLSv1_2_MINOR; |
5106 | |
|
5107 | | #ifdef WOLFSSL_DTLS13 |
5108 | | if (ssl->options.dtls) |
5109 | | tls12minor = DTLSv1_2_MINOR; |
5110 | | #endif /* WOLFSSL_DTLS13 */ |
5111 | |
|
5112 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5113 | | if (ssl->async == NULL) { |
5114 | | ssl->async = (struct WOLFSSL_ASYNC*) |
5115 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
5116 | | DYNAMIC_TYPE_ASYNC); |
5117 | | if (ssl->async == NULL) |
5118 | | return MEMORY_E; |
5119 | | ssl->async->freeArgs = NULL; |
5120 | | } |
5121 | | args = (Dsh13Args*)ssl->async->args; |
5122 | | |
5123 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
5124 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
5125 | | /* Check for error */ |
5126 | | if (ret < 0) { |
5127 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5128 | | /* Mark message as not received so it can process again */ |
5129 | | ssl->msgsReceived.got_server_hello = 0; |
5130 | | } |
5131 | | return ret; |
5132 | | } |
5133 | | } |
5134 | | else |
5135 | | #endif |
5136 | 0 | { |
5137 | | /* Reset state */ |
5138 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
5139 | 0 | XMEMSET(args, 0, sizeof(Dsh13Args)); |
5140 | 0 | } |
5141 | |
|
5142 | 0 | switch (ssl->options.asyncState) { |
5143 | 0 | case TLS_ASYNC_BEGIN: |
5144 | 0 | { |
5145 | 0 | byte b; |
5146 | | #ifdef WOLFSSL_CALLBACKS |
5147 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello"); |
5148 | | if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo); |
5149 | | #endif |
5150 | | |
5151 | | /* Protocol version length check. */ |
5152 | 0 | if (helloSz < OPAQUE16_LEN) |
5153 | 0 | return BUFFER_ERROR; |
5154 | | |
5155 | 0 | args->idx = *inOutIdx; |
5156 | 0 | args->begin = args->idx; |
5157 | | |
5158 | | /* Protocol version */ |
5159 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
5160 | 0 | args->idx += OPAQUE16_LEN; |
5161 | |
|
5162 | | #ifdef WOLFSSL_DTLS |
5163 | | if (ssl->options.dtls && |
5164 | | (args->pv.major != DTLS_MAJOR || args->pv.minor == DTLS_BOGUS_MINOR)) |
5165 | | return VERSION_ERROR; |
5166 | | #endif /* WOLFSSL_DTLS */ |
5167 | |
|
5168 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5169 | 0 | { |
5170 | 0 | byte wantDowngrade; |
5171 | |
|
5172 | 0 | wantDowngrade = args->pv.major == ssl->version.major && |
5173 | 0 | args->pv.minor < TLSv1_2_MINOR; |
5174 | |
|
5175 | | #ifdef WOLFSSL_DTLS13 |
5176 | | if (ssl->options.dtls) |
5177 | | wantDowngrade = args->pv.major == ssl->version.major && |
5178 | | args->pv.minor > DTLSv1_2_MINOR; |
5179 | | #endif /* WOLFSSL_DTLS13 */ |
5180 | |
|
5181 | 0 | if (wantDowngrade && ssl->options.downgrade) { |
5182 | | /* Force client hello version 1.2 to work for static RSA. */ |
5183 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5184 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5185 | 0 | ssl->options.tls1_3 = 0; |
5186 | |
|
5187 | | #ifdef WOLFSSL_DTLS13 |
5188 | | if (ssl->options.dtls) { |
5189 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5190 | | ssl->version.minor = DTLSv1_2_MINOR; |
5191 | | ret = Dtls13ClientDoDowngrade(ssl); |
5192 | | if (ret != 0) |
5193 | | return ret; |
5194 | | } |
5195 | | #endif /* WOLFSSL_DTLS13 */ |
5196 | |
|
5197 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5198 | 0 | } |
5199 | 0 | } |
5200 | 0 | #endif |
5201 | | |
5202 | 0 | if (args->pv.major != ssl->version.major || |
5203 | 0 | args->pv.minor != tls12minor) { |
5204 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5205 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5206 | 0 | return VERSION_ERROR; |
5207 | 0 | } |
5208 | | |
5209 | | /* Random and session id length check */ |
5210 | 0 | if ((args->idx - args->begin) + RAN_LEN + ENUM_LEN > helloSz) |
5211 | 0 | return BUFFER_ERROR; |
5212 | | |
5213 | | /* Check if hello retry request */ |
5214 | 0 | if (XMEMCMP(input + args->idx, helloRetryRequestRandom, RAN_LEN) == 0) { |
5215 | 0 | WOLFSSL_MSG("HelloRetryRequest format"); |
5216 | 0 | *extMsgType = hello_retry_request; |
5217 | |
|
5218 | 0 | if (ssl->msgsReceived.got_hello_verify_request) { |
5219 | 0 | WOLFSSL_MSG("Received HelloRetryRequest after a " |
5220 | 0 | "HelloVerifyRequest"); |
5221 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5222 | 0 | return VERSION_ERROR; |
5223 | 0 | } |
5224 | | |
5225 | | /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat. |
5226 | | * Found message to be a HelloRetryRequest. |
5227 | | * Don't allow more than one HelloRetryRequest or ServerHello. |
5228 | | */ |
5229 | 0 | if (ssl->msgsReceived.got_hello_retry_request) { |
5230 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
5231 | 0 | return DUPLICATE_MSG_E; |
5232 | 0 | } |
5233 | 0 | } |
5234 | 0 | args->extMsgType = *extMsgType; |
5235 | | |
5236 | | /* Server random - keep for debugging. */ |
5237 | 0 | XMEMCPY(ssl->arrays->serverRandom, input + args->idx, RAN_LEN); |
5238 | | #if defined(HAVE_ECH) |
5239 | | /* last 8 bytes of server random */ |
5240 | | args->acceptOffset = args->idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
5241 | | #endif |
5242 | 0 | args->idx += RAN_LEN; |
5243 | | |
5244 | | /* Session id */ |
5245 | 0 | args->sessIdSz = input[args->idx++]; |
5246 | 0 | if ((args->idx - args->begin) + args->sessIdSz > helloSz) |
5247 | 0 | return BUFFER_ERROR; |
5248 | 0 | args->sessId = input + args->idx; |
5249 | 0 | args->idx += args->sessIdSz; |
5250 | |
|
5251 | 0 | ssl->options.haveSessionId = 1; |
5252 | | |
5253 | | /* Ciphersuite and compression check */ |
5254 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz) |
5255 | 0 | return BUFFER_ERROR; |
5256 | | |
5257 | | /* Set the cipher suite from the message. */ |
5258 | 0 | ssl->options.cipherSuite0 = input[args->idx++]; |
5259 | 0 | ssl->options.cipherSuite = input[args->idx++]; |
5260 | 0 | if (*extMsgType == hello_retry_request) { |
5261 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
5262 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
5263 | 0 | } |
5264 | 0 | else if (ssl->msgsReceived.got_hello_retry_request && |
5265 | 0 | (ssl->options.hrrCipherSuite0 != ssl->options.cipherSuite0 || |
5266 | 0 | ssl->options.hrrCipherSuite != ssl->options.cipherSuite)) { |
5267 | 0 | WOLFSSL_MSG("Received ServerHello with different cipher suite than " |
5268 | 0 | "HelloRetryRequest"); |
5269 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5270 | 0 | return INVALID_PARAMETER; |
5271 | 0 | } |
5272 | | #ifdef WOLFSSL_DEBUG_TLS |
5273 | | WOLFSSL_MSG("Chosen cipher suite:"); |
5274 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
5275 | | ssl->options.cipherSuite)); |
5276 | | #endif |
5277 | | |
5278 | | /* Compression */ |
5279 | 0 | b = input[args->idx++]; |
5280 | 0 | if (b != 0) { |
5281 | 0 | WOLFSSL_MSG("Must be no compression types in list"); |
5282 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5283 | 0 | return INVALID_PARAMETER; |
5284 | 0 | } |
5285 | | |
5286 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) { |
5287 | 0 | if (!ssl->options.downgrade) |
5288 | 0 | return BUFFER_ERROR; |
5289 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5290 | | /* Force client hello version 1.2 to work for static RSA. */ |
5291 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5292 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5293 | |
|
5294 | | #ifdef WOLFSSL_DTLS13 |
5295 | | if (ssl->options.dtls) { |
5296 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5297 | | ssl->version.minor = DTLSv1_2_MINOR; |
5298 | | ssl->options.tls1_3 = 0; |
5299 | | ret = Dtls13ClientDoDowngrade(ssl); |
5300 | | if (ret != 0) |
5301 | | return ret; |
5302 | | } |
5303 | | #endif /* WOLFSSL_DTLS13 */ |
5304 | |
|
5305 | 0 | #endif |
5306 | 0 | ssl->options.haveEMS = 0; |
5307 | 0 | if (args->pv.minor < ssl->options.minDowngrade) { |
5308 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5309 | 0 | return VERSION_ERROR; |
5310 | 0 | } |
5311 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5312 | 0 | ssl->options.tls1_3 = 0; |
5313 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5314 | | #else |
5315 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5316 | | return VERSION_ERROR; |
5317 | | #endif |
5318 | 0 | } |
5319 | | |
5320 | 0 | if ((args->idx - args->begin) < helloSz) { |
5321 | 0 | int foundVersion; |
5322 | | |
5323 | | /* Get extension length and length check. */ |
5324 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
5325 | 0 | return BUFFER_ERROR; |
5326 | 0 | ato16(&input[args->idx], &args->totalExtSz); |
5327 | 0 | args->idx += OPAQUE16_LEN; |
5328 | 0 | if ((args->idx - args->begin) + args->totalExtSz > helloSz) |
5329 | 0 | return BUFFER_ERROR; |
5330 | | |
5331 | | /* Need to negotiate version first. */ |
5332 | 0 | if ((ret = TLSX_ParseVersion(ssl, input + args->idx, |
5333 | 0 | args->totalExtSz, *extMsgType, &foundVersion))) { |
5334 | 0 | return ret; |
5335 | 0 | } |
5336 | 0 | if (!foundVersion) { |
5337 | 0 | if (!ssl->options.downgrade) { |
5338 | 0 | WOLFSSL_MSG("Server trying to downgrade to version less than " |
5339 | 0 | "TLS v1.3"); |
5340 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5341 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5342 | 0 | return VERSION_ERROR; |
5343 | 0 | } |
5344 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \ |
5345 | | defined(WOLFSSL_WPAS_SMALL) |
5346 | | /* Check if client has disabled TLS 1.2 */ |
5347 | | if (args->pv.minor == TLSv1_2_MINOR && |
5348 | | (ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) |
5349 | | == WOLFSSL_OP_NO_TLSv1_2) |
5350 | | { |
5351 | | WOLFSSL_MSG("\tOption set to not allow TLSv1.2"); |
5352 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5353 | | return VERSION_ERROR; |
5354 | | } |
5355 | | #endif |
5356 | | |
5357 | 0 | if (!ssl->options.dtls && |
5358 | 0 | args->pv.minor < ssl->options.minDowngrade) { |
5359 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5360 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5361 | 0 | return VERSION_ERROR; |
5362 | 0 | } |
5363 | | |
5364 | 0 | if (ssl->options.dtls && |
5365 | 0 | args->pv.minor > ssl->options.minDowngrade) { |
5366 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5367 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5368 | 0 | return VERSION_ERROR; |
5369 | 0 | } |
5370 | | |
5371 | 0 | ssl->version.minor = args->pv.minor; |
5372 | 0 | ssl->options.tls1_3 = 0; |
5373 | |
|
5374 | | #ifdef WOLFSSL_DTLS13 |
5375 | | if (ssl->options.dtls) { |
5376 | | ret = Dtls13ClientDoDowngrade(ssl); |
5377 | | if (ret != 0) |
5378 | | return ret; |
5379 | | } |
5380 | | #endif /* WOLFSSL_DTLS13 */ |
5381 | 0 | } |
5382 | 0 | } |
5383 | | |
5384 | | #ifdef WOLFSSL_DTLS13 |
5385 | | /* we are sure that version is >= v1.3 now, we can get rid of buffered |
5386 | | * ClientHello that was buffered to re-compute the hash in case of |
5387 | | * downgrade */ |
5388 | | if (ssl->options.dtls && ssl->dtls13ClientHello != NULL) { |
5389 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5390 | | ssl->dtls13ClientHello = NULL; |
5391 | | ssl->dtls13ClientHelloSz = 0; |
5392 | | } |
5393 | | #endif /* WOLFSSL_DTLS13 */ |
5394 | | |
5395 | | /* Advance state and proceed */ |
5396 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
5397 | 0 | } /* case TLS_ASYNC_BEGIN */ |
5398 | 0 | FALL_THROUGH; |
5399 | |
|
5400 | 0 | case TLS_ASYNC_BUILD: |
5401 | 0 | case TLS_ASYNC_DO: |
5402 | 0 | { |
5403 | | /* restore message type */ |
5404 | 0 | *extMsgType = args->extMsgType; |
5405 | | |
5406 | | /* Parse and handle extensions, unless lower than TLS1.3. In that case, |
5407 | | * extensions will be parsed in DoServerHello. */ |
5408 | 0 | if (args->totalExtSz > 0 && IsAtLeastTLSv1_3(ssl->version)) { |
5409 | 0 | ret = TLSX_Parse(ssl, input + args->idx, args->totalExtSz, |
5410 | 0 | *extMsgType, NULL); |
5411 | 0 | if (ret != 0) { |
5412 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5413 | | /* Handle async operation */ |
5414 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5415 | | /* Mark message as not received so it can process again */ |
5416 | | ssl->msgsReceived.got_server_hello = 0; |
5417 | | } |
5418 | | #endif |
5419 | 0 | return ret; |
5420 | 0 | } |
5421 | | |
5422 | 0 | if (*extMsgType == hello_retry_request) { |
5423 | | /* Update counts to reflect change of message type. */ |
5424 | 0 | ssl->msgsReceived.got_hello_retry_request = 1; |
5425 | 0 | ssl->msgsReceived.got_server_hello = 0; |
5426 | 0 | } |
5427 | 0 | } |
5428 | | |
5429 | 0 | if (args->totalExtSz > 0) { |
5430 | 0 | args->idx += args->totalExtSz; |
5431 | 0 | } |
5432 | |
|
5433 | | #ifdef WOLFSSL_DTLS_CID |
5434 | | if (ssl->options.useDtlsCID && *extMsgType == server_hello) |
5435 | | DtlsCIDOnExtensionsParsed(ssl); |
5436 | | #endif /* WOLFSSL_DTLS_CID */ |
5437 | |
|
5438 | 0 | if (IsAtLeastTLSv1_3(ssl->version)) { |
5439 | 0 | *inOutIdx = args->idx; |
5440 | 0 | } |
5441 | |
|
5442 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5443 | |
|
5444 | | #ifdef HAVE_SECRET_CALLBACK |
5445 | | if (ssl->sessionSecretCb != NULL |
5446 | | #ifdef HAVE_SESSION_TICKET |
5447 | | && ssl->session->ticketLen > 0 |
5448 | | #endif |
5449 | | ) { |
5450 | | int secretSz = SECRET_LEN; |
5451 | | ret = ssl->sessionSecretCb(ssl, ssl->session->masterSecret, |
5452 | | &secretSz, ssl->sessionSecretCtx); |
5453 | | if (ret != 0 || secretSz != SECRET_LEN) { |
5454 | | WOLFSSL_ERROR_VERBOSE(SESSION_SECRET_CB_E); |
5455 | | return SESSION_SECRET_CB_E; |
5456 | | } |
5457 | | } |
5458 | | #endif /* HAVE_SECRET_CALLBACK */ |
5459 | | |
5460 | | /* Version only negotiated in extensions for TLS v1.3. |
5461 | | * Only now do we know how to deal with session id. |
5462 | | */ |
5463 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) { |
5464 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5465 | 0 | ssl->arrays->sessionIDSz = args->sessIdSz; |
5466 | |
|
5467 | 0 | if (ssl->arrays->sessionIDSz > ID_LEN) { |
5468 | 0 | WOLFSSL_MSG("Invalid session ID size"); |
5469 | 0 | ssl->arrays->sessionIDSz = 0; |
5470 | 0 | return BUFFER_ERROR; |
5471 | 0 | } |
5472 | 0 | else if (ssl->arrays->sessionIDSz) { |
5473 | 0 | XMEMCPY(ssl->arrays->sessionID, args->sessId, |
5474 | 0 | ssl->arrays->sessionIDSz); |
5475 | 0 | ssl->options.haveSessionId = 1; |
5476 | 0 | } |
5477 | | |
5478 | | /* Force client hello version 1.2 to work for static RSA. */ |
5479 | 0 | if (ssl->options.dtls) |
5480 | 0 | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5481 | 0 | else |
5482 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5483 | | /* Complete TLS v1.2 processing of ServerHello. */ |
5484 | 0 | ret = DoServerHello(ssl, input, inOutIdx, helloSz); |
5485 | | #else |
5486 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
5487 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5488 | | ret = VERSION_ERROR; |
5489 | | #endif |
5490 | |
|
5491 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5492 | |
|
5493 | 0 | return ret; |
5494 | 0 | } |
5495 | | |
5496 | | /* Advance state and proceed */ |
5497 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
5498 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
5499 | 0 | FALL_THROUGH; |
5500 | |
|
5501 | 0 | case TLS_ASYNC_FINALIZE: |
5502 | 0 | { |
5503 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
5504 | | if (ssl->options.tls13MiddleBoxCompat) { |
5505 | | if (args->sessIdSz == 0) { |
5506 | | WOLFSSL_MSG("args->sessIdSz == 0"); |
5507 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5508 | | return INVALID_PARAMETER; |
5509 | | } |
5510 | | if (ssl->session->sessionIDSz != 0) { |
5511 | | if (ssl->session->sessionIDSz != args->sessIdSz || |
5512 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5513 | | args->sessIdSz) != 0) { |
5514 | | WOLFSSL_MSG("session id doesn't match"); |
5515 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5516 | | return INVALID_PARAMETER; |
5517 | | } |
5518 | | } |
5519 | | else if (XMEMCMP(ssl->arrays->clientRandom, args->sessId, |
5520 | | args->sessIdSz) != 0) { |
5521 | | WOLFSSL_MSG("session id doesn't match client random"); |
5522 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5523 | | return INVALID_PARAMETER; |
5524 | | } |
5525 | | } |
5526 | | else |
5527 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
5528 | | #ifdef WOLFSSL_QUIC |
5529 | | if (WOLFSSL_IS_QUIC(ssl)) { |
5530 | | if (args->sessIdSz != 0) { |
5531 | | WOLFSSL_MSG("args->sessIdSz != 0"); |
5532 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5533 | | return INVALID_PARAMETER; |
5534 | | } |
5535 | | } |
5536 | | else |
5537 | | #endif /* WOLFSSL_QUIC */ |
5538 | 0 | if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 && |
5539 | 0 | XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0)) |
5540 | 0 | { |
5541 | 0 | WOLFSSL_MSG("Server sent different session id"); |
5542 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5543 | 0 | return INVALID_PARAMETER; |
5544 | 0 | } |
5545 | | |
5546 | 0 | ret = SetCipherSpecs(ssl); |
5547 | 0 | if (ret != 0) |
5548 | 0 | return ret; |
5549 | | |
5550 | | #ifdef HAVE_NULL_CIPHER |
5551 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
5552 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
5553 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
5554 | | ; |
5555 | | } |
5556 | | else |
5557 | | #endif |
5558 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
5559 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5560 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
5561 | 0 | ; /* Do nothing. */ |
5562 | 0 | } |
5563 | 0 | else |
5564 | 0 | #endif |
5565 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
5566 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5567 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
5568 | 0 | ; /* Do nothing. */ |
5569 | 0 | } |
5570 | 0 | else |
5571 | 0 | #endif |
5572 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
5573 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
5574 | 0 | WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet"); |
5575 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5576 | 0 | return INVALID_PARAMETER; |
5577 | 0 | } |
5578 | | |
5579 | 0 | suite[0] = ssl->options.cipherSuite0; |
5580 | 0 | suite[1] = ssl->options.cipherSuite; |
5581 | 0 | if (!FindSuiteSSL(ssl, suite)) { |
5582 | 0 | WOLFSSL_MSG("Cipher suite not supported on client"); |
5583 | 0 | WOLFSSL_ERROR_VERBOSE(MATCH_SUITE_ERROR); |
5584 | 0 | return MATCH_SUITE_ERROR; |
5585 | 0 | } |
5586 | | |
5587 | | #if defined(HAVE_ECH) |
5588 | | /* check for acceptConfirmation, must be done after hashes restart */ |
5589 | | if (ssl->options.useEch == 1) { |
5590 | | args->echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
5591 | | /* account for hrr extension instead of server random */ |
5592 | | if (args->extMsgType == hello_retry_request) { |
5593 | | args->acceptOffset = |
5594 | | (word32)(((WOLFSSL_ECH*)args->echX->data)->confBuf - input); |
5595 | | args->acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
5596 | | args->acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
5597 | | } |
5598 | | else { |
5599 | | args->acceptLabel = (byte*)echAcceptConfirmationLabel; |
5600 | | args->acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
5601 | | } |
5602 | | /* check acceptance */ |
5603 | | if (ret == 0) { |
5604 | | ret = EchCheckAcceptance(ssl, args->acceptLabel, |
5605 | | args->acceptLabelSz, input, args->acceptOffset, helloSz); |
5606 | | } |
5607 | | if (ret != 0) |
5608 | | return ret; |
5609 | | /* use the inner random for client random */ |
5610 | | if (args->extMsgType != hello_retry_request) { |
5611 | | XMEMCPY(ssl->arrays->clientRandom, ssl->arrays->clientRandomInner, |
5612 | | RAN_LEN); |
5613 | | } |
5614 | | } |
5615 | | #endif /* HAVE_ECH */ |
5616 | | |
5617 | 0 | if (*extMsgType == server_hello) { |
5618 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5619 | | PreSharedKey* psk = NULL; |
5620 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
5621 | | if (ext != NULL) |
5622 | | psk = (PreSharedKey*)ext->data; |
5623 | | while (psk != NULL && !psk->chosen) |
5624 | | psk = psk->next; |
5625 | | if (psk == NULL) { |
5626 | | ssl->options.resuming = 0; |
5627 | | ssl->arrays->psk_keySz = 0; |
5628 | | XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); |
5629 | | } |
5630 | | else { |
5631 | | if ((ret = SetupPskKey(ssl, psk, 0)) != 0) |
5632 | | return ret; |
5633 | | ssl->options.pskNegotiated = 1; |
5634 | | } |
5635 | | #else |
5636 | | /* no resumption possible */ |
5637 | 0 | ssl->options.resuming = 0; |
5638 | 0 | #endif |
5639 | | |
5640 | | /* sanity check on PSK / KSE */ |
5641 | 0 | if ( |
5642 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5643 | | ssl->options.pskNegotiated == 0 && |
5644 | | #endif |
5645 | 0 | ssl->session->namedGroup == 0) { |
5646 | 0 | return EXT_MISSING; |
5647 | 0 | } |
5648 | | |
5649 | 0 | ssl->keys.encryptionOn = 1; |
5650 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5651 | |
|
5652 | 0 | } |
5653 | 0 | else { |
5654 | | /* https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.4 |
5655 | | * Clients MUST abort the handshake with an |
5656 | | * "illegal_parameter" alert if the HelloRetryRequest would not result |
5657 | | * in any change in the ClientHello. |
5658 | | */ |
5659 | | /* Check if the HRR contained a cookie or a keyshare */ |
5660 | 0 | if (!ssl->options.hrrSentKeyShare |
5661 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
5662 | | && !ssl->options.hrrSentCookie |
5663 | | #endif |
5664 | 0 | ) { |
5665 | 0 | SendAlert(ssl, alert_fatal, illegal_parameter); |
5666 | 0 | return DUPLICATE_MSG_E; |
5667 | 0 | } |
5668 | | |
5669 | 0 | ssl->options.tls1_3 = 1; |
5670 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
5671 | |
|
5672 | 0 | ret = RestartHandshakeHash(ssl); |
5673 | 0 | } |
5674 | | |
5675 | 0 | break; |
5676 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
5677 | 0 | default: |
5678 | 0 | ret = INPUT_CASE_ERROR; |
5679 | 0 | } /* switch (ssl->options.asyncState) */ |
5680 | | |
5681 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5682 | | if (ret == 0) |
5683 | | FreeAsyncCtx(ssl, 0); |
5684 | | #endif |
5685 | | |
5686 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5687 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO); |
5688 | |
|
5689 | 0 | return ret; |
5690 | 0 | } |
5691 | | |
5692 | | /* handle processing TLS 1.3 encrypted_extensions (8) */ |
5693 | | /* Parse and handle an EncryptedExtensions message. |
5694 | | * Only a client will receive this message. |
5695 | | * |
5696 | | * ssl The SSL/TLS object. |
5697 | | * input The message buffer. |
5698 | | * inOutIdx On entry, the index into the message buffer of |
5699 | | * EncryptedExtensions. |
5700 | | * On exit, the index of byte after the EncryptedExtensions |
5701 | | * message. |
5702 | | * totalSz The length of the current handshake message. |
5703 | | * returns 0 on success and otherwise failure. |
5704 | | */ |
5705 | | static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input, |
5706 | | word32* inOutIdx, word32 totalSz) |
5707 | 0 | { |
5708 | 0 | int ret; |
5709 | 0 | word32 begin = *inOutIdx; |
5710 | 0 | word32 i = begin; |
5711 | 0 | word16 totalExtSz; |
5712 | |
|
5713 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5714 | 0 | WOLFSSL_ENTER("DoTls13EncryptedExtensions"); |
5715 | |
|
5716 | | #ifdef WOLFSSL_CALLBACKS |
5717 | | if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions"); |
5718 | | if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo); |
5719 | | #endif |
5720 | | |
5721 | | /* Length field of extension data. */ |
5722 | 0 | if (totalSz < OPAQUE16_LEN) |
5723 | 0 | return BUFFER_ERROR; |
5724 | 0 | ato16(&input[i], &totalExtSz); |
5725 | 0 | i += OPAQUE16_LEN; |
5726 | | |
5727 | | /* Extension data. */ |
5728 | 0 | if (i - begin + totalExtSz > totalSz) |
5729 | 0 | return BUFFER_ERROR; |
5730 | 0 | if ((ret = TLSX_Parse(ssl, input + i, totalExtSz, encrypted_extensions, |
5731 | 0 | NULL))) { |
5732 | 0 | return ret; |
5733 | 0 | } |
5734 | | |
5735 | | /* Move index to byte after message. */ |
5736 | 0 | *inOutIdx = i + totalExtSz; |
5737 | | |
5738 | | /* Always encrypted. */ |
5739 | 0 | *inOutIdx += ssl->keys.padSz; |
5740 | |
|
5741 | | #ifdef WOLFSSL_EARLY_DATA |
5742 | | if (ssl->earlyData != no_early_data) { |
5743 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
5744 | | if (ext == NULL || !ext->val) |
5745 | | ssl->earlyData = no_early_data; |
5746 | | } |
5747 | | #endif |
5748 | |
|
5749 | | #ifdef WOLFSSL_EARLY_DATA |
5750 | | if (ssl->earlyData == no_early_data) { |
5751 | | ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY); |
5752 | | if (ret != 0) |
5753 | | return ret; |
5754 | | } |
5755 | | #endif |
5756 | |
|
5757 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
5758 | |
|
5759 | 0 | WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret); |
5760 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5761 | |
|
5762 | 0 | return ret; |
5763 | 0 | } |
5764 | | |
5765 | | #ifndef NO_CERTS |
5766 | | /* handle processing TLS v1.3 certificate_request (13) */ |
5767 | | /* Handle a TLS v1.3 CertificateRequest message. |
5768 | | * This message is always encrypted. |
5769 | | * Only a client will receive this message. |
5770 | | * |
5771 | | * ssl The SSL/TLS object. |
5772 | | * input The message buffer. |
5773 | | * inOutIdx On entry, the index into the message buffer of CertificateRequest. |
5774 | | * On exit, the index of byte after the CertificateRequest message. |
5775 | | * size The length of the current handshake message. |
5776 | | * returns 0 on success and otherwise failure. |
5777 | | */ |
5778 | | static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input, |
5779 | | word32* inOutIdx, word32 size) |
5780 | 0 | { |
5781 | 0 | word16 len; |
5782 | 0 | word32 begin = *inOutIdx; |
5783 | 0 | int ret = 0; |
5784 | 0 | Suites peerSuites; |
5785 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5786 | | CertReqCtx* certReqCtx; |
5787 | | #endif |
5788 | |
|
5789 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5790 | 0 | WOLFSSL_ENTER("DoTls13CertificateRequest"); |
5791 | |
|
5792 | 0 | XMEMSET(&peerSuites, 0, sizeof(Suites)); |
5793 | |
|
5794 | | #ifdef WOLFSSL_CALLBACKS |
5795 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest"); |
5796 | | if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo); |
5797 | | #endif |
5798 | |
|
5799 | | #ifdef WOLFSSL_CERT_SETUP_CB |
5800 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
5801 | | return ret; |
5802 | | #endif |
5803 | |
|
5804 | 0 | if (OPAQUE8_LEN > size) |
5805 | 0 | return BUFFER_ERROR; |
5806 | | |
5807 | | /* Length of the request context. */ |
5808 | 0 | len = input[(*inOutIdx)++]; |
5809 | 0 | if ((*inOutIdx - begin) + len > size) |
5810 | 0 | return BUFFER_ERROR; |
5811 | 0 | if (ssl->options.connectState < FINISHED_DONE && len > 0) |
5812 | 0 | return BUFFER_ERROR; |
5813 | | |
5814 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5815 | | /* CertReqCtx has one byte at end for context value. |
5816 | | * Increase size to handle other implementations sending more than one byte. |
5817 | | * That is, allocate extra space, over one byte, to hold the context value. |
5818 | | */ |
5819 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx) + len - 1, ssl->heap, |
5820 | | DYNAMIC_TYPE_TMP_BUFFER); |
5821 | | if (certReqCtx == NULL) |
5822 | | return MEMORY_E; |
5823 | | certReqCtx->next = ssl->certReqCtx; |
5824 | | certReqCtx->len = len; |
5825 | | XMEMCPY(&certReqCtx->ctx, input + *inOutIdx, len); |
5826 | | ssl->certReqCtx = certReqCtx; |
5827 | | #endif |
5828 | 0 | *inOutIdx += len; |
5829 | | |
5830 | | /* TODO: Add support for more extensions: |
5831 | | * signed_certificate_timestamp, certificate_authorities, oid_filters. |
5832 | | */ |
5833 | | /* Certificate extensions */ |
5834 | 0 | if ((*inOutIdx - begin) + OPAQUE16_LEN > size) |
5835 | 0 | return BUFFER_ERROR; |
5836 | 0 | ato16(input + *inOutIdx, &len); |
5837 | 0 | *inOutIdx += OPAQUE16_LEN; |
5838 | 0 | if ((*inOutIdx - begin) + len > size) |
5839 | 0 | return BUFFER_ERROR; |
5840 | 0 | if (len == 0) |
5841 | 0 | return INVALID_PARAMETER; |
5842 | 0 | if ((ret = TLSX_Parse(ssl, input + *inOutIdx, len, certificate_request, |
5843 | 0 | &peerSuites))) { |
5844 | 0 | return ret; |
5845 | 0 | } |
5846 | 0 | *inOutIdx += len; |
5847 | |
|
5848 | | #ifdef OPENSSL_EXTRA |
5849 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
5850 | | return ret; |
5851 | | #endif |
5852 | |
|
5853 | 0 | if ((ssl->buffers.certificate && ssl->buffers.certificate->buffer && |
5854 | 0 | ((ssl->buffers.key && ssl->buffers.key->buffer) |
5855 | | #ifdef HAVE_PK_CALLBACKS |
5856 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
5857 | | #endif |
5858 | 0 | )) |
5859 | | #ifdef OPENSSL_EXTRA |
5860 | | || ssl->ctx->certSetupCb != NULL |
5861 | | #endif |
5862 | 0 | ) { |
5863 | 0 | if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo, |
5864 | 0 | peerSuites.hashSigAlgoSz, 0) != 0) { |
5865 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5866 | 0 | return INVALID_PARAMETER; |
5867 | 0 | } |
5868 | 0 | ssl->options.sendVerify = SEND_CERT; |
5869 | 0 | } |
5870 | 0 | else { |
5871 | 0 | #ifndef WOLFSSL_NO_CLIENT_CERT_ERROR |
5872 | 0 | ssl->options.sendVerify = SEND_BLANK_CERT; |
5873 | | #else |
5874 | | WOLFSSL_MSG("Certificate required but none set on client"); |
5875 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
5876 | | WOLFSSL_ERROR_VERBOSE(NO_CERT_ERROR); |
5877 | | return NO_CERT_ERROR; |
5878 | | #endif |
5879 | 0 | } |
5880 | | |
5881 | | /* This message is always encrypted so add encryption padding. */ |
5882 | 0 | *inOutIdx += ssl->keys.padSz; |
5883 | |
|
5884 | 0 | WOLFSSL_LEAVE("DoTls13CertificateRequest", ret); |
5885 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5886 | |
|
5887 | 0 | return ret; |
5888 | 0 | } |
5889 | | #endif /* !NO_CERTS */ |
5890 | | #endif /* !NO_WOLFSSL_CLIENT */ |
5891 | | |
5892 | | #ifndef NO_WOLFSSL_SERVER |
5893 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5894 | | #ifndef NO_PSK |
5895 | | int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key, |
5896 | | word32* psk_keySz, const byte* suite, int* found, byte* foundSuite) |
5897 | | { |
5898 | | const char* cipherName = NULL; |
5899 | | byte cipherSuite0 = TLS13_BYTE; |
5900 | | byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
5901 | | int ret = 0; |
5902 | | |
5903 | | *found = 0; |
5904 | | (void)suite; |
5905 | | |
5906 | | if (ssl->options.server_psk_tls13_cb != NULL) { |
5907 | | *psk_keySz = ssl->options.server_psk_tls13_cb((WOLFSSL*)ssl, |
5908 | | (char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName); |
5909 | | if (*psk_keySz != 0) { |
5910 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
5911 | | *found = (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
5912 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0); |
5913 | | (void)cipherSuiteFlags; |
5914 | | } |
5915 | | } |
5916 | | if (*found == 0 && (ssl->options.server_psk_cb != NULL)) { |
5917 | | *psk_keySz = ssl->options.server_psk_cb((WOLFSSL*)ssl, |
5918 | | (char*)psk->identity, psk_key, |
5919 | | MAX_PSK_KEY_LEN); |
5920 | | *found = (*psk_keySz != 0); |
5921 | | } |
5922 | | if (*found) { |
5923 | | if (*psk_keySz > MAX_PSK_KEY_LEN && |
5924 | | *((int*)psk_keySz) != WC_NO_ERR_TRACE(USE_HW_PSK)) { |
5925 | | WOLFSSL_MSG("Key len too long in FindPsk()"); |
5926 | | ret = PSK_KEY_ERROR; |
5927 | | WOLFSSL_ERROR_VERBOSE(ret); |
5928 | | *found = 0; |
5929 | | } |
5930 | | if (ret == 0) { |
5931 | | #if !defined(WOLFSSL_PSK_ONE_ID) && !defined(WOLFSSL_PRIORITIZE_PSK) |
5932 | | /* Check whether PSK ciphersuite is in SSL. */ |
5933 | | *found = (suite[0] == cipherSuite0) && (suite[1] == cipherSuite); |
5934 | | #else |
5935 | | (void)suite; |
5936 | | /* Check whether PSK ciphersuite is in SSL. */ |
5937 | | { |
5938 | | byte s[2] = { |
5939 | | cipherSuite0, |
5940 | | cipherSuite, |
5941 | | }; |
5942 | | *found = FindSuiteSSL(ssl, s); |
5943 | | } |
5944 | | #endif |
5945 | | } |
5946 | | } |
5947 | | if (*found && foundSuite != NULL) { |
5948 | | foundSuite[0] = cipherSuite0; |
5949 | | foundSuite[1] = cipherSuite; |
5950 | | } |
5951 | | |
5952 | | return ret; |
5953 | | } |
5954 | | |
5955 | | /* Attempt to find the PSK (not session ticket) that matches. |
5956 | | * |
5957 | | * @param [in, out] ssl The SSL/TLS object. |
5958 | | * @param [in] psk A pre-shared key from the extension. |
5959 | | * @param [out] suite Cipher suite to use with PSK. |
5960 | | * @param [out] err Error code. |
5961 | | * PSK_KEY_ERROR when key is too big or ticket age is |
5962 | | * invalid, |
5963 | | * UNSUPPORTED_SUITE on invalid suite. |
5964 | | * Other error when attempting to derive early secret. |
5965 | | * @return 1 when a match found - but check error code. |
5966 | | * @return 0 when no match found. |
5967 | | */ |
5968 | | static int FindPsk(WOLFSSL* ssl, PreSharedKey* psk, const byte* suite, int* err) |
5969 | | { |
5970 | | int ret = 0; |
5971 | | int found = 0; |
5972 | | byte foundSuite[SUITE_LEN]; |
5973 | | |
5974 | | WOLFSSL_ENTER("FindPsk"); |
5975 | | |
5976 | | XMEMSET(foundSuite, 0, sizeof(foundSuite)); |
5977 | | |
5978 | | ret = FindPskSuite(ssl, psk, ssl->arrays->psk_key, &ssl->arrays->psk_keySz, |
5979 | | suite, &found, foundSuite); |
5980 | | if (ret == 0 && found) { |
5981 | | /* Default to ciphersuite if cb doesn't specify. */ |
5982 | | ssl->options.resuming = 0; |
5983 | | /* Don't send certificate request when using PSK. */ |
5984 | | ssl->options.verifyPeer = 0; |
5985 | | |
5986 | | /* PSK age is always zero. */ |
5987 | | if (psk->ticketAge != 0) { |
5988 | | ret = PSK_KEY_ERROR; |
5989 | | WOLFSSL_ERROR_VERBOSE(ret); |
5990 | | } |
5991 | | if (ret == 0) { |
5992 | | /* Set PSK ciphersuite into SSL. */ |
5993 | | ssl->options.cipherSuite0 = foundSuite[0]; |
5994 | | ssl->options.cipherSuite = foundSuite[1]; |
5995 | | ret = SetCipherSpecs(ssl); |
5996 | | } |
5997 | | if (ret == 0) { |
5998 | | /* Derive the early secret using the PSK. */ |
5999 | | ret = DeriveEarlySecret(ssl); |
6000 | | } |
6001 | | if (ret == 0) { |
6002 | | /* PSK negotiation has succeeded */ |
6003 | | ssl->options.isPSK = 1; |
6004 | | /* SERVER: using PSK for peer authentication. */ |
6005 | | ssl->options.peerAuthGood = 1; |
6006 | | } |
6007 | | } |
6008 | | |
6009 | | *err = ret; |
6010 | | WOLFSSL_LEAVE("FindPsk", found); |
6011 | | WOLFSSL_LEAVE("FindPsk", ret); |
6012 | | return found; |
6013 | | } |
6014 | | #endif /* !NO_PSK */ |
6015 | | |
6016 | | /* Handle any Pre-Shared Key (PSK) extension. |
6017 | | * Find a PSK that supports the cipher suite passed in. |
6018 | | * |
6019 | | * ssl SSL/TLS object. |
6020 | | * suite Cipher suite to find PSK for. |
6021 | | * usingPSK 1=Indicates handshake is using Pre-Shared Keys (2=Ephemeral) |
6022 | | * first Set to 1 if first in extension |
6023 | | * returns 0 on success and otherwise failure. |
6024 | | */ |
6025 | | static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz, |
6026 | | const byte* suite, int* usingPSK, int* first) |
6027 | | { |
6028 | | int ret = 0; |
6029 | | TLSX* ext; |
6030 | | PreSharedKey* current; |
6031 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
6032 | | byte binder[WC_MAX_DIGEST_SIZE]; |
6033 | | word32 binderLen; |
6034 | | |
6035 | | #ifdef NO_PSK |
6036 | | (void) suite; /* to avoid unused var warning when not used */ |
6037 | | #endif |
6038 | | |
6039 | | WOLFSSL_ENTER("DoPreSharedKeys"); |
6040 | | |
6041 | | (void)suite; |
6042 | | |
6043 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6044 | | if (ext == NULL) { |
6045 | | WOLFSSL_MSG("No pre shared extension keys found"); |
6046 | | return BAD_FUNC_ARG; |
6047 | | } |
6048 | | |
6049 | | /* Look through all client's pre-shared keys for a match. */ |
6050 | | for (current = (PreSharedKey*)ext->data; current != NULL; |
6051 | | current = current->next) { |
6052 | | #ifndef NO_PSK |
6053 | | if (current->identityLen > MAX_PSK_ID_LEN) { |
6054 | | return BUFFER_ERROR; |
6055 | | } |
6056 | | XMEMCPY(ssl->arrays->client_identity, current->identity, |
6057 | | current->identityLen); |
6058 | | ssl->arrays->client_identity[current->identityLen] = '\0'; |
6059 | | #endif |
6060 | | |
6061 | | #ifdef HAVE_SESSION_TICKET |
6062 | | /* Decode the identity. */ |
6063 | | switch (current->decryptRet) { |
6064 | | case PSK_DECRYPT_NONE: |
6065 | | ret = DoClientTicket_ex(ssl, current, 1); |
6066 | | /* psk->sess may be set. Need to clean up later. */ |
6067 | | break; |
6068 | | case PSK_DECRYPT_OK: |
6069 | | ret = WOLFSSL_TICKET_RET_OK; |
6070 | | break; |
6071 | | case PSK_DECRYPT_CREATE: |
6072 | | ret = WOLFSSL_TICKET_RET_CREATE; |
6073 | | break; |
6074 | | case PSK_DECRYPT_FAIL: |
6075 | | ret = WOLFSSL_TICKET_RET_REJECT; |
6076 | | break; |
6077 | | } |
6078 | | |
6079 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6080 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
6081 | | return ret; |
6082 | | #endif |
6083 | | |
6084 | | if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) { |
6085 | | current->sess_free_cb(ssl, current->sess, |
6086 | | ¤t->sess_free_cb_ctx); |
6087 | | current->sess = NULL; |
6088 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6089 | | sizeof(psk_sess_free_cb_ctx)); |
6090 | | } |
6091 | | if (ret == WOLFSSL_TICKET_RET_OK) { |
6092 | | ret = DoClientTicketCheck(ssl, current, ssl->timeout, suite); |
6093 | | if (ret == 0) |
6094 | | DoClientTicketFinalize(ssl, current->it, current->sess); |
6095 | | if (current->sess_free_cb != NULL) { |
6096 | | current->sess_free_cb(ssl, current->sess, |
6097 | | ¤t->sess_free_cb_ctx); |
6098 | | current->sess = NULL; |
6099 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6100 | | sizeof(psk_sess_free_cb_ctx)); |
6101 | | } |
6102 | | if (ret != 0) |
6103 | | continue; |
6104 | | |
6105 | | /* SERVER: using secret in session ticket for peer auth. */ |
6106 | | ssl->options.peerAuthGood = 1; |
6107 | | |
6108 | | #ifdef WOLFSSL_EARLY_DATA |
6109 | | ssl->options.maxEarlyDataSz = ssl->session->maxEarlyDataSz; |
6110 | | #endif |
6111 | | /* Use the same cipher suite as before and set up for use. */ |
6112 | | ssl->options.cipherSuite0 = ssl->session->cipherSuite0; |
6113 | | ssl->options.cipherSuite = ssl->session->cipherSuite; |
6114 | | ret = SetCipherSpecs(ssl); |
6115 | | if (ret != 0) |
6116 | | return ret; |
6117 | | |
6118 | | /* Resumption PSK is resumption master secret. */ |
6119 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
6120 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
6121 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
6122 | | return ret; |
6123 | | } |
6124 | | |
6125 | | /* Derive the early secret using the PSK. */ |
6126 | | ret = DeriveEarlySecret(ssl); |
6127 | | if (ret != 0) |
6128 | | return ret; |
6129 | | |
6130 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6131 | | ret = HashInput(ssl, input, (int)inputSz); |
6132 | | if (ret < 0) |
6133 | | return ret; |
6134 | | |
6135 | | /* Derive the binder key to use with HMAC. */ |
6136 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
6137 | | if (ret != 0) |
6138 | | return ret; |
6139 | | } |
6140 | | else |
6141 | | #endif /* HAVE_SESSION_TICKET */ |
6142 | | #ifndef NO_PSK |
6143 | | if (FindPsk(ssl, current, suite, &ret)) { |
6144 | | if (ret != 0) |
6145 | | return ret; |
6146 | | |
6147 | | ret = HashInput(ssl, input, (int)inputSz); |
6148 | | if (ret < 0) |
6149 | | return ret; |
6150 | | |
6151 | | /* Derive the binder key to use with HMAC. */ |
6152 | | ret = DeriveBinderKey(ssl, binderKey); |
6153 | | if (ret != 0) |
6154 | | return ret; |
6155 | | } |
6156 | | else |
6157 | | #endif |
6158 | | { |
6159 | | continue; |
6160 | | } |
6161 | | |
6162 | | ssl->options.sendVerify = 0; |
6163 | | |
6164 | | /* Derive the Finished message secret. */ |
6165 | | ret = DeriveFinishedSecret(ssl, binderKey, |
6166 | | ssl->keys.client_write_MAC_secret, |
6167 | | 0 /* neither end */); |
6168 | | if (ret != 0) |
6169 | | return ret; |
6170 | | |
6171 | | /* Derive the binder and compare with the one in the extension. */ |
6172 | | ret = BuildTls13HandshakeHmac(ssl, |
6173 | | ssl->keys.client_write_MAC_secret, binder, &binderLen); |
6174 | | if (ret != 0) |
6175 | | return ret; |
6176 | | if (binderLen != current->binderLen || |
6177 | | ConstantCompare(binder, current->binder, |
6178 | | binderLen) != 0) { |
6179 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6180 | | return BAD_BINDER; |
6181 | | } |
6182 | | |
6183 | | /* This PSK works, no need to try any more. */ |
6184 | | current->chosen = 1; |
6185 | | ext->resp = 1; |
6186 | | break; |
6187 | | } |
6188 | | |
6189 | | if (current == NULL) { |
6190 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6191 | | #ifndef NO_CERTS |
6192 | | if (ssl->buffers.certChainCnt != 0) |
6193 | | return 0; |
6194 | | #endif |
6195 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6196 | | return BAD_BINDER; |
6197 | | #else |
6198 | | return 0; |
6199 | | #endif |
6200 | | } |
6201 | | |
6202 | | *first = (current == ext->data); |
6203 | | *usingPSK = 1; |
6204 | | |
6205 | | WOLFSSL_LEAVE("DoPreSharedKeys", ret); |
6206 | | |
6207 | | return ret; |
6208 | | } |
6209 | | |
6210 | | /* Handle any Pre-Shared Key (PSK) extension. |
6211 | | * Must do this in ClientHello as it requires a hash of the truncated message. |
6212 | | * Don't know size of binders until Pre-Shared Key extension has been parsed. |
6213 | | * |
6214 | | * ssl SSL/TLS object. |
6215 | | * input ClientHello message. |
6216 | | * helloSz Size of the ClientHello message (including binders if present). |
6217 | | * clSuites Client's cipher suite list. |
6218 | | * usingPSK Indicates handshake is using Pre-Shared Keys. |
6219 | | */ |
6220 | | static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz, |
6221 | | Suites* clSuites, int* usingPSK) |
6222 | | { |
6223 | | int ret; |
6224 | | TLSX* ext; |
6225 | | word16 bindersLen; |
6226 | | int first = 0; |
6227 | | #ifndef WOLFSSL_PSK_ONE_ID |
6228 | | int i; |
6229 | | const Suites* suites; |
6230 | | #else |
6231 | | byte suite[2]; |
6232 | | #endif |
6233 | | |
6234 | | WOLFSSL_ENTER("CheckPreSharedKeys"); |
6235 | | |
6236 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6237 | | if (ext == NULL) { |
6238 | | #ifdef WOLFSSL_EARLY_DATA |
6239 | | ssl->earlyData = no_early_data; |
6240 | | #endif |
6241 | | if (usingPSK) |
6242 | | *usingPSK = 0; |
6243 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6244 | | ret = HashInput(ssl, input, (int)helloSz); |
6245 | | return ret; |
6246 | | } |
6247 | | |
6248 | | /* Extensions pushed on stack/list and PSK must be last. */ |
6249 | | if (ssl->extensions != ext) { |
6250 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6251 | | return PSK_KEY_ERROR; |
6252 | | } |
6253 | | |
6254 | | /* Assume we are going to resume with a pre-shared key. */ |
6255 | | ssl->options.resuming = 1; |
6256 | | |
6257 | | /* Find the pre-shared key extension and calculate hash of truncated |
6258 | | * ClientHello for binders. |
6259 | | */ |
6260 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
6261 | | client_hello, &bindersLen); |
6262 | | if (ret < 0) |
6263 | | return ret; |
6264 | | |
6265 | | /* Refine list for PSK processing. */ |
6266 | | sslRefineSuites(ssl, clSuites); |
6267 | | #ifndef WOLFSSL_PSK_ONE_ID |
6268 | | if (usingPSK == NULL) |
6269 | | return BAD_FUNC_ARG; |
6270 | | |
6271 | | /* set after refineSuites, to avoid taking a stale ptr to ctx->Suites */ |
6272 | | suites = WOLFSSL_SUITES(ssl); |
6273 | | /* Server list has only common suites from refining in server or client |
6274 | | * order. */ |
6275 | | for (i = 0; !(*usingPSK) && i < suites->suiteSz; i += 2) { |
6276 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, |
6277 | | suites->suites + i, usingPSK, &first); |
6278 | | if (ret != 0) { |
6279 | | #ifdef HAVE_SESSION_TICKET |
6280 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6281 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
6282 | | #endif |
6283 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6284 | | #endif |
6285 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6286 | | return ret; |
6287 | | } |
6288 | | } |
6289 | | #ifdef HAVE_SESSION_TICKET |
6290 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6291 | | #endif |
6292 | | #else |
6293 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, suite, usingPSK, |
6294 | | &first); |
6295 | | if (ret != 0) { |
6296 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6297 | | return ret; |
6298 | | } |
6299 | | #endif |
6300 | | |
6301 | | if (*usingPSK) { |
6302 | | /* While verifying the selected PSK, we updated the |
6303 | | * handshake hash up to the binder bytes in the PSK extensions. |
6304 | | * Continuing, we need the rest of the ClientHello hashed as well. |
6305 | | */ |
6306 | | ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen); |
6307 | | } |
6308 | | else { |
6309 | | /* No suitable PSK found, Hash the complete ClientHello, |
6310 | | * as caller expect it after we return */ |
6311 | | ret = HashInput(ssl, input, (int)helloSz); |
6312 | | } |
6313 | | if (ret != 0) |
6314 | | return ret; |
6315 | | |
6316 | | if (*usingPSK != 0) { |
6317 | | word32 modes; |
6318 | | #ifdef WOLFSSL_EARLY_DATA |
6319 | | TLSX* extEarlyData; |
6320 | | |
6321 | | extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6322 | | if (extEarlyData != NULL) { |
6323 | | /* Check if accepting early data and first PSK. */ |
6324 | | if (ssl->earlyData != no_early_data && first) { |
6325 | | extEarlyData->resp = 1; |
6326 | | |
6327 | | /* Derive early data decryption key. */ |
6328 | | ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, |
6329 | | 1); |
6330 | | if (ret != 0) |
6331 | | return ret; |
6332 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
6333 | | return ret; |
6334 | | |
6335 | | ssl->keys.encryptionOn = 1; |
6336 | | ssl->earlyData = process_early_data; |
6337 | | } |
6338 | | else |
6339 | | extEarlyData->resp = 0; |
6340 | | } |
6341 | | #endif |
6342 | | |
6343 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
6344 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
6345 | | if (ext == NULL) { |
6346 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6347 | | return MISSING_HANDSHAKE_DATA; |
6348 | | } |
6349 | | modes = ext->val; |
6350 | | |
6351 | | #ifdef HAVE_SUPPORTED_CURVES |
6352 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
6353 | | /* Use (EC)DHE for forward-security if possible. */ |
6354 | | if ((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe && |
6355 | | ext != NULL) { |
6356 | | /* Only use named group used in last session. */ |
6357 | | ssl->namedGroup = ssl->session->namedGroup; |
6358 | | |
6359 | | *usingPSK = 2; /* generate new ephemeral key */ |
6360 | | } |
6361 | | else if (ssl->options.onlyPskDheKe) { |
6362 | | return PSK_KEY_ERROR; |
6363 | | } |
6364 | | else |
6365 | | #endif |
6366 | | { |
6367 | | if ((modes & (1 << PSK_KE)) == 0) { |
6368 | | WOLFSSL_MSG("psk_ke mode does not allow key share"); |
6369 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6370 | | return PSK_KEY_ERROR; |
6371 | | } |
6372 | | ssl->options.noPskDheKe = 1; |
6373 | | ssl->arrays->preMasterSz = 0; |
6374 | | |
6375 | | *usingPSK = 1; |
6376 | | } |
6377 | | } |
6378 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6379 | | else { |
6380 | | #ifndef NO_CERTS |
6381 | | if (ssl->buffers.certChainCnt != 0) |
6382 | | return 0; |
6383 | | #endif |
6384 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6385 | | return BAD_BINDER; |
6386 | | } |
6387 | | #endif |
6388 | | |
6389 | | WOLFSSL_LEAVE("CheckPreSharedKeys", ret); |
6390 | | |
6391 | | return 0; |
6392 | | } |
6393 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
6394 | | |
6395 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
6396 | | /* Check that the Cookie data's integrity. |
6397 | | * |
6398 | | * ssl SSL/TLS object. |
6399 | | * cookie The cookie data - hash and MAC. |
6400 | | * cookieSz The length of the cookie data in bytes. |
6401 | | * returns Length of the hash on success, otherwise failure. |
6402 | | */ |
6403 | | int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz) |
6404 | | { |
6405 | | int ret; |
6406 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
6407 | | Hmac cookieHmac; |
6408 | | byte cookieType = 0; |
6409 | | byte macSz = 0; |
6410 | | |
6411 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
6412 | | ssl->buffers.tls13CookieSecret.length == 0) { |
6413 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
6414 | | return COOKIE_ERROR; |
6415 | | } |
6416 | | |
6417 | | #ifndef NO_SHA256 |
6418 | | cookieType = WC_SHA256; |
6419 | | macSz = WC_SHA256_DIGEST_SIZE; |
6420 | | #elif defined(WOLFSSL_SHA384) |
6421 | | cookieType = WC_SHA384; |
6422 | | macSz = WC_SHA384_DIGEST_SIZE; |
6423 | | #elif defined(WOLFSSL_TLS13_SHA512) |
6424 | | cookieType = WC_SHA512; |
6425 | | macSz = WC_SHA512_DIGEST_SIZE; |
6426 | | #elif defined(WOLFSSL_SM3) |
6427 | | cookieType = WC_SM3; |
6428 | | macSz = WC_SM3_DIGEST_SIZE; |
6429 | | #else |
6430 | | #error "No digest to available to use with HMAC for cookies." |
6431 | | #endif /* NO_SHA */ |
6432 | | |
6433 | | if (cookieSz < ssl->specs.hash_size + macSz) |
6434 | | return HRR_COOKIE_ERROR; |
6435 | | cookieSz -= macSz; |
6436 | | |
6437 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
6438 | | if (ret == 0) { |
6439 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
6440 | | ssl->buffers.tls13CookieSecret.buffer, |
6441 | | ssl->buffers.tls13CookieSecret.length); |
6442 | | } |
6443 | | if (ret == 0) |
6444 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
6445 | | #ifdef WOLFSSL_DTLS13 |
6446 | | /* Tie cookie to peer address */ |
6447 | | if (ret == 0) { |
6448 | | /* peerLock not necessary. Still in handshake phase. */ |
6449 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
6450 | | ret = wc_HmacUpdate(&cookieHmac, |
6451 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
6452 | | ssl->buffers.dtlsCtx.peer.sz); |
6453 | | } |
6454 | | } |
6455 | | #endif |
6456 | | if (ret == 0) |
6457 | | ret = wc_HmacFinal(&cookieHmac, mac); |
6458 | | |
6459 | | wc_HmacFree(&cookieHmac); |
6460 | | if (ret != 0) |
6461 | | return ret; |
6462 | | |
6463 | | if (ConstantCompare(cookie + cookieSz, mac, macSz) != 0) { |
6464 | | WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR); |
6465 | | return HRR_COOKIE_ERROR; |
6466 | | } |
6467 | | return cookieSz; |
6468 | | } |
6469 | | |
6470 | | /* Length of the KeyShare Extension */ |
6471 | | #define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6472 | | /* Length of the Supported Versions Extension */ |
6473 | | #define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6474 | | /* Length of the Cookie Extension excluding cookie data */ |
6475 | | #define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6476 | | /* PV | Random | Session Id | CipherSuite | Compression | Ext Len */ |
6477 | | #define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \ |
6478 | | SUITE_LEN + COMP_LEN + OPAQUE16_LEN) |
6479 | | /* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */ |
6480 | | #define MAX_HRR_SZ (HRR_MAX_HS_HEADER_SZ + \ |
6481 | | HRR_BODY_SZ + \ |
6482 | | HRR_KEY_SHARE_SZ + \ |
6483 | | HRR_VERSIONS_SZ + \ |
6484 | | HRR_COOKIE_HDR_SZ) |
6485 | | |
6486 | | |
6487 | | /* Restart the handshake hash from the cookie value. |
6488 | | * |
6489 | | * ssl SSL/TLS object. |
6490 | | * cookie Cookie data from client. |
6491 | | * returns 0 on success, otherwise failure. |
6492 | | */ |
6493 | | static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie) |
6494 | | { |
6495 | | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
6496 | | byte hrr[MAX_HRR_SZ] = {0}; |
6497 | | int hrrIdx; |
6498 | | word32 idx; |
6499 | | byte hashSz; |
6500 | | byte* cookieData; |
6501 | | word16 cookieDataSz; |
6502 | | word16 length; |
6503 | | int keyShareExt = 0; |
6504 | | int ret; |
6505 | | |
6506 | | ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len); |
6507 | | if (ret < 0) |
6508 | | return ret; |
6509 | | cookieDataSz = (word16)ret; |
6510 | | hashSz = cookie->data[0]; |
6511 | | cookieData = cookie->data; |
6512 | | idx = OPAQUE8_LEN; |
6513 | | |
6514 | | /* Restart handshake hash with synthetic message hash. */ |
6515 | | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
6516 | | |
6517 | | if ((ret = InitHandshakeHashes(ssl)) != 0) |
6518 | | return ret; |
6519 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
6520 | | return ret; |
6521 | | #ifdef WOLFSSL_DEBUG_TLS |
6522 | | WOLFSSL_MSG("Restart Hash from Cookie"); |
6523 | | WOLFSSL_BUFFER(cookieData + idx, hashSz); |
6524 | | #endif |
6525 | | if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0) |
6526 | | return ret; |
6527 | | |
6528 | | /* Reconstruct the HelloRetryMessage for handshake hash. */ |
6529 | | length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz + |
6530 | | HRR_COOKIE_HDR_SZ + cookie->len; |
6531 | | length += HRR_VERSIONS_SZ; |
6532 | | /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */ |
6533 | | if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) { |
6534 | | keyShareExt = 1; |
6535 | | length += HRR_KEY_SHARE_SZ; |
6536 | | } |
6537 | | |
6538 | | AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl); |
6539 | | |
6540 | | idx += hashSz; |
6541 | | hrrIdx = HANDSHAKE_HEADER_SZ; |
6542 | | |
6543 | | #ifdef WOLFSSL_DTLS13 |
6544 | | if (ssl->options.dtls) |
6545 | | hrrIdx += DTLS_HANDSHAKE_EXTRA; |
6546 | | #endif /* WOLFSSL_DTLS13 */ |
6547 | | |
6548 | | /* The negotiated protocol version. */ |
6549 | | hrr[hrrIdx++] = ssl->version.major; |
6550 | | hrr[hrrIdx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
6551 | | |
6552 | | /* HelloRetryRequest message has fixed value for random. */ |
6553 | | XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN); |
6554 | | hrrIdx += RAN_LEN; |
6555 | | |
6556 | | hrr[hrrIdx++] = ssl->session->sessionIDSz; |
6557 | | if (ssl->session->sessionIDSz > 0) { |
6558 | | XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz); |
6559 | | hrrIdx += ssl->session->sessionIDSz; |
6560 | | } |
6561 | | |
6562 | | /* Cipher Suite */ |
6563 | | hrr[hrrIdx++] = cookieData[idx++]; |
6564 | | hrr[hrrIdx++] = cookieData[idx++]; |
6565 | | |
6566 | | /* Compression not supported in TLS v1.3. */ |
6567 | | hrr[hrrIdx++] = 0; |
6568 | | |
6569 | | /* Extensions' length */ |
6570 | | length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz; |
6571 | | c16toa(length, hrr + hrrIdx); |
6572 | | hrrIdx += 2; |
6573 | | |
6574 | | /* Optional KeyShare Extension */ |
6575 | | if (keyShareExt) { |
6576 | | c16toa(TLSX_KEY_SHARE, hrr + hrrIdx); |
6577 | | hrrIdx += 2; |
6578 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6579 | | hrrIdx += 2; |
6580 | | hrr[hrrIdx++] = cookieData[idx++]; |
6581 | | hrr[hrrIdx++] = cookieData[idx++]; |
6582 | | } |
6583 | | c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx); |
6584 | | hrrIdx += 2; |
6585 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6586 | | hrrIdx += 2; |
6587 | | #ifdef WOLFSSL_TLS13_DRAFT |
6588 | | hrr[hrrIdx++] = TLS_DRAFT_MAJOR; |
6589 | | hrr[hrrIdx++] = TLS_DRAFT_MINOR; |
6590 | | #else |
6591 | | hrr[hrrIdx++] = ssl->version.major; |
6592 | | hrr[hrrIdx++] = ssl->version.minor; |
6593 | | #endif |
6594 | | |
6595 | | /* Mandatory Cookie Extension */ |
6596 | | c16toa(TLSX_COOKIE, hrr + hrrIdx); |
6597 | | hrrIdx += 2; |
6598 | | c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx); |
6599 | | hrrIdx += 2; |
6600 | | c16toa(cookie->len, hrr + hrrIdx); |
6601 | | hrrIdx += 2; |
6602 | | |
6603 | | #ifdef WOLFSSL_DEBUG_TLS |
6604 | | WOLFSSL_MSG("Reconstructed HelloRetryRequest"); |
6605 | | WOLFSSL_BUFFER(hrr, hrrIdx); |
6606 | | WOLFSSL_MSG("Cookie"); |
6607 | | WOLFSSL_BUFFER(cookieData, cookie->len); |
6608 | | #endif |
6609 | | |
6610 | | #ifdef WOLFSSL_DTLS13 |
6611 | | if (ssl->options.dtls) { |
6612 | | ret = Dtls13HashHandshake(ssl, hrr, (word16)hrrIdx); |
6613 | | } |
6614 | | else |
6615 | | #endif /* WOLFSSL_DTLS13 */ |
6616 | | { |
6617 | | ret = HashRaw(ssl, hrr, hrrIdx); |
6618 | | } |
6619 | | |
6620 | | if (ret != 0) |
6621 | | return ret; |
6622 | | |
6623 | | return HashRaw(ssl, cookieData, cookie->len); |
6624 | | } |
6625 | | #endif |
6626 | | |
6627 | | /* Do SupportedVersion extension for TLS v1.3+ otherwise it is not. |
6628 | | * |
6629 | | * ssl The SSL/TLS object. |
6630 | | * input The message buffer. |
6631 | | * i The index into the message buffer of ClientHello. |
6632 | | * helloSz The length of the current handshake message. |
6633 | | * returns 0 on success and otherwise failure. |
6634 | | */ |
6635 | | static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i, |
6636 | | word32 helloSz, int* wantDowngrade) |
6637 | | { |
6638 | | int ret; |
6639 | | byte b; |
6640 | | word16 suiteSz; |
6641 | | word16 totalExtSz; |
6642 | | int foundVersion = 0; |
6643 | | |
6644 | | /* Client random */ |
6645 | | i += RAN_LEN; |
6646 | | /* Session id - not used in TLS v1.3 */ |
6647 | | b = input[i++]; |
6648 | | if (i + b > helloSz) { |
6649 | | return BUFFER_ERROR; |
6650 | | } |
6651 | | i += b; |
6652 | | #ifdef WOLFSSL_DTLS13 |
6653 | | if (ssl->options.dtls) { |
6654 | | /* legacy_cookie - not used in DTLS v1.3 */ |
6655 | | b = input[i++]; |
6656 | | if (i + b > helloSz) { |
6657 | | return BUFFER_ERROR; |
6658 | | } |
6659 | | i += b; |
6660 | | } |
6661 | | #endif /* WOLFSSL_DTLS13 */ |
6662 | | /* Cipher suites */ |
6663 | | if (i + OPAQUE16_LEN > helloSz) |
6664 | | return BUFFER_ERROR; |
6665 | | ato16(input + i, &suiteSz); |
6666 | | i += OPAQUE16_LEN; |
6667 | | if (i + suiteSz + 1 > helloSz) |
6668 | | return BUFFER_ERROR; |
6669 | | i += suiteSz; |
6670 | | /* Compression */ |
6671 | | b = input[i++]; |
6672 | | if (i + b > helloSz) |
6673 | | return BUFFER_ERROR; |
6674 | | i += b; |
6675 | | |
6676 | | /* TLS 1.3 must have extensions */ |
6677 | | if (i < helloSz) { |
6678 | | if (i + OPAQUE16_LEN > helloSz) |
6679 | | return BUFFER_ERROR; |
6680 | | ato16(&input[i], &totalExtSz); |
6681 | | i += OPAQUE16_LEN; |
6682 | | if (totalExtSz != helloSz - i) |
6683 | | return BUFFER_ERROR; |
6684 | | |
6685 | | /* Need to negotiate version first. */ |
6686 | | if ((ret = TLSX_ParseVersion(ssl, input + i, totalExtSz, client_hello, |
6687 | | &foundVersion))) { |
6688 | | return ret; |
6689 | | } |
6690 | | } |
6691 | | *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version); |
6692 | | |
6693 | | return 0; |
6694 | | } |
6695 | | |
6696 | | /* Handle a ClientHello handshake message. |
6697 | | * If the protocol version in the message is not TLS v1.3 or higher, use |
6698 | | * DoClientHello() |
6699 | | * Only a server will receive this message. |
6700 | | * |
6701 | | * ssl The SSL/TLS object. |
6702 | | * input The message buffer. |
6703 | | * inOutIdx On entry, the index into the message buffer of ClientHello. |
6704 | | * On exit, the index of byte after the ClientHello message and |
6705 | | * padding. |
6706 | | * helloSz The length of the current handshake message. |
6707 | | * returns 0 on success and otherwise failure. |
6708 | | */ |
6709 | | |
6710 | | typedef struct Dch13Args { |
6711 | | ProtocolVersion pv; |
6712 | | word32 idx; |
6713 | | word32 begin; |
6714 | | int usingPSK; |
6715 | | } Dch13Args; |
6716 | | |
6717 | | static void FreeDch13Args(WOLFSSL* ssl, void* pArgs) |
6718 | | { |
6719 | | /* openssl compat builds hang on to the client suites until WOLFSSL object |
6720 | | * is destroyed */ |
6721 | | #ifndef OPENSSL_EXTRA |
6722 | | if (ssl->clSuites) { |
6723 | | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
6724 | | ssl->clSuites = NULL; |
6725 | | } |
6726 | | #endif |
6727 | | (void)ssl; |
6728 | | (void)pArgs; |
6729 | | |
6730 | | } |
6731 | | |
6732 | | int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
6733 | | word32 helloSz) |
6734 | 0 | { |
6735 | 0 | int ret; |
6736 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6737 | | Dch13Args* args = NULL; |
6738 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
6739 | | #else |
6740 | 0 | Dch13Args args[1]; |
6741 | 0 | #endif |
6742 | | #if defined(HAVE_ECH) |
6743 | | TLSX* echX = NULL; |
6744 | | HS_Hashes* tmpHashes; |
6745 | | #endif |
6746 | |
|
6747 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO); |
6748 | 0 | WOLFSSL_ENTER("DoTls13ClientHello"); |
6749 | |
|
6750 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6751 | | if (ssl->async == NULL) { |
6752 | | ssl->async = (struct WOLFSSL_ASYNC*) |
6753 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
6754 | | DYNAMIC_TYPE_ASYNC); |
6755 | | if (ssl->async == NULL) |
6756 | | ERROR_OUT(MEMORY_E, exit_dch); |
6757 | | } |
6758 | | args = (Dch13Args*)ssl->async->args; |
6759 | | |
6760 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
6761 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
6762 | | /* Check for error */ |
6763 | | if (ret < 0) { |
6764 | | goto exit_dch; |
6765 | | } |
6766 | | } |
6767 | | else |
6768 | | #endif |
6769 | 0 | { |
6770 | | /* Reset state */ |
6771 | 0 | ret = VERSION_ERROR; |
6772 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
6773 | 0 | XMEMSET(args, 0, sizeof(Dch13Args)); |
6774 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6775 | | ssl->async->freeArgs = FreeDch13Args; |
6776 | | #endif |
6777 | 0 | } |
6778 | |
|
6779 | 0 | switch (ssl->options.asyncState) { |
6780 | 0 | case TLS_ASYNC_BEGIN: |
6781 | 0 | { |
6782 | 0 | byte b; |
6783 | 0 | byte sessIdSz; |
6784 | 0 | int wantDowngrade = 0; |
6785 | 0 | word16 totalExtSz = 0; |
6786 | |
|
6787 | | #ifdef WOLFSSL_CALLBACKS |
6788 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
6789 | | if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo); |
6790 | | #endif |
6791 | | |
6792 | | /* do not change state in the SSL object before the next region of code |
6793 | | * to be able to statelessly compute a DTLS cookie */ |
6794 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
6795 | | /* Update the ssl->options.dtlsStateful setting `if` statement in |
6796 | | * wolfSSL_accept_TLSv13 when changing this one. */ |
6797 | | if (IsDtlsNotSctpMode(ssl) && ssl->options.sendCookie && |
6798 | | !ssl->options.dtlsStateful) { |
6799 | | DtlsSetSeqNumForReply(ssl); |
6800 | | ret = DoClientHelloStateless(ssl, input + *inOutIdx, helloSz, 0, NULL); |
6801 | | if (ret != 0 || !ssl->options.dtlsStateful) { |
6802 | | *inOutIdx += helloSz; |
6803 | | goto exit_dch; |
6804 | | } |
6805 | | if (ssl->chGoodCb != NULL) { |
6806 | | int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
6807 | | if (cbret < 0) { |
6808 | | ssl->error = cbret; |
6809 | | WOLFSSL_MSG("ClientHello Good Cb don't continue error"); |
6810 | | return WOLFSSL_FATAL_ERROR; |
6811 | | } |
6812 | | } |
6813 | | } |
6814 | | ssl->options.dtlsStateful = 1; |
6815 | | #endif /* WOLFSSL_DTLS */ |
6816 | |
|
6817 | 0 | args->idx = *inOutIdx; |
6818 | 0 | args->begin = args->idx; |
6819 | | |
6820 | | /* protocol version, random and session id length check */ |
6821 | 0 | if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz) { |
6822 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6823 | 0 | } |
6824 | | |
6825 | | /* Protocol version */ |
6826 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
6827 | 0 | ssl->chVersion = args->pv; /* store */ |
6828 | 0 | args->idx += OPAQUE16_LEN; |
6829 | | |
6830 | | |
6831 | | /* this check pass for DTLS Major (0xff) */ |
6832 | 0 | if (args->pv.major < SSLv3_MAJOR) { |
6833 | 0 | WOLFSSL_MSG("Legacy version field contains unsupported value"); |
6834 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6835 | 0 | } |
6836 | | |
6837 | | #ifdef WOLFSSL_DTLS13 |
6838 | | if (ssl->options.dtls && |
6839 | | args->pv.major == DTLS_MAJOR && args->pv.minor > DTLSv1_2_MINOR) { |
6840 | | wantDowngrade = 1; |
6841 | | ssl->version.minor = args->pv.minor; |
6842 | | } |
6843 | | #endif /* WOLFSSL_DTLS13 */ |
6844 | | |
6845 | 0 | if (!ssl->options.dtls) { |
6846 | 0 | #ifndef WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION |
6847 | | /* Check for TLS 1.3 version (0x0304) in legacy version field. RFC 8446 |
6848 | | * Section 4.2.1 allows this action: |
6849 | | * |
6850 | | * "Servers MAY abort the handshake upon receiving a ClientHello with |
6851 | | * legacy_version 0x0304 or later." |
6852 | | * |
6853 | | * Note that if WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION is defined then the |
6854 | | * semantics of RFC 5246 Appendix E will be followed. A ServerHello with |
6855 | | * version 1.2 will be sent. The same is true if TLS 1.3 is not enabled. |
6856 | | */ |
6857 | 0 | if (args->pv.major == SSLv3_MAJOR && args->pv.minor >= TLSv1_3_MINOR) { |
6858 | 0 | WOLFSSL_MSG("Legacy version field is TLS 1.3 or later. Aborting."); |
6859 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6860 | 0 | } |
6861 | 0 | #endif /* WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION */ |
6862 | | |
6863 | | /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */ |
6864 | 0 | if (args->pv.major > SSLv3_MAJOR || (args->pv.major == SSLv3_MAJOR && |
6865 | 0 | args->pv.minor >= TLSv1_3_MINOR)) { |
6866 | 0 | args->pv.major = SSLv3_MAJOR; |
6867 | 0 | args->pv.minor = TLSv1_2_MINOR; |
6868 | 0 | wantDowngrade = 1; |
6869 | 0 | ssl->version.minor = args->pv.minor; |
6870 | 0 | } |
6871 | | /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */ |
6872 | 0 | else if (args->pv.major == SSLv3_MAJOR && |
6873 | 0 | args->pv.minor < TLSv1_2_MINOR) { |
6874 | 0 | wantDowngrade = 1; |
6875 | 0 | ssl->version.minor = args->pv.minor; |
6876 | 0 | } |
6877 | 0 | } |
6878 | | |
6879 | 0 | if (!wantDowngrade) { |
6880 | 0 | ret = DoTls13SupportedVersions(ssl, input + args->begin, |
6881 | 0 | args->idx - args->begin, helloSz, &wantDowngrade); |
6882 | 0 | if (ret < 0) |
6883 | 0 | goto exit_dch; |
6884 | 0 | } |
6885 | | |
6886 | 0 | if (wantDowngrade) { |
6887 | 0 | #ifndef WOLFSSL_NO_TLS12 |
6888 | 0 | byte realMinor; |
6889 | 0 | if (!ssl->options.downgrade) { |
6890 | 0 | WOLFSSL_MSG("Client trying to connect with lesser version than " |
6891 | 0 | "TLS v1.3"); |
6892 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6893 | 0 | } |
6894 | | |
6895 | 0 | if ((!ssl->options.dtls |
6896 | 0 | && args->pv.minor < ssl->options.minDowngrade) || |
6897 | 0 | (ssl->options.dtls && args->pv.minor > ssl->options.minDowngrade)) { |
6898 | 0 | WOLFSSL_MSG("\tversion below minimum allowed, fatal error"); |
6899 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6900 | 0 | } |
6901 | | |
6902 | 0 | realMinor = ssl->version.minor; |
6903 | 0 | ssl->version.minor = args->pv.minor; |
6904 | 0 | ret = HashInput(ssl, input + args->begin, (int)helloSz); |
6905 | 0 | ssl->version.minor = realMinor; |
6906 | 0 | if (ret == 0) { |
6907 | 0 | ret = DoClientHello(ssl, input, inOutIdx, helloSz); |
6908 | 0 | } |
6909 | 0 | goto exit_dch; |
6910 | | #else |
6911 | | WOLFSSL_MSG("Client trying to connect with lesser version than " |
6912 | | "TLS v1.3"); |
6913 | | ERROR_OUT(VERSION_ERROR, exit_dch); |
6914 | | #endif |
6915 | 0 | } |
6916 | | |
6917 | | /* From here on we are a TLS 1.3 ClientHello. */ |
6918 | | |
6919 | | /* Client random */ |
6920 | 0 | XMEMCPY(ssl->arrays->clientRandom, input + args->idx, RAN_LEN); |
6921 | 0 | args->idx += RAN_LEN; |
6922 | |
|
6923 | | #ifdef WOLFSSL_DEBUG_TLS |
6924 | | WOLFSSL_MSG("client random"); |
6925 | | WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN); |
6926 | | #endif |
6927 | |
|
6928 | 0 | sessIdSz = input[args->idx++]; |
6929 | 0 | #ifndef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
6930 | 0 | if (sessIdSz > ID_LEN) |
6931 | | #else |
6932 | | if (sessIdSz != ID_LEN && sessIdSz != 0) |
6933 | | #endif |
6934 | 0 | { |
6935 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6936 | 0 | } |
6937 | | |
6938 | 0 | if (sessIdSz + args->idx > helloSz) |
6939 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6940 | |
|
6941 | 0 | ssl->session->sessionIDSz = sessIdSz; |
6942 | 0 | if (sessIdSz > 0) |
6943 | 0 | XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz); |
6944 | 0 | args->idx += sessIdSz; |
6945 | |
|
6946 | | #ifdef WOLFSSL_DTLS13 |
6947 | | /* legacy_cookie */ |
6948 | | if (ssl->options.dtls) { |
6949 | | /* https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 */ |
6950 | | byte cookieLen = input[args->idx++]; |
6951 | | if (cookieLen != 0) { |
6952 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6953 | | } |
6954 | | } |
6955 | | #endif /* WOLFSSL_DTLS13 */ |
6956 | |
|
6957 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
6958 | 0 | ssl->clSuites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap, |
6959 | 0 | DYNAMIC_TYPE_SUITES); |
6960 | 0 | if (ssl->clSuites == NULL) { |
6961 | 0 | ERROR_OUT(MEMORY_E, exit_dch); |
6962 | 0 | } |
6963 | | |
6964 | | /* Cipher suites */ |
6965 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
6966 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6967 | 0 | ato16(&input[args->idx], &ssl->clSuites->suiteSz); |
6968 | 0 | args->idx += OPAQUE16_LEN; |
6969 | 0 | if ((ssl->clSuites->suiteSz % 2) != 0) { |
6970 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6971 | 0 | } |
6972 | | /* suites and compression length check */ |
6973 | 0 | if ((args->idx - args->begin) + ssl->clSuites->suiteSz + OPAQUE8_LEN > |
6974 | 0 | helloSz) { |
6975 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6976 | 0 | } |
6977 | 0 | if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) |
6978 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6979 | 0 | XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); |
6980 | 0 | args->idx += ssl->clSuites->suiteSz; |
6981 | 0 | ssl->clSuites->hashSigAlgoSz = 0; |
6982 | | |
6983 | | /* Compression */ |
6984 | 0 | b = input[args->idx++]; |
6985 | 0 | if ((args->idx - args->begin) + b > helloSz) |
6986 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6987 | 0 | if (b != COMP_LEN) { |
6988 | 0 | WOLFSSL_MSG("Must be one compression type in list"); |
6989 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6990 | 0 | } |
6991 | 0 | b = input[args->idx++]; |
6992 | 0 | if (b != NO_COMPRESSION) { |
6993 | 0 | WOLFSSL_MSG("Must be no compression type in list"); |
6994 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6995 | 0 | } |
6996 | | |
6997 | | /* Extensions */ |
6998 | 0 | if ((args->idx - args->begin) == helloSz) |
6999 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7000 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7001 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7002 | |
|
7003 | 0 | ato16(&input[args->idx], &totalExtSz); |
7004 | 0 | args->idx += OPAQUE16_LEN; |
7005 | 0 | if ((args->idx - args->begin) + totalExtSz > helloSz) |
7006 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7007 | | |
7008 | | /* Auto populate extensions supported unless user defined. */ |
7009 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0) |
7010 | 0 | goto exit_dch; |
7011 | | |
7012 | | #if defined(HAVE_ECH) |
7013 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7014 | | /* save the start of the buffer so we can use it when parsing ech */ |
7015 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7016 | | |
7017 | | if (echX == NULL) |
7018 | | ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch); |
7019 | | |
7020 | | ((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ; |
7021 | | ((WOLFSSL_ECH*)echX->data)->aadLen = helloSz; |
7022 | | } |
7023 | | #endif |
7024 | | |
7025 | | /* Parse extensions */ |
7026 | 0 | if ((ret = TLSX_Parse(ssl, input + args->idx, totalExtSz, client_hello, |
7027 | 0 | ssl->clSuites))) { |
7028 | 0 | goto exit_dch; |
7029 | 0 | } |
7030 | | |
7031 | | #if defined(HAVE_ECH) |
7032 | | /* jump to the end to clean things up */ |
7033 | | if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) |
7034 | | goto exit_dch; |
7035 | | #endif |
7036 | | |
7037 | 0 | #ifdef HAVE_SNI |
7038 | 0 | if ((ret = SNI_Callback(ssl)) != 0) |
7039 | 0 | goto exit_dch; |
7040 | 0 | ssl->options.side = WOLFSSL_SERVER_END; |
7041 | 0 | #endif |
7042 | |
|
7043 | 0 | args->idx += totalExtSz; |
7044 | 0 | ssl->options.haveSessionId = 1; |
7045 | 0 | ssl->options.sendVerify = SEND_CERT; |
7046 | |
|
7047 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7048 | | ssl->options.cookieGood = 0; |
7049 | | if (ssl->options.sendCookie && |
7050 | | (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE |
7051 | | #ifdef WOLFSSL_DTLS13 |
7052 | | /* Always check for a valid cookie since we may have already |
7053 | | * sent a HRR but we reset the state. */ |
7054 | | || ssl->options.dtls |
7055 | | #endif |
7056 | | )) { |
7057 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_COOKIE); |
7058 | | |
7059 | | if (ext != NULL) { |
7060 | | /* Ensure the cookie came from client and isn't the one in the |
7061 | | * response - HelloRetryRequest. |
7062 | | */ |
7063 | | if (ext->resp == 0) { |
7064 | | ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data); |
7065 | | if (ret != 0) |
7066 | | goto exit_dch; |
7067 | | /* Don't change state here as we may want to enter |
7068 | | * DoTls13ClientHello again. */ |
7069 | | ssl->options.cookieGood = 1; |
7070 | | } |
7071 | | else { |
7072 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7073 | | } |
7074 | | } |
7075 | | else { |
7076 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME) |
7077 | | /* Don't error out as we may be resuming. We confirm this later. */ |
7078 | | if (!ssl->options.dtls) |
7079 | | #endif |
7080 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7081 | | } |
7082 | | } |
7083 | | #endif |
7084 | |
|
7085 | | #if defined(HAVE_ECH) |
7086 | | /* hash clientHelloInner to hsHashesEch independently since it can't include |
7087 | | * the HRR */ |
7088 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7089 | | tmpHashes = ssl->hsHashes; |
7090 | | ssl->hsHashes = NULL; |
7091 | | ret = InitHandshakeHashes(ssl); |
7092 | | if (ret != 0) |
7093 | | goto exit_dch; |
7094 | | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7095 | | goto exit_dch; |
7096 | | ssl->hsHashesEch = ssl->hsHashes; |
7097 | | ssl->hsHashes = tmpHashes; |
7098 | | } |
7099 | | #endif |
7100 | |
|
7101 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7102 | | defined(HAVE_TLS_EXTENSIONS) |
7103 | | ret = CheckPreSharedKeys(ssl, input + args->begin, helloSz, ssl->clSuites, |
7104 | | &args->usingPSK); |
7105 | | if (ret != 0) |
7106 | | goto exit_dch; |
7107 | | #else |
7108 | 0 | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7109 | 0 | goto exit_dch; |
7110 | 0 | #endif |
7111 | | |
7112 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7113 | | defined(HAVE_TLS_EXTENSIONS) |
7114 | | if (!args->usingPSK) |
7115 | | #endif |
7116 | 0 | { |
7117 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7118 | | /* Not using PSK so don't require no KE. */ |
7119 | | ssl->options.noPskDheKe = 0; |
7120 | | #endif |
7121 | |
|
7122 | 0 | #ifndef NO_CERTS |
7123 | 0 | if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) { |
7124 | 0 | WOLFSSL_MSG("Client did not send a KeyShare extension"); |
7125 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7126 | 0 | } |
7127 | | /* Can't check ssl->extensions here as SigAlgs are unconditionally |
7128 | | set by TLSX_PopulateExtensions */ |
7129 | 0 | if (ssl->clSuites->hashSigAlgoSz == 0) { |
7130 | 0 | WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension"); |
7131 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7132 | 0 | } |
7133 | | #else |
7134 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7135 | | #endif |
7136 | 0 | } |
7137 | |
|
7138 | | #ifdef HAVE_ALPN |
7139 | | /* With PSK and all other things validated, it's time to |
7140 | | * select the ALPN protocol, if so requested */ |
7141 | | if ((ret = ALPN_Select(ssl)) != 0) |
7142 | | goto exit_dch; |
7143 | | #endif |
7144 | 0 | } /* case TLS_ASYNC_BEGIN */ |
7145 | 0 | FALL_THROUGH; |
7146 | |
|
7147 | 0 | case TLS_ASYNC_BUILD: |
7148 | | /* Advance state and proceed */ |
7149 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
7150 | 0 | FALL_THROUGH; |
7151 | |
|
7152 | 0 | case TLS_ASYNC_DO: |
7153 | 0 | { |
7154 | | #ifdef WOLFSSL_CERT_SETUP_CB |
7155 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
7156 | | goto exit_dch; |
7157 | | #endif |
7158 | 0 | #ifndef NO_CERTS |
7159 | 0 | if (!args->usingPSK) { |
7160 | 0 | if ((ret = MatchSuite(ssl, ssl->clSuites)) < 0) { |
7161 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7162 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7163 | | #endif |
7164 | 0 | WOLFSSL_MSG("Unsupported cipher suite, ClientHello 1.3"); |
7165 | 0 | goto exit_dch; |
7166 | 0 | } |
7167 | 0 | } |
7168 | 0 | #endif |
7169 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7170 | 0 | if (args->usingPSK == 2) { |
7171 | | /* Pick key share and Generate a new key if not present. */ |
7172 | 0 | int doHelloRetry = 0; |
7173 | 0 | ret = TLSX_KeyShare_Establish(ssl, &doHelloRetry); |
7174 | 0 | if (doHelloRetry) { |
7175 | | /* Make sure we don't send HRR twice */ |
7176 | 0 | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7177 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7178 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7179 | 0 | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7180 | 0 | ret = 0; /* for hello_retry return 0 */ |
7181 | 0 | } |
7182 | 0 | if (ret != 0) |
7183 | 0 | goto exit_dch; |
7184 | 0 | } |
7185 | 0 | #endif |
7186 | | |
7187 | | /* Advance state and proceed */ |
7188 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
7189 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
7190 | 0 | FALL_THROUGH; |
7191 | |
|
7192 | 0 | case TLS_ASYNC_VERIFY: |
7193 | 0 | { |
7194 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SUPPORTED_CURVES) |
7195 | | /* Check if the KeyShare calculations from the previous state are complete. |
7196 | | * wolfSSL_AsyncPop advances ssl->options.asyncState so we may end up here |
7197 | | * with a pending calculation. */ |
7198 | | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7199 | | if (extension != NULL && extension->resp == 1) { |
7200 | | KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data; |
7201 | | if (serverKSE != NULL && |
7202 | | serverKSE->lastRet == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7203 | | ret = TLSX_KeyShare_GenKey(ssl, serverKSE); |
7204 | | if (ret != 0) |
7205 | | goto exit_dch; |
7206 | | } |
7207 | | } |
7208 | | #endif |
7209 | | /* Advance state and proceed */ |
7210 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
7211 | 0 | } |
7212 | 0 | FALL_THROUGH; |
7213 | |
|
7214 | 0 | case TLS_ASYNC_FINALIZE: |
7215 | 0 | { |
7216 | 0 | *inOutIdx = args->idx; |
7217 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
7218 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7219 | | ssl->options.pskNegotiated = (args->usingPSK != 0); |
7220 | | #endif |
7221 | |
|
7222 | 0 | if (!args->usingPSK) { |
7223 | 0 | #ifndef NO_CERTS |
7224 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
7225 | | #ifdef HAVE_NULL_CIPHER |
7226 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
7227 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
7228 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
7229 | | ; |
7230 | | } |
7231 | | else |
7232 | | #endif |
7233 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
7234 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7235 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
7236 | 0 | ; /* Do nothing. */ |
7237 | 0 | } |
7238 | 0 | else |
7239 | 0 | #endif |
7240 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
7241 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7242 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
7243 | 0 | ; /* Do nothing. */ |
7244 | 0 | } |
7245 | 0 | else |
7246 | 0 | #endif |
7247 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
7248 | 0 | WOLFSSL_MSG("Negotiated ciphersuite from lesser version than " |
7249 | 0 | "TLS v1.3"); |
7250 | 0 | ERROR_OUT(MATCH_SUITE_ERROR, exit_dch); |
7251 | 0 | } |
7252 | | |
7253 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7254 | | if (ssl->options.resuming) { |
7255 | | ssl->options.resuming = 0; |
7256 | | ssl->arrays->psk_keySz = 0; |
7257 | | XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size); |
7258 | | } |
7259 | | #endif |
7260 | | |
7261 | | /* Derive early secret for handshake secret. */ |
7262 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
7263 | 0 | goto exit_dch; |
7264 | 0 | #endif /* !NO_CERTS */ |
7265 | 0 | } |
7266 | 0 | break; |
7267 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
7268 | 0 | default: |
7269 | 0 | ret = INPUT_CASE_ERROR; |
7270 | 0 | } /* switch (ssl->options.asyncState) */ |
7271 | | |
7272 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7273 | | if (ret == 0 && ssl->options.sendCookie) { |
7274 | | if (ssl->options.cookieGood && |
7275 | | ssl->options.acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE) { |
7276 | | /* Processing second ClientHello. Clear HRR state. */ |
7277 | | ssl->options.serverState = NULL_STATE; |
7278 | | } |
7279 | | |
7280 | | if (ssl->options.cookieGood && |
7281 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7282 | | /* If we already verified the peer with a cookie then we can't |
7283 | | * do another HRR for cipher negotiation. Send alert and restart |
7284 | | * the entire handshake. */ |
7285 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7286 | | } |
7287 | | #ifdef WOLFSSL_DTLS13 |
7288 | | if (ssl->options.dtls && |
7289 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7290 | | /* Cookie and key share negotiation should be handled in |
7291 | | * DoClientHelloStateless. If we enter here then something went |
7292 | | * wrong in our logic. */ |
7293 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7294 | | } |
7295 | | #endif |
7296 | | /* Send a cookie */ |
7297 | | if (!ssl->options.cookieGood && |
7298 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7299 | | #ifdef WOLFSSL_DTLS13 |
7300 | | if (ssl->options.dtls) { |
7301 | | #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME |
7302 | | /* We can skip cookie on resumption */ |
7303 | | if (!ssl->options.dtls || !ssl->options.dtls13NoHrrOnResume || |
7304 | | !args->usingPSK) |
7305 | | #endif |
7306 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7307 | | } |
7308 | | else |
7309 | | #endif |
7310 | | { |
7311 | | /* Need to remove the keyshare ext if we found a common group |
7312 | | * and are not doing curve negotiation. */ |
7313 | | TLSX_Remove(&ssl->extensions, TLSX_KEY_SHARE, ssl->heap); |
7314 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7315 | | } |
7316 | | |
7317 | | } |
7318 | | } |
7319 | | #endif /* WOLFSSL_DTLS13 */ |
7320 | | |
7321 | | #ifdef WOLFSSL_DTLS_CID |
7322 | | /* do not modify CID state if we are sending an HRR */ |
7323 | | if (ret == 0 && ssl->options.dtls && ssl->options.useDtlsCID && |
7324 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7325 | | DtlsCIDOnExtensionsParsed(ssl); |
7326 | | #endif /* WOLFSSL_DTLS_CID */ |
7327 | | |
7328 | | |
7329 | | |
7330 | 0 | exit_dch: |
7331 | |
|
7332 | 0 | WOLFSSL_LEAVE("DoTls13ClientHello", ret); |
7333 | |
|
7334 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7335 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7336 | | ssl->msgsReceived.got_client_hello = 0; |
7337 | | return ret; |
7338 | | } |
7339 | | #endif |
7340 | |
|
7341 | 0 | FreeDch13Args(ssl, args); |
7342 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7343 | | FreeAsyncCtx(ssl, 0); |
7344 | | #endif |
7345 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO); |
7346 | |
|
7347 | 0 | if (ret != 0) { |
7348 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
7349 | 0 | } |
7350 | |
|
7351 | | #if defined(HAVE_ECH) |
7352 | | if (ret == 0 && echX != NULL && |
7353 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
7354 | | |
7355 | | /* add the header to the inner hello */ |
7356 | | AddTls13HandShakeHeader(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
7357 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen, 0, 0, |
7358 | | client_hello, ssl); |
7359 | | } |
7360 | | #endif |
7361 | |
|
7362 | 0 | return ret; |
7363 | 0 | } |
7364 | | |
7365 | | /* Send TLS v1.3 ServerHello message to client. |
7366 | | * Only a server will send this message. |
7367 | | * |
7368 | | * ssl The SSL/TLS object. |
7369 | | * returns 0 on success, otherwise failure. |
7370 | | */ |
7371 | | /* handle generation of TLS 1.3 server_hello (2) */ |
7372 | | int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType) |
7373 | 0 | { |
7374 | 0 | int ret; |
7375 | 0 | byte* output; |
7376 | 0 | word16 length; |
7377 | 0 | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7378 | 0 | int sendSz; |
7379 | | #if defined(HAVE_ECH) |
7380 | | TLSX* echX = NULL; |
7381 | | byte* acceptLabel = (byte*)echAcceptConfirmationLabel; |
7382 | | word32 acceptOffset; |
7383 | | word16 acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
7384 | | #endif |
7385 | |
|
7386 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND); |
7387 | 0 | WOLFSSL_ENTER("SendTls13ServerHello"); |
7388 | | |
7389 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated in |
7390 | | * dtls.c */ |
7391 | 0 | if (extMsgType == hello_retry_request |
7392 | | #ifdef WOLFSSL_DTLS13 |
7393 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
7394 | | #endif |
7395 | 0 | ) { |
7396 | 0 | WOLFSSL_MSG("wolfSSL Sending HelloRetryRequest"); |
7397 | 0 | if ((ret = RestartHandshakeHash(ssl)) < 0) |
7398 | 0 | return ret; |
7399 | 0 | } |
7400 | | |
7401 | 0 | ssl->options.buildingMsg = 1; |
7402 | | #ifdef WOLFSSL_DTLS13 |
7403 | | if (ssl->options.dtls) |
7404 | | idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ; |
7405 | | #endif /* WOLFSSL_DTLS13 */ |
7406 | | |
7407 | | /* Protocol version, server random, session id, cipher suite, compression |
7408 | | * and extensions. |
7409 | | */ |
7410 | 0 | length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz + |
7411 | 0 | SUITE_LEN + COMP_LEN; |
7412 | 0 | ret = TLSX_GetResponseSize(ssl, extMsgType, &length); |
7413 | 0 | if (ret != 0) |
7414 | 0 | return ret; |
7415 | 0 | sendSz = (int)(idx + length); |
7416 | | |
7417 | | /* Check buffers are big enough and grow if needed. */ |
7418 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7419 | 0 | return ret; |
7420 | | |
7421 | | /* Get position in output buffer to write new message to. */ |
7422 | 0 | output = GetOutputBuffer(ssl); |
7423 | | |
7424 | | /* Put the record and handshake headers on. */ |
7425 | 0 | AddTls13Headers(output, length, server_hello, ssl); |
7426 | | |
7427 | | /* The protocol version must be TLS v1.2 for middleboxes. */ |
7428 | 0 | output[idx++] = ssl->version.major; |
7429 | 0 | output[idx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
7430 | |
|
7431 | 0 | if (extMsgType == server_hello) { |
7432 | | /* Generate server random. */ |
7433 | 0 | if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0) |
7434 | 0 | return ret; |
7435 | 0 | } |
7436 | 0 | else { |
7437 | | /* HelloRetryRequest message has fixed value for random. */ |
7438 | 0 | XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN); |
7439 | 0 | } |
7440 | | |
7441 | | #if defined(HAVE_ECH) |
7442 | | /* last 8 bytes of server random */ |
7443 | | acceptOffset = idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
7444 | | #endif |
7445 | | |
7446 | | /* Store in SSL for debugging. */ |
7447 | 0 | XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN); |
7448 | 0 | idx += RAN_LEN; |
7449 | |
|
7450 | | #ifdef WOLFSSL_DEBUG_TLS |
7451 | | WOLFSSL_MSG("Server random"); |
7452 | | WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN); |
7453 | | #endif |
7454 | |
|
7455 | 0 | output[idx++] = ssl->session->sessionIDSz; |
7456 | 0 | if (ssl->session->sessionIDSz > 0) { |
7457 | 0 | XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz); |
7458 | 0 | idx += ssl->session->sessionIDSz; |
7459 | 0 | } |
7460 | | |
7461 | | /* Chosen cipher suite */ |
7462 | 0 | output[idx++] = ssl->options.cipherSuite0; |
7463 | 0 | output[idx++] = ssl->options.cipherSuite; |
7464 | | #ifdef WOLFSSL_DEBUG_TLS |
7465 | | WOLFSSL_MSG("Chosen cipher suite:"); |
7466 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
7467 | | ssl->options.cipherSuite)); |
7468 | | #endif |
7469 | | |
7470 | | /* Compression not supported in TLS v1.3. */ |
7471 | 0 | output[idx++] = 0; |
7472 | | |
7473 | | /* Extensions */ |
7474 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL); |
7475 | 0 | if (ret != 0) |
7476 | 0 | return ret; |
7477 | | |
7478 | 0 | if (extMsgType == hello_retry_request) { |
7479 | 0 | TLSX* ksExt = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7480 | 0 | if (ksExt != NULL) { |
7481 | 0 | KeyShareEntry* kse = (KeyShareEntry*)ksExt->data; |
7482 | 0 | if (kse != NULL) |
7483 | 0 | ssl->hrr_keyshare_group = kse->group; |
7484 | 0 | } |
7485 | 0 | } |
7486 | |
|
7487 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7488 | | if (ssl->options.sendCookie && extMsgType == hello_retry_request) { |
7489 | | /* Reset the hashes from here. We will be able to restart the hashes |
7490 | | * from the cookie in RestartHandshakeHashWithCookie */ |
7491 | | #ifdef WOLFSSL_DTLS13 |
7492 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated |
7493 | | * in dtls.c */ |
7494 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) |
7495 | | ret = 0; |
7496 | | else |
7497 | | #endif |
7498 | | ret = InitHandshakeHashes(ssl); |
7499 | | } |
7500 | | else |
7501 | | #endif |
7502 | 0 | { |
7503 | | #ifdef WOLFSSL_DTLS13 |
7504 | | if (ssl->options.dtls) { |
7505 | | ret = Dtls13HashHandshake( |
7506 | | ssl, |
7507 | | output + Dtls13GetRlHeaderLength(ssl, 0) , |
7508 | | (word16)sendSz - Dtls13GetRlHeaderLength(ssl, 0)); |
7509 | | } |
7510 | | else |
7511 | | #endif /* WOLFSSL_DTLS13 */ |
7512 | 0 | { |
7513 | | #if defined(HAVE_ECH) |
7514 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7515 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7516 | | if (echX == NULL) |
7517 | | return WOLFSSL_FATAL_ERROR; |
7518 | | /* use hrr offset */ |
7519 | | if (extMsgType == hello_retry_request) { |
7520 | | acceptOffset = |
7521 | | (word32)(((WOLFSSL_ECH*)echX->data)->confBuf - output); |
7522 | | acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
7523 | | acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
7524 | | } |
7525 | | /* replace the last 8 bytes of server random with the accept */ |
7526 | | if (((WOLFSSL_ECH*)echX->data)->state == ECH_PARSED_INTERNAL) { |
7527 | | if (ret == 0) { |
7528 | | ret = EchWriteAcceptance(ssl, acceptLabel, |
7529 | | acceptLabelSz, output + RECORD_HEADER_SZ, |
7530 | | acceptOffset - RECORD_HEADER_SZ, |
7531 | | sendSz - RECORD_HEADER_SZ, extMsgType); |
7532 | | } |
7533 | | if (extMsgType == hello_retry_request) { |
7534 | | /* reset the ech state for round 2 */ |
7535 | | ((WOLFSSL_ECH*)echX->data)->state = ECH_WRITE_NONE; |
7536 | | } |
7537 | | else { |
7538 | | if (ret == 0) { |
7539 | | /* update serverRandom on success */ |
7540 | | XMEMCPY(ssl->arrays->serverRandom, |
7541 | | output + acceptOffset - |
7542 | | (RAN_LEN -ECH_ACCEPT_CONFIRMATION_SZ), RAN_LEN); |
7543 | | } |
7544 | | /* remove ech so we don't keep sending it in write */ |
7545 | | TLSX_Remove(&ssl->extensions, TLSX_ECH, ssl->heap); |
7546 | | } |
7547 | | } |
7548 | | } |
7549 | | #endif |
7550 | 0 | if (ret == 0) |
7551 | 0 | ret = HashOutput(ssl, output, sendSz, 0); |
7552 | 0 | } |
7553 | 0 | } |
7554 | |
|
7555 | 0 | if (ret != 0) |
7556 | 0 | return ret; |
7557 | | |
7558 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7559 | | if (ssl->hsInfoOn) |
7560 | | AddPacketName(ssl, "ServerHello"); |
7561 | | if (ssl->toInfoOn) { |
7562 | | ret = AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz, |
7563 | | WRITE_PROTO, 0, ssl->heap); |
7564 | | if (ret != 0) |
7565 | | return ret; |
7566 | | } |
7567 | | #endif |
7568 | | |
7569 | 0 | if (extMsgType == server_hello) |
7570 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
7571 | |
|
7572 | 0 | ssl->options.buildingMsg = 0; |
7573 | | #ifdef WOLFSSL_DTLS13 |
7574 | | if (ssl->options.dtls) { |
7575 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)sendSz, |
7576 | | (enum HandShakeType)extMsgType, 0); |
7577 | | |
7578 | | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7579 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7580 | | return ret; |
7581 | | } |
7582 | | #endif /* WOLFSSL_DTLS13 */ |
7583 | |
|
7584 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7585 | |
|
7586 | 0 | if (!ssl->options.groupMessages || extMsgType != server_hello) |
7587 | 0 | ret = SendBuffered(ssl); |
7588 | |
|
7589 | 0 | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7590 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7591 | |
|
7592 | 0 | return ret; |
7593 | 0 | } |
7594 | | |
7595 | | /* handle generation of TLS 1.3 encrypted_extensions (8) */ |
7596 | | /* Send the rest of the extensions encrypted under the handshake key. |
7597 | | * This message is always encrypted in TLS v1.3. |
7598 | | * Only a server will send this message. |
7599 | | * |
7600 | | * ssl The SSL/TLS object. |
7601 | | * returns 0 on success, otherwise failure. |
7602 | | */ |
7603 | | static int SendTls13EncryptedExtensions(WOLFSSL* ssl) |
7604 | 0 | { |
7605 | 0 | int ret; |
7606 | 0 | byte* output; |
7607 | 0 | word16 length = 0; |
7608 | 0 | word32 idx; |
7609 | 0 | int sendSz; |
7610 | |
|
7611 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7612 | 0 | WOLFSSL_ENTER("SendTls13EncryptedExtensions"); |
7613 | |
|
7614 | 0 | ssl->options.buildingMsg = 1; |
7615 | 0 | ssl->keys.encryptionOn = 1; |
7616 | |
|
7617 | | #ifdef WOLFSSL_DTLS13 |
7618 | | if (ssl->options.dtls) { |
7619 | | idx = Dtls13GetHeadersLength(ssl, encrypted_extensions); |
7620 | | } |
7621 | | else |
7622 | | #endif /* WOLFSSL_DTLS13 */ |
7623 | 0 | { |
7624 | 0 | idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7625 | 0 | } |
7626 | |
|
7627 | 0 | #if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
7628 | 0 | if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0) |
7629 | 0 | return ret; |
7630 | 0 | #endif |
7631 | | |
7632 | | /* Derive the handshake secret now that we are at first message to be |
7633 | | * encrypted under the keys. |
7634 | | */ |
7635 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
7636 | 0 | return ret; |
7637 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
7638 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) |
7639 | 0 | return ret; |
7640 | | |
7641 | | /* Setup encrypt/decrypt keys for following messages. */ |
7642 | | #ifdef WOLFSSL_EARLY_DATA |
7643 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
7644 | | return ret; |
7645 | | if (ssl->earlyData != process_early_data) { |
7646 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
7647 | | return ret; |
7648 | | } |
7649 | | #else |
7650 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
7651 | 0 | return ret; |
7652 | 0 | #endif |
7653 | | #ifdef WOLFSSL_QUIC |
7654 | | if (IsAtLeastTLSv1_3(ssl->version) && WOLFSSL_IS_QUIC(ssl)) { |
7655 | | ret = wolfSSL_quic_add_transport_extensions(ssl, encrypted_extensions); |
7656 | | if (ret != 0) |
7657 | | return ret; |
7658 | | } |
7659 | | #endif |
7660 | | |
7661 | | #ifdef WOLFSSL_DTLS13 |
7662 | | if (ssl->options.dtls) { |
7663 | | w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
7664 | | ssl->dtls13Epoch = epochHandshake; |
7665 | | |
7666 | | ret = Dtls13SetEpochKeys( |
7667 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
7668 | | if (ret != 0) |
7669 | | return ret; |
7670 | | |
7671 | | } |
7672 | | #endif /* WOLFSSL_DTLS13 */ |
7673 | | |
7674 | 0 | ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length); |
7675 | 0 | if (ret != 0) |
7676 | 0 | return ret; |
7677 | | |
7678 | 0 | sendSz = (int)(idx + length); |
7679 | | /* Encryption always on. */ |
7680 | 0 | sendSz += MAX_MSG_EXTRA; |
7681 | | |
7682 | | /* Check buffers are big enough and grow if needed. */ |
7683 | 0 | ret = CheckAvailableSize(ssl, sendSz); |
7684 | 0 | if (ret != 0) |
7685 | 0 | return ret; |
7686 | | |
7687 | | /* Get position in output buffer to write new message to. */ |
7688 | 0 | output = GetOutputBuffer(ssl); |
7689 | | |
7690 | | /* Put the record and handshake headers on. */ |
7691 | 0 | AddTls13Headers(output, length, encrypted_extensions, ssl); |
7692 | |
|
7693 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL); |
7694 | 0 | if (ret != 0) |
7695 | 0 | return ret; |
7696 | 0 | idx += length; |
7697 | |
|
7698 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7699 | | if (ssl->hsInfoOn) |
7700 | | AddPacketName(ssl, "EncryptedExtensions"); |
7701 | | if (ssl->toInfoOn) { |
7702 | | ret = AddPacketInfo(ssl, "EncryptedExtensions", handshake, output, |
7703 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
7704 | | if (ret != 0) |
7705 | | return ret; |
7706 | | } |
7707 | | #endif |
7708 | |
|
7709 | | #ifdef WOLFSSL_DTLS13 |
7710 | | if (ssl->options.dtls) { |
7711 | | ssl->options.buildingMsg = 0; |
7712 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)idx, |
7713 | | encrypted_extensions, 1); |
7714 | | |
7715 | | if (ret == 0) |
7716 | | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7717 | | |
7718 | | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7719 | | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7720 | | |
7721 | | return ret; |
7722 | | } |
7723 | | #endif /* WOLFSSL_DTLS13 */ |
7724 | | |
7725 | | /* This handshake message is always encrypted. */ |
7726 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
7727 | 0 | (int)(idx - RECORD_HEADER_SZ), |
7728 | 0 | handshake, 1, 0, 0); |
7729 | 0 | if (sendSz < 0) |
7730 | 0 | return sendSz; |
7731 | | |
7732 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7733 | 0 | ssl->options.buildingMsg = 0; |
7734 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7735 | |
|
7736 | 0 | if (!ssl->options.groupMessages) |
7737 | 0 | ret = SendBuffered(ssl); |
7738 | | |
7739 | |
|
7740 | 0 | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7741 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7742 | |
|
7743 | 0 | return ret; |
7744 | 0 | } |
7745 | | |
7746 | | #ifndef NO_CERTS |
7747 | | /* handle generation TLS v1.3 certificate_request (13) */ |
7748 | | /* Send the TLS v1.3 CertificateRequest message. |
7749 | | * This message is always encrypted in TLS v1.3. |
7750 | | * Only a server will send this message. |
7751 | | * |
7752 | | * ssl SSL/TLS object. |
7753 | | * reqCtx Request context. |
7754 | | * reqCtxLen Length of context. 0 when sending as part of handshake. |
7755 | | * returns 0 on success, otherwise failure. |
7756 | | */ |
7757 | | static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx, |
7758 | | word32 reqCtxLen) |
7759 | 0 | { |
7760 | 0 | byte* output; |
7761 | 0 | int ret; |
7762 | 0 | int sendSz; |
7763 | 0 | word32 i; |
7764 | 0 | word32 reqSz; |
7765 | 0 | word16 hashSigAlgoSz = 0; |
7766 | 0 | SignatureAlgorithms* sa; |
7767 | |
|
7768 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7769 | 0 | WOLFSSL_ENTER("SendTls13CertificateRequest"); |
7770 | |
|
7771 | 0 | ssl->options.buildingMsg = 1; |
7772 | |
|
7773 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) |
7774 | 0 | return SIDE_ERROR; |
7775 | | |
7776 | | /* Get the length of the hashSigAlgo buffer */ |
7777 | 0 | InitSuitesHashSigAlgo(NULL, SIG_ALL, 1, ssl->buffers.keySz, |
7778 | 0 | &hashSigAlgoSz); |
7779 | 0 | sa = TLSX_SignatureAlgorithms_New(ssl, hashSigAlgoSz, ssl->heap); |
7780 | 0 | if (sa == NULL) |
7781 | 0 | return MEMORY_ERROR; |
7782 | 0 | InitSuitesHashSigAlgo(sa->hashSigAlgo, SIG_ALL, 1, ssl->buffers.keySz, |
7783 | 0 | &hashSigAlgoSz); |
7784 | 0 | ret = TLSX_Push(&ssl->extensions, TLSX_SIGNATURE_ALGORITHMS, sa, ssl->heap); |
7785 | 0 | if (ret != 0) { |
7786 | 0 | TLSX_SignatureAlgorithms_FreeAll(sa, ssl->heap); |
7787 | 0 | return ret; |
7788 | 0 | } |
7789 | | |
7790 | 0 | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7791 | | #ifdef WOLFSSL_DTLS13 |
7792 | | if (ssl->options.dtls) |
7793 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
7794 | | #endif /* WOLFSSL_DTLS13 */ |
7795 | |
|
7796 | 0 | reqSz = (word16)(OPAQUE8_LEN + reqCtxLen); |
7797 | 0 | ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz); |
7798 | 0 | if (ret != 0) |
7799 | 0 | return ret; |
7800 | | |
7801 | 0 | sendSz = (int)(i + reqSz); |
7802 | | /* Always encrypted and make room for padding. */ |
7803 | 0 | sendSz += MAX_MSG_EXTRA; |
7804 | | |
7805 | | /* Check buffers are big enough and grow if needed. */ |
7806 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7807 | 0 | return ret; |
7808 | | |
7809 | | /* Get position in output buffer to write new message to. */ |
7810 | 0 | output = GetOutputBuffer(ssl); |
7811 | | |
7812 | | /* Put the record and handshake headers on. */ |
7813 | 0 | AddTls13Headers(output, reqSz, certificate_request, ssl); |
7814 | | |
7815 | | /* Certificate request context. */ |
7816 | 0 | output[i++] = (byte)reqCtxLen; |
7817 | 0 | if (reqCtxLen != 0) { |
7818 | 0 | XMEMCPY(output + i, reqCtx, reqCtxLen); |
7819 | 0 | i += reqCtxLen; |
7820 | 0 | } |
7821 | | |
7822 | | /* Certificate extensions. */ |
7823 | 0 | reqSz = 0; |
7824 | 0 | ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz); |
7825 | 0 | if (ret != 0) |
7826 | 0 | return ret; |
7827 | 0 | i += reqSz; |
7828 | |
|
7829 | | #ifdef WOLFSSL_DTLS13 |
7830 | | if (ssl->options.dtls) { |
7831 | | ssl->options.buildingMsg = 0; |
7832 | | ret = |
7833 | | Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
7834 | | certificate_request, 1); |
7835 | | |
7836 | | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
7837 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7838 | | |
7839 | | return ret; |
7840 | | |
7841 | | } |
7842 | | #endif /* WOLFSSL_DTLS13 */ |
7843 | | |
7844 | | /* Always encrypted. */ |
7845 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
7846 | 0 | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
7847 | 0 | if (sendSz < 0) |
7848 | 0 | return sendSz; |
7849 | | |
7850 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7851 | | if (ssl->hsInfoOn) |
7852 | | AddPacketName(ssl, "CertificateRequest"); |
7853 | | if (ssl->toInfoOn) { |
7854 | | ret = AddPacketInfo(ssl, "CertificateRequest", handshake, output, |
7855 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
7856 | | if (ret != 0) |
7857 | | return ret; |
7858 | | } |
7859 | | #endif |
7860 | | |
7861 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7862 | 0 | ssl->options.buildingMsg = 0; |
7863 | 0 | if (!ssl->options.groupMessages) |
7864 | 0 | ret = SendBuffered(ssl); |
7865 | |
|
7866 | 0 | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
7867 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7868 | |
|
7869 | 0 | return ret; |
7870 | 0 | } |
7871 | | #endif /* NO_CERTS */ |
7872 | | #endif /* NO_WOLFSSL_SERVER */ |
7873 | | |
7874 | | #ifndef NO_CERTS |
7875 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
7876 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
7877 | | /* Encode the signature algorithm into buffer. |
7878 | | * |
7879 | | * hashalgo The hash algorithm. |
7880 | | * hsType The signature type. |
7881 | | * output The buffer to encode into. |
7882 | | */ |
7883 | | static WC_INLINE void EncodeSigAlg(const WOLFSSL * ssl, byte hashAlgo, byte hsType, byte* output) |
7884 | | { |
7885 | | (void)ssl; |
7886 | | switch (hsType) { |
7887 | | #ifdef HAVE_ECC |
7888 | | case ecc_dsa_sa_algo: |
7889 | | output[0] = hashAlgo; |
7890 | | output[1] = ecc_dsa_sa_algo; |
7891 | | break; |
7892 | | #endif |
7893 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
7894 | | case sm2_sa_algo: |
7895 | | output[0] = SM2_SA_MAJOR; |
7896 | | output[1] = SM2_SA_MINOR; |
7897 | | break; |
7898 | | #endif |
7899 | | #ifdef HAVE_ED25519 |
7900 | | /* ED25519: 0x0807 */ |
7901 | | case ed25519_sa_algo: |
7902 | | output[0] = ED25519_SA_MAJOR; |
7903 | | output[1] = ED25519_SA_MINOR; |
7904 | | (void)hashAlgo; |
7905 | | break; |
7906 | | #endif |
7907 | | #ifdef HAVE_ED448 |
7908 | | /* ED448: 0x0808 */ |
7909 | | case ed448_sa_algo: |
7910 | | output[0] = ED448_SA_MAJOR; |
7911 | | output[1] = ED448_SA_MINOR; |
7912 | | (void)hashAlgo; |
7913 | | break; |
7914 | | #endif |
7915 | | #ifndef NO_RSA |
7916 | | /* PSS signatures: 0x080[4-6] or 0x080[9-B] */ |
7917 | | case rsa_pss_sa_algo: |
7918 | | output[0] = rsa_pss_sa_algo; |
7919 | | #ifdef WC_RSA_PSS |
7920 | | /* If the private key uses the RSA-PSS OID, and the peer supports |
7921 | | * the rsa_pss_pss_* signature algorithm in use, then report |
7922 | | * rsa_pss_pss_* rather than rsa_pss_rsae_*. */ |
7923 | | if (ssl->useRsaPss && |
7924 | | ((ssl->pssAlgo & (1U << hashAlgo)) != 0U) && |
7925 | | (sha256_mac <= hashAlgo) && (hashAlgo <= sha512_mac)) |
7926 | | { |
7927 | | output[1] = PSS_RSAE_TO_PSS_PSS(hashAlgo); |
7928 | | } |
7929 | | else |
7930 | | #endif |
7931 | | { |
7932 | | output[1] = hashAlgo; |
7933 | | } |
7934 | | break; |
7935 | | #endif |
7936 | | #ifdef HAVE_FALCON |
7937 | | case falcon_level1_sa_algo: |
7938 | | output[0] = FALCON_LEVEL1_SA_MAJOR; |
7939 | | output[1] = FALCON_LEVEL1_SA_MINOR; |
7940 | | break; |
7941 | | case falcon_level5_sa_algo: |
7942 | | output[0] = FALCON_LEVEL5_SA_MAJOR; |
7943 | | output[1] = FALCON_LEVEL5_SA_MINOR; |
7944 | | break; |
7945 | | #endif |
7946 | | #ifdef HAVE_DILITHIUM |
7947 | | case dilithium_level2_sa_algo: |
7948 | | output[0] = DILITHIUM_LEVEL2_SA_MAJOR; |
7949 | | output[1] = DILITHIUM_LEVEL2_SA_MINOR; |
7950 | | break; |
7951 | | case dilithium_level3_sa_algo: |
7952 | | output[0] = DILITHIUM_LEVEL3_SA_MAJOR; |
7953 | | output[1] = DILITHIUM_LEVEL3_SA_MINOR; |
7954 | | break; |
7955 | | case dilithium_level5_sa_algo: |
7956 | | output[0] = DILITHIUM_LEVEL5_SA_MAJOR; |
7957 | | output[1] = DILITHIUM_LEVEL5_SA_MINOR; |
7958 | | break; |
7959 | | #endif |
7960 | | default: |
7961 | | break; |
7962 | | } |
7963 | | } |
7964 | | |
7965 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
7966 | | /* These match up with what the OQS team has defined. */ |
7967 | | #define HYBRID_SA_MAJOR 0xFE |
7968 | | #define HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR 0xA1 |
7969 | | #define HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR 0xA2 |
7970 | | #define HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR 0xA4 |
7971 | | #define HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR 0xA6 |
7972 | | #define HYBRID_P256_FALCON_LEVEL1_SA_MINOR 0xAF |
7973 | | #define HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR 0xB0 |
7974 | | #define HYBRID_P521_FALCON_LEVEL5_SA_MINOR 0xB2 |
7975 | | |
7976 | | /* Custom defined ones for PQC first */ |
7977 | | #define HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR 0xD1 |
7978 | | #define HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR 0xD2 |
7979 | | #define HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR 0xD3 |
7980 | | #define HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR 0xD4 |
7981 | | #define HYBRID_FALCON_LEVEL1_P256_SA_MINOR 0xD5 |
7982 | | #define HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR 0xD6 |
7983 | | #define HYBRID_FALCON_LEVEL5_P521_SA_MINOR 0xD7 |
7984 | | |
7985 | | |
7986 | | static void EncodeDualSigAlg(byte sigAlg, byte altSigAlg, byte* output) |
7987 | | { |
7988 | | /* Initialize output to error indicator. */ |
7989 | | output[0] = 0x0; |
7990 | | output[1] = 0x0; |
7991 | | |
7992 | | if (sigAlg == ecc_dsa_sa_algo && altSigAlg == dilithium_level2_sa_algo) { |
7993 | | output[1] = HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR; |
7994 | | } |
7995 | | else if (sigAlg == rsa_pss_sa_algo && |
7996 | | altSigAlg == dilithium_level2_sa_algo) { |
7997 | | output[1] = HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR; |
7998 | | } |
7999 | | else if (sigAlg == ecc_dsa_sa_algo && |
8000 | | altSigAlg == dilithium_level3_sa_algo) { |
8001 | | output[1] = HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR; |
8002 | | } |
8003 | | else if (sigAlg == ecc_dsa_sa_algo && |
8004 | | altSigAlg == dilithium_level5_sa_algo) { |
8005 | | output[1] = HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR; |
8006 | | } |
8007 | | else if (sigAlg == ecc_dsa_sa_algo && |
8008 | | altSigAlg == falcon_level1_sa_algo) { |
8009 | | output[1] = HYBRID_P256_FALCON_LEVEL1_SA_MINOR; |
8010 | | } |
8011 | | else if (sigAlg == rsa_pss_sa_algo && |
8012 | | altSigAlg == falcon_level1_sa_algo) { |
8013 | | output[1] = HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR; |
8014 | | } |
8015 | | else if (sigAlg == ecc_dsa_sa_algo && |
8016 | | altSigAlg == falcon_level5_sa_algo) { |
8017 | | output[1] = HYBRID_P521_FALCON_LEVEL5_SA_MINOR; |
8018 | | } |
8019 | | else if (sigAlg == dilithium_level2_sa_algo && |
8020 | | altSigAlg == ecc_dsa_sa_algo) { |
8021 | | output[1] = HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR; |
8022 | | } |
8023 | | else if (sigAlg == dilithium_level2_sa_algo && |
8024 | | altSigAlg == rsa_pss_sa_algo) { |
8025 | | output[1] = HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR; |
8026 | | } |
8027 | | else if (sigAlg == dilithium_level3_sa_algo && |
8028 | | altSigAlg == ecc_dsa_sa_algo) { |
8029 | | output[1] = HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR; |
8030 | | } |
8031 | | else if (sigAlg == dilithium_level5_sa_algo && |
8032 | | altSigAlg == ecc_dsa_sa_algo) { |
8033 | | output[1] = HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR; |
8034 | | } |
8035 | | else if (sigAlg == falcon_level1_sa_algo && |
8036 | | altSigAlg == ecc_dsa_sa_algo) { |
8037 | | output[1] = HYBRID_FALCON_LEVEL1_P256_SA_MINOR; |
8038 | | } |
8039 | | else if (sigAlg == falcon_level1_sa_algo && |
8040 | | altSigAlg == rsa_pss_sa_algo) { |
8041 | | output[1] = HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR; |
8042 | | } |
8043 | | else if (sigAlg == falcon_level5_sa_algo && |
8044 | | altSigAlg == ecc_dsa_sa_algo) { |
8045 | | output[1] = HYBRID_FALCON_LEVEL5_P521_SA_MINOR; |
8046 | | } |
8047 | | |
8048 | | if (output[1] != 0x0) { |
8049 | | output[0] = HYBRID_SA_MAJOR; |
8050 | | } |
8051 | | } |
8052 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8053 | | |
8054 | | static enum wc_MACAlgorithm GetNewSAHashAlgo(int typeIn) |
8055 | 0 | { |
8056 | 0 | switch (typeIn) { |
8057 | 0 | case RSA_PSS_RSAE_SHA256_MINOR: |
8058 | 0 | case RSA_PSS_PSS_SHA256_MINOR: |
8059 | 0 | return sha256_mac; |
8060 | | |
8061 | 0 | case RSA_PSS_RSAE_SHA384_MINOR: |
8062 | 0 | case RSA_PSS_PSS_SHA384_MINOR: |
8063 | 0 | return sha384_mac; |
8064 | | |
8065 | 0 | case RSA_PSS_RSAE_SHA512_MINOR: |
8066 | 0 | case RSA_PSS_PSS_SHA512_MINOR: |
8067 | 0 | case ED25519_SA_MINOR: |
8068 | 0 | case ED448_SA_MINOR: |
8069 | 0 | return sha512_mac; |
8070 | 0 | default: |
8071 | 0 | return no_mac; |
8072 | 0 | } |
8073 | 0 | } |
8074 | | |
8075 | | /* Decode the signature algorithm. |
8076 | | * |
8077 | | * input The encoded signature algorithm. |
8078 | | * hashalgo The hash algorithm. |
8079 | | * hsType The signature type. |
8080 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8081 | | */ |
8082 | | static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo, |
8083 | | byte* hsType) |
8084 | 0 | { |
8085 | 0 | int ret = 0; |
8086 | |
|
8087 | 0 | switch (input[0]) { |
8088 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8089 | 0 | case SM2_SA_MAJOR: |
8090 | 0 | if (input[1] == SM2_SA_MINOR) { |
8091 | 0 | *hsType = sm2_sa_algo; |
8092 | 0 | *hashAlgo = sm3_mac; |
8093 | 0 | } |
8094 | 0 | else |
8095 | 0 | ret = INVALID_PARAMETER; |
8096 | 0 | break; |
8097 | 0 | #endif |
8098 | 0 | case NEW_SA_MAJOR: |
8099 | 0 | *hashAlgo = GetNewSAHashAlgo(input[1]); |
8100 | | |
8101 | | /* PSS encryption: 0x080[4-6] */ |
8102 | 0 | if (input[1] >= RSA_PSS_RSAE_SHA256_MINOR && |
8103 | 0 | input[1] <= RSA_PSS_RSAE_SHA512_MINOR) { |
8104 | 0 | *hsType = input[0]; |
8105 | 0 | } |
8106 | | /* PSS signature: 0x080[9-B] */ |
8107 | 0 | else if (input[1] >= RSA_PSS_PSS_SHA256_MINOR && |
8108 | 0 | input[1] <= RSA_PSS_PSS_SHA512_MINOR) { |
8109 | 0 | *hsType = input[0]; |
8110 | 0 | } |
8111 | 0 | #ifdef HAVE_ED25519 |
8112 | | /* ED25519: 0x0807 */ |
8113 | 0 | else if (input[1] == ED25519_SA_MINOR) { |
8114 | 0 | *hsType = ed25519_sa_algo; |
8115 | | /* Hash performed as part of sign/verify operation. */ |
8116 | 0 | } |
8117 | 0 | #endif |
8118 | 0 | #ifdef HAVE_ED448 |
8119 | | /* ED448: 0x0808 */ |
8120 | 0 | else if (input[1] == ED448_SA_MINOR) { |
8121 | 0 | *hsType = ed448_sa_algo; |
8122 | | /* Hash performed as part of sign/verify operation. */ |
8123 | 0 | } |
8124 | 0 | #endif |
8125 | 0 | else |
8126 | 0 | ret = INVALID_PARAMETER; |
8127 | 0 | break; |
8128 | | #if defined(HAVE_FALCON) |
8129 | | case FALCON_SA_MAJOR: |
8130 | | if (input[1] == FALCON_LEVEL1_SA_MINOR) { |
8131 | | *hsType = falcon_level1_sa_algo; |
8132 | | /* Hash performed as part of sign/verify operation. */ |
8133 | | *hashAlgo = sha512_mac; |
8134 | | } else if (input[1] == FALCON_LEVEL5_SA_MINOR) { |
8135 | | *hsType = falcon_level5_sa_algo; |
8136 | | /* Hash performed as part of sign/verify operation. */ |
8137 | | *hashAlgo = sha512_mac; |
8138 | | } |
8139 | | else |
8140 | | ret = INVALID_PARAMETER; |
8141 | | break; |
8142 | | #endif /* HAVE_FALCON */ |
8143 | | #if defined(HAVE_DILITHIUM) |
8144 | | case DILITHIUM_SA_MAJOR: |
8145 | | if (input[1] == DILITHIUM_LEVEL2_SA_MINOR) { |
8146 | | *hsType = dilithium_level2_sa_algo; |
8147 | | /* Hash performed as part of sign/verify operation. */ |
8148 | | *hashAlgo = sha512_mac; |
8149 | | } else if (input[1] == DILITHIUM_LEVEL3_SA_MINOR) { |
8150 | | *hsType = dilithium_level3_sa_algo; |
8151 | | /* Hash performed as part of sign/verify operation. */ |
8152 | | *hashAlgo = sha512_mac; |
8153 | | } else if (input[1] == DILITHIUM_LEVEL5_SA_MINOR) { |
8154 | | *hsType = dilithium_level5_sa_algo; |
8155 | | /* Hash performed as part of sign/verify operation. */ |
8156 | | *hashAlgo = sha512_mac; |
8157 | | } |
8158 | | else |
8159 | | { |
8160 | | ret = INVALID_PARAMETER; |
8161 | | } |
8162 | | break; |
8163 | | #endif /* HAVE_DILITHIUM */ |
8164 | 0 | default: |
8165 | 0 | *hashAlgo = input[0]; |
8166 | 0 | *hsType = input[1]; |
8167 | 0 | break; |
8168 | 0 | } |
8169 | | |
8170 | 0 | return ret; |
8171 | 0 | } |
8172 | | |
8173 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8174 | | /* Decode the hybrid signature algorithm. |
8175 | | * |
8176 | | * input The encoded signature algorithm. |
8177 | | * hashalgo The hash algorithm. |
8178 | | * hsType The signature type. |
8179 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8180 | | */ |
8181 | | static WC_INLINE int DecodeTls13HybridSigAlg(byte* input, byte* hashAlg, |
8182 | | byte *sigAlg, byte *altSigAlg) |
8183 | | { |
8184 | | |
8185 | | if (input[0] != HYBRID_SA_MAJOR) { |
8186 | | return INVALID_PARAMETER; |
8187 | | } |
8188 | | |
8189 | | if (input[1] == HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR) { |
8190 | | *sigAlg = ecc_dsa_sa_algo; |
8191 | | *hashAlg = sha256_mac; |
8192 | | *altSigAlg = dilithium_level2_sa_algo; |
8193 | | } |
8194 | | else if (input[1] == HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR) { |
8195 | | *sigAlg = rsa_pss_sa_algo; |
8196 | | *hashAlg = sha256_mac; |
8197 | | *altSigAlg = dilithium_level2_sa_algo; |
8198 | | } |
8199 | | else if (input[1] == HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR) { |
8200 | | *sigAlg = ecc_dsa_sa_algo; |
8201 | | *hashAlg = sha384_mac; |
8202 | | *altSigAlg = dilithium_level3_sa_algo; |
8203 | | } |
8204 | | else if (input[1] == HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR) { |
8205 | | *sigAlg = ecc_dsa_sa_algo; |
8206 | | *hashAlg = sha512_mac; |
8207 | | *altSigAlg = dilithium_level5_sa_algo; |
8208 | | } |
8209 | | else if (input[1] == HYBRID_P256_FALCON_LEVEL1_SA_MINOR) { |
8210 | | *sigAlg = ecc_dsa_sa_algo; |
8211 | | *hashAlg = sha256_mac; |
8212 | | *altSigAlg = falcon_level1_sa_algo; |
8213 | | } |
8214 | | else if (input[1] == HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR) { |
8215 | | *sigAlg = rsa_pss_sa_algo; |
8216 | | *hashAlg = sha256_mac; |
8217 | | *altSigAlg = falcon_level1_sa_algo; |
8218 | | } |
8219 | | else if (input[1] == HYBRID_P521_FALCON_LEVEL5_SA_MINOR) { |
8220 | | *sigAlg = ecc_dsa_sa_algo; |
8221 | | *hashAlg = sha512_mac; |
8222 | | *altSigAlg = falcon_level5_sa_algo; |
8223 | | } |
8224 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR) { |
8225 | | *sigAlg = dilithium_level2_sa_algo; |
8226 | | *hashAlg = sha256_mac; |
8227 | | *altSigAlg = ecc_dsa_sa_algo; |
8228 | | } |
8229 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR) { |
8230 | | *sigAlg = dilithium_level2_sa_algo; |
8231 | | *hashAlg = sha256_mac; |
8232 | | *altSigAlg = rsa_pss_sa_algo; |
8233 | | } |
8234 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR) { |
8235 | | *sigAlg = dilithium_level3_sa_algo; |
8236 | | *hashAlg = sha384_mac; |
8237 | | *altSigAlg = ecc_dsa_sa_algo; |
8238 | | } |
8239 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR) { |
8240 | | *sigAlg = dilithium_level5_sa_algo; |
8241 | | *hashAlg = sha512_mac; |
8242 | | *altSigAlg = ecc_dsa_sa_algo; |
8243 | | } |
8244 | | else if (input[1] == HYBRID_FALCON_LEVEL1_P256_SA_MINOR) { |
8245 | | *sigAlg = falcon_level1_sa_algo; |
8246 | | *hashAlg = sha256_mac; |
8247 | | *altSigAlg = ecc_dsa_sa_algo; |
8248 | | } |
8249 | | else if (input[1] == HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR) { |
8250 | | *sigAlg = falcon_level1_sa_algo; |
8251 | | *hashAlg = sha256_mac; |
8252 | | *altSigAlg = rsa_pss_sa_algo; |
8253 | | } |
8254 | | else if (input[1] == HYBRID_FALCON_LEVEL5_P521_SA_MINOR) { |
8255 | | *sigAlg = falcon_level5_sa_algo; |
8256 | | *hashAlg = sha512_mac; |
8257 | | *altSigAlg = ecc_dsa_sa_algo; |
8258 | | } |
8259 | | else { |
8260 | | return INVALID_PARAMETER; |
8261 | | } |
8262 | | |
8263 | | return 0; |
8264 | | } |
8265 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8266 | | |
8267 | | /* Get the hash of the messages so far. |
8268 | | * |
8269 | | * ssl The SSL/TLS object. |
8270 | | * hash The buffer to write the hash to. |
8271 | | * returns the length of the hash. |
8272 | | */ |
8273 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash) |
8274 | 0 | { |
8275 | 0 | int ret = 0; |
8276 | 0 | switch (ssl->specs.mac_algorithm) { |
8277 | 0 | #ifndef NO_SHA256 |
8278 | 0 | case sha256_mac: |
8279 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
8280 | 0 | if (ret == 0) |
8281 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
8282 | 0 | break; |
8283 | 0 | #endif /* !NO_SHA256 */ |
8284 | 0 | #ifdef WOLFSSL_SHA384 |
8285 | 0 | case sha384_mac: |
8286 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
8287 | 0 | if (ret == 0) |
8288 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
8289 | 0 | break; |
8290 | 0 | #endif /* WOLFSSL_SHA384 */ |
8291 | | #ifdef WOLFSSL_TLS13_SHA512 |
8292 | | case sha512_mac: |
8293 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
8294 | | if (ret == 0) |
8295 | | ret = WC_SHA512_DIGEST_SIZE; |
8296 | | break; |
8297 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
8298 | 0 | #ifdef WOLFSSL_SM3 |
8299 | 0 | case sm3_mac: |
8300 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
8301 | 0 | if (ret == 0) |
8302 | 0 | ret = WC_SM3_DIGEST_SIZE; |
8303 | 0 | break; |
8304 | 0 | #endif /* WOLFSSL_SM3 */ |
8305 | 0 | default: |
8306 | 0 | break; |
8307 | 0 | } |
8308 | 0 | return ret; |
8309 | 0 | } |
8310 | | |
8311 | | /* The server certificate verification label. */ |
8312 | | static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8313 | | "TLS 1.3, server CertificateVerify"; |
8314 | | /* The client certificate verification label. */ |
8315 | | static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8316 | | "TLS 1.3, client CertificateVerify"; |
8317 | | /* The prefix byte in the signature data. */ |
8318 | | #define SIGNING_DATA_PREFIX_BYTE 0x20 |
8319 | | |
8320 | | /* Create the signature data for TLS v1.3 certificate verification. |
8321 | | * |
8322 | | * ssl The SSL/TLS object. |
8323 | | * sigData The signature data. |
8324 | | * sigDataSz The length of the signature data. |
8325 | | * check Indicates this is a check not create. |
8326 | | */ |
8327 | | int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz, |
8328 | | int check) |
8329 | 526 | { |
8330 | 526 | word16 idx; |
8331 | 526 | int side = ssl->options.side; |
8332 | 526 | int ret; |
8333 | | |
8334 | | /* Signature Data = Prefix | Label | Handshake Hash */ |
8335 | 526 | XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ); |
8336 | 526 | idx = SIGNING_DATA_PREFIX_SZ; |
8337 | | |
8338 | 526 | if ((side == WOLFSSL_SERVER_END && check) || |
8339 | 526 | (side == WOLFSSL_CLIENT_END && !check)) { |
8340 | 0 | XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ); |
8341 | 0 | } |
8342 | 526 | if ((side == WOLFSSL_CLIENT_END && check) || |
8343 | 526 | (side == WOLFSSL_SERVER_END && !check)) { |
8344 | 526 | XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ); |
8345 | 526 | } |
8346 | 526 | idx += CERT_VFY_LABEL_SZ; |
8347 | | |
8348 | 526 | ret = GetMsgHash(ssl, &sigData[idx]); |
8349 | 526 | if (ret < 0) |
8350 | 5 | return ret; |
8351 | | |
8352 | 521 | *sigDataSz = (word16)(idx + ret); |
8353 | 521 | ret = 0; |
8354 | | |
8355 | 521 | return ret; |
8356 | 526 | } |
8357 | | |
8358 | | #ifndef NO_RSA |
8359 | | /* Encode the PKCS #1.5 RSA signature. |
8360 | | * |
8361 | | * sig The buffer to place the encoded signature into. |
8362 | | * sigData The data to be signed. |
8363 | | * sigDataSz The size of the data to be signed. |
8364 | | * hashAlgo The hash algorithm to use when signing. |
8365 | | * returns the length of the encoded signature or negative on error. |
8366 | | */ |
8367 | | int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz, |
8368 | | int sigAlgo, int hashAlgo) |
8369 | 519 | { |
8370 | 519 | Digest digest; |
8371 | 519 | int hashSz = 0; |
8372 | 519 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8373 | 519 | byte* hash; |
8374 | | |
8375 | 519 | (void)sigAlgo; |
8376 | | |
8377 | 519 | hash = sig; |
8378 | | |
8379 | | /* Digest the signature data. */ |
8380 | 519 | switch (hashAlgo) { |
8381 | 0 | #ifndef NO_SHA256 |
8382 | 354 | case sha256_mac: |
8383 | 354 | ret = wc_InitSha256(&digest.sha256); |
8384 | 354 | if (ret == 0) { |
8385 | 354 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8386 | 354 | if (ret == 0) |
8387 | 353 | ret = wc_Sha256Final(&digest.sha256, hash); |
8388 | 354 | wc_Sha256Free(&digest.sha256); |
8389 | 354 | } |
8390 | 354 | hashSz = WC_SHA256_DIGEST_SIZE; |
8391 | 354 | break; |
8392 | 0 | #endif |
8393 | 0 | #ifdef WOLFSSL_SHA384 |
8394 | 78 | case sha384_mac: |
8395 | 78 | ret = wc_InitSha384(&digest.sha384); |
8396 | 78 | if (ret == 0) { |
8397 | 78 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8398 | 78 | if (ret == 0) |
8399 | 75 | ret = wc_Sha384Final(&digest.sha384, hash); |
8400 | 78 | wc_Sha384Free(&digest.sha384); |
8401 | 78 | } |
8402 | 78 | hashSz = WC_SHA384_DIGEST_SIZE; |
8403 | 78 | break; |
8404 | 0 | #endif |
8405 | 0 | #ifdef WOLFSSL_SHA512 |
8406 | 87 | case sha512_mac: |
8407 | 87 | ret = wc_InitSha512(&digest.sha512); |
8408 | 87 | if (ret == 0) { |
8409 | 87 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8410 | 87 | if (ret == 0) |
8411 | 86 | ret = wc_Sha512Final(&digest.sha512, hash); |
8412 | 87 | wc_Sha512Free(&digest.sha512); |
8413 | 87 | } |
8414 | 87 | hashSz = WC_SHA512_DIGEST_SIZE; |
8415 | 87 | break; |
8416 | 0 | #endif |
8417 | 0 | default: |
8418 | 0 | ret = BAD_FUNC_ARG; |
8419 | 0 | break; |
8420 | | |
8421 | 519 | } |
8422 | | |
8423 | 519 | if (ret != 0) |
8424 | 9 | return ret; |
8425 | | |
8426 | 510 | return hashSz; |
8427 | 519 | } |
8428 | | #endif /* !NO_RSA */ |
8429 | | |
8430 | | #ifdef HAVE_ECC |
8431 | | /* Encode the ECC signature. |
8432 | | * |
8433 | | * sigData The data to be signed. |
8434 | | * sigDataSz The size of the data to be signed. |
8435 | | * hashAlgo The hash algorithm to use when signing. |
8436 | | * returns the length of the encoded signature or negative on error. |
8437 | | */ |
8438 | | static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo) |
8439 | 0 | { |
8440 | 0 | Digest digest; |
8441 | 0 | int hashSz = 0; |
8442 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8443 | | |
8444 | | /* Digest the signature data. */ |
8445 | 0 | switch (hashAlgo) { |
8446 | 0 | #ifndef NO_SHA256 |
8447 | 0 | case sha256_mac: |
8448 | 0 | ret = wc_InitSha256(&digest.sha256); |
8449 | 0 | if (ret == 0) { |
8450 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8451 | 0 | if (ret == 0) |
8452 | 0 | ret = wc_Sha256Final(&digest.sha256, sigData); |
8453 | 0 | wc_Sha256Free(&digest.sha256); |
8454 | 0 | } |
8455 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
8456 | 0 | break; |
8457 | 0 | #endif |
8458 | 0 | #ifdef WOLFSSL_SHA384 |
8459 | 0 | case sha384_mac: |
8460 | 0 | ret = wc_InitSha384(&digest.sha384); |
8461 | 0 | if (ret == 0) { |
8462 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8463 | 0 | if (ret == 0) |
8464 | 0 | ret = wc_Sha384Final(&digest.sha384, sigData); |
8465 | 0 | wc_Sha384Free(&digest.sha384); |
8466 | 0 | } |
8467 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
8468 | 0 | break; |
8469 | 0 | #endif |
8470 | 0 | #ifdef WOLFSSL_SHA512 |
8471 | 0 | case sha512_mac: |
8472 | 0 | ret = wc_InitSha512(&digest.sha512); |
8473 | 0 | if (ret == 0) { |
8474 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8475 | 0 | if (ret == 0) |
8476 | 0 | ret = wc_Sha512Final(&digest.sha512, sigData); |
8477 | 0 | wc_Sha512Free(&digest.sha512); |
8478 | 0 | } |
8479 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
8480 | 0 | break; |
8481 | 0 | #endif |
8482 | 0 | default: |
8483 | 0 | ret = BAD_FUNC_ARG; |
8484 | 0 | break; |
8485 | 0 | } |
8486 | | |
8487 | 0 | if (ret != 0) |
8488 | 0 | return ret; |
8489 | | |
8490 | 0 | return hashSz; |
8491 | 0 | } |
8492 | | #endif /* HAVE_ECC */ |
8493 | | |
8494 | | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
8495 | | /* Check that the decrypted signature matches the encoded signature |
8496 | | * based on the digest of the signature data. |
8497 | | * |
8498 | | * ssl The SSL/TLS object. |
8499 | | * sigAlgo The signature algorithm used to generate signature. |
8500 | | * hashAlgo The hash algorithm used to generate signature. |
8501 | | * decSig The decrypted signature. |
8502 | | * decSigSz The size of the decrypted signature. |
8503 | | * returns 0 on success, otherwise failure. |
8504 | | */ |
8505 | | static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo, |
8506 | | byte* decSig, word32 decSigSz) |
8507 | 0 | { |
8508 | 0 | int ret = 0; |
8509 | 0 | byte sigData[MAX_SIG_DATA_SZ]; |
8510 | 0 | word16 sigDataSz; |
8511 | |
|
8512 | 0 | ret = CreateSigData(ssl, sigData, &sigDataSz, 1); |
8513 | 0 | if (ret != 0) |
8514 | 0 | return ret; |
8515 | | |
8516 | 0 | if (sigAlgo == rsa_pss_sa_algo) { |
8517 | 0 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
8518 | 0 | word32 sigSz; |
8519 | |
|
8520 | 0 | ret = ConvertHashPss(hashAlgo, &hashType, NULL); |
8521 | 0 | if (ret < 0) |
8522 | 0 | return ret; |
8523 | | |
8524 | | /* PSS signature can be done in-place */ |
8525 | 0 | ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz, |
8526 | 0 | sigAlgo, hashAlgo); |
8527 | 0 | if (ret < 0) |
8528 | 0 | return ret; |
8529 | 0 | sigSz = (word32)ret; |
8530 | |
|
8531 | 0 | ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz, |
8532 | 0 | hashType); |
8533 | 0 | } |
8534 | | |
8535 | 0 | return ret; |
8536 | 0 | } |
8537 | | #endif /* !NO_RSA && WC_RSA_PSS */ |
8538 | | #endif /* !NO_RSA || HAVE_ECC */ |
8539 | | |
8540 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
8541 | | /* Get the next certificate from the list for writing into the TLS v1.3 |
8542 | | * Certificate message. |
8543 | | * |
8544 | | * data The certificate list. |
8545 | | * length The length of the certificate data in the list. |
8546 | | * idx The index of the next certificate. |
8547 | | * returns the length of the certificate data. 0 indicates no more certificates |
8548 | | * in the list. |
8549 | | */ |
8550 | | static word32 NextCert(byte* data, word32 length, word32* idx) |
8551 | 0 | { |
8552 | 0 | word32 len; |
8553 | | |
8554 | | /* Is index at end of list. */ |
8555 | 0 | if (*idx == length) |
8556 | 0 | return 0; |
8557 | | |
8558 | | /* Length of the current ASN.1 encoded certificate. */ |
8559 | 0 | c24to32(data + *idx, &len); |
8560 | | /* Include the length field. */ |
8561 | 0 | len += 3; |
8562 | | |
8563 | | /* Move index to next certificate and return the current certificate's |
8564 | | * length. |
8565 | | */ |
8566 | 0 | *idx += len; |
8567 | 0 | return len; |
8568 | 0 | } |
8569 | | |
8570 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8571 | | /* Write certificate status request into certificate to buffer. |
8572 | | * |
8573 | | * ssl SSL/TLS object. |
8574 | | * certExts DerBuffer array. buffers written |
8575 | | * extSz word32 array. |
8576 | | * Length of the certificate status request data for the certificate. |
8577 | | * extSz_num number of the CSR written |
8578 | | * extIdx The index number of certificate status request data |
8579 | | * for the certificate. |
8580 | | * offset index offset |
8581 | | * returns Total number of bytes written. |
8582 | | */ |
8583 | | static int WriteCSRToBuffer(WOLFSSL* ssl, DerBuffer** certExts, |
8584 | | word16* extSz, word16 extSz_num) |
8585 | | { |
8586 | | int ret = 0; |
8587 | | TLSX* ext; |
8588 | | CertificateStatusRequest* csr; |
8589 | | word32 ex_offset = HELLO_EXT_TYPE_SZ + OPAQUE16_LEN /* extension type */ |
8590 | | + OPAQUE16_LEN /* extension length */; |
8591 | | word32 totalSz = 0; |
8592 | | word32 tmpSz; |
8593 | | word32 extIdx; |
8594 | | DerBuffer* der; |
8595 | | |
8596 | | ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
8597 | | csr = ext ? (CertificateStatusRequest*)ext->data : NULL; |
8598 | | |
8599 | | if (csr) { |
8600 | | for (extIdx = 0; extIdx < (word16)(extSz_num); extIdx++) { |
8601 | | tmpSz = TLSX_CSR_GetSize_ex(csr, 0, (int)extIdx); |
8602 | | |
8603 | | if (tmpSz > (OPAQUE8_LEN + OPAQUE24_LEN) && |
8604 | | certExts[extIdx] == NULL) { |
8605 | | /* csr extension is not zero */ |
8606 | | extSz[extIdx] = tmpSz; |
8607 | | |
8608 | | ret = AllocDer(&certExts[extIdx], extSz[extIdx] + ex_offset, |
8609 | | CERT_TYPE, ssl->heap); |
8610 | | if (ret < 0) |
8611 | | return ret; |
8612 | | der = certExts[extIdx]; |
8613 | | |
8614 | | /* write extension type */ |
8615 | | c16toa(ext->type, der->buffer |
8616 | | + OPAQUE16_LEN); |
8617 | | /* writes extension data length. */ |
8618 | | c16toa(extSz[extIdx], der->buffer |
8619 | | + HELLO_EXT_TYPE_SZ + OPAQUE16_LEN); |
8620 | | /* write extension data */ |
8621 | | extSz[extIdx] = (word16)TLSX_CSR_Write_ex(csr, |
8622 | | der->buffer + ex_offset, 0, extIdx); |
8623 | | /* add extension offset */ |
8624 | | extSz[extIdx] += (word16)ex_offset; |
8625 | | /* extension length */ |
8626 | | c16toa(extSz[extIdx] - OPAQUE16_LEN, |
8627 | | der->buffer); |
8628 | | } |
8629 | | totalSz += extSz[extIdx]; |
8630 | | } |
8631 | | } |
8632 | | else { |
8633 | | /* chain cert empty extension size */ |
8634 | | totalSz += OPAQUE16_LEN * extSz_num; |
8635 | | } |
8636 | | return (int)totalSz; |
8637 | | } |
8638 | | #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */ |
8639 | | /* Add certificate data and empty extension to output up to the fragment size. |
8640 | | * |
8641 | | * ssl SSL/TLS object. |
8642 | | * cert The certificate data to write out. |
8643 | | * len The length of the certificate data. |
8644 | | * extSz Length of the extension data with the certificate. |
8645 | | * idx The start of the certificate data to write out. |
8646 | | * fragSz The maximum size of this fragment. |
8647 | | * output The buffer to write to. |
8648 | | * extIdx The index number of the extension data with the certificate |
8649 | | * returns the number of bytes written. |
8650 | | */ |
8651 | | static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz, |
8652 | | word32 idx, word32 fragSz, byte* output, word16 extIdx) |
8653 | 571 | { |
8654 | 571 | word32 i = 0; |
8655 | 571 | word32 copySz = min(len - idx, fragSz); |
8656 | | |
8657 | 571 | if (idx < len) { |
8658 | 571 | XMEMCPY(output, cert + idx, copySz); |
8659 | 571 | i = copySz; |
8660 | 571 | if (copySz == fragSz) |
8661 | 18 | return i; |
8662 | 571 | } |
8663 | 553 | copySz = len + extSz - idx - i; |
8664 | | |
8665 | 553 | if (extSz == OPAQUE16_LEN) { |
8666 | 553 | if (copySz <= fragSz) { |
8667 | | /* Empty extension */ |
8668 | 553 | output[i++] = 0; |
8669 | 553 | output[i++] = 0; |
8670 | 553 | } |
8671 | 553 | } |
8672 | 0 | else { |
8673 | 0 | byte* certExts = ssl->buffers.certExts[extIdx]->buffer + idx + i - len; |
8674 | | /* Put out as much of the extensions' data as will fit in fragment. */ |
8675 | 0 | if (copySz > fragSz - i) |
8676 | 0 | copySz = fragSz - i; |
8677 | 0 | XMEMCPY(output + i, certExts, copySz); |
8678 | 0 | i += copySz; |
8679 | 0 | } |
8680 | | |
8681 | 553 | return i; |
8682 | 571 | } |
8683 | | |
8684 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8685 | | static int SetupOcspResp(WOLFSSL* ssl) |
8686 | | { |
8687 | | DecodedCert* cert = NULL; |
8688 | | CertificateStatusRequest* csr = NULL; |
8689 | | TLSX* extension = NULL; |
8690 | | int ret = 0; |
8691 | | OcspRequest* request = NULL; |
8692 | | |
8693 | | extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
8694 | | if (extension == NULL) |
8695 | | return 0; /* peer didn't signal ocsp support */ |
8696 | | csr = (CertificateStatusRequest*)extension->data; |
8697 | | if (csr == NULL) |
8698 | | return MEMORY_ERROR; |
8699 | | |
8700 | | if (SSL_CM(ssl) != NULL && |
8701 | | SSL_CM(ssl)->ocsp_stapling != NULL && |
8702 | | SSL_CM(ssl)->ocsp_stapling->statusCb != NULL) { |
8703 | | return TLSX_CSR_SetResponseWithStatusCB(ssl); |
8704 | | } |
8705 | | |
8706 | | if (ssl->buffers.certificate == NULL) { |
8707 | | WOLFSSL_MSG("Certificate buffer not set!"); |
8708 | | return BUFFER_ERROR; |
8709 | | } |
8710 | | cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap, |
8711 | | DYNAMIC_TYPE_DCERT); |
8712 | | if (cert == NULL) { |
8713 | | return MEMORY_E; |
8714 | | } |
8715 | | InitDecodedCert(cert, ssl->buffers.certificate->buffer, |
8716 | | ssl->buffers.certificate->length, ssl->heap); |
8717 | | ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, SSL_CM(ssl)); |
8718 | | if (ret != 0) { |
8719 | | FreeDecodedCert(cert); |
8720 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
8721 | | return ret; |
8722 | | } |
8723 | | ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap); |
8724 | | FreeDecodedCert(cert); |
8725 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
8726 | | if (ret != 0 ) |
8727 | | return ret; |
8728 | | |
8729 | | request = &csr->request.ocsp[0]; |
8730 | | ret = CreateOcspResponse(ssl, &request, &csr->responses[0]); |
8731 | | if (request != &csr->request.ocsp[0] && |
8732 | | ssl->buffers.weOwnCert) { |
8733 | | /* request will be allocated in CreateOcspResponse() */ |
8734 | | FreeOcspRequest(request); |
8735 | | XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
8736 | | } |
8737 | | if (ret != 0) |
8738 | | return ret; |
8739 | | |
8740 | | if (csr->responses[0].buffer) |
8741 | | extension->resp = 1; |
8742 | | #if defined(WOLFSSL_TLS_OCSP_MULTI) |
8743 | | /* process OCSP request in certificate chain */ |
8744 | | if ((ret = ProcessChainOCSPRequest(ssl)) != 0) { |
8745 | | WOLFSSL_MSG("Process Cert Chain OCSP request failed"); |
8746 | | WOLFSSL_ERROR_VERBOSE(ret); |
8747 | | return ret; |
8748 | | } |
8749 | | #endif |
8750 | | return ret; |
8751 | | } |
8752 | | #endif |
8753 | | |
8754 | | /* handle generation TLS v1.3 certificate (11) */ |
8755 | | /* Send the certificate for this end and any CAs that help with validation. |
8756 | | * This message is always encrypted in TLS v1.3. |
8757 | | * |
8758 | | * ssl The SSL/TLS object. |
8759 | | * returns 0 on success, otherwise failure. |
8760 | | */ |
8761 | | static int SendTls13Certificate(WOLFSSL* ssl) |
8762 | | { |
8763 | | int ret = 0; |
8764 | | word32 certSz, certChainSz, headerSz, listSz, payloadSz; |
8765 | | word16 extSz[MAX_CERT_EXTENSIONS]; |
8766 | | word16 extIdx = 0; |
8767 | | word32 maxFragment; |
8768 | | word32 totalextSz = 0; |
8769 | | word32 len = 0; |
8770 | | word32 idx = 0; |
8771 | | word32 offset = OPAQUE16_LEN; |
8772 | | byte* p = NULL; |
8773 | | byte certReqCtxLen = 0; |
8774 | | sword32 length; |
8775 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8776 | | byte* certReqCtx = NULL; |
8777 | | #endif |
8778 | | |
8779 | | #ifdef OPENSSL_EXTRA |
8780 | | WOLFSSL_X509* x509 = NULL; |
8781 | | WOLFSSL_EVP_PKEY* pkey = NULL; |
8782 | | #endif |
8783 | | |
8784 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND); |
8785 | | WOLFSSL_ENTER("SendTls13Certificate"); |
8786 | | |
8787 | | XMEMSET(extSz, 0, sizeof(extSz)); |
8788 | | |
8789 | | ssl->options.buildingMsg = 1; |
8790 | | |
8791 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8792 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
8793 | | certReqCtxLen = ssl->certReqCtx->len; |
8794 | | certReqCtx = &ssl->certReqCtx->ctx; |
8795 | | } |
8796 | | #endif |
8797 | | |
8798 | | #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_SETUP_CB) |
8799 | | /* call client cert callback if no cert has been loaded */ |
8800 | | if ((ssl->ctx->CBClientCert != NULL) && |
8801 | | (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) { |
8802 | | ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey); |
8803 | | if (ret == 1) { |
8804 | | if ((wolfSSL_CTX_use_certificate(ssl->ctx, x509) == WOLFSSL_SUCCESS) && |
8805 | | (wolfSSL_CTX_use_PrivateKey(ssl->ctx, pkey) == WOLFSSL_SUCCESS)) { |
8806 | | ssl->options.sendVerify = SEND_CERT; |
8807 | | } |
8808 | | wolfSSL_X509_free(x509); |
8809 | | x509 = NULL; |
8810 | | wolfSSL_EVP_PKEY_free(pkey); |
8811 | | } |
8812 | | } |
8813 | | #endif |
8814 | | |
8815 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
8816 | | certSz = 0; |
8817 | | certChainSz = 0; |
8818 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ; |
8819 | | length = (sword32)headerSz; |
8820 | | listSz = 0; |
8821 | | } |
8822 | | else { |
8823 | | if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) { |
8824 | | WOLFSSL_MSG("Send Cert missing certificate buffer"); |
8825 | | return NO_CERT_ERROR; |
8826 | | } |
8827 | | /* Certificate Data */ |
8828 | | certSz = ssl->buffers.certificate->length; |
8829 | | /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */ |
8830 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ + |
8831 | | CERT_HEADER_SZ; |
8832 | | /* set empty extension as default */ |
8833 | | for (extIdx = 0; extIdx < (word16)XELEM_CNT(extSz); extIdx++) |
8834 | | extSz[extIdx] = OPAQUE16_LEN; |
8835 | | |
8836 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8837 | | /* We only send CSR on the server side. On client side, the CSR data |
8838 | | * is populated with the server response. We would be sending the server |
8839 | | * its own stapling data. */ |
8840 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
8841 | | ret = SetupOcspResp(ssl); |
8842 | | if (ret != 0) |
8843 | | return ret; |
8844 | | |
8845 | | ret = WriteCSRToBuffer(ssl, &ssl->buffers.certExts[0], &extSz[0], |
8846 | | 1 /* +1 for leaf */ + ssl->buffers.certChainCnt); |
8847 | | if (ret < 0) |
8848 | | return ret; |
8849 | | totalextSz += ret; |
8850 | | ret = 0; /* Clear to signal no error */ |
8851 | | } |
8852 | | else |
8853 | | #endif |
8854 | | { |
8855 | | /* Leaf cert empty extension size */ |
8856 | | totalextSz += OPAQUE16_LEN; |
8857 | | /* chain cert empty extension size */ |
8858 | | totalextSz += OPAQUE16_LEN * ssl->buffers.certChainCnt; |
8859 | | } |
8860 | | |
8861 | | /* Length of message data with one certificate and extensions. */ |
8862 | | length = (sword32)(headerSz + certSz + totalextSz); |
8863 | | /* Length of list data with one certificate and extensions. */ |
8864 | | listSz = CERT_HEADER_SZ + certSz + totalextSz; |
8865 | | |
8866 | | /* Send rest of chain if sending cert (chain has leading size/s). */ |
8867 | | if (certSz > 0 && ssl->buffers.certChainCnt > 0) { |
8868 | | p = ssl->buffers.certChain->buffer; |
8869 | | /* Chain length including extensions. */ |
8870 | | certChainSz = ssl->buffers.certChain->length; |
8871 | | |
8872 | | length += certChainSz; |
8873 | | listSz += certChainSz; |
8874 | | } |
8875 | | else |
8876 | | certChainSz = 0; |
8877 | | } |
8878 | | |
8879 | | payloadSz = (word32)length; |
8880 | | |
8881 | | if (ssl->fragOffset != 0) |
8882 | | length -= (ssl->fragOffset + headerSz); |
8883 | | |
8884 | | maxFragment = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
8885 | | |
8886 | | extIdx = 0; |
8887 | | |
8888 | | while (length > 0 && ret == 0) { |
8889 | | byte* output = NULL; |
8890 | | word32 fragSz = 0; |
8891 | | word32 i = RECORD_HEADER_SZ; |
8892 | | int sendSz = RECORD_HEADER_SZ; |
8893 | | |
8894 | | #ifdef WOLFSSL_DTLS13 |
8895 | | if (ssl->options.dtls) { |
8896 | | i = Dtls13GetRlHeaderLength(ssl, 1); |
8897 | | sendSz = (int)i; |
8898 | | } |
8899 | | #endif /* WOLFSSL_DTLS13 */ |
8900 | | |
8901 | | if (ssl->fragOffset == 0) { |
8902 | | if (headerSz + certSz + totalextSz + certChainSz <= |
8903 | | maxFragment - HANDSHAKE_HEADER_SZ) { |
8904 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
8905 | | } |
8906 | | #ifdef WOLFSSL_DTLS13 |
8907 | | else if (ssl->options.dtls){ |
8908 | | /* short-circuit the fragmentation logic here. DTLS |
8909 | | fragmentation will be done in dtls13HandshakeSend() */ |
8910 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
8911 | | } |
8912 | | #endif /* WOLFSSL_DTLS13 */ |
8913 | | else { |
8914 | | fragSz = maxFragment - HANDSHAKE_HEADER_SZ; |
8915 | | } |
8916 | | |
8917 | | sendSz += fragSz + HANDSHAKE_HEADER_SZ; |
8918 | | i += HANDSHAKE_HEADER_SZ; |
8919 | | #ifdef WOLFSSL_DTLS13 |
8920 | | if (ssl->options.dtls) { |
8921 | | sendSz += DTLS_HANDSHAKE_EXTRA; |
8922 | | i += DTLS_HANDSHAKE_EXTRA; |
8923 | | } |
8924 | | #endif /* WOLFSSL_DTLS13 */ |
8925 | | } |
8926 | | else { |
8927 | | fragSz = min((word32)length, maxFragment); |
8928 | | sendSz += fragSz; |
8929 | | } |
8930 | | |
8931 | | sendSz += MAX_MSG_EXTRA; |
8932 | | |
8933 | | /* Check buffers are big enough and grow if needed. */ |
8934 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8935 | | return ret; |
8936 | | |
8937 | | /* Get position in output buffer to write new message to. */ |
8938 | | output = GetOutputBuffer(ssl); |
8939 | | |
8940 | | if (ssl->fragOffset == 0) { |
8941 | | AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl); |
8942 | | |
8943 | | /* Request context. */ |
8944 | | output[i++] = certReqCtxLen; |
8945 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8946 | | if (certReqCtxLen > 0) { |
8947 | | XMEMCPY(output + i, certReqCtx, certReqCtxLen); |
8948 | | i += certReqCtxLen; |
8949 | | } |
8950 | | #endif |
8951 | | length -= OPAQUE8_LEN + certReqCtxLen; |
8952 | | fragSz -= OPAQUE8_LEN + certReqCtxLen; |
8953 | | /* Certificate list length. */ |
8954 | | c32to24(listSz, output + i); |
8955 | | i += CERT_HEADER_SZ; |
8956 | | length -= CERT_HEADER_SZ; |
8957 | | fragSz -= CERT_HEADER_SZ; |
8958 | | /* Leaf certificate data length. */ |
8959 | | if (certSz > 0) { |
8960 | | c32to24(certSz, output + i); |
8961 | | i += CERT_HEADER_SZ; |
8962 | | length -= CERT_HEADER_SZ; |
8963 | | fragSz -= CERT_HEADER_SZ; |
8964 | | } |
8965 | | } |
8966 | | else |
8967 | | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
8968 | | |
8969 | | if (extIdx == 0) { |
8970 | | if (certSz > 0 && ssl->fragOffset < certSz + extSz[0]) { |
8971 | | /* Put in the leaf certificate with extensions. */ |
8972 | | word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer, |
8973 | | certSz, extSz[0], ssl->fragOffset, fragSz, |
8974 | | output + i, 0); |
8975 | | i += copySz; |
8976 | | ssl->fragOffset += copySz; |
8977 | | length -= copySz; |
8978 | | fragSz -= copySz; |
8979 | | if (ssl->fragOffset == certSz + extSz[0]) |
8980 | | FreeDer(&ssl->buffers.certExts[0]); |
8981 | | } |
8982 | | } |
8983 | | if (certChainSz > 0 && fragSz > 0) { |
8984 | | /* Put in the CA certificates with extensions. */ |
8985 | | while (fragSz > 0) { |
8986 | | word32 l; |
8987 | | |
8988 | | if (offset == len + OPAQUE16_LEN) { |
8989 | | /* Find next CA certificate to write out. */ |
8990 | | offset = 0; |
8991 | | /* Point to the start of current cert in chain buffer. */ |
8992 | | p = ssl->buffers.certChain->buffer + idx; |
8993 | | len = NextCert(ssl->buffers.certChain->buffer, |
8994 | | ssl->buffers.certChain->length, &idx); |
8995 | | if (len == 0) |
8996 | | break; |
8997 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
8998 | | !defined(NO_WOLFSSL_SERVER) |
8999 | | if (MAX_CERT_EXTENSIONS > extIdx) |
9000 | | extIdx++; |
9001 | | #endif |
9002 | | } |
9003 | | /* Write out certificate and extension. */ |
9004 | | l = AddCertExt(ssl, p, len, extSz[extIdx], offset, fragSz, |
9005 | | output + i, extIdx); |
9006 | | i += l; |
9007 | | ssl->fragOffset += l; |
9008 | | length -= l; |
9009 | | fragSz -= l; |
9010 | | offset += l; |
9011 | | |
9012 | | if (extIdx != 0 && extIdx < MAX_CERT_EXTENSIONS && |
9013 | | ssl->buffers.certExts[extIdx] != NULL && |
9014 | | offset == len + extSz[extIdx]) |
9015 | | FreeDer(&ssl->buffers.certExts[extIdx]); |
9016 | | /* for next chain cert */ |
9017 | | len += extSz[extIdx] - OPAQUE16_LEN; |
9018 | | } |
9019 | | } |
9020 | | |
9021 | | if ((int)i - RECORD_HEADER_SZ < 0) { |
9022 | | WOLFSSL_MSG("Send Cert bad inputSz"); |
9023 | | return BUFFER_E; |
9024 | | } |
9025 | | |
9026 | | #ifdef WOLFSSL_DTLS13 |
9027 | | if (ssl->options.dtls) { |
9028 | | /* DTLS1.3 uses a separate variable and logic for fragments */ |
9029 | | ssl->options.buildingMsg = 0; |
9030 | | ssl->fragOffset = 0; |
9031 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
9032 | | certificate, 1); |
9033 | | } |
9034 | | else |
9035 | | #endif /* WOLFSSL_DTLS13 */ |
9036 | | { |
9037 | | /* This message is always encrypted. */ |
9038 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
9039 | | output + RECORD_HEADER_SZ, (int)(i - RECORD_HEADER_SZ), |
9040 | | handshake, 1, |
9041 | | 0, 0); |
9042 | | if (sendSz < 0) |
9043 | | return sendSz; |
9044 | | |
9045 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
9046 | | if (ssl->hsInfoOn) |
9047 | | AddPacketName(ssl, "Certificate"); |
9048 | | if (ssl->toInfoOn) { |
9049 | | ret = AddPacketInfo(ssl, "Certificate", handshake, output, |
9050 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
9051 | | if (ret != 0) |
9052 | | return ret; |
9053 | | } |
9054 | | #endif |
9055 | | |
9056 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
9057 | | ssl->options.buildingMsg = 0; |
9058 | | if (!ssl->options.groupMessages) |
9059 | | ret = SendBuffered(ssl); |
9060 | | } |
9061 | | } |
9062 | | |
9063 | | if (ret != WC_NO_ERR_TRACE(WANT_WRITE)) { |
9064 | | /* Clean up the fragment offset. */ |
9065 | | ssl->options.buildingMsg = 0; |
9066 | | ssl->fragOffset = 0; |
9067 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
9068 | | ssl->options.serverState = SERVER_CERT_COMPLETE; |
9069 | | } |
9070 | | |
9071 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9072 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9073 | | CertReqCtx* ctx = ssl->certReqCtx; |
9074 | | ssl->certReqCtx = ssl->certReqCtx->next; |
9075 | | XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
9076 | | } |
9077 | | #endif |
9078 | | |
9079 | | WOLFSSL_LEAVE("SendTls13Certificate", ret); |
9080 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND); |
9081 | | |
9082 | | return ret; |
9083 | | } |
9084 | | |
9085 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
9086 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
9087 | | defined(HAVE_DILITHIUM)) && \ |
9088 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
9089 | | typedef struct Scv13Args { |
9090 | | byte* output; /* not allocated */ |
9091 | | byte* verify; /* not allocated */ |
9092 | | word32 idx; |
9093 | | word32 sigLen; |
9094 | | int sendSz; |
9095 | | word16 length; |
9096 | | |
9097 | | byte sigAlgo; |
9098 | | byte* sigData; |
9099 | | word16 sigDataSz; |
9100 | | #ifndef NO_RSA |
9101 | | byte* toSign; /* not allocated */ |
9102 | | word32 toSignSz; |
9103 | | #endif |
9104 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9105 | | byte altSigAlgo; |
9106 | | word32 altSigLen; /* Only used in the case of both native and alt. */ |
9107 | | byte* altSigData; |
9108 | | word16 altSigDataSz; |
9109 | | #endif |
9110 | | } Scv13Args; |
9111 | | |
9112 | | static void FreeScv13Args(WOLFSSL* ssl, void* pArgs) |
9113 | 535 | { |
9114 | 535 | Scv13Args* args = (Scv13Args*)pArgs; |
9115 | | |
9116 | 535 | (void)ssl; |
9117 | | |
9118 | 535 | if (args && args->sigData) { |
9119 | 526 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9120 | 526 | args->sigData = NULL; |
9121 | 526 | } |
9122 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9123 | | if (args && args->altSigData != NULL) { |
9124 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9125 | | args->altSigData = NULL; |
9126 | | } |
9127 | | #endif |
9128 | 535 | } |
9129 | | |
9130 | | /* handle generation TLS v1.3 certificate_verify (15) */ |
9131 | | /* Send the TLS v1.3 CertificateVerify message. |
9132 | | * A hash of all the message so far is used. |
9133 | | * The signed data is: |
9134 | | * 0x20 * 64 | context string | 0x00 | hash of messages |
9135 | | * This message is always encrypted in TLS v1.3. |
9136 | | * |
9137 | | * ssl The SSL/TLS object. |
9138 | | * returns 0 on success, otherwise failure. |
9139 | | */ |
9140 | | static int SendTls13CertificateVerify(WOLFSSL* ssl) |
9141 | | { |
9142 | | int ret = 0; |
9143 | | #ifndef NO_RSA |
9144 | | /* Use this as a temporary buffer for RSA signature verification. */ |
9145 | | buffer* rsaSigBuf = &ssl->buffers.sig; |
9146 | | #endif |
9147 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9148 | | Scv13Args* args = NULL; |
9149 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
9150 | | #else |
9151 | | Scv13Args args[1]; |
9152 | | #endif |
9153 | | |
9154 | | #ifdef WOLFSSL_DTLS13 |
9155 | | int recordLayerHdrExtra; |
9156 | | #endif /* WOLFSSL_DTLS13 */ |
9157 | | |
9158 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9159 | | WOLFSSL_ENTER("SendTls13CertificateVerify"); |
9160 | | |
9161 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9162 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9163 | | #endif |
9164 | | |
9165 | | ssl->options.buildingMsg = 1; |
9166 | | |
9167 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
9168 | | ret = tsip_Tls13SendCertVerify(ssl); |
9169 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
9170 | | goto exit_scv; |
9171 | | } |
9172 | | ret = 0; |
9173 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
9174 | | |
9175 | | #ifdef WOLFSSL_DTLS13 |
9176 | | /* can be negative */ |
9177 | | if (ssl->options.dtls) |
9178 | | recordLayerHdrExtra = Dtls13GetRlHeaderLength(ssl, 1) - RECORD_HEADER_SZ; |
9179 | | else |
9180 | | recordLayerHdrExtra = 0; |
9181 | | |
9182 | | #endif /* WOLFSSL_DTLS13 */ |
9183 | | |
9184 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9185 | | if (ssl->async == NULL) { |
9186 | | ssl->async = (struct WOLFSSL_ASYNC*) |
9187 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
9188 | | DYNAMIC_TYPE_ASYNC); |
9189 | | if (ssl->async == NULL) |
9190 | | ERROR_OUT(MEMORY_E, exit_scv); |
9191 | | } |
9192 | | args = (Scv13Args*)ssl->async->args; |
9193 | | |
9194 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
9195 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
9196 | | /* Check for error */ |
9197 | | if (ret < 0) |
9198 | | goto exit_scv; |
9199 | | } |
9200 | | else |
9201 | | #endif |
9202 | | { |
9203 | | /* Reset state */ |
9204 | | ret = 0; |
9205 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
9206 | | XMEMSET(args, 0, sizeof(Scv13Args)); |
9207 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9208 | | ssl->async->freeArgs = FreeScv13Args; |
9209 | | #endif |
9210 | | } |
9211 | | |
9212 | | switch(ssl->options.asyncState) |
9213 | | { |
9214 | | case TLS_ASYNC_BEGIN: |
9215 | | { |
9216 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9217 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9218 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, |
9219 | | ssl->buffers.keyMask); |
9220 | | #endif |
9221 | | return 0; /* sent blank cert, can't verify */ |
9222 | | } |
9223 | | |
9224 | | args->sendSz = WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA; |
9225 | | /* Always encrypted. */ |
9226 | | args->sendSz += MAX_MSG_EXTRA; |
9227 | | |
9228 | | /* check for available size */ |
9229 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) { |
9230 | | goto exit_scv; |
9231 | | } |
9232 | | |
9233 | | /* get output buffer */ |
9234 | | args->output = GetOutputBuffer(ssl); |
9235 | | |
9236 | | /* Advance state and proceed */ |
9237 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
9238 | | } /* case TLS_ASYNC_BEGIN */ |
9239 | | FALL_THROUGH; |
9240 | | |
9241 | | case TLS_ASYNC_BUILD: |
9242 | | { |
9243 | | int rem = (int)(ssl->buffers.outputBuffer.bufferSize |
9244 | | - ssl->buffers.outputBuffer.length |
9245 | | - RECORD_HEADER_SZ - HANDSHAKE_HEADER_SZ); |
9246 | | |
9247 | | /* idx is used to track verify pointer offset to output */ |
9248 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
9249 | | args->verify = |
9250 | | &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ]; |
9251 | | |
9252 | | #ifdef WOLFSSL_DTLS13 |
9253 | | if (ssl->options.dtls) { |
9254 | | rem -= recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9255 | | args->idx += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9256 | | args->verify += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9257 | | } |
9258 | | #endif /* WOLFSSL_DTLS13 */ |
9259 | | |
9260 | | if (ssl->buffers.key == NULL) { |
9261 | | #ifdef HAVE_PK_CALLBACKS |
9262 | | if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) |
9263 | | args->sigLen = (word16)GetPrivateKeySigSize(ssl); |
9264 | | else |
9265 | | #endif |
9266 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9267 | | } |
9268 | | else { |
9269 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9270 | | if (ssl->sigSpec != NULL && |
9271 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
9272 | | /* In the case of alternative, we swap in the alt. */ |
9273 | | if (ssl->buffers.altKey == NULL) { |
9274 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9275 | | } |
9276 | | ssl->buffers.keyType = ssl->buffers.altKeyType; |
9277 | | ssl->buffers.keySz = ssl->buffers.altKeySz; |
9278 | | /* If we own it, free key before overriding it. */ |
9279 | | if (ssl->buffers.weOwnKey) { |
9280 | | FreeDer(&ssl->buffers.key); |
9281 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9282 | | FreeDer(&ssl->buffers.keyMask); |
9283 | | #endif |
9284 | | } |
9285 | | |
9286 | | /* Swap keys */ |
9287 | | ssl->buffers.key = ssl->buffers.altKey; |
9288 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9289 | | ssl->buffers.keyMask = ssl->buffers.altKeyMask; |
9290 | | #endif |
9291 | | ssl->buffers.weOwnKey = ssl->buffers.weOwnAltKey; |
9292 | | } |
9293 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9294 | | ret = DecodePrivateKey(ssl, &args->sigLen); |
9295 | | if (ret != 0) |
9296 | | goto exit_scv; |
9297 | | } |
9298 | | |
9299 | | if (rem < 0 || (int)args->sigLen > rem) { |
9300 | | ERROR_OUT(BUFFER_E, exit_scv); |
9301 | | } |
9302 | | |
9303 | | if (args->sigLen == 0) { |
9304 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9305 | | } |
9306 | | |
9307 | | /* Add signature algorithm. */ |
9308 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) |
9309 | | args->sigAlgo = rsa_pss_sa_algo; |
9310 | | #ifdef HAVE_ECC |
9311 | | else if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9312 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9313 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9314 | | args->sigAlgo = sm2_sa_algo; |
9315 | | } |
9316 | | else |
9317 | | #endif |
9318 | | { |
9319 | | args->sigAlgo = ecc_dsa_sa_algo; |
9320 | | } |
9321 | | } |
9322 | | #endif |
9323 | | #ifdef HAVE_ED25519 |
9324 | | else if (ssl->hsType == DYNAMIC_TYPE_ED25519) |
9325 | | args->sigAlgo = ed25519_sa_algo; |
9326 | | #endif |
9327 | | #ifdef HAVE_ED448 |
9328 | | else if (ssl->hsType == DYNAMIC_TYPE_ED448) |
9329 | | args->sigAlgo = ed448_sa_algo; |
9330 | | #endif |
9331 | | #if defined(HAVE_FALCON) |
9332 | | else if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9333 | | args->sigAlgo = ssl->buffers.keyType; |
9334 | | } |
9335 | | #endif /* HAVE_FALCON */ |
9336 | | #if defined(HAVE_DILITHIUM) |
9337 | | else if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9338 | | args->sigAlgo = ssl->buffers.keyType; |
9339 | | } |
9340 | | #endif /* HAVE_DILITHIUM */ |
9341 | | else { |
9342 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9343 | | } |
9344 | | |
9345 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9346 | | if (ssl->peerSigSpec == NULL) { |
9347 | | /* The peer did not respond. We didn't send CKS or they don't |
9348 | | * support it. Either way, we do not need to handle dual |
9349 | | * key/sig case. */ |
9350 | | ssl->sigSpec = NULL; |
9351 | | ssl->sigSpecSz = 0; |
9352 | | } |
9353 | | |
9354 | | if (ssl->sigSpec != NULL && |
9355 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9356 | | /* The native was already decoded. Now we need to do the |
9357 | | * alternative. Note that no swap was done because this case is |
9358 | | * both native and alternative, not just alternative. */ |
9359 | | if (ssl->ctx->altPrivateKey == NULL) { |
9360 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9361 | | } |
9362 | | |
9363 | | /* After this call, args->altSigLen has the length we need for |
9364 | | * the alternative signature. */ |
9365 | | ret = DecodeAltPrivateKey(ssl, &args->altSigLen); |
9366 | | if (ret != 0) |
9367 | | goto exit_scv; |
9368 | | |
9369 | | if (ssl->buffers.altKeyType == ecc_dsa_sa_algo || |
9370 | | ssl->buffers.altKeyType == falcon_level1_sa_algo || |
9371 | | ssl->buffers.altKeyType == falcon_level5_sa_algo || |
9372 | | ssl->buffers.altKeyType == dilithium_level2_sa_algo || |
9373 | | ssl->buffers.altKeyType == dilithium_level3_sa_algo || |
9374 | | ssl->buffers.altKeyType == dilithium_level5_sa_algo) { |
9375 | | args->altSigAlgo = ssl->buffers.altKeyType; |
9376 | | } |
9377 | | else if (ssl->buffers.altKeyType == rsa_sa_algo && |
9378 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9379 | | args->altSigAlgo = rsa_pss_sa_algo; |
9380 | | } |
9381 | | else { |
9382 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9383 | | } |
9384 | | |
9385 | | EncodeDualSigAlg(args->sigAlgo, args->altSigAlgo, args->verify); |
9386 | | if (args->verify[0] == 0) { |
9387 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9388 | | } |
9389 | | } |
9390 | | else |
9391 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9392 | | EncodeSigAlg(ssl, ssl->options.hashAlgo, args->sigAlgo, |
9393 | | args->verify); |
9394 | | |
9395 | | if (args->sigData == NULL) { |
9396 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9397 | | if ((ssl->hsType == DYNAMIC_TYPE_RSA) && |
9398 | | (args->sigLen > MAX_SIG_DATA_SZ)) { |
9399 | | /* We store the RSA signature in the sigData buffer |
9400 | | * temporarily, hence its size must be fitting. */ |
9401 | | sigLen = args->sigLen; |
9402 | | } |
9403 | | args->sigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9404 | | DYNAMIC_TYPE_SIGNATURE); |
9405 | | if (args->sigData == NULL) { |
9406 | | ERROR_OUT(MEMORY_E, exit_scv); |
9407 | | } |
9408 | | } |
9409 | | |
9410 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9411 | | if ((ssl->sigSpec != NULL) && |
9412 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
9413 | | (args->altSigData == NULL)) { |
9414 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9415 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA && |
9416 | | args->altSigLen > MAX_SIG_DATA_SZ) { |
9417 | | /* We store the RSA signature in the sigData buffer |
9418 | | * temporarily, hence its size must be fitting. */ |
9419 | | sigLen = args->altSigLen; |
9420 | | } |
9421 | | args->altSigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9422 | | DYNAMIC_TYPE_SIGNATURE); |
9423 | | if (args->altSigData == NULL) { |
9424 | | ERROR_OUT(MEMORY_E, exit_scv); |
9425 | | } |
9426 | | } |
9427 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9428 | | |
9429 | | /* Create the data to be signed. */ |
9430 | | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0); |
9431 | | if (ret != 0) |
9432 | | goto exit_scv; |
9433 | | |
9434 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9435 | | if ((ssl->sigSpec != NULL) && |
9436 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
9437 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
9438 | | args->altSigDataSz = args->sigDataSz; |
9439 | | } |
9440 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9441 | | |
9442 | | #ifndef NO_RSA |
9443 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9444 | | /* build encoded signature buffer */ |
9445 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9446 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, ssl->heap, |
9447 | | DYNAMIC_TYPE_SIGNATURE); |
9448 | | if (rsaSigBuf->buffer == NULL) { |
9449 | | ERROR_OUT(MEMORY_E, exit_scv); |
9450 | | } |
9451 | | |
9452 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, args->sigData, |
9453 | | args->sigDataSz, args->sigAlgo, ssl->options.hashAlgo); |
9454 | | if (ret < 0) |
9455 | | goto exit_scv; |
9456 | | rsaSigBuf->length = (unsigned int)ret; |
9457 | | ret = 0; |
9458 | | } |
9459 | | #endif /* !NO_RSA */ |
9460 | | #ifdef HAVE_ECC |
9461 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9462 | | args->sigLen = (word32)args->sendSz - args->idx - |
9463 | | HASH_SIG_SIZE - |
9464 | | VERIFY_HEADER; |
9465 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9466 | | if (ssl->buffers.keyType != sm2_sa_algo) |
9467 | | #endif |
9468 | | { |
9469 | | ret = CreateECCEncodedSig(args->sigData, |
9470 | | args->sigDataSz, ssl->options.hashAlgo); |
9471 | | if (ret < 0) |
9472 | | goto exit_scv; |
9473 | | args->sigDataSz = (word16)ret; |
9474 | | ret = 0; |
9475 | | } |
9476 | | } |
9477 | | #endif /* HAVE_ECC */ |
9478 | | #ifdef HAVE_ED25519 |
9479 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9480 | | ret = Ed25519CheckPubKey(ssl); |
9481 | | if (ret < 0) { |
9482 | | ERROR_OUT(ret, exit_scv); |
9483 | | } |
9484 | | args->sigLen = ED25519_SIG_SIZE; |
9485 | | } |
9486 | | #endif /* HAVE_ED25519 */ |
9487 | | #ifdef HAVE_ED448 |
9488 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9489 | | ret = Ed448CheckPubKey(ssl); |
9490 | | if (ret < 0) { |
9491 | | ERROR_OUT(ret, exit_scv); |
9492 | | } |
9493 | | args->sigLen = ED448_SIG_SIZE; |
9494 | | } |
9495 | | |
9496 | | #endif /* HAVE_ED448 */ |
9497 | | #if defined(HAVE_FALCON) |
9498 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9499 | | args->sigLen = FALCON_MAX_SIG_SIZE; |
9500 | | } |
9501 | | #endif /* HAVE_FALCON */ |
9502 | | #if defined(HAVE_DILITHIUM) |
9503 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9504 | | args->sigLen = DILITHIUM_MAX_SIG_SIZE; |
9505 | | } |
9506 | | #endif /* HAVE_DILITHIUM */ |
9507 | | |
9508 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9509 | | if (ssl->sigSpec != NULL && |
9510 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9511 | | |
9512 | | #ifndef NO_RSA |
9513 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9514 | | /* build encoded signature buffer */ |
9515 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9516 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, |
9517 | | ssl->heap, |
9518 | | DYNAMIC_TYPE_SIGNATURE); |
9519 | | if (rsaSigBuf->buffer == NULL) { |
9520 | | ERROR_OUT(MEMORY_E, exit_scv); |
9521 | | } |
9522 | | |
9523 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, |
9524 | | args->altSigData, args->altSigDataSz, |
9525 | | args->altSigAlgo, ssl->options.hashAlgo); |
9526 | | if (ret < 0) |
9527 | | goto exit_scv; |
9528 | | rsaSigBuf->length = ret; |
9529 | | ret = 0; |
9530 | | } |
9531 | | #endif /* !NO_RSA */ |
9532 | | #ifdef HAVE_ECC |
9533 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9534 | | ret = CreateECCEncodedSig(args->altSigData, |
9535 | | args->altSigDataSz, ssl->options.hashAlgo); |
9536 | | if (ret < 0) |
9537 | | goto exit_scv; |
9538 | | args->altSigDataSz = (word16)ret; |
9539 | | ret = 0; |
9540 | | } |
9541 | | #endif /* HAVE_ECC */ |
9542 | | } |
9543 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9544 | | |
9545 | | /* Advance state and proceed */ |
9546 | | ssl->options.asyncState = TLS_ASYNC_DO; |
9547 | | } /* case TLS_ASYNC_BUILD */ |
9548 | | FALL_THROUGH; |
9549 | | |
9550 | | case TLS_ASYNC_DO: |
9551 | | { |
9552 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
9553 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9554 | | if (ssl->sigSpec != NULL && |
9555 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9556 | | /* As we have two signatures in the message, we store |
9557 | | * the length of each before the actual signature. This |
9558 | | * is necessary, as we could have two algorithms with |
9559 | | * variable length signatures. */ |
9560 | | sigOut += OPAQUE16_LEN; |
9561 | | } |
9562 | | #endif |
9563 | | #ifdef HAVE_ECC |
9564 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9565 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9566 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9567 | | ret = Sm2wSm3Sign(ssl, TLS13_SM2_SIG_ID, |
9568 | | TLS13_SM2_SIG_ID_SZ, args->sigData, args->sigDataSz, |
9569 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, NULL); |
9570 | | } |
9571 | | else |
9572 | | #endif |
9573 | | { |
9574 | | ret = EccSign(ssl, args->sigData, args->sigDataSz, |
9575 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, |
9576 | | #ifdef HAVE_PK_CALLBACKS |
9577 | | ssl->buffers.key |
9578 | | #else |
9579 | | NULL |
9580 | | #endif |
9581 | | ); |
9582 | | } |
9583 | | args->length = (word16)args->sigLen; |
9584 | | } |
9585 | | #endif /* HAVE_ECC */ |
9586 | | #ifdef HAVE_ED25519 |
9587 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9588 | | ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz, |
9589 | | sigOut, &args->sigLen, (ed25519_key*)ssl->hsKey, |
9590 | | #ifdef HAVE_PK_CALLBACKS |
9591 | | ssl->buffers.key |
9592 | | #else |
9593 | | NULL |
9594 | | #endif |
9595 | | ); |
9596 | | args->length = (word16)args->sigLen; |
9597 | | } |
9598 | | #endif |
9599 | | #ifdef HAVE_ED448 |
9600 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9601 | | ret = Ed448Sign(ssl, args->sigData, args->sigDataSz, |
9602 | | sigOut, &args->sigLen, (ed448_key*)ssl->hsKey, |
9603 | | #ifdef HAVE_PK_CALLBACKS |
9604 | | ssl->buffers.key |
9605 | | #else |
9606 | | NULL |
9607 | | #endif |
9608 | | ); |
9609 | | args->length = (word16)args->sigLen; |
9610 | | } |
9611 | | #endif |
9612 | | #if defined(HAVE_FALCON) |
9613 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9614 | | ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz, |
9615 | | sigOut, &args->sigLen, |
9616 | | (falcon_key*)ssl->hsKey, ssl->rng); |
9617 | | args->length = (word16)args->sigLen; |
9618 | | } |
9619 | | #endif /* HAVE_FALCON */ |
9620 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9621 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9622 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->sigData, |
9623 | | args->sigDataSz, sigOut, |
9624 | | &args->sigLen, |
9625 | | (dilithium_key*)ssl->hsKey, |
9626 | | ssl->rng); |
9627 | | args->length = (word16)args->sigLen; |
9628 | | } |
9629 | | #endif /* HAVE_DILITHIUM */ |
9630 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
9631 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
9632 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9633 | | args->toSign = rsaSigBuf->buffer; |
9634 | | args->toSignSz = (word32)rsaSigBuf->length; |
9635 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9636 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9637 | | /* Pass full data to sign (args->sigData), not hash of */ |
9638 | | if (ssl->ctx->RsaPssSignCb) { |
9639 | | args->toSign = args->sigData; |
9640 | | args->toSignSz = args->sigDataSz; |
9641 | | } |
9642 | | #endif |
9643 | | ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz, |
9644 | | sigOut, &args->sigLen, args->sigAlgo, |
9645 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9646 | | ssl->buffers.key); |
9647 | | if (ret == 0) { |
9648 | | args->length = (word16)args->sigLen; |
9649 | | XMEMCPY(args->sigData, sigOut, args->sigLen); |
9650 | | } |
9651 | | } |
9652 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
9653 | | |
9654 | | /* Check for error */ |
9655 | | if (ret != 0) { |
9656 | | goto exit_scv; |
9657 | | } |
9658 | | |
9659 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9660 | | if (ssl->sigSpec != NULL && |
9661 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9662 | | /* Add signature length for the first signature. */ |
9663 | | c16toa((word16)args->sigLen, sigOut - OPAQUE16_LEN); |
9664 | | args->length += OPAQUE16_LEN; |
9665 | | |
9666 | | /* Advance our pointer to where we store the alt signature. |
9667 | | * We also add additional space for the length field of the |
9668 | | * second signature. */ |
9669 | | sigOut += args->sigLen + OPAQUE16_LEN; |
9670 | | |
9671 | | /* Generate the alternative signature */ |
9672 | | #ifdef HAVE_ECC |
9673 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9674 | | ret = EccSign(ssl, args->altSigData, args->altSigDataSz, |
9675 | | sigOut, &args->altSigLen, |
9676 | | (ecc_key*)ssl->hsAltKey, |
9677 | | #ifdef HAVE_PK_CALLBACKS |
9678 | | ssl->buffers.altKey |
9679 | | #else |
9680 | | NULL |
9681 | | #endif |
9682 | | ); |
9683 | | } |
9684 | | #endif /* HAVE_ECC */ |
9685 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
9686 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
9687 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9688 | | args->toSign = rsaSigBuf->buffer; |
9689 | | args->toSignSz = (word32)rsaSigBuf->length; |
9690 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9691 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9692 | | /* Pass full data to sign (args->altSigData), not hash of */ |
9693 | | if (ssl->ctx->RsaPssSignCb) { |
9694 | | args->toSign = args->altSigData; |
9695 | | args->toSignSz = (word32)args->altSigDataSz; |
9696 | | } |
9697 | | #endif |
9698 | | ret = RsaSign(ssl, (const byte*)args->toSign, |
9699 | | args->toSignSz, sigOut, &args->altSigLen, |
9700 | | args->altSigAlgo, ssl->options.hashAlgo, |
9701 | | (RsaKey*)ssl->hsAltKey, |
9702 | | ssl->buffers.altKey); |
9703 | | |
9704 | | if (ret == 0) { |
9705 | | XMEMCPY(args->altSigData, sigOut, args->altSigLen); |
9706 | | } |
9707 | | } |
9708 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
9709 | | #if defined(HAVE_FALCON) |
9710 | | if (ssl->hsAltType == DYNAMIC_TYPE_FALCON) { |
9711 | | ret = wc_falcon_sign_msg(args->altSigData, |
9712 | | args->altSigDataSz, sigOut, |
9713 | | &args->altSigLen, |
9714 | | (falcon_key*)ssl->hsAltKey, |
9715 | | ssl->rng); |
9716 | | } |
9717 | | #endif /* HAVE_FALCON */ |
9718 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9719 | | if (ssl->hsAltType == DYNAMIC_TYPE_DILITHIUM) { |
9720 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->altSigData, |
9721 | | args->altSigDataSz, sigOut, &args->altSigLen, |
9722 | | (dilithium_key*)ssl->hsAltKey, ssl->rng); |
9723 | | } |
9724 | | #endif /* HAVE_DILITHIUM */ |
9725 | | |
9726 | | /* Check for error */ |
9727 | | if (ret != 0) { |
9728 | | goto exit_scv; |
9729 | | } |
9730 | | |
9731 | | /* Add signature length for the alternative signature. */ |
9732 | | c16toa((word16)args->altSigLen, sigOut - OPAQUE16_LEN); |
9733 | | |
9734 | | /* Add length of the alt sig to the total length */ |
9735 | | args->length += args->altSigLen + OPAQUE16_LEN; |
9736 | | } |
9737 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9738 | | |
9739 | | /* Add signature length. */ |
9740 | | c16toa(args->length, args->verify + HASH_SIG_SIZE); |
9741 | | |
9742 | | /* Advance state and proceed */ |
9743 | | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
9744 | | } /* case TLS_ASYNC_DO */ |
9745 | | FALL_THROUGH; |
9746 | | |
9747 | | case TLS_ASYNC_VERIFY: |
9748 | | { |
9749 | | #ifndef NO_RSA |
9750 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9751 | | /* check for signature faults */ |
9752 | | ret = VerifyRsaSign(ssl, args->sigData, args->sigLen, |
9753 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, args->sigAlgo, |
9754 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9755 | | ssl->buffers.key); |
9756 | | } |
9757 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9758 | | if (ssl->sigSpec != NULL && |
9759 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
9760 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9761 | | /* check for signature faults */ |
9762 | | ret = VerifyRsaSign(ssl, args->altSigData, args->altSigLen, |
9763 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, |
9764 | | args->altSigAlgo, ssl->options.hashAlgo, |
9765 | | (RsaKey*)ssl->hsAltKey, ssl->buffers.altKey); |
9766 | | } |
9767 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9768 | | #endif /* !NO_RSA */ |
9769 | | #if defined(HAVE_ECC) && defined(WOLFSSL_CHECK_SIG_FAULTS) |
9770 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9771 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
9772 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9773 | | if (ssl->sigSpec != NULL && |
9774 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9775 | | /* Add our length offset. */ |
9776 | | sigOut += OPAQUE16_LEN; |
9777 | | } |
9778 | | #endif |
9779 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9780 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9781 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
9782 | | TLS13_SM2_SIG_ID_SZ, |
9783 | | sigOut, args->sigLen, args->sigData, args->sigDataSz, |
9784 | | (ecc_key*)ssl->hsKey, NULL); |
9785 | | } |
9786 | | else |
9787 | | #endif |
9788 | | { |
9789 | | #ifdef HAVE_PK_CALLBACKS |
9790 | | buffer tmp; |
9791 | | |
9792 | | tmp.length = ssl->buffers.key->length; |
9793 | | tmp.buffer = ssl->buffers.key->buffer; |
9794 | | #endif |
9795 | | ret = EccVerify(ssl, sigOut, args->sigLen, |
9796 | | args->sigData, args->sigDataSz, |
9797 | | (ecc_key*)ssl->hsKey, |
9798 | | #ifdef HAVE_PK_CALLBACKS |
9799 | | &tmp |
9800 | | #else |
9801 | | NULL |
9802 | | #endif |
9803 | | ); |
9804 | | } |
9805 | | } |
9806 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9807 | | if (ssl->sigSpec != NULL && |
9808 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
9809 | | ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9810 | | /* check for signature faults */ |
9811 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER + |
9812 | | args->sigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
9813 | | ret = EccVerify(ssl, sigOut, args->altSigLen, |
9814 | | args->altSigData, args->altSigDataSz, |
9815 | | (ecc_key*)ssl->hsAltKey, |
9816 | | #ifdef HAVE_PK_CALLBACKS |
9817 | | ssl->buffers.altKey |
9818 | | #else |
9819 | | NULL |
9820 | | #endif |
9821 | | ); |
9822 | | } |
9823 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9824 | | #endif /* HAVE_ECC && WOLFSSL_CHECK_SIG_FAULTS */ |
9825 | | |
9826 | | /* Check for error */ |
9827 | | if (ret != 0) { |
9828 | | goto exit_scv; |
9829 | | } |
9830 | | |
9831 | | /* Advance state and proceed */ |
9832 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
9833 | | } /* case TLS_ASYNC_VERIFY */ |
9834 | | FALL_THROUGH; |
9835 | | |
9836 | | case TLS_ASYNC_FINALIZE: |
9837 | | { |
9838 | | /* Put the record and handshake headers on. */ |
9839 | | AddTls13Headers(args->output, args->length + HASH_SIG_SIZE + |
9840 | | VERIFY_HEADER, certificate_verify, ssl); |
9841 | | |
9842 | | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
9843 | | args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
9844 | | #ifdef WOLFSSL_DTLS13 |
9845 | | if (ssl->options.dtls) |
9846 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9847 | | |
9848 | | #endif /* WOLFSSL_DTLS13 */ |
9849 | | /* Advance state and proceed */ |
9850 | | ssl->options.asyncState = TLS_ASYNC_END; |
9851 | | } /* case TLS_ASYNC_FINALIZE */ |
9852 | | FALL_THROUGH; |
9853 | | |
9854 | | case TLS_ASYNC_END: |
9855 | | { |
9856 | | #ifdef WOLFSSL_DTLS13 |
9857 | | if (ssl->options.dtls) { |
9858 | | ssl->options.buildingMsg = 0; |
9859 | | ret = Dtls13HandshakeSend(ssl, args->output, |
9860 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA + MAX_MSG_EXTRA, |
9861 | | (word16)args->sendSz, certificate_verify, 1); |
9862 | | if (ret != 0) |
9863 | | goto exit_scv; |
9864 | | |
9865 | | break; |
9866 | | } |
9867 | | #endif /* WOLFSSL_DTLS13 */ |
9868 | | |
9869 | | /* This message is always encrypted. */ |
9870 | | ret = BuildTls13Message(ssl, args->output, |
9871 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA, |
9872 | | args->output + RECORD_HEADER_SZ, |
9873 | | args->sendSz - RECORD_HEADER_SZ, handshake, |
9874 | | 1, 0, 0); |
9875 | | |
9876 | | if (ret < 0) { |
9877 | | goto exit_scv; |
9878 | | } |
9879 | | else { |
9880 | | args->sendSz = ret; |
9881 | | ret = 0; |
9882 | | } |
9883 | | |
9884 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
9885 | | if (ssl->hsInfoOn) |
9886 | | AddPacketName(ssl, "CertificateVerify"); |
9887 | | if (ssl->toInfoOn) { |
9888 | | ret = AddPacketInfo(ssl, "CertificateVerify", handshake, |
9889 | | args->output, args->sendSz, WRITE_PROTO, 0, |
9890 | | ssl->heap); |
9891 | | if (ret != 0) |
9892 | | goto exit_scv; |
9893 | | } |
9894 | | #endif |
9895 | | |
9896 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
9897 | | ssl->options.buildingMsg = 0; |
9898 | | if (!ssl->options.groupMessages) |
9899 | | ret = SendBuffered(ssl); |
9900 | | break; |
9901 | | } |
9902 | | default: |
9903 | | ret = INPUT_CASE_ERROR; |
9904 | | } /* switch(ssl->options.asyncState) */ |
9905 | | |
9906 | | exit_scv: |
9907 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9908 | | if (ret == 0) { |
9909 | | ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
9910 | | &ssl->buffers.keyMask); |
9911 | | } |
9912 | | else { |
9913 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9914 | | } |
9915 | | #endif |
9916 | | |
9917 | | WOLFSSL_LEAVE("SendTls13CertificateVerify", ret); |
9918 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9919 | | |
9920 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9921 | | /* Handle async operation */ |
9922 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
9923 | | return ret; |
9924 | | } |
9925 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
9926 | | |
9927 | | /* Final cleanup */ |
9928 | | FreeScv13Args(ssl, args); |
9929 | | FreeKeyExchange(ssl); |
9930 | | #ifdef WOLFSSL_ASYNC_IO |
9931 | | /* Cleanup async */ |
9932 | | FreeAsyncCtx(ssl, 0); |
9933 | | #endif |
9934 | | |
9935 | | if (ret != 0) { |
9936 | | WOLFSSL_ERROR_VERBOSE(ret); |
9937 | | } |
9938 | | |
9939 | | return ret; |
9940 | | } |
9941 | | #endif |
9942 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
9943 | | |
9944 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) |
9945 | | /* handle processing TLS v1.3 certificate (11) */ |
9946 | | /* Parse and handle a TLS v1.3 Certificate message. |
9947 | | * |
9948 | | * ssl The SSL/TLS object. |
9949 | | * input The message buffer. |
9950 | | * inOutIdx On entry, the index into the message buffer of Certificate. |
9951 | | * On exit, the index of byte after the Certificate message. |
9952 | | * totalSz The length of the current handshake message. |
9953 | | * returns 0 on success and otherwise failure. |
9954 | | */ |
9955 | | static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
9956 | | word32 totalSz) |
9957 | 0 | { |
9958 | 0 | int ret = 0; |
9959 | |
|
9960 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_DO); |
9961 | 0 | WOLFSSL_ENTER("DoTls13Certificate"); |
9962 | |
|
9963 | | #ifdef WOLFSSL_DTLS13 |
9964 | | if (ssl->options.dtls && ssl->options.handShakeDone) { |
9965 | | /* certificate needs some special care after the handshake */ |
9966 | | ret = Dtls13RtxProcessingCertificate( |
9967 | | ssl, input + *inOutIdx, totalSz); |
9968 | | } |
9969 | | #endif /* WOLFSSL_DTLS13 */ |
9970 | |
|
9971 | 0 | if (ret == 0) |
9972 | 0 | ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz); |
9973 | 0 | if (ret == 0) { |
9974 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
9975 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
9976 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
9977 | 0 | #endif |
9978 | | #if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
9979 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
9980 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
9981 | | /* reset handshake states */ |
9982 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
9983 | | ssl->options.acceptState = TICKET_SENT; |
9984 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
9985 | | } |
9986 | | #endif |
9987 | 0 | } |
9988 | |
|
9989 | 0 | WOLFSSL_LEAVE("DoTls13Certificate", ret); |
9990 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_DO); |
9991 | |
|
9992 | 0 | return ret; |
9993 | 0 | } |
9994 | | #endif |
9995 | | |
9996 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
9997 | | defined(HAVE_ED448) |
9998 | | |
9999 | | typedef struct Dcv13Args { |
10000 | | byte* output; /* not allocated */ |
10001 | | word32 sendSz; |
10002 | | word16 sz; |
10003 | | word32 sigSz; |
10004 | | word32 idx; |
10005 | | word32 begin; |
10006 | | |
10007 | | byte* sigData; |
10008 | | word16 sigDataSz; |
10009 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10010 | | byte altSigAlgo; |
10011 | | byte* altSigData; |
10012 | | word32 altSigDataSz; |
10013 | | word32 altSignatureSz; |
10014 | | byte altPeerAuthGood; |
10015 | | #endif |
10016 | | } Dcv13Args; |
10017 | | |
10018 | | static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs) |
10019 | 0 | { |
10020 | 0 | Dcv13Args* args = (Dcv13Args*)pArgs; |
10021 | |
|
10022 | 0 | if (args && args->sigData != NULL) { |
10023 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10024 | 0 | args->sigData = NULL; |
10025 | 0 | } |
10026 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10027 | | if (args && args->altSigData != NULL) { |
10028 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10029 | | args->altSigData = NULL; |
10030 | | } |
10031 | | #endif |
10032 | 0 | (void)ssl; |
10033 | 0 | } |
10034 | | |
10035 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10036 | | #ifndef NO_RSA |
10037 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10038 | | * RSA public key. Convert it into a usable public key. */ |
10039 | | static int decodeRsaKey(WOLFSSL* ssl) |
10040 | | { |
10041 | | int keyRet; |
10042 | | word32 tmpIdx = 0; |
10043 | | |
10044 | | if (ssl->peerRsaKeyPresent) |
10045 | | return INVALID_PARAMETER; |
10046 | | |
10047 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
10048 | | if (keyRet != 0) |
10049 | | return PEER_KEY_ERROR; |
10050 | | |
10051 | | ssl->peerRsaKeyPresent = 1; |
10052 | | keyRet = wc_RsaPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10053 | | ssl->peerRsaKey, |
10054 | | ssl->peerCert.sapkiLen); |
10055 | | if (keyRet != 0) |
10056 | | return PEER_KEY_ERROR; |
10057 | | |
10058 | | return 0; |
10059 | | } |
10060 | | #endif /* !NO_RSA */ |
10061 | | |
10062 | | #ifdef HAVE_ECC |
10063 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10064 | | * ECC public key. Convert it into a usable public key. */ |
10065 | | static int decodeEccKey(WOLFSSL* ssl) |
10066 | | { |
10067 | | int keyRet; |
10068 | | word32 tmpIdx = 0; |
10069 | | |
10070 | | if (ssl->peerEccDsaKeyPresent) |
10071 | | return INVALID_PARAMETER; |
10072 | | |
10073 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10074 | | if (keyRet != 0) |
10075 | | return PEER_KEY_ERROR; |
10076 | | |
10077 | | ssl->peerEccDsaKeyPresent = 1; |
10078 | | keyRet = wc_EccPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10079 | | ssl->peerEccDsaKey, |
10080 | | ssl->peerCert.sapkiLen); |
10081 | | if (keyRet != 0) |
10082 | | return PEER_KEY_ERROR; |
10083 | | |
10084 | | return 0; |
10085 | | } |
10086 | | #endif /* HAVE_ECC */ |
10087 | | |
10088 | | #ifdef HAVE_DILITHIUM |
10089 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10090 | | * dilithium public key. Convert it into a usable public key. */ |
10091 | | static int decodeDilithiumKey(WOLFSSL* ssl, int level) |
10092 | | { |
10093 | | int keyRet; |
10094 | | word32 tmpIdx = 0; |
10095 | | |
10096 | | if (ssl->peerDilithiumKeyPresent) |
10097 | | return INVALID_PARAMETER; |
10098 | | |
10099 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10100 | | (void**)&ssl->peerDilithiumKey); |
10101 | | if (keyRet != 0) |
10102 | | return PEER_KEY_ERROR; |
10103 | | |
10104 | | ssl->peerDilithiumKeyPresent = 1; |
10105 | | keyRet = wc_dilithium_set_level(ssl->peerDilithiumKey, level); |
10106 | | if (keyRet != 0) |
10107 | | return PEER_KEY_ERROR; |
10108 | | |
10109 | | keyRet = wc_Dilithium_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10110 | | ssl->peerDilithiumKey, |
10111 | | ssl->peerCert.sapkiLen); |
10112 | | if (keyRet != 0) |
10113 | | return PEER_KEY_ERROR; |
10114 | | |
10115 | | return 0; |
10116 | | } |
10117 | | #endif /* HAVE_DILITHIUM */ |
10118 | | |
10119 | | #ifdef HAVE_FALCON |
10120 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10121 | | * falcon public key. Convert it into a usable public key. */ |
10122 | | static int decodeFalconKey(WOLFSSL* ssl, int level) |
10123 | | { |
10124 | | int keyRet; |
10125 | | word32 tmpIdx = 0; |
10126 | | |
10127 | | if (ssl->peerFalconKeyPresent) |
10128 | | return INVALID_PARAMETER; |
10129 | | |
10130 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey); |
10131 | | if (keyRet != 0) |
10132 | | return PEER_KEY_ERROR; |
10133 | | |
10134 | | ssl->peerFalconKeyPresent = 1; |
10135 | | keyRet = wc_falcon_set_level(ssl->peerFalconKey, level); |
10136 | | if (keyRet != 0) |
10137 | | return PEER_KEY_ERROR; |
10138 | | |
10139 | | keyRet = wc_Falcon_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10140 | | ssl->peerFalconKey, |
10141 | | ssl->peerCert.sapkiLen); |
10142 | | if (keyRet != 0) |
10143 | | return PEER_KEY_ERROR; |
10144 | | |
10145 | | return 0; |
10146 | | } |
10147 | | #endif /* HAVE_FALCON */ |
10148 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10149 | | |
10150 | | /* handle processing TLS v1.3 certificate_verify (15) */ |
10151 | | /* Parse and handle a TLS v1.3 CertificateVerify message. |
10152 | | * |
10153 | | * ssl The SSL/TLS object. |
10154 | | * input The message buffer. |
10155 | | * inOutIdx On entry, the index into the message buffer of |
10156 | | * CertificateVerify. |
10157 | | * On exit, the index of byte after the CertificateVerify message. |
10158 | | * totalSz The length of the current handshake message. |
10159 | | * returns 0 on success and otherwise failure. |
10160 | | */ |
10161 | | static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input, |
10162 | | word32* inOutIdx, word32 totalSz) |
10163 | 0 | { |
10164 | 0 | int ret = 0; |
10165 | 0 | byte* sig = NULL; |
10166 | 0 | #ifndef NO_RSA |
10167 | | /* Use this as a temporary buffer for RSA signature verification. */ |
10168 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
10169 | 0 | #endif |
10170 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10171 | | Dcv13Args* args = NULL; |
10172 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
10173 | | #else |
10174 | 0 | Dcv13Args args[1]; |
10175 | 0 | #endif |
10176 | |
|
10177 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10178 | 0 | WOLFSSL_ENTER("DoTls13CertificateVerify"); |
10179 | |
|
10180 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10181 | | ret = tsip_Tls13CertificateVerify(ssl, input, inOutIdx, totalSz); |
10182 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10183 | | goto exit_dcv; |
10184 | | } |
10185 | | ret = 0; |
10186 | | #endif |
10187 | |
|
10188 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10189 | | if (ssl->async == NULL) { |
10190 | | ssl->async = (struct WOLFSSL_ASYNC*) |
10191 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
10192 | | DYNAMIC_TYPE_ASYNC); |
10193 | | if (ssl->async == NULL) |
10194 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10195 | | } |
10196 | | args = (Dcv13Args*)ssl->async->args; |
10197 | | |
10198 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
10199 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
10200 | | /* Check for error */ |
10201 | | if (ret < 0) |
10202 | | goto exit_dcv; |
10203 | | } |
10204 | | else |
10205 | | #endif |
10206 | 0 | { |
10207 | | /* Reset state */ |
10208 | 0 | ret = 0; |
10209 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
10210 | 0 | XMEMSET(args, 0, sizeof(Dcv13Args)); |
10211 | 0 | ssl->options.peerHashAlgo = sha_mac; |
10212 | 0 | ssl->options.peerSigAlgo = anonymous_sa_algo; |
10213 | 0 | args->idx = *inOutIdx; |
10214 | 0 | args->begin = *inOutIdx; |
10215 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10216 | | ssl->async->freeArgs = FreeDcv13Args; |
10217 | | #endif |
10218 | 0 | } |
10219 | |
|
10220 | 0 | switch(ssl->options.asyncState) |
10221 | 0 | { |
10222 | 0 | case TLS_ASYNC_BEGIN: |
10223 | 0 | { |
10224 | | #ifdef WOLFSSL_CALLBACKS |
10225 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify"); |
10226 | | if (ssl->toInfoOn) AddLateName("CertificateVerify", |
10227 | | &ssl->timeoutInfo); |
10228 | | #endif |
10229 | | |
10230 | | /* Advance state and proceed */ |
10231 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
10232 | 0 | } /* case TLS_ASYNC_BEGIN */ |
10233 | 0 | FALL_THROUGH; |
10234 | |
|
10235 | 0 | case TLS_ASYNC_BUILD: |
10236 | 0 | { |
10237 | 0 | int validSigAlgo; |
10238 | 0 | const Suites* suites = WOLFSSL_SUITES(ssl); |
10239 | 0 | word16 i; |
10240 | | |
10241 | | /* Signature algorithm. */ |
10242 | 0 | if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) { |
10243 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10244 | 0 | } |
10245 | | |
10246 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10247 | | if (ssl->peerSigSpec == NULL) { |
10248 | | /* The peer did not respond. We didn't send CKS or they don't |
10249 | | * support it. Either way, we do not need to handle dual |
10250 | | * key/sig case. */ |
10251 | | ssl->sigSpec = NULL; |
10252 | | ssl->sigSpecSz = 0; |
10253 | | } |
10254 | | |
10255 | | /* If no CKS extension or either native or alternative, then just |
10256 | | * get a normal sigalgo. But if BOTH, then get the native and alt |
10257 | | * sig algos. */ |
10258 | | if (ssl->sigSpec == NULL || |
10259 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_NATIVE || |
10260 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10261 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10262 | 0 | validSigAlgo = 0; |
10263 | 0 | for (i = 0; i < suites->hashSigAlgoSz; i += 2) { |
10264 | 0 | if ((suites->hashSigAlgo[i + 0] == input[args->idx + 0]) && |
10265 | 0 | (suites->hashSigAlgo[i + 1] == input[args->idx + 1])) { |
10266 | 0 | validSigAlgo = 1; |
10267 | 0 | break; |
10268 | 0 | } |
10269 | 0 | } |
10270 | 0 | if (!validSigAlgo) { |
10271 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10272 | 0 | } |
10273 | | |
10274 | 0 | ret = DecodeTls13SigAlg(input + args->idx, |
10275 | 0 | &ssl->options.peerHashAlgo, &ssl->options.peerSigAlgo); |
10276 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10277 | | } |
10278 | | else { |
10279 | | ret = DecodeTls13HybridSigAlg(input + args->idx, |
10280 | | &ssl->options.peerHashAlgo, |
10281 | | &ssl->options.peerSigAlgo, |
10282 | | &args->altSigAlgo); |
10283 | | } |
10284 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10285 | |
|
10286 | 0 | if (ret < 0) |
10287 | 0 | goto exit_dcv; |
10288 | 0 | args->idx += OPAQUE16_LEN; |
10289 | | |
10290 | | /* Signature length. */ |
10291 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) { |
10292 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10293 | 0 | } |
10294 | 0 | ato16(input + args->idx, &args->sz); |
10295 | 0 | args->idx += OPAQUE16_LEN; |
10296 | | |
10297 | | /* Signature data. */ |
10298 | 0 | if ((args->idx - args->begin) + args->sz > totalSz || |
10299 | 0 | args->sz > ENCRYPT_LEN) { |
10300 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10301 | 0 | } |
10302 | | |
10303 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10304 | | if ((ssl->sigSpec != NULL) && |
10305 | | (*ssl->sigSpec != WOLFSSL_CKS_SIGSPEC_NATIVE)) { |
10306 | | |
10307 | | word16 sa; |
10308 | | if (args->altSigAlgo == 0) |
10309 | | sa = ssl->options.peerSigAlgo; |
10310 | | else |
10311 | | sa = args->altSigAlgo; |
10312 | | |
10313 | | switch(sa) { |
10314 | | #ifndef NO_RSA |
10315 | | case rsa_pss_sa_algo: |
10316 | | ret = decodeRsaKey(ssl); |
10317 | | break; |
10318 | | #endif |
10319 | | #ifdef HAVE_ECC |
10320 | | case ecc_dsa_sa_algo: |
10321 | | ret = decodeEccKey(ssl); |
10322 | | break; |
10323 | | #endif |
10324 | | #ifdef HAVE_DILITHIUM |
10325 | | case dilithium_level2_sa_algo: |
10326 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_44); |
10327 | | break; |
10328 | | case dilithium_level3_sa_algo: |
10329 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_65); |
10330 | | break; |
10331 | | case dilithium_level5_sa_algo: |
10332 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_87); |
10333 | | break; |
10334 | | #endif |
10335 | | #ifdef HAVE_FALCON |
10336 | | case falcon_level1_sa_algo: |
10337 | | ret = decodeFalconKey(ssl, 1); |
10338 | | break; |
10339 | | case falcon_level5_sa_algo: |
10340 | | ret = decodeFalconKey(ssl, 5); |
10341 | | break; |
10342 | | #endif |
10343 | | default: |
10344 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10345 | | } |
10346 | | |
10347 | | if (ret != 0) |
10348 | | ERROR_OUT(ret, exit_dcv); |
10349 | | |
10350 | | if (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10351 | | /* Now swap in the alternative by removing the native. |
10352 | | * sa contains the alternative signature type. */ |
10353 | | #ifndef NO_RSA |
10354 | | if (ssl->peerRsaKeyPresent && sa != rsa_pss_sa_algo) { |
10355 | | FreeKey(ssl, DYNAMIC_TYPE_RSA, |
10356 | | (void**)&ssl->peerRsaKey); |
10357 | | ssl->peerRsaKeyPresent = 0; |
10358 | | } |
10359 | | #endif |
10360 | | #ifdef HAVE_ECC |
10361 | | else if (ssl->peerEccDsaKeyPresent && |
10362 | | sa != ecc_dsa_sa_algo) { |
10363 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
10364 | | (void**)&ssl->peerEccDsaKey); |
10365 | | ssl->peerEccDsaKeyPresent = 0; |
10366 | | } |
10367 | | #endif |
10368 | | #ifdef HAVE_DILITHIUM |
10369 | | else if (ssl->peerDilithiumKeyPresent && |
10370 | | sa != dilithium_level2_sa_algo && |
10371 | | sa != dilithium_level3_sa_algo && |
10372 | | sa != dilithium_level5_sa_algo) { |
10373 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10374 | | (void**)&ssl->peerDilithiumKey); |
10375 | | ssl->peerDilithiumKeyPresent = 0; |
10376 | | } |
10377 | | #endif |
10378 | | #ifdef HAVE_FALCON |
10379 | | else if (ssl->peerFalconKeyPresent && |
10380 | | sa != falcon_level1_sa_algo && |
10381 | | sa != falcon_level5_sa_algo) { |
10382 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10383 | | (void**)&ssl->peerFalconKey); |
10384 | | ssl->peerFalconKeyPresent = 0; |
10385 | | } |
10386 | | #endif |
10387 | | else { |
10388 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10389 | | } |
10390 | | } |
10391 | | } |
10392 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10393 | | |
10394 | | /* Check for public key of required type. */ |
10395 | | /* Assume invalid unless signature algo matches the key provided */ |
10396 | 0 | validSigAlgo = 0; |
10397 | 0 | #ifdef HAVE_ED25519 |
10398 | 0 | if (ssl->options.peerSigAlgo == ed25519_sa_algo) { |
10399 | 0 | WOLFSSL_MSG("Peer sent ED25519 sig"); |
10400 | 0 | validSigAlgo = (ssl->peerEd25519Key != NULL) && |
10401 | 0 | ssl->peerEd25519KeyPresent; |
10402 | 0 | } |
10403 | 0 | #endif |
10404 | 0 | #ifdef HAVE_ED448 |
10405 | 0 | if (ssl->options.peerSigAlgo == ed448_sa_algo) { |
10406 | 0 | WOLFSSL_MSG("Peer sent ED448 sig"); |
10407 | 0 | validSigAlgo = (ssl->peerEd448Key != NULL) && |
10408 | 0 | ssl->peerEd448KeyPresent; |
10409 | 0 | } |
10410 | 0 | #endif |
10411 | 0 | #ifdef HAVE_ECC |
10412 | 0 | if (ssl->options.peerSigAlgo == ecc_dsa_sa_algo) { |
10413 | 0 | WOLFSSL_MSG("Peer sent ECC sig"); |
10414 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10415 | 0 | ssl->peerEccDsaKeyPresent; |
10416 | 0 | } |
10417 | 0 | #endif |
10418 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10419 | 0 | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
10420 | 0 | WOLFSSL_MSG("Peer sent SM2 sig"); |
10421 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10422 | 0 | ssl->peerEccDsaKeyPresent; |
10423 | 0 | } |
10424 | 0 | #endif |
10425 | | #ifdef HAVE_FALCON |
10426 | | if (ssl->options.peerSigAlgo == falcon_level1_sa_algo) { |
10427 | | WOLFSSL_MSG("Peer sent Falcon Level 1 sig"); |
10428 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10429 | | ssl->peerFalconKeyPresent; |
10430 | | } |
10431 | | if (ssl->options.peerSigAlgo == falcon_level5_sa_algo) { |
10432 | | WOLFSSL_MSG("Peer sent Falcon Level 5 sig"); |
10433 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10434 | | ssl->peerFalconKeyPresent; |
10435 | | } |
10436 | | #endif |
10437 | | #ifdef HAVE_DILITHIUM |
10438 | | if (ssl->options.peerSigAlgo == dilithium_level2_sa_algo) { |
10439 | | WOLFSSL_MSG("Peer sent Dilithium Level 2 sig"); |
10440 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10441 | | ssl->peerDilithiumKeyPresent; |
10442 | | } |
10443 | | if (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) { |
10444 | | WOLFSSL_MSG("Peer sent Dilithium Level 3 sig"); |
10445 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10446 | | ssl->peerDilithiumKeyPresent; |
10447 | | } |
10448 | | if (ssl->options.peerSigAlgo == dilithium_level5_sa_algo) { |
10449 | | WOLFSSL_MSG("Peer sent Dilithium Level 5 sig"); |
10450 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10451 | | ssl->peerDilithiumKeyPresent; |
10452 | | } |
10453 | | #endif |
10454 | 0 | #ifndef NO_RSA |
10455 | 0 | if (ssl->options.peerSigAlgo == rsa_sa_algo) { |
10456 | 0 | WOLFSSL_MSG("Peer sent PKCS#1.5 algo - not valid TLS 1.3"); |
10457 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10458 | 0 | } |
10459 | 0 | if (ssl->options.peerSigAlgo == rsa_pss_sa_algo) { |
10460 | 0 | WOLFSSL_MSG("Peer sent RSA sig"); |
10461 | 0 | validSigAlgo = (ssl->peerRsaKey != NULL) && |
10462 | 0 | ssl->peerRsaKeyPresent; |
10463 | 0 | } |
10464 | 0 | #endif |
10465 | 0 | if (!validSigAlgo) { |
10466 | 0 | WOLFSSL_MSG("Sig algo doesn't correspond to certificate"); |
10467 | 0 | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
10468 | 0 | } |
10469 | | |
10470 | 0 | args->sigSz = args->sz; |
10471 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10472 | | if (ssl->sigSpec != NULL && |
10473 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10474 | | /* In case we received two signatures, both of them are encoded |
10475 | | * with their size as 16-bit integeter prior in memory. Hence, |
10476 | | * we can decode both lengths here now. */ |
10477 | | word32 tmpIdx = args->idx; |
10478 | | word16 tmpSz = 0; |
10479 | | ato16(input + tmpIdx, &tmpSz); |
10480 | | args->sigSz = tmpSz; |
10481 | | |
10482 | | tmpIdx += OPAQUE16_LEN + args->sigSz; |
10483 | | ato16(input + tmpIdx, &tmpSz); |
10484 | | args->altSignatureSz = tmpSz; |
10485 | | |
10486 | | if (args->sz != (args->sigSz + args->altSignatureSz + |
10487 | | OPAQUE16_LEN + OPAQUE16_LEN)) { |
10488 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10489 | | } |
10490 | | } |
10491 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10492 | |
|
10493 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
10494 | | /* In case we have to verify an RSA signature, we have to store the |
10495 | | * signature in the 'rsaSigBuf' structure for further processing. |
10496 | | */ |
10497 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
10498 | 0 | word32 sigSz = args->sigSz; |
10499 | 0 | sig = input + args->idx; |
10500 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10501 | | /* Check if our alternative signature was RSA */ |
10502 | | if (ssl->sigSpec != NULL && |
10503 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10504 | | if (ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10505 | | /* We have to skip the first signature (length field |
10506 | | * and signature itself) and the length field of the |
10507 | | * alternative signature. */ |
10508 | | sig += OPAQUE16_LEN + OPAQUE16_LEN + args->sigSz; |
10509 | | sigSz = args->altSignatureSz; |
10510 | | } |
10511 | | else { |
10512 | | /* We have to skip the length field */ |
10513 | | sig += OPAQUE16_LEN; |
10514 | | } |
10515 | | } |
10516 | | #endif |
10517 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(sigSz, ssl->heap, |
10518 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10519 | 0 | if (rsaSigBuf->buffer == NULL) { |
10520 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10521 | 0 | } |
10522 | 0 | rsaSigBuf->length = sigSz; |
10523 | 0 | XMEMCPY(rsaSigBuf->buffer, sig, rsaSigBuf->length); |
10524 | 0 | } |
10525 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
10526 | | |
10527 | 0 | args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10528 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10529 | 0 | if (args->sigData == NULL) { |
10530 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10531 | 0 | } |
10532 | | |
10533 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1); |
10534 | 0 | if (ret < 0) |
10535 | 0 | goto exit_dcv; |
10536 | | |
10537 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10538 | | if ((ssl->sigSpec != NULL) && |
10539 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
10540 | | args->altSigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10541 | | DYNAMIC_TYPE_SIGNATURE); |
10542 | | if (args->altSigData == NULL) { |
10543 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10544 | | } |
10545 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
10546 | | args->altSigDataSz = args->sigDataSz; |
10547 | | } |
10548 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10549 | | |
10550 | 0 | #ifdef HAVE_ECC |
10551 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10552 | 0 | (ssl->peerEccDsaKeyPresent)) { |
10553 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10554 | 0 | if (ssl->options.peerSigAlgo != sm2_sa_algo) |
10555 | 0 | #endif |
10556 | 0 | { |
10557 | 0 | ret = CreateECCEncodedSig(args->sigData, |
10558 | 0 | args->sigDataSz, ssl->options.peerHashAlgo); |
10559 | 0 | if (ret < 0) |
10560 | 0 | goto exit_dcv; |
10561 | 0 | args->sigDataSz = (word16)ret; |
10562 | 0 | ret = 0; |
10563 | 0 | } |
10564 | 0 | } |
10565 | | |
10566 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10567 | | if ((ssl->sigSpec != NULL) && |
10568 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
10569 | | (args->altSigAlgo == ecc_dsa_sa_algo) && |
10570 | | (ssl->peerEccDsaKeyPresent)) { |
10571 | | ret = CreateECCEncodedSig(args->altSigData, |
10572 | | args->altSigDataSz, ssl->options.peerHashAlgo); |
10573 | | if (ret < 0) |
10574 | | goto exit_dcv; |
10575 | | args->altSigDataSz = (word16)ret; |
10576 | | ret = 0; |
10577 | | } |
10578 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10579 | 0 | #endif /* HAVE_ECC */ |
10580 | | |
10581 | | /* Advance state and proceed */ |
10582 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
10583 | 0 | } /* case TLS_ASYNC_BUILD */ |
10584 | 0 | FALL_THROUGH; |
10585 | |
|
10586 | 0 | case TLS_ASYNC_DO: |
10587 | 0 | { |
10588 | 0 | sig = input + args->idx; |
10589 | 0 | (void)sig; |
10590 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10591 | | if (ssl->sigSpec != NULL && |
10592 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10593 | | /* As we have two signatures in the message, we stored |
10594 | | * the length of each before the actual signature. This |
10595 | | * is necessary, as we could have two algorithms with |
10596 | | * variable length signatures. */ |
10597 | | sig += OPAQUE16_LEN; |
10598 | | } |
10599 | | #endif |
10600 | 0 | #ifndef NO_RSA |
10601 | 0 | if ((ssl->options.peerSigAlgo == rsa_pss_sa_algo) && |
10602 | 0 | (ssl->peerRsaKey != NULL) && (ssl->peerRsaKeyPresent != 0)) { |
10603 | 0 | WOLFSSL_MSG("Doing RSA peer cert verify"); |
10604 | 0 | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
10605 | 0 | (word32)rsaSigBuf->length, &args->output, |
10606 | 0 | ssl->options.peerSigAlgo, |
10607 | 0 | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
10608 | | #ifdef HAVE_PK_CALLBACKS |
10609 | | &ssl->buffers.peerRsaKey |
10610 | | #else |
10611 | 0 | NULL |
10612 | 0 | #endif |
10613 | 0 | ); |
10614 | 0 | if (ret >= 0) { |
10615 | 0 | args->sendSz = (word32)ret; |
10616 | 0 | ret = 0; |
10617 | 0 | } |
10618 | 0 | } |
10619 | 0 | #endif /* !NO_RSA */ |
10620 | 0 | #ifdef HAVE_ECC |
10621 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10622 | 0 | ssl->peerEccDsaKeyPresent) { |
10623 | 0 | WOLFSSL_MSG("Doing ECC peer cert verify"); |
10624 | 0 | ret = EccVerify(ssl, sig, args->sigSz, |
10625 | 0 | args->sigData, args->sigDataSz, |
10626 | 0 | ssl->peerEccDsaKey, |
10627 | | #ifdef HAVE_PK_CALLBACKS |
10628 | | &ssl->buffers.peerEccDsaKey |
10629 | | #else |
10630 | 0 | NULL |
10631 | 0 | #endif |
10632 | 0 | ); |
10633 | |
|
10634 | 0 | if (ret >= 0) { |
10635 | | /* CLIENT/SERVER: data verified with public key from |
10636 | | * certificate. */ |
10637 | 0 | ssl->options.peerAuthGood = 1; |
10638 | |
|
10639 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10640 | 0 | ssl->peerEccDsaKeyPresent = 0; |
10641 | 0 | } |
10642 | 0 | } |
10643 | 0 | #endif /* HAVE_ECC */ |
10644 | 0 | #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10645 | 0 | if ((ssl->options.peerSigAlgo == sm2_sa_algo) && |
10646 | 0 | ssl->peerEccDsaKeyPresent) { |
10647 | 0 | WOLFSSL_MSG("Doing SM2/SM3 peer cert verify"); |
10648 | 0 | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, TLS13_SM2_SIG_ID_SZ, |
10649 | 0 | sig, args->sigSz, args->sigData, args->sigDataSz, |
10650 | 0 | ssl->peerEccDsaKey, NULL); |
10651 | 0 | if (ret >= 0) { |
10652 | | /* CLIENT/SERVER: data verified with public key from |
10653 | | * certificate. */ |
10654 | 0 | ssl->options.peerAuthGood = 1; |
10655 | |
|
10656 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10657 | 0 | ssl->peerEccDsaKeyPresent = 0; |
10658 | 0 | } |
10659 | 0 | } |
10660 | 0 | #endif |
10661 | 0 | #ifdef HAVE_ED25519 |
10662 | 0 | if ((ssl->options.peerSigAlgo == ed25519_sa_algo) && |
10663 | 0 | (ssl->peerEd25519KeyPresent)) { |
10664 | 0 | WOLFSSL_MSG("Doing ED25519 peer cert verify"); |
10665 | 0 | ret = Ed25519Verify(ssl, sig, args->sigSz, |
10666 | 0 | args->sigData, args->sigDataSz, |
10667 | 0 | ssl->peerEd25519Key, |
10668 | | #ifdef HAVE_PK_CALLBACKS |
10669 | | &ssl->buffers.peerEd25519Key |
10670 | | #else |
10671 | 0 | NULL |
10672 | 0 | #endif |
10673 | 0 | ); |
10674 | |
|
10675 | 0 | if (ret >= 0) { |
10676 | | /* CLIENT/SERVER: data verified with public key from |
10677 | | * certificate. */ |
10678 | 0 | ssl->options.peerAuthGood = 1; |
10679 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED25519, |
10680 | 0 | (void**)&ssl->peerEd25519Key); |
10681 | 0 | ssl->peerEd25519KeyPresent = 0; |
10682 | 0 | } |
10683 | 0 | } |
10684 | 0 | #endif |
10685 | 0 | #ifdef HAVE_ED448 |
10686 | 0 | if ((ssl->options.peerSigAlgo == ed448_sa_algo) && |
10687 | 0 | (ssl->peerEd448KeyPresent)) { |
10688 | 0 | WOLFSSL_MSG("Doing ED448 peer cert verify"); |
10689 | 0 | ret = Ed448Verify(ssl, sig, args->sigSz, |
10690 | 0 | args->sigData, args->sigDataSz, |
10691 | 0 | ssl->peerEd448Key, |
10692 | | #ifdef HAVE_PK_CALLBACKS |
10693 | | &ssl->buffers.peerEd448Key |
10694 | | #else |
10695 | 0 | NULL |
10696 | 0 | #endif |
10697 | 0 | ); |
10698 | |
|
10699 | 0 | if (ret >= 0) { |
10700 | | /* CLIENT/SERVER: data verified with public key from |
10701 | | * certificate. */ |
10702 | 0 | ssl->options.peerAuthGood = 1; |
10703 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED448, |
10704 | 0 | (void**)&ssl->peerEd448Key); |
10705 | 0 | ssl->peerEd448KeyPresent = 0; |
10706 | 0 | } |
10707 | 0 | } |
10708 | 0 | #endif |
10709 | | #if defined(HAVE_FALCON) |
10710 | | if (((ssl->options.peerSigAlgo == falcon_level1_sa_algo) || |
10711 | | (ssl->options.peerSigAlgo == falcon_level5_sa_algo)) && |
10712 | | (ssl->peerFalconKeyPresent)) { |
10713 | | int res = 0; |
10714 | | WOLFSSL_MSG("Doing Falcon peer cert verify"); |
10715 | | ret = wc_falcon_verify_msg(sig, args->sigSz, |
10716 | | args->sigData, args->sigDataSz, |
10717 | | &res, ssl->peerFalconKey); |
10718 | | |
10719 | | if ((ret >= 0) && (res == 1)) { |
10720 | | /* CLIENT/SERVER: data verified with public key from |
10721 | | * certificate. */ |
10722 | | ssl->options.peerAuthGood = 1; |
10723 | | |
10724 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10725 | | (void**)&ssl->peerFalconKey); |
10726 | | ssl->peerFalconKeyPresent = 0; |
10727 | | } |
10728 | | else if ((ret >= 0) && (res == 0)) { |
10729 | | WOLFSSL_MSG("Falcon signature verification failed"); |
10730 | | ret = SIG_VERIFY_E; |
10731 | | } |
10732 | | } |
10733 | | #endif /* HAVE_FALCON */ |
10734 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
10735 | | if (((ssl->options.peerSigAlgo == dilithium_level2_sa_algo) || |
10736 | | (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) || |
10737 | | (ssl->options.peerSigAlgo == dilithium_level5_sa_algo)) && |
10738 | | (ssl->peerDilithiumKeyPresent)) { |
10739 | | int res = 0; |
10740 | | WOLFSSL_MSG("Doing Dilithium peer cert verify"); |
10741 | | ret = wc_dilithium_verify_ctx_msg(sig, args->sigSz, NULL, 0, |
10742 | | args->sigData, args->sigDataSz, |
10743 | | &res, ssl->peerDilithiumKey); |
10744 | | |
10745 | | if ((ret >= 0) && (res == 1)) { |
10746 | | /* CLIENT/SERVER: data verified with public key from |
10747 | | * certificate. */ |
10748 | | ssl->options.peerAuthGood = 1; |
10749 | | |
10750 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10751 | | (void**)&ssl->peerDilithiumKey); |
10752 | | ssl->peerDilithiumKeyPresent = 0; |
10753 | | } |
10754 | | else if ((ret >= 0) && (res == 0)) { |
10755 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
10756 | | ret = SIG_VERIFY_E; |
10757 | | } |
10758 | | } |
10759 | | #endif /* HAVE_DILITHIUM */ |
10760 | | |
10761 | | /* Check for error */ |
10762 | 0 | if (ret != 0) { |
10763 | 0 | goto exit_dcv; |
10764 | 0 | } |
10765 | | |
10766 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10767 | | if (ssl->sigSpec != NULL && |
10768 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10769 | | /* Move forward to the alternative signature. */ |
10770 | | sig += args->sigSz + OPAQUE16_LEN; |
10771 | | |
10772 | | /* Verify the alternative signature */ |
10773 | | #ifndef NO_RSA |
10774 | | if ((args->altSigAlgo == rsa_pss_sa_algo) && |
10775 | | (ssl->peerRsaKey != NULL) && |
10776 | | (ssl->peerRsaKeyPresent != 0)) { |
10777 | | WOLFSSL_MSG("Doing RSA peer cert alt verify"); |
10778 | | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
10779 | | (word32)rsaSigBuf->length, |
10780 | | &args->output, args->altSigAlgo, |
10781 | | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
10782 | | #ifdef HAVE_PK_CALLBACKS |
10783 | | &ssl->buffers.peerRsaKey |
10784 | | #else |
10785 | | NULL |
10786 | | #endif |
10787 | | ); |
10788 | | if (ret >= 0) { |
10789 | | args->sendSz = ret; |
10790 | | ret = 0; |
10791 | | } |
10792 | | } |
10793 | | #endif /* !NO_RSA */ |
10794 | | #ifdef HAVE_ECC |
10795 | | if ((args->altSigAlgo == ecc_dsa_sa_algo) && |
10796 | | (ssl->peerEccDsaKeyPresent)) { |
10797 | | WOLFSSL_MSG("Doing ECC peer cert alt verify"); |
10798 | | ret = EccVerify(ssl, sig, args->altSignatureSz, |
10799 | | args->altSigData, args->altSigDataSz, |
10800 | | ssl->peerEccDsaKey, |
10801 | | #ifdef HAVE_PK_CALLBACKS |
10802 | | &ssl->buffers.peerEccDsaKey |
10803 | | #else |
10804 | | NULL |
10805 | | #endif |
10806 | | ); |
10807 | | |
10808 | | if (ret >= 0) { |
10809 | | /* CLIENT/SERVER: data verified with public key from |
10810 | | * certificate. */ |
10811 | | args->altPeerAuthGood = 1; |
10812 | | |
10813 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
10814 | | (void**)&ssl->peerEccDsaKey); |
10815 | | ssl->peerEccDsaKeyPresent = 0; |
10816 | | } |
10817 | | } |
10818 | | #endif /* HAVE_ECC */ |
10819 | | #if defined(HAVE_FALCON) |
10820 | | if (((args->altSigAlgo == falcon_level1_sa_algo) || |
10821 | | (args->altSigAlgo == falcon_level5_sa_algo)) && |
10822 | | (ssl->peerFalconKeyPresent)) { |
10823 | | int res = 0; |
10824 | | WOLFSSL_MSG("Doing Falcon peer cert alt verify"); |
10825 | | ret = wc_falcon_verify_msg(sig, args->altSignatureSz, |
10826 | | args->altSigData, args->altSigDataSz, |
10827 | | &res, ssl->peerFalconKey); |
10828 | | |
10829 | | if ((ret >= 0) && (res == 1)) { |
10830 | | /* CLIENT/SERVER: data verified with public key from |
10831 | | * certificate. */ |
10832 | | args->altPeerAuthGood = 1; |
10833 | | |
10834 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10835 | | (void**)&ssl->peerFalconKey); |
10836 | | ssl->peerFalconKeyPresent = 0; |
10837 | | } |
10838 | | else if ((ret >= 0) && (res == 0)) { |
10839 | | WOLFSSL_MSG("Falcon signature verification failed"); |
10840 | | ret = SIG_VERIFY_E; |
10841 | | } |
10842 | | } |
10843 | | #endif /* HAVE_FALCON */ |
10844 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
10845 | | if (((args->altSigAlgo == dilithium_level2_sa_algo) || |
10846 | | (args->altSigAlgo == dilithium_level3_sa_algo) || |
10847 | | (args->altSigAlgo == dilithium_level5_sa_algo)) && |
10848 | | (ssl->peerDilithiumKeyPresent)) { |
10849 | | int res = 0; |
10850 | | WOLFSSL_MSG("Doing Dilithium peer cert alt verify"); |
10851 | | ret = wc_dilithium_verify_ctx_msg(sig, args->altSignatureSz, |
10852 | | NULL, 0, args->altSigData, |
10853 | | args->altSigDataSz, &res, |
10854 | | ssl->peerDilithiumKey); |
10855 | | |
10856 | | if ((ret >= 0) && (res == 1)) { |
10857 | | /* CLIENT/SERVER: data verified with public key from |
10858 | | * certificate. */ |
10859 | | args->altPeerAuthGood = 1; |
10860 | | |
10861 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10862 | | (void**)&ssl->peerDilithiumKey); |
10863 | | ssl->peerDilithiumKeyPresent = 0; |
10864 | | } |
10865 | | else if ((ret >= 0) && (res == 0)) { |
10866 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
10867 | | ret = SIG_VERIFY_E; |
10868 | | } |
10869 | | } |
10870 | | #endif /* HAVE_DILITHIUM */ |
10871 | | |
10872 | | /* Check for error */ |
10873 | | if (ret != 0) { |
10874 | | goto exit_dcv; |
10875 | | } |
10876 | | } |
10877 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10878 | | |
10879 | | /* Advance state and proceed */ |
10880 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
10881 | 0 | } /* case TLS_ASYNC_DO */ |
10882 | 0 | FALL_THROUGH; |
10883 | |
|
10884 | 0 | case TLS_ASYNC_VERIFY: |
10885 | 0 | { |
10886 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
10887 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
10888 | 0 | int sigAlgo = ssl->options.peerSigAlgo; |
10889 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10890 | | /* Check if our alternative signature was RSA */ |
10891 | | if (ssl->sigSpec != NULL && |
10892 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10893 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10894 | | sigAlgo = args->altSigAlgo; |
10895 | | } |
10896 | | #endif |
10897 | 0 | ret = CheckRSASignature(ssl, sigAlgo, |
10898 | 0 | ssl->options.peerHashAlgo, args->output, args->sendSz); |
10899 | 0 | if (ret != 0) |
10900 | 0 | goto exit_dcv; |
10901 | | |
10902 | | /* CLIENT/SERVER: data verified with public key from |
10903 | | * certificate. */ |
10904 | 0 | ssl->peerRsaKeyPresent = 0; |
10905 | 0 | FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
10906 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10907 | | /* Check if our alternative signature was RSA */ |
10908 | | if (ssl->sigSpec != NULL && |
10909 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10910 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10911 | | args->altPeerAuthGood = 1; |
10912 | | } |
10913 | | else |
10914 | | #endif |
10915 | 0 | ssl->options.peerAuthGood = 1; |
10916 | 0 | } |
10917 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
10918 | | |
10919 | | /* Advance state and proceed */ |
10920 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
10921 | 0 | } /* case TLS_ASYNC_VERIFY */ |
10922 | 0 | FALL_THROUGH; |
10923 | |
|
10924 | 0 | case TLS_ASYNC_FINALIZE: |
10925 | 0 | { |
10926 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10927 | | if (ssl->options.peerAuthGood && |
10928 | | ssl->sigSpec != NULL && |
10929 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10930 | | ssl->options.peerAuthGood = args->altPeerAuthGood; |
10931 | | } |
10932 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10933 | 0 | ssl->options.havePeerVerify = 1; |
10934 | | |
10935 | | /* Set final index */ |
10936 | 0 | args->idx += args->sz; |
10937 | 0 | *inOutIdx = args->idx; |
10938 | | |
10939 | | /* Encryption is always on: add padding */ |
10940 | 0 | *inOutIdx += ssl->keys.padSz; |
10941 | | |
10942 | | /* Advance state and proceed */ |
10943 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
10944 | |
|
10945 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
10946 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
10947 | 0 | ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE; |
10948 | 0 | #endif |
10949 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
10950 | 0 | FALL_THROUGH; |
10951 | |
|
10952 | 0 | case TLS_ASYNC_END: |
10953 | 0 | { |
10954 | 0 | break; |
10955 | 0 | } |
10956 | | |
10957 | 0 | default: |
10958 | 0 | ret = INPUT_CASE_ERROR; |
10959 | 0 | } /* switch(ssl->options.asyncState) */ |
10960 | | |
10961 | 0 | exit_dcv: |
10962 | |
|
10963 | 0 | WOLFSSL_LEAVE("DoTls13CertificateVerify", ret); |
10964 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10965 | |
|
10966 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10967 | | /* Handle async operation */ |
10968 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
10969 | | /* Mark message as not received so it can process again */ |
10970 | | ssl->msgsReceived.got_certificate_verify = 0; |
10971 | | |
10972 | | return ret; |
10973 | | } |
10974 | | else |
10975 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
10976 | 0 | if (ret != 0) { |
10977 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
10978 | |
|
10979 | 0 | if (ret != WC_NO_ERR_TRACE(INVALID_PARAMETER)) { |
10980 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
10981 | 0 | } |
10982 | 0 | } |
10983 | | |
10984 | | /* Final cleanup */ |
10985 | 0 | FreeDcv13Args(ssl, args); |
10986 | 0 | FreeKeyExchange(ssl); |
10987 | 0 | #ifdef WOLFSSL_ASYNC_IO |
10988 | | /* Cleanup async */ |
10989 | 0 | FreeAsyncCtx(ssl, 0); |
10990 | 0 | #endif |
10991 | |
|
10992 | 0 | return ret; |
10993 | 0 | } |
10994 | | #endif /* !NO_RSA || HAVE_ECC */ |
10995 | | #endif /* !NO_CERTS */ |
10996 | | |
10997 | | /* Parse and handle a TLS v1.3 Finished message. |
10998 | | * |
10999 | | * ssl The SSL/TLS object. |
11000 | | * input The message buffer. |
11001 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11002 | | * On exit, the index of byte after the Finished message and padding. |
11003 | | * size Length of message data. |
11004 | | * totalSz Length of remaining data in the message buffer. |
11005 | | * sniff Indicates whether we are sniffing packets. |
11006 | | * returns 0 on success and otherwise failure. |
11007 | | */ |
11008 | | int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11009 | | word32 size, word32 totalSz, int sniff) |
11010 | 0 | { |
11011 | 0 | int ret; |
11012 | 0 | word32 finishedSz = 0; |
11013 | 0 | byte* secret; |
11014 | 0 | byte mac[WC_MAX_DIGEST_SIZE]; |
11015 | |
|
11016 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_DO); |
11017 | 0 | WOLFSSL_ENTER("DoTls13Finished"); |
11018 | |
|
11019 | 0 | #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH) |
11020 | | /* verify the client sent certificate if required */ |
11021 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && !ssl->options.resuming && |
11022 | 0 | (ssl->options.mutualAuth || ssl->options.failNoCert)) { |
11023 | | #ifdef OPENSSL_COMPATIBLE_DEFAULTS |
11024 | | if (ssl->options.isPSK) { |
11025 | | WOLFSSL_MSG("TLS v1.3 client used PSK but cert required. Allowing " |
11026 | | "for OpenSSL compatibility"); |
11027 | | } |
11028 | | else |
11029 | | #endif |
11030 | 0 | if ( |
11031 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
11032 | | !ssl->options.verifyPostHandshake && |
11033 | | #endif |
11034 | 0 | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
11035 | 0 | ret = NO_PEER_CERT; /* NO_PEER_VERIFY */ |
11036 | 0 | WOLFSSL_MSG("TLS v1.3 client did not present peer cert"); |
11037 | 0 | DoCertFatalAlert(ssl, ret); |
11038 | 0 | return ret; |
11039 | 0 | } |
11040 | 0 | } |
11041 | 0 | #endif |
11042 | | |
11043 | | /* check against totalSz */ |
11044 | 0 | if (*inOutIdx + size > totalSz) |
11045 | 0 | return BUFFER_E; |
11046 | | |
11047 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11048 | | ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz); |
11049 | | if (ret == 0) { |
11050 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11051 | | return ret; |
11052 | | } |
11053 | | if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) { |
11054 | | SendAlert(ssl, alert_fatal, decrypt_error); |
11055 | | return ret; |
11056 | | } |
11057 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11058 | | /* other errors */ |
11059 | | return ret; |
11060 | | } |
11061 | | ret = 0; |
11062 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11063 | | |
11064 | 0 | if (ssl->options.handShakeDone) { |
11065 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11066 | 0 | ssl->keys.client_write_MAC_secret, |
11067 | 0 | WOLFSSL_CLIENT_END); |
11068 | 0 | if (ret != 0) |
11069 | 0 | return ret; |
11070 | | |
11071 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11072 | 0 | } |
11073 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11074 | | /* All the handshake messages have been received to calculate |
11075 | | * client and server finished keys. |
11076 | | */ |
11077 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11078 | 0 | ssl->keys.client_write_MAC_secret, |
11079 | 0 | WOLFSSL_CLIENT_END); |
11080 | 0 | if (ret != 0) |
11081 | 0 | return ret; |
11082 | | |
11083 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11084 | 0 | ssl->keys.server_write_MAC_secret, |
11085 | 0 | WOLFSSL_SERVER_END); |
11086 | 0 | if (ret != 0) |
11087 | 0 | return ret; |
11088 | | |
11089 | 0 | secret = ssl->keys.server_write_MAC_secret; |
11090 | 0 | } |
11091 | 0 | else { |
11092 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11093 | 0 | } |
11094 | | |
11095 | 0 | if (sniff == NO_SNIFF) { |
11096 | |
|
11097 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz); |
11098 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11099 | | if (finishedSz > TLS_FINISHED_SZ_MAX) { |
11100 | | return BUFFER_ERROR; |
11101 | | } |
11102 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11103 | | XMEMCPY(ssl->serverFinished, mac, finishedSz); |
11104 | | ssl->serverFinished_len = (byte)finishedSz; |
11105 | | } |
11106 | | else { |
11107 | | XMEMCPY(ssl->clientFinished, mac, finishedSz); |
11108 | | ssl->clientFinished_len = (byte)finishedSz; |
11109 | | } |
11110 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11111 | 0 | if (ret != 0) |
11112 | 0 | return ret; |
11113 | 0 | if (size != finishedSz) |
11114 | 0 | return BUFFER_ERROR; |
11115 | 0 | } |
11116 | | |
11117 | | #ifdef WOLFSSL_CALLBACKS |
11118 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11119 | | if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo); |
11120 | | #endif |
11121 | | |
11122 | 0 | if (sniff == NO_SNIFF) { |
11123 | | /* Actually check verify data. */ |
11124 | 0 | if (size > WC_MAX_DIGEST_SIZE || |
11125 | 0 | XMEMCMP(input + *inOutIdx, mac, size) != 0){ |
11126 | 0 | WOLFSSL_MSG("Verify finished error on hashes"); |
11127 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
11128 | 0 | WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR); |
11129 | 0 | return VERIFY_FINISHED_ERROR; |
11130 | 0 | } |
11131 | 0 | } |
11132 | | |
11133 | | /* Force input exhaustion at ProcessReply by consuming padSz. */ |
11134 | 0 | *inOutIdx += size + ssl->keys.padSz; |
11135 | |
|
11136 | 0 | #ifndef NO_WOLFSSL_SERVER |
11137 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
11138 | 0 | !ssl->options.handShakeDone) { |
11139 | | #ifdef WOLFSSL_EARLY_DATA |
11140 | | if (ssl->earlyData != no_early_data) { |
11141 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0) |
11142 | | return ret; |
11143 | | } |
11144 | | #endif |
11145 | | /* Setup keys for application data messages from client. */ |
11146 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11147 | 0 | return ret; |
11148 | 0 | } |
11149 | 0 | #endif |
11150 | | |
11151 | 0 | #ifndef NO_WOLFSSL_CLIENT |
11152 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11153 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11154 | 0 | #endif |
11155 | 0 | #ifndef NO_WOLFSSL_SERVER |
11156 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11157 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11158 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
11159 | 0 | ssl->options.handShakeDone = 1; |
11160 | 0 | } |
11161 | 0 | #endif |
11162 | |
|
11163 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_EARLY_DATA) |
11164 | | if (ssl->options.dtls && ssl->earlyData > early_data_ext) { |
11165 | | /* DTLSv1.3 has no EndOfearlydata messages. We stop processing EarlyData |
11166 | | as soon we receive the client's finished message */ |
11167 | | ssl->earlyData = done_early_data; |
11168 | | } |
11169 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_EARLY_DATA */ |
11170 | | #if defined(WOLFSSL_QUIC) && defined(WOLFSSL_EARLY_DATA) |
11171 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData > early_data_ext) { |
11172 | | /* QUIC has no EndOfEarlyData messages. We stop processing EarlyData |
11173 | | as soon we receive the client's finished message */ |
11174 | | ssl->earlyData = done_early_data; |
11175 | | } |
11176 | | #endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */ |
11177 | |
|
11178 | 0 | WOLFSSL_LEAVE("DoTls13Finished", 0); |
11179 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_DO); |
11180 | |
|
11181 | 0 | return 0; |
11182 | 0 | } |
11183 | | |
11184 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
11185 | | /* Send the TLS v1.3 Finished message. |
11186 | | * |
11187 | | * ssl The SSL/TLS object. |
11188 | | * returns 0 on success, otherwise failure. |
11189 | | */ |
11190 | | static int SendTls13Finished(WOLFSSL* ssl) |
11191 | | { |
11192 | | byte finishedSz = ssl->specs.hash_size; |
11193 | | byte* input; |
11194 | | byte* output; |
11195 | | int ret; |
11196 | | int headerSz = HANDSHAKE_HEADER_SZ; |
11197 | | int outputSz; |
11198 | | byte* secret; |
11199 | | |
11200 | | #ifdef WOLFSSL_DTLS13 |
11201 | | int dtlsRet = 0, isDtls = 0; |
11202 | | #endif /* WOLFSSL_DTLS13 */ |
11203 | | |
11204 | | WOLFSSL_START(WC_FUNC_FINISHED_SEND); |
11205 | | WOLFSSL_ENTER("SendTls13Finished"); |
11206 | | |
11207 | | ssl->options.buildingMsg = 1; |
11208 | | #ifdef WOLFSSL_DTLS13 |
11209 | | if (ssl->options.dtls) { |
11210 | | headerSz = DTLS_HANDSHAKE_HEADER_SZ; |
11211 | | /* using isDtls instead of ssl->options.dtls will abide clang static |
11212 | | analyzer on using an uninitialized value */ |
11213 | | isDtls = 1; |
11214 | | } |
11215 | | #endif /* WOLFSSL_DTLS13 */ |
11216 | | |
11217 | | outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA; |
11218 | | /* Check buffers are big enough and grow if needed. */ |
11219 | | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11220 | | return ret; |
11221 | | |
11222 | | /* get output buffer */ |
11223 | | output = GetOutputBuffer(ssl); |
11224 | | input = output + RECORD_HEADER_SZ; |
11225 | | |
11226 | | #ifdef WOLFSSL_DTLS13 |
11227 | | if (isDtls) |
11228 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11229 | | #endif /* WOLFSSL_DTLS13 */ |
11230 | | |
11231 | | AddTls13HandShakeHeader(input, (word32)finishedSz, 0, (word32)finishedSz, |
11232 | | finished, ssl); |
11233 | | |
11234 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11235 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11236 | | ret = tsip_Tls13SendFinished(ssl, output, outputSz, input, 1); |
11237 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11238 | | return ret; |
11239 | | } |
11240 | | ret = 0; |
11241 | | } |
11242 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11243 | | |
11244 | | /* make finished hashes */ |
11245 | | if (ssl->options.handShakeDone) { |
11246 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11247 | | ssl->keys.client_write_MAC_secret, |
11248 | | WOLFSSL_CLIENT_END); |
11249 | | if (ret != 0) |
11250 | | return ret; |
11251 | | |
11252 | | secret = ssl->keys.client_write_MAC_secret; |
11253 | | } |
11254 | | else if (ssl->options.side == WOLFSSL_CLIENT_END) |
11255 | | secret = ssl->keys.client_write_MAC_secret; |
11256 | | else { |
11257 | | /* All the handshake messages have been done to calculate client and |
11258 | | * server finished keys. |
11259 | | */ |
11260 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11261 | | ssl->keys.client_write_MAC_secret, |
11262 | | WOLFSSL_SERVER_END); |
11263 | | if (ret != 0) |
11264 | | return ret; |
11265 | | |
11266 | | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11267 | | ssl->keys.server_write_MAC_secret, |
11268 | | WOLFSSL_CLIENT_END); |
11269 | | if (ret != 0) |
11270 | | return ret; |
11271 | | |
11272 | | secret = ssl->keys.server_write_MAC_secret; |
11273 | | } |
11274 | | ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL); |
11275 | | if (ret != 0) |
11276 | | return ret; |
11277 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11278 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11279 | | XMEMCPY(ssl->clientFinished, &input[headerSz], finishedSz); |
11280 | | ssl->clientFinished_len = finishedSz; |
11281 | | } |
11282 | | else { |
11283 | | XMEMCPY(ssl->serverFinished, &input[headerSz], finishedSz); |
11284 | | ssl->serverFinished_len = finishedSz; |
11285 | | } |
11286 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11287 | | |
11288 | | #ifdef WOLFSSL_DTLS13 |
11289 | | if (isDtls) { |
11290 | | dtlsRet = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11291 | | (word16)(Dtls13GetRlHeaderLength(ssl, 1) + headerSz + finishedSz), finished, |
11292 | | 1); |
11293 | | if (dtlsRet != 0 && dtlsRet != WC_NO_ERR_TRACE(WANT_WRITE)) |
11294 | | return ret; |
11295 | | |
11296 | | } else |
11297 | | #endif /* WOLFSSL_DTLS13 */ |
11298 | | { |
11299 | | /* This message is always encrypted. */ |
11300 | | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11301 | | headerSz + finishedSz, handshake, 1, 0, 0); |
11302 | | if (sendSz < 0) { |
11303 | | WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR); |
11304 | | return BUILD_MSG_ERROR; |
11305 | | } |
11306 | | |
11307 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11308 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11309 | | if (ssl->toInfoOn) { |
11310 | | ret = AddPacketInfo(ssl, "Finished", handshake, output, sendSz, |
11311 | | WRITE_PROTO, 0, ssl->heap); |
11312 | | if (ret != 0) |
11313 | | return ret; |
11314 | | } |
11315 | | #endif |
11316 | | |
11317 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11318 | | ssl->options.buildingMsg = 0; |
11319 | | } |
11320 | | |
11321 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11322 | | #ifdef WOLFSSL_EARLY_DATA |
11323 | | byte storeTrafficDecKeys = ssl->earlyData == no_early_data; |
11324 | | #endif |
11325 | | /* Can send application data now. */ |
11326 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
11327 | | return ret; |
11328 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
11329 | | ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz); |
11330 | | #ifdef WOLFSSL_EARLY_DATA |
11331 | | |
11332 | | #ifdef WOLFSSL_DTLS13 |
11333 | | /* DTLS13 dynamically change keys and it needs all |
11334 | | the keys in ssl->keys to save the keying material */ |
11335 | | if (isDtls) |
11336 | | storeTrafficDecKeys = 1; |
11337 | | #endif /* WOLFSSL_DTLS13 */ |
11338 | | |
11339 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11340 | | != 0) { |
11341 | | return ret; |
11342 | | } |
11343 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY, |
11344 | | storeTrafficDecKeys)) != 0) { |
11345 | | return ret; |
11346 | | } |
11347 | | #else |
11348 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE, |
11349 | | 1)) != 0) { |
11350 | | return ret; |
11351 | | } |
11352 | | #endif |
11353 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11354 | | return ret; |
11355 | | |
11356 | | #ifdef WOLFSSL_DTLS13 |
11357 | | if (isDtls) { |
11358 | | w64wrapper epochTraffic0; |
11359 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11360 | | ssl->dtls13Epoch = epochTraffic0; |
11361 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11362 | | |
11363 | | ret = Dtls13SetEpochKeys( |
11364 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11365 | | if (ret != 0) |
11366 | | return ret; |
11367 | | |
11368 | | } |
11369 | | #endif /* WOLFSSL_DTLS13 */ |
11370 | | |
11371 | | } |
11372 | | |
11373 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
11374 | | !ssl->options.handShakeDone) { |
11375 | | #ifdef WOLFSSL_EARLY_DATA |
11376 | | if (ssl->earlyData != no_early_data) { |
11377 | | if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_SIDE_ONLY, |
11378 | | 1)) != 0) { |
11379 | | return ret; |
11380 | | } |
11381 | | } |
11382 | | #endif |
11383 | | /* Setup keys for application data messages. */ |
11384 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11385 | | return ret; |
11386 | | |
11387 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
11388 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
11389 | | if (ret != 0) |
11390 | | return ret; |
11391 | | #endif |
11392 | | |
11393 | | #ifdef WOLFSSL_DTLS13 |
11394 | | if (isDtls) { |
11395 | | w64wrapper epochTraffic0; |
11396 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11397 | | ssl->dtls13Epoch = epochTraffic0; |
11398 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11399 | | |
11400 | | ret = Dtls13SetEpochKeys( |
11401 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11402 | | if (ret != 0) |
11403 | | return ret; |
11404 | | |
11405 | | } |
11406 | | #endif /* WOLFSSL_DTLS13 */ |
11407 | | } |
11408 | | |
11409 | | #ifndef NO_WOLFSSL_CLIENT |
11410 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11411 | | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11412 | | ssl->options.handShakeState = HANDSHAKE_DONE; |
11413 | | ssl->options.handShakeDone = 1; |
11414 | | } |
11415 | | #endif |
11416 | | #ifndef NO_WOLFSSL_SERVER |
11417 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11418 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11419 | | } |
11420 | | #endif |
11421 | | |
11422 | | #ifdef WOLFSSL_DTLS13 |
11423 | | if (isDtls) { |
11424 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11425 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11426 | | |
11427 | | return dtlsRet; |
11428 | | } |
11429 | | #endif /* WOLFSSL_DTLS13 */ |
11430 | | |
11431 | | if ((ret = SendBuffered(ssl)) != 0) |
11432 | | return ret; |
11433 | | |
11434 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11435 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11436 | | |
11437 | | return ret; |
11438 | | } |
11439 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
11440 | | |
11441 | | /* handle generation TLS v1.3 key_update (24) */ |
11442 | | /* Send the TLS v1.3 KeyUpdate message. |
11443 | | * |
11444 | | * ssl The SSL/TLS object. |
11445 | | * returns 0 on success, otherwise failure. |
11446 | | */ |
11447 | | int SendTls13KeyUpdate(WOLFSSL* ssl) |
11448 | 0 | { |
11449 | 0 | byte* input; |
11450 | 0 | byte* output; |
11451 | 0 | int ret; |
11452 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
11453 | 0 | int outputSz; |
11454 | 0 | word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11455 | |
|
11456 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND); |
11457 | 0 | WOLFSSL_ENTER("SendTls13KeyUpdate"); |
11458 | |
|
11459 | | #ifdef WOLFSSL_DTLS13 |
11460 | | if (ssl->options.dtls) |
11461 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
11462 | | #endif /* WOLFSSL_DTLS13 */ |
11463 | |
|
11464 | 0 | outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA; |
11465 | | /* Check buffers are big enough and grow if needed. */ |
11466 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11467 | 0 | return ret; |
11468 | | |
11469 | | /* get output buffer */ |
11470 | 0 | output = GetOutputBuffer(ssl); |
11471 | 0 | input = output + RECORD_HEADER_SZ; |
11472 | |
|
11473 | | #ifdef WOLFSSL_DTLS13 |
11474 | | if (ssl->options.dtls) |
11475 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11476 | | #endif /* WOLFSSL_DTLS13 */ |
11477 | |
|
11478 | 0 | AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl); |
11479 | | |
11480 | | /* If: |
11481 | | * 1. I haven't sent a KeyUpdate requesting a response and |
11482 | | * 2. This isn't responding to peer KeyUpdate requiring a response then, |
11483 | | * I want a response. |
11484 | | */ |
11485 | 0 | ssl->keys.updateResponseReq = output[i++] = |
11486 | 0 | !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond; |
11487 | | /* Sent response, no longer need to respond. */ |
11488 | 0 | ssl->keys.keyUpdateRespond = 0; |
11489 | |
|
11490 | | #ifdef WOLFSSL_DTLS13 |
11491 | | if (ssl->options.dtls) { |
11492 | | ret = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11493 | | OPAQUE8_LEN + Dtls13GetRlHeaderLength(ssl, 1) + |
11494 | | DTLS_HANDSHAKE_HEADER_SZ, |
11495 | | key_update, 0); |
11496 | | } |
11497 | | else |
11498 | | #endif /* WOLFSSL_DTLS13 */ |
11499 | 0 | { |
11500 | | /* This message is always encrypted. */ |
11501 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11502 | 0 | headerSz + OPAQUE8_LEN, handshake, 0, 0, 0); |
11503 | 0 | if (sendSz < 0) |
11504 | 0 | return BUILD_MSG_ERROR; |
11505 | | |
11506 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11507 | | if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate"); |
11508 | | if (ssl->toInfoOn) { |
11509 | | ret = AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz, |
11510 | | WRITE_PROTO, 0, ssl->heap); |
11511 | | if (ret != 0) |
11512 | | return ret; |
11513 | | } |
11514 | | #endif |
11515 | | |
11516 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11517 | |
|
11518 | 0 | ret = SendBuffered(ssl); |
11519 | | |
11520 | |
|
11521 | 0 | if (ret != 0 && ret != WC_NO_ERR_TRACE(WANT_WRITE)) |
11522 | 0 | return ret; |
11523 | 0 | } |
11524 | | |
11525 | | /* In DTLS we must wait for the ack before setting up the new keys */ |
11526 | 0 | if (!ssl->options.dtls) { |
11527 | | |
11528 | | /* Future traffic uses new encryption keys. */ |
11529 | 0 | if ((ret = DeriveTls13Keys( |
11530 | 0 | ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11531 | 0 | != 0) |
11532 | 0 | return ret; |
11533 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11534 | 0 | return ret; |
11535 | 0 | } |
11536 | | |
11537 | | |
11538 | 0 | WOLFSSL_LEAVE("SendTls13KeyUpdate", ret); |
11539 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND); |
11540 | |
|
11541 | 0 | return ret; |
11542 | 0 | } |
11543 | | |
11544 | | /* handle processing TLS v1.3 key_update (24) */ |
11545 | | /* Parse and handle a TLS v1.3 KeyUpdate message. |
11546 | | * |
11547 | | * ssl The SSL/TLS object. |
11548 | | * input The message buffer. |
11549 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11550 | | * On exit, the index of byte after the Finished message and padding. |
11551 | | * totalSz The length of the current handshake message. |
11552 | | * returns 0 on success and otherwise failure. |
11553 | | */ |
11554 | | static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11555 | | word32 totalSz) |
11556 | 0 | { |
11557 | 0 | int ret; |
11558 | 0 | word32 i = *inOutIdx; |
11559 | |
|
11560 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO); |
11561 | 0 | WOLFSSL_ENTER("DoTls13KeyUpdate"); |
11562 | | |
11563 | | /* check against totalSz */ |
11564 | 0 | if (OPAQUE8_LEN != totalSz) |
11565 | 0 | return BUFFER_E; |
11566 | | |
11567 | 0 | switch (input[i]) { |
11568 | 0 | case update_not_requested: |
11569 | | /* This message in response to any outstanding request. */ |
11570 | 0 | ssl->keys.keyUpdateRespond = 0; |
11571 | 0 | ssl->keys.updateResponseReq = 0; |
11572 | 0 | break; |
11573 | 0 | case update_requested: |
11574 | | /* New key update requiring a response. */ |
11575 | 0 | ssl->keys.keyUpdateRespond = 1; |
11576 | 0 | break; |
11577 | 0 | default: |
11578 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
11579 | 0 | return INVALID_PARAMETER; |
11580 | 0 | } |
11581 | | |
11582 | | /* Move index to byte after message. */ |
11583 | 0 | *inOutIdx += totalSz; |
11584 | | /* Always encrypted. */ |
11585 | 0 | *inOutIdx += ssl->keys.padSz; |
11586 | | |
11587 | | /* Future traffic uses new decryption keys. */ |
11588 | 0 | if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1)) |
11589 | 0 | != 0) { |
11590 | 0 | return ret; |
11591 | 0 | } |
11592 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11593 | 0 | return ret; |
11594 | | |
11595 | | #ifdef WOLFSSL_DTLS13 |
11596 | | if (ssl->options.dtls) { |
11597 | | w64Increment(&ssl->dtls13PeerEpoch); |
11598 | | |
11599 | | ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY); |
11600 | | if (ret != 0) |
11601 | | return ret; |
11602 | | } |
11603 | | #endif /* WOLFSSL_DTLS13 */ |
11604 | | |
11605 | 0 | if (ssl->keys.keyUpdateRespond) { |
11606 | |
|
11607 | | #ifdef WOLFSSL_DTLS13 |
11608 | | /* we already sent a keyUpdate (either in response to a previous |
11609 | | KeyUpdate or initiated by the application) and we are waiting for the |
11610 | | ack. We can't send a new KeyUpdate right away but to honor the RFC we |
11611 | | should send another KeyUpdate after the one in-flight is acked. We |
11612 | | don't do that as it looks redundant, it will make the code more |
11613 | | complex and I don't see a good use case for that. */ |
11614 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) { |
11615 | | ssl->keys.keyUpdateRespond = 0; |
11616 | | return 0; |
11617 | | } |
11618 | | #endif /* WOLFSSL_DTLS13 */ |
11619 | |
|
11620 | 0 | #ifndef WOLFSSL_RW_THREADED |
11621 | 0 | return SendTls13KeyUpdate(ssl); |
11622 | | #else |
11623 | | ssl->options.sendKeyUpdate = 1; |
11624 | | return 0; |
11625 | | #endif |
11626 | 0 | } |
11627 | | |
11628 | 0 | WOLFSSL_LEAVE("DoTls13KeyUpdate", ret); |
11629 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO); |
11630 | |
|
11631 | 0 | return 0; |
11632 | 0 | } |
11633 | | |
11634 | | #ifdef WOLFSSL_EARLY_DATA |
11635 | | #ifndef NO_WOLFSSL_CLIENT |
11636 | | /* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no |
11637 | | * more early application data. |
11638 | | * The encryption key now changes to the pre-calculated handshake key. |
11639 | | * |
11640 | | * ssl The SSL/TLS object. |
11641 | | * returns 0 on success and otherwise failure. |
11642 | | */ |
11643 | | static int SendTls13EndOfEarlyData(WOLFSSL* ssl) |
11644 | | { |
11645 | | byte* output; |
11646 | | int ret; |
11647 | | int sendSz; |
11648 | | word32 length; |
11649 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11650 | | |
11651 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11652 | | WOLFSSL_ENTER("SendTls13EndOfEarlyData"); |
11653 | | |
11654 | | length = 0; |
11655 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
11656 | | ssl->options.buildingMsg = 1; |
11657 | | |
11658 | | /* Check buffers are big enough and grow if needed. */ |
11659 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
11660 | | return ret; |
11661 | | |
11662 | | /* Get position in output buffer to write new message to. */ |
11663 | | output = GetOutputBuffer(ssl); |
11664 | | |
11665 | | /* Put the record and handshake headers on. */ |
11666 | | AddTls13Headers(output, length, end_of_early_data, ssl); |
11667 | | |
11668 | | /* This message is always encrypted. */ |
11669 | | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
11670 | | idx - RECORD_HEADER_SZ, handshake, 1, 0, 0); |
11671 | | if (sendSz < 0) |
11672 | | return sendSz; |
11673 | | |
11674 | | ssl->buffers.outputBuffer.length += sendSz; |
11675 | | |
11676 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11677 | | return ret; |
11678 | | |
11679 | | ssl->options.buildingMsg = 0; |
11680 | | if (!ssl->options.groupMessages) |
11681 | | ret = SendBuffered(ssl); |
11682 | | |
11683 | | ssl->earlyData = done_early_data; |
11684 | | |
11685 | | WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret); |
11686 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11687 | | |
11688 | | return ret; |
11689 | | } |
11690 | | #endif /* !NO_WOLFSSL_CLIENT */ |
11691 | | |
11692 | | #ifndef NO_WOLFSSL_SERVER |
11693 | | /* handle processing of TLS 1.3 end_of_early_data (5) */ |
11694 | | /* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be |
11695 | | * no more early application data. |
11696 | | * The decryption key now changes to the pre-calculated handshake key. |
11697 | | * |
11698 | | * ssl The SSL/TLS object. |
11699 | | * returns 0 on success and otherwise failure. |
11700 | | */ |
11701 | | static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input, |
11702 | | word32* inOutIdx, word32 size) |
11703 | | { |
11704 | | int ret; |
11705 | | word32 begin = *inOutIdx; |
11706 | | |
11707 | | (void)input; |
11708 | | |
11709 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO); |
11710 | | WOLFSSL_ENTER("DoTls13EndOfEarlyData"); |
11711 | | |
11712 | | if ((*inOutIdx - begin) != size) |
11713 | | return BUFFER_ERROR; |
11714 | | |
11715 | | if (ssl->earlyData == no_early_data) { |
11716 | | WOLFSSL_MSG("EndOfEarlyData received unexpectedly"); |
11717 | | SendAlert(ssl, alert_fatal, unexpected_message); |
11718 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
11719 | | return OUT_OF_ORDER_E; |
11720 | | } |
11721 | | |
11722 | | ssl->earlyData = done_early_data; |
11723 | | |
11724 | | /* Always encrypted. */ |
11725 | | *inOutIdx += ssl->keys.padSz; |
11726 | | |
11727 | | ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY); |
11728 | | |
11729 | | WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret); |
11730 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO); |
11731 | | |
11732 | | return ret; |
11733 | | } |
11734 | | #endif /* !NO_WOLFSSL_SERVER */ |
11735 | | #endif /* WOLFSSL_EARLY_DATA */ |
11736 | | |
11737 | | #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11738 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
11739 | | int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce, |
11740 | | byte len) |
11741 | | { |
11742 | | if (session->ticketNonce.data |
11743 | | != session->ticketNonce.dataStatic) { |
11744 | | XFREE(session->ticketNonce.data, session->heap, |
11745 | | DYNAMIC_TYPE_SESSION_TICK); |
11746 | | session->ticketNonce.data = session->ticketNonce.dataStatic; |
11747 | | session->ticketNonce.len = 0; |
11748 | | } |
11749 | | |
11750 | | if (len > MAX_TICKET_NONCE_STATIC_SZ) { |
11751 | | WOLFSSL_MSG("Using dynamic nonce buffer"); |
11752 | | session->ticketNonce.data = (byte*)XMALLOC(len, |
11753 | | session->heap, DYNAMIC_TYPE_SESSION_TICK); |
11754 | | if (session->ticketNonce.data == NULL) |
11755 | | return MEMORY_ERROR; |
11756 | | } |
11757 | | XMEMCPY(session->ticketNonce.data, nonce, len); |
11758 | | session->ticketNonce.len = len; |
11759 | | return 0; |
11760 | | } |
11761 | | #endif |
11762 | | #ifndef NO_WOLFSSL_CLIENT |
11763 | | /* Handle a New Session Ticket handshake message. |
11764 | | * Message contains the information required to perform resumption. |
11765 | | * |
11766 | | * ssl The SSL/TLS object. |
11767 | | * input The message buffer. |
11768 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11769 | | * On exit, the index of byte after the Finished message and padding. |
11770 | | * size The length of the current handshake message. |
11771 | | * returns 0 on success, otherwise failure. |
11772 | | */ |
11773 | | static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input, |
11774 | | word32* inOutIdx, word32 size) |
11775 | 0 | { |
11776 | | #ifdef HAVE_SESSION_TICKET |
11777 | | int ret; |
11778 | | word32 begin = *inOutIdx; |
11779 | | word32 lifetime; |
11780 | | word32 ageAdd; |
11781 | | word16 length; |
11782 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
11783 | | word32 now; |
11784 | | #else |
11785 | | sword64 now; |
11786 | | #endif |
11787 | | const byte* nonce; |
11788 | | byte nonceLength; |
11789 | | |
11790 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO); |
11791 | | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
11792 | | |
11793 | | /* Lifetime hint. */ |
11794 | | if ((*inOutIdx - begin) + SESSION_HINT_SZ > size) |
11795 | | return BUFFER_ERROR; |
11796 | | ato32(input + *inOutIdx, &lifetime); |
11797 | | *inOutIdx += SESSION_HINT_SZ; |
11798 | | if (lifetime > MAX_LIFETIME) { |
11799 | | WOLFSSL_ERROR_VERBOSE(SERVER_HINT_ERROR); |
11800 | | return SERVER_HINT_ERROR; |
11801 | | } |
11802 | | |
11803 | | /* Age add. */ |
11804 | | if ((*inOutIdx - begin) + SESSION_ADD_SZ > size) |
11805 | | return BUFFER_ERROR; |
11806 | | ato32(input + *inOutIdx, &ageAdd); |
11807 | | *inOutIdx += SESSION_ADD_SZ; |
11808 | | |
11809 | | /* Ticket nonce. */ |
11810 | | if ((*inOutIdx - begin) + 1 > size) |
11811 | | return BUFFER_ERROR; |
11812 | | nonceLength = input[*inOutIdx]; |
11813 | | #if !defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11814 | | (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) |
11815 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
11816 | | WOLFSSL_MSG("Nonce length not supported"); |
11817 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
11818 | | return INVALID_PARAMETER; |
11819 | | } |
11820 | | #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */ |
11821 | | *inOutIdx += 1; |
11822 | | if ((*inOutIdx - begin) + nonceLength > size) |
11823 | | return BUFFER_ERROR; |
11824 | | nonce = input + *inOutIdx; |
11825 | | *inOutIdx += nonceLength; |
11826 | | |
11827 | | /* Ticket length. */ |
11828 | | if ((*inOutIdx - begin) + LENGTH_SZ > size) |
11829 | | return BUFFER_ERROR; |
11830 | | ato16(input + *inOutIdx, &length); |
11831 | | *inOutIdx += LENGTH_SZ; |
11832 | | if ((*inOutIdx - begin) + length > size) |
11833 | | return BUFFER_ERROR; |
11834 | | |
11835 | | if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0) |
11836 | | return ret; |
11837 | | *inOutIdx += length; |
11838 | | |
11839 | | now = TimeNowInMilliseconds(); |
11840 | | if (now == 0) |
11841 | | return GETTIME_ERROR; |
11842 | | /* Copy in ticket data (server identity). */ |
11843 | | ssl->timeout = lifetime; |
11844 | | ssl->session->timeout = lifetime; |
11845 | | ssl->session->cipherSuite0 = ssl->options.cipherSuite0; |
11846 | | ssl->session->cipherSuite = ssl->options.cipherSuite; |
11847 | | ssl->session->ticketSeen = now; |
11848 | | ssl->session->ticketAdd = ageAdd; |
11849 | | #ifdef WOLFSSL_EARLY_DATA |
11850 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
11851 | | #endif |
11852 | | |
11853 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11854 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
11855 | | ret = SessionTicketNoncePopulate(ssl->session, nonce, nonceLength); |
11856 | | if (ret != 0) |
11857 | | return ret; |
11858 | | #else |
11859 | | ssl->session->ticketNonce.len = nonceLength; |
11860 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
11861 | | ret = BUFFER_ERROR; |
11862 | | return ret; |
11863 | | } |
11864 | | if (nonceLength > 0) |
11865 | | XMEMCPY(ssl->session->ticketNonce.data, nonce, nonceLength); |
11866 | | #endif /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && FIPS_VERSION_GE(5,3) */ |
11867 | | |
11868 | | ssl->session->namedGroup = ssl->namedGroup; |
11869 | | |
11870 | | if ((*inOutIdx - begin) + EXTS_SZ > size) |
11871 | | return BUFFER_ERROR; |
11872 | | ato16(input + *inOutIdx, &length); |
11873 | | *inOutIdx += EXTS_SZ; |
11874 | | if ((*inOutIdx - begin) + length != size) |
11875 | | return BUFFER_ERROR; |
11876 | | #ifdef WOLFSSL_EARLY_DATA |
11877 | | ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket, |
11878 | | NULL); |
11879 | | if (ret != 0) |
11880 | | return ret; |
11881 | | #endif |
11882 | | *inOutIdx += length; |
11883 | | |
11884 | | SetupSession(ssl); |
11885 | | #ifndef NO_SESSION_CACHE |
11886 | | AddSession(ssl); |
11887 | | #endif |
11888 | | |
11889 | | /* Always encrypted. */ |
11890 | | *inOutIdx += ssl->keys.padSz; |
11891 | | |
11892 | | ssl->expect_session_ticket = 0; |
11893 | | #else |
11894 | 0 | (void)ssl; |
11895 | 0 | (void)input; |
11896 | |
|
11897 | 0 | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
11898 | |
|
11899 | 0 | *inOutIdx += size + ssl->keys.padSz; |
11900 | 0 | #endif /* HAVE_SESSION_TICKET */ |
11901 | |
|
11902 | 0 | WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0); |
11903 | 0 | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO); |
11904 | |
|
11905 | 0 | return 0; |
11906 | 0 | } |
11907 | | #endif /* NO_WOLFSSL_CLIENT */ |
11908 | | |
11909 | | #ifndef NO_WOLFSSL_SERVER |
11910 | | #ifdef HAVE_SESSION_TICKET |
11911 | | |
11912 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
11913 | | /* Offset of the MAC size in the finished message. */ |
11914 | | #define FINISHED_MSG_SIZE_OFFSET 3 |
11915 | | |
11916 | | /* Calculate the resumption secret which includes the unseen client finished |
11917 | | * message. |
11918 | | * |
11919 | | * ssl The SSL/TLS object. |
11920 | | * returns 0 on success, otherwise failure. |
11921 | | */ |
11922 | | static int ExpectedResumptionSecret(WOLFSSL* ssl) |
11923 | | { |
11924 | | int ret; |
11925 | | word32 finishedSz = 0; |
11926 | | byte mac[WC_MAX_DIGEST_SIZE]; |
11927 | | Digest digest; |
11928 | | static byte header[] = { 0x14, 0x00, 0x00, 0x00 }; |
11929 | | |
11930 | | /* Copy the running hash so we can restore it after. */ |
11931 | | switch (ssl->specs.mac_algorithm) { |
11932 | | #ifndef NO_SHA256 |
11933 | | case sha256_mac: |
11934 | | ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256); |
11935 | | if (ret != 0) |
11936 | | return ret; |
11937 | | break; |
11938 | | #endif |
11939 | | #ifdef WOLFSSL_SHA384 |
11940 | | case sha384_mac: |
11941 | | ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384); |
11942 | | if (ret != 0) |
11943 | | return ret; |
11944 | | break; |
11945 | | #endif |
11946 | | #ifdef WOLFSSL_TLS13_SHA512 |
11947 | | case sha512_mac: |
11948 | | ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512); |
11949 | | if (ret != 0) |
11950 | | return ret; |
11951 | | break; |
11952 | | #endif |
11953 | | #ifdef WOLFSSL_SM3 |
11954 | | case sm3_mac: |
11955 | | ret = wc_Sm3Copy(&ssl->hsHashes->hashSm3, &digest.sm3); |
11956 | | if (ret != 0) |
11957 | | return ret; |
11958 | | break; |
11959 | | #endif |
11960 | | } |
11961 | | |
11962 | | /* Generate the Client's Finished message and hash it. */ |
11963 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac, |
11964 | | &finishedSz); |
11965 | | if (ret != 0) |
11966 | | return ret; |
11967 | | header[FINISHED_MSG_SIZE_OFFSET] = finishedSz; |
11968 | | #ifdef WOLFSSL_EARLY_DATA |
11969 | | if (ssl->earlyData != no_early_data) { |
11970 | | static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 }; |
11971 | | ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData)); |
11972 | | if (ret != 0) |
11973 | | return ret; |
11974 | | } |
11975 | | #endif |
11976 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
11977 | | return ret; |
11978 | | if ((ret = HashRaw(ssl, mac, finishedSz)) != 0) |
11979 | | return ret; |
11980 | | |
11981 | | if ((ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret)) != 0) |
11982 | | return ret; |
11983 | | |
11984 | | /* Restore the hash inline with currently seen messages. */ |
11985 | | switch (ssl->specs.mac_algorithm) { |
11986 | | #ifndef NO_SHA256 |
11987 | | case sha256_mac: |
11988 | | wc_Sha256Free(&ssl->hsHashes->hashSha256); |
11989 | | ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256); |
11990 | | wc_Sha256Free(&digest.sha256); |
11991 | | if (ret != 0) |
11992 | | return ret; |
11993 | | break; |
11994 | | #endif |
11995 | | #ifdef WOLFSSL_SHA384 |
11996 | | case sha384_mac: |
11997 | | wc_Sha384Free(&ssl->hsHashes->hashSha384); |
11998 | | ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384); |
11999 | | wc_Sha384Free(&digest.sha384); |
12000 | | if (ret != 0) |
12001 | | return ret; |
12002 | | break; |
12003 | | #endif |
12004 | | #ifdef WOLFSSL_TLS13_SHA512 |
12005 | | case sha512_mac: |
12006 | | wc_Sha512Free(&ssl->hsHashes->hashSha512); |
12007 | | ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512); |
12008 | | wc_Sha512Free(&digest.sha512); |
12009 | | if (ret != 0) |
12010 | | return ret; |
12011 | | break; |
12012 | | #endif |
12013 | | #ifdef WOLFSSL_SM3 |
12014 | | case sm3_mac: |
12015 | | wc_Sm3Free(&ssl->hsHashes->hashSm3); |
12016 | | ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3); |
12017 | | wc_Sm3Free(&digest.sm3); |
12018 | | if (ret != 0) |
12019 | | return ret; |
12020 | | break; |
12021 | | #endif |
12022 | | } |
12023 | | |
12024 | | return ret; |
12025 | | } |
12026 | | #endif |
12027 | | |
12028 | | /* Send New Session Ticket handshake message. |
12029 | | * Message contains the information required to perform resumption. |
12030 | | * |
12031 | | * ssl The SSL/TLS object. |
12032 | | * returns 0 on success, otherwise failure. |
12033 | | */ |
12034 | | static int SendTls13NewSessionTicket(WOLFSSL* ssl) |
12035 | | { |
12036 | | byte* output; |
12037 | | int ret; |
12038 | | word32 length; |
12039 | | int sendSz; |
12040 | | word16 extSz; |
12041 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
12042 | | |
12043 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12044 | | WOLFSSL_ENTER("SendTls13NewSessionTicket"); |
12045 | | |
12046 | | #ifdef WOLFSSL_DTLS13 |
12047 | | if (ssl->options.dtls) |
12048 | | idx = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
12049 | | #endif /* WOLFSSL_DTLS13 */ |
12050 | | |
12051 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12052 | | if (!ssl->msgsReceived.got_finished) { |
12053 | | if ((ret = ExpectedResumptionSecret(ssl)) != 0) |
12054 | | return ret; |
12055 | | } |
12056 | | #endif |
12057 | | |
12058 | | /* Start ticket nonce at 0 and go up to 255. */ |
12059 | | if (ssl->session->ticketNonce.len == 0) { |
12060 | | ssl->session->ticketNonce.len = DEF_TICKET_NONCE_SZ; |
12061 | | ssl->session->ticketNonce.data[0] = 0; |
12062 | | } |
12063 | | else |
12064 | | #ifdef WOLFSSL_ASYNC_CRYPT |
12065 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
12066 | | #endif |
12067 | | { |
12068 | | ssl->session->ticketNonce.data[0]++; |
12069 | | } |
12070 | | |
12071 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12072 | | /* In this case we only send the ID as the ticket. Let's generate a new |
12073 | | * ID for the new ticket so that we don't overwrite any old ones */ |
12074 | | ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID, |
12075 | | ID_LEN); |
12076 | | if (ret != 0) |
12077 | | return ret; |
12078 | | ssl->session->haveAltSessionID = 1; |
12079 | | } |
12080 | | |
12081 | | if (!ssl->options.noTicketTls13) { |
12082 | | if ((ret = SetupTicket(ssl)) != 0) |
12083 | | return ret; |
12084 | | /* No need to create the ticket if we only send the ID */ |
12085 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == 0) { |
12086 | | if ((ret = CreateTicket(ssl)) != 0) |
12087 | | return ret; |
12088 | | } |
12089 | | } |
12090 | | |
12091 | | #ifdef WOLFSSL_EARLY_DATA |
12092 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
12093 | | if (ssl->session->maxEarlyDataSz > 0) |
12094 | | TLSX_EarlyData_Use(ssl, ssl->session->maxEarlyDataSz, 1); |
12095 | | extSz = 0; |
12096 | | ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz); |
12097 | | if (ret != 0) |
12098 | | return ret; |
12099 | | #else |
12100 | | extSz = EXTS_SZ; |
12101 | | #endif |
12102 | | /* Lifetime | Age Add | Ticket session ID | Extensions */ |
12103 | | length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ; |
12104 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) |
12105 | | length += ID_LEN + extSz; |
12106 | | else |
12107 | | length += ssl->session->ticketLen + extSz; |
12108 | | /* Nonce */ |
12109 | | length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ; |
12110 | | |
12111 | | sendSz = (word16)(idx + length + MAX_MSG_EXTRA); |
12112 | | |
12113 | | /* Check buffers are big enough and grow if needed. */ |
12114 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
12115 | | return ret; |
12116 | | |
12117 | | /* Get position in output buffer to write new message to. */ |
12118 | | output = GetOutputBuffer(ssl); |
12119 | | |
12120 | | /* Put the record and handshake headers on. */ |
12121 | | AddTls13Headers(output, length, session_ticket, ssl); |
12122 | | |
12123 | | /* Lifetime hint */ |
12124 | | c32toa(ssl->ctx->ticketHint, output + idx); |
12125 | | idx += SESSION_HINT_SZ; |
12126 | | /* Age add - obfuscator */ |
12127 | | c32toa(ssl->session->ticketAdd, output + idx); |
12128 | | idx += SESSION_ADD_SZ; |
12129 | | |
12130 | | output[idx++] = ssl->session->ticketNonce.len; |
12131 | | output[idx++] = ssl->session->ticketNonce.data[0]; |
12132 | | |
12133 | | /* length */ |
12134 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12135 | | c16toa(ID_LEN, output + idx); |
12136 | | } |
12137 | | else { |
12138 | | c16toa(ssl->session->ticketLen, output + idx); |
12139 | | } |
12140 | | |
12141 | | idx += LENGTH_SZ; |
12142 | | /* ticket */ |
12143 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12144 | | if (ssl->session->haveAltSessionID) |
12145 | | XMEMCPY(output + idx, ssl->session->altSessionID, ID_LEN); |
12146 | | else |
12147 | | return BAD_FUNC_ARG; /* Should not happen */ |
12148 | | idx += ID_LEN; |
12149 | | } |
12150 | | else { |
12151 | | XMEMCPY(output + idx, ssl->session->ticket, ssl->session->ticketLen); |
12152 | | idx += ssl->session->ticketLen; |
12153 | | } |
12154 | | |
12155 | | #ifdef WOLFSSL_EARLY_DATA |
12156 | | extSz = 0; |
12157 | | ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz); |
12158 | | if (ret != 0) |
12159 | | return ret; |
12160 | | idx += extSz; |
12161 | | #else |
12162 | | /* No extension support - empty extensions. */ |
12163 | | c16toa(0, output + idx); |
12164 | | idx += EXTS_SZ; |
12165 | | #endif |
12166 | | |
12167 | | if (idx > WOLFSSL_MAX_16BIT) { |
12168 | | return BAD_LENGTH_E; |
12169 | | } |
12170 | | |
12171 | | ssl->options.haveSessionId = 1; |
12172 | | |
12173 | | SetupSession(ssl); |
12174 | | /* Only add to cache when support built in and when the ticket contains |
12175 | | * an ID. Otherwise we have no way to actually retrieve the ticket from the |
12176 | | * cache. */ |
12177 | | #if !defined(NO_SESSION_CACHE) && defined(WOLFSSL_TICKET_HAVE_ID) |
12178 | | AddSession(ssl); |
12179 | | #endif |
12180 | | |
12181 | | #ifdef WOLFSSL_DTLS13 |
12182 | | if (ssl->options.dtls) |
12183 | | return Dtls13HandshakeSend(ssl, output, (word16)sendSz, |
12184 | | (word16)idx, session_ticket, 0); |
12185 | | #endif /* WOLFSSL_DTLS13 */ |
12186 | | |
12187 | | /* This message is always encrypted. */ |
12188 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
12189 | | output + RECORD_HEADER_SZ, |
12190 | | (word16)idx - RECORD_HEADER_SZ, |
12191 | | handshake, 0, 0, 0); |
12192 | | if (sendSz < 0) |
12193 | | return sendSz; |
12194 | | |
12195 | | ssl->buffers.outputBuffer.length += sendSz; |
12196 | | |
12197 | | /* Always send as this is either directly after server's Finished or only |
12198 | | * message after client's Finished. |
12199 | | */ |
12200 | | ret = SendBuffered(ssl); |
12201 | | |
12202 | | WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0); |
12203 | | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12204 | | |
12205 | | return ret; |
12206 | | } |
12207 | | #endif /* HAVE_SESSION_TICKET */ |
12208 | | #endif /* NO_WOLFSSL_SERVER */ |
12209 | | |
12210 | | /* Make sure no duplicates, no fast forward, or other problems |
12211 | | * |
12212 | | * ssl The SSL/TLS object. |
12213 | | * type Type of handshake message received. |
12214 | | * returns 0 on success, otherwise failure. |
12215 | | */ |
12216 | | static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type) |
12217 | 0 | { |
12218 | | /* verify not a duplicate, mark received, check state */ |
12219 | 0 | switch (type) { |
12220 | | |
12221 | 0 | #ifndef NO_WOLFSSL_SERVER |
12222 | 0 | case client_hello: |
12223 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12224 | | /* Only valid when received on SERVER side. */ |
12225 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12226 | 0 | WOLFSSL_MSG("ClientHello received by client"); |
12227 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12228 | 0 | return SIDE_ERROR; |
12229 | 0 | } |
12230 | 0 | #endif |
12231 | | /* Check state. */ |
12232 | 0 | if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) { |
12233 | 0 | WOLFSSL_MSG("ClientHello received out of order"); |
12234 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12235 | 0 | return OUT_OF_ORDER_E; |
12236 | 0 | } |
12237 | | /* Check previously seen. */ |
12238 | | /* Initial and after HelloRetryRequest - no more than 2. */ |
12239 | 0 | if (ssl->msgsReceived.got_client_hello == 2) { |
12240 | 0 | WOLFSSL_MSG("Too many ClientHello received"); |
12241 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12242 | 0 | return DUPLICATE_MSG_E; |
12243 | 0 | } |
12244 | | /* Second only after HelloRetryRequest seen. */ |
12245 | 0 | if (ssl->msgsReceived.got_client_hello == 1 && |
12246 | 0 | ssl->options.serverState != |
12247 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
12248 | 0 | WOLFSSL_MSG("Duplicate ClientHello received"); |
12249 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12250 | 0 | return DUPLICATE_MSG_E; |
12251 | 0 | } |
12252 | 0 | ssl->msgsReceived.got_client_hello++; |
12253 | |
|
12254 | 0 | break; |
12255 | 0 | #endif |
12256 | | |
12257 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12258 | 0 | case server_hello: |
12259 | 0 | #ifndef NO_WOLFSSL_SERVER |
12260 | | /* Only valid when received on CLIENT side. */ |
12261 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12262 | 0 | WOLFSSL_MSG("ServerHello received by server"); |
12263 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12264 | 0 | return SIDE_ERROR; |
12265 | 0 | } |
12266 | 0 | #endif |
12267 | | /* Check state. */ |
12268 | 0 | if (ssl->options.serverState >= SERVER_HELLO_COMPLETE) { |
12269 | 0 | WOLFSSL_MSG("ServerHello received out of order"); |
12270 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12271 | 0 | return OUT_OF_ORDER_E; |
12272 | 0 | } |
12273 | | /* Check previously seen. */ |
12274 | | /* Only once after ClientHello. |
12275 | | * HelloRetryRequest has ServerHello type but count fixed up later |
12276 | | * - see DoTls13ServerHello(). |
12277 | | */ |
12278 | 0 | if (ssl->msgsReceived.got_server_hello) { |
12279 | 0 | WOLFSSL_MSG("Duplicate ServerHello received"); |
12280 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12281 | 0 | return DUPLICATE_MSG_E; |
12282 | 0 | } |
12283 | 0 | ssl->msgsReceived.got_server_hello = 1; |
12284 | |
|
12285 | 0 | break; |
12286 | 0 | #endif |
12287 | | |
12288 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12289 | 0 | case session_ticket: |
12290 | 0 | #ifndef NO_WOLFSSL_SERVER |
12291 | | /* Only valid when received on CLIENT side. */ |
12292 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12293 | 0 | WOLFSSL_MSG("NewSessionTicket received by server"); |
12294 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12295 | 0 | return SIDE_ERROR; |
12296 | 0 | } |
12297 | 0 | #endif |
12298 | | /* Check state. */ |
12299 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12300 | | /* Only allowed after server's Finished message. */ |
12301 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12302 | | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12303 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12304 | | return OUT_OF_ORDER_E; |
12305 | | } |
12306 | | #else |
12307 | | /* Only allowed after client's Finished message. */ |
12308 | 0 | if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
12309 | 0 | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12310 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12311 | 0 | return OUT_OF_ORDER_E; |
12312 | 0 | } |
12313 | 0 | #endif |
12314 | | /* Many SessionTickets can be sent. */ |
12315 | 0 | ssl->msgsReceived.got_session_ticket = 1; |
12316 | |
|
12317 | 0 | break; |
12318 | 0 | #endif |
12319 | | |
12320 | 0 | #ifndef NO_WOLFSSL_SERVER |
12321 | | #ifdef WOLFSSL_EARLY_DATA |
12322 | | case end_of_early_data: |
12323 | | #ifndef NO_WOLFSSL_CLIENT |
12324 | | /* Only valid when received on SERVER side. */ |
12325 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12326 | | WOLFSSL_MSG("EndOfEarlyData received by client"); |
12327 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12328 | | return SIDE_ERROR; |
12329 | | } |
12330 | | #endif |
12331 | | /* Check state. */ |
12332 | | /* Only after server's Finished and before client's Finished. */ |
12333 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12334 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12335 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12336 | | return OUT_OF_ORDER_E; |
12337 | | } |
12338 | | if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) { |
12339 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12340 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12341 | | return OUT_OF_ORDER_E; |
12342 | | } |
12343 | | /* Check previously seen. */ |
12344 | | if (ssl->msgsReceived.got_end_of_early_data) { |
12345 | | WOLFSSL_MSG("Too many EndOfEarlyData received"); |
12346 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12347 | | return DUPLICATE_MSG_E; |
12348 | | } |
12349 | | ssl->msgsReceived.got_end_of_early_data = 1; |
12350 | | |
12351 | | break; |
12352 | | #endif |
12353 | 0 | #endif |
12354 | | |
12355 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12356 | 0 | case encrypted_extensions: |
12357 | 0 | #ifndef NO_WOLFSSL_SERVER |
12358 | | /* Only valid when received on CLIENT side. */ |
12359 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12360 | 0 | WOLFSSL_MSG("EncryptedExtensions received by server"); |
12361 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12362 | 0 | return SIDE_ERROR; |
12363 | 0 | } |
12364 | 0 | #endif |
12365 | | /* Check state. */ |
12366 | | /* Must be received directly after ServerHello. |
12367 | | * DoTls13EncryptedExtensions() changes state to: |
12368 | | * SERVER_ENCRYPTED_EXTENSIONS_COMPLETE. |
12369 | | */ |
12370 | 0 | if (ssl->options.serverState != SERVER_HELLO_COMPLETE) { |
12371 | 0 | WOLFSSL_MSG("EncryptedExtensions received out of order"); |
12372 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12373 | 0 | return OUT_OF_ORDER_E; |
12374 | 0 | } |
12375 | | /* Check previously seen. */ |
12376 | 0 | if (ssl->msgsReceived.got_encrypted_extensions) { |
12377 | 0 | WOLFSSL_MSG("Duplicate EncryptedExtensions received"); |
12378 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12379 | 0 | return DUPLICATE_MSG_E; |
12380 | 0 | } |
12381 | 0 | ssl->msgsReceived.got_encrypted_extensions = 1; |
12382 | |
|
12383 | 0 | break; |
12384 | 0 | #endif |
12385 | | |
12386 | 0 | case certificate: |
12387 | | /* Valid on both sides. */ |
12388 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12389 | | /* Check state. */ |
12390 | | /* On client, seen after EncryptedExtension and CertificateRequest |
12391 | | * (if sent) and before CertificateVerify and Finished. |
12392 | | * DoTls13Certificate() sets serverState to SERVER_CERT_COMPLETE. |
12393 | | */ |
12394 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12395 | 0 | ssl->options.serverState != |
12396 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12397 | 0 | WOLFSSL_MSG("Certificate received out of order - Client"); |
12398 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12399 | 0 | return OUT_OF_ORDER_E; |
12400 | 0 | } |
12401 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12402 | | /* Server's authenticating with PSK must not send this. */ |
12403 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12404 | | ssl->options.serverState == SERVER_CERT_COMPLETE && |
12405 | | ssl->options.pskNegotiated) { |
12406 | | WOLFSSL_MSG("Certificate received while using PSK"); |
12407 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12408 | | return SANITY_MSG_E; |
12409 | | } |
12410 | | #endif |
12411 | 0 | #endif |
12412 | 0 | #ifndef NO_WOLFSSL_SERVER |
12413 | | /* Check state. */ |
12414 | | /* On Server, valid after ClientHello received and ServerFinished |
12415 | | * sent. */ |
12416 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
12417 | 0 | ssl->options.clientState != CLIENT_HELLO_COMPLETE && |
12418 | 0 | ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12419 | 0 | WOLFSSL_MSG("Certificate received out of order - Server"); |
12420 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12421 | 0 | return OUT_OF_ORDER_E; |
12422 | 0 | } |
12423 | 0 | #endif |
12424 | | /* Check previously seen. */ |
12425 | 0 | if (ssl->msgsReceived.got_certificate) { |
12426 | 0 | WOLFSSL_MSG("Duplicate Certificate received"); |
12427 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12428 | 0 | return DUPLICATE_MSG_E; |
12429 | 0 | } |
12430 | 0 | ssl->msgsReceived.got_certificate = 1; |
12431 | |
|
12432 | 0 | break; |
12433 | | |
12434 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12435 | 0 | case certificate_request: |
12436 | 0 | #ifndef NO_WOLFSSL_SERVER |
12437 | | /* Only valid when received on CLIENT side. */ |
12438 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12439 | 0 | WOLFSSL_MSG("CertificateRequest received by server"); |
12440 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12441 | 0 | return SIDE_ERROR; |
12442 | 0 | } |
12443 | 0 | #endif |
12444 | | /* Check state. */ |
12445 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12446 | | /* Only valid when sent after EncryptedExtensions and before |
12447 | | * Certificate. */ |
12448 | 0 | if (ssl->options.serverState != |
12449 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12450 | 0 | WOLFSSL_MSG("CertificateRequest received out of order"); |
12451 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12452 | 0 | return OUT_OF_ORDER_E; |
12453 | 0 | } |
12454 | | #else |
12455 | | /* Valid when sent after EncryptedExtensions and before Certificate |
12456 | | * and after both client and server have sent Finished (Post |
12457 | | * Handshake Authentication). */ |
12458 | | if (ssl->options.serverState != |
12459 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE && |
12460 | | (ssl->options.serverState < SERVER_FINISHED_COMPLETE || |
12461 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) { |
12462 | | WOLFSSL_MSG("CertificateRequest received out of order"); |
12463 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12464 | | return OUT_OF_ORDER_E; |
12465 | | } |
12466 | | #endif |
12467 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12468 | | /* Server's authenticating with PSK must not send this. */ |
12469 | | if (ssl->options.pskNegotiated) { |
12470 | | WOLFSSL_MSG("CertificateRequest received while using PSK"); |
12471 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12472 | | return SANITY_MSG_E; |
12473 | | } |
12474 | | #endif |
12475 | | /* Check previously seen. */ |
12476 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12477 | | /* Only once during handshake. */ |
12478 | 0 | if (ssl->msgsReceived.got_certificate_request) { |
12479 | 0 | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12480 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12481 | 0 | return DUPLICATE_MSG_E; |
12482 | 0 | } |
12483 | | #else |
12484 | | /* Only once during handshake. */ |
12485 | | if (ssl->msgsReceived.got_certificate_request && |
12486 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE) { |
12487 | | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12488 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12489 | | return DUPLICATE_MSG_E; |
12490 | | } |
12491 | | #endif |
12492 | 0 | ssl->msgsReceived.got_certificate_request = 1; |
12493 | |
|
12494 | 0 | break; |
12495 | 0 | #endif |
12496 | | |
12497 | 0 | case certificate_verify: |
12498 | | /* Valid on both sides. */ |
12499 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12500 | | /* Check state on client. |
12501 | | * Valid only directly after a Certificate message. */ |
12502 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12503 | 0 | if (ssl->options.serverState != SERVER_CERT_COMPLETE) { |
12504 | 0 | WOLFSSL_MSG("No Cert before CertVerify"); |
12505 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12506 | 0 | return OUT_OF_ORDER_E; |
12507 | 0 | } |
12508 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12509 | | /* Server's authenticating with PSK must not send this. */ |
12510 | | if (ssl->options.pskNegotiated) { |
12511 | | WOLFSSL_MSG("CertificateVerify received while using PSK"); |
12512 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12513 | | return SANITY_MSG_E; |
12514 | | } |
12515 | | #endif |
12516 | 0 | } |
12517 | 0 | #endif |
12518 | 0 | #ifndef NO_WOLFSSL_SERVER |
12519 | | /* Check state on server. */ |
12520 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12521 | | /* Server must have sent Finished message. */ |
12522 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12523 | 0 | WOLFSSL_MSG("CertificateVerify received out of order"); |
12524 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12525 | 0 | return OUT_OF_ORDER_E; |
12526 | 0 | } |
12527 | | /* Valid only directly after a Certificate message. */ |
12528 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12529 | 0 | WOLFSSL_MSG("CertificateVerify before ClientHello done"); |
12530 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12531 | 0 | return OUT_OF_ORDER_E; |
12532 | 0 | } |
12533 | 0 | if (!ssl->msgsReceived.got_certificate) { |
12534 | 0 | WOLFSSL_MSG("No Cert before CertificateVerify"); |
12535 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12536 | 0 | return OUT_OF_ORDER_E; |
12537 | 0 | } |
12538 | 0 | } |
12539 | 0 | #endif |
12540 | | /* Check previously seen. */ |
12541 | 0 | if (ssl->msgsReceived.got_certificate_verify) { |
12542 | 0 | WOLFSSL_MSG("Duplicate CertificateVerify received"); |
12543 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12544 | 0 | return DUPLICATE_MSG_E; |
12545 | 0 | } |
12546 | 0 | ssl->msgsReceived.got_certificate_verify = 1; |
12547 | |
|
12548 | 0 | break; |
12549 | | |
12550 | 0 | case finished: |
12551 | | /* Valid on both sides. */ |
12552 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12553 | | /* Check state on client. */ |
12554 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12555 | | /* After sending ClientHello */ |
12556 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12557 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12558 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12559 | 0 | return OUT_OF_ORDER_E; |
12560 | 0 | } |
12561 | | /* Must have seen certificate and verify from server except when |
12562 | | * using PSK. */ |
12563 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12564 | | if (ssl->options.pskNegotiated) { |
12565 | | if (ssl->options.serverState != |
12566 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12567 | | WOLFSSL_MSG("Finished received out of order - PSK"); |
12568 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12569 | | return OUT_OF_ORDER_E; |
12570 | | } |
12571 | | } |
12572 | | else |
12573 | | #endif |
12574 | 0 | if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) { |
12575 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12576 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12577 | 0 | return OUT_OF_ORDER_E; |
12578 | 0 | } |
12579 | 0 | } |
12580 | 0 | #endif |
12581 | 0 | #ifndef NO_WOLFSSL_SERVER |
12582 | | /* Check state on server. */ |
12583 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12584 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12585 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12586 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12587 | 0 | return OUT_OF_ORDER_E; |
12588 | 0 | } |
12589 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12590 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12591 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12592 | 0 | return OUT_OF_ORDER_E; |
12593 | 0 | } |
12594 | | #ifdef WOLFSSL_EARLY_DATA |
12595 | | if (ssl->earlyData == process_early_data && |
12596 | | /* early data may be lost when using DTLS */ |
12597 | | !ssl->options.dtls |
12598 | | /* QUIC does not use EndOfEarlyData records */ |
12599 | | && !WOLFSSL_IS_QUIC(ssl)) { |
12600 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12601 | | return OUT_OF_ORDER_E; |
12602 | | } |
12603 | | #endif |
12604 | 0 | } |
12605 | 0 | #endif |
12606 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12607 | | if (!ssl->options.pskNegotiated) |
12608 | | #endif |
12609 | 0 | { |
12610 | | /* Must have received a Certificate message from client if |
12611 | | * verifying the peer. Empty certificate message indicates |
12612 | | * no certificate available. |
12613 | | */ |
12614 | 0 | if (ssl->options.verifyPeer && |
12615 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
12616 | | !ssl->options.verifyPostHandshake && |
12617 | | #endif |
12618 | 0 | !ssl->msgsReceived.got_certificate) { |
12619 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12620 | 0 | "missing Certificate message"); |
12621 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12622 | 0 | return OUT_OF_ORDER_E; |
12623 | 0 | } |
12624 | | /* Mutual authentication on server requires a certificate from |
12625 | | * peer. Verify peer set on client side requires a certificate |
12626 | | * from peer as not doing PSK. |
12627 | | */ |
12628 | 0 | if ((ssl->options.mutualAuth || |
12629 | 0 | (ssl->options.side == WOLFSSL_CLIENT_END && |
12630 | 0 | ssl->options.verifyPeer)) && !ssl->options.havePeerCert) { |
12631 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12632 | 0 | "no valid certificate"); |
12633 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12634 | 0 | return OUT_OF_ORDER_E; |
12635 | 0 | } |
12636 | | /* Must have received a valid CertificateVerify if verifying |
12637 | | * peer and got a peer certificate. |
12638 | | */ |
12639 | 0 | if ((ssl->options.mutualAuth || ssl->options.verifyPeer) && |
12640 | 0 | ssl->options.havePeerCert && !ssl->options.havePeerVerify) { |
12641 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12642 | 0 | "Certificate message but no CertificateVerify"); |
12643 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12644 | 0 | return OUT_OF_ORDER_E; |
12645 | 0 | } |
12646 | 0 | } |
12647 | | /* Check previously seen. */ |
12648 | 0 | if (ssl->msgsReceived.got_finished) { |
12649 | 0 | WOLFSSL_MSG("Duplicate Finished received"); |
12650 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12651 | 0 | return DUPLICATE_MSG_E; |
12652 | 0 | } |
12653 | 0 | ssl->msgsReceived.got_finished = 1; |
12654 | |
|
12655 | 0 | break; |
12656 | | |
12657 | 0 | case key_update: |
12658 | | /* Valid on both sides. */ |
12659 | | /* Check state. |
12660 | | * Client and server must have received finished message from other |
12661 | | * side. |
12662 | | */ |
12663 | 0 | if (!ssl->msgsReceived.got_finished) { |
12664 | 0 | WOLFSSL_MSG("No KeyUpdate before Finished"); |
12665 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12666 | 0 | return OUT_OF_ORDER_E; |
12667 | 0 | } |
12668 | | /* Multiple KeyUpdates can be sent. */ |
12669 | 0 | break; |
12670 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
12671 | | case hello_verify_request: |
12672 | | if (!ssl->options.dtls) { |
12673 | | WOLFSSL_MSG("HelloVerifyRequest when not in DTLS"); |
12674 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12675 | | return OUT_OF_ORDER_E; |
12676 | | } |
12677 | | if (ssl->msgsReceived.got_hello_verify_request) { |
12678 | | WOLFSSL_MSG("Duplicate HelloVerifyRequest received"); |
12679 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12680 | | return DUPLICATE_MSG_E; |
12681 | | } |
12682 | | ssl->msgsReceived.got_hello_verify_request = 1; |
12683 | | if (ssl->msgsReceived.got_hello_retry_request) { |
12684 | | WOLFSSL_MSG( |
12685 | | "Both HelloVerifyRequest and HelloRetryRequest received"); |
12686 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12687 | | return DUPLICATE_MSG_E; |
12688 | | } |
12689 | | if (ssl->options.serverState >= |
12690 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE || |
12691 | | ssl->options.connectState != CLIENT_HELLO_SENT) { |
12692 | | WOLFSSL_MSG("HelloVerifyRequest received out of order"); |
12693 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12694 | | return OUT_OF_ORDER_E; |
12695 | | } |
12696 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12697 | | WOLFSSL_MSG("HelloVerifyRequest received on the server"); |
12698 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12699 | | return SIDE_ERROR; |
12700 | | } |
12701 | | if (!ssl->options.downgrade || |
12702 | | ssl->options.minDowngrade < DTLSv1_2_MINOR) { |
12703 | | WOLFSSL_MSG( |
12704 | | "HelloVerifyRequest received but not DTLSv1.2 allowed"); |
12705 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
12706 | | return VERSION_ERROR; |
12707 | | } |
12708 | | break; |
12709 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_TLS12*/ |
12710 | | |
12711 | 0 | default: |
12712 | 0 | WOLFSSL_MSG("Unknown message type"); |
12713 | 0 | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12714 | 0 | return SANITY_MSG_E; |
12715 | 0 | } |
12716 | | |
12717 | 0 | return 0; |
12718 | 0 | } |
12719 | | |
12720 | | /* Handle a type of handshake message that has been received. |
12721 | | * |
12722 | | * ssl The SSL/TLS object. |
12723 | | * input The message buffer. |
12724 | | * inOutIdx On entry, the index into the buffer of the current message. |
12725 | | * On exit, the index into the buffer of the next message. |
12726 | | * size The length of the current handshake message. |
12727 | | * totalSz Length of remaining data in the message buffer. |
12728 | | * returns 0 on success and otherwise failure. |
12729 | | */ |
12730 | | int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
12731 | | byte type, word32 size, word32 totalSz) |
12732 | | { |
12733 | | int ret = 0, tmp; |
12734 | | word32 inIdx = *inOutIdx; |
12735 | | int alertType; |
12736 | | #if defined(HAVE_ECH) |
12737 | | TLSX* echX = NULL; |
12738 | | word32 echInOutIdx; |
12739 | | #endif |
12740 | | |
12741 | | (void)totalSz; |
12742 | | |
12743 | | WOLFSSL_ENTER("DoTls13HandShakeMsgType"); |
12744 | | |
12745 | | /* make sure we can read the message */ |
12746 | | if (*inOutIdx + size > totalSz) |
12747 | | return INCOMPLETE_DATA; |
12748 | | |
12749 | | /* sanity check msg received */ |
12750 | | if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) { |
12751 | | WOLFSSL_MSG("Sanity Check on handshake message type received failed"); |
12752 | | if (ret == WC_NO_ERR_TRACE(VERSION_ERROR)) |
12753 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
12754 | | else |
12755 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12756 | | return ret; |
12757 | | } |
12758 | | |
12759 | | #if defined(WOLFSSL_CALLBACKS) |
12760 | | /* add name later, add on record and handshake header part back on */ |
12761 | | if (ssl->toInfoOn) { |
12762 | | ret = AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - |
12763 | | HANDSHAKE_HEADER_SZ, size + HANDSHAKE_HEADER_SZ, READ_PROTO, |
12764 | | RECORD_HEADER_SZ, ssl->heap); |
12765 | | if (ret != 0) |
12766 | | return ret; |
12767 | | AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo); |
12768 | | } |
12769 | | #endif |
12770 | | |
12771 | | if (ssl->options.handShakeState == HANDSHAKE_DONE && |
12772 | | type != session_ticket && type != certificate_request && |
12773 | | type != certificate && type != key_update && type != finished) { |
12774 | | WOLFSSL_MSG("HandShake message after handshake complete"); |
12775 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12776 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12777 | | return OUT_OF_ORDER_E; |
12778 | | } |
12779 | | |
12780 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12781 | | ssl->options.serverState == NULL_STATE && |
12782 | | type != server_hello && type != hello_retry_request |
12783 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
12784 | | && (!ssl->options.dtls || type != hello_verify_request) |
12785 | | #endif /* defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) */ |
12786 | | ) { |
12787 | | WOLFSSL_MSG("First server message not server hello"); |
12788 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12789 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12790 | | return OUT_OF_ORDER_E; |
12791 | | } |
12792 | | |
12793 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
12794 | | ssl->options.clientState == NULL_STATE && type != client_hello) { |
12795 | | WOLFSSL_MSG("First client message not client hello"); |
12796 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12797 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12798 | | return OUT_OF_ORDER_E; |
12799 | | } |
12800 | | |
12801 | | /* above checks handshake state */ |
12802 | | switch (type) { |
12803 | | #ifndef NO_WOLFSSL_CLIENT |
12804 | | /* Messages only received by client. */ |
12805 | | case server_hello: |
12806 | | WOLFSSL_MSG("processing server hello"); |
12807 | | ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type); |
12808 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
12809 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
12810 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
12811 | | if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) || |
12812 | | IsAtLeastTLSv1_3(ssl->version)) { |
12813 | | ssl->options.cacheMessages = 0; |
12814 | | if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) { |
12815 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
12816 | | XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES); |
12817 | | ssl->hsHashes->messages = NULL; |
12818 | | } |
12819 | | } |
12820 | | #endif |
12821 | | break; |
12822 | | |
12823 | | case encrypted_extensions: |
12824 | | WOLFSSL_MSG("processing encrypted extensions"); |
12825 | | ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size); |
12826 | | break; |
12827 | | |
12828 | | #ifndef NO_CERTS |
12829 | | case certificate_request: |
12830 | | WOLFSSL_MSG("processing certificate request"); |
12831 | | ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size); |
12832 | | break; |
12833 | | #endif |
12834 | | |
12835 | | case session_ticket: |
12836 | | WOLFSSL_MSG("processing new session ticket"); |
12837 | | ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size); |
12838 | | break; |
12839 | | #endif /* !NO_WOLFSSL_CLIENT */ |
12840 | | |
12841 | | #ifndef NO_WOLFSSL_SERVER |
12842 | | /* Messages only received by server. */ |
12843 | | case client_hello: |
12844 | | WOLFSSL_MSG("processing client hello"); |
12845 | | #if defined(HAVE_ECH) |
12846 | | /* keep the start idx so we can restore it for the inner call */ |
12847 | | echInOutIdx = *inOutIdx; |
12848 | | #endif |
12849 | | ret = DoTls13ClientHello(ssl, input, inOutIdx, size); |
12850 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
12851 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
12852 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
12853 | | if ((ssl->options.resuming || !ssl->options.verifyPeer || |
12854 | | !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) |
12855 | | #ifdef WOLFSSL_DTLS13 |
12856 | | && (!ssl->options.dtls) |
12857 | | #endif |
12858 | | ) { |
12859 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
12860 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E) && |
12861 | | ret != WC_NO_ERR_TRACE(OCSP_WANT_READ)) |
12862 | | #endif |
12863 | | { |
12864 | | ssl->options.cacheMessages = 0; |
12865 | | if ((ssl->hsHashes != NULL) && |
12866 | | (ssl->hsHashes->messages != NULL)) { |
12867 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
12868 | | XFREE(ssl->hsHashes->messages, ssl->heap, |
12869 | | DYNAMIC_TYPE_HASHES); |
12870 | | ssl->hsHashes->messages = NULL; |
12871 | | } |
12872 | | } |
12873 | | } |
12874 | | #endif |
12875 | | #if defined(HAVE_ECH) |
12876 | | if (ret == 0) { |
12877 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
12878 | | |
12879 | | if (echX != NULL && |
12880 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
12881 | | /* reset the inOutIdx to the outer start */ |
12882 | | *inOutIdx = echInOutIdx; |
12883 | | /* call again with the inner hello */ |
12884 | | if (ret == 0) { |
12885 | | ret = DoTls13ClientHello(ssl, |
12886 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello, |
12887 | | &echInOutIdx, |
12888 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen); |
12889 | | } |
12890 | | /* if the inner ech parsed successfully we have successfully |
12891 | | * handled the hello and can skip the whole message */ |
12892 | | if (ret == 0) |
12893 | | *inOutIdx += size; |
12894 | | } |
12895 | | } |
12896 | | #endif /* HAVE_ECH */ |
12897 | | break; |
12898 | | |
12899 | | #ifdef WOLFSSL_EARLY_DATA |
12900 | | case end_of_early_data: |
12901 | | WOLFSSL_MSG("processing end of early data"); |
12902 | | ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size); |
12903 | | break; |
12904 | | #endif |
12905 | | #endif /* !NO_WOLFSSL_SERVER */ |
12906 | | |
12907 | | /* Messages received by both client and server. */ |
12908 | | #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \ |
12909 | | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
12910 | | case certificate: |
12911 | | WOLFSSL_MSG("processing certificate"); |
12912 | | ret = DoTls13Certificate(ssl, input, inOutIdx, size); |
12913 | | break; |
12914 | | #endif |
12915 | | |
12916 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
12917 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
12918 | | case certificate_verify: |
12919 | | WOLFSSL_MSG("processing certificate verify"); |
12920 | | ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size); |
12921 | | break; |
12922 | | #endif |
12923 | | case finished: |
12924 | | WOLFSSL_MSG("processing finished"); |
12925 | | ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF); |
12926 | | break; |
12927 | | |
12928 | | case key_update: |
12929 | | WOLFSSL_MSG("processing key update"); |
12930 | | ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size); |
12931 | | break; |
12932 | | |
12933 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
12934 | | !defined(NO_WOLFSSL_CLIENT) |
12935 | | case hello_verify_request: |
12936 | | WOLFSSL_MSG("processing hello verify request"); |
12937 | | ret = DoHelloVerifyRequest(ssl, input, inOutIdx, size); |
12938 | | break; |
12939 | | #endif |
12940 | | default: |
12941 | | WOLFSSL_MSG("Unknown handshake message type"); |
12942 | | ret = UNKNOWN_HANDSHAKE_TYPE; |
12943 | | break; |
12944 | | } |
12945 | | |
12946 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_ASYNC_IO) |
12947 | | /* if async, offset index so this msg will be processed again */ |
12948 | | /* NOTE: check this now before other calls can overwrite ret */ |
12949 | | if ((ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
12950 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) && *inOutIdx > 0) { |
12951 | | /* DTLS always stores a message in a buffer when async is enable, so we |
12952 | | * don't need to adjust for the extra bytes here (*inOutIdx is always |
12953 | | * == 0) */ |
12954 | | *inOutIdx -= HANDSHAKE_HEADER_SZ; |
12955 | | } |
12956 | | |
12957 | | /* make sure async error is cleared */ |
12958 | | if (ret == 0 && |
12959 | | (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E) || |
12960 | | ssl->error == WC_NO_ERR_TRACE(OCSP_WANT_READ))) { |
12961 | | ssl->error = 0; |
12962 | | } |
12963 | | #endif |
12964 | | if (ret == 0 && type != client_hello && type != session_ticket && |
12965 | | type != key_update) { |
12966 | | ret = HashInput(ssl, input + inIdx, (int)size); |
12967 | | } |
12968 | | |
12969 | | alertType = TranslateErrorToAlert(ret); |
12970 | | |
12971 | | if (alertType != invalid_alert) { |
12972 | | #ifdef WOLFSSL_DTLS13 |
12973 | | if (type == client_hello && ssl->options.dtls) |
12974 | | DtlsSetSeqNumForReply(ssl); |
12975 | | #endif |
12976 | | tmp = SendAlert(ssl, alert_fatal, alertType); |
12977 | | /* propagate socket error instead of tls error to be sure the error is |
12978 | | * not ignored by DTLS code */ |
12979 | | if (tmp == WC_NO_ERR_TRACE(SOCKET_ERROR_E)) |
12980 | | ret = SOCKET_ERROR_E; |
12981 | | } |
12982 | | |
12983 | | if (ret == 0 && ssl->options.tls1_3) { |
12984 | | /* Need to hash input message before deriving secrets. */ |
12985 | | #ifndef NO_WOLFSSL_CLIENT |
12986 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12987 | | if (type == server_hello) { |
12988 | | if ((ret = DeriveEarlySecret(ssl)) != 0) |
12989 | | return ret; |
12990 | | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
12991 | | return ret; |
12992 | | |
12993 | | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
12994 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
12995 | | return ret; |
12996 | | } |
12997 | | #ifdef WOLFSSL_EARLY_DATA |
12998 | | if (ssl->earlyData != no_early_data) { |
12999 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13000 | | return ret; |
13001 | | } |
13002 | | else |
13003 | | #endif |
13004 | | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
13005 | | return ret; |
13006 | | |
13007 | | #ifdef WOLFSSL_DTLS13 |
13008 | | if (ssl->options.dtls) { |
13009 | | w64wrapper epochHandshake; |
13010 | | epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
13011 | | ssl->dtls13Epoch = epochHandshake; |
13012 | | ssl->dtls13PeerEpoch = epochHandshake; |
13013 | | |
13014 | | ret = Dtls13SetEpochKeys( |
13015 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
13016 | | if (ret != 0) |
13017 | | return ret; |
13018 | | |
13019 | | } |
13020 | | #endif /* WOLFSSL_DTLS13 */ |
13021 | | } |
13022 | | |
13023 | | if (type == finished) { |
13024 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
13025 | | return ret; |
13026 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
13027 | | ForceZero(ssl->arrays->preMasterSecret, |
13028 | | ssl->arrays->preMasterSz); |
13029 | | #ifdef WOLFSSL_EARLY_DATA |
13030 | | #ifdef WOLFSSL_QUIC |
13031 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData != no_early_data) { |
13032 | | /* QUIC never sends/receives EndOfEarlyData, but having |
13033 | | * early data means the last encryption keys had not been |
13034 | | * set yet. */ |
13035 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13036 | | return ret; |
13037 | | } |
13038 | | #endif |
13039 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13040 | | ENCRYPT_AND_DECRYPT_SIDE, |
13041 | | ssl->earlyData == no_early_data)) != 0) { |
13042 | | return ret; |
13043 | | } |
13044 | | if (ssl->earlyData != no_early_data) { |
13045 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, |
13046 | | 1)) != 0) { |
13047 | | return ret; |
13048 | | } |
13049 | | } |
13050 | | #else |
13051 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13052 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
13053 | | return ret; |
13054 | | } |
13055 | | #endif |
13056 | | /* Setup keys for application data messages. */ |
13057 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13058 | | return ret; |
13059 | | } |
13060 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
13061 | | if (type == certificate_request && |
13062 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
13063 | | /* reset handshake states */ |
13064 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
13065 | | ssl->options.connectState = FIRST_REPLY_DONE; |
13066 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13067 | | ssl->options.processReply = 0; /* doProcessInit */ |
13068 | | |
13069 | | /* |
13070 | | DTLSv1.3 note: We can't reset serverState to |
13071 | | SERVER_FINISHED_COMPLETE with the goal that this connect |
13072 | | blocks until the cert/cert_verify/finished flight gets ACKed |
13073 | | by the server. The problem is that we will invoke |
13074 | | ProcessReplyEx() in that case, but we came here from |
13075 | | ProcessReplyEx() and it is not re-entrant safe (the input |
13076 | | buffer would still have the certificate_request message). */ |
13077 | | |
13078 | | if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) { |
13079 | | ret = ssl->error; |
13080 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
13081 | | ret = POST_HAND_AUTH_ERROR; |
13082 | | } |
13083 | | } |
13084 | | #endif |
13085 | | } |
13086 | | #endif /* NO_WOLFSSL_CLIENT */ |
13087 | | |
13088 | | #ifndef NO_WOLFSSL_SERVER |
13089 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13090 | | if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) { |
13091 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
13092 | | if (ret != 0) |
13093 | | return ret; |
13094 | | } |
13095 | | #endif |
13096 | | #endif /* NO_WOLFSSL_SERVER */ |
13097 | | } |
13098 | | |
13099 | | #ifdef WOLFSSL_DTLS13 |
13100 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) { |
13101 | | DtlsResetState(ssl); |
13102 | | if (DtlsIgnoreError(ret)) |
13103 | | ret = 0; |
13104 | | } |
13105 | | #endif |
13106 | | |
13107 | | WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret); |
13108 | | return ret; |
13109 | | } |
13110 | | |
13111 | | |
13112 | | /* Handle a handshake message that has been received. |
13113 | | * |
13114 | | * ssl The SSL/TLS object. |
13115 | | * input The message buffer. |
13116 | | * inOutIdx On entry, the index into the buffer of the current message. |
13117 | | * On exit, the index into the buffer of the next message. |
13118 | | * totalSz Length of remaining data in the message buffer. |
13119 | | * returns 0 on success and otherwise failure. |
13120 | | */ |
13121 | | int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
13122 | | word32 totalSz) |
13123 | 18.3k | { |
13124 | 18.3k | int ret = 0; |
13125 | 18.3k | word32 inputLength; |
13126 | 18.3k | byte type; |
13127 | 18.3k | word32 size = 0; |
13128 | | |
13129 | 18.3k | WOLFSSL_ENTER("DoTls13HandShakeMsg"); |
13130 | | |
13131 | 18.3k | if (ssl->arrays == NULL) { |
13132 | 0 | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13133 | 0 | totalSz) != 0) { |
13134 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13135 | 0 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13136 | 0 | return PARSE_ERROR; |
13137 | 0 | } |
13138 | | |
13139 | 0 | ret = EarlySanityCheckMsgReceived(ssl, type, size); |
13140 | 0 | if (ret != 0) { |
13141 | 0 | WOLFSSL_ERROR(ret); |
13142 | 0 | return ret; |
13143 | 0 | } |
13144 | | |
13145 | 0 | return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13146 | 0 | totalSz); |
13147 | 0 | } |
13148 | | |
13149 | 18.3k | inputLength = ssl->buffers.inputBuffer.length - *inOutIdx - ssl->keys.padSz; |
13150 | | |
13151 | | /* If there is a pending fragmented handshake message, |
13152 | | * pending message size will be non-zero. */ |
13153 | 18.3k | if (ssl->arrays->pendingMsgSz == 0) { |
13154 | | |
13155 | 11.7k | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13156 | 11.7k | totalSz) != 0) { |
13157 | 16 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13158 | 16 | return PARSE_ERROR; |
13159 | 16 | } |
13160 | | |
13161 | 11.7k | ret = EarlySanityCheckMsgReceived(ssl, type, |
13162 | 11.7k | min(inputLength - HANDSHAKE_HEADER_SZ, size)); |
13163 | 11.7k | if (ret != 0) { |
13164 | 115 | WOLFSSL_ERROR(ret); |
13165 | 115 | return ret; |
13166 | 115 | } |
13167 | | |
13168 | | /* Cap the maximum size of a handshake message to something reasonable. |
13169 | | * By default is the maximum size of a certificate message assuming |
13170 | | * nine 2048-bit RSA certificates in the chain. */ |
13171 | 11.6k | if (size > MAX_HANDSHAKE_SZ) { |
13172 | 62 | WOLFSSL_MSG("Handshake message too large"); |
13173 | 62 | WOLFSSL_ERROR_VERBOSE(HANDSHAKE_SIZE_ERROR); |
13174 | 62 | return HANDSHAKE_SIZE_ERROR; |
13175 | 62 | } |
13176 | | |
13177 | | /* size is the size of the certificate message payload */ |
13178 | 11.5k | if (inputLength - HANDSHAKE_HEADER_SZ < size) { |
13179 | 538 | ssl->arrays->pendingMsgType = type; |
13180 | 538 | ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ; |
13181 | 538 | ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ, |
13182 | 538 | ssl->heap, |
13183 | 538 | DYNAMIC_TYPE_ARRAYS); |
13184 | 538 | if (ssl->arrays->pendingMsg == NULL) |
13185 | 20 | return MEMORY_E; |
13186 | 518 | XMEMCPY(ssl->arrays->pendingMsg, |
13187 | 518 | input + *inOutIdx - HANDSHAKE_HEADER_SZ, |
13188 | 518 | inputLength); |
13189 | 518 | ssl->arrays->pendingMsgOffset = inputLength; |
13190 | 518 | *inOutIdx += inputLength + ssl->keys.padSz - HANDSHAKE_HEADER_SZ; |
13191 | 518 | return 0; |
13192 | 538 | } |
13193 | | |
13194 | 11.0k | ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13195 | 11.0k | totalSz); |
13196 | 11.0k | } |
13197 | 6.61k | else { |
13198 | 6.61k | if (inputLength + ssl->arrays->pendingMsgOffset > |
13199 | 6.61k | ssl->arrays->pendingMsgSz) { |
13200 | 50 | inputLength = ssl->arrays->pendingMsgSz - |
13201 | 50 | ssl->arrays->pendingMsgOffset; |
13202 | 50 | } |
13203 | | |
13204 | 6.61k | ret = EarlySanityCheckMsgReceived(ssl, ssl->arrays->pendingMsgType, |
13205 | 6.61k | inputLength); |
13206 | 6.61k | if (ret != 0) { |
13207 | 23 | WOLFSSL_ERROR(ret); |
13208 | 23 | return ret; |
13209 | 23 | } |
13210 | | |
13211 | 6.58k | XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset, |
13212 | 6.58k | input + *inOutIdx, inputLength); |
13213 | 6.58k | ssl->arrays->pendingMsgOffset += inputLength; |
13214 | 6.58k | *inOutIdx += inputLength + ssl->keys.padSz; |
13215 | | |
13216 | 6.58k | if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz) |
13217 | 63 | { |
13218 | 63 | word32 idx = 0; |
13219 | 63 | ret = DoTls13HandShakeMsgType(ssl, |
13220 | 63 | ssl->arrays->pendingMsg + HANDSHAKE_HEADER_SZ, |
13221 | 63 | &idx, ssl->arrays->pendingMsgType, |
13222 | 63 | ssl->arrays->pendingMsgSz - HANDSHAKE_HEADER_SZ, |
13223 | 63 | ssl->arrays->pendingMsgSz); |
13224 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13225 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
13226 | | /* setup to process fragment again */ |
13227 | | ssl->arrays->pendingMsgOffset -= inputLength; |
13228 | | *inOutIdx -= inputLength + ssl->keys.padSz; |
13229 | | } |
13230 | | else |
13231 | | #endif |
13232 | 63 | { |
13233 | 63 | XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS); |
13234 | 63 | ssl->arrays->pendingMsg = NULL; |
13235 | 63 | ssl->arrays->pendingMsgSz = 0; |
13236 | 63 | } |
13237 | 63 | } |
13238 | 6.58k | } |
13239 | | |
13240 | 17.6k | WOLFSSL_LEAVE("DoTls13HandShakeMsg", ret); |
13241 | 17.6k | return ret; |
13242 | 18.3k | } |
13243 | | |
13244 | | #ifndef NO_WOLFSSL_CLIENT |
13245 | | |
13246 | | /* The client connecting to the server. |
13247 | | * The protocol version is expecting to be TLS v1.3. |
13248 | | * If the server downgrades, and older versions of the protocol are compiled |
13249 | | * in, the client will fallback to wolfSSL_connect(). |
13250 | | * Please see note at top of README if you get an error from connect. |
13251 | | * |
13252 | | * ssl The SSL/TLS object. |
13253 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
13254 | | * unrecoverable error occurs and 0 otherwise. |
13255 | | * For more error information use wolfSSL_get_error(). |
13256 | | */ |
13257 | | int wolfSSL_connect_TLSv13(WOLFSSL* ssl) |
13258 | 0 | { |
13259 | 0 | int advanceState; |
13260 | 0 | int ret = 0; |
13261 | |
|
13262 | 0 | WOLFSSL_ENTER("wolfSSL_connect_TLSv13"); |
13263 | |
|
13264 | 0 | #ifdef HAVE_ERRNO_H |
13265 | 0 | errno = 0; |
13266 | 0 | #endif |
13267 | |
|
13268 | 0 | if (ssl == NULL) |
13269 | 0 | return BAD_FUNC_ARG; |
13270 | | |
13271 | 0 | if (ssl->options.side != WOLFSSL_CLIENT_END) { |
13272 | 0 | ssl->error = SIDE_ERROR; |
13273 | 0 | WOLFSSL_ERROR(ssl->error); |
13274 | 0 | return WOLFSSL_FATAL_ERROR; |
13275 | 0 | } |
13276 | | |
13277 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
13278 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
13279 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
13280 | 0 | return ret; |
13281 | 0 | } |
13282 | | |
13283 | | #ifdef WOLFSSL_DTLS |
13284 | | if (ssl->version.major == DTLS_MAJOR) { |
13285 | | ssl->options.dtls = 1; |
13286 | | ssl->options.dtlsStateful = 1; |
13287 | | } |
13288 | | #endif |
13289 | | |
13290 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
13291 | | if ((ssl->ConnectFilter != NULL) && |
13292 | | (ssl->options.connectState == CONNECT_BEGIN)) |
13293 | | { |
13294 | | wolfSSL_netfilter_decision_t res; |
13295 | | if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) == |
13296 | | WOLFSSL_SUCCESS) && |
13297 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
13298 | | ssl->error = SOCKET_FILTERED_E; |
13299 | | WOLFSSL_ERROR(ssl->error); |
13300 | | return WOLFSSL_FATAL_ERROR; |
13301 | | } |
13302 | | } |
13303 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
13304 | | |
13305 | | /* fragOffset is non-zero when sending fragments. On the last |
13306 | | * fragment, fragOffset is zero again, and the state can be |
13307 | | * advanced. Also, only advance from states in which we send data */ |
13308 | 0 | advanceState = (ssl->options.connectState == CONNECT_BEGIN || |
13309 | 0 | ssl->options.connectState == HELLO_AGAIN || |
13310 | 0 | (ssl->options.connectState >= FIRST_REPLY_DONE && |
13311 | 0 | ssl->options.connectState <= FIRST_REPLY_FOURTH)); |
13312 | |
|
13313 | | #ifdef WOLFSSL_DTLS13 |
13314 | | if (ssl->options.dtls) |
13315 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
13316 | | && !ssl->dtls13SendingAckOrRtx; |
13317 | | #endif /* WOLFSSL_DTLS13 */ |
13318 | |
|
13319 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
13320 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13321 | | /* do not send buffered or advance state if last error was an |
13322 | | async pending operation */ |
13323 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
13324 | | #endif |
13325 | 0 | ) { |
13326 | 0 | if ((ret = SendBuffered(ssl)) == 0) { |
13327 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
13328 | 0 | if (advanceState) { |
13329 | | #ifdef WOLFSSL_DTLS13 |
13330 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) && |
13331 | | ssl->options.connectState == FIRST_REPLY_FOURTH) { |
13332 | | /* WAIT_FINISHED_ACK is a state added afterwards, but it |
13333 | | can't follow FIRST_REPLY_FOURTH in the enum order. Indeed |
13334 | | the value of the enum ConnectState is stored in |
13335 | | serialized session. This would make importing serialized |
13336 | | session from other wolfSSL version incompatible */ |
13337 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13338 | | } |
13339 | | else |
13340 | | #endif /* WOLFSSL_DTLS13 */ |
13341 | 0 | { |
13342 | 0 | ssl->options.connectState++; |
13343 | 0 | } |
13344 | 0 | WOLFSSL_MSG("connect state: " |
13345 | 0 | "Advanced from last buffered fragment send"); |
13346 | 0 | #ifdef WOLFSSL_ASYNC_IO |
13347 | 0 | FreeAsyncCtx(ssl, 0); |
13348 | 0 | #endif |
13349 | |
|
13350 | 0 | } |
13351 | 0 | } |
13352 | 0 | else { |
13353 | 0 | WOLFSSL_MSG("connect state: " |
13354 | 0 | "Not advanced, more fragments to send"); |
13355 | 0 | } |
13356 | | #ifdef WOLFSSL_DTLS13 |
13357 | | if (ssl->options.dtls) |
13358 | | ssl->dtls13SendingAckOrRtx = 0; |
13359 | | #endif /* WOLFSSL_DTLS13 */ |
13360 | |
|
13361 | 0 | } |
13362 | 0 | else { |
13363 | 0 | ssl->error = ret; |
13364 | 0 | WOLFSSL_ERROR(ssl->error); |
13365 | 0 | return WOLFSSL_FATAL_ERROR; |
13366 | 0 | } |
13367 | 0 | } |
13368 | | |
13369 | 0 | ret = RetrySendAlert(ssl); |
13370 | 0 | if (ret != 0) { |
13371 | 0 | ssl->error = ret; |
13372 | 0 | WOLFSSL_ERROR(ssl->error); |
13373 | 0 | return WOLFSSL_FATAL_ERROR; |
13374 | 0 | } |
13375 | | |
13376 | | #ifdef WOLFSSL_DTLS13 |
13377 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
13378 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
13379 | | WOLFSSL_ERROR(ssl->error); |
13380 | | return WOLFSSL_FATAL_ERROR; |
13381 | | } |
13382 | | |
13383 | | /* we sent all the fragments. Advance state. */ |
13384 | | ssl->options.connectState++; |
13385 | | } |
13386 | | #endif /* WOLFSSL_DTLS13 */ |
13387 | | |
13388 | 0 | switch (ssl->options.connectState) { |
13389 | | |
13390 | 0 | case CONNECT_BEGIN: |
13391 | | /* Always send client hello first. */ |
13392 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13393 | 0 | WOLFSSL_ERROR(ssl->error); |
13394 | 0 | return WOLFSSL_FATAL_ERROR; |
13395 | 0 | } |
13396 | | |
13397 | 0 | ssl->options.connectState = CLIENT_HELLO_SENT; |
13398 | 0 | WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); |
13399 | 0 | FALL_THROUGH; |
13400 | |
|
13401 | 0 | case CLIENT_HELLO_SENT: |
13402 | | #ifdef WOLFSSL_EARLY_DATA |
13403 | | if (ssl->earlyData != no_early_data && |
13404 | | ssl->options.handShakeState != CLIENT_HELLO_COMPLETE) { |
13405 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13406 | | if (!ssl->options.dtls && |
13407 | | ssl->options.tls13MiddleBoxCompat) { |
13408 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13409 | | WOLFSSL_ERROR(ssl->error); |
13410 | | return WOLFSSL_FATAL_ERROR; |
13411 | | } |
13412 | | ssl->options.sentChangeCipher = 1; |
13413 | | } |
13414 | | #endif |
13415 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13416 | | return WOLFSSL_SUCCESS; |
13417 | | } |
13418 | | #endif |
13419 | | /* Get the response/s from the server. */ |
13420 | 0 | while (ssl->options.serverState < |
13421 | 0 | SERVER_HELLOVERIFYREQUEST_COMPLETE) { |
13422 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13423 | 0 | WOLFSSL_ERROR(ssl->error); |
13424 | 0 | return WOLFSSL_FATAL_ERROR; |
13425 | 0 | } |
13426 | |
|
13427 | | #ifdef WOLFSSL_DTLS13 |
13428 | | if (ssl->options.dtls) { |
13429 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13430 | | WOLFSSL_ERROR(ssl->error); |
13431 | | return WOLFSSL_FATAL_ERROR; |
13432 | | } |
13433 | | } |
13434 | | #endif /* WOLFSSL_DTLS13 */ |
13435 | 0 | } |
13436 | | |
13437 | 0 | if (!ssl->options.tls1_3) { |
13438 | 0 | #ifndef WOLFSSL_NO_TLS12 |
13439 | 0 | if (ssl->options.downgrade) |
13440 | 0 | return wolfSSL_connect(ssl); |
13441 | 0 | #endif |
13442 | 0 | WOLFSSL_MSG("Client using higher version, fatal error"); |
13443 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
13444 | 0 | return VERSION_ERROR; |
13445 | 0 | } |
13446 | | |
13447 | 0 | ssl->options.connectState = HELLO_AGAIN; |
13448 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN"); |
13449 | 0 | FALL_THROUGH; |
13450 | |
|
13451 | 0 | case HELLO_AGAIN: |
13452 | |
|
13453 | 0 | if (ssl->options.serverState == |
13454 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
13455 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13456 | | if (!ssl->options.dtls && !ssl->options.sentChangeCipher |
13457 | | && ssl->options.tls13MiddleBoxCompat) { |
13458 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13459 | | WOLFSSL_ERROR(ssl->error); |
13460 | | return WOLFSSL_FATAL_ERROR; |
13461 | | } |
13462 | | ssl->options.sentChangeCipher = 1; |
13463 | | } |
13464 | | #endif |
13465 | | /* Try again with different security parameters. */ |
13466 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13467 | 0 | WOLFSSL_ERROR(ssl->error); |
13468 | 0 | return WOLFSSL_FATAL_ERROR; |
13469 | 0 | } |
13470 | 0 | } |
13471 | | |
13472 | 0 | ssl->options.connectState = HELLO_AGAIN_REPLY; |
13473 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY"); |
13474 | 0 | FALL_THROUGH; |
13475 | |
|
13476 | 0 | case HELLO_AGAIN_REPLY: |
13477 | | /* Get the response/s from the server. */ |
13478 | 0 | while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13479 | | #ifdef WOLFSSL_DTLS13 |
13480 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
13481 | | #ifndef WOLFSSL_NO_TLS12 |
13482 | | if (ssl->options.downgrade) |
13483 | | return wolfSSL_connect(ssl); |
13484 | | #endif |
13485 | | } |
13486 | | #endif /* WOLFSSL_DTLS13 */ |
13487 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13488 | 0 | WOLFSSL_ERROR(ssl->error); |
13489 | 0 | return WOLFSSL_FATAL_ERROR; |
13490 | 0 | } |
13491 | |
|
13492 | | #ifdef WOLFSSL_DTLS13 |
13493 | | if (ssl->options.dtls) { |
13494 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13495 | | WOLFSSL_ERROR(ssl->error); |
13496 | | return WOLFSSL_FATAL_ERROR; |
13497 | | } |
13498 | | } |
13499 | | #endif /* WOLFSSL_DTLS13 */ |
13500 | 0 | } |
13501 | | |
13502 | 0 | ssl->options.connectState = FIRST_REPLY_DONE; |
13503 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_DONE"); |
13504 | 0 | FALL_THROUGH; |
13505 | |
|
13506 | 0 | case FIRST_REPLY_DONE: |
13507 | 0 | if (ssl->options.certOnly) |
13508 | 0 | return WOLFSSL_SUCCESS; |
13509 | | #ifdef WOLFSSL_EARLY_DATA |
13510 | | if (!ssl->options.dtls && ssl->earlyData != no_early_data |
13511 | | && !WOLFSSL_IS_QUIC(ssl)) { |
13512 | | if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) { |
13513 | | WOLFSSL_ERROR(ssl->error); |
13514 | | return WOLFSSL_FATAL_ERROR; |
13515 | | } |
13516 | | WOLFSSL_MSG("sent: end_of_early_data"); |
13517 | | } |
13518 | | #endif |
13519 | | |
13520 | 0 | ssl->options.connectState = FIRST_REPLY_FIRST; |
13521 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST"); |
13522 | 0 | FALL_THROUGH; |
13523 | |
|
13524 | 0 | case FIRST_REPLY_FIRST: |
13525 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13526 | | if (!ssl->options.sentChangeCipher && !ssl->options.dtls |
13527 | | && ssl->options.tls13MiddleBoxCompat) { |
13528 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13529 | | WOLFSSL_ERROR(ssl->error); |
13530 | | return WOLFSSL_FATAL_ERROR; |
13531 | | } |
13532 | | ssl->options.sentChangeCipher = 1; |
13533 | | } |
13534 | | #endif |
13535 | |
|
13536 | 0 | ssl->options.connectState = FIRST_REPLY_SECOND; |
13537 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND"); |
13538 | 0 | FALL_THROUGH; |
13539 | |
|
13540 | 0 | case FIRST_REPLY_SECOND: |
13541 | | /* CLIENT: check peer authentication. */ |
13542 | 0 | if (!ssl->options.peerAuthGood) { |
13543 | 0 | WOLFSSL_MSG("Server authentication did not happen"); |
13544 | 0 | WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR); |
13545 | 0 | return WOLFSSL_FATAL_ERROR; |
13546 | 0 | } |
13547 | 0 | #ifndef NO_CERTS |
13548 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13549 | 0 | ssl->error = SendTls13Certificate(ssl); |
13550 | 0 | if (ssl->error != 0) { |
13551 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13552 | 0 | WOLFSSL_ERROR(ssl->error); |
13553 | 0 | return WOLFSSL_FATAL_ERROR; |
13554 | 0 | } |
13555 | 0 | WOLFSSL_MSG("sent: certificate"); |
13556 | 0 | } |
13557 | 0 | #endif |
13558 | | |
13559 | 0 | ssl->options.connectState = FIRST_REPLY_THIRD; |
13560 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD"); |
13561 | 0 | FALL_THROUGH; |
13562 | |
|
13563 | 0 | case FIRST_REPLY_THIRD: |
13564 | 0 | #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
13565 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
13566 | 0 | defined(HAVE_FALCON) || defined(HAVE_DILITHIUM))) && \ |
13567 | 0 | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
13568 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13569 | 0 | ssl->error = SendTls13CertificateVerify(ssl); |
13570 | 0 | if (ssl->error != 0) { |
13571 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13572 | 0 | WOLFSSL_ERROR(ssl->error); |
13573 | 0 | return WOLFSSL_FATAL_ERROR; |
13574 | 0 | } |
13575 | 0 | WOLFSSL_MSG("sent: certificate verify"); |
13576 | 0 | } |
13577 | 0 | #endif |
13578 | | |
13579 | 0 | ssl->options.connectState = FIRST_REPLY_FOURTH; |
13580 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH"); |
13581 | 0 | FALL_THROUGH; |
13582 | |
|
13583 | 0 | case FIRST_REPLY_FOURTH: |
13584 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
13585 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13586 | 0 | WOLFSSL_ERROR(ssl->error); |
13587 | 0 | return WOLFSSL_FATAL_ERROR; |
13588 | 0 | } |
13589 | 0 | WOLFSSL_MSG("sent: finished"); |
13590 | |
|
13591 | | #ifdef WOLFSSL_DTLS13 |
13592 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13593 | | WOLFSSL_MSG("connect state: WAIT_FINISHED_ACK"); |
13594 | | FALL_THROUGH; |
13595 | | |
13596 | | case WAIT_FINISHED_ACK: |
13597 | | if (ssl->options.dtls) { |
13598 | | while (ssl->options.serverState != SERVER_FINISHED_ACKED) { |
13599 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13600 | | WOLFSSL_ERROR(ssl->error); |
13601 | | return WOLFSSL_FATAL_ERROR; |
13602 | | } |
13603 | | |
13604 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13605 | | WOLFSSL_ERROR(ssl->error); |
13606 | | return WOLFSSL_FATAL_ERROR; |
13607 | | } |
13608 | | } |
13609 | | } |
13610 | | #endif /* WOLFSSL_DTLS13 */ |
13611 | 0 | ssl->options.connectState = FINISHED_DONE; |
13612 | 0 | WOLFSSL_MSG("connect state: FINISHED_DONE"); |
13613 | 0 | FALL_THROUGH; |
13614 | |
|
13615 | 0 | case FINISHED_DONE: |
13616 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
13617 | 0 | if (ssl->hsDoneCb != NULL) { |
13618 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
13619 | 0 | if (cbret < 0) { |
13620 | 0 | ssl->error = cbret; |
13621 | 0 | WOLFSSL_ERROR_VERBOSE(ssl->error); |
13622 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
13623 | 0 | return WOLFSSL_FATAL_ERROR; |
13624 | 0 | } |
13625 | 0 | } |
13626 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
13627 | | |
13628 | 0 | if (!ssl->options.keepResources) { |
13629 | 0 | FreeHandshakeResources(ssl); |
13630 | 0 | } |
13631 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
13632 | | /* Free the remaining async context if not using it for crypto */ |
13633 | 0 | FreeAsyncCtx(ssl, 1); |
13634 | 0 | #endif |
13635 | |
|
13636 | 0 | ssl->error = 0; /* clear the error */ |
13637 | |
|
13638 | 0 | WOLFSSL_LEAVE("wolfSSL_connect_TLSv13", WOLFSSL_SUCCESS); |
13639 | 0 | return WOLFSSL_SUCCESS; |
13640 | | |
13641 | 0 | default: |
13642 | 0 | WOLFSSL_MSG("Unknown connect state ERROR"); |
13643 | 0 | return WOLFSSL_FATAL_ERROR; /* unknown connect state */ |
13644 | 0 | } |
13645 | 0 | } |
13646 | | #endif |
13647 | | |
13648 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
13649 | | /* Send a cookie with the HelloRetryRequest to avoid storing state. |
13650 | | * |
13651 | | * ssl SSL/TLS object. |
13652 | | * secret Secret to use when generating integrity check for cookie. |
13653 | | * A value of NULL indicates to generate a new random secret. |
13654 | | * secretSz Size of secret data in bytes. |
13655 | | * Use a value of 0 to indicate use of default size. |
13656 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when |
13657 | | * called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
13658 | | */ |
13659 | | int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret, |
13660 | | unsigned int secretSz) |
13661 | | { |
13662 | | int ret; |
13663 | | |
13664 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13665 | | return BAD_FUNC_ARG; |
13666 | | #ifndef NO_WOLFSSL_SERVER |
13667 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13668 | | return SIDE_ERROR; |
13669 | | |
13670 | | if (secretSz == 0) { |
13671 | | #ifndef NO_SHA256 |
13672 | | secretSz = WC_SHA256_DIGEST_SIZE; |
13673 | | #elif defined(WOLFSSL_SHA384) |
13674 | | secretSz = WC_SHA384_DIGEST_SIZE; |
13675 | | #elif defined(WOLFSSL_TLS13_SHA512) |
13676 | | secretSz = WC_SHA512_DIGEST_SIZE; |
13677 | | #elif defined(WOLFSSL_SM3) |
13678 | | secretSz = WC_SM3_DIGEST_SIZE; |
13679 | | #else |
13680 | | #error "No digest to available to use with HMAC for cookies." |
13681 | | #endif /* NO_SHA */ |
13682 | | } |
13683 | | |
13684 | | if (secretSz != ssl->buffers.tls13CookieSecret.length) { |
13685 | | byte* newSecret; |
13686 | | |
13687 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
13688 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
13689 | | ssl->buffers.tls13CookieSecret.length); |
13690 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, |
13691 | | ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
13692 | | } |
13693 | | |
13694 | | newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
13695 | | DYNAMIC_TYPE_COOKIE_PWD); |
13696 | | if (newSecret == NULL) { |
13697 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
13698 | | ssl->buffers.tls13CookieSecret.length = 0; |
13699 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
13700 | | return MEMORY_ERROR; |
13701 | | } |
13702 | | ssl->buffers.tls13CookieSecret.buffer = newSecret; |
13703 | | ssl->buffers.tls13CookieSecret.length = secretSz; |
13704 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
13705 | | wc_MemZero_Add("wolfSSL_send_hrr_cookie secret", |
13706 | | ssl->buffers.tls13CookieSecret.buffer, |
13707 | | ssl->buffers.tls13CookieSecret.length); |
13708 | | #endif |
13709 | | } |
13710 | | |
13711 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
13712 | | if (secret == NULL) { |
13713 | | ret = wc_RNG_GenerateBlock(ssl->rng, |
13714 | | ssl->buffers.tls13CookieSecret.buffer, secretSz); |
13715 | | if (ret < 0) |
13716 | | return ret; |
13717 | | } |
13718 | | else |
13719 | | XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz); |
13720 | | |
13721 | | ssl->options.sendCookie = 1; |
13722 | | |
13723 | | ret = WOLFSSL_SUCCESS; |
13724 | | #else |
13725 | | (void)secret; |
13726 | | (void)secretSz; |
13727 | | |
13728 | | ret = SIDE_ERROR; |
13729 | | #endif |
13730 | | |
13731 | | return ret; |
13732 | | } |
13733 | | |
13734 | | int wolfSSL_disable_hrr_cookie(WOLFSSL* ssl) |
13735 | | { |
13736 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13737 | | return BAD_FUNC_ARG; |
13738 | | |
13739 | | #ifdef NO_WOLFSSL_SERVER |
13740 | | return SIDE_ERROR; |
13741 | | #else |
13742 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13743 | | return SIDE_ERROR; |
13744 | | |
13745 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
13746 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
13747 | | ssl->buffers.tls13CookieSecret.length); |
13748 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap, |
13749 | | DYNAMIC_TYPE_COOKIE_PWD); |
13750 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
13751 | | ssl->buffers.tls13CookieSecret.length = 0; |
13752 | | } |
13753 | | |
13754 | | ssl->options.sendCookie = 0; |
13755 | | return WOLFSSL_SUCCESS; |
13756 | | #endif /* NO_WOLFSSL_SERVER */ |
13757 | | } |
13758 | | |
13759 | | #endif /* defined(WOLFSSL_SEND_HRR_COOKIE) */ |
13760 | | |
13761 | | #ifdef HAVE_SUPPORTED_CURVES |
13762 | | /* Create a key share entry from group. |
13763 | | * Generates a key pair. |
13764 | | * |
13765 | | * ssl The SSL/TLS object. |
13766 | | * group The named group. |
13767 | | * returns 0 on success, otherwise failure. |
13768 | | * for async can return WC_PENDING_E and should be called again |
13769 | | */ |
13770 | | int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group) |
13771 | 0 | { |
13772 | 0 | int ret; |
13773 | |
|
13774 | 0 | if (ssl == NULL) |
13775 | 0 | return BAD_FUNC_ARG; |
13776 | | |
13777 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13778 | | ret = wolfSSL_AsyncPop(ssl, NULL); |
13779 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
13780 | | /* Check for error */ |
13781 | | if (ret < 0) |
13782 | | return ret; |
13783 | | } |
13784 | | #endif |
13785 | | |
13786 | | #if defined(WOLFSSL_HAVE_MLKEM) |
13787 | | if (WOLFSSL_NAMED_GROUP_IS_PQC(group) || |
13788 | | WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group)) { |
13789 | | |
13790 | | if (ssl->ctx != NULL && ssl->ctx->method != NULL && |
13791 | | !IsAtLeastTLSv1_3(ssl->version)) { |
13792 | | return BAD_FUNC_ARG; |
13793 | | } |
13794 | | |
13795 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13796 | | /* If I am the server of a KEM connection, do not do keygen because |
13797 | | * I'm going to encapsulate with the client's public key. Note that |
13798 | | * I might be the client and ssl->option.side has not been properly |
13799 | | * set yet. In that case the KeyGen operation will be deferred to |
13800 | | * connection time. */ |
13801 | | return WOLFSSL_SUCCESS; |
13802 | | } |
13803 | | } |
13804 | | #endif |
13805 | | #if defined(NO_TLS) |
13806 | | (void)ret; |
13807 | | (void)group; |
13808 | | #else |
13809 | 0 | ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions); |
13810 | 0 | if (ret != 0) |
13811 | 0 | return ret; |
13812 | 0 | #endif /* NO_TLS */ |
13813 | 0 | return WOLFSSL_SUCCESS; |
13814 | 0 | } |
13815 | | |
13816 | | /* Send no key share entries - use HelloRetryRequest to negotiate shared group. |
13817 | | * |
13818 | | * ssl The SSL/TLS object. |
13819 | | * returns 0 on success, otherwise failure. |
13820 | | */ |
13821 | | int wolfSSL_NoKeyShares(WOLFSSL* ssl) |
13822 | 0 | { |
13823 | 0 | int ret; |
13824 | |
|
13825 | 0 | if (ssl == NULL) |
13826 | 0 | return BAD_FUNC_ARG; |
13827 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
13828 | 0 | return SIDE_ERROR; |
13829 | | #if defined(NO_TLS) |
13830 | | (void)ret; |
13831 | | #else |
13832 | 0 | ret = TLSX_KeyShare_Empty(ssl); |
13833 | 0 | if (ret != 0) |
13834 | 0 | return ret; |
13835 | 0 | #endif /* NO_TLS */ |
13836 | 0 | return WOLFSSL_SUCCESS; |
13837 | 0 | } |
13838 | | #endif |
13839 | | |
13840 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
13841 | | int wolfSSL_UseCKS(WOLFSSL* ssl, byte *sigSpec, word16 sigSpecSz) |
13842 | | { |
13843 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->ctx->method->version) || |
13844 | | sigSpec == NULL || sigSpecSz == 0) |
13845 | | return BAD_FUNC_ARG; |
13846 | | |
13847 | | ssl->sigSpec = sigSpec; |
13848 | | ssl->sigSpecSz = sigSpecSz; |
13849 | | return WOLFSSL_SUCCESS; |
13850 | | } |
13851 | | |
13852 | | int wolfSSL_CTX_UseCKS(WOLFSSL_CTX* ctx, byte *sigSpec, word16 sigSpecSz) |
13853 | | { |
13854 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version) || |
13855 | | sigSpec == NULL || sigSpecSz == 0) |
13856 | | return BAD_FUNC_ARG; |
13857 | | |
13858 | | ctx->sigSpec = sigSpec; |
13859 | | ctx->sigSpecSz = sigSpecSz; |
13860 | | return WOLFSSL_SUCCESS; |
13861 | | } |
13862 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
13863 | | |
13864 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
13865 | | * |
13866 | | * ctx The SSL/TLS CTX object. |
13867 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13868 | | */ |
13869 | | int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx) |
13870 | 0 | { |
13871 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13872 | 0 | return BAD_FUNC_ARG; |
13873 | 0 | if (ctx->method->side == WOLFSSL_CLIENT_END) |
13874 | 0 | return SIDE_ERROR; |
13875 | | |
13876 | | #ifdef HAVE_SESSION_TICKET |
13877 | | ctx->noTicketTls13 = 1; |
13878 | | #endif |
13879 | | |
13880 | 0 | return 0; |
13881 | 0 | } |
13882 | | |
13883 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
13884 | | * |
13885 | | * ssl The SSL/TLS object. |
13886 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on |
13887 | | * a client and 0 on success. |
13888 | | */ |
13889 | | int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl) |
13890 | 0 | { |
13891 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13892 | 0 | return BAD_FUNC_ARG; |
13893 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13894 | 0 | return SIDE_ERROR; |
13895 | | |
13896 | | #ifdef HAVE_SESSION_TICKET |
13897 | | ssl->options.noTicketTls13 = 1; |
13898 | | #endif |
13899 | | |
13900 | 0 | return 0; |
13901 | 0 | } |
13902 | | |
13903 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
13904 | | * |
13905 | | * ctx The SSL/TLS CTX object. |
13906 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13907 | | */ |
13908 | | int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx) |
13909 | 0 | { |
13910 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13911 | 0 | return BAD_FUNC_ARG; |
13912 | | |
13913 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13914 | | ctx->noPskDheKe = 1; |
13915 | | #endif |
13916 | | |
13917 | 0 | return 0; |
13918 | 0 | } |
13919 | | |
13920 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
13921 | | * |
13922 | | * ssl The SSL/TLS object. |
13923 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
13924 | | * success. |
13925 | | */ |
13926 | | int wolfSSL_no_dhe_psk(WOLFSSL* ssl) |
13927 | 0 | { |
13928 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13929 | 0 | return BAD_FUNC_ARG; |
13930 | | |
13931 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13932 | | ssl->options.noPskDheKe = 1; |
13933 | | #endif |
13934 | | |
13935 | 0 | return 0; |
13936 | 0 | } |
13937 | | |
13938 | | #ifdef HAVE_SUPPORTED_CURVES |
13939 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
13940 | | * |
13941 | | * ctx The SSL/TLS CTX object. |
13942 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13943 | | */ |
13944 | | int wolfSSL_CTX_only_dhe_psk(WOLFSSL_CTX* ctx) |
13945 | 0 | { |
13946 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13947 | 0 | return BAD_FUNC_ARG; |
13948 | | |
13949 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13950 | | ctx->onlyPskDheKe = 1; |
13951 | | #endif |
13952 | | |
13953 | 0 | return 0; |
13954 | 0 | } |
13955 | | |
13956 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
13957 | | * |
13958 | | * ssl The SSL/TLS object. |
13959 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
13960 | | * success. |
13961 | | */ |
13962 | | int wolfSSL_only_dhe_psk(WOLFSSL* ssl) |
13963 | 0 | { |
13964 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13965 | 0 | return BAD_FUNC_ARG; |
13966 | | |
13967 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13968 | | ssl->options.onlyPskDheKe = 1; |
13969 | | #endif |
13970 | | |
13971 | 0 | return 0; |
13972 | 0 | } |
13973 | | #endif /* HAVE_SUPPORTED_CURVES */ |
13974 | | |
13975 | | int Tls13UpdateKeys(WOLFSSL* ssl) |
13976 | 0 | { |
13977 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13978 | 0 | return BAD_FUNC_ARG; |
13979 | | |
13980 | | #ifdef WOLFSSL_DTLS13 |
13981 | | /* we are already waiting for the ack of a sent key update message. We can't |
13982 | | send another one before receiving its ack. Either wolfSSL_update_keys() |
13983 | | was invoked multiple times over a short period of time or we replied to a |
13984 | | KeyUpdate with update request. We'll just ignore sending this |
13985 | | KeyUpdate. */ |
13986 | | /* TODO: add WOLFSSL_ERROR_ALREADY_IN_PROGRESS type of error here */ |
13987 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) |
13988 | | return 0; |
13989 | | #endif /* WOLFSSL_DTLS13 */ |
13990 | | |
13991 | 0 | return SendTls13KeyUpdate(ssl); |
13992 | 0 | } |
13993 | | |
13994 | | /* Update the keys for encryption and decryption. |
13995 | | * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then |
13996 | | * calling wolfSSL_write() will have the message sent when ready. |
13997 | | * |
13998 | | * ssl The SSL/TLS object. |
13999 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14000 | | * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write, |
14001 | | * WOLFSSL_SUCCESS on success and otherwise failure. |
14002 | | */ |
14003 | | int wolfSSL_update_keys(WOLFSSL* ssl) |
14004 | 0 | { |
14005 | 0 | int ret; |
14006 | 0 | ret = Tls13UpdateKeys(ssl); |
14007 | 0 | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14008 | 0 | ret = WOLFSSL_ERROR_WANT_WRITE; |
14009 | 0 | else if (ret == 0) |
14010 | 0 | ret = WOLFSSL_SUCCESS; |
14011 | 0 | return ret; |
14012 | 0 | } |
14013 | | |
14014 | | /* Whether a response is waiting for key update request. |
14015 | | * |
14016 | | * ssl The SSL/TLS object. |
14017 | | * required 0 when no key update response required. |
14018 | | * 1 when no key update response required. |
14019 | | * return 0 on success. |
14020 | | * return BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3 |
14021 | | */ |
14022 | | int wolfSSL_key_update_response(WOLFSSL* ssl, int* required) |
14023 | 0 | { |
14024 | 0 | if (required == NULL || ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14025 | 0 | return BAD_FUNC_ARG; |
14026 | | |
14027 | 0 | *required = ssl->keys.updateResponseReq; |
14028 | |
|
14029 | 0 | return 0; |
14030 | 0 | } |
14031 | | |
14032 | | #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
14033 | | /* Allow post-handshake authentication in TLS v1.3 connections. |
14034 | | * |
14035 | | * ctx The SSL/TLS CTX object. |
14036 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and |
14037 | | * 0 on success. |
14038 | | */ |
14039 | | int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx) |
14040 | | { |
14041 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14042 | | return BAD_FUNC_ARG; |
14043 | | if (ctx->method->side == WOLFSSL_SERVER_END) |
14044 | | return SIDE_ERROR; |
14045 | | |
14046 | | ctx->postHandshakeAuth = 1; |
14047 | | |
14048 | | return 0; |
14049 | | } |
14050 | | |
14051 | | /* Allow post-handshake authentication in TLS v1.3 connection. |
14052 | | * |
14053 | | * ssl The SSL/TLS object. |
14054 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14055 | | * SIDE_ERROR when not a client and 0 on success. |
14056 | | */ |
14057 | | int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl) |
14058 | | { |
14059 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14060 | | return BAD_FUNC_ARG; |
14061 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
14062 | | return SIDE_ERROR; |
14063 | | |
14064 | | ssl->options.postHandshakeAuth = 1; |
14065 | | |
14066 | | return 0; |
14067 | | } |
14068 | | |
14069 | | /* Request a certificate of the client. |
14070 | | * Can be called any time after handshake completion. |
14071 | | * A maximum of 256 requests can be sent on a connection. |
14072 | | * |
14073 | | * ssl SSL/TLS object. |
14074 | | */ |
14075 | | int wolfSSL_request_certificate(WOLFSSL* ssl) |
14076 | | { |
14077 | | int ret; |
14078 | | #ifndef NO_WOLFSSL_SERVER |
14079 | | CertReqCtx* certReqCtx; |
14080 | | #endif |
14081 | | |
14082 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14083 | | return BAD_FUNC_ARG; |
14084 | | #ifndef NO_WOLFSSL_SERVER |
14085 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14086 | | return SIDE_ERROR; |
14087 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14088 | | return NOT_READY_ERROR; |
14089 | | if (!ssl->options.postHandshakeAuth) |
14090 | | return POST_HAND_AUTH_ERROR; |
14091 | | |
14092 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap, |
14093 | | DYNAMIC_TYPE_TMP_BUFFER); |
14094 | | if (certReqCtx == NULL) |
14095 | | return MEMORY_E; |
14096 | | XMEMSET(certReqCtx, 0, sizeof(CertReqCtx)); |
14097 | | certReqCtx->next = ssl->certReqCtx; |
14098 | | certReqCtx->len = 1; |
14099 | | if (certReqCtx->next != NULL) |
14100 | | certReqCtx->ctx = certReqCtx->next->ctx + 1; |
14101 | | ssl->certReqCtx = certReqCtx; |
14102 | | |
14103 | | ssl->msgsReceived.got_certificate = 0; |
14104 | | ssl->msgsReceived.got_certificate_verify = 0; |
14105 | | ssl->msgsReceived.got_finished = 0; |
14106 | | |
14107 | | ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len); |
14108 | | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14109 | | ret = WOLFSSL_ERROR_WANT_WRITE; |
14110 | | else if (ret == 0) |
14111 | | ret = WOLFSSL_SUCCESS; |
14112 | | #else |
14113 | | ret = SIDE_ERROR; |
14114 | | #endif |
14115 | | |
14116 | | return ret; |
14117 | | } |
14118 | | #endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */ |
14119 | | |
14120 | | #if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
14121 | | /* Get the preferred key exchange group. |
14122 | | * |
14123 | | * ssl The SSL/TLS object. |
14124 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, |
14125 | | * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete |
14126 | | * and group number on success. |
14127 | | */ |
14128 | | int wolfSSL_preferred_group(WOLFSSL* ssl) |
14129 | 0 | { |
14130 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14131 | 0 | return BAD_FUNC_ARG; |
14132 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14133 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
14134 | 0 | return SIDE_ERROR; |
14135 | 0 | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14136 | 0 | return NOT_READY_ERROR; |
14137 | | |
14138 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
14139 | | /* Return supported groups only. */ |
14140 | 0 | return TLSX_SupportedCurve_Preferred(ssl, 1); |
14141 | | #else |
14142 | | return 0; |
14143 | | #endif |
14144 | | #else |
14145 | | return SIDE_ERROR; |
14146 | | #endif |
14147 | 0 | } |
14148 | | #endif |
14149 | | |
14150 | | #ifndef NO_PSK |
14151 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14152 | | * against context object. |
14153 | | * |
14154 | | * @param [in, out] ctx SSL/TLS context object. |
14155 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14156 | | */ |
14157 | | void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, |
14158 | | wc_psk_client_cs_callback cb) |
14159 | | { |
14160 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_cs_callback"); |
14161 | | |
14162 | | if (ctx == NULL) |
14163 | | return; |
14164 | | |
14165 | | ctx->havePSK = 1; |
14166 | | ctx->client_psk_cs_cb = cb; |
14167 | | } |
14168 | | |
14169 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14170 | | * against SSL object. |
14171 | | * |
14172 | | * @param [in, out] ssl SSL/TLS object. |
14173 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14174 | | */ |
14175 | | void wolfSSL_set_psk_client_cs_callback(WOLFSSL* ssl, |
14176 | | wc_psk_client_cs_callback cb) |
14177 | | { |
14178 | | byte haveRSA = 1; |
14179 | | int keySz = 0; |
14180 | | |
14181 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_cs_callback"); |
14182 | | |
14183 | | if (ssl == NULL) |
14184 | | return; |
14185 | | |
14186 | | ssl->options.havePSK = 1; |
14187 | | ssl->options.client_psk_cs_cb = cb; |
14188 | | |
14189 | | #ifdef NO_RSA |
14190 | | haveRSA = 0; |
14191 | | #endif |
14192 | | #ifndef NO_CERTS |
14193 | | keySz = ssl->buffers.keySz; |
14194 | | #endif |
14195 | | if (AllocateSuites(ssl) != 0) |
14196 | | return; |
14197 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14198 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14199 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14200 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14201 | | } |
14202 | | |
14203 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14204 | | * against context object. |
14205 | | * |
14206 | | * @param [in, out] ctx SSL/TLS context object. |
14207 | | * @param [in] cb Client PSK callback returning cipher suite. |
14208 | | */ |
14209 | | void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, |
14210 | | wc_psk_client_tls13_callback cb) |
14211 | | { |
14212 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_tls13_callback"); |
14213 | | |
14214 | | if (ctx == NULL) |
14215 | | return; |
14216 | | |
14217 | | ctx->havePSK = 1; |
14218 | | ctx->client_psk_tls13_cb = cb; |
14219 | | } |
14220 | | |
14221 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14222 | | * against SSL object. |
14223 | | * |
14224 | | * @param [in, out] ssl SSL/TLS object. |
14225 | | * @param [in] cb Client PSK callback returning cipher suite. |
14226 | | */ |
14227 | | void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl, |
14228 | | wc_psk_client_tls13_callback cb) |
14229 | | { |
14230 | | byte haveRSA = 1; |
14231 | | int keySz = 0; |
14232 | | |
14233 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_tls13_callback"); |
14234 | | |
14235 | | if (ssl == NULL) |
14236 | | return; |
14237 | | |
14238 | | ssl->options.havePSK = 1; |
14239 | | ssl->options.client_psk_tls13_cb = cb; |
14240 | | |
14241 | | #ifdef NO_RSA |
14242 | | haveRSA = 0; |
14243 | | #endif |
14244 | | #ifndef NO_CERTS |
14245 | | keySz = ssl->buffers.keySz; |
14246 | | #endif |
14247 | | if (AllocateSuites(ssl) != 0) |
14248 | | return; |
14249 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14250 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14251 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14252 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14253 | | } |
14254 | | |
14255 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14256 | | * against context object. |
14257 | | * |
14258 | | * @param [in, out] ctx SSL/TLS context object. |
14259 | | * @param [in] cb Server PSK callback returning cipher suite. |
14260 | | */ |
14261 | | void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, |
14262 | | wc_psk_server_tls13_callback cb) |
14263 | | { |
14264 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_tls13_callback"); |
14265 | | if (ctx == NULL) |
14266 | | return; |
14267 | | ctx->havePSK = 1; |
14268 | | ctx->server_psk_tls13_cb = cb; |
14269 | | } |
14270 | | |
14271 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14272 | | * against SSL object. |
14273 | | * |
14274 | | * @param [in, out] ssl SSL/TLS object. |
14275 | | * @param [in] cb Server PSK callback returning cipher suite. |
14276 | | */ |
14277 | | void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl, |
14278 | | wc_psk_server_tls13_callback cb) |
14279 | | { |
14280 | | byte haveRSA = 1; |
14281 | | int keySz = 0; |
14282 | | |
14283 | | WOLFSSL_ENTER("wolfSSL_set_psk_server_tls13_callback"); |
14284 | | if (ssl == NULL) |
14285 | | return; |
14286 | | |
14287 | | ssl->options.havePSK = 1; |
14288 | | ssl->options.server_psk_tls13_cb = cb; |
14289 | | |
14290 | | #ifdef NO_RSA |
14291 | | haveRSA = 0; |
14292 | | #endif |
14293 | | #ifndef NO_CERTS |
14294 | | keySz = ssl->buffers.keySz; |
14295 | | #endif |
14296 | | if (AllocateSuites(ssl) != 0) |
14297 | | return; |
14298 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14299 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14300 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14301 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14302 | | } |
14303 | | |
14304 | | /* Get name of first supported cipher suite that uses the hash indicated. |
14305 | | * |
14306 | | * @param [in] ssl SSL/TLS object. |
14307 | | * @param [in] hash Name of hash algorithm. e.g. "SHA256", "SHA384" |
14308 | | * @return Name of cipher suite. |
14309 | | * @return NULL on failure. |
14310 | | */ |
14311 | | const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash) |
14312 | | { |
14313 | | const char* name = NULL; |
14314 | | byte mac = no_mac; |
14315 | | int i; |
14316 | | const Suites* suites = WOLFSSL_SUITES(ssl); |
14317 | | |
14318 | | if (XSTRCMP(hash, "SHA256") == 0) { |
14319 | | mac = sha256_mac; |
14320 | | } |
14321 | | else if (XSTRCMP(hash, "SHA384") == 0) { |
14322 | | mac = sha384_mac; |
14323 | | } |
14324 | | if (mac != no_mac) { |
14325 | | for (i = 0; i < suites->suiteSz; i += 2) { |
14326 | | if (SuiteMac(suites->suites + i) == mac) { |
14327 | | name = GetCipherNameInternal(suites->suites[i + 0], |
14328 | | suites->suites[i + 1]); |
14329 | | break; |
14330 | | } |
14331 | | } |
14332 | | } |
14333 | | return name; |
14334 | | } |
14335 | | #endif /* !NO_PSK */ |
14336 | | |
14337 | | |
14338 | | #ifndef NO_WOLFSSL_SERVER |
14339 | | |
14340 | | /* The server accepting a connection from a client. |
14341 | | * The protocol version is expecting to be TLS v1.3. |
14342 | | * If the client downgrades, and older versions of the protocol are compiled |
14343 | | * in, the server will fallback to wolfSSL_accept(). |
14344 | | * Please see note at top of README if you get an error from accept. |
14345 | | * |
14346 | | * ssl The SSL/TLS object. |
14347 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
14348 | | * unrecoverable error occurs and 0 otherwise. |
14349 | | * For more error information use wolfSSL_get_error(). |
14350 | | */ |
14351 | | int wolfSSL_accept_TLSv13(WOLFSSL* ssl) |
14352 | | { |
14353 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14354 | | word16 havePSK = 0; |
14355 | | #endif |
14356 | | int ret = 0; |
14357 | | |
14358 | | WOLFSSL_ENTER("wolfSSL_accept_TLSv13"); |
14359 | | |
14360 | | #ifdef HAVE_ERRNO_H |
14361 | | errno = 0; |
14362 | | #endif |
14363 | | |
14364 | | if (ssl == NULL) |
14365 | | return WOLFSSL_FATAL_ERROR; |
14366 | | |
14367 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14368 | | havePSK = ssl->options.havePSK; |
14369 | | #endif |
14370 | | |
14371 | | if (ssl->options.side != WOLFSSL_SERVER_END) { |
14372 | | ssl->error = SIDE_ERROR; |
14373 | | WOLFSSL_ERROR(ssl->error); |
14374 | | return WOLFSSL_FATAL_ERROR; |
14375 | | } |
14376 | | |
14377 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
14378 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
14379 | | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
14380 | | return ret; |
14381 | | } |
14382 | | |
14383 | | #ifdef WOLFSSL_DTLS |
14384 | | if (ssl->version.major == DTLS_MAJOR) { |
14385 | | ssl->options.dtls = 1; |
14386 | | if (!IsDtlsNotSctpMode(ssl) || !ssl->options.sendCookie) |
14387 | | ssl->options.dtlsStateful = 1; |
14388 | | } |
14389 | | #endif |
14390 | | |
14391 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
14392 | | if ((ssl->AcceptFilter != NULL) && |
14393 | | ((ssl->options.acceptState == TLS13_ACCEPT_BEGIN) |
14394 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14395 | | || (ssl->options.acceptState == TLS13_ACCEPT_BEGIN_RENEG) |
14396 | | #endif |
14397 | | )) |
14398 | | { |
14399 | | wolfSSL_netfilter_decision_t res; |
14400 | | if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) == |
14401 | | WOLFSSL_SUCCESS) && |
14402 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
14403 | | ssl->error = SOCKET_FILTERED_E; |
14404 | | WOLFSSL_ERROR(ssl->error); |
14405 | | return WOLFSSL_FATAL_ERROR; |
14406 | | } |
14407 | | } |
14408 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
14409 | | |
14410 | | #ifndef NO_CERTS |
14411 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14412 | | if (!havePSK) |
14413 | | #endif |
14414 | | { |
14415 | | #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \ |
14416 | | defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY) |
14417 | | if (ssl->ctx->certSetupCb != NULL) { |
14418 | | WOLFSSL_MSG("CertSetupCb set. server cert and " |
14419 | | "key not checked"); |
14420 | | } |
14421 | | else |
14422 | | #endif |
14423 | | { |
14424 | | if (!ssl->buffers.certificate || |
14425 | | !ssl->buffers.certificate->buffer) { |
14426 | | |
14427 | | WOLFSSL_MSG("accept error: server cert required"); |
14428 | | ssl->error = NO_PRIVATE_KEY; |
14429 | | WOLFSSL_ERROR(ssl->error); |
14430 | | return WOLFSSL_FATAL_ERROR; |
14431 | | } |
14432 | | |
14433 | | if (!ssl->buffers.key || !ssl->buffers.key->buffer) { |
14434 | | /* allow no private key if using existing key */ |
14435 | | #ifdef WOLF_PRIVATE_KEY_ID |
14436 | | if (ssl->devId != INVALID_DEVID |
14437 | | #ifdef HAVE_PK_CALLBACKS |
14438 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
14439 | | #endif |
14440 | | ) { |
14441 | | WOLFSSL_MSG("Allowing no server private key (external)"); |
14442 | | } |
14443 | | else |
14444 | | #endif |
14445 | | { |
14446 | | WOLFSSL_MSG("accept error: server key required"); |
14447 | | ssl->error = NO_PRIVATE_KEY; |
14448 | | WOLFSSL_ERROR(ssl->error); |
14449 | | return WOLFSSL_FATAL_ERROR; |
14450 | | } |
14451 | | } |
14452 | | } |
14453 | | } |
14454 | | #endif /* NO_CERTS */ |
14455 | | |
14456 | | if (ssl->buffers.outputBuffer.length > 0 |
14457 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14458 | | /* do not send buffered or advance state if last error was an |
14459 | | async pending operation */ |
14460 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
14461 | | #endif |
14462 | | ) { |
14463 | | |
14464 | | /* fragOffset is non-zero when sending fragments. On the last |
14465 | | * fragment, fragOffset is zero again, and the state can be |
14466 | | * advanced. */ |
14467 | | int advanceState = |
14468 | | (ssl->options.acceptState == TLS13_ACCEPT_CLIENT_HELLO_DONE || |
14469 | | ssl->options.acceptState == |
14470 | | TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE || |
14471 | | ssl->options.acceptState == TLS13_ACCEPT_SECOND_REPLY_DONE || |
14472 | | ssl->options.acceptState == TLS13_SERVER_HELLO_SENT || |
14473 | | ssl->options.acceptState == TLS13_ACCEPT_THIRD_REPLY_DONE || |
14474 | | ssl->options.acceptState == TLS13_SERVER_EXTENSIONS_SENT || |
14475 | | ssl->options.acceptState == TLS13_CERT_REQ_SENT || |
14476 | | ssl->options.acceptState == TLS13_CERT_SENT || |
14477 | | ssl->options.acceptState == TLS13_CERT_VERIFY_SENT || |
14478 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_SENT || |
14479 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_DONE); |
14480 | | |
14481 | | #ifdef WOLFSSL_DTLS13 |
14482 | | if (ssl->options.dtls) |
14483 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
14484 | | && !ssl->dtls13SendingAckOrRtx; |
14485 | | #endif /* WOLFSSL_DTLS13 */ |
14486 | | |
14487 | | ret = SendBuffered(ssl); |
14488 | | if (ret == 0) { |
14489 | | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
14490 | | if (advanceState) { |
14491 | | ssl->options.acceptState++; |
14492 | | WOLFSSL_MSG("accept state: " |
14493 | | "Advanced from last buffered fragment send"); |
14494 | | #ifdef WOLFSSL_ASYNC_IO |
14495 | | FreeAsyncCtx(ssl, 0); |
14496 | | #endif |
14497 | | } |
14498 | | } |
14499 | | else { |
14500 | | WOLFSSL_MSG("accept state: " |
14501 | | "Not advanced, more fragments to send"); |
14502 | | } |
14503 | | |
14504 | | #ifdef WOLFSSL_DTLS13 |
14505 | | if (ssl->options.dtls) |
14506 | | ssl->dtls13SendingAckOrRtx = 0; |
14507 | | #endif /* WOLFSSL_DTLS13 */ |
14508 | | |
14509 | | } |
14510 | | else { |
14511 | | ssl->error = ret; |
14512 | | WOLFSSL_ERROR(ssl->error); |
14513 | | return WOLFSSL_FATAL_ERROR; |
14514 | | } |
14515 | | } |
14516 | | |
14517 | | ret = RetrySendAlert(ssl); |
14518 | | if (ret != 0) { |
14519 | | ssl->error = ret; |
14520 | | WOLFSSL_ERROR(ssl->error); |
14521 | | return WOLFSSL_FATAL_ERROR; |
14522 | | } |
14523 | | #ifdef WOLFSSL_DTLS13 |
14524 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
14525 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
14526 | | WOLFSSL_ERROR(ssl->error); |
14527 | | return WOLFSSL_FATAL_ERROR; |
14528 | | } |
14529 | | |
14530 | | /* we sent all the fragments. Advance state. */ |
14531 | | ssl->options.acceptState++; |
14532 | | } |
14533 | | #endif /* WOLFSSL_DTLS13 */ |
14534 | | |
14535 | | switch (ssl->options.acceptState) { |
14536 | | |
14537 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14538 | | case TLS13_ACCEPT_BEGIN_RENEG: |
14539 | | #endif |
14540 | | case TLS13_ACCEPT_BEGIN : |
14541 | | /* get client_hello */ |
14542 | | |
14543 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14544 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14545 | | WOLFSSL_ERROR(ssl->error); |
14546 | | return WOLFSSL_FATAL_ERROR; |
14547 | | } |
14548 | | |
14549 | | #ifdef WOLFSSL_DTLS13 |
14550 | | if (ssl->options.dtls) { |
14551 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14552 | | WOLFSSL_ERROR(ssl->error); |
14553 | | return WOLFSSL_FATAL_ERROR; |
14554 | | } |
14555 | | } |
14556 | | #endif /* WOLFSSL_DTLS13 */ |
14557 | | |
14558 | | } |
14559 | | |
14560 | | ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE; |
14561 | | WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE"); |
14562 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
14563 | | return wolfSSL_accept(ssl); |
14564 | | FALL_THROUGH; |
14565 | | |
14566 | | case TLS13_ACCEPT_CLIENT_HELLO_DONE : |
14567 | | if (ssl->options.serverState == |
14568 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14569 | | if ((ssl->error = SendTls13ServerHello(ssl, |
14570 | | hello_retry_request)) != 0) { |
14571 | | WOLFSSL_ERROR(ssl->error); |
14572 | | return WOLFSSL_FATAL_ERROR; |
14573 | | } |
14574 | | } |
14575 | | |
14576 | | ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE; |
14577 | | WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE"); |
14578 | | FALL_THROUGH; |
14579 | | |
14580 | | case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE : |
14581 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
14582 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14583 | | && ssl->options.serverState == |
14584 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14585 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14586 | | WOLFSSL_ERROR(ssl->error); |
14587 | | return WOLFSSL_FATAL_ERROR; |
14588 | | } |
14589 | | ssl->options.sentChangeCipher = 1; |
14590 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
14591 | | } |
14592 | | #endif |
14593 | | ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE; |
14594 | | WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE"); |
14595 | | FALL_THROUGH; |
14596 | | |
14597 | | case TLS13_ACCEPT_FIRST_REPLY_DONE : |
14598 | | if (ssl->options.serverState == |
14599 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14600 | | ssl->options.clientState = CLIENT_HELLO_RETRY; |
14601 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14602 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14603 | | WOLFSSL_ERROR(ssl->error); |
14604 | | return WOLFSSL_FATAL_ERROR; |
14605 | | } |
14606 | | |
14607 | | #ifdef WOLFSSL_DTLS13 |
14608 | | if (ssl->options.dtls) { |
14609 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14610 | | WOLFSSL_ERROR(ssl->error); |
14611 | | return WOLFSSL_FATAL_ERROR; |
14612 | | } |
14613 | | } |
14614 | | #endif /* WOLFSSL_DTLS13 */ |
14615 | | |
14616 | | } |
14617 | | } |
14618 | | |
14619 | | ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE; |
14620 | | WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE"); |
14621 | | FALL_THROUGH; |
14622 | | |
14623 | | case TLS13_ACCEPT_SECOND_REPLY_DONE : |
14624 | | if (ssl->options.returnOnGoodCh) { |
14625 | | /* Higher level in stack wants us to return. Simulate a |
14626 | | * WANT_WRITE to accomplish this. */ |
14627 | | ssl->error = WANT_WRITE; |
14628 | | return WOLFSSL_FATAL_ERROR; |
14629 | | } |
14630 | | |
14631 | | if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) { |
14632 | | WOLFSSL_ERROR(ssl->error); |
14633 | | return WOLFSSL_FATAL_ERROR; |
14634 | | } |
14635 | | ssl->options.acceptState = TLS13_SERVER_HELLO_SENT; |
14636 | | WOLFSSL_MSG("accept state SERVER_HELLO_SENT"); |
14637 | | FALL_THROUGH; |
14638 | | |
14639 | | case TLS13_SERVER_HELLO_SENT : |
14640 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14641 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14642 | | && !ssl->options.sentChangeCipher && !ssl->options.dtls) { |
14643 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14644 | | WOLFSSL_ERROR(ssl->error); |
14645 | | return WOLFSSL_FATAL_ERROR; |
14646 | | } |
14647 | | ssl->options.sentChangeCipher = 1; |
14648 | | } |
14649 | | #endif |
14650 | | |
14651 | | ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE; |
14652 | | WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE"); |
14653 | | FALL_THROUGH; |
14654 | | |
14655 | | case TLS13_ACCEPT_THIRD_REPLY_DONE : |
14656 | | #ifdef HAVE_SUPPORTED_CURVES |
14657 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14658 | | if (!ssl->options.noPskDheKe) |
14659 | | #endif |
14660 | | { |
14661 | | ssl->error = TLSX_KeyShare_DeriveSecret(ssl); |
14662 | | if (ssl->error != 0) |
14663 | | return WOLFSSL_FATAL_ERROR; |
14664 | | } |
14665 | | #endif |
14666 | | |
14667 | | if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) { |
14668 | | WOLFSSL_ERROR(ssl->error); |
14669 | | return WOLFSSL_FATAL_ERROR; |
14670 | | } |
14671 | | ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT; |
14672 | | WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT"); |
14673 | | FALL_THROUGH; |
14674 | | |
14675 | | case TLS13_SERVER_EXTENSIONS_SENT : |
14676 | | #ifndef NO_CERTS |
14677 | | if (!ssl->options.resuming) { |
14678 | | if (ssl->options.verifyPeer |
14679 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14680 | | && !ssl->options.verifyPostHandshake |
14681 | | #endif |
14682 | | ) { |
14683 | | ssl->error = SendTls13CertificateRequest(ssl, NULL, 0); |
14684 | | if (ssl->error != 0) { |
14685 | | WOLFSSL_ERROR(ssl->error); |
14686 | | return WOLFSSL_FATAL_ERROR; |
14687 | | } |
14688 | | } |
14689 | | else { |
14690 | | /* SERVER: Peer auth good if not verifying client. */ |
14691 | | ssl->options.peerAuthGood = 1; |
14692 | | } |
14693 | | } |
14694 | | #endif |
14695 | | ssl->options.acceptState = TLS13_CERT_REQ_SENT; |
14696 | | WOLFSSL_MSG("accept state CERT_REQ_SENT"); |
14697 | | FALL_THROUGH; |
14698 | | |
14699 | | case TLS13_CERT_REQ_SENT : |
14700 | | #ifndef NO_CERTS |
14701 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14702 | | if ((ssl->error = SendTls13Certificate(ssl)) != 0) { |
14703 | | WOLFSSL_ERROR(ssl->error); |
14704 | | return WOLFSSL_FATAL_ERROR; |
14705 | | } |
14706 | | } |
14707 | | #endif |
14708 | | ssl->options.acceptState = TLS13_CERT_SENT; |
14709 | | WOLFSSL_MSG("accept state CERT_SENT"); |
14710 | | FALL_THROUGH; |
14711 | | |
14712 | | case TLS13_CERT_SENT : |
14713 | | #if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
14714 | | defined(HAVE_ED25519) || defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
14715 | | defined(HAVE_DILITHIUM)) |
14716 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14717 | | if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) { |
14718 | | WOLFSSL_ERROR(ssl->error); |
14719 | | return WOLFSSL_FATAL_ERROR; |
14720 | | } |
14721 | | } |
14722 | | #endif |
14723 | | ssl->options.acceptState = TLS13_CERT_VERIFY_SENT; |
14724 | | WOLFSSL_MSG("accept state CERT_VERIFY_SENT"); |
14725 | | FALL_THROUGH; |
14726 | | |
14727 | | case TLS13_CERT_VERIFY_SENT : |
14728 | | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
14729 | | WOLFSSL_ERROR(ssl->error); |
14730 | | return WOLFSSL_FATAL_ERROR; |
14731 | | } |
14732 | | |
14733 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT; |
14734 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT"); |
14735 | | FALL_THROUGH; |
14736 | | |
14737 | | case TLS13_ACCEPT_FINISHED_SENT: |
14738 | | #ifdef WOLFSSL_EARLY_DATA |
14739 | | if (ssl->earlyData != no_early_data && |
14740 | | ssl->options.handShakeState != SERVER_FINISHED_COMPLETE) { |
14741 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
14742 | | return WOLFSSL_SUCCESS; |
14743 | | } |
14744 | | #endif |
14745 | | #ifdef HAVE_SESSION_TICKET |
14746 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
14747 | | if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 && |
14748 | | ssl->ctx->ticketEncCb != NULL) { |
14749 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14750 | | WOLFSSL_ERROR(ssl->error); |
14751 | | return WOLFSSL_FATAL_ERROR; |
14752 | | } |
14753 | | ssl->options.ticketsSent = 1; |
14754 | | } |
14755 | | #endif |
14756 | | #endif /* HAVE_SESSION_TICKET */ |
14757 | | ssl->options.acceptState = TLS13_PRE_TICKET_SENT; |
14758 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
14759 | | FALL_THROUGH; |
14760 | | |
14761 | | case TLS13_PRE_TICKET_SENT : |
14762 | | while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
14763 | | if ( (ssl->error = ProcessReply(ssl)) < 0) { |
14764 | | WOLFSSL_ERROR(ssl->error); |
14765 | | return WOLFSSL_FATAL_ERROR; |
14766 | | } |
14767 | | |
14768 | | #ifdef WOLFSSL_DTLS13 |
14769 | | if (ssl->options.dtls) { |
14770 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14771 | | WOLFSSL_ERROR(ssl->error); |
14772 | | return WOLFSSL_FATAL_ERROR; |
14773 | | } |
14774 | | } |
14775 | | #endif /* WOLFSSL_DTLS13 */ |
14776 | | } |
14777 | | |
14778 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE; |
14779 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE"); |
14780 | | FALL_THROUGH; |
14781 | | |
14782 | | case TLS13_ACCEPT_FINISHED_DONE : |
14783 | | /* SERVER: When not resuming and verifying peer but no certificate |
14784 | | * received and not failing when not received then peer auth good. |
14785 | | */ |
14786 | | if (!ssl->options.resuming && ssl->options.verifyPeer && |
14787 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14788 | | !ssl->options.verifyPostHandshake && |
14789 | | #endif |
14790 | | !ssl->options.havePeerCert && !ssl->options.failNoCert) { |
14791 | | ssl->options.peerAuthGood = 1; |
14792 | | } |
14793 | | /* SERVER: check peer authentication. */ |
14794 | | if (!ssl->options.peerAuthGood) { |
14795 | | WOLFSSL_MSG("Client authentication did not happen"); |
14796 | | return WOLFSSL_FATAL_ERROR; |
14797 | | } |
14798 | | #ifdef HAVE_SESSION_TICKET |
14799 | | while (ssl->options.ticketsSent < ssl->options.maxTicketTls13) { |
14800 | | if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb |
14801 | | != NULL) { |
14802 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14803 | | WOLFSSL_ERROR(ssl->error); |
14804 | | return WOLFSSL_FATAL_ERROR; |
14805 | | } |
14806 | | } |
14807 | | ssl->options.ticketsSent++; |
14808 | | |
14809 | | /* only one session ticket is sent on session resumption */ |
14810 | | if (ssl->options.resuming) { |
14811 | | break; |
14812 | | } |
14813 | | } |
14814 | | #endif /* HAVE_SESSION_TICKET */ |
14815 | | ssl->options.acceptState = TLS13_TICKET_SENT; |
14816 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
14817 | | FALL_THROUGH; |
14818 | | |
14819 | | case TLS13_TICKET_SENT : |
14820 | | #ifndef NO_HANDSHAKE_DONE_CB |
14821 | | if (ssl->hsDoneCb) { |
14822 | | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
14823 | | if (cbret < 0) { |
14824 | | ssl->error = cbret; |
14825 | | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
14826 | | return WOLFSSL_FATAL_ERROR; |
14827 | | } |
14828 | | } |
14829 | | #endif /* NO_HANDSHAKE_DONE_CB */ |
14830 | | |
14831 | | if (!ssl->options.keepResources) { |
14832 | | FreeHandshakeResources(ssl); |
14833 | | } |
14834 | | |
14835 | | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
14836 | | /* Free the remaining async context if not using it for crypto */ |
14837 | | FreeAsyncCtx(ssl, 1); |
14838 | | #endif |
14839 | | |
14840 | | ssl->error = 0; /* clear the error */ |
14841 | | |
14842 | | WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS); |
14843 | | return WOLFSSL_SUCCESS; |
14844 | | |
14845 | | default: |
14846 | | WOLFSSL_MSG("Unknown accept state ERROR"); |
14847 | | return WOLFSSL_FATAL_ERROR; |
14848 | | } |
14849 | | } |
14850 | | #endif |
14851 | | |
14852 | | #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET) |
14853 | | /* Server sends a session ticket to the peer. |
14854 | | * |
14855 | | * RFC 8446, section 4.6.1, para 1. |
14856 | | * |
14857 | | * ssl The SSL/TLS object. |
14858 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14859 | | * SIDE_ERROR when not a server, |
14860 | | * NOT_READY_ERROR when handshake not complete, |
14861 | | * WOLFSSL_FATAL_ERROR when creating or sending message fails, and |
14862 | | * WOLFSSL_SUCCESS on success. |
14863 | | */ |
14864 | | int wolfSSL_send_SessionTicket(WOLFSSL* ssl) |
14865 | | { |
14866 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14867 | | return BAD_FUNC_ARG; |
14868 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14869 | | return SIDE_ERROR; |
14870 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14871 | | return NOT_READY_ERROR; |
14872 | | |
14873 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14874 | | WOLFSSL_ERROR(ssl->error); |
14875 | | return WOLFSSL_FATAL_ERROR; |
14876 | | } |
14877 | | ssl->options.ticketsSent++; |
14878 | | |
14879 | | return WOLFSSL_SUCCESS; |
14880 | | } |
14881 | | #endif |
14882 | | |
14883 | | #ifdef WOLFSSL_EARLY_DATA |
14884 | | /* Sets the maximum amount of early data that can be seen by server when using |
14885 | | * session tickets for resumption. |
14886 | | * A value of zero indicates no early data is to be sent by client using session |
14887 | | * tickets. |
14888 | | * |
14889 | | * ctx The SSL/TLS CTX object. |
14890 | | * sz Maximum size of the early data. |
14891 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
14892 | | * 0 on success. |
14893 | | */ |
14894 | | int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz) |
14895 | | { |
14896 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14897 | | return BAD_FUNC_ARG; |
14898 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14899 | | return SIDE_ERROR; |
14900 | | |
14901 | | ctx->maxEarlyDataSz = sz; |
14902 | | |
14903 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
14904 | | /* 1 on success in OpenSSL*/ |
14905 | | return WOLFSSL_SUCCESS; |
14906 | | #else |
14907 | | return 0; |
14908 | | #endif |
14909 | | } |
14910 | | |
14911 | | /* Sets the maximum amount of early data that a client or server would like |
14912 | | * to exchange. Servers will advertise this value in session tickets sent |
14913 | | * to a client. |
14914 | | * A value of zero indicates no early data will be sent by a client, or |
14915 | | * no early data is accepted by a server (and announced as such in send out |
14916 | | * session tickets). |
14917 | | * |
14918 | | * ssl The SSL/TLS object. |
14919 | | * sz Maximum size of the early data. |
14920 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14921 | | * and 0 on success. |
14922 | | */ |
14923 | | int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz) |
14924 | | { |
14925 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14926 | | return BAD_FUNC_ARG; |
14927 | | |
14928 | | ssl->options.maxEarlyDataSz = sz; |
14929 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
14930 | | /* 1 on success in OpenSSL*/ |
14931 | | return WOLFSSL_SUCCESS; |
14932 | | #else |
14933 | | return 0; |
14934 | | #endif |
14935 | | } |
14936 | | |
14937 | | /* Gets the maximum amount of early data that can be seen by server when using |
14938 | | * session tickets for resumption. |
14939 | | * A value of zero indicates no early data is to be sent by client using session |
14940 | | * tickets. |
14941 | | * |
14942 | | * ctx The SSL/TLS CTX object. |
14943 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
14944 | | * returns the maximum amount of early data to be set |
14945 | | */ |
14946 | | int wolfSSL_CTX_get_max_early_data(WOLFSSL_CTX* ctx) |
14947 | | { |
14948 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14949 | | return BAD_FUNC_ARG; |
14950 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14951 | | return SIDE_ERROR; |
14952 | | |
14953 | | return ctx->maxEarlyDataSz; |
14954 | | } |
14955 | | |
14956 | | /* Gets the maximum amount of early data that can be seen by server when using |
14957 | | * session tickets for resumption. |
14958 | | * A value of zero indicates no early data is to be sent by client using session |
14959 | | * tickets. |
14960 | | * |
14961 | | * ssl The SSL/TLS object. |
14962 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14963 | | * SIDE_ERROR when not a server and |
14964 | | * returns the maximum amount of early data to be set |
14965 | | */ |
14966 | | int wolfSSL_get_max_early_data(WOLFSSL* ssl) |
14967 | | { |
14968 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14969 | | return BAD_FUNC_ARG; |
14970 | | |
14971 | | return ssl->options.maxEarlyDataSz; |
14972 | | } |
14973 | | |
14974 | | /* Write early data to the server. |
14975 | | * |
14976 | | * ssl The SSL/TLS object. |
14977 | | * data Early data to write |
14978 | | * sz The size of the early data in bytes. |
14979 | | * outSz The number of early data bytes written. |
14980 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
14981 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
14982 | | * early data bytes written. |
14983 | | */ |
14984 | | int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz) |
14985 | | { |
14986 | | int ret = 0; |
14987 | | |
14988 | | WOLFSSL_ENTER("wolfSSL_write_early_data"); |
14989 | | |
14990 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
14991 | | return BAD_FUNC_ARG; |
14992 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
14993 | | return BAD_FUNC_ARG; |
14994 | | |
14995 | | #ifndef NO_WOLFSSL_CLIENT |
14996 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
14997 | | return SIDE_ERROR; |
14998 | | |
14999 | | if (ssl->options.handShakeState == NULL_STATE) { |
15000 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
15001 | | ssl->earlyData = expecting_early_data; |
15002 | | ret = wolfSSL_connect_TLSv13(ssl); |
15003 | | if (ret != WOLFSSL_SUCCESS) |
15004 | | return WOLFSSL_FATAL_ERROR; |
15005 | | /* on client side, status is set to rejected */ |
15006 | | /* until sever accepts the early data extension. */ |
15007 | | ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED; |
15008 | | } |
15009 | | if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) { |
15010 | | #ifdef OPENSSL_EXTRA |
15011 | | /* when processed early data exceeds max size */ |
15012 | | if (ssl->session->maxEarlyDataSz > 0 && |
15013 | | (ssl->earlyDataSz + sz > ssl->session->maxEarlyDataSz)) { |
15014 | | ssl->error = TOO_MUCH_EARLY_DATA; |
15015 | | return WOLFSSL_FATAL_ERROR; |
15016 | | } |
15017 | | #endif |
15018 | | ret = SendData(ssl, data, sz); |
15019 | | if (ret > 0) { |
15020 | | *outSz = ret; |
15021 | | /* store amount of processed early data from client */ |
15022 | | ssl->earlyDataSz += ret; |
15023 | | } |
15024 | | } |
15025 | | #else |
15026 | | return SIDE_ERROR; |
15027 | | #endif |
15028 | | |
15029 | | WOLFSSL_LEAVE("wolfSSL_write_early_data", ret); |
15030 | | |
15031 | | if (ret < 0) |
15032 | | ret = WOLFSSL_FATAL_ERROR; |
15033 | | return ret; |
15034 | | } |
15035 | | |
15036 | | /* Read the any early data from the client. |
15037 | | * |
15038 | | * ssl The SSL/TLS object. |
15039 | | * data Buffer to put the early data into. |
15040 | | * sz The size of the buffer in bytes. |
15041 | | * outSz The number of early data bytes read. |
15042 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
15043 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
15044 | | * early data bytes read. |
15045 | | */ |
15046 | | int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz) |
15047 | | { |
15048 | | int ret = 0; |
15049 | | |
15050 | | WOLFSSL_ENTER("wolfSSL_read_early_data"); |
15051 | | |
15052 | | |
15053 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
15054 | | return BAD_FUNC_ARG; |
15055 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
15056 | | return BAD_FUNC_ARG; |
15057 | | |
15058 | | *outSz = 0; |
15059 | | #ifndef NO_WOLFSSL_SERVER |
15060 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15061 | | return SIDE_ERROR; |
15062 | | |
15063 | | if (ssl->options.handShakeState == NULL_STATE) { |
15064 | | /* the server flight can return WANT_WRITE and we re-enter here after |
15065 | | * setting ssl->earlyData = process_early_data, set earlyData to |
15066 | | * expecting_early_data just once */ |
15067 | | if (ssl->earlyData < expecting_early_data) |
15068 | | ssl->earlyData = expecting_early_data; |
15069 | | /* this used to be: ret = wolfSSL_accept_TLSv13(ssl); |
15070 | | * However, wolfSSL_accept_TLSv13() expects a certificate to |
15071 | | * be installed already, which is not the case in servers |
15072 | | * such as HAProxy. They do it after inspecting the ClientHello. |
15073 | | * The common wolfssl_accept() allows that. */ |
15074 | | ret = wolfSSL_accept(ssl); |
15075 | | if (ret <= 0) |
15076 | | return WOLFSSL_FATAL_ERROR; |
15077 | | } |
15078 | | if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) { |
15079 | | ssl->options.clientInEarlyData = 1; |
15080 | | ret = ReceiveData(ssl, (byte*)data, (size_t)sz, FALSE); |
15081 | | ssl->options.clientInEarlyData = 0; |
15082 | | if (ret > 0) |
15083 | | *outSz = ret; |
15084 | | if (ssl->error == WC_NO_ERR_TRACE(APP_DATA_READY)) { |
15085 | | ret = 0; |
15086 | | ssl->error = WOLFSSL_ERROR_NONE; |
15087 | | #ifdef WOLFSSL_DTLS13 |
15088 | | if (ssl->options.dtls) { |
15089 | | ret = Dtls13DoScheduledWork(ssl); |
15090 | | if (ret < 0) { |
15091 | | ssl->error = ret; |
15092 | | WOLFSSL_ERROR(ssl->error); |
15093 | | return WOLFSSL_FATAL_ERROR; |
15094 | | } |
15095 | | } |
15096 | | #endif /* WOLFSSL_DTLS13 */ |
15097 | | } |
15098 | | } |
15099 | | #ifdef WOLFSSL_DTLS13 |
15100 | | else if (ssl->buffers.outputBuffer.length > 0 && |
15101 | | ssl->options.dtls && ssl->dtls13SendingAckOrRtx) { |
15102 | | ret = SendBuffered(ssl); |
15103 | | if (ret == 0) { |
15104 | | ssl->dtls13SendingAckOrRtx = 0; |
15105 | | } |
15106 | | else { |
15107 | | ssl->error = ret; |
15108 | | WOLFSSL_ERROR(ssl->error); |
15109 | | return WOLFSSL_FATAL_ERROR; |
15110 | | } |
15111 | | } |
15112 | | #endif /* WOLFSSL_DTLS13 */ |
15113 | | else |
15114 | | ret = 0; |
15115 | | #else |
15116 | | return SIDE_ERROR; |
15117 | | #endif |
15118 | | |
15119 | | WOLFSSL_LEAVE("wolfSSL_read_early_data", ret); |
15120 | | |
15121 | | if (ret < 0) |
15122 | | ret = WOLFSSL_FATAL_ERROR; |
15123 | | return ret; |
15124 | | } |
15125 | | |
15126 | | /* Returns early data status |
15127 | | * |
15128 | | * ssl The SSL/TLS object. |
15129 | | * returns WOLFSSL_EARLY_DATA_ACCEPTED if the data was accepted |
15130 | | * WOLFSSL_EARLY_DATA_REJECTED if the data was rejected |
15131 | | * WOLFSSL_EARLY_DATA_NOT_SENT if no early data was sent |
15132 | | */ |
15133 | | int wolfSSL_get_early_data_status(const WOLFSSL* ssl) |
15134 | | { |
15135 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15136 | | return BAD_FUNC_ARG; |
15137 | | |
15138 | | return ssl->earlyDataStatus; |
15139 | | } |
15140 | | #endif |
15141 | | |
15142 | | #ifdef HAVE_SECRET_CALLBACK |
15143 | | int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx) |
15144 | | { |
15145 | | WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb"); |
15146 | | if (ssl == NULL) |
15147 | | return WOLFSSL_FATAL_ERROR; |
15148 | | |
15149 | | ssl->tls13SecretCb = cb; |
15150 | | ssl->tls13SecretCtx = ctx; |
15151 | | |
15152 | | return WOLFSSL_SUCCESS; |
15153 | | } |
15154 | | |
15155 | | #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE) |
15156 | | int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret, |
15157 | | int secretSz, void* ctx) |
15158 | | { |
15159 | | int i; |
15160 | | const char* str = NULL; |
15161 | | byte clientRandom[RAN_LEN]; |
15162 | | int clientRandomSz; |
15163 | | XFILE fp; |
15164 | | |
15165 | | (void) ctx; |
15166 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15167 | | fp = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "ab"); |
15168 | | if (fp == XBADFILE) { |
15169 | | return BAD_FUNC_ARG; |
15170 | | } |
15171 | | #else |
15172 | | fp = stderr; |
15173 | | #endif |
15174 | | |
15175 | | clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom, |
15176 | | sizeof(clientRandom)); |
15177 | | |
15178 | | if (clientRandomSz <= 0) { |
15179 | | printf("Error getting server random %d\n", clientRandomSz); |
15180 | | return BAD_FUNC_ARG; |
15181 | | } |
15182 | | |
15183 | | #if 0 |
15184 | | printf("TLS Server Secret CB: Rand %d, Secret %d\n", |
15185 | | serverRandomSz, secretSz); |
15186 | | #endif |
15187 | | |
15188 | | switch (id) { |
15189 | | case CLIENT_EARLY_TRAFFIC_SECRET: |
15190 | | str = "CLIENT_EARLY_TRAFFIC_SECRET"; break; |
15191 | | case EARLY_EXPORTER_SECRET: |
15192 | | str = "EARLY_EXPORTER_SECRET"; break; |
15193 | | case CLIENT_HANDSHAKE_TRAFFIC_SECRET: |
15194 | | str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break; |
15195 | | case SERVER_HANDSHAKE_TRAFFIC_SECRET: |
15196 | | str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break; |
15197 | | case CLIENT_TRAFFIC_SECRET: |
15198 | | str = "CLIENT_TRAFFIC_SECRET_0"; break; |
15199 | | case SERVER_TRAFFIC_SECRET: |
15200 | | str = "SERVER_TRAFFIC_SECRET_0"; break; |
15201 | | case EXPORTER_SECRET: |
15202 | | str = "EXPORTER_SECRET"; break; |
15203 | | default: |
15204 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15205 | | XFCLOSE(fp); |
15206 | | #endif |
15207 | | return BAD_FUNC_ARG; |
15208 | | break; |
15209 | | } |
15210 | | |
15211 | | fprintf(fp, "%s ", str); |
15212 | | for (i = 0; i < (int)clientRandomSz; i++) { |
15213 | | fprintf(fp, "%02x", clientRandom[i]); |
15214 | | } |
15215 | | fprintf(fp, " "); |
15216 | | for (i = 0; i < secretSz; i++) { |
15217 | | fprintf(fp, "%02x", secret[i]); |
15218 | | } |
15219 | | fprintf(fp, "\n"); |
15220 | | |
15221 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15222 | | XFCLOSE(fp); |
15223 | | #endif |
15224 | | |
15225 | | return 0; |
15226 | | } |
15227 | | #endif |
15228 | | #endif |
15229 | | |
15230 | | #undef ERROR_OUT |
15231 | | |
15232 | | #endif /* !WOLFCRYPT_ONLY */ |
15233 | | |
15234 | | #endif /* !NO_TLS && WOLFSSL_TLS13 */ |