/src/wolfssl-fastmath/src/tls13.c
Line | Count | Source (jump to first uncovered line) |
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 | 3.86k | { |
203 | 3.86k | 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 | 3.86k | (void)ssl; |
218 | 3.86k | PRIVATE_KEY_UNLOCK(); |
219 | 3.86k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
220 | 3.86k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
221 | 3.86k | protocol, protocolLen, |
222 | 3.86k | label, labelLen, |
223 | 3.86k | info, infoLen, digest, |
224 | 3.86k | 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 | 3.86k | PRIVATE_KEY_LOCK(); |
232 | 3.86k | return ret; |
233 | 3.86k | } |
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 | 8.92k | { |
245 | 8.92k | 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 | 8.92k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
260 | 8.92k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
261 | 8.92k | protocol, protocolLen, |
262 | 8.92k | label, labelLen, |
263 | 8.92k | info, infoLen, digest, |
264 | 8.92k | 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 | 8.92k | (void)ssl; |
278 | 8.92k | (void)side; |
279 | 8.92k | return ret; |
280 | 8.92k | } |
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_WOLFSSL_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 | 671 | #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 | 671 | { |
671 | 671 | int ret; |
672 | 671 | WOLFSSL_MSG("Derive Client Handshake Secret"); |
673 | 671 | 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 | 671 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
688 | 671 | clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ, |
689 | 671 | 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 | 671 | return ret; |
711 | 671 | } |
712 | | |
713 | | /* The length of the server handshake label. */ |
714 | 664 | #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 | 664 | { |
727 | 664 | int ret; |
728 | 664 | WOLFSSL_MSG("Derive Server Handshake Secret"); |
729 | 664 | 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 | 664 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
744 | 664 | serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ, |
745 | 664 | 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 | 664 | return ret; |
768 | 664 | } |
769 | | |
770 | | /* The length of the client application traffic label. */ |
771 | 326 | #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 | 326 | { |
784 | 326 | int ret; |
785 | 326 | WOLFSSL_MSG("Derive Client Traffic Secret"); |
786 | 326 | 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 | 326 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
801 | 326 | clientAppLabel, CLIENT_APP_LABEL_SZ, |
802 | 326 | 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 | 326 | return ret; |
825 | 326 | } |
826 | | |
827 | | /* The length of the server application traffic label. */ |
828 | 331 | #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 | 331 | { |
841 | 331 | int ret; |
842 | 331 | WOLFSSL_MSG("Derive Server Traffic Secret"); |
843 | 331 | 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 | 331 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
858 | 331 | serverAppLabel, SERVER_APP_LABEL_SZ, |
859 | 331 | 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 | 331 | return ret; |
882 | 331 | } |
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.52k | #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.52k | { |
1087 | 3.52k | WOLFSSL_MSG("Derive Finished Secret"); |
1088 | 3.52k | return Tls13DeriveKey(ssl, secret, -1, key, finishedLabel, |
1089 | 3.52k | FINISHED_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1090 | 3.52k | side); |
1091 | 3.52k | } |
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.85k | { |
1152 | 3.85k | int ret; |
1153 | | |
1154 | 3.85k | WOLFSSL_MSG("Derive Early Secret"); |
1155 | 3.85k | 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.85k | 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.85k | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1170 | 3.85k | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1171 | 3.85k | #endif |
1172 | 3.85k | PRIVATE_KEY_LOCK(); |
1173 | 3.85k | return ret; |
1174 | 3.85k | } |
1175 | | |
1176 | | /* The length of the derived label. */ |
1177 | 695 | #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 | 695 | { |
1188 | 695 | byte key[WC_MAX_DIGEST_SIZE]; |
1189 | 695 | int ret; |
1190 | 695 | WOLFSSL_MSG("Derive Handshake Secret"); |
1191 | 695 | 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 | 695 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
1201 | 695 | derivedLabel, DERIVED_LABEL_SZ, |
1202 | 695 | NULL, 0, ssl->specs.mac_algorithm); |
1203 | 695 | if (ret != 0) |
1204 | 14 | return ret; |
1205 | | |
1206 | 681 | PRIVATE_KEY_UNLOCK(); |
1207 | 681 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->preMasterSecret, |
1208 | 681 | key, ssl->specs.hash_size, |
1209 | 681 | ssl->arrays->preMasterSecret, (int)ssl->arrays->preMasterSz, |
1210 | 681 | mac2hash(ssl->specs.mac_algorithm)); |
1211 | 681 | PRIVATE_KEY_LOCK(); |
1212 | | |
1213 | 681 | return ret; |
1214 | 695 | } |
1215 | | |
1216 | | /* Derive the master secret using HKDF Extract. |
1217 | | * |
1218 | | * ssl The SSL/TLS object. |
1219 | | */ |
1220 | | int DeriveMasterSecret(WOLFSSL* ssl) |
1221 | | { |
1222 | | byte key[WC_MAX_DIGEST_SIZE]; |
1223 | | int ret; |
1224 | | WOLFSSL_MSG("Derive Master Secret"); |
1225 | | if (ssl == NULL || ssl->arrays == NULL) { |
1226 | | return BAD_FUNC_ARG; |
1227 | | } |
1228 | | |
1229 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1230 | | ret = tsip_Tls13DeriveMasterSecret(ssl); |
1231 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1232 | | return ret; |
1233 | | #endif |
1234 | | |
1235 | | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret, |
1236 | | derivedLabel, DERIVED_LABEL_SZ, |
1237 | | NULL, 0, ssl->specs.mac_algorithm); |
1238 | | if (ret != 0) |
1239 | | return ret; |
1240 | | |
1241 | | PRIVATE_KEY_UNLOCK(); |
1242 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->masterSecret, |
1243 | | key, ssl->specs.hash_size, |
1244 | | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1245 | | PRIVATE_KEY_LOCK(); |
1246 | | |
1247 | | #ifdef HAVE_KEYING_MATERIAL |
1248 | | if (ret != 0) |
1249 | | return ret; |
1250 | | /* Calculate exporter secret only when saving arrays */ |
1251 | | if (ssl->options.saveArrays) |
1252 | | ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret); |
1253 | | #endif |
1254 | | |
1255 | | return ret; |
1256 | | } |
1257 | | |
1258 | | #if defined(HAVE_SESSION_TICKET) |
1259 | | /* Length of the resumption label. */ |
1260 | | #define RESUMPTION_LABEL_SZ 10 |
1261 | | /* Resumption label for generating PSK associated with the ticket. */ |
1262 | | static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption"; |
1263 | | |
1264 | | /* Derive the PSK associated with the ticket. |
1265 | | * |
1266 | | * ssl The SSL/TLS object. |
1267 | | * nonce The nonce to derive with. |
1268 | | * nonceLen The length of the nonce to derive with. |
1269 | | * secret The derived secret. |
1270 | | * returns 0 on success, otherwise failure. |
1271 | | */ |
1272 | | int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret) |
1273 | | { |
1274 | | int digestAlg; |
1275 | | /* Only one protocol version defined at this time. */ |
1276 | | const byte* protocol = tls13ProtocolLabel; |
1277 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
1278 | | int ret; |
1279 | | |
1280 | | WOLFSSL_MSG("Derive Resumption PSK"); |
1281 | | |
1282 | | #ifdef WOLFSSL_DTLS13 |
1283 | | if (ssl->options.dtls) { |
1284 | | protocol = dtls13ProtocolLabel; |
1285 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1286 | | } |
1287 | | #endif /* WOLFSSL_DTLS13 */ |
1288 | | |
1289 | | switch (ssl->specs.mac_algorithm) { |
1290 | | #ifndef NO_SHA256 |
1291 | | case sha256_mac: |
1292 | | digestAlg = WC_SHA256; |
1293 | | break; |
1294 | | #endif |
1295 | | |
1296 | | #ifdef WOLFSSL_SHA384 |
1297 | | case sha384_mac: |
1298 | | digestAlg = WC_SHA384; |
1299 | | break; |
1300 | | #endif |
1301 | | |
1302 | | #ifdef WOLFSSL_TLS13_SHA512 |
1303 | | case sha512_mac: |
1304 | | digestAlg = WC_SHA512; |
1305 | | break; |
1306 | | #endif |
1307 | | |
1308 | | #ifdef WOLFSSL_SM3 |
1309 | | case sm3_mac: |
1310 | | digestAlg = WC_SM3; |
1311 | | break; |
1312 | | #endif |
1313 | | |
1314 | | default: |
1315 | | return BAD_FUNC_ARG; |
1316 | | } |
1317 | | |
1318 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
1319 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
1320 | | PRIVATE_KEY_UNLOCK(); |
1321 | | ret = wc_Tls13_HKDF_Expand_Label_Alloc(secret, ssl->specs.hash_size, |
1322 | | ssl->session->masterSecret, ssl->specs.hash_size, protocol, protocolLen, |
1323 | | resumptionLabel, RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg, |
1324 | | ssl->heap); |
1325 | | PRIVATE_KEY_LOCK(); |
1326 | | #else |
1327 | | ret = Tls13HKDFExpandLabel(ssl, secret, ssl->specs.hash_size, |
1328 | | ssl->session->masterSecret, ssl->specs.hash_size, |
1329 | | protocol, protocolLen, resumptionLabel, |
1330 | | RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg); |
1331 | | #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */ |
1332 | | return ret; |
1333 | | } |
1334 | | #endif /* HAVE_SESSION_TICKET */ |
1335 | | |
1336 | | |
1337 | | /* Calculate the HMAC of message data to this point. |
1338 | | * |
1339 | | * ssl The SSL/TLS object. |
1340 | | * key The HMAC key. |
1341 | | * hash The hash result - verify data. |
1342 | | * returns length of verify data generated. |
1343 | | */ |
1344 | | static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash, |
1345 | | word32* pHashSz) |
1346 | | { |
1347 | | #ifdef WOLFSSL_SMALL_STACK |
1348 | | Hmac* verifyHmac; |
1349 | | #else |
1350 | | Hmac verifyHmac[1]; |
1351 | | #endif |
1352 | | int hashType = WC_SHA256; |
1353 | | int hashSz = WC_SHA256_DIGEST_SIZE; |
1354 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
1355 | | |
1356 | | if (ssl == NULL || key == NULL || hash == NULL) { |
1357 | | return BAD_FUNC_ARG; |
1358 | | } |
1359 | | |
1360 | | /* Get the hash of the previous handshake messages. */ |
1361 | | switch (ssl->specs.mac_algorithm) { |
1362 | | #ifndef NO_SHA256 |
1363 | | case sha256_mac: |
1364 | | hashType = WC_SHA256; |
1365 | | hashSz = WC_SHA256_DIGEST_SIZE; |
1366 | | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
1367 | | break; |
1368 | | #endif /* !NO_SHA256 */ |
1369 | | #ifdef WOLFSSL_SHA384 |
1370 | | case sha384_mac: |
1371 | | hashType = WC_SHA384; |
1372 | | hashSz = WC_SHA384_DIGEST_SIZE; |
1373 | | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
1374 | | break; |
1375 | | #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 | | #ifdef WOLFSSL_SM3 |
1384 | | case sm3_mac: |
1385 | | hashType = WC_SM3; |
1386 | | hashSz = WC_SM3_DIGEST_SIZE; |
1387 | | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
1388 | | break; |
1389 | | #endif /* WOLFSSL_SM3 */ |
1390 | | default: |
1391 | | ret = BAD_FUNC_ARG; |
1392 | | break; |
1393 | | } |
1394 | | if (ret != 0) |
1395 | | 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 | | #ifdef WOLFSSL_SMALL_STACK |
1405 | | verifyHmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_HMAC); |
1406 | | if (verifyHmac == NULL) { |
1407 | | return MEMORY_E; |
1408 | | } |
1409 | | #endif |
1410 | | |
1411 | | /* Calculate the verify data. */ |
1412 | | ret = wc_HmacInit(verifyHmac, ssl->heap, ssl->devId); |
1413 | | if (ret == 0) { |
1414 | | ret = wc_HmacSetKey(verifyHmac, hashType, key, ssl->specs.hash_size); |
1415 | | if (ret == 0) |
1416 | | ret = wc_HmacUpdate(verifyHmac, hash, (word32)hashSz); |
1417 | | if (ret == 0) |
1418 | | ret = wc_HmacFinal(verifyHmac, hash); |
1419 | | wc_HmacFree(verifyHmac); |
1420 | | } |
1421 | | |
1422 | | #ifdef WOLFSSL_SMALL_STACK |
1423 | | XFREE(verifyHmac, NULL, DYNAMIC_TYPE_HMAC); |
1424 | | #endif |
1425 | | |
1426 | | #ifdef WOLFSSL_DEBUG_TLS |
1427 | | WOLFSSL_MSG(" Hash"); |
1428 | | WOLFSSL_BUFFER(hash, hashSz); |
1429 | | #endif |
1430 | | |
1431 | | if (pHashSz) |
1432 | | *pHashSz = (word32)hashSz; |
1433 | | |
1434 | | return ret; |
1435 | | } |
1436 | | |
1437 | | /* The length of the label to use when deriving keys. */ |
1438 | | #define WRITE_KEY_LABEL_SZ 3 |
1439 | | /* The length of the label to use when deriving IVs. */ |
1440 | | #define WRITE_IV_LABEL_SZ 2 |
1441 | | /* The label to use when deriving keys. */ |
1442 | | static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key"; |
1443 | | /* The label to use when deriving IVs. */ |
1444 | | static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv"; |
1445 | | |
1446 | | /* Derive the keys and IVs for TLS v1.3. |
1447 | | * |
1448 | | * ssl The SSL/TLS object. |
1449 | | * secret early_data_key when deriving the key and IV for encrypting early |
1450 | | * data application data and end_of_early_data messages. |
1451 | | * handshake_key when deriving keys and IVs for encrypting handshake |
1452 | | * messages. |
1453 | | * traffic_key when deriving first keys and IVs for encrypting |
1454 | | * traffic messages. |
1455 | | * update_traffic_key when deriving next keys and IVs for encrypting |
1456 | | * traffic messages. |
1457 | | * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived. |
1458 | | * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived. |
1459 | | * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived. |
1460 | | * store 1 indicates to derive the keys and IVs from derived secret and |
1461 | | * store ready for provisioning. |
1462 | | * returns 0 on success, otherwise failure. |
1463 | | */ |
1464 | | int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store) |
1465 | | { |
1466 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1467 | | int i = 0; |
1468 | | #ifdef WOLFSSL_SMALL_STACK |
1469 | | byte* key_dig; |
1470 | | #else |
1471 | | byte key_dig[MAX_PRF_DIG]; |
1472 | | #endif |
1473 | | int provision; |
1474 | | |
1475 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1476 | | ret = tsip_Tls13DeriveKeys(ssl, secret, side); |
1477 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1478 | | return ret; |
1479 | | } |
1480 | | ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1481 | | #endif |
1482 | | |
1483 | | #ifdef WOLFSSL_SMALL_STACK |
1484 | | key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1485 | | if (key_dig == NULL) |
1486 | | return MEMORY_E; |
1487 | | #endif |
1488 | | |
1489 | | if (side == ENCRYPT_AND_DECRYPT_SIDE) { |
1490 | | provision = PROVISION_CLIENT_SERVER; |
1491 | | } |
1492 | | else { |
1493 | | provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^ |
1494 | | (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT : |
1495 | | PROVISION_SERVER; |
1496 | | } |
1497 | | |
1498 | | /* Derive the appropriate secret to use in the HKDF. */ |
1499 | | switch (secret) { |
1500 | | #ifdef WOLFSSL_EARLY_DATA |
1501 | | case early_data_key: |
1502 | | ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret, |
1503 | | WOLFSSL_CLIENT_END); |
1504 | | if (ret != 0) |
1505 | | goto end; |
1506 | | break; |
1507 | | #endif |
1508 | | |
1509 | | case handshake_key: |
1510 | | if (provision & PROVISION_CLIENT) { |
1511 | | ret = DeriveClientHandshakeSecret(ssl, |
1512 | | ssl->clientSecret); |
1513 | | if (ret != 0) |
1514 | | goto end; |
1515 | | } |
1516 | | if (provision & PROVISION_SERVER) { |
1517 | | ret = DeriveServerHandshakeSecret(ssl, |
1518 | | ssl->serverSecret); |
1519 | | if (ret != 0) |
1520 | | goto end; |
1521 | | } |
1522 | | break; |
1523 | | |
1524 | | case traffic_key: |
1525 | | if (provision & PROVISION_CLIENT) { |
1526 | | ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret); |
1527 | | if (ret != 0) |
1528 | | goto end; |
1529 | | } |
1530 | | if (provision & PROVISION_SERVER) { |
1531 | | ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret); |
1532 | | if (ret != 0) |
1533 | | goto end; |
1534 | | } |
1535 | | break; |
1536 | | |
1537 | | case update_traffic_key: |
1538 | | if (provision & PROVISION_CLIENT) { |
1539 | | ret = DeriveTrafficSecret(ssl, ssl->clientSecret, |
1540 | | WOLFSSL_CLIENT_END); |
1541 | | if (ret != 0) |
1542 | | goto end; |
1543 | | } |
1544 | | if (provision & PROVISION_SERVER) { |
1545 | | ret = DeriveTrafficSecret(ssl, ssl->serverSecret, |
1546 | | WOLFSSL_SERVER_END); |
1547 | | if (ret != 0) |
1548 | | goto end; |
1549 | | } |
1550 | | break; |
1551 | | |
1552 | | default: |
1553 | | ret = BAD_FUNC_ARG; |
1554 | | break; |
1555 | | } |
1556 | | |
1557 | | #ifdef WOLFSSL_QUIC |
1558 | | if (WOLFSSL_IS_QUIC(ssl)) { |
1559 | | ret = wolfSSL_quic_forward_secrets(ssl, secret, side); |
1560 | | if (ret != 0) |
1561 | | goto end; |
1562 | | } |
1563 | | #endif /* WOLFSSL_QUIC */ |
1564 | | |
1565 | | if (!store) |
1566 | | goto end; |
1567 | | |
1568 | | /* Key data = client key | server key | client IV | server IV */ |
1569 | | |
1570 | | if (provision & PROVISION_CLIENT) { |
1571 | | /* Derive the client key. */ |
1572 | | WOLFSSL_MSG("Derive Client Key"); |
1573 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1574 | | ssl->clientSecret, writeKeyLabel, |
1575 | | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1576 | | WOLFSSL_CLIENT_END); |
1577 | | if (ret != 0) |
1578 | | goto end; |
1579 | | i += ssl->specs.key_size; |
1580 | | } |
1581 | | |
1582 | | if (provision & PROVISION_SERVER) { |
1583 | | /* Derive the server key. */ |
1584 | | WOLFSSL_MSG("Derive Server Key"); |
1585 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1586 | | ssl->serverSecret, writeKeyLabel, |
1587 | | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1588 | | WOLFSSL_SERVER_END); |
1589 | | if (ret != 0) |
1590 | | goto end; |
1591 | | i += ssl->specs.key_size; |
1592 | | } |
1593 | | |
1594 | | if (provision & PROVISION_CLIENT) { |
1595 | | /* Derive the client IV. */ |
1596 | | WOLFSSL_MSG("Derive Client IV"); |
1597 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1598 | | ssl->clientSecret, writeIVLabel, |
1599 | | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1600 | | WOLFSSL_CLIENT_END); |
1601 | | if (ret != 0) |
1602 | | goto end; |
1603 | | i += ssl->specs.iv_size; |
1604 | | } |
1605 | | |
1606 | | if (provision & PROVISION_SERVER) { |
1607 | | /* Derive the server IV. */ |
1608 | | WOLFSSL_MSG("Derive Server IV"); |
1609 | | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1610 | | ssl->serverSecret, writeIVLabel, |
1611 | | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1612 | | WOLFSSL_SERVER_END); |
1613 | | if (ret != 0) |
1614 | | goto end; |
1615 | | i += ssl->specs.iv_size; |
1616 | | } |
1617 | | |
1618 | | /* Store keys and IVs but don't activate them. */ |
1619 | | ret = StoreKeys(ssl, key_dig, provision); |
1620 | | |
1621 | | #ifdef WOLFSSL_DTLS13 |
1622 | | if (ret != 0) |
1623 | | goto end; |
1624 | | |
1625 | | if (ssl->options.dtls) { |
1626 | | w64wrapper epochNumber; |
1627 | | ret = Dtls13DeriveSnKeys(ssl, provision); |
1628 | | if (ret != 0) |
1629 | | goto end; |
1630 | | |
1631 | | switch (secret) { |
1632 | | case early_data_key: |
1633 | | epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA); |
1634 | | break; |
1635 | | case handshake_key: |
1636 | | epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
1637 | | break; |
1638 | | case traffic_key: |
1639 | | case no_key: |
1640 | | epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
1641 | | break; |
1642 | | case update_traffic_key: |
1643 | | if (side == ENCRYPT_SIDE_ONLY) { |
1644 | | epochNumber = ssl->dtls13Epoch; |
1645 | | } |
1646 | | else if (side == DECRYPT_SIDE_ONLY) { |
1647 | | epochNumber = ssl->dtls13PeerEpoch; |
1648 | | } |
1649 | | else { |
1650 | | ret = BAD_STATE_E; |
1651 | | goto end; |
1652 | | } |
1653 | | w64Increment(&epochNumber); |
1654 | | break; |
1655 | | default: |
1656 | | ret = BAD_STATE_E; |
1657 | | goto end; |
1658 | | } |
1659 | | ret = Dtls13NewEpoch(ssl, epochNumber, side); |
1660 | | if (ret != 0) |
1661 | | goto end; |
1662 | | } |
1663 | | |
1664 | | #endif /* WOLFSSL_DTLS13 */ |
1665 | | |
1666 | | end: |
1667 | | ForceZero(key_dig, (word32)i); |
1668 | | #ifdef WOLFSSL_SMALL_STACK |
1669 | | XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1670 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1671 | | wc_MemZero_Check(key_dig, MAX_PRF_DIG); |
1672 | | #endif |
1673 | | |
1674 | | if (ret != 0) { |
1675 | | WOLFSSL_ERROR_VERBOSE(ret); |
1676 | | } |
1677 | | |
1678 | | return ret; |
1679 | | } |
1680 | | |
1681 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
1682 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
1683 | | #ifndef NO_ASN_TIME |
1684 | | #if defined(USER_TICKS) |
1685 | | #if 0 |
1686 | | word32 TimeNowInMilliseconds(void) |
1687 | | { |
1688 | | /* |
1689 | | write your own clock tick function if don't want gettimeofday() |
1690 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1691 | | */ |
1692 | | } |
1693 | | #endif |
1694 | | |
1695 | | #elif defined(TIME_OVERRIDES) |
1696 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1697 | | word32 TimeNowInMilliseconds(void) |
1698 | | { |
1699 | | return (word32) wc_Time(0) * 1000; |
1700 | | } |
1701 | | #else |
1702 | | #ifndef HAVE_TIME_T_TYPE |
1703 | | typedef long time_t; |
1704 | | #endif |
1705 | | extern time_t XTIME(time_t * timer); |
1706 | | |
1707 | | /* The time in milliseconds. |
1708 | | * Used for tickets to represent difference between when first seen and when |
1709 | | * sending. |
1710 | | * |
1711 | | * returns the time in milliseconds as a 32-bit value. |
1712 | | */ |
1713 | | word32 TimeNowInMilliseconds(void) |
1714 | | { |
1715 | | return (word32) XTIME(0) * 1000; |
1716 | | } |
1717 | | #endif |
1718 | | |
1719 | | #elif defined(XTIME_MS) |
1720 | | word32 TimeNowInMilliseconds(void) |
1721 | | { |
1722 | | return (word32)XTIME_MS(0); |
1723 | | } |
1724 | | |
1725 | | #elif defined(USE_WINDOWS_API) |
1726 | | /* The time in milliseconds. |
1727 | | * Used for tickets to represent difference between when first seen and when |
1728 | | * sending. |
1729 | | * |
1730 | | * returns the time in milliseconds as a 32-bit value. |
1731 | | */ |
1732 | | word32 TimeNowInMilliseconds(void) |
1733 | | { |
1734 | | static int init = 0; |
1735 | | static LARGE_INTEGER freq; |
1736 | | LARGE_INTEGER count; |
1737 | | |
1738 | | if (!init) { |
1739 | | QueryPerformanceFrequency(&freq); |
1740 | | init = 1; |
1741 | | } |
1742 | | |
1743 | | QueryPerformanceCounter(&count); |
1744 | | |
1745 | | return (word32)(count.QuadPart / (freq.QuadPart / 1000)); |
1746 | | } |
1747 | | |
1748 | | #elif defined(HAVE_RTP_SYS) |
1749 | | #include "rtptime.h" |
1750 | | |
1751 | | /* The time in milliseconds. |
1752 | | * Used for tickets to represent difference between when first seen and when |
1753 | | * sending. |
1754 | | * |
1755 | | * returns the time in milliseconds as a 32-bit value. |
1756 | | */ |
1757 | | word32 TimeNowInMilliseconds(void) |
1758 | | { |
1759 | | return (word32)rtp_get_system_sec() * 1000; |
1760 | | } |
1761 | | #elif defined(WOLFSSL_DEOS) |
1762 | | word32 TimeNowInMilliseconds(void) |
1763 | | { |
1764 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
1765 | | word32 *systemTickPtr = systemTickPointer(); |
1766 | | |
1767 | | return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000; |
1768 | | } |
1769 | | #elif defined(MICRIUM) |
1770 | | /* The time in milliseconds. |
1771 | | * Used for tickets to represent difference between when first seen and when |
1772 | | * sending. |
1773 | | * |
1774 | | * returns the time in milliseconds as a 32-bit value. |
1775 | | */ |
1776 | | word32 TimeNowInMilliseconds(void) |
1777 | | { |
1778 | | OS_TICK ticks = 0; |
1779 | | OS_ERR err; |
1780 | | |
1781 | | ticks = OSTimeGet(&err); |
1782 | | |
1783 | | return (word32) (ticks / OSCfg_TickRate_Hz) * 1000; |
1784 | | } |
1785 | | #elif defined(MICROCHIP_TCPIP_V5) |
1786 | | /* The time in milliseconds. |
1787 | | * Used for tickets to represent difference between when first seen and when |
1788 | | * sending. |
1789 | | * |
1790 | | * returns the time in milliseconds as a 32-bit value. |
1791 | | */ |
1792 | | word32 TimeNowInMilliseconds(void) |
1793 | | { |
1794 | | return (word32) (TickGet() / (TICKS_PER_SECOND / 1000)); |
1795 | | } |
1796 | | #elif defined(MICROCHIP_TCPIP) |
1797 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
1798 | | #include <system/tmr/sys_tmr.h> |
1799 | | |
1800 | | /* The time in milliseconds. |
1801 | | * Used for tickets to represent difference between when first seen and when |
1802 | | * sending. |
1803 | | * |
1804 | | * returns the time in milliseconds as a 32-bit value. |
1805 | | */ |
1806 | | word32 TimeNowInMilliseconds(void) |
1807 | | { |
1808 | | return (word32)(SYS_TMR_TickCountGet() / |
1809 | | (SYS_TMR_TickCounterFrequencyGet() / 1000)); |
1810 | | } |
1811 | | #else |
1812 | | /* The time in milliseconds. |
1813 | | * Used for tickets to represent difference between when first seen and when |
1814 | | * sending. |
1815 | | * |
1816 | | * returns the time in milliseconds as a 32-bit value. |
1817 | | */ |
1818 | | word32 TimeNowInMilliseconds(void) |
1819 | | { |
1820 | | return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000)); |
1821 | | } |
1822 | | |
1823 | | #endif |
1824 | | |
1825 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1826 | | /* The time in milliseconds. |
1827 | | * Used for tickets to represent difference between when first seen and when |
1828 | | * sending. |
1829 | | * |
1830 | | * returns the time in milliseconds as a 32-bit value. |
1831 | | */ |
1832 | | word32 TimeNowInMilliseconds(void) |
1833 | | { |
1834 | | TIME_STRUCT mqxTime; |
1835 | | |
1836 | | _time_get_elapsed(&mqxTime); |
1837 | | |
1838 | | return (word32) mqxTime.SECONDS * 1000; |
1839 | | } |
1840 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
1841 | | #include "include/task.h" |
1842 | | |
1843 | | /* The time in milliseconds. |
1844 | | * Used for tickets to represent difference between when first seen and when |
1845 | | * sending. |
1846 | | * |
1847 | | * returns the time in milliseconds as a 32-bit value. |
1848 | | */ |
1849 | | word32 TimeNowInMilliseconds(void) |
1850 | | { |
1851 | | return (unsigned int)(((float)xTaskGetTickCount()) / |
1852 | | (configTICK_RATE_HZ / 1000)); |
1853 | | } |
1854 | | #elif defined(FREESCALE_KSDK_BM) |
1855 | | #include "lwip/sys.h" /* lwIP */ |
1856 | | |
1857 | | /* The time in milliseconds. |
1858 | | * Used for tickets to represent difference between when first seen and when |
1859 | | * sending. |
1860 | | * |
1861 | | * returns the time in milliseconds as a 32-bit value. |
1862 | | */ |
1863 | | word32 TimeNowInMilliseconds(void) |
1864 | | { |
1865 | | return sys_now(); |
1866 | | } |
1867 | | |
1868 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
1869 | | |
1870 | | word32 TimeNowInMilliseconds(void) |
1871 | | { |
1872 | | return (word32)osKernelGetTickCount(); |
1873 | | } |
1874 | | |
1875 | | #elif defined(WOLFSSL_TIRTOS) |
1876 | | /* The time in milliseconds. |
1877 | | * Used for tickets to represent difference between when first seen and when |
1878 | | * sending. |
1879 | | * |
1880 | | * returns the time in milliseconds as a 32-bit value. |
1881 | | */ |
1882 | | word32 TimeNowInMilliseconds(void) |
1883 | | { |
1884 | | return (word32) Seconds_get() * 1000; |
1885 | | } |
1886 | | #elif defined(WOLFSSL_UTASKER) |
1887 | | /* The time in milliseconds. |
1888 | | * Used for tickets to represent difference between when first seen and when |
1889 | | * sending. |
1890 | | * |
1891 | | * returns the time in milliseconds as a 32-bit value. |
1892 | | */ |
1893 | | word32 TimeNowInMilliseconds(void) |
1894 | | { |
1895 | | return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
1896 | | } |
1897 | | #elif defined(WOLFSSL_LINUXKM) |
1898 | | word32 TimeNowInMilliseconds(void) |
1899 | | { |
1900 | | s64 t; |
1901 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
1902 | | struct timespec ts; |
1903 | | getnstimeofday(&ts); |
1904 | | t = ts.tv_sec * (s64)1000; |
1905 | | t += ts.tv_nsec / (s64)1000000; |
1906 | | #else |
1907 | | struct timespec64 ts; |
1908 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
1909 | | ts = current_kernel_time64(); |
1910 | | #else |
1911 | | ktime_get_coarse_real_ts64(&ts); |
1912 | | #endif |
1913 | | t = ts.tv_sec * 1000L; |
1914 | | t += ts.tv_nsec / 1000000L; |
1915 | | #endif |
1916 | | return (word32)t; |
1917 | | } |
1918 | | #elif defined(WOLFSSL_QNX_CAAM) |
1919 | | word32 TimeNowInMilliseconds(void) |
1920 | | { |
1921 | | struct timespec now; |
1922 | | clock_gettime(CLOCK_REALTIME, &now); |
1923 | | return (word32)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
1924 | | } |
1925 | | #elif defined(FUSION_RTOS) |
1926 | | /* The time in milliseconds. |
1927 | | * Used for tickets to represent difference between when first seen and when |
1928 | | * sending. |
1929 | | * |
1930 | | * returns the time in milliseconds as a 32-bit value. |
1931 | | */ |
1932 | | word32 TimeNowInMilliseconds(void) |
1933 | | { |
1934 | | struct timeval now; |
1935 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
1936 | | return 0; |
1937 | | |
1938 | | /* Convert to milliseconds number. */ |
1939 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1940 | | } |
1941 | | #elif defined(WOLFSSL_ZEPHYR) |
1942 | | word32 TimeNowInMilliseconds(void) |
1943 | | { |
1944 | | int64_t t; |
1945 | | #if defined(CONFIG_ARCH_POSIX) |
1946 | | k_cpu_idle(); |
1947 | | #endif |
1948 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
1949 | | return (word32)t; |
1950 | | } |
1951 | | |
1952 | | #else |
1953 | | /* The time in milliseconds. |
1954 | | * Used for tickets to represent difference between when first seen and when |
1955 | | * sending. |
1956 | | * |
1957 | | * returns the time in milliseconds as a 32-bit value. |
1958 | | */ |
1959 | | word32 TimeNowInMilliseconds(void) |
1960 | | { |
1961 | | struct timeval now; |
1962 | | |
1963 | | if (gettimeofday(&now, 0) < 0) |
1964 | | return 0; |
1965 | | |
1966 | | /* Convert to milliseconds number. */ |
1967 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1968 | | } |
1969 | | #endif |
1970 | | #else |
1971 | | /* user must supply time in milliseconds function: |
1972 | | * word32 TimeNowInMilliseconds(void); |
1973 | | * The response is milliseconds elapsed |
1974 | | */ |
1975 | | #endif /* !NO_ASN_TIME */ |
1976 | | #else |
1977 | | #ifndef NO_ASN_TIME |
1978 | | #if defined(USER_TICKS) |
1979 | | #if 0 |
1980 | | sword64 TimeNowInMilliseconds(void) |
1981 | | { |
1982 | | /* |
1983 | | write your own clock tick function if don't want gettimeofday() |
1984 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1985 | | */ |
1986 | | } |
1987 | | #endif |
1988 | | |
1989 | | #elif defined(TIME_OVERRIDES) |
1990 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1991 | | sword64 TimeNowInMilliseconds(void) |
1992 | | { |
1993 | | return (sword64) wc_Time(0) * 1000; |
1994 | | } |
1995 | | #else |
1996 | | #ifndef HAVE_TIME_T_TYPE |
1997 | | typedef long time_t; |
1998 | | #endif |
1999 | | extern time_t XTIME(time_t * timer); |
2000 | | |
2001 | | /* The time in milliseconds. |
2002 | | * Used for tickets to represent difference between when first seen and when |
2003 | | * sending. |
2004 | | * |
2005 | | * returns the time in milliseconds as a 32-bit value. |
2006 | | */ |
2007 | | sword64 TimeNowInMilliseconds(void) |
2008 | | { |
2009 | | return (sword64) XTIME(0) * 1000; |
2010 | | } |
2011 | | #endif |
2012 | | |
2013 | | #elif defined(XTIME_MS) |
2014 | | sword64 TimeNowInMilliseconds(void) |
2015 | | { |
2016 | | return (sword64)XTIME_MS(0); |
2017 | | } |
2018 | | |
2019 | | #elif defined(USE_WINDOWS_API) |
2020 | | /* The time in milliseconds. |
2021 | | * Used for tickets to represent difference between when first seen and when |
2022 | | * sending. |
2023 | | * |
2024 | | * returns the time in milliseconds as a 64-bit value. |
2025 | | */ |
2026 | | sword64 TimeNowInMilliseconds(void) |
2027 | | { |
2028 | | static int init = 0; |
2029 | | static LARGE_INTEGER freq; |
2030 | | LARGE_INTEGER count; |
2031 | | |
2032 | | if (!init) { |
2033 | | QueryPerformanceFrequency(&freq); |
2034 | | init = 1; |
2035 | | } |
2036 | | |
2037 | | QueryPerformanceCounter(&count); |
2038 | | |
2039 | | return (sword64)(count.QuadPart / (freq.QuadPart / 1000)); |
2040 | | } |
2041 | | |
2042 | | #elif defined(HAVE_RTP_SYS) |
2043 | | #include "rtptime.h" |
2044 | | |
2045 | | /* The time in milliseconds. |
2046 | | * Used for tickets to represent difference between when first seen and when |
2047 | | * sending. |
2048 | | * |
2049 | | * returns the time in milliseconds as a 64-bit value. |
2050 | | */ |
2051 | | sword64 TimeNowInMilliseconds(void) |
2052 | | { |
2053 | | return (sword64)rtp_get_system_sec() * 1000; |
2054 | | } |
2055 | | #elif defined(WOLFSSL_DEOS) |
2056 | | sword64 TimeNowInMilliseconds(void) |
2057 | | { |
2058 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2059 | | word32 *systemTickPtr = systemTickPointer(); |
2060 | | |
2061 | | return (sword64) (*systemTickPtr/systemTickTimeInHz) * 1000; |
2062 | | } |
2063 | | #elif defined(MICRIUM) |
2064 | | /* The time in milliseconds. |
2065 | | * Used for tickets to represent difference between when first seen and when |
2066 | | * sending. |
2067 | | * |
2068 | | * returns the time in milliseconds as a 64-bit value. |
2069 | | */ |
2070 | | sword64 TimeNowInMilliseconds(void) |
2071 | | { |
2072 | | OS_TICK ticks = 0; |
2073 | | OS_ERR err; |
2074 | | |
2075 | | ticks = OSTimeGet(&err); |
2076 | | |
2077 | | return (sword64) (ticks / OSCfg_TickRate_Hz) * 1000; |
2078 | | } |
2079 | | #elif defined(MICROCHIP_TCPIP_V5) |
2080 | | /* The time in milliseconds. |
2081 | | * Used for tickets to represent difference between when first seen and when |
2082 | | * sending. |
2083 | | * |
2084 | | * returns the time in milliseconds as a 64-bit value. |
2085 | | */ |
2086 | | sword64 TimeNowInMilliseconds(void) |
2087 | | { |
2088 | | return (sword64) (TickGet() / (TICKS_PER_SECOND / 1000)); |
2089 | | } |
2090 | | #elif defined(MICROCHIP_TCPIP) |
2091 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
2092 | | #include <system/tmr/sys_tmr.h> |
2093 | | |
2094 | | /* The time in milliseconds. |
2095 | | * Used for tickets to represent difference between when first seen and when |
2096 | | * sending. |
2097 | | * |
2098 | | * returns the time in milliseconds as a 64-bit value. |
2099 | | */ |
2100 | | sword64 TimeNowInMilliseconds(void) |
2101 | | { |
2102 | | return (sword64)SYS_TMR_TickCountGet() / |
2103 | | (SYS_TMR_TickCounterFrequencyGet() / 1000); |
2104 | | } |
2105 | | #else |
2106 | | /* The time in milliseconds. |
2107 | | * Used for tickets to represent difference between when first seen and when |
2108 | | * sending. |
2109 | | * |
2110 | | * returns the time in milliseconds as a 64-bit value. |
2111 | | */ |
2112 | | sword64 TimeNowInMilliseconds(void) |
2113 | | { |
2114 | | return (sword64)SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000); |
2115 | | } |
2116 | | |
2117 | | #endif |
2118 | | |
2119 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2120 | | /* The time in milliseconds. |
2121 | | * Used for tickets to represent difference between when first seen and when |
2122 | | * sending. |
2123 | | * |
2124 | | * returns the time in milliseconds as a 64-bit value. |
2125 | | */ |
2126 | | sword64 TimeNowInMilliseconds(void) |
2127 | | { |
2128 | | TIME_STRUCT mqxTime; |
2129 | | |
2130 | | _time_get_elapsed(&mqxTime); |
2131 | | |
2132 | | return (sword64) mqxTime.SECONDS * 1000; |
2133 | | } |
2134 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
2135 | | #include "include/task.h" |
2136 | | |
2137 | | /* The time in milliseconds. |
2138 | | * Used for tickets to represent difference between when first seen and when |
2139 | | * sending. |
2140 | | * |
2141 | | * returns the time in milliseconds as a 64-bit value. |
2142 | | */ |
2143 | | sword64 TimeNowInMilliseconds(void) |
2144 | | { |
2145 | | return (sword64)xTaskGetTickCount() / (configTICK_RATE_HZ / 1000); |
2146 | | } |
2147 | | #elif defined(FREESCALE_KSDK_BM) |
2148 | | #include "lwip/sys.h" /* lwIP */ |
2149 | | |
2150 | | /* The time in milliseconds. |
2151 | | * Used for tickets to represent difference between when first seen and when |
2152 | | * sending. |
2153 | | * |
2154 | | * returns the time in milliseconds as a 64-bit value. |
2155 | | */ |
2156 | | sword64 TimeNowInMilliseconds(void) |
2157 | | { |
2158 | | return sys_now(); |
2159 | | } |
2160 | | |
2161 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
2162 | | |
2163 | | sword64 TimeNowInMilliseconds(void) |
2164 | | { |
2165 | | return (sword64)osKernelGetTickCount(); |
2166 | | } |
2167 | | |
2168 | | #elif defined(WOLFSSL_TIRTOS) |
2169 | | /* The time in milliseconds. |
2170 | | * Used for tickets to represent difference between when first seen and when |
2171 | | * sending. |
2172 | | * |
2173 | | * returns the time in milliseconds as a 64-bit value. |
2174 | | */ |
2175 | | sword64 TimeNowInMilliseconds(void) |
2176 | | { |
2177 | | return (sword64) Seconds_get() * 1000; |
2178 | | } |
2179 | | #elif defined(WOLFSSL_UTASKER) |
2180 | | /* The time in milliseconds. |
2181 | | * Used for tickets to represent difference between when first seen and when |
2182 | | * sending. |
2183 | | * |
2184 | | * returns the time in milliseconds as a 64-bit value. |
2185 | | */ |
2186 | | sword64 TimeNowInMilliseconds(void) |
2187 | | { |
2188 | | return (sword64)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
2189 | | } |
2190 | | #elif defined(WOLFSSL_LINUXKM) |
2191 | | sword64 TimeNowInMilliseconds(void) |
2192 | | { |
2193 | | s64 t; |
2194 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2195 | | struct timespec ts; |
2196 | | getnstimeofday(&ts); |
2197 | | t = ts.tv_sec * (s64)1000; |
2198 | | t += ts.tv_nsec / (s64)1000000; |
2199 | | #else |
2200 | | struct timespec64 ts; |
2201 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2202 | | ts = current_kernel_time64(); |
2203 | | #else |
2204 | | ktime_get_coarse_real_ts64(&ts); |
2205 | | #endif |
2206 | | t = ts.tv_sec * 1000L; |
2207 | | t += ts.tv_nsec / 1000000L; |
2208 | | #endif |
2209 | | return (sword64)t; |
2210 | | } |
2211 | | #elif defined(WOLFSSL_QNX_CAAM) |
2212 | | sword64 TimeNowInMilliseconds(void) |
2213 | | { |
2214 | | struct timespec now; |
2215 | | clock_gettime(CLOCK_REALTIME, &now); |
2216 | | return (sword64)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
2217 | | } |
2218 | | #elif defined(FUSION_RTOS) |
2219 | | /* The time in milliseconds. |
2220 | | * Used for tickets to represent difference between when first seen and when |
2221 | | * sending. |
2222 | | * |
2223 | | * returns the time in milliseconds as a 64-bit value. |
2224 | | */ |
2225 | | sword64 TimeNowInMilliseconds(void) |
2226 | | { |
2227 | | struct timeval now; |
2228 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2229 | | return 0; |
2230 | | |
2231 | | /* Convert to milliseconds number. */ |
2232 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2233 | | } |
2234 | | #elif defined(WOLFSSL_ZEPHYR) |
2235 | | sword64 TimeNowInMilliseconds(void) |
2236 | | { |
2237 | | int64_t t; |
2238 | | #if defined(CONFIG_ARCH_POSIX) |
2239 | | k_cpu_idle(); |
2240 | | #endif |
2241 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2242 | | return (sword64)t; |
2243 | | } |
2244 | | |
2245 | | #else |
2246 | | /* The time in milliseconds. |
2247 | | * Used for tickets to represent difference between when first seen and when |
2248 | | * sending. |
2249 | | * |
2250 | | * returns the time in milliseconds as a 64-bit value. |
2251 | | */ |
2252 | | sword64 TimeNowInMilliseconds(void) |
2253 | | { |
2254 | | struct timeval now; |
2255 | | |
2256 | | if (gettimeofday(&now, 0) < 0) |
2257 | | return 0; |
2258 | | |
2259 | | /* Convert to milliseconds number. */ |
2260 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2261 | | } |
2262 | | #endif |
2263 | | #else |
2264 | | /* user must supply time in milliseconds function: |
2265 | | * sword64 TimeNowInMilliseconds(void); |
2266 | | * The response is milliseconds elapsed |
2267 | | */ |
2268 | | #endif /* !NO_ASN_TIME */ |
2269 | | #endif /* WOLFSSL_32BIT_MILLI_TIME */ |
2270 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
2271 | | |
2272 | | /* Add record layer header to message. |
2273 | | * |
2274 | | * output The buffer to write the record layer header into. |
2275 | | * length The length of the record data. |
2276 | | * type The type of record message. |
2277 | | * ssl The SSL/TLS object. |
2278 | | */ |
2279 | | static void AddTls13RecordHeader(byte* output, word32 length, byte type, |
2280 | | WOLFSSL* ssl) |
2281 | 5.37k | { |
2282 | 5.37k | RecordLayerHeader* rl; |
2283 | | |
2284 | 5.37k | rl = (RecordLayerHeader*)output; |
2285 | 5.37k | rl->type = type; |
2286 | 5.37k | rl->pvMajor = ssl->version.major; |
2287 | | /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */ |
2288 | 5.37k | rl->pvMinor = TLSv1_2_MINOR; |
2289 | 5.37k | c16toa((word16)length, rl->length); |
2290 | 5.37k | } |
2291 | | |
2292 | | /* Add handshake header to message. |
2293 | | * |
2294 | | * output The buffer to write the handshake header into. |
2295 | | * length The length of the handshake data. |
2296 | | * fragOffset The offset of the fragment data. (DTLS) |
2297 | | * fragLength The length of the fragment data. (DTLS) |
2298 | | * type The type of handshake message. |
2299 | | * ssl The SSL/TLS object. (DTLS) |
2300 | | */ |
2301 | | static void AddTls13HandShakeHeader(byte* output, word32 length, |
2302 | | word32 fragOffset, word32 fragLength, |
2303 | | byte type, WOLFSSL* ssl) |
2304 | | { |
2305 | | HandShakeHeader* hs; |
2306 | | (void)fragOffset; |
2307 | | (void)fragLength; |
2308 | | (void)ssl; |
2309 | | |
2310 | | #ifdef WOLFSSL_DTLS13 |
2311 | | /* message_hash type is used for a synthetic message that replaces the first |
2312 | | ClientHello in the hash transcript when using HelloRetryRequest. It will |
2313 | | never be transmitted and, as the DTLS-only fields must not be considered |
2314 | | when computing the hash transcript, we can avoid to use the DTLS |
2315 | | handshake header. */ |
2316 | | if (ssl->options.dtls && type != message_hash) { |
2317 | | Dtls13HandshakeAddHeader(ssl, output, (enum HandShakeType)type, length); |
2318 | | return; |
2319 | | } |
2320 | | #endif /* WOLFSSL_DTLS13 */ |
2321 | | |
2322 | | /* handshake header */ |
2323 | | hs = (HandShakeHeader*)output; |
2324 | | hs->type = type; |
2325 | | c32to24(length, hs->length); |
2326 | | } |
2327 | | |
2328 | | |
2329 | | /* Add both record layer and handshake header to message. |
2330 | | * |
2331 | | * output The buffer to write the headers into. |
2332 | | * length The length of the handshake data. |
2333 | | * type The type of record layer message. |
2334 | | * ssl The SSL/TLS object. (DTLS) |
2335 | | */ |
2336 | | static void AddTls13Headers(byte* output, word32 length, byte type, |
2337 | | WOLFSSL* ssl) |
2338 | | { |
2339 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2340 | | word32 outputAdj = RECORD_HEADER_SZ; |
2341 | | |
2342 | | #ifdef WOLFSSL_DTLS13 |
2343 | | if (ssl->options.dtls) { |
2344 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2345 | | return; |
2346 | | } |
2347 | | #endif /* WOLFSSL_DTLS13 */ |
2348 | | |
2349 | | AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl); |
2350 | | AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl); |
2351 | | } |
2352 | | |
2353 | | #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) \ |
2354 | | && !defined(NO_CERTS) |
2355 | | /* Add both record layer and fragment handshake header to message. |
2356 | | * |
2357 | | * output The buffer to write the headers into. |
2358 | | * fragOffset The offset of the fragment data. (DTLS) |
2359 | | * fragLength The length of the fragment data. (DTLS) |
2360 | | * length The length of the handshake data. |
2361 | | * type The type of record layer message. |
2362 | | * ssl The SSL/TLS object. (DTLS) |
2363 | | */ |
2364 | | static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset, |
2365 | | word32 length, byte type, WOLFSSL* ssl) |
2366 | | { |
2367 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2368 | | word32 outputAdj = RECORD_HEADER_SZ; |
2369 | | (void)fragSz; |
2370 | | |
2371 | | #ifdef WOLFSSL_DTLS13 |
2372 | | /* we ignore fragmentation fields here because fragmentation logic for |
2373 | | DTLS1.3 is inside dtls13_handshake_send(). */ |
2374 | | if (ssl->options.dtls) { |
2375 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2376 | | return; |
2377 | | } |
2378 | | #endif /* WOLFSSL_DTLS13 */ |
2379 | | |
2380 | | AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl); |
2381 | | AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz, |
2382 | | type, ssl); |
2383 | | } |
2384 | | #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !NO_CERTS */ |
2385 | | |
2386 | | /* Write the sequence number into the buffer. |
2387 | | * No DTLS v1.3 support. |
2388 | | * |
2389 | | * ssl The SSL/TLS object. |
2390 | | * verifyOrder Which set of sequence numbers to use. |
2391 | | * out The buffer to write into. |
2392 | | */ |
2393 | | static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out) |
2394 | 7.39k | { |
2395 | 7.39k | word32 seq[2] = {0, 0}; |
2396 | | |
2397 | 7.39k | if (ssl->options.dtls) { |
2398 | | #ifdef WOLFSSL_DTLS13 |
2399 | | Dtls13GetSeq(ssl, verifyOrder, seq, 1); |
2400 | | #endif /* WOLFSSL_DTLS13 */ |
2401 | 0 | } |
2402 | 7.39k | else if (verifyOrder == PEER_ORDER) { |
2403 | 5.31k | seq[0] = ssl->keys.peer_sequence_number_hi; |
2404 | 5.31k | seq[1] = ssl->keys.peer_sequence_number_lo++; |
2405 | | /* handle rollover */ |
2406 | 5.31k | if (seq[1] > ssl->keys.peer_sequence_number_lo) |
2407 | 0 | ssl->keys.peer_sequence_number_hi++; |
2408 | 5.31k | } |
2409 | 2.07k | else { |
2410 | 2.07k | seq[0] = ssl->keys.sequence_number_hi; |
2411 | 2.07k | seq[1] = ssl->keys.sequence_number_lo++; |
2412 | | /* handle rollover */ |
2413 | 2.07k | if (seq[1] > ssl->keys.sequence_number_lo) |
2414 | 0 | ssl->keys.sequence_number_hi++; |
2415 | 2.07k | } |
2416 | | #ifdef WOLFSSL_DEBUG_TLS |
2417 | | WOLFSSL_MSG_EX("TLS 1.3 Write Sequence %d %d", seq[0], seq[1]); |
2418 | | #endif |
2419 | | |
2420 | 7.39k | c32toa(seq[0], out); |
2421 | 7.39k | c32toa(seq[1], out + OPAQUE32_LEN); |
2422 | 7.39k | } |
2423 | | |
2424 | | /* Build the nonce for TLS v1.3 encryption and decryption. |
2425 | | * |
2426 | | * ssl The SSL/TLS object. |
2427 | | * nonce The nonce data to use when encrypting or decrypting. |
2428 | | * iv The derived IV. |
2429 | | * order The side on which the message is to be or was sent. |
2430 | | */ |
2431 | | static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv, |
2432 | | int order) |
2433 | 7.39k | { |
2434 | 7.39k | int seq_offset = AEAD_NONCE_SZ - SEQ_SZ; |
2435 | | /* The nonce is the IV with the sequence XORed into the last bytes. */ |
2436 | 7.39k | WriteSEQTls13(ssl, order, nonce + seq_offset); |
2437 | 7.39k | XMEMCPY(nonce, iv, seq_offset); |
2438 | 7.39k | xorbuf(nonce + seq_offset, iv + seq_offset, SEQ_SZ); |
2439 | 7.39k | } |
2440 | | |
2441 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2442 | | /* Encrypt with ChaCha20 and create authentication tag with Poly1305. |
2443 | | * |
2444 | | * ssl The SSL/TLS object. |
2445 | | * output The buffer to write encrypted data and authentication tag into. |
2446 | | * May be the same pointer as input. |
2447 | | * input The data to encrypt. |
2448 | | * sz The number of bytes to encrypt. |
2449 | | * nonce The nonce to use with ChaCha20. |
2450 | | * aad The additional authentication data. |
2451 | | * aadSz The size of the addition authentication data. |
2452 | | * tag The authentication tag buffer. |
2453 | | * returns 0 on success, otherwise failure. |
2454 | | */ |
2455 | | static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output, |
2456 | | const byte* input, word16 sz, byte* nonce, |
2457 | | const byte* aad, word16 aadSz, byte* tag) |
2458 | 647 | { |
2459 | 647 | int ret = 0; |
2460 | 647 | byte poly[CHACHA20_256_KEY_SIZE]; |
2461 | | |
2462 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2463 | 647 | XMEMSET(poly, 0, sizeof(poly)); |
2464 | | |
2465 | | /* Set the nonce for ChaCha and get Poly1305 key. */ |
2466 | 647 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0); |
2467 | 647 | if (ret != 0) |
2468 | 0 | return ret; |
2469 | | /* Create Poly1305 key using ChaCha20 keystream. */ |
2470 | 647 | ret = wc_Chacha_Process(ssl->encrypt.chacha, poly, poly, sizeof(poly)); |
2471 | 647 | if (ret != 0) |
2472 | 0 | return ret; |
2473 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2474 | | wc_MemZero_Add("ChaCha20Poly1305_Encrypt poly", poly, sizeof(poly)); |
2475 | | #endif |
2476 | 647 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1); |
2477 | 647 | if (ret != 0) |
2478 | 0 | return ret; |
2479 | | /* Encrypt the plain text. */ |
2480 | 647 | ret = wc_Chacha_Process(ssl->encrypt.chacha, output, input, sz); |
2481 | 647 | if (ret != 0) { |
2482 | 0 | ForceZero(poly, sizeof(poly)); |
2483 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2484 | | wc_MemZero_Check(poly, sizeof(poly)); |
2485 | | #endif |
2486 | 0 | return ret; |
2487 | 0 | } |
2488 | | |
2489 | | /* Set key for Poly1305. */ |
2490 | 647 | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2491 | 647 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2492 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2493 | | wc_MemZero_Check(poly, sizeof(poly)); |
2494 | | #endif |
2495 | 647 | if (ret != 0) |
2496 | 0 | return ret; |
2497 | | /* Add authentication code of encrypted data to end. */ |
2498 | 647 | ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, output, sz, tag, |
2499 | 647 | POLY1305_AUTH_SZ); |
2500 | | |
2501 | 647 | return ret; |
2502 | 647 | } |
2503 | | #endif |
2504 | | |
2505 | | #ifdef HAVE_NULL_CIPHER |
2506 | | /* Create authentication tag and copy data over input. |
2507 | | * |
2508 | | * ssl The SSL/TLS object. |
2509 | | * output The buffer to copy data into. |
2510 | | * May be the same pointer as input. |
2511 | | * input The data. |
2512 | | * sz The number of bytes of data. |
2513 | | * nonce The nonce to use with authentication. |
2514 | | * aad The additional authentication data. |
2515 | | * aadSz The size of the addition authentication data. |
2516 | | * tag The authentication tag buffer. |
2517 | | * returns 0 on success, otherwise failure. |
2518 | | */ |
2519 | | static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output, |
2520 | | const byte* input, word16 sz, |
2521 | | const byte* nonce, |
2522 | | const byte* aad, word16 aadSz, byte* tag) |
2523 | | { |
2524 | | int ret; |
2525 | | |
2526 | | /* HMAC: nonce | aad | input */ |
2527 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, HMAC_NONCE_SZ); |
2528 | | if (ret == 0) |
2529 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz); |
2530 | | if (ret == 0) |
2531 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz); |
2532 | | if (ret == 0) |
2533 | | ret = wc_HmacFinal(ssl->encrypt.hmac, tag); |
2534 | | /* Copy the input to output if not the same buffer */ |
2535 | | if (ret == 0 && output != input) |
2536 | | XMEMCPY(output, input, sz); |
2537 | | return ret; |
2538 | | } |
2539 | | #endif |
2540 | | |
2541 | | /* Encrypt data for TLS v1.3. |
2542 | | * |
2543 | | * ssl The SSL/TLS object. |
2544 | | * output The buffer to write encrypted data and authentication tag into. |
2545 | | * May be the same pointer as input. |
2546 | | * input The record header and data to encrypt. |
2547 | | * sz The number of bytes to encrypt. |
2548 | | * aad The additional authentication data. |
2549 | | * aadSz The size of the addition authentication data. |
2550 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
2551 | | * returns 0 on success, otherwise failure. |
2552 | | */ |
2553 | | static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, |
2554 | | word16 sz, const byte* aad, word16 aadSz, int asyncOkay) |
2555 | 0 | { |
2556 | 0 | int ret = 0; |
2557 | 0 | word16 dataSz = sz - ssl->specs.aead_mac_size; |
2558 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2559 | 0 | word32 nonceSz = 0; |
2560 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2561 | | WC_ASYNC_DEV* asyncDev = NULL; |
2562 | | word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN; |
2563 | | #endif |
2564 | |
|
2565 | 0 | WOLFSSL_ENTER("EncryptTls13"); |
2566 | |
|
2567 | 0 | (void)output; |
2568 | 0 | (void)input; |
2569 | 0 | (void)sz; |
2570 | 0 | (void)dataSz; |
2571 | 0 | (void)macSz; |
2572 | 0 | (void)asyncOkay; |
2573 | 0 | (void)nonceSz; |
2574 | |
|
2575 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2576 | | if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2577 | | ssl->error = 0; /* clear async */ |
2578 | | } |
2579 | | #endif |
2580 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2581 | | ret = tsip_Tls13AesEncrypt(ssl, output, input, dataSz); |
2582 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2583 | | if (ret > 0) { |
2584 | | ret = 0; /* tsip_Tls13AesEncrypt returns output size */ |
2585 | | } |
2586 | | return ret; |
2587 | | } |
2588 | | ret = 0; |
2589 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
2590 | |
|
2591 | 0 | switch (ssl->encrypt.state) { |
2592 | 0 | case CIPHER_STATE_BEGIN: |
2593 | 0 | { |
2594 | | #ifdef WOLFSSL_DEBUG_TLS |
2595 | | WOLFSSL_MSG("Data to encrypt"); |
2596 | | WOLFSSL_BUFFER(input, dataSz); |
2597 | | WOLFSSL_MSG("Additional Authentication Data"); |
2598 | | WOLFSSL_BUFFER(aad, aadSz); |
2599 | | #endif |
2600 | |
|
2601 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2602 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) { |
2603 | | XMEMCPY(ssl->encrypt.sanityCheck, input, |
2604 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))); |
2605 | | } |
2606 | | #endif |
2607 | |
|
2608 | 0 | #ifdef CIPHER_NONCE |
2609 | 0 | if (ssl->encrypt.nonce == NULL) { |
2610 | 0 | ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ, |
2611 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
2612 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2613 | | if (ssl->encrypt.nonce != NULL) { |
2614 | | wc_MemZero_Add("EncryptTls13 nonce", ssl->encrypt.nonce, |
2615 | | AEAD_NONCE_SZ); |
2616 | | } |
2617 | | #endif |
2618 | 0 | } |
2619 | 0 | if (ssl->encrypt.nonce == NULL) |
2620 | 0 | return MEMORY_E; |
2621 | | |
2622 | 0 | BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV, |
2623 | 0 | CUR_ORDER); |
2624 | 0 | #endif |
2625 | | |
2626 | | /* Advance state and proceed */ |
2627 | 0 | ssl->encrypt.state = CIPHER_STATE_DO; |
2628 | 0 | } |
2629 | 0 | FALL_THROUGH; |
2630 | |
|
2631 | 0 | case CIPHER_STATE_DO: |
2632 | 0 | { |
2633 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
2634 | 0 | #ifdef BUILD_AESGCM |
2635 | 0 | case wolfssl_aes_gcm: |
2636 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2637 | | /* initialize event */ |
2638 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2639 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2640 | | if (ret != 0) |
2641 | | break; |
2642 | | #endif |
2643 | |
|
2644 | 0 | nonceSz = AESGCM_NONCE_SZ; |
2645 | |
|
2646 | | #if defined(HAVE_PK_CALLBACKS) |
2647 | | ret = NOT_COMPILED_IN; |
2648 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2649 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2650 | | output, input, dataSz, |
2651 | | ssl->encrypt.nonce, nonceSz, |
2652 | | output + dataSz, macSz, |
2653 | | aad, aadSz); |
2654 | | } |
2655 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2656 | | #endif |
2657 | 0 | { |
2658 | |
|
2659 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2660 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2661 | | ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, |
2662 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2663 | | output + dataSz, macSz, aad, aadSz); |
2664 | | #else |
2665 | 0 | ret = wc_AesGcmSetExtIV(ssl->encrypt.aes, |
2666 | 0 | ssl->encrypt.nonce, nonceSz); |
2667 | 0 | if (ret == 0) { |
2668 | 0 | ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output, |
2669 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2670 | 0 | output + dataSz, macSz, aad, aadSz); |
2671 | 0 | } |
2672 | 0 | #endif |
2673 | 0 | } |
2674 | 0 | break; |
2675 | 0 | #endif |
2676 | | |
2677 | 0 | #ifdef HAVE_AESCCM |
2678 | 0 | case wolfssl_aes_ccm: |
2679 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2680 | | /* initialize event */ |
2681 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2682 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2683 | | if (ret != 0) |
2684 | | break; |
2685 | | #endif |
2686 | |
|
2687 | 0 | nonceSz = AESCCM_NONCE_SZ; |
2688 | | #if defined(HAVE_PK_CALLBACKS) |
2689 | | ret = NOT_COMPILED_IN; |
2690 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2691 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2692 | | output, input, dataSz, |
2693 | | ssl->encrypt.nonce, nonceSz, |
2694 | | output + dataSz, macSz, |
2695 | | aad, aadSz); |
2696 | | } |
2697 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2698 | | #endif |
2699 | 0 | { |
2700 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2701 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2702 | | ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, |
2703 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2704 | | output + dataSz, macSz, aad, aadSz); |
2705 | | #else |
2706 | 0 | ret = wc_AesCcmSetNonce(ssl->encrypt.aes, |
2707 | 0 | ssl->encrypt.nonce, nonceSz); |
2708 | 0 | if (ret == 0) { |
2709 | 0 | ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output, |
2710 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2711 | 0 | output + dataSz, macSz, aad, aadSz); |
2712 | 0 | } |
2713 | 0 | #endif |
2714 | 0 | } |
2715 | 0 | break; |
2716 | 0 | #endif |
2717 | | |
2718 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2719 | 0 | case wolfssl_chacha: |
2720 | 0 | ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz, |
2721 | 0 | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2722 | 0 | break; |
2723 | 0 | #endif |
2724 | | |
2725 | 0 | #ifdef WOLFSSL_SM4_GCM |
2726 | 0 | case wolfssl_sm4_gcm: |
2727 | 0 | nonceSz = SM4_GCM_NONCE_SZ; |
2728 | 0 | ret = wc_Sm4GcmEncrypt(ssl->encrypt.sm4, output, input, |
2729 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2730 | 0 | macSz, aad, aadSz); |
2731 | 0 | break; |
2732 | 0 | #endif |
2733 | | |
2734 | 0 | #ifdef WOLFSSL_SM4_CCM |
2735 | 0 | case wolfssl_sm4_ccm: |
2736 | 0 | nonceSz = SM4_CCM_NONCE_SZ; |
2737 | 0 | ret = wc_Sm4CcmEncrypt(ssl->encrypt.sm4, output, input, |
2738 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2739 | 0 | macSz, aad, aadSz); |
2740 | 0 | break; |
2741 | 0 | #endif |
2742 | | |
2743 | | #ifdef HAVE_NULL_CIPHER |
2744 | | case wolfssl_cipher_null: |
2745 | | ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz, |
2746 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2747 | | break; |
2748 | | #endif |
2749 | | |
2750 | 0 | default: |
2751 | 0 | WOLFSSL_MSG("wolfSSL Encrypt programming error"); |
2752 | 0 | return ENCRYPT_ERROR; |
2753 | 0 | } |
2754 | | |
2755 | | /* Advance state */ |
2756 | 0 | ssl->encrypt.state = CIPHER_STATE_END; |
2757 | |
|
2758 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2759 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2760 | | /* if async is not okay, then block */ |
2761 | | if (!asyncOkay) { |
2762 | | ret = wc_AsyncWait(ret, asyncDev, event_flags); |
2763 | | } |
2764 | | else { |
2765 | | /* If pending, then leave and return will resume below */ |
2766 | | return wolfSSL_AsyncPush(ssl, asyncDev); |
2767 | | } |
2768 | | } |
2769 | | #endif |
2770 | 0 | } |
2771 | 0 | FALL_THROUGH; |
2772 | |
|
2773 | 0 | case CIPHER_STATE_END: |
2774 | 0 | { |
2775 | | #ifdef WOLFSSL_DEBUG_TLS |
2776 | | #ifdef CIPHER_NONCE |
2777 | | WOLFSSL_MSG("Nonce"); |
2778 | | WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size); |
2779 | | #endif |
2780 | | WOLFSSL_MSG("Encrypted data"); |
2781 | | WOLFSSL_BUFFER(output, dataSz); |
2782 | | WOLFSSL_MSG("Authentication Tag"); |
2783 | | WOLFSSL_BUFFER(output + dataSz, macSz); |
2784 | | #endif |
2785 | |
|
2786 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2787 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2788 | | XMEMCMP(output, ssl->encrypt.sanityCheck, |
2789 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))) == 0) { |
2790 | | |
2791 | | WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?"); |
2792 | | return ENCRYPT_ERROR; |
2793 | | } |
2794 | | ForceZero(ssl->encrypt.sanityCheck, |
2795 | | sizeof(ssl->encrypt.sanityCheck)); |
2796 | | #endif |
2797 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2798 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
2799 | | (output != input) && (ret == 0)) { |
2800 | | wc_MemZero_Add("TLS 1.3 Encrypt plaintext", input, sz); |
2801 | | } |
2802 | | #endif |
2803 | |
|
2804 | 0 | #ifdef CIPHER_NONCE |
2805 | 0 | ForceZero(ssl->encrypt.nonce, AEAD_NONCE_SZ); |
2806 | 0 | #endif |
2807 | |
|
2808 | 0 | break; |
2809 | 0 | } |
2810 | | |
2811 | 0 | default: |
2812 | 0 | break; |
2813 | 0 | } |
2814 | | |
2815 | | |
2816 | | /* Reset state */ |
2817 | 0 | ssl->encrypt.state = CIPHER_STATE_BEGIN; |
2818 | |
|
2819 | 0 | return ret; |
2820 | 0 | } |
2821 | | |
2822 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2823 | | /* Decrypt with ChaCha20 and check authentication tag with Poly1305. |
2824 | | * |
2825 | | * ssl The SSL/TLS object. |
2826 | | * output The buffer to write decrypted data into. |
2827 | | * May be the same pointer as input. |
2828 | | * input The data to decrypt. |
2829 | | * sz The number of bytes to decrypt. |
2830 | | * nonce The nonce to use with ChaCha20. |
2831 | | * aad The additional authentication data. |
2832 | | * aadSz The size of the addition authentication data. |
2833 | | * tagIn The authentication tag data from packet. |
2834 | | * returns 0 on success, otherwise failure. |
2835 | | */ |
2836 | | static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output, |
2837 | | const byte* input, word16 sz, byte* nonce, |
2838 | | const byte* aad, word16 aadSz, |
2839 | | const byte* tagIn) |
2840 | 2.67k | { |
2841 | 2.67k | int ret; |
2842 | 2.67k | byte tag[POLY1305_AUTH_SZ]; |
2843 | 2.67k | byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */ |
2844 | | |
2845 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2846 | 2.67k | XMEMSET(poly, 0, sizeof(poly)); |
2847 | | |
2848 | | /* Set nonce and get Poly1305 key. */ |
2849 | 2.67k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0); |
2850 | 2.67k | if (ret != 0) |
2851 | 0 | return ret; |
2852 | | /* Use ChaCha20 keystream to get Poly1305 key for tag. */ |
2853 | 2.67k | ret = wc_Chacha_Process(ssl->decrypt.chacha, poly, poly, sizeof(poly)); |
2854 | 2.67k | if (ret != 0) |
2855 | 0 | return ret; |
2856 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2857 | | wc_MemZero_Add("ChaCha20Poly1305_Decrypt poly", poly, sizeof(poly)); |
2858 | | #endif |
2859 | 2.67k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1); |
2860 | 2.67k | if (ret != 0) { |
2861 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2862 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2863 | | wc_MemZero_Check(poly, sizeof(poly)); |
2864 | | #endif |
2865 | 0 | return ret; |
2866 | 0 | } |
2867 | | |
2868 | | /* Set key for Poly1305. */ |
2869 | 2.67k | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2870 | 2.67k | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2871 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2872 | | wc_MemZero_Check(poly, sizeof(poly)); |
2873 | | #endif |
2874 | 2.67k | if (ret != 0) |
2875 | 0 | return ret; |
2876 | | /* Generate authentication tag for encrypted data. */ |
2877 | 2.67k | if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, input, sz, tag, |
2878 | 2.67k | sizeof(tag))) != 0) { |
2879 | 0 | return ret; |
2880 | 0 | } |
2881 | | |
2882 | | /* Check tag sent along with packet. */ |
2883 | 2.67k | if (ConstantCompare(tagIn, tag, POLY1305_AUTH_SZ) != 0) { |
2884 | 2.67k | WOLFSSL_MSG("MAC did not match"); |
2885 | 2.67k | return VERIFY_MAC_ERROR; |
2886 | 2.67k | } |
2887 | | |
2888 | | /* If the tag was good decrypt message. */ |
2889 | 0 | ret = wc_Chacha_Process(ssl->decrypt.chacha, output, input, sz); |
2890 | |
|
2891 | 0 | return ret; |
2892 | 2.67k | } |
2893 | | #endif |
2894 | | |
2895 | | #ifdef HAVE_NULL_CIPHER |
2896 | | /* Check HMAC tag and copy over input. |
2897 | | * |
2898 | | * ssl The SSL/TLS object. |
2899 | | * output The buffer to copy data into. |
2900 | | * May be the same pointer as input. |
2901 | | * input The data. |
2902 | | * sz The number of bytes of data. |
2903 | | * nonce The nonce to use with authentication. |
2904 | | * aad The additional authentication data. |
2905 | | * aadSz The size of the addition authentication data. |
2906 | | * tagIn The authentication tag data from packet. |
2907 | | * returns 0 on success, otherwise failure. |
2908 | | */ |
2909 | | static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output, |
2910 | | const byte* input, word16 sz, |
2911 | | const byte* nonce, |
2912 | | const byte* aad, word16 aadSz, |
2913 | | const byte* tagIn) |
2914 | | { |
2915 | | int ret; |
2916 | | byte hmac[WC_MAX_DIGEST_SIZE]; |
2917 | | |
2918 | | /* HMAC: nonce | aad | input */ |
2919 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, HMAC_NONCE_SZ); |
2920 | | if (ret == 0) |
2921 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz); |
2922 | | if (ret == 0) |
2923 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz); |
2924 | | if (ret == 0) |
2925 | | ret = wc_HmacFinal(ssl->decrypt.hmac, hmac); |
2926 | | /* Check authentication tag matches */ |
2927 | | if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0) |
2928 | | ret = DECRYPT_ERROR; |
2929 | | /* Copy the input to output if not the same buffer */ |
2930 | | if (ret == 0 && output != input) |
2931 | | XMEMCPY(output, input, sz); |
2932 | | return ret; |
2933 | | } |
2934 | | #endif |
2935 | | |
2936 | | /* Decrypt data for TLS v1.3. |
2937 | | * |
2938 | | * ssl The SSL/TLS object. |
2939 | | * output The buffer to write decrypted data into. |
2940 | | * May be the same pointer as input. |
2941 | | * input The data to decrypt and authentication tag. |
2942 | | * sz The length of the encrypted data plus authentication tag. |
2943 | | * aad The additional authentication data. |
2944 | | * aadSz The size of the addition authentication data. |
2945 | | * returns 0 on success, otherwise failure. |
2946 | | */ |
2947 | | int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz, |
2948 | | const byte* aad, word16 aadSz) |
2949 | | { |
2950 | | int ret = 0; |
2951 | | word16 dataSz = sz - ssl->specs.aead_mac_size; |
2952 | | word16 macSz = ssl->specs.aead_mac_size; |
2953 | | word32 nonceSz = 0; |
2954 | | |
2955 | | WOLFSSL_ENTER("DecryptTls13"); |
2956 | | |
2957 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2958 | | ret = tsip_Tls13AesDecrypt(ssl, output, input, sz); |
2959 | | |
2960 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2961 | | #ifndef WOLFSSL_EARLY_DATA |
2962 | | if (ret < 0) { |
2963 | | ret = VERIFY_MAC_ERROR; |
2964 | | WOLFSSL_ERROR_VERBOSE(ret); |
2965 | | } |
2966 | | #endif |
2967 | | return ret; |
2968 | | } |
2969 | | #endif |
2970 | | |
2971 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2972 | | ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state); |
2973 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
2974 | | /* check for still pending */ |
2975 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
2976 | | return ret; |
2977 | | |
2978 | | ssl->error = 0; /* clear async */ |
2979 | | |
2980 | | /* let failures through so CIPHER_STATE_END logic is run */ |
2981 | | } |
2982 | | else |
2983 | | #endif |
2984 | | { |
2985 | | /* Reset state */ |
2986 | | ret = 0; |
2987 | | ssl->decrypt.state = CIPHER_STATE_BEGIN; |
2988 | | } |
2989 | | |
2990 | | (void)output; |
2991 | | (void)input; |
2992 | | (void)sz; |
2993 | | (void)dataSz; |
2994 | | (void)macSz; |
2995 | | (void)nonceSz; |
2996 | | |
2997 | | switch (ssl->decrypt.state) { |
2998 | | case CIPHER_STATE_BEGIN: |
2999 | | { |
3000 | | #ifdef WOLFSSL_DEBUG_TLS |
3001 | | WOLFSSL_MSG("Data to decrypt"); |
3002 | | WOLFSSL_BUFFER(input, dataSz); |
3003 | | WOLFSSL_MSG("Additional Authentication Data"); |
3004 | | WOLFSSL_BUFFER(aad, aadSz); |
3005 | | WOLFSSL_MSG("Authentication tag"); |
3006 | | WOLFSSL_BUFFER(input + dataSz, macSz); |
3007 | | #endif |
3008 | | |
3009 | | #ifdef CIPHER_NONCE |
3010 | | if (ssl->decrypt.nonce == NULL) { |
3011 | | ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ, |
3012 | | ssl->heap, DYNAMIC_TYPE_CIPHER); |
3013 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3014 | | if (ssl->decrypt.nonce != NULL) { |
3015 | | wc_MemZero_Add("DecryptTls13 nonce", ssl->decrypt.nonce, |
3016 | | AEAD_NONCE_SZ); |
3017 | | } |
3018 | | #endif |
3019 | | } |
3020 | | if (ssl->decrypt.nonce == NULL) |
3021 | | return MEMORY_E; |
3022 | | |
3023 | | BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV, |
3024 | | PEER_ORDER); |
3025 | | #endif |
3026 | | |
3027 | | /* Advance state and proceed */ |
3028 | | ssl->decrypt.state = CIPHER_STATE_DO; |
3029 | | } |
3030 | | FALL_THROUGH; |
3031 | | |
3032 | | case CIPHER_STATE_DO: |
3033 | | { |
3034 | | switch (ssl->specs.bulk_cipher_algorithm) { |
3035 | | #ifdef BUILD_AESGCM |
3036 | | case wolfssl_aes_gcm: |
3037 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3038 | | /* initialize event */ |
3039 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3040 | | WC_ASYNC_FLAG_NONE); |
3041 | | if (ret != 0) |
3042 | | break; |
3043 | | #endif |
3044 | | |
3045 | | nonceSz = AESGCM_NONCE_SZ; |
3046 | | |
3047 | | #if defined(HAVE_PK_CALLBACKS) |
3048 | | ret = NOT_COMPILED_IN; |
3049 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3050 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3051 | | output, input, dataSz, |
3052 | | ssl->decrypt.nonce, nonceSz, |
3053 | | (byte *)(input + dataSz), macSz, |
3054 | | aad, aadSz); |
3055 | | } |
3056 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3057 | | #endif |
3058 | | { |
3059 | | |
3060 | | ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, |
3061 | | dataSz, ssl->decrypt.nonce, nonceSz, |
3062 | | input + dataSz, macSz, aad, aadSz); |
3063 | | |
3064 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3065 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3066 | | ret = wolfSSL_AsyncPush(ssl, |
3067 | | &ssl->decrypt.aes->asyncDev); |
3068 | | } |
3069 | | #endif |
3070 | | |
3071 | | } |
3072 | | break; |
3073 | | #endif |
3074 | | |
3075 | | #ifdef HAVE_AESCCM |
3076 | | case wolfssl_aes_ccm: |
3077 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3078 | | /* initialize event */ |
3079 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3080 | | WC_ASYNC_FLAG_NONE); |
3081 | | if (ret != 0) |
3082 | | break; |
3083 | | #endif |
3084 | | |
3085 | | nonceSz = AESCCM_NONCE_SZ; |
3086 | | #if defined(HAVE_PK_CALLBACKS) |
3087 | | ret = NOT_COMPILED_IN; |
3088 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3089 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3090 | | output, input, dataSz, |
3091 | | ssl->decrypt.nonce, nonceSz, |
3092 | | (byte *)(input + dataSz), macSz, |
3093 | | aad, aadSz); |
3094 | | } |
3095 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3096 | | #endif |
3097 | | { |
3098 | | ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, |
3099 | | dataSz, ssl->decrypt.nonce, nonceSz, |
3100 | | input + dataSz, macSz, aad, aadSz); |
3101 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3102 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3103 | | ret = wolfSSL_AsyncPush(ssl, |
3104 | | &ssl->decrypt.aes->asyncDev); |
3105 | | } |
3106 | | #endif |
3107 | | } |
3108 | | break; |
3109 | | #endif |
3110 | | |
3111 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
3112 | | case wolfssl_chacha: |
3113 | | ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz, |
3114 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3115 | | break; |
3116 | | #endif |
3117 | | |
3118 | | #ifdef WOLFSSL_SM4_GCM |
3119 | | case wolfssl_sm4_gcm: |
3120 | | nonceSz = SM4_GCM_NONCE_SZ; |
3121 | | ret = wc_Sm4GcmDecrypt(ssl->decrypt.sm4, output, input, |
3122 | | dataSz, ssl->decrypt.nonce, nonceSz, output + dataSz, |
3123 | | macSz, aad, aadSz); |
3124 | | break; |
3125 | | #endif |
3126 | | |
3127 | | #ifdef WOLFSSL_SM4_CCM |
3128 | | case wolfssl_sm4_ccm: |
3129 | | nonceSz = SM4_CCM_NONCE_SZ; |
3130 | | ret = wc_Sm4CcmDecrypt(ssl->decrypt.sm4, output, input, |
3131 | | dataSz, ssl->decrypt.nonce, nonceSz, output + dataSz, |
3132 | | macSz, aad, aadSz); |
3133 | | break; |
3134 | | #endif |
3135 | | |
3136 | | #ifdef HAVE_NULL_CIPHER |
3137 | | case wolfssl_cipher_null: |
3138 | | ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz, |
3139 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3140 | | break; |
3141 | | #endif |
3142 | | default: |
3143 | | WOLFSSL_MSG("wolfSSL Decrypt programming error"); |
3144 | | return DECRYPT_ERROR; |
3145 | | } |
3146 | | |
3147 | | /* Advance state */ |
3148 | | ssl->decrypt.state = CIPHER_STATE_END; |
3149 | | |
3150 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3151 | | /* If pending, leave now */ |
3152 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3153 | | return ret; |
3154 | | } |
3155 | | #endif |
3156 | | } |
3157 | | FALL_THROUGH; |
3158 | | |
3159 | | case CIPHER_STATE_END: |
3160 | | { |
3161 | | #ifdef WOLFSSL_DEBUG_TLS |
3162 | | #ifdef CIPHER_NONCE |
3163 | | WOLFSSL_MSG("Nonce"); |
3164 | | WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size); |
3165 | | #endif |
3166 | | WOLFSSL_MSG("Decrypted data"); |
3167 | | WOLFSSL_BUFFER(output, dataSz); |
3168 | | #endif |
3169 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3170 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
3171 | | (ret == 0)) { |
3172 | | wc_MemZero_Add("TLS 1.3 Decrypted data", output, sz); |
3173 | | } |
3174 | | #endif |
3175 | | |
3176 | | #ifdef CIPHER_NONCE |
3177 | | ForceZero(ssl->decrypt.nonce, AEAD_NONCE_SZ); |
3178 | | #endif |
3179 | | |
3180 | | break; |
3181 | | } |
3182 | | |
3183 | | default: |
3184 | | break; |
3185 | | } |
3186 | | |
3187 | | if (ret < 0) { |
3188 | | WOLFSSL_ERROR_VERBOSE(ret); |
3189 | | } |
3190 | | |
3191 | | return ret; |
3192 | | } |
3193 | | |
3194 | | /* Persistable BuildTls13Message arguments */ |
3195 | | typedef struct BuildMsg13Args { |
3196 | | word32 sz; |
3197 | | word32 idx; |
3198 | | word32 headerSz; |
3199 | | word16 size; |
3200 | | word32 paddingSz; |
3201 | | } BuildMsg13Args; |
3202 | | |
3203 | | static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs) |
3204 | 2.09k | { |
3205 | 2.09k | BuildMsg13Args* args = (BuildMsg13Args*)pArgs; |
3206 | | |
3207 | 2.09k | (void)ssl; |
3208 | 2.09k | (void)args; |
3209 | | |
3210 | | /* no allocations in BuildTls13Message */ |
3211 | 2.09k | } |
3212 | | |
3213 | | /* Build SSL Message, encrypted. |
3214 | | * TLS v1.3 encryption is AEAD only. |
3215 | | * |
3216 | | * ssl The SSL/TLS object. |
3217 | | * output The buffer to write record message to. |
3218 | | * outSz Size of the buffer being written into. |
3219 | | * input The record data to encrypt (excluding record header). |
3220 | | * inSz The size of the record data. |
3221 | | * type The recorder header content type. |
3222 | | * hashOutput Whether to hash the unencrypted record data. |
3223 | | * sizeOnly Only want the size of the record message. |
3224 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
3225 | | * returns the size of the encrypted record message or negative value on error. |
3226 | | */ |
3227 | | int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input, |
3228 | | int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay) |
3229 | | { |
3230 | | int ret; |
3231 | | BuildMsg13Args* args; |
3232 | | BuildMsg13Args lcl_args; |
3233 | | |
3234 | | WOLFSSL_ENTER("BuildTls13Message"); |
3235 | | |
3236 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3237 | | ret = WC_NO_PENDING_E; |
3238 | | if (asyncOkay) { |
3239 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
3240 | | |
3241 | | if (ssl->async == NULL) { |
3242 | | ssl->async = (struct WOLFSSL_ASYNC*) |
3243 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
3244 | | DYNAMIC_TYPE_ASYNC); |
3245 | | if (ssl->async == NULL) |
3246 | | return MEMORY_E; |
3247 | | } |
3248 | | args = (BuildMsg13Args*)ssl->async->args; |
3249 | | |
3250 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState); |
3251 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3252 | | /* Check for error */ |
3253 | | if (ret < 0) |
3254 | | goto exit_buildmsg; |
3255 | | } |
3256 | | } |
3257 | | else |
3258 | | #endif |
3259 | | { |
3260 | | args = &lcl_args; |
3261 | | } |
3262 | | |
3263 | | /* Reset state */ |
3264 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3265 | | if (ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) |
3266 | | #endif |
3267 | | { |
3268 | | ret = 0; |
3269 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3270 | | XMEMSET(args, 0, sizeof(BuildMsg13Args)); |
3271 | | |
3272 | | args->headerSz = RECORD_HEADER_SZ; |
3273 | | #ifdef WOLFSSL_DTLS13 |
3274 | | if (ssl->options.dtls) |
3275 | | args->headerSz = Dtls13GetRlHeaderLength(ssl, 1); |
3276 | | #endif /* WOLFSSL_DTLS13 */ |
3277 | | |
3278 | | args->sz = args->headerSz + (word32)inSz; |
3279 | | args->idx = args->headerSz; |
3280 | | |
3281 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3282 | | if (asyncOkay) |
3283 | | ssl->async->freeArgs = FreeBuildMsg13Args; |
3284 | | #endif |
3285 | | } |
3286 | | |
3287 | | switch (ssl->options.buildMsgState) { |
3288 | | case BUILD_MSG_BEGIN: |
3289 | | { |
3290 | | /* catch mistaken sizeOnly parameter */ |
3291 | | if (sizeOnly) { |
3292 | | if (output || input) { |
3293 | | WOLFSSL_MSG("BuildTls13Message with sizeOnly " |
3294 | | "doesn't need input or output"); |
3295 | | return BAD_FUNC_ARG; |
3296 | | } |
3297 | | } |
3298 | | else if (output == NULL || input == NULL) { |
3299 | | return BAD_FUNC_ARG; |
3300 | | } |
3301 | | |
3302 | | /* Record layer content type at the end of record data. */ |
3303 | | args->sz++; |
3304 | | /* Authentication data at the end. */ |
3305 | | args->sz += ssl->specs.aead_mac_size; |
3306 | | #ifdef WOLFSSL_DTLS13 |
3307 | | /* Pad to minimum length */ |
3308 | | if (ssl->options.dtls && |
3309 | | args->sz < (word32)Dtls13MinimumRecordLength(ssl)) { |
3310 | | args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz; |
3311 | | args->sz = Dtls13MinimumRecordLength(ssl); |
3312 | | } |
3313 | | #endif |
3314 | | if (sizeOnly) |
3315 | | return (int)args->sz; |
3316 | | |
3317 | | if (args->sz > (word32)outSz) { |
3318 | | WOLFSSL_MSG("Oops, want to write past output buffer size"); |
3319 | | return BUFFER_E; |
3320 | | } |
3321 | | |
3322 | | /* Record data length. */ |
3323 | | args->size = (word16)(args->sz - args->headerSz); |
3324 | | /* Write/update the record header with the new size. |
3325 | | * Always have the content type as application data for encrypted |
3326 | | * messages in TLS v1.3. |
3327 | | */ |
3328 | | |
3329 | | if (ssl->options.dtls) { |
3330 | | #ifdef WOLFSSL_DTLS13 |
3331 | | Dtls13RlAddCiphertextHeader(ssl, output, args->size); |
3332 | | #endif /* WOLFSSL_DTLS13 */ |
3333 | | } |
3334 | | else { |
3335 | | AddTls13RecordHeader(output, args->size, application_data, ssl); |
3336 | | } |
3337 | | |
3338 | | /* TLS v1.3 can do in place encryption. */ |
3339 | | if (input != output + args->idx) |
3340 | | XMEMCPY(output + args->idx, input, (size_t)inSz); |
3341 | | args->idx += (word32)inSz; |
3342 | | |
3343 | | ssl->options.buildMsgState = BUILD_MSG_HASH; |
3344 | | } |
3345 | | FALL_THROUGH; |
3346 | | |
3347 | | case BUILD_MSG_HASH: |
3348 | | { |
3349 | | if (hashOutput) { |
3350 | | ret = HashOutput(ssl, output, (int)args->headerSz + inSz, 0); |
3351 | | if (ret != 0) |
3352 | | goto exit_buildmsg; |
3353 | | } |
3354 | | |
3355 | | /* The real record content type goes at the end of the data. */ |
3356 | | output[args->idx++] = (byte)type; |
3357 | | /* Double check that any necessary padding is zero'd out */ |
3358 | | XMEMSET(output + args->idx, 0, args->paddingSz); |
3359 | | args->idx += args->paddingSz; |
3360 | | |
3361 | | ssl->options.buildMsgState = BUILD_MSG_ENCRYPT; |
3362 | | } |
3363 | | FALL_THROUGH; |
3364 | | |
3365 | | case BUILD_MSG_ENCRYPT: |
3366 | | { |
3367 | | #ifdef WOLFSSL_QUIC |
3368 | | if (WOLFSSL_IS_QUIC(ssl)) { |
3369 | | /* QUIC does not use encryption of the TLS Record Layer. |
3370 | | * Return the original length + added headers |
3371 | | * and restore it in the record header. */ |
3372 | | AddTls13RecordHeader(output, (word32)inSz, (byte)type, ssl); |
3373 | | ret = (int)args->headerSz + inSz; |
3374 | | goto exit_buildmsg; |
3375 | | } |
3376 | | #endif |
3377 | | #ifdef ATOMIC_USER |
3378 | | if (ssl->ctx->MacEncryptCb) { |
3379 | | /* User Record Layer Callback handling */ |
3380 | | byte* mac = output + args->idx; |
3381 | | output += args->headerSz; |
3382 | | |
3383 | | ret = ssl->ctx->MacEncryptCb(ssl, mac, output, (unsigned int)inSz, (byte)type, 0, |
3384 | | output, output, args->size, ssl->MacEncryptCtx); |
3385 | | } |
3386 | | else |
3387 | | #endif |
3388 | | { |
3389 | | const byte* aad = output; |
3390 | | output += args->headerSz; |
3391 | | ret = EncryptTls13(ssl, output, output, args->size, aad, |
3392 | | (word16)args->headerSz, asyncOkay); |
3393 | | if (ret != 0) { |
3394 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3395 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
3396 | | #endif |
3397 | | { |
3398 | | /* Zeroize plaintext. */ |
3399 | | ForceZero(output, args->size); |
3400 | | } |
3401 | | } |
3402 | | #ifdef WOLFSSL_DTLS13 |
3403 | | if (ret == 0 && ssl->options.dtls) { |
3404 | | /* AAD points to the header. Reuse the variable */ |
3405 | | ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, |
3406 | | (word16)args->sz); |
3407 | | } |
3408 | | #endif /* WOLFSSL_DTLS13 */ |
3409 | | } |
3410 | | break; |
3411 | | } |
3412 | | |
3413 | | default: |
3414 | | break; |
3415 | | } |
3416 | | |
3417 | | exit_buildmsg: |
3418 | | |
3419 | | WOLFSSL_LEAVE("BuildTls13Message", ret); |
3420 | | |
3421 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3422 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3423 | | return ret; |
3424 | | } |
3425 | | #endif |
3426 | | |
3427 | | /* make sure build message state is reset */ |
3428 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3429 | | |
3430 | | /* return sz on success */ |
3431 | | if (ret == 0) { |
3432 | | ret = (int)args->sz; |
3433 | | } |
3434 | | else { |
3435 | | WOLFSSL_ERROR_VERBOSE(ret); |
3436 | | } |
3437 | | |
3438 | | /* Final cleanup */ |
3439 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3440 | | if (asyncOkay) |
3441 | | FreeAsyncCtx(ssl, 0); |
3442 | | else |
3443 | | #endif |
3444 | | FreeBuildMsg13Args(ssl, args); |
3445 | | |
3446 | | return ret; |
3447 | | } |
3448 | | |
3449 | | #if !defined(NO_WOLFSSL_CLIENT) || (!defined(NO_WOLFSSL_SERVER) && \ |
3450 | | (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
3451 | | (defined(WOLFSSL_PSK_ONE_ID) || defined(WOLFSSL_PRIORITIZE_PSK))) |
3452 | | /* Find the cipher suite in the suites set in the SSL. |
3453 | | * |
3454 | | * ssl SSL/TLS object. |
3455 | | * suite Cipher suite to look for. |
3456 | | * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise. |
3457 | | */ |
3458 | | int FindSuiteSSL(const WOLFSSL* ssl, byte* suite) |
3459 | 225 | { |
3460 | 225 | word16 i; |
3461 | 225 | const Suites* suites = WOLFSSL_SUITES(ssl); |
3462 | | |
3463 | 958 | for (i = 0; i < suites->suiteSz; i += 2) { |
3464 | 954 | if (suites->suites[i+0] == suite[0] && |
3465 | 954 | suites->suites[i+1] == suite[1]) { |
3466 | 221 | return 1; |
3467 | 221 | } |
3468 | 954 | } |
3469 | | |
3470 | 4 | return 0; |
3471 | 225 | } |
3472 | | #endif |
3473 | | |
3474 | | #ifndef NO_PSK |
3475 | | /* Get the MAC algorithm for the TLS 1.3 cipher suite. |
3476 | | * |
3477 | | * @param [in] suite. |
3478 | | * @return A value from wc_MACAlgorithm enumeration. |
3479 | | */ |
3480 | | byte SuiteMac(const byte* suite) |
3481 | | { |
3482 | | byte mac = no_mac; |
3483 | | |
3484 | | if (suite[0] == TLS13_BYTE) { |
3485 | | switch (suite[1]) { |
3486 | | #ifdef BUILD_TLS_AES_128_GCM_SHA256 |
3487 | | case TLS_AES_128_GCM_SHA256: |
3488 | | mac = sha256_mac; |
3489 | | break; |
3490 | | #endif |
3491 | | #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256 |
3492 | | case TLS_CHACHA20_POLY1305_SHA256: |
3493 | | mac = sha256_mac; |
3494 | | break; |
3495 | | #endif |
3496 | | #ifdef BUILD_TLS_AES_128_CCM_SHA256 |
3497 | | case TLS_AES_128_CCM_SHA256: |
3498 | | mac = sha256_mac; |
3499 | | break; |
3500 | | #endif |
3501 | | #ifdef BUILD_TLS_AES_128_CCM_8_SHA256 |
3502 | | case TLS_AES_128_CCM_8_SHA256: |
3503 | | mac = sha256_mac; |
3504 | | break; |
3505 | | #endif |
3506 | | #ifdef BUILD_TLS_AES_256_GCM_SHA384 |
3507 | | case TLS_AES_256_GCM_SHA384: |
3508 | | mac = sha384_mac; |
3509 | | break; |
3510 | | #endif |
3511 | | default: |
3512 | | break; |
3513 | | } |
3514 | | } |
3515 | | #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \ |
3516 | | defined(WOLFSSL_SM3) |
3517 | | else if (suite[0] == CIPHER_BYTE) { |
3518 | | switch (suite[1]) { |
3519 | | #ifdef BUILD_TLS_SM4_GCM_SM3 |
3520 | | case TLS_SM4_GCM_SM3: |
3521 | | mac = sm3_mac; |
3522 | | break; |
3523 | | #endif |
3524 | | #ifdef BUILD_TLS_SM4_CCM_SM3 |
3525 | | case TLS_SM4_CCM_SM3: |
3526 | | mac = sm3_mac; |
3527 | | break; |
3528 | | #endif |
3529 | | default: |
3530 | | break; |
3531 | | } |
3532 | | } |
3533 | | #endif |
3534 | | #ifdef HAVE_NULL_CIPHER |
3535 | | else if (suite[0] == ECC_BYTE) { |
3536 | | switch (suite[1]) { |
3537 | | #ifdef BUILD_TLS_SHA256_SHA256 |
3538 | | case TLS_SHA256_SHA256: |
3539 | | mac = sha256_mac; |
3540 | | break; |
3541 | | #endif |
3542 | | #ifdef BUILD_TLS_SHA384_SHA384 |
3543 | | case TLS_SHA384_SHA384: |
3544 | | mac = sha384_mac; |
3545 | | break; |
3546 | | #endif |
3547 | | default: |
3548 | | break; |
3549 | | } |
3550 | | } |
3551 | | #endif |
3552 | | |
3553 | | return mac; |
3554 | | } |
3555 | | #endif |
3556 | | |
3557 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3558 | | /* Create Cookie extension using the hash of the first ClientHello. |
3559 | | * |
3560 | | * ssl SSL/TLS object. |
3561 | | * hash The hash data. |
3562 | | * hashSz The size of the hash data in bytes. |
3563 | | * returns 0 on success, otherwise failure. |
3564 | | */ |
3565 | | int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz, |
3566 | | TLSX** exts, byte cipherSuite0, byte cipherSuite) |
3567 | | { |
3568 | | int ret; |
3569 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
3570 | | Hmac cookieHmac; |
3571 | | byte cookieType = 0; |
3572 | | byte macSz = 0; |
3573 | | byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2]; |
3574 | | TLSX* ext; |
3575 | | word16 cookieSz = 0; |
3576 | | |
3577 | | if (hash == NULL || hashSz == 0) { |
3578 | | return BAD_FUNC_ARG; |
3579 | | } |
3580 | | |
3581 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
3582 | | ssl->buffers.tls13CookieSecret.length == 0) { |
3583 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
3584 | | return COOKIE_ERROR; |
3585 | | } |
3586 | | |
3587 | | /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */ |
3588 | | cookie[cookieSz++] = (byte)hashSz; |
3589 | | XMEMCPY(cookie + cookieSz, hash, hashSz); |
3590 | | cookieSz += hashSz; |
3591 | | cookie[cookieSz++] = cipherSuite0; |
3592 | | cookie[cookieSz++] = cipherSuite; |
3593 | | if ((ext = TLSX_Find(*exts, TLSX_KEY_SHARE)) != NULL) { |
3594 | | KeyShareEntry* kse = (KeyShareEntry*)ext->data; |
3595 | | if (kse == NULL) { |
3596 | | WOLFSSL_MSG("KeyShareEntry can't be empty when negotiating " |
3597 | | "parameters"); |
3598 | | return BAD_STATE_E; |
3599 | | } |
3600 | | c16toa(kse->group, cookie + cookieSz); |
3601 | | cookieSz += OPAQUE16_LEN; |
3602 | | } |
3603 | | |
3604 | | #if !defined(NO_SHA) && defined(NO_SHA256) |
3605 | | cookieType = SHA; |
3606 | | macSz = WC_SHA_DIGEST_SIZE; |
3607 | | #endif /* NO_SHA */ |
3608 | | #ifndef NO_SHA256 |
3609 | | cookieType = WC_SHA256; |
3610 | | macSz = WC_SHA256_DIGEST_SIZE; |
3611 | | #endif /* NO_SHA256 */ |
3612 | | |
3613 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
3614 | | if (ret == 0) { |
3615 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
3616 | | ssl->buffers.tls13CookieSecret.buffer, |
3617 | | ssl->buffers.tls13CookieSecret.length); |
3618 | | } |
3619 | | if (ret == 0) |
3620 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
3621 | | #ifdef WOLFSSL_DTLS13 |
3622 | | /* Tie cookie to peer address */ |
3623 | | if (ret == 0) { |
3624 | | /* peerLock not necessary. Still in handshake phase. */ |
3625 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
3626 | | ret = wc_HmacUpdate(&cookieHmac, |
3627 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
3628 | | ssl->buffers.dtlsCtx.peer.sz); |
3629 | | } |
3630 | | } |
3631 | | #endif |
3632 | | if (ret == 0) |
3633 | | ret = wc_HmacFinal(&cookieHmac, mac); |
3634 | | |
3635 | | wc_HmacFree(&cookieHmac); |
3636 | | if (ret != 0) |
3637 | | return ret; |
3638 | | |
3639 | | /* The cookie data is the hash and the integrity check. */ |
3640 | | return TLSX_Cookie_Use(ssl, cookie, cookieSz, mac, macSz, 1, exts); |
3641 | | } |
3642 | | #endif |
3643 | | |
3644 | | #ifdef WOLFSSL_DTLS13 |
3645 | | #define HRR_MAX_HS_HEADER_SZ DTLS_HANDSHAKE_HEADER_SZ |
3646 | | #else |
3647 | | #define HRR_MAX_HS_HEADER_SZ HANDSHAKE_HEADER_SZ |
3648 | | #endif /* WOLFSSL_DTLS13 */ |
3649 | | |
3650 | | static int CreateCookie(const WOLFSSL* ssl, byte** hash, byte* hashSz, |
3651 | | Hashes* hashes, TLSX** exts) |
3652 | | { |
3653 | | int ret = 0; |
3654 | | |
3655 | | (void)exts; |
3656 | | |
3657 | | *hash = NULL; |
3658 | | switch (ssl->specs.mac_algorithm) { |
3659 | | #ifndef NO_SHA256 |
3660 | | case sha256_mac: |
3661 | | *hash = hashes->sha256; |
3662 | | break; |
3663 | | #endif |
3664 | | #ifdef WOLFSSL_SHA384 |
3665 | | case sha384_mac: |
3666 | | *hash = hashes->sha384; |
3667 | | break; |
3668 | | #endif |
3669 | | #ifdef WOLFSSL_TLS13_SHA512 |
3670 | | case sha512_mac: |
3671 | | *hash = hashes->sha512; |
3672 | | break; |
3673 | | #endif |
3674 | | #ifdef WOLFSSL_SM3 |
3675 | | case sm3_mac: |
3676 | | *hash = hashes->sm3; |
3677 | | break; |
3678 | | #endif |
3679 | | } |
3680 | | *hashSz = ssl->specs.hash_size; |
3681 | | |
3682 | | /* check hash */ |
3683 | | if (*hash == NULL && *hashSz > 0) |
3684 | | return BAD_FUNC_ARG; |
3685 | | |
3686 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3687 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3688 | | ret = CreateCookieExt(ssl, *hash, *hashSz, exts, |
3689 | | ssl->options.cipherSuite0, ssl->options.cipherSuite); |
3690 | | #endif |
3691 | | return ret; |
3692 | | } |
3693 | | |
3694 | | /* Restart the handshake hash with a hash of the previous messages. |
3695 | | * |
3696 | | * ssl The SSL/TLS object. |
3697 | | * returns 0 on success, otherwise failure. |
3698 | | */ |
3699 | | int RestartHandshakeHash(WOLFSSL* ssl) |
3700 | 0 | { |
3701 | 0 | int ret; |
3702 | 0 | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
3703 | 0 | Hashes hashes; |
3704 | 0 | byte* hash = NULL; |
3705 | 0 | byte hashSz = 0; |
3706 | |
|
3707 | 0 | ret = BuildCertHashes(ssl, &hashes); |
3708 | 0 | if (ret != 0) |
3709 | 0 | return ret; |
3710 | 0 | ret = CreateCookie(ssl, &hash, &hashSz, &hashes, &ssl->extensions); |
3711 | 0 | if (ret != 0) |
3712 | 0 | return ret; |
3713 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3714 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3715 | | return 0; |
3716 | | #endif |
3717 | | |
3718 | 0 | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
3719 | |
|
3720 | | #ifdef WOLFSSL_DEBUG_TLS |
3721 | | WOLFSSL_MSG("Restart Hash"); |
3722 | | WOLFSSL_BUFFER(hash, hashSz); |
3723 | | #endif |
3724 | |
|
3725 | 0 | ret = InitHandshakeHashes(ssl); |
3726 | 0 | if (ret != 0) |
3727 | 0 | return ret; |
3728 | 0 | ret = HashRaw(ssl, header, sizeof(header)); |
3729 | 0 | if (ret != 0) |
3730 | 0 | return ret; |
3731 | 0 | return HashRaw(ssl, hash, hashSz); |
3732 | 0 | } |
3733 | | |
3734 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
3735 | | /* The value in the random field of a ServerHello to indicate |
3736 | | * HelloRetryRequest. |
3737 | | */ |
3738 | | static byte helloRetryRequestRandom[] = { |
3739 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
3740 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
3741 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
3742 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
3743 | | }; |
3744 | | #endif |
3745 | | |
3746 | | #ifndef NO_WOLFSSL_CLIENT |
3747 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
3748 | | #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_PSK_ONE_ID) && \ |
3749 | | !defined(NO_PSK) |
3750 | | /** |
3751 | | * convert mac algorithm to WOLFSSL_EVP_MD |
3752 | | * @param mac_alg mac algorithm |
3753 | | * @return const WOLFSSL_EVP_MD on successful, otherwise NULL |
3754 | | */ |
3755 | | static const WOLFSSL_EVP_MD* ssl_handshake_md(const byte mac_alg) |
3756 | | { |
3757 | | switch(mac_alg) { |
3758 | | case no_mac: |
3759 | | #ifndef NO_MD5 |
3760 | | case md5_mac: |
3761 | | return wolfSSL_EVP_md5(); |
3762 | | #endif |
3763 | | #ifndef NO_SHA |
3764 | | case sha_mac: |
3765 | | return wolfSSL_EVP_sha1(); |
3766 | | #endif |
3767 | | #ifdef WOLFSSL_SHA224 |
3768 | | case sha224_mac: |
3769 | | return wolfSSL_EVP_sha224(); |
3770 | | #endif |
3771 | | case sha256_mac: |
3772 | | return wolfSSL_EVP_sha256(); |
3773 | | #ifdef WOLFSSL_SHA384 |
3774 | | case sha384_mac: |
3775 | | return wolfSSL_EVP_sha384(); |
3776 | | #endif |
3777 | | #ifdef WOLFSSL_SHA512 |
3778 | | case sha512_mac: |
3779 | | return wolfSSL_EVP_sha512(); |
3780 | | #endif |
3781 | | case rmd_mac: |
3782 | | case blake2b_mac: |
3783 | | WOLFSSL_MSG("no suitable EVP_MD"); |
3784 | | return NULL; |
3785 | | default: |
3786 | | WOLFSSL_MSG("Unknown mac algorithm"); |
3787 | | return NULL; |
3788 | | } |
3789 | | } |
3790 | | #endif |
3791 | | /* Setup pre-shared key based on the details in the extension data. |
3792 | | * |
3793 | | * ssl SSL/TLS object. |
3794 | | * psk Pre-shared key extension data. |
3795 | | * clientHello Whether called from client_hello construction. |
3796 | | * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and |
3797 | | * other negative value on failure. |
3798 | | */ |
3799 | | static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello) |
3800 | | { |
3801 | | #if defined(HAVE_SESSION_TICKET) || !defined(WOLFSSL_PSK_ONE_ID) |
3802 | | int ret; |
3803 | | #endif |
3804 | | byte suite[2]; |
3805 | | |
3806 | | if (psk == NULL) |
3807 | | return BAD_FUNC_ARG; |
3808 | | |
3809 | | if (!HaveUniqueSessionObj(ssl)) { |
3810 | | WOLFSSL_MSG("Unable to have unique session object"); |
3811 | | WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR); |
3812 | | return MEMORY_ERROR; |
3813 | | } |
3814 | | |
3815 | | suite[0] = ssl->options.cipherSuite0; |
3816 | | suite[1] = ssl->options.cipherSuite; |
3817 | | |
3818 | | #ifdef HAVE_SESSION_TICKET |
3819 | | if (psk->resumption) { |
3820 | | if (clientHello) { |
3821 | | suite[0] = psk->cipherSuite0; |
3822 | | suite[1] = psk->cipherSuite; |
3823 | | |
3824 | | /* Ensure cipher suite is supported or changed suite to one with |
3825 | | * the same MAC algorithm. */ |
3826 | | if (!FindSuiteSSL(ssl, suite)) { |
3827 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3828 | | return PSK_KEY_ERROR; |
3829 | | } |
3830 | | |
3831 | | ssl->options.cipherSuite0 = suite[0]; |
3832 | | ssl->options.cipherSuite = suite[1]; |
3833 | | |
3834 | | /* Setting mac for binder and keys for deriving EarlyData. */ |
3835 | | ret = SetCipherSpecs(ssl); |
3836 | | if (ret != 0) |
3837 | | return ret; |
3838 | | } |
3839 | | |
3840 | | #ifdef WOLFSSL_EARLY_DATA |
3841 | | if (ssl->session->maxEarlyDataSz == 0) |
3842 | | ssl->earlyData = no_early_data; |
3843 | | #endif |
3844 | | /* Resumption PSK is master secret. */ |
3845 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
3846 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
3847 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
3848 | | return ret; |
3849 | | } |
3850 | | if (!clientHello) { |
3851 | | /* CLIENT: using secret in ticket for peer authentication. */ |
3852 | | ssl->options.peerAuthGood = 1; |
3853 | | } |
3854 | | } |
3855 | | #endif |
3856 | | #ifndef NO_PSK |
3857 | | if (!psk->resumption) { |
3858 | | /* Get the pre-shared key. */ |
3859 | | #ifndef WOLFSSL_PSK_ONE_ID |
3860 | | const char* cipherName = NULL; |
3861 | | #ifdef OPENSSL_EXTRA |
3862 | | WOLFSSL_SESSION* psksession = NULL; |
3863 | | #endif |
3864 | | |
3865 | | /* Set the client identity to use. */ |
3866 | | XMEMSET(ssl->arrays->client_identity, 0, |
3867 | | sizeof(ssl->arrays->client_identity)); |
3868 | | XMEMCPY(ssl->arrays->client_identity, psk->identity, psk->identityLen); |
3869 | | |
3870 | | #ifdef WOLFSSL_DEBUG_TLS |
3871 | | WOLFSSL_MSG("PSK cipher suite:"); |
3872 | | WOLFSSL_MSG(GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
3873 | | #endif |
3874 | | |
3875 | | /* Get the pre-shared key. */ |
3876 | | #ifdef OPENSSL_EXTRA |
3877 | | if (ssl->options.session_psk_cb != NULL) { |
3878 | | const unsigned char* id = NULL; |
3879 | | size_t idlen = 0; |
3880 | | const WOLFSSL_EVP_MD* handshake_md = NULL; |
3881 | | |
3882 | | if (ssl->msgsReceived.got_hello_retry_request >= 1) { |
3883 | | handshake_md = ssl_handshake_md(ssl->specs.mac_algorithm); |
3884 | | } |
3885 | | /* OpenSSL compatible callback that gets cached session. */ |
3886 | | if (ssl->options.session_psk_cb(ssl, handshake_md, &id, &idlen, |
3887 | | &psksession) == 0) { |
3888 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3889 | | WOLFSSL_MSG("psk session callback failed"); |
3890 | | return PSK_KEY_ERROR; |
3891 | | } |
3892 | | if (psksession != NULL) { |
3893 | | if (idlen > MAX_PSK_KEY_LEN) { |
3894 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3895 | | WOLFSSL_MSG("psk key length is too long"); |
3896 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3897 | | return PSK_KEY_ERROR; |
3898 | | } |
3899 | | |
3900 | | ssl->arrays->psk_keySz = (word32)idlen; |
3901 | | XMEMCPY(ssl->arrays->psk_key, id, idlen); |
3902 | | suite[0] = psksession->cipherSuite0; |
3903 | | suite[1] = psksession->cipherSuite; |
3904 | | /* Not needed anymore. */ |
3905 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
3906 | | /* Leave pointer not NULL to indicate success with callback. */ |
3907 | | } |
3908 | | } |
3909 | | if (psksession != NULL) { |
3910 | | /* Don't try other callbacks - we have an answer. */ |
3911 | | } |
3912 | | else |
3913 | | #endif /* OPENSSL_EXTRA */ |
3914 | | if (ssl->options.client_psk_cs_cb != NULL) { |
3915 | | #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
3916 | | ssl->arrays->client_identity[0] = 0; |
3917 | | #endif |
3918 | | /* Lookup key again for next identity. */ |
3919 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cs_cb( |
3920 | | ssl, ssl->arrays->server_hint, |
3921 | | ssl->arrays->client_identity, MAX_PSK_ID_LEN, |
3922 | | ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
3923 | | GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
3924 | | if (clientHello) { |
3925 | | /* Use PSK cipher suite. */ |
3926 | | ssl->options.cipherSuite0 = psk->cipherSuite0; |
3927 | | ssl->options.cipherSuite = psk->cipherSuite; |
3928 | | } |
3929 | | else { |
3930 | | byte pskCS[2]; |
3931 | | pskCS[0] = psk->cipherSuite0; |
3932 | | pskCS[1] = psk->cipherSuite; |
3933 | | |
3934 | | /* Ensure PSK and negotiated cipher suites have same hash. */ |
3935 | | if (SuiteMac(pskCS) != SuiteMac(suite)) { |
3936 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3937 | | return PSK_KEY_ERROR; |
3938 | | } |
3939 | | /* Negotiated cipher suite is to be used - update PSK. */ |
3940 | | psk->cipherSuite0 = suite[0]; |
3941 | | psk->cipherSuite = suite[1]; |
3942 | | } |
3943 | | } |
3944 | | else if (ssl->options.client_psk_tls13_cb != NULL) { |
3945 | | byte cipherSuite0; |
3946 | | byte cipherSuite; |
3947 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
3948 | | |
3949 | | ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl, |
3950 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
3951 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
3952 | | &cipherName); |
3953 | | if (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
3954 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) { |
3955 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3956 | | return PSK_KEY_ERROR; |
3957 | | } |
3958 | | ssl->options.cipherSuite0 = cipherSuite0; |
3959 | | ssl->options.cipherSuite = cipherSuite; |
3960 | | (void)cipherSuiteFlags; |
3961 | | } |
3962 | | else { |
3963 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl, |
3964 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
3965 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN); |
3966 | | ssl->options.cipherSuite0 = TLS13_BYTE; |
3967 | | ssl->options.cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
3968 | | } |
3969 | | if (ssl->arrays->psk_keySz == 0 || |
3970 | | (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN && |
3971 | | (int)ssl->arrays->psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK))) { |
3972 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3973 | | return PSK_KEY_ERROR; |
3974 | | } |
3975 | | |
3976 | | ret = SetCipherSpecs(ssl); |
3977 | | if (ret != 0) |
3978 | | return ret; |
3979 | | #else |
3980 | | /* PSK information loaded during setting of default TLS extensions. */ |
3981 | | #endif /* !WOLFSSL_PSK_ONE_ID */ |
3982 | | |
3983 | | if (!clientHello && (psk->cipherSuite0 != suite[0] || |
3984 | | psk->cipherSuite != suite[1])) { |
3985 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
3986 | | return PSK_KEY_ERROR; |
3987 | | } |
3988 | | |
3989 | | if (!clientHello) { |
3990 | | /* CLIENT: using PSK for peer authentication. */ |
3991 | | ssl->options.peerAuthGood = 1; |
3992 | | } |
3993 | | } |
3994 | | #endif |
3995 | | |
3996 | | if (ssl->options.noPskDheKe) { |
3997 | | ssl->arrays->preMasterSz = 0; |
3998 | | } |
3999 | | |
4000 | | /* Derive the early secret using the PSK. */ |
4001 | | return DeriveEarlySecret(ssl); |
4002 | | } |
4003 | | |
4004 | | /* Derive and write the binders into the ClientHello in space left when |
4005 | | * writing the Pre-Shared Key extension. |
4006 | | * |
4007 | | * ssl The SSL/TLS object. |
4008 | | * output The buffer containing the ClientHello. |
4009 | | * idx The index at the end of the completed ClientHello. |
4010 | | * returns 0 on success and otherwise failure. |
4011 | | */ |
4012 | | static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx) |
4013 | | { |
4014 | | int ret; |
4015 | | TLSX* ext; |
4016 | | PreSharedKey* current; |
4017 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
4018 | | word16 len; |
4019 | | |
4020 | | WOLFSSL_ENTER("WritePSKBinders"); |
4021 | | |
4022 | | if (idx > WOLFSSL_MAX_16BIT) { |
4023 | | return INPUT_SIZE_E; |
4024 | | } |
4025 | | |
4026 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
4027 | | if (ext == NULL) |
4028 | | return SANITY_MSG_E; |
4029 | | |
4030 | | /* Get the size of the binders to determine where to write binders. */ |
4031 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
4032 | | client_hello, &len); |
4033 | | if (ret < 0) |
4034 | | return ret; |
4035 | | idx -= len; |
4036 | | |
4037 | | /* Hash truncated ClientHello - up to binders. */ |
4038 | | #ifdef WOLFSSL_DTLS13 |
4039 | | if (ssl->options.dtls) |
4040 | | ret = Dtls13HashHandshake(ssl, output + Dtls13GetRlHeaderLength(ssl, 0), |
4041 | | (word16)idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4042 | | else |
4043 | | #endif /* WOLFSSL_DTLS13 */ |
4044 | | ret = HashOutput(ssl, output, (int)idx, 0); |
4045 | | |
4046 | | if (ret != 0) |
4047 | | return ret; |
4048 | | |
4049 | | current = (PreSharedKey*)ext->data; |
4050 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4051 | | if (current != NULL) { |
4052 | | wc_MemZero_Add("WritePSKBinders binderKey", binderKey, |
4053 | | sizeof(binderKey)); |
4054 | | } |
4055 | | #endif |
4056 | | /* Calculate the binder for each identity based on previous handshake data. |
4057 | | */ |
4058 | | while (current != NULL) { |
4059 | | if ((ret = SetupPskKey(ssl, current, 1)) != 0) |
4060 | | break; |
4061 | | |
4062 | | #ifdef HAVE_SESSION_TICKET |
4063 | | if (current->resumption) |
4064 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
4065 | | #endif |
4066 | | #ifndef NO_PSK |
4067 | | if (!current->resumption) |
4068 | | ret = DeriveBinderKey(ssl, binderKey); |
4069 | | #endif |
4070 | | if (ret != 0) |
4071 | | break; |
4072 | | |
4073 | | /* Derive the Finished message secret. */ |
4074 | | ret = DeriveFinishedSecret(ssl, binderKey, |
4075 | | ssl->keys.client_write_MAC_secret, |
4076 | | 0 /* neither end */); |
4077 | | if (ret != 0) |
4078 | | break; |
4079 | | |
4080 | | /* Build the HMAC of the handshake message data = binder. */ |
4081 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, |
4082 | | current->binder, ¤t->binderLen); |
4083 | | if (ret != 0) |
4084 | | break; |
4085 | | |
4086 | | current = current->next; |
4087 | | } |
4088 | | |
4089 | | ForceZero(binderKey, sizeof(binderKey)); |
4090 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4091 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
4092 | | #endif |
4093 | | if (ret != 0) |
4094 | | return ret; |
4095 | | |
4096 | | /* Data entered into extension, now write to message. */ |
4097 | | ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx, |
4098 | | client_hello, &len); |
4099 | | if (ret < 0) |
4100 | | return ret; |
4101 | | |
4102 | | /* Hash binders to complete the hash of the ClientHello. */ |
4103 | | ret = HashRaw(ssl, output + idx, len); |
4104 | | if (ret < 0) |
4105 | | return ret; |
4106 | | |
4107 | | #ifdef WOLFSSL_EARLY_DATA |
4108 | | if (ssl->earlyData != no_early_data) { |
4109 | | if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data, 1)) != 0) |
4110 | | return ret; |
4111 | | |
4112 | | /* Derive early data encryption key. */ |
4113 | | ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1); |
4114 | | if (ret != 0) |
4115 | | return ret; |
4116 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
4117 | | return ret; |
4118 | | |
4119 | | } |
4120 | | #endif |
4121 | | |
4122 | | WOLFSSL_LEAVE("WritePSKBinders", ret); |
4123 | | |
4124 | | return ret; |
4125 | | } |
4126 | | #endif |
4127 | | |
4128 | | #if defined(HAVE_ECH) |
4129 | | /* returns the index of the first supported cipher suite, -1 if none */ |
4130 | | int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config) |
4131 | | { |
4132 | | int i, j, supported = 0; |
4133 | | |
4134 | | for (i = 0; i < config->numCipherSuites; i++) { |
4135 | | supported = 0; |
4136 | | |
4137 | | for (j = 0; j < HPKE_SUPPORTED_KDF_LEN; j++) { |
4138 | | if (config->cipherSuites[i].kdfId == hpkeSupportedKdf[j]) |
4139 | | break; |
4140 | | } |
4141 | | |
4142 | | if (j < HPKE_SUPPORTED_KDF_LEN) |
4143 | | for (j = 0; j < HPKE_SUPPORTED_AEAD_LEN; j++) { |
4144 | | if (config->cipherSuites[i].aeadId == hpkeSupportedAead[j]) { |
4145 | | supported = 1; |
4146 | | break; |
4147 | | } |
4148 | | } |
4149 | | |
4150 | | if (supported) |
4151 | | return i; |
4152 | | } |
4153 | | |
4154 | | return WOLFSSL_FATAL_ERROR; |
4155 | | } |
4156 | | |
4157 | | /* returns status after we hash the ech inner */ |
4158 | | static int EchHashHelloInner(WOLFSSL* ssl, WOLFSSL_ECH* ech) |
4159 | | { |
4160 | | int ret = 0; |
4161 | | word32 realSz; |
4162 | | HS_Hashes* tmpHashes; |
4163 | | #ifdef WOLFSSL_DTLS13 |
4164 | | byte falseHeader[DTLS13_HANDSHAKE_HEADER_SZ]; |
4165 | | #else |
4166 | | byte falseHeader[HANDSHAKE_HEADER_SZ]; |
4167 | | #endif |
4168 | | |
4169 | | if (ssl == NULL || ech == NULL) |
4170 | | return BAD_FUNC_ARG; |
4171 | | realSz = ech->innerClientHelloLen - ech->paddingLen - ech->hpke->Nt; |
4172 | | tmpHashes = ssl->hsHashes; |
4173 | | ssl->hsHashes = NULL; |
4174 | | /* init the ech hashes */ |
4175 | | ret = InitHandshakeHashes(ssl); |
4176 | | if (ret == 0) { |
4177 | | ssl->hsHashesEch = ssl->hsHashes; |
4178 | | /* do the handshake header then the body */ |
4179 | | AddTls13HandShakeHeader(falseHeader, realSz, 0, 0, client_hello, ssl); |
4180 | | ret = HashRaw(ssl, falseHeader, HANDSHAKE_HEADER_SZ); |
4181 | | /* hash with inner */ |
4182 | | if (ret == 0) { |
4183 | | /* init hsHashesEchInner */ |
4184 | | if (ech->innerCount == 0) { |
4185 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4186 | | ret = InitHandshakeHashes(ssl); |
4187 | | if (ret == 0) { |
4188 | | ssl->hsHashesEchInner = ssl->hsHashes; |
4189 | | ech->innerCount = 1; |
4190 | | } |
4191 | | } |
4192 | | else { |
4193 | | /* switch back to hsHashes so we have hrr -> echInner2 */ |
4194 | | ssl->hsHashes = tmpHashes; |
4195 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, |
4196 | | &ssl->hsHashesEchInner); |
4197 | | } |
4198 | | |
4199 | | if (ret == 0) { |
4200 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4201 | | ret = HashRaw(ssl, falseHeader, HANDSHAKE_HEADER_SZ); |
4202 | | ssl->hsHashes = ssl->hsHashesEch; |
4203 | | } |
4204 | | } |
4205 | | } |
4206 | | /* hash the body */ |
4207 | | if (ret == 0) |
4208 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
4209 | | /* hash with inner */ |
4210 | | if (ret == 0) { |
4211 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4212 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
4213 | | } |
4214 | | /* swap hsHashes back */ |
4215 | | ssl->hsHashes = tmpHashes; |
4216 | | return ret; |
4217 | | } |
4218 | | #endif |
4219 | | |
4220 | | static void GetTls13SessionId(WOLFSSL* ssl, byte* output, word32* idx) |
4221 | 6.19k | { |
4222 | 6.19k | if (ssl->session->sessionIDSz > 0) { |
4223 | | /* Session resumption for old versions of protocol. */ |
4224 | 0 | if (ssl->session->sessionIDSz <= ID_LEN) { |
4225 | 0 | if (output != NULL) |
4226 | 0 | output[*idx] = ssl->session->sessionIDSz; |
4227 | 0 | (*idx)++; |
4228 | 0 | if (output != NULL) { |
4229 | 0 | XMEMCPY(output + *idx, ssl->session->sessionID, |
4230 | 0 | ssl->session->sessionIDSz); |
4231 | 0 | } |
4232 | 0 | *idx += ssl->session->sessionIDSz; |
4233 | 0 | } |
4234 | 0 | else { |
4235 | | /* Invalid session ID length. Reset it. */ |
4236 | 0 | ssl->session->sessionIDSz = 0; |
4237 | 0 | if (output != NULL) |
4238 | 0 | output[*idx] = 0; |
4239 | 0 | (*idx)++; |
4240 | 0 | } |
4241 | 0 | } |
4242 | 6.19k | else { |
4243 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
4244 | | if (ssl->options.tls13MiddleBoxCompat) { |
4245 | | if (output != NULL) |
4246 | | output[*idx] = ID_LEN; |
4247 | | (*idx)++; |
4248 | | if (output != NULL) |
4249 | | XMEMCPY(output + *idx, ssl->arrays->clientRandom, ID_LEN); |
4250 | | *idx += ID_LEN; |
4251 | | } |
4252 | | else |
4253 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
4254 | 6.19k | { |
4255 | | /* TLS v1.3 does not use session id - 0 length. */ |
4256 | 6.19k | if (output != NULL) |
4257 | 2.84k | output[*idx] = 0; |
4258 | 6.19k | (*idx)++; |
4259 | 6.19k | } |
4260 | 6.19k | } |
4261 | 6.19k | } |
4262 | | |
4263 | | /* handle generation of TLS 1.3 client_hello (1) */ |
4264 | | /* Send a ClientHello message to the server. |
4265 | | * Include the information required to start a handshake with servers using |
4266 | | * protocol versions less than TLS v1.3. |
4267 | | * Only a client will send this message. |
4268 | | * |
4269 | | * ssl The SSL/TLS object. |
4270 | | * returns 0 on success and otherwise failure. |
4271 | | */ |
4272 | | |
4273 | | typedef struct Sch13Args { |
4274 | | byte* output; |
4275 | | word32 idx; |
4276 | | int sendSz; |
4277 | | word32 length; |
4278 | | #if defined(HAVE_ECH) |
4279 | | int clientRandomOffset; |
4280 | | int preXLength; |
4281 | | WOLFSSL_ECH* ech; |
4282 | | #endif |
4283 | | } Sch13Args; |
4284 | | |
4285 | | int SendTls13ClientHello(WOLFSSL* ssl) |
4286 | | { |
4287 | | int ret; |
4288 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4289 | | Sch13Args* args = NULL; |
4290 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
4291 | | #else |
4292 | | Sch13Args args[1]; |
4293 | | #endif |
4294 | | byte major, tls12minor; |
4295 | | const Suites* suites; |
4296 | | |
4297 | | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND); |
4298 | | WOLFSSL_ENTER("SendTls13ClientHello"); |
4299 | | |
4300 | | if (ssl == NULL) { |
4301 | | return BAD_FUNC_ARG; |
4302 | | } |
4303 | | |
4304 | | ssl->options.buildingMsg = 1; |
4305 | | major = SSLv3_MAJOR; |
4306 | | tls12minor = TLSv1_2_MINOR; |
4307 | | |
4308 | | #ifdef WOLFSSL_DTLS13 |
4309 | | if (ssl->options.dtls) { |
4310 | | major = DTLS_MAJOR; |
4311 | | tls12minor = DTLSv1_2_MINOR; |
4312 | | } |
4313 | | #endif /* WOLFSSL_DTLS */ |
4314 | | |
4315 | | #ifdef HAVE_SESSION_TICKET |
4316 | | if (ssl->options.resuming && |
4317 | | (ssl->session->version.major != ssl->version.major || |
4318 | | ssl->session->version.minor != ssl->version.minor)) { |
4319 | | #ifndef WOLFSSL_NO_TLS12 |
4320 | | if (ssl->session->version.major == ssl->version.major && |
4321 | | ssl->session->version.minor < ssl->version.minor) { |
4322 | | /* Cannot resume with a different protocol version. */ |
4323 | | ssl->options.resuming = 0; |
4324 | | ssl->version.major = ssl->session->version.major; |
4325 | | ssl->version.minor = ssl->session->version.minor; |
4326 | | return SendClientHello(ssl); |
4327 | | } |
4328 | | else |
4329 | | #endif |
4330 | | { |
4331 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
4332 | | return VERSION_ERROR; |
4333 | | } |
4334 | | } |
4335 | | #endif |
4336 | | |
4337 | | suites = WOLFSSL_SUITES(ssl); |
4338 | | if (suites == NULL) { |
4339 | | WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello"); |
4340 | | return SUITES_ERROR; |
4341 | | } |
4342 | | |
4343 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4344 | | if (ssl->async == NULL) { |
4345 | | ssl->async = (struct WOLFSSL_ASYNC*) |
4346 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
4347 | | DYNAMIC_TYPE_ASYNC); |
4348 | | if (ssl->async == NULL) |
4349 | | return MEMORY_E; |
4350 | | ssl->async->freeArgs = NULL; |
4351 | | } |
4352 | | args = (Sch13Args*)ssl->async->args; |
4353 | | |
4354 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
4355 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
4356 | | /* Check for error */ |
4357 | | if (ret < 0) |
4358 | | return ret; |
4359 | | } |
4360 | | else |
4361 | | #endif |
4362 | | { |
4363 | | /* Reset state */ |
4364 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
4365 | | XMEMSET(args, 0, sizeof(Sch13Args)); |
4366 | | } |
4367 | | |
4368 | | switch (ssl->options.asyncState) { |
4369 | | case TLS_ASYNC_BEGIN: |
4370 | | { |
4371 | | word32 sessIdSz = 0; |
4372 | | |
4373 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
4374 | | |
4375 | | #ifdef WOLFSSL_DTLS13 |
4376 | | if (ssl->options.dtls) |
4377 | | args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4378 | | #endif /* WOLFSSL_DTLS13 */ |
4379 | | |
4380 | | /* Version | Random | Cipher Suites | Compression */ |
4381 | | args->length = VERSION_SZ + RAN_LEN + suites->suiteSz + |
4382 | | SUITE_LEN + COMP_LEN + ENUM_LEN; |
4383 | | #ifdef WOLFSSL_QUIC |
4384 | | if (WOLFSSL_IS_QUIC(ssl)) { |
4385 | | /* RFC 9001 ch. 8.4 sessionID in ClientHello MUST be 0 length */ |
4386 | | ssl->session->sessionIDSz = 0; |
4387 | | ssl->options.tls13MiddleBoxCompat = 0; |
4388 | | } |
4389 | | #endif |
4390 | | GetTls13SessionId(ssl, NULL, &sessIdSz); |
4391 | | args->length += (word16)sessIdSz; |
4392 | | |
4393 | | #ifdef WOLFSSL_DTLS13 |
4394 | | if (ssl->options.dtls) { |
4395 | | /* legacy_cookie_id len */ |
4396 | | args->length += ENUM_LEN; |
4397 | | |
4398 | | /* server sent us an HelloVerifyRequest and we allow downgrade */ |
4399 | | if (ssl->arrays->cookieSz > 0 && ssl->options.downgrade) |
4400 | | args->length += ssl->arrays->cookieSz; |
4401 | | } |
4402 | | #endif /* WOLFSSL_DTLS13 */ |
4403 | | |
4404 | | /* Advance state and proceed */ |
4405 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
4406 | | } /* case TLS_ASYNC_BEGIN */ |
4407 | | FALL_THROUGH; |
4408 | | |
4409 | | case TLS_ASYNC_BUILD: |
4410 | | case TLS_ASYNC_DO: |
4411 | | { |
4412 | | /* Auto populate extensions supported unless user defined. */ |
4413 | | if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0) |
4414 | | return ret; |
4415 | | |
4416 | | /* Advance state and proceed */ |
4417 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
4418 | | } /* case TLS_ASYNC_BUILD */ |
4419 | | FALL_THROUGH; |
4420 | | |
4421 | | case TLS_ASYNC_FINALIZE: |
4422 | | { |
4423 | | #ifdef WOLFSSL_EARLY_DATA |
4424 | | #ifndef NO_PSK |
4425 | | if (!ssl->options.resuming && |
4426 | | ssl->options.client_psk_tls13_cb == NULL && |
4427 | | ssl->options.client_psk_cb == NULL) |
4428 | | #else |
4429 | | if (!ssl->options.resuming) |
4430 | | #endif |
4431 | | ssl->earlyData = no_early_data; |
4432 | | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
4433 | | ssl->earlyData = no_early_data; |
4434 | | if (ssl->earlyData == no_early_data) |
4435 | | TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap); |
4436 | | if (ssl->earlyData != no_early_data && |
4437 | | (ret = TLSX_EarlyData_Use(ssl, 0, 0)) < 0) { |
4438 | | return ret; |
4439 | | } |
4440 | | #endif |
4441 | | #ifdef WOLFSSL_QUIC |
4442 | | if (WOLFSSL_IS_QUIC(ssl) && IsAtLeastTLSv1_3(ssl->version)) { |
4443 | | ret = wolfSSL_quic_add_transport_extensions(ssl, client_hello); |
4444 | | if (ret != 0) |
4445 | | return ret; |
4446 | | } |
4447 | | #endif |
4448 | | |
4449 | | /* find length of outer and inner */ |
4450 | | #if defined(HAVE_ECH) |
4451 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH) { |
4452 | | TLSX* echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
4453 | | if (echX == NULL) |
4454 | | return WOLFSSL_FATAL_ERROR; |
4455 | | |
4456 | | args->ech = (WOLFSSL_ECH*)echX->data; |
4457 | | if (args->ech == NULL) |
4458 | | return WOLFSSL_FATAL_ERROR; |
4459 | | |
4460 | | /* only prepare if we have a chance at acceptance */ |
4461 | | if (ssl->options.echAccepted || args->ech->innerCount == 0) { |
4462 | | /* set the type to inner */ |
4463 | | args->ech->type = ECH_TYPE_INNER; |
4464 | | args->preXLength = (int)args->length; |
4465 | | |
4466 | | /* get size for inner */ |
4467 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4468 | | if (ret != 0) |
4469 | | return ret; |
4470 | | |
4471 | | /* set the type to outer */ |
4472 | | args->ech->type = 0; |
4473 | | /* set innerClientHelloLen to ClientHelloInner + padding + tag */ |
4474 | | args->ech->paddingLen = 31 - ((args->length - 1) % 32); |
4475 | | args->ech->innerClientHelloLen = (word16)(args->length + |
4476 | | args->ech->paddingLen + args->ech->hpke->Nt); |
4477 | | /* set the length back to before we computed ClientHelloInner size */ |
4478 | | args->length = (word32)args->preXLength; |
4479 | | } |
4480 | | } |
4481 | | #endif |
4482 | | |
4483 | | { |
4484 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4485 | | word16 maxFrag = wolfSSL_GetMaxFragSize(ssl, MAX_RECORD_SIZE); |
4486 | | word16 lenWithoutExts = args->length; |
4487 | | #endif |
4488 | | |
4489 | | /* Include length of TLS extensions. */ |
4490 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4491 | | if (ret != 0) |
4492 | | return ret; |
4493 | | |
4494 | | /* Total message size. */ |
4495 | | args->sendSz = |
4496 | | (int)(args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ); |
4497 | | |
4498 | | #ifdef WOLFSSL_DTLS13 |
4499 | | if (ssl->options.dtls) |
4500 | | args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4501 | | #endif /* WOLFSSL_DTLS13 */ |
4502 | | |
4503 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4504 | | if (ssl->options.dtls && args->sendSz > maxFrag && |
4505 | | TLSX_Find(ssl->extensions, TLSX_COOKIE) == NULL) { |
4506 | | /* Try again with an empty key share if we would be fragmenting |
4507 | | * without a cookie */ |
4508 | | ret = TLSX_KeyShare_Empty(ssl); |
4509 | | if (ret != 0) |
4510 | | return ret; |
4511 | | args->length = lenWithoutExts; |
4512 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4513 | | if (ret != 0) |
4514 | | return ret; |
4515 | | args->sendSz = (int)(args->length + |
4516 | | DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ); |
4517 | | if (args->sendSz > maxFrag) { |
4518 | | WOLFSSL_MSG("Can't fit first CH in one fragment."); |
4519 | | return BUFFER_ERROR; |
4520 | | } |
4521 | | WOLFSSL_MSG("Sending empty key share so we don't fragment CH1"); |
4522 | | } |
4523 | | #endif |
4524 | | } |
4525 | | |
4526 | | /* Check buffers are big enough and grow if needed. */ |
4527 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) |
4528 | | return ret; |
4529 | | |
4530 | | /* Get position in output buffer to write new message to. */ |
4531 | | args->output = GetOutputBuffer(ssl); |
4532 | | |
4533 | | /* Put the record and handshake headers on. */ |
4534 | | AddTls13Headers(args->output, args->length, client_hello, ssl); |
4535 | | |
4536 | | /* Protocol version - negotiation now in extension: supported_versions. */ |
4537 | | args->output[args->idx++] = major; |
4538 | | args->output[args->idx++] = tls12minor; |
4539 | | |
4540 | | /* Keep for downgrade. */ |
4541 | | ssl->chVersion = ssl->version; |
4542 | | |
4543 | | if (ssl->arrays == NULL) { |
4544 | | return BAD_FUNC_ARG; |
4545 | | } |
4546 | | /* Client Random */ |
4547 | | if (ssl->options.connectState == CONNECT_BEGIN) { |
4548 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + args->idx, RAN_LEN); |
4549 | | if (ret != 0) |
4550 | | return ret; |
4551 | | |
4552 | | /* Store random for possible second ClientHello. */ |
4553 | | XMEMCPY(ssl->arrays->clientRandom, args->output + args->idx, RAN_LEN); |
4554 | | } |
4555 | | else |
4556 | | XMEMCPY(args->output + args->idx, ssl->arrays->clientRandom, RAN_LEN); |
4557 | | |
4558 | | #if defined(HAVE_ECH) |
4559 | | args->clientRandomOffset = (int)args->idx; |
4560 | | #endif |
4561 | | |
4562 | | args->idx += RAN_LEN; |
4563 | | |
4564 | | GetTls13SessionId(ssl, args->output, &args->idx); |
4565 | | |
4566 | | #ifdef WOLFSSL_DTLS13 |
4567 | | if (ssl->options.dtls) { |
4568 | | args->output[args->idx++] = ssl->arrays->cookieSz; |
4569 | | |
4570 | | if (ssl->arrays->cookieSz > 0) { |
4571 | | /* We have a cookie saved, so the server sent us an |
4572 | | * HelloVerifyRequest, it means it is a v1.2 server */ |
4573 | | if (!ssl->options.downgrade) |
4574 | | return VERSION_ERROR; |
4575 | | XMEMCPY(args->output + args->idx, ssl->arrays->cookie, |
4576 | | ssl->arrays->cookieSz); |
4577 | | args->idx += ssl->arrays->cookieSz; |
4578 | | } |
4579 | | } |
4580 | | #endif /* WOLFSSL_DTLS13 */ |
4581 | | |
4582 | | /* Cipher suites */ |
4583 | | c16toa(suites->suiteSz, args->output + args->idx); |
4584 | | args->idx += OPAQUE16_LEN; |
4585 | | XMEMCPY(args->output + args->idx, &suites->suites, |
4586 | | suites->suiteSz); |
4587 | | args->idx += suites->suiteSz; |
4588 | | #ifdef WOLFSSL_DEBUG_TLS |
4589 | | { |
4590 | | int ii; |
4591 | | WOLFSSL_MSG("Ciphers:"); |
4592 | | for (ii = 0 ; ii < suites->suiteSz; ii += 2) { |
4593 | | WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0], |
4594 | | suites->suites[ii+1])); |
4595 | | } |
4596 | | } |
4597 | | #endif |
4598 | | |
4599 | | /* Compression not supported in TLS v1.3. */ |
4600 | | args->output[args->idx++] = COMP_LEN; |
4601 | | args->output[args->idx++] = NO_COMPRESSION; |
4602 | | |
4603 | | #if defined(HAVE_ECH) |
4604 | | /* write inner then outer */ |
4605 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4606 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4607 | | /* set the type to inner */ |
4608 | | args->ech->type = ECH_TYPE_INNER; |
4609 | | /* innerClientHello may already exist from hrr, free if it does */ |
4610 | | if (args->ech->innerClientHello != NULL) { |
4611 | | XFREE(args->ech->innerClientHello, ssl->heap, |
4612 | | DYNAMIC_TYPE_TMP_BUFFER); |
4613 | | } |
4614 | | /* allocate the inner */ |
4615 | | args->ech->innerClientHello = |
4616 | | (byte*)XMALLOC(args->ech->innerClientHelloLen - args->ech->hpke->Nt, |
4617 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
4618 | | if (args->ech->innerClientHello == NULL) |
4619 | | return MEMORY_E; |
4620 | | /* set the padding bytes to 0 */ |
4621 | | XMEMSET(args->ech->innerClientHello + args->ech->innerClientHelloLen - |
4622 | | args->ech->hpke->Nt - args->ech->paddingLen, 0, |
4623 | | args->ech->paddingLen); |
4624 | | /* copy the client hello to the ech innerClientHello, exclude record */ |
4625 | | /* and handshake headers */ |
4626 | | XMEMCPY(args->ech->innerClientHello, |
4627 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4628 | | args->idx - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ)); |
4629 | | /* copy the client random to inner */ |
4630 | | XMEMCPY(ssl->arrays->clientRandomInner, ssl->arrays->clientRandom, |
4631 | | RAN_LEN); |
4632 | | /* change the outer client random */ |
4633 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + |
4634 | | args->clientRandomOffset, RAN_LEN); |
4635 | | if (ret != 0) |
4636 | | return ret; |
4637 | | /* copy the new client random */ |
4638 | | XMEMCPY(ssl->arrays->clientRandom, args->output + |
4639 | | args->clientRandomOffset, RAN_LEN); |
4640 | | /* write the extensions for inner */ |
4641 | | args->length = 0; |
4642 | | ret = TLSX_WriteRequest(ssl, args->ech->innerClientHello + args->idx - |
4643 | | (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ), client_hello, |
4644 | | &args->length); |
4645 | | if (ret != 0) |
4646 | | return ret; |
4647 | | /* set the type to outer */ |
4648 | | args->ech->type = 0; |
4649 | | } |
4650 | | #endif |
4651 | | |
4652 | | /* Write out extensions for a request. */ |
4653 | | args->length = 0; |
4654 | | ret = TLSX_WriteRequest(ssl, args->output + args->idx, client_hello, |
4655 | | &args->length); |
4656 | | if (ret != 0) |
4657 | | return ret; |
4658 | | |
4659 | | args->idx += args->length; |
4660 | | |
4661 | | #if defined(HAVE_ECH) |
4662 | | /* encrypt and pack the ech innerClientHello */ |
4663 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4664 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4665 | | ret = TLSX_FinalizeEch(args->ech, |
4666 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4667 | | (word32)(args->sendSz - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ))); |
4668 | | |
4669 | | if (ret != 0) |
4670 | | return ret; |
4671 | | } |
4672 | | #endif |
4673 | | |
4674 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4675 | | /* Resumption has a specific set of extensions and binder is calculated |
4676 | | * for each identity. |
4677 | | */ |
4678 | | if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY)) { |
4679 | | ret = WritePSKBinders(ssl, args->output, args->idx); |
4680 | | } |
4681 | | else |
4682 | | #endif |
4683 | | { |
4684 | | #ifdef WOLFSSL_DTLS13 |
4685 | | if (ssl->options.dtls) |
4686 | | ret = Dtls13HashHandshake(ssl, |
4687 | | args->output + Dtls13GetRlHeaderLength(ssl, 0), |
4688 | | (word16)args->idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4689 | | else |
4690 | | #endif /* WOLFSSL_DTLS13 */ |
4691 | | { |
4692 | | #if defined(HAVE_ECH) |
4693 | | /* compute the inner hash */ |
4694 | | if (ssl->options.useEch == 1 && !ssl->options.disableECH && |
4695 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) |
4696 | | ret = EchHashHelloInner(ssl, args->ech); |
4697 | | #endif |
4698 | | /* compute the outer hash */ |
4699 | | if (ret == 0) |
4700 | | ret = HashOutput(ssl, args->output, (int)args->idx, 0); |
4701 | | } |
4702 | | } |
4703 | | if (ret != 0) |
4704 | | return ret; |
4705 | | |
4706 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
4707 | | |
4708 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
4709 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
4710 | | if (ssl->toInfoOn) { |
4711 | | ret = AddPacketInfo(ssl, "ClientHello", handshake, args->output, |
4712 | | args->sendSz, WRITE_PROTO, 0, ssl->heap); |
4713 | | if (ret != 0) |
4714 | | return ret; |
4715 | | } |
4716 | | #endif |
4717 | | |
4718 | | ssl->options.buildingMsg = 0; |
4719 | | #ifdef WOLFSSL_DTLS13 |
4720 | | if (ssl->options.dtls) { |
4721 | | ret = Dtls13HandshakeSend(ssl, args->output, (word16)args->sendSz, |
4722 | | (word16)args->idx, client_hello, 0); |
4723 | | break; |
4724 | | } |
4725 | | #endif /* WOLFSSL_DTLS13 */ |
4726 | | |
4727 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
4728 | | |
4729 | | /* Advance state and proceed */ |
4730 | | ssl->options.asyncState = TLS_ASYNC_END; |
4731 | | } |
4732 | | /* case TLS_ASYNC_BUILD */ |
4733 | | FALL_THROUGH; |
4734 | | |
4735 | | case TLS_ASYNC_END: |
4736 | | { |
4737 | | #ifdef WOLFSSL_EARLY_DATA_GROUP |
4738 | | /* QUIC needs to forward records at their encryption level |
4739 | | * and is therefore unable to group here */ |
4740 | | if (ssl->earlyData == no_early_data || WOLFSSL_IS_QUIC(ssl)) |
4741 | | #endif |
4742 | | ret = SendBuffered(ssl); |
4743 | | |
4744 | | break; |
4745 | | } |
4746 | | default: |
4747 | | ret = INPUT_CASE_ERROR; |
4748 | | } /* switch (ssl->options.asyncState) */ |
4749 | | |
4750 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4751 | | if (ret == 0) |
4752 | | FreeAsyncCtx(ssl, 0); |
4753 | | #endif |
4754 | | |
4755 | | WOLFSSL_LEAVE("SendTls13ClientHello", ret); |
4756 | | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND); |
4757 | | |
4758 | | return ret; |
4759 | | } |
4760 | | |
4761 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_CLIENT) |
4762 | | static int Dtls13ClientDoDowngrade(WOLFSSL* ssl) |
4763 | | { |
4764 | | int ret; |
4765 | | if (ssl->dtls13ClientHello == NULL) |
4766 | | return BAD_STATE_E; |
4767 | | |
4768 | | /* v1.3 and v1.2 hash messages to compute the transcript hash. When we are |
4769 | | * using DTLSv1.3 we hash the first clientHello following v1.3 but the |
4770 | | * server can negotiate a lower version. So we need to re-hash the |
4771 | | * clientHello to adhere to DTLS <= v1.2 rules. */ |
4772 | | ret = InitHandshakeHashes(ssl); |
4773 | | if (ret != 0) |
4774 | | return ret; |
4775 | | ret = HashRaw(ssl, ssl->dtls13ClientHello, ssl->dtls13ClientHelloSz); |
4776 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
4777 | | ssl->dtls13ClientHello = NULL; |
4778 | | ssl->dtls13ClientHelloSz = 0; |
4779 | | ssl->keys.dtls_sequence_number_hi = |
4780 | | (word16)w64GetHigh32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
4781 | | ssl->keys.dtls_sequence_number_lo = |
4782 | | w64GetLow32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
4783 | | return ret; |
4784 | | } |
4785 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_CLIENT*/ |
4786 | | |
4787 | | #if defined(HAVE_ECH) |
4788 | | /* check if the server accepted ech or not, must be run after an hsHashes |
4789 | | * restart */ |
4790 | | static int EchCheckAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
4791 | | const byte* input, int acceptOffset, int helloSz) |
4792 | | { |
4793 | | int ret = 0; |
4794 | | int digestType = 0; |
4795 | | int digestSize = 0; |
4796 | | HS_Hashes* tmpHashes; |
4797 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
4798 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
4799 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
4800 | | byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ]; |
4801 | | XMEMSET(zeros, 0, sizeof(zeros)); |
4802 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
4803 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
4804 | | XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation)); |
4805 | | /* store so we can restore regardless of the outcome */ |
4806 | | tmpHashes = ssl->hsHashes; |
4807 | | /* swap hsHashes to hsHashesEch */ |
4808 | | ssl->hsHashes = ssl->hsHashesEch; |
4809 | | /* hash up to the last 8 bytes */ |
4810 | | ret = HashRaw(ssl, input, acceptOffset); |
4811 | | /* hash 8 zeros */ |
4812 | | if (ret == 0) |
4813 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
4814 | | /* hash the rest of the hello */ |
4815 | | if (ret == 0) { |
4816 | | ret = HashRaw(ssl, input + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
4817 | | helloSz + HANDSHAKE_HEADER_SZ - |
4818 | | (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
4819 | | } |
4820 | | /* get the modified transcript hash */ |
4821 | | if (ret == 0) |
4822 | | ret = GetMsgHash(ssl, transcriptEchConf); |
4823 | | if (ret > 0) |
4824 | | ret = 0; |
4825 | | /* pick the right type and size based on mac_algorithm */ |
4826 | | if (ret == 0) { |
4827 | | switch (ssl->specs.mac_algorithm) { |
4828 | | #ifndef NO_SHA256 |
4829 | | case sha256_mac: |
4830 | | digestType = WC_SHA256; |
4831 | | digestSize = WC_SHA256_DIGEST_SIZE; |
4832 | | break; |
4833 | | #endif /* !NO_SHA256 */ |
4834 | | #ifdef WOLFSSL_SHA384 |
4835 | | case sha384_mac: |
4836 | | digestType = WC_SHA384; |
4837 | | digestSize = WC_SHA384_DIGEST_SIZE; |
4838 | | break; |
4839 | | #endif /* WOLFSSL_SHA384 */ |
4840 | | #ifdef WOLFSSL_TLS13_SHA512 |
4841 | | case sha512_mac: |
4842 | | digestType = WC_SHA512; |
4843 | | digestSize = WC_SHA512_DIGEST_SIZE; |
4844 | | break; |
4845 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
4846 | | #ifdef WOLFSSL_SM3 |
4847 | | case sm3_mac: |
4848 | | digestType = WC_SM3; |
4849 | | digestSize = WC_SM3_DIGEST_SIZE; |
4850 | | break; |
4851 | | #endif /* WOLFSSL_SM3 */ |
4852 | | default: |
4853 | | ret = WOLFSSL_FATAL_ERROR; |
4854 | | break; |
4855 | | } |
4856 | | } |
4857 | | /* extract clientRandomInner with a key of all zeros */ |
4858 | | if (ret == 0) { |
4859 | | PRIVATE_KEY_UNLOCK(); |
4860 | | #if !defined(HAVE_FIPS) || \ |
4861 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
4862 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
4863 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk, |
4864 | | ssl->heap, ssl->devId); |
4865 | | #else |
4866 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4867 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk); |
4868 | | #endif |
4869 | | PRIVATE_KEY_LOCK(); |
4870 | | } |
4871 | | /* tls expand with the confirmation label */ |
4872 | | if (ret == 0) { |
4873 | | PRIVATE_KEY_UNLOCK(); |
4874 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptConfirmation, |
4875 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4876 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4877 | | transcriptEchConf, (word32)digestSize, digestType, |
4878 | | WOLFSSL_SERVER_END); |
4879 | | PRIVATE_KEY_LOCK(); |
4880 | | } |
4881 | | if (ret == 0) { |
4882 | | /* last 8 bytes should match our expand output */ |
4883 | | ret = XMEMCMP(acceptConfirmation, input + acceptOffset, |
4884 | | ECH_ACCEPT_CONFIRMATION_SZ); |
4885 | | /* ech accepted */ |
4886 | | if (ret == 0) { |
4887 | | /* set echAccepted to 1 */ |
4888 | | ssl->options.echAccepted = 1; |
4889 | | /* free hsHashes and go with inner */ |
4890 | | ssl->hsHashes = tmpHashes; |
4891 | | FreeHandshakeHashes(ssl); |
4892 | | ssl->hsHashes = ssl->hsHashesEch; |
4893 | | tmpHashes = ssl->hsHashesEchInner; |
4894 | | ssl->hsHashesEchInner = NULL; |
4895 | | } |
4896 | | /* ech rejected */ |
4897 | | else { |
4898 | | /* set echAccepted to 0, needed in case HRR */ |
4899 | | ssl->options.echAccepted = 0; |
4900 | | /* free inner since we're continuing with outer */ |
4901 | | ssl->hsHashes = ssl->hsHashesEchInner; |
4902 | | FreeHandshakeHashes(ssl); |
4903 | | ssl->hsHashesEchInner = NULL; |
4904 | | } |
4905 | | /* continue with outer if we failed to verify ech was accepted */ |
4906 | | ret = 0; |
4907 | | } |
4908 | | FreeHandshakeHashes(ssl); |
4909 | | /* set hsHashesEch to NULL to avoid double free */ |
4910 | | ssl->hsHashesEch = NULL; |
4911 | | /* swap to tmp, will be inner if accepted, hsHashes if rejected */ |
4912 | | ssl->hsHashes = tmpHashes; |
4913 | | return ret; |
4914 | | } |
4915 | | |
4916 | | /* replace the last acceptance field for either sever hello or hrr with the ech |
4917 | | * acceptance parameter, return status */ |
4918 | | static int EchWriteAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
4919 | | byte* output, int acceptOffset, int helloSz, byte msgType) |
4920 | | { |
4921 | | int ret = 0; |
4922 | | int digestType = 0; |
4923 | | int digestSize = 0; |
4924 | | HS_Hashes* tmpHashes = NULL; |
4925 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
4926 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
4927 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
4928 | | XMEMSET(zeros, 0, sizeof(zeros)); |
4929 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
4930 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
4931 | | /* store so we can restore regardless of the outcome */ |
4932 | | tmpHashes = ssl->hsHashes; |
4933 | | ssl->hsHashes = ssl->hsHashesEch; |
4934 | | /* hash up to the acceptOffset */ |
4935 | | ret = HashRaw(ssl, output, acceptOffset); |
4936 | | /* hash 8 zeros */ |
4937 | | if (ret == 0) |
4938 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
4939 | | /* hash the rest of the hello */ |
4940 | | if (ret == 0) { |
4941 | | ret = HashRaw(ssl, output + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
4942 | | helloSz - (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
4943 | | } |
4944 | | /* get the modified transcript hash */ |
4945 | | if (ret == 0) |
4946 | | ret = GetMsgHash(ssl, transcriptEchConf); |
4947 | | if (ret > 0) |
4948 | | ret = 0; |
4949 | | /* pick the right type and size based on mac_algorithm */ |
4950 | | if (ret == 0) { |
4951 | | switch (ssl->specs.mac_algorithm) { |
4952 | | #ifndef NO_SHA256 |
4953 | | case sha256_mac: |
4954 | | digestType = WC_SHA256; |
4955 | | digestSize = WC_SHA256_DIGEST_SIZE; |
4956 | | break; |
4957 | | #endif /* !NO_SHA256 */ |
4958 | | #ifdef WOLFSSL_SHA384 |
4959 | | case sha384_mac: |
4960 | | digestType = WC_SHA384; |
4961 | | digestSize = WC_SHA384_DIGEST_SIZE; |
4962 | | break; |
4963 | | #endif /* WOLFSSL_SHA384 */ |
4964 | | #ifdef WOLFSSL_TLS13_SHA512 |
4965 | | case sha512_mac: |
4966 | | digestType = WC_SHA512; |
4967 | | digestSize = WC_SHA512_DIGEST_SIZE; |
4968 | | break; |
4969 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
4970 | | #ifdef WOLFSSL_SM3 |
4971 | | case sm3_mac: |
4972 | | digestType = WC_SM3; |
4973 | | digestSize = WC_SM3_DIGEST_SIZE; |
4974 | | break; |
4975 | | #endif /* WOLFSSL_SM3 */ |
4976 | | default: |
4977 | | ret = WOLFSSL_FATAL_ERROR; |
4978 | | break; |
4979 | | } |
4980 | | } |
4981 | | /* extract clientRandom with a key of all zeros */ |
4982 | | if (ret == 0) { |
4983 | | PRIVATE_KEY_UNLOCK(); |
4984 | | #if !defined(HAVE_FIPS) || \ |
4985 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
4986 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
4987 | | ssl->arrays->clientRandom, RAN_LEN, expandLabelPrk, |
4988 | | ssl->heap, ssl->devId); |
4989 | | #else |
4990 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4991 | | ssl->arrays->clientRandom, RAN_LEN, expandLabelPrk); |
4992 | | #endif |
4993 | | PRIVATE_KEY_LOCK(); |
4994 | | } |
4995 | | /* tls expand with the confirmation label */ |
4996 | | if (ret == 0) { |
4997 | | PRIVATE_KEY_UNLOCK(); |
4998 | | ret = Tls13HKDFExpandKeyLabel(ssl, output + acceptOffset, |
4999 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
5000 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
5001 | | transcriptEchConf, (word32)digestSize, digestType, |
5002 | | WOLFSSL_SERVER_END); |
5003 | | PRIVATE_KEY_LOCK(); |
5004 | | } |
5005 | | /* mark that ech was accepted */ |
5006 | | if (ret == 0 && msgType != hello_retry_request) |
5007 | | ssl->options.echAccepted = 1; |
5008 | | /* free hsHashesEch, if this is an HRR we will start at client hello 2*/ |
5009 | | FreeHandshakeHashes(ssl); |
5010 | | ssl->hsHashesEch = NULL; |
5011 | | ssl->hsHashes = tmpHashes; |
5012 | | return ret; |
5013 | | } |
5014 | | #endif |
5015 | | |
5016 | | /* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */ |
5017 | | /* Handle the ServerHello message from the server. |
5018 | | * Only a client will receive this message. |
5019 | | * |
5020 | | * ssl The SSL/TLS object. |
5021 | | * input The message buffer. |
5022 | | * inOutIdx On entry, the index into the message buffer of ServerHello. |
5023 | | * On exit, the index of byte after the ServerHello message. |
5024 | | * helloSz The length of the current handshake message. |
5025 | | * returns 0 on success and otherwise failure. |
5026 | | */ |
5027 | | |
5028 | | typedef struct Dsh13Args { |
5029 | | ProtocolVersion pv; |
5030 | | word32 idx; |
5031 | | word32 begin; |
5032 | | const byte* sessId; |
5033 | | word16 totalExtSz; |
5034 | | byte sessIdSz; |
5035 | | byte extMsgType; |
5036 | | #if defined(HAVE_ECH) |
5037 | | TLSX* echX; |
5038 | | byte* acceptLabel; |
5039 | | word32 acceptOffset; |
5040 | | word16 acceptLabelSz; |
5041 | | #endif |
5042 | | } Dsh13Args; |
5043 | | |
5044 | | int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
5045 | | word32 helloSz, byte* extMsgType) |
5046 | | { |
5047 | | int ret; |
5048 | | byte suite[2]; |
5049 | | byte tls12minor; |
5050 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5051 | | Dsh13Args* args = NULL; |
5052 | | #else |
5053 | | Dsh13Args args[1]; |
5054 | | #endif |
5055 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5056 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
5057 | | #endif |
5058 | | |
5059 | | WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO); |
5060 | | WOLFSSL_ENTER("DoTls13ServerHello"); |
5061 | | |
5062 | | if (ssl == NULL || ssl->arrays == NULL) |
5063 | | return BAD_FUNC_ARG; |
5064 | | |
5065 | | tls12minor = TLSv1_2_MINOR; |
5066 | | |
5067 | | #ifdef WOLFSSL_DTLS13 |
5068 | | if (ssl->options.dtls) |
5069 | | tls12minor = DTLSv1_2_MINOR; |
5070 | | #endif /* WOLFSSL_DTLS13 */ |
5071 | | |
5072 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5073 | | if (ssl->async == NULL) { |
5074 | | ssl->async = (struct WOLFSSL_ASYNC*) |
5075 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
5076 | | DYNAMIC_TYPE_ASYNC); |
5077 | | if (ssl->async == NULL) |
5078 | | return MEMORY_E; |
5079 | | ssl->async->freeArgs = NULL; |
5080 | | } |
5081 | | args = (Dsh13Args*)ssl->async->args; |
5082 | | |
5083 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
5084 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
5085 | | /* Check for error */ |
5086 | | if (ret < 0) { |
5087 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5088 | | /* Mark message as not received so it can process again */ |
5089 | | ssl->msgsReceived.got_server_hello = 0; |
5090 | | } |
5091 | | return ret; |
5092 | | } |
5093 | | } |
5094 | | else |
5095 | | #endif |
5096 | | { |
5097 | | /* Reset state */ |
5098 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
5099 | | XMEMSET(args, 0, sizeof(Dsh13Args)); |
5100 | | } |
5101 | | |
5102 | | switch (ssl->options.asyncState) { |
5103 | | case TLS_ASYNC_BEGIN: |
5104 | | { |
5105 | | byte b; |
5106 | | #ifdef WOLFSSL_CALLBACKS |
5107 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello"); |
5108 | | if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo); |
5109 | | #endif |
5110 | | |
5111 | | /* Protocol version length check. */ |
5112 | | if (helloSz < OPAQUE16_LEN) |
5113 | | return BUFFER_ERROR; |
5114 | | |
5115 | | args->idx = *inOutIdx; |
5116 | | args->begin = args->idx; |
5117 | | |
5118 | | /* Protocol version */ |
5119 | | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
5120 | | args->idx += OPAQUE16_LEN; |
5121 | | |
5122 | | #ifdef WOLFSSL_DTLS |
5123 | | if (ssl->options.dtls && |
5124 | | (args->pv.major != DTLS_MAJOR || args->pv.minor == DTLS_BOGUS_MINOR)) |
5125 | | return VERSION_ERROR; |
5126 | | #endif /* WOLFSSL_DTLS */ |
5127 | | |
5128 | | #ifndef WOLFSSL_NO_TLS12 |
5129 | | { |
5130 | | byte wantDowngrade; |
5131 | | |
5132 | | wantDowngrade = args->pv.major == ssl->version.major && |
5133 | | args->pv.minor < TLSv1_2_MINOR; |
5134 | | |
5135 | | #ifdef WOLFSSL_DTLS13 |
5136 | | if (ssl->options.dtls) |
5137 | | wantDowngrade = args->pv.major == ssl->version.major && |
5138 | | args->pv.minor > DTLSv1_2_MINOR; |
5139 | | #endif /* WOLFSSL_DTLS13 */ |
5140 | | |
5141 | | if (wantDowngrade && ssl->options.downgrade) { |
5142 | | /* Force client hello version 1.2 to work for static RSA. */ |
5143 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5144 | | ssl->version.minor = TLSv1_2_MINOR; |
5145 | | ssl->options.tls1_3 = 0; |
5146 | | |
5147 | | #ifdef WOLFSSL_DTLS13 |
5148 | | if (ssl->options.dtls) { |
5149 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5150 | | ssl->version.minor = DTLSv1_2_MINOR; |
5151 | | ret = Dtls13ClientDoDowngrade(ssl); |
5152 | | if (ret != 0) |
5153 | | return ret; |
5154 | | } |
5155 | | #endif /* WOLFSSL_DTLS13 */ |
5156 | | |
5157 | | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5158 | | } |
5159 | | } |
5160 | | #endif |
5161 | | |
5162 | | if (args->pv.major != ssl->version.major || |
5163 | | args->pv.minor != tls12minor) { |
5164 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5165 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5166 | | return VERSION_ERROR; |
5167 | | } |
5168 | | |
5169 | | /* Random and session id length check */ |
5170 | | if ((args->idx - args->begin) + RAN_LEN + ENUM_LEN > helloSz) |
5171 | | return BUFFER_ERROR; |
5172 | | |
5173 | | /* Check if hello retry request */ |
5174 | | if (XMEMCMP(input + args->idx, helloRetryRequestRandom, RAN_LEN) == 0) { |
5175 | | WOLFSSL_MSG("HelloRetryRequest format"); |
5176 | | *extMsgType = hello_retry_request; |
5177 | | |
5178 | | if (ssl->msgsReceived.got_hello_verify_request) { |
5179 | | WOLFSSL_MSG("Received HelloRetryRequest after a " |
5180 | | "HelloVerifyRequest"); |
5181 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5182 | | return VERSION_ERROR; |
5183 | | } |
5184 | | |
5185 | | /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat. |
5186 | | * Found message to be a HelloRetryRequest. |
5187 | | * Don't allow more than one HelloRetryRequest or ServerHello. |
5188 | | */ |
5189 | | if (ssl->msgsReceived.got_hello_retry_request) { |
5190 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
5191 | | return DUPLICATE_MSG_E; |
5192 | | } |
5193 | | } |
5194 | | args->extMsgType = *extMsgType; |
5195 | | |
5196 | | /* Server random - keep for debugging. */ |
5197 | | XMEMCPY(ssl->arrays->serverRandom, input + args->idx, RAN_LEN); |
5198 | | #if defined(HAVE_ECH) |
5199 | | /* last 8 bytes of server random */ |
5200 | | args->acceptOffset = args->idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
5201 | | #endif |
5202 | | args->idx += RAN_LEN; |
5203 | | |
5204 | | /* Session id */ |
5205 | | args->sessIdSz = input[args->idx++]; |
5206 | | if ((args->idx - args->begin) + args->sessIdSz > helloSz) |
5207 | | return BUFFER_ERROR; |
5208 | | args->sessId = input + args->idx; |
5209 | | args->idx += args->sessIdSz; |
5210 | | |
5211 | | ssl->options.haveSessionId = 1; |
5212 | | |
5213 | | /* Ciphersuite and compression check */ |
5214 | | if ((args->idx - args->begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz) |
5215 | | return BUFFER_ERROR; |
5216 | | |
5217 | | /* Set the cipher suite from the message. */ |
5218 | | ssl->options.cipherSuite0 = input[args->idx++]; |
5219 | | ssl->options.cipherSuite = input[args->idx++]; |
5220 | | #ifdef WOLFSSL_DEBUG_TLS |
5221 | | WOLFSSL_MSG("Chosen cipher suite:"); |
5222 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
5223 | | ssl->options.cipherSuite)); |
5224 | | #endif |
5225 | | |
5226 | | /* Compression */ |
5227 | | b = input[args->idx++]; |
5228 | | if (b != 0) { |
5229 | | WOLFSSL_MSG("Must be no compression types in list"); |
5230 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5231 | | return INVALID_PARAMETER; |
5232 | | } |
5233 | | |
5234 | | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) { |
5235 | | if (!ssl->options.downgrade) |
5236 | | return BUFFER_ERROR; |
5237 | | #ifndef WOLFSSL_NO_TLS12 |
5238 | | /* Force client hello version 1.2 to work for static RSA. */ |
5239 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5240 | | ssl->version.minor = TLSv1_2_MINOR; |
5241 | | |
5242 | | #ifdef WOLFSSL_DTLS13 |
5243 | | if (ssl->options.dtls) { |
5244 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5245 | | ssl->version.minor = DTLSv1_2_MINOR; |
5246 | | ssl->options.tls1_3 = 0; |
5247 | | ret = Dtls13ClientDoDowngrade(ssl); |
5248 | | if (ret != 0) |
5249 | | return ret; |
5250 | | } |
5251 | | #endif /* WOLFSSL_DTLS13 */ |
5252 | | |
5253 | | #endif |
5254 | | ssl->options.haveEMS = 0; |
5255 | | if (args->pv.minor < ssl->options.minDowngrade) { |
5256 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5257 | | return VERSION_ERROR; |
5258 | | } |
5259 | | #ifndef WOLFSSL_NO_TLS12 |
5260 | | ssl->options.tls1_3 = 0; |
5261 | | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5262 | | #else |
5263 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5264 | | return VERSION_ERROR; |
5265 | | #endif |
5266 | | } |
5267 | | |
5268 | | if ((args->idx - args->begin) < helloSz) { |
5269 | | int foundVersion; |
5270 | | |
5271 | | /* Get extension length and length check. */ |
5272 | | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
5273 | | return BUFFER_ERROR; |
5274 | | ato16(&input[args->idx], &args->totalExtSz); |
5275 | | args->idx += OPAQUE16_LEN; |
5276 | | if ((args->idx - args->begin) + args->totalExtSz > helloSz) |
5277 | | return BUFFER_ERROR; |
5278 | | |
5279 | | /* Need to negotiate version first. */ |
5280 | | if ((ret = TLSX_ParseVersion(ssl, input + args->idx, |
5281 | | args->totalExtSz, *extMsgType, &foundVersion))) { |
5282 | | return ret; |
5283 | | } |
5284 | | if (!foundVersion) { |
5285 | | if (!ssl->options.downgrade) { |
5286 | | WOLFSSL_MSG("Server trying to downgrade to version less than " |
5287 | | "TLS v1.3"); |
5288 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5289 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5290 | | return VERSION_ERROR; |
5291 | | } |
5292 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \ |
5293 | | defined(WOLFSSL_WPAS_SMALL) |
5294 | | /* Check if client has disabled TLS 1.2 */ |
5295 | | if (args->pv.minor == TLSv1_2_MINOR && |
5296 | | (ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) |
5297 | | == WOLFSSL_OP_NO_TLSv1_2) |
5298 | | { |
5299 | | WOLFSSL_MSG("\tOption set to not allow TLSv1.2"); |
5300 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5301 | | return VERSION_ERROR; |
5302 | | } |
5303 | | #endif |
5304 | | |
5305 | | if (!ssl->options.dtls && |
5306 | | args->pv.minor < ssl->options.minDowngrade) { |
5307 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5308 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5309 | | return VERSION_ERROR; |
5310 | | } |
5311 | | |
5312 | | if (ssl->options.dtls && |
5313 | | args->pv.minor > ssl->options.minDowngrade) { |
5314 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5315 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5316 | | return VERSION_ERROR; |
5317 | | } |
5318 | | |
5319 | | ssl->version.minor = args->pv.minor; |
5320 | | ssl->options.tls1_3 = 0; |
5321 | | |
5322 | | #ifdef WOLFSSL_DTLS13 |
5323 | | if (ssl->options.dtls) { |
5324 | | ret = Dtls13ClientDoDowngrade(ssl); |
5325 | | if (ret != 0) |
5326 | | return ret; |
5327 | | } |
5328 | | #endif /* WOLFSSL_DTLS13 */ |
5329 | | } |
5330 | | } |
5331 | | |
5332 | | #ifdef WOLFSSL_DTLS13 |
5333 | | /* we are sure that version is >= v1.3 now, we can get rid of buffered |
5334 | | * ClientHello that was buffered to re-compute the hash in case of |
5335 | | * downgrade */ |
5336 | | if (ssl->options.dtls && ssl->dtls13ClientHello != NULL) { |
5337 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5338 | | ssl->dtls13ClientHello = NULL; |
5339 | | ssl->dtls13ClientHelloSz = 0; |
5340 | | } |
5341 | | #endif /* WOLFSSL_DTLS13 */ |
5342 | | |
5343 | | /* Advance state and proceed */ |
5344 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
5345 | | } /* case TLS_ASYNC_BEGIN */ |
5346 | | FALL_THROUGH; |
5347 | | |
5348 | | case TLS_ASYNC_BUILD: |
5349 | | case TLS_ASYNC_DO: |
5350 | | { |
5351 | | /* restore message type */ |
5352 | | *extMsgType = args->extMsgType; |
5353 | | |
5354 | | /* Parse and handle extensions, unless lower than TLS1.3. In that case, |
5355 | | * extensions will be parsed in DoServerHello. */ |
5356 | | if (args->totalExtSz > 0 && IsAtLeastTLSv1_3(ssl->version)) { |
5357 | | ret = TLSX_Parse(ssl, input + args->idx, args->totalExtSz, |
5358 | | *extMsgType, NULL); |
5359 | | if (ret != 0) { |
5360 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5361 | | /* Handle async operation */ |
5362 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5363 | | /* Mark message as not received so it can process again */ |
5364 | | ssl->msgsReceived.got_server_hello = 0; |
5365 | | } |
5366 | | #endif |
5367 | | return ret; |
5368 | | } |
5369 | | |
5370 | | if (*extMsgType == hello_retry_request) { |
5371 | | /* Update counts to reflect change of message type. */ |
5372 | | ssl->msgsReceived.got_hello_retry_request = 1; |
5373 | | ssl->msgsReceived.got_server_hello = 0; |
5374 | | } |
5375 | | } |
5376 | | |
5377 | | if (args->totalExtSz > 0) { |
5378 | | args->idx += args->totalExtSz; |
5379 | | } |
5380 | | |
5381 | | #ifdef WOLFSSL_DTLS_CID |
5382 | | if (ssl->options.useDtlsCID && *extMsgType == server_hello) |
5383 | | DtlsCIDOnExtensionsParsed(ssl); |
5384 | | #endif /* WOLFSSL_DTLS_CID */ |
5385 | | |
5386 | | if (IsAtLeastTLSv1_3(ssl->version)) { |
5387 | | *inOutIdx = args->idx; |
5388 | | } |
5389 | | |
5390 | | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5391 | | |
5392 | | #ifdef HAVE_SECRET_CALLBACK |
5393 | | if (ssl->sessionSecretCb != NULL |
5394 | | #ifdef HAVE_SESSION_TICKET |
5395 | | && ssl->session->ticketLen > 0 |
5396 | | #endif |
5397 | | ) { |
5398 | | int secretSz = SECRET_LEN; |
5399 | | ret = ssl->sessionSecretCb(ssl, ssl->session->masterSecret, |
5400 | | &secretSz, ssl->sessionSecretCtx); |
5401 | | if (ret != 0 || secretSz != SECRET_LEN) { |
5402 | | WOLFSSL_ERROR_VERBOSE(SESSION_SECRET_CB_E); |
5403 | | return SESSION_SECRET_CB_E; |
5404 | | } |
5405 | | } |
5406 | | #endif /* HAVE_SECRET_CALLBACK */ |
5407 | | |
5408 | | /* Version only negotiated in extensions for TLS v1.3. |
5409 | | * Only now do we know how to deal with session id. |
5410 | | */ |
5411 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
5412 | | #ifndef WOLFSSL_NO_TLS12 |
5413 | | ssl->arrays->sessionIDSz = args->sessIdSz; |
5414 | | |
5415 | | if (ssl->arrays->sessionIDSz > ID_LEN) { |
5416 | | WOLFSSL_MSG("Invalid session ID size"); |
5417 | | ssl->arrays->sessionIDSz = 0; |
5418 | | return BUFFER_ERROR; |
5419 | | } |
5420 | | else if (ssl->arrays->sessionIDSz) { |
5421 | | XMEMCPY(ssl->arrays->sessionID, args->sessId, |
5422 | | ssl->arrays->sessionIDSz); |
5423 | | ssl->options.haveSessionId = 1; |
5424 | | } |
5425 | | |
5426 | | /* Force client hello version 1.2 to work for static RSA. */ |
5427 | | if (ssl->options.dtls) |
5428 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5429 | | else |
5430 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5431 | | /* Complete TLS v1.2 processing of ServerHello. */ |
5432 | | ret = DoServerHello(ssl, input, inOutIdx, helloSz); |
5433 | | #else |
5434 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
5435 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5436 | | ret = VERSION_ERROR; |
5437 | | #endif |
5438 | | |
5439 | | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5440 | | |
5441 | | return ret; |
5442 | | } |
5443 | | |
5444 | | /* Advance state and proceed */ |
5445 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
5446 | | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
5447 | | FALL_THROUGH; |
5448 | | |
5449 | | case TLS_ASYNC_FINALIZE: |
5450 | | { |
5451 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
5452 | | if (ssl->options.tls13MiddleBoxCompat) { |
5453 | | if (args->sessIdSz == 0) { |
5454 | | WOLFSSL_MSG("args->sessIdSz == 0"); |
5455 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5456 | | return INVALID_PARAMETER; |
5457 | | } |
5458 | | if (ssl->session->sessionIDSz != 0) { |
5459 | | if (ssl->session->sessionIDSz != args->sessIdSz || |
5460 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5461 | | args->sessIdSz) != 0) { |
5462 | | WOLFSSL_MSG("session id doesn't match"); |
5463 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5464 | | return INVALID_PARAMETER; |
5465 | | } |
5466 | | } |
5467 | | else if (XMEMCMP(ssl->arrays->clientRandom, args->sessId, |
5468 | | args->sessIdSz) != 0) { |
5469 | | WOLFSSL_MSG("session id doesn't match client random"); |
5470 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5471 | | return INVALID_PARAMETER; |
5472 | | } |
5473 | | } |
5474 | | else |
5475 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
5476 | | #ifdef WOLFSSL_QUIC |
5477 | | if (WOLFSSL_IS_QUIC(ssl)) { |
5478 | | if (args->sessIdSz != 0) { |
5479 | | WOLFSSL_MSG("args->sessIdSz != 0"); |
5480 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5481 | | return INVALID_PARAMETER; |
5482 | | } |
5483 | | } |
5484 | | else |
5485 | | #endif /* WOLFSSL_QUIC */ |
5486 | | if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 && |
5487 | | XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0)) |
5488 | | { |
5489 | | WOLFSSL_MSG("Server sent different session id"); |
5490 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5491 | | return INVALID_PARAMETER; |
5492 | | } |
5493 | | |
5494 | | ret = SetCipherSpecs(ssl); |
5495 | | if (ret != 0) |
5496 | | return ret; |
5497 | | |
5498 | | #ifdef HAVE_NULL_CIPHER |
5499 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
5500 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
5501 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
5502 | | ; |
5503 | | } |
5504 | | else |
5505 | | #endif |
5506 | | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
5507 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5508 | | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
5509 | | ; /* Do nothing. */ |
5510 | | } |
5511 | | else |
5512 | | #endif |
5513 | | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
5514 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5515 | | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
5516 | | ; /* Do nothing. */ |
5517 | | } |
5518 | | else |
5519 | | #endif |
5520 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
5521 | | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
5522 | | WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet"); |
5523 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5524 | | return INVALID_PARAMETER; |
5525 | | } |
5526 | | |
5527 | | suite[0] = ssl->options.cipherSuite0; |
5528 | | suite[1] = ssl->options.cipherSuite; |
5529 | | if (!FindSuiteSSL(ssl, suite)) { |
5530 | | WOLFSSL_MSG("Cipher suite not supported on client"); |
5531 | | WOLFSSL_ERROR_VERBOSE(MATCH_SUITE_ERROR); |
5532 | | return MATCH_SUITE_ERROR; |
5533 | | } |
5534 | | |
5535 | | #if defined(HAVE_ECH) |
5536 | | /* check for acceptConfirmation, must be done after hashes restart */ |
5537 | | if (ssl->options.useEch == 1) { |
5538 | | args->echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
5539 | | /* account for hrr extension instead of server random */ |
5540 | | if (args->extMsgType == hello_retry_request) { |
5541 | | args->acceptOffset = |
5542 | | (word32)(((WOLFSSL_ECH*)args->echX->data)->confBuf - input); |
5543 | | args->acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
5544 | | args->acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
5545 | | } |
5546 | | else { |
5547 | | args->acceptLabel = (byte*)echAcceptConfirmationLabel; |
5548 | | args->acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
5549 | | } |
5550 | | /* check acceptance */ |
5551 | | if (ret == 0) { |
5552 | | ret = EchCheckAcceptance(ssl, args->acceptLabel, |
5553 | | args->acceptLabelSz, input, args->acceptOffset, helloSz); |
5554 | | } |
5555 | | if (ret != 0) |
5556 | | return ret; |
5557 | | /* use the inner random for client random */ |
5558 | | if (args->extMsgType != hello_retry_request) { |
5559 | | XMEMCPY(ssl->arrays->clientRandom, ssl->arrays->clientRandomInner, |
5560 | | RAN_LEN); |
5561 | | } |
5562 | | } |
5563 | | #endif /* HAVE_ECH */ |
5564 | | |
5565 | | if (*extMsgType == server_hello) { |
5566 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5567 | | PreSharedKey* psk = NULL; |
5568 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
5569 | | if (ext != NULL) |
5570 | | psk = (PreSharedKey*)ext->data; |
5571 | | while (psk != NULL && !psk->chosen) |
5572 | | psk = psk->next; |
5573 | | if (psk == NULL) { |
5574 | | ssl->options.resuming = 0; |
5575 | | ssl->arrays->psk_keySz = 0; |
5576 | | XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); |
5577 | | } |
5578 | | else { |
5579 | | if ((ret = SetupPskKey(ssl, psk, 0)) != 0) |
5580 | | return ret; |
5581 | | ssl->options.pskNegotiated = 1; |
5582 | | } |
5583 | | #else |
5584 | | /* no resumption possible */ |
5585 | | ssl->options.resuming = 0; |
5586 | | #endif |
5587 | | |
5588 | | /* sanity check on PSK / KSE */ |
5589 | | if ( |
5590 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5591 | | ssl->options.pskNegotiated == 0 && |
5592 | | #endif |
5593 | | ssl->session->namedGroup == 0) { |
5594 | | return EXT_MISSING; |
5595 | | } |
5596 | | |
5597 | | ssl->keys.encryptionOn = 1; |
5598 | | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5599 | | |
5600 | | } |
5601 | | else { |
5602 | | ssl->options.tls1_3 = 1; |
5603 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
5604 | | |
5605 | | ret = RestartHandshakeHash(ssl); |
5606 | | } |
5607 | | |
5608 | | break; |
5609 | | } /* case TLS_ASYNC_FINALIZE */ |
5610 | | default: |
5611 | | ret = INPUT_CASE_ERROR; |
5612 | | } /* switch (ssl->options.asyncState) */ |
5613 | | |
5614 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5615 | | if (ret == 0) |
5616 | | FreeAsyncCtx(ssl, 0); |
5617 | | #endif |
5618 | | |
5619 | | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5620 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO); |
5621 | | |
5622 | | return ret; |
5623 | | } |
5624 | | |
5625 | | /* handle processing TLS 1.3 encrypted_extensions (8) */ |
5626 | | /* Parse and handle an EncryptedExtensions message. |
5627 | | * Only a client will receive this message. |
5628 | | * |
5629 | | * ssl The SSL/TLS object. |
5630 | | * input The message buffer. |
5631 | | * inOutIdx On entry, the index into the message buffer of |
5632 | | * EncryptedExtensions. |
5633 | | * On exit, the index of byte after the EncryptedExtensions |
5634 | | * message. |
5635 | | * totalSz The length of the current handshake message. |
5636 | | * returns 0 on success and otherwise failure. |
5637 | | */ |
5638 | | static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input, |
5639 | | word32* inOutIdx, word32 totalSz) |
5640 | 0 | { |
5641 | 0 | int ret; |
5642 | 0 | word32 begin = *inOutIdx; |
5643 | 0 | word32 i = begin; |
5644 | 0 | word16 totalExtSz; |
5645 | |
|
5646 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5647 | 0 | WOLFSSL_ENTER("DoTls13EncryptedExtensions"); |
5648 | |
|
5649 | | #ifdef WOLFSSL_CALLBACKS |
5650 | | if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions"); |
5651 | | if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo); |
5652 | | #endif |
5653 | | |
5654 | | /* Length field of extension data. */ |
5655 | 0 | if (totalSz < OPAQUE16_LEN) |
5656 | 0 | return BUFFER_ERROR; |
5657 | 0 | ato16(&input[i], &totalExtSz); |
5658 | 0 | i += OPAQUE16_LEN; |
5659 | | |
5660 | | /* Extension data. */ |
5661 | 0 | if (i - begin + totalExtSz > totalSz) |
5662 | 0 | return BUFFER_ERROR; |
5663 | 0 | if ((ret = TLSX_Parse(ssl, input + i, totalExtSz, encrypted_extensions, |
5664 | 0 | NULL))) { |
5665 | 0 | return ret; |
5666 | 0 | } |
5667 | | |
5668 | | /* Move index to byte after message. */ |
5669 | 0 | *inOutIdx = i + totalExtSz; |
5670 | | |
5671 | | /* Always encrypted. */ |
5672 | 0 | *inOutIdx += ssl->keys.padSz; |
5673 | |
|
5674 | | #ifdef WOLFSSL_EARLY_DATA |
5675 | | if (ssl->earlyData != no_early_data) { |
5676 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
5677 | | if (ext == NULL || !ext->val) |
5678 | | ssl->earlyData = no_early_data; |
5679 | | } |
5680 | | #endif |
5681 | |
|
5682 | | #ifdef WOLFSSL_EARLY_DATA |
5683 | | if (ssl->earlyData == no_early_data) { |
5684 | | ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY); |
5685 | | if (ret != 0) |
5686 | | return ret; |
5687 | | } |
5688 | | #endif |
5689 | |
|
5690 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
5691 | |
|
5692 | 0 | WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret); |
5693 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5694 | |
|
5695 | 0 | return ret; |
5696 | 0 | } |
5697 | | |
5698 | | #ifndef NO_CERTS |
5699 | | /* handle processing TLS v1.3 certificate_request (13) */ |
5700 | | /* Handle a TLS v1.3 CertificateRequest message. |
5701 | | * This message is always encrypted. |
5702 | | * Only a client will receive this message. |
5703 | | * |
5704 | | * ssl The SSL/TLS object. |
5705 | | * input The message buffer. |
5706 | | * inOutIdx On entry, the index into the message buffer of CertificateRequest. |
5707 | | * On exit, the index of byte after the CertificateRequest message. |
5708 | | * size The length of the current handshake message. |
5709 | | * returns 0 on success and otherwise failure. |
5710 | | */ |
5711 | | static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input, |
5712 | | word32* inOutIdx, word32 size) |
5713 | 0 | { |
5714 | 0 | word16 len; |
5715 | 0 | word32 begin = *inOutIdx; |
5716 | 0 | int ret = 0; |
5717 | 0 | Suites peerSuites; |
5718 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5719 | | CertReqCtx* certReqCtx; |
5720 | | #endif |
5721 | |
|
5722 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5723 | 0 | WOLFSSL_ENTER("DoTls13CertificateRequest"); |
5724 | |
|
5725 | 0 | XMEMSET(&peerSuites, 0, sizeof(Suites)); |
5726 | |
|
5727 | | #ifdef WOLFSSL_CALLBACKS |
5728 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest"); |
5729 | | if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo); |
5730 | | #endif |
5731 | |
|
5732 | | #ifdef OPENSSL_EXTRA |
5733 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
5734 | | return ret; |
5735 | | #endif |
5736 | |
|
5737 | 0 | if (OPAQUE8_LEN > size) |
5738 | 0 | return BUFFER_ERROR; |
5739 | | |
5740 | | /* Length of the request context. */ |
5741 | 0 | len = input[(*inOutIdx)++]; |
5742 | 0 | if ((*inOutIdx - begin) + len > size) |
5743 | 0 | return BUFFER_ERROR; |
5744 | 0 | if (ssl->options.connectState < FINISHED_DONE && len > 0) |
5745 | 0 | return BUFFER_ERROR; |
5746 | | |
5747 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5748 | | /* CertReqCtx has one byte at end for context value. |
5749 | | * Increase size to handle other implementations sending more than one byte. |
5750 | | * That is, allocate extra space, over one byte, to hold the context value. |
5751 | | */ |
5752 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx) + len - 1, ssl->heap, |
5753 | | DYNAMIC_TYPE_TMP_BUFFER); |
5754 | | if (certReqCtx == NULL) |
5755 | | return MEMORY_E; |
5756 | | certReqCtx->next = ssl->certReqCtx; |
5757 | | certReqCtx->len = len; |
5758 | | XMEMCPY(&certReqCtx->ctx, input + *inOutIdx, len); |
5759 | | ssl->certReqCtx = certReqCtx; |
5760 | | #endif |
5761 | 0 | *inOutIdx += len; |
5762 | | |
5763 | | /* TODO: Add support for more extensions: |
5764 | | * signed_certificate_timestamp, certificate_authorities, oid_filters. |
5765 | | */ |
5766 | | /* Certificate extensions */ |
5767 | 0 | if ((*inOutIdx - begin) + OPAQUE16_LEN > size) |
5768 | 0 | return BUFFER_ERROR; |
5769 | 0 | ato16(input + *inOutIdx, &len); |
5770 | 0 | *inOutIdx += OPAQUE16_LEN; |
5771 | 0 | if ((*inOutIdx - begin) + len > size) |
5772 | 0 | return BUFFER_ERROR; |
5773 | 0 | if (len == 0) |
5774 | 0 | return INVALID_PARAMETER; |
5775 | 0 | if ((ret = TLSX_Parse(ssl, input + *inOutIdx, len, certificate_request, |
5776 | 0 | &peerSuites))) { |
5777 | 0 | return ret; |
5778 | 0 | } |
5779 | 0 | *inOutIdx += len; |
5780 | |
|
5781 | 0 | if ((ssl->buffers.certificate && ssl->buffers.certificate->buffer && |
5782 | 0 | ((ssl->buffers.key && ssl->buffers.key->buffer) |
5783 | | #ifdef HAVE_PK_CALLBACKS |
5784 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
5785 | | #endif |
5786 | 0 | )) |
5787 | | #ifdef OPENSSL_EXTRA |
5788 | | || ssl->ctx->certSetupCb != NULL |
5789 | | #endif |
5790 | 0 | ) { |
5791 | 0 | if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo, |
5792 | 0 | peerSuites.hashSigAlgoSz, 0) != 0) { |
5793 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5794 | 0 | return INVALID_PARAMETER; |
5795 | 0 | } |
5796 | 0 | ssl->options.sendVerify = SEND_CERT; |
5797 | 0 | } |
5798 | 0 | else { |
5799 | 0 | #ifndef WOLFSSL_NO_CLIENT_CERT_ERROR |
5800 | 0 | ssl->options.sendVerify = SEND_BLANK_CERT; |
5801 | | #else |
5802 | | WOLFSSL_MSG("Certificate required but none set on client"); |
5803 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
5804 | | WOLFSSL_ERROR_VERBOSE(NO_CERT_ERROR); |
5805 | | return NO_CERT_ERROR; |
5806 | | #endif |
5807 | 0 | } |
5808 | | |
5809 | | /* This message is always encrypted so add encryption padding. */ |
5810 | 0 | *inOutIdx += ssl->keys.padSz; |
5811 | |
|
5812 | 0 | WOLFSSL_LEAVE("DoTls13CertificateRequest", ret); |
5813 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5814 | |
|
5815 | 0 | return ret; |
5816 | 0 | } |
5817 | | #endif /* !NO_CERTS */ |
5818 | | #endif /* !NO_WOLFSSL_CLIENT */ |
5819 | | |
5820 | | #ifndef NO_WOLFSSL_SERVER |
5821 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5822 | | /* Refine list of supported cipher suites to those common to server and client. |
5823 | | * |
5824 | | * ssl SSL/TLS object. |
5825 | | * peerSuites The peer's advertised list of supported cipher suites. |
5826 | | */ |
5827 | | static void RefineSuites(WOLFSSL* ssl, Suites* peerSuites) |
5828 | | { |
5829 | | byte suites[WOLFSSL_MAX_SUITE_SZ]; |
5830 | | word16 suiteSz = 0; |
5831 | | word16 i; |
5832 | | word16 j; |
5833 | | |
5834 | | if (AllocateSuites(ssl) != 0) |
5835 | | return; |
5836 | | |
5837 | | XMEMSET(suites, 0, sizeof(suites)); |
5838 | | |
5839 | | if (!ssl->options.useClientOrder) { |
5840 | | /* Server order refining. */ |
5841 | | for (i = 0; i < ssl->suites->suiteSz; i += 2) { |
5842 | | for (j = 0; j < peerSuites->suiteSz; j += 2) { |
5843 | | if ((ssl->suites->suites[i+0] == peerSuites->suites[j+0]) && |
5844 | | (ssl->suites->suites[i+1] == peerSuites->suites[j+1])) { |
5845 | | suites[suiteSz++] = peerSuites->suites[j+0]; |
5846 | | suites[suiteSz++] = peerSuites->suites[j+1]; |
5847 | | break; |
5848 | | } |
5849 | | } |
5850 | | if (suiteSz == WOLFSSL_MAX_SUITE_SZ) |
5851 | | break; |
5852 | | } |
5853 | | } |
5854 | | else { |
5855 | | /* Client order refining. */ |
5856 | | for (j = 0; j < peerSuites->suiteSz; j += 2) { |
5857 | | for (i = 0; i < ssl->suites->suiteSz; i += 2) { |
5858 | | if ((ssl->suites->suites[i+0] == peerSuites->suites[j+0]) && |
5859 | | (ssl->suites->suites[i+1] == peerSuites->suites[j+1])) { |
5860 | | suites[suiteSz++] = peerSuites->suites[j+0]; |
5861 | | suites[suiteSz++] = peerSuites->suites[j+1]; |
5862 | | break; |
5863 | | } |
5864 | | } |
5865 | | if (suiteSz == WOLFSSL_MAX_SUITE_SZ) |
5866 | | break; |
5867 | | } |
5868 | | } |
5869 | | |
5870 | | ssl->suites->suiteSz = suiteSz; |
5871 | | XMEMCPY(ssl->suites->suites, &suites, sizeof(suites)); |
5872 | | #ifdef WOLFSSL_DEBUG_TLS |
5873 | | { |
5874 | | int ii; |
5875 | | WOLFSSL_MSG("Refined Ciphers:"); |
5876 | | for (ii = 0 ; ii < ssl->suites->suiteSz; ii += 2) { |
5877 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->suites->suites[ii+0], |
5878 | | ssl->suites->suites[ii+1])); |
5879 | | } |
5880 | | } |
5881 | | #endif |
5882 | | } |
5883 | | |
5884 | | |
5885 | | #ifndef NO_PSK |
5886 | | int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key, |
5887 | | word32* psk_keySz, const byte* suite, int* found, byte* foundSuite) |
5888 | | { |
5889 | | const char* cipherName = NULL; |
5890 | | byte cipherSuite0 = TLS13_BYTE; |
5891 | | byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
5892 | | int ret = 0; |
5893 | | |
5894 | | *found = 0; |
5895 | | (void)suite; |
5896 | | |
5897 | | if (ssl->options.server_psk_tls13_cb != NULL) { |
5898 | | *psk_keySz = ssl->options.server_psk_tls13_cb((WOLFSSL*)ssl, |
5899 | | (char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName); |
5900 | | if (*psk_keySz != 0) { |
5901 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
5902 | | *found = (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
5903 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0); |
5904 | | (void)cipherSuiteFlags; |
5905 | | } |
5906 | | } |
5907 | | if (*found == 0 && (ssl->options.server_psk_cb != NULL)) { |
5908 | | *psk_keySz = ssl->options.server_psk_cb((WOLFSSL*)ssl, |
5909 | | (char*)psk->identity, psk_key, |
5910 | | MAX_PSK_KEY_LEN); |
5911 | | *found = (*psk_keySz != 0); |
5912 | | } |
5913 | | if (*found) { |
5914 | | if (*psk_keySz > MAX_PSK_KEY_LEN && |
5915 | | *((int*)psk_keySz) != WC_NO_ERR_TRACE(USE_HW_PSK)) { |
5916 | | WOLFSSL_MSG("Key len too long in FindPsk()"); |
5917 | | ret = PSK_KEY_ERROR; |
5918 | | WOLFSSL_ERROR_VERBOSE(ret); |
5919 | | *found = 0; |
5920 | | } |
5921 | | if (ret == 0) { |
5922 | | #if !defined(WOLFSSL_PSK_ONE_ID) && !defined(WOLFSSL_PRIORITIZE_PSK) |
5923 | | /* Check whether PSK ciphersuite is in SSL. */ |
5924 | | *found = (suite[0] == cipherSuite0) && (suite[1] == cipherSuite); |
5925 | | #else |
5926 | | (void)suite; |
5927 | | /* Check whether PSK ciphersuite is in SSL. */ |
5928 | | { |
5929 | | byte s[2] = { |
5930 | | cipherSuite0, |
5931 | | cipherSuite, |
5932 | | }; |
5933 | | *found = FindSuiteSSL(ssl, s); |
5934 | | } |
5935 | | #endif |
5936 | | } |
5937 | | } |
5938 | | if (*found && foundSuite != NULL) { |
5939 | | foundSuite[0] = cipherSuite0; |
5940 | | foundSuite[1] = cipherSuite; |
5941 | | } |
5942 | | |
5943 | | return ret; |
5944 | | } |
5945 | | |
5946 | | /* Attempt to find the PSK (not session ticket) that matches. |
5947 | | * |
5948 | | * @param [in, out] ssl The SSL/TLS object. |
5949 | | * @param [in] psk A pre-shared key from the extension. |
5950 | | * @param [out] suite Cipher suite to use with PSK. |
5951 | | * @param [out] err Error code. |
5952 | | * PSK_KEY_ERROR when key is too big or ticket age is |
5953 | | * invalid, |
5954 | | * UNSUPPORTED_SUITE on invalid suite. |
5955 | | * Other error when attempting to derive early secret. |
5956 | | * @return 1 when a match found - but check error code. |
5957 | | * @return 0 when no match found. |
5958 | | */ |
5959 | | static int FindPsk(WOLFSSL* ssl, PreSharedKey* psk, const byte* suite, int* err) |
5960 | | { |
5961 | | int ret = 0; |
5962 | | int found = 0; |
5963 | | byte foundSuite[SUITE_LEN]; |
5964 | | |
5965 | | WOLFSSL_ENTER("FindPsk"); |
5966 | | |
5967 | | XMEMSET(foundSuite, 0, sizeof(foundSuite)); |
5968 | | |
5969 | | ret = FindPskSuite(ssl, psk, ssl->arrays->psk_key, &ssl->arrays->psk_keySz, |
5970 | | suite, &found, foundSuite); |
5971 | | if (ret == 0 && found) { |
5972 | | /* Default to ciphersuite if cb doesn't specify. */ |
5973 | | ssl->options.resuming = 0; |
5974 | | /* Don't send certificate request when using PSK. */ |
5975 | | ssl->options.verifyPeer = 0; |
5976 | | |
5977 | | /* PSK age is always zero. */ |
5978 | | if (psk->ticketAge != 0) { |
5979 | | ret = PSK_KEY_ERROR; |
5980 | | WOLFSSL_ERROR_VERBOSE(ret); |
5981 | | } |
5982 | | if (ret == 0) { |
5983 | | /* Set PSK ciphersuite into SSL. */ |
5984 | | ssl->options.cipherSuite0 = foundSuite[0]; |
5985 | | ssl->options.cipherSuite = foundSuite[1]; |
5986 | | ret = SetCipherSpecs(ssl); |
5987 | | } |
5988 | | if (ret == 0) { |
5989 | | /* Derive the early secret using the PSK. */ |
5990 | | ret = DeriveEarlySecret(ssl); |
5991 | | } |
5992 | | if (ret == 0) { |
5993 | | /* PSK negotiation has succeeded */ |
5994 | | ssl->options.isPSK = 1; |
5995 | | /* SERVER: using PSK for peer authentication. */ |
5996 | | ssl->options.peerAuthGood = 1; |
5997 | | } |
5998 | | } |
5999 | | |
6000 | | *err = ret; |
6001 | | WOLFSSL_LEAVE("FindPsk", found); |
6002 | | WOLFSSL_LEAVE("FindPsk", ret); |
6003 | | return found; |
6004 | | } |
6005 | | #endif /* !NO_PSK */ |
6006 | | |
6007 | | /* Handle any Pre-Shared Key (PSK) extension. |
6008 | | * Find a PSK that supports the cipher suite passed in. |
6009 | | * |
6010 | | * ssl SSL/TLS object. |
6011 | | * suite Cipher suite to find PSK for. |
6012 | | * usingPSK 1=Indicates handshake is using Pre-Shared Keys (2=Ephemeral) |
6013 | | * first Set to 1 if first in extension |
6014 | | * returns 0 on success and otherwise failure. |
6015 | | */ |
6016 | | static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz, |
6017 | | const byte* suite, int* usingPSK, int* first) |
6018 | | { |
6019 | | int ret = 0; |
6020 | | TLSX* ext; |
6021 | | PreSharedKey* current; |
6022 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
6023 | | byte binder[WC_MAX_DIGEST_SIZE]; |
6024 | | word32 binderLen; |
6025 | | |
6026 | | #ifdef NO_PSK |
6027 | | (void) suite; /* to avoid unused var warning when not used */ |
6028 | | #endif |
6029 | | |
6030 | | WOLFSSL_ENTER("DoPreSharedKeys"); |
6031 | | |
6032 | | (void)suite; |
6033 | | |
6034 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6035 | | if (ext == NULL) { |
6036 | | WOLFSSL_MSG("No pre shared extension keys found"); |
6037 | | return BAD_FUNC_ARG; |
6038 | | } |
6039 | | |
6040 | | /* Look through all client's pre-shared keys for a match. */ |
6041 | | for (current = (PreSharedKey*)ext->data; current != NULL; |
6042 | | current = current->next) { |
6043 | | #ifndef NO_PSK |
6044 | | if (current->identityLen > MAX_PSK_ID_LEN) { |
6045 | | return BUFFER_ERROR; |
6046 | | } |
6047 | | XMEMCPY(ssl->arrays->client_identity, current->identity, |
6048 | | current->identityLen); |
6049 | | ssl->arrays->client_identity[current->identityLen] = '\0'; |
6050 | | #endif |
6051 | | |
6052 | | #ifdef HAVE_SESSION_TICKET |
6053 | | /* Decode the identity. */ |
6054 | | switch (current->decryptRet) { |
6055 | | case PSK_DECRYPT_NONE: |
6056 | | ret = DoClientTicket_ex(ssl, current, 1); |
6057 | | /* psk->sess may be set. Need to clean up later. */ |
6058 | | break; |
6059 | | case PSK_DECRYPT_OK: |
6060 | | ret = WOLFSSL_TICKET_RET_OK; |
6061 | | break; |
6062 | | case PSK_DECRYPT_CREATE: |
6063 | | ret = WOLFSSL_TICKET_RET_CREATE; |
6064 | | break; |
6065 | | case PSK_DECRYPT_FAIL: |
6066 | | ret = WOLFSSL_TICKET_RET_REJECT; |
6067 | | break; |
6068 | | } |
6069 | | |
6070 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6071 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
6072 | | return ret; |
6073 | | #endif |
6074 | | |
6075 | | if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) { |
6076 | | current->sess_free_cb(ssl, current->sess, |
6077 | | ¤t->sess_free_cb_ctx); |
6078 | | current->sess = NULL; |
6079 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6080 | | sizeof(psk_sess_free_cb_ctx)); |
6081 | | } |
6082 | | if (ret == WOLFSSL_TICKET_RET_OK) { |
6083 | | ret = DoClientTicketCheck(ssl, current, ssl->timeout, suite); |
6084 | | if (ret == 0) |
6085 | | DoClientTicketFinalize(ssl, current->it, current->sess); |
6086 | | if (current->sess_free_cb != NULL) { |
6087 | | current->sess_free_cb(ssl, current->sess, |
6088 | | ¤t->sess_free_cb_ctx); |
6089 | | current->sess = NULL; |
6090 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6091 | | sizeof(psk_sess_free_cb_ctx)); |
6092 | | } |
6093 | | if (ret != 0) |
6094 | | continue; |
6095 | | |
6096 | | /* SERVER: using secret in session ticket for peer auth. */ |
6097 | | ssl->options.peerAuthGood = 1; |
6098 | | |
6099 | | #ifdef WOLFSSL_EARLY_DATA |
6100 | | ssl->options.maxEarlyDataSz = ssl->session->maxEarlyDataSz; |
6101 | | #endif |
6102 | | /* Use the same cipher suite as before and set up for use. */ |
6103 | | ssl->options.cipherSuite0 = ssl->session->cipherSuite0; |
6104 | | ssl->options.cipherSuite = ssl->session->cipherSuite; |
6105 | | ret = SetCipherSpecs(ssl); |
6106 | | if (ret != 0) |
6107 | | return ret; |
6108 | | |
6109 | | /* Resumption PSK is resumption master secret. */ |
6110 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
6111 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
6112 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
6113 | | return ret; |
6114 | | } |
6115 | | |
6116 | | /* Derive the early secret using the PSK. */ |
6117 | | ret = DeriveEarlySecret(ssl); |
6118 | | if (ret != 0) |
6119 | | return ret; |
6120 | | |
6121 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6122 | | ret = HashInput(ssl, input, (int)inputSz); |
6123 | | if (ret < 0) |
6124 | | return ret; |
6125 | | |
6126 | | /* Derive the binder key to use with HMAC. */ |
6127 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
6128 | | if (ret != 0) |
6129 | | return ret; |
6130 | | } |
6131 | | else |
6132 | | #endif /* HAVE_SESSION_TICKET */ |
6133 | | #ifndef NO_PSK |
6134 | | if (FindPsk(ssl, current, suite, &ret)) { |
6135 | | if (ret != 0) |
6136 | | return ret; |
6137 | | |
6138 | | ret = HashInput(ssl, input, (int)inputSz); |
6139 | | if (ret < 0) |
6140 | | return ret; |
6141 | | |
6142 | | /* Derive the binder key to use with HMAC. */ |
6143 | | ret = DeriveBinderKey(ssl, binderKey); |
6144 | | if (ret != 0) |
6145 | | return ret; |
6146 | | } |
6147 | | else |
6148 | | #endif |
6149 | | { |
6150 | | continue; |
6151 | | } |
6152 | | |
6153 | | ssl->options.sendVerify = 0; |
6154 | | |
6155 | | /* Derive the Finished message secret. */ |
6156 | | ret = DeriveFinishedSecret(ssl, binderKey, |
6157 | | ssl->keys.client_write_MAC_secret, |
6158 | | 0 /* neither end */); |
6159 | | if (ret != 0) |
6160 | | return ret; |
6161 | | |
6162 | | /* Derive the binder and compare with the one in the extension. */ |
6163 | | ret = BuildTls13HandshakeHmac(ssl, |
6164 | | ssl->keys.client_write_MAC_secret, binder, &binderLen); |
6165 | | if (ret != 0) |
6166 | | return ret; |
6167 | | if (binderLen != current->binderLen || |
6168 | | XMEMCMP(binder, current->binder, binderLen) != 0) { |
6169 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6170 | | return BAD_BINDER; |
6171 | | } |
6172 | | |
6173 | | /* This PSK works, no need to try any more. */ |
6174 | | current->chosen = 1; |
6175 | | ext->resp = 1; |
6176 | | break; |
6177 | | } |
6178 | | |
6179 | | if (current == NULL) { |
6180 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6181 | | #ifndef NO_CERTS |
6182 | | if (ssl->buffers.certChainCnt != 0) |
6183 | | return 0; |
6184 | | #endif |
6185 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6186 | | return BAD_BINDER; |
6187 | | #else |
6188 | | return 0; |
6189 | | #endif |
6190 | | } |
6191 | | |
6192 | | *first = (current == ext->data); |
6193 | | *usingPSK = 1; |
6194 | | |
6195 | | WOLFSSL_LEAVE("DoPreSharedKeys", ret); |
6196 | | |
6197 | | return ret; |
6198 | | } |
6199 | | |
6200 | | /* Handle any Pre-Shared Key (PSK) extension. |
6201 | | * Must do this in ClientHello as it requires a hash of the truncated message. |
6202 | | * Don't know size of binders until Pre-Shared Key extension has been parsed. |
6203 | | * |
6204 | | * ssl SSL/TLS object. |
6205 | | * input ClientHello message. |
6206 | | * helloSz Size of the ClientHello message (including binders if present). |
6207 | | * clSuites Client's cipher suite list. |
6208 | | * usingPSK Indicates handshake is using Pre-Shared Keys. |
6209 | | */ |
6210 | | static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz, |
6211 | | Suites* clSuites, int* usingPSK) |
6212 | | { |
6213 | | int ret; |
6214 | | TLSX* ext; |
6215 | | word16 bindersLen; |
6216 | | int first = 0; |
6217 | | #ifndef WOLFSSL_PSK_ONE_ID |
6218 | | int i; |
6219 | | const Suites* suites; |
6220 | | #else |
6221 | | byte suite[2]; |
6222 | | #endif |
6223 | | |
6224 | | WOLFSSL_ENTER("CheckPreSharedKeys"); |
6225 | | |
6226 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6227 | | if (ext == NULL) { |
6228 | | #ifdef WOLFSSL_EARLY_DATA |
6229 | | ssl->earlyData = no_early_data; |
6230 | | #endif |
6231 | | if (usingPSK) |
6232 | | *usingPSK = 0; |
6233 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6234 | | ret = HashInput(ssl, input, (int)helloSz); |
6235 | | return ret; |
6236 | | } |
6237 | | |
6238 | | /* Extensions pushed on stack/list and PSK must be last. */ |
6239 | | if (ssl->extensions != ext) { |
6240 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6241 | | return PSK_KEY_ERROR; |
6242 | | } |
6243 | | |
6244 | | /* Assume we are going to resume with a pre-shared key. */ |
6245 | | ssl->options.resuming = 1; |
6246 | | |
6247 | | /* Find the pre-shared key extension and calculate hash of truncated |
6248 | | * ClientHello for binders. |
6249 | | */ |
6250 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
6251 | | client_hello, &bindersLen); |
6252 | | if (ret < 0) |
6253 | | return ret; |
6254 | | |
6255 | | /* Refine list for PSK processing. */ |
6256 | | RefineSuites(ssl, clSuites); |
6257 | | #ifndef WOLFSSL_PSK_ONE_ID |
6258 | | if (usingPSK == NULL) |
6259 | | return BAD_FUNC_ARG; |
6260 | | |
6261 | | /* set after refineSuites, to avoid taking a stale ptr to ctx->Suites */ |
6262 | | suites = WOLFSSL_SUITES(ssl); |
6263 | | /* Server list has only common suites from refining in server or client |
6264 | | * order. */ |
6265 | | for (i = 0; !(*usingPSK) && i < suites->suiteSz; i += 2) { |
6266 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, |
6267 | | suites->suites + i, usingPSK, &first); |
6268 | | if (ret != 0) { |
6269 | | #ifdef HAVE_SESSION_TICKET |
6270 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6271 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
6272 | | #endif |
6273 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6274 | | #endif |
6275 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6276 | | return ret; |
6277 | | } |
6278 | | } |
6279 | | #ifdef HAVE_SESSION_TICKET |
6280 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6281 | | #endif |
6282 | | #else |
6283 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, suite, usingPSK, |
6284 | | &first); |
6285 | | if (ret != 0) { |
6286 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6287 | | return ret; |
6288 | | } |
6289 | | #endif |
6290 | | |
6291 | | if (*usingPSK) { |
6292 | | /* While verifying the selected PSK, we updated the |
6293 | | * handshake hash up to the binder bytes in the PSK extensions. |
6294 | | * Continuing, we need the rest of the ClientHello hashed as well. |
6295 | | */ |
6296 | | ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen); |
6297 | | } |
6298 | | else { |
6299 | | /* No suitable PSK found, Hash the complete ClientHello, |
6300 | | * as caller expect it after we return */ |
6301 | | ret = HashInput(ssl, input, (int)helloSz); |
6302 | | } |
6303 | | if (ret != 0) |
6304 | | return ret; |
6305 | | |
6306 | | if (*usingPSK != 0) { |
6307 | | word32 modes; |
6308 | | #ifdef WOLFSSL_EARLY_DATA |
6309 | | TLSX* extEarlyData; |
6310 | | |
6311 | | extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6312 | | if (extEarlyData != NULL) { |
6313 | | /* Check if accepting early data and first PSK. */ |
6314 | | if (ssl->earlyData != no_early_data && first) { |
6315 | | extEarlyData->resp = 1; |
6316 | | |
6317 | | /* Derive early data decryption key. */ |
6318 | | ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, |
6319 | | 1); |
6320 | | if (ret != 0) |
6321 | | return ret; |
6322 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
6323 | | return ret; |
6324 | | |
6325 | | ssl->keys.encryptionOn = 1; |
6326 | | ssl->earlyData = process_early_data; |
6327 | | } |
6328 | | else |
6329 | | extEarlyData->resp = 0; |
6330 | | } |
6331 | | #endif |
6332 | | |
6333 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
6334 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
6335 | | if (ext == NULL) { |
6336 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6337 | | return MISSING_HANDSHAKE_DATA; |
6338 | | } |
6339 | | modes = ext->val; |
6340 | | |
6341 | | #ifdef HAVE_SUPPORTED_CURVES |
6342 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
6343 | | /* Use (EC)DHE for forward-security if possible. */ |
6344 | | if ((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe && |
6345 | | ext != NULL) { |
6346 | | /* Only use named group used in last session. */ |
6347 | | ssl->namedGroup = ssl->session->namedGroup; |
6348 | | |
6349 | | *usingPSK = 2; /* generate new ephemeral key */ |
6350 | | } |
6351 | | else if (ssl->options.onlyPskDheKe) { |
6352 | | return PSK_KEY_ERROR; |
6353 | | } |
6354 | | else |
6355 | | #endif |
6356 | | { |
6357 | | if ((modes & (1 << PSK_KE)) == 0) { |
6358 | | WOLFSSL_MSG("psk_ke mode does not allow key share"); |
6359 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6360 | | return PSK_KEY_ERROR; |
6361 | | } |
6362 | | ssl->options.noPskDheKe = 1; |
6363 | | ssl->arrays->preMasterSz = 0; |
6364 | | |
6365 | | *usingPSK = 1; |
6366 | | } |
6367 | | } |
6368 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6369 | | else { |
6370 | | #ifndef NO_CERTS |
6371 | | if (ssl->buffers.certChainCnt != 0) |
6372 | | return 0; |
6373 | | #endif |
6374 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6375 | | return BAD_BINDER; |
6376 | | } |
6377 | | #endif |
6378 | | |
6379 | | WOLFSSL_LEAVE("CheckPreSharedKeys", ret); |
6380 | | |
6381 | | return 0; |
6382 | | } |
6383 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
6384 | | |
6385 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
6386 | | /* Check that the Cookie data's integrity. |
6387 | | * |
6388 | | * ssl SSL/TLS object. |
6389 | | * cookie The cookie data - hash and MAC. |
6390 | | * cookieSz The length of the cookie data in bytes. |
6391 | | * returns Length of the hash on success, otherwise failure. |
6392 | | */ |
6393 | | int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz) |
6394 | | { |
6395 | | int ret; |
6396 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
6397 | | Hmac cookieHmac; |
6398 | | byte cookieType = 0; |
6399 | | byte macSz = 0; |
6400 | | |
6401 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
6402 | | ssl->buffers.tls13CookieSecret.length == 0) { |
6403 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
6404 | | return COOKIE_ERROR; |
6405 | | } |
6406 | | |
6407 | | #if !defined(NO_SHA) && defined(NO_SHA256) |
6408 | | cookieType = SHA; |
6409 | | macSz = WC_SHA_DIGEST_SIZE; |
6410 | | #endif /* NO_SHA */ |
6411 | | #ifndef NO_SHA256 |
6412 | | cookieType = WC_SHA256; |
6413 | | macSz = WC_SHA256_DIGEST_SIZE; |
6414 | | #endif /* NO_SHA256 */ |
6415 | | |
6416 | | if (cookieSz < ssl->specs.hash_size + macSz) |
6417 | | return HRR_COOKIE_ERROR; |
6418 | | cookieSz -= macSz; |
6419 | | |
6420 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
6421 | | if (ret == 0) { |
6422 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
6423 | | ssl->buffers.tls13CookieSecret.buffer, |
6424 | | ssl->buffers.tls13CookieSecret.length); |
6425 | | } |
6426 | | if (ret == 0) |
6427 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
6428 | | #ifdef WOLFSSL_DTLS13 |
6429 | | /* Tie cookie to peer address */ |
6430 | | if (ret == 0) { |
6431 | | /* peerLock not necessary. Still in handshake phase. */ |
6432 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
6433 | | ret = wc_HmacUpdate(&cookieHmac, |
6434 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
6435 | | ssl->buffers.dtlsCtx.peer.sz); |
6436 | | } |
6437 | | } |
6438 | | #endif |
6439 | | if (ret == 0) |
6440 | | ret = wc_HmacFinal(&cookieHmac, mac); |
6441 | | |
6442 | | wc_HmacFree(&cookieHmac); |
6443 | | if (ret != 0) |
6444 | | return ret; |
6445 | | |
6446 | | if (ConstantCompare(cookie + cookieSz, mac, macSz) != 0) { |
6447 | | WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR); |
6448 | | return HRR_COOKIE_ERROR; |
6449 | | } |
6450 | | return cookieSz; |
6451 | | } |
6452 | | |
6453 | | /* Length of the KeyShare Extension */ |
6454 | | #define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6455 | | /* Length of the Supported Versions Extension */ |
6456 | | #define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6457 | | /* Length of the Cookie Extension excluding cookie data */ |
6458 | | #define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6459 | | /* PV | Random | Session Id | CipherSuite | Compression | Ext Len */ |
6460 | | #define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \ |
6461 | | SUITE_LEN + COMP_LEN + OPAQUE16_LEN) |
6462 | | /* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */ |
6463 | | #define MAX_HRR_SZ (HRR_MAX_HS_HEADER_SZ + \ |
6464 | | HRR_BODY_SZ + \ |
6465 | | HRR_KEY_SHARE_SZ + \ |
6466 | | HRR_VERSIONS_SZ + \ |
6467 | | HRR_COOKIE_HDR_SZ) |
6468 | | |
6469 | | |
6470 | | /* Restart the handshake hash from the cookie value. |
6471 | | * |
6472 | | * ssl SSL/TLS object. |
6473 | | * cookie Cookie data from client. |
6474 | | * returns 0 on success, otherwise failure. |
6475 | | */ |
6476 | | static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie) |
6477 | | { |
6478 | | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
6479 | | byte hrr[MAX_HRR_SZ] = {0}; |
6480 | | int hrrIdx; |
6481 | | word32 idx; |
6482 | | byte hashSz; |
6483 | | byte* cookieData; |
6484 | | word16 cookieDataSz; |
6485 | | word16 length; |
6486 | | int keyShareExt = 0; |
6487 | | int ret; |
6488 | | |
6489 | | ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len); |
6490 | | if (ret < 0) |
6491 | | return ret; |
6492 | | cookieDataSz = (word16)ret; |
6493 | | hashSz = cookie->data[0]; |
6494 | | cookieData = cookie->data; |
6495 | | idx = OPAQUE8_LEN; |
6496 | | |
6497 | | /* Restart handshake hash with synthetic message hash. */ |
6498 | | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
6499 | | |
6500 | | if ((ret = InitHandshakeHashes(ssl)) != 0) |
6501 | | return ret; |
6502 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
6503 | | return ret; |
6504 | | #ifdef WOLFSSL_DEBUG_TLS |
6505 | | WOLFSSL_MSG("Restart Hash from Cookie"); |
6506 | | WOLFSSL_BUFFER(cookieData + idx, hashSz); |
6507 | | #endif |
6508 | | if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0) |
6509 | | return ret; |
6510 | | |
6511 | | /* Reconstruct the HelloRetryMessage for handshake hash. */ |
6512 | | length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz + |
6513 | | HRR_COOKIE_HDR_SZ + cookie->len; |
6514 | | length += HRR_VERSIONS_SZ; |
6515 | | /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */ |
6516 | | if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) { |
6517 | | keyShareExt = 1; |
6518 | | length += HRR_KEY_SHARE_SZ; |
6519 | | } |
6520 | | |
6521 | | AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl); |
6522 | | |
6523 | | idx += hashSz; |
6524 | | hrrIdx = HANDSHAKE_HEADER_SZ; |
6525 | | |
6526 | | #ifdef WOLFSSL_DTLS13 |
6527 | | if (ssl->options.dtls) |
6528 | | hrrIdx += DTLS_HANDSHAKE_EXTRA; |
6529 | | #endif /* WOLFSSL_DTLS13 */ |
6530 | | |
6531 | | /* The negotiated protocol version. */ |
6532 | | hrr[hrrIdx++] = ssl->version.major; |
6533 | | hrr[hrrIdx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
6534 | | |
6535 | | /* HelloRetryRequest message has fixed value for random. */ |
6536 | | XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN); |
6537 | | hrrIdx += RAN_LEN; |
6538 | | |
6539 | | hrr[hrrIdx++] = ssl->session->sessionIDSz; |
6540 | | if (ssl->session->sessionIDSz > 0) { |
6541 | | XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz); |
6542 | | hrrIdx += ssl->session->sessionIDSz; |
6543 | | } |
6544 | | |
6545 | | /* Cipher Suite */ |
6546 | | hrr[hrrIdx++] = cookieData[idx++]; |
6547 | | hrr[hrrIdx++] = cookieData[idx++]; |
6548 | | |
6549 | | /* Compression not supported in TLS v1.3. */ |
6550 | | hrr[hrrIdx++] = 0; |
6551 | | |
6552 | | /* Extensions' length */ |
6553 | | length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz; |
6554 | | c16toa(length, hrr + hrrIdx); |
6555 | | hrrIdx += 2; |
6556 | | |
6557 | | /* Optional KeyShare Extension */ |
6558 | | if (keyShareExt) { |
6559 | | c16toa(TLSX_KEY_SHARE, hrr + hrrIdx); |
6560 | | hrrIdx += 2; |
6561 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6562 | | hrrIdx += 2; |
6563 | | hrr[hrrIdx++] = cookieData[idx++]; |
6564 | | hrr[hrrIdx++] = cookieData[idx++]; |
6565 | | } |
6566 | | c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx); |
6567 | | hrrIdx += 2; |
6568 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6569 | | hrrIdx += 2; |
6570 | | #ifdef WOLFSSL_TLS13_DRAFT |
6571 | | hrr[hrrIdx++] = TLS_DRAFT_MAJOR; |
6572 | | hrr[hrrIdx++] = TLS_DRAFT_MINOR; |
6573 | | #else |
6574 | | hrr[hrrIdx++] = ssl->version.major; |
6575 | | hrr[hrrIdx++] = ssl->version.minor; |
6576 | | #endif |
6577 | | |
6578 | | /* Mandatory Cookie Extension */ |
6579 | | c16toa(TLSX_COOKIE, hrr + hrrIdx); |
6580 | | hrrIdx += 2; |
6581 | | c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx); |
6582 | | hrrIdx += 2; |
6583 | | c16toa(cookie->len, hrr + hrrIdx); |
6584 | | hrrIdx += 2; |
6585 | | |
6586 | | #ifdef WOLFSSL_DEBUG_TLS |
6587 | | WOLFSSL_MSG("Reconstructed HelloRetryRequest"); |
6588 | | WOLFSSL_BUFFER(hrr, hrrIdx); |
6589 | | WOLFSSL_MSG("Cookie"); |
6590 | | WOLFSSL_BUFFER(cookieData, cookie->len); |
6591 | | #endif |
6592 | | |
6593 | | #ifdef WOLFSSL_DTLS13 |
6594 | | if (ssl->options.dtls) { |
6595 | | ret = Dtls13HashHandshake(ssl, hrr, (word16)hrrIdx); |
6596 | | } |
6597 | | else |
6598 | | #endif /* WOLFSSL_DTLS13 */ |
6599 | | { |
6600 | | ret = HashRaw(ssl, hrr, hrrIdx); |
6601 | | } |
6602 | | |
6603 | | if (ret != 0) |
6604 | | return ret; |
6605 | | |
6606 | | return HashRaw(ssl, cookieData, cookie->len); |
6607 | | } |
6608 | | #endif |
6609 | | |
6610 | | /* Do SupportedVersion extension for TLS v1.3+ otherwise it is not. |
6611 | | * |
6612 | | * ssl The SSL/TLS object. |
6613 | | * input The message buffer. |
6614 | | * i The index into the message buffer of ClientHello. |
6615 | | * helloSz The length of the current handshake message. |
6616 | | * returns 0 on success and otherwise failure. |
6617 | | */ |
6618 | | static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i, |
6619 | | word32 helloSz, int* wantDowngrade) |
6620 | | { |
6621 | | int ret; |
6622 | | byte b; |
6623 | | word16 suiteSz; |
6624 | | word16 totalExtSz; |
6625 | | int foundVersion = 0; |
6626 | | |
6627 | | /* Client random */ |
6628 | | i += RAN_LEN; |
6629 | | /* Session id - not used in TLS v1.3 */ |
6630 | | b = input[i++]; |
6631 | | if (i + b > helloSz) { |
6632 | | return BUFFER_ERROR; |
6633 | | } |
6634 | | i += b; |
6635 | | #ifdef WOLFSSL_DTLS13 |
6636 | | if (ssl->options.dtls) { |
6637 | | /* legacy_cookie - not used in DTLS v1.3 */ |
6638 | | b = input[i++]; |
6639 | | if (i + b > helloSz) { |
6640 | | return BUFFER_ERROR; |
6641 | | } |
6642 | | i += b; |
6643 | | } |
6644 | | #endif /* WOLFSSL_DTLS13 */ |
6645 | | /* Cipher suites */ |
6646 | | if (i + OPAQUE16_LEN > helloSz) |
6647 | | return BUFFER_ERROR; |
6648 | | ato16(input + i, &suiteSz); |
6649 | | i += OPAQUE16_LEN; |
6650 | | if (i + suiteSz + 1 > helloSz) |
6651 | | return BUFFER_ERROR; |
6652 | | i += suiteSz; |
6653 | | /* Compression */ |
6654 | | b = input[i++]; |
6655 | | if (i + b > helloSz) |
6656 | | return BUFFER_ERROR; |
6657 | | i += b; |
6658 | | |
6659 | | /* TLS 1.3 must have extensions */ |
6660 | | if (i < helloSz) { |
6661 | | if (i + OPAQUE16_LEN > helloSz) |
6662 | | return BUFFER_ERROR; |
6663 | | ato16(&input[i], &totalExtSz); |
6664 | | i += OPAQUE16_LEN; |
6665 | | if (totalExtSz != helloSz - i) |
6666 | | return BUFFER_ERROR; |
6667 | | |
6668 | | /* Need to negotiate version first. */ |
6669 | | if ((ret = TLSX_ParseVersion(ssl, input + i, totalExtSz, client_hello, |
6670 | | &foundVersion))) { |
6671 | | return ret; |
6672 | | } |
6673 | | } |
6674 | | *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version); |
6675 | | |
6676 | | return 0; |
6677 | | } |
6678 | | |
6679 | | /* Handle a ClientHello handshake message. |
6680 | | * If the protocol version in the message is not TLS v1.3 or higher, use |
6681 | | * DoClientHello() |
6682 | | * Only a server will receive this message. |
6683 | | * |
6684 | | * ssl The SSL/TLS object. |
6685 | | * input The message buffer. |
6686 | | * inOutIdx On entry, the index into the message buffer of ClientHello. |
6687 | | * On exit, the index of byte after the ClientHello message and |
6688 | | * padding. |
6689 | | * helloSz The length of the current handshake message. |
6690 | | * returns 0 on success and otherwise failure. |
6691 | | */ |
6692 | | |
6693 | | typedef struct Dch13Args { |
6694 | | ProtocolVersion pv; |
6695 | | word32 idx; |
6696 | | word32 begin; |
6697 | | int usingPSK; |
6698 | | } Dch13Args; |
6699 | | |
6700 | | static void FreeDch13Args(WOLFSSL* ssl, void* pArgs) |
6701 | | { |
6702 | | /* openssl compat builds hang on to the client suites until WOLFSSL object |
6703 | | * is destroyed */ |
6704 | | #ifndef OPENSSL_EXTRA |
6705 | | if (ssl->clSuites) { |
6706 | | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
6707 | | ssl->clSuites = NULL; |
6708 | | } |
6709 | | #endif |
6710 | | (void)ssl; |
6711 | | (void)pArgs; |
6712 | | |
6713 | | } |
6714 | | |
6715 | | int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
6716 | | word32 helloSz) |
6717 | 0 | { |
6718 | 0 | int ret; |
6719 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6720 | | Dch13Args* args = NULL; |
6721 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
6722 | | #else |
6723 | 0 | Dch13Args args[1]; |
6724 | 0 | #endif |
6725 | | #if defined(HAVE_ECH) |
6726 | | TLSX* echX = NULL; |
6727 | | HS_Hashes* tmpHashes; |
6728 | | #endif |
6729 | |
|
6730 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO); |
6731 | 0 | WOLFSSL_ENTER("DoTls13ClientHello"); |
6732 | |
|
6733 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6734 | | if (ssl->async == NULL) { |
6735 | | ssl->async = (struct WOLFSSL_ASYNC*) |
6736 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
6737 | | DYNAMIC_TYPE_ASYNC); |
6738 | | if (ssl->async == NULL) |
6739 | | ERROR_OUT(MEMORY_E, exit_dch); |
6740 | | } |
6741 | | args = (Dch13Args*)ssl->async->args; |
6742 | | |
6743 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
6744 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
6745 | | /* Check for error */ |
6746 | | if (ret < 0) { |
6747 | | goto exit_dch; |
6748 | | } |
6749 | | } |
6750 | | else |
6751 | | #endif |
6752 | 0 | { |
6753 | | /* Reset state */ |
6754 | 0 | ret = VERSION_ERROR; |
6755 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
6756 | 0 | XMEMSET(args, 0, sizeof(Dch13Args)); |
6757 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6758 | | ssl->async->freeArgs = FreeDch13Args; |
6759 | | #endif |
6760 | 0 | } |
6761 | |
|
6762 | 0 | switch (ssl->options.asyncState) { |
6763 | 0 | case TLS_ASYNC_BEGIN: |
6764 | 0 | { |
6765 | 0 | byte b; |
6766 | 0 | byte sessIdSz; |
6767 | 0 | int wantDowngrade = 0; |
6768 | 0 | word16 totalExtSz = 0; |
6769 | |
|
6770 | | #ifdef WOLFSSL_CALLBACKS |
6771 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
6772 | | if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo); |
6773 | | #endif |
6774 | | |
6775 | | /* do not change state in the SSL object before the next region of code |
6776 | | * to be able to statelessly compute a DTLS cookie */ |
6777 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
6778 | | /* Update the ssl->options.dtlsStateful setting `if` statement in |
6779 | | * wolfSSL_accept_TLSv13 when changing this one. */ |
6780 | | if (IsDtlsNotSctpMode(ssl) && ssl->options.sendCookie && |
6781 | | !ssl->options.dtlsStateful) { |
6782 | | DtlsSetSeqNumForReply(ssl); |
6783 | | ret = DoClientHelloStateless(ssl, input + *inOutIdx, helloSz, 0, NULL); |
6784 | | if (ret != 0 || !ssl->options.dtlsStateful) { |
6785 | | *inOutIdx += helloSz; |
6786 | | goto exit_dch; |
6787 | | } |
6788 | | if (ssl->chGoodCb != NULL) { |
6789 | | int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
6790 | | if (cbret < 0) { |
6791 | | ssl->error = cbret; |
6792 | | WOLFSSL_MSG("ClientHello Good Cb don't continue error"); |
6793 | | return WOLFSSL_FATAL_ERROR; |
6794 | | } |
6795 | | } |
6796 | | } |
6797 | | ssl->options.dtlsStateful = 1; |
6798 | | #endif /* WOLFSSL_DTLS */ |
6799 | |
|
6800 | 0 | args->idx = *inOutIdx; |
6801 | 0 | args->begin = args->idx; |
6802 | | |
6803 | | /* protocol version, random and session id length check */ |
6804 | 0 | if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz) { |
6805 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6806 | 0 | } |
6807 | | |
6808 | | /* Protocol version */ |
6809 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
6810 | 0 | ssl->chVersion = args->pv; /* store */ |
6811 | 0 | args->idx += OPAQUE16_LEN; |
6812 | | |
6813 | | |
6814 | | /* this check pass for DTLS Major (0xff) */ |
6815 | 0 | if (args->pv.major < SSLv3_MAJOR) { |
6816 | 0 | WOLFSSL_MSG("Legacy version field contains unsupported value"); |
6817 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6818 | 0 | } |
6819 | | |
6820 | | #ifdef WOLFSSL_DTLS13 |
6821 | | if (ssl->options.dtls && |
6822 | | args->pv.major == DTLS_MAJOR && args->pv.minor > DTLSv1_2_MINOR) { |
6823 | | wantDowngrade = 1; |
6824 | | ssl->version.minor = args->pv.minor; |
6825 | | } |
6826 | | #endif /* WOLFSSL_DTLS13 */ |
6827 | | |
6828 | 0 | if (!ssl->options.dtls) { |
6829 | 0 | #ifndef WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION |
6830 | | /* Check for TLS 1.3 version (0x0304) in legacy version field. RFC 8446 |
6831 | | * Section 4.2.1 allows this action: |
6832 | | * |
6833 | | * "Servers MAY abort the handshake upon receiving a ClientHello with |
6834 | | * legacy_version 0x0304 or later." |
6835 | | * |
6836 | | * Note that if WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION is defined then the |
6837 | | * semantics of RFC 5246 Appendix E will be followed. A ServerHello with |
6838 | | * version 1.2 will be sent. The same is true if TLS 1.3 is not enabled. |
6839 | | */ |
6840 | 0 | if (args->pv.major == SSLv3_MAJOR && args->pv.minor >= TLSv1_3_MINOR) { |
6841 | 0 | WOLFSSL_MSG("Legacy version field is TLS 1.3 or later. Aborting."); |
6842 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6843 | 0 | } |
6844 | 0 | #endif /* WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION */ |
6845 | | |
6846 | | /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */ |
6847 | 0 | if (args->pv.major > SSLv3_MAJOR || (args->pv.major == SSLv3_MAJOR && |
6848 | 0 | args->pv.minor >= TLSv1_3_MINOR)) { |
6849 | 0 | args->pv.major = SSLv3_MAJOR; |
6850 | 0 | args->pv.minor = TLSv1_2_MINOR; |
6851 | 0 | wantDowngrade = 1; |
6852 | 0 | ssl->version.minor = args->pv.minor; |
6853 | 0 | } |
6854 | | /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */ |
6855 | 0 | else if (args->pv.major == SSLv3_MAJOR && |
6856 | 0 | args->pv.minor < TLSv1_2_MINOR) { |
6857 | 0 | wantDowngrade = 1; |
6858 | 0 | ssl->version.minor = args->pv.minor; |
6859 | 0 | } |
6860 | 0 | } |
6861 | | |
6862 | 0 | if (!wantDowngrade) { |
6863 | 0 | ret = DoTls13SupportedVersions(ssl, input + args->begin, |
6864 | 0 | args->idx - args->begin, helloSz, &wantDowngrade); |
6865 | 0 | if (ret < 0) |
6866 | 0 | goto exit_dch; |
6867 | 0 | } |
6868 | | |
6869 | 0 | if (wantDowngrade) { |
6870 | 0 | #ifndef WOLFSSL_NO_TLS12 |
6871 | 0 | byte realMinor; |
6872 | 0 | if (!ssl->options.downgrade) { |
6873 | 0 | WOLFSSL_MSG("Client trying to connect with lesser version than " |
6874 | 0 | "TLS v1.3"); |
6875 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6876 | 0 | } |
6877 | | |
6878 | 0 | if ((!ssl->options.dtls |
6879 | 0 | && args->pv.minor < ssl->options.minDowngrade) || |
6880 | 0 | (ssl->options.dtls && args->pv.minor > ssl->options.minDowngrade)) { |
6881 | 0 | WOLFSSL_MSG("\tversion below minimum allowed, fatal error"); |
6882 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6883 | 0 | } |
6884 | | |
6885 | 0 | realMinor = ssl->version.minor; |
6886 | 0 | ssl->version.minor = args->pv.minor; |
6887 | 0 | ret = HashInput(ssl, input + args->begin, (int)helloSz); |
6888 | 0 | ssl->version.minor = realMinor; |
6889 | 0 | if (ret == 0) { |
6890 | 0 | ret = DoClientHello(ssl, input, inOutIdx, helloSz); |
6891 | 0 | } |
6892 | 0 | goto exit_dch; |
6893 | | #else |
6894 | | WOLFSSL_MSG("Client trying to connect with lesser version than " |
6895 | | "TLS v1.3"); |
6896 | | ERROR_OUT(VERSION_ERROR, exit_dch); |
6897 | | #endif |
6898 | 0 | } |
6899 | | |
6900 | | /* From here on we are a TLS 1.3 ClientHello. */ |
6901 | | |
6902 | | /* Client random */ |
6903 | 0 | XMEMCPY(ssl->arrays->clientRandom, input + args->idx, RAN_LEN); |
6904 | 0 | args->idx += RAN_LEN; |
6905 | |
|
6906 | | #ifdef WOLFSSL_DEBUG_TLS |
6907 | | WOLFSSL_MSG("client random"); |
6908 | | WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN); |
6909 | | #endif |
6910 | |
|
6911 | 0 | sessIdSz = input[args->idx++]; |
6912 | 0 | #ifndef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
6913 | 0 | if (sessIdSz > ID_LEN) |
6914 | | #else |
6915 | | if (sessIdSz != ID_LEN && sessIdSz != 0) |
6916 | | #endif |
6917 | 0 | { |
6918 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6919 | 0 | } |
6920 | | |
6921 | 0 | if (sessIdSz + args->idx > helloSz) |
6922 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6923 | |
|
6924 | 0 | ssl->session->sessionIDSz = sessIdSz; |
6925 | 0 | if (sessIdSz > 0) |
6926 | 0 | XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz); |
6927 | 0 | args->idx += sessIdSz; |
6928 | |
|
6929 | | #ifdef WOLFSSL_DTLS13 |
6930 | | /* legacy_cookie */ |
6931 | | if (ssl->options.dtls) { |
6932 | | /* https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 */ |
6933 | | byte cookieLen = input[args->idx++]; |
6934 | | if (cookieLen != 0) { |
6935 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6936 | | } |
6937 | | } |
6938 | | #endif /* WOLFSSL_DTLS13 */ |
6939 | |
|
6940 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
6941 | 0 | ssl->clSuites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap, |
6942 | 0 | DYNAMIC_TYPE_SUITES); |
6943 | 0 | if (ssl->clSuites == NULL) { |
6944 | 0 | ERROR_OUT(MEMORY_E, exit_dch); |
6945 | 0 | } |
6946 | | |
6947 | | /* Cipher suites */ |
6948 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
6949 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6950 | 0 | ato16(&input[args->idx], &ssl->clSuites->suiteSz); |
6951 | 0 | args->idx += OPAQUE16_LEN; |
6952 | 0 | if ((ssl->clSuites->suiteSz % 2) != 0) { |
6953 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6954 | 0 | } |
6955 | | /* suites and compression length check */ |
6956 | 0 | if ((args->idx - args->begin) + ssl->clSuites->suiteSz + OPAQUE8_LEN > |
6957 | 0 | helloSz) { |
6958 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6959 | 0 | } |
6960 | 0 | if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) |
6961 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6962 | 0 | XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); |
6963 | 0 | args->idx += ssl->clSuites->suiteSz; |
6964 | 0 | ssl->clSuites->hashSigAlgoSz = 0; |
6965 | | |
6966 | | /* Compression */ |
6967 | 0 | b = input[args->idx++]; |
6968 | 0 | if ((args->idx - args->begin) + b > helloSz) |
6969 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6970 | 0 | if (b != COMP_LEN) { |
6971 | 0 | WOLFSSL_MSG("Must be one compression type in list"); |
6972 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6973 | 0 | } |
6974 | 0 | b = input[args->idx++]; |
6975 | 0 | if (b != NO_COMPRESSION) { |
6976 | 0 | WOLFSSL_MSG("Must be no compression type in list"); |
6977 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
6978 | 0 | } |
6979 | | |
6980 | | /* Extensions */ |
6981 | 0 | if ((args->idx - args->begin) == helloSz) |
6982 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6983 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
6984 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6985 | |
|
6986 | 0 | ato16(&input[args->idx], &totalExtSz); |
6987 | 0 | args->idx += OPAQUE16_LEN; |
6988 | 0 | if ((args->idx - args->begin) + totalExtSz > helloSz) |
6989 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6990 | | |
6991 | | /* Auto populate extensions supported unless user defined. */ |
6992 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0) |
6993 | 0 | goto exit_dch; |
6994 | | |
6995 | | #if defined(HAVE_ECH) |
6996 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
6997 | | /* save the start of the buffer so we can use it when parsing ech */ |
6998 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
6999 | | |
7000 | | if (echX == NULL) |
7001 | | ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch); |
7002 | | |
7003 | | ((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ; |
7004 | | ((WOLFSSL_ECH*)echX->data)->aadLen = helloSz; |
7005 | | } |
7006 | | #endif |
7007 | | |
7008 | | /* Parse extensions */ |
7009 | 0 | if ((ret = TLSX_Parse(ssl, input + args->idx, totalExtSz, client_hello, |
7010 | 0 | ssl->clSuites))) { |
7011 | 0 | goto exit_dch; |
7012 | 0 | } |
7013 | | |
7014 | | #if defined(HAVE_ECH) |
7015 | | /* jump to the end to clean things up */ |
7016 | | if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) |
7017 | | goto exit_dch; |
7018 | | #endif |
7019 | | |
7020 | 0 | #ifdef HAVE_SNI |
7021 | 0 | if ((ret = SNI_Callback(ssl)) != 0) |
7022 | 0 | goto exit_dch; |
7023 | 0 | ssl->options.side = WOLFSSL_SERVER_END; |
7024 | 0 | #endif |
7025 | |
|
7026 | 0 | args->idx += totalExtSz; |
7027 | 0 | ssl->options.haveSessionId = 1; |
7028 | 0 | ssl->options.sendVerify = SEND_CERT; |
7029 | |
|
7030 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7031 | | ssl->options.cookieGood = 0; |
7032 | | if (ssl->options.sendCookie && |
7033 | | (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE |
7034 | | #ifdef WOLFSSL_DTLS13 |
7035 | | /* Always check for a valid cookie since we may have already |
7036 | | * sent a HRR but we reset the state. */ |
7037 | | || ssl->options.dtls |
7038 | | #endif |
7039 | | )) { |
7040 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_COOKIE); |
7041 | | |
7042 | | if (ext != NULL) { |
7043 | | /* Ensure the cookie came from client and isn't the one in the |
7044 | | * response - HelloRetryRequest. |
7045 | | */ |
7046 | | if (ext->resp == 0) { |
7047 | | ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data); |
7048 | | if (ret != 0) |
7049 | | goto exit_dch; |
7050 | | /* Don't change state here as we may want to enter |
7051 | | * DoTls13ClientHello again. */ |
7052 | | ssl->options.cookieGood = 1; |
7053 | | } |
7054 | | else { |
7055 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7056 | | } |
7057 | | } |
7058 | | else { |
7059 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME) |
7060 | | /* Don't error out as we may be resuming. We confirm this later. */ |
7061 | | if (!ssl->options.dtls) |
7062 | | #endif |
7063 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7064 | | } |
7065 | | } |
7066 | | #endif |
7067 | |
|
7068 | | #if defined(HAVE_ECH) |
7069 | | /* hash clientHelloInner to hsHashesEch independently since it can't include |
7070 | | * the HRR */ |
7071 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7072 | | tmpHashes = ssl->hsHashes; |
7073 | | ssl->hsHashes = NULL; |
7074 | | ret = InitHandshakeHashes(ssl); |
7075 | | if (ret != 0) |
7076 | | goto exit_dch; |
7077 | | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7078 | | goto exit_dch; |
7079 | | ssl->hsHashesEch = ssl->hsHashes; |
7080 | | ssl->hsHashes = tmpHashes; |
7081 | | } |
7082 | | #endif |
7083 | |
|
7084 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7085 | | defined(HAVE_TLS_EXTENSIONS) |
7086 | | ret = CheckPreSharedKeys(ssl, input + args->begin, helloSz, ssl->clSuites, |
7087 | | &args->usingPSK); |
7088 | | if (ret != 0) |
7089 | | goto exit_dch; |
7090 | | #else |
7091 | 0 | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7092 | 0 | goto exit_dch; |
7093 | 0 | #endif |
7094 | | |
7095 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7096 | | defined(HAVE_TLS_EXTENSIONS) |
7097 | | if (!args->usingPSK) |
7098 | | #endif |
7099 | 0 | { |
7100 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7101 | | /* Not using PSK so don't require no KE. */ |
7102 | | ssl->options.noPskDheKe = 0; |
7103 | | #endif |
7104 | |
|
7105 | 0 | #ifndef NO_CERTS |
7106 | 0 | if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) { |
7107 | 0 | WOLFSSL_MSG("Client did not send a KeyShare extension"); |
7108 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7109 | 0 | } |
7110 | | /* Can't check ssl->extensions here as SigAlgs are unconditionally |
7111 | | set by TLSX_PopulateExtensions */ |
7112 | 0 | if (ssl->clSuites->hashSigAlgoSz == 0) { |
7113 | 0 | WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension"); |
7114 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7115 | 0 | } |
7116 | | #else |
7117 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7118 | | #endif |
7119 | 0 | } |
7120 | |
|
7121 | | #ifdef HAVE_ALPN |
7122 | | /* With PSK and all other things validated, it's time to |
7123 | | * select the ALPN protocol, if so requested */ |
7124 | | if ((ret = ALPN_Select(ssl)) != 0) |
7125 | | goto exit_dch; |
7126 | | #endif |
7127 | 0 | } /* case TLS_ASYNC_BEGIN */ |
7128 | 0 | FALL_THROUGH; |
7129 | |
|
7130 | 0 | case TLS_ASYNC_BUILD: |
7131 | | /* Advance state and proceed */ |
7132 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
7133 | 0 | FALL_THROUGH; |
7134 | |
|
7135 | 0 | case TLS_ASYNC_DO: |
7136 | 0 | { |
7137 | | #ifdef OPENSSL_EXTRA |
7138 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
7139 | | goto exit_dch; |
7140 | | #endif |
7141 | 0 | #ifndef NO_CERTS |
7142 | 0 | if (!args->usingPSK) { |
7143 | 0 | if ((ret = MatchSuite(ssl, ssl->clSuites)) < 0) { |
7144 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7145 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7146 | | #endif |
7147 | 0 | WOLFSSL_MSG("Unsupported cipher suite, ClientHello 1.3"); |
7148 | 0 | goto exit_dch; |
7149 | 0 | } |
7150 | 0 | } |
7151 | 0 | #endif |
7152 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7153 | 0 | if (args->usingPSK == 2) { |
7154 | | /* Pick key share and Generate a new key if not present. */ |
7155 | 0 | int doHelloRetry = 0; |
7156 | 0 | ret = TLSX_KeyShare_Establish(ssl, &doHelloRetry); |
7157 | 0 | if (doHelloRetry) { |
7158 | | /* Make sure we don't send HRR twice */ |
7159 | 0 | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7160 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7161 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7162 | 0 | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7163 | 0 | ret = 0; /* for hello_retry return 0 */ |
7164 | 0 | } |
7165 | 0 | if (ret != 0) |
7166 | 0 | goto exit_dch; |
7167 | 0 | } |
7168 | 0 | #endif |
7169 | | |
7170 | | /* Advance state and proceed */ |
7171 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
7172 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
7173 | 0 | FALL_THROUGH; |
7174 | |
|
7175 | 0 | case TLS_ASYNC_VERIFY: |
7176 | 0 | { |
7177 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SUPPORTED_CURVES) |
7178 | | /* Check if the KeyShare calculations from the previous state are complete. |
7179 | | * wolfSSL_AsyncPop advances ssl->options.asyncState so we may end up here |
7180 | | * with a pending calculation. */ |
7181 | | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7182 | | if (extension != NULL && extension->resp == 1) { |
7183 | | KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data; |
7184 | | if (serverKSE != NULL && |
7185 | | serverKSE->lastRet == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7186 | | ret = TLSX_KeyShare_GenKey(ssl, serverKSE); |
7187 | | if (ret != 0) |
7188 | | goto exit_dch; |
7189 | | } |
7190 | | } |
7191 | | #endif |
7192 | | /* Advance state and proceed */ |
7193 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
7194 | 0 | } |
7195 | 0 | FALL_THROUGH; |
7196 | |
|
7197 | 0 | case TLS_ASYNC_FINALIZE: |
7198 | 0 | { |
7199 | 0 | *inOutIdx = args->idx; |
7200 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
7201 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7202 | | ssl->options.pskNegotiated = (args->usingPSK != 0); |
7203 | | #endif |
7204 | |
|
7205 | 0 | if (!args->usingPSK) { |
7206 | 0 | #ifndef NO_CERTS |
7207 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
7208 | | #ifdef HAVE_NULL_CIPHER |
7209 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
7210 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
7211 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
7212 | | ; |
7213 | | } |
7214 | | else |
7215 | | #endif |
7216 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
7217 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7218 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
7219 | 0 | ; /* Do nothing. */ |
7220 | 0 | } |
7221 | 0 | else |
7222 | 0 | #endif |
7223 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
7224 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7225 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
7226 | 0 | ; /* Do nothing. */ |
7227 | 0 | } |
7228 | 0 | else |
7229 | 0 | #endif |
7230 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
7231 | 0 | WOLFSSL_MSG("Negotiated ciphersuite from lesser version than " |
7232 | 0 | "TLS v1.3"); |
7233 | 0 | ERROR_OUT(MATCH_SUITE_ERROR, exit_dch); |
7234 | 0 | } |
7235 | | |
7236 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7237 | | if (ssl->options.resuming) { |
7238 | | ssl->options.resuming = 0; |
7239 | | ssl->arrays->psk_keySz = 0; |
7240 | | XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size); |
7241 | | } |
7242 | | #endif |
7243 | | |
7244 | | /* Derive early secret for handshake secret. */ |
7245 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
7246 | 0 | goto exit_dch; |
7247 | 0 | #endif /* !NO_CERTS */ |
7248 | 0 | } |
7249 | 0 | break; |
7250 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
7251 | 0 | default: |
7252 | 0 | ret = INPUT_CASE_ERROR; |
7253 | 0 | } /* switch (ssl->options.asyncState) */ |
7254 | | |
7255 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7256 | | if (ret == 0 && ssl->options.sendCookie) { |
7257 | | if (ssl->options.cookieGood && |
7258 | | ssl->options.acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE) { |
7259 | | /* Processing second ClientHello. Clear HRR state. */ |
7260 | | ssl->options.serverState = NULL_STATE; |
7261 | | } |
7262 | | |
7263 | | if (ssl->options.cookieGood && |
7264 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7265 | | /* If we already verified the peer with a cookie then we can't |
7266 | | * do another HRR for cipher negotiation. Send alert and restart |
7267 | | * the entire handshake. */ |
7268 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7269 | | } |
7270 | | #ifdef WOLFSSL_DTLS13 |
7271 | | if (ssl->options.dtls && |
7272 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7273 | | /* Cookie and key share negotiation should be handled in |
7274 | | * DoClientHelloStateless. If we enter here then something went |
7275 | | * wrong in our logic. */ |
7276 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7277 | | } |
7278 | | #endif |
7279 | | /* Send a cookie */ |
7280 | | if (!ssl->options.cookieGood && |
7281 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7282 | | #ifdef WOLFSSL_DTLS13 |
7283 | | if (ssl->options.dtls) { |
7284 | | #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME |
7285 | | /* We can skip cookie on resumption */ |
7286 | | if (!ssl->options.dtls || !ssl->options.dtls13NoHrrOnResume || |
7287 | | !args->usingPSK) |
7288 | | #endif |
7289 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7290 | | } |
7291 | | else |
7292 | | #endif |
7293 | | { |
7294 | | /* Need to remove the keyshare ext if we found a common group |
7295 | | * and are not doing curve negotiation. */ |
7296 | | TLSX_Remove(&ssl->extensions, TLSX_KEY_SHARE, ssl->heap); |
7297 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7298 | | } |
7299 | | |
7300 | | } |
7301 | | } |
7302 | | #endif /* WOLFSSL_DTLS13 */ |
7303 | | |
7304 | | #ifdef WOLFSSL_DTLS_CID |
7305 | | /* do not modify CID state if we are sending an HRR */ |
7306 | | if (ret == 0 && ssl->options.dtls && ssl->options.useDtlsCID && |
7307 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7308 | | DtlsCIDOnExtensionsParsed(ssl); |
7309 | | #endif /* WOLFSSL_DTLS_CID */ |
7310 | | |
7311 | | |
7312 | | |
7313 | 0 | exit_dch: |
7314 | |
|
7315 | 0 | WOLFSSL_LEAVE("DoTls13ClientHello", ret); |
7316 | |
|
7317 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7318 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7319 | | ssl->msgsReceived.got_client_hello = 0; |
7320 | | return ret; |
7321 | | } |
7322 | | #endif |
7323 | |
|
7324 | 0 | FreeDch13Args(ssl, args); |
7325 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7326 | | FreeAsyncCtx(ssl, 0); |
7327 | | #endif |
7328 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO); |
7329 | |
|
7330 | 0 | if (ret != 0) { |
7331 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
7332 | 0 | } |
7333 | |
|
7334 | | #if defined(HAVE_ECH) |
7335 | | if (ret == 0 && echX != NULL && |
7336 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
7337 | | |
7338 | | /* add the header to the inner hello */ |
7339 | | AddTls13HandShakeHeader(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
7340 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen, 0, 0, |
7341 | | client_hello, ssl); |
7342 | | } |
7343 | | #endif |
7344 | |
|
7345 | 0 | return ret; |
7346 | 0 | } |
7347 | | |
7348 | | /* Send TLS v1.3 ServerHello message to client. |
7349 | | * Only a server will send this message. |
7350 | | * |
7351 | | * ssl The SSL/TLS object. |
7352 | | * returns 0 on success, otherwise failure. |
7353 | | */ |
7354 | | /* handle generation of TLS 1.3 server_hello (2) */ |
7355 | | int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType) |
7356 | 0 | { |
7357 | 0 | int ret; |
7358 | 0 | byte* output; |
7359 | 0 | word16 length; |
7360 | 0 | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7361 | 0 | int sendSz; |
7362 | | #if defined(HAVE_ECH) |
7363 | | TLSX* echX = NULL; |
7364 | | byte* acceptLabel = (byte*)echAcceptConfirmationLabel; |
7365 | | word32 acceptOffset; |
7366 | | word16 acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
7367 | | #endif |
7368 | |
|
7369 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND); |
7370 | 0 | WOLFSSL_ENTER("SendTls13ServerHello"); |
7371 | | |
7372 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated in |
7373 | | * dtls.c */ |
7374 | 0 | if (extMsgType == hello_retry_request |
7375 | | #ifdef WOLFSSL_DTLS13 |
7376 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
7377 | | #endif |
7378 | 0 | ) { |
7379 | 0 | WOLFSSL_MSG("wolfSSL Sending HelloRetryRequest"); |
7380 | 0 | if ((ret = RestartHandshakeHash(ssl)) < 0) |
7381 | 0 | return ret; |
7382 | 0 | } |
7383 | | |
7384 | 0 | ssl->options.buildingMsg = 1; |
7385 | | #ifdef WOLFSSL_DTLS13 |
7386 | | if (ssl->options.dtls) |
7387 | | idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ; |
7388 | | #endif /* WOLFSSL_DTLS13 */ |
7389 | | |
7390 | | /* Protocol version, server random, session id, cipher suite, compression |
7391 | | * and extensions. |
7392 | | */ |
7393 | 0 | length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz + |
7394 | 0 | SUITE_LEN + COMP_LEN; |
7395 | 0 | ret = TLSX_GetResponseSize(ssl, extMsgType, &length); |
7396 | 0 | if (ret != 0) |
7397 | 0 | return ret; |
7398 | 0 | sendSz = (int)(idx + length); |
7399 | | |
7400 | | /* Check buffers are big enough and grow if needed. */ |
7401 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7402 | 0 | return ret; |
7403 | | |
7404 | | /* Get position in output buffer to write new message to. */ |
7405 | 0 | output = GetOutputBuffer(ssl); |
7406 | | |
7407 | | /* Put the record and handshake headers on. */ |
7408 | 0 | AddTls13Headers(output, length, server_hello, ssl); |
7409 | | |
7410 | | /* The protocol version must be TLS v1.2 for middleboxes. */ |
7411 | 0 | output[idx++] = ssl->version.major; |
7412 | 0 | output[idx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
7413 | |
|
7414 | 0 | if (extMsgType == server_hello) { |
7415 | | /* Generate server random. */ |
7416 | 0 | if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0) |
7417 | 0 | return ret; |
7418 | 0 | } |
7419 | 0 | else { |
7420 | | /* HelloRetryRequest message has fixed value for random. */ |
7421 | 0 | XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN); |
7422 | 0 | } |
7423 | | |
7424 | | #if defined(HAVE_ECH) |
7425 | | /* last 8 bytes of server random */ |
7426 | | acceptOffset = idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
7427 | | #endif |
7428 | | |
7429 | | /* Store in SSL for debugging. */ |
7430 | 0 | XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN); |
7431 | 0 | idx += RAN_LEN; |
7432 | |
|
7433 | | #ifdef WOLFSSL_DEBUG_TLS |
7434 | | WOLFSSL_MSG("Server random"); |
7435 | | WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN); |
7436 | | #endif |
7437 | |
|
7438 | 0 | output[idx++] = ssl->session->sessionIDSz; |
7439 | 0 | if (ssl->session->sessionIDSz > 0) { |
7440 | 0 | XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz); |
7441 | 0 | idx += ssl->session->sessionIDSz; |
7442 | 0 | } |
7443 | | |
7444 | | /* Chosen cipher suite */ |
7445 | 0 | output[idx++] = ssl->options.cipherSuite0; |
7446 | 0 | output[idx++] = ssl->options.cipherSuite; |
7447 | | #ifdef WOLFSSL_DEBUG_TLS |
7448 | | WOLFSSL_MSG("Chosen cipher suite:"); |
7449 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
7450 | | ssl->options.cipherSuite)); |
7451 | | #endif |
7452 | | |
7453 | | /* Compression not supported in TLS v1.3. */ |
7454 | 0 | output[idx++] = 0; |
7455 | | |
7456 | | /* Extensions */ |
7457 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL); |
7458 | 0 | if (ret != 0) |
7459 | 0 | return ret; |
7460 | | |
7461 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7462 | | if (ssl->options.sendCookie && extMsgType == hello_retry_request) { |
7463 | | /* Reset the hashes from here. We will be able to restart the hashes |
7464 | | * from the cookie in RestartHandshakeHashWithCookie */ |
7465 | | #ifdef WOLFSSL_DTLS13 |
7466 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated |
7467 | | * in dtls.c */ |
7468 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) |
7469 | | ret = 0; |
7470 | | else |
7471 | | #endif |
7472 | | ret = InitHandshakeHashes(ssl); |
7473 | | } |
7474 | | else |
7475 | | #endif |
7476 | 0 | { |
7477 | | #ifdef WOLFSSL_DTLS13 |
7478 | | if (ssl->options.dtls) { |
7479 | | ret = Dtls13HashHandshake( |
7480 | | ssl, |
7481 | | output + Dtls13GetRlHeaderLength(ssl, 0) , |
7482 | | (word16)sendSz - Dtls13GetRlHeaderLength(ssl, 0)); |
7483 | | } |
7484 | | else |
7485 | | #endif /* WOLFSSL_DTLS13 */ |
7486 | 0 | { |
7487 | | #if defined(HAVE_ECH) |
7488 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7489 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7490 | | if (echX == NULL) |
7491 | | return WOLFSSL_FATAL_ERROR; |
7492 | | /* use hrr offset */ |
7493 | | if (extMsgType == hello_retry_request) { |
7494 | | acceptOffset = |
7495 | | (word32)(((WOLFSSL_ECH*)echX->data)->confBuf - output); |
7496 | | acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
7497 | | acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
7498 | | } |
7499 | | /* replace the last 8 bytes of server random with the accept */ |
7500 | | if (((WOLFSSL_ECH*)echX->data)->state == ECH_PARSED_INTERNAL) { |
7501 | | if (ret == 0) { |
7502 | | ret = EchWriteAcceptance(ssl, acceptLabel, |
7503 | | acceptLabelSz, output + RECORD_HEADER_SZ, |
7504 | | acceptOffset - RECORD_HEADER_SZ, |
7505 | | sendSz - RECORD_HEADER_SZ, extMsgType); |
7506 | | } |
7507 | | if (extMsgType == hello_retry_request) { |
7508 | | /* reset the ech state for round 2 */ |
7509 | | ((WOLFSSL_ECH*)echX->data)->state = ECH_WRITE_NONE; |
7510 | | } |
7511 | | else { |
7512 | | if (ret == 0) { |
7513 | | /* update serverRandom on success */ |
7514 | | XMEMCPY(ssl->arrays->serverRandom, |
7515 | | output + acceptOffset - |
7516 | | (RAN_LEN -ECH_ACCEPT_CONFIRMATION_SZ), RAN_LEN); |
7517 | | } |
7518 | | /* remove ech so we don't keep sending it in write */ |
7519 | | TLSX_Remove(&ssl->extensions, TLSX_ECH, ssl->heap); |
7520 | | } |
7521 | | } |
7522 | | } |
7523 | | #endif |
7524 | 0 | if (ret == 0) |
7525 | 0 | ret = HashOutput(ssl, output, sendSz, 0); |
7526 | 0 | } |
7527 | 0 | } |
7528 | |
|
7529 | 0 | if (ret != 0) |
7530 | 0 | return ret; |
7531 | | |
7532 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7533 | | if (ssl->hsInfoOn) |
7534 | | AddPacketName(ssl, "ServerHello"); |
7535 | | if (ssl->toInfoOn) { |
7536 | | ret = AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz, |
7537 | | WRITE_PROTO, 0, ssl->heap); |
7538 | | if (ret != 0) |
7539 | | return ret; |
7540 | | } |
7541 | | #endif |
7542 | | |
7543 | 0 | if (extMsgType == server_hello) |
7544 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
7545 | |
|
7546 | 0 | ssl->options.buildingMsg = 0; |
7547 | | #ifdef WOLFSSL_DTLS13 |
7548 | | if (ssl->options.dtls) { |
7549 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)sendSz, |
7550 | | (enum HandShakeType)extMsgType, 0); |
7551 | | |
7552 | | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7553 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7554 | | return ret; |
7555 | | } |
7556 | | #endif /* WOLFSSL_DTLS13 */ |
7557 | |
|
7558 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7559 | |
|
7560 | 0 | if (!ssl->options.groupMessages || extMsgType != server_hello) |
7561 | 0 | ret = SendBuffered(ssl); |
7562 | |
|
7563 | 0 | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7564 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7565 | |
|
7566 | 0 | return ret; |
7567 | 0 | } |
7568 | | |
7569 | | /* handle generation of TLS 1.3 encrypted_extensions (8) */ |
7570 | | /* Send the rest of the extensions encrypted under the handshake key. |
7571 | | * This message is always encrypted in TLS v1.3. |
7572 | | * Only a server will send this message. |
7573 | | * |
7574 | | * ssl The SSL/TLS object. |
7575 | | * returns 0 on success, otherwise failure. |
7576 | | */ |
7577 | | static int SendTls13EncryptedExtensions(WOLFSSL* ssl) |
7578 | 0 | { |
7579 | 0 | int ret; |
7580 | 0 | byte* output; |
7581 | 0 | word16 length = 0; |
7582 | 0 | word32 idx; |
7583 | 0 | int sendSz; |
7584 | |
|
7585 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7586 | 0 | WOLFSSL_ENTER("SendTls13EncryptedExtensions"); |
7587 | |
|
7588 | 0 | ssl->options.buildingMsg = 1; |
7589 | 0 | ssl->keys.encryptionOn = 1; |
7590 | |
|
7591 | | #ifdef WOLFSSL_DTLS13 |
7592 | | if (ssl->options.dtls) { |
7593 | | idx = Dtls13GetHeadersLength(ssl, encrypted_extensions); |
7594 | | } |
7595 | | else |
7596 | | #endif /* WOLFSSL_DTLS13 */ |
7597 | 0 | { |
7598 | 0 | idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7599 | 0 | } |
7600 | |
|
7601 | 0 | #if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
7602 | 0 | if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0) |
7603 | 0 | return ret; |
7604 | 0 | #endif |
7605 | | |
7606 | | /* Derive the handshake secret now that we are at first message to be |
7607 | | * encrypted under the keys. |
7608 | | */ |
7609 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
7610 | 0 | return ret; |
7611 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
7612 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) |
7613 | 0 | return ret; |
7614 | | |
7615 | | /* Setup encrypt/decrypt keys for following messages. */ |
7616 | | #ifdef WOLFSSL_EARLY_DATA |
7617 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
7618 | | return ret; |
7619 | | if (ssl->earlyData != process_early_data) { |
7620 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
7621 | | return ret; |
7622 | | } |
7623 | | #else |
7624 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
7625 | 0 | return ret; |
7626 | 0 | #endif |
7627 | | #ifdef WOLFSSL_QUIC |
7628 | | if (IsAtLeastTLSv1_3(ssl->version) && WOLFSSL_IS_QUIC(ssl)) { |
7629 | | ret = wolfSSL_quic_add_transport_extensions(ssl, encrypted_extensions); |
7630 | | if (ret != 0) |
7631 | | return ret; |
7632 | | } |
7633 | | #endif |
7634 | | |
7635 | | #ifdef WOLFSSL_DTLS13 |
7636 | | if (ssl->options.dtls) { |
7637 | | w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
7638 | | ssl->dtls13Epoch = epochHandshake; |
7639 | | |
7640 | | ret = Dtls13SetEpochKeys( |
7641 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
7642 | | if (ret != 0) |
7643 | | return ret; |
7644 | | |
7645 | | } |
7646 | | #endif /* WOLFSSL_DTLS13 */ |
7647 | | |
7648 | 0 | ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length); |
7649 | 0 | if (ret != 0) |
7650 | 0 | return ret; |
7651 | | |
7652 | 0 | sendSz = (int)(idx + length); |
7653 | | /* Encryption always on. */ |
7654 | 0 | sendSz += MAX_MSG_EXTRA; |
7655 | | |
7656 | | /* Check buffers are big enough and grow if needed. */ |
7657 | 0 | ret = CheckAvailableSize(ssl, sendSz); |
7658 | 0 | if (ret != 0) |
7659 | 0 | return ret; |
7660 | | |
7661 | | /* Get position in output buffer to write new message to. */ |
7662 | 0 | output = GetOutputBuffer(ssl); |
7663 | | |
7664 | | /* Put the record and handshake headers on. */ |
7665 | 0 | AddTls13Headers(output, length, encrypted_extensions, ssl); |
7666 | |
|
7667 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL); |
7668 | 0 | if (ret != 0) |
7669 | 0 | return ret; |
7670 | 0 | idx += length; |
7671 | |
|
7672 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7673 | | if (ssl->hsInfoOn) |
7674 | | AddPacketName(ssl, "EncryptedExtensions"); |
7675 | | if (ssl->toInfoOn) { |
7676 | | ret = AddPacketInfo(ssl, "EncryptedExtensions", handshake, output, |
7677 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
7678 | | if (ret != 0) |
7679 | | return ret; |
7680 | | } |
7681 | | #endif |
7682 | |
|
7683 | | #ifdef WOLFSSL_DTLS13 |
7684 | | if (ssl->options.dtls) { |
7685 | | ssl->options.buildingMsg = 0; |
7686 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)idx, |
7687 | | encrypted_extensions, 1); |
7688 | | |
7689 | | if (ret == 0) |
7690 | | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7691 | | |
7692 | | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7693 | | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7694 | | |
7695 | | return ret; |
7696 | | } |
7697 | | #endif /* WOLFSSL_DTLS13 */ |
7698 | | |
7699 | | /* This handshake message is always encrypted. */ |
7700 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
7701 | 0 | (int)(idx - RECORD_HEADER_SZ), |
7702 | 0 | handshake, 1, 0, 0); |
7703 | 0 | if (sendSz < 0) |
7704 | 0 | return sendSz; |
7705 | | |
7706 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7707 | 0 | ssl->options.buildingMsg = 0; |
7708 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7709 | |
|
7710 | 0 | if (!ssl->options.groupMessages) |
7711 | 0 | ret = SendBuffered(ssl); |
7712 | | |
7713 | |
|
7714 | 0 | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7715 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7716 | |
|
7717 | 0 | return ret; |
7718 | 0 | } |
7719 | | |
7720 | | #ifndef NO_CERTS |
7721 | | /* handle generation TLS v1.3 certificate_request (13) */ |
7722 | | /* Send the TLS v1.3 CertificateRequest message. |
7723 | | * This message is always encrypted in TLS v1.3. |
7724 | | * Only a server will send this message. |
7725 | | * |
7726 | | * ssl SSL/TLS object. |
7727 | | * reqCtx Request context. |
7728 | | * reqCtxLen Length of context. 0 when sending as part of handshake. |
7729 | | * returns 0 on success, otherwise failure. |
7730 | | */ |
7731 | | static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx, |
7732 | | word32 reqCtxLen) |
7733 | 0 | { |
7734 | 0 | byte* output; |
7735 | 0 | int ret; |
7736 | 0 | int sendSz; |
7737 | 0 | word32 i; |
7738 | 0 | word32 reqSz; |
7739 | 0 | word16 hashSigAlgoSz = 0; |
7740 | 0 | SignatureAlgorithms* sa; |
7741 | |
|
7742 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7743 | 0 | WOLFSSL_ENTER("SendTls13CertificateRequest"); |
7744 | |
|
7745 | 0 | ssl->options.buildingMsg = 1; |
7746 | |
|
7747 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) |
7748 | 0 | return SIDE_ERROR; |
7749 | | |
7750 | | /* Get the length of the hashSigAlgo buffer */ |
7751 | 0 | InitSuitesHashSigAlgo(NULL, SIG_ALL, 1, ssl->buffers.keySz, |
7752 | 0 | &hashSigAlgoSz); |
7753 | 0 | sa = TLSX_SignatureAlgorithms_New(ssl, hashSigAlgoSz, ssl->heap); |
7754 | 0 | if (sa == NULL) |
7755 | 0 | return MEMORY_ERROR; |
7756 | 0 | InitSuitesHashSigAlgo(sa->hashSigAlgo, SIG_ALL, 1, ssl->buffers.keySz, |
7757 | 0 | &hashSigAlgoSz); |
7758 | 0 | ret = TLSX_Push(&ssl->extensions, TLSX_SIGNATURE_ALGORITHMS, sa, ssl->heap); |
7759 | 0 | if (ret != 0) { |
7760 | 0 | TLSX_SignatureAlgorithms_FreeAll(sa, ssl->heap); |
7761 | 0 | return ret; |
7762 | 0 | } |
7763 | | |
7764 | 0 | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7765 | | #ifdef WOLFSSL_DTLS13 |
7766 | | if (ssl->options.dtls) |
7767 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
7768 | | #endif /* WOLFSSL_DTLS13 */ |
7769 | |
|
7770 | 0 | reqSz = (word16)(OPAQUE8_LEN + reqCtxLen); |
7771 | 0 | ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz); |
7772 | 0 | if (ret != 0) |
7773 | 0 | return ret; |
7774 | | |
7775 | 0 | sendSz = (int)(i + reqSz); |
7776 | | /* Always encrypted and make room for padding. */ |
7777 | 0 | sendSz += MAX_MSG_EXTRA; |
7778 | | |
7779 | | /* Check buffers are big enough and grow if needed. */ |
7780 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7781 | 0 | return ret; |
7782 | | |
7783 | | /* Get position in output buffer to write new message to. */ |
7784 | 0 | output = GetOutputBuffer(ssl); |
7785 | | |
7786 | | /* Put the record and handshake headers on. */ |
7787 | 0 | AddTls13Headers(output, reqSz, certificate_request, ssl); |
7788 | | |
7789 | | /* Certificate request context. */ |
7790 | 0 | output[i++] = (byte)reqCtxLen; |
7791 | 0 | if (reqCtxLen != 0) { |
7792 | 0 | XMEMCPY(output + i, reqCtx, reqCtxLen); |
7793 | 0 | i += reqCtxLen; |
7794 | 0 | } |
7795 | | |
7796 | | /* Certificate extensions. */ |
7797 | 0 | reqSz = 0; |
7798 | 0 | ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz); |
7799 | 0 | if (ret != 0) |
7800 | 0 | return ret; |
7801 | 0 | i += reqSz; |
7802 | |
|
7803 | | #ifdef WOLFSSL_DTLS13 |
7804 | | if (ssl->options.dtls) { |
7805 | | ssl->options.buildingMsg = 0; |
7806 | | ret = |
7807 | | Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
7808 | | certificate_request, 1); |
7809 | | |
7810 | | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
7811 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7812 | | |
7813 | | return ret; |
7814 | | |
7815 | | } |
7816 | | #endif /* WOLFSSL_DTLS13 */ |
7817 | | |
7818 | | /* Always encrypted. */ |
7819 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
7820 | 0 | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
7821 | 0 | if (sendSz < 0) |
7822 | 0 | return sendSz; |
7823 | | |
7824 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7825 | | if (ssl->hsInfoOn) |
7826 | | AddPacketName(ssl, "CertificateRequest"); |
7827 | | if (ssl->toInfoOn) { |
7828 | | ret = AddPacketInfo(ssl, "CertificateRequest", handshake, output, |
7829 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
7830 | | if (ret != 0) |
7831 | | return ret; |
7832 | | } |
7833 | | #endif |
7834 | | |
7835 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7836 | 0 | ssl->options.buildingMsg = 0; |
7837 | 0 | if (!ssl->options.groupMessages) |
7838 | 0 | ret = SendBuffered(ssl); |
7839 | |
|
7840 | 0 | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
7841 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7842 | |
|
7843 | 0 | return ret; |
7844 | 0 | } |
7845 | | #endif /* NO_CERTS */ |
7846 | | #endif /* NO_WOLFSSL_SERVER */ |
7847 | | |
7848 | | #ifndef NO_CERTS |
7849 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
7850 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
7851 | | /* Encode the signature algorithm into buffer. |
7852 | | * |
7853 | | * hashalgo The hash algorithm. |
7854 | | * hsType The signature type. |
7855 | | * output The buffer to encode into. |
7856 | | */ |
7857 | | static WC_INLINE void EncodeSigAlg(byte hashAlgo, byte hsType, byte* output) |
7858 | | { |
7859 | | switch (hsType) { |
7860 | | #ifdef HAVE_ECC |
7861 | | case ecc_dsa_sa_algo: |
7862 | | output[0] = hashAlgo; |
7863 | | output[1] = ecc_dsa_sa_algo; |
7864 | | break; |
7865 | | #endif |
7866 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
7867 | | case sm2_sa_algo: |
7868 | | output[0] = SM2_SA_MAJOR; |
7869 | | output[1] = SM2_SA_MINOR; |
7870 | | break; |
7871 | | #endif |
7872 | | #ifdef HAVE_ED25519 |
7873 | | /* ED25519: 0x0807 */ |
7874 | | case ed25519_sa_algo: |
7875 | | output[0] = ED25519_SA_MAJOR; |
7876 | | output[1] = ED25519_SA_MINOR; |
7877 | | (void)hashAlgo; |
7878 | | break; |
7879 | | #endif |
7880 | | #ifdef HAVE_ED448 |
7881 | | /* ED448: 0x0808 */ |
7882 | | case ed448_sa_algo: |
7883 | | output[0] = ED448_SA_MAJOR; |
7884 | | output[1] = ED448_SA_MINOR; |
7885 | | (void)hashAlgo; |
7886 | | break; |
7887 | | #endif |
7888 | | #ifndef NO_RSA |
7889 | | /* PSS signatures: 0x080[4-6] */ |
7890 | | case rsa_pss_sa_algo: |
7891 | | output[0] = rsa_pss_sa_algo; |
7892 | | output[1] = hashAlgo; |
7893 | | break; |
7894 | | #endif |
7895 | | #ifdef HAVE_FALCON |
7896 | | case falcon_level1_sa_algo: |
7897 | | output[0] = FALCON_LEVEL1_SA_MAJOR; |
7898 | | output[1] = FALCON_LEVEL1_SA_MINOR; |
7899 | | break; |
7900 | | case falcon_level5_sa_algo: |
7901 | | output[0] = FALCON_LEVEL5_SA_MAJOR; |
7902 | | output[1] = FALCON_LEVEL5_SA_MINOR; |
7903 | | break; |
7904 | | #endif |
7905 | | #ifdef HAVE_DILITHIUM |
7906 | | case dilithium_level2_sa_algo: |
7907 | | output[0] = DILITHIUM_LEVEL2_SA_MAJOR; |
7908 | | output[1] = DILITHIUM_LEVEL2_SA_MINOR; |
7909 | | break; |
7910 | | case dilithium_level3_sa_algo: |
7911 | | output[0] = DILITHIUM_LEVEL3_SA_MAJOR; |
7912 | | output[1] = DILITHIUM_LEVEL3_SA_MINOR; |
7913 | | break; |
7914 | | case dilithium_level5_sa_algo: |
7915 | | output[0] = DILITHIUM_LEVEL5_SA_MAJOR; |
7916 | | output[1] = DILITHIUM_LEVEL5_SA_MINOR; |
7917 | | break; |
7918 | | #endif |
7919 | | default: |
7920 | | break; |
7921 | | } |
7922 | | } |
7923 | | |
7924 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
7925 | | /* These match up with what the OQS team has defined. */ |
7926 | | #define HYBRID_SA_MAJOR 0xFE |
7927 | | #define HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR 0xA1 |
7928 | | #define HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR 0xA2 |
7929 | | #define HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR 0xA4 |
7930 | | #define HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR 0xA6 |
7931 | | #define HYBRID_P256_FALCON_LEVEL1_SA_MINOR 0xAF |
7932 | | #define HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR 0xB0 |
7933 | | #define HYBRID_P521_FALCON_LEVEL5_SA_MINOR 0xB2 |
7934 | | |
7935 | | /* Custom defined ones for PQC first */ |
7936 | | #define HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR 0xD1 |
7937 | | #define HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR 0xD2 |
7938 | | #define HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR 0xD3 |
7939 | | #define HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR 0xD4 |
7940 | | #define HYBRID_FALCON_LEVEL1_P256_SA_MINOR 0xD5 |
7941 | | #define HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR 0xD6 |
7942 | | #define HYBRID_FALCON_LEVEL5_P521_SA_MINOR 0xD7 |
7943 | | |
7944 | | |
7945 | | static void EncodeDualSigAlg(byte sigAlg, byte altSigAlg, byte* output) |
7946 | | { |
7947 | | /* Initialize output to error indicator. */ |
7948 | | output[0] = 0x0; |
7949 | | output[1] = 0x0; |
7950 | | |
7951 | | if (sigAlg == ecc_dsa_sa_algo && altSigAlg == dilithium_level2_sa_algo) { |
7952 | | output[1] = HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR; |
7953 | | } |
7954 | | else if (sigAlg == rsa_pss_sa_algo && |
7955 | | altSigAlg == dilithium_level2_sa_algo) { |
7956 | | output[1] = HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR; |
7957 | | } |
7958 | | else if (sigAlg == ecc_dsa_sa_algo && |
7959 | | altSigAlg == dilithium_level3_sa_algo) { |
7960 | | output[1] = HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR; |
7961 | | } |
7962 | | else if (sigAlg == ecc_dsa_sa_algo && |
7963 | | altSigAlg == dilithium_level5_sa_algo) { |
7964 | | output[1] = HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR; |
7965 | | } |
7966 | | else if (sigAlg == ecc_dsa_sa_algo && |
7967 | | altSigAlg == falcon_level1_sa_algo) { |
7968 | | output[1] = HYBRID_P256_FALCON_LEVEL1_SA_MINOR; |
7969 | | } |
7970 | | else if (sigAlg == rsa_pss_sa_algo && |
7971 | | altSigAlg == falcon_level1_sa_algo) { |
7972 | | output[1] = HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR; |
7973 | | } |
7974 | | else if (sigAlg == ecc_dsa_sa_algo && |
7975 | | altSigAlg == falcon_level5_sa_algo) { |
7976 | | output[1] = HYBRID_P521_FALCON_LEVEL5_SA_MINOR; |
7977 | | } |
7978 | | else if (sigAlg == dilithium_level2_sa_algo && |
7979 | | altSigAlg == ecc_dsa_sa_algo) { |
7980 | | output[1] = HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR; |
7981 | | } |
7982 | | else if (sigAlg == dilithium_level2_sa_algo && |
7983 | | altSigAlg == rsa_pss_sa_algo) { |
7984 | | output[1] = HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR; |
7985 | | } |
7986 | | else if (sigAlg == dilithium_level3_sa_algo && |
7987 | | altSigAlg == ecc_dsa_sa_algo) { |
7988 | | output[1] = HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR; |
7989 | | } |
7990 | | else if (sigAlg == dilithium_level5_sa_algo && |
7991 | | altSigAlg == ecc_dsa_sa_algo) { |
7992 | | output[1] = HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR; |
7993 | | } |
7994 | | else if (sigAlg == falcon_level1_sa_algo && |
7995 | | altSigAlg == ecc_dsa_sa_algo) { |
7996 | | output[1] = HYBRID_FALCON_LEVEL1_P256_SA_MINOR; |
7997 | | } |
7998 | | else if (sigAlg == falcon_level1_sa_algo && |
7999 | | altSigAlg == rsa_pss_sa_algo) { |
8000 | | output[1] = HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR; |
8001 | | } |
8002 | | else if (sigAlg == falcon_level5_sa_algo && |
8003 | | altSigAlg == ecc_dsa_sa_algo) { |
8004 | | output[1] = HYBRID_FALCON_LEVEL5_P521_SA_MINOR; |
8005 | | } |
8006 | | |
8007 | | if (output[1] != 0x0) { |
8008 | | output[0] = HYBRID_SA_MAJOR; |
8009 | | } |
8010 | | } |
8011 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8012 | | |
8013 | | static enum wc_MACAlgorithm GetNewSAHashAlgo(int typeIn) |
8014 | 0 | { |
8015 | 0 | switch (typeIn) { |
8016 | 0 | case RSA_PSS_RSAE_SHA256_MINOR: |
8017 | 0 | case RSA_PSS_PSS_SHA256_MINOR: |
8018 | 0 | return sha256_mac; |
8019 | | |
8020 | 0 | case RSA_PSS_RSAE_SHA384_MINOR: |
8021 | 0 | case RSA_PSS_PSS_SHA384_MINOR: |
8022 | 0 | return sha384_mac; |
8023 | | |
8024 | 0 | case RSA_PSS_RSAE_SHA512_MINOR: |
8025 | 0 | case RSA_PSS_PSS_SHA512_MINOR: |
8026 | 0 | case ED25519_SA_MINOR: |
8027 | 0 | case ED448_SA_MINOR: |
8028 | 0 | return sha512_mac; |
8029 | 0 | default: |
8030 | 0 | return no_mac; |
8031 | 0 | } |
8032 | 0 | } |
8033 | | |
8034 | | /* Decode the signature algorithm. |
8035 | | * |
8036 | | * input The encoded signature algorithm. |
8037 | | * hashalgo The hash algorithm. |
8038 | | * hsType The signature type. |
8039 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8040 | | */ |
8041 | | static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo, |
8042 | | byte* hsType) |
8043 | 0 | { |
8044 | 0 | int ret = 0; |
8045 | |
|
8046 | 0 | switch (input[0]) { |
8047 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8048 | 0 | case SM2_SA_MAJOR: |
8049 | 0 | if (input[1] == SM2_SA_MINOR) { |
8050 | 0 | *hsType = sm2_sa_algo; |
8051 | 0 | *hashAlgo = sm3_mac; |
8052 | 0 | } |
8053 | 0 | else |
8054 | 0 | ret = INVALID_PARAMETER; |
8055 | 0 | break; |
8056 | 0 | #endif |
8057 | 0 | case NEW_SA_MAJOR: |
8058 | 0 | *hashAlgo = GetNewSAHashAlgo(input[1]); |
8059 | | |
8060 | | /* PSS encryption: 0x080[4-6] */ |
8061 | 0 | if (input[1] >= RSA_PSS_RSAE_SHA256_MINOR && |
8062 | 0 | input[1] <= RSA_PSS_RSAE_SHA512_MINOR) { |
8063 | 0 | *hsType = input[0]; |
8064 | 0 | } |
8065 | | /* PSS signature: 0x080[9-B] */ |
8066 | 0 | else if (input[1] >= RSA_PSS_PSS_SHA256_MINOR && |
8067 | 0 | input[1] <= RSA_PSS_PSS_SHA512_MINOR) { |
8068 | 0 | *hsType = input[0]; |
8069 | 0 | } |
8070 | 0 | #ifdef HAVE_ED25519 |
8071 | | /* ED25519: 0x0807 */ |
8072 | 0 | else if (input[1] == ED25519_SA_MINOR) { |
8073 | 0 | *hsType = ed25519_sa_algo; |
8074 | | /* Hash performed as part of sign/verify operation. */ |
8075 | 0 | } |
8076 | 0 | #endif |
8077 | 0 | #ifdef HAVE_ED448 |
8078 | | /* ED448: 0x0808 */ |
8079 | 0 | else if (input[1] == ED448_SA_MINOR) { |
8080 | 0 | *hsType = ed448_sa_algo; |
8081 | | /* Hash performed as part of sign/verify operation. */ |
8082 | 0 | } |
8083 | 0 | #endif |
8084 | 0 | else |
8085 | 0 | ret = INVALID_PARAMETER; |
8086 | 0 | break; |
8087 | | #if defined(HAVE_FALCON) |
8088 | | case FALCON_SA_MAJOR: |
8089 | | if (input[1] == FALCON_LEVEL1_SA_MINOR) { |
8090 | | *hsType = falcon_level1_sa_algo; |
8091 | | /* Hash performed as part of sign/verify operation. */ |
8092 | | *hashAlgo = sha512_mac; |
8093 | | } else if (input[1] == FALCON_LEVEL5_SA_MINOR) { |
8094 | | *hsType = falcon_level5_sa_algo; |
8095 | | /* Hash performed as part of sign/verify operation. */ |
8096 | | *hashAlgo = sha512_mac; |
8097 | | } |
8098 | | else |
8099 | | ret = INVALID_PARAMETER; |
8100 | | break; |
8101 | | #endif /* HAVE_FALCON */ |
8102 | | #if defined(HAVE_DILITHIUM) |
8103 | | case DILITHIUM_SA_MAJOR: |
8104 | | if (input[1] == DILITHIUM_LEVEL2_SA_MINOR) { |
8105 | | *hsType = dilithium_level2_sa_algo; |
8106 | | /* Hash performed as part of sign/verify operation. */ |
8107 | | *hashAlgo = sha512_mac; |
8108 | | } else if (input[1] == DILITHIUM_LEVEL3_SA_MINOR) { |
8109 | | *hsType = dilithium_level3_sa_algo; |
8110 | | /* Hash performed as part of sign/verify operation. */ |
8111 | | *hashAlgo = sha512_mac; |
8112 | | } else if (input[1] == DILITHIUM_LEVEL5_SA_MINOR) { |
8113 | | *hsType = dilithium_level5_sa_algo; |
8114 | | /* Hash performed as part of sign/verify operation. */ |
8115 | | *hashAlgo = sha512_mac; |
8116 | | } |
8117 | | else |
8118 | | { |
8119 | | ret = INVALID_PARAMETER; |
8120 | | } |
8121 | | break; |
8122 | | #endif /* HAVE_DILITHIUM */ |
8123 | 0 | default: |
8124 | 0 | *hashAlgo = input[0]; |
8125 | 0 | *hsType = input[1]; |
8126 | 0 | break; |
8127 | 0 | } |
8128 | | |
8129 | 0 | return ret; |
8130 | 0 | } |
8131 | | |
8132 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8133 | | /* Decode the hybrid signature algorithm. |
8134 | | * |
8135 | | * input The encoded signature algorithm. |
8136 | | * hashalgo The hash algorithm. |
8137 | | * hsType The signature type. |
8138 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8139 | | */ |
8140 | | static WC_INLINE int DecodeTls13HybridSigAlg(byte* input, byte* hashAlg, |
8141 | | byte *sigAlg, byte *altSigAlg) |
8142 | | { |
8143 | | |
8144 | | if (input[0] != HYBRID_SA_MAJOR) { |
8145 | | return INVALID_PARAMETER; |
8146 | | } |
8147 | | |
8148 | | if (input[1] == HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR) { |
8149 | | *sigAlg = ecc_dsa_sa_algo; |
8150 | | *hashAlg = sha256_mac; |
8151 | | *altSigAlg = dilithium_level2_sa_algo; |
8152 | | } |
8153 | | else if (input[1] == HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR) { |
8154 | | *sigAlg = rsa_pss_sa_algo; |
8155 | | *hashAlg = sha256_mac; |
8156 | | *altSigAlg = dilithium_level2_sa_algo; |
8157 | | } |
8158 | | else if (input[1] == HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR) { |
8159 | | *sigAlg = ecc_dsa_sa_algo; |
8160 | | *hashAlg = sha384_mac; |
8161 | | *altSigAlg = dilithium_level3_sa_algo; |
8162 | | } |
8163 | | else if (input[1] == HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR) { |
8164 | | *sigAlg = ecc_dsa_sa_algo; |
8165 | | *hashAlg = sha512_mac; |
8166 | | *altSigAlg = dilithium_level5_sa_algo; |
8167 | | } |
8168 | | else if (input[1] == HYBRID_P256_FALCON_LEVEL1_SA_MINOR) { |
8169 | | *sigAlg = ecc_dsa_sa_algo; |
8170 | | *hashAlg = sha256_mac; |
8171 | | *altSigAlg = falcon_level1_sa_algo; |
8172 | | } |
8173 | | else if (input[1] == HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR) { |
8174 | | *sigAlg = rsa_pss_sa_algo; |
8175 | | *hashAlg = sha256_mac; |
8176 | | *altSigAlg = falcon_level1_sa_algo; |
8177 | | } |
8178 | | else if (input[1] == HYBRID_P521_FALCON_LEVEL5_SA_MINOR) { |
8179 | | *sigAlg = ecc_dsa_sa_algo; |
8180 | | *hashAlg = sha512_mac; |
8181 | | *altSigAlg = falcon_level5_sa_algo; |
8182 | | } |
8183 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR) { |
8184 | | *sigAlg = dilithium_level2_sa_algo; |
8185 | | *hashAlg = sha256_mac; |
8186 | | *altSigAlg = ecc_dsa_sa_algo; |
8187 | | } |
8188 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR) { |
8189 | | *sigAlg = dilithium_level2_sa_algo; |
8190 | | *hashAlg = sha256_mac; |
8191 | | *altSigAlg = rsa_pss_sa_algo; |
8192 | | } |
8193 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR) { |
8194 | | *sigAlg = dilithium_level3_sa_algo; |
8195 | | *hashAlg = sha384_mac; |
8196 | | *altSigAlg = ecc_dsa_sa_algo; |
8197 | | } |
8198 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR) { |
8199 | | *sigAlg = dilithium_level5_sa_algo; |
8200 | | *hashAlg = sha512_mac; |
8201 | | *altSigAlg = ecc_dsa_sa_algo; |
8202 | | } |
8203 | | else if (input[1] == HYBRID_FALCON_LEVEL1_P256_SA_MINOR) { |
8204 | | *sigAlg = falcon_level1_sa_algo; |
8205 | | *hashAlg = sha256_mac; |
8206 | | *altSigAlg = ecc_dsa_sa_algo; |
8207 | | } |
8208 | | else if (input[1] == HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR) { |
8209 | | *sigAlg = falcon_level1_sa_algo; |
8210 | | *hashAlg = sha256_mac; |
8211 | | *altSigAlg = rsa_pss_sa_algo; |
8212 | | } |
8213 | | else if (input[1] == HYBRID_FALCON_LEVEL5_P521_SA_MINOR) { |
8214 | | *sigAlg = falcon_level5_sa_algo; |
8215 | | *hashAlg = sha512_mac; |
8216 | | *altSigAlg = ecc_dsa_sa_algo; |
8217 | | } |
8218 | | else { |
8219 | | return INVALID_PARAMETER; |
8220 | | } |
8221 | | |
8222 | | return 0; |
8223 | | } |
8224 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8225 | | |
8226 | | /* Get the hash of the messages so far. |
8227 | | * |
8228 | | * ssl The SSL/TLS object. |
8229 | | * hash The buffer to write the hash to. |
8230 | | * returns the length of the hash. |
8231 | | */ |
8232 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash) |
8233 | 0 | { |
8234 | 0 | int ret = 0; |
8235 | 0 | switch (ssl->specs.mac_algorithm) { |
8236 | 0 | #ifndef NO_SHA256 |
8237 | 0 | case sha256_mac: |
8238 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
8239 | 0 | if (ret == 0) |
8240 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
8241 | 0 | break; |
8242 | 0 | #endif /* !NO_SHA256 */ |
8243 | 0 | #ifdef WOLFSSL_SHA384 |
8244 | 0 | case sha384_mac: |
8245 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
8246 | 0 | if (ret == 0) |
8247 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
8248 | 0 | break; |
8249 | 0 | #endif /* WOLFSSL_SHA384 */ |
8250 | | #ifdef WOLFSSL_TLS13_SHA512 |
8251 | | case sha512_mac: |
8252 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
8253 | | if (ret == 0) |
8254 | | ret = WC_SHA512_DIGEST_SIZE; |
8255 | | break; |
8256 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
8257 | 0 | #ifdef WOLFSSL_SM3 |
8258 | 0 | case sm3_mac: |
8259 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
8260 | 0 | if (ret == 0) |
8261 | 0 | ret = WC_SM3_DIGEST_SIZE; |
8262 | 0 | break; |
8263 | 0 | #endif /* WOLFSSL_SM3 */ |
8264 | 0 | default: |
8265 | 0 | break; |
8266 | 0 | } |
8267 | 0 | return ret; |
8268 | 0 | } |
8269 | | |
8270 | | /* The server certificate verification label. */ |
8271 | | static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8272 | | "TLS 1.3, server CertificateVerify"; |
8273 | | /* The client certificate verification label. */ |
8274 | | static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8275 | | "TLS 1.3, client CertificateVerify"; |
8276 | | /* The prefix byte in the signature data. */ |
8277 | | #define SIGNING_DATA_PREFIX_BYTE 0x20 |
8278 | | |
8279 | | /* Create the signature data for TLS v1.3 certificate verification. |
8280 | | * |
8281 | | * ssl The SSL/TLS object. |
8282 | | * sigData The signature data. |
8283 | | * sigDataSz The length of the signature data. |
8284 | | * check Indicates this is a check not create. |
8285 | | */ |
8286 | | int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz, |
8287 | | int check) |
8288 | 451 | { |
8289 | 451 | word16 idx; |
8290 | 451 | int side = ssl->options.side; |
8291 | 451 | int ret; |
8292 | | |
8293 | | /* Signature Data = Prefix | Label | Handshake Hash */ |
8294 | 451 | XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ); |
8295 | 451 | idx = SIGNING_DATA_PREFIX_SZ; |
8296 | | |
8297 | 451 | if ((side == WOLFSSL_SERVER_END && check) || |
8298 | 451 | (side == WOLFSSL_CLIENT_END && !check)) { |
8299 | 0 | XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ); |
8300 | 0 | } |
8301 | 451 | if ((side == WOLFSSL_CLIENT_END && check) || |
8302 | 451 | (side == WOLFSSL_SERVER_END && !check)) { |
8303 | 451 | XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ); |
8304 | 451 | } |
8305 | 451 | idx += CERT_VFY_LABEL_SZ; |
8306 | | |
8307 | 451 | ret = GetMsgHash(ssl, &sigData[idx]); |
8308 | 451 | if (ret < 0) |
8309 | 3 | return ret; |
8310 | | |
8311 | 448 | *sigDataSz = (word16)(idx + ret); |
8312 | 448 | ret = 0; |
8313 | | |
8314 | 448 | return ret; |
8315 | 451 | } |
8316 | | |
8317 | | #ifndef NO_RSA |
8318 | | /* Encode the PKCS #1.5 RSA signature. |
8319 | | * |
8320 | | * sig The buffer to place the encoded signature into. |
8321 | | * sigData The data to be signed. |
8322 | | * sigDataSz The size of the data to be signed. |
8323 | | * hashAlgo The hash algorithm to use when signing. |
8324 | | * returns the length of the encoded signature or negative on error. |
8325 | | */ |
8326 | | int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz, |
8327 | | int sigAlgo, int hashAlgo) |
8328 | 446 | { |
8329 | 446 | Digest digest; |
8330 | 446 | int hashSz = 0; |
8331 | 446 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8332 | 446 | byte* hash; |
8333 | | |
8334 | 446 | (void)sigAlgo; |
8335 | | |
8336 | 446 | hash = sig; |
8337 | | |
8338 | | /* Digest the signature data. */ |
8339 | 446 | switch (hashAlgo) { |
8340 | 0 | #ifndef NO_WOLFSSL_SHA256 |
8341 | 305 | case sha256_mac: |
8342 | 305 | ret = wc_InitSha256(&digest.sha256); |
8343 | 305 | if (ret == 0) { |
8344 | 305 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8345 | 305 | if (ret == 0) |
8346 | 303 | ret = wc_Sha256Final(&digest.sha256, hash); |
8347 | 305 | wc_Sha256Free(&digest.sha256); |
8348 | 305 | } |
8349 | 305 | hashSz = WC_SHA256_DIGEST_SIZE; |
8350 | 305 | break; |
8351 | 0 | #endif |
8352 | 0 | #ifdef WOLFSSL_SHA384 |
8353 | 65 | case sha384_mac: |
8354 | 65 | ret = wc_InitSha384(&digest.sha384); |
8355 | 65 | if (ret == 0) { |
8356 | 65 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8357 | 65 | if (ret == 0) |
8358 | 64 | ret = wc_Sha384Final(&digest.sha384, hash); |
8359 | 65 | wc_Sha384Free(&digest.sha384); |
8360 | 65 | } |
8361 | 65 | hashSz = WC_SHA384_DIGEST_SIZE; |
8362 | 65 | break; |
8363 | 0 | #endif |
8364 | 0 | #ifdef WOLFSSL_SHA512 |
8365 | 76 | case sha512_mac: |
8366 | 76 | ret = wc_InitSha512(&digest.sha512); |
8367 | 76 | if (ret == 0) { |
8368 | 76 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8369 | 76 | if (ret == 0) |
8370 | 75 | ret = wc_Sha512Final(&digest.sha512, hash); |
8371 | 76 | wc_Sha512Free(&digest.sha512); |
8372 | 76 | } |
8373 | 76 | hashSz = WC_SHA512_DIGEST_SIZE; |
8374 | 76 | break; |
8375 | 0 | #endif |
8376 | 0 | default: |
8377 | 0 | ret = BAD_FUNC_ARG; |
8378 | 0 | break; |
8379 | | |
8380 | 446 | } |
8381 | | |
8382 | 446 | if (ret != 0) |
8383 | 5 | return ret; |
8384 | | |
8385 | 441 | return hashSz; |
8386 | 446 | } |
8387 | | #endif /* !NO_RSA */ |
8388 | | |
8389 | | #ifdef HAVE_ECC |
8390 | | /* Encode the ECC signature. |
8391 | | * |
8392 | | * sigData The data to be signed. |
8393 | | * sigDataSz The size of the data to be signed. |
8394 | | * hashAlgo The hash algorithm to use when signing. |
8395 | | * returns the length of the encoded signature or negative on error. |
8396 | | */ |
8397 | | static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo) |
8398 | 0 | { |
8399 | 0 | Digest digest; |
8400 | 0 | int hashSz = 0; |
8401 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8402 | | |
8403 | | /* Digest the signature data. */ |
8404 | 0 | switch (hashAlgo) { |
8405 | 0 | #ifndef NO_WOLFSSL_SHA256 |
8406 | 0 | case sha256_mac: |
8407 | 0 | ret = wc_InitSha256(&digest.sha256); |
8408 | 0 | if (ret == 0) { |
8409 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8410 | 0 | if (ret == 0) |
8411 | 0 | ret = wc_Sha256Final(&digest.sha256, sigData); |
8412 | 0 | wc_Sha256Free(&digest.sha256); |
8413 | 0 | } |
8414 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
8415 | 0 | break; |
8416 | 0 | #endif |
8417 | 0 | #ifdef WOLFSSL_SHA384 |
8418 | 0 | case sha384_mac: |
8419 | 0 | ret = wc_InitSha384(&digest.sha384); |
8420 | 0 | if (ret == 0) { |
8421 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8422 | 0 | if (ret == 0) |
8423 | 0 | ret = wc_Sha384Final(&digest.sha384, sigData); |
8424 | 0 | wc_Sha384Free(&digest.sha384); |
8425 | 0 | } |
8426 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
8427 | 0 | break; |
8428 | 0 | #endif |
8429 | 0 | #ifdef WOLFSSL_SHA512 |
8430 | 0 | case sha512_mac: |
8431 | 0 | ret = wc_InitSha512(&digest.sha512); |
8432 | 0 | if (ret == 0) { |
8433 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8434 | 0 | if (ret == 0) |
8435 | 0 | ret = wc_Sha512Final(&digest.sha512, sigData); |
8436 | 0 | wc_Sha512Free(&digest.sha512); |
8437 | 0 | } |
8438 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
8439 | 0 | break; |
8440 | 0 | #endif |
8441 | 0 | default: |
8442 | 0 | ret = BAD_FUNC_ARG; |
8443 | 0 | break; |
8444 | 0 | } |
8445 | | |
8446 | 0 | if (ret != 0) |
8447 | 0 | return ret; |
8448 | | |
8449 | 0 | return hashSz; |
8450 | 0 | } |
8451 | | #endif /* HAVE_ECC */ |
8452 | | |
8453 | | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
8454 | | /* Check that the decrypted signature matches the encoded signature |
8455 | | * based on the digest of the signature data. |
8456 | | * |
8457 | | * ssl The SSL/TLS object. |
8458 | | * sigAlgo The signature algorithm used to generate signature. |
8459 | | * hashAlgo The hash algorithm used to generate signature. |
8460 | | * decSig The decrypted signature. |
8461 | | * decSigSz The size of the decrypted signature. |
8462 | | * returns 0 on success, otherwise failure. |
8463 | | */ |
8464 | | static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo, |
8465 | | byte* decSig, word32 decSigSz) |
8466 | 0 | { |
8467 | 0 | int ret = 0; |
8468 | 0 | byte sigData[MAX_SIG_DATA_SZ]; |
8469 | 0 | word16 sigDataSz; |
8470 | |
|
8471 | 0 | ret = CreateSigData(ssl, sigData, &sigDataSz, 1); |
8472 | 0 | if (ret != 0) |
8473 | 0 | return ret; |
8474 | | |
8475 | 0 | if (sigAlgo == rsa_pss_sa_algo) { |
8476 | 0 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
8477 | 0 | word32 sigSz; |
8478 | |
|
8479 | 0 | ret = ConvertHashPss(hashAlgo, &hashType, NULL); |
8480 | 0 | if (ret < 0) |
8481 | 0 | return ret; |
8482 | | |
8483 | | /* PSS signature can be done in-place */ |
8484 | 0 | ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz, |
8485 | 0 | sigAlgo, hashAlgo); |
8486 | 0 | if (ret < 0) |
8487 | 0 | return ret; |
8488 | 0 | sigSz = (word32)ret; |
8489 | |
|
8490 | 0 | ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz, |
8491 | 0 | hashType); |
8492 | 0 | } |
8493 | | |
8494 | 0 | return ret; |
8495 | 0 | } |
8496 | | #endif /* !NO_RSA && WC_RSA_PSS */ |
8497 | | #endif /* !NO_RSA || HAVE_ECC */ |
8498 | | |
8499 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
8500 | | /* Get the next certificate from the list for writing into the TLS v1.3 |
8501 | | * Certificate message. |
8502 | | * |
8503 | | * data The certificate list. |
8504 | | * length The length of the certificate data in the list. |
8505 | | * idx The index of the next certificate. |
8506 | | * returns the length of the certificate data. 0 indicates no more certificates |
8507 | | * in the list. |
8508 | | */ |
8509 | | static word32 NextCert(byte* data, word32 length, word32* idx) |
8510 | 0 | { |
8511 | 0 | word32 len; |
8512 | | |
8513 | | /* Is index at end of list. */ |
8514 | 0 | if (*idx == length) |
8515 | 0 | return 0; |
8516 | | |
8517 | | /* Length of the current ASN.1 encoded certificate. */ |
8518 | 0 | c24to32(data + *idx, &len); |
8519 | | /* Include the length field. */ |
8520 | 0 | len += 3; |
8521 | | |
8522 | | /* Move index to next certificate and return the current certificate's |
8523 | | * length. |
8524 | | */ |
8525 | 0 | *idx += len; |
8526 | 0 | return len; |
8527 | 0 | } |
8528 | | |
8529 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8530 | | /* Write certificate status request into certificate to buffer. |
8531 | | * |
8532 | | * ssl SSL/TLS object. |
8533 | | * certExts DerBuffer array. buffers written |
8534 | | * extSz word32 array. |
8535 | | * Length of the certificate status request data for the certificate. |
8536 | | * extSz_num number of the CSR written |
8537 | | * extIdx The index number of certificate status request data |
8538 | | * for the certificate. |
8539 | | * offset index offset |
8540 | | * returns Total number of bytes written. |
8541 | | */ |
8542 | | static int WriteCSRToBuffer(WOLFSSL* ssl, DerBuffer** certExts, |
8543 | | word16* extSz, word16 extSz_num) |
8544 | | { |
8545 | | int ret = 0; |
8546 | | TLSX* ext; |
8547 | | CertificateStatusRequest* csr; |
8548 | | word32 ex_offset = HELLO_EXT_TYPE_SZ + OPAQUE16_LEN /* extension type */ |
8549 | | + OPAQUE16_LEN /* extension length */; |
8550 | | word32 totalSz = 0; |
8551 | | word32 tmpSz; |
8552 | | word32 extIdx; |
8553 | | DerBuffer* der; |
8554 | | |
8555 | | ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
8556 | | csr = ext ? (CertificateStatusRequest*)ext->data : NULL; |
8557 | | |
8558 | | if (csr) { |
8559 | | for (extIdx = 0; extIdx < (word16)(extSz_num); extIdx++) { |
8560 | | tmpSz = TLSX_CSR_GetSize_ex(csr, 0, (int)extIdx); |
8561 | | |
8562 | | if (tmpSz > (OPAQUE8_LEN + OPAQUE24_LEN) && |
8563 | | certExts[extIdx] == NULL) { |
8564 | | /* csr extension is not zero */ |
8565 | | extSz[extIdx] = tmpSz; |
8566 | | |
8567 | | ret = AllocDer(&certExts[extIdx], extSz[extIdx] + ex_offset, |
8568 | | CERT_TYPE, ssl->heap); |
8569 | | if (ret < 0) |
8570 | | return ret; |
8571 | | der = certExts[extIdx]; |
8572 | | |
8573 | | /* write extension type */ |
8574 | | c16toa(ext->type, der->buffer |
8575 | | + OPAQUE16_LEN); |
8576 | | /* writes extension data length. */ |
8577 | | c16toa(extSz[extIdx], der->buffer |
8578 | | + HELLO_EXT_TYPE_SZ + OPAQUE16_LEN); |
8579 | | /* write extension data */ |
8580 | | extSz[extIdx] = (word16)TLSX_CSR_Write_ex(csr, |
8581 | | der->buffer + ex_offset, 0, extIdx); |
8582 | | /* add extension offset */ |
8583 | | extSz[extIdx] += (word16)ex_offset; |
8584 | | /* extension length */ |
8585 | | c16toa(extSz[extIdx] - OPAQUE16_LEN, |
8586 | | der->buffer); |
8587 | | } |
8588 | | totalSz += extSz[extIdx]; |
8589 | | } |
8590 | | } |
8591 | | else { |
8592 | | /* chain cert empty extension size */ |
8593 | | totalSz += OPAQUE16_LEN * extSz_num; |
8594 | | } |
8595 | | return (int)totalSz; |
8596 | | } |
8597 | | #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */ |
8598 | | /* Add certificate data and empty extension to output up to the fragment size. |
8599 | | * |
8600 | | * ssl SSL/TLS object. |
8601 | | * cert The certificate data to write out. |
8602 | | * len The length of the certificate data. |
8603 | | * extSz Length of the extension data with the certificate. |
8604 | | * idx The start of the certificate data to write out. |
8605 | | * fragSz The maximum size of this fragment. |
8606 | | * output The buffer to write to. |
8607 | | * extIdx The index number of the extension data with the certificate |
8608 | | * returns the number of bytes written. |
8609 | | */ |
8610 | | static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz, |
8611 | | word32 idx, word32 fragSz, byte* output, word16 extIdx) |
8612 | 495 | { |
8613 | 495 | word32 i = 0; |
8614 | 495 | word32 copySz = min(len - idx, fragSz); |
8615 | | |
8616 | 495 | if (idx < len) { |
8617 | 495 | XMEMCPY(output, cert + idx, copySz); |
8618 | 495 | i = copySz; |
8619 | 495 | if (copySz == fragSz) |
8620 | 14 | return i; |
8621 | 495 | } |
8622 | 481 | copySz = len + extSz - idx - i; |
8623 | | |
8624 | 481 | if (extSz == OPAQUE16_LEN) { |
8625 | 481 | if (copySz <= fragSz) { |
8626 | | /* Empty extension */ |
8627 | 481 | output[i++] = 0; |
8628 | 481 | output[i++] = 0; |
8629 | 481 | } |
8630 | 481 | } |
8631 | 0 | else { |
8632 | 0 | byte* certExts = ssl->buffers.certExts[extIdx]->buffer + idx + i - len; |
8633 | | /* Put out as much of the extensions' data as will fit in fragment. */ |
8634 | 0 | if (copySz > fragSz - i) |
8635 | 0 | copySz = fragSz - i; |
8636 | 0 | XMEMCPY(output + i, certExts, copySz); |
8637 | 0 | i += copySz; |
8638 | 0 | } |
8639 | | |
8640 | 481 | return i; |
8641 | 495 | } |
8642 | | |
8643 | | /* handle generation TLS v1.3 certificate (11) */ |
8644 | | /* Send the certificate for this end and any CAs that help with validation. |
8645 | | * This message is always encrypted in TLS v1.3. |
8646 | | * |
8647 | | * ssl The SSL/TLS object. |
8648 | | * returns 0 on success, otherwise failure. |
8649 | | */ |
8650 | | static int SendTls13Certificate(WOLFSSL* ssl) |
8651 | | { |
8652 | | int ret = 0; |
8653 | | word32 certSz, certChainSz, headerSz, listSz, payloadSz; |
8654 | | word16 extSz[MAX_CERT_EXTENSIONS]; |
8655 | | word16 extIdx = 0; |
8656 | | word32 maxFragment; |
8657 | | word32 totalextSz = 0; |
8658 | | word32 len = 0; |
8659 | | word32 idx = 0; |
8660 | | word32 offset = OPAQUE16_LEN; |
8661 | | byte* p = NULL; |
8662 | | byte certReqCtxLen = 0; |
8663 | | sword32 length; |
8664 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8665 | | byte* certReqCtx = NULL; |
8666 | | #endif |
8667 | | |
8668 | | #ifdef OPENSSL_EXTRA |
8669 | | WOLFSSL_X509* x509 = NULL; |
8670 | | WOLFSSL_EVP_PKEY* pkey = NULL; |
8671 | | #endif |
8672 | | |
8673 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND); |
8674 | | WOLFSSL_ENTER("SendTls13Certificate"); |
8675 | | |
8676 | | XMEMSET(extSz, 0, sizeof(extSz)); |
8677 | | |
8678 | | ssl->options.buildingMsg = 1; |
8679 | | |
8680 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8681 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
8682 | | certReqCtxLen = ssl->certReqCtx->len; |
8683 | | certReqCtx = &ssl->certReqCtx->ctx; |
8684 | | } |
8685 | | #endif |
8686 | | |
8687 | | #ifdef OPENSSL_EXTRA |
8688 | | /* call client cert callback if no cert has been loaded */ |
8689 | | if ((ssl->ctx->CBClientCert != NULL) && |
8690 | | (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) { |
8691 | | ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey); |
8692 | | if (ret == 1) { |
8693 | | if ((wolfSSL_CTX_use_certificate(ssl->ctx, x509) == WOLFSSL_SUCCESS) && |
8694 | | (wolfSSL_CTX_use_PrivateKey(ssl->ctx, pkey) == WOLFSSL_SUCCESS)) { |
8695 | | ssl->options.sendVerify = SEND_CERT; |
8696 | | } |
8697 | | wolfSSL_X509_free(x509); |
8698 | | x509 = NULL; |
8699 | | wolfSSL_EVP_PKEY_free(pkey); |
8700 | | } |
8701 | | } |
8702 | | #endif |
8703 | | |
8704 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
8705 | | certSz = 0; |
8706 | | certChainSz = 0; |
8707 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ; |
8708 | | length = (sword32)headerSz; |
8709 | | listSz = 0; |
8710 | | } |
8711 | | else { |
8712 | | if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) { |
8713 | | WOLFSSL_MSG("Send Cert missing certificate buffer"); |
8714 | | return NO_CERT_ERROR; |
8715 | | } |
8716 | | /* Certificate Data */ |
8717 | | certSz = ssl->buffers.certificate->length; |
8718 | | /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */ |
8719 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ + |
8720 | | CERT_HEADER_SZ; |
8721 | | /* set empty extension as default */ |
8722 | | for (extIdx = 0; extIdx < (word16)XELEM_CNT(extSz); extIdx++) |
8723 | | extSz[extIdx] = OPAQUE16_LEN; |
8724 | | |
8725 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8726 | | /* We only send CSR on the server side. On client side, the CSR data |
8727 | | * is populated with the server response. We would be sending the server |
8728 | | * its own stapling data. */ |
8729 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
8730 | | ret = WriteCSRToBuffer(ssl, &ssl->buffers.certExts[0], &extSz[0], |
8731 | | 1 /* +1 for leaf */ + ssl->buffers.certChainCnt); |
8732 | | if (ret < 0) |
8733 | | return ret; |
8734 | | totalextSz += ret; |
8735 | | ret = 0; /* Clear to signal no error */ |
8736 | | } |
8737 | | else |
8738 | | #endif |
8739 | | { |
8740 | | /* Leaf cert empty extension size */ |
8741 | | totalextSz += OPAQUE16_LEN; |
8742 | | /* chain cert empty extension size */ |
8743 | | totalextSz += OPAQUE16_LEN * ssl->buffers.certChainCnt; |
8744 | | } |
8745 | | |
8746 | | /* Length of message data with one certificate and extensions. */ |
8747 | | length = (sword32)(headerSz + certSz + totalextSz); |
8748 | | /* Length of list data with one certificate and extensions. */ |
8749 | | listSz = CERT_HEADER_SZ + certSz + totalextSz; |
8750 | | |
8751 | | /* Send rest of chain if sending cert (chain has leading size/s). */ |
8752 | | if (certSz > 0 && ssl->buffers.certChainCnt > 0) { |
8753 | | p = ssl->buffers.certChain->buffer; |
8754 | | /* Chain length including extensions. */ |
8755 | | certChainSz = ssl->buffers.certChain->length; |
8756 | | |
8757 | | length += certChainSz; |
8758 | | listSz += certChainSz; |
8759 | | } |
8760 | | else |
8761 | | certChainSz = 0; |
8762 | | } |
8763 | | |
8764 | | payloadSz = (word32)length; |
8765 | | |
8766 | | if (ssl->fragOffset != 0) |
8767 | | length -= (ssl->fragOffset + headerSz); |
8768 | | |
8769 | | maxFragment = (word32)wolfSSL_GetMaxFragSize(ssl, MAX_RECORD_SIZE); |
8770 | | |
8771 | | extIdx = 0; |
8772 | | |
8773 | | while (length > 0 && ret == 0) { |
8774 | | byte* output = NULL; |
8775 | | word32 fragSz = 0; |
8776 | | word32 i = RECORD_HEADER_SZ; |
8777 | | int sendSz = RECORD_HEADER_SZ; |
8778 | | |
8779 | | #ifdef WOLFSSL_DTLS13 |
8780 | | if (ssl->options.dtls) { |
8781 | | i = Dtls13GetRlHeaderLength(ssl, 1); |
8782 | | sendSz = (int)i; |
8783 | | } |
8784 | | #endif /* WOLFSSL_DTLS13 */ |
8785 | | |
8786 | | if (ssl->fragOffset == 0) { |
8787 | | if (headerSz + certSz + totalextSz + certChainSz <= |
8788 | | maxFragment - HANDSHAKE_HEADER_SZ) { |
8789 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
8790 | | } |
8791 | | #ifdef WOLFSSL_DTLS13 |
8792 | | else if (ssl->options.dtls){ |
8793 | | /* short-circuit the fragmentation logic here. DTLS |
8794 | | fragmentation will be done in dtls13HandshakeSend() */ |
8795 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
8796 | | } |
8797 | | #endif /* WOLFSSL_DTLS13 */ |
8798 | | else { |
8799 | | fragSz = maxFragment - HANDSHAKE_HEADER_SZ; |
8800 | | } |
8801 | | |
8802 | | sendSz += fragSz + HANDSHAKE_HEADER_SZ; |
8803 | | i += HANDSHAKE_HEADER_SZ; |
8804 | | #ifdef WOLFSSL_DTLS13 |
8805 | | if (ssl->options.dtls) { |
8806 | | sendSz += DTLS_HANDSHAKE_EXTRA; |
8807 | | i += DTLS_HANDSHAKE_EXTRA; |
8808 | | } |
8809 | | #endif /* WOLFSSL_DTLS13 */ |
8810 | | } |
8811 | | else { |
8812 | | fragSz = min((word32)length, maxFragment); |
8813 | | sendSz += fragSz; |
8814 | | } |
8815 | | |
8816 | | sendSz += MAX_MSG_EXTRA; |
8817 | | |
8818 | | /* Check buffers are big enough and grow if needed. */ |
8819 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8820 | | return ret; |
8821 | | |
8822 | | /* Get position in output buffer to write new message to. */ |
8823 | | output = GetOutputBuffer(ssl); |
8824 | | |
8825 | | if (ssl->fragOffset == 0) { |
8826 | | AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl); |
8827 | | |
8828 | | /* Request context. */ |
8829 | | output[i++] = certReqCtxLen; |
8830 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8831 | | if (certReqCtxLen > 0) { |
8832 | | XMEMCPY(output + i, certReqCtx, certReqCtxLen); |
8833 | | i += certReqCtxLen; |
8834 | | } |
8835 | | #endif |
8836 | | length -= OPAQUE8_LEN + certReqCtxLen; |
8837 | | fragSz -= OPAQUE8_LEN + certReqCtxLen; |
8838 | | /* Certificate list length. */ |
8839 | | c32to24(listSz, output + i); |
8840 | | i += CERT_HEADER_SZ; |
8841 | | length -= CERT_HEADER_SZ; |
8842 | | fragSz -= CERT_HEADER_SZ; |
8843 | | /* Leaf certificate data length. */ |
8844 | | if (certSz > 0) { |
8845 | | c32to24(certSz, output + i); |
8846 | | i += CERT_HEADER_SZ; |
8847 | | length -= CERT_HEADER_SZ; |
8848 | | fragSz -= CERT_HEADER_SZ; |
8849 | | } |
8850 | | } |
8851 | | else |
8852 | | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
8853 | | |
8854 | | if (extIdx == 0) { |
8855 | | if (certSz > 0 && ssl->fragOffset < certSz + extSz[0]) { |
8856 | | /* Put in the leaf certificate with extensions. */ |
8857 | | word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer, |
8858 | | certSz, extSz[0], ssl->fragOffset, fragSz, |
8859 | | output + i, 0); |
8860 | | i += copySz; |
8861 | | ssl->fragOffset += copySz; |
8862 | | length -= copySz; |
8863 | | fragSz -= copySz; |
8864 | | if (ssl->fragOffset == certSz + extSz[0]) |
8865 | | FreeDer(&ssl->buffers.certExts[0]); |
8866 | | } |
8867 | | } |
8868 | | if (certChainSz > 0 && fragSz > 0) { |
8869 | | /* Put in the CA certificates with extensions. */ |
8870 | | while (fragSz > 0) { |
8871 | | word32 l; |
8872 | | |
8873 | | if (offset == len + OPAQUE16_LEN) { |
8874 | | /* Find next CA certificate to write out. */ |
8875 | | offset = 0; |
8876 | | /* Point to the start of current cert in chain buffer. */ |
8877 | | p = ssl->buffers.certChain->buffer + idx; |
8878 | | len = NextCert(ssl->buffers.certChain->buffer, |
8879 | | ssl->buffers.certChain->length, &idx); |
8880 | | if (len == 0) |
8881 | | break; |
8882 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
8883 | | !defined(NO_WOLFSSL_SERVER) |
8884 | | if (MAX_CERT_EXTENSIONS > extIdx) |
8885 | | extIdx++; |
8886 | | #endif |
8887 | | } |
8888 | | /* Write out certificate and extension. */ |
8889 | | l = AddCertExt(ssl, p, len, extSz[extIdx], offset, fragSz, |
8890 | | output + i, extIdx); |
8891 | | i += l; |
8892 | | ssl->fragOffset += l; |
8893 | | length -= l; |
8894 | | fragSz -= l; |
8895 | | offset += l; |
8896 | | |
8897 | | if (extIdx != 0 && extIdx < MAX_CERT_EXTENSIONS && |
8898 | | ssl->buffers.certExts[extIdx] != NULL && |
8899 | | offset == len + extSz[extIdx]) |
8900 | | FreeDer(&ssl->buffers.certExts[extIdx]); |
8901 | | /* for next chain cert */ |
8902 | | len += extSz[extIdx] - OPAQUE16_LEN; |
8903 | | } |
8904 | | } |
8905 | | |
8906 | | if ((int)i - RECORD_HEADER_SZ < 0) { |
8907 | | WOLFSSL_MSG("Send Cert bad inputSz"); |
8908 | | return BUFFER_E; |
8909 | | } |
8910 | | |
8911 | | #ifdef WOLFSSL_DTLS13 |
8912 | | if (ssl->options.dtls) { |
8913 | | /* DTLS1.3 uses a separate variable and logic for fragments */ |
8914 | | ssl->options.buildingMsg = 0; |
8915 | | ssl->fragOffset = 0; |
8916 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
8917 | | certificate, 1); |
8918 | | } |
8919 | | else |
8920 | | #endif /* WOLFSSL_DTLS13 */ |
8921 | | { |
8922 | | /* This message is always encrypted. */ |
8923 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
8924 | | output + RECORD_HEADER_SZ, (int)(i - RECORD_HEADER_SZ), |
8925 | | handshake, 1, |
8926 | | 0, 0); |
8927 | | if (sendSz < 0) |
8928 | | return sendSz; |
8929 | | |
8930 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8931 | | if (ssl->hsInfoOn) |
8932 | | AddPacketName(ssl, "Certificate"); |
8933 | | if (ssl->toInfoOn) { |
8934 | | ret = AddPacketInfo(ssl, "Certificate", handshake, output, |
8935 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8936 | | if (ret != 0) |
8937 | | return ret; |
8938 | | } |
8939 | | #endif |
8940 | | |
8941 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8942 | | ssl->options.buildingMsg = 0; |
8943 | | if (!ssl->options.groupMessages) |
8944 | | ret = SendBuffered(ssl); |
8945 | | } |
8946 | | } |
8947 | | |
8948 | | if (ret != WC_NO_ERR_TRACE(WANT_WRITE)) { |
8949 | | /* Clean up the fragment offset. */ |
8950 | | ssl->options.buildingMsg = 0; |
8951 | | ssl->fragOffset = 0; |
8952 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
8953 | | ssl->options.serverState = SERVER_CERT_COMPLETE; |
8954 | | } |
8955 | | |
8956 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
8957 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
8958 | | CertReqCtx* ctx = ssl->certReqCtx; |
8959 | | ssl->certReqCtx = ssl->certReqCtx->next; |
8960 | | XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
8961 | | } |
8962 | | #endif |
8963 | | |
8964 | | WOLFSSL_LEAVE("SendTls13Certificate", ret); |
8965 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND); |
8966 | | |
8967 | | return ret; |
8968 | | } |
8969 | | |
8970 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8971 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
8972 | | defined(HAVE_DILITHIUM)) && \ |
8973 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
8974 | | typedef struct Scv13Args { |
8975 | | byte* output; /* not allocated */ |
8976 | | byte* verify; /* not allocated */ |
8977 | | word32 idx; |
8978 | | word32 sigLen; |
8979 | | int sendSz; |
8980 | | word16 length; |
8981 | | |
8982 | | byte sigAlgo; |
8983 | | byte* sigData; |
8984 | | word16 sigDataSz; |
8985 | | #ifndef NO_RSA |
8986 | | byte* toSign; /* not allocated */ |
8987 | | word32 toSignSz; |
8988 | | #endif |
8989 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8990 | | byte altSigAlgo; |
8991 | | word32 altSigLen; /* Only used in the case of both native and alt. */ |
8992 | | byte* altSigData; |
8993 | | word16 altSigDataSz; |
8994 | | #endif |
8995 | | } Scv13Args; |
8996 | | |
8997 | | static void FreeScv13Args(WOLFSSL* ssl, void* pArgs) |
8998 | 462 | { |
8999 | 462 | Scv13Args* args = (Scv13Args*)pArgs; |
9000 | | |
9001 | 462 | (void)ssl; |
9002 | | |
9003 | 462 | if (args && args->sigData) { |
9004 | 451 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9005 | 451 | args->sigData = NULL; |
9006 | 451 | } |
9007 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9008 | | if (args && args->altSigData != NULL) { |
9009 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9010 | | args->altSigData = NULL; |
9011 | | } |
9012 | | #endif |
9013 | 462 | } |
9014 | | |
9015 | | /* handle generation TLS v1.3 certificate_verify (15) */ |
9016 | | /* Send the TLS v1.3 CertificateVerify message. |
9017 | | * A hash of all the message so far is used. |
9018 | | * The signed data is: |
9019 | | * 0x20 * 64 | context string | 0x00 | hash of messages |
9020 | | * This message is always encrypted in TLS v1.3. |
9021 | | * |
9022 | | * ssl The SSL/TLS object. |
9023 | | * returns 0 on success, otherwise failure. |
9024 | | */ |
9025 | | static int SendTls13CertificateVerify(WOLFSSL* ssl) |
9026 | | { |
9027 | | int ret = 0; |
9028 | | #ifndef NO_RSA |
9029 | | /* Use this as a temporary buffer for RSA signature verification. */ |
9030 | | buffer* rsaSigBuf = &ssl->buffers.sig; |
9031 | | #endif |
9032 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9033 | | Scv13Args* args = NULL; |
9034 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
9035 | | #else |
9036 | | Scv13Args args[1]; |
9037 | | #endif |
9038 | | |
9039 | | #ifdef WOLFSSL_DTLS13 |
9040 | | int recordLayerHdrExtra; |
9041 | | #endif /* WOLFSSL_DTLS13 */ |
9042 | | |
9043 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9044 | | WOLFSSL_ENTER("SendTls13CertificateVerify"); |
9045 | | |
9046 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9047 | | wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask); |
9048 | | #endif |
9049 | | |
9050 | | ssl->options.buildingMsg = 1; |
9051 | | |
9052 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
9053 | | ret = tsip_Tls13SendCertVerify(ssl); |
9054 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
9055 | | goto exit_scv; |
9056 | | } |
9057 | | ret = 0; |
9058 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
9059 | | |
9060 | | #ifdef WOLFSSL_DTLS13 |
9061 | | /* can be negative */ |
9062 | | if (ssl->options.dtls) |
9063 | | recordLayerHdrExtra = Dtls13GetRlHeaderLength(ssl, 1) - RECORD_HEADER_SZ; |
9064 | | else |
9065 | | recordLayerHdrExtra = 0; |
9066 | | |
9067 | | #endif /* WOLFSSL_DTLS13 */ |
9068 | | |
9069 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9070 | | if (ssl->async == NULL) { |
9071 | | ssl->async = (struct WOLFSSL_ASYNC*) |
9072 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
9073 | | DYNAMIC_TYPE_ASYNC); |
9074 | | if (ssl->async == NULL) |
9075 | | ERROR_OUT(MEMORY_E, exit_scv); |
9076 | | } |
9077 | | args = (Scv13Args*)ssl->async->args; |
9078 | | |
9079 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
9080 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
9081 | | /* Check for error */ |
9082 | | if (ret < 0) |
9083 | | goto exit_scv; |
9084 | | } |
9085 | | else |
9086 | | #endif |
9087 | | { |
9088 | | /* Reset state */ |
9089 | | ret = 0; |
9090 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
9091 | | XMEMSET(args, 0, sizeof(Scv13Args)); |
9092 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9093 | | ssl->async->freeArgs = FreeScv13Args; |
9094 | | #endif |
9095 | | } |
9096 | | |
9097 | | switch(ssl->options.asyncState) |
9098 | | { |
9099 | | case TLS_ASYNC_BEGIN: |
9100 | | { |
9101 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9102 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9103 | | wolfssl_priv_der_unblind(ssl->buffers.key, |
9104 | | ssl->buffers.keyMask); |
9105 | | #endif |
9106 | | return 0; /* sent blank cert, can't verify */ |
9107 | | } |
9108 | | |
9109 | | args->sendSz = WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA; |
9110 | | /* Always encrypted. */ |
9111 | | args->sendSz += MAX_MSG_EXTRA; |
9112 | | |
9113 | | /* check for available size */ |
9114 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) { |
9115 | | goto exit_scv; |
9116 | | } |
9117 | | |
9118 | | /* get output buffer */ |
9119 | | args->output = GetOutputBuffer(ssl); |
9120 | | |
9121 | | /* Advance state and proceed */ |
9122 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
9123 | | } /* case TLS_ASYNC_BEGIN */ |
9124 | | FALL_THROUGH; |
9125 | | |
9126 | | case TLS_ASYNC_BUILD: |
9127 | | { |
9128 | | int rem = (int)(ssl->buffers.outputBuffer.bufferSize |
9129 | | - ssl->buffers.outputBuffer.length |
9130 | | - RECORD_HEADER_SZ - HANDSHAKE_HEADER_SZ); |
9131 | | |
9132 | | /* idx is used to track verify pointer offset to output */ |
9133 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
9134 | | args->verify = |
9135 | | &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ]; |
9136 | | |
9137 | | #ifdef WOLFSSL_DTLS13 |
9138 | | if (ssl->options.dtls) { |
9139 | | rem -= recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9140 | | args->idx += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9141 | | args->verify += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9142 | | } |
9143 | | #endif /* WOLFSSL_DTLS13 */ |
9144 | | |
9145 | | if (ssl->buffers.key == NULL) { |
9146 | | #ifdef HAVE_PK_CALLBACKS |
9147 | | if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) |
9148 | | args->sigLen = (word16)GetPrivateKeySigSize(ssl); |
9149 | | else |
9150 | | #endif |
9151 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9152 | | } |
9153 | | else { |
9154 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9155 | | if (ssl->sigSpec != NULL && |
9156 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
9157 | | /* In the case of alternative, we swap in the alt. */ |
9158 | | if (ssl->buffers.altKey == NULL) { |
9159 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9160 | | } |
9161 | | ssl->buffers.keyType = ssl->buffers.altKeyType; |
9162 | | ssl->buffers.keySz = ssl->buffers.altKeySz; |
9163 | | /* If we own it, free key before overriding it. */ |
9164 | | if (ssl->buffers.weOwnKey) { |
9165 | | FreeDer(&ssl->buffers.key); |
9166 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9167 | | FreeDer(&ssl->buffers.keyMask); |
9168 | | #endif |
9169 | | } |
9170 | | |
9171 | | /* Swap keys */ |
9172 | | ssl->buffers.key = ssl->buffers.altKey; |
9173 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9174 | | ssl->buffers.keyMask = ssl->buffers.altKeyMask; |
9175 | | #endif |
9176 | | ssl->buffers.weOwnKey = ssl->buffers.weOwnAltKey; |
9177 | | } |
9178 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9179 | | ret = DecodePrivateKey(ssl, &args->sigLen); |
9180 | | if (ret != 0) |
9181 | | goto exit_scv; |
9182 | | } |
9183 | | |
9184 | | if (rem < 0 || (int)args->sigLen > rem) { |
9185 | | ERROR_OUT(BUFFER_E, exit_scv); |
9186 | | } |
9187 | | |
9188 | | if (args->sigLen == 0) { |
9189 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9190 | | } |
9191 | | |
9192 | | /* Add signature algorithm. */ |
9193 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) |
9194 | | args->sigAlgo = rsa_pss_sa_algo; |
9195 | | #ifdef HAVE_ECC |
9196 | | else if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9197 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9198 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9199 | | args->sigAlgo = sm2_sa_algo; |
9200 | | } |
9201 | | else |
9202 | | #endif |
9203 | | { |
9204 | | args->sigAlgo = ecc_dsa_sa_algo; |
9205 | | } |
9206 | | } |
9207 | | #endif |
9208 | | #ifdef HAVE_ED25519 |
9209 | | else if (ssl->hsType == DYNAMIC_TYPE_ED25519) |
9210 | | args->sigAlgo = ed25519_sa_algo; |
9211 | | #endif |
9212 | | #ifdef HAVE_ED448 |
9213 | | else if (ssl->hsType == DYNAMIC_TYPE_ED448) |
9214 | | args->sigAlgo = ed448_sa_algo; |
9215 | | #endif |
9216 | | #if defined(HAVE_FALCON) |
9217 | | else if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9218 | | args->sigAlgo = ssl->buffers.keyType; |
9219 | | } |
9220 | | #endif /* HAVE_FALCON */ |
9221 | | #if defined(HAVE_DILITHIUM) |
9222 | | else if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9223 | | args->sigAlgo = ssl->buffers.keyType; |
9224 | | } |
9225 | | #endif /* HAVE_DILITHIUM */ |
9226 | | else { |
9227 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9228 | | } |
9229 | | |
9230 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9231 | | if (ssl->peerSigSpec == NULL) { |
9232 | | /* The peer did not respond. We didn't send CKS or they don't |
9233 | | * support it. Either way, we do not need to handle dual |
9234 | | * key/sig case. */ |
9235 | | ssl->sigSpec = NULL; |
9236 | | ssl->sigSpecSz = 0; |
9237 | | } |
9238 | | |
9239 | | if (ssl->sigSpec != NULL && |
9240 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9241 | | /* The native was already decoded. Now we need to do the |
9242 | | * alternative. Note that no swap was done because this case is |
9243 | | * both native and alternative, not just alternative. */ |
9244 | | if (ssl->ctx->altPrivateKey == NULL) { |
9245 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9246 | | } |
9247 | | |
9248 | | /* After this call, args->altSigLen has the length we need for |
9249 | | * the alternative signature. */ |
9250 | | ret = DecodeAltPrivateKey(ssl, &args->altSigLen); |
9251 | | if (ret != 0) |
9252 | | goto exit_scv; |
9253 | | |
9254 | | if (ssl->buffers.altKeyType == ecc_dsa_sa_algo || |
9255 | | ssl->buffers.altKeyType == falcon_level1_sa_algo || |
9256 | | ssl->buffers.altKeyType == falcon_level5_sa_algo || |
9257 | | ssl->buffers.altKeyType == dilithium_level2_sa_algo || |
9258 | | ssl->buffers.altKeyType == dilithium_level3_sa_algo || |
9259 | | ssl->buffers.altKeyType == dilithium_level5_sa_algo) { |
9260 | | args->altSigAlgo = ssl->buffers.altKeyType; |
9261 | | } |
9262 | | else if (ssl->buffers.altKeyType == rsa_sa_algo && |
9263 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9264 | | args->altSigAlgo = rsa_pss_sa_algo; |
9265 | | } |
9266 | | else { |
9267 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9268 | | } |
9269 | | |
9270 | | EncodeDualSigAlg(args->sigAlgo, args->altSigAlgo, args->verify); |
9271 | | if (args->verify[0] == 0) { |
9272 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9273 | | } |
9274 | | } |
9275 | | else |
9276 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9277 | | EncodeSigAlg(ssl->options.hashAlgo, args->sigAlgo, |
9278 | | args->verify); |
9279 | | |
9280 | | if (args->sigData == NULL) { |
9281 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9282 | | if ((ssl->hsType == DYNAMIC_TYPE_RSA) && |
9283 | | (args->sigLen > MAX_SIG_DATA_SZ)) { |
9284 | | /* We store the RSA signature in the sigData buffer |
9285 | | * temporarily, hence its size must be fitting. */ |
9286 | | sigLen = args->sigLen; |
9287 | | } |
9288 | | args->sigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9289 | | DYNAMIC_TYPE_SIGNATURE); |
9290 | | if (args->sigData == NULL) { |
9291 | | ERROR_OUT(MEMORY_E, exit_scv); |
9292 | | } |
9293 | | } |
9294 | | |
9295 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9296 | | if ((ssl->sigSpec != NULL) && |
9297 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
9298 | | (args->altSigData == NULL)) { |
9299 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9300 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA && |
9301 | | args->altSigLen > MAX_SIG_DATA_SZ) { |
9302 | | /* We store the RSA signature in the sigData buffer |
9303 | | * temporarily, hence its size must be fitting. */ |
9304 | | sigLen = args->altSigLen; |
9305 | | } |
9306 | | args->altSigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9307 | | DYNAMIC_TYPE_SIGNATURE); |
9308 | | if (args->altSigData == NULL) { |
9309 | | ERROR_OUT(MEMORY_E, exit_scv); |
9310 | | } |
9311 | | } |
9312 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9313 | | |
9314 | | /* Create the data to be signed. */ |
9315 | | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0); |
9316 | | if (ret != 0) |
9317 | | goto exit_scv; |
9318 | | |
9319 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9320 | | if ((ssl->sigSpec != NULL) && |
9321 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
9322 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
9323 | | args->altSigDataSz = args->sigDataSz; |
9324 | | } |
9325 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9326 | | |
9327 | | #ifndef NO_RSA |
9328 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9329 | | /* build encoded signature buffer */ |
9330 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9331 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, ssl->heap, |
9332 | | DYNAMIC_TYPE_SIGNATURE); |
9333 | | if (rsaSigBuf->buffer == NULL) { |
9334 | | ERROR_OUT(MEMORY_E, exit_scv); |
9335 | | } |
9336 | | |
9337 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, args->sigData, |
9338 | | args->sigDataSz, args->sigAlgo, ssl->options.hashAlgo); |
9339 | | if (ret < 0) |
9340 | | goto exit_scv; |
9341 | | rsaSigBuf->length = (unsigned int)ret; |
9342 | | ret = 0; |
9343 | | } |
9344 | | #endif /* !NO_RSA */ |
9345 | | #ifdef HAVE_ECC |
9346 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9347 | | args->sigLen = (word32)args->sendSz - args->idx - |
9348 | | HASH_SIG_SIZE - |
9349 | | VERIFY_HEADER; |
9350 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9351 | | if (ssl->buffers.keyType != sm2_sa_algo) |
9352 | | #endif |
9353 | | { |
9354 | | ret = CreateECCEncodedSig(args->sigData, |
9355 | | args->sigDataSz, ssl->options.hashAlgo); |
9356 | | if (ret < 0) |
9357 | | goto exit_scv; |
9358 | | args->sigDataSz = (word16)ret; |
9359 | | ret = 0; |
9360 | | } |
9361 | | } |
9362 | | #endif /* HAVE_ECC */ |
9363 | | #ifdef HAVE_ED25519 |
9364 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9365 | | ret = Ed25519CheckPubKey(ssl); |
9366 | | if (ret < 0) { |
9367 | | ERROR_OUT(ret, exit_scv); |
9368 | | } |
9369 | | args->sigLen = ED25519_SIG_SIZE; |
9370 | | } |
9371 | | #endif /* HAVE_ED25519 */ |
9372 | | #ifdef HAVE_ED448 |
9373 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9374 | | ret = Ed448CheckPubKey(ssl); |
9375 | | if (ret < 0) { |
9376 | | ERROR_OUT(ret, exit_scv); |
9377 | | } |
9378 | | args->sigLen = ED448_SIG_SIZE; |
9379 | | } |
9380 | | |
9381 | | #endif /* HAVE_ED448 */ |
9382 | | #if defined(HAVE_FALCON) |
9383 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9384 | | args->sigLen = FALCON_MAX_SIG_SIZE; |
9385 | | } |
9386 | | #endif /* HAVE_FALCON */ |
9387 | | #if defined(HAVE_DILITHIUM) |
9388 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9389 | | args->sigLen = DILITHIUM_MAX_SIG_SIZE; |
9390 | | } |
9391 | | #endif /* HAVE_DILITHIUM */ |
9392 | | |
9393 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9394 | | if (ssl->sigSpec != NULL && |
9395 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9396 | | |
9397 | | #ifndef NO_RSA |
9398 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9399 | | /* build encoded signature buffer */ |
9400 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9401 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, |
9402 | | ssl->heap, |
9403 | | DYNAMIC_TYPE_SIGNATURE); |
9404 | | if (rsaSigBuf->buffer == NULL) { |
9405 | | ERROR_OUT(MEMORY_E, exit_scv); |
9406 | | } |
9407 | | |
9408 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, |
9409 | | args->altSigData, args->altSigDataSz, |
9410 | | args->altSigAlgo, ssl->options.hashAlgo); |
9411 | | if (ret < 0) |
9412 | | goto exit_scv; |
9413 | | rsaSigBuf->length = ret; |
9414 | | ret = 0; |
9415 | | } |
9416 | | #endif /* !NO_RSA */ |
9417 | | #ifdef HAVE_ECC |
9418 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9419 | | ret = CreateECCEncodedSig(args->altSigData, |
9420 | | args->altSigDataSz, ssl->options.hashAlgo); |
9421 | | if (ret < 0) |
9422 | | goto exit_scv; |
9423 | | args->altSigDataSz = (word16)ret; |
9424 | | ret = 0; |
9425 | | } |
9426 | | #endif /* HAVE_ECC */ |
9427 | | } |
9428 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9429 | | |
9430 | | /* Advance state and proceed */ |
9431 | | ssl->options.asyncState = TLS_ASYNC_DO; |
9432 | | } /* case TLS_ASYNC_BUILD */ |
9433 | | FALL_THROUGH; |
9434 | | |
9435 | | case TLS_ASYNC_DO: |
9436 | | { |
9437 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
9438 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9439 | | if (ssl->sigSpec != NULL && |
9440 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9441 | | /* As we have two signatures in the message, we store |
9442 | | * the length of each before the actual signature. This |
9443 | | * is necessary, as we could have two algorithms with |
9444 | | * variable length signatures. */ |
9445 | | sigOut += OPAQUE16_LEN; |
9446 | | } |
9447 | | #endif |
9448 | | #ifdef HAVE_ECC |
9449 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9450 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9451 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9452 | | ret = Sm2wSm3Sign(ssl, TLS13_SM2_SIG_ID, |
9453 | | TLS13_SM2_SIG_ID_SZ, args->sigData, args->sigDataSz, |
9454 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, NULL); |
9455 | | } |
9456 | | else |
9457 | | #endif |
9458 | | { |
9459 | | ret = EccSign(ssl, args->sigData, args->sigDataSz, |
9460 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, |
9461 | | #ifdef HAVE_PK_CALLBACKS |
9462 | | ssl->buffers.key |
9463 | | #else |
9464 | | NULL |
9465 | | #endif |
9466 | | ); |
9467 | | } |
9468 | | args->length = (word16)args->sigLen; |
9469 | | } |
9470 | | #endif /* HAVE_ECC */ |
9471 | | #ifdef HAVE_ED25519 |
9472 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9473 | | ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz, |
9474 | | sigOut, &args->sigLen, (ed25519_key*)ssl->hsKey, |
9475 | | #ifdef HAVE_PK_CALLBACKS |
9476 | | ssl->buffers.key |
9477 | | #else |
9478 | | NULL |
9479 | | #endif |
9480 | | ); |
9481 | | args->length = (word16)args->sigLen; |
9482 | | } |
9483 | | #endif |
9484 | | #ifdef HAVE_ED448 |
9485 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9486 | | ret = Ed448Sign(ssl, args->sigData, args->sigDataSz, |
9487 | | sigOut, &args->sigLen, (ed448_key*)ssl->hsKey, |
9488 | | #ifdef HAVE_PK_CALLBACKS |
9489 | | ssl->buffers.key |
9490 | | #else |
9491 | | NULL |
9492 | | #endif |
9493 | | ); |
9494 | | args->length = (word16)args->sigLen; |
9495 | | } |
9496 | | #endif |
9497 | | #if defined(HAVE_FALCON) |
9498 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9499 | | ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz, |
9500 | | sigOut, &args->sigLen, |
9501 | | (falcon_key*)ssl->hsKey, ssl->rng); |
9502 | | args->length = (word16)args->sigLen; |
9503 | | } |
9504 | | #endif /* HAVE_FALCON */ |
9505 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9506 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9507 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->sigData, |
9508 | | args->sigDataSz, sigOut, |
9509 | | &args->sigLen, |
9510 | | (dilithium_key*)ssl->hsKey, |
9511 | | ssl->rng); |
9512 | | args->length = (word16)args->sigLen; |
9513 | | } |
9514 | | #endif /* HAVE_DILITHIUM */ |
9515 | | #ifndef NO_RSA |
9516 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9517 | | args->toSign = rsaSigBuf->buffer; |
9518 | | args->toSignSz = (word32)rsaSigBuf->length; |
9519 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9520 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9521 | | /* Pass full data to sign (args->sigData), not hash of */ |
9522 | | if (ssl->ctx->RsaPssSignCb) { |
9523 | | args->toSign = args->sigData; |
9524 | | args->toSignSz = args->sigDataSz; |
9525 | | } |
9526 | | #endif |
9527 | | ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz, |
9528 | | sigOut, &args->sigLen, args->sigAlgo, |
9529 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9530 | | ssl->buffers.key); |
9531 | | if (ret == 0) { |
9532 | | args->length = (word16)args->sigLen; |
9533 | | XMEMCPY(args->sigData, sigOut, args->sigLen); |
9534 | | } |
9535 | | } |
9536 | | #endif /* !NO_RSA */ |
9537 | | |
9538 | | /* Check for error */ |
9539 | | if (ret != 0) { |
9540 | | goto exit_scv; |
9541 | | } |
9542 | | |
9543 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9544 | | if (ssl->sigSpec != NULL && |
9545 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9546 | | /* Add signature length for the first signature. */ |
9547 | | c16toa((word16)args->sigLen, sigOut - OPAQUE16_LEN); |
9548 | | args->length += OPAQUE16_LEN; |
9549 | | |
9550 | | /* Advance our pointer to where we store the alt signature. |
9551 | | * We also add additional space for the length field of the |
9552 | | * second signature. */ |
9553 | | sigOut += args->sigLen + OPAQUE16_LEN; |
9554 | | |
9555 | | /* Generate the alternative signature */ |
9556 | | #ifdef HAVE_ECC |
9557 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9558 | | ret = EccSign(ssl, args->altSigData, args->altSigDataSz, |
9559 | | sigOut, &args->altSigLen, |
9560 | | (ecc_key*)ssl->hsAltKey, |
9561 | | #ifdef HAVE_PK_CALLBACKS |
9562 | | ssl->buffers.altKey |
9563 | | #else |
9564 | | NULL |
9565 | | #endif |
9566 | | ); |
9567 | | } |
9568 | | #endif /* HAVE_ECC */ |
9569 | | #ifndef NO_RSA |
9570 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9571 | | args->toSign = rsaSigBuf->buffer; |
9572 | | args->toSignSz = (word32)rsaSigBuf->length; |
9573 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9574 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9575 | | /* Pass full data to sign (args->altSigData), not hash of */ |
9576 | | if (ssl->ctx->RsaPssSignCb) { |
9577 | | args->toSign = args->altSigData; |
9578 | | args->toSignSz = (word32)args->altSigDataSz; |
9579 | | } |
9580 | | #endif |
9581 | | ret = RsaSign(ssl, (const byte*)args->toSign, |
9582 | | args->toSignSz, sigOut, &args->altSigLen, |
9583 | | args->altSigAlgo, ssl->options.hashAlgo, |
9584 | | (RsaKey*)ssl->hsAltKey, |
9585 | | ssl->buffers.altKey); |
9586 | | |
9587 | | if (ret == 0) { |
9588 | | XMEMCPY(args->altSigData, sigOut, args->altSigLen); |
9589 | | } |
9590 | | } |
9591 | | #endif /* !NO_RSA */ |
9592 | | #if defined(HAVE_FALCON) |
9593 | | if (ssl->hsAltType == DYNAMIC_TYPE_FALCON) { |
9594 | | ret = wc_falcon_sign_msg(args->altSigData, |
9595 | | args->altSigDataSz, sigOut, |
9596 | | &args->altSigLen, |
9597 | | (falcon_key*)ssl->hsAltKey, |
9598 | | ssl->rng); |
9599 | | } |
9600 | | #endif /* HAVE_FALCON */ |
9601 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9602 | | if (ssl->hsAltType == DYNAMIC_TYPE_DILITHIUM) { |
9603 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->altSigData, |
9604 | | args->altSigDataSz, sigOut, &args->altSigLen, |
9605 | | (dilithium_key*)ssl->hsAltKey, ssl->rng); |
9606 | | } |
9607 | | #endif /* HAVE_DILITHIUM */ |
9608 | | |
9609 | | /* Check for error */ |
9610 | | if (ret != 0) { |
9611 | | goto exit_scv; |
9612 | | } |
9613 | | |
9614 | | /* Add signature length for the alternative signature. */ |
9615 | | c16toa((word16)args->altSigLen, sigOut - OPAQUE16_LEN); |
9616 | | |
9617 | | /* Add length of the alt sig to the total length */ |
9618 | | args->length += args->altSigLen + OPAQUE16_LEN; |
9619 | | } |
9620 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9621 | | |
9622 | | /* Add signature length. */ |
9623 | | c16toa(args->length, args->verify + HASH_SIG_SIZE); |
9624 | | |
9625 | | /* Advance state and proceed */ |
9626 | | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
9627 | | } /* case TLS_ASYNC_DO */ |
9628 | | FALL_THROUGH; |
9629 | | |
9630 | | case TLS_ASYNC_VERIFY: |
9631 | | { |
9632 | | #ifndef NO_RSA |
9633 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9634 | | /* check for signature faults */ |
9635 | | ret = VerifyRsaSign(ssl, args->sigData, args->sigLen, |
9636 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, args->sigAlgo, |
9637 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9638 | | ssl->buffers.key); |
9639 | | } |
9640 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9641 | | if (ssl->sigSpec != NULL && |
9642 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
9643 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9644 | | /* check for signature faults */ |
9645 | | ret = VerifyRsaSign(ssl, args->altSigData, args->altSigLen, |
9646 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, |
9647 | | args->altSigAlgo, ssl->options.hashAlgo, |
9648 | | (RsaKey*)ssl->hsAltKey, ssl->buffers.altKey); |
9649 | | } |
9650 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9651 | | #endif /* !NO_RSA */ |
9652 | | #if defined(HAVE_ECC) && defined(WOLFSSL_CHECK_SIG_FAULTS) |
9653 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9654 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
9655 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9656 | | if (ssl->sigSpec != NULL && |
9657 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9658 | | /* Add our length offset. */ |
9659 | | sigOut += OPAQUE16_LEN; |
9660 | | } |
9661 | | #endif |
9662 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9663 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9664 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
9665 | | TLS13_SM2_SIG_ID_SZ, |
9666 | | sigOut, args->sigLen, args->sigData, args->sigDataSz, |
9667 | | (ecc_key*)ssl->hsKey, NULL); |
9668 | | } |
9669 | | else |
9670 | | #endif |
9671 | | { |
9672 | | #ifdef HAVE_PK_CALLBACKS |
9673 | | buffer tmp; |
9674 | | |
9675 | | tmp.length = ssl->buffers.key->length; |
9676 | | tmp.buffer = ssl->buffers.key->buffer; |
9677 | | #endif |
9678 | | ret = EccVerify(ssl, sigOut, args->sigLen, |
9679 | | args->sigData, args->sigDataSz, |
9680 | | (ecc_key*)ssl->hsKey, |
9681 | | #ifdef HAVE_PK_CALLBACKS |
9682 | | &tmp |
9683 | | #else |
9684 | | NULL |
9685 | | #endif |
9686 | | ); |
9687 | | } |
9688 | | } |
9689 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9690 | | if (ssl->sigSpec != NULL && |
9691 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
9692 | | ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9693 | | /* check for signature faults */ |
9694 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER + |
9695 | | args->sigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
9696 | | ret = EccVerify(ssl, sigOut, args->altSigLen, |
9697 | | args->altSigData, args->altSigDataSz, |
9698 | | (ecc_key*)ssl->hsAltKey, |
9699 | | #ifdef HAVE_PK_CALLBACKS |
9700 | | ssl->buffers.altKey |
9701 | | #else |
9702 | | NULL |
9703 | | #endif |
9704 | | ); |
9705 | | } |
9706 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9707 | | #endif /* HAVE_ECC && WOLFSSL_CHECK_SIG_FAULTS */ |
9708 | | |
9709 | | /* Check for error */ |
9710 | | if (ret != 0) { |
9711 | | goto exit_scv; |
9712 | | } |
9713 | | |
9714 | | /* Advance state and proceed */ |
9715 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
9716 | | } /* case TLS_ASYNC_VERIFY */ |
9717 | | FALL_THROUGH; |
9718 | | |
9719 | | case TLS_ASYNC_FINALIZE: |
9720 | | { |
9721 | | /* Put the record and handshake headers on. */ |
9722 | | AddTls13Headers(args->output, args->length + HASH_SIG_SIZE + |
9723 | | VERIFY_HEADER, certificate_verify, ssl); |
9724 | | |
9725 | | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
9726 | | args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
9727 | | #ifdef WOLFSSL_DTLS13 |
9728 | | if (ssl->options.dtls) |
9729 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9730 | | |
9731 | | #endif /* WOLFSSL_DTLS13 */ |
9732 | | /* Advance state and proceed */ |
9733 | | ssl->options.asyncState = TLS_ASYNC_END; |
9734 | | } /* case TLS_ASYNC_FINALIZE */ |
9735 | | FALL_THROUGH; |
9736 | | |
9737 | | case TLS_ASYNC_END: |
9738 | | { |
9739 | | #ifdef WOLFSSL_DTLS13 |
9740 | | if (ssl->options.dtls) { |
9741 | | ssl->options.buildingMsg = 0; |
9742 | | ret = Dtls13HandshakeSend(ssl, args->output, |
9743 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA + MAX_MSG_EXTRA, |
9744 | | (word16)args->sendSz, certificate_verify, 1); |
9745 | | if (ret != 0) |
9746 | | goto exit_scv; |
9747 | | |
9748 | | break; |
9749 | | } |
9750 | | #endif /* WOLFSSL_DTLS13 */ |
9751 | | |
9752 | | /* This message is always encrypted. */ |
9753 | | ret = BuildTls13Message(ssl, args->output, |
9754 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA, |
9755 | | args->output + RECORD_HEADER_SZ, |
9756 | | args->sendSz - RECORD_HEADER_SZ, handshake, |
9757 | | 1, 0, 0); |
9758 | | |
9759 | | if (ret < 0) { |
9760 | | goto exit_scv; |
9761 | | } |
9762 | | else { |
9763 | | args->sendSz = ret; |
9764 | | ret = 0; |
9765 | | } |
9766 | | |
9767 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
9768 | | if (ssl->hsInfoOn) |
9769 | | AddPacketName(ssl, "CertificateVerify"); |
9770 | | if (ssl->toInfoOn) { |
9771 | | ret = AddPacketInfo(ssl, "CertificateVerify", handshake, |
9772 | | args->output, args->sendSz, WRITE_PROTO, 0, |
9773 | | ssl->heap); |
9774 | | if (ret != 0) |
9775 | | goto exit_scv; |
9776 | | } |
9777 | | #endif |
9778 | | |
9779 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
9780 | | ssl->options.buildingMsg = 0; |
9781 | | if (!ssl->options.groupMessages) |
9782 | | ret = SendBuffered(ssl); |
9783 | | break; |
9784 | | } |
9785 | | default: |
9786 | | ret = INPUT_CASE_ERROR; |
9787 | | } /* switch(ssl->options.asyncState) */ |
9788 | | |
9789 | | exit_scv: |
9790 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9791 | | if (ret == 0) { |
9792 | | ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
9793 | | &ssl->buffers.keyMask); |
9794 | | } |
9795 | | else { |
9796 | | wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask); |
9797 | | } |
9798 | | #endif |
9799 | | |
9800 | | WOLFSSL_LEAVE("SendTls13CertificateVerify", ret); |
9801 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9802 | | |
9803 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9804 | | /* Handle async operation */ |
9805 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
9806 | | return ret; |
9807 | | } |
9808 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
9809 | | |
9810 | | /* Final cleanup */ |
9811 | | FreeScv13Args(ssl, args); |
9812 | | FreeKeyExchange(ssl); |
9813 | | #ifdef WOLFSSL_ASYNC_IO |
9814 | | /* Cleanup async */ |
9815 | | FreeAsyncCtx(ssl, 0); |
9816 | | #endif |
9817 | | |
9818 | | if (ret != 0) { |
9819 | | WOLFSSL_ERROR_VERBOSE(ret); |
9820 | | } |
9821 | | |
9822 | | return ret; |
9823 | | } |
9824 | | #endif |
9825 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
9826 | | |
9827 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) |
9828 | | /* handle processing TLS v1.3 certificate (11) */ |
9829 | | /* Parse and handle a TLS v1.3 Certificate message. |
9830 | | * |
9831 | | * ssl The SSL/TLS object. |
9832 | | * input The message buffer. |
9833 | | * inOutIdx On entry, the index into the message buffer of Certificate. |
9834 | | * On exit, the index of byte after the Certificate message. |
9835 | | * totalSz The length of the current handshake message. |
9836 | | * returns 0 on success and otherwise failure. |
9837 | | */ |
9838 | | static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
9839 | | word32 totalSz) |
9840 | 0 | { |
9841 | 0 | int ret = 0; |
9842 | |
|
9843 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_DO); |
9844 | 0 | WOLFSSL_ENTER("DoTls13Certificate"); |
9845 | |
|
9846 | | #ifdef WOLFSSL_DTLS13 |
9847 | | if (ssl->options.dtls && ssl->options.handShakeDone) { |
9848 | | /* certificate needs some special care after the handshake */ |
9849 | | ret = Dtls13RtxProcessingCertificate( |
9850 | | ssl, input + *inOutIdx, totalSz); |
9851 | | } |
9852 | | #endif /* WOLFSSL_DTLS13 */ |
9853 | |
|
9854 | 0 | if (ret == 0) |
9855 | 0 | ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz); |
9856 | 0 | if (ret == 0) { |
9857 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
9858 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
9859 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
9860 | 0 | #endif |
9861 | | #if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
9862 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
9863 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
9864 | | /* reset handshake states */ |
9865 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
9866 | | ssl->options.acceptState = TICKET_SENT; |
9867 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
9868 | | } |
9869 | | #endif |
9870 | 0 | } |
9871 | |
|
9872 | 0 | WOLFSSL_LEAVE("DoTls13Certificate", ret); |
9873 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_DO); |
9874 | |
|
9875 | 0 | return ret; |
9876 | 0 | } |
9877 | | #endif |
9878 | | |
9879 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
9880 | | defined(HAVE_ED448) |
9881 | | |
9882 | | typedef struct Dcv13Args { |
9883 | | byte* output; /* not allocated */ |
9884 | | word32 sendSz; |
9885 | | word16 sz; |
9886 | | word32 sigSz; |
9887 | | word32 idx; |
9888 | | word32 begin; |
9889 | | |
9890 | | byte* sigData; |
9891 | | word16 sigDataSz; |
9892 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9893 | | byte altSigAlgo; |
9894 | | byte* altSigData; |
9895 | | word32 altSigDataSz; |
9896 | | word32 altSignatureSz; |
9897 | | byte altPeerAuthGood; |
9898 | | #endif |
9899 | | } Dcv13Args; |
9900 | | |
9901 | | static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs) |
9902 | 0 | { |
9903 | 0 | Dcv13Args* args = (Dcv13Args*)pArgs; |
9904 | |
|
9905 | 0 | if (args && args->sigData != NULL) { |
9906 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9907 | 0 | args->sigData = NULL; |
9908 | 0 | } |
9909 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9910 | | if (args && args->altSigData != NULL) { |
9911 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9912 | | args->altSigData = NULL; |
9913 | | } |
9914 | | #endif |
9915 | 0 | (void)ssl; |
9916 | 0 | } |
9917 | | |
9918 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9919 | | #ifndef NO_RSA |
9920 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
9921 | | * RSA public key. Convert it into a usable public key. */ |
9922 | | static int decodeRsaKey(WOLFSSL* ssl) |
9923 | | { |
9924 | | int keyRet; |
9925 | | word32 tmpIdx = 0; |
9926 | | |
9927 | | if (ssl->peerRsaKeyPresent) |
9928 | | return INVALID_PARAMETER; |
9929 | | |
9930 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
9931 | | if (keyRet != 0) |
9932 | | return PEER_KEY_ERROR; |
9933 | | |
9934 | | ssl->peerRsaKeyPresent = 1; |
9935 | | keyRet = wc_RsaPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
9936 | | ssl->peerRsaKey, |
9937 | | ssl->peerCert.sapkiLen); |
9938 | | if (keyRet != 0) |
9939 | | return PEER_KEY_ERROR; |
9940 | | |
9941 | | return 0; |
9942 | | } |
9943 | | #endif /* !NO_RSA */ |
9944 | | |
9945 | | #ifdef HAVE_ECC |
9946 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
9947 | | * ECC public key. Convert it into a usable public key. */ |
9948 | | static int decodeEccKey(WOLFSSL* ssl) |
9949 | | { |
9950 | | int keyRet; |
9951 | | word32 tmpIdx = 0; |
9952 | | |
9953 | | if (ssl->peerEccDsaKeyPresent) |
9954 | | return INVALID_PARAMETER; |
9955 | | |
9956 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
9957 | | if (keyRet != 0) |
9958 | | return PEER_KEY_ERROR; |
9959 | | |
9960 | | ssl->peerEccDsaKeyPresent = 1; |
9961 | | keyRet = wc_EccPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
9962 | | ssl->peerEccDsaKey, |
9963 | | ssl->peerCert.sapkiLen); |
9964 | | if (keyRet != 0) |
9965 | | return PEER_KEY_ERROR; |
9966 | | |
9967 | | return 0; |
9968 | | } |
9969 | | #endif /* HAVE_ECC */ |
9970 | | |
9971 | | #ifdef HAVE_DILITHIUM |
9972 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
9973 | | * dilithium public key. Convert it into a usable public key. */ |
9974 | | static int decodeDilithiumKey(WOLFSSL* ssl, int level) |
9975 | | { |
9976 | | int keyRet; |
9977 | | word32 tmpIdx = 0; |
9978 | | |
9979 | | if (ssl->peerDilithiumKeyPresent) |
9980 | | return INVALID_PARAMETER; |
9981 | | |
9982 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
9983 | | (void**)&ssl->peerDilithiumKey); |
9984 | | if (keyRet != 0) |
9985 | | return PEER_KEY_ERROR; |
9986 | | |
9987 | | ssl->peerDilithiumKeyPresent = 1; |
9988 | | keyRet = wc_dilithium_set_level(ssl->peerDilithiumKey, level); |
9989 | | if (keyRet != 0) |
9990 | | return PEER_KEY_ERROR; |
9991 | | |
9992 | | keyRet = wc_Dilithium_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
9993 | | ssl->peerDilithiumKey, |
9994 | | ssl->peerCert.sapkiLen); |
9995 | | if (keyRet != 0) |
9996 | | return PEER_KEY_ERROR; |
9997 | | |
9998 | | return 0; |
9999 | | } |
10000 | | #endif /* HAVE_DILITHIUM */ |
10001 | | |
10002 | | #ifdef HAVE_FALCON |
10003 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10004 | | * falcon public key. Convert it into a usable public key. */ |
10005 | | static int decodeFalconKey(WOLFSSL* ssl, int level) |
10006 | | { |
10007 | | int keyRet; |
10008 | | word32 tmpIdx = 0; |
10009 | | |
10010 | | if (ssl->peerFalconKeyPresent) |
10011 | | return INVALID_PARAMETER; |
10012 | | |
10013 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey); |
10014 | | if (keyRet != 0) |
10015 | | return PEER_KEY_ERROR; |
10016 | | |
10017 | | ssl->peerFalconKeyPresent = 1; |
10018 | | keyRet = wc_falcon_set_level(ssl->peerFalconKey, level); |
10019 | | if (keyRet != 0) |
10020 | | return PEER_KEY_ERROR; |
10021 | | |
10022 | | keyRet = wc_Falcon_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10023 | | ssl->peerFalconKey, |
10024 | | ssl->peerCert.sapkiLen); |
10025 | | if (keyRet != 0) |
10026 | | return PEER_KEY_ERROR; |
10027 | | |
10028 | | return 0; |
10029 | | } |
10030 | | #endif /* HAVE_FALCON */ |
10031 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10032 | | |
10033 | | /* handle processing TLS v1.3 certificate_verify (15) */ |
10034 | | /* Parse and handle a TLS v1.3 CertificateVerify message. |
10035 | | * |
10036 | | * ssl The SSL/TLS object. |
10037 | | * input The message buffer. |
10038 | | * inOutIdx On entry, the index into the message buffer of |
10039 | | * CertificateVerify. |
10040 | | * On exit, the index of byte after the CertificateVerify message. |
10041 | | * totalSz The length of the current handshake message. |
10042 | | * returns 0 on success and otherwise failure. |
10043 | | */ |
10044 | | static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input, |
10045 | | word32* inOutIdx, word32 totalSz) |
10046 | 0 | { |
10047 | 0 | int ret = 0; |
10048 | 0 | byte* sig = NULL; |
10049 | 0 | #ifndef NO_RSA |
10050 | | /* Use this as a temporary buffer for RSA signature verification. */ |
10051 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
10052 | 0 | #endif |
10053 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10054 | | Dcv13Args* args = NULL; |
10055 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
10056 | | #else |
10057 | 0 | Dcv13Args args[1]; |
10058 | 0 | #endif |
10059 | |
|
10060 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10061 | 0 | WOLFSSL_ENTER("DoTls13CertificateVerify"); |
10062 | |
|
10063 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10064 | | ret = tsip_Tls13CertificateVerify(ssl, input, inOutIdx, totalSz); |
10065 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10066 | | goto exit_dcv; |
10067 | | } |
10068 | | ret = 0; |
10069 | | #endif |
10070 | |
|
10071 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10072 | | if (ssl->async == NULL) { |
10073 | | ssl->async = (struct WOLFSSL_ASYNC*) |
10074 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
10075 | | DYNAMIC_TYPE_ASYNC); |
10076 | | if (ssl->async == NULL) |
10077 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10078 | | } |
10079 | | args = (Dcv13Args*)ssl->async->args; |
10080 | | |
10081 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
10082 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
10083 | | /* Check for error */ |
10084 | | if (ret < 0) |
10085 | | goto exit_dcv; |
10086 | | } |
10087 | | else |
10088 | | #endif |
10089 | 0 | { |
10090 | | /* Reset state */ |
10091 | 0 | ret = 0; |
10092 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
10093 | 0 | XMEMSET(args, 0, sizeof(Dcv13Args)); |
10094 | 0 | ssl->options.peerHashAlgo = sha_mac; |
10095 | 0 | ssl->options.peerSigAlgo = anonymous_sa_algo; |
10096 | 0 | args->idx = *inOutIdx; |
10097 | 0 | args->begin = *inOutIdx; |
10098 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10099 | | ssl->async->freeArgs = FreeDcv13Args; |
10100 | | #endif |
10101 | 0 | } |
10102 | |
|
10103 | 0 | switch(ssl->options.asyncState) |
10104 | 0 | { |
10105 | 0 | case TLS_ASYNC_BEGIN: |
10106 | 0 | { |
10107 | | #ifdef WOLFSSL_CALLBACKS |
10108 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify"); |
10109 | | if (ssl->toInfoOn) AddLateName("CertificateVerify", |
10110 | | &ssl->timeoutInfo); |
10111 | | #endif |
10112 | | |
10113 | | /* Advance state and proceed */ |
10114 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
10115 | 0 | } /* case TLS_ASYNC_BEGIN */ |
10116 | 0 | FALL_THROUGH; |
10117 | |
|
10118 | 0 | case TLS_ASYNC_BUILD: |
10119 | 0 | { |
10120 | 0 | int validSigAlgo; |
10121 | | |
10122 | | /* Signature algorithm. */ |
10123 | 0 | if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) { |
10124 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10125 | 0 | } |
10126 | | |
10127 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10128 | | if (ssl->peerSigSpec == NULL) { |
10129 | | /* The peer did not respond. We didn't send CKS or they don't |
10130 | | * support it. Either way, we do not need to handle dual |
10131 | | * key/sig case. */ |
10132 | | ssl->sigSpec = NULL; |
10133 | | ssl->sigSpecSz = 0; |
10134 | | } |
10135 | | |
10136 | | /* If no CKS extension or either native or alternative, then just |
10137 | | * get a normal sigalgo. But if BOTH, then get the native and alt |
10138 | | * sig algos. */ |
10139 | | if (ssl->sigSpec == NULL || |
10140 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_NATIVE || |
10141 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10142 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10143 | 0 | ret = DecodeTls13SigAlg(input + args->idx, |
10144 | 0 | &ssl->options.peerHashAlgo, &ssl->options.peerSigAlgo); |
10145 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10146 | | } |
10147 | | else { |
10148 | | ret = DecodeTls13HybridSigAlg(input + args->idx, |
10149 | | &ssl->options.peerHashAlgo, |
10150 | | &ssl->options.peerSigAlgo, |
10151 | | &args->altSigAlgo); |
10152 | | } |
10153 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10154 | |
|
10155 | 0 | if (ret < 0) |
10156 | 0 | goto exit_dcv; |
10157 | 0 | args->idx += OPAQUE16_LEN; |
10158 | | |
10159 | | /* Signature length. */ |
10160 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) { |
10161 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10162 | 0 | } |
10163 | 0 | ato16(input + args->idx, &args->sz); |
10164 | 0 | args->idx += OPAQUE16_LEN; |
10165 | | |
10166 | | /* Signature data. */ |
10167 | 0 | if ((args->idx - args->begin) + args->sz > totalSz || |
10168 | 0 | args->sz > ENCRYPT_LEN) { |
10169 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10170 | 0 | } |
10171 | | |
10172 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10173 | | if ((ssl->sigSpec != NULL) && |
10174 | | (*ssl->sigSpec != WOLFSSL_CKS_SIGSPEC_NATIVE)) { |
10175 | | |
10176 | | word16 sa; |
10177 | | if (args->altSigAlgo == 0) |
10178 | | sa = ssl->options.peerSigAlgo; |
10179 | | else |
10180 | | sa = args->altSigAlgo; |
10181 | | |
10182 | | switch(sa) { |
10183 | | #ifndef NO_RSA |
10184 | | case rsa_pss_sa_algo: |
10185 | | ret = decodeRsaKey(ssl); |
10186 | | break; |
10187 | | #endif |
10188 | | #ifdef HAVE_ECC |
10189 | | case ecc_dsa_sa_algo: |
10190 | | ret = decodeEccKey(ssl); |
10191 | | break; |
10192 | | #endif |
10193 | | #ifdef HAVE_DILITHIUM |
10194 | | case dilithium_level2_sa_algo: |
10195 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_44); |
10196 | | break; |
10197 | | case dilithium_level3_sa_algo: |
10198 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_65); |
10199 | | break; |
10200 | | case dilithium_level5_sa_algo: |
10201 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_87); |
10202 | | break; |
10203 | | #endif |
10204 | | #ifdef HAVE_FALCON |
10205 | | case falcon_level1_sa_algo: |
10206 | | ret = decodeFalconKey(ssl, 1); |
10207 | | break; |
10208 | | case falcon_level5_sa_algo: |
10209 | | ret = decodeFalconKey(ssl, 5); |
10210 | | break; |
10211 | | #endif |
10212 | | default: |
10213 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10214 | | } |
10215 | | |
10216 | | if (ret != 0) |
10217 | | ERROR_OUT(ret, exit_dcv); |
10218 | | |
10219 | | if (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10220 | | /* Now swap in the alternative by removing the native. |
10221 | | * sa contains the alternative signature type. */ |
10222 | | #ifndef NO_RSA |
10223 | | if (ssl->peerRsaKeyPresent && sa != rsa_pss_sa_algo) { |
10224 | | FreeKey(ssl, DYNAMIC_TYPE_RSA, |
10225 | | (void**)&ssl->peerRsaKey); |
10226 | | ssl->peerRsaKeyPresent = 0; |
10227 | | } |
10228 | | #endif |
10229 | | #ifdef HAVE_ECC |
10230 | | else if (ssl->peerEccDsaKeyPresent && |
10231 | | sa != ecc_dsa_sa_algo) { |
10232 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
10233 | | (void**)&ssl->peerEccDsaKey); |
10234 | | ssl->peerEccDsaKeyPresent = 0; |
10235 | | } |
10236 | | #endif |
10237 | | #ifdef HAVE_DILITHIUM |
10238 | | else if (ssl->peerDilithiumKeyPresent && |
10239 | | sa != dilithium_level2_sa_algo && |
10240 | | sa != dilithium_level3_sa_algo && |
10241 | | sa != dilithium_level5_sa_algo) { |
10242 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10243 | | (void**)&ssl->peerDilithiumKey); |
10244 | | ssl->peerDilithiumKeyPresent = 0; |
10245 | | } |
10246 | | #endif |
10247 | | #ifdef HAVE_FALCON |
10248 | | else if (ssl->peerFalconKeyPresent && |
10249 | | sa != falcon_level1_sa_algo && |
10250 | | sa != falcon_level5_sa_algo) { |
10251 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10252 | | (void**)&ssl->peerFalconKey); |
10253 | | ssl->peerFalconKeyPresent = 0; |
10254 | | } |
10255 | | #endif |
10256 | | else { |
10257 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10258 | | } |
10259 | | } |
10260 | | } |
10261 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10262 | | |
10263 | | /* Check for public key of required type. */ |
10264 | | /* Assume invalid unless signature algo matches the key provided */ |
10265 | 0 | validSigAlgo = 0; |
10266 | 0 | #ifdef HAVE_ED25519 |
10267 | 0 | if (ssl->options.peerSigAlgo == ed25519_sa_algo) { |
10268 | 0 | WOLFSSL_MSG("Peer sent ED25519 sig"); |
10269 | 0 | validSigAlgo = (ssl->peerEd25519Key != NULL) && |
10270 | 0 | ssl->peerEd25519KeyPresent; |
10271 | 0 | } |
10272 | 0 | #endif |
10273 | 0 | #ifdef HAVE_ED448 |
10274 | 0 | if (ssl->options.peerSigAlgo == ed448_sa_algo) { |
10275 | 0 | WOLFSSL_MSG("Peer sent ED448 sig"); |
10276 | 0 | validSigAlgo = (ssl->peerEd448Key != NULL) && |
10277 | 0 | ssl->peerEd448KeyPresent; |
10278 | 0 | } |
10279 | 0 | #endif |
10280 | 0 | #ifdef HAVE_ECC |
10281 | 0 | if (ssl->options.peerSigAlgo == ecc_dsa_sa_algo) { |
10282 | 0 | WOLFSSL_MSG("Peer sent ECC sig"); |
10283 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10284 | 0 | ssl->peerEccDsaKeyPresent; |
10285 | 0 | } |
10286 | 0 | #endif |
10287 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10288 | 0 | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
10289 | 0 | WOLFSSL_MSG("Peer sent SM2 sig"); |
10290 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10291 | 0 | ssl->peerEccDsaKeyPresent; |
10292 | 0 | } |
10293 | 0 | #endif |
10294 | | #ifdef HAVE_FALCON |
10295 | | if (ssl->options.peerSigAlgo == falcon_level1_sa_algo) { |
10296 | | WOLFSSL_MSG("Peer sent Falcon Level 1 sig"); |
10297 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10298 | | ssl->peerFalconKeyPresent; |
10299 | | } |
10300 | | if (ssl->options.peerSigAlgo == falcon_level5_sa_algo) { |
10301 | | WOLFSSL_MSG("Peer sent Falcon Level 5 sig"); |
10302 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10303 | | ssl->peerFalconKeyPresent; |
10304 | | } |
10305 | | #endif |
10306 | | #ifdef HAVE_DILITHIUM |
10307 | | if (ssl->options.peerSigAlgo == dilithium_level2_sa_algo) { |
10308 | | WOLFSSL_MSG("Peer sent Dilithium Level 2 sig"); |
10309 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10310 | | ssl->peerDilithiumKeyPresent; |
10311 | | } |
10312 | | if (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) { |
10313 | | WOLFSSL_MSG("Peer sent Dilithium Level 3 sig"); |
10314 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10315 | | ssl->peerDilithiumKeyPresent; |
10316 | | } |
10317 | | if (ssl->options.peerSigAlgo == dilithium_level5_sa_algo) { |
10318 | | WOLFSSL_MSG("Peer sent Dilithium Level 5 sig"); |
10319 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10320 | | ssl->peerDilithiumKeyPresent; |
10321 | | } |
10322 | | #endif |
10323 | 0 | #ifndef NO_RSA |
10324 | 0 | if (ssl->options.peerSigAlgo == rsa_sa_algo) { |
10325 | 0 | WOLFSSL_MSG("Peer sent PKCS#1.5 algo - not valid TLS 1.3"); |
10326 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10327 | 0 | } |
10328 | 0 | if (ssl->options.peerSigAlgo == rsa_pss_sa_algo) { |
10329 | 0 | WOLFSSL_MSG("Peer sent RSA sig"); |
10330 | 0 | validSigAlgo = (ssl->peerRsaKey != NULL) && |
10331 | 0 | ssl->peerRsaKeyPresent; |
10332 | 0 | } |
10333 | 0 | #endif |
10334 | 0 | if (!validSigAlgo) { |
10335 | 0 | WOLFSSL_MSG("Sig algo doesn't correspond to certificate"); |
10336 | 0 | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
10337 | 0 | } |
10338 | | |
10339 | 0 | args->sigSz = args->sz; |
10340 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10341 | | if (ssl->sigSpec != NULL && |
10342 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10343 | | /* In case we received two signatures, both of them are encoded |
10344 | | * with their size as 16-bit integeter prior in memory. Hence, |
10345 | | * we can decode both lengths here now. */ |
10346 | | word32 tmpIdx = args->idx; |
10347 | | word16 tmpSz = 0; |
10348 | | ato16(input + tmpIdx, &tmpSz); |
10349 | | args->sigSz = tmpSz; |
10350 | | |
10351 | | tmpIdx += OPAQUE16_LEN + args->sigSz; |
10352 | | ato16(input + tmpIdx, &tmpSz); |
10353 | | args->altSignatureSz = tmpSz; |
10354 | | |
10355 | | if (args->sz != (args->sigSz + args->altSignatureSz + |
10356 | | OPAQUE16_LEN + OPAQUE16_LEN)) { |
10357 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10358 | | } |
10359 | | } |
10360 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10361 | |
|
10362 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
10363 | | /* In case we have to verify an RSA signature, we have to store the |
10364 | | * signature in the 'rsaSigBuf' structure for further processing. |
10365 | | */ |
10366 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
10367 | 0 | word32 sigSz = args->sigSz; |
10368 | 0 | sig = input + args->idx; |
10369 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10370 | | /* Check if our alternative signature was RSA */ |
10371 | | if (ssl->sigSpec != NULL && |
10372 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10373 | | if (ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10374 | | /* We have to skip the first signature (length field |
10375 | | * and signature itself) and the length field of the |
10376 | | * alternative signature. */ |
10377 | | sig += OPAQUE16_LEN + OPAQUE16_LEN + args->sigSz; |
10378 | | sigSz = args->altSignatureSz; |
10379 | | } |
10380 | | else { |
10381 | | /* We have to skip the length field */ |
10382 | | sig += OPAQUE16_LEN; |
10383 | | } |
10384 | | } |
10385 | | #endif |
10386 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(sigSz, ssl->heap, |
10387 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10388 | 0 | if (rsaSigBuf->buffer == NULL) { |
10389 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10390 | 0 | } |
10391 | 0 | rsaSigBuf->length = sigSz; |
10392 | 0 | XMEMCPY(rsaSigBuf->buffer, sig, rsaSigBuf->length); |
10393 | 0 | } |
10394 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
10395 | | |
10396 | 0 | args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10397 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10398 | 0 | if (args->sigData == NULL) { |
10399 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10400 | 0 | } |
10401 | | |
10402 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1); |
10403 | 0 | if (ret < 0) |
10404 | 0 | goto exit_dcv; |
10405 | | |
10406 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10407 | | if ((ssl->sigSpec != NULL) && |
10408 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
10409 | | args->altSigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10410 | | DYNAMIC_TYPE_SIGNATURE); |
10411 | | if (args->altSigData == NULL) { |
10412 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10413 | | } |
10414 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
10415 | | args->altSigDataSz = args->sigDataSz; |
10416 | | } |
10417 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10418 | | |
10419 | 0 | #ifdef HAVE_ECC |
10420 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10421 | 0 | (ssl->peerEccDsaKeyPresent)) { |
10422 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10423 | 0 | if (ssl->options.peerSigAlgo != sm2_sa_algo) |
10424 | 0 | #endif |
10425 | 0 | { |
10426 | 0 | ret = CreateECCEncodedSig(args->sigData, |
10427 | 0 | args->sigDataSz, ssl->options.peerHashAlgo); |
10428 | 0 | if (ret < 0) |
10429 | 0 | goto exit_dcv; |
10430 | 0 | args->sigDataSz = (word16)ret; |
10431 | 0 | ret = 0; |
10432 | 0 | } |
10433 | 0 | } |
10434 | | |
10435 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10436 | | if ((ssl->sigSpec != NULL) && |
10437 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
10438 | | (args->altSigAlgo == ecc_dsa_sa_algo) && |
10439 | | (ssl->peerEccDsaKeyPresent)) { |
10440 | | ret = CreateECCEncodedSig(args->altSigData, |
10441 | | args->altSigDataSz, ssl->options.peerHashAlgo); |
10442 | | if (ret < 0) |
10443 | | goto exit_dcv; |
10444 | | args->altSigDataSz = (word16)ret; |
10445 | | ret = 0; |
10446 | | } |
10447 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10448 | 0 | #endif /* HAVE_ECC */ |
10449 | | |
10450 | | /* Advance state and proceed */ |
10451 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
10452 | 0 | } /* case TLS_ASYNC_BUILD */ |
10453 | 0 | FALL_THROUGH; |
10454 | |
|
10455 | 0 | case TLS_ASYNC_DO: |
10456 | 0 | { |
10457 | 0 | sig = input + args->idx; |
10458 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10459 | | if (ssl->sigSpec != NULL && |
10460 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10461 | | /* As we have two signatures in the message, we stored |
10462 | | * the length of each before the actual signature. This |
10463 | | * is necessary, as we could have two algorithms with |
10464 | | * variable length signatures. */ |
10465 | | sig += OPAQUE16_LEN; |
10466 | | } |
10467 | | #endif |
10468 | 0 | #ifndef NO_RSA |
10469 | 0 | if ((ssl->options.peerSigAlgo == rsa_pss_sa_algo) && |
10470 | 0 | (ssl->peerRsaKey != NULL) && (ssl->peerRsaKeyPresent != 0)) { |
10471 | 0 | WOLFSSL_MSG("Doing RSA peer cert verify"); |
10472 | 0 | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
10473 | 0 | (word32)rsaSigBuf->length, &args->output, |
10474 | 0 | ssl->options.peerSigAlgo, |
10475 | 0 | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
10476 | | #ifdef HAVE_PK_CALLBACKS |
10477 | | &ssl->buffers.peerRsaKey |
10478 | | #else |
10479 | 0 | NULL |
10480 | 0 | #endif |
10481 | 0 | ); |
10482 | 0 | if (ret >= 0) { |
10483 | 0 | args->sendSz = (word32)ret; |
10484 | 0 | ret = 0; |
10485 | 0 | } |
10486 | 0 | } |
10487 | 0 | #endif /* !NO_RSA */ |
10488 | 0 | #ifdef HAVE_ECC |
10489 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10490 | 0 | (ssl->peerEccDsaKeyPresent)) { |
10491 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10492 | 0 | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
10493 | 0 | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
10494 | 0 | TLS13_SM2_SIG_ID_SZ, sig, args->sigSz, |
10495 | 0 | args->sigData, args->sigDataSz, |
10496 | 0 | ssl->peerEccDsaKey, NULL); |
10497 | 0 | } |
10498 | 0 | else |
10499 | 0 | #endif |
10500 | 0 | { |
10501 | 0 | WOLFSSL_MSG("Doing ECC peer cert verify"); |
10502 | 0 | ret = EccVerify(ssl, sig, args->sigSz, |
10503 | 0 | args->sigData, args->sigDataSz, |
10504 | 0 | ssl->peerEccDsaKey, |
10505 | | #ifdef HAVE_PK_CALLBACKS |
10506 | | &ssl->buffers.peerEccDsaKey |
10507 | | #else |
10508 | 0 | NULL |
10509 | 0 | #endif |
10510 | 0 | ); |
10511 | 0 | } |
10512 | |
|
10513 | 0 | if (ret >= 0) { |
10514 | | /* CLIENT/SERVER: data verified with public key from |
10515 | | * certificate. */ |
10516 | 0 | ssl->options.peerAuthGood = 1; |
10517 | |
|
10518 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10519 | 0 | ssl->peerEccDsaKeyPresent = 0; |
10520 | 0 | } |
10521 | 0 | } |
10522 | 0 | #endif /* HAVE_ECC */ |
10523 | 0 | #ifdef HAVE_ED25519 |
10524 | 0 | if ((ssl->options.peerSigAlgo == ed25519_sa_algo) && |
10525 | 0 | (ssl->peerEd25519KeyPresent)) { |
10526 | 0 | WOLFSSL_MSG("Doing ED25519 peer cert verify"); |
10527 | 0 | ret = Ed25519Verify(ssl, sig, args->sigSz, |
10528 | 0 | args->sigData, args->sigDataSz, |
10529 | 0 | ssl->peerEd25519Key, |
10530 | | #ifdef HAVE_PK_CALLBACKS |
10531 | | &ssl->buffers.peerEd25519Key |
10532 | | #else |
10533 | 0 | NULL |
10534 | 0 | #endif |
10535 | 0 | ); |
10536 | |
|
10537 | 0 | if (ret >= 0) { |
10538 | | /* CLIENT/SERVER: data verified with public key from |
10539 | | * certificate. */ |
10540 | 0 | ssl->options.peerAuthGood = 1; |
10541 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED25519, |
10542 | 0 | (void**)&ssl->peerEd25519Key); |
10543 | 0 | ssl->peerEd25519KeyPresent = 0; |
10544 | 0 | } |
10545 | 0 | } |
10546 | 0 | #endif |
10547 | 0 | #ifdef HAVE_ED448 |
10548 | 0 | if ((ssl->options.peerSigAlgo == ed448_sa_algo) && |
10549 | 0 | (ssl->peerEd448KeyPresent)) { |
10550 | 0 | WOLFSSL_MSG("Doing ED448 peer cert verify"); |
10551 | 0 | ret = Ed448Verify(ssl, sig, args->sigSz, |
10552 | 0 | args->sigData, args->sigDataSz, |
10553 | 0 | ssl->peerEd448Key, |
10554 | | #ifdef HAVE_PK_CALLBACKS |
10555 | | &ssl->buffers.peerEd448Key |
10556 | | #else |
10557 | 0 | NULL |
10558 | 0 | #endif |
10559 | 0 | ); |
10560 | |
|
10561 | 0 | if (ret >= 0) { |
10562 | | /* CLIENT/SERVER: data verified with public key from |
10563 | | * certificate. */ |
10564 | 0 | ssl->options.peerAuthGood = 1; |
10565 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED448, |
10566 | 0 | (void**)&ssl->peerEd448Key); |
10567 | 0 | ssl->peerEd448KeyPresent = 0; |
10568 | 0 | } |
10569 | 0 | } |
10570 | 0 | #endif |
10571 | | #if defined(HAVE_FALCON) |
10572 | | if (((ssl->options.peerSigAlgo == falcon_level1_sa_algo) || |
10573 | | (ssl->options.peerSigAlgo == falcon_level5_sa_algo)) && |
10574 | | (ssl->peerFalconKeyPresent)) { |
10575 | | int res = 0; |
10576 | | WOLFSSL_MSG("Doing Falcon peer cert verify"); |
10577 | | ret = wc_falcon_verify_msg(sig, args->sigSz, |
10578 | | args->sigData, args->sigDataSz, |
10579 | | &res, ssl->peerFalconKey); |
10580 | | |
10581 | | if ((ret >= 0) && (res == 1)) { |
10582 | | /* CLIENT/SERVER: data verified with public key from |
10583 | | * certificate. */ |
10584 | | ssl->options.peerAuthGood = 1; |
10585 | | |
10586 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10587 | | (void**)&ssl->peerFalconKey); |
10588 | | ssl->peerFalconKeyPresent = 0; |
10589 | | } |
10590 | | else if ((ret >= 0) && (res == 0)) { |
10591 | | WOLFSSL_MSG("Falcon signature verification failed"); |
10592 | | ret = SIG_VERIFY_E; |
10593 | | } |
10594 | | } |
10595 | | #endif /* HAVE_FALCON */ |
10596 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
10597 | | if (((ssl->options.peerSigAlgo == dilithium_level2_sa_algo) || |
10598 | | (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) || |
10599 | | (ssl->options.peerSigAlgo == dilithium_level5_sa_algo)) && |
10600 | | (ssl->peerDilithiumKeyPresent)) { |
10601 | | int res = 0; |
10602 | | WOLFSSL_MSG("Doing Dilithium peer cert verify"); |
10603 | | ret = wc_dilithium_verify_ctx_msg(sig, args->sigSz, NULL, 0, |
10604 | | args->sigData, args->sigDataSz, |
10605 | | &res, ssl->peerDilithiumKey); |
10606 | | |
10607 | | if ((ret >= 0) && (res == 1)) { |
10608 | | /* CLIENT/SERVER: data verified with public key from |
10609 | | * certificate. */ |
10610 | | ssl->options.peerAuthGood = 1; |
10611 | | |
10612 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10613 | | (void**)&ssl->peerDilithiumKey); |
10614 | | ssl->peerDilithiumKeyPresent = 0; |
10615 | | } |
10616 | | else if ((ret >= 0) && (res == 0)) { |
10617 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
10618 | | ret = SIG_VERIFY_E; |
10619 | | } |
10620 | | } |
10621 | | #endif /* HAVE_DILITHIUM */ |
10622 | | |
10623 | | /* Check for error */ |
10624 | 0 | if (ret != 0) { |
10625 | 0 | goto exit_dcv; |
10626 | 0 | } |
10627 | | |
10628 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10629 | | if (ssl->sigSpec != NULL && |
10630 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10631 | | /* Move forward to the alternative signature. */ |
10632 | | sig += args->sigSz + OPAQUE16_LEN; |
10633 | | |
10634 | | /* Verify the alternative signature */ |
10635 | | #ifndef NO_RSA |
10636 | | if ((args->altSigAlgo == rsa_pss_sa_algo) && |
10637 | | (ssl->peerRsaKey != NULL) && |
10638 | | (ssl->peerRsaKeyPresent != 0)) { |
10639 | | WOLFSSL_MSG("Doing RSA peer cert alt verify"); |
10640 | | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
10641 | | (word32)rsaSigBuf->length, |
10642 | | &args->output, args->altSigAlgo, |
10643 | | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
10644 | | #ifdef HAVE_PK_CALLBACKS |
10645 | | &ssl->buffers.peerRsaKey |
10646 | | #else |
10647 | | NULL |
10648 | | #endif |
10649 | | ); |
10650 | | if (ret >= 0) { |
10651 | | args->sendSz = ret; |
10652 | | ret = 0; |
10653 | | } |
10654 | | } |
10655 | | #endif /* !NO_RSA */ |
10656 | | #ifdef HAVE_ECC |
10657 | | if ((args->altSigAlgo == ecc_dsa_sa_algo) && |
10658 | | (ssl->peerEccDsaKeyPresent)) { |
10659 | | WOLFSSL_MSG("Doing ECC peer cert alt verify"); |
10660 | | ret = EccVerify(ssl, sig, args->altSignatureSz, |
10661 | | args->altSigData, args->altSigDataSz, |
10662 | | ssl->peerEccDsaKey, |
10663 | | #ifdef HAVE_PK_CALLBACKS |
10664 | | &ssl->buffers.peerEccDsaKey |
10665 | | #else |
10666 | | NULL |
10667 | | #endif |
10668 | | ); |
10669 | | |
10670 | | if (ret >= 0) { |
10671 | | /* CLIENT/SERVER: data verified with public key from |
10672 | | * certificate. */ |
10673 | | args->altPeerAuthGood = 1; |
10674 | | |
10675 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
10676 | | (void**)&ssl->peerEccDsaKey); |
10677 | | ssl->peerEccDsaKeyPresent = 0; |
10678 | | } |
10679 | | } |
10680 | | #endif /* HAVE_ECC */ |
10681 | | #if defined(HAVE_FALCON) |
10682 | | if (((args->altSigAlgo == falcon_level1_sa_algo) || |
10683 | | (args->altSigAlgo == falcon_level5_sa_algo)) && |
10684 | | (ssl->peerFalconKeyPresent)) { |
10685 | | int res = 0; |
10686 | | WOLFSSL_MSG("Doing Falcon peer cert alt verify"); |
10687 | | ret = wc_falcon_verify_msg(sig, args->altSignatureSz, |
10688 | | args->altSigData, args->altSigDataSz, |
10689 | | &res, ssl->peerFalconKey); |
10690 | | |
10691 | | if ((ret >= 0) && (res == 1)) { |
10692 | | /* CLIENT/SERVER: data verified with public key from |
10693 | | * certificate. */ |
10694 | | args->altPeerAuthGood = 1; |
10695 | | |
10696 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10697 | | (void**)&ssl->peerFalconKey); |
10698 | | ssl->peerFalconKeyPresent = 0; |
10699 | | } |
10700 | | else if ((ret >= 0) && (res == 0)) { |
10701 | | WOLFSSL_MSG("Falcon signature verification failed"); |
10702 | | ret = SIG_VERIFY_E; |
10703 | | } |
10704 | | } |
10705 | | #endif /* HAVE_FALCON */ |
10706 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
10707 | | if (((args->altSigAlgo == dilithium_level2_sa_algo) || |
10708 | | (args->altSigAlgo == dilithium_level3_sa_algo) || |
10709 | | (args->altSigAlgo == dilithium_level5_sa_algo)) && |
10710 | | (ssl->peerDilithiumKeyPresent)) { |
10711 | | int res = 0; |
10712 | | WOLFSSL_MSG("Doing Dilithium peer cert alt verify"); |
10713 | | ret = wc_dilithium_verify_ctx_msg(sig, args->altSignatureSz, |
10714 | | NULL, 0, args->altSigData, |
10715 | | args->altSigDataSz, &res, |
10716 | | ssl->peerDilithiumKey); |
10717 | | |
10718 | | if ((ret >= 0) && (res == 1)) { |
10719 | | /* CLIENT/SERVER: data verified with public key from |
10720 | | * certificate. */ |
10721 | | args->altPeerAuthGood = 1; |
10722 | | |
10723 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10724 | | (void**)&ssl->peerDilithiumKey); |
10725 | | ssl->peerDilithiumKeyPresent = 0; |
10726 | | } |
10727 | | else if ((ret >= 0) && (res == 0)) { |
10728 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
10729 | | ret = SIG_VERIFY_E; |
10730 | | } |
10731 | | } |
10732 | | #endif /* HAVE_DILITHIUM */ |
10733 | | |
10734 | | /* Check for error */ |
10735 | | if (ret != 0) { |
10736 | | goto exit_dcv; |
10737 | | } |
10738 | | } |
10739 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10740 | | |
10741 | | /* Advance state and proceed */ |
10742 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
10743 | 0 | } /* case TLS_ASYNC_DO */ |
10744 | 0 | FALL_THROUGH; |
10745 | |
|
10746 | 0 | case TLS_ASYNC_VERIFY: |
10747 | 0 | { |
10748 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
10749 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
10750 | 0 | int sigAlgo = ssl->options.peerSigAlgo; |
10751 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10752 | | /* Check if our alternative signature was RSA */ |
10753 | | if (ssl->sigSpec != NULL && |
10754 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10755 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10756 | | sigAlgo = args->altSigAlgo; |
10757 | | } |
10758 | | #endif |
10759 | 0 | ret = CheckRSASignature(ssl, sigAlgo, |
10760 | 0 | ssl->options.peerHashAlgo, args->output, args->sendSz); |
10761 | 0 | if (ret != 0) |
10762 | 0 | goto exit_dcv; |
10763 | | |
10764 | | /* CLIENT/SERVER: data verified with public key from |
10765 | | * certificate. */ |
10766 | 0 | ssl->peerRsaKeyPresent = 0; |
10767 | 0 | FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
10768 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10769 | | /* Check if our alternative signature was RSA */ |
10770 | | if (ssl->sigSpec != NULL && |
10771 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10772 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10773 | | args->altPeerAuthGood = 1; |
10774 | | } |
10775 | | else |
10776 | | #endif |
10777 | 0 | ssl->options.peerAuthGood = 1; |
10778 | 0 | } |
10779 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
10780 | | |
10781 | | /* Advance state and proceed */ |
10782 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
10783 | 0 | } /* case TLS_ASYNC_VERIFY */ |
10784 | 0 | FALL_THROUGH; |
10785 | |
|
10786 | 0 | case TLS_ASYNC_FINALIZE: |
10787 | 0 | { |
10788 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10789 | | if (ssl->options.peerAuthGood && |
10790 | | ssl->sigSpec != NULL && |
10791 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10792 | | ssl->options.peerAuthGood = args->altPeerAuthGood; |
10793 | | } |
10794 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10795 | 0 | ssl->options.havePeerVerify = 1; |
10796 | | |
10797 | | /* Set final index */ |
10798 | 0 | args->idx += args->sz; |
10799 | 0 | *inOutIdx = args->idx; |
10800 | | |
10801 | | /* Encryption is always on: add padding */ |
10802 | 0 | *inOutIdx += ssl->keys.padSz; |
10803 | | |
10804 | | /* Advance state and proceed */ |
10805 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
10806 | |
|
10807 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
10808 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
10809 | 0 | ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE; |
10810 | 0 | #endif |
10811 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
10812 | 0 | FALL_THROUGH; |
10813 | |
|
10814 | 0 | case TLS_ASYNC_END: |
10815 | 0 | { |
10816 | 0 | break; |
10817 | 0 | } |
10818 | | |
10819 | 0 | default: |
10820 | 0 | ret = INPUT_CASE_ERROR; |
10821 | 0 | } /* switch(ssl->options.asyncState) */ |
10822 | | |
10823 | 0 | exit_dcv: |
10824 | |
|
10825 | 0 | WOLFSSL_LEAVE("DoTls13CertificateVerify", ret); |
10826 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10827 | |
|
10828 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10829 | | /* Handle async operation */ |
10830 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
10831 | | /* Mark message as not received so it can process again */ |
10832 | | ssl->msgsReceived.got_certificate_verify = 0; |
10833 | | |
10834 | | return ret; |
10835 | | } |
10836 | | else |
10837 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
10838 | 0 | if (ret != 0) { |
10839 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
10840 | |
|
10841 | 0 | if (ret != WC_NO_ERR_TRACE(INVALID_PARAMETER)) { |
10842 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
10843 | 0 | } |
10844 | 0 | } |
10845 | | |
10846 | | /* Final cleanup */ |
10847 | 0 | FreeDcv13Args(ssl, args); |
10848 | 0 | FreeKeyExchange(ssl); |
10849 | 0 | #ifdef WOLFSSL_ASYNC_IO |
10850 | | /* Cleanup async */ |
10851 | 0 | FreeAsyncCtx(ssl, 0); |
10852 | 0 | #endif |
10853 | |
|
10854 | 0 | return ret; |
10855 | 0 | } |
10856 | | #endif /* !NO_RSA || HAVE_ECC */ |
10857 | | #endif /* !NO_CERTS */ |
10858 | | |
10859 | | /* Parse and handle a TLS v1.3 Finished message. |
10860 | | * |
10861 | | * ssl The SSL/TLS object. |
10862 | | * input The message buffer. |
10863 | | * inOutIdx On entry, the index into the message buffer of Finished. |
10864 | | * On exit, the index of byte after the Finished message and padding. |
10865 | | * size Length of message data. |
10866 | | * totalSz Length of remaining data in the message buffer. |
10867 | | * sniff Indicates whether we are sniffing packets. |
10868 | | * returns 0 on success and otherwise failure. |
10869 | | */ |
10870 | | int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
10871 | | word32 size, word32 totalSz, int sniff) |
10872 | 0 | { |
10873 | 0 | int ret; |
10874 | 0 | word32 finishedSz = 0; |
10875 | 0 | byte* secret; |
10876 | 0 | byte mac[WC_MAX_DIGEST_SIZE]; |
10877 | |
|
10878 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_DO); |
10879 | 0 | WOLFSSL_ENTER("DoTls13Finished"); |
10880 | |
|
10881 | 0 | #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH) |
10882 | | /* verify the client sent certificate if required */ |
10883 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && !ssl->options.resuming && |
10884 | 0 | (ssl->options.mutualAuth || ssl->options.failNoCert)) { |
10885 | | #ifdef OPENSSL_COMPATIBLE_DEFAULTS |
10886 | | if (ssl->options.isPSK) { |
10887 | | WOLFSSL_MSG("TLS v1.3 client used PSK but cert required. Allowing " |
10888 | | "for OpenSSL compatibility"); |
10889 | | } |
10890 | | else |
10891 | | #endif |
10892 | 0 | if ( |
10893 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
10894 | | !ssl->options.verifyPostHandshake && |
10895 | | #endif |
10896 | 0 | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
10897 | 0 | ret = NO_PEER_CERT; /* NO_PEER_VERIFY */ |
10898 | 0 | WOLFSSL_MSG("TLS v1.3 client did not present peer cert"); |
10899 | 0 | DoCertFatalAlert(ssl, ret); |
10900 | 0 | return ret; |
10901 | 0 | } |
10902 | 0 | } |
10903 | 0 | #endif |
10904 | | |
10905 | | /* check against totalSz */ |
10906 | 0 | if (*inOutIdx + size > totalSz) |
10907 | 0 | return BUFFER_E; |
10908 | | |
10909 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10910 | | ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz); |
10911 | | if (ret == 0) { |
10912 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
10913 | | return ret; |
10914 | | } |
10915 | | if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) { |
10916 | | SendAlert(ssl, alert_fatal, decrypt_error); |
10917 | | return ret; |
10918 | | } |
10919 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10920 | | /* other errors */ |
10921 | | return ret; |
10922 | | } |
10923 | | ret = 0; |
10924 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
10925 | | |
10926 | 0 | if (ssl->options.handShakeDone) { |
10927 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
10928 | 0 | ssl->keys.client_write_MAC_secret, |
10929 | 0 | WOLFSSL_CLIENT_END); |
10930 | 0 | if (ret != 0) |
10931 | 0 | return ret; |
10932 | | |
10933 | 0 | secret = ssl->keys.client_write_MAC_secret; |
10934 | 0 | } |
10935 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) { |
10936 | | /* All the handshake messages have been received to calculate |
10937 | | * client and server finished keys. |
10938 | | */ |
10939 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
10940 | 0 | ssl->keys.client_write_MAC_secret, |
10941 | 0 | WOLFSSL_CLIENT_END); |
10942 | 0 | if (ret != 0) |
10943 | 0 | return ret; |
10944 | | |
10945 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
10946 | 0 | ssl->keys.server_write_MAC_secret, |
10947 | 0 | WOLFSSL_SERVER_END); |
10948 | 0 | if (ret != 0) |
10949 | 0 | return ret; |
10950 | | |
10951 | 0 | secret = ssl->keys.server_write_MAC_secret; |
10952 | 0 | } |
10953 | 0 | else { |
10954 | 0 | secret = ssl->keys.client_write_MAC_secret; |
10955 | 0 | } |
10956 | | |
10957 | 0 | if (sniff == NO_SNIFF) { |
10958 | |
|
10959 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz); |
10960 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
10961 | | if (finishedSz > TLS_FINISHED_SZ_MAX) { |
10962 | | return BUFFER_ERROR; |
10963 | | } |
10964 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
10965 | | XMEMCPY(ssl->serverFinished, mac, finishedSz); |
10966 | | ssl->serverFinished_len = (byte)finishedSz; |
10967 | | } |
10968 | | else { |
10969 | | XMEMCPY(ssl->clientFinished, mac, finishedSz); |
10970 | | ssl->clientFinished_len = (byte)finishedSz; |
10971 | | } |
10972 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
10973 | 0 | if (ret != 0) |
10974 | 0 | return ret; |
10975 | 0 | if (size != finishedSz) |
10976 | 0 | return BUFFER_ERROR; |
10977 | 0 | } |
10978 | | |
10979 | | #ifdef WOLFSSL_CALLBACKS |
10980 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
10981 | | if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo); |
10982 | | #endif |
10983 | | |
10984 | 0 | if (sniff == NO_SNIFF) { |
10985 | | /* Actually check verify data. */ |
10986 | 0 | if (size > WC_MAX_DIGEST_SIZE || |
10987 | 0 | XMEMCMP(input + *inOutIdx, mac, size) != 0){ |
10988 | 0 | WOLFSSL_MSG("Verify finished error on hashes"); |
10989 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
10990 | 0 | WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR); |
10991 | 0 | return VERIFY_FINISHED_ERROR; |
10992 | 0 | } |
10993 | 0 | } |
10994 | | |
10995 | | /* Force input exhaustion at ProcessReply by consuming padSz. */ |
10996 | 0 | *inOutIdx += size + ssl->keys.padSz; |
10997 | |
|
10998 | 0 | #ifndef NO_WOLFSSL_SERVER |
10999 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
11000 | 0 | !ssl->options.handShakeDone) { |
11001 | | #ifdef WOLFSSL_EARLY_DATA |
11002 | | if (ssl->earlyData != no_early_data) { |
11003 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0) |
11004 | | return ret; |
11005 | | } |
11006 | | #endif |
11007 | | /* Setup keys for application data messages from client. */ |
11008 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11009 | 0 | return ret; |
11010 | 0 | } |
11011 | 0 | #endif |
11012 | | |
11013 | 0 | #ifndef NO_WOLFSSL_CLIENT |
11014 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11015 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11016 | 0 | #endif |
11017 | 0 | #ifndef NO_WOLFSSL_SERVER |
11018 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11019 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11020 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
11021 | 0 | ssl->options.handShakeDone = 1; |
11022 | 0 | } |
11023 | 0 | #endif |
11024 | |
|
11025 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_EARLY_DATA) |
11026 | | if (ssl->options.dtls && ssl->earlyData > early_data_ext) { |
11027 | | /* DTLSv1.3 has no EndOfearlydata messages. We stop processing EarlyData |
11028 | | as soon we receive the client's finished message */ |
11029 | | ssl->earlyData = done_early_data; |
11030 | | } |
11031 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_EARLY_DATA */ |
11032 | | #if defined(WOLFSSL_QUIC) && defined(WOLFSSL_EARLY_DATA) |
11033 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData > early_data_ext) { |
11034 | | /* QUIC has no EndOfEarlyData messages. We stop processing EarlyData |
11035 | | as soon we receive the client's finished message */ |
11036 | | ssl->earlyData = done_early_data; |
11037 | | } |
11038 | | #endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */ |
11039 | |
|
11040 | 0 | WOLFSSL_LEAVE("DoTls13Finished", 0); |
11041 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_DO); |
11042 | |
|
11043 | 0 | return 0; |
11044 | 0 | } |
11045 | | |
11046 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
11047 | | /* Send the TLS v1.3 Finished message. |
11048 | | * |
11049 | | * ssl The SSL/TLS object. |
11050 | | * returns 0 on success, otherwise failure. |
11051 | | */ |
11052 | | static int SendTls13Finished(WOLFSSL* ssl) |
11053 | | { |
11054 | | byte finishedSz = ssl->specs.hash_size; |
11055 | | byte* input; |
11056 | | byte* output; |
11057 | | int ret; |
11058 | | int headerSz = HANDSHAKE_HEADER_SZ; |
11059 | | int outputSz; |
11060 | | byte* secret; |
11061 | | |
11062 | | #ifdef WOLFSSL_DTLS13 |
11063 | | int dtlsRet = 0, isDtls = 0; |
11064 | | #endif /* WOLFSSL_DTLS13 */ |
11065 | | |
11066 | | WOLFSSL_START(WC_FUNC_FINISHED_SEND); |
11067 | | WOLFSSL_ENTER("SendTls13Finished"); |
11068 | | |
11069 | | ssl->options.buildingMsg = 1; |
11070 | | #ifdef WOLFSSL_DTLS13 |
11071 | | if (ssl->options.dtls) { |
11072 | | headerSz = DTLS_HANDSHAKE_HEADER_SZ; |
11073 | | /* using isDtls instead of ssl->options.dtls will abide clang static |
11074 | | analyzer on using an uninitialized value */ |
11075 | | isDtls = 1; |
11076 | | } |
11077 | | #endif /* WOLFSSL_DTLS13 */ |
11078 | | |
11079 | | outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA; |
11080 | | /* Check buffers are big enough and grow if needed. */ |
11081 | | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11082 | | return ret; |
11083 | | |
11084 | | /* get output buffer */ |
11085 | | output = GetOutputBuffer(ssl); |
11086 | | input = output + RECORD_HEADER_SZ; |
11087 | | |
11088 | | #ifdef WOLFSSL_DTLS13 |
11089 | | if (isDtls) |
11090 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11091 | | #endif /* WOLFSSL_DTLS13 */ |
11092 | | |
11093 | | AddTls13HandShakeHeader(input, (word32)finishedSz, 0, (word32)finishedSz, |
11094 | | finished, ssl); |
11095 | | |
11096 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11097 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11098 | | ret = tsip_Tls13SendFinished(ssl, output, outputSz, input, 1); |
11099 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11100 | | return ret; |
11101 | | } |
11102 | | ret = 0; |
11103 | | } |
11104 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11105 | | |
11106 | | /* make finished hashes */ |
11107 | | if (ssl->options.handShakeDone) { |
11108 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11109 | | ssl->keys.client_write_MAC_secret, |
11110 | | WOLFSSL_CLIENT_END); |
11111 | | if (ret != 0) |
11112 | | return ret; |
11113 | | |
11114 | | secret = ssl->keys.client_write_MAC_secret; |
11115 | | } |
11116 | | else if (ssl->options.side == WOLFSSL_CLIENT_END) |
11117 | | secret = ssl->keys.client_write_MAC_secret; |
11118 | | else { |
11119 | | /* All the handshake messages have been done to calculate client and |
11120 | | * server finished keys. |
11121 | | */ |
11122 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11123 | | ssl->keys.client_write_MAC_secret, |
11124 | | WOLFSSL_SERVER_END); |
11125 | | if (ret != 0) |
11126 | | return ret; |
11127 | | |
11128 | | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11129 | | ssl->keys.server_write_MAC_secret, |
11130 | | WOLFSSL_CLIENT_END); |
11131 | | if (ret != 0) |
11132 | | return ret; |
11133 | | |
11134 | | secret = ssl->keys.server_write_MAC_secret; |
11135 | | } |
11136 | | ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL); |
11137 | | if (ret != 0) |
11138 | | return ret; |
11139 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11140 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11141 | | XMEMCPY(ssl->clientFinished, &input[headerSz], finishedSz); |
11142 | | ssl->clientFinished_len = finishedSz; |
11143 | | } |
11144 | | else { |
11145 | | XMEMCPY(ssl->serverFinished, &input[headerSz], finishedSz); |
11146 | | ssl->serverFinished_len = finishedSz; |
11147 | | } |
11148 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11149 | | |
11150 | | #ifdef WOLFSSL_DTLS13 |
11151 | | if (isDtls) { |
11152 | | dtlsRet = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11153 | | (word16)(Dtls13GetRlHeaderLength(ssl, 1) + headerSz + finishedSz), finished, |
11154 | | 1); |
11155 | | if (dtlsRet != 0 && dtlsRet != WC_NO_ERR_TRACE(WANT_WRITE)) |
11156 | | return ret; |
11157 | | |
11158 | | } else |
11159 | | #endif /* WOLFSSL_DTLS13 */ |
11160 | | { |
11161 | | /* This message is always encrypted. */ |
11162 | | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11163 | | headerSz + finishedSz, handshake, 1, 0, 0); |
11164 | | if (sendSz < 0) { |
11165 | | WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR); |
11166 | | return BUILD_MSG_ERROR; |
11167 | | } |
11168 | | |
11169 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11170 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11171 | | if (ssl->toInfoOn) { |
11172 | | ret = AddPacketInfo(ssl, "Finished", handshake, output, sendSz, |
11173 | | WRITE_PROTO, 0, ssl->heap); |
11174 | | if (ret != 0) |
11175 | | return ret; |
11176 | | } |
11177 | | #endif |
11178 | | |
11179 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11180 | | ssl->options.buildingMsg = 0; |
11181 | | } |
11182 | | |
11183 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11184 | | #ifdef WOLFSSL_EARLY_DATA |
11185 | | byte storeTrafficDecKeys = ssl->earlyData == no_early_data; |
11186 | | #endif |
11187 | | /* Can send application data now. */ |
11188 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
11189 | | return ret; |
11190 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
11191 | | ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz); |
11192 | | #ifdef WOLFSSL_EARLY_DATA |
11193 | | |
11194 | | #ifdef WOLFSSL_DTLS13 |
11195 | | /* DTLS13 dynamically change keys and it needs all |
11196 | | the keys in ssl->keys to save the keying material */ |
11197 | | if (isDtls) |
11198 | | storeTrafficDecKeys = 1; |
11199 | | #endif /* WOLFSSL_DTLS13 */ |
11200 | | |
11201 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11202 | | != 0) { |
11203 | | return ret; |
11204 | | } |
11205 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY, |
11206 | | storeTrafficDecKeys)) != 0) { |
11207 | | return ret; |
11208 | | } |
11209 | | #else |
11210 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE, |
11211 | | 1)) != 0) { |
11212 | | return ret; |
11213 | | } |
11214 | | #endif |
11215 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11216 | | return ret; |
11217 | | |
11218 | | #ifdef WOLFSSL_DTLS13 |
11219 | | if (isDtls) { |
11220 | | w64wrapper epochTraffic0; |
11221 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11222 | | ssl->dtls13Epoch = epochTraffic0; |
11223 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11224 | | |
11225 | | ret = Dtls13SetEpochKeys( |
11226 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11227 | | if (ret != 0) |
11228 | | return ret; |
11229 | | |
11230 | | } |
11231 | | #endif /* WOLFSSL_DTLS13 */ |
11232 | | |
11233 | | } |
11234 | | |
11235 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
11236 | | !ssl->options.handShakeDone) { |
11237 | | #ifdef WOLFSSL_EARLY_DATA |
11238 | | if (ssl->earlyData != no_early_data) { |
11239 | | if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_SIDE_ONLY, |
11240 | | 1)) != 0) { |
11241 | | return ret; |
11242 | | } |
11243 | | } |
11244 | | #endif |
11245 | | /* Setup keys for application data messages. */ |
11246 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11247 | | return ret; |
11248 | | |
11249 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
11250 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
11251 | | if (ret != 0) |
11252 | | return ret; |
11253 | | #endif |
11254 | | |
11255 | | #ifdef WOLFSSL_DTLS13 |
11256 | | if (isDtls) { |
11257 | | w64wrapper epochTraffic0; |
11258 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11259 | | ssl->dtls13Epoch = epochTraffic0; |
11260 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11261 | | |
11262 | | ret = Dtls13SetEpochKeys( |
11263 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11264 | | if (ret != 0) |
11265 | | return ret; |
11266 | | |
11267 | | } |
11268 | | #endif /* WOLFSSL_DTLS13 */ |
11269 | | } |
11270 | | |
11271 | | #ifndef NO_WOLFSSL_CLIENT |
11272 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11273 | | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11274 | | ssl->options.handShakeState = HANDSHAKE_DONE; |
11275 | | ssl->options.handShakeDone = 1; |
11276 | | } |
11277 | | #endif |
11278 | | #ifndef NO_WOLFSSL_SERVER |
11279 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11280 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11281 | | } |
11282 | | #endif |
11283 | | |
11284 | | #ifdef WOLFSSL_DTLS13 |
11285 | | if (isDtls) { |
11286 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11287 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11288 | | |
11289 | | return dtlsRet; |
11290 | | } |
11291 | | #endif /* WOLFSSL_DTLS13 */ |
11292 | | |
11293 | | if ((ret = SendBuffered(ssl)) != 0) |
11294 | | return ret; |
11295 | | |
11296 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11297 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11298 | | |
11299 | | return ret; |
11300 | | } |
11301 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
11302 | | |
11303 | | /* handle generation TLS v1.3 key_update (24) */ |
11304 | | /* Send the TLS v1.3 KeyUpdate message. |
11305 | | * |
11306 | | * ssl The SSL/TLS object. |
11307 | | * returns 0 on success, otherwise failure. |
11308 | | */ |
11309 | | int SendTls13KeyUpdate(WOLFSSL* ssl) |
11310 | 0 | { |
11311 | 0 | byte* input; |
11312 | 0 | byte* output; |
11313 | 0 | int ret; |
11314 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
11315 | 0 | int outputSz; |
11316 | 0 | word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11317 | |
|
11318 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND); |
11319 | 0 | WOLFSSL_ENTER("SendTls13KeyUpdate"); |
11320 | |
|
11321 | | #ifdef WOLFSSL_DTLS13 |
11322 | | if (ssl->options.dtls) |
11323 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
11324 | | #endif /* WOLFSSL_DTLS13 */ |
11325 | |
|
11326 | 0 | outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA; |
11327 | | /* Check buffers are big enough and grow if needed. */ |
11328 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11329 | 0 | return ret; |
11330 | | |
11331 | | /* get output buffer */ |
11332 | 0 | output = GetOutputBuffer(ssl); |
11333 | 0 | input = output + RECORD_HEADER_SZ; |
11334 | |
|
11335 | | #ifdef WOLFSSL_DTLS13 |
11336 | | if (ssl->options.dtls) |
11337 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11338 | | #endif /* WOLFSSL_DTLS13 */ |
11339 | |
|
11340 | 0 | AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl); |
11341 | | |
11342 | | /* If: |
11343 | | * 1. I haven't sent a KeyUpdate requesting a response and |
11344 | | * 2. This isn't responding to peer KeyUpdate requiring a response then, |
11345 | | * I want a response. |
11346 | | */ |
11347 | 0 | ssl->keys.updateResponseReq = output[i++] = |
11348 | 0 | !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond; |
11349 | | /* Sent response, no longer need to respond. */ |
11350 | 0 | ssl->keys.keyUpdateRespond = 0; |
11351 | |
|
11352 | | #ifdef WOLFSSL_DTLS13 |
11353 | | if (ssl->options.dtls) { |
11354 | | ret = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11355 | | OPAQUE8_LEN + Dtls13GetRlHeaderLength(ssl, 1) + |
11356 | | DTLS_HANDSHAKE_HEADER_SZ, |
11357 | | key_update, 0); |
11358 | | } |
11359 | | else |
11360 | | #endif /* WOLFSSL_DTLS13 */ |
11361 | 0 | { |
11362 | | /* This message is always encrypted. */ |
11363 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11364 | 0 | headerSz + OPAQUE8_LEN, handshake, 0, 0, 0); |
11365 | 0 | if (sendSz < 0) |
11366 | 0 | return BUILD_MSG_ERROR; |
11367 | | |
11368 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11369 | | if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate"); |
11370 | | if (ssl->toInfoOn) { |
11371 | | ret = AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz, |
11372 | | WRITE_PROTO, 0, ssl->heap); |
11373 | | if (ret != 0) |
11374 | | return ret; |
11375 | | } |
11376 | | #endif |
11377 | | |
11378 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11379 | |
|
11380 | 0 | ret = SendBuffered(ssl); |
11381 | | |
11382 | |
|
11383 | 0 | if (ret != 0 && ret != WC_NO_ERR_TRACE(WANT_WRITE)) |
11384 | 0 | return ret; |
11385 | 0 | } |
11386 | | |
11387 | | /* In DTLS we must wait for the ack before setting up the new keys */ |
11388 | 0 | if (!ssl->options.dtls) { |
11389 | | |
11390 | | /* Future traffic uses new encryption keys. */ |
11391 | 0 | if ((ret = DeriveTls13Keys( |
11392 | 0 | ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11393 | 0 | != 0) |
11394 | 0 | return ret; |
11395 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11396 | 0 | return ret; |
11397 | 0 | } |
11398 | | |
11399 | | |
11400 | 0 | WOLFSSL_LEAVE("SendTls13KeyUpdate", ret); |
11401 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND); |
11402 | |
|
11403 | 0 | return ret; |
11404 | 0 | } |
11405 | | |
11406 | | /* handle processing TLS v1.3 key_update (24) */ |
11407 | | /* Parse and handle a TLS v1.3 KeyUpdate message. |
11408 | | * |
11409 | | * ssl The SSL/TLS object. |
11410 | | * input The message buffer. |
11411 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11412 | | * On exit, the index of byte after the Finished message and padding. |
11413 | | * totalSz The length of the current handshake message. |
11414 | | * returns 0 on success and otherwise failure. |
11415 | | */ |
11416 | | static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11417 | | word32 totalSz) |
11418 | 0 | { |
11419 | 0 | int ret; |
11420 | 0 | word32 i = *inOutIdx; |
11421 | |
|
11422 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO); |
11423 | 0 | WOLFSSL_ENTER("DoTls13KeyUpdate"); |
11424 | | |
11425 | | /* check against totalSz */ |
11426 | 0 | if (OPAQUE8_LEN != totalSz) |
11427 | 0 | return BUFFER_E; |
11428 | | |
11429 | 0 | switch (input[i]) { |
11430 | 0 | case update_not_requested: |
11431 | | /* This message in response to any outstanding request. */ |
11432 | 0 | ssl->keys.keyUpdateRespond = 0; |
11433 | 0 | ssl->keys.updateResponseReq = 0; |
11434 | 0 | break; |
11435 | 0 | case update_requested: |
11436 | | /* New key update requiring a response. */ |
11437 | 0 | ssl->keys.keyUpdateRespond = 1; |
11438 | 0 | break; |
11439 | 0 | default: |
11440 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
11441 | 0 | return INVALID_PARAMETER; |
11442 | 0 | } |
11443 | | |
11444 | | /* Move index to byte after message. */ |
11445 | 0 | *inOutIdx += totalSz; |
11446 | | /* Always encrypted. */ |
11447 | 0 | *inOutIdx += ssl->keys.padSz; |
11448 | | |
11449 | | /* Future traffic uses new decryption keys. */ |
11450 | 0 | if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1)) |
11451 | 0 | != 0) { |
11452 | 0 | return ret; |
11453 | 0 | } |
11454 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11455 | 0 | return ret; |
11456 | | |
11457 | | #ifdef WOLFSSL_DTLS13 |
11458 | | if (ssl->options.dtls) { |
11459 | | w64Increment(&ssl->dtls13PeerEpoch); |
11460 | | |
11461 | | ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY); |
11462 | | if (ret != 0) |
11463 | | return ret; |
11464 | | } |
11465 | | #endif /* WOLFSSL_DTLS13 */ |
11466 | | |
11467 | 0 | if (ssl->keys.keyUpdateRespond) { |
11468 | |
|
11469 | | #ifdef WOLFSSL_DTLS13 |
11470 | | /* we already sent a keyUpdate (either in response to a previous |
11471 | | KeyUpdate or initiated by the application) and we are waiting for the |
11472 | | ack. We can't send a new KeyUpdate right away but to honor the RFC we |
11473 | | should send another KeyUpdate after the one in-flight is acked. We |
11474 | | don't do that as it looks redundant, it will make the code more |
11475 | | complex and I don't see a good use case for that. */ |
11476 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) { |
11477 | | ssl->keys.keyUpdateRespond = 0; |
11478 | | return 0; |
11479 | | } |
11480 | | #endif /* WOLFSSL_DTLS13 */ |
11481 | |
|
11482 | 0 | #ifndef WOLFSSL_RW_THREADED |
11483 | 0 | return SendTls13KeyUpdate(ssl); |
11484 | | #else |
11485 | | ssl->options.sendKeyUpdate = 1; |
11486 | | return 0; |
11487 | | #endif |
11488 | 0 | } |
11489 | | |
11490 | 0 | WOLFSSL_LEAVE("DoTls13KeyUpdate", ret); |
11491 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO); |
11492 | |
|
11493 | 0 | return 0; |
11494 | 0 | } |
11495 | | |
11496 | | #ifdef WOLFSSL_EARLY_DATA |
11497 | | #ifndef NO_WOLFSSL_CLIENT |
11498 | | /* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no |
11499 | | * more early application data. |
11500 | | * The encryption key now changes to the pre-calculated handshake key. |
11501 | | * |
11502 | | * ssl The SSL/TLS object. |
11503 | | * returns 0 on success and otherwise failure. |
11504 | | */ |
11505 | | static int SendTls13EndOfEarlyData(WOLFSSL* ssl) |
11506 | | { |
11507 | | byte* output; |
11508 | | int ret; |
11509 | | int sendSz; |
11510 | | word32 length; |
11511 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11512 | | |
11513 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11514 | | WOLFSSL_ENTER("SendTls13EndOfEarlyData"); |
11515 | | |
11516 | | length = 0; |
11517 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
11518 | | ssl->options.buildingMsg = 1; |
11519 | | |
11520 | | /* Check buffers are big enough and grow if needed. */ |
11521 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
11522 | | return ret; |
11523 | | |
11524 | | /* Get position in output buffer to write new message to. */ |
11525 | | output = GetOutputBuffer(ssl); |
11526 | | |
11527 | | /* Put the record and handshake headers on. */ |
11528 | | AddTls13Headers(output, length, end_of_early_data, ssl); |
11529 | | |
11530 | | /* This message is always encrypted. */ |
11531 | | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
11532 | | idx - RECORD_HEADER_SZ, handshake, 1, 0, 0); |
11533 | | if (sendSz < 0) |
11534 | | return sendSz; |
11535 | | |
11536 | | ssl->buffers.outputBuffer.length += sendSz; |
11537 | | |
11538 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11539 | | return ret; |
11540 | | |
11541 | | ssl->options.buildingMsg = 0; |
11542 | | if (!ssl->options.groupMessages) |
11543 | | ret = SendBuffered(ssl); |
11544 | | |
11545 | | ssl->earlyData = done_early_data; |
11546 | | |
11547 | | WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret); |
11548 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11549 | | |
11550 | | return ret; |
11551 | | } |
11552 | | #endif /* !NO_WOLFSSL_CLIENT */ |
11553 | | |
11554 | | #ifndef NO_WOLFSSL_SERVER |
11555 | | /* handle processing of TLS 1.3 end_of_early_data (5) */ |
11556 | | /* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be |
11557 | | * no more early application data. |
11558 | | * The decryption key now changes to the pre-calculated handshake key. |
11559 | | * |
11560 | | * ssl The SSL/TLS object. |
11561 | | * returns 0 on success and otherwise failure. |
11562 | | */ |
11563 | | static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input, |
11564 | | word32* inOutIdx, word32 size) |
11565 | | { |
11566 | | int ret; |
11567 | | word32 begin = *inOutIdx; |
11568 | | |
11569 | | (void)input; |
11570 | | |
11571 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO); |
11572 | | WOLFSSL_ENTER("DoTls13EndOfEarlyData"); |
11573 | | |
11574 | | if ((*inOutIdx - begin) != size) |
11575 | | return BUFFER_ERROR; |
11576 | | |
11577 | | if (ssl->earlyData == no_early_data) { |
11578 | | WOLFSSL_MSG("EndOfEarlyData received unexpectedly"); |
11579 | | SendAlert(ssl, alert_fatal, unexpected_message); |
11580 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
11581 | | return OUT_OF_ORDER_E; |
11582 | | } |
11583 | | |
11584 | | ssl->earlyData = done_early_data; |
11585 | | |
11586 | | /* Always encrypted. */ |
11587 | | *inOutIdx += ssl->keys.padSz; |
11588 | | |
11589 | | ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY); |
11590 | | |
11591 | | WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret); |
11592 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO); |
11593 | | |
11594 | | return ret; |
11595 | | } |
11596 | | #endif /* !NO_WOLFSSL_SERVER */ |
11597 | | #endif /* WOLFSSL_EARLY_DATA */ |
11598 | | |
11599 | | #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11600 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
11601 | | int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce, |
11602 | | byte len) |
11603 | | { |
11604 | | if (session->ticketNonce.data |
11605 | | != session->ticketNonce.dataStatic) { |
11606 | | XFREE(session->ticketNonce.data, session->heap, |
11607 | | DYNAMIC_TYPE_SESSION_TICK); |
11608 | | session->ticketNonce.data = session->ticketNonce.dataStatic; |
11609 | | session->ticketNonce.len = 0; |
11610 | | } |
11611 | | |
11612 | | if (len > MAX_TICKET_NONCE_STATIC_SZ) { |
11613 | | WOLFSSL_MSG("Using dynamic nonce buffer"); |
11614 | | session->ticketNonce.data = (byte*)XMALLOC(len, |
11615 | | session->heap, DYNAMIC_TYPE_SESSION_TICK); |
11616 | | if (session->ticketNonce.data == NULL) |
11617 | | return MEMORY_ERROR; |
11618 | | } |
11619 | | XMEMCPY(session->ticketNonce.data, nonce, len); |
11620 | | session->ticketNonce.len = len; |
11621 | | return 0; |
11622 | | } |
11623 | | #endif |
11624 | | #ifndef NO_WOLFSSL_CLIENT |
11625 | | /* Handle a New Session Ticket handshake message. |
11626 | | * Message contains the information required to perform resumption. |
11627 | | * |
11628 | | * ssl The SSL/TLS object. |
11629 | | * input The message buffer. |
11630 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11631 | | * On exit, the index of byte after the Finished message and padding. |
11632 | | * size The length of the current handshake message. |
11633 | | * returns 0 on success, otherwise failure. |
11634 | | */ |
11635 | | static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input, |
11636 | | word32* inOutIdx, word32 size) |
11637 | 0 | { |
11638 | | #ifdef HAVE_SESSION_TICKET |
11639 | | int ret; |
11640 | | word32 begin = *inOutIdx; |
11641 | | word32 lifetime; |
11642 | | word32 ageAdd; |
11643 | | word16 length; |
11644 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
11645 | | word32 now; |
11646 | | #else |
11647 | | sword64 now; |
11648 | | #endif |
11649 | | const byte* nonce; |
11650 | | byte nonceLength; |
11651 | | |
11652 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO); |
11653 | | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
11654 | | |
11655 | | /* Lifetime hint. */ |
11656 | | if ((*inOutIdx - begin) + SESSION_HINT_SZ > size) |
11657 | | return BUFFER_ERROR; |
11658 | | ato32(input + *inOutIdx, &lifetime); |
11659 | | *inOutIdx += SESSION_HINT_SZ; |
11660 | | if (lifetime > MAX_LIFETIME) { |
11661 | | WOLFSSL_ERROR_VERBOSE(SERVER_HINT_ERROR); |
11662 | | return SERVER_HINT_ERROR; |
11663 | | } |
11664 | | |
11665 | | /* Age add. */ |
11666 | | if ((*inOutIdx - begin) + SESSION_ADD_SZ > size) |
11667 | | return BUFFER_ERROR; |
11668 | | ato32(input + *inOutIdx, &ageAdd); |
11669 | | *inOutIdx += SESSION_ADD_SZ; |
11670 | | |
11671 | | /* Ticket nonce. */ |
11672 | | if ((*inOutIdx - begin) + 1 > size) |
11673 | | return BUFFER_ERROR; |
11674 | | nonceLength = input[*inOutIdx]; |
11675 | | #if !defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11676 | | (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) |
11677 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
11678 | | WOLFSSL_MSG("Nonce length not supported"); |
11679 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
11680 | | return INVALID_PARAMETER; |
11681 | | } |
11682 | | #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */ |
11683 | | *inOutIdx += 1; |
11684 | | if ((*inOutIdx - begin) + nonceLength > size) |
11685 | | return BUFFER_ERROR; |
11686 | | nonce = input + *inOutIdx; |
11687 | | *inOutIdx += nonceLength; |
11688 | | |
11689 | | /* Ticket length. */ |
11690 | | if ((*inOutIdx - begin) + LENGTH_SZ > size) |
11691 | | return BUFFER_ERROR; |
11692 | | ato16(input + *inOutIdx, &length); |
11693 | | *inOutIdx += LENGTH_SZ; |
11694 | | if ((*inOutIdx - begin) + length > size) |
11695 | | return BUFFER_ERROR; |
11696 | | |
11697 | | if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0) |
11698 | | return ret; |
11699 | | *inOutIdx += length; |
11700 | | |
11701 | | now = TimeNowInMilliseconds(); |
11702 | | if (now == 0) |
11703 | | return GETTIME_ERROR; |
11704 | | /* Copy in ticket data (server identity). */ |
11705 | | ssl->timeout = lifetime; |
11706 | | ssl->session->timeout = lifetime; |
11707 | | ssl->session->cipherSuite0 = ssl->options.cipherSuite0; |
11708 | | ssl->session->cipherSuite = ssl->options.cipherSuite; |
11709 | | ssl->session->ticketSeen = now; |
11710 | | ssl->session->ticketAdd = ageAdd; |
11711 | | #ifdef WOLFSSL_EARLY_DATA |
11712 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
11713 | | #endif |
11714 | | |
11715 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11716 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
11717 | | ret = SessionTicketNoncePopulate(ssl->session, nonce, nonceLength); |
11718 | | if (ret != 0) |
11719 | | return ret; |
11720 | | #else |
11721 | | ssl->session->ticketNonce.len = nonceLength; |
11722 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
11723 | | ret = BUFFER_ERROR; |
11724 | | return ret; |
11725 | | } |
11726 | | if (nonceLength > 0) |
11727 | | XMEMCPY(ssl->session->ticketNonce.data, nonce, nonceLength); |
11728 | | #endif /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && FIPS_VERSION_GE(5,3) */ |
11729 | | |
11730 | | ssl->session->namedGroup = ssl->namedGroup; |
11731 | | |
11732 | | if ((*inOutIdx - begin) + EXTS_SZ > size) |
11733 | | return BUFFER_ERROR; |
11734 | | ato16(input + *inOutIdx, &length); |
11735 | | *inOutIdx += EXTS_SZ; |
11736 | | if ((*inOutIdx - begin) + length != size) |
11737 | | return BUFFER_ERROR; |
11738 | | #ifdef WOLFSSL_EARLY_DATA |
11739 | | ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket, |
11740 | | NULL); |
11741 | | if (ret != 0) |
11742 | | return ret; |
11743 | | #endif |
11744 | | *inOutIdx += length; |
11745 | | |
11746 | | SetupSession(ssl); |
11747 | | #ifndef NO_SESSION_CACHE |
11748 | | AddSession(ssl); |
11749 | | #endif |
11750 | | |
11751 | | /* Always encrypted. */ |
11752 | | *inOutIdx += ssl->keys.padSz; |
11753 | | |
11754 | | ssl->expect_session_ticket = 0; |
11755 | | #else |
11756 | 0 | (void)ssl; |
11757 | 0 | (void)input; |
11758 | |
|
11759 | 0 | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
11760 | |
|
11761 | 0 | *inOutIdx += size + ssl->keys.padSz; |
11762 | 0 | #endif /* HAVE_SESSION_TICKET */ |
11763 | |
|
11764 | 0 | WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0); |
11765 | 0 | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO); |
11766 | |
|
11767 | 0 | return 0; |
11768 | 0 | } |
11769 | | #endif /* NO_WOLFSSL_CLIENT */ |
11770 | | |
11771 | | #ifndef NO_WOLFSSL_SERVER |
11772 | | #ifdef HAVE_SESSION_TICKET |
11773 | | |
11774 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
11775 | | /* Offset of the MAC size in the finished message. */ |
11776 | | #define FINISHED_MSG_SIZE_OFFSET 3 |
11777 | | |
11778 | | /* Calculate the resumption secret which includes the unseen client finished |
11779 | | * message. |
11780 | | * |
11781 | | * ssl The SSL/TLS object. |
11782 | | * returns 0 on success, otherwise failure. |
11783 | | */ |
11784 | | static int ExpectedResumptionSecret(WOLFSSL* ssl) |
11785 | | { |
11786 | | int ret; |
11787 | | word32 finishedSz = 0; |
11788 | | byte mac[WC_MAX_DIGEST_SIZE]; |
11789 | | Digest digest; |
11790 | | static byte header[] = { 0x14, 0x00, 0x00, 0x00 }; |
11791 | | |
11792 | | /* Copy the running hash so we can restore it after. */ |
11793 | | switch (ssl->specs.mac_algorithm) { |
11794 | | #ifndef NO_SHA256 |
11795 | | case sha256_mac: |
11796 | | ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256); |
11797 | | if (ret != 0) |
11798 | | return ret; |
11799 | | break; |
11800 | | #endif |
11801 | | #ifdef WOLFSSL_SHA384 |
11802 | | case sha384_mac: |
11803 | | ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384); |
11804 | | if (ret != 0) |
11805 | | return ret; |
11806 | | break; |
11807 | | #endif |
11808 | | #ifdef WOLFSSL_TLS13_SHA512 |
11809 | | case sha512_mac: |
11810 | | ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512); |
11811 | | if (ret != 0) |
11812 | | return ret; |
11813 | | break; |
11814 | | #endif |
11815 | | #ifdef WOLFSSL_SM3 |
11816 | | case sm3_mac: |
11817 | | ret = wc_Sm3Copy(&ssl->hsHashes->hashSm3, &digest.sm3); |
11818 | | if (ret != 0) |
11819 | | return ret; |
11820 | | break; |
11821 | | #endif |
11822 | | } |
11823 | | |
11824 | | /* Generate the Client's Finished message and hash it. */ |
11825 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac, |
11826 | | &finishedSz); |
11827 | | if (ret != 0) |
11828 | | return ret; |
11829 | | header[FINISHED_MSG_SIZE_OFFSET] = finishedSz; |
11830 | | #ifdef WOLFSSL_EARLY_DATA |
11831 | | if (ssl->earlyData != no_early_data) { |
11832 | | static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 }; |
11833 | | ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData)); |
11834 | | if (ret != 0) |
11835 | | return ret; |
11836 | | } |
11837 | | #endif |
11838 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
11839 | | return ret; |
11840 | | if ((ret = HashRaw(ssl, mac, finishedSz)) != 0) |
11841 | | return ret; |
11842 | | |
11843 | | if ((ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret)) != 0) |
11844 | | return ret; |
11845 | | |
11846 | | /* Restore the hash inline with currently seen messages. */ |
11847 | | switch (ssl->specs.mac_algorithm) { |
11848 | | #ifndef NO_SHA256 |
11849 | | case sha256_mac: |
11850 | | wc_Sha256Free(&ssl->hsHashes->hashSha256); |
11851 | | ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256); |
11852 | | wc_Sha256Free(&digest.sha256); |
11853 | | if (ret != 0) |
11854 | | return ret; |
11855 | | break; |
11856 | | #endif |
11857 | | #ifdef WOLFSSL_SHA384 |
11858 | | case sha384_mac: |
11859 | | wc_Sha384Free(&ssl->hsHashes->hashSha384); |
11860 | | ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384); |
11861 | | wc_Sha384Free(&digest.sha384); |
11862 | | if (ret != 0) |
11863 | | return ret; |
11864 | | break; |
11865 | | #endif |
11866 | | #ifdef WOLFSSL_TLS13_SHA512 |
11867 | | case sha512_mac: |
11868 | | wc_Sha512Free(&ssl->hsHashes->hashSha512); |
11869 | | ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512); |
11870 | | wc_Sha512Free(&digest.sha512); |
11871 | | if (ret != 0) |
11872 | | return ret; |
11873 | | break; |
11874 | | #endif |
11875 | | #ifdef WOLFSSL_SM3 |
11876 | | case sm3_mac: |
11877 | | wc_Sm3Free(&ssl->hsHashes->hashSm3); |
11878 | | ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3); |
11879 | | wc_Sm3Free(&digest.sm3); |
11880 | | if (ret != 0) |
11881 | | return ret; |
11882 | | break; |
11883 | | #endif |
11884 | | } |
11885 | | |
11886 | | return ret; |
11887 | | } |
11888 | | #endif |
11889 | | |
11890 | | /* Send New Session Ticket handshake message. |
11891 | | * Message contains the information required to perform resumption. |
11892 | | * |
11893 | | * ssl The SSL/TLS object. |
11894 | | * returns 0 on success, otherwise failure. |
11895 | | */ |
11896 | | static int SendTls13NewSessionTicket(WOLFSSL* ssl) |
11897 | | { |
11898 | | byte* output; |
11899 | | int ret; |
11900 | | word32 length; |
11901 | | int sendSz; |
11902 | | word16 extSz; |
11903 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11904 | | |
11905 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND); |
11906 | | WOLFSSL_ENTER("SendTls13NewSessionTicket"); |
11907 | | |
11908 | | #ifdef WOLFSSL_DTLS13 |
11909 | | if (ssl->options.dtls) |
11910 | | idx = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
11911 | | #endif /* WOLFSSL_DTLS13 */ |
11912 | | |
11913 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
11914 | | if (!ssl->msgsReceived.got_finished) { |
11915 | | if ((ret = ExpectedResumptionSecret(ssl)) != 0) |
11916 | | return ret; |
11917 | | } |
11918 | | #endif |
11919 | | |
11920 | | /* Start ticket nonce at 0 and go up to 255. */ |
11921 | | if (ssl->session->ticketNonce.len == 0) { |
11922 | | ssl->session->ticketNonce.len = DEF_TICKET_NONCE_SZ; |
11923 | | ssl->session->ticketNonce.data[0] = 0; |
11924 | | } |
11925 | | else |
11926 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11927 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
11928 | | #endif |
11929 | | { |
11930 | | ssl->session->ticketNonce.data[0]++; |
11931 | | } |
11932 | | |
11933 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
11934 | | /* In this case we only send the ID as the ticket. Let's generate a new |
11935 | | * ID for the new ticket so that we don't overwrite any old ones */ |
11936 | | ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID, |
11937 | | ID_LEN); |
11938 | | if (ret != 0) |
11939 | | return ret; |
11940 | | ssl->session->haveAltSessionID = 1; |
11941 | | } |
11942 | | |
11943 | | if (!ssl->options.noTicketTls13) { |
11944 | | if ((ret = SetupTicket(ssl)) != 0) |
11945 | | return ret; |
11946 | | /* No need to create the ticket if we only send the ID */ |
11947 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == 0) { |
11948 | | if ((ret = CreateTicket(ssl)) != 0) |
11949 | | return ret; |
11950 | | } |
11951 | | } |
11952 | | |
11953 | | #ifdef WOLFSSL_EARLY_DATA |
11954 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
11955 | | if (ssl->session->maxEarlyDataSz > 0) |
11956 | | TLSX_EarlyData_Use(ssl, ssl->session->maxEarlyDataSz, 1); |
11957 | | extSz = 0; |
11958 | | ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz); |
11959 | | if (ret != 0) |
11960 | | return ret; |
11961 | | #else |
11962 | | extSz = EXTS_SZ; |
11963 | | #endif |
11964 | | /* Lifetime | Age Add | Ticket session ID | Extensions */ |
11965 | | length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ; |
11966 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) |
11967 | | length += ID_LEN + extSz; |
11968 | | else |
11969 | | length += ssl->session->ticketLen + extSz; |
11970 | | /* Nonce */ |
11971 | | length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ; |
11972 | | |
11973 | | sendSz = (word16)(idx + length + MAX_MSG_EXTRA); |
11974 | | |
11975 | | /* Check buffers are big enough and grow if needed. */ |
11976 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
11977 | | return ret; |
11978 | | |
11979 | | /* Get position in output buffer to write new message to. */ |
11980 | | output = GetOutputBuffer(ssl); |
11981 | | |
11982 | | /* Put the record and handshake headers on. */ |
11983 | | AddTls13Headers(output, length, session_ticket, ssl); |
11984 | | |
11985 | | /* Lifetime hint */ |
11986 | | c32toa(ssl->ctx->ticketHint, output + idx); |
11987 | | idx += SESSION_HINT_SZ; |
11988 | | /* Age add - obfuscator */ |
11989 | | c32toa(ssl->session->ticketAdd, output + idx); |
11990 | | idx += SESSION_ADD_SZ; |
11991 | | |
11992 | | output[idx++] = ssl->session->ticketNonce.len; |
11993 | | output[idx++] = ssl->session->ticketNonce.data[0]; |
11994 | | |
11995 | | /* length */ |
11996 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
11997 | | c16toa(ID_LEN, output + idx); |
11998 | | } |
11999 | | else { |
12000 | | c16toa(ssl->session->ticketLen, output + idx); |
12001 | | } |
12002 | | |
12003 | | idx += LENGTH_SZ; |
12004 | | /* ticket */ |
12005 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12006 | | if (ssl->session->haveAltSessionID) |
12007 | | XMEMCPY(output + idx, ssl->session->altSessionID, ID_LEN); |
12008 | | else |
12009 | | return BAD_FUNC_ARG; /* Should not happen */ |
12010 | | idx += ID_LEN; |
12011 | | } |
12012 | | else { |
12013 | | XMEMCPY(output + idx, ssl->session->ticket, ssl->session->ticketLen); |
12014 | | idx += ssl->session->ticketLen; |
12015 | | } |
12016 | | |
12017 | | #ifdef WOLFSSL_EARLY_DATA |
12018 | | extSz = 0; |
12019 | | ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz); |
12020 | | if (ret != 0) |
12021 | | return ret; |
12022 | | idx += extSz; |
12023 | | #else |
12024 | | /* No extension support - empty extensions. */ |
12025 | | c16toa(0, output + idx); |
12026 | | idx += EXTS_SZ; |
12027 | | #endif |
12028 | | |
12029 | | if (idx > WOLFSSL_MAX_16BIT) { |
12030 | | return BAD_LENGTH_E; |
12031 | | } |
12032 | | |
12033 | | ssl->options.haveSessionId = 1; |
12034 | | |
12035 | | SetupSession(ssl); |
12036 | | /* Only add to cache when support built in and when the ticket contains |
12037 | | * an ID. Otherwise we have no way to actually retrieve the ticket from the |
12038 | | * cache. */ |
12039 | | #if !defined(NO_SESSION_CACHE) && defined(WOLFSSL_TICKET_HAVE_ID) |
12040 | | AddSession(ssl); |
12041 | | #endif |
12042 | | |
12043 | | #ifdef WOLFSSL_DTLS13 |
12044 | | if (ssl->options.dtls) |
12045 | | return Dtls13HandshakeSend(ssl, output, (word16)sendSz, |
12046 | | (word16)idx, session_ticket, 0); |
12047 | | #endif /* WOLFSSL_DTLS13 */ |
12048 | | |
12049 | | /* This message is always encrypted. */ |
12050 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
12051 | | output + RECORD_HEADER_SZ, |
12052 | | (word16)idx - RECORD_HEADER_SZ, |
12053 | | handshake, 0, 0, 0); |
12054 | | if (sendSz < 0) |
12055 | | return sendSz; |
12056 | | |
12057 | | ssl->buffers.outputBuffer.length += sendSz; |
12058 | | |
12059 | | /* Always send as this is either directly after server's Finished or only |
12060 | | * message after client's Finished. |
12061 | | */ |
12062 | | ret = SendBuffered(ssl); |
12063 | | |
12064 | | WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0); |
12065 | | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12066 | | |
12067 | | return ret; |
12068 | | } |
12069 | | #endif /* HAVE_SESSION_TICKET */ |
12070 | | #endif /* NO_WOLFSSL_SERVER */ |
12071 | | |
12072 | | /* Make sure no duplicates, no fast forward, or other problems |
12073 | | * |
12074 | | * ssl The SSL/TLS object. |
12075 | | * type Type of handshake message received. |
12076 | | * returns 0 on success, otherwise failure. |
12077 | | */ |
12078 | | static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type) |
12079 | 0 | { |
12080 | | /* verify not a duplicate, mark received, check state */ |
12081 | 0 | switch (type) { |
12082 | | |
12083 | 0 | #ifndef NO_WOLFSSL_SERVER |
12084 | 0 | case client_hello: |
12085 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12086 | | /* Only valid when received on SERVER side. */ |
12087 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12088 | 0 | WOLFSSL_MSG("ClientHello received by client"); |
12089 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12090 | 0 | return SIDE_ERROR; |
12091 | 0 | } |
12092 | 0 | #endif |
12093 | | /* Check state. */ |
12094 | 0 | if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) { |
12095 | 0 | WOLFSSL_MSG("ClientHello received out of order"); |
12096 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12097 | 0 | return OUT_OF_ORDER_E; |
12098 | 0 | } |
12099 | | /* Check previously seen. */ |
12100 | | /* Initial and after HelloRetryRequest - no more than 2. */ |
12101 | 0 | if (ssl->msgsReceived.got_client_hello == 2) { |
12102 | 0 | WOLFSSL_MSG("Too many ClientHello received"); |
12103 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12104 | 0 | return DUPLICATE_MSG_E; |
12105 | 0 | } |
12106 | | /* Second only after HelloRetryRequest seen. */ |
12107 | 0 | if (ssl->msgsReceived.got_client_hello == 1 && |
12108 | 0 | ssl->options.serverState != |
12109 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
12110 | 0 | WOLFSSL_MSG("Duplicate ClientHello received"); |
12111 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12112 | 0 | return DUPLICATE_MSG_E; |
12113 | 0 | } |
12114 | 0 | ssl->msgsReceived.got_client_hello++; |
12115 | |
|
12116 | 0 | break; |
12117 | 0 | #endif |
12118 | | |
12119 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12120 | 0 | case server_hello: |
12121 | 0 | #ifndef NO_WOLFSSL_SERVER |
12122 | | /* Only valid when received on CLIENT side. */ |
12123 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12124 | 0 | WOLFSSL_MSG("ServerHello received by server"); |
12125 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12126 | 0 | return SIDE_ERROR; |
12127 | 0 | } |
12128 | 0 | #endif |
12129 | | /* Check state. */ |
12130 | 0 | if (ssl->options.serverState >= SERVER_HELLO_COMPLETE) { |
12131 | 0 | WOLFSSL_MSG("ServerHello received out of order"); |
12132 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12133 | 0 | return OUT_OF_ORDER_E; |
12134 | 0 | } |
12135 | | /* Check previously seen. */ |
12136 | | /* Only once after ClientHello. |
12137 | | * HelloRetryRequest has ServerHello type but count fixed up later |
12138 | | * - see DoTls13ServerHello(). |
12139 | | */ |
12140 | 0 | if (ssl->msgsReceived.got_server_hello) { |
12141 | 0 | WOLFSSL_MSG("Duplicate ServerHello received"); |
12142 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12143 | 0 | return DUPLICATE_MSG_E; |
12144 | 0 | } |
12145 | 0 | ssl->msgsReceived.got_server_hello = 1; |
12146 | |
|
12147 | 0 | break; |
12148 | 0 | #endif |
12149 | | |
12150 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12151 | 0 | case session_ticket: |
12152 | 0 | #ifndef NO_WOLFSSL_SERVER |
12153 | | /* Only valid when received on CLIENT side. */ |
12154 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12155 | 0 | WOLFSSL_MSG("NewSessionTicket received by server"); |
12156 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12157 | 0 | return SIDE_ERROR; |
12158 | 0 | } |
12159 | 0 | #endif |
12160 | | /* Check state. */ |
12161 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12162 | | /* Only allowed after server's Finished message. */ |
12163 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12164 | | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12165 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12166 | | return OUT_OF_ORDER_E; |
12167 | | } |
12168 | | #else |
12169 | | /* Only allowed after client's Finished message. */ |
12170 | 0 | if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
12171 | 0 | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12172 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12173 | 0 | return OUT_OF_ORDER_E; |
12174 | 0 | } |
12175 | 0 | #endif |
12176 | | /* Many SessionTickets can be sent. */ |
12177 | 0 | ssl->msgsReceived.got_session_ticket = 1; |
12178 | |
|
12179 | 0 | break; |
12180 | 0 | #endif |
12181 | | |
12182 | 0 | #ifndef NO_WOLFSSL_SERVER |
12183 | | #ifdef WOLFSSL_EARLY_DATA |
12184 | | case end_of_early_data: |
12185 | | #ifndef NO_WOLFSSL_CLIENT |
12186 | | /* Only valid when received on SERVER side. */ |
12187 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12188 | | WOLFSSL_MSG("EndOfEarlyData received by client"); |
12189 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12190 | | return SIDE_ERROR; |
12191 | | } |
12192 | | #endif |
12193 | | /* Check state. */ |
12194 | | /* Only after server's Finished and before client's Finished. */ |
12195 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12196 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12197 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12198 | | return OUT_OF_ORDER_E; |
12199 | | } |
12200 | | if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) { |
12201 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12202 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12203 | | return OUT_OF_ORDER_E; |
12204 | | } |
12205 | | /* Check previously seen. */ |
12206 | | if (ssl->msgsReceived.got_end_of_early_data) { |
12207 | | WOLFSSL_MSG("Too many EndOfEarlyData received"); |
12208 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12209 | | return DUPLICATE_MSG_E; |
12210 | | } |
12211 | | ssl->msgsReceived.got_end_of_early_data = 1; |
12212 | | |
12213 | | break; |
12214 | | #endif |
12215 | 0 | #endif |
12216 | | |
12217 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12218 | 0 | case encrypted_extensions: |
12219 | 0 | #ifndef NO_WOLFSSL_SERVER |
12220 | | /* Only valid when received on CLIENT side. */ |
12221 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12222 | 0 | WOLFSSL_MSG("EncryptedExtensions received by server"); |
12223 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12224 | 0 | return SIDE_ERROR; |
12225 | 0 | } |
12226 | 0 | #endif |
12227 | | /* Check state. */ |
12228 | | /* Must be received directly after ServerHello. |
12229 | | * DoTls13EncryptedExtensions() changes state to: |
12230 | | * SERVER_ENCRYPTED_EXTENSIONS_COMPLETE. |
12231 | | */ |
12232 | 0 | if (ssl->options.serverState != SERVER_HELLO_COMPLETE) { |
12233 | 0 | WOLFSSL_MSG("EncryptedExtensions 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 | 0 | if (ssl->msgsReceived.got_encrypted_extensions) { |
12239 | 0 | WOLFSSL_MSG("Duplicate EncryptedExtensions received"); |
12240 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12241 | 0 | return DUPLICATE_MSG_E; |
12242 | 0 | } |
12243 | 0 | ssl->msgsReceived.got_encrypted_extensions = 1; |
12244 | |
|
12245 | 0 | break; |
12246 | 0 | #endif |
12247 | | |
12248 | 0 | case certificate: |
12249 | | /* Valid on both sides. */ |
12250 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12251 | | /* Check state. */ |
12252 | | /* On client, seen after EncryptedExtension and CertificateRequest |
12253 | | * (if sent) and before CertificateVerify and Finished. |
12254 | | * DoTls13Certificate() sets serverState to SERVER_CERT_COMPLETE. |
12255 | | */ |
12256 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12257 | 0 | ssl->options.serverState != |
12258 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12259 | 0 | WOLFSSL_MSG("Certificate received out of order - Client"); |
12260 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12261 | 0 | return OUT_OF_ORDER_E; |
12262 | 0 | } |
12263 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12264 | | /* Server's authenticating with PSK must not send this. */ |
12265 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12266 | | ssl->options.serverState == SERVER_CERT_COMPLETE && |
12267 | | ssl->options.pskNegotiated) { |
12268 | | WOLFSSL_MSG("Certificate received while using PSK"); |
12269 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12270 | | return SANITY_MSG_E; |
12271 | | } |
12272 | | #endif |
12273 | 0 | #endif |
12274 | 0 | #ifndef NO_WOLFSSL_SERVER |
12275 | | /* Check state. */ |
12276 | | /* On Server, valid after ClientHello received and ServerFinished |
12277 | | * sent. */ |
12278 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
12279 | 0 | ssl->options.clientState != CLIENT_HELLO_COMPLETE && |
12280 | 0 | ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12281 | 0 | WOLFSSL_MSG("Certificate received out of order - Server"); |
12282 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12283 | 0 | return OUT_OF_ORDER_E; |
12284 | 0 | } |
12285 | 0 | #endif |
12286 | | /* Check previously seen. */ |
12287 | 0 | if (ssl->msgsReceived.got_certificate) { |
12288 | 0 | WOLFSSL_MSG("Duplicate Certificate received"); |
12289 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12290 | 0 | return DUPLICATE_MSG_E; |
12291 | 0 | } |
12292 | 0 | ssl->msgsReceived.got_certificate = 1; |
12293 | |
|
12294 | 0 | break; |
12295 | | |
12296 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12297 | 0 | case certificate_request: |
12298 | 0 | #ifndef NO_WOLFSSL_SERVER |
12299 | | /* Only valid when received on CLIENT side. */ |
12300 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12301 | 0 | WOLFSSL_MSG("CertificateRequest received by server"); |
12302 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12303 | 0 | return SIDE_ERROR; |
12304 | 0 | } |
12305 | 0 | #endif |
12306 | | /* Check state. */ |
12307 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12308 | | /* Only valid when sent after EncryptedExtensions and before |
12309 | | * Certificate. */ |
12310 | 0 | if (ssl->options.serverState != |
12311 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12312 | 0 | WOLFSSL_MSG("CertificateRequest received out of order"); |
12313 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12314 | 0 | return OUT_OF_ORDER_E; |
12315 | 0 | } |
12316 | | #else |
12317 | | /* Valid when sent after EncryptedExtensions and before Certificate |
12318 | | * and after both client and server have sent Finished (Post |
12319 | | * Handshake Authentication). */ |
12320 | | if (ssl->options.serverState != |
12321 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE && |
12322 | | (ssl->options.serverState < SERVER_FINISHED_COMPLETE || |
12323 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) { |
12324 | | WOLFSSL_MSG("CertificateRequest received out of order"); |
12325 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12326 | | return OUT_OF_ORDER_E; |
12327 | | } |
12328 | | #endif |
12329 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12330 | | /* Server's authenticating with PSK must not send this. */ |
12331 | | if (ssl->options.pskNegotiated) { |
12332 | | WOLFSSL_MSG("CertificateRequest received while using PSK"); |
12333 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12334 | | return SANITY_MSG_E; |
12335 | | } |
12336 | | #endif |
12337 | | /* Check previously seen. */ |
12338 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12339 | | /* Only once during handshake. */ |
12340 | 0 | if (ssl->msgsReceived.got_certificate_request) { |
12341 | 0 | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12342 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12343 | 0 | return DUPLICATE_MSG_E; |
12344 | 0 | } |
12345 | | #else |
12346 | | /* Only once during handshake. */ |
12347 | | if (ssl->msgsReceived.got_certificate_request && |
12348 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE) { |
12349 | | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12350 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12351 | | return DUPLICATE_MSG_E; |
12352 | | } |
12353 | | #endif |
12354 | 0 | ssl->msgsReceived.got_certificate_request = 1; |
12355 | |
|
12356 | 0 | break; |
12357 | 0 | #endif |
12358 | | |
12359 | 0 | case certificate_verify: |
12360 | | /* Valid on both sides. */ |
12361 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12362 | | /* Check state on client. |
12363 | | * Valid only directly after a Certificate message. */ |
12364 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12365 | 0 | if (ssl->options.serverState != SERVER_CERT_COMPLETE) { |
12366 | 0 | WOLFSSL_MSG("No Cert before CertVerify"); |
12367 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12368 | 0 | return OUT_OF_ORDER_E; |
12369 | 0 | } |
12370 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12371 | | /* Server's authenticating with PSK must not send this. */ |
12372 | | if (ssl->options.pskNegotiated) { |
12373 | | WOLFSSL_MSG("CertificateVerify received while using PSK"); |
12374 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12375 | | return SANITY_MSG_E; |
12376 | | } |
12377 | | #endif |
12378 | 0 | } |
12379 | 0 | #endif |
12380 | 0 | #ifndef NO_WOLFSSL_SERVER |
12381 | | /* Check state on server. */ |
12382 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12383 | | /* Server must have sent Finished message. */ |
12384 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12385 | 0 | WOLFSSL_MSG("CertificateVerify received out of order"); |
12386 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12387 | 0 | return OUT_OF_ORDER_E; |
12388 | 0 | } |
12389 | | /* Valid only directly after a Certificate message. */ |
12390 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12391 | 0 | WOLFSSL_MSG("CertificateVerify before ClientHello done"); |
12392 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12393 | 0 | return OUT_OF_ORDER_E; |
12394 | 0 | } |
12395 | 0 | if (!ssl->msgsReceived.got_certificate) { |
12396 | 0 | WOLFSSL_MSG("No Cert before CertificateVerify"); |
12397 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12398 | 0 | return OUT_OF_ORDER_E; |
12399 | 0 | } |
12400 | 0 | } |
12401 | 0 | #endif |
12402 | | /* Check previously seen. */ |
12403 | 0 | if (ssl->msgsReceived.got_certificate_verify) { |
12404 | 0 | WOLFSSL_MSG("Duplicate CertificateVerify received"); |
12405 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12406 | 0 | return DUPLICATE_MSG_E; |
12407 | 0 | } |
12408 | 0 | ssl->msgsReceived.got_certificate_verify = 1; |
12409 | |
|
12410 | 0 | break; |
12411 | | |
12412 | 0 | case finished: |
12413 | | /* Valid on both sides. */ |
12414 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12415 | | /* Check state on client. */ |
12416 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12417 | | /* After sending ClientHello */ |
12418 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12419 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12420 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12421 | 0 | return OUT_OF_ORDER_E; |
12422 | 0 | } |
12423 | | /* Must have seen certificate and verify from server except when |
12424 | | * using PSK. */ |
12425 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12426 | | if (ssl->options.pskNegotiated) { |
12427 | | if (ssl->options.serverState != |
12428 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12429 | | WOLFSSL_MSG("Finished received out of order - PSK"); |
12430 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12431 | | return OUT_OF_ORDER_E; |
12432 | | } |
12433 | | } |
12434 | | else |
12435 | | #endif |
12436 | 0 | if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) { |
12437 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12438 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12439 | 0 | return OUT_OF_ORDER_E; |
12440 | 0 | } |
12441 | 0 | } |
12442 | 0 | #endif |
12443 | 0 | #ifndef NO_WOLFSSL_SERVER |
12444 | | /* Check state on server. */ |
12445 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12446 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12447 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12448 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12449 | 0 | return OUT_OF_ORDER_E; |
12450 | 0 | } |
12451 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12452 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12453 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12454 | 0 | return OUT_OF_ORDER_E; |
12455 | 0 | } |
12456 | | #ifdef WOLFSSL_EARLY_DATA |
12457 | | if (ssl->earlyData == process_early_data && |
12458 | | /* early data may be lost when using DTLS */ |
12459 | | !ssl->options.dtls |
12460 | | /* QUIC does not use EndOfEarlyData records */ |
12461 | | && !WOLFSSL_IS_QUIC(ssl)) { |
12462 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12463 | | return OUT_OF_ORDER_E; |
12464 | | } |
12465 | | #endif |
12466 | 0 | } |
12467 | 0 | #endif |
12468 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12469 | | if (!ssl->options.pskNegotiated) |
12470 | | #endif |
12471 | 0 | { |
12472 | | /* Must have received a Certificate message from client if |
12473 | | * verifying the peer. Empty certificate message indicates |
12474 | | * no certificate available. |
12475 | | */ |
12476 | 0 | if (ssl->options.verifyPeer && |
12477 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
12478 | | !ssl->options.verifyPostHandshake && |
12479 | | #endif |
12480 | 0 | !ssl->msgsReceived.got_certificate) { |
12481 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12482 | 0 | "missing Certificate message"); |
12483 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12484 | 0 | return OUT_OF_ORDER_E; |
12485 | 0 | } |
12486 | | /* Mutual authentication on server requires a certificate from |
12487 | | * peer. Verify peer set on client side requires a certificate |
12488 | | * from peer as not doing PSK. |
12489 | | */ |
12490 | 0 | if ((ssl->options.mutualAuth || |
12491 | 0 | (ssl->options.side == WOLFSSL_CLIENT_END && |
12492 | 0 | ssl->options.verifyPeer)) && !ssl->options.havePeerCert) { |
12493 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12494 | 0 | "no valid certificate"); |
12495 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12496 | 0 | return OUT_OF_ORDER_E; |
12497 | 0 | } |
12498 | | /* Must have received a valid CertificateVerify if verifying |
12499 | | * peer and got a peer certificate. |
12500 | | */ |
12501 | 0 | if ((ssl->options.mutualAuth || ssl->options.verifyPeer) && |
12502 | 0 | ssl->options.havePeerCert && !ssl->options.havePeerVerify) { |
12503 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12504 | 0 | "Certificate message but no CertificateVerify"); |
12505 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12506 | 0 | return OUT_OF_ORDER_E; |
12507 | 0 | } |
12508 | 0 | } |
12509 | | /* Check previously seen. */ |
12510 | 0 | if (ssl->msgsReceived.got_finished) { |
12511 | 0 | WOLFSSL_MSG("Duplicate Finished received"); |
12512 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12513 | 0 | return DUPLICATE_MSG_E; |
12514 | 0 | } |
12515 | 0 | ssl->msgsReceived.got_finished = 1; |
12516 | |
|
12517 | 0 | break; |
12518 | | |
12519 | 0 | case key_update: |
12520 | | /* Valid on both sides. */ |
12521 | | /* Check state. |
12522 | | * Client and server must have received finished message from other |
12523 | | * side. |
12524 | | */ |
12525 | 0 | if (!ssl->msgsReceived.got_finished) { |
12526 | 0 | WOLFSSL_MSG("No KeyUpdate before Finished"); |
12527 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12528 | 0 | return OUT_OF_ORDER_E; |
12529 | 0 | } |
12530 | | /* Multiple KeyUpdates can be sent. */ |
12531 | 0 | break; |
12532 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
12533 | | case hello_verify_request: |
12534 | | if (!ssl->options.dtls) { |
12535 | | WOLFSSL_MSG("HelloVerifyRequest when not in DTLS"); |
12536 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12537 | | return OUT_OF_ORDER_E; |
12538 | | } |
12539 | | if (ssl->msgsReceived.got_hello_verify_request) { |
12540 | | WOLFSSL_MSG("Duplicate HelloVerifyRequest received"); |
12541 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12542 | | return DUPLICATE_MSG_E; |
12543 | | } |
12544 | | ssl->msgsReceived.got_hello_verify_request = 1; |
12545 | | if (ssl->msgsReceived.got_hello_retry_request) { |
12546 | | WOLFSSL_MSG( |
12547 | | "Both HelloVerifyRequest and HelloRetryRequest received"); |
12548 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12549 | | return DUPLICATE_MSG_E; |
12550 | | } |
12551 | | if (ssl->options.serverState >= |
12552 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE || |
12553 | | ssl->options.connectState != CLIENT_HELLO_SENT) { |
12554 | | WOLFSSL_MSG("HelloVerifyRequest received out of order"); |
12555 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12556 | | return OUT_OF_ORDER_E; |
12557 | | } |
12558 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12559 | | WOLFSSL_MSG("HelloVerifyRequest received on the server"); |
12560 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12561 | | return SIDE_ERROR; |
12562 | | } |
12563 | | if (!ssl->options.downgrade || |
12564 | | ssl->options.minDowngrade < DTLSv1_2_MINOR) { |
12565 | | WOLFSSL_MSG( |
12566 | | "HelloVerifyRequest received but not DTLSv1.2 allowed"); |
12567 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
12568 | | return VERSION_ERROR; |
12569 | | } |
12570 | | break; |
12571 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_TLS12*/ |
12572 | | |
12573 | 0 | default: |
12574 | 0 | WOLFSSL_MSG("Unknown message type"); |
12575 | 0 | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12576 | 0 | return SANITY_MSG_E; |
12577 | 0 | } |
12578 | | |
12579 | 0 | return 0; |
12580 | 0 | } |
12581 | | |
12582 | | /* Handle a type of handshake message that has been received. |
12583 | | * |
12584 | | * ssl The SSL/TLS object. |
12585 | | * input The message buffer. |
12586 | | * inOutIdx On entry, the index into the buffer of the current message. |
12587 | | * On exit, the index into the buffer of the next message. |
12588 | | * size The length of the current handshake message. |
12589 | | * totalSz Length of remaining data in the message buffer. |
12590 | | * returns 0 on success and otherwise failure. |
12591 | | */ |
12592 | | int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
12593 | | byte type, word32 size, word32 totalSz) |
12594 | | { |
12595 | | int ret = 0, tmp; |
12596 | | word32 inIdx = *inOutIdx; |
12597 | | int alertType; |
12598 | | #if defined(HAVE_ECH) |
12599 | | TLSX* echX = NULL; |
12600 | | word32 echInOutIdx; |
12601 | | #endif |
12602 | | |
12603 | | (void)totalSz; |
12604 | | |
12605 | | WOLFSSL_ENTER("DoTls13HandShakeMsgType"); |
12606 | | |
12607 | | /* make sure we can read the message */ |
12608 | | if (*inOutIdx + size > totalSz) |
12609 | | return INCOMPLETE_DATA; |
12610 | | |
12611 | | /* sanity check msg received */ |
12612 | | if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) { |
12613 | | WOLFSSL_MSG("Sanity Check on handshake message type received failed"); |
12614 | | if (ret == WC_NO_ERR_TRACE(VERSION_ERROR)) |
12615 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
12616 | | else |
12617 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12618 | | return ret; |
12619 | | } |
12620 | | |
12621 | | #if defined(WOLFSSL_CALLBACKS) |
12622 | | /* add name later, add on record and handshake header part back on */ |
12623 | | if (ssl->toInfoOn) { |
12624 | | ret = AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - |
12625 | | HANDSHAKE_HEADER_SZ, size + HANDSHAKE_HEADER_SZ, READ_PROTO, |
12626 | | RECORD_HEADER_SZ, ssl->heap); |
12627 | | if (ret != 0) |
12628 | | return ret; |
12629 | | AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo); |
12630 | | } |
12631 | | #endif |
12632 | | |
12633 | | if (ssl->options.handShakeState == HANDSHAKE_DONE && |
12634 | | type != session_ticket && type != certificate_request && |
12635 | | type != certificate && type != key_update && type != finished) { |
12636 | | WOLFSSL_MSG("HandShake message after handshake complete"); |
12637 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12638 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12639 | | return OUT_OF_ORDER_E; |
12640 | | } |
12641 | | |
12642 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12643 | | ssl->options.serverState == NULL_STATE && |
12644 | | type != server_hello && type != hello_retry_request |
12645 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
12646 | | && (!ssl->options.dtls || type != hello_verify_request) |
12647 | | #endif /* defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) */ |
12648 | | ) { |
12649 | | WOLFSSL_MSG("First server message not server hello"); |
12650 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12651 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12652 | | return OUT_OF_ORDER_E; |
12653 | | } |
12654 | | |
12655 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
12656 | | ssl->options.clientState == NULL_STATE && type != client_hello) { |
12657 | | WOLFSSL_MSG("First client message not client hello"); |
12658 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12659 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12660 | | return OUT_OF_ORDER_E; |
12661 | | } |
12662 | | |
12663 | | /* above checks handshake state */ |
12664 | | switch (type) { |
12665 | | #ifndef NO_WOLFSSL_CLIENT |
12666 | | /* Messages only received by client. */ |
12667 | | case server_hello: |
12668 | | WOLFSSL_MSG("processing server hello"); |
12669 | | ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type); |
12670 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
12671 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
12672 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
12673 | | if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) || |
12674 | | IsAtLeastTLSv1_3(ssl->version)) { |
12675 | | ssl->options.cacheMessages = 0; |
12676 | | if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) { |
12677 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
12678 | | XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES); |
12679 | | ssl->hsHashes->messages = NULL; |
12680 | | } |
12681 | | } |
12682 | | #endif |
12683 | | break; |
12684 | | |
12685 | | case encrypted_extensions: |
12686 | | WOLFSSL_MSG("processing encrypted extensions"); |
12687 | | ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size); |
12688 | | break; |
12689 | | |
12690 | | #ifndef NO_CERTS |
12691 | | case certificate_request: |
12692 | | WOLFSSL_MSG("processing certificate request"); |
12693 | | ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size); |
12694 | | break; |
12695 | | #endif |
12696 | | |
12697 | | case session_ticket: |
12698 | | WOLFSSL_MSG("processing new session ticket"); |
12699 | | ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size); |
12700 | | break; |
12701 | | #endif /* !NO_WOLFSSL_CLIENT */ |
12702 | | |
12703 | | #ifndef NO_WOLFSSL_SERVER |
12704 | | /* Messages only received by server. */ |
12705 | | case client_hello: |
12706 | | WOLFSSL_MSG("processing client hello"); |
12707 | | #if defined(HAVE_ECH) |
12708 | | /* keep the start idx so we can restore it for the inner call */ |
12709 | | echInOutIdx = *inOutIdx; |
12710 | | #endif |
12711 | | ret = DoTls13ClientHello(ssl, input, inOutIdx, size); |
12712 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
12713 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
12714 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
12715 | | if ((ssl->options.resuming || !ssl->options.verifyPeer || |
12716 | | !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) |
12717 | | #ifdef WOLFSSL_DTLS13 |
12718 | | && (!ssl->options.dtls) |
12719 | | #endif |
12720 | | ) { |
12721 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
12722 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E) && |
12723 | | ret != WC_NO_ERR_TRACE(OCSP_WANT_READ)) |
12724 | | #endif |
12725 | | { |
12726 | | ssl->options.cacheMessages = 0; |
12727 | | if ((ssl->hsHashes != NULL) && |
12728 | | (ssl->hsHashes->messages != NULL)) { |
12729 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
12730 | | XFREE(ssl->hsHashes->messages, ssl->heap, |
12731 | | DYNAMIC_TYPE_HASHES); |
12732 | | ssl->hsHashes->messages = NULL; |
12733 | | } |
12734 | | } |
12735 | | } |
12736 | | #endif |
12737 | | #if defined(HAVE_ECH) |
12738 | | if (ret == 0) { |
12739 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
12740 | | |
12741 | | if (echX != NULL && |
12742 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
12743 | | /* reset the inOutIdx to the outer start */ |
12744 | | *inOutIdx = echInOutIdx; |
12745 | | /* call again with the inner hello */ |
12746 | | if (ret == 0) { |
12747 | | ret = DoTls13ClientHello(ssl, |
12748 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello, |
12749 | | &echInOutIdx, |
12750 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen); |
12751 | | } |
12752 | | /* if the inner ech parsed successfully we have successfully |
12753 | | * handled the hello and can skip the whole message */ |
12754 | | if (ret == 0) |
12755 | | *inOutIdx += size; |
12756 | | } |
12757 | | } |
12758 | | #endif /* HAVE_ECH */ |
12759 | | break; |
12760 | | |
12761 | | #ifdef WOLFSSL_EARLY_DATA |
12762 | | case end_of_early_data: |
12763 | | WOLFSSL_MSG("processing end of early data"); |
12764 | | ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size); |
12765 | | break; |
12766 | | #endif |
12767 | | #endif /* !NO_WOLFSSL_SERVER */ |
12768 | | |
12769 | | /* Messages received by both client and server. */ |
12770 | | #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \ |
12771 | | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
12772 | | case certificate: |
12773 | | WOLFSSL_MSG("processing certificate"); |
12774 | | ret = DoTls13Certificate(ssl, input, inOutIdx, size); |
12775 | | break; |
12776 | | #endif |
12777 | | |
12778 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
12779 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
12780 | | case certificate_verify: |
12781 | | WOLFSSL_MSG("processing certificate verify"); |
12782 | | ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size); |
12783 | | break; |
12784 | | #endif |
12785 | | case finished: |
12786 | | WOLFSSL_MSG("processing finished"); |
12787 | | ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF); |
12788 | | break; |
12789 | | |
12790 | | case key_update: |
12791 | | WOLFSSL_MSG("processing key update"); |
12792 | | ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size); |
12793 | | break; |
12794 | | |
12795 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
12796 | | !defined(NO_WOLFSSL_CLIENT) |
12797 | | case hello_verify_request: |
12798 | | WOLFSSL_MSG("processing hello verify request"); |
12799 | | ret = DoHelloVerifyRequest(ssl, input, inOutIdx, size); |
12800 | | break; |
12801 | | #endif |
12802 | | default: |
12803 | | WOLFSSL_MSG("Unknown handshake message type"); |
12804 | | ret = UNKNOWN_HANDSHAKE_TYPE; |
12805 | | break; |
12806 | | } |
12807 | | |
12808 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_ASYNC_IO) |
12809 | | /* if async, offset index so this msg will be processed again */ |
12810 | | /* NOTE: check this now before other calls can overwrite ret */ |
12811 | | if ((ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
12812 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) && *inOutIdx > 0) { |
12813 | | /* DTLS always stores a message in a buffer when async is enable, so we |
12814 | | * don't need to adjust for the extra bytes here (*inOutIdx is always |
12815 | | * == 0) */ |
12816 | | *inOutIdx -= HANDSHAKE_HEADER_SZ; |
12817 | | } |
12818 | | |
12819 | | /* make sure async error is cleared */ |
12820 | | if (ret == 0 && |
12821 | | (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E) || |
12822 | | ssl->error == WC_NO_ERR_TRACE(OCSP_WANT_READ))) { |
12823 | | ssl->error = 0; |
12824 | | } |
12825 | | #endif |
12826 | | if (ret == 0 && type != client_hello && type != session_ticket && |
12827 | | type != key_update) { |
12828 | | ret = HashInput(ssl, input + inIdx, (int)size); |
12829 | | } |
12830 | | |
12831 | | alertType = TranslateErrorToAlert(ret); |
12832 | | |
12833 | | if (alertType != invalid_alert) { |
12834 | | #ifdef WOLFSSL_DTLS13 |
12835 | | if (type == client_hello && ssl->options.dtls) |
12836 | | DtlsSetSeqNumForReply(ssl); |
12837 | | #endif |
12838 | | tmp = SendAlert(ssl, alert_fatal, alertType); |
12839 | | /* propagate socket error instead of tls error to be sure the error is |
12840 | | * not ignored by DTLS code */ |
12841 | | if (tmp == WC_NO_ERR_TRACE(SOCKET_ERROR_E)) |
12842 | | ret = SOCKET_ERROR_E; |
12843 | | } |
12844 | | |
12845 | | if (ret == 0 && ssl->options.tls1_3) { |
12846 | | /* Need to hash input message before deriving secrets. */ |
12847 | | #ifndef NO_WOLFSSL_CLIENT |
12848 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12849 | | if (type == server_hello) { |
12850 | | if ((ret = DeriveEarlySecret(ssl)) != 0) |
12851 | | return ret; |
12852 | | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
12853 | | return ret; |
12854 | | |
12855 | | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
12856 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
12857 | | return ret; |
12858 | | } |
12859 | | #ifdef WOLFSSL_EARLY_DATA |
12860 | | if (ssl->earlyData != no_early_data) { |
12861 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
12862 | | return ret; |
12863 | | } |
12864 | | else |
12865 | | #endif |
12866 | | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
12867 | | return ret; |
12868 | | |
12869 | | #ifdef WOLFSSL_DTLS13 |
12870 | | if (ssl->options.dtls) { |
12871 | | w64wrapper epochHandshake; |
12872 | | epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
12873 | | ssl->dtls13Epoch = epochHandshake; |
12874 | | ssl->dtls13PeerEpoch = epochHandshake; |
12875 | | |
12876 | | ret = Dtls13SetEpochKeys( |
12877 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
12878 | | if (ret != 0) |
12879 | | return ret; |
12880 | | |
12881 | | } |
12882 | | #endif /* WOLFSSL_DTLS13 */ |
12883 | | } |
12884 | | |
12885 | | if (type == finished) { |
12886 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
12887 | | return ret; |
12888 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
12889 | | ForceZero(ssl->arrays->preMasterSecret, |
12890 | | ssl->arrays->preMasterSz); |
12891 | | #ifdef WOLFSSL_EARLY_DATA |
12892 | | #ifdef WOLFSSL_QUIC |
12893 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData != no_early_data) { |
12894 | | /* QUIC never sends/receives EndOfEarlyData, but having |
12895 | | * early data means the last encryption keys had not been |
12896 | | * set yet. */ |
12897 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
12898 | | return ret; |
12899 | | } |
12900 | | #endif |
12901 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
12902 | | ENCRYPT_AND_DECRYPT_SIDE, |
12903 | | ssl->earlyData == no_early_data)) != 0) { |
12904 | | return ret; |
12905 | | } |
12906 | | if (ssl->earlyData != no_early_data) { |
12907 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, |
12908 | | 1)) != 0) { |
12909 | | return ret; |
12910 | | } |
12911 | | } |
12912 | | #else |
12913 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
12914 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
12915 | | return ret; |
12916 | | } |
12917 | | #endif |
12918 | | /* Setup keys for application data messages. */ |
12919 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
12920 | | return ret; |
12921 | | } |
12922 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
12923 | | if (type == certificate_request && |
12924 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
12925 | | /* reset handshake states */ |
12926 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
12927 | | ssl->options.connectState = FIRST_REPLY_DONE; |
12928 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
12929 | | ssl->options.processReply = 0; /* doProcessInit */ |
12930 | | |
12931 | | /* |
12932 | | DTLSv1.3 note: We can't reset serverState to |
12933 | | SERVER_FINISHED_COMPLETE with the goal that this connect |
12934 | | blocks until the cert/cert_verify/finished flight gets ACKed |
12935 | | by the server. The problem is that we will invoke |
12936 | | ProcessReplyEx() in that case, but we came here from |
12937 | | ProcessReplyEx() and it is not re-entrant safe (the input |
12938 | | buffer would still have the certificate_request message). */ |
12939 | | |
12940 | | if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) { |
12941 | | ret = ssl->error; |
12942 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
12943 | | ret = POST_HAND_AUTH_ERROR; |
12944 | | } |
12945 | | } |
12946 | | #endif |
12947 | | } |
12948 | | #endif /* NO_WOLFSSL_CLIENT */ |
12949 | | |
12950 | | #ifndef NO_WOLFSSL_SERVER |
12951 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12952 | | if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) { |
12953 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
12954 | | if (ret != 0) |
12955 | | return ret; |
12956 | | } |
12957 | | #endif |
12958 | | #endif /* NO_WOLFSSL_SERVER */ |
12959 | | } |
12960 | | |
12961 | | #ifdef WOLFSSL_DTLS13 |
12962 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) { |
12963 | | DtlsResetState(ssl); |
12964 | | if (DtlsIgnoreError(ret)) |
12965 | | ret = 0; |
12966 | | } |
12967 | | #endif |
12968 | | |
12969 | | WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret); |
12970 | | return ret; |
12971 | | } |
12972 | | |
12973 | | |
12974 | | /* Handle a handshake message that has been received. |
12975 | | * |
12976 | | * ssl The SSL/TLS object. |
12977 | | * input The message buffer. |
12978 | | * inOutIdx On entry, the index into the buffer of the current message. |
12979 | | * On exit, the index into the buffer of the next message. |
12980 | | * totalSz Length of remaining data in the message buffer. |
12981 | | * returns 0 on success and otherwise failure. |
12982 | | */ |
12983 | | int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
12984 | | word32 totalSz) |
12985 | 22.3k | { |
12986 | 22.3k | int ret = 0; |
12987 | 22.3k | word32 inputLength; |
12988 | 22.3k | byte type; |
12989 | 22.3k | word32 size = 0; |
12990 | | |
12991 | 22.3k | WOLFSSL_ENTER("DoTls13HandShakeMsg"); |
12992 | | |
12993 | 22.3k | if (ssl->arrays == NULL) { |
12994 | 0 | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
12995 | 0 | totalSz) != 0) { |
12996 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
12997 | 0 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
12998 | 0 | return PARSE_ERROR; |
12999 | 0 | } |
13000 | | |
13001 | 0 | ret = EarlySanityCheckMsgReceived(ssl, type, size); |
13002 | 0 | if (ret != 0) { |
13003 | 0 | WOLFSSL_ERROR(ret); |
13004 | 0 | return ret; |
13005 | 0 | } |
13006 | | |
13007 | 0 | return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13008 | 0 | totalSz); |
13009 | 0 | } |
13010 | | |
13011 | 22.3k | inputLength = ssl->buffers.inputBuffer.length - *inOutIdx - ssl->keys.padSz; |
13012 | | |
13013 | | /* If there is a pending fragmented handshake message, |
13014 | | * pending message size will be non-zero. */ |
13015 | 22.3k | if (ssl->arrays->pendingMsgSz == 0) { |
13016 | | |
13017 | 11.9k | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13018 | 11.9k | totalSz) != 0) { |
13019 | 16 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13020 | 16 | return PARSE_ERROR; |
13021 | 16 | } |
13022 | | |
13023 | 11.9k | ret = EarlySanityCheckMsgReceived(ssl, type, |
13024 | 11.9k | min(inputLength - HANDSHAKE_HEADER_SZ, size)); |
13025 | 11.9k | if (ret != 0) { |
13026 | 63 | WOLFSSL_ERROR(ret); |
13027 | 63 | return ret; |
13028 | 63 | } |
13029 | | |
13030 | | /* Cap the maximum size of a handshake message to something reasonable. |
13031 | | * By default is the maximum size of a certificate message assuming |
13032 | | * nine 2048-bit RSA certificates in the chain. */ |
13033 | 11.8k | if (size > MAX_HANDSHAKE_SZ) { |
13034 | 68 | WOLFSSL_MSG("Handshake message too large"); |
13035 | 68 | WOLFSSL_ERROR_VERBOSE(HANDSHAKE_SIZE_ERROR); |
13036 | 68 | return HANDSHAKE_SIZE_ERROR; |
13037 | 68 | } |
13038 | | |
13039 | | /* size is the size of the certificate message payload */ |
13040 | 11.8k | if (inputLength - HANDSHAKE_HEADER_SZ < size) { |
13041 | 690 | ssl->arrays->pendingMsgType = type; |
13042 | 690 | ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ; |
13043 | 690 | ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ, |
13044 | 690 | ssl->heap, |
13045 | 690 | DYNAMIC_TYPE_ARRAYS); |
13046 | 690 | if (ssl->arrays->pendingMsg == NULL) |
13047 | 22 | return MEMORY_E; |
13048 | 668 | XMEMCPY(ssl->arrays->pendingMsg, |
13049 | 668 | input + *inOutIdx - HANDSHAKE_HEADER_SZ, |
13050 | 668 | inputLength); |
13051 | 668 | ssl->arrays->pendingMsgOffset = inputLength; |
13052 | 668 | *inOutIdx += inputLength + ssl->keys.padSz - HANDSHAKE_HEADER_SZ; |
13053 | 668 | return 0; |
13054 | 690 | } |
13055 | | |
13056 | 11.1k | ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13057 | 11.1k | totalSz); |
13058 | 11.1k | } |
13059 | 10.3k | else { |
13060 | 10.3k | if (inputLength + ssl->arrays->pendingMsgOffset > |
13061 | 10.3k | ssl->arrays->pendingMsgSz) { |
13062 | 43 | inputLength = ssl->arrays->pendingMsgSz - |
13063 | 43 | ssl->arrays->pendingMsgOffset; |
13064 | 43 | } |
13065 | | |
13066 | 10.3k | ret = EarlySanityCheckMsgReceived(ssl, ssl->arrays->pendingMsgType, |
13067 | 10.3k | inputLength); |
13068 | 10.3k | if (ret != 0) { |
13069 | 25 | WOLFSSL_ERROR(ret); |
13070 | 25 | return ret; |
13071 | 25 | } |
13072 | | |
13073 | 10.3k | XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset, |
13074 | 10.3k | input + *inOutIdx, inputLength); |
13075 | 10.3k | ssl->arrays->pendingMsgOffset += inputLength; |
13076 | 10.3k | *inOutIdx += inputLength + ssl->keys.padSz; |
13077 | | |
13078 | 10.3k | if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz) |
13079 | 120 | { |
13080 | 120 | word32 idx = 0; |
13081 | 120 | ret = DoTls13HandShakeMsgType(ssl, |
13082 | 120 | ssl->arrays->pendingMsg + HANDSHAKE_HEADER_SZ, |
13083 | 120 | &idx, ssl->arrays->pendingMsgType, |
13084 | 120 | ssl->arrays->pendingMsgSz - HANDSHAKE_HEADER_SZ, |
13085 | 120 | ssl->arrays->pendingMsgSz); |
13086 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13087 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
13088 | | /* setup to process fragment again */ |
13089 | | ssl->arrays->pendingMsgOffset -= inputLength; |
13090 | | *inOutIdx -= inputLength + ssl->keys.padSz; |
13091 | | } |
13092 | | else |
13093 | | #endif |
13094 | 120 | { |
13095 | 120 | XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS); |
13096 | 120 | ssl->arrays->pendingMsg = NULL; |
13097 | 120 | ssl->arrays->pendingMsgSz = 0; |
13098 | 120 | } |
13099 | 120 | } |
13100 | 10.3k | } |
13101 | | |
13102 | 21.4k | WOLFSSL_LEAVE("DoTls13HandShakeMsg", ret); |
13103 | 21.4k | return ret; |
13104 | 22.3k | } |
13105 | | |
13106 | | #ifndef NO_WOLFSSL_CLIENT |
13107 | | |
13108 | | /* The client connecting to the server. |
13109 | | * The protocol version is expecting to be TLS v1.3. |
13110 | | * If the server downgrades, and older versions of the protocol are compiled |
13111 | | * in, the client will fallback to wolfSSL_connect(). |
13112 | | * Please see note at top of README if you get an error from connect. |
13113 | | * |
13114 | | * ssl The SSL/TLS object. |
13115 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
13116 | | * unrecoverable error occurs and 0 otherwise. |
13117 | | * For more error information use wolfSSL_get_error(). |
13118 | | */ |
13119 | | int wolfSSL_connect_TLSv13(WOLFSSL* ssl) |
13120 | | { |
13121 | | int advanceState; |
13122 | | int ret = 0; |
13123 | | |
13124 | | WOLFSSL_ENTER("wolfSSL_connect_TLSv13"); |
13125 | | |
13126 | | #ifdef HAVE_ERRNO_H |
13127 | | errno = 0; |
13128 | | #endif |
13129 | | |
13130 | | if (ssl == NULL) |
13131 | | return BAD_FUNC_ARG; |
13132 | | |
13133 | | if (ssl->options.side != WOLFSSL_CLIENT_END) { |
13134 | | ssl->error = SIDE_ERROR; |
13135 | | WOLFSSL_ERROR(ssl->error); |
13136 | | return WOLFSSL_FATAL_ERROR; |
13137 | | } |
13138 | | |
13139 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
13140 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
13141 | | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
13142 | | return ret; |
13143 | | } |
13144 | | |
13145 | | #ifdef WOLFSSL_DTLS |
13146 | | if (ssl->version.major == DTLS_MAJOR) { |
13147 | | ssl->options.dtls = 1; |
13148 | | ssl->options.dtlsStateful = 1; |
13149 | | } |
13150 | | #endif |
13151 | | |
13152 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
13153 | | if ((ssl->ConnectFilter != NULL) && |
13154 | | (ssl->options.connectState == CONNECT_BEGIN)) |
13155 | | { |
13156 | | wolfSSL_netfilter_decision_t res; |
13157 | | if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) == |
13158 | | WOLFSSL_SUCCESS) && |
13159 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
13160 | | ssl->error = SOCKET_FILTERED_E; |
13161 | | WOLFSSL_ERROR(ssl->error); |
13162 | | return WOLFSSL_FATAL_ERROR; |
13163 | | } |
13164 | | } |
13165 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
13166 | | |
13167 | | /* fragOffset is non-zero when sending fragments. On the last |
13168 | | * fragment, fragOffset is zero again, and the state can be |
13169 | | * advanced. Also, only advance from states in which we send data */ |
13170 | | advanceState = (ssl->options.connectState == CONNECT_BEGIN || |
13171 | | ssl->options.connectState == HELLO_AGAIN || |
13172 | | (ssl->options.connectState >= FIRST_REPLY_DONE && |
13173 | | ssl->options.connectState <= FIRST_REPLY_FOURTH)); |
13174 | | |
13175 | | #ifdef WOLFSSL_DTLS13 |
13176 | | if (ssl->options.dtls) |
13177 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
13178 | | && !ssl->dtls13SendingAckOrRtx; |
13179 | | #endif /* WOLFSSL_DTLS13 */ |
13180 | | |
13181 | | if (ssl->buffers.outputBuffer.length > 0 |
13182 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13183 | | /* do not send buffered or advance state if last error was an |
13184 | | async pending operation */ |
13185 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
13186 | | #endif |
13187 | | ) { |
13188 | | if ((ret = SendBuffered(ssl)) == 0) { |
13189 | | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
13190 | | if (advanceState) { |
13191 | | #ifdef WOLFSSL_DTLS13 |
13192 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) && |
13193 | | ssl->options.connectState == FIRST_REPLY_FOURTH) { |
13194 | | /* WAIT_FINISHED_ACK is a state added afterwards, but it |
13195 | | can't follow FIRST_REPLY_FOURTH in the enum order. Indeed |
13196 | | the value of the enum ConnectState is stored in |
13197 | | serialized session. This would make importing serialized |
13198 | | session from other wolfSSL version incompatible */ |
13199 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13200 | | } |
13201 | | else |
13202 | | #endif /* WOLFSSL_DTLS13 */ |
13203 | | { |
13204 | | ssl->options.connectState++; |
13205 | | } |
13206 | | WOLFSSL_MSG("connect state: " |
13207 | | "Advanced from last buffered fragment send"); |
13208 | | #ifdef WOLFSSL_ASYNC_IO |
13209 | | FreeAsyncCtx(ssl, 0); |
13210 | | #endif |
13211 | | |
13212 | | } |
13213 | | } |
13214 | | else { |
13215 | | WOLFSSL_MSG("connect state: " |
13216 | | "Not advanced, more fragments to send"); |
13217 | | } |
13218 | | #ifdef WOLFSSL_DTLS13 |
13219 | | if (ssl->options.dtls) |
13220 | | ssl->dtls13SendingAckOrRtx = 0; |
13221 | | #endif /* WOLFSSL_DTLS13 */ |
13222 | | |
13223 | | } |
13224 | | else { |
13225 | | ssl->error = ret; |
13226 | | WOLFSSL_ERROR(ssl->error); |
13227 | | return WOLFSSL_FATAL_ERROR; |
13228 | | } |
13229 | | } |
13230 | | |
13231 | | ret = RetrySendAlert(ssl); |
13232 | | if (ret != 0) { |
13233 | | ssl->error = ret; |
13234 | | WOLFSSL_ERROR(ssl->error); |
13235 | | return WOLFSSL_FATAL_ERROR; |
13236 | | } |
13237 | | |
13238 | | #ifdef WOLFSSL_DTLS13 |
13239 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
13240 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
13241 | | WOLFSSL_ERROR(ssl->error); |
13242 | | return WOLFSSL_FATAL_ERROR; |
13243 | | } |
13244 | | |
13245 | | /* we sent all the fragments. Advance state. */ |
13246 | | ssl->options.connectState++; |
13247 | | } |
13248 | | #endif /* WOLFSSL_DTLS13 */ |
13249 | | |
13250 | | switch (ssl->options.connectState) { |
13251 | | |
13252 | | case CONNECT_BEGIN: |
13253 | | /* Always send client hello first. */ |
13254 | | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13255 | | WOLFSSL_ERROR(ssl->error); |
13256 | | return WOLFSSL_FATAL_ERROR; |
13257 | | } |
13258 | | |
13259 | | ssl->options.connectState = CLIENT_HELLO_SENT; |
13260 | | WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); |
13261 | | #ifdef WOLFSSL_EARLY_DATA |
13262 | | if (ssl->earlyData != no_early_data) { |
13263 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13264 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat) { |
13265 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13266 | | WOLFSSL_ERROR(ssl->error); |
13267 | | return WOLFSSL_FATAL_ERROR; |
13268 | | } |
13269 | | ssl->options.sentChangeCipher = 1; |
13270 | | } |
13271 | | #endif |
13272 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13273 | | return WOLFSSL_SUCCESS; |
13274 | | } |
13275 | | #endif |
13276 | | FALL_THROUGH; |
13277 | | |
13278 | | case CLIENT_HELLO_SENT: |
13279 | | /* Get the response/s from the server. */ |
13280 | | while (ssl->options.serverState < |
13281 | | SERVER_HELLOVERIFYREQUEST_COMPLETE) { |
13282 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13283 | | WOLFSSL_ERROR(ssl->error); |
13284 | | return WOLFSSL_FATAL_ERROR; |
13285 | | } |
13286 | | |
13287 | | #ifdef WOLFSSL_DTLS13 |
13288 | | if (ssl->options.dtls) { |
13289 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13290 | | WOLFSSL_ERROR(ssl->error); |
13291 | | return WOLFSSL_FATAL_ERROR; |
13292 | | } |
13293 | | } |
13294 | | #endif /* WOLFSSL_DTLS13 */ |
13295 | | } |
13296 | | |
13297 | | if (!ssl->options.tls1_3) { |
13298 | | #ifndef WOLFSSL_NO_TLS12 |
13299 | | if (ssl->options.downgrade) |
13300 | | return wolfSSL_connect(ssl); |
13301 | | #endif |
13302 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
13303 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
13304 | | return VERSION_ERROR; |
13305 | | } |
13306 | | |
13307 | | ssl->options.connectState = HELLO_AGAIN; |
13308 | | WOLFSSL_MSG("connect state: HELLO_AGAIN"); |
13309 | | FALL_THROUGH; |
13310 | | |
13311 | | case HELLO_AGAIN: |
13312 | | |
13313 | | if (ssl->options.serverState == |
13314 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
13315 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13316 | | if (!ssl->options.dtls && !ssl->options.sentChangeCipher |
13317 | | && ssl->options.tls13MiddleBoxCompat) { |
13318 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13319 | | WOLFSSL_ERROR(ssl->error); |
13320 | | return WOLFSSL_FATAL_ERROR; |
13321 | | } |
13322 | | ssl->options.sentChangeCipher = 1; |
13323 | | } |
13324 | | #endif |
13325 | | /* Try again with different security parameters. */ |
13326 | | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13327 | | WOLFSSL_ERROR(ssl->error); |
13328 | | return WOLFSSL_FATAL_ERROR; |
13329 | | } |
13330 | | } |
13331 | | |
13332 | | ssl->options.connectState = HELLO_AGAIN_REPLY; |
13333 | | WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY"); |
13334 | | FALL_THROUGH; |
13335 | | |
13336 | | case HELLO_AGAIN_REPLY: |
13337 | | /* Get the response/s from the server. */ |
13338 | | while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13339 | | #ifdef WOLFSSL_DTLS13 |
13340 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
13341 | | #ifndef WOLFSSL_NO_TLS12 |
13342 | | if (ssl->options.downgrade) |
13343 | | return wolfSSL_connect(ssl); |
13344 | | #endif |
13345 | | } |
13346 | | #endif /* WOLFSSL_DTLS13 */ |
13347 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13348 | | WOLFSSL_ERROR(ssl->error); |
13349 | | return WOLFSSL_FATAL_ERROR; |
13350 | | } |
13351 | | |
13352 | | #ifdef WOLFSSL_DTLS13 |
13353 | | if (ssl->options.dtls) { |
13354 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13355 | | WOLFSSL_ERROR(ssl->error); |
13356 | | return WOLFSSL_FATAL_ERROR; |
13357 | | } |
13358 | | } |
13359 | | #endif /* WOLFSSL_DTLS13 */ |
13360 | | } |
13361 | | |
13362 | | ssl->options.connectState = FIRST_REPLY_DONE; |
13363 | | WOLFSSL_MSG("connect state: FIRST_REPLY_DONE"); |
13364 | | FALL_THROUGH; |
13365 | | |
13366 | | case FIRST_REPLY_DONE: |
13367 | | if (ssl->options.certOnly) |
13368 | | return WOLFSSL_SUCCESS; |
13369 | | #ifdef WOLFSSL_EARLY_DATA |
13370 | | if (!ssl->options.dtls && ssl->earlyData != no_early_data |
13371 | | && !WOLFSSL_IS_QUIC(ssl)) { |
13372 | | if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) { |
13373 | | WOLFSSL_ERROR(ssl->error); |
13374 | | return WOLFSSL_FATAL_ERROR; |
13375 | | } |
13376 | | WOLFSSL_MSG("sent: end_of_early_data"); |
13377 | | } |
13378 | | #endif |
13379 | | |
13380 | | ssl->options.connectState = FIRST_REPLY_FIRST; |
13381 | | WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST"); |
13382 | | FALL_THROUGH; |
13383 | | |
13384 | | case FIRST_REPLY_FIRST: |
13385 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13386 | | if (!ssl->options.sentChangeCipher && !ssl->options.dtls |
13387 | | && ssl->options.tls13MiddleBoxCompat) { |
13388 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13389 | | WOLFSSL_ERROR(ssl->error); |
13390 | | return WOLFSSL_FATAL_ERROR; |
13391 | | } |
13392 | | ssl->options.sentChangeCipher = 1; |
13393 | | } |
13394 | | #endif |
13395 | | |
13396 | | ssl->options.connectState = FIRST_REPLY_SECOND; |
13397 | | WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND"); |
13398 | | FALL_THROUGH; |
13399 | | |
13400 | | case FIRST_REPLY_SECOND: |
13401 | | /* CLIENT: check peer authentication. */ |
13402 | | if (!ssl->options.peerAuthGood) { |
13403 | | WOLFSSL_MSG("Server authentication did not happen"); |
13404 | | WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR); |
13405 | | return WOLFSSL_FATAL_ERROR; |
13406 | | } |
13407 | | #ifndef NO_CERTS |
13408 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13409 | | ssl->error = SendTls13Certificate(ssl); |
13410 | | if (ssl->error != 0) { |
13411 | | #ifdef WOLFSSL_CHECK_ALERT_ON_ERR |
13412 | | ProcessReplyEx(ssl, 1); /* See if an alert was sent. */ |
13413 | | #endif |
13414 | | WOLFSSL_ERROR(ssl->error); |
13415 | | return WOLFSSL_FATAL_ERROR; |
13416 | | } |
13417 | | WOLFSSL_MSG("sent: certificate"); |
13418 | | } |
13419 | | #endif |
13420 | | |
13421 | | ssl->options.connectState = FIRST_REPLY_THIRD; |
13422 | | WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD"); |
13423 | | FALL_THROUGH; |
13424 | | |
13425 | | case FIRST_REPLY_THIRD: |
13426 | | #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
13427 | | defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
13428 | | defined(HAVE_FALCON) || defined(HAVE_DILITHIUM))) && \ |
13429 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
13430 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13431 | | ssl->error = SendTls13CertificateVerify(ssl); |
13432 | | if (ssl->error != 0) { |
13433 | | #ifdef WOLFSSL_CHECK_ALERT_ON_ERR |
13434 | | ProcessReplyEx(ssl, 1); /* See if an alert was sent. */ |
13435 | | #endif |
13436 | | WOLFSSL_ERROR(ssl->error); |
13437 | | return WOLFSSL_FATAL_ERROR; |
13438 | | } |
13439 | | WOLFSSL_MSG("sent: certificate verify"); |
13440 | | } |
13441 | | #endif |
13442 | | |
13443 | | ssl->options.connectState = FIRST_REPLY_FOURTH; |
13444 | | WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH"); |
13445 | | FALL_THROUGH; |
13446 | | |
13447 | | case FIRST_REPLY_FOURTH: |
13448 | | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
13449 | | #ifdef WOLFSSL_CHECK_ALERT_ON_ERR |
13450 | | ProcessReplyEx(ssl, 1); /* See if an alert was sent. */ |
13451 | | #endif |
13452 | | WOLFSSL_ERROR(ssl->error); |
13453 | | return WOLFSSL_FATAL_ERROR; |
13454 | | } |
13455 | | WOLFSSL_MSG("sent: finished"); |
13456 | | |
13457 | | #ifdef WOLFSSL_DTLS13 |
13458 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13459 | | WOLFSSL_MSG("connect state: WAIT_FINISHED_ACK"); |
13460 | | FALL_THROUGH; |
13461 | | |
13462 | | case WAIT_FINISHED_ACK: |
13463 | | if (ssl->options.dtls) { |
13464 | | while (ssl->options.serverState != SERVER_FINISHED_ACKED) { |
13465 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13466 | | WOLFSSL_ERROR(ssl->error); |
13467 | | return WOLFSSL_FATAL_ERROR; |
13468 | | } |
13469 | | |
13470 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13471 | | WOLFSSL_ERROR(ssl->error); |
13472 | | return WOLFSSL_FATAL_ERROR; |
13473 | | } |
13474 | | } |
13475 | | } |
13476 | | #endif /* WOLFSSL_DTLS13 */ |
13477 | | ssl->options.connectState = FINISHED_DONE; |
13478 | | WOLFSSL_MSG("connect state: FINISHED_DONE"); |
13479 | | FALL_THROUGH; |
13480 | | |
13481 | | case FINISHED_DONE: |
13482 | | #ifndef NO_HANDSHAKE_DONE_CB |
13483 | | if (ssl->hsDoneCb != NULL) { |
13484 | | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
13485 | | if (cbret < 0) { |
13486 | | ssl->error = cbret; |
13487 | | WOLFSSL_ERROR_VERBOSE(ssl->error); |
13488 | | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
13489 | | return WOLFSSL_FATAL_ERROR; |
13490 | | } |
13491 | | } |
13492 | | #endif /* NO_HANDSHAKE_DONE_CB */ |
13493 | | |
13494 | | if (!ssl->options.keepResources) { |
13495 | | FreeHandshakeResources(ssl); |
13496 | | } |
13497 | | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
13498 | | /* Free the remaining async context if not using it for crypto */ |
13499 | | FreeAsyncCtx(ssl, 1); |
13500 | | #endif |
13501 | | |
13502 | | ssl->error = 0; /* clear the error */ |
13503 | | |
13504 | | WOLFSSL_LEAVE("wolfSSL_connect_TLSv13", WOLFSSL_SUCCESS); |
13505 | | return WOLFSSL_SUCCESS; |
13506 | | |
13507 | | default: |
13508 | | WOLFSSL_MSG("Unknown connect state ERROR"); |
13509 | | return WOLFSSL_FATAL_ERROR; /* unknown connect state */ |
13510 | | } |
13511 | | } |
13512 | | #endif |
13513 | | |
13514 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
13515 | | /* Send a cookie with the HelloRetryRequest to avoid storing state. |
13516 | | * |
13517 | | * ssl SSL/TLS object. |
13518 | | * secret Secret to use when generating integrity check for cookie. |
13519 | | * A value of NULL indicates to generate a new random secret. |
13520 | | * secretSz Size of secret data in bytes. |
13521 | | * Use a value of 0 to indicate use of default size. |
13522 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when |
13523 | | * called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
13524 | | */ |
13525 | | int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret, |
13526 | | unsigned int secretSz) |
13527 | | { |
13528 | | int ret; |
13529 | | |
13530 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13531 | | return BAD_FUNC_ARG; |
13532 | | #ifndef NO_WOLFSSL_SERVER |
13533 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13534 | | return SIDE_ERROR; |
13535 | | |
13536 | | if (secretSz == 0) { |
13537 | | #if !defined(NO_SHA) && defined(NO_SHA256) |
13538 | | secretSz = WC_SHA_DIGEST_SIZE; |
13539 | | #endif /* NO_SHA */ |
13540 | | #ifndef NO_SHA256 |
13541 | | secretSz = WC_SHA256_DIGEST_SIZE; |
13542 | | #endif /* NO_SHA256 */ |
13543 | | } |
13544 | | |
13545 | | if (secretSz != ssl->buffers.tls13CookieSecret.length) { |
13546 | | byte* newSecret; |
13547 | | |
13548 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
13549 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
13550 | | ssl->buffers.tls13CookieSecret.length); |
13551 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, |
13552 | | ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
13553 | | } |
13554 | | |
13555 | | newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
13556 | | DYNAMIC_TYPE_COOKIE_PWD); |
13557 | | if (newSecret == NULL) { |
13558 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
13559 | | ssl->buffers.tls13CookieSecret.length = 0; |
13560 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
13561 | | return MEMORY_ERROR; |
13562 | | } |
13563 | | ssl->buffers.tls13CookieSecret.buffer = newSecret; |
13564 | | ssl->buffers.tls13CookieSecret.length = secretSz; |
13565 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
13566 | | wc_MemZero_Add("wolfSSL_send_hrr_cookie secret", |
13567 | | ssl->buffers.tls13CookieSecret.buffer, |
13568 | | ssl->buffers.tls13CookieSecret.length); |
13569 | | #endif |
13570 | | } |
13571 | | |
13572 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
13573 | | if (secret == NULL) { |
13574 | | ret = wc_RNG_GenerateBlock(ssl->rng, |
13575 | | ssl->buffers.tls13CookieSecret.buffer, secretSz); |
13576 | | if (ret < 0) |
13577 | | return ret; |
13578 | | } |
13579 | | else |
13580 | | XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz); |
13581 | | |
13582 | | ssl->options.sendCookie = 1; |
13583 | | |
13584 | | ret = WOLFSSL_SUCCESS; |
13585 | | #else |
13586 | | (void)secret; |
13587 | | (void)secretSz; |
13588 | | |
13589 | | ret = SIDE_ERROR; |
13590 | | #endif |
13591 | | |
13592 | | return ret; |
13593 | | } |
13594 | | |
13595 | | int wolfSSL_disable_hrr_cookie(WOLFSSL* ssl) |
13596 | | { |
13597 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13598 | | return BAD_FUNC_ARG; |
13599 | | |
13600 | | #ifdef NO_WOLFSSL_SERVER |
13601 | | return SIDE_ERROR; |
13602 | | #else |
13603 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13604 | | return SIDE_ERROR; |
13605 | | |
13606 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
13607 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
13608 | | ssl->buffers.tls13CookieSecret.length); |
13609 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap, |
13610 | | DYNAMIC_TYPE_COOKIE_PWD); |
13611 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
13612 | | ssl->buffers.tls13CookieSecret.length = 0; |
13613 | | } |
13614 | | |
13615 | | ssl->options.sendCookie = 0; |
13616 | | return WOLFSSL_SUCCESS; |
13617 | | #endif /* NO_WOLFSSL_SERVER */ |
13618 | | } |
13619 | | |
13620 | | #endif /* defined(WOLFSSL_SEND_HRR_COOKIE) */ |
13621 | | |
13622 | | #ifdef HAVE_SUPPORTED_CURVES |
13623 | | /* Create a key share entry from group. |
13624 | | * Generates a key pair. |
13625 | | * |
13626 | | * ssl The SSL/TLS object. |
13627 | | * group The named group. |
13628 | | * returns 0 on success, otherwise failure. |
13629 | | * for async can return WC_PENDING_E and should be called again |
13630 | | */ |
13631 | | int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group) |
13632 | 0 | { |
13633 | 0 | int ret; |
13634 | |
|
13635 | 0 | if (ssl == NULL) |
13636 | 0 | return BAD_FUNC_ARG; |
13637 | | |
13638 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13639 | | ret = wolfSSL_AsyncPop(ssl, NULL); |
13640 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
13641 | | /* Check for error */ |
13642 | | if (ret < 0) |
13643 | | return ret; |
13644 | | } |
13645 | | #endif |
13646 | | |
13647 | | #if defined(WOLFSSL_HAVE_MLKEM) |
13648 | | if (WOLFSSL_NAMED_GROUP_IS_PQC(group) || |
13649 | | WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group)) { |
13650 | | |
13651 | | if (ssl->ctx != NULL && ssl->ctx->method != NULL && |
13652 | | !IsAtLeastTLSv1_3(ssl->version)) { |
13653 | | return BAD_FUNC_ARG; |
13654 | | } |
13655 | | |
13656 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13657 | | /* If I am the server of a KEM connection, do not do keygen because |
13658 | | * I'm going to encapsulate with the client's public key. Note that |
13659 | | * I might be the client and ssl->option.side has not been properly |
13660 | | * set yet. In that case the KeyGen operation will be deferred to |
13661 | | * connection time. */ |
13662 | | return WOLFSSL_SUCCESS; |
13663 | | } |
13664 | | } |
13665 | | #endif |
13666 | | #if defined(NO_TLS) |
13667 | | (void)ret; |
13668 | | (void)group; |
13669 | | #else |
13670 | 0 | ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions); |
13671 | 0 | if (ret != 0) |
13672 | 0 | return ret; |
13673 | 0 | #endif /* NO_TLS */ |
13674 | 0 | return WOLFSSL_SUCCESS; |
13675 | 0 | } |
13676 | | |
13677 | | /* Send no key share entries - use HelloRetryRequest to negotiate shared group. |
13678 | | * |
13679 | | * ssl The SSL/TLS object. |
13680 | | * returns 0 on success, otherwise failure. |
13681 | | */ |
13682 | | int wolfSSL_NoKeyShares(WOLFSSL* ssl) |
13683 | 0 | { |
13684 | 0 | int ret; |
13685 | |
|
13686 | 0 | if (ssl == NULL) |
13687 | 0 | return BAD_FUNC_ARG; |
13688 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
13689 | 0 | return SIDE_ERROR; |
13690 | | #if defined(NO_TLS) |
13691 | | (void)ret; |
13692 | | #else |
13693 | 0 | ret = TLSX_KeyShare_Empty(ssl); |
13694 | 0 | if (ret != 0) |
13695 | 0 | return ret; |
13696 | 0 | #endif /* NO_TLS */ |
13697 | 0 | return WOLFSSL_SUCCESS; |
13698 | 0 | } |
13699 | | #endif |
13700 | | |
13701 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
13702 | | int wolfSSL_UseCKS(WOLFSSL* ssl, byte *sigSpec, word16 sigSpecSz) |
13703 | | { |
13704 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->ctx->method->version) || |
13705 | | sigSpec == NULL || sigSpecSz == 0) |
13706 | | return BAD_FUNC_ARG; |
13707 | | |
13708 | | ssl->sigSpec = sigSpec; |
13709 | | ssl->sigSpecSz = sigSpecSz; |
13710 | | return WOLFSSL_SUCCESS; |
13711 | | } |
13712 | | |
13713 | | int wolfSSL_CTX_UseCKS(WOLFSSL_CTX* ctx, byte *sigSpec, word16 sigSpecSz) |
13714 | | { |
13715 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version) || |
13716 | | sigSpec == NULL || sigSpecSz == 0) |
13717 | | return BAD_FUNC_ARG; |
13718 | | |
13719 | | ctx->sigSpec = sigSpec; |
13720 | | ctx->sigSpecSz = sigSpecSz; |
13721 | | return WOLFSSL_SUCCESS; |
13722 | | } |
13723 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
13724 | | |
13725 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
13726 | | * |
13727 | | * ctx The SSL/TLS CTX object. |
13728 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13729 | | */ |
13730 | | int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx) |
13731 | 0 | { |
13732 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13733 | 0 | return BAD_FUNC_ARG; |
13734 | 0 | if (ctx->method->side == WOLFSSL_CLIENT_END) |
13735 | 0 | return SIDE_ERROR; |
13736 | | |
13737 | | #ifdef HAVE_SESSION_TICKET |
13738 | | ctx->noTicketTls13 = 1; |
13739 | | #endif |
13740 | | |
13741 | 0 | return 0; |
13742 | 0 | } |
13743 | | |
13744 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
13745 | | * |
13746 | | * ssl The SSL/TLS object. |
13747 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on |
13748 | | * a client and 0 on success. |
13749 | | */ |
13750 | | int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl) |
13751 | 0 | { |
13752 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13753 | 0 | return BAD_FUNC_ARG; |
13754 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13755 | 0 | return SIDE_ERROR; |
13756 | | |
13757 | | #ifdef HAVE_SESSION_TICKET |
13758 | | ssl->options.noTicketTls13 = 1; |
13759 | | #endif |
13760 | | |
13761 | 0 | return 0; |
13762 | 0 | } |
13763 | | |
13764 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
13765 | | * |
13766 | | * ctx The SSL/TLS CTX object. |
13767 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13768 | | */ |
13769 | | int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx) |
13770 | 0 | { |
13771 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13772 | 0 | return BAD_FUNC_ARG; |
13773 | | |
13774 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13775 | | ctx->noPskDheKe = 1; |
13776 | | #endif |
13777 | | |
13778 | 0 | return 0; |
13779 | 0 | } |
13780 | | |
13781 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
13782 | | * |
13783 | | * ssl The SSL/TLS object. |
13784 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
13785 | | * success. |
13786 | | */ |
13787 | | int wolfSSL_no_dhe_psk(WOLFSSL* ssl) |
13788 | 0 | { |
13789 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13790 | 0 | return BAD_FUNC_ARG; |
13791 | | |
13792 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13793 | | ssl->options.noPskDheKe = 1; |
13794 | | #endif |
13795 | | |
13796 | 0 | return 0; |
13797 | 0 | } |
13798 | | |
13799 | | #ifdef HAVE_SUPPORTED_CURVES |
13800 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
13801 | | * |
13802 | | * ctx The SSL/TLS CTX object. |
13803 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
13804 | | */ |
13805 | | int wolfSSL_CTX_only_dhe_psk(WOLFSSL_CTX* ctx) |
13806 | 0 | { |
13807 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13808 | 0 | return BAD_FUNC_ARG; |
13809 | | |
13810 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13811 | | ctx->onlyPskDheKe = 1; |
13812 | | #endif |
13813 | | |
13814 | 0 | return 0; |
13815 | 0 | } |
13816 | | |
13817 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
13818 | | * |
13819 | | * ssl The SSL/TLS object. |
13820 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
13821 | | * success. |
13822 | | */ |
13823 | | int wolfSSL_only_dhe_psk(WOLFSSL* ssl) |
13824 | 0 | { |
13825 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13826 | 0 | return BAD_FUNC_ARG; |
13827 | | |
13828 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13829 | | ssl->options.onlyPskDheKe = 1; |
13830 | | #endif |
13831 | | |
13832 | 0 | return 0; |
13833 | 0 | } |
13834 | | #endif /* HAVE_SUPPORTED_CURVES */ |
13835 | | |
13836 | | int Tls13UpdateKeys(WOLFSSL* ssl) |
13837 | 0 | { |
13838 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13839 | 0 | return BAD_FUNC_ARG; |
13840 | | |
13841 | | #ifdef WOLFSSL_DTLS13 |
13842 | | /* we are already waiting for the ack of a sent key update message. We can't |
13843 | | send another one before receiving its ack. Either wolfSSL_update_keys() |
13844 | | was invoked multiple times over a short period of time or we replied to a |
13845 | | KeyUpdate with update request. We'll just ignore sending this |
13846 | | KeyUpdate. */ |
13847 | | /* TODO: add WOLFSSL_ERROR_ALREADY_IN_PROGRESS type of error here */ |
13848 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) |
13849 | | return 0; |
13850 | | #endif /* WOLFSSL_DTLS13 */ |
13851 | | |
13852 | 0 | return SendTls13KeyUpdate(ssl); |
13853 | 0 | } |
13854 | | |
13855 | | /* Update the keys for encryption and decryption. |
13856 | | * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then |
13857 | | * calling wolfSSL_write() will have the message sent when ready. |
13858 | | * |
13859 | | * ssl The SSL/TLS object. |
13860 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
13861 | | * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write, |
13862 | | * WOLFSSL_SUCCESS on success and otherwise failure. |
13863 | | */ |
13864 | | int wolfSSL_update_keys(WOLFSSL* ssl) |
13865 | 0 | { |
13866 | 0 | int ret; |
13867 | 0 | ret = Tls13UpdateKeys(ssl); |
13868 | 0 | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
13869 | 0 | ret = WOLFSSL_ERROR_WANT_WRITE; |
13870 | 0 | else if (ret == 0) |
13871 | 0 | ret = WOLFSSL_SUCCESS; |
13872 | 0 | return ret; |
13873 | 0 | } |
13874 | | |
13875 | | /* Whether a response is waiting for key update request. |
13876 | | * |
13877 | | * ssl The SSL/TLS object. |
13878 | | * required 0 when no key update response required. |
13879 | | * 1 when no key update response required. |
13880 | | * return 0 on success. |
13881 | | * return BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3 |
13882 | | */ |
13883 | | int wolfSSL_key_update_response(WOLFSSL* ssl, int* required) |
13884 | 0 | { |
13885 | 0 | if (required == NULL || ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13886 | 0 | return BAD_FUNC_ARG; |
13887 | | |
13888 | 0 | *required = ssl->keys.updateResponseReq; |
13889 | |
|
13890 | 0 | return 0; |
13891 | 0 | } |
13892 | | |
13893 | | #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
13894 | | /* Allow post-handshake authentication in TLS v1.3 connections. |
13895 | | * |
13896 | | * ctx The SSL/TLS CTX object. |
13897 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and |
13898 | | * 0 on success. |
13899 | | */ |
13900 | | int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx) |
13901 | | { |
13902 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
13903 | | return BAD_FUNC_ARG; |
13904 | | if (ctx->method->side == WOLFSSL_SERVER_END) |
13905 | | return SIDE_ERROR; |
13906 | | |
13907 | | ctx->postHandshakeAuth = 1; |
13908 | | |
13909 | | return 0; |
13910 | | } |
13911 | | |
13912 | | /* Allow post-handshake authentication in TLS v1.3 connection. |
13913 | | * |
13914 | | * ssl The SSL/TLS object. |
13915 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
13916 | | * SIDE_ERROR when not a client and 0 on success. |
13917 | | */ |
13918 | | int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl) |
13919 | | { |
13920 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13921 | | return BAD_FUNC_ARG; |
13922 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
13923 | | return SIDE_ERROR; |
13924 | | |
13925 | | ssl->options.postHandshakeAuth = 1; |
13926 | | |
13927 | | return 0; |
13928 | | } |
13929 | | |
13930 | | /* Request a certificate of the client. |
13931 | | * Can be called any time after handshake completion. |
13932 | | * A maximum of 256 requests can be sent on a connection. |
13933 | | * |
13934 | | * ssl SSL/TLS object. |
13935 | | */ |
13936 | | int wolfSSL_request_certificate(WOLFSSL* ssl) |
13937 | | { |
13938 | | int ret; |
13939 | | #ifndef NO_WOLFSSL_SERVER |
13940 | | CertReqCtx* certReqCtx; |
13941 | | #endif |
13942 | | |
13943 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13944 | | return BAD_FUNC_ARG; |
13945 | | #ifndef NO_WOLFSSL_SERVER |
13946 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13947 | | return SIDE_ERROR; |
13948 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
13949 | | return NOT_READY_ERROR; |
13950 | | if (!ssl->options.postHandshakeAuth) |
13951 | | return POST_HAND_AUTH_ERROR; |
13952 | | |
13953 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap, |
13954 | | DYNAMIC_TYPE_TMP_BUFFER); |
13955 | | if (certReqCtx == NULL) |
13956 | | return MEMORY_E; |
13957 | | XMEMSET(certReqCtx, 0, sizeof(CertReqCtx)); |
13958 | | certReqCtx->next = ssl->certReqCtx; |
13959 | | certReqCtx->len = 1; |
13960 | | if (certReqCtx->next != NULL) |
13961 | | certReqCtx->ctx = certReqCtx->next->ctx + 1; |
13962 | | ssl->certReqCtx = certReqCtx; |
13963 | | |
13964 | | ssl->msgsReceived.got_certificate = 0; |
13965 | | ssl->msgsReceived.got_certificate_verify = 0; |
13966 | | ssl->msgsReceived.got_finished = 0; |
13967 | | |
13968 | | ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len); |
13969 | | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
13970 | | ret = WOLFSSL_ERROR_WANT_WRITE; |
13971 | | else if (ret == 0) |
13972 | | ret = WOLFSSL_SUCCESS; |
13973 | | #else |
13974 | | ret = SIDE_ERROR; |
13975 | | #endif |
13976 | | |
13977 | | return ret; |
13978 | | } |
13979 | | #endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */ |
13980 | | |
13981 | | #if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
13982 | | /* Get the preferred key exchange group. |
13983 | | * |
13984 | | * ssl The SSL/TLS object. |
13985 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, |
13986 | | * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete |
13987 | | * and group number on success. |
13988 | | */ |
13989 | | int wolfSSL_preferred_group(WOLFSSL* ssl) |
13990 | 0 | { |
13991 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13992 | 0 | return BAD_FUNC_ARG; |
13993 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13994 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
13995 | 0 | return SIDE_ERROR; |
13996 | 0 | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
13997 | 0 | return NOT_READY_ERROR; |
13998 | | |
13999 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
14000 | | /* Return supported groups only. */ |
14001 | 0 | return TLSX_SupportedCurve_Preferred(ssl, 1); |
14002 | | #else |
14003 | | return 0; |
14004 | | #endif |
14005 | | #else |
14006 | | return SIDE_ERROR; |
14007 | | #endif |
14008 | 0 | } |
14009 | | #endif |
14010 | | |
14011 | | #ifndef NO_PSK |
14012 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14013 | | * against context object. |
14014 | | * |
14015 | | * @param [in, out] ctx SSL/TLS context object. |
14016 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14017 | | */ |
14018 | | void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, |
14019 | | wc_psk_client_cs_callback cb) |
14020 | | { |
14021 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_cs_callback"); |
14022 | | |
14023 | | if (ctx == NULL) |
14024 | | return; |
14025 | | |
14026 | | ctx->havePSK = 1; |
14027 | | ctx->client_psk_cs_cb = cb; |
14028 | | } |
14029 | | |
14030 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14031 | | * against SSL object. |
14032 | | * |
14033 | | * @param [in, out] ssl SSL/TLS object. |
14034 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14035 | | */ |
14036 | | void wolfSSL_set_psk_client_cs_callback(WOLFSSL* ssl, |
14037 | | wc_psk_client_cs_callback cb) |
14038 | | { |
14039 | | byte haveRSA = 1; |
14040 | | int keySz = 0; |
14041 | | |
14042 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_cs_callback"); |
14043 | | |
14044 | | if (ssl == NULL) |
14045 | | return; |
14046 | | |
14047 | | ssl->options.havePSK = 1; |
14048 | | ssl->options.client_psk_cs_cb = cb; |
14049 | | |
14050 | | #ifdef NO_RSA |
14051 | | haveRSA = 0; |
14052 | | #endif |
14053 | | #ifndef NO_CERTS |
14054 | | keySz = ssl->buffers.keySz; |
14055 | | #endif |
14056 | | if (AllocateSuites(ssl) != 0) |
14057 | | return; |
14058 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14059 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14060 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14061 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14062 | | } |
14063 | | |
14064 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14065 | | * against context object. |
14066 | | * |
14067 | | * @param [in, out] ctx SSL/TLS context object. |
14068 | | * @param [in] cb Client PSK callback returning cipher suite. |
14069 | | */ |
14070 | | void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, |
14071 | | wc_psk_client_tls13_callback cb) |
14072 | | { |
14073 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_tls13_callback"); |
14074 | | |
14075 | | if (ctx == NULL) |
14076 | | return; |
14077 | | |
14078 | | ctx->havePSK = 1; |
14079 | | ctx->client_psk_tls13_cb = cb; |
14080 | | } |
14081 | | |
14082 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14083 | | * against SSL object. |
14084 | | * |
14085 | | * @param [in, out] ssl SSL/TLS object. |
14086 | | * @param [in] cb Client PSK callback returning cipher suite. |
14087 | | */ |
14088 | | void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl, |
14089 | | wc_psk_client_tls13_callback cb) |
14090 | | { |
14091 | | byte haveRSA = 1; |
14092 | | int keySz = 0; |
14093 | | |
14094 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_tls13_callback"); |
14095 | | |
14096 | | if (ssl == NULL) |
14097 | | return; |
14098 | | |
14099 | | ssl->options.havePSK = 1; |
14100 | | ssl->options.client_psk_tls13_cb = cb; |
14101 | | |
14102 | | #ifdef NO_RSA |
14103 | | haveRSA = 0; |
14104 | | #endif |
14105 | | #ifndef NO_CERTS |
14106 | | keySz = ssl->buffers.keySz; |
14107 | | #endif |
14108 | | if (AllocateSuites(ssl) != 0) |
14109 | | return; |
14110 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14111 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14112 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14113 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14114 | | } |
14115 | | |
14116 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14117 | | * against context object. |
14118 | | * |
14119 | | * @param [in, out] ctx SSL/TLS context object. |
14120 | | * @param [in] cb Server PSK callback returning cipher suite. |
14121 | | */ |
14122 | | void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, |
14123 | | wc_psk_server_tls13_callback cb) |
14124 | | { |
14125 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_tls13_callback"); |
14126 | | if (ctx == NULL) |
14127 | | return; |
14128 | | ctx->havePSK = 1; |
14129 | | ctx->server_psk_tls13_cb = cb; |
14130 | | } |
14131 | | |
14132 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14133 | | * against SSL object. |
14134 | | * |
14135 | | * @param [in, out] ssl SSL/TLS object. |
14136 | | * @param [in] cb Server PSK callback returning cipher suite. |
14137 | | */ |
14138 | | void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl, |
14139 | | wc_psk_server_tls13_callback cb) |
14140 | | { |
14141 | | byte haveRSA = 1; |
14142 | | int keySz = 0; |
14143 | | |
14144 | | WOLFSSL_ENTER("wolfSSL_set_psk_server_tls13_callback"); |
14145 | | if (ssl == NULL) |
14146 | | return; |
14147 | | |
14148 | | ssl->options.havePSK = 1; |
14149 | | ssl->options.server_psk_tls13_cb = cb; |
14150 | | |
14151 | | #ifdef NO_RSA |
14152 | | haveRSA = 0; |
14153 | | #endif |
14154 | | #ifndef NO_CERTS |
14155 | | keySz = ssl->buffers.keySz; |
14156 | | #endif |
14157 | | if (AllocateSuites(ssl) != 0) |
14158 | | return; |
14159 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14160 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14161 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14162 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14163 | | } |
14164 | | |
14165 | | /* Get name of first supported cipher suite that uses the hash indicated. |
14166 | | * |
14167 | | * @param [in] ssl SSL/TLS object. |
14168 | | * @param [in] hash Name of hash algorithm. e.g. "SHA256", "SHA384" |
14169 | | * @return Name of cipher suite. |
14170 | | * @return NULL on failure. |
14171 | | */ |
14172 | | const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash) |
14173 | | { |
14174 | | const char* name = NULL; |
14175 | | byte mac = no_mac; |
14176 | | int i; |
14177 | | const Suites* suites = WOLFSSL_SUITES(ssl); |
14178 | | |
14179 | | if (XSTRCMP(hash, "SHA256") == 0) { |
14180 | | mac = sha256_mac; |
14181 | | } |
14182 | | else if (XSTRCMP(hash, "SHA384") == 0) { |
14183 | | mac = sha384_mac; |
14184 | | } |
14185 | | if (mac != no_mac) { |
14186 | | for (i = 0; i < suites->suiteSz; i += 2) { |
14187 | | if (SuiteMac(suites->suites + i) == mac) { |
14188 | | name = GetCipherNameInternal(suites->suites[i + 0], |
14189 | | suites->suites[i + 1]); |
14190 | | break; |
14191 | | } |
14192 | | } |
14193 | | } |
14194 | | return name; |
14195 | | } |
14196 | | #endif /* !NO_PSK */ |
14197 | | |
14198 | | |
14199 | | #ifndef NO_WOLFSSL_SERVER |
14200 | | |
14201 | | /* The server accepting a connection from a client. |
14202 | | * The protocol version is expecting to be TLS v1.3. |
14203 | | * If the client downgrades, and older versions of the protocol are compiled |
14204 | | * in, the server will fallback to wolfSSL_accept(). |
14205 | | * Please see note at top of README if you get an error from accept. |
14206 | | * |
14207 | | * ssl The SSL/TLS object. |
14208 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
14209 | | * unrecoverable error occurs and 0 otherwise. |
14210 | | * For more error information use wolfSSL_get_error(). |
14211 | | */ |
14212 | | int wolfSSL_accept_TLSv13(WOLFSSL* ssl) |
14213 | 0 | { |
14214 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14215 | | word16 havePSK = 0; |
14216 | | #endif |
14217 | 0 | int ret = 0; |
14218 | |
|
14219 | 0 | WOLFSSL_ENTER("wolfSSL_accept_TLSv13"); |
14220 | |
|
14221 | 0 | #ifdef HAVE_ERRNO_H |
14222 | 0 | errno = 0; |
14223 | 0 | #endif |
14224 | |
|
14225 | 0 | if (ssl == NULL) |
14226 | 0 | return WOLFSSL_FATAL_ERROR; |
14227 | | |
14228 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14229 | | havePSK = ssl->options.havePSK; |
14230 | | #endif |
14231 | | |
14232 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) { |
14233 | 0 | ssl->error = SIDE_ERROR; |
14234 | 0 | WOLFSSL_ERROR(ssl->error); |
14235 | 0 | return WOLFSSL_FATAL_ERROR; |
14236 | 0 | } |
14237 | | |
14238 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
14239 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
14240 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
14241 | 0 | return ret; |
14242 | 0 | } |
14243 | | |
14244 | | #ifdef WOLFSSL_DTLS |
14245 | | if (ssl->version.major == DTLS_MAJOR) { |
14246 | | ssl->options.dtls = 1; |
14247 | | if (!IsDtlsNotSctpMode(ssl) || !ssl->options.sendCookie) |
14248 | | ssl->options.dtlsStateful = 1; |
14249 | | } |
14250 | | #endif |
14251 | | |
14252 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
14253 | | if ((ssl->AcceptFilter != NULL) && |
14254 | | ((ssl->options.acceptState == TLS13_ACCEPT_BEGIN) |
14255 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14256 | | || (ssl->options.acceptState == TLS13_ACCEPT_BEGIN_RENEG) |
14257 | | #endif |
14258 | | )) |
14259 | | { |
14260 | | wolfSSL_netfilter_decision_t res; |
14261 | | if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) == |
14262 | | WOLFSSL_SUCCESS) && |
14263 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
14264 | | ssl->error = SOCKET_FILTERED_E; |
14265 | | WOLFSSL_ERROR(ssl->error); |
14266 | | return WOLFSSL_FATAL_ERROR; |
14267 | | } |
14268 | | } |
14269 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
14270 | | |
14271 | 0 | #ifndef NO_CERTS |
14272 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14273 | | if (!havePSK) |
14274 | | #endif |
14275 | 0 | { |
14276 | | #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \ |
14277 | | defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY) |
14278 | | if (ssl->ctx->certSetupCb != NULL) { |
14279 | | WOLFSSL_MSG("CertSetupCb set. server cert and " |
14280 | | "key not checked"); |
14281 | | } |
14282 | | else |
14283 | | #endif |
14284 | 0 | { |
14285 | 0 | if (!ssl->buffers.certificate || |
14286 | 0 | !ssl->buffers.certificate->buffer) { |
14287 | |
|
14288 | 0 | WOLFSSL_MSG("accept error: server cert required"); |
14289 | 0 | ssl->error = NO_PRIVATE_KEY; |
14290 | 0 | WOLFSSL_ERROR(ssl->error); |
14291 | 0 | return WOLFSSL_FATAL_ERROR; |
14292 | 0 | } |
14293 | | |
14294 | 0 | if (!ssl->buffers.key || !ssl->buffers.key->buffer) { |
14295 | | /* allow no private key if using existing key */ |
14296 | 0 | #ifdef WOLF_PRIVATE_KEY_ID |
14297 | 0 | if (ssl->devId != INVALID_DEVID |
14298 | | #ifdef HAVE_PK_CALLBACKS |
14299 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
14300 | | #endif |
14301 | 0 | ) { |
14302 | 0 | WOLFSSL_MSG("Allowing no server private key (external)"); |
14303 | 0 | } |
14304 | 0 | else |
14305 | 0 | #endif |
14306 | 0 | { |
14307 | 0 | WOLFSSL_MSG("accept error: server key required"); |
14308 | 0 | ssl->error = NO_PRIVATE_KEY; |
14309 | 0 | WOLFSSL_ERROR(ssl->error); |
14310 | 0 | return WOLFSSL_FATAL_ERROR; |
14311 | 0 | } |
14312 | 0 | } |
14313 | 0 | } |
14314 | 0 | } |
14315 | 0 | #endif /* NO_CERTS */ |
14316 | | |
14317 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
14318 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14319 | | /* do not send buffered or advance state if last error was an |
14320 | | async pending operation */ |
14321 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
14322 | | #endif |
14323 | 0 | ) { |
14324 | | |
14325 | | /* fragOffset is non-zero when sending fragments. On the last |
14326 | | * fragment, fragOffset is zero again, and the state can be |
14327 | | * advanced. */ |
14328 | 0 | int advanceState = |
14329 | 0 | (ssl->options.acceptState == TLS13_ACCEPT_CLIENT_HELLO_DONE || |
14330 | 0 | ssl->options.acceptState == |
14331 | 0 | TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE || |
14332 | 0 | ssl->options.acceptState == TLS13_ACCEPT_SECOND_REPLY_DONE || |
14333 | 0 | ssl->options.acceptState == TLS13_SERVER_HELLO_SENT || |
14334 | 0 | ssl->options.acceptState == TLS13_ACCEPT_THIRD_REPLY_DONE || |
14335 | 0 | ssl->options.acceptState == TLS13_SERVER_EXTENSIONS_SENT || |
14336 | 0 | ssl->options.acceptState == TLS13_CERT_REQ_SENT || |
14337 | 0 | ssl->options.acceptState == TLS13_CERT_SENT || |
14338 | 0 | ssl->options.acceptState == TLS13_CERT_VERIFY_SENT || |
14339 | 0 | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_SENT || |
14340 | 0 | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_DONE); |
14341 | |
|
14342 | | #ifdef WOLFSSL_DTLS13 |
14343 | | if (ssl->options.dtls) |
14344 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
14345 | | && !ssl->dtls13SendingAckOrRtx; |
14346 | | #endif /* WOLFSSL_DTLS13 */ |
14347 | |
|
14348 | 0 | ret = SendBuffered(ssl); |
14349 | 0 | if (ret == 0) { |
14350 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
14351 | 0 | if (advanceState) { |
14352 | 0 | ssl->options.acceptState++; |
14353 | 0 | WOLFSSL_MSG("accept state: " |
14354 | 0 | "Advanced from last buffered fragment send"); |
14355 | 0 | #ifdef WOLFSSL_ASYNC_IO |
14356 | 0 | FreeAsyncCtx(ssl, 0); |
14357 | 0 | #endif |
14358 | 0 | } |
14359 | 0 | } |
14360 | 0 | else { |
14361 | 0 | WOLFSSL_MSG("accept state: " |
14362 | 0 | "Not advanced, more fragments to send"); |
14363 | 0 | } |
14364 | |
|
14365 | | #ifdef WOLFSSL_DTLS13 |
14366 | | if (ssl->options.dtls) |
14367 | | ssl->dtls13SendingAckOrRtx = 0; |
14368 | | #endif /* WOLFSSL_DTLS13 */ |
14369 | |
|
14370 | 0 | } |
14371 | 0 | else { |
14372 | 0 | ssl->error = ret; |
14373 | 0 | WOLFSSL_ERROR(ssl->error); |
14374 | 0 | return WOLFSSL_FATAL_ERROR; |
14375 | 0 | } |
14376 | 0 | } |
14377 | | |
14378 | 0 | ret = RetrySendAlert(ssl); |
14379 | 0 | if (ret != 0) { |
14380 | 0 | ssl->error = ret; |
14381 | 0 | WOLFSSL_ERROR(ssl->error); |
14382 | 0 | return WOLFSSL_FATAL_ERROR; |
14383 | 0 | } |
14384 | | #ifdef WOLFSSL_DTLS13 |
14385 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
14386 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
14387 | | WOLFSSL_ERROR(ssl->error); |
14388 | | return WOLFSSL_FATAL_ERROR; |
14389 | | } |
14390 | | |
14391 | | /* we sent all the fragments. Advance state. */ |
14392 | | ssl->options.acceptState++; |
14393 | | } |
14394 | | #endif /* WOLFSSL_DTLS13 */ |
14395 | | |
14396 | 0 | switch (ssl->options.acceptState) { |
14397 | | |
14398 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14399 | | case TLS13_ACCEPT_BEGIN_RENEG: |
14400 | | #endif |
14401 | 0 | case TLS13_ACCEPT_BEGIN : |
14402 | | /* get client_hello */ |
14403 | |
|
14404 | 0 | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14405 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14406 | 0 | WOLFSSL_ERROR(ssl->error); |
14407 | 0 | return WOLFSSL_FATAL_ERROR; |
14408 | 0 | } |
14409 | |
|
14410 | | #ifdef WOLFSSL_DTLS13 |
14411 | | if (ssl->options.dtls) { |
14412 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14413 | | WOLFSSL_ERROR(ssl->error); |
14414 | | return WOLFSSL_FATAL_ERROR; |
14415 | | } |
14416 | | } |
14417 | | #endif /* WOLFSSL_DTLS13 */ |
14418 | |
|
14419 | 0 | } |
14420 | | |
14421 | 0 | ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE; |
14422 | 0 | WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE"); |
14423 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) |
14424 | 0 | return wolfSSL_accept(ssl); |
14425 | 0 | FALL_THROUGH; |
14426 | |
|
14427 | 0 | case TLS13_ACCEPT_CLIENT_HELLO_DONE : |
14428 | 0 | if (ssl->options.serverState == |
14429 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14430 | 0 | if ((ssl->error = SendTls13ServerHello(ssl, |
14431 | 0 | hello_retry_request)) != 0) { |
14432 | 0 | WOLFSSL_ERROR(ssl->error); |
14433 | 0 | return WOLFSSL_FATAL_ERROR; |
14434 | 0 | } |
14435 | 0 | } |
14436 | | |
14437 | 0 | ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE; |
14438 | 0 | WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE"); |
14439 | 0 | FALL_THROUGH; |
14440 | |
|
14441 | 0 | case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE : |
14442 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
14443 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14444 | | && ssl->options.serverState == |
14445 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14446 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14447 | | WOLFSSL_ERROR(ssl->error); |
14448 | | return WOLFSSL_FATAL_ERROR; |
14449 | | } |
14450 | | ssl->options.sentChangeCipher = 1; |
14451 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
14452 | | } |
14453 | | #endif |
14454 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE; |
14455 | 0 | WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE"); |
14456 | 0 | FALL_THROUGH; |
14457 | |
|
14458 | 0 | case TLS13_ACCEPT_FIRST_REPLY_DONE : |
14459 | 0 | if (ssl->options.serverState == |
14460 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14461 | 0 | ssl->options.clientState = CLIENT_HELLO_RETRY; |
14462 | 0 | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14463 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14464 | 0 | WOLFSSL_ERROR(ssl->error); |
14465 | 0 | return WOLFSSL_FATAL_ERROR; |
14466 | 0 | } |
14467 | |
|
14468 | | #ifdef WOLFSSL_DTLS13 |
14469 | | if (ssl->options.dtls) { |
14470 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14471 | | WOLFSSL_ERROR(ssl->error); |
14472 | | return WOLFSSL_FATAL_ERROR; |
14473 | | } |
14474 | | } |
14475 | | #endif /* WOLFSSL_DTLS13 */ |
14476 | |
|
14477 | 0 | } |
14478 | 0 | } |
14479 | | |
14480 | 0 | ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE; |
14481 | 0 | WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE"); |
14482 | 0 | FALL_THROUGH; |
14483 | |
|
14484 | 0 | case TLS13_ACCEPT_SECOND_REPLY_DONE : |
14485 | |
|
14486 | 0 | if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) { |
14487 | 0 | WOLFSSL_ERROR(ssl->error); |
14488 | 0 | return WOLFSSL_FATAL_ERROR; |
14489 | 0 | } |
14490 | 0 | ssl->options.acceptState = TLS13_SERVER_HELLO_SENT; |
14491 | 0 | WOLFSSL_MSG("accept state SERVER_HELLO_SENT"); |
14492 | 0 | FALL_THROUGH; |
14493 | |
|
14494 | 0 | case TLS13_SERVER_HELLO_SENT : |
14495 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14496 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14497 | | && !ssl->options.sentChangeCipher && !ssl->options.dtls) { |
14498 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14499 | | WOLFSSL_ERROR(ssl->error); |
14500 | | return WOLFSSL_FATAL_ERROR; |
14501 | | } |
14502 | | ssl->options.sentChangeCipher = 1; |
14503 | | } |
14504 | | #endif |
14505 | |
|
14506 | 0 | ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE; |
14507 | 0 | WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE"); |
14508 | 0 | FALL_THROUGH; |
14509 | |
|
14510 | 0 | case TLS13_ACCEPT_THIRD_REPLY_DONE : |
14511 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
14512 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14513 | | if (!ssl->options.noPskDheKe) |
14514 | | #endif |
14515 | 0 | { |
14516 | 0 | ssl->error = TLSX_KeyShare_DeriveSecret(ssl); |
14517 | 0 | if (ssl->error != 0) |
14518 | 0 | return WOLFSSL_FATAL_ERROR; |
14519 | 0 | } |
14520 | 0 | #endif |
14521 | | |
14522 | 0 | if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) { |
14523 | 0 | WOLFSSL_ERROR(ssl->error); |
14524 | 0 | return WOLFSSL_FATAL_ERROR; |
14525 | 0 | } |
14526 | 0 | ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT; |
14527 | 0 | WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT"); |
14528 | 0 | FALL_THROUGH; |
14529 | |
|
14530 | 0 | case TLS13_SERVER_EXTENSIONS_SENT : |
14531 | 0 | #ifndef NO_CERTS |
14532 | 0 | if (!ssl->options.resuming) { |
14533 | 0 | if (ssl->options.verifyPeer |
14534 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14535 | | && !ssl->options.verifyPostHandshake |
14536 | | #endif |
14537 | 0 | ) { |
14538 | 0 | ssl->error = SendTls13CertificateRequest(ssl, NULL, 0); |
14539 | 0 | if (ssl->error != 0) { |
14540 | 0 | WOLFSSL_ERROR(ssl->error); |
14541 | 0 | return WOLFSSL_FATAL_ERROR; |
14542 | 0 | } |
14543 | 0 | } |
14544 | 0 | else { |
14545 | | /* SERVER: Peer auth good if not verifying client. */ |
14546 | 0 | ssl->options.peerAuthGood = 1; |
14547 | 0 | } |
14548 | 0 | } |
14549 | 0 | #endif |
14550 | 0 | ssl->options.acceptState = TLS13_CERT_REQ_SENT; |
14551 | 0 | WOLFSSL_MSG("accept state CERT_REQ_SENT"); |
14552 | 0 | FALL_THROUGH; |
14553 | |
|
14554 | 0 | case TLS13_CERT_REQ_SENT : |
14555 | 0 | #ifndef NO_CERTS |
14556 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14557 | 0 | if ((ssl->error = SendTls13Certificate(ssl)) != 0) { |
14558 | 0 | WOLFSSL_ERROR(ssl->error); |
14559 | 0 | return WOLFSSL_FATAL_ERROR; |
14560 | 0 | } |
14561 | 0 | } |
14562 | 0 | #endif |
14563 | 0 | ssl->options.acceptState = TLS13_CERT_SENT; |
14564 | 0 | WOLFSSL_MSG("accept state CERT_SENT"); |
14565 | 0 | FALL_THROUGH; |
14566 | |
|
14567 | 0 | case TLS13_CERT_SENT : |
14568 | 0 | #if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
14569 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
14570 | 0 | defined(HAVE_DILITHIUM)) |
14571 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14572 | 0 | if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) { |
14573 | 0 | WOLFSSL_ERROR(ssl->error); |
14574 | 0 | return WOLFSSL_FATAL_ERROR; |
14575 | 0 | } |
14576 | 0 | } |
14577 | 0 | #endif |
14578 | 0 | ssl->options.acceptState = TLS13_CERT_VERIFY_SENT; |
14579 | 0 | WOLFSSL_MSG("accept state CERT_VERIFY_SENT"); |
14580 | 0 | FALL_THROUGH; |
14581 | |
|
14582 | 0 | case TLS13_CERT_VERIFY_SENT : |
14583 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
14584 | 0 | WOLFSSL_ERROR(ssl->error); |
14585 | 0 | return WOLFSSL_FATAL_ERROR; |
14586 | 0 | } |
14587 | | |
14588 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT; |
14589 | 0 | WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT"); |
14590 | | #ifdef WOLFSSL_EARLY_DATA |
14591 | | if (ssl->earlyData != no_early_data) { |
14592 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
14593 | | return WOLFSSL_SUCCESS; |
14594 | | } |
14595 | | #endif |
14596 | 0 | FALL_THROUGH; |
14597 | |
|
14598 | 0 | case TLS13_ACCEPT_FINISHED_SENT : |
14599 | | #ifdef HAVE_SESSION_TICKET |
14600 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
14601 | | if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 && |
14602 | | ssl->ctx->ticketEncCb != NULL) { |
14603 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14604 | | WOLFSSL_ERROR(ssl->error); |
14605 | | return WOLFSSL_FATAL_ERROR; |
14606 | | } |
14607 | | ssl->options.ticketsSent = 1; |
14608 | | } |
14609 | | #endif |
14610 | | #endif /* HAVE_SESSION_TICKET */ |
14611 | 0 | ssl->options.acceptState = TLS13_PRE_TICKET_SENT; |
14612 | 0 | WOLFSSL_MSG("accept state TICKET_SENT"); |
14613 | 0 | FALL_THROUGH; |
14614 | |
|
14615 | 0 | case TLS13_PRE_TICKET_SENT : |
14616 | 0 | while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
14617 | 0 | if ( (ssl->error = ProcessReply(ssl)) < 0) { |
14618 | 0 | WOLFSSL_ERROR(ssl->error); |
14619 | 0 | return WOLFSSL_FATAL_ERROR; |
14620 | 0 | } |
14621 | |
|
14622 | | #ifdef WOLFSSL_DTLS13 |
14623 | | if (ssl->options.dtls) { |
14624 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14625 | | WOLFSSL_ERROR(ssl->error); |
14626 | | return WOLFSSL_FATAL_ERROR; |
14627 | | } |
14628 | | } |
14629 | | #endif /* WOLFSSL_DTLS13 */ |
14630 | 0 | } |
14631 | | |
14632 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE; |
14633 | 0 | WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE"); |
14634 | 0 | FALL_THROUGH; |
14635 | |
|
14636 | 0 | case TLS13_ACCEPT_FINISHED_DONE : |
14637 | | /* SERVER: When not resuming and verifying peer but no certificate |
14638 | | * received and not failing when not received then peer auth good. |
14639 | | */ |
14640 | 0 | if (!ssl->options.resuming && ssl->options.verifyPeer && |
14641 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14642 | | !ssl->options.verifyPostHandshake && |
14643 | | #endif |
14644 | 0 | !ssl->options.havePeerCert && !ssl->options.failNoCert) { |
14645 | 0 | ssl->options.peerAuthGood = 1; |
14646 | 0 | } |
14647 | | /* SERVER: check peer authentication. */ |
14648 | 0 | if (!ssl->options.peerAuthGood) { |
14649 | 0 | WOLFSSL_MSG("Client authentication did not happen"); |
14650 | 0 | return WOLFSSL_FATAL_ERROR; |
14651 | 0 | } |
14652 | | #ifdef HAVE_SESSION_TICKET |
14653 | | while (ssl->options.ticketsSent < ssl->options.maxTicketTls13) { |
14654 | | if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb |
14655 | | != NULL) { |
14656 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14657 | | WOLFSSL_ERROR(ssl->error); |
14658 | | return WOLFSSL_FATAL_ERROR; |
14659 | | } |
14660 | | } |
14661 | | ssl->options.ticketsSent++; |
14662 | | |
14663 | | /* only one session ticket is sent on session resumption */ |
14664 | | if (ssl->options.resuming) { |
14665 | | break; |
14666 | | } |
14667 | | } |
14668 | | #endif /* HAVE_SESSION_TICKET */ |
14669 | 0 | ssl->options.acceptState = TLS13_TICKET_SENT; |
14670 | 0 | WOLFSSL_MSG("accept state TICKET_SENT"); |
14671 | 0 | FALL_THROUGH; |
14672 | |
|
14673 | 0 | case TLS13_TICKET_SENT : |
14674 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
14675 | 0 | if (ssl->hsDoneCb) { |
14676 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
14677 | 0 | if (cbret < 0) { |
14678 | 0 | ssl->error = cbret; |
14679 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
14680 | 0 | return WOLFSSL_FATAL_ERROR; |
14681 | 0 | } |
14682 | 0 | } |
14683 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
14684 | | |
14685 | 0 | if (!ssl->options.keepResources) { |
14686 | 0 | FreeHandshakeResources(ssl); |
14687 | 0 | } |
14688 | |
|
14689 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
14690 | | /* Free the remaining async context if not using it for crypto */ |
14691 | 0 | FreeAsyncCtx(ssl, 1); |
14692 | 0 | #endif |
14693 | |
|
14694 | 0 | ssl->error = 0; /* clear the error */ |
14695 | |
|
14696 | 0 | WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS); |
14697 | 0 | return WOLFSSL_SUCCESS; |
14698 | | |
14699 | 0 | default: |
14700 | 0 | WOLFSSL_MSG("Unknown accept state ERROR"); |
14701 | 0 | return WOLFSSL_FATAL_ERROR; |
14702 | 0 | } |
14703 | 0 | } |
14704 | | #endif |
14705 | | |
14706 | | #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET) |
14707 | | /* Server sends a session ticket to the peer. |
14708 | | * |
14709 | | * RFC 8446, section 4.6.1, para 1. |
14710 | | * |
14711 | | * ssl The SSL/TLS object. |
14712 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14713 | | * SIDE_ERROR when not a server, |
14714 | | * NOT_READY_ERROR when handshake not complete, |
14715 | | * WOLFSSL_FATAL_ERROR when creating or sending message fails, and |
14716 | | * WOLFSSL_SUCCESS on success. |
14717 | | */ |
14718 | | int wolfSSL_send_SessionTicket(WOLFSSL* ssl) |
14719 | | { |
14720 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14721 | | return BAD_FUNC_ARG; |
14722 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14723 | | return SIDE_ERROR; |
14724 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14725 | | return NOT_READY_ERROR; |
14726 | | |
14727 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
14728 | | WOLFSSL_ERROR(ssl->error); |
14729 | | return WOLFSSL_FATAL_ERROR; |
14730 | | } |
14731 | | ssl->options.ticketsSent++; |
14732 | | |
14733 | | return WOLFSSL_SUCCESS; |
14734 | | } |
14735 | | #endif |
14736 | | |
14737 | | #ifdef WOLFSSL_EARLY_DATA |
14738 | | /* Sets the maximum amount of early data that can be seen by server when using |
14739 | | * session tickets for resumption. |
14740 | | * A value of zero indicates no early data is to be sent by client using session |
14741 | | * tickets. |
14742 | | * |
14743 | | * ctx The SSL/TLS CTX object. |
14744 | | * sz Maximum size of the early data. |
14745 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
14746 | | * 0 on success. |
14747 | | */ |
14748 | | int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz) |
14749 | | { |
14750 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14751 | | return BAD_FUNC_ARG; |
14752 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14753 | | return SIDE_ERROR; |
14754 | | |
14755 | | ctx->maxEarlyDataSz = sz; |
14756 | | |
14757 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
14758 | | /* 1 on success in OpenSSL*/ |
14759 | | return WOLFSSL_SUCCESS; |
14760 | | #else |
14761 | | return 0; |
14762 | | #endif |
14763 | | } |
14764 | | |
14765 | | /* Sets the maximum amount of early data that a client or server would like |
14766 | | * to exchange. Servers will advertise this value in session tickets sent |
14767 | | * to a client. |
14768 | | * A value of zero indicates no early data will be sent by a client, or |
14769 | | * no early data is accepted by a server (and announced as such in send out |
14770 | | * session tickets). |
14771 | | * |
14772 | | * ssl The SSL/TLS object. |
14773 | | * sz Maximum size of the early data. |
14774 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14775 | | * and 0 on success. |
14776 | | */ |
14777 | | int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz) |
14778 | | { |
14779 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14780 | | return BAD_FUNC_ARG; |
14781 | | |
14782 | | ssl->options.maxEarlyDataSz = sz; |
14783 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
14784 | | /* 1 on success in OpenSSL*/ |
14785 | | return WOLFSSL_SUCCESS; |
14786 | | #else |
14787 | | return 0; |
14788 | | #endif |
14789 | | } |
14790 | | |
14791 | | /* Gets the maximum amount of early data that can be seen by server when using |
14792 | | * session tickets for resumption. |
14793 | | * A value of zero indicates no early data is to be sent by client using session |
14794 | | * tickets. |
14795 | | * |
14796 | | * ctx The SSL/TLS CTX object. |
14797 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
14798 | | * returns the maximum amount of early data to be set |
14799 | | */ |
14800 | | int wolfSSL_CTX_get_max_early_data(WOLFSSL_CTX* ctx) |
14801 | | { |
14802 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14803 | | return BAD_FUNC_ARG; |
14804 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14805 | | return SIDE_ERROR; |
14806 | | |
14807 | | return ctx->maxEarlyDataSz; |
14808 | | } |
14809 | | |
14810 | | /* Gets the maximum amount of early data that can be seen by server when using |
14811 | | * session tickets for resumption. |
14812 | | * A value of zero indicates no early data is to be sent by client using session |
14813 | | * tickets. |
14814 | | * |
14815 | | * ssl The SSL/TLS object. |
14816 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14817 | | * SIDE_ERROR when not a server and |
14818 | | * returns the maximum amount of early data to be set |
14819 | | */ |
14820 | | int wolfSSL_get_max_early_data(WOLFSSL* ssl) |
14821 | | { |
14822 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14823 | | return BAD_FUNC_ARG; |
14824 | | |
14825 | | return ssl->options.maxEarlyDataSz; |
14826 | | } |
14827 | | |
14828 | | /* Write early data to the server. |
14829 | | * |
14830 | | * ssl The SSL/TLS object. |
14831 | | * data Early data to write |
14832 | | * sz The size of the early data in bytes. |
14833 | | * outSz The number of early data bytes written. |
14834 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
14835 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
14836 | | * early data bytes written. |
14837 | | */ |
14838 | | int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz) |
14839 | | { |
14840 | | int ret = 0; |
14841 | | |
14842 | | WOLFSSL_ENTER("wolfSSL_write_early_data"); |
14843 | | |
14844 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
14845 | | return BAD_FUNC_ARG; |
14846 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
14847 | | return BAD_FUNC_ARG; |
14848 | | |
14849 | | #ifndef NO_WOLFSSL_CLIENT |
14850 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
14851 | | return SIDE_ERROR; |
14852 | | |
14853 | | if (ssl->options.handShakeState == NULL_STATE) { |
14854 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
14855 | | ssl->earlyData = expecting_early_data; |
14856 | | ret = wolfSSL_connect_TLSv13(ssl); |
14857 | | if (ret != WOLFSSL_SUCCESS) |
14858 | | return WOLFSSL_FATAL_ERROR; |
14859 | | /* on client side, status is set to rejected */ |
14860 | | /* until sever accepts the early data extension. */ |
14861 | | ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED; |
14862 | | } |
14863 | | if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) { |
14864 | | #ifdef OPENSSL_EXTRA |
14865 | | /* when processed early data exceeds max size */ |
14866 | | if (ssl->session->maxEarlyDataSz > 0 && |
14867 | | (ssl->earlyDataSz + sz > ssl->session->maxEarlyDataSz)) { |
14868 | | ssl->error = TOO_MUCH_EARLY_DATA; |
14869 | | return WOLFSSL_FATAL_ERROR; |
14870 | | } |
14871 | | #endif |
14872 | | ret = SendData(ssl, data, sz); |
14873 | | if (ret > 0) { |
14874 | | *outSz = ret; |
14875 | | /* store amount of processed early data from client */ |
14876 | | ssl->earlyDataSz += ret; |
14877 | | } |
14878 | | } |
14879 | | #else |
14880 | | return SIDE_ERROR; |
14881 | | #endif |
14882 | | |
14883 | | WOLFSSL_LEAVE("wolfSSL_write_early_data", ret); |
14884 | | |
14885 | | if (ret < 0) |
14886 | | ret = WOLFSSL_FATAL_ERROR; |
14887 | | return ret; |
14888 | | } |
14889 | | |
14890 | | /* Read the any early data from the client. |
14891 | | * |
14892 | | * ssl The SSL/TLS object. |
14893 | | * data Buffer to put the early data into. |
14894 | | * sz The size of the buffer in bytes. |
14895 | | * outSz The number of early data bytes read. |
14896 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
14897 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
14898 | | * early data bytes read. |
14899 | | */ |
14900 | | int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz) |
14901 | | { |
14902 | | int ret = 0; |
14903 | | |
14904 | | WOLFSSL_ENTER("wolfSSL_read_early_data"); |
14905 | | |
14906 | | |
14907 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
14908 | | return BAD_FUNC_ARG; |
14909 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
14910 | | return BAD_FUNC_ARG; |
14911 | | |
14912 | | *outSz = 0; |
14913 | | #ifndef NO_WOLFSSL_SERVER |
14914 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14915 | | return SIDE_ERROR; |
14916 | | |
14917 | | if (ssl->options.handShakeState == NULL_STATE) { |
14918 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
14919 | | ssl->earlyData = expecting_early_data; |
14920 | | /* this used to be: ret = wolfSSL_accept_TLSv13(ssl); |
14921 | | * However, wolfSSL_accept_TLSv13() expects a certificate to |
14922 | | * be installed already, which is not the case in servers |
14923 | | * such as HAProxy. They do it after inspecting the ClientHello. |
14924 | | * The common wolfssl_accept() allows that. */ |
14925 | | ret = wolfSSL_accept(ssl); |
14926 | | if (ret <= 0) |
14927 | | return WOLFSSL_FATAL_ERROR; |
14928 | | } |
14929 | | if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) { |
14930 | | ret = ReceiveData(ssl, (byte*)data, (size_t)sz, FALSE); |
14931 | | if (ret > 0) |
14932 | | *outSz = ret; |
14933 | | if (ssl->error == WC_NO_ERR_TRACE(ZERO_RETURN)) { |
14934 | | ssl->error = WOLFSSL_ERROR_NONE; |
14935 | | #ifdef WOLFSSL_DTLS13 |
14936 | | if (ssl->options.dtls) { |
14937 | | ret = Dtls13DoScheduledWork(ssl); |
14938 | | if (ret < 0) { |
14939 | | ssl->error = ret; |
14940 | | WOLFSSL_ERROR(ssl->error); |
14941 | | return WOLFSSL_FATAL_ERROR; |
14942 | | } |
14943 | | } |
14944 | | #endif /* WOLFSSL_DTLS13 */ |
14945 | | } |
14946 | | } |
14947 | | else |
14948 | | ret = 0; |
14949 | | #else |
14950 | | return SIDE_ERROR; |
14951 | | #endif |
14952 | | |
14953 | | WOLFSSL_LEAVE("wolfSSL_read_early_data", ret); |
14954 | | |
14955 | | if (ret < 0) |
14956 | | ret = WOLFSSL_FATAL_ERROR; |
14957 | | return ret; |
14958 | | } |
14959 | | |
14960 | | /* Returns early data status |
14961 | | * |
14962 | | * ssl The SSL/TLS object. |
14963 | | * returns WOLFSSL_EARLY_DATA_ACCEPTED if the data was accepted |
14964 | | * WOLFSSL_EARLY_DATA_REJECTED if the data was rejected |
14965 | | * WOLFSSL_EARLY_DATA_NOT_SENT if no early data was sent |
14966 | | */ |
14967 | | int wolfSSL_get_early_data_status(const WOLFSSL* ssl) |
14968 | | { |
14969 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14970 | | return BAD_FUNC_ARG; |
14971 | | |
14972 | | return ssl->earlyDataStatus; |
14973 | | } |
14974 | | #endif |
14975 | | |
14976 | | #ifdef HAVE_SECRET_CALLBACK |
14977 | | int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx) |
14978 | | { |
14979 | | WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb"); |
14980 | | if (ssl == NULL) |
14981 | | return WOLFSSL_FATAL_ERROR; |
14982 | | |
14983 | | ssl->tls13SecretCb = cb; |
14984 | | ssl->tls13SecretCtx = ctx; |
14985 | | |
14986 | | return WOLFSSL_SUCCESS; |
14987 | | } |
14988 | | |
14989 | | #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE) |
14990 | | int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret, |
14991 | | int secretSz, void* ctx) |
14992 | | { |
14993 | | int i; |
14994 | | const char* str = NULL; |
14995 | | byte clientRandom[RAN_LEN]; |
14996 | | int clientRandomSz; |
14997 | | XFILE fp; |
14998 | | |
14999 | | (void) ctx; |
15000 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15001 | | fp = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "ab"); |
15002 | | if (fp == XBADFILE) { |
15003 | | return BAD_FUNC_ARG; |
15004 | | } |
15005 | | #else |
15006 | | fp = stderr; |
15007 | | #endif |
15008 | | |
15009 | | clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom, |
15010 | | sizeof(clientRandom)); |
15011 | | |
15012 | | if (clientRandomSz <= 0) { |
15013 | | printf("Error getting server random %d\n", clientRandomSz); |
15014 | | return BAD_FUNC_ARG; |
15015 | | } |
15016 | | |
15017 | | #if 0 |
15018 | | printf("TLS Server Secret CB: Rand %d, Secret %d\n", |
15019 | | serverRandomSz, secretSz); |
15020 | | #endif |
15021 | | |
15022 | | switch (id) { |
15023 | | case CLIENT_EARLY_TRAFFIC_SECRET: |
15024 | | str = "CLIENT_EARLY_TRAFFIC_SECRET"; break; |
15025 | | case EARLY_EXPORTER_SECRET: |
15026 | | str = "EARLY_EXPORTER_SECRET"; break; |
15027 | | case CLIENT_HANDSHAKE_TRAFFIC_SECRET: |
15028 | | str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break; |
15029 | | case SERVER_HANDSHAKE_TRAFFIC_SECRET: |
15030 | | str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break; |
15031 | | case CLIENT_TRAFFIC_SECRET: |
15032 | | str = "CLIENT_TRAFFIC_SECRET_0"; break; |
15033 | | case SERVER_TRAFFIC_SECRET: |
15034 | | str = "SERVER_TRAFFIC_SECRET_0"; break; |
15035 | | case EXPORTER_SECRET: |
15036 | | str = "EXPORTER_SECRET"; break; |
15037 | | default: |
15038 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15039 | | XFCLOSE(fp); |
15040 | | #endif |
15041 | | return BAD_FUNC_ARG; |
15042 | | break; |
15043 | | } |
15044 | | |
15045 | | fprintf(fp, "%s ", str); |
15046 | | for (i = 0; i < (int)clientRandomSz; i++) { |
15047 | | fprintf(fp, "%02x", clientRandom[i]); |
15048 | | } |
15049 | | fprintf(fp, " "); |
15050 | | for (i = 0; i < secretSz; i++) { |
15051 | | fprintf(fp, "%02x", secret[i]); |
15052 | | } |
15053 | | fprintf(fp, "\n"); |
15054 | | |
15055 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15056 | | XFCLOSE(fp); |
15057 | | #endif |
15058 | | |
15059 | | return 0; |
15060 | | } |
15061 | | #endif |
15062 | | #endif |
15063 | | |
15064 | | #undef ERROR_OUT |
15065 | | |
15066 | | #endif /* !WOLFCRYPT_ONLY */ |
15067 | | |
15068 | | #endif /* !NO_TLS && WOLFSSL_TLS13 */ |