/src/wolfssl-fastmath/src/tls13.c
Line | Count | Source |
1 | | /* tls13.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 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 | | * TLS 1.3-Specific Build Options: |
26 | | * (See tls.c for generic TLS options: extensions, curves, callbacks, etc.) |
27 | | * |
28 | | * Protocol: |
29 | | * WOLFSSL_TLS13: Enable TLS 1.3 protocol default: on |
30 | | * WOLFSSL_TLS13_DRAFT: Enable TLS 1.3 draft version support default: off |
31 | | * WOLFSSL_QUIC: Enable QUIC protocol support (TLS 1.3) default: off |
32 | | * WOLFSSL_DTLS13_NO_HRR_ON_RESUME: Skip HRR on DTLS 1.3 resume default: off |
33 | | * WOLFSSL_DTLS_CH_FRAG: Enable DTLS 1.3 ClientHello frag default: off |
34 | | * |
35 | | * Handshake: |
36 | | * WOLFSSL_TLS13_MIDDLEBOX_COMPAT: Enable middlebox compatibility default: on |
37 | | * Sends ChangeCipherSpec and includes session id |
38 | | * WOLFSSL_SEND_HRR_COOKIE: Send cookie in HelloRetryRequest default: off |
39 | | * for stateless ClientHello tracking |
40 | | * WOLFSSL_EARLY_DATA: Allow 0-RTT early data default: off |
41 | | * WOLFSSL_EARLY_DATA_GROUP: Group early data with ClientHello default: off |
42 | | * WOLFSSL_POST_HANDSHAKE_AUTH: Post-handshake client auth default: off |
43 | | * WOLFSSL_TLS13_TICKET_BEFORE_FINISHED: Send NewSessionTicket default: off |
44 | | * before client Finished message |
45 | | * WOLFSSL_NO_CLIENT_AUTH: Disable TLS 1.3 client authentication default: off |
46 | | * WOLFSSL_NO_CLIENT_CERT_ERROR: Require client certificate default: off |
47 | | * WOLFSSL_CERT_SETUP_CB: Certificate setup callback default: off |
48 | | * WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION: Allow bad legacy version default: off |
49 | | * |
50 | | * Security: |
51 | | * WOLFSSL_BLIND_PRIVATE_KEY: Blind private key during signing default: off |
52 | | * WOLFSSL_CHECK_SIG_FAULTS: Verify signature after ECC signing default: off |
53 | | * to detect fault injection attacks |
54 | | * WOLFSSL_CIPHER_TEXT_CHECK: Verify ciphertext integrity default: off |
55 | | * |
56 | | * TLS 1.3 PSK: |
57 | | * WOLFSSL_PSK_ONE_ID: Single PSK identity per connect default: off |
58 | | * WOLFSSL_PSK_MULTI_ID_PER_CS: Multiple PSK IDs per cipher suite default: off |
59 | | * WOLFSSL_PRIORITIZE_PSK: Prioritize PSK over ciphersuite order default: off |
60 | | * WOLFSSL_PSK_ID_PROTECTION: Enable PSK identity protection default: off |
61 | | * |
62 | | * TLS 1.3 Session Tickets: |
63 | | * WOLFSSL_TICKET_HAVE_ID: Session tickets include ID default: off |
64 | | * WOLFSSL_TICKET_NONCE_MALLOC: Dynamically allocate ticket nonce default: off |
65 | | * |
66 | | * TLS 1.3 Key Exchange: |
67 | | * HAVE_KEYING_MATERIAL: Export keying material (RFC 8446 7.5) default: off |
68 | | * WOLFSSL_HAVE_TLS_UNIQUE: Enable tls-unique channel binding default: off |
69 | | * |
70 | | * TLS 1.3 Hash/Signature: |
71 | | * WOLFSSL_TLS13_SHA512: Allow SHA-512 in TLS 1.3 handshake default: off |
72 | | * (no ciphersuite requires it currently) |
73 | | * WOLFSSL_ERROR_CODE_OPENSSL: Use OpenSSL-compatible error codes default: off |
74 | | * WOLFSSL_SSLKEYLOGFILE_OUTPUT: Set key log output file path default: off |
75 | | * WOLFSSL_RW_THREADED: Enable read/write threading support default: off |
76 | | * WOLFSSL_ASYNC_IO: Enable async I/O operations default: off |
77 | | * WOLFSSL_NONBLOCK_OCSP: Non-blocking OCSP processing default: off |
78 | | * WOLFSSL_TLS_OCSP_MULTI: Multiple OCSP responses default: off |
79 | | * WOLFSSL_WOLFSENTRY_HOOKS: wolfSentry integration hooks default: off |
80 | | */ |
81 | | |
82 | | #if !defined(NO_TLS) && defined(WOLFSSL_TLS13) |
83 | | |
84 | | #ifndef WOLFCRYPT_ONLY |
85 | | |
86 | | #ifdef HAVE_ERRNO_H |
87 | | #include <errno.h> |
88 | | #endif |
89 | | |
90 | | #if defined(__MACH__) || defined(__FreeBSD__) || \ |
91 | | defined(__INCLUDE_NUTTX_CONFIG_H) || defined(WOLFSSL_RIOT_OS) |
92 | | #include <sys/time.h> |
93 | | #endif /* __MACH__ || __FreeBSD__ || |
94 | | __INCLUDE_NUTTX_CONFIG_H || WOLFSSL_RIOT_OS */ |
95 | | |
96 | | |
97 | | #include <wolfssl/internal.h> |
98 | | #include <wolfssl/error-ssl.h> |
99 | | #include <wolfssl/wolfcrypt/asn.h> |
100 | | #include <wolfssl/wolfcrypt/dh.h> |
101 | | #include <wolfssl/wolfcrypt/kdf.h> |
102 | | #include <wolfssl/wolfcrypt/signature.h> |
103 | | #ifdef NO_INLINE |
104 | | #include <wolfssl/wolfcrypt/misc.h> |
105 | | #else |
106 | | #define WOLFSSL_MISC_INCLUDED |
107 | | #include <wolfcrypt/src/misc.c> |
108 | | #endif |
109 | | |
110 | | #ifdef __sun |
111 | | #include <sys/filio.h> |
112 | | #endif |
113 | | |
114 | | #ifndef TRUE |
115 | | #define TRUE 1 |
116 | | #endif |
117 | | #ifndef FALSE |
118 | | #define FALSE 0 |
119 | | #endif |
120 | | |
121 | | #ifndef HAVE_AEAD |
122 | | #ifndef _MSC_VER |
123 | | #error "The build option HAVE_AEAD is required for TLS 1.3" |
124 | | #else |
125 | | #pragma \ |
126 | | message("error: The build option HAVE_AEAD is required for TLS 1.3") |
127 | | #endif |
128 | | #endif |
129 | | |
130 | | #ifndef HAVE_HKDF |
131 | | #ifndef _MSC_VER |
132 | | #error "The build option HAVE_HKDF is required for TLS 1.3" |
133 | | #else |
134 | | #pragma message("error: The build option HAVE_HKDF is required for TLS 1.3") |
135 | | #endif |
136 | | #endif |
137 | | |
138 | | #ifndef HAVE_TLS_EXTENSIONS |
139 | | #ifndef _MSC_VER |
140 | | #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3" |
141 | | #else |
142 | | #pragma message("error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3") |
143 | | #endif |
144 | | #endif |
145 | | |
146 | | |
147 | | /* Set ret to error value and jump to label. |
148 | | * |
149 | | * err The error value to set. |
150 | | * eLabel The label to jump to. |
151 | | */ |
152 | 0 | #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } |
153 | | |
154 | | /* Size of the TLS v1.3 label use when deriving keys. */ |
155 | 0 | #define TLS13_PROTOCOL_LABEL_SZ 6 |
156 | | /* The protocol label for TLS v1.3. */ |
157 | | static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 "; |
158 | | |
159 | | #ifdef WOLFSSL_DTLS13 |
160 | | #define DTLS13_PROTOCOL_LABEL_SZ 6 |
161 | | static const byte dtls13ProtocolLabel[DTLS13_PROTOCOL_LABEL_SZ + 1] = "dtls13"; |
162 | | #endif /* WOLFSSL_DTLS13 */ |
163 | | |
164 | | #if defined(HAVE_ECH) |
165 | | #define ECH_ACCEPT_CONFIRMATION_LABEL_SZ 23 |
166 | | #define ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ 27 |
167 | | static const byte |
168 | | echAcceptConfirmationLabel[ECH_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
169 | | "ech accept confirmation"; |
170 | | static const byte |
171 | | echHrrAcceptConfirmationLabel[ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
172 | | "hrr ech accept confirmation"; |
173 | | #endif |
174 | | |
175 | | #ifndef NO_CERTS |
176 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
177 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
178 | | |
179 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash); |
180 | | |
181 | | #endif |
182 | | #endif |
183 | | |
184 | | /* Expand data using HMAC, salt and label and info. |
185 | | * TLS v1.3 defines this function. Use callback if available. */ |
186 | | static int Tls13HKDFExpandLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
187 | | const byte* prk, word32 prkLen, |
188 | | const byte* protocol, word32 protocolLen, |
189 | | const byte* label, word32 labelLen, |
190 | | const byte* info, word32 infoLen, |
191 | | int digest) |
192 | 4.21k | { |
193 | 4.21k | int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
194 | | |
195 | | #if defined(HAVE_PK_CALLBACKS) |
196 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
197 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
198 | | protocol, protocolLen, |
199 | | label, labelLen, |
200 | | info, infoLen, digest, |
201 | | WOLFSSL_CLIENT_END /* ignored */); |
202 | | } |
203 | | |
204 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
205 | | return ret; |
206 | | #endif |
207 | 4.21k | (void)ssl; |
208 | 4.21k | PRIVATE_KEY_UNLOCK(); |
209 | 4.21k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
210 | 4.21k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
211 | 4.21k | protocol, protocolLen, |
212 | 4.21k | label, labelLen, |
213 | 4.21k | info, infoLen, digest, |
214 | 4.21k | ssl->heap, ssl->devId); |
215 | | #else |
216 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
217 | | protocol, protocolLen, |
218 | | label, labelLen, |
219 | | info, infoLen, digest); |
220 | | #endif |
221 | 4.21k | PRIVATE_KEY_LOCK(); |
222 | 4.21k | return ret; |
223 | 4.21k | } |
224 | | |
225 | | /* Same as above, but pass in the side we are expanding for: |
226 | | * side: either WOLFSSL_CLIENT_END or WOLFSSL_SERVER_END. |
227 | | */ |
228 | | static int Tls13HKDFExpandKeyLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
229 | | const byte* prk, word32 prkLen, |
230 | | const byte* protocol, word32 protocolLen, |
231 | | const byte* label, word32 labelLen, |
232 | | const byte* info, word32 infoLen, |
233 | | int digest, int side) |
234 | 9.27k | { |
235 | 9.27k | int ret; |
236 | | #if defined(HAVE_PK_CALLBACKS) |
237 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
238 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
239 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
240 | | protocol, protocolLen, |
241 | | label, labelLen, |
242 | | info, infoLen, |
243 | | digest, side); |
244 | | } |
245 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
246 | | return ret; |
247 | | #endif |
248 | | |
249 | 9.27k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
250 | 9.27k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
251 | 9.27k | protocol, protocolLen, |
252 | 9.27k | label, labelLen, |
253 | 9.27k | info, infoLen, digest, |
254 | 9.27k | ssl->heap, ssl->devId); |
255 | | |
256 | | #elif defined(HAVE_FIPS) && defined(wc_Tls13_HKDF_Expand_Label) |
257 | | ret = wc_Tls13_HKDF_Expand_Label_fips(okm, okmLen, prk, prkLen, |
258 | | protocol, protocolLen, |
259 | | label, labelLen, |
260 | | info, infoLen, digest); |
261 | | #else |
262 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
263 | | protocol, protocolLen, |
264 | | label, labelLen, |
265 | | info, infoLen, digest); |
266 | | #endif |
267 | 9.27k | (void)ssl; |
268 | 9.27k | (void)side; |
269 | 9.27k | return ret; |
270 | 9.27k | } |
271 | | |
272 | | |
273 | | /* Derive a key from a message. |
274 | | * |
275 | | * ssl The SSL/TLS object. |
276 | | * output The buffer to hold the derived key. |
277 | | * outputLen The length of the derived key. |
278 | | * secret The secret used to derive the key (HMAC secret). |
279 | | * label The label used to distinguish the context. |
280 | | * labelLen The length of the label. |
281 | | * msg The message data to derive key from. |
282 | | * msgLen The length of the message data to derive key from. |
283 | | * hashAlgo The hash algorithm to use in the HMAC. |
284 | | * returns 0 on success, otherwise failure. |
285 | | */ |
286 | | static int DeriveKeyMsg(WOLFSSL* ssl, byte* output, int outputLen, |
287 | | const byte* secret, const byte* label, word32 labelLen, |
288 | | byte* msg, int msgLen, int hashAlgo) |
289 | | { |
290 | | byte hash[WC_MAX_DIGEST_SIZE]; |
291 | | Digest digest; |
292 | | word32 hashSz = 0; |
293 | | const byte* protocol; |
294 | | word32 protocolLen; |
295 | | int digestAlg = -1; |
296 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
297 | | |
298 | | switch (hashAlgo) { |
299 | | #ifndef NO_SHA256 |
300 | | case sha256_mac: |
301 | | ret = wc_InitSha256_ex(&digest.sha256, ssl->heap, ssl->devId); |
302 | | if (ret == 0) { |
303 | | ret = wc_Sha256Update(&digest.sha256, msg, (word32)msgLen); |
304 | | if (ret == 0) |
305 | | ret = wc_Sha256Final(&digest.sha256, hash); |
306 | | wc_Sha256Free(&digest.sha256); |
307 | | } |
308 | | hashSz = WC_SHA256_DIGEST_SIZE; |
309 | | digestAlg = WC_SHA256; |
310 | | break; |
311 | | #endif |
312 | | #ifdef WOLFSSL_SHA384 |
313 | | case sha384_mac: |
314 | | ret = wc_InitSha384_ex(&digest.sha384, ssl->heap, ssl->devId); |
315 | | if (ret == 0) { |
316 | | ret = wc_Sha384Update(&digest.sha384, msg, (word32)msgLen); |
317 | | if (ret == 0) |
318 | | ret = wc_Sha384Final(&digest.sha384, hash); |
319 | | wc_Sha384Free(&digest.sha384); |
320 | | } |
321 | | hashSz = WC_SHA384_DIGEST_SIZE; |
322 | | digestAlg = WC_SHA384; |
323 | | break; |
324 | | #endif |
325 | | #ifdef WOLFSSL_TLS13_SHA512 |
326 | | case sha512_mac: |
327 | | ret = wc_InitSha512_ex(&digest.sha512, ssl->heap, ssl->devId); |
328 | | if (ret == 0) { |
329 | | ret = wc_Sha512Update(&digest.sha512, msg, (word32)msgLen); |
330 | | if (ret == 0) |
331 | | ret = wc_Sha512Final(&digest.sha512, hash); |
332 | | wc_Sha512Free(&digest.sha512); |
333 | | } |
334 | | hashSz = WC_SHA512_DIGEST_SIZE; |
335 | | digestAlg = WC_SHA512; |
336 | | break; |
337 | | #endif |
338 | | #ifdef WOLFSSL_SM3 |
339 | | case sm3_mac: |
340 | | ret = wc_InitSm3(&digest.sm3, ssl->heap, ssl->devId); |
341 | | if (ret == 0) { |
342 | | ret = wc_Sm3Update(&digest.sm3, msg, (word32)msgLen); |
343 | | if (ret == 0) |
344 | | ret = wc_Sm3Final(&digest.sm3, hash); |
345 | | wc_Sm3Free(&digest.sm3); |
346 | | } |
347 | | hashSz = WC_SM3_DIGEST_SIZE; |
348 | | digestAlg = WC_SM3; |
349 | | break; |
350 | | #endif |
351 | | default: |
352 | | ret = BAD_FUNC_ARG; |
353 | | digestAlg = -1; |
354 | | break; |
355 | | } |
356 | | |
357 | | if (digestAlg < 0) |
358 | | return HASH_TYPE_E; |
359 | | |
360 | | if (ret != 0) |
361 | | return ret; |
362 | | |
363 | | switch (ssl->version.minor) { |
364 | | case TLSv1_3_MINOR: |
365 | | protocol = tls13ProtocolLabel; |
366 | | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
367 | | break; |
368 | | #ifdef WOLFSSL_DTLS13 |
369 | | case DTLSv1_3_MINOR: |
370 | | if (!ssl->options.dtls) |
371 | | return VERSION_ERROR; |
372 | | |
373 | | protocol = dtls13ProtocolLabel; |
374 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
375 | | break; |
376 | | #endif /* WOLFSSL_DTLS13 */ |
377 | | default: |
378 | | return VERSION_ERROR; |
379 | | } |
380 | | if (outputLen == -1) |
381 | | outputLen = (int)hashSz; |
382 | | |
383 | | ret = Tls13HKDFExpandLabel(ssl, output, (word32)outputLen, secret, hashSz, |
384 | | protocol, protocolLen, label, labelLen, |
385 | | hash, hashSz, digestAlg); |
386 | | return ret; |
387 | | } |
388 | | |
389 | | /* Derive a key. |
390 | | * |
391 | | * ssl The SSL/TLS object. |
392 | | * output The buffer to hold the derived key. |
393 | | * outputLen The length of the derived key. |
394 | | * secret The secret used to derive the key (HMAC secret). |
395 | | * label The label used to distinguish the context. |
396 | | * labelLen The length of the label. |
397 | | * hashAlgo The hash algorithm to use in the HMAC. |
398 | | * includeMsgs Whether to include a hash of the handshake messages so far. |
399 | | * side The side that we are deriving the secret for. |
400 | | * returns 0 on success, otherwise failure. |
401 | | */ |
402 | | int Tls13DeriveKey(WOLFSSL* ssl, byte* output, int outputLen, |
403 | | const byte* secret, const byte* label, word32 labelLen, |
404 | | int hashAlgo, int includeMsgs, int side) |
405 | 0 | { |
406 | 0 | int ret = 0; |
407 | 0 | byte hash[WC_MAX_DIGEST_SIZE]; |
408 | 0 | word32 hashSz = 0; |
409 | 0 | word32 hashOutSz = 0; |
410 | 0 | const byte* protocol; |
411 | 0 | word32 protocolLen; |
412 | 0 | int digestAlg = 0; |
413 | | |
414 | |
|
415 | 0 | switch (hashAlgo) { |
416 | 0 | #ifndef NO_SHA256 |
417 | 0 | case sha256_mac: |
418 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
419 | 0 | digestAlg = WC_SHA256; |
420 | 0 | if (includeMsgs) |
421 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
422 | 0 | break; |
423 | 0 | #endif |
424 | | |
425 | 0 | #ifdef WOLFSSL_SHA384 |
426 | 0 | case sha384_mac: |
427 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
428 | 0 | digestAlg = WC_SHA384; |
429 | 0 | if (includeMsgs) |
430 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
431 | 0 | break; |
432 | 0 | #endif |
433 | | |
434 | | #ifdef WOLFSSL_TLS13_SHA512 |
435 | | case sha512_mac: |
436 | | hashSz = WC_SHA512_DIGEST_SIZE; |
437 | | digestAlg = WC_SHA512; |
438 | | if (includeMsgs) |
439 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
440 | | break; |
441 | | #endif |
442 | | |
443 | 0 | #ifdef WOLFSSL_SM3 |
444 | 0 | case sm3_mac: |
445 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
446 | 0 | digestAlg = WC_SM3; |
447 | 0 | if (includeMsgs) |
448 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
449 | 0 | break; |
450 | 0 | #endif |
451 | | |
452 | 0 | default: |
453 | 0 | ret = HASH_TYPE_E; |
454 | 0 | break; |
455 | 0 | } |
456 | 0 | if (ret != 0) |
457 | 0 | return ret; |
458 | | |
459 | 0 | protocol = tls13ProtocolLabel; |
460 | 0 | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
461 | |
|
462 | | #ifdef WOLFSSL_DTLS13 |
463 | | if (ssl->options.dtls) { |
464 | | protocol = dtls13ProtocolLabel; |
465 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
466 | | } |
467 | | #endif /* WOLFSSL_DTLS13 */ |
468 | |
|
469 | 0 | if (outputLen == -1) { |
470 | 0 | outputLen = (int)hashSz; |
471 | 0 | } |
472 | 0 | if (includeMsgs) { |
473 | 0 | hashOutSz = hashSz; |
474 | 0 | } |
475 | 0 | else { |
476 | | /* Appease static analyzers by making sure hash is cleared, since it is |
477 | | * passed into expand key label where older wc_Tls13_HKDF_Expand_Label |
478 | | * will unconditionally try to call a memcpy on it, however length will |
479 | | * always be 0. */ |
480 | 0 | XMEMSET(hash, 0, sizeof(hash)); |
481 | 0 | hashOutSz = 0; |
482 | 0 | } |
483 | |
|
484 | 0 | PRIVATE_KEY_UNLOCK(); |
485 | 0 | ret = Tls13HKDFExpandKeyLabel(ssl, output, (word32)outputLen, secret, hashSz, |
486 | 0 | protocol, protocolLen, label, labelLen, |
487 | 0 | hash, hashOutSz, digestAlg, side); |
488 | 0 | PRIVATE_KEY_LOCK(); |
489 | |
|
490 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
491 | | wc_MemZero_Add("TLS 1.3 derived key", output, outputLen); |
492 | | #endif |
493 | 0 | return ret; |
494 | 0 | } |
495 | | |
496 | | /* Convert TLS mac ID to a hash algorithm ID |
497 | | * |
498 | | * mac Mac ID to convert |
499 | | * returns hash ID on success, or the NONE type. |
500 | | */ |
501 | | static WC_INLINE int mac2hash(int mac) |
502 | | { |
503 | | int hash; |
504 | | switch (mac) { |
505 | | #ifndef NO_SHA256 |
506 | | case sha256_mac: |
507 | | hash = WC_SHA256; |
508 | | break; |
509 | | #endif |
510 | | |
511 | | #ifdef WOLFSSL_SHA384 |
512 | | case sha384_mac: |
513 | | hash = WC_SHA384; |
514 | | break; |
515 | | #endif |
516 | | |
517 | | #ifdef WOLFSSL_TLS13_SHA512 |
518 | | case sha512_mac: |
519 | | hash = WC_SHA512; |
520 | | break; |
521 | | #endif |
522 | | |
523 | | #ifdef WOLFSSL_SM3 |
524 | | case sm3_mac: |
525 | | hash = WC_SM3; |
526 | | break; |
527 | | #endif |
528 | | |
529 | | default: |
530 | | hash = WC_HASH_TYPE_NONE; |
531 | | } |
532 | | return hash; |
533 | | } |
534 | | |
535 | | #ifndef NO_PSK |
536 | | /* The length of the binder key label. */ |
537 | | #define BINDER_KEY_LABEL_SZ 10 |
538 | | /* The binder key label. */ |
539 | | static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] = |
540 | | "ext binder"; |
541 | | |
542 | | /* Derive the binder key. |
543 | | * |
544 | | * ssl The SSL/TLS object. |
545 | | * key The derived key. |
546 | | * returns 0 on success, otherwise failure. |
547 | | */ |
548 | | static int DeriveBinderKey(WOLFSSL* ssl, byte* key) |
549 | | { |
550 | | WOLFSSL_MSG("Derive Binder Key"); |
551 | | if (ssl == NULL || ssl->arrays == NULL) { |
552 | | return BAD_FUNC_ARG; |
553 | | } |
554 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
555 | | binderKeyLabel, BINDER_KEY_LABEL_SZ, |
556 | | NULL, 0, ssl->specs.mac_algorithm); |
557 | | } |
558 | | #endif /* !NO_PSK */ |
559 | | |
560 | | #if defined(HAVE_SESSION_TICKET) && \ |
561 | | (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) |
562 | | /* The length of the binder key resume label. */ |
563 | | #define BINDER_KEY_RESUME_LABEL_SZ 10 |
564 | | /* The binder key resume label. */ |
565 | | static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] = |
566 | | "res binder"; |
567 | | |
568 | | /* Derive the binder resumption key. |
569 | | * |
570 | | * ssl The SSL/TLS object. |
571 | | * key The derived key. |
572 | | * returns 0 on success, otherwise failure. |
573 | | */ |
574 | | static int DeriveBinderKeyResume(WOLFSSL* ssl, byte* key) |
575 | | { |
576 | | WOLFSSL_MSG("Derive Binder Key - Resumption"); |
577 | | if (ssl == NULL || ssl->arrays == NULL) { |
578 | | return BAD_FUNC_ARG; |
579 | | } |
580 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
581 | | binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ, |
582 | | NULL, 0, ssl->specs.mac_algorithm); |
583 | | } |
584 | | #endif /* HAVE_SESSION_TICKET && (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) */ |
585 | | |
586 | | #ifdef WOLFSSL_EARLY_DATA |
587 | | |
588 | | /* The length of the early traffic label. */ |
589 | | #define EARLY_TRAFFIC_LABEL_SZ 11 |
590 | | /* The early traffic label. */ |
591 | | static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] = |
592 | | "c e traffic"; |
593 | | |
594 | | /* Derive the early traffic key. |
595 | | * |
596 | | * ssl The SSL/TLS object. |
597 | | * key The derived key. |
598 | | * side The side that we are deriving the secret for. |
599 | | * returns 0 on success, otherwise failure. |
600 | | */ |
601 | | static int DeriveEarlyTrafficSecret(WOLFSSL* ssl, byte* key, int side) |
602 | | { |
603 | | int ret; |
604 | | WOLFSSL_MSG("Derive Early Traffic Secret"); |
605 | | if (ssl == NULL || ssl->arrays == NULL) { |
606 | | return BAD_FUNC_ARG; |
607 | | } |
608 | | |
609 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
610 | | /* If this is called from a sniffer session with keylog file support, |
611 | | * obtain the appropriate secret from the callback */ |
612 | | if (ssl->snifferSecretCb != NULL) { |
613 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
614 | | SNIFFER_SECRET_CLIENT_EARLY_TRAFFIC_SECRET, |
615 | | key); |
616 | | } |
617 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
618 | | |
619 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->secret, |
620 | | earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ, |
621 | | ssl->specs.mac_algorithm, 1, side); |
622 | | #ifdef HAVE_SECRET_CALLBACK |
623 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
624 | | ret = ssl->tls13SecretCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
625 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
626 | | if (ret != 0) { |
627 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
628 | | return TLS13_SECRET_CB_E; |
629 | | } |
630 | | } |
631 | | #ifdef OPENSSL_EXTRA |
632 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
633 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
634 | | ssl->specs.hash_size, NULL); |
635 | | if (ret != 0) { |
636 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
637 | | return TLS13_SECRET_CB_E; |
638 | | } |
639 | | } |
640 | | #endif /* OPENSSL_EXTRA */ |
641 | | #endif /* HAVE_SECRET_CALLBACK */ |
642 | | return ret; |
643 | | } |
644 | | |
645 | | #endif |
646 | | |
647 | | /* The length of the client handshake label. */ |
648 | 636 | #define CLIENT_HANDSHAKE_LABEL_SZ 12 |
649 | | /* The client handshake label. */ |
650 | | static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] = |
651 | | "c hs traffic"; |
652 | | |
653 | | /* Derive the client handshake key. |
654 | | * |
655 | | * ssl The SSL/TLS object. |
656 | | * key The derived key. |
657 | | * returns 0 on success, otherwise failure. |
658 | | */ |
659 | | static int DeriveClientHandshakeSecret(WOLFSSL* ssl, byte* key) |
660 | 636 | { |
661 | 636 | int ret; |
662 | 636 | WOLFSSL_MSG("Derive Client Handshake Secret"); |
663 | 636 | if (ssl == NULL || ssl->arrays == NULL) { |
664 | 0 | return BAD_FUNC_ARG; |
665 | 0 | } |
666 | | |
667 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
668 | | /* If this is called from a sniffer session with keylog file support, |
669 | | * obtain the appropriate secret from the callback */ |
670 | | if (ssl->snifferSecretCb != NULL) { |
671 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
672 | | SNIFFER_SECRET_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
673 | | key); |
674 | | } |
675 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
676 | | |
677 | 636 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
678 | 636 | clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ, |
679 | 636 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
680 | | #ifdef HAVE_SECRET_CALLBACK |
681 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
682 | | ret = ssl->tls13SecretCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
683 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
684 | | if (ret != 0) { |
685 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
686 | | return TLS13_SECRET_CB_E; |
687 | | } |
688 | | } |
689 | | #ifdef OPENSSL_EXTRA |
690 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
691 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
692 | | ssl->specs.hash_size, NULL); |
693 | | if (ret != 0) { |
694 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
695 | | return TLS13_SECRET_CB_E; |
696 | | } |
697 | | } |
698 | | #endif /* OPENSSL_EXTRA */ |
699 | | #endif /* HAVE_SECRET_CALLBACK */ |
700 | 636 | return ret; |
701 | 636 | } |
702 | | |
703 | | /* The length of the server handshake label. */ |
704 | 628 | #define SERVER_HANDSHAKE_LABEL_SZ 12 |
705 | | /* The server handshake label. */ |
706 | | static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] = |
707 | | "s hs traffic"; |
708 | | |
709 | | /* Derive the server handshake key. |
710 | | * |
711 | | * ssl The SSL/TLS object. |
712 | | * key The derived key. |
713 | | * returns 0 on success, otherwise failure. |
714 | | */ |
715 | | static int DeriveServerHandshakeSecret(WOLFSSL* ssl, byte* key) |
716 | 628 | { |
717 | 628 | int ret; |
718 | 628 | WOLFSSL_MSG("Derive Server Handshake Secret"); |
719 | 628 | if (ssl == NULL || ssl->arrays == NULL) { |
720 | 0 | return BAD_FUNC_ARG; |
721 | 0 | } |
722 | | |
723 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
724 | | /* If this is called from a sniffer session with keylog file support, |
725 | | * obtain the appropriate secret from the callback */ |
726 | | if (ssl->snifferSecretCb != NULL) { |
727 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
728 | | SNIFFER_SECRET_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
729 | | key); |
730 | | } |
731 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
732 | | |
733 | 628 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
734 | 628 | serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ, |
735 | 628 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
736 | | |
737 | | #ifdef HAVE_SECRET_CALLBACK |
738 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
739 | | ret = ssl->tls13SecretCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
740 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
741 | | if (ret != 0) { |
742 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
743 | | return TLS13_SECRET_CB_E; |
744 | | } |
745 | | } |
746 | | #ifdef OPENSSL_EXTRA |
747 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
748 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
749 | | ssl->specs.hash_size, NULL); |
750 | | if (ret != 0) { |
751 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
752 | | return TLS13_SECRET_CB_E; |
753 | | } |
754 | | } |
755 | | #endif /* OPENSSL_EXTRA */ |
756 | | #endif /* HAVE_SECRET_CALLBACK */ |
757 | 628 | return ret; |
758 | 628 | } |
759 | | |
760 | | /* The length of the client application traffic label. */ |
761 | 358 | #define CLIENT_APP_LABEL_SZ 12 |
762 | | /* The client application traffic label. */ |
763 | | static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] = |
764 | | "c ap traffic"; |
765 | | |
766 | | /* Derive the client application traffic key. |
767 | | * |
768 | | * ssl The SSL/TLS object. |
769 | | * key The derived key. |
770 | | * returns 0 on success, otherwise failure. |
771 | | */ |
772 | | static int DeriveClientTrafficSecret(WOLFSSL* ssl, byte* key) |
773 | 358 | { |
774 | 358 | int ret; |
775 | 358 | WOLFSSL_MSG("Derive Client Traffic Secret"); |
776 | 358 | if (ssl == NULL || ssl->arrays == NULL) { |
777 | 0 | return BAD_FUNC_ARG; |
778 | 0 | } |
779 | | |
780 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
781 | | /* If this is called from a sniffer session with keylog file support, |
782 | | * obtain the appropriate secret from the callback */ |
783 | | if (ssl->snifferSecretCb != NULL) { |
784 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
785 | | SNIFFER_SECRET_CLIENT_TRAFFIC_SECRET, |
786 | | key); |
787 | | } |
788 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
789 | | |
790 | 358 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
791 | 358 | clientAppLabel, CLIENT_APP_LABEL_SZ, |
792 | 358 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
793 | | |
794 | | #ifdef HAVE_SECRET_CALLBACK |
795 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
796 | | ret = ssl->tls13SecretCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
797 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
798 | | if (ret != 0) { |
799 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
800 | | return TLS13_SECRET_CB_E; |
801 | | } |
802 | | } |
803 | | #ifdef OPENSSL_EXTRA |
804 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
805 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
806 | | ssl->specs.hash_size, NULL); |
807 | | if (ret != 0) { |
808 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
809 | | return TLS13_SECRET_CB_E; |
810 | | } |
811 | | } |
812 | | #endif /* OPENSSL_EXTRA */ |
813 | | #endif /* HAVE_SECRET_CALLBACK */ |
814 | 358 | return ret; |
815 | 358 | } |
816 | | |
817 | | /* The length of the server application traffic label. */ |
818 | 364 | #define SERVER_APP_LABEL_SZ 12 |
819 | | /* The server application traffic label. */ |
820 | | static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] = |
821 | | "s ap traffic"; |
822 | | |
823 | | /* Derive the server application traffic key. |
824 | | * |
825 | | * ssl The SSL/TLS object. |
826 | | * key The derived key. |
827 | | * returns 0 on success, otherwise failure. |
828 | | */ |
829 | | static int DeriveServerTrafficSecret(WOLFSSL* ssl, byte* key) |
830 | 364 | { |
831 | 364 | int ret; |
832 | 364 | WOLFSSL_MSG("Derive Server Traffic Secret"); |
833 | 364 | if (ssl == NULL || ssl->arrays == NULL) { |
834 | 0 | return BAD_FUNC_ARG; |
835 | 0 | } |
836 | | |
837 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
838 | | /* If this is called from a sniffer session with keylog file support, |
839 | | * obtain the appropriate secret from the callback */ |
840 | | if (ssl->snifferSecretCb != NULL) { |
841 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
842 | | SNIFFER_SECRET_SERVER_TRAFFIC_SECRET, |
843 | | key); |
844 | | } |
845 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
846 | | |
847 | 364 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
848 | 364 | serverAppLabel, SERVER_APP_LABEL_SZ, |
849 | 364 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
850 | | |
851 | | #ifdef HAVE_SECRET_CALLBACK |
852 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
853 | | ret = ssl->tls13SecretCb(ssl, SERVER_TRAFFIC_SECRET, key, |
854 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
855 | | if (ret != 0) { |
856 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
857 | | return TLS13_SECRET_CB_E; |
858 | | } |
859 | | } |
860 | | #ifdef OPENSSL_EXTRA |
861 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
862 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_TRAFFIC_SECRET, key, |
863 | | ssl->specs.hash_size, NULL); |
864 | | if (ret != 0) { |
865 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
866 | | return TLS13_SECRET_CB_E; |
867 | | } |
868 | | } |
869 | | #endif /* OPENSSL_EXTRA */ |
870 | | #endif /* HAVE_SECRET_CALLBACK */ |
871 | 364 | return ret; |
872 | 364 | } |
873 | | |
874 | | #ifdef HAVE_KEYING_MATERIAL |
875 | | /* The length of the exporter master secret label. */ |
876 | | #define EXPORTER_MASTER_LABEL_SZ 10 |
877 | | /* The exporter master secret label. */ |
878 | | static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] = |
879 | | "exp master"; |
880 | | |
881 | | /* Derive the exporter secret. |
882 | | * |
883 | | * ssl The SSL/TLS object. |
884 | | * key The derived key. |
885 | | * returns 0 on success, otherwise failure. |
886 | | */ |
887 | | static int DeriveExporterSecret(WOLFSSL* ssl, byte* key) |
888 | | { |
889 | | int ret; |
890 | | WOLFSSL_ENTER("Derive Exporter Secret"); |
891 | | if (ssl == NULL || ssl->arrays == NULL) { |
892 | | return BAD_FUNC_ARG; |
893 | | } |
894 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
895 | | exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ, |
896 | | ssl->specs.mac_algorithm, 1, 0 /* Unused */); |
897 | | #ifdef HAVE_SECRET_CALLBACK |
898 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
899 | | ret = ssl->tls13SecretCb(ssl, EXPORTER_SECRET, key, |
900 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
901 | | if (ret != 0) { |
902 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
903 | | return TLS13_SECRET_CB_E; |
904 | | } |
905 | | } |
906 | | #ifdef OPENSSL_EXTRA |
907 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
908 | | ret = ssl->tls13KeyLogCb(ssl, EXPORTER_SECRET, key, |
909 | | ssl->specs.hash_size, NULL); |
910 | | if (ret != 0) { |
911 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
912 | | return TLS13_SECRET_CB_E; |
913 | | } |
914 | | } |
915 | | #endif /* OPENSSL_EXTRA */ |
916 | | #endif /* HAVE_SECRET_CALLBACK */ |
917 | | return ret; |
918 | | } |
919 | | |
920 | | /* The length of the exporter label. */ |
921 | | #define EXPORTER_LABEL_SZ 8 |
922 | | /* The exporter label. */ |
923 | | static const byte exporterLabel[EXPORTER_LABEL_SZ + 1] = |
924 | | "exporter"; |
925 | | /* Hash("") */ |
926 | | #ifndef NO_SHA256 |
927 | | static const byte emptySHA256Hash[] = { |
928 | | 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, |
929 | | 0x99, 0x6F, 0xB9, 0x24, 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, |
930 | | 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 |
931 | | }; |
932 | | #endif |
933 | | #ifdef WOLFSSL_SHA384 |
934 | | static const byte emptySHA384Hash[] = { |
935 | | 0x38, 0xB0, 0x60, 0xA7, 0x51, 0xAC, 0x96, 0x38, 0x4C, 0xD9, 0x32, 0x7E, |
936 | | 0xB1, 0xB1, 0xE3, 0x6A, 0x21, 0xFD, 0xB7, 0x11, 0x14, 0xBE, 0x07, 0x43, |
937 | | 0x4C, 0x0C, 0xC7, 0xBF, 0x63, 0xF6, 0xE1, 0xDA, 0x27, 0x4E, 0xDE, 0xBF, |
938 | | 0xE7, 0x6F, 0x65, 0xFB, 0xD5, 0x1A, 0xD2, 0xF1, 0x48, 0x98, 0xB9, 0x5B |
939 | | }; |
940 | | #endif |
941 | | #ifdef WOLFSSL_TLS13_SHA512 |
942 | | static const byte emptySHA512Hash[] = { |
943 | | 0xCF, 0x83, 0xE1, 0x35, 0x7E, 0xEF, 0xB8, 0xBD, 0xF1, 0x54, 0x28, 0x50, |
944 | | 0xD6, 0x6D, 0x80, 0x07, 0xD6, 0x20, 0xE4, 0x05, 0x0B, 0x57, 0x15, 0xDC, |
945 | | 0x83, 0xF4, 0xA9, 0x21, 0xD3, 0x6C, 0xE9, 0xCE, 0x47, 0xD0, 0xD1, 0x3C, |
946 | | 0x5D, 0x85, 0xF2, 0xB0, 0xFF, 0x83, 0x18, 0xD2, 0x87, 0x7E, 0xEC, 0x2F, |
947 | | 0x63, 0xB9, 0x31, 0xBD, 0x47, 0x41, 0x7A, 0x81, 0xA5, 0x38, 0x32, 0x7A, |
948 | | 0xF9, 0x27, 0xDA, 0x3E |
949 | | }; |
950 | | #endif |
951 | | /** |
952 | | * Implement section 7.5 of RFC 8446 |
953 | | * @return 0 on success |
954 | | * <0 on failure |
955 | | */ |
956 | | int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen, |
957 | | const char *label, size_t labelLen, |
958 | | const unsigned char *context, size_t contextLen) |
959 | | { |
960 | | int ret; |
961 | | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
962 | | word32 hashLen = 0; |
963 | | byte hashOut[WC_MAX_DIGEST_SIZE]; |
964 | | const byte* emptyHash = NULL; |
965 | | byte firstExpand[WC_MAX_DIGEST_SIZE]; |
966 | | const byte* protocol = tls13ProtocolLabel; |
967 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
968 | | |
969 | | if (ssl->options.dtls && ssl->version.minor != DTLSv1_3_MINOR) |
970 | | return VERSION_ERROR; |
971 | | |
972 | | if (!ssl->options.dtls && ssl->version.minor != TLSv1_3_MINOR) |
973 | | return VERSION_ERROR; |
974 | | |
975 | | #ifdef WOLFSSL_DTLS13 |
976 | | if (ssl->options.dtls) { |
977 | | protocol = dtls13ProtocolLabel; |
978 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
979 | | } |
980 | | #endif /* WOLFSSL_DTLS13 */ |
981 | | |
982 | | switch (ssl->specs.mac_algorithm) { |
983 | | #ifndef NO_SHA256 |
984 | | case sha256_mac: |
985 | | hashType = WC_HASH_TYPE_SHA256; |
986 | | hashLen = WC_SHA256_DIGEST_SIZE; |
987 | | emptyHash = emptySHA256Hash; |
988 | | break; |
989 | | #endif |
990 | | |
991 | | #ifdef WOLFSSL_SHA384 |
992 | | case sha384_mac: |
993 | | hashType = WC_HASH_TYPE_SHA384; |
994 | | hashLen = WC_SHA384_DIGEST_SIZE; |
995 | | emptyHash = emptySHA384Hash; |
996 | | break; |
997 | | #endif |
998 | | |
999 | | #ifdef WOLFSSL_TLS13_SHA512 |
1000 | | case sha512_mac: |
1001 | | hashType = WC_HASH_TYPE_SHA512; |
1002 | | hashLen = WC_SHA512_DIGEST_SIZE; |
1003 | | emptyHash = emptySHA512Hash; |
1004 | | break; |
1005 | | #endif |
1006 | | } |
1007 | | |
1008 | | /* Derive-Secret(Secret, label, "") */ |
1009 | | ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen, |
1010 | | ssl->arrays->exporterSecret, hashLen, |
1011 | | protocol, protocolLen, (byte*)label, (word32)labelLen, |
1012 | | emptyHash, hashLen, (int)hashType); |
1013 | | if (ret != 0) |
1014 | | return ret; |
1015 | | |
1016 | | /* Sanity check contextLen to prevent truncation when cast to word32. */ |
1017 | | if (contextLen > WOLFSSL_MAX_32BIT) { |
1018 | | return BAD_FUNC_ARG; |
1019 | | } |
1020 | | |
1021 | | /* Hash(context_value) */ |
1022 | | ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE); |
1023 | | if (ret != 0) |
1024 | | return ret; |
1025 | | |
1026 | | ret = Tls13HKDFExpandLabel(ssl, out, (word32)outLen, firstExpand, hashLen, |
1027 | | protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ, |
1028 | | hashOut, hashLen, (int)hashType); |
1029 | | |
1030 | | return ret; |
1031 | | } |
1032 | | #endif |
1033 | | |
1034 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1035 | | /* The length of the resumption master secret label. */ |
1036 | | #define RESUME_MASTER_LABEL_SZ 10 |
1037 | | /* The resumption master secret label. */ |
1038 | | static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] = |
1039 | | "res master"; |
1040 | | |
1041 | | /* Derive the resumption secret. |
1042 | | * |
1043 | | * ssl The SSL/TLS object. |
1044 | | * key The derived key. |
1045 | | * returns 0 on success, otherwise failure. |
1046 | | */ |
1047 | | int DeriveResumptionSecret(WOLFSSL* ssl, byte* key) |
1048 | | { |
1049 | | byte* masterSecret; |
1050 | | |
1051 | | WOLFSSL_MSG("Derive Resumption Secret"); |
1052 | | if (ssl == NULL) { |
1053 | | return BAD_FUNC_ARG; |
1054 | | } |
1055 | | if (ssl->arrays != NULL) { |
1056 | | masterSecret = ssl->arrays->masterSecret; |
1057 | | } |
1058 | | else { |
1059 | | masterSecret = ssl->session->masterSecret; |
1060 | | } |
1061 | | return Tls13DeriveKey(ssl, key, -1, masterSecret, resumeMasterLabel, |
1062 | | RESUME_MASTER_LABEL_SZ, ssl->specs.mac_algorithm, 1, |
1063 | | 0 /* Unused */); |
1064 | | } |
1065 | | #endif |
1066 | | |
1067 | | /* Length of the finished label. */ |
1068 | 3.93k | #define FINISHED_LABEL_SZ 8 |
1069 | | /* Finished label for generating finished key. */ |
1070 | | static const byte finishedLabel[FINISHED_LABEL_SZ+1] = "finished"; |
1071 | | /* Derive the finished secret. |
1072 | | * |
1073 | | * ssl The SSL/TLS object. |
1074 | | * key The key to use with the HMAC. |
1075 | | * secret The derived secret. |
1076 | | * side The side that we are deriving the secret for. |
1077 | | * returns 0 on success, otherwise failure. |
1078 | | */ |
1079 | | static int DeriveFinishedSecret(WOLFSSL* ssl, byte* key, byte* secret, |
1080 | | int side) |
1081 | 3.93k | { |
1082 | 3.93k | WOLFSSL_MSG("Derive Finished Secret"); |
1083 | 3.93k | return Tls13DeriveKey(ssl, secret, -1, key, finishedLabel, |
1084 | 3.93k | FINISHED_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1085 | 3.93k | side); |
1086 | 3.93k | } |
1087 | | |
1088 | | /* The length of the application traffic label. */ |
1089 | 0 | #define APP_TRAFFIC_LABEL_SZ 11 |
1090 | | /* The application traffic label. */ |
1091 | | static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] = |
1092 | | "traffic upd"; |
1093 | | |
1094 | | /* Update the traffic secret. |
1095 | | * |
1096 | | * ssl The SSL/TLS object. |
1097 | | * secret The previous secret and derived secret. |
1098 | | * side The side that we are deriving the secret for. |
1099 | | * returns 0 on success, otherwise failure. |
1100 | | */ |
1101 | | static int DeriveTrafficSecret(WOLFSSL* ssl, byte* secret, int side) |
1102 | 0 | { |
1103 | 0 | WOLFSSL_MSG("Derive New Application Traffic Secret"); |
1104 | 0 | return Tls13DeriveKey(ssl, secret, -1, secret, |
1105 | 0 | appTrafficLabel, APP_TRAFFIC_LABEL_SZ, |
1106 | 0 | ssl->specs.mac_algorithm, 0, side); |
1107 | 0 | } |
1108 | | |
1109 | | |
1110 | | static int Tls13_HKDF_Extract(WOLFSSL *ssl, byte* prk, const byte* salt, |
1111 | | int saltLen, byte* ikm, int ikmLen, int digest) |
1112 | | { |
1113 | | int ret; |
1114 | | #ifdef HAVE_PK_CALLBACKS |
1115 | | void *cb_ctx = ssl->HkdfExtractCtx; |
1116 | | CallbackHKDFExtract cb = ssl->ctx->HkdfExtractCb; |
1117 | | if (cb != NULL) { |
1118 | | ret = cb(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, cb_ctx); |
1119 | | } |
1120 | | else |
1121 | | #endif |
1122 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1123 | | if ((int)ssl->arrays->psk_keySz < 0) { |
1124 | | ret = PSK_KEY_ERROR; |
1125 | | } |
1126 | | else |
1127 | | #endif |
1128 | | { |
1129 | | #if !defined(HAVE_FIPS) || \ |
1130 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
1131 | | ret = wc_Tls13_HKDF_Extract_ex(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, |
1132 | | ssl->heap, ssl->devId); |
1133 | | #else |
1134 | | ret = wc_Tls13_HKDF_Extract(prk, salt, saltLen, ikm, ikmLen, digest); |
1135 | | (void)ssl; |
1136 | | #endif |
1137 | | } |
1138 | | return ret; |
1139 | | } |
1140 | | |
1141 | | /* Derive the early secret using HKDF Extract. |
1142 | | * |
1143 | | * ssl The SSL/TLS object. |
1144 | | */ |
1145 | | int DeriveEarlySecret(WOLFSSL* ssl) |
1146 | 4.22k | { |
1147 | 4.22k | int ret; |
1148 | | |
1149 | 4.22k | WOLFSSL_MSG("Derive Early Secret"); |
1150 | 4.22k | if (ssl == NULL || ssl->arrays == NULL) { |
1151 | 0 | return BAD_FUNC_ARG; |
1152 | 0 | } |
1153 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1154 | | ret = tsip_Tls13DeriveEarlySecret(ssl); |
1155 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1156 | | return ret; |
1157 | | #endif |
1158 | 4.22k | PRIVATE_KEY_UNLOCK(); |
1159 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1160 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1161 | | ssl->arrays->psk_key, (int)ssl->arrays->psk_keySz, |
1162 | | mac2hash(ssl->specs.mac_algorithm)); |
1163 | | #else |
1164 | 4.22k | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1165 | 4.22k | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1166 | 4.22k | #endif |
1167 | 4.22k | PRIVATE_KEY_LOCK(); |
1168 | 4.22k | return ret; |
1169 | 4.22k | } |
1170 | | |
1171 | | /* The length of the derived label. */ |
1172 | 656 | #define DERIVED_LABEL_SZ 7 |
1173 | | /* The derived label. */ |
1174 | | static const byte derivedLabel[DERIVED_LABEL_SZ + 1] = |
1175 | | "derived"; |
1176 | | |
1177 | | /* Derive the handshake secret using HKDF Extract. |
1178 | | * |
1179 | | * ssl The SSL/TLS object. |
1180 | | */ |
1181 | | int DeriveHandshakeSecret(WOLFSSL* ssl) |
1182 | 656 | { |
1183 | 656 | byte key[WC_MAX_DIGEST_SIZE]; |
1184 | 656 | int ret; |
1185 | 656 | WOLFSSL_MSG("Derive Handshake Secret"); |
1186 | 656 | if (ssl == NULL || ssl->arrays == NULL) { |
1187 | 0 | return BAD_FUNC_ARG; |
1188 | 0 | } |
1189 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1190 | | ret = tsip_Tls13DeriveHandshakeSecret(ssl); |
1191 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1192 | | return ret; |
1193 | | #endif |
1194 | | |
1195 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1196 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1197 | 656 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
1198 | 656 | derivedLabel, DERIVED_LABEL_SZ, |
1199 | 656 | NULL, 0, ssl->specs.mac_algorithm); |
1200 | 656 | if (ret == 0) { |
1201 | 643 | PRIVATE_KEY_UNLOCK(); |
1202 | 643 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->preMasterSecret, |
1203 | 643 | key, ssl->specs.hash_size, |
1204 | 643 | ssl->arrays->preMasterSecret, (int)ssl->arrays->preMasterSz, |
1205 | 643 | mac2hash(ssl->specs.mac_algorithm)); |
1206 | 643 | PRIVATE_KEY_LOCK(); |
1207 | 643 | } |
1208 | | |
1209 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1210 | | wc_MemZero_Add("DeriveHandshakeSecret key", key, WC_MAX_DIGEST_SIZE); |
1211 | | #endif |
1212 | 656 | ForceZero(key, sizeof(key)); |
1213 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1214 | | wc_MemZero_Check(key, sizeof(key)); |
1215 | | #endif |
1216 | 656 | return ret; |
1217 | 656 | } |
1218 | | |
1219 | | /* Derive the master secret using HKDF Extract. |
1220 | | * |
1221 | | * ssl The SSL/TLS object. |
1222 | | */ |
1223 | | int DeriveMasterSecret(WOLFSSL* ssl) |
1224 | | { |
1225 | | byte key[WC_MAX_DIGEST_SIZE]; |
1226 | | int ret; |
1227 | | WOLFSSL_MSG("Derive Master Secret"); |
1228 | | if (ssl == NULL || ssl->arrays == NULL) { |
1229 | | return BAD_FUNC_ARG; |
1230 | | } |
1231 | | |
1232 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1233 | | ret = tsip_Tls13DeriveMasterSecret(ssl); |
1234 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1235 | | return ret; |
1236 | | #endif |
1237 | | |
1238 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1239 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1240 | | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret, |
1241 | | derivedLabel, DERIVED_LABEL_SZ, |
1242 | | NULL, 0, ssl->specs.mac_algorithm); |
1243 | | if (ret == 0) { |
1244 | | PRIVATE_KEY_UNLOCK(); |
1245 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->masterSecret, |
1246 | | key, ssl->specs.hash_size, |
1247 | | ssl->arrays->masterSecret, 0, |
1248 | | mac2hash(ssl->specs.mac_algorithm)); |
1249 | | PRIVATE_KEY_LOCK(); |
1250 | | } |
1251 | | |
1252 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1253 | | wc_MemZero_Add("DeriveMasterSecret key", key, WC_MAX_DIGEST_SIZE); |
1254 | | #endif |
1255 | | ForceZero(key, sizeof(key)); |
1256 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1257 | | wc_MemZero_Check(key, sizeof(key)); |
1258 | | #endif |
1259 | | |
1260 | | #ifdef HAVE_KEYING_MATERIAL |
1261 | | if (ret != 0) |
1262 | | return ret; |
1263 | | /* Calculate exporter secret only when saving arrays */ |
1264 | | if (ssl->options.saveArrays) |
1265 | | ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret); |
1266 | | #endif |
1267 | | |
1268 | | return ret; |
1269 | | } |
1270 | | |
1271 | | #if defined(HAVE_SESSION_TICKET) |
1272 | | /* Length of the resumption label. */ |
1273 | | #define RESUMPTION_LABEL_SZ 10 |
1274 | | /* Resumption label for generating PSK associated with the ticket. */ |
1275 | | static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption"; |
1276 | | |
1277 | | /* Derive the PSK associated with the ticket. |
1278 | | * |
1279 | | * ssl The SSL/TLS object. |
1280 | | * nonce The nonce to derive with. |
1281 | | * nonceLen The length of the nonce to derive with. |
1282 | | * secret The derived secret. |
1283 | | * returns 0 on success, otherwise failure. |
1284 | | */ |
1285 | | int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret) |
1286 | | { |
1287 | | int digestAlg; |
1288 | | /* Only one protocol version defined at this time. */ |
1289 | | const byte* protocol = tls13ProtocolLabel; |
1290 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
1291 | | int ret; |
1292 | | |
1293 | | WOLFSSL_MSG("Derive Resumption PSK"); |
1294 | | |
1295 | | #ifdef WOLFSSL_DTLS13 |
1296 | | if (ssl->options.dtls) { |
1297 | | protocol = dtls13ProtocolLabel; |
1298 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1299 | | } |
1300 | | #endif /* WOLFSSL_DTLS13 */ |
1301 | | |
1302 | | switch (ssl->specs.mac_algorithm) { |
1303 | | #ifndef NO_SHA256 |
1304 | | case sha256_mac: |
1305 | | digestAlg = WC_SHA256; |
1306 | | break; |
1307 | | #endif |
1308 | | |
1309 | | #ifdef WOLFSSL_SHA384 |
1310 | | case sha384_mac: |
1311 | | digestAlg = WC_SHA384; |
1312 | | break; |
1313 | | #endif |
1314 | | |
1315 | | #ifdef WOLFSSL_TLS13_SHA512 |
1316 | | case sha512_mac: |
1317 | | digestAlg = WC_SHA512; |
1318 | | break; |
1319 | | #endif |
1320 | | |
1321 | | #ifdef WOLFSSL_SM3 |
1322 | | case sm3_mac: |
1323 | | digestAlg = WC_SM3; |
1324 | | break; |
1325 | | #endif |
1326 | | |
1327 | | default: |
1328 | | return BAD_FUNC_ARG; |
1329 | | } |
1330 | | |
1331 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
1332 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
1333 | | PRIVATE_KEY_UNLOCK(); |
1334 | | ret = wc_Tls13_HKDF_Expand_Label_Alloc(secret, ssl->specs.hash_size, |
1335 | | ssl->session->masterSecret, ssl->specs.hash_size, protocol, protocolLen, |
1336 | | resumptionLabel, RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg, |
1337 | | ssl->heap); |
1338 | | PRIVATE_KEY_LOCK(); |
1339 | | #else |
1340 | | ret = Tls13HKDFExpandLabel(ssl, secret, ssl->specs.hash_size, |
1341 | | ssl->session->masterSecret, ssl->specs.hash_size, |
1342 | | protocol, protocolLen, resumptionLabel, |
1343 | | RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg); |
1344 | | #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */ |
1345 | | return ret; |
1346 | | } |
1347 | | #endif /* HAVE_SESSION_TICKET */ |
1348 | | |
1349 | | |
1350 | | /* Calculate the HMAC of message data to this point. |
1351 | | * |
1352 | | * ssl The SSL/TLS object. |
1353 | | * key The HMAC key. |
1354 | | * hash The hash result - verify data. |
1355 | | * returns length of verify data generated. |
1356 | | */ |
1357 | | static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash, |
1358 | | word32* pHashSz) |
1359 | 0 | { |
1360 | 0 | WC_DECLARE_VAR(verifyHmac, Hmac, 1, 0); |
1361 | 0 | int hashType = WC_SHA256; |
1362 | 0 | int hashSz = WC_SHA256_DIGEST_SIZE; |
1363 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
1364 | |
|
1365 | 0 | if (ssl == NULL || key == NULL || hash == NULL) { |
1366 | 0 | return BAD_FUNC_ARG; |
1367 | 0 | } |
1368 | | |
1369 | | /* Get the hash of the previous handshake messages. */ |
1370 | 0 | switch (ssl->specs.mac_algorithm) { |
1371 | 0 | #ifndef NO_SHA256 |
1372 | 0 | case sha256_mac: |
1373 | 0 | hashType = WC_SHA256; |
1374 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
1375 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
1376 | 0 | break; |
1377 | 0 | #endif /* !NO_SHA256 */ |
1378 | 0 | #ifdef WOLFSSL_SHA384 |
1379 | 0 | case sha384_mac: |
1380 | 0 | hashType = WC_SHA384; |
1381 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
1382 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
1383 | 0 | break; |
1384 | 0 | #endif /* WOLFSSL_SHA384 */ |
1385 | | #ifdef WOLFSSL_TLS13_SHA512 |
1386 | | case sha512_mac: |
1387 | | hashType = WC_SHA512; |
1388 | | hashSz = WC_SHA512_DIGEST_SIZE; |
1389 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
1390 | | break; |
1391 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
1392 | 0 | #ifdef WOLFSSL_SM3 |
1393 | 0 | case sm3_mac: |
1394 | 0 | hashType = WC_SM3; |
1395 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
1396 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
1397 | 0 | break; |
1398 | 0 | #endif /* WOLFSSL_SM3 */ |
1399 | 0 | default: |
1400 | 0 | ret = BAD_FUNC_ARG; |
1401 | 0 | break; |
1402 | 0 | } |
1403 | 0 | if (ret != 0) |
1404 | 0 | return ret; |
1405 | | |
1406 | | #ifdef WOLFSSL_DEBUG_TLS |
1407 | | WOLFSSL_MSG(" Key"); |
1408 | | WOLFSSL_BUFFER(key, ssl->specs.hash_size); |
1409 | | WOLFSSL_MSG(" Msg Hash"); |
1410 | | WOLFSSL_BUFFER(hash, hashSz); |
1411 | | #endif |
1412 | | |
1413 | 0 | WC_ALLOC_VAR_EX(verifyHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
1414 | 0 | return MEMORY_E); |
1415 | | |
1416 | | /* Calculate the verify data. */ |
1417 | 0 | ret = wc_HmacInit(verifyHmac, ssl->heap, ssl->devId); |
1418 | 0 | if (ret == 0) { |
1419 | 0 | ret = wc_HmacSetKey(verifyHmac, hashType, key, ssl->specs.hash_size); |
1420 | 0 | if (ret == 0) |
1421 | 0 | ret = wc_HmacUpdate(verifyHmac, hash, (word32)hashSz); |
1422 | 0 | if (ret == 0) |
1423 | 0 | ret = wc_HmacFinal(verifyHmac, hash); |
1424 | 0 | wc_HmacFree(verifyHmac); |
1425 | 0 | } |
1426 | |
|
1427 | 0 | WC_FREE_VAR_EX(verifyHmac, NULL, DYNAMIC_TYPE_HMAC); |
1428 | |
|
1429 | | #ifdef WOLFSSL_DEBUG_TLS |
1430 | | WOLFSSL_MSG(" Hash"); |
1431 | | WOLFSSL_BUFFER(hash, hashSz); |
1432 | | #endif |
1433 | |
|
1434 | 0 | if (pHashSz) |
1435 | 0 | *pHashSz = (word32)hashSz; |
1436 | |
|
1437 | 0 | return ret; |
1438 | 0 | } |
1439 | | |
1440 | | /* The length of the label to use when deriving keys. */ |
1441 | 0 | #define WRITE_KEY_LABEL_SZ 3 |
1442 | | /* The length of the label to use when deriving IVs. */ |
1443 | 0 | #define WRITE_IV_LABEL_SZ 2 |
1444 | | /* The label to use when deriving keys. */ |
1445 | | static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key"; |
1446 | | /* The label to use when deriving IVs. */ |
1447 | | static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv"; |
1448 | | |
1449 | | /* Derive the keys and IVs for TLS v1.3. |
1450 | | * |
1451 | | * ssl The SSL/TLS object. |
1452 | | * secret early_data_key when deriving the key and IV for encrypting early |
1453 | | * data application data and end_of_early_data messages. |
1454 | | * handshake_key when deriving keys and IVs for encrypting handshake |
1455 | | * messages. |
1456 | | * traffic_key when deriving first keys and IVs for encrypting |
1457 | | * traffic messages. |
1458 | | * update_traffic_key when deriving next keys and IVs for encrypting |
1459 | | * traffic messages. |
1460 | | * no_key when deriving keys and IVs from existing secrets without |
1461 | | * re-deriving the secrets. Used during early data transitions. |
1462 | | * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived. |
1463 | | * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived. |
1464 | | * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived. |
1465 | | * store 1 indicates to derive the keys and IVs from derived secret and |
1466 | | * store ready for provisioning. |
1467 | | * returns 0 on success, otherwise failure. |
1468 | | */ |
1469 | | int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store) |
1470 | 0 | { |
1471 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1472 | 0 | int i = 0; |
1473 | 0 | WC_DECLARE_VAR(key_dig, byte, MAX_PRF_DIG, 0); |
1474 | 0 | int provision; |
1475 | |
|
1476 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1477 | | ret = tsip_Tls13DeriveKeys(ssl, secret, side); |
1478 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1479 | | return ret; |
1480 | | } |
1481 | | ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1482 | | #endif |
1483 | |
|
1484 | 0 | WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap, |
1485 | 0 | DYNAMIC_TYPE_DIGEST, return MEMORY_E); |
1486 | | |
1487 | 0 | if (side == ENCRYPT_AND_DECRYPT_SIDE) { |
1488 | 0 | provision = PROVISION_CLIENT_SERVER; |
1489 | 0 | } |
1490 | 0 | else { |
1491 | 0 | provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^ |
1492 | 0 | (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT : |
1493 | 0 | PROVISION_SERVER; |
1494 | 0 | } |
1495 | | |
1496 | | /* Derive the appropriate secret to use in the HKDF. */ |
1497 | 0 | switch (secret) { |
1498 | | #ifdef WOLFSSL_EARLY_DATA |
1499 | | case early_data_key: |
1500 | | ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret, |
1501 | | WOLFSSL_CLIENT_END); |
1502 | | if (ret != 0) |
1503 | | goto end; |
1504 | | break; |
1505 | | #endif |
1506 | | |
1507 | 0 | case handshake_key: |
1508 | 0 | if (provision & PROVISION_CLIENT) { |
1509 | 0 | ret = DeriveClientHandshakeSecret(ssl, |
1510 | 0 | ssl->clientSecret); |
1511 | 0 | if (ret != 0) |
1512 | 0 | goto end; |
1513 | 0 | } |
1514 | 0 | if (provision & PROVISION_SERVER) { |
1515 | 0 | ret = DeriveServerHandshakeSecret(ssl, |
1516 | 0 | ssl->serverSecret); |
1517 | 0 | if (ret != 0) |
1518 | 0 | goto end; |
1519 | 0 | } |
1520 | 0 | break; |
1521 | | |
1522 | 0 | case traffic_key: |
1523 | 0 | if (provision & PROVISION_CLIENT) { |
1524 | 0 | ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret); |
1525 | 0 | if (ret != 0) |
1526 | 0 | goto end; |
1527 | 0 | } |
1528 | 0 | if (provision & PROVISION_SERVER) { |
1529 | 0 | ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret); |
1530 | 0 | if (ret != 0) |
1531 | 0 | goto end; |
1532 | 0 | } |
1533 | 0 | break; |
1534 | | |
1535 | 0 | case update_traffic_key: |
1536 | 0 | if (provision & PROVISION_CLIENT) { |
1537 | 0 | ret = DeriveTrafficSecret(ssl, ssl->clientSecret, |
1538 | 0 | WOLFSSL_CLIENT_END); |
1539 | 0 | if (ret != 0) |
1540 | 0 | goto end; |
1541 | 0 | } |
1542 | 0 | if (provision & PROVISION_SERVER) { |
1543 | 0 | ret = DeriveTrafficSecret(ssl, ssl->serverSecret, |
1544 | 0 | WOLFSSL_SERVER_END); |
1545 | 0 | if (ret != 0) |
1546 | 0 | goto end; |
1547 | 0 | } |
1548 | 0 | break; |
1549 | | |
1550 | 0 | case no_key: |
1551 | | /* Called with early data to derive keys from existing secrets |
1552 | | * without re-deriving the secrets themselves. */ |
1553 | 0 | ret = 0; |
1554 | 0 | break; |
1555 | | |
1556 | 0 | default: |
1557 | 0 | ret = BAD_FUNC_ARG; |
1558 | 0 | break; |
1559 | 0 | } |
1560 | | |
1561 | | #ifdef WOLFSSL_QUIC |
1562 | | if (WOLFSSL_IS_QUIC(ssl)) { |
1563 | | ret = wolfSSL_quic_forward_secrets(ssl, secret, side); |
1564 | | if (ret != 0) |
1565 | | goto end; |
1566 | | } |
1567 | | #endif /* WOLFSSL_QUIC */ |
1568 | | |
1569 | 0 | if (!store) |
1570 | 0 | goto end; |
1571 | | |
1572 | | /* Key data = client key | server key | client IV | server IV */ |
1573 | | |
1574 | 0 | if (provision & PROVISION_CLIENT) { |
1575 | | /* Derive the client key. */ |
1576 | 0 | WOLFSSL_MSG("Derive Client Key"); |
1577 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1578 | 0 | ssl->clientSecret, writeKeyLabel, |
1579 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1580 | 0 | WOLFSSL_CLIENT_END); |
1581 | 0 | if (ret != 0) |
1582 | 0 | goto end; |
1583 | 0 | i += ssl->specs.key_size; |
1584 | 0 | } |
1585 | | |
1586 | 0 | if (provision & PROVISION_SERVER) { |
1587 | | /* Derive the server key. */ |
1588 | 0 | WOLFSSL_MSG("Derive Server Key"); |
1589 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1590 | 0 | ssl->serverSecret, writeKeyLabel, |
1591 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1592 | 0 | WOLFSSL_SERVER_END); |
1593 | 0 | if (ret != 0) |
1594 | 0 | goto end; |
1595 | 0 | i += ssl->specs.key_size; |
1596 | 0 | } |
1597 | | |
1598 | 0 | if (provision & PROVISION_CLIENT) { |
1599 | | /* Derive the client IV. */ |
1600 | 0 | WOLFSSL_MSG("Derive Client IV"); |
1601 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1602 | 0 | ssl->clientSecret, writeIVLabel, |
1603 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1604 | 0 | WOLFSSL_CLIENT_END); |
1605 | 0 | if (ret != 0) |
1606 | 0 | goto end; |
1607 | 0 | i += ssl->specs.iv_size; |
1608 | 0 | } |
1609 | | |
1610 | 0 | if (provision & PROVISION_SERVER) { |
1611 | | /* Derive the server IV. */ |
1612 | 0 | WOLFSSL_MSG("Derive Server IV"); |
1613 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1614 | 0 | ssl->serverSecret, writeIVLabel, |
1615 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1616 | 0 | WOLFSSL_SERVER_END); |
1617 | 0 | if (ret != 0) |
1618 | 0 | goto end; |
1619 | 0 | i += ssl->specs.iv_size; |
1620 | 0 | } |
1621 | | |
1622 | | /* Store keys and IVs but don't activate them. */ |
1623 | 0 | ret = StoreKeys(ssl, key_dig, provision); |
1624 | |
|
1625 | | #ifdef WOLFSSL_DTLS13 |
1626 | | if (ret != 0) |
1627 | | goto end; |
1628 | | |
1629 | | if (ssl->options.dtls) { |
1630 | | w64wrapper epochNumber; |
1631 | | ret = Dtls13DeriveSnKeys(ssl, provision); |
1632 | | if (ret != 0) |
1633 | | goto end; |
1634 | | |
1635 | | switch (secret) { |
1636 | | case early_data_key: |
1637 | | epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA); |
1638 | | break; |
1639 | | case handshake_key: |
1640 | | epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
1641 | | break; |
1642 | | case traffic_key: |
1643 | | case no_key: |
1644 | | epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
1645 | | break; |
1646 | | case update_traffic_key: |
1647 | | if (side == ENCRYPT_SIDE_ONLY) { |
1648 | | epochNumber = ssl->dtls13Epoch; |
1649 | | } |
1650 | | else if (side == DECRYPT_SIDE_ONLY) { |
1651 | | epochNumber = ssl->dtls13PeerEpoch; |
1652 | | } |
1653 | | else { |
1654 | | ret = BAD_STATE_E; |
1655 | | goto end; |
1656 | | } |
1657 | | w64Increment(&epochNumber); |
1658 | | break; |
1659 | | default: |
1660 | | ret = BAD_STATE_E; |
1661 | | goto end; |
1662 | | } |
1663 | | ret = Dtls13NewEpoch(ssl, epochNumber, side); |
1664 | | if (ret != 0) |
1665 | | goto end; |
1666 | | } |
1667 | | |
1668 | | #endif /* WOLFSSL_DTLS13 */ |
1669 | |
|
1670 | 0 | end: |
1671 | 0 | ForceZero(key_dig, (word32)i); |
1672 | 0 | #ifdef WOLFSSL_SMALL_STACK |
1673 | 0 | XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1674 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1675 | | wc_MemZero_Check(key_dig, MAX_PRF_DIG); |
1676 | | #endif |
1677 | |
|
1678 | 0 | if (ret != 0) { |
1679 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
1680 | 0 | } |
1681 | |
|
1682 | 0 | return ret; |
1683 | 0 | } |
1684 | | |
1685 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) || defined(WOLFSSL_DTLS13) |
1686 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
1687 | | #ifndef NO_ASN_TIME |
1688 | | #if defined(USER_TICKS) |
1689 | | #if 0 |
1690 | | word32 TimeNowInMilliseconds(void) |
1691 | | { |
1692 | | /* |
1693 | | write your own clock tick function if don't want gettimeofday() |
1694 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1695 | | */ |
1696 | | } |
1697 | | #endif |
1698 | | |
1699 | | #elif defined(TIME_OVERRIDES) |
1700 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1701 | | word32 TimeNowInMilliseconds(void) |
1702 | | { |
1703 | | return (word32) wc_Time(0) * 1000; |
1704 | | } |
1705 | | #else |
1706 | | #ifndef HAVE_TIME_T_TYPE |
1707 | | typedef long time_t; |
1708 | | #endif |
1709 | | extern time_t XTIME(time_t * timer); |
1710 | | |
1711 | | /* The time in milliseconds. |
1712 | | * Used for tickets to represent difference between when first seen and when |
1713 | | * sending. |
1714 | | * |
1715 | | * returns the time in milliseconds as a 32-bit value. |
1716 | | */ |
1717 | | word32 TimeNowInMilliseconds(void) |
1718 | | { |
1719 | | return (word32) XTIME(0) * 1000; |
1720 | | } |
1721 | | #endif |
1722 | | |
1723 | | #elif defined(XTIME_MS) |
1724 | | word32 TimeNowInMilliseconds(void) |
1725 | | { |
1726 | | return (word32)XTIME_MS(0); |
1727 | | } |
1728 | | |
1729 | | #elif defined(USE_WINDOWS_API) |
1730 | | /* The time in milliseconds. |
1731 | | * Used for tickets to represent difference between when first seen and when |
1732 | | * sending. |
1733 | | * |
1734 | | * returns the time in milliseconds as a 32-bit value. |
1735 | | */ |
1736 | | word32 TimeNowInMilliseconds(void) |
1737 | | { |
1738 | | static int init = 0; |
1739 | | static LARGE_INTEGER freq; |
1740 | | LARGE_INTEGER count; |
1741 | | |
1742 | | if (!init) { |
1743 | | QueryPerformanceFrequency(&freq); |
1744 | | init = 1; |
1745 | | } |
1746 | | |
1747 | | QueryPerformanceCounter(&count); |
1748 | | |
1749 | | return (word32)(count.QuadPart / (freq.QuadPart / 1000)); |
1750 | | } |
1751 | | |
1752 | | #elif defined(HAVE_RTP_SYS) |
1753 | | #include "rtptime.h" |
1754 | | |
1755 | | /* The time in milliseconds. |
1756 | | * Used for tickets to represent difference between when first seen and when |
1757 | | * sending. |
1758 | | * |
1759 | | * returns the time in milliseconds as a 32-bit value. |
1760 | | */ |
1761 | | word32 TimeNowInMilliseconds(void) |
1762 | | { |
1763 | | return (word32)rtp_get_system_sec() * 1000; |
1764 | | } |
1765 | | #elif defined(WOLFSSL_DEOS) |
1766 | | word32 TimeNowInMilliseconds(void) |
1767 | | { |
1768 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
1769 | | word32 *systemTickPtr = systemTickPointer(); |
1770 | | |
1771 | | return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000; |
1772 | | } |
1773 | | #elif defined(MICRIUM) |
1774 | | /* The time in milliseconds. |
1775 | | * Used for tickets to represent difference between when first seen and when |
1776 | | * sending. |
1777 | | * |
1778 | | * returns the time in milliseconds as a 32-bit value. |
1779 | | */ |
1780 | | word32 TimeNowInMilliseconds(void) |
1781 | | { |
1782 | | OS_TICK ticks = 0; |
1783 | | OS_ERR err; |
1784 | | |
1785 | | ticks = OSTimeGet(&err); |
1786 | | |
1787 | | return (word32) (ticks / OSCfg_TickRate_Hz) * 1000; |
1788 | | } |
1789 | | #elif defined(MICROCHIP_TCPIP_V5) |
1790 | | /* The time in milliseconds. |
1791 | | * Used for tickets to represent difference between when first seen and when |
1792 | | * sending. |
1793 | | * |
1794 | | * returns the time in milliseconds as a 32-bit value. |
1795 | | */ |
1796 | | word32 TimeNowInMilliseconds(void) |
1797 | | { |
1798 | | return (word32) (TickGet() / (TICKS_PER_SECOND / 1000)); |
1799 | | } |
1800 | | #elif defined(MICROCHIP_TCPIP) |
1801 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
1802 | | #include <system/tmr/sys_tmr.h> |
1803 | | |
1804 | | /* The time in milliseconds. |
1805 | | * Used for tickets to represent difference between when first seen and when |
1806 | | * sending. |
1807 | | * |
1808 | | * returns the time in milliseconds as a 32-bit value. |
1809 | | */ |
1810 | | word32 TimeNowInMilliseconds(void) |
1811 | | { |
1812 | | return (word32)(SYS_TMR_TickCountGet() / |
1813 | | (SYS_TMR_TickCounterFrequencyGet() / 1000)); |
1814 | | } |
1815 | | #else |
1816 | | /* The time in milliseconds. |
1817 | | * Used for tickets to represent difference between when first seen and when |
1818 | | * sending. |
1819 | | * |
1820 | | * returns the time in milliseconds as a 32-bit value. |
1821 | | */ |
1822 | | word32 TimeNowInMilliseconds(void) |
1823 | | { |
1824 | | return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000)); |
1825 | | } |
1826 | | |
1827 | | #endif |
1828 | | |
1829 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1830 | | /* The time in milliseconds. |
1831 | | * Used for tickets to represent difference between when first seen and when |
1832 | | * sending. |
1833 | | * |
1834 | | * returns the time in milliseconds as a 32-bit value. |
1835 | | */ |
1836 | | word32 TimeNowInMilliseconds(void) |
1837 | | { |
1838 | | TIME_STRUCT mqxTime; |
1839 | | |
1840 | | _time_get_elapsed(&mqxTime); |
1841 | | |
1842 | | return (word32) mqxTime.SECONDS * 1000; |
1843 | | } |
1844 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
1845 | | #include "include/task.h" |
1846 | | |
1847 | | /* The time in milliseconds. |
1848 | | * Used for tickets to represent difference between when first seen and when |
1849 | | * sending. |
1850 | | * |
1851 | | * returns the time in milliseconds as a 32-bit value. |
1852 | | */ |
1853 | | word32 TimeNowInMilliseconds(void) |
1854 | | { |
1855 | | return (unsigned int)(((float)xTaskGetTickCount()) / |
1856 | | (configTICK_RATE_HZ / 1000)); |
1857 | | } |
1858 | | #elif defined(FREESCALE_KSDK_BM) |
1859 | | #include "lwip/sys.h" /* lwIP */ |
1860 | | |
1861 | | /* The time in milliseconds. |
1862 | | * Used for tickets to represent difference between when first seen and when |
1863 | | * sending. |
1864 | | * |
1865 | | * returns the time in milliseconds as a 32-bit value. |
1866 | | */ |
1867 | | word32 TimeNowInMilliseconds(void) |
1868 | | { |
1869 | | return sys_now(); |
1870 | | } |
1871 | | |
1872 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
1873 | | |
1874 | | word32 TimeNowInMilliseconds(void) |
1875 | | { |
1876 | | return (word32)osKernelGetTickCount(); |
1877 | | } |
1878 | | |
1879 | | #elif defined(WOLFSSL_TIRTOS) |
1880 | | /* The time in milliseconds. |
1881 | | * Used for tickets to represent difference between when first seen and when |
1882 | | * sending. |
1883 | | * |
1884 | | * returns the time in milliseconds as a 32-bit value. |
1885 | | */ |
1886 | | word32 TimeNowInMilliseconds(void) |
1887 | | { |
1888 | | return (word32) Seconds_get() * 1000; |
1889 | | } |
1890 | | #elif defined(WOLFSSL_UTASKER) |
1891 | | /* The time in milliseconds. |
1892 | | * Used for tickets to represent difference between when first seen and when |
1893 | | * sending. |
1894 | | * |
1895 | | * returns the time in milliseconds as a 32-bit value. |
1896 | | */ |
1897 | | word32 TimeNowInMilliseconds(void) |
1898 | | { |
1899 | | return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
1900 | | } |
1901 | | #elif defined(WOLFSSL_LINUXKM) |
1902 | | word32 TimeNowInMilliseconds(void) |
1903 | | { |
1904 | | s64 t; |
1905 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
1906 | | struct timespec ts; |
1907 | | getnstimeofday(&ts); |
1908 | | t = ts.tv_sec * (s64)1000; |
1909 | | t += ts.tv_nsec / (s64)1000000; |
1910 | | #else |
1911 | | struct timespec64 ts; |
1912 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
1913 | | ts = current_kernel_time64(); |
1914 | | #else |
1915 | | ktime_get_coarse_real_ts64(&ts); |
1916 | | #endif |
1917 | | t = ts.tv_sec * 1000L; |
1918 | | t += ts.tv_nsec / 1000000L; |
1919 | | #endif |
1920 | | return (word32)t; |
1921 | | } |
1922 | | #elif defined(WOLFSSL_QNX_CAAM) |
1923 | | word32 TimeNowInMilliseconds(void) |
1924 | | { |
1925 | | struct timespec now; |
1926 | | clock_gettime(CLOCK_REALTIME, &now); |
1927 | | return (word32)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
1928 | | } |
1929 | | #elif defined(FUSION_RTOS) |
1930 | | /* The time in milliseconds. |
1931 | | * Used for tickets to represent difference between when first seen and when |
1932 | | * sending. |
1933 | | * |
1934 | | * returns the time in milliseconds as a 32-bit value. |
1935 | | */ |
1936 | | word32 TimeNowInMilliseconds(void) |
1937 | | { |
1938 | | struct timeval now; |
1939 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
1940 | | return 0; |
1941 | | |
1942 | | /* Convert to milliseconds number. */ |
1943 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1944 | | } |
1945 | | #elif defined(WOLFSSL_ZEPHYR) |
1946 | | word32 TimeNowInMilliseconds(void) |
1947 | | { |
1948 | | int64_t t; |
1949 | | #if defined(CONFIG_ARCH_POSIX) |
1950 | | k_cpu_idle(); |
1951 | | #endif |
1952 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
1953 | | return (word32)t; |
1954 | | } |
1955 | | #elif defined(FREERTOS) |
1956 | | word32 TimeNowInMilliseconds(void) |
1957 | | { |
1958 | | return (word32)((uint64_t)(xTaskGetTickCount() * 1000) / |
1959 | | configTICK_RATE_HZ); |
1960 | | } |
1961 | | #else |
1962 | | /* The time in milliseconds. |
1963 | | * Used for tickets to represent difference between when first seen and when |
1964 | | * sending. |
1965 | | * |
1966 | | * returns the time in milliseconds as a 32-bit value. |
1967 | | */ |
1968 | | word32 TimeNowInMilliseconds(void) |
1969 | | { |
1970 | | struct timeval now; |
1971 | | |
1972 | | if (gettimeofday(&now, 0) < 0) |
1973 | | return 0; |
1974 | | |
1975 | | /* Convert to milliseconds number. */ |
1976 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1977 | | } |
1978 | | #endif |
1979 | | #else |
1980 | | /* user must supply time in milliseconds function: |
1981 | | * word32 TimeNowInMilliseconds(void); |
1982 | | * The response is milliseconds elapsed |
1983 | | */ |
1984 | | #endif /* !NO_ASN_TIME */ |
1985 | | #else |
1986 | | #ifndef NO_ASN_TIME |
1987 | | #if defined(USER_TICKS) |
1988 | | #if 0 |
1989 | | sword64 TimeNowInMilliseconds(void) |
1990 | | { |
1991 | | /* |
1992 | | write your own clock tick function if don't want gettimeofday() |
1993 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1994 | | */ |
1995 | | } |
1996 | | #endif |
1997 | | |
1998 | | #elif defined(TIME_OVERRIDES) |
1999 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
2000 | | sword64 TimeNowInMilliseconds(void) |
2001 | | { |
2002 | | return (sword64) wc_Time(0) * 1000; |
2003 | | } |
2004 | | #else |
2005 | | #ifndef HAVE_TIME_T_TYPE |
2006 | | typedef long time_t; |
2007 | | #endif |
2008 | | extern time_t XTIME(time_t * timer); |
2009 | | |
2010 | | /* The time in milliseconds. |
2011 | | * Used for tickets to represent difference between when first seen and when |
2012 | | * sending. |
2013 | | * |
2014 | | * returns the time in milliseconds as a 32-bit value. |
2015 | | */ |
2016 | | sword64 TimeNowInMilliseconds(void) |
2017 | | { |
2018 | | return (sword64) XTIME(0) * 1000; |
2019 | | } |
2020 | | #endif |
2021 | | |
2022 | | #elif defined(XTIME_MS) |
2023 | | sword64 TimeNowInMilliseconds(void) |
2024 | | { |
2025 | | return (sword64)XTIME_MS(0); |
2026 | | } |
2027 | | |
2028 | | #elif defined(USE_WINDOWS_API) |
2029 | | /* The time in milliseconds. |
2030 | | * Used for tickets to represent difference between when first seen and when |
2031 | | * sending. |
2032 | | * |
2033 | | * returns the time in milliseconds as a 64-bit value. |
2034 | | */ |
2035 | | sword64 TimeNowInMilliseconds(void) |
2036 | | { |
2037 | | static int init = 0; |
2038 | | static LARGE_INTEGER freq; |
2039 | | LARGE_INTEGER count; |
2040 | | |
2041 | | if (!init) { |
2042 | | QueryPerformanceFrequency(&freq); |
2043 | | init = 1; |
2044 | | } |
2045 | | |
2046 | | QueryPerformanceCounter(&count); |
2047 | | |
2048 | | return (sword64)(count.QuadPart / (freq.QuadPart / 1000)); |
2049 | | } |
2050 | | |
2051 | | #elif defined(HAVE_RTP_SYS) |
2052 | | #include "rtptime.h" |
2053 | | |
2054 | | /* The time in milliseconds. |
2055 | | * Used for tickets to represent difference between when first seen and when |
2056 | | * sending. |
2057 | | * |
2058 | | * returns the time in milliseconds as a 64-bit value. |
2059 | | */ |
2060 | | sword64 TimeNowInMilliseconds(void) |
2061 | | { |
2062 | | return (sword64)rtp_get_system_sec() * 1000; |
2063 | | } |
2064 | | #elif defined(WOLFSSL_DEOS) |
2065 | | sword64 TimeNowInMilliseconds(void) |
2066 | | { |
2067 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2068 | | word32 *systemTickPtr = systemTickPointer(); |
2069 | | |
2070 | | return (sword64) (*systemTickPtr/systemTickTimeInHz) * 1000; |
2071 | | } |
2072 | | #elif defined(MICRIUM) |
2073 | | /* The time in milliseconds. |
2074 | | * Used for tickets to represent difference between when first seen and when |
2075 | | * sending. |
2076 | | * |
2077 | | * returns the time in milliseconds as a 64-bit value. |
2078 | | */ |
2079 | | sword64 TimeNowInMilliseconds(void) |
2080 | | { |
2081 | | OS_TICK ticks = 0; |
2082 | | OS_ERR err; |
2083 | | |
2084 | | ticks = OSTimeGet(&err); |
2085 | | |
2086 | | return (sword64) (ticks / OSCfg_TickRate_Hz) * 1000; |
2087 | | } |
2088 | | #elif defined(MICROCHIP_TCPIP_V5) |
2089 | | /* The time in milliseconds. |
2090 | | * Used for tickets to represent difference between when first seen and when |
2091 | | * sending. |
2092 | | * |
2093 | | * returns the time in milliseconds as a 64-bit value. |
2094 | | */ |
2095 | | sword64 TimeNowInMilliseconds(void) |
2096 | | { |
2097 | | return (sword64) (TickGet() / (TICKS_PER_SECOND / 1000)); |
2098 | | } |
2099 | | #elif defined(MICROCHIP_TCPIP) |
2100 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
2101 | | #include <system/tmr/sys_tmr.h> |
2102 | | |
2103 | | /* The time in milliseconds. |
2104 | | * Used for tickets to represent difference between when first seen and when |
2105 | | * sending. |
2106 | | * |
2107 | | * returns the time in milliseconds as a 64-bit value. |
2108 | | */ |
2109 | | sword64 TimeNowInMilliseconds(void) |
2110 | | { |
2111 | | return (sword64)SYS_TMR_TickCountGet() / |
2112 | | (SYS_TMR_TickCounterFrequencyGet() / 1000); |
2113 | | } |
2114 | | #else |
2115 | | /* The time in milliseconds. |
2116 | | * Used for tickets to represent difference between when first seen and when |
2117 | | * sending. |
2118 | | * |
2119 | | * returns the time in milliseconds as a 64-bit value. |
2120 | | */ |
2121 | | sword64 TimeNowInMilliseconds(void) |
2122 | | { |
2123 | | return (sword64)SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000); |
2124 | | } |
2125 | | |
2126 | | #endif |
2127 | | |
2128 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2129 | | /* The time in milliseconds. |
2130 | | * Used for tickets to represent difference between when first seen and when |
2131 | | * sending. |
2132 | | * |
2133 | | * returns the time in milliseconds as a 64-bit value. |
2134 | | */ |
2135 | | sword64 TimeNowInMilliseconds(void) |
2136 | | { |
2137 | | TIME_STRUCT mqxTime; |
2138 | | |
2139 | | _time_get_elapsed(&mqxTime); |
2140 | | |
2141 | | return (sword64) mqxTime.SECONDS * 1000; |
2142 | | } |
2143 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
2144 | | #include "include/task.h" |
2145 | | |
2146 | | /* The time in milliseconds. |
2147 | | * Used for tickets to represent difference between when first seen and when |
2148 | | * sending. |
2149 | | * |
2150 | | * returns the time in milliseconds as a 64-bit value. |
2151 | | */ |
2152 | | sword64 TimeNowInMilliseconds(void) |
2153 | | { |
2154 | | return (sword64)xTaskGetTickCount() / (configTICK_RATE_HZ / 1000); |
2155 | | } |
2156 | | #elif defined(FREESCALE_KSDK_BM) |
2157 | | #include "lwip/sys.h" /* lwIP */ |
2158 | | |
2159 | | /* The time in milliseconds. |
2160 | | * Used for tickets to represent difference between when first seen and when |
2161 | | * sending. |
2162 | | * |
2163 | | * returns the time in milliseconds as a 64-bit value. |
2164 | | */ |
2165 | | sword64 TimeNowInMilliseconds(void) |
2166 | | { |
2167 | | return sys_now(); |
2168 | | } |
2169 | | |
2170 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
2171 | | |
2172 | | sword64 TimeNowInMilliseconds(void) |
2173 | | { |
2174 | | return (sword64)osKernelGetTickCount(); |
2175 | | } |
2176 | | |
2177 | | #elif defined(WOLFSSL_TIRTOS) |
2178 | | /* The time in milliseconds. |
2179 | | * Used for tickets to represent difference between when first seen and when |
2180 | | * sending. |
2181 | | * |
2182 | | * returns the time in milliseconds as a 64-bit value. |
2183 | | */ |
2184 | | sword64 TimeNowInMilliseconds(void) |
2185 | | { |
2186 | | return (sword64) Seconds_get() * 1000; |
2187 | | } |
2188 | | #elif defined(WOLFSSL_UTASKER) |
2189 | | /* The time in milliseconds. |
2190 | | * Used for tickets to represent difference between when first seen and when |
2191 | | * sending. |
2192 | | * |
2193 | | * returns the time in milliseconds as a 64-bit value. |
2194 | | */ |
2195 | | sword64 TimeNowInMilliseconds(void) |
2196 | | { |
2197 | | return (sword64)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
2198 | | } |
2199 | | #elif defined(WOLFSSL_LINUXKM) |
2200 | | sword64 TimeNowInMilliseconds(void) |
2201 | | { |
2202 | | s64 t; |
2203 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2204 | | struct timespec ts; |
2205 | | getnstimeofday(&ts); |
2206 | | t = ts.tv_sec * (s64)1000; |
2207 | | t += ts.tv_nsec / (s64)1000000; |
2208 | | #else |
2209 | | struct timespec64 ts; |
2210 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2211 | | ts = current_kernel_time64(); |
2212 | | #else |
2213 | | ktime_get_coarse_real_ts64(&ts); |
2214 | | #endif |
2215 | | t = ts.tv_sec * 1000L; |
2216 | | t += ts.tv_nsec / 1000000L; |
2217 | | #endif |
2218 | | return (sword64)t; |
2219 | | } |
2220 | | #elif defined(WOLFSSL_QNX_CAAM) |
2221 | | sword64 TimeNowInMilliseconds(void) |
2222 | | { |
2223 | | struct timespec now; |
2224 | | clock_gettime(CLOCK_REALTIME, &now); |
2225 | | return (sword64)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
2226 | | } |
2227 | | #elif defined(FUSION_RTOS) |
2228 | | /* The time in milliseconds. |
2229 | | * Used for tickets to represent difference between when first seen and when |
2230 | | * sending. |
2231 | | * |
2232 | | * returns the time in milliseconds as a 64-bit value. |
2233 | | */ |
2234 | | sword64 TimeNowInMilliseconds(void) |
2235 | | { |
2236 | | struct timeval now; |
2237 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2238 | | return 0; |
2239 | | |
2240 | | /* Convert to milliseconds number. */ |
2241 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2242 | | } |
2243 | | #elif defined(WOLFSSL_ZEPHYR) |
2244 | | sword64 TimeNowInMilliseconds(void) |
2245 | | { |
2246 | | int64_t t; |
2247 | | #if defined(CONFIG_ARCH_POSIX) |
2248 | | k_cpu_idle(); |
2249 | | #endif |
2250 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2251 | | return (sword64)t; |
2252 | | } |
2253 | | #elif defined(FREERTOS) |
2254 | | sword64 TimeNowInMilliseconds(void) |
2255 | | { |
2256 | | return (sword64)((uint64_t)(xTaskGetTickCount() * 1000) / |
2257 | | configTICK_RATE_HZ); |
2258 | | } |
2259 | | #else |
2260 | | /* The time in milliseconds. |
2261 | | * Used for tickets to represent difference between when first seen and when |
2262 | | * sending. |
2263 | | * |
2264 | | * returns the time in milliseconds as a 64-bit value. |
2265 | | */ |
2266 | | sword64 TimeNowInMilliseconds(void) |
2267 | | { |
2268 | | struct timeval now; |
2269 | | |
2270 | | if (gettimeofday(&now, 0) < 0) |
2271 | | return 0; |
2272 | | |
2273 | | /* Convert to milliseconds number. */ |
2274 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2275 | | } |
2276 | | #endif |
2277 | | #else |
2278 | | /* user must supply time in milliseconds function: |
2279 | | * sword64 TimeNowInMilliseconds(void); |
2280 | | * The response is milliseconds elapsed |
2281 | | */ |
2282 | | #endif /* !NO_ASN_TIME */ |
2283 | | #endif /* WOLFSSL_32BIT_MILLI_TIME */ |
2284 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK || WOLFSSL_DTLS13 */ |
2285 | | |
2286 | | /* Add record layer header to message. |
2287 | | * |
2288 | | * output The buffer to write the record layer header into. |
2289 | | * length The length of the record data. |
2290 | | * type The type of record message. |
2291 | | * ssl The SSL/TLS object. |
2292 | | */ |
2293 | | static void AddTls13RecordHeader(byte* output, word32 length, byte type, |
2294 | | WOLFSSL* ssl) |
2295 | 5.69k | { |
2296 | 5.69k | RecordLayerHeader* rl; |
2297 | | |
2298 | 5.69k | rl = (RecordLayerHeader*)output; |
2299 | 5.69k | rl->type = type; |
2300 | 5.69k | rl->pvMajor = ssl->version.major; |
2301 | | /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */ |
2302 | 5.69k | rl->pvMinor = TLSv1_2_MINOR; |
2303 | 5.69k | c16toa((word16)length, rl->length); |
2304 | 5.69k | } |
2305 | | |
2306 | | /* Add handshake header to message. |
2307 | | * |
2308 | | * output The buffer to write the handshake header into. |
2309 | | * length The length of the handshake data. |
2310 | | * fragOffset The offset of the fragment data. (DTLS) |
2311 | | * fragLength The length of the fragment data. (DTLS) |
2312 | | * type The type of handshake message. |
2313 | | * ssl The SSL/TLS object. (DTLS) |
2314 | | */ |
2315 | | static void AddTls13HandShakeHeader(byte* output, word32 length, |
2316 | | word32 fragOffset, word32 fragLength, |
2317 | | byte type, WOLFSSL* ssl) |
2318 | | { |
2319 | | HandShakeHeader* hs; |
2320 | | (void)fragOffset; |
2321 | | (void)fragLength; |
2322 | | (void)ssl; |
2323 | | |
2324 | | #ifdef WOLFSSL_DTLS13 |
2325 | | /* message_hash type is used for a synthetic message that replaces the first |
2326 | | ClientHello in the hash transcript when using HelloRetryRequest. It will |
2327 | | never be transmitted and, as the DTLS-only fields must not be considered |
2328 | | when computing the hash transcript, we can avoid to use the DTLS |
2329 | | handshake header. */ |
2330 | | if (ssl->options.dtls && type != message_hash) { |
2331 | | Dtls13HandshakeAddHeader(ssl, output, (enum HandShakeType)type, length); |
2332 | | return; |
2333 | | } |
2334 | | #endif /* WOLFSSL_DTLS13 */ |
2335 | | |
2336 | | /* handshake header */ |
2337 | | hs = (HandShakeHeader*)output; |
2338 | | hs->type = type; |
2339 | | c32to24(length, hs->length); |
2340 | | } |
2341 | | |
2342 | | |
2343 | | /* Add both record layer and handshake header to message. |
2344 | | * |
2345 | | * output The buffer to write the headers into. |
2346 | | * length The length of the handshake data. |
2347 | | * type The type of record layer message. |
2348 | | * ssl The SSL/TLS object. (DTLS) |
2349 | | */ |
2350 | | static void AddTls13Headers(byte* output, word32 length, byte type, |
2351 | | WOLFSSL* ssl) |
2352 | | { |
2353 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2354 | | word32 outputAdj = RECORD_HEADER_SZ; |
2355 | | |
2356 | | #ifdef WOLFSSL_DTLS13 |
2357 | | if (ssl->options.dtls) { |
2358 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2359 | | return; |
2360 | | } |
2361 | | #endif /* WOLFSSL_DTLS13 */ |
2362 | | |
2363 | | AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl); |
2364 | | AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl); |
2365 | | } |
2366 | | |
2367 | | #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) \ |
2368 | | && !defined(NO_CERTS) |
2369 | | /* Add both record layer and fragment handshake header to message. |
2370 | | * |
2371 | | * output The buffer to write the headers into. |
2372 | | * fragOffset The offset of the fragment data. (DTLS) |
2373 | | * fragLength The length of the fragment data. (DTLS) |
2374 | | * length The length of the handshake data. |
2375 | | * type The type of record layer message. |
2376 | | * ssl The SSL/TLS object. (DTLS) |
2377 | | */ |
2378 | | static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset, |
2379 | | word32 length, byte type, WOLFSSL* ssl) |
2380 | | { |
2381 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2382 | | word32 outputAdj = RECORD_HEADER_SZ; |
2383 | | (void)fragSz; |
2384 | | |
2385 | | #ifdef WOLFSSL_DTLS13 |
2386 | | /* we ignore fragmentation fields here because fragmentation logic for |
2387 | | DTLS1.3 is inside dtls13_handshake_send(). */ |
2388 | | if (ssl->options.dtls) { |
2389 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2390 | | return; |
2391 | | } |
2392 | | #endif /* WOLFSSL_DTLS13 */ |
2393 | | |
2394 | | AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl); |
2395 | | AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz, |
2396 | | type, ssl); |
2397 | | } |
2398 | | #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !NO_CERTS */ |
2399 | | |
2400 | | /* Write the sequence number into the buffer. |
2401 | | * No DTLS v1.3 support. |
2402 | | * |
2403 | | * ssl The SSL/TLS object. |
2404 | | * verifyOrder Which set of sequence numbers to use. |
2405 | | * out The buffer to write into. |
2406 | | */ |
2407 | | static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out) |
2408 | 7.71k | { |
2409 | 7.71k | word32 seq[2] = {0, 0}; |
2410 | | |
2411 | 7.71k | if (ssl->options.dtls) { |
2412 | | #ifdef WOLFSSL_DTLS13 |
2413 | | Dtls13GetSeq(ssl, verifyOrder, seq, 1); |
2414 | | #endif /* WOLFSSL_DTLS13 */ |
2415 | 0 | } |
2416 | 7.71k | else if (verifyOrder == PEER_ORDER) { |
2417 | 5.50k | seq[0] = ssl->keys.peer_sequence_number_hi; |
2418 | 5.50k | seq[1] = ssl->keys.peer_sequence_number_lo++; |
2419 | | /* handle rollover */ |
2420 | 5.50k | if (seq[1] > ssl->keys.peer_sequence_number_lo) |
2421 | 11 | ssl->keys.peer_sequence_number_hi++; |
2422 | 5.50k | } |
2423 | 2.20k | else { |
2424 | 2.20k | seq[0] = ssl->keys.sequence_number_hi; |
2425 | 2.20k | seq[1] = ssl->keys.sequence_number_lo++; |
2426 | | /* handle rollover */ |
2427 | 2.20k | if (seq[1] > ssl->keys.sequence_number_lo) |
2428 | 0 | ssl->keys.sequence_number_hi++; |
2429 | 2.20k | } |
2430 | | #ifdef WOLFSSL_DEBUG_TLS |
2431 | | WOLFSSL_MSG_EX("TLS 1.3 Write Sequence %d %d", seq[0], seq[1]); |
2432 | | #endif |
2433 | | |
2434 | 7.71k | c32toa(seq[0], out); |
2435 | 7.71k | c32toa(seq[1], out + OPAQUE32_LEN); |
2436 | 7.71k | } |
2437 | | |
2438 | | /* Build the nonce for TLS v1.3 encryption and decryption. |
2439 | | * |
2440 | | * ssl The SSL/TLS object. |
2441 | | * nonce The nonce data to use when encrypting or decrypting. |
2442 | | * iv The derived IV. |
2443 | | * order The side on which the message is to be or was sent. |
2444 | | */ |
2445 | | static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv, |
2446 | | int ivSz, int order) |
2447 | 7.71k | { |
2448 | 7.71k | int seq_offset; |
2449 | | /* Ensure minimum nonce size for standard AEAD ciphers */ |
2450 | 7.71k | if (ivSz < AEAD_NONCE_SZ) |
2451 | 0 | ivSz = AEAD_NONCE_SZ; |
2452 | 7.71k | seq_offset = ivSz - SEQ_SZ; |
2453 | | /* The nonce is the IV with the sequence XORed into the last bytes. */ |
2454 | 7.71k | WriteSEQTls13(ssl, order, nonce + seq_offset); |
2455 | 7.71k | XMEMCPY(nonce, iv, seq_offset); |
2456 | 7.71k | xorbuf(nonce + seq_offset, iv + seq_offset, SEQ_SZ); |
2457 | 7.71k | } |
2458 | | |
2459 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2460 | | /* Encrypt with ChaCha20 and create authentication tag with Poly1305. |
2461 | | * |
2462 | | * ssl The SSL/TLS object. |
2463 | | * output The buffer to write encrypted data and authentication tag into. |
2464 | | * May be the same pointer as input. |
2465 | | * input The data to encrypt. |
2466 | | * sz The number of bytes to encrypt. |
2467 | | * nonce The nonce to use with ChaCha20. |
2468 | | * aad The additional authentication data. |
2469 | | * aadSz The size of the addition authentication data. |
2470 | | * tag The authentication tag buffer. |
2471 | | * returns 0 on success, otherwise failure. |
2472 | | */ |
2473 | | static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output, |
2474 | | const byte* input, word16 sz, byte* nonce, |
2475 | | const byte* aad, word16 aadSz, byte* tag) |
2476 | 623 | { |
2477 | 623 | int ret = 0; |
2478 | 623 | byte poly[CHACHA20_256_KEY_SIZE]; |
2479 | | |
2480 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2481 | 623 | XMEMSET(poly, 0, sizeof(poly)); |
2482 | | |
2483 | | /* Set the nonce for ChaCha and get Poly1305 key. */ |
2484 | 623 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0); |
2485 | 623 | if (ret != 0) |
2486 | 0 | return ret; |
2487 | | /* Create Poly1305 key using ChaCha20 keystream. */ |
2488 | 623 | ret = wc_Chacha_Process(ssl->encrypt.chacha, poly, poly, sizeof(poly)); |
2489 | 623 | if (ret != 0) |
2490 | 0 | return ret; |
2491 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2492 | | wc_MemZero_Add("ChaCha20Poly1305_Encrypt poly", poly, sizeof(poly)); |
2493 | | #endif |
2494 | 623 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1); |
2495 | 623 | if (ret != 0) |
2496 | 0 | return ret; |
2497 | | /* Encrypt the plain text. */ |
2498 | 623 | ret = wc_Chacha_Process(ssl->encrypt.chacha, output, input, sz); |
2499 | 623 | if (ret != 0) { |
2500 | 0 | ForceZero(poly, sizeof(poly)); |
2501 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2502 | | wc_MemZero_Check(poly, sizeof(poly)); |
2503 | | #endif |
2504 | 0 | return ret; |
2505 | 0 | } |
2506 | | |
2507 | | /* Set key for Poly1305. */ |
2508 | 623 | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2509 | 623 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2510 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2511 | | wc_MemZero_Check(poly, sizeof(poly)); |
2512 | | #endif |
2513 | 623 | if (ret != 0) |
2514 | 0 | return ret; |
2515 | | /* Add authentication code of encrypted data to end. */ |
2516 | 623 | ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, output, sz, tag, |
2517 | 623 | POLY1305_AUTH_SZ); |
2518 | | |
2519 | 623 | return ret; |
2520 | 623 | } |
2521 | | #endif |
2522 | | |
2523 | | #ifdef HAVE_NULL_CIPHER |
2524 | | /* Create authentication tag and copy data over input. |
2525 | | * |
2526 | | * ssl The SSL/TLS object. |
2527 | | * output The buffer to copy data into. |
2528 | | * May be the same pointer as input. |
2529 | | * input The data. |
2530 | | * sz The number of bytes of data. |
2531 | | * nonce The nonce to use with authentication. |
2532 | | * aad The additional authentication data. |
2533 | | * aadSz The size of the addition authentication data. |
2534 | | * tag The authentication tag buffer. |
2535 | | * returns 0 on success, otherwise failure. |
2536 | | */ |
2537 | | static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output, |
2538 | | const byte* input, word16 sz, |
2539 | | const byte* nonce, |
2540 | | const byte* aad, word16 aadSz, byte* tag) |
2541 | | { |
2542 | | int ret; |
2543 | | |
2544 | | /* HMAC: nonce | aad | input */ |
2545 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, ssl->specs.iv_size); |
2546 | | if (ret == 0) |
2547 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz); |
2548 | | if (ret == 0) |
2549 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz); |
2550 | | if (ret == 0) |
2551 | | ret = wc_HmacFinal(ssl->encrypt.hmac, tag); |
2552 | | /* Copy the input to output if not the same buffer */ |
2553 | | if (ret == 0 && output != input) |
2554 | | XMEMCPY(output, input, sz); |
2555 | | return ret; |
2556 | | } |
2557 | | #endif |
2558 | | |
2559 | | /* Encrypt data for TLS v1.3. |
2560 | | * |
2561 | | * ssl The SSL/TLS object. |
2562 | | * output The buffer to write encrypted data and authentication tag into. |
2563 | | * May be the same pointer as input. |
2564 | | * input The record header and data to encrypt. |
2565 | | * sz The number of bytes to encrypt. |
2566 | | * aad The additional authentication data. |
2567 | | * aadSz The size of the addition authentication data. |
2568 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
2569 | | * returns 0 on success, otherwise failure. |
2570 | | */ |
2571 | | static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, |
2572 | | word16 sz, const byte* aad, word16 aadSz, int asyncOkay) |
2573 | 0 | { |
2574 | 0 | int ret = 0; |
2575 | 0 | word16 dataSz = sz - ssl->specs.aead_mac_size; |
2576 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2577 | 0 | word32 nonceSz = 0; |
2578 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2579 | | WC_ASYNC_DEV* asyncDev = NULL; |
2580 | | word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN; |
2581 | | #endif |
2582 | |
|
2583 | 0 | WOLFSSL_ENTER("EncryptTls13"); |
2584 | |
|
2585 | 0 | (void)output; |
2586 | 0 | (void)input; |
2587 | 0 | (void)sz; |
2588 | 0 | (void)dataSz; |
2589 | 0 | (void)macSz; |
2590 | 0 | (void)asyncOkay; |
2591 | 0 | (void)nonceSz; |
2592 | |
|
2593 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2594 | | if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2595 | | ssl->error = 0; /* clear async */ |
2596 | | } |
2597 | | #endif |
2598 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2599 | | ret = tsip_Tls13AesEncrypt(ssl, output, input, dataSz); |
2600 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2601 | | if (ret > 0) { |
2602 | | ret = 0; /* tsip_Tls13AesEncrypt returns output size */ |
2603 | | } |
2604 | | return ret; |
2605 | | } |
2606 | | ret = 0; |
2607 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
2608 | |
|
2609 | 0 | switch (ssl->encrypt.state) { |
2610 | 0 | case CIPHER_STATE_BEGIN: |
2611 | 0 | { |
2612 | | #ifdef WOLFSSL_DEBUG_TLS |
2613 | | WOLFSSL_MSG("Data to encrypt"); |
2614 | | WOLFSSL_BUFFER(input, dataSz); |
2615 | | WOLFSSL_MSG("Additional Authentication Data"); |
2616 | | WOLFSSL_BUFFER(aad, aadSz); |
2617 | | #endif |
2618 | |
|
2619 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2620 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) { |
2621 | | XMEMCPY(ssl->encrypt.sanityCheck, input, |
2622 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))); |
2623 | | } |
2624 | | #endif |
2625 | |
|
2626 | 0 | #ifdef CIPHER_NONCE |
2627 | 0 | if (ssl->encrypt.nonce == NULL) { |
2628 | 0 | ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
2629 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
2630 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2631 | | if (ssl->encrypt.nonce != NULL) { |
2632 | | wc_MemZero_Add("EncryptTls13 nonce", ssl->encrypt.nonce, |
2633 | | ssl->specs.iv_size); |
2634 | | } |
2635 | | #endif |
2636 | 0 | } |
2637 | 0 | if (ssl->encrypt.nonce == NULL) |
2638 | 0 | return MEMORY_E; |
2639 | | |
2640 | 0 | BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV, |
2641 | 0 | ssl->specs.iv_size, CUR_ORDER); |
2642 | 0 | #endif |
2643 | | |
2644 | | /* Advance state and proceed */ |
2645 | 0 | ssl->encrypt.state = CIPHER_STATE_DO; |
2646 | 0 | } |
2647 | 0 | FALL_THROUGH; |
2648 | |
|
2649 | 0 | case CIPHER_STATE_DO: |
2650 | 0 | { |
2651 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
2652 | 0 | #ifdef BUILD_AESGCM |
2653 | 0 | case wolfssl_aes_gcm: |
2654 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2655 | | /* initialize event */ |
2656 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2657 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2658 | | if (ret != 0) |
2659 | | break; |
2660 | | #endif |
2661 | |
|
2662 | 0 | nonceSz = AESGCM_NONCE_SZ; |
2663 | |
|
2664 | | #if defined(HAVE_PK_CALLBACKS) |
2665 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2666 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2667 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2668 | | output, input, dataSz, |
2669 | | ssl->encrypt.nonce, nonceSz, |
2670 | | output + dataSz, macSz, |
2671 | | aad, aadSz); |
2672 | | } |
2673 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2674 | | #endif |
2675 | 0 | { |
2676 | |
|
2677 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2678 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2679 | | ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, |
2680 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2681 | | output + dataSz, macSz, aad, aadSz); |
2682 | | #else |
2683 | 0 | ret = wc_AesGcmSetExtIV(ssl->encrypt.aes, |
2684 | 0 | ssl->encrypt.nonce, nonceSz); |
2685 | 0 | if (ret == 0) { |
2686 | 0 | ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output, |
2687 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2688 | 0 | output + dataSz, macSz, aad, aadSz); |
2689 | 0 | } |
2690 | 0 | #endif |
2691 | 0 | } |
2692 | 0 | break; |
2693 | 0 | #endif |
2694 | | |
2695 | 0 | #ifdef HAVE_AESCCM |
2696 | 0 | case wolfssl_aes_ccm: |
2697 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2698 | | /* initialize event */ |
2699 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2700 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2701 | | if (ret != 0) |
2702 | | break; |
2703 | | #endif |
2704 | |
|
2705 | 0 | nonceSz = AESCCM_NONCE_SZ; |
2706 | | #if defined(HAVE_PK_CALLBACKS) |
2707 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2708 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2709 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2710 | | output, input, dataSz, |
2711 | | ssl->encrypt.nonce, nonceSz, |
2712 | | output + dataSz, macSz, |
2713 | | aad, aadSz); |
2714 | | } |
2715 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2716 | | #endif |
2717 | 0 | { |
2718 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2719 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2720 | | ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, |
2721 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2722 | | output + dataSz, macSz, aad, aadSz); |
2723 | | #else |
2724 | 0 | ret = wc_AesCcmSetNonce(ssl->encrypt.aes, |
2725 | 0 | ssl->encrypt.nonce, nonceSz); |
2726 | 0 | if (ret == 0) { |
2727 | 0 | ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output, |
2728 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2729 | 0 | output + dataSz, macSz, aad, aadSz); |
2730 | 0 | } |
2731 | 0 | #endif |
2732 | 0 | } |
2733 | 0 | break; |
2734 | 0 | #endif |
2735 | | |
2736 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2737 | 0 | case wolfssl_chacha: |
2738 | 0 | ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz, |
2739 | 0 | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2740 | 0 | break; |
2741 | 0 | #endif |
2742 | | |
2743 | 0 | #ifdef WOLFSSL_SM4_GCM |
2744 | 0 | case wolfssl_sm4_gcm: |
2745 | 0 | nonceSz = SM4_GCM_NONCE_SZ; |
2746 | 0 | ret = wc_Sm4GcmEncrypt(ssl->encrypt.sm4, output, input, |
2747 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2748 | 0 | macSz, aad, aadSz); |
2749 | 0 | break; |
2750 | 0 | #endif |
2751 | | |
2752 | 0 | #ifdef WOLFSSL_SM4_CCM |
2753 | 0 | case wolfssl_sm4_ccm: |
2754 | 0 | nonceSz = SM4_CCM_NONCE_SZ; |
2755 | 0 | ret = wc_Sm4CcmEncrypt(ssl->encrypt.sm4, output, input, |
2756 | 0 | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2757 | 0 | macSz, aad, aadSz); |
2758 | 0 | break; |
2759 | 0 | #endif |
2760 | | |
2761 | | #ifdef HAVE_NULL_CIPHER |
2762 | | case wolfssl_cipher_null: |
2763 | | ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz, |
2764 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2765 | | break; |
2766 | | #endif |
2767 | | |
2768 | 0 | default: |
2769 | 0 | WOLFSSL_MSG("wolfSSL Encrypt programming error"); |
2770 | 0 | return ENCRYPT_ERROR; |
2771 | 0 | } |
2772 | | |
2773 | | /* Advance state */ |
2774 | 0 | ssl->encrypt.state = CIPHER_STATE_END; |
2775 | |
|
2776 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2777 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2778 | | /* if async is not okay, then block */ |
2779 | | if (!asyncOkay) { |
2780 | | ret = wc_AsyncWait(ret, asyncDev, event_flags); |
2781 | | } |
2782 | | else { |
2783 | | /* If pending, then leave and return will resume below */ |
2784 | | return wolfSSL_AsyncPush(ssl, asyncDev); |
2785 | | } |
2786 | | } |
2787 | | #endif |
2788 | 0 | } |
2789 | 0 | FALL_THROUGH; |
2790 | |
|
2791 | 0 | case CIPHER_STATE_END: |
2792 | 0 | { |
2793 | | #ifdef WOLFSSL_DEBUG_TLS |
2794 | | #ifdef CIPHER_NONCE |
2795 | | WOLFSSL_MSG("Nonce"); |
2796 | | WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size); |
2797 | | #endif |
2798 | | WOLFSSL_MSG("Encrypted data"); |
2799 | | WOLFSSL_BUFFER(output, dataSz); |
2800 | | WOLFSSL_MSG("Authentication Tag"); |
2801 | | WOLFSSL_BUFFER(output + dataSz, macSz); |
2802 | | #endif |
2803 | |
|
2804 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2805 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2806 | | XMEMCMP(output, ssl->encrypt.sanityCheck, |
2807 | | min(dataSz, sizeof(ssl->encrypt.sanityCheck))) == 0) { |
2808 | | |
2809 | | WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?"); |
2810 | | return ENCRYPT_ERROR; |
2811 | | } |
2812 | | ForceZero(ssl->encrypt.sanityCheck, |
2813 | | sizeof(ssl->encrypt.sanityCheck)); |
2814 | | #endif |
2815 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2816 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
2817 | | (output != input) && (ret == 0)) { |
2818 | | wc_MemZero_Add("TLS 1.3 Encrypt plaintext", input, sz); |
2819 | | } |
2820 | | #endif |
2821 | |
|
2822 | 0 | #ifdef CIPHER_NONCE |
2823 | 0 | ForceZero(ssl->encrypt.nonce, ssl->specs.iv_size); |
2824 | 0 | #endif |
2825 | |
|
2826 | 0 | break; |
2827 | 0 | } |
2828 | | |
2829 | 0 | default: |
2830 | 0 | break; |
2831 | 0 | } |
2832 | | |
2833 | | |
2834 | | /* Reset state */ |
2835 | 0 | ssl->encrypt.state = CIPHER_STATE_BEGIN; |
2836 | |
|
2837 | 0 | return ret; |
2838 | 0 | } |
2839 | | |
2840 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2841 | | /* Decrypt with ChaCha20 and check authentication tag with Poly1305. |
2842 | | * |
2843 | | * ssl The SSL/TLS object. |
2844 | | * output The buffer to write decrypted data into. |
2845 | | * May be the same pointer as input. |
2846 | | * input The data to decrypt. |
2847 | | * sz The number of bytes to decrypt. |
2848 | | * nonce The nonce to use with ChaCha20. |
2849 | | * aad The additional authentication data. |
2850 | | * aadSz The size of the addition authentication data. |
2851 | | * tagIn The authentication tag data from packet. |
2852 | | * returns 0 on success, otherwise failure. |
2853 | | */ |
2854 | | static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output, |
2855 | | const byte* input, word16 sz, byte* nonce, |
2856 | | const byte* aad, word16 aadSz, |
2857 | | const byte* tagIn) |
2858 | 2.35k | { |
2859 | 2.35k | int ret; |
2860 | 2.35k | byte tag[POLY1305_AUTH_SZ]; |
2861 | 2.35k | byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */ |
2862 | | |
2863 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2864 | 2.35k | XMEMSET(poly, 0, sizeof(poly)); |
2865 | | |
2866 | | /* Set nonce and get Poly1305 key. */ |
2867 | 2.35k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0); |
2868 | 2.35k | if (ret != 0) |
2869 | 0 | return ret; |
2870 | | /* Use ChaCha20 keystream to get Poly1305 key for tag. */ |
2871 | 2.35k | ret = wc_Chacha_Process(ssl->decrypt.chacha, poly, poly, sizeof(poly)); |
2872 | 2.35k | if (ret != 0) |
2873 | 0 | return ret; |
2874 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2875 | | wc_MemZero_Add("ChaCha20Poly1305_Decrypt poly", poly, sizeof(poly)); |
2876 | | #endif |
2877 | 2.35k | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1); |
2878 | 2.35k | if (ret != 0) { |
2879 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2880 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2881 | | wc_MemZero_Check(poly, sizeof(poly)); |
2882 | | #endif |
2883 | 0 | return ret; |
2884 | 0 | } |
2885 | | |
2886 | | /* Set key for Poly1305. */ |
2887 | 2.35k | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2888 | 2.35k | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2889 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2890 | | wc_MemZero_Check(poly, sizeof(poly)); |
2891 | | #endif |
2892 | 2.35k | if (ret != 0) |
2893 | 0 | return ret; |
2894 | | /* Generate authentication tag for encrypted data. */ |
2895 | 2.35k | if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, input, sz, tag, |
2896 | 2.35k | sizeof(tag))) != 0) { |
2897 | 0 | return ret; |
2898 | 0 | } |
2899 | | |
2900 | | /* Check tag sent along with packet. */ |
2901 | 2.35k | if (ConstantCompare(tagIn, tag, POLY1305_AUTH_SZ) != 0) { |
2902 | 2.35k | WOLFSSL_MSG("MAC did not match"); |
2903 | 2.35k | return VERIFY_MAC_ERROR; |
2904 | 2.35k | } |
2905 | | |
2906 | | /* If the tag was good decrypt message. */ |
2907 | 0 | ret = wc_Chacha_Process(ssl->decrypt.chacha, output, input, sz); |
2908 | |
|
2909 | 0 | return ret; |
2910 | 2.35k | } |
2911 | | #endif |
2912 | | |
2913 | | #ifdef HAVE_NULL_CIPHER |
2914 | | /* Check HMAC tag and copy over input. |
2915 | | * |
2916 | | * ssl The SSL/TLS object. |
2917 | | * output The buffer to copy data into. |
2918 | | * May be the same pointer as input. |
2919 | | * input The data. |
2920 | | * sz The number of bytes of data. |
2921 | | * nonce The nonce to use with authentication. |
2922 | | * aad The additional authentication data. |
2923 | | * aadSz The size of the addition authentication data. |
2924 | | * tagIn The authentication tag data from packet. |
2925 | | * returns 0 on success, otherwise failure. |
2926 | | */ |
2927 | | static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output, |
2928 | | const byte* input, word16 sz, |
2929 | | const byte* nonce, |
2930 | | const byte* aad, word16 aadSz, |
2931 | | const byte* tagIn) |
2932 | | { |
2933 | | int ret; |
2934 | | byte hmac[WC_MAX_DIGEST_SIZE]; |
2935 | | |
2936 | | /* HMAC: nonce | aad | input */ |
2937 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, ssl->specs.iv_size); |
2938 | | if (ret == 0) |
2939 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz); |
2940 | | if (ret == 0) |
2941 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz); |
2942 | | if (ret == 0) |
2943 | | ret = wc_HmacFinal(ssl->decrypt.hmac, hmac); |
2944 | | /* Check authentication tag matches */ |
2945 | | if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0) |
2946 | | ret = DECRYPT_ERROR; |
2947 | | /* Copy the input to output if not the same buffer */ |
2948 | | if (ret == 0 && output != input) |
2949 | | XMEMCPY(output, input, sz); |
2950 | | return ret; |
2951 | | } |
2952 | | #endif |
2953 | | |
2954 | | /* Decrypt data for TLS v1.3. |
2955 | | * |
2956 | | * ssl The SSL/TLS object. |
2957 | | * output The buffer to write decrypted data into. |
2958 | | * May be the same pointer as input. |
2959 | | * input The data to decrypt and authentication tag. |
2960 | | * sz The length of the encrypted data plus authentication tag. |
2961 | | * aad The additional authentication data. |
2962 | | * aadSz The size of the addition authentication data. |
2963 | | * returns 0 on success, otherwise failure. |
2964 | | */ |
2965 | | int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz, |
2966 | | const byte* aad, word16 aadSz) |
2967 | 0 | { |
2968 | 0 | int ret = 0; |
2969 | 0 | word16 dataSz; |
2970 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2971 | 0 | word32 nonceSz = 0; |
2972 | |
|
2973 | 0 | WOLFSSL_ENTER("DecryptTls13"); |
2974 | 0 | if (sz < ssl->specs.aead_mac_size) { |
2975 | 0 | return BAD_FUNC_ARG; |
2976 | 0 | } |
2977 | 0 | dataSz = sz - ssl->specs.aead_mac_size; |
2978 | |
|
2979 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2980 | | ret = tsip_Tls13AesDecrypt(ssl, output, input, sz); |
2981 | | |
2982 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2983 | | #ifndef WOLFSSL_EARLY_DATA |
2984 | | if (ret < 0) { |
2985 | | ret = VERIFY_MAC_ERROR; |
2986 | | WOLFSSL_ERROR_VERBOSE(ret); |
2987 | | } |
2988 | | #endif |
2989 | | return ret; |
2990 | | } |
2991 | | #endif |
2992 | |
|
2993 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2994 | | ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state); |
2995 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
2996 | | /* check for still pending */ |
2997 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
2998 | | return ret; |
2999 | | |
3000 | | ssl->error = 0; /* clear async */ |
3001 | | |
3002 | | /* let failures through so CIPHER_STATE_END logic is run */ |
3003 | | } |
3004 | | else |
3005 | | #endif |
3006 | 0 | { |
3007 | | /* Reset state */ |
3008 | 0 | ret = 0; |
3009 | 0 | ssl->decrypt.state = CIPHER_STATE_BEGIN; |
3010 | 0 | } |
3011 | |
|
3012 | 0 | (void)output; |
3013 | 0 | (void)input; |
3014 | 0 | (void)sz; |
3015 | 0 | (void)dataSz; |
3016 | 0 | (void)macSz; |
3017 | 0 | (void)nonceSz; |
3018 | |
|
3019 | 0 | switch (ssl->decrypt.state) { |
3020 | 0 | case CIPHER_STATE_BEGIN: |
3021 | 0 | { |
3022 | | #ifdef WOLFSSL_DEBUG_TLS |
3023 | | WOLFSSL_MSG("Data to decrypt"); |
3024 | | WOLFSSL_BUFFER(input, dataSz); |
3025 | | WOLFSSL_MSG("Additional Authentication Data"); |
3026 | | WOLFSSL_BUFFER(aad, aadSz); |
3027 | | WOLFSSL_MSG("Authentication tag"); |
3028 | | WOLFSSL_BUFFER(input + dataSz, macSz); |
3029 | | #endif |
3030 | |
|
3031 | 0 | #ifdef CIPHER_NONCE |
3032 | 0 | if (ssl->decrypt.nonce == NULL) { |
3033 | 0 | ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
3034 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
3035 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3036 | | if (ssl->decrypt.nonce != NULL) { |
3037 | | wc_MemZero_Add("DecryptTls13 nonce", ssl->decrypt.nonce, |
3038 | | ssl->specs.iv_size); |
3039 | | } |
3040 | | #endif |
3041 | 0 | } |
3042 | 0 | if (ssl->decrypt.nonce == NULL) |
3043 | 0 | return MEMORY_E; |
3044 | | |
3045 | 0 | BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV, |
3046 | 0 | ssl->specs.iv_size, PEER_ORDER); |
3047 | 0 | #endif |
3048 | | |
3049 | | /* Advance state and proceed */ |
3050 | 0 | ssl->decrypt.state = CIPHER_STATE_DO; |
3051 | 0 | } |
3052 | 0 | FALL_THROUGH; |
3053 | |
|
3054 | 0 | case CIPHER_STATE_DO: |
3055 | 0 | { |
3056 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
3057 | 0 | #ifdef BUILD_AESGCM |
3058 | 0 | case wolfssl_aes_gcm: |
3059 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3060 | | /* initialize event */ |
3061 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3062 | | WC_ASYNC_FLAG_NONE); |
3063 | | if (ret != 0) |
3064 | | break; |
3065 | | #endif |
3066 | |
|
3067 | 0 | nonceSz = AESGCM_NONCE_SZ; |
3068 | |
|
3069 | | #if defined(HAVE_PK_CALLBACKS) |
3070 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3071 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3072 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3073 | | output, input, dataSz, |
3074 | | ssl->decrypt.nonce, nonceSz, |
3075 | | (byte *)(input + dataSz), macSz, |
3076 | | aad, aadSz); |
3077 | | } |
3078 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3079 | | #endif |
3080 | 0 | { |
3081 | |
|
3082 | 0 | ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, |
3083 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, |
3084 | 0 | input + dataSz, macSz, aad, aadSz); |
3085 | |
|
3086 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3087 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3088 | | ret = wolfSSL_AsyncPush(ssl, |
3089 | | &ssl->decrypt.aes->asyncDev); |
3090 | | } |
3091 | | #endif |
3092 | |
|
3093 | 0 | } |
3094 | 0 | break; |
3095 | 0 | #endif |
3096 | | |
3097 | 0 | #ifdef HAVE_AESCCM |
3098 | 0 | case wolfssl_aes_ccm: |
3099 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3100 | | /* initialize event */ |
3101 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3102 | | WC_ASYNC_FLAG_NONE); |
3103 | | if (ret != 0) |
3104 | | break; |
3105 | | #endif |
3106 | |
|
3107 | 0 | nonceSz = AESCCM_NONCE_SZ; |
3108 | | #if defined(HAVE_PK_CALLBACKS) |
3109 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3110 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3111 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3112 | | output, input, dataSz, |
3113 | | ssl->decrypt.nonce, nonceSz, |
3114 | | (byte *)(input + dataSz), macSz, |
3115 | | aad, aadSz); |
3116 | | } |
3117 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3118 | | #endif |
3119 | 0 | { |
3120 | 0 | ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, |
3121 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, |
3122 | 0 | input + dataSz, macSz, aad, aadSz); |
3123 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3124 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3125 | | ret = wolfSSL_AsyncPush(ssl, |
3126 | | &ssl->decrypt.aes->asyncDev); |
3127 | | } |
3128 | | #endif |
3129 | 0 | } |
3130 | 0 | break; |
3131 | 0 | #endif |
3132 | | |
3133 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
3134 | 0 | case wolfssl_chacha: |
3135 | 0 | ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz, |
3136 | 0 | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3137 | 0 | break; |
3138 | 0 | #endif |
3139 | | |
3140 | 0 | #ifdef WOLFSSL_SM4_GCM |
3141 | 0 | case wolfssl_sm4_gcm: |
3142 | 0 | nonceSz = SM4_GCM_NONCE_SZ; |
3143 | 0 | ret = wc_Sm4GcmDecrypt(ssl->decrypt.sm4, output, input, |
3144 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3145 | 0 | macSz, aad, aadSz); |
3146 | 0 | break; |
3147 | 0 | #endif |
3148 | | |
3149 | 0 | #ifdef WOLFSSL_SM4_CCM |
3150 | 0 | case wolfssl_sm4_ccm: |
3151 | 0 | nonceSz = SM4_CCM_NONCE_SZ; |
3152 | 0 | ret = wc_Sm4CcmDecrypt(ssl->decrypt.sm4, output, input, |
3153 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3154 | 0 | macSz, aad, aadSz); |
3155 | 0 | break; |
3156 | 0 | #endif |
3157 | | |
3158 | | #ifdef HAVE_NULL_CIPHER |
3159 | | case wolfssl_cipher_null: |
3160 | | ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz, |
3161 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3162 | | break; |
3163 | | #endif |
3164 | 0 | default: |
3165 | 0 | WOLFSSL_MSG("wolfSSL Decrypt programming error"); |
3166 | 0 | return DECRYPT_ERROR; |
3167 | 0 | } |
3168 | | |
3169 | | /* Advance state */ |
3170 | 0 | ssl->decrypt.state = CIPHER_STATE_END; |
3171 | |
|
3172 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3173 | | /* If pending, leave now */ |
3174 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3175 | | return ret; |
3176 | | } |
3177 | | #endif |
3178 | 0 | } |
3179 | 0 | FALL_THROUGH; |
3180 | |
|
3181 | 0 | case CIPHER_STATE_END: |
3182 | 0 | { |
3183 | | #ifdef WOLFSSL_DEBUG_TLS |
3184 | | #ifdef CIPHER_NONCE |
3185 | | WOLFSSL_MSG("Nonce"); |
3186 | | WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size); |
3187 | | #endif |
3188 | | WOLFSSL_MSG("Decrypted data"); |
3189 | | WOLFSSL_BUFFER(output, dataSz); |
3190 | | #endif |
3191 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3192 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
3193 | | (ret == 0)) { |
3194 | | wc_MemZero_Add("TLS 1.3 Decrypted data", output, sz); |
3195 | | } |
3196 | | #endif |
3197 | |
|
3198 | 0 | #ifdef CIPHER_NONCE |
3199 | 0 | ForceZero(ssl->decrypt.nonce, ssl->specs.iv_size); |
3200 | 0 | #endif |
3201 | |
|
3202 | 0 | break; |
3203 | 0 | } |
3204 | | |
3205 | 0 | default: |
3206 | 0 | break; |
3207 | 0 | } |
3208 | | |
3209 | 0 | if (ret < 0) { |
3210 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
3211 | 0 | } |
3212 | |
|
3213 | 0 | return ret; |
3214 | 0 | } |
3215 | | |
3216 | | /* Persistable BuildTls13Message arguments */ |
3217 | | typedef struct BuildMsg13Args { |
3218 | | word32 sz; |
3219 | | word32 idx; |
3220 | | word32 headerSz; |
3221 | | word16 size; |
3222 | | word32 paddingSz; |
3223 | | } BuildMsg13Args; |
3224 | | |
3225 | | static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs) |
3226 | 2.21k | { |
3227 | 2.21k | BuildMsg13Args* args = (BuildMsg13Args*)pArgs; |
3228 | | |
3229 | 2.21k | (void)ssl; |
3230 | 2.21k | (void)args; |
3231 | | |
3232 | | /* no allocations in BuildTls13Message */ |
3233 | 2.21k | } |
3234 | | |
3235 | | /* Build SSL Message, encrypted. |
3236 | | * TLS v1.3 encryption is AEAD only. |
3237 | | * |
3238 | | * ssl The SSL/TLS object. |
3239 | | * output The buffer to write record message to. |
3240 | | * outSz Size of the buffer being written into. |
3241 | | * input The record data to encrypt (excluding record header). |
3242 | | * inSz The size of the record data. |
3243 | | * type The recorder header content type. |
3244 | | * hashOutput Whether to hash the unencrypted record data. |
3245 | | * sizeOnly Only want the size of the record message. |
3246 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
3247 | | * returns the size of the encrypted record message or negative value on error. |
3248 | | */ |
3249 | | int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input, |
3250 | | int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay) |
3251 | | { |
3252 | | int ret; |
3253 | | BuildMsg13Args* args; |
3254 | | BuildMsg13Args lcl_args; |
3255 | | |
3256 | | WOLFSSL_ENTER("BuildTls13Message"); |
3257 | | |
3258 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3259 | | ret = WC_NO_PENDING_E; |
3260 | | if (asyncOkay) { |
3261 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
3262 | | |
3263 | | if (ssl->async == NULL) { |
3264 | | ssl->async = (struct WOLFSSL_ASYNC*) |
3265 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
3266 | | DYNAMIC_TYPE_ASYNC); |
3267 | | if (ssl->async == NULL) |
3268 | | return MEMORY_E; |
3269 | | } |
3270 | | args = (BuildMsg13Args*)ssl->async->args; |
3271 | | |
3272 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState); |
3273 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3274 | | /* Check for error */ |
3275 | | if (ret < 0) |
3276 | | goto exit_buildmsg; |
3277 | | } |
3278 | | } |
3279 | | else |
3280 | | #endif |
3281 | | { |
3282 | | args = &lcl_args; |
3283 | | } |
3284 | | |
3285 | | /* Reset state */ |
3286 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3287 | | if (ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) |
3288 | | #endif |
3289 | | { |
3290 | | ret = 0; |
3291 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3292 | | XMEMSET(args, 0, sizeof(BuildMsg13Args)); |
3293 | | |
3294 | | args->headerSz = RECORD_HEADER_SZ; |
3295 | | #ifdef WOLFSSL_DTLS13 |
3296 | | if (ssl->options.dtls) |
3297 | | args->headerSz = Dtls13GetRlHeaderLength(ssl, 1); |
3298 | | #endif /* WOLFSSL_DTLS13 */ |
3299 | | |
3300 | | args->sz = args->headerSz + (word32)inSz; |
3301 | | args->idx = args->headerSz; |
3302 | | |
3303 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3304 | | if (asyncOkay) |
3305 | | ssl->async->freeArgs = FreeBuildMsg13Args; |
3306 | | #endif |
3307 | | } |
3308 | | |
3309 | | switch (ssl->options.buildMsgState) { |
3310 | | case BUILD_MSG_BEGIN: |
3311 | | { |
3312 | | /* catch mistaken sizeOnly parameter */ |
3313 | | if (sizeOnly) { |
3314 | | if (output || input) { |
3315 | | WOLFSSL_MSG("BuildTls13Message with sizeOnly " |
3316 | | "doesn't need input or output"); |
3317 | | return BAD_FUNC_ARG; |
3318 | | } |
3319 | | } |
3320 | | else if (output == NULL || input == NULL) { |
3321 | | return BAD_FUNC_ARG; |
3322 | | } |
3323 | | |
3324 | | /* Record layer content type at the end of record data. */ |
3325 | | args->sz++; |
3326 | | /* Authentication data at the end. */ |
3327 | | args->sz += ssl->specs.aead_mac_size; |
3328 | | #ifdef WOLFSSL_DTLS13 |
3329 | | /* Pad to minimum length */ |
3330 | | if (ssl->options.dtls && |
3331 | | args->sz < (word32)Dtls13MinimumRecordLength(ssl)) { |
3332 | | args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz; |
3333 | | args->sz = Dtls13MinimumRecordLength(ssl); |
3334 | | } |
3335 | | #endif |
3336 | | if (sizeOnly) |
3337 | | return (int)args->sz; |
3338 | | |
3339 | | if (args->sz > (word32)outSz) { |
3340 | | WOLFSSL_MSG("Oops, want to write past output buffer size"); |
3341 | | return BUFFER_E; |
3342 | | } |
3343 | | |
3344 | | /* Record data length. */ |
3345 | | args->size = (word16)(args->sz - args->headerSz); |
3346 | | /* Write/update the record header with the new size. |
3347 | | * Always have the content type as application data for encrypted |
3348 | | * messages in TLS v1.3. |
3349 | | */ |
3350 | | |
3351 | | if (ssl->options.dtls) { |
3352 | | #ifdef WOLFSSL_DTLS13 |
3353 | | Dtls13RlAddCiphertextHeader(ssl, output, args->size); |
3354 | | #endif /* WOLFSSL_DTLS13 */ |
3355 | | } |
3356 | | else { |
3357 | | AddTls13RecordHeader(output, args->size, application_data, ssl); |
3358 | | } |
3359 | | |
3360 | | /* TLS v1.3 can do in place encryption. */ |
3361 | | if (input != output + args->idx) |
3362 | | XMEMCPY(output + args->idx, input, (size_t)inSz); |
3363 | | args->idx += (word32)inSz; |
3364 | | |
3365 | | ssl->options.buildMsgState = BUILD_MSG_HASH; |
3366 | | } |
3367 | | FALL_THROUGH; |
3368 | | |
3369 | | case BUILD_MSG_HASH: |
3370 | | { |
3371 | | if (hashOutput) { |
3372 | | ret = HashOutput(ssl, output, (int)args->headerSz + inSz, 0); |
3373 | | if (ret != 0) |
3374 | | goto exit_buildmsg; |
3375 | | } |
3376 | | |
3377 | | /* The real record content type goes at the end of the data. */ |
3378 | | output[args->idx++] = (byte)type; |
3379 | | /* Double check that any necessary padding is zero'd out */ |
3380 | | XMEMSET(output + args->idx, 0, args->paddingSz); |
3381 | | args->idx += args->paddingSz; |
3382 | | |
3383 | | ssl->options.buildMsgState = BUILD_MSG_ENCRYPT; |
3384 | | } |
3385 | | FALL_THROUGH; |
3386 | | |
3387 | | case BUILD_MSG_ENCRYPT: |
3388 | | { |
3389 | | #ifdef WOLFSSL_QUIC |
3390 | | if (WOLFSSL_IS_QUIC(ssl)) { |
3391 | | /* QUIC does not use encryption of the TLS Record Layer. |
3392 | | * Return the original length + added headers |
3393 | | * and restore it in the record header. */ |
3394 | | AddTls13RecordHeader(output, (word32)inSz, (byte)type, ssl); |
3395 | | ret = (int)args->headerSz + inSz; |
3396 | | goto exit_buildmsg; |
3397 | | } |
3398 | | #endif |
3399 | | #ifdef ATOMIC_USER |
3400 | | if (ssl->ctx->MacEncryptCb) { |
3401 | | /* User Record Layer Callback handling */ |
3402 | | byte* mac = output + args->idx; |
3403 | | output += args->headerSz; |
3404 | | |
3405 | | ret = ssl->ctx->MacEncryptCb(ssl, mac, output, (unsigned int)inSz, (byte)type, 0, |
3406 | | output, output, args->size, ssl->MacEncryptCtx); |
3407 | | } |
3408 | | else |
3409 | | #endif |
3410 | | { |
3411 | | const byte* aad = output; |
3412 | | output += args->headerSz; |
3413 | | ret = EncryptTls13(ssl, output, output, args->size, aad, |
3414 | | (word16)args->headerSz, asyncOkay); |
3415 | | if (ret != 0) { |
3416 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3417 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
3418 | | #endif |
3419 | | { |
3420 | | /* Zeroize plaintext. */ |
3421 | | ForceZero(output, args->size); |
3422 | | } |
3423 | | } |
3424 | | #ifdef WOLFSSL_DTLS13 |
3425 | | if (ret == 0 && ssl->options.dtls) { |
3426 | | /* AAD points to the header. Reuse the variable */ |
3427 | | ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, |
3428 | | (word16)args->sz); |
3429 | | } |
3430 | | #endif /* WOLFSSL_DTLS13 */ |
3431 | | } |
3432 | | break; |
3433 | | } |
3434 | | |
3435 | | default: |
3436 | | break; |
3437 | | } |
3438 | | |
3439 | | exit_buildmsg: |
3440 | | |
3441 | | WOLFSSL_LEAVE("BuildTls13Message", ret); |
3442 | | |
3443 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3444 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3445 | | return ret; |
3446 | | } |
3447 | | #endif |
3448 | | |
3449 | | /* make sure build message state is reset */ |
3450 | | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3451 | | |
3452 | | /* return sz on success */ |
3453 | | if (ret == 0) { |
3454 | | ret = (int)args->sz; |
3455 | | } |
3456 | | else { |
3457 | | WOLFSSL_ERROR_VERBOSE(ret); |
3458 | | } |
3459 | | |
3460 | | /* Final cleanup */ |
3461 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3462 | | if (asyncOkay) |
3463 | | FreeAsyncCtx(ssl, 0); |
3464 | | else |
3465 | | #endif |
3466 | | FreeBuildMsg13Args(ssl, args); |
3467 | | |
3468 | | return ret; |
3469 | | } |
3470 | | |
3471 | | #if !defined(NO_WOLFSSL_CLIENT) || (!defined(NO_WOLFSSL_SERVER) && \ |
3472 | | (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
3473 | | (defined(WOLFSSL_PSK_ONE_ID) || defined(WOLFSSL_PRIORITIZE_PSK))) |
3474 | | /* Find the cipher suite in the suites set in the SSL. |
3475 | | * |
3476 | | * ssl SSL/TLS object. |
3477 | | * suite Cipher suite to look for. |
3478 | | * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise. |
3479 | | */ |
3480 | | int FindSuiteSSL(const WOLFSSL* ssl, byte* suite) |
3481 | 111 | { |
3482 | 111 | word16 i; |
3483 | 111 | const Suites* suites = WOLFSSL_SUITES(ssl); |
3484 | | |
3485 | 835 | for (i = 0; i < suites->suiteSz; i += 2) { |
3486 | 826 | if (suites->suites[i+0] == suite[0] && |
3487 | 253 | suites->suites[i+1] == suite[1]) { |
3488 | 102 | return 1; |
3489 | 102 | } |
3490 | 826 | } |
3491 | | |
3492 | 9 | return 0; |
3493 | 111 | } |
3494 | | #endif |
3495 | | |
3496 | | #ifndef NO_PSK |
3497 | | /* Get the MAC algorithm for the TLS 1.3 cipher suite. |
3498 | | * |
3499 | | * @param [in] suite. |
3500 | | * @return A value from wc_MACAlgorithm enumeration. |
3501 | | */ |
3502 | | byte SuiteMac(const byte* suite) |
3503 | | { |
3504 | | byte mac = no_mac; |
3505 | | |
3506 | | if (suite[0] == TLS13_BYTE) { |
3507 | | switch (suite[1]) { |
3508 | | #ifdef BUILD_TLS_AES_128_GCM_SHA256 |
3509 | | case TLS_AES_128_GCM_SHA256: |
3510 | | mac = sha256_mac; |
3511 | | break; |
3512 | | #endif |
3513 | | #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256 |
3514 | | case TLS_CHACHA20_POLY1305_SHA256: |
3515 | | mac = sha256_mac; |
3516 | | break; |
3517 | | #endif |
3518 | | #ifdef BUILD_TLS_AES_128_CCM_SHA256 |
3519 | | case TLS_AES_128_CCM_SHA256: |
3520 | | mac = sha256_mac; |
3521 | | break; |
3522 | | #endif |
3523 | | #ifdef BUILD_TLS_AES_128_CCM_8_SHA256 |
3524 | | case TLS_AES_128_CCM_8_SHA256: |
3525 | | mac = sha256_mac; |
3526 | | break; |
3527 | | #endif |
3528 | | #ifdef BUILD_TLS_AES_256_GCM_SHA384 |
3529 | | case TLS_AES_256_GCM_SHA384: |
3530 | | mac = sha384_mac; |
3531 | | break; |
3532 | | #endif |
3533 | | default: |
3534 | | break; |
3535 | | } |
3536 | | } |
3537 | | #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \ |
3538 | | defined(WOLFSSL_SM3) |
3539 | | else if (suite[0] == CIPHER_BYTE) { |
3540 | | switch (suite[1]) { |
3541 | | #ifdef BUILD_TLS_SM4_GCM_SM3 |
3542 | | case TLS_SM4_GCM_SM3: |
3543 | | mac = sm3_mac; |
3544 | | break; |
3545 | | #endif |
3546 | | #ifdef BUILD_TLS_SM4_CCM_SM3 |
3547 | | case TLS_SM4_CCM_SM3: |
3548 | | mac = sm3_mac; |
3549 | | break; |
3550 | | #endif |
3551 | | default: |
3552 | | break; |
3553 | | } |
3554 | | } |
3555 | | #endif |
3556 | | #ifdef HAVE_NULL_CIPHER |
3557 | | else if (suite[0] == ECC_BYTE) { |
3558 | | switch (suite[1]) { |
3559 | | #ifdef BUILD_TLS_SHA256_SHA256 |
3560 | | case TLS_SHA256_SHA256: |
3561 | | mac = sha256_mac; |
3562 | | break; |
3563 | | #endif |
3564 | | #ifdef BUILD_TLS_SHA384_SHA384 |
3565 | | case TLS_SHA384_SHA384: |
3566 | | mac = sha384_mac; |
3567 | | break; |
3568 | | #endif |
3569 | | default: |
3570 | | break; |
3571 | | } |
3572 | | } |
3573 | | #endif |
3574 | | |
3575 | | return mac; |
3576 | | } |
3577 | | #endif |
3578 | | |
3579 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3580 | | /* Create Cookie extension using the hash of the first ClientHello. |
3581 | | * |
3582 | | * ssl SSL/TLS object. |
3583 | | * hash The hash data. |
3584 | | * hashSz The size of the hash data in bytes. |
3585 | | * returns 0 on success, otherwise failure. |
3586 | | */ |
3587 | | int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz, |
3588 | | TLSX** exts, byte cipherSuite0, byte cipherSuite) |
3589 | | { |
3590 | | int ret; |
3591 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
3592 | | Hmac cookieHmac; |
3593 | | byte cookieType = 0; |
3594 | | byte macSz = 0; |
3595 | | byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2]; |
3596 | | TLSX* ext; |
3597 | | word16 cookieSz = 0; |
3598 | | |
3599 | | if (hash == NULL || hashSz == 0) { |
3600 | | return BAD_FUNC_ARG; |
3601 | | } |
3602 | | |
3603 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
3604 | | ssl->buffers.tls13CookieSecret.length == 0) { |
3605 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
3606 | | return COOKIE_ERROR; |
3607 | | } |
3608 | | |
3609 | | /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */ |
3610 | | cookie[cookieSz++] = (byte)hashSz; |
3611 | | XMEMCPY(cookie + cookieSz, hash, hashSz); |
3612 | | cookieSz += hashSz; |
3613 | | cookie[cookieSz++] = cipherSuite0; |
3614 | | cookie[cookieSz++] = cipherSuite; |
3615 | | if ((ext = TLSX_Find(*exts, TLSX_KEY_SHARE)) != NULL) { |
3616 | | KeyShareEntry* kse = (KeyShareEntry*)ext->data; |
3617 | | if (kse == NULL) { |
3618 | | WOLFSSL_MSG("KeyShareEntry can't be empty when negotiating " |
3619 | | "parameters"); |
3620 | | return BAD_STATE_E; |
3621 | | } |
3622 | | c16toa(kse->group, cookie + cookieSz); |
3623 | | cookieSz += OPAQUE16_LEN; |
3624 | | } |
3625 | | |
3626 | | #ifndef NO_SHA256 |
3627 | | cookieType = WC_SHA256; |
3628 | | macSz = WC_SHA256_DIGEST_SIZE; |
3629 | | #elif defined(WOLFSSL_SHA384) |
3630 | | cookieType = WC_SHA384; |
3631 | | macSz = WC_SHA384_DIGEST_SIZE; |
3632 | | #elif defined(WOLFSSL_TLS13_SHA512) |
3633 | | cookieType = WC_SHA512; |
3634 | | macSz = WC_SHA512_DIGEST_SIZE; |
3635 | | #elif defined(WOLFSSL_SM3) |
3636 | | cookieType = WC_SM3; |
3637 | | macSz = WC_SM3_DIGEST_SIZE; |
3638 | | #else |
3639 | | #error "No digest to available to use with HMAC for cookies." |
3640 | | #endif /* NO_SHA */ |
3641 | | |
3642 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
3643 | | if (ret == 0) { |
3644 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
3645 | | ssl->buffers.tls13CookieSecret.buffer, |
3646 | | ssl->buffers.tls13CookieSecret.length); |
3647 | | } |
3648 | | if (ret == 0) |
3649 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
3650 | | #ifdef WOLFSSL_DTLS13 |
3651 | | /* Tie cookie to peer address */ |
3652 | | if (ret == 0) { |
3653 | | /* peerLock not necessary. Still in handshake phase. */ |
3654 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
3655 | | ret = wc_HmacUpdate(&cookieHmac, |
3656 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
3657 | | ssl->buffers.dtlsCtx.peer.sz); |
3658 | | } |
3659 | | } |
3660 | | #endif |
3661 | | if (ret == 0) |
3662 | | ret = wc_HmacFinal(&cookieHmac, mac); |
3663 | | |
3664 | | wc_HmacFree(&cookieHmac); |
3665 | | if (ret != 0) |
3666 | | return ret; |
3667 | | |
3668 | | /* The cookie data is the hash and the integrity check. */ |
3669 | | return TLSX_Cookie_Use(ssl, cookie, cookieSz, mac, macSz, 1, exts); |
3670 | | } |
3671 | | #endif |
3672 | | |
3673 | | #ifdef WOLFSSL_DTLS13 |
3674 | | #define HRR_MAX_HS_HEADER_SZ DTLS_HANDSHAKE_HEADER_SZ |
3675 | | #else |
3676 | | #define HRR_MAX_HS_HEADER_SZ HANDSHAKE_HEADER_SZ |
3677 | | #endif /* WOLFSSL_DTLS13 */ |
3678 | | |
3679 | | static int CreateCookie(const WOLFSSL* ssl, byte** hash, byte* hashSz, |
3680 | | Hashes* hashes, TLSX** exts) |
3681 | | { |
3682 | | int ret = 0; |
3683 | | |
3684 | | (void)exts; |
3685 | | |
3686 | | *hash = NULL; |
3687 | | switch (ssl->specs.mac_algorithm) { |
3688 | | #ifndef NO_SHA256 |
3689 | | case sha256_mac: |
3690 | | *hash = hashes->sha256; |
3691 | | break; |
3692 | | #endif |
3693 | | #ifdef WOLFSSL_SHA384 |
3694 | | case sha384_mac: |
3695 | | *hash = hashes->sha384; |
3696 | | break; |
3697 | | #endif |
3698 | | #ifdef WOLFSSL_TLS13_SHA512 |
3699 | | case sha512_mac: |
3700 | | *hash = hashes->sha512; |
3701 | | break; |
3702 | | #endif |
3703 | | #ifdef WOLFSSL_SM3 |
3704 | | case sm3_mac: |
3705 | | *hash = hashes->sm3; |
3706 | | break; |
3707 | | #endif |
3708 | | } |
3709 | | *hashSz = ssl->specs.hash_size; |
3710 | | |
3711 | | /* check hash */ |
3712 | | if (*hash == NULL && *hashSz > 0) |
3713 | | return BAD_FUNC_ARG; |
3714 | | |
3715 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3716 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3717 | | ret = CreateCookieExt(ssl, *hash, *hashSz, exts, |
3718 | | ssl->options.cipherSuite0, ssl->options.cipherSuite); |
3719 | | #endif |
3720 | | return ret; |
3721 | | } |
3722 | | |
3723 | | /* Restart the handshake hash with a hash of the previous messages. |
3724 | | * |
3725 | | * ssl The SSL/TLS object. |
3726 | | * returns 0 on success, otherwise failure. |
3727 | | */ |
3728 | | int RestartHandshakeHash(WOLFSSL* ssl) |
3729 | 0 | { |
3730 | 0 | int ret; |
3731 | 0 | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
3732 | 0 | Hashes hashes; |
3733 | 0 | byte* hash = NULL; |
3734 | 0 | byte hashSz = 0; |
3735 | |
|
3736 | 0 | ret = BuildCertHashes(ssl, &hashes); |
3737 | 0 | if (ret != 0) |
3738 | 0 | return ret; |
3739 | 0 | ret = CreateCookie(ssl, &hash, &hashSz, &hashes, &ssl->extensions); |
3740 | 0 | if (ret != 0) |
3741 | 0 | return ret; |
3742 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3743 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3744 | | return 0; |
3745 | | #endif |
3746 | | |
3747 | 0 | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
3748 | |
|
3749 | | #ifdef WOLFSSL_DEBUG_TLS |
3750 | | WOLFSSL_MSG("Restart Hash"); |
3751 | | WOLFSSL_BUFFER(hash, hashSz); |
3752 | | #endif |
3753 | |
|
3754 | 0 | ret = InitHandshakeHashes(ssl); |
3755 | 0 | if (ret != 0) |
3756 | 0 | return ret; |
3757 | 0 | ret = HashRaw(ssl, header, sizeof(header)); |
3758 | 0 | if (ret != 0) |
3759 | 0 | return ret; |
3760 | 0 | return HashRaw(ssl, hash, hashSz); |
3761 | 0 | } |
3762 | | |
3763 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
3764 | | /* The value in the random field of a ServerHello to indicate |
3765 | | * HelloRetryRequest. |
3766 | | */ |
3767 | | static byte helloRetryRequestRandom[] = { |
3768 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
3769 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
3770 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
3771 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
3772 | | }; |
3773 | | #endif |
3774 | | |
3775 | | #ifdef HAVE_ECH |
3776 | | /* returns the index of the first supported cipher suite, -1 if none */ |
3777 | | int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config) |
3778 | | { |
3779 | | int i = 0; |
3780 | | |
3781 | | if (!wc_HpkeKemIsSupported(config->kemId)) { |
3782 | | return WOLFSSL_FATAL_ERROR; |
3783 | | } |
3784 | | |
3785 | | for (i = 0; i < config->numCipherSuites; i++) { |
3786 | | if (wc_HpkeKdfIsSupported(config->cipherSuites[i].kdfId) && |
3787 | | wc_HpkeAeadIsSupported(config->cipherSuites[i].aeadId)) { |
3788 | | return i; |
3789 | | } |
3790 | | } |
3791 | | |
3792 | | return WOLFSSL_FATAL_ERROR; |
3793 | | } |
3794 | | |
3795 | | /* Hash the inner client hello, initializing the hsHashesEch field if needed. |
3796 | | * |
3797 | | * ssl SSL/TLS object. |
3798 | | * ech ECH object. |
3799 | | * returns 0 on success and otherwise failure. |
3800 | | */ |
3801 | | static int EchHashHelloInner(WOLFSSL* ssl, WOLFSSL_ECH* ech) |
3802 | | { |
3803 | | int ret = 0; |
3804 | | int headerSz; |
3805 | | word32 realSz; |
3806 | | HS_Hashes* tmpHashes; |
3807 | | #ifndef NO_WOLFSSL_CLIENT |
3808 | | byte falseHeader[HRR_MAX_HS_HEADER_SZ]; |
3809 | | #endif |
3810 | | |
3811 | | if (ssl == NULL || ech == NULL) { |
3812 | | return BAD_FUNC_ARG; |
3813 | | } |
3814 | | |
3815 | | #ifdef WOLFSSL_DTLS13 |
3816 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3817 | | HANDSHAKE_HEADER_SZ; |
3818 | | #else |
3819 | | headerSz = HANDSHAKE_HEADER_SZ; |
3820 | | #endif |
3821 | | |
3822 | | realSz = ech->innerClientHelloLen; |
3823 | | #ifndef NO_WOLFSSL_CLIENT |
3824 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
3825 | | realSz -= ech->paddingLen + ech->hpke->Nt; |
3826 | | } |
3827 | | #endif |
3828 | | |
3829 | | tmpHashes = ssl->hsHashes; |
3830 | | |
3831 | | ssl->hsHashes = ssl->hsHashesEch; |
3832 | | if (ssl->options.echAccepted == 0 && ssl->hsHashes == NULL) { |
3833 | | ret = InitHandshakeHashes(ssl); |
3834 | | if (ret == 0) { |
3835 | | ssl->hsHashesEch = ssl->hsHashes; |
3836 | | ech->innerCount = 1; |
3837 | | } |
3838 | | } |
3839 | | |
3840 | | if (ret == 0) { |
3841 | | #ifndef NO_WOLFSSL_CLIENT |
3842 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
3843 | | /* client-side: innerClientHello contains body only */ |
3844 | | AddTls13HandShakeHeader(falseHeader, realSz, 0, 0, client_hello, |
3845 | | ssl); |
3846 | | ret = HashRaw(ssl, falseHeader, headerSz); |
3847 | | if (ret == 0) { |
3848 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
3849 | | } |
3850 | | } |
3851 | | #endif |
3852 | | #ifndef NO_WOLFSSL_SERVER |
3853 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
3854 | | /* server-side: innerClientHello contains header + body */ |
3855 | | ret = HashRaw(ssl, ech->innerClientHello, headerSz + realSz); |
3856 | | } |
3857 | | #endif |
3858 | | } |
3859 | | |
3860 | | ssl->hsHashes = tmpHashes; |
3861 | | return ret; |
3862 | | } |
3863 | | |
3864 | | /* Calculate the 8 ECH confirmation bytes. |
3865 | | * |
3866 | | * ssl SSL/TLS object. |
3867 | | * label Ascii string describing ECH acceptance or rejection. |
3868 | | * labelSz Length of label excluding NULL character. |
3869 | | * input The buffer to calculate confirmation off of. |
3870 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
3871 | | * helloSz Size of hello message. |
3872 | | * isHrr Whether message is a HelloRetryRequest or not. |
3873 | | * acceptExpanded An 8 byte array to store calculated confirmation to. |
3874 | | * returns 0 on success and otherwise failure. |
3875 | | */ |
3876 | | static int EchCalcAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
3877 | | const byte* input, int acceptOffset, int helloSz, byte isHrr, |
3878 | | byte* acceptExpanded) |
3879 | | { |
3880 | | int ret = 0; |
3881 | | int digestType = 0; |
3882 | | int digestSize = 0; |
3883 | | int hashSz = 0; |
3884 | | int headerSz; |
3885 | | HS_Hashes* tmpHashes; |
3886 | | HS_Hashes* acceptHash = NULL; |
3887 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
3888 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
3889 | | byte clientHelloInnerHash[WC_MAX_DIGEST_SIZE]; |
3890 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
3891 | | byte messageHashHeader[HRR_MAX_HS_HEADER_SZ]; |
3892 | | |
3893 | | XMEMSET(zeros, 0, sizeof(zeros)); |
3894 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
3895 | | XMEMSET(clientHelloInnerHash, 0, sizeof(clientHelloInnerHash)); |
3896 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
3897 | | |
3898 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3899 | | wc_MemZero_Add("ECH PRK", expandLabelPrk, sizeof(expandLabelPrk)); |
3900 | | #endif |
3901 | | |
3902 | | tmpHashes = ssl->hsHashes; |
3903 | | ssl->hsHashes = ssl->hsHashesEch; |
3904 | | |
3905 | | #ifdef WOLFSSL_DTLS13 |
3906 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3907 | | HANDSHAKE_HEADER_SZ; |
3908 | | #else |
3909 | | headerSz = HANDSHAKE_HEADER_SZ; |
3910 | | #endif |
3911 | | |
3912 | | if (isHrr) { |
3913 | | /* the transcript hash of ClientHelloInner1 */ |
3914 | | hashSz = GetMsgHash(ssl, clientHelloInnerHash); |
3915 | | if (hashSz > 0) { |
3916 | | ret = 0; |
3917 | | } |
3918 | | |
3919 | | /* restart ECH transcript hash, similar to RestartHandshakeHash but |
3920 | | * don't add a cookie */ |
3921 | | if (ret == 0) { |
3922 | | ret = InitHandshakeHashes(ssl); |
3923 | | } |
3924 | | if (ret == 0) { |
3925 | | ssl->hsHashesEch = ssl->hsHashes; |
3926 | | AddTls13HandShakeHeader(messageHashHeader, (word32)hashSz, 0, 0, |
3927 | | message_hash, ssl); |
3928 | | ret = HashRaw(ssl, messageHashHeader, headerSz); |
3929 | | } |
3930 | | if (ret == 0) { |
3931 | | ret = HashRaw(ssl, clientHelloInnerHash, (word32)hashSz); |
3932 | | } |
3933 | | } |
3934 | | |
3935 | | /* hash with zeros for confirmation computation */ |
3936 | | if (ret == 0) { |
3937 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashesEch, &acceptHash); |
3938 | | } |
3939 | | if (ret == 0) { |
3940 | | ssl->hsHashes = acceptHash; |
3941 | | ret = HashRaw(ssl, input, acceptOffset); |
3942 | | } |
3943 | | if (ret == 0) { |
3944 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
3945 | | } |
3946 | | if (ret == 0) { |
3947 | | ret = HashRaw(ssl, input + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
3948 | | helloSz + headerSz - (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
3949 | | } |
3950 | | |
3951 | | /* get the modified transcript hash */ |
3952 | | if (ret == 0) { |
3953 | | ret = GetMsgHash(ssl, transcriptEchConf); |
3954 | | if (ret > 0) { |
3955 | | ret = 0; |
3956 | | } |
3957 | | } |
3958 | | |
3959 | | /* pick the right type and size based on mac_algorithm */ |
3960 | | if (ret == 0) { |
3961 | | switch (ssl->specs.mac_algorithm) { |
3962 | | #ifndef NO_SHA256 |
3963 | | case sha256_mac: |
3964 | | digestType = WC_SHA256; |
3965 | | digestSize = WC_SHA256_DIGEST_SIZE; |
3966 | | break; |
3967 | | #endif /* !NO_SHA256 */ |
3968 | | #ifdef WOLFSSL_SHA384 |
3969 | | case sha384_mac: |
3970 | | digestType = WC_SHA384; |
3971 | | digestSize = WC_SHA384_DIGEST_SIZE; |
3972 | | break; |
3973 | | #endif /* WOLFSSL_SHA384 */ |
3974 | | #ifdef WOLFSSL_TLS13_SHA512 |
3975 | | case sha512_mac: |
3976 | | digestType = WC_SHA512; |
3977 | | digestSize = WC_SHA512_DIGEST_SIZE; |
3978 | | break; |
3979 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
3980 | | #ifdef WOLFSSL_SM3 |
3981 | | case sm3_mac: |
3982 | | digestType = WC_SM3; |
3983 | | digestSize = WC_SM3_DIGEST_SIZE; |
3984 | | break; |
3985 | | #endif /* WOLFSSL_SM3 */ |
3986 | | default: |
3987 | | ret = WOLFSSL_FATAL_ERROR; |
3988 | | break; |
3989 | | } |
3990 | | } |
3991 | | |
3992 | | /* extract clientRandomInner with a key of all zeros */ |
3993 | | if (ret == 0) { |
3994 | | PRIVATE_KEY_UNLOCK(); |
3995 | | #if !defined(HAVE_FIPS) || \ |
3996 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
3997 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
3998 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk, |
3999 | | ssl->heap, ssl->devId); |
4000 | | #else |
4001 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4002 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk); |
4003 | | #endif |
4004 | | PRIVATE_KEY_LOCK(); |
4005 | | } |
4006 | | |
4007 | | /* tls expand with the confirmation label */ |
4008 | | if (ret == 0) { |
4009 | | PRIVATE_KEY_UNLOCK(); |
4010 | | #ifdef WOLFSSL_DTLS13 |
4011 | | if (ssl->options.dtls) { |
4012 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4013 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4014 | | dtls13ProtocolLabel, DTLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4015 | | transcriptEchConf, (word32)digestSize, digestType, |
4016 | | WOLFSSL_SERVER_END); |
4017 | | } |
4018 | | else |
4019 | | #endif |
4020 | | { |
4021 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4022 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4023 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4024 | | transcriptEchConf, (word32)digestSize, digestType, |
4025 | | WOLFSSL_SERVER_END); |
4026 | | } |
4027 | | PRIVATE_KEY_LOCK(); |
4028 | | } |
4029 | | |
4030 | | if (acceptHash != NULL) { |
4031 | | ssl->hsHashes = acceptHash; |
4032 | | FreeHandshakeHashes(ssl); |
4033 | | } |
4034 | | |
4035 | | ssl->hsHashes = tmpHashes; |
4036 | | ForceZero(expandLabelPrk, sizeof(expandLabelPrk)); |
4037 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4038 | | wc_MemZero_Check(expandLabelPrk, sizeof(expandLabelPrk)); |
4039 | | #endif |
4040 | | return ret; |
4041 | | } |
4042 | | #endif |
4043 | | |
4044 | | #ifndef NO_WOLFSSL_CLIENT |
4045 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4046 | | #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_PSK_ONE_ID) && \ |
4047 | | !defined(NO_PSK) |
4048 | | /** |
4049 | | * convert mac algorithm to WOLFSSL_EVP_MD |
4050 | | * @param mac_alg mac algorithm |
4051 | | * @return const WOLFSSL_EVP_MD on successful, otherwise NULL |
4052 | | */ |
4053 | | static const WOLFSSL_EVP_MD* ssl_handshake_md(const byte mac_alg) |
4054 | | { |
4055 | | switch(mac_alg) { |
4056 | | case no_mac: |
4057 | | #ifndef NO_MD5 |
4058 | | case md5_mac: |
4059 | | return wolfSSL_EVP_md5(); |
4060 | | #endif |
4061 | | #ifndef NO_SHA |
4062 | | case sha_mac: |
4063 | | return wolfSSL_EVP_sha1(); |
4064 | | #endif |
4065 | | #ifdef WOLFSSL_SHA224 |
4066 | | case sha224_mac: |
4067 | | return wolfSSL_EVP_sha224(); |
4068 | | #endif |
4069 | | case sha256_mac: |
4070 | | return wolfSSL_EVP_sha256(); |
4071 | | #ifdef WOLFSSL_SHA384 |
4072 | | case sha384_mac: |
4073 | | return wolfSSL_EVP_sha384(); |
4074 | | #endif |
4075 | | #ifdef WOLFSSL_SHA512 |
4076 | | case sha512_mac: |
4077 | | return wolfSSL_EVP_sha512(); |
4078 | | #endif |
4079 | | case rmd_mac: |
4080 | | case blake2b_mac: |
4081 | | WOLFSSL_MSG("no suitable EVP_MD"); |
4082 | | return NULL; |
4083 | | default: |
4084 | | WOLFSSL_MSG("Unknown mac algorithm"); |
4085 | | return NULL; |
4086 | | } |
4087 | | } |
4088 | | #endif |
4089 | | /* Setup pre-shared key based on the details in the extension data. |
4090 | | * |
4091 | | * ssl SSL/TLS object. |
4092 | | * psk Pre-shared key extension data. |
4093 | | * clientHello Whether called from client_hello construction. |
4094 | | * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and |
4095 | | * other negative value on failure. |
4096 | | */ |
4097 | | static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello) |
4098 | | { |
4099 | | #if defined(HAVE_SESSION_TICKET) || !defined(WOLFSSL_PSK_ONE_ID) |
4100 | | int ret; |
4101 | | #endif |
4102 | | byte suite[2]; |
4103 | | |
4104 | | if (psk == NULL) |
4105 | | return BAD_FUNC_ARG; |
4106 | | |
4107 | | if (!HaveUniqueSessionObj(ssl)) { |
4108 | | WOLFSSL_MSG("Unable to have unique session object"); |
4109 | | WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR); |
4110 | | return MEMORY_ERROR; |
4111 | | } |
4112 | | |
4113 | | suite[0] = ssl->options.cipherSuite0; |
4114 | | suite[1] = ssl->options.cipherSuite; |
4115 | | |
4116 | | #ifdef HAVE_SESSION_TICKET |
4117 | | if (psk->resumption) { |
4118 | | if (clientHello) { |
4119 | | suite[0] = psk->cipherSuite0; |
4120 | | suite[1] = psk->cipherSuite; |
4121 | | |
4122 | | /* Ensure cipher suite is supported or changed suite to one with |
4123 | | * the same MAC algorithm. */ |
4124 | | if (!FindSuiteSSL(ssl, suite)) { |
4125 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4126 | | return PSK_KEY_ERROR; |
4127 | | } |
4128 | | |
4129 | | ssl->options.cipherSuite0 = suite[0]; |
4130 | | ssl->options.cipherSuite = suite[1]; |
4131 | | |
4132 | | /* Setting mac for binder and keys for deriving EarlyData. */ |
4133 | | ret = SetCipherSpecs(ssl); |
4134 | | if (ret != 0) |
4135 | | return ret; |
4136 | | } |
4137 | | |
4138 | | #ifdef WOLFSSL_EARLY_DATA |
4139 | | if (ssl->session->maxEarlyDataSz == 0) |
4140 | | ssl->earlyData = no_early_data; |
4141 | | #endif |
4142 | | /* Resumption PSK is master secret. */ |
4143 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
4144 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
4145 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
4146 | | return ret; |
4147 | | } |
4148 | | if (!clientHello) { |
4149 | | /* CLIENT: using secret in ticket for peer authentication. */ |
4150 | | ssl->options.peerAuthGood = 1; |
4151 | | } |
4152 | | } |
4153 | | #endif |
4154 | | #ifndef NO_PSK |
4155 | | if (!psk->resumption) { |
4156 | | /* Get the pre-shared key. */ |
4157 | | #ifndef WOLFSSL_PSK_ONE_ID |
4158 | | const char* cipherName = NULL; |
4159 | | #ifdef OPENSSL_EXTRA |
4160 | | WOLFSSL_SESSION* psksession = NULL; |
4161 | | #endif |
4162 | | |
4163 | | /* Set the client identity to use. */ |
4164 | | XMEMSET(ssl->arrays->client_identity, 0, |
4165 | | sizeof(ssl->arrays->client_identity)); |
4166 | | XMEMCPY(ssl->arrays->client_identity, psk->identity, psk->identityLen); |
4167 | | |
4168 | | #ifdef WOLFSSL_DEBUG_TLS |
4169 | | WOLFSSL_MSG("PSK cipher suite:"); |
4170 | | WOLFSSL_MSG(GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4171 | | #endif |
4172 | | |
4173 | | /* Get the pre-shared key. */ |
4174 | | #ifdef OPENSSL_EXTRA |
4175 | | if (ssl->options.session_psk_cb != NULL) { |
4176 | | const unsigned char* id = NULL; |
4177 | | size_t idlen = 0; |
4178 | | const WOLFSSL_EVP_MD* handshake_md = NULL; |
4179 | | |
4180 | | if (ssl->msgsReceived.got_hello_retry_request >= 1) { |
4181 | | handshake_md = ssl_handshake_md(ssl->specs.mac_algorithm); |
4182 | | } |
4183 | | /* OpenSSL compatible callback that gets cached session. */ |
4184 | | if (ssl->options.session_psk_cb(ssl, handshake_md, &id, &idlen, |
4185 | | &psksession) == 0) { |
4186 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4187 | | WOLFSSL_MSG("psk session callback failed"); |
4188 | | return PSK_KEY_ERROR; |
4189 | | } |
4190 | | if (psksession != NULL) { |
4191 | | if (idlen > MAX_PSK_KEY_LEN) { |
4192 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4193 | | WOLFSSL_MSG("psk key length is too long"); |
4194 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4195 | | return PSK_KEY_ERROR; |
4196 | | } |
4197 | | |
4198 | | ssl->arrays->psk_keySz = (word32)idlen; |
4199 | | XMEMCPY(ssl->arrays->psk_key, id, idlen); |
4200 | | suite[0] = psksession->cipherSuite0; |
4201 | | suite[1] = psksession->cipherSuite; |
4202 | | /* Not needed anymore. */ |
4203 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4204 | | /* Leave pointer not NULL to indicate success with callback. */ |
4205 | | } |
4206 | | } |
4207 | | if (psksession != NULL) { |
4208 | | /* Don't try other callbacks - we have an answer. */ |
4209 | | } |
4210 | | else |
4211 | | #endif /* OPENSSL_EXTRA */ |
4212 | | if (ssl->options.client_psk_cs_cb != NULL) { |
4213 | | #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
4214 | | ssl->arrays->client_identity[0] = 0; |
4215 | | #endif |
4216 | | /* Lookup key again for next identity. */ |
4217 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cs_cb( |
4218 | | ssl, ssl->arrays->server_hint, |
4219 | | ssl->arrays->client_identity, MAX_PSK_ID_LEN, |
4220 | | ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4221 | | GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4222 | | if (clientHello) { |
4223 | | /* Use PSK cipher suite. */ |
4224 | | ssl->options.cipherSuite0 = psk->cipherSuite0; |
4225 | | ssl->options.cipherSuite = psk->cipherSuite; |
4226 | | } |
4227 | | else { |
4228 | | byte pskCS[2]; |
4229 | | pskCS[0] = psk->cipherSuite0; |
4230 | | pskCS[1] = psk->cipherSuite; |
4231 | | |
4232 | | /* Ensure PSK and negotiated cipher suites have same hash. */ |
4233 | | if (SuiteMac(pskCS) != SuiteMac(suite)) { |
4234 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4235 | | return PSK_KEY_ERROR; |
4236 | | } |
4237 | | /* Negotiated cipher suite is to be used - update PSK. */ |
4238 | | psk->cipherSuite0 = suite[0]; |
4239 | | psk->cipherSuite = suite[1]; |
4240 | | } |
4241 | | } |
4242 | | else if (ssl->options.client_psk_tls13_cb != NULL) { |
4243 | | byte cipherSuite0; |
4244 | | byte cipherSuite; |
4245 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
4246 | | |
4247 | | ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl, |
4248 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4249 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4250 | | &cipherName); |
4251 | | if (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
4252 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) { |
4253 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4254 | | return PSK_KEY_ERROR; |
4255 | | } |
4256 | | ssl->options.cipherSuite0 = cipherSuite0; |
4257 | | ssl->options.cipherSuite = cipherSuite; |
4258 | | (void)cipherSuiteFlags; |
4259 | | } |
4260 | | else { |
4261 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl, |
4262 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4263 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN); |
4264 | | ssl->options.cipherSuite0 = TLS13_BYTE; |
4265 | | ssl->options.cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
4266 | | } |
4267 | | if (ssl->arrays->psk_keySz == 0 || |
4268 | | (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN && |
4269 | | (int)ssl->arrays->psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK))) { |
4270 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4271 | | return PSK_KEY_ERROR; |
4272 | | } |
4273 | | |
4274 | | ret = SetCipherSpecs(ssl); |
4275 | | if (ret != 0) |
4276 | | return ret; |
4277 | | #else |
4278 | | /* PSK information loaded during setting of default TLS extensions. */ |
4279 | | #endif /* !WOLFSSL_PSK_ONE_ID */ |
4280 | | |
4281 | | if (!clientHello && (psk->cipherSuite0 != suite[0] || |
4282 | | psk->cipherSuite != suite[1])) { |
4283 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4284 | | return PSK_KEY_ERROR; |
4285 | | } |
4286 | | |
4287 | | if (!clientHello) { |
4288 | | /* CLIENT: using PSK for peer authentication. */ |
4289 | | ssl->options.peerAuthGood = 1; |
4290 | | } |
4291 | | } |
4292 | | #endif |
4293 | | |
4294 | | #ifdef HAVE_SUPPORTED_CURVES |
4295 | | if (!clientHello) { |
4296 | | TLSX* ext; |
4297 | | word32 modes; |
4298 | | KeyShareEntry* kse = NULL; |
4299 | | |
4300 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
4301 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
4302 | | if (ext == NULL) { |
4303 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4304 | | return PSK_KEY_ERROR; |
4305 | | } |
4306 | | modes = ext->val; |
4307 | | |
4308 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
4309 | | if (ext != NULL) { |
4310 | | kse = (KeyShareEntry*)ext->data; |
4311 | | } |
4312 | | /* Use (EC)DHE for forward-security if possible. */ |
4313 | | if (((modes & (1 << PSK_DHE_KE)) != 0) && (!ssl->options.noPskDheKe) && |
4314 | | (kse != NULL) && kse->derived) { |
4315 | | if ((kse->session != 0) && (kse->session != kse->group)) { |
4316 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4317 | | return PSK_KEY_ERROR; |
4318 | | } |
4319 | | } |
4320 | | else if (ssl->options.onlyPskDheKe) { |
4321 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4322 | | return PSK_KEY_ERROR; |
4323 | | } |
4324 | | else if (ssl->options.noPskDheKe) { |
4325 | | ssl->arrays->preMasterSz = 0; |
4326 | | } |
4327 | | } |
4328 | | else |
4329 | | #endif |
4330 | | if (ssl->options.noPskDheKe) { |
4331 | | ssl->arrays->preMasterSz = 0; |
4332 | | } |
4333 | | |
4334 | | /* Derive the early secret using the PSK. */ |
4335 | | return DeriveEarlySecret(ssl); |
4336 | | } |
4337 | | |
4338 | | /* Derive and write the binders into the ClientHello in space left when |
4339 | | * writing the Pre-Shared Key extension. |
4340 | | * |
4341 | | * ssl The SSL/TLS object. |
4342 | | * output The buffer containing the ClientHello. |
4343 | | * idx The index at the end of the completed ClientHello. |
4344 | | * returns 0 on success and otherwise failure. |
4345 | | */ |
4346 | | static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx) |
4347 | | { |
4348 | | int ret; |
4349 | | TLSX* ext; |
4350 | | PreSharedKey* current; |
4351 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
4352 | | word16 len; |
4353 | | |
4354 | | WOLFSSL_ENTER("WritePSKBinders"); |
4355 | | |
4356 | | if (idx > WOLFSSL_MAX_16BIT) { |
4357 | | return INPUT_SIZE_E; |
4358 | | } |
4359 | | |
4360 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
4361 | | if (ext == NULL) |
4362 | | return SANITY_MSG_E; |
4363 | | |
4364 | | /* Get the size of the binders to determine where to write binders. */ |
4365 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
4366 | | client_hello, &len); |
4367 | | if (ret < 0) |
4368 | | return ret; |
4369 | | idx -= len; |
4370 | | |
4371 | | /* Hash truncated ClientHello - up to binders. */ |
4372 | | #ifdef WOLFSSL_DTLS13 |
4373 | | if (ssl->options.dtls) |
4374 | | ret = Dtls13HashHandshake(ssl, output + Dtls13GetRlHeaderLength(ssl, 0), |
4375 | | (word16)idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4376 | | else |
4377 | | #endif /* WOLFSSL_DTLS13 */ |
4378 | | ret = HashOutput(ssl, output, (int)idx, 0); |
4379 | | |
4380 | | if (ret != 0) |
4381 | | return ret; |
4382 | | |
4383 | | current = (PreSharedKey*)ext->data; |
4384 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4385 | | if (current != NULL) { |
4386 | | wc_MemZero_Add("WritePSKBinders binderKey", binderKey, |
4387 | | sizeof(binderKey)); |
4388 | | } |
4389 | | #endif |
4390 | | /* Calculate the binder for each identity based on previous handshake data. |
4391 | | */ |
4392 | | while (current != NULL) { |
4393 | | if ((ret = SetupPskKey(ssl, current, 1)) != 0) |
4394 | | break; |
4395 | | |
4396 | | #ifdef HAVE_SESSION_TICKET |
4397 | | if (current->resumption) |
4398 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
4399 | | #endif |
4400 | | #ifndef NO_PSK |
4401 | | if (!current->resumption) |
4402 | | ret = DeriveBinderKey(ssl, binderKey); |
4403 | | #endif |
4404 | | if (ret != 0) |
4405 | | break; |
4406 | | |
4407 | | /* Derive the Finished message secret. */ |
4408 | | ret = DeriveFinishedSecret(ssl, binderKey, |
4409 | | ssl->keys.client_write_MAC_secret, |
4410 | | 0 /* neither end */); |
4411 | | if (ret != 0) |
4412 | | break; |
4413 | | |
4414 | | /* Build the HMAC of the handshake message data = binder. */ |
4415 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, |
4416 | | current->binder, ¤t->binderLen); |
4417 | | if (ret != 0) |
4418 | | break; |
4419 | | |
4420 | | current = current->next; |
4421 | | } |
4422 | | |
4423 | | ForceZero(binderKey, sizeof(binderKey)); |
4424 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4425 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
4426 | | #endif |
4427 | | if (ret != 0) |
4428 | | return ret; |
4429 | | |
4430 | | /* Data entered into extension, now write to message. */ |
4431 | | ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx, |
4432 | | client_hello, &len); |
4433 | | if (ret < 0) |
4434 | | return ret; |
4435 | | |
4436 | | /* Hash binders to complete the hash of the ClientHello. */ |
4437 | | ret = HashRaw(ssl, output + idx, len); |
4438 | | if (ret < 0) |
4439 | | return ret; |
4440 | | |
4441 | | #ifdef WOLFSSL_EARLY_DATA |
4442 | | if (ssl->earlyData != no_early_data) { |
4443 | | if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data, 1)) != 0) |
4444 | | return ret; |
4445 | | |
4446 | | /* Derive early data encryption key. */ |
4447 | | ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1); |
4448 | | if (ret != 0) |
4449 | | return ret; |
4450 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
4451 | | return ret; |
4452 | | |
4453 | | } |
4454 | | #endif |
4455 | | |
4456 | | WOLFSSL_LEAVE("WritePSKBinders", ret); |
4457 | | |
4458 | | return ret; |
4459 | | } |
4460 | | #endif |
4461 | | |
4462 | | static void GetTls13SessionId(WOLFSSL* ssl, byte* output, word32* idx) |
4463 | 6.54k | { |
4464 | 6.54k | if (ssl->session->sessionIDSz > 0) { |
4465 | | /* Session resumption for old versions of protocol. */ |
4466 | 0 | if (ssl->session->sessionIDSz <= ID_LEN) { |
4467 | 0 | if (output != NULL) |
4468 | 0 | output[*idx] = ssl->session->sessionIDSz; |
4469 | 0 | (*idx)++; |
4470 | 0 | if (output != NULL) { |
4471 | 0 | XMEMCPY(output + *idx, ssl->session->sessionID, |
4472 | 0 | ssl->session->sessionIDSz); |
4473 | 0 | } |
4474 | 0 | *idx += ssl->session->sessionIDSz; |
4475 | 0 | } |
4476 | 0 | else { |
4477 | | /* Invalid session ID length. Reset it. */ |
4478 | 0 | ssl->session->sessionIDSz = 0; |
4479 | 0 | if (output != NULL) |
4480 | 0 | output[*idx] = 0; |
4481 | 0 | (*idx)++; |
4482 | 0 | } |
4483 | 0 | } |
4484 | 6.54k | else { |
4485 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
4486 | | if (ssl->options.tls13MiddleBoxCompat) { |
4487 | | if (output != NULL) |
4488 | | output[*idx] = ID_LEN; |
4489 | | (*idx)++; |
4490 | | if (output != NULL) |
4491 | | XMEMCPY(output + *idx, ssl->arrays->clientRandom, ID_LEN); |
4492 | | *idx += ID_LEN; |
4493 | | } |
4494 | | else |
4495 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
4496 | 6.54k | { |
4497 | | /* TLS v1.3 does not use session id - 0 length. */ |
4498 | 6.54k | if (output != NULL) |
4499 | 3.22k | output[*idx] = 0; |
4500 | 6.54k | (*idx)++; |
4501 | 6.54k | } |
4502 | 6.54k | } |
4503 | 6.54k | } |
4504 | | |
4505 | | /* handle generation of TLS 1.3 client_hello (1) */ |
4506 | | /* Send a ClientHello message to the server. |
4507 | | * Include the information required to start a handshake with servers using |
4508 | | * protocol versions less than TLS v1.3. |
4509 | | * Only a client will send this message. |
4510 | | * |
4511 | | * ssl The SSL/TLS object. |
4512 | | * returns 0 on success and otherwise failure. |
4513 | | */ |
4514 | | |
4515 | | typedef struct Sch13Args { |
4516 | | byte* output; |
4517 | | word32 idx; |
4518 | | int sendSz; |
4519 | | word32 length; |
4520 | | #if defined(HAVE_ECH) |
4521 | | int clientRandomOffset; |
4522 | | int preXLength; |
4523 | | WOLFSSL_ECH* ech; |
4524 | | #endif |
4525 | | } Sch13Args; |
4526 | | |
4527 | | #ifdef WOLFSSL_EARLY_DATA |
4528 | | /* Check if early data can potentially be sent. |
4529 | | * Returns 1 if early data is possible, 0 otherwise. |
4530 | | */ |
4531 | | static int EarlyDataPossible(WOLFSSL* ssl) |
4532 | | { |
4533 | | /* Need session resumption OR PSK callback configured */ |
4534 | | if (ssl->options.resuming) { |
4535 | | return 1; |
4536 | | } |
4537 | | #ifndef NO_PSK |
4538 | | if (ssl->options.client_psk_tls13_cb != NULL || |
4539 | | ssl->options.client_psk_cb != NULL) { |
4540 | | return 1; |
4541 | | } |
4542 | | #endif |
4543 | | return 0; |
4544 | | } |
4545 | | #endif /* WOLFSSL_EARLY_DATA */ |
4546 | | |
4547 | | int SendTls13ClientHello(WOLFSSL* ssl) |
4548 | | { |
4549 | | int ret; |
4550 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4551 | | Sch13Args* args = NULL; |
4552 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
4553 | | #else |
4554 | | Sch13Args args[1]; |
4555 | | #endif |
4556 | | byte major, tls12minor; |
4557 | | const Suites* suites; |
4558 | | |
4559 | | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND); |
4560 | | WOLFSSL_ENTER("SendTls13ClientHello"); |
4561 | | |
4562 | | if (ssl == NULL) { |
4563 | | return BAD_FUNC_ARG; |
4564 | | } |
4565 | | |
4566 | | ssl->options.buildingMsg = 1; |
4567 | | major = SSLv3_MAJOR; |
4568 | | tls12minor = TLSv1_2_MINOR; |
4569 | | |
4570 | | #ifdef WOLFSSL_DTLS13 |
4571 | | if (ssl->options.dtls) { |
4572 | | major = DTLS_MAJOR; |
4573 | | tls12minor = DTLSv1_2_MINOR; |
4574 | | } |
4575 | | #endif /* WOLFSSL_DTLS */ |
4576 | | |
4577 | | #ifdef HAVE_SESSION_TICKET |
4578 | | if (ssl->options.resuming && |
4579 | | (ssl->session->version.major != ssl->version.major || |
4580 | | ssl->session->version.minor != ssl->version.minor)) { |
4581 | | #ifndef WOLFSSL_NO_TLS12 |
4582 | | if (ssl->session->version.major == ssl->version.major && |
4583 | | ssl->session->version.minor < ssl->version.minor) { |
4584 | | /* Cannot resume with a different protocol version. */ |
4585 | | ssl->options.resuming = 0; |
4586 | | ssl->version.major = ssl->session->version.major; |
4587 | | ssl->version.minor = ssl->session->version.minor; |
4588 | | return SendClientHello(ssl); |
4589 | | } |
4590 | | else |
4591 | | #endif |
4592 | | { |
4593 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
4594 | | return VERSION_ERROR; |
4595 | | } |
4596 | | } |
4597 | | #endif |
4598 | | |
4599 | | suites = WOLFSSL_SUITES(ssl); |
4600 | | if (suites == NULL) { |
4601 | | WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello"); |
4602 | | return SUITES_ERROR; |
4603 | | } |
4604 | | |
4605 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4606 | | if (ssl->async == NULL) { |
4607 | | ssl->async = (struct WOLFSSL_ASYNC*) |
4608 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
4609 | | DYNAMIC_TYPE_ASYNC); |
4610 | | if (ssl->async == NULL) |
4611 | | return MEMORY_E; |
4612 | | ssl->async->freeArgs = NULL; |
4613 | | } |
4614 | | args = (Sch13Args*)ssl->async->args; |
4615 | | |
4616 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
4617 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
4618 | | /* Check for error */ |
4619 | | if (ret < 0) |
4620 | | return ret; |
4621 | | } |
4622 | | else |
4623 | | #endif |
4624 | | { |
4625 | | /* Reset state */ |
4626 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
4627 | | XMEMSET(args, 0, sizeof(Sch13Args)); |
4628 | | } |
4629 | | |
4630 | | switch (ssl->options.asyncState) { |
4631 | | case TLS_ASYNC_BEGIN: |
4632 | | { |
4633 | | word32 sessIdSz = 0; |
4634 | | |
4635 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
4636 | | |
4637 | | #ifdef WOLFSSL_DTLS13 |
4638 | | if (ssl->options.dtls) |
4639 | | args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4640 | | #endif /* WOLFSSL_DTLS13 */ |
4641 | | |
4642 | | /* Version | Random | Cipher Suites | Compression */ |
4643 | | args->length = VERSION_SZ + RAN_LEN + suites->suiteSz + |
4644 | | SUITE_LEN + COMP_LEN + ENUM_LEN; |
4645 | | #ifdef WOLFSSL_QUIC |
4646 | | if (WOLFSSL_IS_QUIC(ssl)) { |
4647 | | /* RFC 9001 ch. 8.4 sessionID in ClientHello MUST be 0 length */ |
4648 | | ssl->session->sessionIDSz = 0; |
4649 | | ssl->options.tls13MiddleBoxCompat = 0; |
4650 | | } |
4651 | | #endif |
4652 | | GetTls13SessionId(ssl, NULL, &sessIdSz); |
4653 | | args->length += (word16)sessIdSz; |
4654 | | |
4655 | | #ifdef WOLFSSL_DTLS13 |
4656 | | if (ssl->options.dtls) { |
4657 | | /* legacy_cookie_id len */ |
4658 | | args->length += ENUM_LEN; |
4659 | | |
4660 | | /* server sent us an HelloVerifyRequest and we allow downgrade */ |
4661 | | if (ssl->arrays->cookieSz > 0 && ssl->options.downgrade) |
4662 | | args->length += ssl->arrays->cookieSz; |
4663 | | } |
4664 | | #endif /* WOLFSSL_DTLS13 */ |
4665 | | |
4666 | | /* Advance state and proceed */ |
4667 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
4668 | | } /* case TLS_ASYNC_BEGIN */ |
4669 | | FALL_THROUGH; |
4670 | | |
4671 | | case TLS_ASYNC_BUILD: |
4672 | | case TLS_ASYNC_DO: |
4673 | | { |
4674 | | /* Auto populate extensions supported unless user defined. */ |
4675 | | if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0) |
4676 | | return ret; |
4677 | | |
4678 | | /* Advance state and proceed */ |
4679 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
4680 | | } /* case TLS_ASYNC_BUILD */ |
4681 | | FALL_THROUGH; |
4682 | | |
4683 | | case TLS_ASYNC_FINALIZE: |
4684 | | { |
4685 | | #ifdef WOLFSSL_EARLY_DATA |
4686 | | if (!EarlyDataPossible(ssl)) |
4687 | | ssl->earlyData = no_early_data; |
4688 | | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
4689 | | ssl->earlyData = no_early_data; |
4690 | | if (ssl->earlyData == no_early_data) |
4691 | | TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap); |
4692 | | if (ssl->earlyData != no_early_data && |
4693 | | (ret = TLSX_EarlyData_Use(ssl, 0, 0)) < 0) { |
4694 | | return ret; |
4695 | | } |
4696 | | #endif |
4697 | | #ifdef WOLFSSL_QUIC |
4698 | | if (WOLFSSL_IS_QUIC(ssl) && IsAtLeastTLSv1_3(ssl->version)) { |
4699 | | ret = wolfSSL_quic_add_transport_extensions(ssl, client_hello); |
4700 | | if (ret != 0) |
4701 | | return ret; |
4702 | | } |
4703 | | #endif |
4704 | | |
4705 | | /* find length of outer and inner */ |
4706 | | #if defined(HAVE_ECH) |
4707 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH) { |
4708 | | TLSX* echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
4709 | | if (echX == NULL) |
4710 | | return WOLFSSL_FATAL_ERROR; |
4711 | | |
4712 | | args->ech = (WOLFSSL_ECH*)echX->data; |
4713 | | if (args->ech == NULL) |
4714 | | return WOLFSSL_FATAL_ERROR; |
4715 | | |
4716 | | /* only prepare if we have a chance at acceptance */ |
4717 | | if (ssl->options.echAccepted || args->ech->innerCount == 0) { |
4718 | | /* set the type to inner */ |
4719 | | args->ech->type = ECH_TYPE_INNER; |
4720 | | args->preXLength = (int)args->length; |
4721 | | |
4722 | | /* get size for inner */ |
4723 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4724 | | if (ret != 0) |
4725 | | return ret; |
4726 | | |
4727 | | /* set the type to outer */ |
4728 | | args->ech->type = 0; |
4729 | | /* set innerClientHelloLen to ClientHelloInner + padding + tag */ |
4730 | | args->ech->paddingLen = 31 - ((args->length - 1) % 32); |
4731 | | args->ech->innerClientHelloLen = (word16)(args->length + |
4732 | | args->ech->paddingLen + args->ech->hpke->Nt); |
4733 | | /* set the length back to before we computed ClientHelloInner size */ |
4734 | | args->length = (word32)args->preXLength; |
4735 | | } |
4736 | | } |
4737 | | #endif |
4738 | | |
4739 | | { |
4740 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4741 | | word16 maxFrag = wolfssl_local_GetMaxPlaintextSize(ssl); |
4742 | | word16 lenWithoutExts = args->length; |
4743 | | #endif |
4744 | | |
4745 | | /* Include length of TLS extensions. */ |
4746 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4747 | | if (ret != 0) |
4748 | | return ret; |
4749 | | |
4750 | | /* Total message size. */ |
4751 | | args->sendSz = |
4752 | | (int)(args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ); |
4753 | | |
4754 | | #ifdef WOLFSSL_DTLS13 |
4755 | | if (ssl->options.dtls) |
4756 | | args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4757 | | #endif /* WOLFSSL_DTLS13 */ |
4758 | | |
4759 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4760 | | if (ssl->options.dtls && args->sendSz > maxFrag && |
4761 | | TLSX_Find(ssl->extensions, TLSX_COOKIE) == NULL) { |
4762 | | /* Try again with an empty key share if we would be fragmenting |
4763 | | * without a cookie */ |
4764 | | ret = TLSX_KeyShare_Empty(ssl); |
4765 | | if (ret != 0) |
4766 | | return ret; |
4767 | | args->length = lenWithoutExts; |
4768 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4769 | | if (ret != 0) |
4770 | | return ret; |
4771 | | args->sendSz = (int)(args->length + |
4772 | | DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ); |
4773 | | if (args->sendSz > maxFrag) { |
4774 | | WOLFSSL_MSG("Can't fit first CH in one fragment."); |
4775 | | return BUFFER_ERROR; |
4776 | | } |
4777 | | WOLFSSL_MSG("Sending empty key share so we don't fragment CH1"); |
4778 | | } |
4779 | | #endif |
4780 | | } |
4781 | | |
4782 | | /* Check buffers are big enough and grow if needed. */ |
4783 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) |
4784 | | return ret; |
4785 | | |
4786 | | /* Get position in output buffer to write new message to. */ |
4787 | | args->output = GetOutputBuffer(ssl); |
4788 | | |
4789 | | /* Put the record and handshake headers on. */ |
4790 | | AddTls13Headers(args->output, args->length, client_hello, ssl); |
4791 | | |
4792 | | /* Protocol version - negotiation now in extension: supported_versions. */ |
4793 | | args->output[args->idx++] = major; |
4794 | | args->output[args->idx++] = tls12minor; |
4795 | | |
4796 | | /* Keep for downgrade. */ |
4797 | | ssl->chVersion = ssl->version; |
4798 | | |
4799 | | if (ssl->arrays == NULL) { |
4800 | | return BAD_FUNC_ARG; |
4801 | | } |
4802 | | /* Client Random */ |
4803 | | if (ssl->options.connectState == CONNECT_BEGIN) { |
4804 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + args->idx, RAN_LEN); |
4805 | | if (ret != 0) |
4806 | | return ret; |
4807 | | |
4808 | | /* Store random for possible second ClientHello. */ |
4809 | | XMEMCPY(ssl->arrays->clientRandom, args->output + args->idx, RAN_LEN); |
4810 | | } |
4811 | | else |
4812 | | XMEMCPY(args->output + args->idx, ssl->arrays->clientRandom, RAN_LEN); |
4813 | | |
4814 | | #if defined(HAVE_ECH) |
4815 | | args->clientRandomOffset = (int)args->idx; |
4816 | | #endif |
4817 | | |
4818 | | args->idx += RAN_LEN; |
4819 | | |
4820 | | GetTls13SessionId(ssl, args->output, &args->idx); |
4821 | | |
4822 | | #ifdef WOLFSSL_DTLS13 |
4823 | | if (ssl->options.dtls) { |
4824 | | args->output[args->idx++] = ssl->arrays->cookieSz; |
4825 | | |
4826 | | if (ssl->arrays->cookieSz > 0) { |
4827 | | /* We have a cookie saved, so the server sent us an |
4828 | | * HelloVerifyRequest, it means it is a v1.2 server */ |
4829 | | if (!ssl->options.downgrade) |
4830 | | return VERSION_ERROR; |
4831 | | XMEMCPY(args->output + args->idx, ssl->arrays->cookie, |
4832 | | ssl->arrays->cookieSz); |
4833 | | args->idx += ssl->arrays->cookieSz; |
4834 | | } |
4835 | | } |
4836 | | #endif /* WOLFSSL_DTLS13 */ |
4837 | | |
4838 | | /* Cipher suites */ |
4839 | | c16toa(suites->suiteSz, args->output + args->idx); |
4840 | | args->idx += OPAQUE16_LEN; |
4841 | | XMEMCPY(args->output + args->idx, &suites->suites, |
4842 | | suites->suiteSz); |
4843 | | args->idx += suites->suiteSz; |
4844 | | #ifdef WOLFSSL_DEBUG_TLS |
4845 | | { |
4846 | | int ii; |
4847 | | WOLFSSL_MSG("Ciphers:"); |
4848 | | for (ii = 0 ; ii < suites->suiteSz; ii += 2) { |
4849 | | WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0], |
4850 | | suites->suites[ii+1])); |
4851 | | } |
4852 | | } |
4853 | | #endif |
4854 | | |
4855 | | /* Compression not supported in TLS v1.3. */ |
4856 | | args->output[args->idx++] = COMP_LEN; |
4857 | | args->output[args->idx++] = NO_COMPRESSION; |
4858 | | |
4859 | | #if defined(HAVE_ECH) |
4860 | | /* write inner then outer */ |
4861 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
4862 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4863 | | /* set the type to inner */ |
4864 | | args->ech->type = ECH_TYPE_INNER; |
4865 | | /* innerClientHello may already exist from hrr, free if it does */ |
4866 | | if (args->ech->innerClientHello != NULL) { |
4867 | | XFREE(args->ech->innerClientHello, ssl->heap, |
4868 | | DYNAMIC_TYPE_TMP_BUFFER); |
4869 | | } |
4870 | | /* allocate the inner */ |
4871 | | args->ech->innerClientHello = |
4872 | | (byte*)XMALLOC(args->ech->innerClientHelloLen - args->ech->hpke->Nt, |
4873 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
4874 | | if (args->ech->innerClientHello == NULL) |
4875 | | return MEMORY_E; |
4876 | | /* set the padding bytes to 0 */ |
4877 | | XMEMSET(args->ech->innerClientHello + args->ech->innerClientHelloLen - |
4878 | | args->ech->hpke->Nt - args->ech->paddingLen, 0, |
4879 | | args->ech->paddingLen); |
4880 | | /* copy the client hello to the ech innerClientHello, exclude record */ |
4881 | | /* and handshake headers */ |
4882 | | XMEMCPY(args->ech->innerClientHello, |
4883 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4884 | | args->idx - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ)); |
4885 | | /* copy the client random to inner - only for first CH, not after HRR */ |
4886 | | if (!ssl->options.echAccepted) { |
4887 | | XMEMCPY(ssl->arrays->clientRandomInner, ssl->arrays->clientRandom, |
4888 | | RAN_LEN); |
4889 | | } |
4890 | | else { |
4891 | | /* After HRR, use the same inner random as CH1 */ |
4892 | | XMEMCPY(args->ech->innerClientHello + VERSION_SZ, |
4893 | | ssl->arrays->clientRandomInner, RAN_LEN); |
4894 | | } |
4895 | | /* change the outer client random */ |
4896 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + |
4897 | | args->clientRandomOffset, RAN_LEN); |
4898 | | if (ret != 0) |
4899 | | return ret; |
4900 | | /* copy the new client random */ |
4901 | | XMEMCPY(ssl->arrays->clientRandom, args->output + |
4902 | | args->clientRandomOffset, RAN_LEN); |
4903 | | /* write the extensions for inner */ |
4904 | | args->length = 0; |
4905 | | ret = TLSX_WriteRequest(ssl, args->ech->innerClientHello + args->idx - |
4906 | | (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ), client_hello, |
4907 | | &args->length); |
4908 | | if (ret != 0) |
4909 | | return ret; |
4910 | | /* set the type to outer */ |
4911 | | args->ech->type = 0; |
4912 | | } |
4913 | | #endif |
4914 | | |
4915 | | /* Write out extensions for a request. */ |
4916 | | args->length = 0; |
4917 | | ret = TLSX_WriteRequest(ssl, args->output + args->idx, client_hello, |
4918 | | &args->length); |
4919 | | if (ret != 0) |
4920 | | return ret; |
4921 | | |
4922 | | args->idx += args->length; |
4923 | | |
4924 | | #if defined(HAVE_ECH) |
4925 | | /* encrypt and pack the ech innerClientHello */ |
4926 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
4927 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4928 | | ret = TLSX_FinalizeEch(args->ech, |
4929 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
4930 | | (word32)(args->sendSz - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ))); |
4931 | | |
4932 | | if (ret != 0) |
4933 | | return ret; |
4934 | | } |
4935 | | #endif |
4936 | | |
4937 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4938 | | /* Resumption has a specific set of extensions and binder is calculated |
4939 | | * for each identity. |
4940 | | */ |
4941 | | if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY)) { |
4942 | | ret = WritePSKBinders(ssl, args->output, args->idx); |
4943 | | } |
4944 | | else |
4945 | | #endif |
4946 | | { |
4947 | | #ifdef WOLFSSL_DTLS13 |
4948 | | if (ssl->options.dtls) |
4949 | | ret = Dtls13HashHandshake(ssl, |
4950 | | args->output + Dtls13GetRlHeaderLength(ssl, 0), |
4951 | | (word16)args->idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4952 | | else |
4953 | | #endif /* WOLFSSL_DTLS13 */ |
4954 | | { |
4955 | | #if defined(HAVE_ECH) |
4956 | | /* compute the inner hash */ |
4957 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
4958 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4959 | | ret = EchHashHelloInner(ssl, args->ech); |
4960 | | } |
4961 | | #endif |
4962 | | /* compute the outer hash */ |
4963 | | if (ret == 0) |
4964 | | ret = HashOutput(ssl, args->output, (int)args->idx, 0); |
4965 | | } |
4966 | | } |
4967 | | if (ret != 0) |
4968 | | return ret; |
4969 | | |
4970 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
4971 | | |
4972 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
4973 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
4974 | | if (ssl->toInfoOn) { |
4975 | | ret = AddPacketInfo(ssl, "ClientHello", handshake, args->output, |
4976 | | args->sendSz, WRITE_PROTO, 0, ssl->heap); |
4977 | | if (ret != 0) |
4978 | | return ret; |
4979 | | } |
4980 | | #endif |
4981 | | |
4982 | | ssl->options.buildingMsg = 0; |
4983 | | #ifdef WOLFSSL_DTLS13 |
4984 | | if (ssl->options.dtls) { |
4985 | | ret = Dtls13HandshakeSend(ssl, args->output, (word16)args->sendSz, |
4986 | | (word16)args->idx, client_hello, 0); |
4987 | | break; |
4988 | | } |
4989 | | #endif /* WOLFSSL_DTLS13 */ |
4990 | | |
4991 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
4992 | | |
4993 | | /* Advance state and proceed */ |
4994 | | ssl->options.asyncState = TLS_ASYNC_END; |
4995 | | } |
4996 | | /* case TLS_ASYNC_BUILD */ |
4997 | | FALL_THROUGH; |
4998 | | |
4999 | | case TLS_ASYNC_END: |
5000 | | { |
5001 | | #ifdef WOLFSSL_EARLY_DATA_GROUP |
5002 | | /* QUIC needs to forward records at their encryption level |
5003 | | * and is therefore unable to group here */ |
5004 | | if (ssl->earlyData == no_early_data || WOLFSSL_IS_QUIC(ssl)) |
5005 | | #endif |
5006 | | ret = SendBuffered(ssl); |
5007 | | |
5008 | | break; |
5009 | | } |
5010 | | default: |
5011 | | ret = INPUT_CASE_ERROR; |
5012 | | } /* switch (ssl->options.asyncState) */ |
5013 | | |
5014 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5015 | | if (ret == 0) |
5016 | | FreeAsyncCtx(ssl, 0); |
5017 | | #endif |
5018 | | |
5019 | | WOLFSSL_LEAVE("SendTls13ClientHello", ret); |
5020 | | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND); |
5021 | | |
5022 | | return ret; |
5023 | | } |
5024 | | |
5025 | | #if defined(WOLFSSL_DTLS13) && !defined(NO_WOLFSSL_CLIENT) |
5026 | | static int Dtls13ClientDoDowngrade(WOLFSSL* ssl) |
5027 | | { |
5028 | | int ret; |
5029 | | if (ssl->dtls13ClientHello == NULL) |
5030 | | return BAD_STATE_E; |
5031 | | |
5032 | | /* v1.3 and v1.2 hash messages to compute the transcript hash. When we are |
5033 | | * using DTLSv1.3 we hash the first clientHello following v1.3 but the |
5034 | | * server can negotiate a lower version. So we need to re-hash the |
5035 | | * clientHello to adhere to DTLS <= v1.2 rules. */ |
5036 | | ret = InitHandshakeHashes(ssl); |
5037 | | if (ret != 0) |
5038 | | return ret; |
5039 | | ret = HashRaw(ssl, ssl->dtls13ClientHello, ssl->dtls13ClientHelloSz); |
5040 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5041 | | ssl->dtls13ClientHello = NULL; |
5042 | | ssl->dtls13ClientHelloSz = 0; |
5043 | | ssl->keys.dtls_sequence_number_hi = |
5044 | | (word16)w64GetHigh32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5045 | | ssl->keys.dtls_sequence_number_lo = |
5046 | | w64GetLow32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5047 | | return ret; |
5048 | | } |
5049 | | #endif /* WOLFSSL_DTLS13 && !NO_WOLFSSL_CLIENT*/ |
5050 | | |
5051 | | #if defined(HAVE_ECH) |
5052 | | /* Calculate ECH acceptance and verify the server accepted ECH. |
5053 | | * |
5054 | | * ssl SSL/TLS object. |
5055 | | * label Ascii string describing ECH acceptance type. |
5056 | | * labelSz Length of label excluding NULL character. |
5057 | | * input The buffer to calculate confirmation off of. |
5058 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
5059 | | * helloSz Size of hello message. |
5060 | | * returns 0 on success and otherwise failure. |
5061 | | */ |
5062 | | static int EchCheckAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
5063 | | const byte* input, int acceptOffset, int helloSz, byte msgType) |
5064 | | { |
5065 | | int ret = 0; |
5066 | | int headerSz; |
5067 | | HS_Hashes* tmpHashes; |
5068 | | byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ]; |
5069 | | |
5070 | | XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation)); |
5071 | | |
5072 | | #ifdef WOLFSSL_DTLS13 |
5073 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
5074 | | HANDSHAKE_HEADER_SZ; |
5075 | | #else |
5076 | | headerSz = HANDSHAKE_HEADER_SZ; |
5077 | | #endif |
5078 | | |
5079 | | ret = EchCalcAcceptance(ssl, label, labelSz, input, acceptOffset, helloSz, |
5080 | | msgType == hello_retry_request, acceptConfirmation); |
5081 | | |
5082 | | if (ret == 0) { |
5083 | | tmpHashes = ssl->hsHashes; |
5084 | | ssl->hsHashes = ssl->hsHashesEch; |
5085 | | |
5086 | | /* last 8 bytes must match the expand output */ |
5087 | | ret = ConstantCompare(acceptConfirmation, input + acceptOffset, |
5088 | | ECH_ACCEPT_CONFIRMATION_SZ); |
5089 | | |
5090 | | if (ret == 0) { |
5091 | | ssl->options.echAccepted = 1; |
5092 | | |
5093 | | /* after HRR, hsHashesEch must contain: |
5094 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
5095 | | if (msgType == hello_retry_request) { |
5096 | | ret = HashRaw(ssl, input, helloSz + headerSz); |
5097 | | } |
5098 | | /* normal TLS code will calculate transcript of ServerHello */ |
5099 | | else { |
5100 | | ssl->hsHashes = tmpHashes; |
5101 | | FreeHandshakeHashes(ssl); |
5102 | | tmpHashes = ssl->hsHashesEch; |
5103 | | ssl->hsHashesEch = NULL; |
5104 | | } |
5105 | | } |
5106 | | else { |
5107 | | ssl->options.echAccepted = 0; |
5108 | | ret = 0; |
5109 | | |
5110 | | /* ECH rejected, continue with outer transcript */ |
5111 | | FreeHandshakeHashes(ssl); |
5112 | | ssl->hsHashesEch = NULL; |
5113 | | } |
5114 | | |
5115 | | ssl->hsHashes = tmpHashes; |
5116 | | } |
5117 | | |
5118 | | return ret; |
5119 | | } |
5120 | | #endif /* HAVE_ECH */ |
5121 | | |
5122 | | /* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */ |
5123 | | /* Handle the ServerHello message from the server. |
5124 | | * Only a client will receive this message. |
5125 | | * |
5126 | | * ssl The SSL/TLS object. |
5127 | | * input The message buffer. |
5128 | | * inOutIdx On entry, the index into the message buffer of ServerHello. |
5129 | | * On exit, the index of byte after the ServerHello message. |
5130 | | * helloSz The length of the current handshake message. |
5131 | | * returns 0 on success and otherwise failure. |
5132 | | */ |
5133 | | |
5134 | | typedef struct Dsh13Args { |
5135 | | ProtocolVersion pv; |
5136 | | word32 idx; |
5137 | | word32 begin; |
5138 | | const byte* sessId; |
5139 | | word16 totalExtSz; |
5140 | | byte sessIdSz; |
5141 | | byte extMsgType; |
5142 | | #if defined(HAVE_ECH) |
5143 | | TLSX* echX; |
5144 | | byte* acceptLabel; |
5145 | | word32 acceptOffset; |
5146 | | word16 acceptLabelSz; |
5147 | | #endif |
5148 | | } Dsh13Args; |
5149 | | |
5150 | | int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
5151 | | word32 helloSz, byte* extMsgType) |
5152 | | { |
5153 | | int ret; |
5154 | | byte suite[2]; |
5155 | | byte tls12minor; |
5156 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5157 | | Dsh13Args* args = NULL; |
5158 | | #else |
5159 | | Dsh13Args args[1]; |
5160 | | #endif |
5161 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5162 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
5163 | | #endif |
5164 | | |
5165 | | WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO); |
5166 | | WOLFSSL_ENTER("DoTls13ServerHello"); |
5167 | | |
5168 | | if (ssl == NULL || ssl->arrays == NULL) |
5169 | | return BAD_FUNC_ARG; |
5170 | | |
5171 | | tls12minor = TLSv1_2_MINOR; |
5172 | | |
5173 | | #ifdef WOLFSSL_DTLS13 |
5174 | | if (ssl->options.dtls) |
5175 | | tls12minor = DTLSv1_2_MINOR; |
5176 | | #endif /* WOLFSSL_DTLS13 */ |
5177 | | |
5178 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5179 | | if (ssl->async == NULL) { |
5180 | | ssl->async = (struct WOLFSSL_ASYNC*) |
5181 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
5182 | | DYNAMIC_TYPE_ASYNC); |
5183 | | if (ssl->async == NULL) |
5184 | | return MEMORY_E; |
5185 | | ssl->async->freeArgs = NULL; |
5186 | | } |
5187 | | args = (Dsh13Args*)ssl->async->args; |
5188 | | |
5189 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
5190 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
5191 | | /* Check for error */ |
5192 | | if (ret < 0) { |
5193 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5194 | | /* Mark message as not received so it can process again */ |
5195 | | ssl->msgsReceived.got_server_hello = 0; |
5196 | | } |
5197 | | return ret; |
5198 | | } |
5199 | | } |
5200 | | else |
5201 | | #endif |
5202 | | { |
5203 | | /* Reset state */ |
5204 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
5205 | | XMEMSET(args, 0, sizeof(Dsh13Args)); |
5206 | | } |
5207 | | |
5208 | | switch (ssl->options.asyncState) { |
5209 | | case TLS_ASYNC_BEGIN: |
5210 | | { |
5211 | | byte b; |
5212 | | #ifdef WOLFSSL_CALLBACKS |
5213 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello"); |
5214 | | if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo); |
5215 | | #endif |
5216 | | |
5217 | | /* Protocol version length check. */ |
5218 | | if (helloSz < OPAQUE16_LEN) |
5219 | | return BUFFER_ERROR; |
5220 | | |
5221 | | args->idx = *inOutIdx; |
5222 | | args->begin = args->idx; |
5223 | | |
5224 | | /* Protocol version */ |
5225 | | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
5226 | | args->idx += OPAQUE16_LEN; |
5227 | | |
5228 | | #ifdef WOLFSSL_DTLS |
5229 | | if (ssl->options.dtls && |
5230 | | (args->pv.major != DTLS_MAJOR || args->pv.minor == DTLS_BOGUS_MINOR)) |
5231 | | return VERSION_ERROR; |
5232 | | #endif /* WOLFSSL_DTLS */ |
5233 | | |
5234 | | #ifndef WOLFSSL_NO_TLS12 |
5235 | | { |
5236 | | byte wantDowngrade; |
5237 | | |
5238 | | wantDowngrade = args->pv.major == ssl->version.major && |
5239 | | args->pv.minor < TLSv1_2_MINOR; |
5240 | | |
5241 | | #ifdef WOLFSSL_DTLS13 |
5242 | | if (ssl->options.dtls) |
5243 | | wantDowngrade = args->pv.major == ssl->version.major && |
5244 | | args->pv.minor > DTLSv1_2_MINOR; |
5245 | | #endif /* WOLFSSL_DTLS13 */ |
5246 | | |
5247 | | if (wantDowngrade && ssl->options.downgrade) { |
5248 | | /* Force client hello version 1.2 to work for static RSA. */ |
5249 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5250 | | ssl->version.minor = TLSv1_2_MINOR; |
5251 | | ssl->options.tls1_3 = 0; |
5252 | | |
5253 | | #ifdef WOLFSSL_DTLS13 |
5254 | | if (ssl->options.dtls) { |
5255 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5256 | | ssl->version.minor = DTLSv1_2_MINOR; |
5257 | | ret = Dtls13ClientDoDowngrade(ssl); |
5258 | | if (ret != 0) |
5259 | | return ret; |
5260 | | } |
5261 | | #endif /* WOLFSSL_DTLS13 */ |
5262 | | |
5263 | | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5264 | | } |
5265 | | } |
5266 | | #endif |
5267 | | |
5268 | | if (args->pv.major != ssl->version.major || |
5269 | | args->pv.minor != tls12minor) { |
5270 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5271 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5272 | | return VERSION_ERROR; |
5273 | | } |
5274 | | |
5275 | | /* Random and session id length check */ |
5276 | | if ((args->idx - args->begin) + RAN_LEN + ENUM_LEN > helloSz) |
5277 | | return BUFFER_ERROR; |
5278 | | |
5279 | | /* Check if hello retry request */ |
5280 | | if (XMEMCMP(input + args->idx, helloRetryRequestRandom, RAN_LEN) == 0) { |
5281 | | WOLFSSL_MSG("HelloRetryRequest format"); |
5282 | | *extMsgType = hello_retry_request; |
5283 | | |
5284 | | if (ssl->msgsReceived.got_hello_verify_request) { |
5285 | | WOLFSSL_MSG("Received HelloRetryRequest after a " |
5286 | | "HelloVerifyRequest"); |
5287 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5288 | | return VERSION_ERROR; |
5289 | | } |
5290 | | |
5291 | | /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat. |
5292 | | * Found message to be a HelloRetryRequest. |
5293 | | * Don't allow more than one HelloRetryRequest or ServerHello. |
5294 | | */ |
5295 | | if (ssl->msgsReceived.got_hello_retry_request) { |
5296 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
5297 | | return DUPLICATE_MSG_E; |
5298 | | } |
5299 | | } |
5300 | | args->extMsgType = *extMsgType; |
5301 | | |
5302 | | /* Server random - keep for debugging. */ |
5303 | | XMEMCPY(ssl->arrays->serverRandom, input + args->idx, RAN_LEN); |
5304 | | #if defined(HAVE_ECH) |
5305 | | /* last 8 bytes of server random */ |
5306 | | args->acceptOffset = args->idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
5307 | | #endif |
5308 | | args->idx += RAN_LEN; |
5309 | | |
5310 | | /* Session id */ |
5311 | | args->sessIdSz = input[args->idx++]; |
5312 | | if ((args->idx - args->begin) + args->sessIdSz > helloSz) |
5313 | | return BUFFER_ERROR; |
5314 | | args->sessId = input + args->idx; |
5315 | | args->idx += args->sessIdSz; |
5316 | | |
5317 | | ssl->options.haveSessionId = 1; |
5318 | | |
5319 | | /* Ciphersuite and compression check */ |
5320 | | if ((args->idx - args->begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz) |
5321 | | return BUFFER_ERROR; |
5322 | | |
5323 | | /* Set the cipher suite from the message. */ |
5324 | | ssl->options.cipherSuite0 = input[args->idx++]; |
5325 | | ssl->options.cipherSuite = input[args->idx++]; |
5326 | | if (*extMsgType == hello_retry_request) { |
5327 | | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
5328 | | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
5329 | | } |
5330 | | else if (ssl->msgsReceived.got_hello_retry_request && |
5331 | | (ssl->options.hrrCipherSuite0 != ssl->options.cipherSuite0 || |
5332 | | ssl->options.hrrCipherSuite != ssl->options.cipherSuite)) { |
5333 | | WOLFSSL_MSG("Received ServerHello with different cipher suite than " |
5334 | | "HelloRetryRequest"); |
5335 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5336 | | return INVALID_PARAMETER; |
5337 | | } |
5338 | | #ifdef WOLFSSL_DEBUG_TLS |
5339 | | WOLFSSL_MSG("Chosen cipher suite:"); |
5340 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
5341 | | ssl->options.cipherSuite)); |
5342 | | #endif |
5343 | | |
5344 | | /* Compression */ |
5345 | | b = input[args->idx++]; |
5346 | | if (b != 0) { |
5347 | | WOLFSSL_MSG("Must be no compression types in list"); |
5348 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5349 | | return INVALID_PARAMETER; |
5350 | | } |
5351 | | |
5352 | | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) { |
5353 | | if (!ssl->options.downgrade) |
5354 | | return BUFFER_ERROR; |
5355 | | #ifndef WOLFSSL_NO_TLS12 |
5356 | | /* Force client hello version 1.2 to work for static RSA. */ |
5357 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5358 | | ssl->version.minor = TLSv1_2_MINOR; |
5359 | | |
5360 | | #ifdef WOLFSSL_DTLS13 |
5361 | | if (ssl->options.dtls) { |
5362 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5363 | | ssl->version.minor = DTLSv1_2_MINOR; |
5364 | | ssl->options.tls1_3 = 0; |
5365 | | ret = Dtls13ClientDoDowngrade(ssl); |
5366 | | if (ret != 0) |
5367 | | return ret; |
5368 | | } |
5369 | | #endif /* WOLFSSL_DTLS13 */ |
5370 | | |
5371 | | #endif |
5372 | | ssl->options.haveEMS = 0; |
5373 | | if (args->pv.minor < ssl->options.minDowngrade) { |
5374 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5375 | | return VERSION_ERROR; |
5376 | | } |
5377 | | #ifndef WOLFSSL_NO_TLS12 |
5378 | | ssl->options.tls1_3 = 0; |
5379 | | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5380 | | #else |
5381 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5382 | | return VERSION_ERROR; |
5383 | | #endif |
5384 | | } |
5385 | | |
5386 | | if ((args->idx - args->begin) < helloSz) { |
5387 | | int foundVersion; |
5388 | | |
5389 | | /* Get extension length and length check. */ |
5390 | | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
5391 | | return BUFFER_ERROR; |
5392 | | ato16(&input[args->idx], &args->totalExtSz); |
5393 | | args->idx += OPAQUE16_LEN; |
5394 | | if ((args->idx - args->begin) + args->totalExtSz > helloSz) |
5395 | | return BUFFER_ERROR; |
5396 | | |
5397 | | /* Need to negotiate version first. */ |
5398 | | if ((ret = TLSX_ParseVersion(ssl, input + args->idx, |
5399 | | args->totalExtSz, *extMsgType, &foundVersion))) { |
5400 | | return ret; |
5401 | | } |
5402 | | if (!foundVersion) { |
5403 | | if (!ssl->options.downgrade) { |
5404 | | WOLFSSL_MSG("Server trying to downgrade to version less than " |
5405 | | "TLS v1.3"); |
5406 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5407 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5408 | | return VERSION_ERROR; |
5409 | | } |
5410 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \ |
5411 | | defined(WOLFSSL_WPAS_SMALL) |
5412 | | /* Check if client has disabled TLS 1.2 */ |
5413 | | if (args->pv.minor == TLSv1_2_MINOR && |
5414 | | (ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) |
5415 | | == WOLFSSL_OP_NO_TLSv1_2) |
5416 | | { |
5417 | | WOLFSSL_MSG("\tOption set to not allow TLSv1.2"); |
5418 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5419 | | return VERSION_ERROR; |
5420 | | } |
5421 | | #endif |
5422 | | |
5423 | | if (!ssl->options.dtls && |
5424 | | args->pv.minor < ssl->options.minDowngrade) { |
5425 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5426 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5427 | | return VERSION_ERROR; |
5428 | | } |
5429 | | |
5430 | | if (ssl->options.dtls && |
5431 | | args->pv.minor > ssl->options.minDowngrade) { |
5432 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5433 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5434 | | return VERSION_ERROR; |
5435 | | } |
5436 | | |
5437 | | ssl->version.minor = args->pv.minor; |
5438 | | ssl->options.tls1_3 = 0; |
5439 | | |
5440 | | #ifdef WOLFSSL_DTLS13 |
5441 | | if (ssl->options.dtls) { |
5442 | | ret = Dtls13ClientDoDowngrade(ssl); |
5443 | | if (ret != 0) |
5444 | | return ret; |
5445 | | } |
5446 | | #endif /* WOLFSSL_DTLS13 */ |
5447 | | } |
5448 | | } |
5449 | | |
5450 | | #ifdef WOLFSSL_DTLS13 |
5451 | | /* we are sure that version is >= v1.3 now, we can get rid of buffered |
5452 | | * ClientHello that was buffered to re-compute the hash in case of |
5453 | | * downgrade */ |
5454 | | if (ssl->options.dtls && ssl->dtls13ClientHello != NULL) { |
5455 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5456 | | ssl->dtls13ClientHello = NULL; |
5457 | | ssl->dtls13ClientHelloSz = 0; |
5458 | | } |
5459 | | #endif /* WOLFSSL_DTLS13 */ |
5460 | | |
5461 | | /* Advance state and proceed */ |
5462 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
5463 | | } /* case TLS_ASYNC_BEGIN */ |
5464 | | FALL_THROUGH; |
5465 | | |
5466 | | case TLS_ASYNC_BUILD: |
5467 | | case TLS_ASYNC_DO: |
5468 | | { |
5469 | | /* restore message type */ |
5470 | | *extMsgType = args->extMsgType; |
5471 | | |
5472 | | /* Parse and handle extensions, unless lower than TLS1.3. In that case, |
5473 | | * extensions will be parsed in DoServerHello. */ |
5474 | | if (args->totalExtSz > 0 && IsAtLeastTLSv1_3(ssl->version)) { |
5475 | | ret = TLSX_Parse(ssl, input + args->idx, args->totalExtSz, |
5476 | | *extMsgType, NULL); |
5477 | | if (ret != 0) { |
5478 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5479 | | /* Handle async operation */ |
5480 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5481 | | /* Mark message as not received so it can process again */ |
5482 | | ssl->msgsReceived.got_server_hello = 0; |
5483 | | } |
5484 | | #endif |
5485 | | return ret; |
5486 | | } |
5487 | | |
5488 | | if (*extMsgType == hello_retry_request) { |
5489 | | /* Update counts to reflect change of message type. */ |
5490 | | ssl->msgsReceived.got_hello_retry_request = 1; |
5491 | | ssl->msgsReceived.got_server_hello = 0; |
5492 | | } |
5493 | | } |
5494 | | |
5495 | | if (args->totalExtSz > 0) { |
5496 | | args->idx += args->totalExtSz; |
5497 | | } |
5498 | | |
5499 | | #ifdef WOLFSSL_DTLS_CID |
5500 | | if (ssl->options.useDtlsCID && *extMsgType == server_hello) |
5501 | | DtlsCIDOnExtensionsParsed(ssl); |
5502 | | #endif /* WOLFSSL_DTLS_CID */ |
5503 | | |
5504 | | if (IsAtLeastTLSv1_3(ssl->version)) { |
5505 | | *inOutIdx = args->idx; |
5506 | | } |
5507 | | |
5508 | | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5509 | | |
5510 | | #ifdef HAVE_SECRET_CALLBACK |
5511 | | if (ssl->sessionSecretCb != NULL |
5512 | | #ifdef HAVE_SESSION_TICKET |
5513 | | && ssl->session->ticketLen > 0 |
5514 | | #endif |
5515 | | ) { |
5516 | | int secretSz = SECRET_LEN; |
5517 | | ret = ssl->sessionSecretCb(ssl, ssl->session->masterSecret, |
5518 | | &secretSz, ssl->sessionSecretCtx); |
5519 | | if (ret != 0 || secretSz != SECRET_LEN) { |
5520 | | WOLFSSL_ERROR_VERBOSE(SESSION_SECRET_CB_E); |
5521 | | return SESSION_SECRET_CB_E; |
5522 | | } |
5523 | | } |
5524 | | #endif /* HAVE_SECRET_CALLBACK */ |
5525 | | |
5526 | | /* Version only negotiated in extensions for TLS v1.3. |
5527 | | * Only now do we know how to deal with session id. |
5528 | | */ |
5529 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
5530 | | #ifndef WOLFSSL_NO_TLS12 |
5531 | | ssl->arrays->sessionIDSz = args->sessIdSz; |
5532 | | |
5533 | | if (ssl->arrays->sessionIDSz > ID_LEN) { |
5534 | | WOLFSSL_MSG("Invalid session ID size"); |
5535 | | ssl->arrays->sessionIDSz = 0; |
5536 | | return BUFFER_ERROR; |
5537 | | } |
5538 | | else if (ssl->arrays->sessionIDSz) { |
5539 | | XMEMCPY(ssl->arrays->sessionID, args->sessId, |
5540 | | ssl->arrays->sessionIDSz); |
5541 | | ssl->options.haveSessionId = 1; |
5542 | | } |
5543 | | |
5544 | | /* Force client hello version 1.2 to work for static RSA. */ |
5545 | | if (ssl->options.dtls) |
5546 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5547 | | else |
5548 | | ssl->chVersion.minor = TLSv1_2_MINOR; |
5549 | | /* Complete TLS v1.2 processing of ServerHello. */ |
5550 | | ret = DoServerHello(ssl, input, inOutIdx, helloSz); |
5551 | | #else |
5552 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
5553 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5554 | | ret = VERSION_ERROR; |
5555 | | #endif |
5556 | | |
5557 | | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5558 | | |
5559 | | return ret; |
5560 | | } |
5561 | | |
5562 | | /* Advance state and proceed */ |
5563 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
5564 | | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
5565 | | FALL_THROUGH; |
5566 | | |
5567 | | case TLS_ASYNC_FINALIZE: |
5568 | | { |
5569 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
5570 | | if (ssl->options.tls13MiddleBoxCompat) { |
5571 | | if (args->sessIdSz == 0) { |
5572 | | WOLFSSL_MSG("args->sessIdSz == 0"); |
5573 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5574 | | return INVALID_PARAMETER; |
5575 | | } |
5576 | | if (ssl->session->sessionIDSz != 0) { |
5577 | | if (ssl->session->sessionIDSz != args->sessIdSz || |
5578 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5579 | | args->sessIdSz) != 0) { |
5580 | | WOLFSSL_MSG("session id doesn't match"); |
5581 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5582 | | return INVALID_PARAMETER; |
5583 | | } |
5584 | | } |
5585 | | else if (XMEMCMP(ssl->arrays->clientRandom, args->sessId, |
5586 | | args->sessIdSz) != 0) { |
5587 | | WOLFSSL_MSG("session id doesn't match client random"); |
5588 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5589 | | return INVALID_PARAMETER; |
5590 | | } |
5591 | | } |
5592 | | else |
5593 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
5594 | | #ifdef WOLFSSL_QUIC |
5595 | | if (WOLFSSL_IS_QUIC(ssl)) { |
5596 | | if (args->sessIdSz != 0) { |
5597 | | WOLFSSL_MSG("args->sessIdSz != 0"); |
5598 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5599 | | return INVALID_PARAMETER; |
5600 | | } |
5601 | | } |
5602 | | else |
5603 | | #endif /* WOLFSSL_QUIC */ |
5604 | | if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 && |
5605 | | XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0)) |
5606 | | { |
5607 | | WOLFSSL_MSG("Server sent different session id"); |
5608 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5609 | | return INVALID_PARAMETER; |
5610 | | } |
5611 | | |
5612 | | ret = SetCipherSpecs(ssl); |
5613 | | if (ret != 0) |
5614 | | return ret; |
5615 | | |
5616 | | #ifdef HAVE_NULL_CIPHER |
5617 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
5618 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
5619 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
5620 | | ; |
5621 | | } |
5622 | | else |
5623 | | #endif |
5624 | | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
5625 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5626 | | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
5627 | | ; /* Do nothing. */ |
5628 | | } |
5629 | | else |
5630 | | #endif |
5631 | | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
5632 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5633 | | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
5634 | | ; /* Do nothing. */ |
5635 | | } |
5636 | | else |
5637 | | #endif |
5638 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
5639 | | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
5640 | | WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet"); |
5641 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5642 | | return INVALID_PARAMETER; |
5643 | | } |
5644 | | |
5645 | | suite[0] = ssl->options.cipherSuite0; |
5646 | | suite[1] = ssl->options.cipherSuite; |
5647 | | if (!FindSuiteSSL(ssl, suite)) { |
5648 | | WOLFSSL_MSG("Cipher suite not supported on client"); |
5649 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5650 | | return INVALID_PARAMETER; |
5651 | | } |
5652 | | |
5653 | | #if defined(HAVE_ECH) |
5654 | | /* check for acceptConfirmation */ |
5655 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH) { |
5656 | | args->echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
5657 | | /* account for hrr extension instead of server random */ |
5658 | | if (args->extMsgType == hello_retry_request) { |
5659 | | args->acceptOffset = |
5660 | | (word32)(((WOLFSSL_ECH*)args->echX->data)->confBuf - input); |
5661 | | args->acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
5662 | | args->acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
5663 | | } |
5664 | | else { |
5665 | | args->acceptLabel = (byte*)echAcceptConfirmationLabel; |
5666 | | args->acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
5667 | | } |
5668 | | /* check acceptance */ |
5669 | | if (ret == 0) { |
5670 | | ret = EchCheckAcceptance(ssl, args->acceptLabel, |
5671 | | args->acceptLabelSz, input, args->acceptOffset, helloSz, |
5672 | | args->extMsgType); |
5673 | | } |
5674 | | if (ret != 0) |
5675 | | return ret; |
5676 | | /* use the inner random for client random */ |
5677 | | if (args->extMsgType != hello_retry_request) { |
5678 | | XMEMCPY(ssl->arrays->clientRandom, ssl->arrays->clientRandomInner, |
5679 | | RAN_LEN); |
5680 | | } |
5681 | | } |
5682 | | #endif /* HAVE_ECH */ |
5683 | | |
5684 | | if (*extMsgType == server_hello) { |
5685 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5686 | | PreSharedKey* psk = NULL; |
5687 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
5688 | | if (ext != NULL) |
5689 | | psk = (PreSharedKey*)ext->data; |
5690 | | while (psk != NULL && !psk->chosen) |
5691 | | psk = psk->next; |
5692 | | if (psk == NULL) { |
5693 | | ssl->options.resuming = 0; |
5694 | | ssl->arrays->psk_keySz = 0; |
5695 | | XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); |
5696 | | } |
5697 | | else { |
5698 | | if ((ret = SetupPskKey(ssl, psk, 0)) != 0) |
5699 | | return ret; |
5700 | | ssl->options.pskNegotiated = 1; |
5701 | | } |
5702 | | #else |
5703 | | /* no resumption possible */ |
5704 | | ssl->options.resuming = 0; |
5705 | | #endif |
5706 | | |
5707 | | /* sanity check on PSK / KSE */ |
5708 | | if ( |
5709 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5710 | | ssl->options.pskNegotiated == 0 && |
5711 | | #endif |
5712 | | (ssl->session->namedGroup == 0 || |
5713 | | ssl->options.shSentKeyShare == 0)) { |
5714 | | return EXT_MISSING; |
5715 | | } |
5716 | | |
5717 | | ssl->keys.encryptionOn = 1; |
5718 | | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5719 | | |
5720 | | } |
5721 | | else { |
5722 | | /* https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.4 |
5723 | | * Clients MUST abort the handshake with an |
5724 | | * "illegal_parameter" alert if the HelloRetryRequest would not result |
5725 | | * in any change in the ClientHello. |
5726 | | */ |
5727 | | /* Check if the HRR contained a cookie or a keyshare */ |
5728 | | if (!ssl->options.hrrSentKeyShare |
5729 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
5730 | | && !ssl->options.hrrSentCookie |
5731 | | #endif |
5732 | | ) { |
5733 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
5734 | | return EXT_MISSING; |
5735 | | } |
5736 | | |
5737 | | ssl->options.tls1_3 = 1; |
5738 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
5739 | | |
5740 | | ret = RestartHandshakeHash(ssl); |
5741 | | } |
5742 | | |
5743 | | break; |
5744 | | } /* case TLS_ASYNC_FINALIZE */ |
5745 | | default: |
5746 | | ret = INPUT_CASE_ERROR; |
5747 | | } /* switch (ssl->options.asyncState) */ |
5748 | | |
5749 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5750 | | if (ret == 0) |
5751 | | FreeAsyncCtx(ssl, 0); |
5752 | | #endif |
5753 | | |
5754 | | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5755 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO); |
5756 | | |
5757 | | return ret; |
5758 | | } |
5759 | | |
5760 | | /* handle processing TLS 1.3 encrypted_extensions (8) */ |
5761 | | /* Parse and handle an EncryptedExtensions message. |
5762 | | * Only a client will receive this message. |
5763 | | * |
5764 | | * ssl The SSL/TLS object. |
5765 | | * input The message buffer. |
5766 | | * inOutIdx On entry, the index into the message buffer of |
5767 | | * EncryptedExtensions. |
5768 | | * On exit, the index of byte after the EncryptedExtensions |
5769 | | * message. |
5770 | | * totalSz The length of the current handshake message. |
5771 | | * returns 0 on success and otherwise failure. |
5772 | | */ |
5773 | | static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input, |
5774 | | word32* inOutIdx, word32 totalSz) |
5775 | 0 | { |
5776 | 0 | int ret; |
5777 | 0 | word32 begin = *inOutIdx; |
5778 | 0 | word32 i = begin; |
5779 | 0 | word16 totalExtSz; |
5780 | |
|
5781 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5782 | 0 | WOLFSSL_ENTER("DoTls13EncryptedExtensions"); |
5783 | |
|
5784 | | #ifdef WOLFSSL_CALLBACKS |
5785 | | if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions"); |
5786 | | if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo); |
5787 | | #endif |
5788 | | |
5789 | | /* Length field of extension data. */ |
5790 | 0 | if (totalSz < OPAQUE16_LEN) |
5791 | 0 | return BUFFER_ERROR; |
5792 | 0 | ato16(&input[i], &totalExtSz); |
5793 | 0 | i += OPAQUE16_LEN; |
5794 | | |
5795 | | /* Extension data. */ |
5796 | 0 | if (i - begin + totalExtSz > totalSz) |
5797 | 0 | return BUFFER_ERROR; |
5798 | 0 | if ((ret = TLSX_Parse(ssl, input + i, totalExtSz, encrypted_extensions, |
5799 | 0 | NULL))) { |
5800 | 0 | return ret; |
5801 | 0 | } |
5802 | | |
5803 | | /* Move index to byte after message. */ |
5804 | 0 | *inOutIdx = i + totalExtSz; |
5805 | | |
5806 | | /* Always encrypted. */ |
5807 | 0 | *inOutIdx += ssl->keys.padSz; |
5808 | |
|
5809 | | #ifdef WOLFSSL_EARLY_DATA |
5810 | | if (ssl->earlyData != no_early_data) { |
5811 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
5812 | | if (ext == NULL || !ext->val) |
5813 | | ssl->earlyData = no_early_data; |
5814 | | } |
5815 | | |
5816 | | if (ssl->earlyData == no_early_data) { |
5817 | | ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY); |
5818 | | if (ret != 0) |
5819 | | return ret; |
5820 | | } |
5821 | | #endif /* WOLFSSL_EARLY_DATA */ |
5822 | |
|
5823 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
5824 | |
|
5825 | 0 | WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret); |
5826 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5827 | |
|
5828 | 0 | return ret; |
5829 | 0 | } |
5830 | | |
5831 | | #ifndef NO_CERTS |
5832 | | /* handle processing TLS v1.3 certificate_request (13) */ |
5833 | | /* Handle a TLS v1.3 CertificateRequest message. |
5834 | | * This message is always encrypted. |
5835 | | * Only a client will receive this message. |
5836 | | * |
5837 | | * ssl The SSL/TLS object. |
5838 | | * input The message buffer. |
5839 | | * inOutIdx On entry, the index into the message buffer of CertificateRequest. |
5840 | | * On exit, the index of byte after the CertificateRequest message. |
5841 | | * size The length of the current handshake message. |
5842 | | * returns 0 on success and otherwise failure. |
5843 | | */ |
5844 | | static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input, |
5845 | | word32* inOutIdx, word32 size) |
5846 | 0 | { |
5847 | 0 | word16 len; |
5848 | 0 | word32 begin = *inOutIdx; |
5849 | 0 | int ret = 0; |
5850 | 0 | Suites peerSuites; |
5851 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5852 | | CertReqCtx* certReqCtx; |
5853 | | #endif |
5854 | |
|
5855 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5856 | 0 | WOLFSSL_ENTER("DoTls13CertificateRequest"); |
5857 | |
|
5858 | 0 | XMEMSET(&peerSuites, 0, sizeof(Suites)); |
5859 | |
|
5860 | | #ifdef WOLFSSL_CALLBACKS |
5861 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest"); |
5862 | | if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo); |
5863 | | #endif |
5864 | |
|
5865 | 0 | if (OPAQUE8_LEN > size) |
5866 | 0 | return BUFFER_ERROR; |
5867 | | |
5868 | | /* Length of the request context. */ |
5869 | 0 | len = input[(*inOutIdx)++]; |
5870 | 0 | if ((*inOutIdx - begin) + len > size) |
5871 | 0 | return BUFFER_ERROR; |
5872 | 0 | if (ssl->options.connectState < FINISHED_DONE && len > 0) |
5873 | 0 | return BUFFER_ERROR; |
5874 | | |
5875 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
5876 | | /* CertReqCtx has one byte at end for context value. |
5877 | | * Increase size to handle other implementations sending more than one byte. |
5878 | | * That is, allocate extra space, over one byte, to hold the context value. |
5879 | | */ |
5880 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx) + (len == 0 ? 0 : len - 1), ssl->heap, |
5881 | | DYNAMIC_TYPE_TMP_BUFFER); |
5882 | | if (certReqCtx == NULL) |
5883 | | return MEMORY_E; |
5884 | | certReqCtx->next = ssl->certReqCtx; |
5885 | | certReqCtx->len = len; |
5886 | | XMEMCPY(&certReqCtx->ctx, input + *inOutIdx, len); |
5887 | | ssl->certReqCtx = certReqCtx; |
5888 | | #endif |
5889 | 0 | *inOutIdx += len; |
5890 | | |
5891 | | /* TODO: Add support for more extensions: |
5892 | | * signed_certificate_timestamp, certificate_authorities, oid_filters. |
5893 | | */ |
5894 | | /* Certificate extensions */ |
5895 | 0 | if ((*inOutIdx - begin) + OPAQUE16_LEN > size) |
5896 | 0 | return BUFFER_ERROR; |
5897 | 0 | ato16(input + *inOutIdx, &len); |
5898 | 0 | *inOutIdx += OPAQUE16_LEN; |
5899 | 0 | if ((*inOutIdx - begin) + len > size) |
5900 | 0 | return BUFFER_ERROR; |
5901 | 0 | if (len == 0) |
5902 | 0 | return INVALID_PARAMETER; |
5903 | 0 | if ((ret = TLSX_Parse(ssl, input + *inOutIdx, len, certificate_request, |
5904 | 0 | &peerSuites))) { |
5905 | 0 | return ret; |
5906 | 0 | } |
5907 | 0 | *inOutIdx += len; |
5908 | |
|
5909 | | #ifdef WOLFSSL_CERT_SETUP_CB |
5910 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
5911 | | return ret; |
5912 | | #endif |
5913 | |
|
5914 | 0 | if ((ssl->buffers.certificate && ssl->buffers.certificate->buffer && |
5915 | 0 | ((ssl->buffers.key && ssl->buffers.key->buffer) |
5916 | | #ifdef HAVE_PK_CALLBACKS |
5917 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
5918 | | #endif |
5919 | 0 | )) |
5920 | | #ifdef OPENSSL_EXTRA |
5921 | | || ssl->ctx->certSetupCb != NULL |
5922 | | #endif |
5923 | 0 | ) { |
5924 | 0 | if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo, |
5925 | 0 | peerSuites.hashSigAlgoSz, 0) != 0) { |
5926 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5927 | 0 | return INVALID_PARAMETER; |
5928 | 0 | } |
5929 | 0 | ssl->options.sendVerify = SEND_CERT; |
5930 | 0 | } |
5931 | 0 | else { |
5932 | 0 | #ifndef WOLFSSL_NO_CLIENT_CERT_ERROR |
5933 | 0 | ssl->options.sendVerify = SEND_BLANK_CERT; |
5934 | | #else |
5935 | | WOLFSSL_MSG("Certificate required but none set on client"); |
5936 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
5937 | | WOLFSSL_ERROR_VERBOSE(NO_CERT_ERROR); |
5938 | | return NO_CERT_ERROR; |
5939 | | #endif |
5940 | 0 | } |
5941 | | |
5942 | | /* This message is always encrypted so add encryption padding. */ |
5943 | 0 | *inOutIdx += ssl->keys.padSz; |
5944 | |
|
5945 | 0 | WOLFSSL_LEAVE("DoTls13CertificateRequest", ret); |
5946 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO); |
5947 | |
|
5948 | 0 | return ret; |
5949 | 0 | } |
5950 | | #endif /* !NO_CERTS */ |
5951 | | #endif /* !NO_WOLFSSL_CLIENT */ |
5952 | | |
5953 | | #ifndef NO_WOLFSSL_SERVER |
5954 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5955 | | #ifndef NO_PSK |
5956 | | int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key, |
5957 | | word32* psk_keySz, const byte* suite, int* found, byte* foundSuite) |
5958 | | { |
5959 | | const char* cipherName = NULL; |
5960 | | byte cipherSuite0 = TLS13_BYTE; |
5961 | | byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
5962 | | int ret = 0; |
5963 | | |
5964 | | *found = 0; |
5965 | | (void)suite; |
5966 | | |
5967 | | if (ssl->options.server_psk_tls13_cb != NULL) { |
5968 | | *psk_keySz = ssl->options.server_psk_tls13_cb((WOLFSSL*)ssl, |
5969 | | (char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName); |
5970 | | if (*psk_keySz != 0) { |
5971 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
5972 | | *found = (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
5973 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0); |
5974 | | (void)cipherSuiteFlags; |
5975 | | } |
5976 | | } |
5977 | | if (*found == 0 && (ssl->options.server_psk_cb != NULL)) { |
5978 | | *psk_keySz = ssl->options.server_psk_cb((WOLFSSL*)ssl, |
5979 | | (char*)psk->identity, psk_key, |
5980 | | MAX_PSK_KEY_LEN); |
5981 | | *found = (*psk_keySz != 0); |
5982 | | } |
5983 | | if (*found) { |
5984 | | if (*psk_keySz > MAX_PSK_KEY_LEN && |
5985 | | *((int*)psk_keySz) != WC_NO_ERR_TRACE(USE_HW_PSK)) { |
5986 | | WOLFSSL_MSG("Key len too long in FindPsk()"); |
5987 | | ret = PSK_KEY_ERROR; |
5988 | | WOLFSSL_ERROR_VERBOSE(ret); |
5989 | | *found = 0; |
5990 | | } |
5991 | | if (ret == 0) { |
5992 | | #if !defined(WOLFSSL_PSK_ONE_ID) && !defined(WOLFSSL_PRIORITIZE_PSK) |
5993 | | /* Check whether PSK ciphersuite is in SSL. */ |
5994 | | *found = (suite[0] == cipherSuite0) && (suite[1] == cipherSuite); |
5995 | | #else |
5996 | | (void)suite; |
5997 | | /* Check whether PSK ciphersuite is in SSL. */ |
5998 | | { |
5999 | | byte s[2] = { |
6000 | | cipherSuite0, |
6001 | | cipherSuite, |
6002 | | }; |
6003 | | *found = FindSuiteSSL(ssl, s); |
6004 | | } |
6005 | | #endif |
6006 | | } |
6007 | | } |
6008 | | if (*found && foundSuite != NULL) { |
6009 | | foundSuite[0] = cipherSuite0; |
6010 | | foundSuite[1] = cipherSuite; |
6011 | | } |
6012 | | |
6013 | | return ret; |
6014 | | } |
6015 | | |
6016 | | /* Attempt to find the PSK (not session ticket) that matches. |
6017 | | * |
6018 | | * @param [in, out] ssl The SSL/TLS object. |
6019 | | * @param [in] psk A pre-shared key from the extension. |
6020 | | * @param [out] suite Cipher suite to use with PSK. |
6021 | | * @param [out] err Error code. |
6022 | | * PSK_KEY_ERROR when key is too big or ticket age is |
6023 | | * invalid, |
6024 | | * UNSUPPORTED_SUITE on invalid suite. |
6025 | | * Other error when attempting to derive early secret. |
6026 | | * @return 1 when a match found - but check error code. |
6027 | | * @return 0 when no match found. |
6028 | | */ |
6029 | | static int FindPsk(WOLFSSL* ssl, PreSharedKey* psk, const byte* suite, int* err) |
6030 | | { |
6031 | | int ret = 0; |
6032 | | int found = 0; |
6033 | | byte foundSuite[SUITE_LEN]; |
6034 | | |
6035 | | WOLFSSL_ENTER("FindPsk"); |
6036 | | |
6037 | | XMEMSET(foundSuite, 0, sizeof(foundSuite)); |
6038 | | |
6039 | | ret = FindPskSuite(ssl, psk, ssl->arrays->psk_key, &ssl->arrays->psk_keySz, |
6040 | | suite, &found, foundSuite); |
6041 | | if (ret == 0 && found) { |
6042 | | /* Default to ciphersuite if cb doesn't specify. */ |
6043 | | ssl->options.resuming = 0; |
6044 | | /* Don't send certificate request when using PSK. */ |
6045 | | ssl->options.verifyPeer = 0; |
6046 | | |
6047 | | /* PSK age is always zero. */ |
6048 | | if (psk->ticketAge != 0) { |
6049 | | ret = PSK_KEY_ERROR; |
6050 | | WOLFSSL_ERROR_VERBOSE(ret); |
6051 | | } |
6052 | | if (ret == 0) { |
6053 | | /* Set PSK ciphersuite into SSL. */ |
6054 | | ssl->options.cipherSuite0 = foundSuite[0]; |
6055 | | ssl->options.cipherSuite = foundSuite[1]; |
6056 | | ret = SetCipherSpecs(ssl); |
6057 | | } |
6058 | | if (ret == 0) { |
6059 | | /* Derive the early secret using the PSK. */ |
6060 | | ret = DeriveEarlySecret(ssl); |
6061 | | } |
6062 | | if (ret == 0) { |
6063 | | /* PSK negotiation has succeeded */ |
6064 | | ssl->options.isPSK = 1; |
6065 | | /* SERVER: using PSK for peer authentication. */ |
6066 | | ssl->options.peerAuthGood = 1; |
6067 | | } |
6068 | | } |
6069 | | |
6070 | | *err = ret; |
6071 | | WOLFSSL_LEAVE("FindPsk", found); |
6072 | | WOLFSSL_LEAVE("FindPsk", ret); |
6073 | | return found; |
6074 | | } |
6075 | | #endif /* !NO_PSK */ |
6076 | | |
6077 | | /* Handle any Pre-Shared Key (PSK) extension. |
6078 | | * Find a PSK that supports the cipher suite passed in. |
6079 | | * |
6080 | | * ssl SSL/TLS object. |
6081 | | * suite Cipher suite to find PSK for. |
6082 | | * usingPSK 1=Indicates handshake is using Pre-Shared Keys (2=Ephemeral) |
6083 | | * first Set to 1 if first in extension |
6084 | | * returns 0 on success and otherwise failure. |
6085 | | */ |
6086 | | static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz, |
6087 | | const byte* suite, int* usingPSK, int* first) |
6088 | | { |
6089 | | int ret = 0; |
6090 | | TLSX* ext; |
6091 | | PreSharedKey* current; |
6092 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
6093 | | byte binder[WC_MAX_DIGEST_SIZE]; |
6094 | | word32 binderLen; |
6095 | | |
6096 | | #ifdef NO_PSK |
6097 | | (void) suite; /* to avoid unused var warning when not used */ |
6098 | | #endif |
6099 | | |
6100 | | WOLFSSL_ENTER("DoPreSharedKeys"); |
6101 | | |
6102 | | (void)suite; |
6103 | | |
6104 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6105 | | if (ext == NULL) { |
6106 | | WOLFSSL_MSG("No pre shared extension keys found"); |
6107 | | return BAD_FUNC_ARG; |
6108 | | } |
6109 | | |
6110 | | /* Look through all client's pre-shared keys for a match. */ |
6111 | | for (current = (PreSharedKey*)ext->data; current != NULL; |
6112 | | current = current->next) { |
6113 | | #ifndef NO_PSK |
6114 | | if (current->identityLen > MAX_PSK_ID_LEN) { |
6115 | | return BUFFER_ERROR; |
6116 | | } |
6117 | | XMEMCPY(ssl->arrays->client_identity, current->identity, |
6118 | | current->identityLen); |
6119 | | ssl->arrays->client_identity[current->identityLen] = '\0'; |
6120 | | #endif |
6121 | | |
6122 | | #ifdef HAVE_SESSION_TICKET |
6123 | | /* Decode the identity. */ |
6124 | | switch (current->decryptRet) { |
6125 | | case PSK_DECRYPT_NONE: |
6126 | | ret = DoClientTicket_ex(ssl, current, 1); |
6127 | | /* psk->sess may be set. Need to clean up later. */ |
6128 | | break; |
6129 | | case PSK_DECRYPT_OK: |
6130 | | ret = WOLFSSL_TICKET_RET_OK; |
6131 | | break; |
6132 | | case PSK_DECRYPT_CREATE: |
6133 | | ret = WOLFSSL_TICKET_RET_CREATE; |
6134 | | break; |
6135 | | case PSK_DECRYPT_FAIL: |
6136 | | ret = WOLFSSL_TICKET_RET_REJECT; |
6137 | | break; |
6138 | | } |
6139 | | |
6140 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6141 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
6142 | | return ret; |
6143 | | #endif |
6144 | | |
6145 | | if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) { |
6146 | | current->sess_free_cb(ssl, current->sess, |
6147 | | ¤t->sess_free_cb_ctx); |
6148 | | current->sess = NULL; |
6149 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6150 | | sizeof(psk_sess_free_cb_ctx)); |
6151 | | } |
6152 | | if (ret == WOLFSSL_TICKET_RET_OK) { |
6153 | | ret = DoClientTicketCheck(ssl, current, ssl->timeout, suite); |
6154 | | if (ret == 0) |
6155 | | DoClientTicketFinalize(ssl, current->it, current->sess); |
6156 | | if (current->sess_free_cb != NULL) { |
6157 | | current->sess_free_cb(ssl, current->sess, |
6158 | | ¤t->sess_free_cb_ctx); |
6159 | | current->sess = NULL; |
6160 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6161 | | sizeof(psk_sess_free_cb_ctx)); |
6162 | | } |
6163 | | if (ret != 0) |
6164 | | continue; |
6165 | | |
6166 | | /* SERVER: using secret in session ticket for peer auth. */ |
6167 | | ssl->options.peerAuthGood = 1; |
6168 | | |
6169 | | #ifdef WOLFSSL_EARLY_DATA |
6170 | | ssl->options.maxEarlyDataSz = ssl->session->maxEarlyDataSz; |
6171 | | #endif |
6172 | | /* Use the same cipher suite as before and set up for use. */ |
6173 | | ssl->options.cipherSuite0 = ssl->session->cipherSuite0; |
6174 | | ssl->options.cipherSuite = ssl->session->cipherSuite; |
6175 | | ret = SetCipherSpecs(ssl); |
6176 | | if (ret != 0) |
6177 | | return ret; |
6178 | | |
6179 | | /* Resumption PSK is resumption master secret. */ |
6180 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
6181 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
6182 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
6183 | | return ret; |
6184 | | } |
6185 | | |
6186 | | /* Derive the early secret using the PSK. */ |
6187 | | ret = DeriveEarlySecret(ssl); |
6188 | | if (ret != 0) |
6189 | | return ret; |
6190 | | |
6191 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6192 | | ret = HashInput(ssl, input, (int)inputSz); |
6193 | | if (ret < 0) |
6194 | | return ret; |
6195 | | |
6196 | | /* Derive the binder key to use with HMAC. */ |
6197 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
6198 | | if (ret != 0) |
6199 | | return ret; |
6200 | | } |
6201 | | else |
6202 | | #endif /* HAVE_SESSION_TICKET */ |
6203 | | #ifndef NO_PSK |
6204 | | if (FindPsk(ssl, current, suite, &ret)) { |
6205 | | if (ret != 0) |
6206 | | return ret; |
6207 | | |
6208 | | ret = HashInput(ssl, input, (int)inputSz); |
6209 | | if (ret < 0) |
6210 | | return ret; |
6211 | | |
6212 | | /* Derive the binder key to use with HMAC. */ |
6213 | | ret = DeriveBinderKey(ssl, binderKey); |
6214 | | if (ret != 0) |
6215 | | return ret; |
6216 | | } |
6217 | | else |
6218 | | #endif |
6219 | | { |
6220 | | continue; |
6221 | | } |
6222 | | |
6223 | | ssl->options.sendVerify = 0; |
6224 | | |
6225 | | /* Derive the Finished message secret. */ |
6226 | | ret = DeriveFinishedSecret(ssl, binderKey, |
6227 | | ssl->keys.client_write_MAC_secret, |
6228 | | 0 /* neither end */); |
6229 | | if (ret != 0) |
6230 | | return ret; |
6231 | | |
6232 | | /* Derive the binder and compare with the one in the extension. */ |
6233 | | ret = BuildTls13HandshakeHmac(ssl, |
6234 | | ssl->keys.client_write_MAC_secret, binder, &binderLen); |
6235 | | if (ret != 0) |
6236 | | return ret; |
6237 | | if (binderLen != current->binderLen || |
6238 | | ConstantCompare(binder, current->binder, |
6239 | | binderLen) != 0) { |
6240 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6241 | | return BAD_BINDER; |
6242 | | } |
6243 | | |
6244 | | /* This PSK works, no need to try any more. */ |
6245 | | current->chosen = 1; |
6246 | | ext->resp = 1; |
6247 | | break; |
6248 | | } |
6249 | | |
6250 | | if (current == NULL) { |
6251 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6252 | | #ifndef NO_CERTS |
6253 | | if (ssl->buffers.certChainCnt != 0) |
6254 | | return 0; |
6255 | | #endif |
6256 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6257 | | return BAD_BINDER; |
6258 | | #else |
6259 | | return 0; |
6260 | | #endif |
6261 | | } |
6262 | | |
6263 | | *first = (current == ext->data); |
6264 | | *usingPSK = 1; |
6265 | | |
6266 | | WOLFSSL_LEAVE("DoPreSharedKeys", ret); |
6267 | | |
6268 | | return ret; |
6269 | | } |
6270 | | |
6271 | | /* Handle any Pre-Shared Key (PSK) extension. |
6272 | | * Must do this in ClientHello as it requires a hash of the truncated message. |
6273 | | * Don't know size of binders until Pre-Shared Key extension has been parsed. |
6274 | | * |
6275 | | * ssl SSL/TLS object. |
6276 | | * input ClientHello message. |
6277 | | * helloSz Size of the ClientHello message (including binders if present). |
6278 | | * clSuites Client's cipher suite list. |
6279 | | * usingPSK Indicates handshake is using Pre-Shared Keys. |
6280 | | */ |
6281 | | static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz, |
6282 | | Suites* clSuites, int* usingPSK) |
6283 | | { |
6284 | | int ret; |
6285 | | TLSX* ext; |
6286 | | word16 bindersLen; |
6287 | | int first = 0; |
6288 | | #ifndef WOLFSSL_PSK_ONE_ID |
6289 | | int i; |
6290 | | const Suites* suites; |
6291 | | #else |
6292 | | byte suite[2]; |
6293 | | #endif |
6294 | | |
6295 | | WOLFSSL_ENTER("CheckPreSharedKeys"); |
6296 | | |
6297 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6298 | | if (ext == NULL) { |
6299 | | #ifdef WOLFSSL_EARLY_DATA |
6300 | | ssl->earlyData = no_early_data; |
6301 | | #endif |
6302 | | if (usingPSK) |
6303 | | *usingPSK = 0; |
6304 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6305 | | ret = HashInput(ssl, input, (int)helloSz); |
6306 | | return ret; |
6307 | | } |
6308 | | |
6309 | | /* Extensions pushed on stack/list and PSK must be last. */ |
6310 | | if (ssl->extensions != ext) { |
6311 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6312 | | return PSK_KEY_ERROR; |
6313 | | } |
6314 | | |
6315 | | /* Assume we are going to resume with a pre-shared key. */ |
6316 | | ssl->options.resuming = 1; |
6317 | | |
6318 | | /* Find the pre-shared key extension and calculate hash of truncated |
6319 | | * ClientHello for binders. |
6320 | | */ |
6321 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
6322 | | client_hello, &bindersLen); |
6323 | | if (ret < 0) |
6324 | | return ret; |
6325 | | |
6326 | | /* Refine list for PSK processing. */ |
6327 | | sslRefineSuites(ssl, clSuites); |
6328 | | #ifndef WOLFSSL_PSK_ONE_ID |
6329 | | if (usingPSK == NULL) |
6330 | | return BAD_FUNC_ARG; |
6331 | | |
6332 | | /* set after refineSuites, to avoid taking a stale ptr to ctx->Suites */ |
6333 | | suites = WOLFSSL_SUITES(ssl); |
6334 | | /* Server list has only common suites from refining in server or client |
6335 | | * order. */ |
6336 | | for (i = 0; !(*usingPSK) && i < suites->suiteSz; i += 2) { |
6337 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, |
6338 | | suites->suites + i, usingPSK, &first); |
6339 | | if (ret != 0) { |
6340 | | #ifdef HAVE_SESSION_TICKET |
6341 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6342 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
6343 | | #endif |
6344 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6345 | | #endif |
6346 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6347 | | return ret; |
6348 | | } |
6349 | | } |
6350 | | #ifdef HAVE_SESSION_TICKET |
6351 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6352 | | #endif |
6353 | | #else |
6354 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, suite, usingPSK, |
6355 | | &first); |
6356 | | if (ret != 0) { |
6357 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6358 | | return ret; |
6359 | | } |
6360 | | #endif |
6361 | | |
6362 | | if (*usingPSK) { |
6363 | | /* While verifying the selected PSK, we updated the |
6364 | | * handshake hash up to the binder bytes in the PSK extensions. |
6365 | | * Continuing, we need the rest of the ClientHello hashed as well. |
6366 | | */ |
6367 | | ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen); |
6368 | | } |
6369 | | else { |
6370 | | /* No suitable PSK found, Hash the complete ClientHello, |
6371 | | * as caller expect it after we return */ |
6372 | | ret = HashInput(ssl, input, (int)helloSz); |
6373 | | } |
6374 | | if (ret != 0) |
6375 | | return ret; |
6376 | | |
6377 | | if (*usingPSK != 0) { |
6378 | | word32 modes; |
6379 | | #ifdef WOLFSSL_EARLY_DATA |
6380 | | TLSX* extEarlyData; |
6381 | | |
6382 | | extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6383 | | if (extEarlyData != NULL) { |
6384 | | /* Check if accepting early data and first PSK. */ |
6385 | | if (ssl->earlyData != no_early_data && first) { |
6386 | | extEarlyData->resp = 1; |
6387 | | |
6388 | | /* Derive early data decryption key. */ |
6389 | | ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, |
6390 | | 1); |
6391 | | if (ret != 0) |
6392 | | return ret; |
6393 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
6394 | | return ret; |
6395 | | |
6396 | | ssl->keys.encryptionOn = 1; |
6397 | | ssl->earlyData = process_early_data; |
6398 | | } |
6399 | | else |
6400 | | extEarlyData->resp = 0; |
6401 | | } |
6402 | | #endif |
6403 | | |
6404 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
6405 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
6406 | | if (ext == NULL) { |
6407 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6408 | | return MISSING_HANDSHAKE_DATA; |
6409 | | } |
6410 | | modes = ext->val; |
6411 | | |
6412 | | #ifdef HAVE_SUPPORTED_CURVES |
6413 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
6414 | | /* Use (EC)DHE for forward-security if possible. */ |
6415 | | if ((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe && |
6416 | | ext != NULL) { |
6417 | | /* Only use named group used in last session. */ |
6418 | | ssl->namedGroup = ssl->session->namedGroup; |
6419 | | |
6420 | | *usingPSK = 2; /* generate new ephemeral key */ |
6421 | | } |
6422 | | else if (ssl->options.onlyPskDheKe) { |
6423 | | return PSK_KEY_ERROR; |
6424 | | } |
6425 | | else |
6426 | | #endif |
6427 | | { |
6428 | | if ((modes & (1 << PSK_KE)) == 0) { |
6429 | | WOLFSSL_MSG("psk_ke mode does not allow key share"); |
6430 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6431 | | return PSK_KEY_ERROR; |
6432 | | } |
6433 | | ssl->options.noPskDheKe = 1; |
6434 | | ssl->arrays->preMasterSz = 0; |
6435 | | |
6436 | | *usingPSK = 1; |
6437 | | } |
6438 | | } |
6439 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6440 | | else { |
6441 | | #ifndef NO_CERTS |
6442 | | if (ssl->buffers.certChainCnt != 0) |
6443 | | return 0; |
6444 | | #endif |
6445 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6446 | | return BAD_BINDER; |
6447 | | } |
6448 | | #endif |
6449 | | |
6450 | | WOLFSSL_LEAVE("CheckPreSharedKeys", ret); |
6451 | | |
6452 | | return 0; |
6453 | | } |
6454 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
6455 | | |
6456 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
6457 | | /* Check that the Cookie data's integrity. |
6458 | | * |
6459 | | * ssl SSL/TLS object. |
6460 | | * cookie The cookie data - hash and MAC. |
6461 | | * cookieSz The length of the cookie data in bytes. |
6462 | | * returns Length of the hash on success, otherwise failure. |
6463 | | */ |
6464 | | int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz) |
6465 | | { |
6466 | | int ret; |
6467 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
6468 | | Hmac cookieHmac; |
6469 | | byte cookieType = 0; |
6470 | | byte macSz = 0; |
6471 | | |
6472 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
6473 | | ssl->buffers.tls13CookieSecret.length == 0) { |
6474 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
6475 | | return COOKIE_ERROR; |
6476 | | } |
6477 | | |
6478 | | #ifndef NO_SHA256 |
6479 | | cookieType = WC_SHA256; |
6480 | | macSz = WC_SHA256_DIGEST_SIZE; |
6481 | | #elif defined(WOLFSSL_SHA384) |
6482 | | cookieType = WC_SHA384; |
6483 | | macSz = WC_SHA384_DIGEST_SIZE; |
6484 | | #elif defined(WOLFSSL_TLS13_SHA512) |
6485 | | cookieType = WC_SHA512; |
6486 | | macSz = WC_SHA512_DIGEST_SIZE; |
6487 | | #elif defined(WOLFSSL_SM3) |
6488 | | cookieType = WC_SM3; |
6489 | | macSz = WC_SM3_DIGEST_SIZE; |
6490 | | #else |
6491 | | #error "No digest to available to use with HMAC for cookies." |
6492 | | #endif /* NO_SHA */ |
6493 | | |
6494 | | if (cookieSz < ssl->specs.hash_size + macSz) |
6495 | | return HRR_COOKIE_ERROR; |
6496 | | cookieSz -= macSz; |
6497 | | |
6498 | | ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId); |
6499 | | if (ret == 0) { |
6500 | | ret = wc_HmacSetKey(&cookieHmac, cookieType, |
6501 | | ssl->buffers.tls13CookieSecret.buffer, |
6502 | | ssl->buffers.tls13CookieSecret.length); |
6503 | | } |
6504 | | if (ret == 0) |
6505 | | ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz); |
6506 | | #ifdef WOLFSSL_DTLS13 |
6507 | | /* Tie cookie to peer address */ |
6508 | | if (ret == 0) { |
6509 | | /* peerLock not necessary. Still in handshake phase. */ |
6510 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
6511 | | ret = wc_HmacUpdate(&cookieHmac, |
6512 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
6513 | | ssl->buffers.dtlsCtx.peer.sz); |
6514 | | } |
6515 | | } |
6516 | | #endif |
6517 | | if (ret == 0) |
6518 | | ret = wc_HmacFinal(&cookieHmac, mac); |
6519 | | |
6520 | | wc_HmacFree(&cookieHmac); |
6521 | | if (ret != 0) |
6522 | | return ret; |
6523 | | |
6524 | | if (ConstantCompare(cookie + cookieSz, mac, macSz) != 0) { |
6525 | | WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR); |
6526 | | return HRR_COOKIE_ERROR; |
6527 | | } |
6528 | | return cookieSz; |
6529 | | } |
6530 | | |
6531 | | /* Length of the KeyShare Extension */ |
6532 | | #define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6533 | | /* Length of the Supported Versions Extension */ |
6534 | | #define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6535 | | /* Length of the Cookie Extension excluding cookie data */ |
6536 | | #define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6537 | | /* PV | Random | Session Id | CipherSuite | Compression | Ext Len */ |
6538 | | #define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \ |
6539 | | SUITE_LEN + COMP_LEN + OPAQUE16_LEN) |
6540 | | /* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */ |
6541 | | #define MAX_HRR_SZ (HRR_MAX_HS_HEADER_SZ + \ |
6542 | | HRR_BODY_SZ + \ |
6543 | | HRR_KEY_SHARE_SZ + \ |
6544 | | HRR_VERSIONS_SZ + \ |
6545 | | HRR_COOKIE_HDR_SZ) |
6546 | | |
6547 | | |
6548 | | /* Restart the handshake hash from the cookie value. |
6549 | | * |
6550 | | * ssl SSL/TLS object. |
6551 | | * cookie Cookie data from client. |
6552 | | * returns 0 on success, otherwise failure. |
6553 | | */ |
6554 | | static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie) |
6555 | | { |
6556 | | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
6557 | | byte hrr[MAX_HRR_SZ] = {0}; |
6558 | | int hrrIdx; |
6559 | | word32 idx; |
6560 | | byte hashSz; |
6561 | | byte* cookieData; |
6562 | | word16 cookieDataSz; |
6563 | | word16 length; |
6564 | | int keyShareExt = 0; |
6565 | | int ret; |
6566 | | |
6567 | | ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len); |
6568 | | if (ret < 0) |
6569 | | return ret; |
6570 | | cookieDataSz = (word16)ret; |
6571 | | hashSz = cookie->data[0]; |
6572 | | cookieData = cookie->data; |
6573 | | idx = OPAQUE8_LEN; |
6574 | | |
6575 | | /* Restart handshake hash with synthetic message hash. */ |
6576 | | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
6577 | | |
6578 | | if ((ret = InitHandshakeHashes(ssl)) != 0) |
6579 | | return ret; |
6580 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
6581 | | return ret; |
6582 | | #ifdef WOLFSSL_DEBUG_TLS |
6583 | | WOLFSSL_MSG("Restart Hash from Cookie"); |
6584 | | WOLFSSL_BUFFER(cookieData + idx, hashSz); |
6585 | | #endif |
6586 | | if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0) |
6587 | | return ret; |
6588 | | |
6589 | | /* Reconstruct the HelloRetryMessage for handshake hash. */ |
6590 | | length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz + |
6591 | | HRR_COOKIE_HDR_SZ + cookie->len; |
6592 | | length += HRR_VERSIONS_SZ; |
6593 | | /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */ |
6594 | | if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) { |
6595 | | keyShareExt = 1; |
6596 | | length += HRR_KEY_SHARE_SZ; |
6597 | | } |
6598 | | |
6599 | | AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl); |
6600 | | |
6601 | | idx += hashSz; |
6602 | | hrrIdx = HANDSHAKE_HEADER_SZ; |
6603 | | |
6604 | | #ifdef WOLFSSL_DTLS13 |
6605 | | if (ssl->options.dtls) |
6606 | | hrrIdx += DTLS_HANDSHAKE_EXTRA; |
6607 | | #endif /* WOLFSSL_DTLS13 */ |
6608 | | |
6609 | | /* The negotiated protocol version. */ |
6610 | | hrr[hrrIdx++] = ssl->version.major; |
6611 | | hrr[hrrIdx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
6612 | | |
6613 | | /* HelloRetryRequest message has fixed value for random. */ |
6614 | | XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN); |
6615 | | hrrIdx += RAN_LEN; |
6616 | | |
6617 | | hrr[hrrIdx++] = ssl->session->sessionIDSz; |
6618 | | if (ssl->session->sessionIDSz > 0) { |
6619 | | XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz); |
6620 | | hrrIdx += ssl->session->sessionIDSz; |
6621 | | } |
6622 | | |
6623 | | /* Restore the cipher suite from the cookie. */ |
6624 | | ssl->options.hrrCipherSuite0 = cookieData[idx]; |
6625 | | hrr[hrrIdx++] = cookieData[idx++]; |
6626 | | ssl->options.hrrCipherSuite = cookieData[idx]; |
6627 | | hrr[hrrIdx++] = cookieData[idx++]; |
6628 | | |
6629 | | /* Compression not supported in TLS v1.3. */ |
6630 | | hrr[hrrIdx++] = 0; |
6631 | | |
6632 | | /* Extensions' length */ |
6633 | | length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz; |
6634 | | c16toa(length, hrr + hrrIdx); |
6635 | | hrrIdx += 2; |
6636 | | |
6637 | | /* Optional KeyShare Extension */ |
6638 | | if (keyShareExt) { |
6639 | | c16toa(TLSX_KEY_SHARE, hrr + hrrIdx); |
6640 | | hrrIdx += 2; |
6641 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6642 | | hrrIdx += 2; |
6643 | | /* Restore the HRR key share group from the cookie. */ |
6644 | | ato16(cookieData + idx, &ssl->hrr_keyshare_group); |
6645 | | hrr[hrrIdx++] = cookieData[idx++]; |
6646 | | hrr[hrrIdx++] = cookieData[idx++]; |
6647 | | } |
6648 | | c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx); |
6649 | | hrrIdx += 2; |
6650 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
6651 | | hrrIdx += 2; |
6652 | | #ifdef WOLFSSL_TLS13_DRAFT |
6653 | | hrr[hrrIdx++] = TLS_DRAFT_MAJOR; |
6654 | | hrr[hrrIdx++] = TLS_DRAFT_MINOR; |
6655 | | #else |
6656 | | hrr[hrrIdx++] = ssl->version.major; |
6657 | | hrr[hrrIdx++] = ssl->version.minor; |
6658 | | #endif |
6659 | | |
6660 | | /* Mandatory Cookie Extension */ |
6661 | | c16toa(TLSX_COOKIE, hrr + hrrIdx); |
6662 | | hrrIdx += 2; |
6663 | | c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx); |
6664 | | hrrIdx += 2; |
6665 | | c16toa(cookie->len, hrr + hrrIdx); |
6666 | | hrrIdx += 2; |
6667 | | |
6668 | | #ifdef WOLFSSL_DEBUG_TLS |
6669 | | WOLFSSL_MSG("Reconstructed HelloRetryRequest"); |
6670 | | WOLFSSL_BUFFER(hrr, hrrIdx); |
6671 | | WOLFSSL_MSG("Cookie"); |
6672 | | WOLFSSL_BUFFER(cookieData, cookie->len); |
6673 | | #endif |
6674 | | |
6675 | | #ifdef WOLFSSL_DTLS13 |
6676 | | if (ssl->options.dtls) { |
6677 | | ret = Dtls13HashHandshake(ssl, hrr, (word16)hrrIdx); |
6678 | | } |
6679 | | else |
6680 | | #endif /* WOLFSSL_DTLS13 */ |
6681 | | { |
6682 | | ret = HashRaw(ssl, hrr, hrrIdx); |
6683 | | } |
6684 | | |
6685 | | if (ret != 0) |
6686 | | return ret; |
6687 | | |
6688 | | return HashRaw(ssl, cookieData, cookie->len); |
6689 | | } |
6690 | | #endif |
6691 | | |
6692 | | /* Do SupportedVersion extension for TLS v1.3+ otherwise it is not. |
6693 | | * |
6694 | | * ssl The SSL/TLS object. |
6695 | | * input The message buffer. |
6696 | | * i The index into the message buffer of ClientHello. |
6697 | | * helloSz The length of the current handshake message. |
6698 | | * returns 0 on success and otherwise failure. |
6699 | | */ |
6700 | | static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i, |
6701 | | word32 helloSz, int* wantDowngrade) |
6702 | | { |
6703 | | int ret; |
6704 | | byte b; |
6705 | | word16 suiteSz; |
6706 | | word16 totalExtSz; |
6707 | | int foundVersion = 0; |
6708 | | |
6709 | | /* Client random */ |
6710 | | i += RAN_LEN; |
6711 | | /* Session id - not used in TLS v1.3 */ |
6712 | | b = input[i++]; |
6713 | | if (i + b > helloSz) { |
6714 | | return BUFFER_ERROR; |
6715 | | } |
6716 | | i += b; |
6717 | | #ifdef WOLFSSL_DTLS13 |
6718 | | if (ssl->options.dtls) { |
6719 | | /* legacy_cookie - not used in DTLS v1.3 */ |
6720 | | b = input[i++]; |
6721 | | if (i + b > helloSz) { |
6722 | | return BUFFER_ERROR; |
6723 | | } |
6724 | | i += b; |
6725 | | } |
6726 | | #endif /* WOLFSSL_DTLS13 */ |
6727 | | /* Cipher suites */ |
6728 | | if (i + OPAQUE16_LEN > helloSz) |
6729 | | return BUFFER_ERROR; |
6730 | | ato16(input + i, &suiteSz); |
6731 | | i += OPAQUE16_LEN; |
6732 | | if (i + suiteSz + 1 > helloSz) |
6733 | | return BUFFER_ERROR; |
6734 | | i += suiteSz; |
6735 | | /* Compression */ |
6736 | | b = input[i++]; |
6737 | | if (i + b > helloSz) |
6738 | | return BUFFER_ERROR; |
6739 | | i += b; |
6740 | | |
6741 | | /* TLS 1.3 must have extensions */ |
6742 | | if (i < helloSz) { |
6743 | | if (i + OPAQUE16_LEN > helloSz) |
6744 | | return BUFFER_ERROR; |
6745 | | ato16(&input[i], &totalExtSz); |
6746 | | i += OPAQUE16_LEN; |
6747 | | if (totalExtSz != helloSz - i) |
6748 | | return BUFFER_ERROR; |
6749 | | |
6750 | | /* Need to negotiate version first. */ |
6751 | | if ((ret = TLSX_ParseVersion(ssl, input + i, totalExtSz, client_hello, |
6752 | | &foundVersion))) { |
6753 | | return ret; |
6754 | | } |
6755 | | } |
6756 | | *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version); |
6757 | | |
6758 | | return 0; |
6759 | | } |
6760 | | |
6761 | | #ifdef HAVE_ECH |
6762 | | /* Calculate and write the 8 ECH confirmation bytes. |
6763 | | * Output into confirmation field on HRR and into ServerRandom on ServerHello. |
6764 | | * |
6765 | | * ssl SSL/TLS object. |
6766 | | * label Ascii string describing ECH acceptance or rejection. |
6767 | | * labelSz Length of label excluding NULL character. |
6768 | | * output The buffer to calculate/write confirmation from/to. |
6769 | | * acceptOffset Where the 8 ECH confirmation bytes should be placed. |
6770 | | * helloSz Size of hello message. |
6771 | | * msgType Type of message being written. |
6772 | | * returns 0 on success and otherwise failure. |
6773 | | */ |
6774 | | static int EchWriteAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
6775 | | byte* output, int acceptOffset, int helloSz, byte msgType) |
6776 | | { |
6777 | | int ret = 0; |
6778 | | int headerSz; |
6779 | | HS_Hashes* tmpHashes; |
6780 | | |
6781 | | #ifdef WOLFSSL_DTLS13 |
6782 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
6783 | | HANDSHAKE_HEADER_SZ; |
6784 | | #else |
6785 | | headerSz = HANDSHAKE_HEADER_SZ; |
6786 | | #endif |
6787 | | |
6788 | | ret = EchCalcAcceptance(ssl, label, labelSz, output, acceptOffset, |
6789 | | helloSz - headerSz, msgType == hello_retry_request, |
6790 | | output + acceptOffset); |
6791 | | |
6792 | | if (ret == 0) { |
6793 | | tmpHashes = ssl->hsHashes; |
6794 | | ssl->hsHashes = ssl->hsHashesEch; |
6795 | | |
6796 | | /* after HRR, hsHashesEch must contain: |
6797 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
6798 | | if (msgType == hello_retry_request) { |
6799 | | ret = HashRaw(ssl, output, helloSz); |
6800 | | } |
6801 | | /* normal TLS code will calculate transcript of ServerHello */ |
6802 | | else { |
6803 | | ssl->options.echAccepted = 1; |
6804 | | |
6805 | | ssl->hsHashes = tmpHashes; |
6806 | | FreeHandshakeHashes(ssl); |
6807 | | tmpHashes = ssl->hsHashesEch; |
6808 | | ssl->hsHashesEch = NULL; |
6809 | | } |
6810 | | |
6811 | | ssl->hsHashes = tmpHashes; |
6812 | | } |
6813 | | |
6814 | | return ret; |
6815 | | } |
6816 | | #endif |
6817 | | |
6818 | | /* Handle a ClientHello handshake message. |
6819 | | * If the protocol version in the message is not TLS v1.3 or higher, use |
6820 | | * DoClientHello() |
6821 | | * Only a server will receive this message. |
6822 | | * |
6823 | | * ssl The SSL/TLS object. |
6824 | | * input The message buffer. |
6825 | | * inOutIdx On entry, the index into the message buffer of ClientHello. |
6826 | | * On exit, the index of byte after the ClientHello message and |
6827 | | * padding. |
6828 | | * helloSz The length of the current handshake message. |
6829 | | * returns 0 on success and otherwise failure. |
6830 | | */ |
6831 | | |
6832 | | typedef struct Dch13Args { |
6833 | | ProtocolVersion pv; |
6834 | | word32 idx; |
6835 | | word32 begin; |
6836 | | int usingPSK; |
6837 | | } Dch13Args; |
6838 | | |
6839 | | static void FreeDch13Args(WOLFSSL* ssl, void* pArgs) |
6840 | | { |
6841 | | /* openssl compat builds hang on to the client suites until WOLFSSL object |
6842 | | * is destroyed */ |
6843 | | #ifndef OPENSSL_EXTRA |
6844 | | if (ssl->clSuites) { |
6845 | | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
6846 | | ssl->clSuites = NULL; |
6847 | | } |
6848 | | #endif |
6849 | | (void)ssl; |
6850 | | (void)pArgs; |
6851 | | |
6852 | | } |
6853 | | |
6854 | | int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
6855 | | word32 helloSz) |
6856 | 0 | { |
6857 | 0 | int ret; |
6858 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6859 | | Dch13Args* args = NULL; |
6860 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
6861 | | #else |
6862 | 0 | Dch13Args args[1]; |
6863 | 0 | #endif |
6864 | | #if defined(HAVE_ECH) |
6865 | | TLSX* echX = NULL; |
6866 | | #endif |
6867 | |
|
6868 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO); |
6869 | 0 | WOLFSSL_ENTER("DoTls13ClientHello"); |
6870 | |
|
6871 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6872 | | if (ssl->async == NULL) { |
6873 | | ssl->async = (struct WOLFSSL_ASYNC*) |
6874 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
6875 | | DYNAMIC_TYPE_ASYNC); |
6876 | | if (ssl->async == NULL) |
6877 | | ERROR_OUT(MEMORY_E, exit_dch); |
6878 | | } |
6879 | | args = (Dch13Args*)ssl->async->args; |
6880 | | |
6881 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
6882 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
6883 | | /* Check for error */ |
6884 | | if (ret < 0) { |
6885 | | goto exit_dch; |
6886 | | } |
6887 | | } |
6888 | | else |
6889 | | #endif |
6890 | 0 | { |
6891 | | /* Reset state */ |
6892 | 0 | ret = WC_NO_ERR_TRACE(VERSION_ERROR); |
6893 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
6894 | 0 | XMEMSET(args, 0, sizeof(Dch13Args)); |
6895 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6896 | | ssl->async->freeArgs = FreeDch13Args; |
6897 | | #endif |
6898 | 0 | } |
6899 | |
|
6900 | 0 | switch (ssl->options.asyncState) { |
6901 | 0 | case TLS_ASYNC_BEGIN: |
6902 | 0 | { |
6903 | 0 | byte b; |
6904 | 0 | byte sessIdSz; |
6905 | 0 | int wantDowngrade = 0; |
6906 | 0 | word16 totalExtSz = 0; |
6907 | |
|
6908 | | #ifdef WOLFSSL_CALLBACKS |
6909 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
6910 | | if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo); |
6911 | | #endif |
6912 | | |
6913 | | /* do not change state in the SSL object before the next region of code |
6914 | | * to be able to statelessly compute a DTLS cookie */ |
6915 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
6916 | | /* Update the ssl->options.dtlsStateful setting `if` statement in |
6917 | | * wolfSSL_accept_TLSv13 when changing this one. */ |
6918 | | if (IsDtlsNotSctpMode(ssl) && ssl->options.sendCookie && |
6919 | | !ssl->options.dtlsStateful) { |
6920 | | DtlsSetSeqNumForReply(ssl); |
6921 | | ret = DoClientHelloStateless(ssl, input + *inOutIdx, helloSz, 0, NULL); |
6922 | | if (ret != 0 || !ssl->options.dtlsStateful) { |
6923 | | *inOutIdx += helloSz; |
6924 | | goto exit_dch; |
6925 | | } |
6926 | | if (ssl->chGoodCb != NULL) { |
6927 | | int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
6928 | | if (cbret < 0) { |
6929 | | ssl->error = cbret; |
6930 | | WOLFSSL_MSG("ClientHello Good Cb don't continue error"); |
6931 | | return WOLFSSL_FATAL_ERROR; |
6932 | | } |
6933 | | } |
6934 | | } |
6935 | | ssl->options.dtlsStateful = 1; |
6936 | | #endif /* WOLFSSL_DTLS */ |
6937 | |
|
6938 | 0 | args->idx = *inOutIdx; |
6939 | 0 | args->begin = args->idx; |
6940 | | |
6941 | | /* protocol version, random and session id length check */ |
6942 | 0 | if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz) { |
6943 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
6944 | 0 | } |
6945 | | |
6946 | | /* Protocol version */ |
6947 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
6948 | 0 | ssl->chVersion = args->pv; /* store */ |
6949 | 0 | args->idx += OPAQUE16_LEN; |
6950 | | |
6951 | | |
6952 | | /* this check pass for DTLS Major (0xff) */ |
6953 | 0 | if (args->pv.major < SSLv3_MAJOR) { |
6954 | 0 | WOLFSSL_MSG("Legacy version field contains unsupported value"); |
6955 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6956 | 0 | } |
6957 | | |
6958 | | #ifdef WOLFSSL_DTLS13 |
6959 | | if (ssl->options.dtls && |
6960 | | args->pv.major == DTLS_MAJOR && args->pv.minor > DTLSv1_2_MINOR) { |
6961 | | wantDowngrade = 1; |
6962 | | ssl->version.minor = args->pv.minor; |
6963 | | } |
6964 | | #endif /* WOLFSSL_DTLS13 */ |
6965 | | |
6966 | 0 | if (!ssl->options.dtls) { |
6967 | 0 | #ifndef WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION |
6968 | | /* Check for TLS 1.3 version (0x0304) in legacy version field. RFC 8446 |
6969 | | * Section 4.2.1 allows this action: |
6970 | | * |
6971 | | * "Servers MAY abort the handshake upon receiving a ClientHello with |
6972 | | * legacy_version 0x0304 or later." |
6973 | | * |
6974 | | * Note that if WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION is defined then the |
6975 | | * semantics of RFC 5246 Appendix E will be followed. A ServerHello with |
6976 | | * version 1.2 will be sent. The same is true if TLS 1.3 is not enabled. |
6977 | | */ |
6978 | 0 | if (args->pv.major == SSLv3_MAJOR && args->pv.minor >= TLSv1_3_MINOR) { |
6979 | 0 | WOLFSSL_MSG("Legacy version field is TLS 1.3 or later. Aborting."); |
6980 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
6981 | 0 | } |
6982 | 0 | #endif /* WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION */ |
6983 | | |
6984 | | /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */ |
6985 | 0 | if (args->pv.major > SSLv3_MAJOR || (args->pv.major == SSLv3_MAJOR && |
6986 | 0 | args->pv.minor >= TLSv1_3_MINOR)) { |
6987 | 0 | args->pv.major = SSLv3_MAJOR; |
6988 | 0 | args->pv.minor = TLSv1_2_MINOR; |
6989 | 0 | wantDowngrade = 1; |
6990 | 0 | ssl->version.minor = args->pv.minor; |
6991 | 0 | } |
6992 | | /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */ |
6993 | 0 | else if (args->pv.major == SSLv3_MAJOR && |
6994 | 0 | args->pv.minor < TLSv1_2_MINOR) { |
6995 | 0 | wantDowngrade = 1; |
6996 | 0 | ssl->version.minor = args->pv.minor; |
6997 | 0 | } |
6998 | 0 | } |
6999 | | |
7000 | 0 | if (!wantDowngrade) { |
7001 | 0 | ret = DoTls13SupportedVersions(ssl, input + args->begin, |
7002 | 0 | args->idx - args->begin, helloSz, &wantDowngrade); |
7003 | 0 | if (ret < 0) |
7004 | 0 | goto exit_dch; |
7005 | 0 | } |
7006 | | |
7007 | 0 | if (wantDowngrade) { |
7008 | 0 | #ifndef WOLFSSL_NO_TLS12 |
7009 | 0 | byte realMinor; |
7010 | 0 | if (!ssl->options.downgrade) { |
7011 | 0 | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7012 | 0 | "TLS v1.3"); |
7013 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7014 | 0 | } |
7015 | | |
7016 | 0 | if ((!ssl->options.dtls |
7017 | 0 | && args->pv.minor < ssl->options.minDowngrade) || |
7018 | 0 | (ssl->options.dtls && args->pv.minor > ssl->options.minDowngrade)) { |
7019 | 0 | WOLFSSL_MSG("\tversion below minimum allowed, fatal error"); |
7020 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7021 | 0 | } |
7022 | | |
7023 | 0 | realMinor = ssl->version.minor; |
7024 | 0 | ssl->version.minor = args->pv.minor; |
7025 | 0 | ret = HashInput(ssl, input + args->begin, (int)helloSz); |
7026 | 0 | ssl->version.minor = realMinor; |
7027 | 0 | if (ret == 0) { |
7028 | 0 | ret = DoClientHello(ssl, input, inOutIdx, helloSz); |
7029 | 0 | } |
7030 | 0 | goto exit_dch; |
7031 | | #else |
7032 | | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7033 | | "TLS v1.3"); |
7034 | | ERROR_OUT(VERSION_ERROR, exit_dch); |
7035 | | #endif |
7036 | 0 | } |
7037 | | |
7038 | | /* From here on we are a TLS 1.3 ClientHello. */ |
7039 | | |
7040 | | /* Client random */ |
7041 | 0 | XMEMCPY(ssl->arrays->clientRandom, input + args->idx, RAN_LEN); |
7042 | 0 | args->idx += RAN_LEN; |
7043 | |
|
7044 | | #ifdef WOLFSSL_DEBUG_TLS |
7045 | | WOLFSSL_MSG("client random"); |
7046 | | WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN); |
7047 | | #endif |
7048 | |
|
7049 | 0 | sessIdSz = input[args->idx++]; |
7050 | 0 | if (sessIdSz > ID_LEN) |
7051 | 0 | { |
7052 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7053 | 0 | } |
7054 | | |
7055 | 0 | if (sessIdSz + args->idx > helloSz) |
7056 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7057 | |
|
7058 | 0 | ssl->session->sessionIDSz = sessIdSz; |
7059 | 0 | if (sessIdSz > 0) |
7060 | 0 | XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz); |
7061 | 0 | args->idx += sessIdSz; |
7062 | |
|
7063 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
7064 | | /* RFC 8446 Appendix D.4: server MUST only send CCS if the client's |
7065 | | * ClientHello contains a non-empty legacy_session_id. */ |
7066 | | if (sessIdSz == 0) { |
7067 | | ssl->options.tls13MiddleBoxCompat = 0; |
7068 | | } |
7069 | | #endif |
7070 | |
|
7071 | | #ifdef WOLFSSL_DTLS13 |
7072 | | /* legacy_cookie */ |
7073 | | if (ssl->options.dtls) { |
7074 | | /* https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 */ |
7075 | | byte cookieLen = input[args->idx++]; |
7076 | | if (cookieLen != 0) { |
7077 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7078 | | } |
7079 | | } |
7080 | | #endif /* WOLFSSL_DTLS13 */ |
7081 | |
|
7082 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
7083 | 0 | ssl->clSuites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap, |
7084 | 0 | DYNAMIC_TYPE_SUITES); |
7085 | 0 | if (ssl->clSuites == NULL) { |
7086 | 0 | ERROR_OUT(MEMORY_E, exit_dch); |
7087 | 0 | } |
7088 | | |
7089 | | /* Cipher suites */ |
7090 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7091 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7092 | 0 | ato16(&input[args->idx], &ssl->clSuites->suiteSz); |
7093 | 0 | args->idx += OPAQUE16_LEN; |
7094 | 0 | if ((ssl->clSuites->suiteSz % 2) != 0) { |
7095 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7096 | 0 | } |
7097 | | /* suites and compression length check */ |
7098 | 0 | if ((args->idx - args->begin) + ssl->clSuites->suiteSz + OPAQUE8_LEN > |
7099 | 0 | helloSz) { |
7100 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7101 | 0 | } |
7102 | 0 | if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) |
7103 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7104 | 0 | XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); |
7105 | 0 | args->idx += ssl->clSuites->suiteSz; |
7106 | 0 | ssl->clSuites->hashSigAlgoSz = 0; |
7107 | | |
7108 | | /* Compression */ |
7109 | 0 | b = input[args->idx++]; |
7110 | 0 | if ((args->idx - args->begin) + b > helloSz) |
7111 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7112 | 0 | if (b != COMP_LEN) { |
7113 | 0 | WOLFSSL_MSG("Must be one compression type in list"); |
7114 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7115 | 0 | } |
7116 | 0 | b = input[args->idx++]; |
7117 | 0 | if (b != NO_COMPRESSION) { |
7118 | 0 | WOLFSSL_MSG("Must be no compression type in list"); |
7119 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7120 | 0 | } |
7121 | | |
7122 | | /* Extensions */ |
7123 | 0 | if ((args->idx - args->begin) == helloSz) |
7124 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7125 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7126 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7127 | |
|
7128 | 0 | ato16(&input[args->idx], &totalExtSz); |
7129 | 0 | args->idx += OPAQUE16_LEN; |
7130 | 0 | if ((args->idx - args->begin) + totalExtSz > helloSz) |
7131 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7132 | | |
7133 | | /* Auto populate extensions supported unless user defined. */ |
7134 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0) |
7135 | 0 | goto exit_dch; |
7136 | | |
7137 | | #if defined(HAVE_ECH) |
7138 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7139 | | /* save the start of the buffer so we can use it when parsing ech */ |
7140 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7141 | | |
7142 | | if (echX == NULL) |
7143 | | ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch); |
7144 | | |
7145 | | ((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ; |
7146 | | ((WOLFSSL_ECH*)echX->data)->aadLen = helloSz; |
7147 | | } |
7148 | | #endif |
7149 | | |
7150 | | /* Parse extensions */ |
7151 | 0 | if ((ret = TLSX_Parse(ssl, input + args->idx, totalExtSz, client_hello, |
7152 | 0 | ssl->clSuites))) { |
7153 | 0 | goto exit_dch; |
7154 | 0 | } |
7155 | | |
7156 | | #if defined(HAVE_ECH) |
7157 | | if (echX != NULL && ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
7158 | | if (((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7159 | | /* Client sent real ECH and inner hello was decrypted, jump to |
7160 | | * exit so the caller can re-invoke with the inner hello */ |
7161 | | goto exit_dch; |
7162 | | } |
7163 | | else { |
7164 | | /* Server has ECH but client did not send ECH. Clear the |
7165 | | * response flag so the empty ECH extension is not written |
7166 | | * in EncryptedExtensions. */ |
7167 | | echX->resp = 0; |
7168 | | } |
7169 | | } |
7170 | | #endif |
7171 | | |
7172 | 0 | #ifdef HAVE_SNI |
7173 | 0 | if ((ret = SNI_Callback(ssl)) != 0) |
7174 | 0 | goto exit_dch; |
7175 | 0 | ssl->options.side = WOLFSSL_SERVER_END; |
7176 | 0 | #endif |
7177 | |
|
7178 | 0 | args->idx += totalExtSz; |
7179 | 0 | ssl->options.haveSessionId = 1; |
7180 | 0 | ssl->options.sendVerify = SEND_CERT; |
7181 | |
|
7182 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7183 | | ssl->options.cookieGood = 0; |
7184 | | if (ssl->options.sendCookie && |
7185 | | (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE |
7186 | | #ifdef WOLFSSL_DTLS13 |
7187 | | /* Always check for a valid cookie since we may have already |
7188 | | * sent a HRR but we reset the state. */ |
7189 | | || ssl->options.dtls |
7190 | | #endif |
7191 | | )) { |
7192 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_COOKIE); |
7193 | | |
7194 | | if (ext != NULL) { |
7195 | | /* Ensure the cookie came from client and isn't the one in the |
7196 | | * response - HelloRetryRequest. |
7197 | | */ |
7198 | | if (ext->resp == 0) { |
7199 | | ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data); |
7200 | | if (ret != 0) |
7201 | | goto exit_dch; |
7202 | | /* Don't change state here as we may want to enter |
7203 | | * DoTls13ClientHello again. */ |
7204 | | ssl->options.cookieGood = 1; |
7205 | | } |
7206 | | else { |
7207 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7208 | | } |
7209 | | } |
7210 | | else { |
7211 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME) |
7212 | | /* Don't error out as we may be resuming. We confirm this later. */ |
7213 | | if (!ssl->options.dtls) |
7214 | | #endif |
7215 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7216 | | } |
7217 | | } |
7218 | | #endif |
7219 | |
|
7220 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7221 | 0 | if (ssl->hrr_keyshare_group != 0) { |
7222 | | /* |
7223 | | * https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8 |
7224 | | * when sending the new ClientHello, the client MUST |
7225 | | * replace the original "key_share" extension with one containing only |
7226 | | * a new KeyShareEntry for the group indicated in the selected_group |
7227 | | * field of the triggering HelloRetryRequest. |
7228 | | */ |
7229 | 0 | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7230 | 0 | if (extension != NULL) { |
7231 | 0 | KeyShareEntry* kse = (KeyShareEntry*)extension->data; |
7232 | | /* Exactly one KeyShareEntry with the HRR group must be present. */ |
7233 | 0 | if (kse == NULL || kse->next != NULL || |
7234 | 0 | kse->group != ssl->hrr_keyshare_group) { |
7235 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7236 | 0 | } |
7237 | 0 | } |
7238 | 0 | else |
7239 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7240 | 0 | } |
7241 | 0 | #endif |
7242 | | |
7243 | | #if defined(HAVE_ECH) |
7244 | | /* hash clientHelloInner to hsHashesEch */ |
7245 | | if (echX != NULL && ssl->ctx->echConfigs != NULL && |
7246 | | !ssl->options.disableECH && |
7247 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7248 | | ret = EchHashHelloInner(ssl, (WOLFSSL_ECH*)echX->data); |
7249 | | if (ret != 0) |
7250 | | goto exit_dch; |
7251 | | } |
7252 | | #endif |
7253 | | |
7254 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7255 | | defined(HAVE_TLS_EXTENSIONS) |
7256 | | ret = CheckPreSharedKeys(ssl, input + args->begin, helloSz, ssl->clSuites, |
7257 | | &args->usingPSK); |
7258 | | if (ret != 0) |
7259 | | goto exit_dch; |
7260 | | #else |
7261 | 0 | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7262 | 0 | goto exit_dch; |
7263 | 0 | #endif |
7264 | | |
7265 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7266 | | defined(HAVE_TLS_EXTENSIONS) |
7267 | | if (!args->usingPSK) |
7268 | | #endif |
7269 | 0 | { |
7270 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7271 | | /* Not using PSK so don't require no KE. */ |
7272 | | ssl->options.noPskDheKe = 0; |
7273 | | #endif |
7274 | |
|
7275 | 0 | #ifndef NO_CERTS |
7276 | 0 | if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) { |
7277 | 0 | WOLFSSL_MSG("Client did not send a KeyShare extension"); |
7278 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7279 | 0 | } |
7280 | | /* Can't check ssl->extensions here as SigAlgs are unconditionally |
7281 | | set by TLSX_PopulateExtensions */ |
7282 | 0 | if (ssl->clSuites->hashSigAlgoSz == 0) { |
7283 | 0 | WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension"); |
7284 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7285 | 0 | } |
7286 | | #else |
7287 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7288 | | #endif |
7289 | 0 | } |
7290 | |
|
7291 | | #ifdef HAVE_ALPN |
7292 | | /* With PSK and all other things validated, it's time to |
7293 | | * select the ALPN protocol, if so requested */ |
7294 | | if ((ret = ALPN_Select(ssl)) != 0) |
7295 | | goto exit_dch; |
7296 | | #endif |
7297 | 0 | } /* case TLS_ASYNC_BEGIN */ |
7298 | 0 | FALL_THROUGH; |
7299 | |
|
7300 | 0 | case TLS_ASYNC_BUILD: |
7301 | | /* Advance state and proceed */ |
7302 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
7303 | 0 | FALL_THROUGH; |
7304 | |
|
7305 | 0 | case TLS_ASYNC_DO: |
7306 | 0 | { |
7307 | | #ifdef WOLFSSL_CERT_SETUP_CB |
7308 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
7309 | | goto exit_dch; |
7310 | | #endif |
7311 | 0 | #ifndef NO_CERTS |
7312 | 0 | if (!args->usingPSK) { |
7313 | 0 | if ((ret = MatchSuite(ssl, ssl->clSuites)) < 0) { |
7314 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7315 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7316 | | #endif |
7317 | 0 | WOLFSSL_MSG("Unsupported cipher suite, ClientHello 1.3"); |
7318 | 0 | goto exit_dch; |
7319 | 0 | } |
7320 | 0 | } |
7321 | 0 | #endif |
7322 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7323 | 0 | if (args->usingPSK == 2) { |
7324 | | /* Pick key share and Generate a new key if not present. */ |
7325 | 0 | int doHelloRetry = 0; |
7326 | 0 | ret = TLSX_KeyShare_Establish(ssl, &doHelloRetry); |
7327 | 0 | if (doHelloRetry) { |
7328 | | /* Make sure we don't send HRR twice */ |
7329 | 0 | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7330 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7331 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7332 | 0 | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7333 | 0 | ret = 0; /* for hello_retry return 0 */ |
7334 | 0 | } |
7335 | 0 | if (ret != 0) |
7336 | 0 | goto exit_dch; |
7337 | 0 | } |
7338 | 0 | #endif |
7339 | | |
7340 | | /* Verify the cipher suite is the same as what was chosen in HRR. |
7341 | | * got_client_hello == 2 covers the stateful path. |
7342 | | * cookieGood covers the stateless DTLS path. */ |
7343 | 0 | if ((ssl->msgsReceived.got_client_hello == 2 |
7344 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7345 | | || ssl->options.cookieGood |
7346 | | #endif |
7347 | 0 | ) && |
7348 | 0 | (ssl->options.cipherSuite0 != ssl->options.hrrCipherSuite0 || |
7349 | 0 | ssl->options.cipherSuite != ssl->options.hrrCipherSuite)) { |
7350 | 0 | WOLFSSL_MSG("Cipher suite in second ClientHello does not match " |
7351 | 0 | "HelloRetryRequest"); |
7352 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7353 | 0 | } |
7354 | | |
7355 | | /* Advance state and proceed */ |
7356 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
7357 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
7358 | 0 | FALL_THROUGH; |
7359 | |
|
7360 | 0 | case TLS_ASYNC_VERIFY: |
7361 | 0 | { |
7362 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SUPPORTED_CURVES) |
7363 | | /* Check if the KeyShare calculations from the previous state are complete. |
7364 | | * wolfSSL_AsyncPop advances ssl->options.asyncState so we may end up here |
7365 | | * with a pending calculation. */ |
7366 | | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7367 | | if (extension != NULL && extension->resp == 1) { |
7368 | | KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data; |
7369 | | if (serverKSE != NULL && |
7370 | | serverKSE->lastRet == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7371 | | #if defined(WOLFSSL_HAVE_MLKEM) |
7372 | | if (WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(serverKSE->group)) { |
7373 | | ret = TLSX_KeyShare_HandlePqcHybridKeyServer(ssl, serverKSE, |
7374 | | serverKSE->ke, serverKSE->keLen); |
7375 | | } |
7376 | | else |
7377 | | #endif |
7378 | | { |
7379 | | ret = TLSX_KeyShare_GenKey(ssl, serverKSE); |
7380 | | } |
7381 | | if (ret != 0) |
7382 | | goto exit_dch; |
7383 | | } |
7384 | | } |
7385 | | #endif |
7386 | | /* Advance state and proceed */ |
7387 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
7388 | 0 | } |
7389 | 0 | FALL_THROUGH; |
7390 | |
|
7391 | 0 | case TLS_ASYNC_FINALIZE: |
7392 | 0 | { |
7393 | 0 | *inOutIdx = args->idx; |
7394 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
7395 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7396 | | ssl->options.pskNegotiated = (args->usingPSK != 0); |
7397 | | #endif |
7398 | |
|
7399 | 0 | if (!args->usingPSK) { |
7400 | 0 | #ifndef NO_CERTS |
7401 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
7402 | | #ifdef HAVE_NULL_CIPHER |
7403 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
7404 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
7405 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
7406 | | ; |
7407 | | } |
7408 | | else |
7409 | | #endif |
7410 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
7411 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7412 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
7413 | 0 | ; /* Do nothing. */ |
7414 | 0 | } |
7415 | 0 | else |
7416 | 0 | #endif |
7417 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
7418 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7419 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
7420 | 0 | ; /* Do nothing. */ |
7421 | 0 | } |
7422 | 0 | else |
7423 | 0 | #endif |
7424 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
7425 | 0 | WOLFSSL_MSG("Negotiated ciphersuite from lesser version than " |
7426 | 0 | "TLS v1.3"); |
7427 | 0 | ERROR_OUT(MATCH_SUITE_ERROR, exit_dch); |
7428 | 0 | } |
7429 | | |
7430 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7431 | | if (ssl->options.resuming) { |
7432 | | ssl->options.resuming = 0; |
7433 | | ssl->arrays->psk_keySz = 0; |
7434 | | XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size); |
7435 | | } |
7436 | | #endif |
7437 | | |
7438 | | /* Derive early secret for handshake secret. */ |
7439 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
7440 | 0 | goto exit_dch; |
7441 | 0 | #endif /* !NO_CERTS */ |
7442 | 0 | } |
7443 | 0 | break; |
7444 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
7445 | 0 | default: |
7446 | 0 | ret = INPUT_CASE_ERROR; |
7447 | 0 | } /* switch (ssl->options.asyncState) */ |
7448 | | |
7449 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7450 | | if (ret == 0 && ssl->options.sendCookie) { |
7451 | | if (ssl->options.cookieGood && |
7452 | | ssl->options.acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE) { |
7453 | | /* Processing second ClientHello. Clear HRR state. */ |
7454 | | ssl->options.serverState = NULL_STATE; |
7455 | | } |
7456 | | |
7457 | | if (ssl->options.cookieGood && |
7458 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7459 | | /* If we already verified the peer with a cookie then we can't |
7460 | | * do another HRR for cipher negotiation. Send alert and restart |
7461 | | * the entire handshake. */ |
7462 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7463 | | } |
7464 | | #ifdef WOLFSSL_DTLS13 |
7465 | | if (ssl->options.dtls && |
7466 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7467 | | /* Cookie and key share negotiation should be handled in |
7468 | | * DoClientHelloStateless. If we enter here then something went |
7469 | | * wrong in our logic. */ |
7470 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7471 | | } |
7472 | | #endif |
7473 | | /* Send a cookie */ |
7474 | | if (!ssl->options.cookieGood && |
7475 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7476 | | #ifdef WOLFSSL_DTLS13 |
7477 | | if (ssl->options.dtls) { |
7478 | | #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME |
7479 | | /* We can skip cookie on resumption */ |
7480 | | if (!ssl->options.dtls || !ssl->options.dtls13NoHrrOnResume || |
7481 | | !args->usingPSK) |
7482 | | #endif |
7483 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7484 | | } |
7485 | | else |
7486 | | #endif |
7487 | | { |
7488 | | /* Need to remove the keyshare ext if we found a common group |
7489 | | * and are not doing curve negotiation. */ |
7490 | | TLSX_Remove(&ssl->extensions, TLSX_KEY_SHARE, ssl->heap); |
7491 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7492 | | } |
7493 | | |
7494 | | } |
7495 | | } |
7496 | | #endif /* WOLFSSL_DTLS13 */ |
7497 | | |
7498 | | #ifdef WOLFSSL_DTLS_CID |
7499 | | /* do not modify CID state if we are sending an HRR */ |
7500 | | if (ret == 0 && ssl->options.dtls && ssl->options.useDtlsCID && |
7501 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7502 | | DtlsCIDOnExtensionsParsed(ssl); |
7503 | | #endif /* WOLFSSL_DTLS_CID */ |
7504 | | |
7505 | | |
7506 | | |
7507 | 0 | exit_dch: |
7508 | |
|
7509 | 0 | WOLFSSL_LEAVE("DoTls13ClientHello", ret); |
7510 | |
|
7511 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7512 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7513 | | ssl->msgsReceived.got_client_hello = 0; |
7514 | | return ret; |
7515 | | } |
7516 | | #endif |
7517 | |
|
7518 | 0 | FreeDch13Args(ssl, args); |
7519 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7520 | | FreeAsyncCtx(ssl, 0); |
7521 | | #endif |
7522 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO); |
7523 | |
|
7524 | 0 | if (ret != 0) { |
7525 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
7526 | 0 | } |
7527 | |
|
7528 | | #if defined(HAVE_ECH) |
7529 | | if (ret == 0 && echX != NULL && |
7530 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
7531 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7532 | | |
7533 | | /* add the header to the inner hello */ |
7534 | | AddTls13HandShakeHeader(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
7535 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen, 0, 0, |
7536 | | client_hello, ssl); |
7537 | | } |
7538 | | #endif |
7539 | |
|
7540 | 0 | return ret; |
7541 | 0 | } |
7542 | | |
7543 | | /* Send TLS v1.3 ServerHello message to client. |
7544 | | * Only a server will send this message. |
7545 | | * |
7546 | | * ssl The SSL/TLS object. |
7547 | | * returns 0 on success, otherwise failure. |
7548 | | */ |
7549 | | /* handle generation of TLS 1.3 server_hello (2) */ |
7550 | | int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType) |
7551 | 0 | { |
7552 | 0 | int ret; |
7553 | 0 | byte* output; |
7554 | 0 | word16 length; |
7555 | 0 | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7556 | 0 | int sendSz; |
7557 | | #if defined(HAVE_ECH) |
7558 | | TLSX* echX = NULL; |
7559 | | byte* acceptLabel = (byte*)echAcceptConfirmationLabel; |
7560 | | word32 acceptOffset; |
7561 | | word16 acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
7562 | | #endif |
7563 | |
|
7564 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND); |
7565 | 0 | WOLFSSL_ENTER("SendTls13ServerHello"); |
7566 | | |
7567 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated in |
7568 | | * dtls.c */ |
7569 | 0 | if (extMsgType == hello_retry_request |
7570 | | #ifdef WOLFSSL_DTLS13 |
7571 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
7572 | | #endif |
7573 | 0 | ) { |
7574 | 0 | WOLFSSL_MSG("wolfSSL Sending HelloRetryRequest"); |
7575 | 0 | if ((ret = RestartHandshakeHash(ssl)) < 0) |
7576 | 0 | return ret; |
7577 | 0 | } |
7578 | | |
7579 | 0 | ssl->options.buildingMsg = 1; |
7580 | | #ifdef WOLFSSL_DTLS13 |
7581 | | if (ssl->options.dtls) |
7582 | | idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ; |
7583 | | #endif /* WOLFSSL_DTLS13 */ |
7584 | | |
7585 | | /* Protocol version, server random, session id, cipher suite, compression |
7586 | | * and extensions. |
7587 | | */ |
7588 | 0 | length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz + |
7589 | 0 | SUITE_LEN + COMP_LEN; |
7590 | 0 | ret = TLSX_GetResponseSize(ssl, extMsgType, &length); |
7591 | 0 | if (ret != 0) |
7592 | 0 | return ret; |
7593 | 0 | sendSz = (int)(idx + length); |
7594 | | |
7595 | | /* Check buffers are big enough and grow if needed. */ |
7596 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7597 | 0 | return ret; |
7598 | | |
7599 | | /* Get position in output buffer to write new message to. */ |
7600 | 0 | output = GetOutputBuffer(ssl); |
7601 | | |
7602 | | /* Put the record and handshake headers on. */ |
7603 | 0 | AddTls13Headers(output, length, server_hello, ssl); |
7604 | | |
7605 | | /* The protocol version must be TLS v1.2 for middleboxes. */ |
7606 | 0 | output[idx++] = ssl->version.major; |
7607 | 0 | output[idx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
7608 | |
|
7609 | 0 | if (extMsgType == server_hello) { |
7610 | | /* Generate server random. */ |
7611 | 0 | if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0) |
7612 | 0 | return ret; |
7613 | 0 | } |
7614 | 0 | else { |
7615 | | /* HelloRetryRequest message has fixed value for random. */ |
7616 | 0 | XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN); |
7617 | 0 | } |
7618 | | |
7619 | | #if defined(HAVE_ECH) |
7620 | | /* last 8 bytes of server random */ |
7621 | | acceptOffset = idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
7622 | | #endif |
7623 | | |
7624 | | /* Store in SSL for debugging. */ |
7625 | 0 | XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN); |
7626 | 0 | idx += RAN_LEN; |
7627 | |
|
7628 | | #ifdef WOLFSSL_DEBUG_TLS |
7629 | | WOLFSSL_MSG("Server random"); |
7630 | | WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN); |
7631 | | #endif |
7632 | |
|
7633 | 0 | output[idx++] = ssl->session->sessionIDSz; |
7634 | 0 | if (ssl->session->sessionIDSz > 0) { |
7635 | 0 | XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz); |
7636 | 0 | idx += ssl->session->sessionIDSz; |
7637 | 0 | } |
7638 | | |
7639 | | /* Chosen cipher suite */ |
7640 | 0 | output[idx++] = ssl->options.cipherSuite0; |
7641 | 0 | output[idx++] = ssl->options.cipherSuite; |
7642 | | #ifdef WOLFSSL_DEBUG_TLS |
7643 | | WOLFSSL_MSG("Chosen cipher suite:"); |
7644 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
7645 | | ssl->options.cipherSuite)); |
7646 | | #endif |
7647 | | |
7648 | | /* Compression not supported in TLS v1.3. */ |
7649 | 0 | output[idx++] = 0; |
7650 | | |
7651 | | /* Extensions */ |
7652 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL); |
7653 | 0 | if (ret != 0) |
7654 | 0 | return ret; |
7655 | | |
7656 | | /* When we send a HRR, we store the selected key share group to later check |
7657 | | * that the client uses the same group in the second ClientHello. |
7658 | | * |
7659 | | * In case of stateless DTLS, we do not store the group, however, as it is |
7660 | | * already stored in the cookie that is sent to the client. We later recover |
7661 | | * the group from the cookie to prevent storing a state in a stateless |
7662 | | * server. |
7663 | | * |
7664 | | * Similar logic holds for the hrrCipherSuite. */ |
7665 | 0 | if (extMsgType == hello_retry_request |
7666 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
7667 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
7668 | | #endif |
7669 | 0 | ) { |
7670 | 0 | TLSX* ksExt = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7671 | 0 | if (ksExt != NULL) { |
7672 | 0 | KeyShareEntry* kse = (KeyShareEntry*)ksExt->data; |
7673 | 0 | if (kse != NULL) |
7674 | 0 | ssl->hrr_keyshare_group = kse->group; |
7675 | 0 | } |
7676 | |
|
7677 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
7678 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
7679 | 0 | } |
7680 | |
|
7681 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7682 | | if (ssl->options.sendCookie && extMsgType == hello_retry_request) { |
7683 | | /* Reset the hashes from here. We will be able to restart the hashes |
7684 | | * from the cookie in RestartHandshakeHashWithCookie */ |
7685 | | #ifdef WOLFSSL_DTLS13 |
7686 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated |
7687 | | * in dtls.c */ |
7688 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) |
7689 | | ret = 0; |
7690 | | else |
7691 | | #endif |
7692 | | ret = InitHandshakeHashes(ssl); |
7693 | | } |
7694 | | else |
7695 | | #endif |
7696 | 0 | { |
7697 | | #ifdef WOLFSSL_DTLS13 |
7698 | | if (ssl->options.dtls) { |
7699 | | ret = Dtls13HashHandshake( |
7700 | | ssl, |
7701 | | output + Dtls13GetRlHeaderLength(ssl, 0) , |
7702 | | (word16)sendSz - Dtls13GetRlHeaderLength(ssl, 0)); |
7703 | | } |
7704 | | else |
7705 | | #endif /* WOLFSSL_DTLS13 */ |
7706 | 0 | { |
7707 | | #if defined(HAVE_ECH) |
7708 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7709 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7710 | | if (echX == NULL) |
7711 | | return WOLFSSL_FATAL_ERROR; |
7712 | | /* use hrr offset */ |
7713 | | if (extMsgType == hello_retry_request) { |
7714 | | acceptOffset = |
7715 | | (word32)(((WOLFSSL_ECH*)echX->data)->confBuf - output); |
7716 | | acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
7717 | | acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
7718 | | } |
7719 | | /* replace the last 8 bytes of server random with the accept */ |
7720 | | if (((WOLFSSL_ECH*)echX->data)->state == ECH_PARSED_INTERNAL) { |
7721 | | if (ret == 0) { |
7722 | | ret = EchWriteAcceptance(ssl, acceptLabel, |
7723 | | acceptLabelSz, output + RECORD_HEADER_SZ, |
7724 | | acceptOffset - RECORD_HEADER_SZ, |
7725 | | sendSz - RECORD_HEADER_SZ, extMsgType); |
7726 | | } |
7727 | | if (extMsgType == hello_retry_request) { |
7728 | | /* reset the ech state for round 2 */ |
7729 | | ((WOLFSSL_ECH*)echX->data)->state = ECH_WRITE_NONE; |
7730 | | } |
7731 | | else { |
7732 | | if (ret == 0) { |
7733 | | /* update serverRandom on success */ |
7734 | | XMEMCPY(ssl->arrays->serverRandom, |
7735 | | output + acceptOffset - |
7736 | | (RAN_LEN -ECH_ACCEPT_CONFIRMATION_SZ), RAN_LEN); |
7737 | | } |
7738 | | /* remove ech so we don't keep sending it in write */ |
7739 | | TLSX_Remove(&ssl->extensions, TLSX_ECH, ssl->heap); |
7740 | | } |
7741 | | } |
7742 | | } |
7743 | | #endif |
7744 | 0 | if (ret == 0) |
7745 | 0 | ret = HashOutput(ssl, output, sendSz, 0); |
7746 | 0 | } |
7747 | 0 | } |
7748 | |
|
7749 | 0 | if (ret != 0) |
7750 | 0 | return ret; |
7751 | | |
7752 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7753 | | if (ssl->hsInfoOn) |
7754 | | AddPacketName(ssl, "ServerHello"); |
7755 | | if (ssl->toInfoOn) { |
7756 | | ret = AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz, |
7757 | | WRITE_PROTO, 0, ssl->heap); |
7758 | | if (ret != 0) |
7759 | | return ret; |
7760 | | } |
7761 | | #endif |
7762 | | |
7763 | 0 | if (extMsgType == server_hello) |
7764 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
7765 | |
|
7766 | 0 | ssl->options.buildingMsg = 0; |
7767 | | #ifdef WOLFSSL_DTLS13 |
7768 | | if (ssl->options.dtls) { |
7769 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)sendSz, |
7770 | | (enum HandShakeType)extMsgType, 0); |
7771 | | |
7772 | | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7773 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7774 | | return ret; |
7775 | | } |
7776 | | #endif /* WOLFSSL_DTLS13 */ |
7777 | |
|
7778 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7779 | |
|
7780 | 0 | if (!ssl->options.groupMessages || extMsgType != server_hello) |
7781 | 0 | ret = SendBuffered(ssl); |
7782 | |
|
7783 | 0 | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
7784 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
7785 | |
|
7786 | 0 | return ret; |
7787 | 0 | } |
7788 | | |
7789 | | /* handle generation of TLS 1.3 encrypted_extensions (8) */ |
7790 | | /* Send the rest of the extensions encrypted under the handshake key. |
7791 | | * This message is always encrypted in TLS v1.3. |
7792 | | * Only a server will send this message. |
7793 | | * |
7794 | | * ssl The SSL/TLS object. |
7795 | | * returns 0 on success, otherwise failure. |
7796 | | */ |
7797 | | static int SendTls13EncryptedExtensions(WOLFSSL* ssl) |
7798 | 0 | { |
7799 | 0 | int ret; |
7800 | 0 | byte* output; |
7801 | 0 | word16 length = 0; |
7802 | 0 | word32 idx; |
7803 | 0 | int sendSz; |
7804 | |
|
7805 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7806 | 0 | WOLFSSL_ENTER("SendTls13EncryptedExtensions"); |
7807 | |
|
7808 | 0 | ssl->options.buildingMsg = 1; |
7809 | 0 | ssl->keys.encryptionOn = 1; |
7810 | |
|
7811 | | #ifdef WOLFSSL_DTLS13 |
7812 | | if (ssl->options.dtls) { |
7813 | | idx = Dtls13GetHeadersLength(ssl, encrypted_extensions); |
7814 | | } |
7815 | | else |
7816 | | #endif /* WOLFSSL_DTLS13 */ |
7817 | 0 | { |
7818 | 0 | idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7819 | 0 | } |
7820 | |
|
7821 | 0 | #if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
7822 | 0 | if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0) |
7823 | 0 | return ret; |
7824 | 0 | #endif |
7825 | | |
7826 | | /* Derive the handshake secret now that we are at first message to be |
7827 | | * encrypted under the keys. |
7828 | | */ |
7829 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
7830 | 0 | return ret; |
7831 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
7832 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) |
7833 | 0 | return ret; |
7834 | | |
7835 | | /* Setup encrypt/decrypt keys for following messages. */ |
7836 | | #ifdef WOLFSSL_EARLY_DATA |
7837 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
7838 | | return ret; |
7839 | | if (ssl->earlyData != process_early_data) { |
7840 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
7841 | | return ret; |
7842 | | } |
7843 | | #else |
7844 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
7845 | 0 | return ret; |
7846 | 0 | #endif |
7847 | | #ifdef WOLFSSL_QUIC |
7848 | | if (IsAtLeastTLSv1_3(ssl->version) && WOLFSSL_IS_QUIC(ssl)) { |
7849 | | ret = wolfSSL_quic_add_transport_extensions(ssl, encrypted_extensions); |
7850 | | if (ret != 0) |
7851 | | return ret; |
7852 | | } |
7853 | | #endif |
7854 | | |
7855 | | #ifdef WOLFSSL_DTLS13 |
7856 | | if (ssl->options.dtls) { |
7857 | | w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
7858 | | ssl->dtls13Epoch = epochHandshake; |
7859 | | |
7860 | | ret = Dtls13SetEpochKeys( |
7861 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
7862 | | if (ret != 0) |
7863 | | return ret; |
7864 | | |
7865 | | } |
7866 | | #endif /* WOLFSSL_DTLS13 */ |
7867 | | |
7868 | 0 | ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length); |
7869 | 0 | if (ret != 0) |
7870 | 0 | return ret; |
7871 | | |
7872 | 0 | sendSz = (int)(idx + length); |
7873 | | /* Encryption always on. */ |
7874 | 0 | sendSz += MAX_MSG_EXTRA; |
7875 | | |
7876 | | /* Check buffers are big enough and grow if needed. */ |
7877 | 0 | ret = CheckAvailableSize(ssl, sendSz); |
7878 | 0 | if (ret != 0) |
7879 | 0 | return ret; |
7880 | | |
7881 | | /* Get position in output buffer to write new message to. */ |
7882 | 0 | output = GetOutputBuffer(ssl); |
7883 | | |
7884 | | /* Put the record and handshake headers on. */ |
7885 | 0 | AddTls13Headers(output, length, encrypted_extensions, ssl); |
7886 | |
|
7887 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL); |
7888 | 0 | if (ret != 0) |
7889 | 0 | return ret; |
7890 | 0 | idx += length; |
7891 | |
|
7892 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
7893 | | if (ssl->hsInfoOn) |
7894 | | AddPacketName(ssl, "EncryptedExtensions"); |
7895 | | if (ssl->toInfoOn) { |
7896 | | ret = AddPacketInfo(ssl, "EncryptedExtensions", handshake, output, |
7897 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
7898 | | if (ret != 0) |
7899 | | return ret; |
7900 | | } |
7901 | | #endif |
7902 | |
|
7903 | | #ifdef WOLFSSL_DTLS13 |
7904 | | if (ssl->options.dtls) { |
7905 | | ssl->options.buildingMsg = 0; |
7906 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)idx, |
7907 | | encrypted_extensions, 1); |
7908 | | |
7909 | | if (ret == 0) |
7910 | | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7911 | | |
7912 | | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7913 | | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7914 | | |
7915 | | return ret; |
7916 | | } |
7917 | | #endif /* WOLFSSL_DTLS13 */ |
7918 | | |
7919 | | /* This handshake message is always encrypted. */ |
7920 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
7921 | 0 | (int)(idx - RECORD_HEADER_SZ), |
7922 | 0 | handshake, 1, 0, 0); |
7923 | 0 | if (sendSz < 0) |
7924 | 0 | return sendSz; |
7925 | | |
7926 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
7927 | 0 | ssl->options.buildingMsg = 0; |
7928 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
7929 | |
|
7930 | 0 | if (!ssl->options.groupMessages) |
7931 | 0 | ret = SendBuffered(ssl); |
7932 | | |
7933 | |
|
7934 | 0 | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
7935 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
7936 | |
|
7937 | 0 | return ret; |
7938 | 0 | } |
7939 | | |
7940 | | #ifndef NO_CERTS |
7941 | | /* handle generation TLS v1.3 certificate_request (13) */ |
7942 | | /* Send the TLS v1.3 CertificateRequest message. |
7943 | | * This message is always encrypted in TLS v1.3. |
7944 | | * Only a server will send this message. |
7945 | | * |
7946 | | * ssl SSL/TLS object. |
7947 | | * reqCtx Request context. |
7948 | | * reqCtxLen Length of context. 0 when sending as part of handshake. |
7949 | | * returns 0 on success, otherwise failure. |
7950 | | */ |
7951 | | static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx, |
7952 | | word32 reqCtxLen) |
7953 | 0 | { |
7954 | 0 | byte* output; |
7955 | 0 | int ret; |
7956 | 0 | int sendSz; |
7957 | 0 | word32 i; |
7958 | 0 | word32 reqSz; |
7959 | 0 | SignatureAlgorithms* sa; |
7960 | |
|
7961 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
7962 | 0 | WOLFSSL_ENTER("SendTls13CertificateRequest"); |
7963 | |
|
7964 | 0 | ssl->options.buildingMsg = 1; |
7965 | |
|
7966 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) |
7967 | 0 | return SIDE_ERROR; |
7968 | | |
7969 | | /* Use ssl->suites->hashSigAlgo so wolfSSL_set1_sigalgs_list() is honored. |
7970 | | * hashSigAlgoSz=0 makes GetSize/Write fall back to WOLFSSL_SUITES(ssl). */ |
7971 | 0 | sa = TLSX_SignatureAlgorithms_New(ssl, 0, ssl->heap); |
7972 | 0 | if (sa == NULL) |
7973 | 0 | return MEMORY_ERROR; |
7974 | 0 | ret = TLSX_Push(&ssl->extensions, TLSX_SIGNATURE_ALGORITHMS, sa, ssl->heap); |
7975 | 0 | if (ret != 0) { |
7976 | 0 | TLSX_SignatureAlgorithms_FreeAll(sa, ssl->heap); |
7977 | 0 | return ret; |
7978 | 0 | } |
7979 | | |
7980 | 0 | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7981 | | #ifdef WOLFSSL_DTLS13 |
7982 | | if (ssl->options.dtls) |
7983 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
7984 | | #endif /* WOLFSSL_DTLS13 */ |
7985 | |
|
7986 | 0 | reqSz = (word16)(OPAQUE8_LEN + reqCtxLen); |
7987 | 0 | ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz); |
7988 | 0 | if (ret != 0) |
7989 | 0 | return ret; |
7990 | | |
7991 | 0 | sendSz = (int)(i + reqSz); |
7992 | | /* Always encrypted and make room for padding. */ |
7993 | 0 | sendSz += MAX_MSG_EXTRA; |
7994 | | |
7995 | | /* Check buffers are big enough and grow if needed. */ |
7996 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
7997 | 0 | return ret; |
7998 | | |
7999 | | /* Get position in output buffer to write new message to. */ |
8000 | 0 | output = GetOutputBuffer(ssl); |
8001 | | |
8002 | | /* Put the record and handshake headers on. */ |
8003 | 0 | AddTls13Headers(output, reqSz, certificate_request, ssl); |
8004 | | |
8005 | | /* Certificate request context. */ |
8006 | 0 | output[i++] = (byte)reqCtxLen; |
8007 | 0 | if (reqCtxLen != 0) { |
8008 | 0 | XMEMCPY(output + i, reqCtx, reqCtxLen); |
8009 | 0 | i += reqCtxLen; |
8010 | 0 | } |
8011 | | |
8012 | | /* Certificate extensions. */ |
8013 | 0 | reqSz = 0; |
8014 | 0 | ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz); |
8015 | 0 | if (ret != 0) |
8016 | 0 | return ret; |
8017 | 0 | i += reqSz; |
8018 | |
|
8019 | | #ifdef WOLFSSL_DTLS13 |
8020 | | if (ssl->options.dtls) { |
8021 | | ssl->options.buildingMsg = 0; |
8022 | | ret = |
8023 | | Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
8024 | | certificate_request, 1); |
8025 | | |
8026 | | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8027 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8028 | | |
8029 | | return ret; |
8030 | | |
8031 | | } |
8032 | | #endif /* WOLFSSL_DTLS13 */ |
8033 | | |
8034 | | /* Always encrypted. */ |
8035 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
8036 | 0 | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
8037 | 0 | if (sendSz < 0) |
8038 | 0 | return sendSz; |
8039 | | |
8040 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8041 | | if (ssl->hsInfoOn) |
8042 | | AddPacketName(ssl, "CertificateRequest"); |
8043 | | if (ssl->toInfoOn) { |
8044 | | ret = AddPacketInfo(ssl, "CertificateRequest", handshake, output, |
8045 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8046 | | if (ret != 0) |
8047 | | return ret; |
8048 | | } |
8049 | | #endif |
8050 | | |
8051 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8052 | 0 | ssl->options.buildingMsg = 0; |
8053 | 0 | if (!ssl->options.groupMessages) |
8054 | 0 | ret = SendBuffered(ssl); |
8055 | |
|
8056 | 0 | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8057 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8058 | |
|
8059 | 0 | return ret; |
8060 | 0 | } |
8061 | | #endif /* NO_CERTS */ |
8062 | | #endif /* NO_WOLFSSL_SERVER */ |
8063 | | |
8064 | | #ifndef NO_CERTS |
8065 | | #if (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) && \ |
8066 | | (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8067 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)) |
8068 | | /* Encode the signature algorithm into buffer. |
8069 | | * |
8070 | | * hashalgo The hash algorithm. |
8071 | | * hsType The signature type. |
8072 | | * output The buffer to encode into. |
8073 | | */ |
8074 | | static WC_INLINE void EncodeSigAlg(const WOLFSSL * ssl, byte hashAlgo, |
8075 | | byte hsType, byte* output) |
8076 | | { |
8077 | | (void)ssl; |
8078 | | switch (hsType) { |
8079 | | #ifdef HAVE_ECC |
8080 | | case ecc_dsa_sa_algo: |
8081 | | if (ssl->pkCurveOID == ECC_BRAINPOOLP256R1_OID) { |
8082 | | output[0] = NEW_SA_MAJOR; |
8083 | | output[1] = ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR; |
8084 | | } |
8085 | | else if (ssl->pkCurveOID == ECC_BRAINPOOLP384R1_OID) { |
8086 | | output[0] = NEW_SA_MAJOR; |
8087 | | output[1] = ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR; |
8088 | | } |
8089 | | else if (ssl->pkCurveOID == ECC_BRAINPOOLP512R1_OID) { |
8090 | | output[0] = NEW_SA_MAJOR; |
8091 | | output[1] = ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR; |
8092 | | } |
8093 | | else { |
8094 | | output[0] = hashAlgo; |
8095 | | output[1] = ecc_dsa_sa_algo; |
8096 | | } |
8097 | | break; |
8098 | | #endif |
8099 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8100 | | case sm2_sa_algo: |
8101 | | output[0] = SM2_SA_MAJOR; |
8102 | | output[1] = SM2_SA_MINOR; |
8103 | | break; |
8104 | | #endif |
8105 | | #ifdef HAVE_ED25519 |
8106 | | /* ED25519: 0x0807 */ |
8107 | | case ed25519_sa_algo: |
8108 | | output[0] = ED25519_SA_MAJOR; |
8109 | | output[1] = ED25519_SA_MINOR; |
8110 | | (void)hashAlgo; |
8111 | | break; |
8112 | | #endif |
8113 | | #ifdef HAVE_ED448 |
8114 | | /* ED448: 0x0808 */ |
8115 | | case ed448_sa_algo: |
8116 | | output[0] = ED448_SA_MAJOR; |
8117 | | output[1] = ED448_SA_MINOR; |
8118 | | (void)hashAlgo; |
8119 | | break; |
8120 | | #endif |
8121 | | #ifndef NO_RSA |
8122 | | /* PSS signatures: 0x080[4-6] or 0x080[9-B] */ |
8123 | | case rsa_pss_sa_algo: |
8124 | | output[0] = rsa_pss_sa_algo; |
8125 | | #ifdef WC_RSA_PSS |
8126 | | /* If the private key uses the RSA-PSS OID, and the peer supports |
8127 | | * the rsa_pss_pss_* signature algorithm in use, then report |
8128 | | * rsa_pss_pss_* rather than rsa_pss_rsae_*. */ |
8129 | | if (ssl->useRsaPss && |
8130 | | ((ssl->pssAlgo & (1U << hashAlgo)) != 0U) && |
8131 | | (sha256_mac <= hashAlgo) && (hashAlgo <= sha512_mac)) |
8132 | | { |
8133 | | output[1] = PSS_RSAE_TO_PSS_PSS(hashAlgo); |
8134 | | } |
8135 | | else |
8136 | | #endif |
8137 | | { |
8138 | | output[1] = hashAlgo; |
8139 | | } |
8140 | | break; |
8141 | | #endif |
8142 | | #ifdef HAVE_FALCON |
8143 | | case falcon_level1_sa_algo: |
8144 | | output[0] = FALCON_LEVEL1_SA_MAJOR; |
8145 | | output[1] = FALCON_LEVEL1_SA_MINOR; |
8146 | | break; |
8147 | | case falcon_level5_sa_algo: |
8148 | | output[0] = FALCON_LEVEL5_SA_MAJOR; |
8149 | | output[1] = FALCON_LEVEL5_SA_MINOR; |
8150 | | break; |
8151 | | #endif |
8152 | | #ifdef HAVE_DILITHIUM |
8153 | | case dilithium_level2_sa_algo: |
8154 | | output[0] = DILITHIUM_LEVEL2_SA_MAJOR; |
8155 | | output[1] = DILITHIUM_LEVEL2_SA_MINOR; |
8156 | | break; |
8157 | | case dilithium_level3_sa_algo: |
8158 | | output[0] = DILITHIUM_LEVEL3_SA_MAJOR; |
8159 | | output[1] = DILITHIUM_LEVEL3_SA_MINOR; |
8160 | | break; |
8161 | | case dilithium_level5_sa_algo: |
8162 | | output[0] = DILITHIUM_LEVEL5_SA_MAJOR; |
8163 | | output[1] = DILITHIUM_LEVEL5_SA_MINOR; |
8164 | | break; |
8165 | | #endif |
8166 | | default: |
8167 | | break; |
8168 | | } |
8169 | | } |
8170 | | #endif |
8171 | | |
8172 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8173 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
8174 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8175 | | /* These match up with what the OQS team has defined. */ |
8176 | | #define HYBRID_SA_MAJOR 0xFE |
8177 | | #define HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR 0xA1 |
8178 | | #define HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR 0xA2 |
8179 | | #define HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR 0xA4 |
8180 | | #define HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR 0xA6 |
8181 | | #define HYBRID_P256_FALCON_LEVEL1_SA_MINOR 0xAF |
8182 | | #define HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR 0xB0 |
8183 | | #define HYBRID_P521_FALCON_LEVEL5_SA_MINOR 0xB2 |
8184 | | |
8185 | | /* Custom defined ones for PQC first */ |
8186 | | #define HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR 0xD1 |
8187 | | #define HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR 0xD2 |
8188 | | #define HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR 0xD3 |
8189 | | #define HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR 0xD4 |
8190 | | #define HYBRID_FALCON_LEVEL1_P256_SA_MINOR 0xD5 |
8191 | | #define HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR 0xD6 |
8192 | | #define HYBRID_FALCON_LEVEL5_P521_SA_MINOR 0xD7 |
8193 | | |
8194 | | |
8195 | | static void EncodeDualSigAlg(byte sigAlg, byte altSigAlg, byte* output) |
8196 | | { |
8197 | | /* Initialize output to error indicator. */ |
8198 | | output[0] = 0x0; |
8199 | | output[1] = 0x0; |
8200 | | |
8201 | | if (sigAlg == ecc_dsa_sa_algo && altSigAlg == dilithium_level2_sa_algo) { |
8202 | | output[1] = HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR; |
8203 | | } |
8204 | | else if (sigAlg == rsa_pss_sa_algo && |
8205 | | altSigAlg == dilithium_level2_sa_algo) { |
8206 | | output[1] = HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR; |
8207 | | } |
8208 | | else if (sigAlg == ecc_dsa_sa_algo && |
8209 | | altSigAlg == dilithium_level3_sa_algo) { |
8210 | | output[1] = HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR; |
8211 | | } |
8212 | | else if (sigAlg == ecc_dsa_sa_algo && |
8213 | | altSigAlg == dilithium_level5_sa_algo) { |
8214 | | output[1] = HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR; |
8215 | | } |
8216 | | else if (sigAlg == ecc_dsa_sa_algo && |
8217 | | altSigAlg == falcon_level1_sa_algo) { |
8218 | | output[1] = HYBRID_P256_FALCON_LEVEL1_SA_MINOR; |
8219 | | } |
8220 | | else if (sigAlg == rsa_pss_sa_algo && |
8221 | | altSigAlg == falcon_level1_sa_algo) { |
8222 | | output[1] = HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR; |
8223 | | } |
8224 | | else if (sigAlg == ecc_dsa_sa_algo && |
8225 | | altSigAlg == falcon_level5_sa_algo) { |
8226 | | output[1] = HYBRID_P521_FALCON_LEVEL5_SA_MINOR; |
8227 | | } |
8228 | | else if (sigAlg == dilithium_level2_sa_algo && |
8229 | | altSigAlg == ecc_dsa_sa_algo) { |
8230 | | output[1] = HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR; |
8231 | | } |
8232 | | else if (sigAlg == dilithium_level2_sa_algo && |
8233 | | altSigAlg == rsa_pss_sa_algo) { |
8234 | | output[1] = HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR; |
8235 | | } |
8236 | | else if (sigAlg == dilithium_level3_sa_algo && |
8237 | | altSigAlg == ecc_dsa_sa_algo) { |
8238 | | output[1] = HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR; |
8239 | | } |
8240 | | else if (sigAlg == dilithium_level5_sa_algo && |
8241 | | altSigAlg == ecc_dsa_sa_algo) { |
8242 | | output[1] = HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR; |
8243 | | } |
8244 | | else if (sigAlg == falcon_level1_sa_algo && |
8245 | | altSigAlg == ecc_dsa_sa_algo) { |
8246 | | output[1] = HYBRID_FALCON_LEVEL1_P256_SA_MINOR; |
8247 | | } |
8248 | | else if (sigAlg == falcon_level1_sa_algo && |
8249 | | altSigAlg == rsa_pss_sa_algo) { |
8250 | | output[1] = HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR; |
8251 | | } |
8252 | | else if (sigAlg == falcon_level5_sa_algo && |
8253 | | altSigAlg == ecc_dsa_sa_algo) { |
8254 | | output[1] = HYBRID_FALCON_LEVEL5_P521_SA_MINOR; |
8255 | | } |
8256 | | |
8257 | | if (output[1] != 0x0) { |
8258 | | output[0] = HYBRID_SA_MAJOR; |
8259 | | } |
8260 | | } |
8261 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8262 | | |
8263 | | static enum wc_MACAlgorithm GetNewSAHashAlgo(int typeIn) |
8264 | 0 | { |
8265 | 0 | switch (typeIn) { |
8266 | 0 | case RSA_PSS_RSAE_SHA256_MINOR: |
8267 | 0 | case RSA_PSS_PSS_SHA256_MINOR: |
8268 | 0 | case ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR: |
8269 | 0 | return sha256_mac; |
8270 | | |
8271 | 0 | case RSA_PSS_RSAE_SHA384_MINOR: |
8272 | 0 | case RSA_PSS_PSS_SHA384_MINOR: |
8273 | 0 | case ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR: |
8274 | 0 | return sha384_mac; |
8275 | | |
8276 | 0 | case RSA_PSS_RSAE_SHA512_MINOR: |
8277 | 0 | case RSA_PSS_PSS_SHA512_MINOR: |
8278 | 0 | case ED25519_SA_MINOR: |
8279 | 0 | case ED448_SA_MINOR: |
8280 | 0 | case ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR: |
8281 | 0 | return sha512_mac; |
8282 | 0 | default: |
8283 | 0 | return no_mac; |
8284 | 0 | } |
8285 | 0 | } |
8286 | | |
8287 | | /* Decode the signature algorithm. |
8288 | | * |
8289 | | * input The encoded signature algorithm. |
8290 | | * hashalgo The hash algorithm. |
8291 | | * hsType The signature type. |
8292 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8293 | | */ |
8294 | | static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo, |
8295 | | byte* hsType) |
8296 | 0 | { |
8297 | 0 | int ret = 0; |
8298 | |
|
8299 | 0 | switch (input[0]) { |
8300 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8301 | 0 | case SM2_SA_MAJOR: |
8302 | 0 | if (input[1] == SM2_SA_MINOR) { |
8303 | 0 | *hsType = sm2_sa_algo; |
8304 | 0 | *hashAlgo = sm3_mac; |
8305 | 0 | } |
8306 | 0 | else |
8307 | 0 | ret = INVALID_PARAMETER; |
8308 | 0 | break; |
8309 | 0 | #endif |
8310 | 0 | case NEW_SA_MAJOR: |
8311 | 0 | *hashAlgo = GetNewSAHashAlgo(input[1]); |
8312 | | |
8313 | | /* PSS encryption: 0x080[4-6] */ |
8314 | 0 | if (input[1] >= RSA_PSS_RSAE_SHA256_MINOR && |
8315 | 0 | input[1] <= RSA_PSS_RSAE_SHA512_MINOR) { |
8316 | 0 | *hsType = input[0]; |
8317 | 0 | } |
8318 | | /* PSS signature: 0x080[9-B] */ |
8319 | 0 | else if (input[1] >= RSA_PSS_PSS_SHA256_MINOR && |
8320 | 0 | input[1] <= RSA_PSS_PSS_SHA512_MINOR) { |
8321 | 0 | *hsType = input[0]; |
8322 | 0 | } |
8323 | 0 | #ifdef HAVE_ED25519 |
8324 | | /* ED25519: 0x0807 */ |
8325 | 0 | else if (input[1] == ED25519_SA_MINOR) { |
8326 | 0 | *hsType = ed25519_sa_algo; |
8327 | | /* Hash performed as part of sign/verify operation. */ |
8328 | 0 | } |
8329 | 0 | #endif |
8330 | 0 | #ifdef HAVE_ED448 |
8331 | | /* ED448: 0x0808 */ |
8332 | 0 | else if (input[1] == ED448_SA_MINOR) { |
8333 | 0 | *hsType = ed448_sa_algo; |
8334 | | /* Hash performed as part of sign/verify operation. */ |
8335 | 0 | } |
8336 | 0 | #endif |
8337 | 0 | #ifdef HAVE_ECC_BRAINPOOL |
8338 | 0 | else if ((input[1] == ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR) || |
8339 | 0 | (input[1] == ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR) || |
8340 | 0 | (input[1] == ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR)) { |
8341 | 0 | *hsType = ecc_dsa_sa_algo; |
8342 | 0 | } |
8343 | 0 | #endif |
8344 | 0 | else |
8345 | 0 | ret = INVALID_PARAMETER; |
8346 | 0 | break; |
8347 | | #if defined(HAVE_FALCON) |
8348 | | case FALCON_SA_MAJOR: |
8349 | | if (input[1] == FALCON_LEVEL1_SA_MINOR) { |
8350 | | *hsType = falcon_level1_sa_algo; |
8351 | | /* Hash performed as part of sign/verify operation. */ |
8352 | | *hashAlgo = sha512_mac; |
8353 | | } else if (input[1] == FALCON_LEVEL5_SA_MINOR) { |
8354 | | *hsType = falcon_level5_sa_algo; |
8355 | | /* Hash performed as part of sign/verify operation. */ |
8356 | | *hashAlgo = sha512_mac; |
8357 | | } |
8358 | | else |
8359 | | ret = INVALID_PARAMETER; |
8360 | | break; |
8361 | | #endif /* HAVE_FALCON */ |
8362 | | #if defined(HAVE_DILITHIUM) |
8363 | | case DILITHIUM_SA_MAJOR: |
8364 | | if (input[1] == DILITHIUM_LEVEL2_SA_MINOR) { |
8365 | | *hsType = dilithium_level2_sa_algo; |
8366 | | /* Hash performed as part of sign/verify operation. */ |
8367 | | *hashAlgo = sha512_mac; |
8368 | | } else if (input[1] == DILITHIUM_LEVEL3_SA_MINOR) { |
8369 | | *hsType = dilithium_level3_sa_algo; |
8370 | | /* Hash performed as part of sign/verify operation. */ |
8371 | | *hashAlgo = sha512_mac; |
8372 | | } else if (input[1] == DILITHIUM_LEVEL5_SA_MINOR) { |
8373 | | *hsType = dilithium_level5_sa_algo; |
8374 | | /* Hash performed as part of sign/verify operation. */ |
8375 | | *hashAlgo = sha512_mac; |
8376 | | } |
8377 | | else |
8378 | | { |
8379 | | ret = INVALID_PARAMETER; |
8380 | | } |
8381 | | break; |
8382 | | #endif /* HAVE_DILITHIUM */ |
8383 | 0 | default: |
8384 | 0 | *hashAlgo = input[0]; |
8385 | 0 | *hsType = input[1]; |
8386 | 0 | break; |
8387 | 0 | } |
8388 | | |
8389 | 0 | return ret; |
8390 | 0 | } |
8391 | | |
8392 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8393 | | /* Decode the hybrid signature algorithm. |
8394 | | * |
8395 | | * input The encoded signature algorithm. |
8396 | | * hashalgo The hash algorithm. |
8397 | | * hsType The signature type. |
8398 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8399 | | */ |
8400 | | static WC_INLINE int DecodeTls13HybridSigAlg(byte* input, byte* hashAlg, |
8401 | | byte *sigAlg, byte *altSigAlg) |
8402 | | { |
8403 | | |
8404 | | if (input[0] != HYBRID_SA_MAJOR) { |
8405 | | return INVALID_PARAMETER; |
8406 | | } |
8407 | | |
8408 | | if (input[1] == HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR) { |
8409 | | *sigAlg = ecc_dsa_sa_algo; |
8410 | | *hashAlg = sha256_mac; |
8411 | | *altSigAlg = dilithium_level2_sa_algo; |
8412 | | } |
8413 | | else if (input[1] == HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR) { |
8414 | | *sigAlg = rsa_pss_sa_algo; |
8415 | | *hashAlg = sha256_mac; |
8416 | | *altSigAlg = dilithium_level2_sa_algo; |
8417 | | } |
8418 | | else if (input[1] == HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR) { |
8419 | | *sigAlg = ecc_dsa_sa_algo; |
8420 | | *hashAlg = sha384_mac; |
8421 | | *altSigAlg = dilithium_level3_sa_algo; |
8422 | | } |
8423 | | else if (input[1] == HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR) { |
8424 | | *sigAlg = ecc_dsa_sa_algo; |
8425 | | *hashAlg = sha512_mac; |
8426 | | *altSigAlg = dilithium_level5_sa_algo; |
8427 | | } |
8428 | | else if (input[1] == HYBRID_P256_FALCON_LEVEL1_SA_MINOR) { |
8429 | | *sigAlg = ecc_dsa_sa_algo; |
8430 | | *hashAlg = sha256_mac; |
8431 | | *altSigAlg = falcon_level1_sa_algo; |
8432 | | } |
8433 | | else if (input[1] == HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR) { |
8434 | | *sigAlg = rsa_pss_sa_algo; |
8435 | | *hashAlg = sha256_mac; |
8436 | | *altSigAlg = falcon_level1_sa_algo; |
8437 | | } |
8438 | | else if (input[1] == HYBRID_P521_FALCON_LEVEL5_SA_MINOR) { |
8439 | | *sigAlg = ecc_dsa_sa_algo; |
8440 | | *hashAlg = sha512_mac; |
8441 | | *altSigAlg = falcon_level5_sa_algo; |
8442 | | } |
8443 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR) { |
8444 | | *sigAlg = dilithium_level2_sa_algo; |
8445 | | *hashAlg = sha256_mac; |
8446 | | *altSigAlg = ecc_dsa_sa_algo; |
8447 | | } |
8448 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR) { |
8449 | | *sigAlg = dilithium_level2_sa_algo; |
8450 | | *hashAlg = sha256_mac; |
8451 | | *altSigAlg = rsa_pss_sa_algo; |
8452 | | } |
8453 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR) { |
8454 | | *sigAlg = dilithium_level3_sa_algo; |
8455 | | *hashAlg = sha384_mac; |
8456 | | *altSigAlg = ecc_dsa_sa_algo; |
8457 | | } |
8458 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR) { |
8459 | | *sigAlg = dilithium_level5_sa_algo; |
8460 | | *hashAlg = sha512_mac; |
8461 | | *altSigAlg = ecc_dsa_sa_algo; |
8462 | | } |
8463 | | else if (input[1] == HYBRID_FALCON_LEVEL1_P256_SA_MINOR) { |
8464 | | *sigAlg = falcon_level1_sa_algo; |
8465 | | *hashAlg = sha256_mac; |
8466 | | *altSigAlg = ecc_dsa_sa_algo; |
8467 | | } |
8468 | | else if (input[1] == HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR) { |
8469 | | *sigAlg = falcon_level1_sa_algo; |
8470 | | *hashAlg = sha256_mac; |
8471 | | *altSigAlg = rsa_pss_sa_algo; |
8472 | | } |
8473 | | else if (input[1] == HYBRID_FALCON_LEVEL5_P521_SA_MINOR) { |
8474 | | *sigAlg = falcon_level5_sa_algo; |
8475 | | *hashAlg = sha512_mac; |
8476 | | *altSigAlg = ecc_dsa_sa_algo; |
8477 | | } |
8478 | | else { |
8479 | | return INVALID_PARAMETER; |
8480 | | } |
8481 | | |
8482 | | return 0; |
8483 | | } |
8484 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8485 | | |
8486 | | /* Get the hash of the messages so far. |
8487 | | * |
8488 | | * ssl The SSL/TLS object. |
8489 | | * hash The buffer to write the hash to. |
8490 | | * returns the length of the hash. |
8491 | | */ |
8492 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash) |
8493 | 0 | { |
8494 | 0 | int ret = 0; |
8495 | 0 | switch (ssl->specs.mac_algorithm) { |
8496 | 0 | #ifndef NO_SHA256 |
8497 | 0 | case sha256_mac: |
8498 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
8499 | 0 | if (ret == 0) |
8500 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
8501 | 0 | break; |
8502 | 0 | #endif /* !NO_SHA256 */ |
8503 | 0 | #ifdef WOLFSSL_SHA384 |
8504 | 0 | case sha384_mac: |
8505 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
8506 | 0 | if (ret == 0) |
8507 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
8508 | 0 | break; |
8509 | 0 | #endif /* WOLFSSL_SHA384 */ |
8510 | | #ifdef WOLFSSL_TLS13_SHA512 |
8511 | | case sha512_mac: |
8512 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
8513 | | if (ret == 0) |
8514 | | ret = WC_SHA512_DIGEST_SIZE; |
8515 | | break; |
8516 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
8517 | 0 | #ifdef WOLFSSL_SM3 |
8518 | 0 | case sm3_mac: |
8519 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
8520 | 0 | if (ret == 0) |
8521 | 0 | ret = WC_SM3_DIGEST_SIZE; |
8522 | 0 | break; |
8523 | 0 | #endif /* WOLFSSL_SM3 */ |
8524 | 0 | default: |
8525 | 0 | break; |
8526 | 0 | } |
8527 | 0 | return ret; |
8528 | 0 | } |
8529 | | |
8530 | | /* The server certificate verification label. */ |
8531 | | static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8532 | | "TLS 1.3, server CertificateVerify"; |
8533 | | /* The client certificate verification label. */ |
8534 | | static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8535 | | "TLS 1.3, client CertificateVerify"; |
8536 | | /* The prefix byte in the signature data. */ |
8537 | | #define SIGNING_DATA_PREFIX_BYTE 0x20 |
8538 | | |
8539 | | /* Create the signature data for TLS v1.3 certificate verification. |
8540 | | * |
8541 | | * ssl The SSL/TLS object. |
8542 | | * sigData The signature data. |
8543 | | * sigDataSz The length of the signature data. |
8544 | | * check Indicates this is a check not create. |
8545 | | */ |
8546 | | int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz, |
8547 | | int check) |
8548 | 507 | { |
8549 | 507 | word16 idx; |
8550 | 507 | int side = ssl->options.side; |
8551 | 507 | int ret; |
8552 | | |
8553 | | /* Signature Data = Prefix | Label | Handshake Hash */ |
8554 | 507 | XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ); |
8555 | 507 | idx = SIGNING_DATA_PREFIX_SZ; |
8556 | | |
8557 | 507 | if ((side == WOLFSSL_SERVER_END && check) || |
8558 | 507 | (side == WOLFSSL_CLIENT_END && !check)) { |
8559 | 0 | XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ); |
8560 | 0 | } |
8561 | 507 | if ((side == WOLFSSL_CLIENT_END && check) || |
8562 | 507 | (side == WOLFSSL_SERVER_END && !check)) { |
8563 | 507 | XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ); |
8564 | 507 | } |
8565 | 507 | idx += CERT_VFY_LABEL_SZ; |
8566 | | |
8567 | 507 | ret = GetMsgHash(ssl, &sigData[idx]); |
8568 | 507 | if (ret < 0) |
8569 | 4 | return ret; |
8570 | | |
8571 | 503 | *sigDataSz = (word16)(idx + ret); |
8572 | 503 | ret = 0; |
8573 | | |
8574 | 503 | return ret; |
8575 | 507 | } |
8576 | | |
8577 | | #ifndef NO_RSA |
8578 | | /* Encode the PKCS #1.5 RSA signature. |
8579 | | * |
8580 | | * sig The buffer to place the encoded signature into. |
8581 | | * sigData The data to be signed. |
8582 | | * sigDataSz The size of the data to be signed. |
8583 | | * hashAlgo The hash algorithm to use when signing. |
8584 | | * returns the length of the encoded signature or negative on error. |
8585 | | */ |
8586 | | int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz, |
8587 | | int sigAlgo, int hashAlgo) |
8588 | 501 | { |
8589 | 501 | Digest digest; |
8590 | 501 | int hashSz = 0; |
8591 | 501 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8592 | 501 | byte* hash; |
8593 | | |
8594 | 501 | (void)sigAlgo; |
8595 | | |
8596 | 501 | hash = sig; |
8597 | | |
8598 | | /* Digest the signature data. */ |
8599 | 501 | switch (hashAlgo) { |
8600 | 0 | #ifndef NO_SHA256 |
8601 | 326 | case sha256_mac: |
8602 | 326 | ret = wc_InitSha256(&digest.sha256); |
8603 | 326 | if (ret == 0) { |
8604 | 326 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8605 | 326 | if (ret == 0) |
8606 | 325 | ret = wc_Sha256Final(&digest.sha256, hash); |
8607 | 326 | wc_Sha256Free(&digest.sha256); |
8608 | 326 | } |
8609 | 326 | hashSz = WC_SHA256_DIGEST_SIZE; |
8610 | 326 | break; |
8611 | 0 | #endif |
8612 | 0 | #ifdef WOLFSSL_SHA384 |
8613 | 80 | case sha384_mac: |
8614 | 80 | ret = wc_InitSha384(&digest.sha384); |
8615 | 80 | if (ret == 0) { |
8616 | 80 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8617 | 80 | if (ret == 0) |
8618 | 79 | ret = wc_Sha384Final(&digest.sha384, hash); |
8619 | 80 | wc_Sha384Free(&digest.sha384); |
8620 | 80 | } |
8621 | 80 | hashSz = WC_SHA384_DIGEST_SIZE; |
8622 | 80 | break; |
8623 | 0 | #endif |
8624 | 0 | #ifdef WOLFSSL_SHA512 |
8625 | 95 | case sha512_mac: |
8626 | 95 | ret = wc_InitSha512(&digest.sha512); |
8627 | 95 | if (ret == 0) { |
8628 | 95 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8629 | 95 | if (ret == 0) |
8630 | 94 | ret = wc_Sha512Final(&digest.sha512, hash); |
8631 | 95 | wc_Sha512Free(&digest.sha512); |
8632 | 95 | } |
8633 | 95 | hashSz = WC_SHA512_DIGEST_SIZE; |
8634 | 95 | break; |
8635 | 0 | #endif |
8636 | 0 | default: |
8637 | 0 | ret = BAD_FUNC_ARG; |
8638 | 0 | break; |
8639 | | |
8640 | 501 | } |
8641 | | |
8642 | 501 | if (ret != 0) |
8643 | 6 | return ret; |
8644 | | |
8645 | 495 | return hashSz; |
8646 | 501 | } |
8647 | | #endif /* !NO_RSA */ |
8648 | | |
8649 | | #ifdef HAVE_ECC |
8650 | | /* Encode the ECC signature. |
8651 | | * |
8652 | | * sigData The data to be signed. |
8653 | | * sigDataSz The size of the data to be signed. |
8654 | | * hashAlgo The hash algorithm to use when signing. |
8655 | | * returns the length of the encoded signature or negative on error. |
8656 | | */ |
8657 | | static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo) |
8658 | 0 | { |
8659 | 0 | Digest digest; |
8660 | 0 | int hashSz = 0; |
8661 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
8662 | | |
8663 | | /* Digest the signature data. */ |
8664 | 0 | switch (hashAlgo) { |
8665 | 0 | #ifndef NO_SHA256 |
8666 | 0 | case sha256_mac: |
8667 | 0 | ret = wc_InitSha256(&digest.sha256); |
8668 | 0 | if (ret == 0) { |
8669 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
8670 | 0 | if (ret == 0) |
8671 | 0 | ret = wc_Sha256Final(&digest.sha256, sigData); |
8672 | 0 | wc_Sha256Free(&digest.sha256); |
8673 | 0 | } |
8674 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
8675 | 0 | break; |
8676 | 0 | #endif |
8677 | 0 | #ifdef WOLFSSL_SHA384 |
8678 | 0 | case sha384_mac: |
8679 | 0 | ret = wc_InitSha384(&digest.sha384); |
8680 | 0 | if (ret == 0) { |
8681 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
8682 | 0 | if (ret == 0) |
8683 | 0 | ret = wc_Sha384Final(&digest.sha384, sigData); |
8684 | 0 | wc_Sha384Free(&digest.sha384); |
8685 | 0 | } |
8686 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
8687 | 0 | break; |
8688 | 0 | #endif |
8689 | 0 | #ifdef WOLFSSL_SHA512 |
8690 | 0 | case sha512_mac: |
8691 | 0 | ret = wc_InitSha512(&digest.sha512); |
8692 | 0 | if (ret == 0) { |
8693 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
8694 | 0 | if (ret == 0) |
8695 | 0 | ret = wc_Sha512Final(&digest.sha512, sigData); |
8696 | 0 | wc_Sha512Free(&digest.sha512); |
8697 | 0 | } |
8698 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
8699 | 0 | break; |
8700 | 0 | #endif |
8701 | 0 | default: |
8702 | 0 | ret = BAD_FUNC_ARG; |
8703 | 0 | break; |
8704 | 0 | } |
8705 | | |
8706 | 0 | if (ret != 0) |
8707 | 0 | return ret; |
8708 | | |
8709 | 0 | return hashSz; |
8710 | 0 | } |
8711 | | #endif /* HAVE_ECC */ |
8712 | | |
8713 | | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
8714 | | /* Check that the decrypted signature matches the encoded signature |
8715 | | * based on the digest of the signature data. |
8716 | | * |
8717 | | * ssl The SSL/TLS object. |
8718 | | * sigAlgo The signature algorithm used to generate signature. |
8719 | | * hashAlgo The hash algorithm used to generate signature. |
8720 | | * decSig The decrypted signature. |
8721 | | * decSigSz The size of the decrypted signature. |
8722 | | * returns 0 on success, otherwise failure. |
8723 | | */ |
8724 | | static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo, |
8725 | | byte* decSig, word32 decSigSz) |
8726 | 0 | { |
8727 | 0 | int ret = 0; |
8728 | 0 | byte sigData[MAX_SIG_DATA_SZ]; |
8729 | 0 | word16 sigDataSz; |
8730 | |
|
8731 | 0 | ret = CreateSigData(ssl, sigData, &sigDataSz, 1); |
8732 | 0 | if (ret != 0) |
8733 | 0 | return ret; |
8734 | | |
8735 | 0 | if (sigAlgo == rsa_pss_sa_algo) { |
8736 | 0 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
8737 | 0 | word32 sigSz; |
8738 | |
|
8739 | 0 | ret = ConvertHashPss(hashAlgo, &hashType, NULL); |
8740 | 0 | if (ret < 0) |
8741 | 0 | return ret; |
8742 | | |
8743 | | /* PSS signature can be done in-place */ |
8744 | 0 | ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz, |
8745 | 0 | sigAlgo, hashAlgo); |
8746 | 0 | if (ret < 0) |
8747 | 0 | return ret; |
8748 | 0 | sigSz = (word32)ret; |
8749 | |
|
8750 | 0 | ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz, |
8751 | 0 | hashType); |
8752 | 0 | } |
8753 | | |
8754 | 0 | return ret; |
8755 | 0 | } |
8756 | | #endif /* !NO_RSA && WC_RSA_PSS */ |
8757 | | #endif /* !NO_RSA || HAVE_ECC */ |
8758 | | |
8759 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
8760 | | /* Get the next certificate from the list for writing into the TLS v1.3 |
8761 | | * Certificate message. |
8762 | | * |
8763 | | * data The certificate list. |
8764 | | * length The length of the certificate data in the list. |
8765 | | * idx The index of the next certificate. |
8766 | | * returns the length of the certificate data. 0 indicates no more certificates |
8767 | | * in the list. |
8768 | | */ |
8769 | | static word32 NextCert(byte* data, word32 length, word32* idx) |
8770 | 0 | { |
8771 | 0 | word32 len; |
8772 | | |
8773 | | /* Would index read past end of list? */ |
8774 | 0 | if (*idx + 3 > length) |
8775 | 0 | return 0; |
8776 | | |
8777 | | /* Length of the current ASN.1 encoded certificate. */ |
8778 | 0 | c24to32(data + *idx, &len); |
8779 | | /* Include the length field. */ |
8780 | 0 | len += 3; |
8781 | | |
8782 | | /* Ensure len does not overrun certificate list */ |
8783 | 0 | if (*idx + len > length) |
8784 | 0 | return 0; |
8785 | | |
8786 | | /* Move index to next certificate and return the current certificate's |
8787 | | * length. |
8788 | | */ |
8789 | 0 | *idx += len; |
8790 | 0 | return len; |
8791 | 0 | } |
8792 | | |
8793 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8794 | | /* Write certificate status request into certificate to buffer. |
8795 | | * |
8796 | | * ssl SSL/TLS object. |
8797 | | * certExts DerBuffer array. buffers written |
8798 | | * extSz word32 array. |
8799 | | * Length of the certificate status request data for the certificate. |
8800 | | * extSz_num number of the CSR written |
8801 | | * extIdx The index number of certificate status request data |
8802 | | * for the certificate. |
8803 | | * offset index offset |
8804 | | * returns Total number of bytes written. |
8805 | | */ |
8806 | | static int WriteCSRToBuffer(WOLFSSL* ssl, DerBuffer** certExts, |
8807 | | word16* extSz, word16 extSz_num) |
8808 | | { |
8809 | | int ret = 0; |
8810 | | TLSX* ext; |
8811 | | CertificateStatusRequest* csr; |
8812 | | word32 ex_offset = HELLO_EXT_TYPE_SZ + OPAQUE16_LEN /* extension type */ |
8813 | | + OPAQUE16_LEN /* extension length */; |
8814 | | word32 totalSz = 0; |
8815 | | word32 tmpSz; |
8816 | | word32 extIdx; |
8817 | | DerBuffer* der; |
8818 | | |
8819 | | ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
8820 | | csr = ext ? (CertificateStatusRequest*)ext->data : NULL; |
8821 | | |
8822 | | if (csr) { |
8823 | | for (extIdx = 0; extIdx < (word16)(extSz_num); extIdx++) { |
8824 | | tmpSz = TLSX_CSR_GetSize_ex(csr, 0, (int)extIdx); |
8825 | | |
8826 | | if (tmpSz > (OPAQUE8_LEN + OPAQUE24_LEN) && |
8827 | | certExts[extIdx] == NULL) { |
8828 | | /* csr extension is not zero */ |
8829 | | if (tmpSz > WOLFSSL_MAX_16BIT) |
8830 | | return BUFFER_E; |
8831 | | extSz[extIdx] = (word16)tmpSz; |
8832 | | |
8833 | | ret = AllocDer(&certExts[extIdx], extSz[extIdx] + ex_offset, |
8834 | | CERT_TYPE, ssl->heap); |
8835 | | if (ret < 0) |
8836 | | return ret; |
8837 | | der = certExts[extIdx]; |
8838 | | |
8839 | | /* write extension type */ |
8840 | | c16toa(ext->type, der->buffer |
8841 | | + OPAQUE16_LEN); |
8842 | | /* writes extension data length. */ |
8843 | | c16toa(extSz[extIdx], der->buffer |
8844 | | + HELLO_EXT_TYPE_SZ + OPAQUE16_LEN); |
8845 | | /* write extension data */ |
8846 | | extSz[extIdx] = (word16)TLSX_CSR_Write_ex(csr, |
8847 | | der->buffer + ex_offset, 0, extIdx); |
8848 | | /* add extension offset */ |
8849 | | extSz[extIdx] += (word16)ex_offset; |
8850 | | /* extension length */ |
8851 | | c16toa(extSz[extIdx] - OPAQUE16_LEN, |
8852 | | der->buffer); |
8853 | | } |
8854 | | totalSz += extSz[extIdx]; |
8855 | | } |
8856 | | } |
8857 | | else { |
8858 | | /* chain cert empty extension size */ |
8859 | | totalSz += OPAQUE16_LEN * extSz_num; |
8860 | | } |
8861 | | return (int)totalSz; |
8862 | | } |
8863 | | #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */ |
8864 | | /* Add certificate data and empty extension to output up to the fragment size. |
8865 | | * |
8866 | | * ssl SSL/TLS object. |
8867 | | * cert The certificate data to write out. |
8868 | | * len The length of the certificate data. |
8869 | | * extSz Length of the extension data with the certificate. |
8870 | | * idx The start of the certificate data to write out. |
8871 | | * fragSz The maximum size of this fragment. |
8872 | | * output The buffer to write to. |
8873 | | * extIdx The index number of the extension data with the certificate |
8874 | | * returns the number of bytes written. |
8875 | | */ |
8876 | | static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz, |
8877 | | word32 idx, word32 fragSz, byte* output, word16 extIdx) |
8878 | 532 | { |
8879 | 532 | word32 i = 0; |
8880 | 532 | word32 copySz = min(len - idx, fragSz); |
8881 | | |
8882 | 532 | if (idx < len) { |
8883 | 532 | XMEMCPY(output, cert + idx, copySz); |
8884 | 532 | i = copySz; |
8885 | 532 | if (copySz == fragSz) |
8886 | 0 | return i; |
8887 | 532 | } |
8888 | 532 | copySz = len + extSz - idx - i; |
8889 | | |
8890 | 532 | if (extSz == OPAQUE16_LEN) { |
8891 | 532 | if (copySz <= fragSz) { |
8892 | | /* Empty extension */ |
8893 | 532 | output[i++] = 0; |
8894 | 532 | output[i++] = 0; |
8895 | 532 | } |
8896 | 532 | } |
8897 | 0 | else { |
8898 | 0 | byte* certExts = ssl->buffers.certExts[extIdx]->buffer + idx + i - len; |
8899 | | /* Put out as much of the extensions' data as will fit in fragment. */ |
8900 | 0 | if (copySz > fragSz - i) |
8901 | 0 | copySz = fragSz - i; |
8902 | 0 | XMEMCPY(output + i, certExts, copySz); |
8903 | 0 | i += copySz; |
8904 | 0 | } |
8905 | | |
8906 | 532 | return i; |
8907 | 532 | } |
8908 | | |
8909 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
8910 | | static int SetupOcspResp(WOLFSSL* ssl) |
8911 | | { |
8912 | | DecodedCert* cert = NULL; |
8913 | | CertificateStatusRequest* csr = NULL; |
8914 | | TLSX* extension = NULL; |
8915 | | int ret = 0; |
8916 | | OcspRequest* request = NULL; |
8917 | | |
8918 | | extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
8919 | | if (extension == NULL) |
8920 | | return 0; /* peer didn't signal ocsp support */ |
8921 | | csr = (CertificateStatusRequest*)extension->data; |
8922 | | if (csr == NULL) |
8923 | | return MEMORY_ERROR; |
8924 | | |
8925 | | if (SSL_CM(ssl) != NULL && |
8926 | | SSL_CM(ssl)->ocsp_stapling != NULL && |
8927 | | SSL_CM(ssl)->ocsp_stapling->statusCb != NULL) { |
8928 | | return TLSX_CSR_SetResponseWithStatusCB(ssl); |
8929 | | } |
8930 | | |
8931 | | if (ssl->buffers.certificate == NULL) { |
8932 | | WOLFSSL_MSG("Certificate buffer not set!"); |
8933 | | return BUFFER_ERROR; |
8934 | | } |
8935 | | cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap, |
8936 | | DYNAMIC_TYPE_DCERT); |
8937 | | if (cert == NULL) { |
8938 | | return MEMORY_E; |
8939 | | } |
8940 | | InitDecodedCert(cert, ssl->buffers.certificate->buffer, |
8941 | | ssl->buffers.certificate->length, ssl->heap); |
8942 | | ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, SSL_CM(ssl)); |
8943 | | if (ret != 0) { |
8944 | | FreeDecodedCert(cert); |
8945 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
8946 | | return ret; |
8947 | | } |
8948 | | ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap); |
8949 | | FreeDecodedCert(cert); |
8950 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
8951 | | if (ret != 0 ) |
8952 | | return ret; |
8953 | | |
8954 | | request = &csr->request.ocsp[0]; |
8955 | | ret = CreateOcspResponse(ssl, &request, &csr->responses[0]); |
8956 | | if (request != &csr->request.ocsp[0] && |
8957 | | ssl->buffers.weOwnCert) { |
8958 | | /* request will be allocated in CreateOcspResponse() */ |
8959 | | FreeOcspRequest(request); |
8960 | | XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
8961 | | } |
8962 | | if (ret != 0) |
8963 | | return ret; |
8964 | | |
8965 | | if (csr->responses[0].buffer) |
8966 | | extension->resp = 1; |
8967 | | #if defined(WOLFSSL_TLS_OCSP_MULTI) |
8968 | | /* process OCSP request in certificate chain */ |
8969 | | if ((ret = ProcessChainOCSPRequest(ssl)) != 0) { |
8970 | | WOLFSSL_MSG("Process Cert Chain OCSP request failed"); |
8971 | | WOLFSSL_ERROR_VERBOSE(ret); |
8972 | | return ret; |
8973 | | } |
8974 | | #endif |
8975 | | return ret; |
8976 | | } |
8977 | | #endif |
8978 | | |
8979 | | /* handle generation TLS v1.3 certificate (11) */ |
8980 | | /* Send the certificate for this end and any CAs that help with validation. |
8981 | | * This message is always encrypted in TLS v1.3. |
8982 | | * |
8983 | | * ssl The SSL/TLS object. |
8984 | | * returns 0 on success, otherwise failure. |
8985 | | */ |
8986 | | static int SendTls13Certificate(WOLFSSL* ssl) |
8987 | | { |
8988 | | int ret = 0; |
8989 | | word32 certSz, certChainSz, headerSz, listSz, payloadSz; |
8990 | | word16 extSz[MAX_CERT_EXTENSIONS]; |
8991 | | word16 extIdx = 0; |
8992 | | word32 maxFragment; |
8993 | | word32 totalextSz = 0; |
8994 | | word32 len = 0; |
8995 | | word32 idx = 0; |
8996 | | word32 offset = OPAQUE16_LEN; |
8997 | | byte* p = NULL; |
8998 | | byte certReqCtxLen = 0; |
8999 | | sword32 length; |
9000 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9001 | | byte* certReqCtx = NULL; |
9002 | | #endif |
9003 | | |
9004 | | #ifdef OPENSSL_EXTRA |
9005 | | WOLFSSL_X509* x509 = NULL; |
9006 | | WOLFSSL_EVP_PKEY* pkey = NULL; |
9007 | | #endif |
9008 | | |
9009 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND); |
9010 | | WOLFSSL_ENTER("SendTls13Certificate"); |
9011 | | |
9012 | | XMEMSET(extSz, 0, sizeof(extSz)); |
9013 | | |
9014 | | ssl->options.buildingMsg = 1; |
9015 | | |
9016 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9017 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9018 | | certReqCtxLen = ssl->certReqCtx->len; |
9019 | | certReqCtx = &ssl->certReqCtx->ctx; |
9020 | | } |
9021 | | #endif |
9022 | | |
9023 | | #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_SETUP_CB) |
9024 | | /* call client cert callback if no cert has been loaded */ |
9025 | | if ((ssl->ctx->CBClientCert != NULL) && |
9026 | | (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) { |
9027 | | ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey); |
9028 | | if (ret == 1) { |
9029 | | if ((wolfSSL_CTX_use_certificate(ssl->ctx, x509) == WOLFSSL_SUCCESS) && |
9030 | | (wolfSSL_CTX_use_PrivateKey(ssl->ctx, pkey) == WOLFSSL_SUCCESS)) { |
9031 | | ssl->options.sendVerify = SEND_CERT; |
9032 | | } |
9033 | | wolfSSL_X509_free(x509); |
9034 | | x509 = NULL; |
9035 | | wolfSSL_EVP_PKEY_free(pkey); |
9036 | | } |
9037 | | } |
9038 | | #endif |
9039 | | |
9040 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9041 | | certSz = 0; |
9042 | | certChainSz = 0; |
9043 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ; |
9044 | | length = (sword32)headerSz; |
9045 | | listSz = 0; |
9046 | | } |
9047 | | else { |
9048 | | if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) { |
9049 | | WOLFSSL_MSG("Send Cert missing certificate buffer"); |
9050 | | return NO_CERT_ERROR; |
9051 | | } |
9052 | | /* Certificate Data */ |
9053 | | certSz = ssl->buffers.certificate->length; |
9054 | | /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */ |
9055 | | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ + |
9056 | | CERT_HEADER_SZ; |
9057 | | /* set empty extension as default */ |
9058 | | for (extIdx = 0; extIdx < (word16)XELEM_CNT(extSz); extIdx++) |
9059 | | extSz[extIdx] = OPAQUE16_LEN; |
9060 | | |
9061 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9062 | | /* We only send CSR on the server side. On client side, the CSR data |
9063 | | * is populated with the server response. We would be sending the server |
9064 | | * its own stapling data. */ |
9065 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
9066 | | ret = SetupOcspResp(ssl); |
9067 | | if (ret != 0) |
9068 | | return ret; |
9069 | | |
9070 | | ret = WriteCSRToBuffer(ssl, &ssl->buffers.certExts[0], &extSz[0], |
9071 | | 1 /* +1 for leaf */ + (word16)ssl->buffers.certChainCnt); |
9072 | | if (ret < 0) |
9073 | | return ret; |
9074 | | totalextSz += ret; |
9075 | | ret = 0; /* Clear to signal no error */ |
9076 | | } |
9077 | | else |
9078 | | #endif |
9079 | | { |
9080 | | /* Leaf cert empty extension size */ |
9081 | | totalextSz += OPAQUE16_LEN; |
9082 | | /* chain cert empty extension size */ |
9083 | | totalextSz += OPAQUE16_LEN * ssl->buffers.certChainCnt; |
9084 | | } |
9085 | | |
9086 | | /* Length of message data with one certificate and extensions. */ |
9087 | | length = (sword32)(headerSz + certSz + totalextSz); |
9088 | | /* Length of list data with one certificate and extensions. */ |
9089 | | listSz = CERT_HEADER_SZ + certSz + totalextSz; |
9090 | | |
9091 | | /* Send rest of chain if sending cert (chain has leading size/s). */ |
9092 | | if (certSz > 0 && ssl->buffers.certChainCnt > 0) { |
9093 | | p = ssl->buffers.certChain->buffer; |
9094 | | /* Chain length including extensions. */ |
9095 | | certChainSz = ssl->buffers.certChain->length; |
9096 | | |
9097 | | length += certChainSz; |
9098 | | listSz += certChainSz; |
9099 | | } |
9100 | | else |
9101 | | certChainSz = 0; |
9102 | | } |
9103 | | |
9104 | | payloadSz = (word32)length; |
9105 | | |
9106 | | if (ssl->fragOffset != 0) |
9107 | | length -= (ssl->fragOffset + headerSz); |
9108 | | |
9109 | | maxFragment = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
9110 | | |
9111 | | extIdx = 0; |
9112 | | |
9113 | | while (length > 0 && ret == 0) { |
9114 | | byte* output = NULL; |
9115 | | word32 fragSz = 0; |
9116 | | word32 i = RECORD_HEADER_SZ; |
9117 | | int sendSz = RECORD_HEADER_SZ; |
9118 | | |
9119 | | #ifdef WOLFSSL_DTLS13 |
9120 | | if (ssl->options.dtls) { |
9121 | | i = Dtls13GetRlHeaderLength(ssl, 1); |
9122 | | sendSz = (int)i; |
9123 | | } |
9124 | | #endif /* WOLFSSL_DTLS13 */ |
9125 | | |
9126 | | if (ssl->fragOffset == 0) { |
9127 | | if (headerSz + certSz + totalextSz + certChainSz <= |
9128 | | maxFragment - HANDSHAKE_HEADER_SZ) { |
9129 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
9130 | | } |
9131 | | #ifdef WOLFSSL_DTLS13 |
9132 | | else if (ssl->options.dtls){ |
9133 | | /* short-circuit the fragmentation logic here. DTLS |
9134 | | fragmentation will be done in dtls13HandshakeSend() */ |
9135 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
9136 | | } |
9137 | | #endif /* WOLFSSL_DTLS13 */ |
9138 | | else { |
9139 | | fragSz = maxFragment - HANDSHAKE_HEADER_SZ; |
9140 | | } |
9141 | | |
9142 | | sendSz += fragSz + HANDSHAKE_HEADER_SZ; |
9143 | | i += HANDSHAKE_HEADER_SZ; |
9144 | | #ifdef WOLFSSL_DTLS13 |
9145 | | if (ssl->options.dtls) { |
9146 | | sendSz += DTLS_HANDSHAKE_EXTRA; |
9147 | | i += DTLS_HANDSHAKE_EXTRA; |
9148 | | } |
9149 | | #endif /* WOLFSSL_DTLS13 */ |
9150 | | } |
9151 | | else { |
9152 | | fragSz = min((word32)length, maxFragment); |
9153 | | sendSz += fragSz; |
9154 | | } |
9155 | | |
9156 | | sendSz += MAX_MSG_EXTRA; |
9157 | | |
9158 | | /* Check buffers are big enough and grow if needed. */ |
9159 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
9160 | | return ret; |
9161 | | |
9162 | | /* Get position in output buffer to write new message to. */ |
9163 | | output = GetOutputBuffer(ssl); |
9164 | | |
9165 | | if (ssl->fragOffset == 0) { |
9166 | | AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl); |
9167 | | |
9168 | | /* Request context. */ |
9169 | | output[i++] = certReqCtxLen; |
9170 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9171 | | if (certReqCtxLen > 0) { |
9172 | | XMEMCPY(output + i, certReqCtx, certReqCtxLen); |
9173 | | i += certReqCtxLen; |
9174 | | } |
9175 | | #endif |
9176 | | length -= OPAQUE8_LEN + certReqCtxLen; |
9177 | | fragSz -= OPAQUE8_LEN + certReqCtxLen; |
9178 | | /* Certificate list length. */ |
9179 | | c32to24(listSz, output + i); |
9180 | | i += CERT_HEADER_SZ; |
9181 | | length -= CERT_HEADER_SZ; |
9182 | | fragSz -= CERT_HEADER_SZ; |
9183 | | /* Leaf certificate data length. */ |
9184 | | if (certSz > 0) { |
9185 | | c32to24(certSz, output + i); |
9186 | | i += CERT_HEADER_SZ; |
9187 | | length -= CERT_HEADER_SZ; |
9188 | | fragSz -= CERT_HEADER_SZ; |
9189 | | } |
9190 | | } |
9191 | | else |
9192 | | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
9193 | | |
9194 | | if (extIdx == 0) { |
9195 | | if (certSz > 0 && ssl->fragOffset < certSz + extSz[0]) { |
9196 | | /* Put in the leaf certificate with extensions. */ |
9197 | | word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer, |
9198 | | certSz, extSz[0], ssl->fragOffset, fragSz, |
9199 | | output + i, 0); |
9200 | | i += copySz; |
9201 | | ssl->fragOffset += copySz; |
9202 | | length -= copySz; |
9203 | | fragSz -= copySz; |
9204 | | if (ssl->fragOffset == certSz + extSz[0]) |
9205 | | FreeDer(&ssl->buffers.certExts[0]); |
9206 | | } |
9207 | | } |
9208 | | if (certChainSz > 0 && fragSz > 0) { |
9209 | | /* Put in the CA certificates with extensions. */ |
9210 | | while (fragSz > 0) { |
9211 | | word32 l; |
9212 | | |
9213 | | if (offset == len + OPAQUE16_LEN) { |
9214 | | /* Find next CA certificate to write out. */ |
9215 | | offset = 0; |
9216 | | /* Point to the start of current cert in chain buffer. */ |
9217 | | p = ssl->buffers.certChain->buffer + idx; |
9218 | | len = NextCert(ssl->buffers.certChain->buffer, |
9219 | | ssl->buffers.certChain->length, &idx); |
9220 | | if (len == 0) |
9221 | | break; |
9222 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
9223 | | !defined(NO_WOLFSSL_SERVER) |
9224 | | if (MAX_CERT_EXTENSIONS > extIdx) |
9225 | | extIdx++; |
9226 | | #endif |
9227 | | } |
9228 | | /* Write out certificate and extension. */ |
9229 | | l = AddCertExt(ssl, p, len, extSz[extIdx], offset, fragSz, |
9230 | | output + i, extIdx); |
9231 | | i += l; |
9232 | | ssl->fragOffset += l; |
9233 | | length -= l; |
9234 | | fragSz -= l; |
9235 | | offset += l; |
9236 | | |
9237 | | if (extIdx != 0 && extIdx < MAX_CERT_EXTENSIONS && |
9238 | | ssl->buffers.certExts[extIdx] != NULL && |
9239 | | offset == len + extSz[extIdx]) { |
9240 | | FreeDer(&ssl->buffers.certExts[extIdx]); |
9241 | | /* for next chain cert */ |
9242 | | len += extSz[extIdx] - OPAQUE16_LEN; |
9243 | | } |
9244 | | } |
9245 | | } |
9246 | | |
9247 | | if ((int)i - RECORD_HEADER_SZ < 0) { |
9248 | | WOLFSSL_MSG("Send Cert bad inputSz"); |
9249 | | return BUFFER_E; |
9250 | | } |
9251 | | |
9252 | | #ifdef WOLFSSL_DTLS13 |
9253 | | if (ssl->options.dtls) { |
9254 | | /* DTLS1.3 uses a separate variable and logic for fragments */ |
9255 | | ssl->options.buildingMsg = 0; |
9256 | | ssl->fragOffset = 0; |
9257 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
9258 | | certificate, 1); |
9259 | | } |
9260 | | else |
9261 | | #endif /* WOLFSSL_DTLS13 */ |
9262 | | { |
9263 | | /* This message is always encrypted. */ |
9264 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
9265 | | output + RECORD_HEADER_SZ, (int)(i - RECORD_HEADER_SZ), |
9266 | | handshake, 1, |
9267 | | 0, 0); |
9268 | | if (sendSz < 0) |
9269 | | return sendSz; |
9270 | | |
9271 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
9272 | | if (ssl->hsInfoOn) |
9273 | | AddPacketName(ssl, "Certificate"); |
9274 | | if (ssl->toInfoOn) { |
9275 | | ret = AddPacketInfo(ssl, "Certificate", handshake, output, |
9276 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
9277 | | if (ret != 0) |
9278 | | return ret; |
9279 | | } |
9280 | | #endif |
9281 | | |
9282 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
9283 | | ssl->options.buildingMsg = 0; |
9284 | | if (!ssl->options.groupMessages) |
9285 | | ret = SendBuffered(ssl); |
9286 | | } |
9287 | | } |
9288 | | |
9289 | | if (ret != WC_NO_ERR_TRACE(WANT_WRITE)) { |
9290 | | /* Clean up the fragment offset. */ |
9291 | | ssl->options.buildingMsg = 0; |
9292 | | ssl->fragOffset = 0; |
9293 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
9294 | | ssl->options.serverState = SERVER_CERT_COMPLETE; |
9295 | | } |
9296 | | |
9297 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9298 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9299 | | CertReqCtx* ctx = ssl->certReqCtx; |
9300 | | ssl->certReqCtx = ssl->certReqCtx->next; |
9301 | | XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
9302 | | } |
9303 | | #endif |
9304 | | |
9305 | | WOLFSSL_LEAVE("SendTls13Certificate", ret); |
9306 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND); |
9307 | | |
9308 | | return ret; |
9309 | | } |
9310 | | |
9311 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
9312 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
9313 | | defined(HAVE_DILITHIUM)) && \ |
9314 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
9315 | | typedef struct Scv13Args { |
9316 | | byte* output; /* not allocated */ |
9317 | | byte* verify; /* not allocated */ |
9318 | | word32 idx; |
9319 | | word32 sigLen; |
9320 | | int sendSz; |
9321 | | word16 length; |
9322 | | |
9323 | | byte sigAlgo; |
9324 | | byte* sigData; |
9325 | | word16 sigDataSz; |
9326 | | #ifndef NO_RSA |
9327 | | byte* toSign; /* not allocated */ |
9328 | | word32 toSignSz; |
9329 | | #endif |
9330 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9331 | | byte altSigAlgo; |
9332 | | word32 altSigLen; /* Only used in the case of both native and alt. */ |
9333 | | byte* altSigData; |
9334 | | word16 altSigDataSz; |
9335 | | #endif |
9336 | | } Scv13Args; |
9337 | | |
9338 | | static void FreeScv13Args(WOLFSSL* ssl, void* pArgs) |
9339 | 519 | { |
9340 | 519 | Scv13Args* args = (Scv13Args*)pArgs; |
9341 | | |
9342 | 519 | (void)ssl; |
9343 | | |
9344 | 519 | if (args && args->sigData) { |
9345 | 507 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9346 | 507 | args->sigData = NULL; |
9347 | 507 | } |
9348 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9349 | | if (args && args->altSigData != NULL) { |
9350 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9351 | | args->altSigData = NULL; |
9352 | | } |
9353 | | #endif |
9354 | 519 | } |
9355 | | |
9356 | | /* handle generation TLS v1.3 certificate_verify (15) */ |
9357 | | /* Send the TLS v1.3 CertificateVerify message. |
9358 | | * A hash of all the message so far is used. |
9359 | | * The signed data is: |
9360 | | * 0x20 * 64 | context string | 0x00 | hash of messages |
9361 | | * This message is always encrypted in TLS v1.3. |
9362 | | * |
9363 | | * ssl The SSL/TLS object. |
9364 | | * returns 0 on success, otherwise failure. |
9365 | | */ |
9366 | | static int SendTls13CertificateVerify(WOLFSSL* ssl) |
9367 | | { |
9368 | | int ret = 0; |
9369 | | #ifndef NO_RSA |
9370 | | /* Use this as a temporary buffer for RSA signature verification. */ |
9371 | | buffer* rsaSigBuf = &ssl->buffers.sig; |
9372 | | #endif |
9373 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9374 | | Scv13Args* args = NULL; |
9375 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
9376 | | #else |
9377 | | Scv13Args args[1]; |
9378 | | #endif |
9379 | | |
9380 | | #ifdef WOLFSSL_DTLS13 |
9381 | | int recordLayerHdrExtra; |
9382 | | #endif /* WOLFSSL_DTLS13 */ |
9383 | | |
9384 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9385 | | WOLFSSL_ENTER("SendTls13CertificateVerify"); |
9386 | | |
9387 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9388 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9389 | | #endif |
9390 | | |
9391 | | ssl->options.buildingMsg = 1; |
9392 | | |
9393 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
9394 | | ret = tsip_Tls13SendCertVerify(ssl); |
9395 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
9396 | | goto exit_scv; |
9397 | | } |
9398 | | ret = 0; |
9399 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
9400 | | |
9401 | | #ifdef WOLFSSL_DTLS13 |
9402 | | /* can be negative */ |
9403 | | if (ssl->options.dtls) |
9404 | | recordLayerHdrExtra = Dtls13GetRlHeaderLength(ssl, 1) - RECORD_HEADER_SZ; |
9405 | | else |
9406 | | recordLayerHdrExtra = 0; |
9407 | | |
9408 | | #endif /* WOLFSSL_DTLS13 */ |
9409 | | |
9410 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9411 | | if (ssl->async == NULL) { |
9412 | | ssl->async = (struct WOLFSSL_ASYNC*) |
9413 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
9414 | | DYNAMIC_TYPE_ASYNC); |
9415 | | if (ssl->async == NULL) |
9416 | | ERROR_OUT(MEMORY_E, exit_scv); |
9417 | | } |
9418 | | args = (Scv13Args*)ssl->async->args; |
9419 | | |
9420 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
9421 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
9422 | | /* Check for error */ |
9423 | | if (ret < 0) |
9424 | | goto exit_scv; |
9425 | | } |
9426 | | else |
9427 | | #endif |
9428 | | { |
9429 | | /* Reset state */ |
9430 | | ret = 0; |
9431 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
9432 | | XMEMSET(args, 0, sizeof(Scv13Args)); |
9433 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9434 | | ssl->async->freeArgs = FreeScv13Args; |
9435 | | #endif |
9436 | | } |
9437 | | |
9438 | | switch(ssl->options.asyncState) |
9439 | | { |
9440 | | case TLS_ASYNC_BEGIN: |
9441 | | { |
9442 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9443 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9444 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, |
9445 | | ssl->buffers.keyMask); |
9446 | | #endif |
9447 | | return 0; /* sent blank cert, can't verify */ |
9448 | | } |
9449 | | |
9450 | | args->sendSz = WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA; |
9451 | | /* Always encrypted. */ |
9452 | | args->sendSz += MAX_MSG_EXTRA; |
9453 | | |
9454 | | /* check for available size */ |
9455 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) { |
9456 | | goto exit_scv; |
9457 | | } |
9458 | | |
9459 | | /* get output buffer */ |
9460 | | args->output = GetOutputBuffer(ssl); |
9461 | | |
9462 | | /* Advance state and proceed */ |
9463 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
9464 | | } /* case TLS_ASYNC_BEGIN */ |
9465 | | FALL_THROUGH; |
9466 | | |
9467 | | case TLS_ASYNC_BUILD: |
9468 | | { |
9469 | | int rem = (int)(ssl->buffers.outputBuffer.bufferSize |
9470 | | - ssl->buffers.outputBuffer.length |
9471 | | - RECORD_HEADER_SZ - HANDSHAKE_HEADER_SZ); |
9472 | | |
9473 | | /* idx is used to track verify pointer offset to output */ |
9474 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
9475 | | args->verify = |
9476 | | &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ]; |
9477 | | |
9478 | | #ifdef WOLFSSL_DTLS13 |
9479 | | if (ssl->options.dtls) { |
9480 | | rem -= recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9481 | | args->idx += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9482 | | args->verify += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9483 | | } |
9484 | | #endif /* WOLFSSL_DTLS13 */ |
9485 | | |
9486 | | if (ssl->buffers.key == NULL) { |
9487 | | #ifdef HAVE_PK_CALLBACKS |
9488 | | if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) |
9489 | | args->sigLen = (word16)GetPrivateKeySigSize(ssl); |
9490 | | else |
9491 | | #endif |
9492 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9493 | | } |
9494 | | else { |
9495 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9496 | | if (ssl->sigSpec != NULL && |
9497 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
9498 | | /* In the case of alternative, we swap in the alt. */ |
9499 | | if (ssl->buffers.altKey == NULL) { |
9500 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9501 | | } |
9502 | | ssl->buffers.keyType = ssl->buffers.altKeyType; |
9503 | | ssl->buffers.keySz = ssl->buffers.altKeySz; |
9504 | | /* If we own it, free key before overriding it. */ |
9505 | | if (ssl->buffers.weOwnKey) { |
9506 | | FreeDer(&ssl->buffers.key); |
9507 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9508 | | FreeDer(&ssl->buffers.keyMask); |
9509 | | #endif |
9510 | | } |
9511 | | |
9512 | | /* Swap keys */ |
9513 | | ssl->buffers.key = ssl->buffers.altKey; |
9514 | | ssl->buffers.weOwnKey = ssl->buffers.weOwnAltKey; |
9515 | | |
9516 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9517 | | ssl->buffers.keyMask = ssl->buffers.altKeyMask; |
9518 | | /* Unblind the alternative key before decoding */ |
9519 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9520 | | #endif |
9521 | | } |
9522 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9523 | | ret = DecodePrivateKey(ssl, &args->sigLen); |
9524 | | if (ret != 0) |
9525 | | goto exit_scv; |
9526 | | } |
9527 | | |
9528 | | if (rem < 0 || (int)args->sigLen > rem) { |
9529 | | ERROR_OUT(BUFFER_E, exit_scv); |
9530 | | } |
9531 | | |
9532 | | if (args->sigLen == 0) { |
9533 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9534 | | } |
9535 | | |
9536 | | /* Add signature algorithm. */ |
9537 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) |
9538 | | args->sigAlgo = rsa_pss_sa_algo; |
9539 | | #ifdef HAVE_ECC |
9540 | | else if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9541 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9542 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9543 | | args->sigAlgo = sm2_sa_algo; |
9544 | | } |
9545 | | else |
9546 | | #endif |
9547 | | { |
9548 | | args->sigAlgo = ecc_dsa_sa_algo; |
9549 | | } |
9550 | | } |
9551 | | #endif |
9552 | | #ifdef HAVE_ED25519 |
9553 | | else if (ssl->hsType == DYNAMIC_TYPE_ED25519) |
9554 | | args->sigAlgo = ed25519_sa_algo; |
9555 | | #endif |
9556 | | #ifdef HAVE_ED448 |
9557 | | else if (ssl->hsType == DYNAMIC_TYPE_ED448) |
9558 | | args->sigAlgo = ed448_sa_algo; |
9559 | | #endif |
9560 | | #if defined(HAVE_FALCON) |
9561 | | else if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9562 | | args->sigAlgo = ssl->buffers.keyType; |
9563 | | } |
9564 | | #endif /* HAVE_FALCON */ |
9565 | | #if defined(HAVE_DILITHIUM) |
9566 | | else if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9567 | | args->sigAlgo = ssl->buffers.keyType; |
9568 | | } |
9569 | | #endif /* HAVE_DILITHIUM */ |
9570 | | else { |
9571 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9572 | | } |
9573 | | |
9574 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9575 | | if (ssl->peerSigSpec == NULL) { |
9576 | | /* The peer did not respond. We didn't send CKS or they don't |
9577 | | * support it. Either way, we do not need to handle dual |
9578 | | * key/sig case. */ |
9579 | | ssl->sigSpec = NULL; |
9580 | | ssl->sigSpecSz = 0; |
9581 | | } |
9582 | | |
9583 | | if (ssl->sigSpec != NULL && |
9584 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9585 | | /* The native was already decoded. Now we need to do the |
9586 | | * alternative. Note that no swap was done because this case is |
9587 | | * both native and alternative, not just alternative. */ |
9588 | | if (ssl->buffers.altKey == NULL) { |
9589 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9590 | | } |
9591 | | |
9592 | | /* After this call, args->altSigLen has the length we need for |
9593 | | * the alternative signature. */ |
9594 | | ret = DecodeAltPrivateKey(ssl, &args->altSigLen); |
9595 | | if (ret != 0) |
9596 | | goto exit_scv; |
9597 | | |
9598 | | if (ssl->buffers.altKeyType == ecc_dsa_sa_algo || |
9599 | | ssl->buffers.altKeyType == falcon_level1_sa_algo || |
9600 | | ssl->buffers.altKeyType == falcon_level5_sa_algo || |
9601 | | ssl->buffers.altKeyType == dilithium_level2_sa_algo || |
9602 | | ssl->buffers.altKeyType == dilithium_level3_sa_algo || |
9603 | | ssl->buffers.altKeyType == dilithium_level5_sa_algo) { |
9604 | | args->altSigAlgo = ssl->buffers.altKeyType; |
9605 | | } |
9606 | | else if (ssl->buffers.altKeyType == rsa_sa_algo && |
9607 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9608 | | args->altSigAlgo = rsa_pss_sa_algo; |
9609 | | } |
9610 | | else { |
9611 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9612 | | } |
9613 | | |
9614 | | EncodeDualSigAlg(args->sigAlgo, args->altSigAlgo, args->verify); |
9615 | | if (args->verify[0] == 0) { |
9616 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
9617 | | } |
9618 | | } |
9619 | | else |
9620 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9621 | | EncodeSigAlg(ssl, ssl->options.hashAlgo, args->sigAlgo, |
9622 | | args->verify); |
9623 | | |
9624 | | if (args->sigData == NULL) { |
9625 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9626 | | if ((ssl->hsType == DYNAMIC_TYPE_RSA) && |
9627 | | (args->sigLen > MAX_SIG_DATA_SZ)) { |
9628 | | /* We store the RSA signature in the sigData buffer |
9629 | | * temporarily, hence its size must be fitting. */ |
9630 | | sigLen = args->sigLen; |
9631 | | } |
9632 | | args->sigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9633 | | DYNAMIC_TYPE_SIGNATURE); |
9634 | | if (args->sigData == NULL) { |
9635 | | ERROR_OUT(MEMORY_E, exit_scv); |
9636 | | } |
9637 | | } |
9638 | | |
9639 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9640 | | if ((ssl->sigSpec != NULL) && |
9641 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
9642 | | (args->altSigData == NULL)) { |
9643 | | word32 sigLen = MAX_SIG_DATA_SZ; |
9644 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA && |
9645 | | args->altSigLen > MAX_SIG_DATA_SZ) { |
9646 | | /* We store the RSA signature in the sigData buffer |
9647 | | * temporarily, hence its size must be fitting. */ |
9648 | | sigLen = args->altSigLen; |
9649 | | } |
9650 | | args->altSigData = (byte*)XMALLOC(sigLen, ssl->heap, |
9651 | | DYNAMIC_TYPE_SIGNATURE); |
9652 | | if (args->altSigData == NULL) { |
9653 | | ERROR_OUT(MEMORY_E, exit_scv); |
9654 | | } |
9655 | | } |
9656 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9657 | | |
9658 | | /* Create the data to be signed. */ |
9659 | | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0); |
9660 | | if (ret != 0) |
9661 | | goto exit_scv; |
9662 | | |
9663 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9664 | | if ((ssl->sigSpec != NULL) && |
9665 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
9666 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
9667 | | args->altSigDataSz = args->sigDataSz; |
9668 | | } |
9669 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9670 | | |
9671 | | #ifndef NO_RSA |
9672 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9673 | | /* build encoded signature buffer */ |
9674 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9675 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, ssl->heap, |
9676 | | DYNAMIC_TYPE_SIGNATURE); |
9677 | | if (rsaSigBuf->buffer == NULL) { |
9678 | | ERROR_OUT(MEMORY_E, exit_scv); |
9679 | | } |
9680 | | |
9681 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, args->sigData, |
9682 | | args->sigDataSz, args->sigAlgo, ssl->options.hashAlgo); |
9683 | | if (ret < 0) |
9684 | | goto exit_scv; |
9685 | | rsaSigBuf->length = (unsigned int)ret; |
9686 | | ret = 0; |
9687 | | } |
9688 | | #endif /* !NO_RSA */ |
9689 | | #ifdef HAVE_ECC |
9690 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9691 | | args->sigLen = (word32)args->sendSz - args->idx - |
9692 | | HASH_SIG_SIZE - |
9693 | | VERIFY_HEADER; |
9694 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9695 | | if (ssl->buffers.keyType != sm2_sa_algo) |
9696 | | #endif |
9697 | | { |
9698 | | ret = CreateECCEncodedSig(args->sigData, |
9699 | | args->sigDataSz, ssl->options.hashAlgo); |
9700 | | if (ret < 0) |
9701 | | goto exit_scv; |
9702 | | args->sigDataSz = (word16)ret; |
9703 | | ret = 0; |
9704 | | } |
9705 | | } |
9706 | | #endif /* HAVE_ECC */ |
9707 | | #ifdef HAVE_ED25519 |
9708 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9709 | | ret = Ed25519CheckPubKey(ssl); |
9710 | | if (ret < 0) { |
9711 | | ERROR_OUT(ret, exit_scv); |
9712 | | } |
9713 | | args->sigLen = ED25519_SIG_SIZE; |
9714 | | } |
9715 | | #endif /* HAVE_ED25519 */ |
9716 | | #ifdef HAVE_ED448 |
9717 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9718 | | ret = Ed448CheckPubKey(ssl); |
9719 | | if (ret < 0) { |
9720 | | ERROR_OUT(ret, exit_scv); |
9721 | | } |
9722 | | args->sigLen = ED448_SIG_SIZE; |
9723 | | } |
9724 | | |
9725 | | #endif /* HAVE_ED448 */ |
9726 | | #if defined(HAVE_FALCON) |
9727 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9728 | | args->sigLen = FALCON_MAX_SIG_SIZE; |
9729 | | } |
9730 | | #endif /* HAVE_FALCON */ |
9731 | | #if defined(HAVE_DILITHIUM) |
9732 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9733 | | args->sigLen = DILITHIUM_MAX_SIG_SIZE; |
9734 | | } |
9735 | | #endif /* HAVE_DILITHIUM */ |
9736 | | |
9737 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9738 | | if (ssl->sigSpec != NULL && |
9739 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9740 | | |
9741 | | #ifndef NO_RSA |
9742 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9743 | | /* build encoded signature buffer */ |
9744 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
9745 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, |
9746 | | ssl->heap, |
9747 | | DYNAMIC_TYPE_SIGNATURE); |
9748 | | if (rsaSigBuf->buffer == NULL) { |
9749 | | ERROR_OUT(MEMORY_E, exit_scv); |
9750 | | } |
9751 | | |
9752 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, |
9753 | | args->altSigData, args->altSigDataSz, |
9754 | | args->altSigAlgo, ssl->options.hashAlgo); |
9755 | | if (ret < 0) |
9756 | | goto exit_scv; |
9757 | | rsaSigBuf->length = ret; |
9758 | | ret = 0; |
9759 | | } |
9760 | | #endif /* !NO_RSA */ |
9761 | | #ifdef HAVE_ECC |
9762 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9763 | | ret = CreateECCEncodedSig(args->altSigData, |
9764 | | args->altSigDataSz, ssl->options.hashAlgo); |
9765 | | if (ret < 0) |
9766 | | goto exit_scv; |
9767 | | args->altSigDataSz = (word16)ret; |
9768 | | ret = 0; |
9769 | | } |
9770 | | #endif /* HAVE_ECC */ |
9771 | | } |
9772 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9773 | | |
9774 | | /* Advance state and proceed */ |
9775 | | ssl->options.asyncState = TLS_ASYNC_DO; |
9776 | | } /* case TLS_ASYNC_BUILD */ |
9777 | | FALL_THROUGH; |
9778 | | |
9779 | | case TLS_ASYNC_DO: |
9780 | | { |
9781 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
9782 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9783 | | if (ssl->sigSpec != NULL && |
9784 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9785 | | /* As we have two signatures in the message, we store |
9786 | | * the length of each before the actual signature. This |
9787 | | * is necessary, as we could have two algorithms with |
9788 | | * variable length signatures. */ |
9789 | | sigOut += OPAQUE16_LEN; |
9790 | | } |
9791 | | #endif |
9792 | | #ifdef HAVE_ECC |
9793 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
9794 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9795 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
9796 | | ret = Sm2wSm3Sign(ssl, TLS13_SM2_SIG_ID, |
9797 | | TLS13_SM2_SIG_ID_SZ, args->sigData, args->sigDataSz, |
9798 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, NULL); |
9799 | | } |
9800 | | else |
9801 | | #endif |
9802 | | { |
9803 | | ret = EccSign(ssl, args->sigData, args->sigDataSz, |
9804 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, |
9805 | | #ifdef HAVE_PK_CALLBACKS |
9806 | | ssl->buffers.key |
9807 | | #else |
9808 | | NULL |
9809 | | #endif |
9810 | | ); |
9811 | | } |
9812 | | args->length = (word16)args->sigLen; |
9813 | | } |
9814 | | #endif /* HAVE_ECC */ |
9815 | | #ifdef HAVE_ED25519 |
9816 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
9817 | | ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz, |
9818 | | sigOut, &args->sigLen, (ed25519_key*)ssl->hsKey, |
9819 | | #ifdef HAVE_PK_CALLBACKS |
9820 | | ssl->buffers.key |
9821 | | #else |
9822 | | NULL |
9823 | | #endif |
9824 | | ); |
9825 | | args->length = (word16)args->sigLen; |
9826 | | } |
9827 | | #endif |
9828 | | #ifdef HAVE_ED448 |
9829 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
9830 | | ret = Ed448Sign(ssl, args->sigData, args->sigDataSz, |
9831 | | sigOut, &args->sigLen, (ed448_key*)ssl->hsKey, |
9832 | | #ifdef HAVE_PK_CALLBACKS |
9833 | | ssl->buffers.key |
9834 | | #else |
9835 | | NULL |
9836 | | #endif |
9837 | | ); |
9838 | | args->length = (word16)args->sigLen; |
9839 | | } |
9840 | | #endif |
9841 | | #if defined(HAVE_FALCON) |
9842 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
9843 | | ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz, |
9844 | | sigOut, &args->sigLen, |
9845 | | (falcon_key*)ssl->hsKey, ssl->rng); |
9846 | | args->length = (word16)args->sigLen; |
9847 | | } |
9848 | | #endif /* HAVE_FALCON */ |
9849 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9850 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
9851 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->sigData, |
9852 | | args->sigDataSz, sigOut, |
9853 | | &args->sigLen, |
9854 | | (dilithium_key*)ssl->hsKey, |
9855 | | ssl->rng); |
9856 | | args->length = (word16)args->sigLen; |
9857 | | } |
9858 | | #endif /* HAVE_DILITHIUM */ |
9859 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
9860 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
9861 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9862 | | args->toSign = rsaSigBuf->buffer; |
9863 | | args->toSignSz = (word32)rsaSigBuf->length; |
9864 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9865 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9866 | | /* Pass full data to sign (args->sigData), not hash of */ |
9867 | | if (ssl->ctx->RsaPssSignCb) { |
9868 | | args->toSign = args->sigData; |
9869 | | args->toSignSz = args->sigDataSz; |
9870 | | } |
9871 | | #endif |
9872 | | ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz, |
9873 | | sigOut, &args->sigLen, args->sigAlgo, |
9874 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9875 | | ssl->buffers.key); |
9876 | | if (ret == 0) { |
9877 | | args->length = (word16)args->sigLen; |
9878 | | XMEMCPY(args->sigData, sigOut, args->sigLen); |
9879 | | } |
9880 | | } |
9881 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
9882 | | |
9883 | | /* Check for error */ |
9884 | | if (ret != 0) { |
9885 | | goto exit_scv; |
9886 | | } |
9887 | | |
9888 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9889 | | if (ssl->sigSpec != NULL && |
9890 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
9891 | | /* Add signature length for the first signature. */ |
9892 | | c16toa((word16)args->sigLen, sigOut - OPAQUE16_LEN); |
9893 | | args->length += OPAQUE16_LEN; |
9894 | | |
9895 | | /* Advance our pointer to where we store the alt signature. |
9896 | | * We also add additional space for the length field of the |
9897 | | * second signature. */ |
9898 | | sigOut += args->sigLen + OPAQUE16_LEN; |
9899 | | |
9900 | | /* Generate the alternative signature */ |
9901 | | #ifdef HAVE_ECC |
9902 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
9903 | | ret = EccSign(ssl, args->altSigData, args->altSigDataSz, |
9904 | | sigOut, &args->altSigLen, |
9905 | | (ecc_key*)ssl->hsAltKey, |
9906 | | #ifdef HAVE_PK_CALLBACKS |
9907 | | ssl->buffers.altKey |
9908 | | #else |
9909 | | NULL |
9910 | | #endif |
9911 | | ); |
9912 | | } |
9913 | | #endif /* HAVE_ECC */ |
9914 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
9915 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
9916 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9917 | | args->toSign = rsaSigBuf->buffer; |
9918 | | args->toSignSz = (word32)rsaSigBuf->length; |
9919 | | #if defined(HAVE_PK_CALLBACKS) && \ |
9920 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
9921 | | /* Pass full data to sign (args->altSigData), not hash of */ |
9922 | | if (ssl->ctx->RsaPssSignCb) { |
9923 | | args->toSign = args->altSigData; |
9924 | | args->toSignSz = (word32)args->altSigDataSz; |
9925 | | } |
9926 | | #endif |
9927 | | ret = RsaSign(ssl, (const byte*)args->toSign, |
9928 | | args->toSignSz, sigOut, &args->altSigLen, |
9929 | | args->altSigAlgo, ssl->options.hashAlgo, |
9930 | | (RsaKey*)ssl->hsAltKey, |
9931 | | ssl->buffers.altKey); |
9932 | | |
9933 | | if (ret == 0) { |
9934 | | XMEMCPY(args->altSigData, sigOut, args->altSigLen); |
9935 | | } |
9936 | | } |
9937 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
9938 | | #if defined(HAVE_FALCON) |
9939 | | if (ssl->hsAltType == DYNAMIC_TYPE_FALCON) { |
9940 | | ret = wc_falcon_sign_msg(args->altSigData, |
9941 | | args->altSigDataSz, sigOut, |
9942 | | &args->altSigLen, |
9943 | | (falcon_key*)ssl->hsAltKey, |
9944 | | ssl->rng); |
9945 | | } |
9946 | | #endif /* HAVE_FALCON */ |
9947 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
9948 | | if (ssl->hsAltType == DYNAMIC_TYPE_DILITHIUM) { |
9949 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->altSigData, |
9950 | | args->altSigDataSz, sigOut, &args->altSigLen, |
9951 | | (dilithium_key*)ssl->hsAltKey, ssl->rng); |
9952 | | } |
9953 | | #endif /* HAVE_DILITHIUM */ |
9954 | | |
9955 | | /* Check for error */ |
9956 | | if (ret != 0) { |
9957 | | goto exit_scv; |
9958 | | } |
9959 | | |
9960 | | /* Add signature length for the alternative signature. */ |
9961 | | c16toa((word16)args->altSigLen, sigOut - OPAQUE16_LEN); |
9962 | | |
9963 | | /* Add length of the alt sig to the total length */ |
9964 | | args->length += args->altSigLen + OPAQUE16_LEN; |
9965 | | } |
9966 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9967 | | |
9968 | | /* Add signature length. */ |
9969 | | c16toa(args->length, args->verify + HASH_SIG_SIZE); |
9970 | | |
9971 | | /* Advance state and proceed */ |
9972 | | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
9973 | | } /* case TLS_ASYNC_DO */ |
9974 | | FALL_THROUGH; |
9975 | | |
9976 | | case TLS_ASYNC_VERIFY: |
9977 | | { |
9978 | | #ifndef NO_RSA |
9979 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
9980 | | /* check for signature faults */ |
9981 | | ret = VerifyRsaSign(ssl, args->sigData, args->sigLen, |
9982 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, args->sigAlgo, |
9983 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
9984 | | ssl->buffers.key); |
9985 | | } |
9986 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9987 | | if (ssl->sigSpec != NULL && |
9988 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
9989 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
9990 | | /* check for signature faults */ |
9991 | | ret = VerifyRsaSign(ssl, args->altSigData, args->altSigLen, |
9992 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, |
9993 | | args->altSigAlgo, ssl->options.hashAlgo, |
9994 | | (RsaKey*)ssl->hsAltKey, ssl->buffers.altKey); |
9995 | | } |
9996 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9997 | | #endif /* !NO_RSA */ |
9998 | | #if defined(HAVE_ECC) && defined(WOLFSSL_CHECK_SIG_FAULTS) |
9999 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10000 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
10001 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10002 | | if (ssl->sigSpec != NULL && |
10003 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10004 | | /* Add our length offset. */ |
10005 | | sigOut += OPAQUE16_LEN; |
10006 | | } |
10007 | | #endif |
10008 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10009 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10010 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
10011 | | TLS13_SM2_SIG_ID_SZ, |
10012 | | sigOut, args->sigLen, args->sigData, args->sigDataSz, |
10013 | | (ecc_key*)ssl->hsKey, NULL); |
10014 | | } |
10015 | | else |
10016 | | #endif |
10017 | | { |
10018 | | #ifdef HAVE_PK_CALLBACKS |
10019 | | buffer tmp; |
10020 | | |
10021 | | tmp.length = ssl->buffers.key->length; |
10022 | | tmp.buffer = ssl->buffers.key->buffer; |
10023 | | #endif |
10024 | | ret = EccVerify(ssl, sigOut, args->sigLen, |
10025 | | args->sigData, args->sigDataSz, |
10026 | | (ecc_key*)ssl->hsKey, |
10027 | | #ifdef HAVE_PK_CALLBACKS |
10028 | | &tmp |
10029 | | #else |
10030 | | NULL |
10031 | | #endif |
10032 | | ); |
10033 | | } |
10034 | | } |
10035 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10036 | | if (ssl->sigSpec != NULL && |
10037 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10038 | | ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
10039 | | /* check for signature faults */ |
10040 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER + |
10041 | | args->sigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
10042 | | ret = EccVerify(ssl, sigOut, args->altSigLen, |
10043 | | args->altSigData, args->altSigDataSz, |
10044 | | (ecc_key*)ssl->hsAltKey, |
10045 | | #ifdef HAVE_PK_CALLBACKS |
10046 | | ssl->buffers.altKey |
10047 | | #else |
10048 | | NULL |
10049 | | #endif |
10050 | | ); |
10051 | | } |
10052 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10053 | | #endif /* HAVE_ECC && WOLFSSL_CHECK_SIG_FAULTS */ |
10054 | | |
10055 | | /* Check for error */ |
10056 | | if (ret != 0) { |
10057 | | goto exit_scv; |
10058 | | } |
10059 | | |
10060 | | /* Advance state and proceed */ |
10061 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
10062 | | } /* case TLS_ASYNC_VERIFY */ |
10063 | | FALL_THROUGH; |
10064 | | |
10065 | | case TLS_ASYNC_FINALIZE: |
10066 | | { |
10067 | | /* Put the record and handshake headers on. */ |
10068 | | AddTls13Headers(args->output, args->length + HASH_SIG_SIZE + |
10069 | | VERIFY_HEADER, certificate_verify, ssl); |
10070 | | |
10071 | | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
10072 | | args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
10073 | | #ifdef WOLFSSL_DTLS13 |
10074 | | if (ssl->options.dtls) |
10075 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10076 | | |
10077 | | #endif /* WOLFSSL_DTLS13 */ |
10078 | | /* Advance state and proceed */ |
10079 | | ssl->options.asyncState = TLS_ASYNC_END; |
10080 | | } /* case TLS_ASYNC_FINALIZE */ |
10081 | | FALL_THROUGH; |
10082 | | |
10083 | | case TLS_ASYNC_END: |
10084 | | { |
10085 | | #ifdef WOLFSSL_DTLS13 |
10086 | | if (ssl->options.dtls) { |
10087 | | ssl->options.buildingMsg = 0; |
10088 | | ret = Dtls13HandshakeSend(ssl, args->output, |
10089 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA + MAX_MSG_EXTRA, |
10090 | | (word16)args->sendSz, certificate_verify, 1); |
10091 | | if (ret != 0) |
10092 | | goto exit_scv; |
10093 | | |
10094 | | break; |
10095 | | } |
10096 | | #endif /* WOLFSSL_DTLS13 */ |
10097 | | |
10098 | | /* This message is always encrypted. */ |
10099 | | ret = BuildTls13Message(ssl, args->output, |
10100 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA, |
10101 | | args->output + RECORD_HEADER_SZ, |
10102 | | args->sendSz - RECORD_HEADER_SZ, handshake, |
10103 | | 1, 0, 0); |
10104 | | |
10105 | | if (ret < 0) { |
10106 | | goto exit_scv; |
10107 | | } |
10108 | | else { |
10109 | | args->sendSz = ret; |
10110 | | ret = 0; |
10111 | | } |
10112 | | |
10113 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
10114 | | if (ssl->hsInfoOn) |
10115 | | AddPacketName(ssl, "CertificateVerify"); |
10116 | | if (ssl->toInfoOn) { |
10117 | | ret = AddPacketInfo(ssl, "CertificateVerify", handshake, |
10118 | | args->output, args->sendSz, WRITE_PROTO, 0, |
10119 | | ssl->heap); |
10120 | | if (ret != 0) |
10121 | | goto exit_scv; |
10122 | | } |
10123 | | #endif |
10124 | | |
10125 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
10126 | | ssl->options.buildingMsg = 0; |
10127 | | if (!ssl->options.groupMessages) |
10128 | | ret = SendBuffered(ssl); |
10129 | | break; |
10130 | | } |
10131 | | default: |
10132 | | ret = INPUT_CASE_ERROR; |
10133 | | } /* switch(ssl->options.asyncState) */ |
10134 | | |
10135 | | exit_scv: |
10136 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10137 | | if (ret == 0) { |
10138 | | ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
10139 | | &ssl->buffers.keyMask); |
10140 | | } |
10141 | | else { |
10142 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
10143 | | } |
10144 | | #endif |
10145 | | |
10146 | | WOLFSSL_LEAVE("SendTls13CertificateVerify", ret); |
10147 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
10148 | | |
10149 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10150 | | /* Handle async operation */ |
10151 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
10152 | | return ret; |
10153 | | } |
10154 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
10155 | | |
10156 | | /* Final cleanup */ |
10157 | | FreeScv13Args(ssl, args); |
10158 | | FreeKeyExchange(ssl); |
10159 | | #ifdef WOLFSSL_ASYNC_IO |
10160 | | /* Cleanup async */ |
10161 | | FreeAsyncCtx(ssl, 0); |
10162 | | #endif |
10163 | | |
10164 | | if (ret != 0) { |
10165 | | WOLFSSL_ERROR_VERBOSE(ret); |
10166 | | } |
10167 | | |
10168 | | return ret; |
10169 | | } |
10170 | | #endif |
10171 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
10172 | | |
10173 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) |
10174 | | /* handle processing TLS v1.3 certificate (11) */ |
10175 | | /* Parse and handle a TLS v1.3 Certificate message. |
10176 | | * |
10177 | | * ssl The SSL/TLS object. |
10178 | | * input The message buffer. |
10179 | | * inOutIdx On entry, the index into the message buffer of Certificate. |
10180 | | * On exit, the index of byte after the Certificate message. |
10181 | | * totalSz The length of the current handshake message. |
10182 | | * returns 0 on success and otherwise failure. |
10183 | | */ |
10184 | | static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
10185 | | word32 totalSz) |
10186 | 0 | { |
10187 | 0 | int ret = 0; |
10188 | |
|
10189 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_DO); |
10190 | 0 | WOLFSSL_ENTER("DoTls13Certificate"); |
10191 | |
|
10192 | | #ifdef WOLFSSL_DTLS13 |
10193 | | if (ssl->options.dtls && ssl->options.handShakeDone) { |
10194 | | /* certificate needs some special care after the handshake */ |
10195 | | ret = Dtls13RtxProcessingCertificate( |
10196 | | ssl, input + *inOutIdx, totalSz); |
10197 | | } |
10198 | | #endif /* WOLFSSL_DTLS13 */ |
10199 | |
|
10200 | 0 | if (ret == 0) |
10201 | 0 | ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz); |
10202 | 0 | if (ret == 0) { |
10203 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
10204 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
10205 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
10206 | 0 | #endif |
10207 | | #if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
10208 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
10209 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
10210 | | /* reset handshake states */ |
10211 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
10212 | | ssl->options.acceptState = TICKET_SENT; |
10213 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
10214 | | } |
10215 | | #endif |
10216 | 0 | } |
10217 | |
|
10218 | 0 | WOLFSSL_LEAVE("DoTls13Certificate", ret); |
10219 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_DO); |
10220 | |
|
10221 | 0 | return ret; |
10222 | 0 | } |
10223 | | #endif |
10224 | | |
10225 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
10226 | | defined(HAVE_ED448) |
10227 | | |
10228 | | typedef struct Dcv13Args { |
10229 | | byte* output; /* not allocated */ |
10230 | | word32 sendSz; |
10231 | | word16 sz; |
10232 | | word32 sigSz; |
10233 | | word32 idx; |
10234 | | word32 begin; |
10235 | | |
10236 | | byte* sigData; |
10237 | | word16 sigDataSz; |
10238 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10239 | | byte altSigAlgo; |
10240 | | byte* altSigData; |
10241 | | word32 altSigDataSz; |
10242 | | word32 altSignatureSz; |
10243 | | byte altPeerAuthGood; |
10244 | | #endif |
10245 | | } Dcv13Args; |
10246 | | |
10247 | | static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs) |
10248 | 0 | { |
10249 | 0 | Dcv13Args* args = (Dcv13Args*)pArgs; |
10250 | |
|
10251 | 0 | if (args && args->sigData != NULL) { |
10252 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10253 | 0 | args->sigData = NULL; |
10254 | 0 | } |
10255 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10256 | | if (args && args->altSigData != NULL) { |
10257 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10258 | | args->altSigData = NULL; |
10259 | | } |
10260 | | #endif |
10261 | 0 | (void)ssl; |
10262 | 0 | } |
10263 | | |
10264 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10265 | | #ifndef NO_RSA |
10266 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10267 | | * RSA public key. Convert it into a usable public key. */ |
10268 | | static int decodeRsaKey(WOLFSSL* ssl) |
10269 | | { |
10270 | | int keyRet; |
10271 | | word32 tmpIdx = 0; |
10272 | | |
10273 | | if (ssl->peerRsaKeyPresent) |
10274 | | return INVALID_PARAMETER; |
10275 | | |
10276 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
10277 | | if (keyRet != 0) |
10278 | | return PEER_KEY_ERROR; |
10279 | | |
10280 | | ssl->peerRsaKeyPresent = 1; |
10281 | | keyRet = wc_RsaPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10282 | | ssl->peerRsaKey, |
10283 | | ssl->peerCert.sapkiLen); |
10284 | | if (keyRet != 0) |
10285 | | return PEER_KEY_ERROR; |
10286 | | |
10287 | | return 0; |
10288 | | } |
10289 | | #endif /* !NO_RSA */ |
10290 | | |
10291 | | #ifdef HAVE_ECC |
10292 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10293 | | * ECC public key. Convert it into a usable public key. */ |
10294 | | static int decodeEccKey(WOLFSSL* ssl) |
10295 | | { |
10296 | | int keyRet; |
10297 | | word32 tmpIdx = 0; |
10298 | | |
10299 | | if (ssl->peerEccDsaKeyPresent) |
10300 | | return INVALID_PARAMETER; |
10301 | | |
10302 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10303 | | if (keyRet != 0) |
10304 | | return PEER_KEY_ERROR; |
10305 | | |
10306 | | ssl->peerEccDsaKeyPresent = 1; |
10307 | | keyRet = wc_EccPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10308 | | ssl->peerEccDsaKey, |
10309 | | ssl->peerCert.sapkiLen); |
10310 | | if (keyRet != 0) |
10311 | | return PEER_KEY_ERROR; |
10312 | | |
10313 | | return 0; |
10314 | | } |
10315 | | #endif /* HAVE_ECC */ |
10316 | | |
10317 | | #ifdef HAVE_DILITHIUM |
10318 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10319 | | * dilithium public key. Convert it into a usable public key. */ |
10320 | | static int decodeDilithiumKey(WOLFSSL* ssl, int level) |
10321 | | { |
10322 | | int keyRet; |
10323 | | word32 tmpIdx = 0; |
10324 | | |
10325 | | if (ssl->peerDilithiumKeyPresent) |
10326 | | return INVALID_PARAMETER; |
10327 | | |
10328 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10329 | | (void**)&ssl->peerDilithiumKey); |
10330 | | if (keyRet != 0) |
10331 | | return PEER_KEY_ERROR; |
10332 | | |
10333 | | ssl->peerDilithiumKeyPresent = 1; |
10334 | | keyRet = wc_dilithium_set_level(ssl->peerDilithiumKey, level); |
10335 | | if (keyRet != 0) |
10336 | | return PEER_KEY_ERROR; |
10337 | | |
10338 | | keyRet = wc_Dilithium_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10339 | | ssl->peerDilithiumKey, |
10340 | | ssl->peerCert.sapkiLen); |
10341 | | if (keyRet != 0) |
10342 | | return PEER_KEY_ERROR; |
10343 | | |
10344 | | return 0; |
10345 | | } |
10346 | | #endif /* HAVE_DILITHIUM */ |
10347 | | |
10348 | | #ifdef HAVE_FALCON |
10349 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10350 | | * falcon public key. Convert it into a usable public key. */ |
10351 | | static int decodeFalconKey(WOLFSSL* ssl, int level) |
10352 | | { |
10353 | | int keyRet; |
10354 | | word32 tmpIdx = 0; |
10355 | | |
10356 | | if (ssl->peerFalconKeyPresent) |
10357 | | return INVALID_PARAMETER; |
10358 | | |
10359 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey); |
10360 | | if (keyRet != 0) |
10361 | | return PEER_KEY_ERROR; |
10362 | | |
10363 | | ssl->peerFalconKeyPresent = 1; |
10364 | | keyRet = wc_falcon_set_level(ssl->peerFalconKey, level); |
10365 | | if (keyRet != 0) |
10366 | | return PEER_KEY_ERROR; |
10367 | | |
10368 | | keyRet = wc_Falcon_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10369 | | ssl->peerFalconKey, |
10370 | | ssl->peerCert.sapkiLen); |
10371 | | if (keyRet != 0) |
10372 | | return PEER_KEY_ERROR; |
10373 | | |
10374 | | return 0; |
10375 | | } |
10376 | | #endif /* HAVE_FALCON */ |
10377 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10378 | | |
10379 | | /* handle processing TLS v1.3 certificate_verify (15) */ |
10380 | | /* Parse and handle a TLS v1.3 CertificateVerify message. |
10381 | | * |
10382 | | * ssl The SSL/TLS object. |
10383 | | * input The message buffer. |
10384 | | * inOutIdx On entry, the index into the message buffer of |
10385 | | * CertificateVerify. |
10386 | | * On exit, the index of byte after the CertificateVerify message. |
10387 | | * totalSz The length of the current handshake message. |
10388 | | * returns 0 on success and otherwise failure. |
10389 | | */ |
10390 | | static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input, |
10391 | | word32* inOutIdx, word32 totalSz) |
10392 | 0 | { |
10393 | 0 | int ret = 0; |
10394 | 0 | byte* sig = NULL; |
10395 | 0 | #ifndef NO_RSA |
10396 | | /* Use this as a temporary buffer for RSA signature verification. */ |
10397 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
10398 | 0 | #endif |
10399 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10400 | | Dcv13Args* args = NULL; |
10401 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
10402 | | #else |
10403 | 0 | Dcv13Args args[1]; |
10404 | 0 | #endif |
10405 | |
|
10406 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10407 | 0 | WOLFSSL_ENTER("DoTls13CertificateVerify"); |
10408 | |
|
10409 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10410 | | ret = tsip_Tls13CertificateVerify(ssl, input, inOutIdx, totalSz); |
10411 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10412 | | goto exit_dcv; |
10413 | | } |
10414 | | ret = 0; |
10415 | | #endif |
10416 | |
|
10417 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10418 | | if (ssl->async == NULL) { |
10419 | | ssl->async = (struct WOLFSSL_ASYNC*) |
10420 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
10421 | | DYNAMIC_TYPE_ASYNC); |
10422 | | if (ssl->async == NULL) |
10423 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10424 | | } |
10425 | | args = (Dcv13Args*)ssl->async->args; |
10426 | | |
10427 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
10428 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
10429 | | /* Check for error */ |
10430 | | if (ret < 0) |
10431 | | goto exit_dcv; |
10432 | | } |
10433 | | else |
10434 | | #endif |
10435 | 0 | { |
10436 | | /* Reset state */ |
10437 | 0 | ret = 0; |
10438 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
10439 | 0 | XMEMSET(args, 0, sizeof(Dcv13Args)); |
10440 | 0 | ssl->options.peerHashAlgo = sha_mac; |
10441 | 0 | ssl->options.peerSigAlgo = anonymous_sa_algo; |
10442 | 0 | args->idx = *inOutIdx; |
10443 | 0 | args->begin = *inOutIdx; |
10444 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10445 | | ssl->async->freeArgs = FreeDcv13Args; |
10446 | | #endif |
10447 | 0 | } |
10448 | |
|
10449 | 0 | switch(ssl->options.asyncState) |
10450 | 0 | { |
10451 | 0 | case TLS_ASYNC_BEGIN: |
10452 | 0 | { |
10453 | | #ifdef WOLFSSL_CALLBACKS |
10454 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify"); |
10455 | | if (ssl->toInfoOn) AddLateName("CertificateVerify", |
10456 | | &ssl->timeoutInfo); |
10457 | | #endif |
10458 | | |
10459 | | /* Advance state and proceed */ |
10460 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
10461 | 0 | } /* case TLS_ASYNC_BEGIN */ |
10462 | 0 | FALL_THROUGH; |
10463 | |
|
10464 | 0 | case TLS_ASYNC_BUILD: |
10465 | 0 | { |
10466 | 0 | int validSigAlgo; |
10467 | 0 | const Suites* suites = WOLFSSL_SUITES(ssl); |
10468 | 0 | word16 i; |
10469 | | |
10470 | | /* Signature algorithm. */ |
10471 | 0 | if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) { |
10472 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10473 | 0 | } |
10474 | | |
10475 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10476 | | if (ssl->peerSigSpec == NULL) { |
10477 | | /* The peer did not respond. We didn't send CKS or they don't |
10478 | | * support it. Either way, we do not need to handle dual |
10479 | | * key/sig case. */ |
10480 | | ssl->sigSpec = NULL; |
10481 | | ssl->sigSpecSz = 0; |
10482 | | } |
10483 | | |
10484 | | /* If no CKS extension or either native or alternative, then just |
10485 | | * get a normal sigalgo. But if BOTH, then get the native and alt |
10486 | | * sig algos. */ |
10487 | | if (ssl->sigSpec == NULL || |
10488 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_NATIVE || |
10489 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10490 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10491 | 0 | validSigAlgo = 0; |
10492 | 0 | for (i = 0; i < suites->hashSigAlgoSz; i += 2) { |
10493 | 0 | if ((suites->hashSigAlgo[i + 0] == input[args->idx + 0]) && |
10494 | 0 | (suites->hashSigAlgo[i + 1] == input[args->idx + 1])) { |
10495 | 0 | validSigAlgo = 1; |
10496 | 0 | break; |
10497 | 0 | } |
10498 | 0 | } |
10499 | 0 | if (!validSigAlgo) { |
10500 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10501 | 0 | } |
10502 | | |
10503 | 0 | ret = DecodeTls13SigAlg(input + args->idx, |
10504 | 0 | &ssl->options.peerHashAlgo, &ssl->options.peerSigAlgo); |
10505 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10506 | | } |
10507 | | else { |
10508 | | ret = DecodeTls13HybridSigAlg(input + args->idx, |
10509 | | &ssl->options.peerHashAlgo, |
10510 | | &ssl->options.peerSigAlgo, |
10511 | | &args->altSigAlgo); |
10512 | | } |
10513 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10514 | |
|
10515 | 0 | if (ret < 0) |
10516 | 0 | goto exit_dcv; |
10517 | 0 | args->idx += OPAQUE16_LEN; |
10518 | | |
10519 | | /* Signature length. */ |
10520 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) { |
10521 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10522 | 0 | } |
10523 | 0 | ato16(input + args->idx, &args->sz); |
10524 | 0 | args->idx += OPAQUE16_LEN; |
10525 | | |
10526 | | /* Signature data. */ |
10527 | 0 | if ((args->idx - args->begin) + args->sz > totalSz) { |
10528 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10529 | 0 | } |
10530 | | |
10531 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10532 | | if ((ssl->sigSpec != NULL) && |
10533 | | (*ssl->sigSpec != WOLFSSL_CKS_SIGSPEC_NATIVE)) { |
10534 | | |
10535 | | word16 sa; |
10536 | | if (args->altSigAlgo == 0) |
10537 | | sa = ssl->options.peerSigAlgo; |
10538 | | else |
10539 | | sa = args->altSigAlgo; |
10540 | | |
10541 | | switch(sa) { |
10542 | | #ifndef NO_RSA |
10543 | | case rsa_pss_sa_algo: |
10544 | | ret = decodeRsaKey(ssl); |
10545 | | break; |
10546 | | #endif |
10547 | | #ifdef HAVE_ECC |
10548 | | case ecc_dsa_sa_algo: |
10549 | | ret = decodeEccKey(ssl); |
10550 | | break; |
10551 | | #endif |
10552 | | #ifdef HAVE_DILITHIUM |
10553 | | case dilithium_level2_sa_algo: |
10554 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_44); |
10555 | | break; |
10556 | | case dilithium_level3_sa_algo: |
10557 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_65); |
10558 | | break; |
10559 | | case dilithium_level5_sa_algo: |
10560 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_87); |
10561 | | break; |
10562 | | #endif |
10563 | | #ifdef HAVE_FALCON |
10564 | | case falcon_level1_sa_algo: |
10565 | | ret = decodeFalconKey(ssl, 1); |
10566 | | break; |
10567 | | case falcon_level5_sa_algo: |
10568 | | ret = decodeFalconKey(ssl, 5); |
10569 | | break; |
10570 | | #endif |
10571 | | default: |
10572 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10573 | | } |
10574 | | |
10575 | | if (ret != 0) |
10576 | | ERROR_OUT(ret, exit_dcv); |
10577 | | |
10578 | | if (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10579 | | /* Now swap in the alternative by removing the native. |
10580 | | * sa contains the alternative signature type. */ |
10581 | | #ifndef NO_RSA |
10582 | | if (ssl->peerRsaKeyPresent && sa != rsa_pss_sa_algo) { |
10583 | | FreeKey(ssl, DYNAMIC_TYPE_RSA, |
10584 | | (void**)&ssl->peerRsaKey); |
10585 | | ssl->peerRsaKeyPresent = 0; |
10586 | | } |
10587 | | #endif |
10588 | | #ifdef HAVE_ECC |
10589 | | else if (ssl->peerEccDsaKeyPresent && |
10590 | | sa != ecc_dsa_sa_algo) { |
10591 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
10592 | | (void**)&ssl->peerEccDsaKey); |
10593 | | ssl->peerEccDsaKeyPresent = 0; |
10594 | | } |
10595 | | #endif |
10596 | | #ifdef HAVE_DILITHIUM |
10597 | | else if (ssl->peerDilithiumKeyPresent && |
10598 | | sa != dilithium_level2_sa_algo && |
10599 | | sa != dilithium_level3_sa_algo && |
10600 | | sa != dilithium_level5_sa_algo) { |
10601 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10602 | | (void**)&ssl->peerDilithiumKey); |
10603 | | ssl->peerDilithiumKeyPresent = 0; |
10604 | | } |
10605 | | #endif |
10606 | | #ifdef HAVE_FALCON |
10607 | | else if (ssl->peerFalconKeyPresent && |
10608 | | sa != falcon_level1_sa_algo && |
10609 | | sa != falcon_level5_sa_algo) { |
10610 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10611 | | (void**)&ssl->peerFalconKey); |
10612 | | ssl->peerFalconKeyPresent = 0; |
10613 | | } |
10614 | | #endif |
10615 | | else { |
10616 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
10617 | | } |
10618 | | } |
10619 | | } |
10620 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10621 | | |
10622 | | /* Check for public key of required type. */ |
10623 | | /* Assume invalid unless signature algo matches the key provided */ |
10624 | 0 | validSigAlgo = 0; |
10625 | 0 | #ifdef HAVE_ED25519 |
10626 | 0 | if (ssl->options.peerSigAlgo == ed25519_sa_algo) { |
10627 | 0 | WOLFSSL_MSG("Peer sent ED25519 sig"); |
10628 | 0 | validSigAlgo = (ssl->peerEd25519Key != NULL) && |
10629 | 0 | ssl->peerEd25519KeyPresent; |
10630 | 0 | } |
10631 | 0 | #endif |
10632 | 0 | #ifdef HAVE_ED448 |
10633 | 0 | if (ssl->options.peerSigAlgo == ed448_sa_algo) { |
10634 | 0 | WOLFSSL_MSG("Peer sent ED448 sig"); |
10635 | 0 | validSigAlgo = (ssl->peerEd448Key != NULL) && |
10636 | 0 | ssl->peerEd448KeyPresent; |
10637 | 0 | } |
10638 | 0 | #endif |
10639 | 0 | #ifdef HAVE_ECC |
10640 | 0 | if (ssl->options.peerSigAlgo == ecc_dsa_sa_algo) { |
10641 | 0 | WOLFSSL_MSG("Peer sent ECC sig"); |
10642 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10643 | 0 | ssl->peerEccDsaKeyPresent; |
10644 | 0 | } |
10645 | 0 | #endif |
10646 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10647 | 0 | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
10648 | 0 | WOLFSSL_MSG("Peer sent SM2 sig"); |
10649 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
10650 | 0 | ssl->peerEccDsaKeyPresent; |
10651 | 0 | } |
10652 | 0 | #endif |
10653 | | #ifdef HAVE_FALCON |
10654 | | if (ssl->options.peerSigAlgo == falcon_level1_sa_algo) { |
10655 | | WOLFSSL_MSG("Peer sent Falcon Level 1 sig"); |
10656 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10657 | | ssl->peerFalconKeyPresent; |
10658 | | } |
10659 | | if (ssl->options.peerSigAlgo == falcon_level5_sa_algo) { |
10660 | | WOLFSSL_MSG("Peer sent Falcon Level 5 sig"); |
10661 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
10662 | | ssl->peerFalconKeyPresent; |
10663 | | } |
10664 | | #endif |
10665 | | #ifdef HAVE_DILITHIUM |
10666 | | if (ssl->options.peerSigAlgo == dilithium_level2_sa_algo) { |
10667 | | WOLFSSL_MSG("Peer sent Dilithium Level 2 sig"); |
10668 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10669 | | ssl->peerDilithiumKeyPresent; |
10670 | | } |
10671 | | if (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) { |
10672 | | WOLFSSL_MSG("Peer sent Dilithium Level 3 sig"); |
10673 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10674 | | ssl->peerDilithiumKeyPresent; |
10675 | | } |
10676 | | if (ssl->options.peerSigAlgo == dilithium_level5_sa_algo) { |
10677 | | WOLFSSL_MSG("Peer sent Dilithium Level 5 sig"); |
10678 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
10679 | | ssl->peerDilithiumKeyPresent; |
10680 | | } |
10681 | | #endif |
10682 | 0 | #ifndef NO_RSA |
10683 | 0 | if (ssl->options.peerSigAlgo == rsa_sa_algo) { |
10684 | 0 | WOLFSSL_MSG("Peer sent PKCS#1.5 algo - not valid TLS 1.3"); |
10685 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10686 | 0 | } |
10687 | 0 | if (ssl->options.peerSigAlgo == rsa_pss_sa_algo) { |
10688 | 0 | WOLFSSL_MSG("Peer sent RSA sig"); |
10689 | 0 | validSigAlgo = (ssl->peerRsaKey != NULL) && |
10690 | 0 | ssl->peerRsaKeyPresent; |
10691 | 0 | } |
10692 | 0 | #endif |
10693 | 0 | if (!validSigAlgo) { |
10694 | 0 | WOLFSSL_MSG("Sig algo doesn't correspond to certificate"); |
10695 | 0 | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
10696 | 0 | } |
10697 | | |
10698 | 0 | args->sigSz = args->sz; |
10699 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10700 | | if (ssl->sigSpec != NULL && |
10701 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10702 | | /* In case we received two signatures, both of them are encoded |
10703 | | * with their size as 16-bit integeter prior in memory. Hence, |
10704 | | * we can decode both lengths here now. */ |
10705 | | word32 tmpIdx = args->idx; |
10706 | | word16 tmpSz = 0; |
10707 | | if (args->sz < OPAQUE16_LEN) { |
10708 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10709 | | } |
10710 | | ato16(input + tmpIdx, &tmpSz); |
10711 | | args->sigSz = tmpSz; |
10712 | | |
10713 | | tmpIdx += OPAQUE16_LEN + args->sigSz; |
10714 | | if (tmpIdx - args->idx + OPAQUE16_LEN > args->sz) { |
10715 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10716 | | } |
10717 | | ato16(input + tmpIdx, &tmpSz); |
10718 | | args->altSignatureSz = tmpSz; |
10719 | | |
10720 | | if (args->sz != (args->sigSz + args->altSignatureSz + |
10721 | | OPAQUE16_LEN + OPAQUE16_LEN)) { |
10722 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10723 | | } |
10724 | | } |
10725 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10726 | |
|
10727 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
10728 | | /* In case we have to verify an RSA signature, we have to store the |
10729 | | * signature in the 'rsaSigBuf' structure for further processing. |
10730 | | */ |
10731 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
10732 | 0 | word32 sigSz = args->sigSz; |
10733 | 0 | sig = input + args->idx; |
10734 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10735 | | /* Check if our alternative signature was RSA */ |
10736 | | if (ssl->sigSpec != NULL && |
10737 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10738 | | if (ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
10739 | | /* We have to skip the first signature (length field |
10740 | | * and signature itself) and the length field of the |
10741 | | * alternative signature. */ |
10742 | | sig += OPAQUE16_LEN + OPAQUE16_LEN + args->sigSz; |
10743 | | sigSz = args->altSignatureSz; |
10744 | | } |
10745 | | else { |
10746 | | /* We have to skip the length field */ |
10747 | | sig += OPAQUE16_LEN; |
10748 | | } |
10749 | | } |
10750 | | #endif |
10751 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(sigSz, ssl->heap, |
10752 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10753 | 0 | if (rsaSigBuf->buffer == NULL) { |
10754 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10755 | 0 | } |
10756 | 0 | rsaSigBuf->length = sigSz; |
10757 | 0 | XMEMCPY(rsaSigBuf->buffer, sig, rsaSigBuf->length); |
10758 | 0 | } |
10759 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
10760 | | |
10761 | 0 | args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10762 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10763 | 0 | if (args->sigData == NULL) { |
10764 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
10765 | 0 | } |
10766 | | |
10767 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1); |
10768 | 0 | if (ret < 0) |
10769 | 0 | goto exit_dcv; |
10770 | | |
10771 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10772 | | if ((ssl->sigSpec != NULL) && |
10773 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
10774 | | args->altSigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
10775 | | DYNAMIC_TYPE_SIGNATURE); |
10776 | | if (args->altSigData == NULL) { |
10777 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10778 | | } |
10779 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
10780 | | args->altSigDataSz = args->sigDataSz; |
10781 | | } |
10782 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10783 | | |
10784 | 0 | #ifdef HAVE_ECC |
10785 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10786 | 0 | (ssl->peerEccDsaKeyPresent)) { |
10787 | 0 | ret = CreateECCEncodedSig(args->sigData, |
10788 | 0 | args->sigDataSz, ssl->options.peerHashAlgo); |
10789 | 0 | if (ret < 0) |
10790 | 0 | goto exit_dcv; |
10791 | 0 | args->sigDataSz = (word16)ret; |
10792 | 0 | ret = 0; |
10793 | 0 | } |
10794 | | |
10795 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10796 | | if ((ssl->sigSpec != NULL) && |
10797 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
10798 | | (args->altSigAlgo == ecc_dsa_sa_algo) && |
10799 | | (ssl->peerEccDsaKeyPresent)) { |
10800 | | ret = CreateECCEncodedSig(args->altSigData, |
10801 | | args->altSigDataSz, ssl->options.peerHashAlgo); |
10802 | | if (ret < 0) |
10803 | | goto exit_dcv; |
10804 | | args->altSigDataSz = (word16)ret; |
10805 | | ret = 0; |
10806 | | } |
10807 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10808 | 0 | #endif /* HAVE_ECC */ |
10809 | | |
10810 | | /* Advance state and proceed */ |
10811 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
10812 | 0 | } /* case TLS_ASYNC_BUILD */ |
10813 | 0 | FALL_THROUGH; |
10814 | |
|
10815 | 0 | case TLS_ASYNC_DO: |
10816 | 0 | { |
10817 | 0 | sig = input + args->idx; |
10818 | 0 | (void)sig; |
10819 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10820 | | if (ssl->sigSpec != NULL && |
10821 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10822 | | /* As we have two signatures in the message, we stored |
10823 | | * the length of each before the actual signature. This |
10824 | | * is necessary, as we could have two algorithms with |
10825 | | * variable length signatures. */ |
10826 | | sig += OPAQUE16_LEN; |
10827 | | } |
10828 | | #endif |
10829 | 0 | #ifndef NO_RSA |
10830 | 0 | if ((ssl->options.peerSigAlgo == rsa_pss_sa_algo) && |
10831 | 0 | (ssl->peerRsaKey != NULL) && (ssl->peerRsaKeyPresent != 0)) { |
10832 | 0 | WOLFSSL_MSG("Doing RSA peer cert verify"); |
10833 | 0 | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
10834 | 0 | (word32)rsaSigBuf->length, &args->output, |
10835 | 0 | ssl->options.peerSigAlgo, |
10836 | 0 | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
10837 | | #ifdef HAVE_PK_CALLBACKS |
10838 | | &ssl->buffers.peerRsaKey |
10839 | | #else |
10840 | 0 | NULL |
10841 | 0 | #endif |
10842 | 0 | ); |
10843 | 0 | if (ret >= 0) { |
10844 | 0 | args->sendSz = (word32)ret; |
10845 | 0 | ret = 0; |
10846 | 0 | } |
10847 | 0 | } |
10848 | 0 | #endif /* !NO_RSA */ |
10849 | 0 | #ifdef HAVE_ECC |
10850 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
10851 | 0 | ssl->peerEccDsaKeyPresent) { |
10852 | 0 | WOLFSSL_MSG("Doing ECC peer cert verify"); |
10853 | 0 | ret = EccVerify(ssl, sig, args->sigSz, |
10854 | 0 | args->sigData, args->sigDataSz, |
10855 | 0 | ssl->peerEccDsaKey, |
10856 | | #ifdef HAVE_PK_CALLBACKS |
10857 | | &ssl->buffers.peerEccDsaKey |
10858 | | #else |
10859 | 0 | NULL |
10860 | 0 | #endif |
10861 | 0 | ); |
10862 | |
|
10863 | 0 | if (ret >= 0) { |
10864 | | /* CLIENT/SERVER: data verified with public key from |
10865 | | * certificate. */ |
10866 | 0 | ssl->options.peerAuthGood = 1; |
10867 | |
|
10868 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10869 | 0 | ssl->peerEccDsaKeyPresent = 0; |
10870 | 0 | } |
10871 | 0 | } |
10872 | 0 | #endif /* HAVE_ECC */ |
10873 | 0 | #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10874 | 0 | if ((ssl->options.peerSigAlgo == sm2_sa_algo) && |
10875 | 0 | ssl->peerEccDsaKeyPresent) { |
10876 | 0 | WOLFSSL_MSG("Doing SM2/SM3 peer cert verify"); |
10877 | 0 | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, TLS13_SM2_SIG_ID_SZ, |
10878 | 0 | sig, args->sigSz, args->sigData, args->sigDataSz, |
10879 | 0 | ssl->peerEccDsaKey, NULL); |
10880 | 0 | if (ret >= 0) { |
10881 | | /* CLIENT/SERVER: data verified with public key from |
10882 | | * certificate. */ |
10883 | 0 | ssl->options.peerAuthGood = 1; |
10884 | |
|
10885 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10886 | 0 | ssl->peerEccDsaKeyPresent = 0; |
10887 | 0 | } |
10888 | 0 | } |
10889 | 0 | #endif |
10890 | 0 | #ifdef HAVE_ED25519 |
10891 | 0 | if ((ssl->options.peerSigAlgo == ed25519_sa_algo) && |
10892 | 0 | (ssl->peerEd25519KeyPresent)) { |
10893 | 0 | WOLFSSL_MSG("Doing ED25519 peer cert verify"); |
10894 | 0 | ret = Ed25519Verify(ssl, sig, args->sigSz, |
10895 | 0 | args->sigData, args->sigDataSz, |
10896 | 0 | ssl->peerEd25519Key, |
10897 | | #ifdef HAVE_PK_CALLBACKS |
10898 | | &ssl->buffers.peerEd25519Key |
10899 | | #else |
10900 | 0 | NULL |
10901 | 0 | #endif |
10902 | 0 | ); |
10903 | |
|
10904 | 0 | if (ret >= 0) { |
10905 | | /* CLIENT/SERVER: data verified with public key from |
10906 | | * certificate. */ |
10907 | 0 | ssl->options.peerAuthGood = 1; |
10908 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED25519, |
10909 | 0 | (void**)&ssl->peerEd25519Key); |
10910 | 0 | ssl->peerEd25519KeyPresent = 0; |
10911 | 0 | } |
10912 | 0 | } |
10913 | 0 | #endif |
10914 | 0 | #ifdef HAVE_ED448 |
10915 | 0 | if ((ssl->options.peerSigAlgo == ed448_sa_algo) && |
10916 | 0 | (ssl->peerEd448KeyPresent)) { |
10917 | 0 | WOLFSSL_MSG("Doing ED448 peer cert verify"); |
10918 | 0 | ret = Ed448Verify(ssl, sig, args->sigSz, |
10919 | 0 | args->sigData, args->sigDataSz, |
10920 | 0 | ssl->peerEd448Key, |
10921 | | #ifdef HAVE_PK_CALLBACKS |
10922 | | &ssl->buffers.peerEd448Key |
10923 | | #else |
10924 | 0 | NULL |
10925 | 0 | #endif |
10926 | 0 | ); |
10927 | |
|
10928 | 0 | if (ret >= 0) { |
10929 | | /* CLIENT/SERVER: data verified with public key from |
10930 | | * certificate. */ |
10931 | 0 | ssl->options.peerAuthGood = 1; |
10932 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED448, |
10933 | 0 | (void**)&ssl->peerEd448Key); |
10934 | 0 | ssl->peerEd448KeyPresent = 0; |
10935 | 0 | } |
10936 | 0 | } |
10937 | 0 | #endif |
10938 | | #if defined(HAVE_FALCON) |
10939 | | if (((ssl->options.peerSigAlgo == falcon_level1_sa_algo) || |
10940 | | (ssl->options.peerSigAlgo == falcon_level5_sa_algo)) && |
10941 | | (ssl->peerFalconKeyPresent)) { |
10942 | | int res = 0; |
10943 | | WOLFSSL_MSG("Doing Falcon peer cert verify"); |
10944 | | ret = wc_falcon_verify_msg(sig, args->sigSz, |
10945 | | args->sigData, args->sigDataSz, |
10946 | | &res, ssl->peerFalconKey); |
10947 | | |
10948 | | if ((ret >= 0) && (res == 1)) { |
10949 | | /* CLIENT/SERVER: data verified with public key from |
10950 | | * certificate. */ |
10951 | | ssl->options.peerAuthGood = 1; |
10952 | | |
10953 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
10954 | | (void**)&ssl->peerFalconKey); |
10955 | | ssl->peerFalconKeyPresent = 0; |
10956 | | } |
10957 | | else if ((ret >= 0) && (res == 0)) { |
10958 | | WOLFSSL_MSG("Falcon signature verification failed"); |
10959 | | ret = SIG_VERIFY_E; |
10960 | | } |
10961 | | } |
10962 | | #endif /* HAVE_FALCON */ |
10963 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
10964 | | if (((ssl->options.peerSigAlgo == dilithium_level2_sa_algo) || |
10965 | | (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) || |
10966 | | (ssl->options.peerSigAlgo == dilithium_level5_sa_algo)) && |
10967 | | (ssl->peerDilithiumKeyPresent)) { |
10968 | | int res = 0; |
10969 | | WOLFSSL_MSG("Doing Dilithium peer cert verify"); |
10970 | | ret = wc_dilithium_verify_ctx_msg(sig, args->sigSz, NULL, 0, |
10971 | | args->sigData, args->sigDataSz, |
10972 | | &res, ssl->peerDilithiumKey); |
10973 | | |
10974 | | if ((ret >= 0) && (res == 1)) { |
10975 | | /* CLIENT/SERVER: data verified with public key from |
10976 | | * certificate. */ |
10977 | | ssl->options.peerAuthGood = 1; |
10978 | | |
10979 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10980 | | (void**)&ssl->peerDilithiumKey); |
10981 | | ssl->peerDilithiumKeyPresent = 0; |
10982 | | } |
10983 | | else if ((ret >= 0) && (res == 0)) { |
10984 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
10985 | | ret = SIG_VERIFY_E; |
10986 | | } |
10987 | | } |
10988 | | #endif /* HAVE_DILITHIUM */ |
10989 | | |
10990 | | /* Check for error */ |
10991 | 0 | if (ret != 0) { |
10992 | 0 | goto exit_dcv; |
10993 | 0 | } |
10994 | | |
10995 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10996 | | if (ssl->sigSpec != NULL && |
10997 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10998 | | /* Move forward to the alternative signature. */ |
10999 | | sig += args->sigSz + OPAQUE16_LEN; |
11000 | | |
11001 | | /* Verify the alternative signature */ |
11002 | | #ifndef NO_RSA |
11003 | | if ((args->altSigAlgo == rsa_pss_sa_algo) && |
11004 | | (ssl->peerRsaKey != NULL) && |
11005 | | (ssl->peerRsaKeyPresent != 0)) { |
11006 | | WOLFSSL_MSG("Doing RSA peer cert alt verify"); |
11007 | | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
11008 | | (word32)rsaSigBuf->length, |
11009 | | &args->output, args->altSigAlgo, |
11010 | | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
11011 | | #ifdef HAVE_PK_CALLBACKS |
11012 | | &ssl->buffers.peerRsaKey |
11013 | | #else |
11014 | | NULL |
11015 | | #endif |
11016 | | ); |
11017 | | if (ret >= 0) { |
11018 | | args->sendSz = ret; |
11019 | | ret = 0; |
11020 | | } |
11021 | | } |
11022 | | #endif /* !NO_RSA */ |
11023 | | #ifdef HAVE_ECC |
11024 | | if ((args->altSigAlgo == ecc_dsa_sa_algo) && |
11025 | | (ssl->peerEccDsaKeyPresent)) { |
11026 | | WOLFSSL_MSG("Doing ECC peer cert alt verify"); |
11027 | | ret = EccVerify(ssl, sig, args->altSignatureSz, |
11028 | | args->altSigData, args->altSigDataSz, |
11029 | | ssl->peerEccDsaKey, |
11030 | | #ifdef HAVE_PK_CALLBACKS |
11031 | | &ssl->buffers.peerEccDsaKey |
11032 | | #else |
11033 | | NULL |
11034 | | #endif |
11035 | | ); |
11036 | | |
11037 | | if (ret >= 0) { |
11038 | | /* CLIENT/SERVER: data verified with public key from |
11039 | | * certificate. */ |
11040 | | args->altPeerAuthGood = 1; |
11041 | | |
11042 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
11043 | | (void**)&ssl->peerEccDsaKey); |
11044 | | ssl->peerEccDsaKeyPresent = 0; |
11045 | | } |
11046 | | } |
11047 | | #endif /* HAVE_ECC */ |
11048 | | #if defined(HAVE_FALCON) |
11049 | | if (((args->altSigAlgo == falcon_level1_sa_algo) || |
11050 | | (args->altSigAlgo == falcon_level5_sa_algo)) && |
11051 | | (ssl->peerFalconKeyPresent)) { |
11052 | | int res = 0; |
11053 | | WOLFSSL_MSG("Doing Falcon peer cert alt verify"); |
11054 | | ret = wc_falcon_verify_msg(sig, args->altSignatureSz, |
11055 | | args->altSigData, args->altSigDataSz, |
11056 | | &res, ssl->peerFalconKey); |
11057 | | |
11058 | | if ((ret >= 0) && (res == 1)) { |
11059 | | /* CLIENT/SERVER: data verified with public key from |
11060 | | * certificate. */ |
11061 | | args->altPeerAuthGood = 1; |
11062 | | |
11063 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
11064 | | (void**)&ssl->peerFalconKey); |
11065 | | ssl->peerFalconKeyPresent = 0; |
11066 | | } |
11067 | | else if ((ret >= 0) && (res == 0)) { |
11068 | | WOLFSSL_MSG("Falcon signature verification failed"); |
11069 | | ret = SIG_VERIFY_E; |
11070 | | } |
11071 | | } |
11072 | | #endif /* HAVE_FALCON */ |
11073 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
11074 | | if (((args->altSigAlgo == dilithium_level2_sa_algo) || |
11075 | | (args->altSigAlgo == dilithium_level3_sa_algo) || |
11076 | | (args->altSigAlgo == dilithium_level5_sa_algo)) && |
11077 | | (ssl->peerDilithiumKeyPresent)) { |
11078 | | int res = 0; |
11079 | | WOLFSSL_MSG("Doing Dilithium peer cert alt verify"); |
11080 | | ret = wc_dilithium_verify_ctx_msg(sig, args->altSignatureSz, |
11081 | | NULL, 0, args->altSigData, |
11082 | | args->altSigDataSz, &res, |
11083 | | ssl->peerDilithiumKey); |
11084 | | |
11085 | | if ((ret >= 0) && (res == 1)) { |
11086 | | /* CLIENT/SERVER: data verified with public key from |
11087 | | * certificate. */ |
11088 | | args->altPeerAuthGood = 1; |
11089 | | |
11090 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
11091 | | (void**)&ssl->peerDilithiumKey); |
11092 | | ssl->peerDilithiumKeyPresent = 0; |
11093 | | } |
11094 | | else if ((ret >= 0) && (res == 0)) { |
11095 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
11096 | | ret = SIG_VERIFY_E; |
11097 | | } |
11098 | | } |
11099 | | #endif /* HAVE_DILITHIUM */ |
11100 | | |
11101 | | /* Check for error */ |
11102 | | if (ret != 0) { |
11103 | | goto exit_dcv; |
11104 | | } |
11105 | | } |
11106 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11107 | | |
11108 | | /* Advance state and proceed */ |
11109 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
11110 | 0 | } /* case TLS_ASYNC_DO */ |
11111 | 0 | FALL_THROUGH; |
11112 | |
|
11113 | 0 | case TLS_ASYNC_VERIFY: |
11114 | 0 | { |
11115 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
11116 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
11117 | 0 | int sigAlgo = ssl->options.peerSigAlgo; |
11118 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11119 | | /* Check if our alternative signature was RSA */ |
11120 | | if (ssl->sigSpec != NULL && |
11121 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11122 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
11123 | | sigAlgo = args->altSigAlgo; |
11124 | | } |
11125 | | #endif |
11126 | 0 | ret = CheckRSASignature(ssl, sigAlgo, |
11127 | 0 | ssl->options.peerHashAlgo, args->output, args->sendSz); |
11128 | 0 | if (ret != 0) |
11129 | 0 | goto exit_dcv; |
11130 | | |
11131 | | /* CLIENT/SERVER: data verified with public key from |
11132 | | * certificate. */ |
11133 | 0 | ssl->peerRsaKeyPresent = 0; |
11134 | 0 | FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
11135 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11136 | | /* Check if our alternative signature was RSA */ |
11137 | | if (ssl->sigSpec != NULL && |
11138 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11139 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
11140 | | args->altPeerAuthGood = 1; |
11141 | | } |
11142 | | else |
11143 | | #endif |
11144 | 0 | ssl->options.peerAuthGood = 1; |
11145 | 0 | } |
11146 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
11147 | | |
11148 | | /* Advance state and proceed */ |
11149 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
11150 | 0 | } /* case TLS_ASYNC_VERIFY */ |
11151 | 0 | FALL_THROUGH; |
11152 | |
|
11153 | 0 | case TLS_ASYNC_FINALIZE: |
11154 | 0 | { |
11155 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11156 | | if (ssl->options.peerAuthGood && |
11157 | | ssl->sigSpec != NULL && |
11158 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11159 | | ssl->options.peerAuthGood = args->altPeerAuthGood; |
11160 | | } |
11161 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11162 | 0 | ssl->options.havePeerVerify = 1; |
11163 | | |
11164 | | /* Set final index */ |
11165 | 0 | args->idx += args->sz; |
11166 | 0 | *inOutIdx = args->idx; |
11167 | | |
11168 | | /* Encryption is always on: add padding */ |
11169 | 0 | *inOutIdx += ssl->keys.padSz; |
11170 | | |
11171 | | /* Advance state and proceed */ |
11172 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
11173 | |
|
11174 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
11175 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11176 | 0 | ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE; |
11177 | 0 | #endif |
11178 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
11179 | 0 | FALL_THROUGH; |
11180 | |
|
11181 | 0 | case TLS_ASYNC_END: |
11182 | 0 | { |
11183 | 0 | break; |
11184 | 0 | } |
11185 | | |
11186 | 0 | default: |
11187 | 0 | ret = INPUT_CASE_ERROR; |
11188 | 0 | } /* switch(ssl->options.asyncState) */ |
11189 | | |
11190 | 0 | exit_dcv: |
11191 | |
|
11192 | 0 | WOLFSSL_LEAVE("DoTls13CertificateVerify", ret); |
11193 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO); |
11194 | |
|
11195 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11196 | | /* Handle async operation */ |
11197 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
11198 | | /* Mark message as not received so it can process again */ |
11199 | | ssl->msgsReceived.got_certificate_verify = 0; |
11200 | | |
11201 | | return ret; |
11202 | | } |
11203 | | else |
11204 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
11205 | 0 | if (ret != 0) { |
11206 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
11207 | |
|
11208 | 0 | if (ret != WC_NO_ERR_TRACE(INVALID_PARAMETER)) { |
11209 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
11210 | 0 | } |
11211 | 0 | } |
11212 | | |
11213 | | /* Final cleanup */ |
11214 | 0 | FreeDcv13Args(ssl, args); |
11215 | 0 | FreeKeyExchange(ssl); |
11216 | 0 | #ifdef WOLFSSL_ASYNC_IO |
11217 | | /* Cleanup async */ |
11218 | 0 | FreeAsyncCtx(ssl, 0); |
11219 | 0 | #endif |
11220 | |
|
11221 | 0 | return ret; |
11222 | 0 | } |
11223 | | #endif /* !NO_RSA || HAVE_ECC */ |
11224 | | #endif /* !NO_CERTS */ |
11225 | | |
11226 | | /* Parse and handle a TLS v1.3 Finished message. |
11227 | | * |
11228 | | * ssl The SSL/TLS object. |
11229 | | * input The message buffer. |
11230 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11231 | | * On exit, the index of byte after the Finished message and padding. |
11232 | | * size Length of message data. |
11233 | | * totalSz Length of remaining data in the message buffer. |
11234 | | * sniff Indicates whether we are sniffing packets. |
11235 | | * returns 0 on success and otherwise failure. |
11236 | | */ |
11237 | | int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11238 | | word32 size, word32 totalSz, int sniff) |
11239 | 0 | { |
11240 | 0 | int ret; |
11241 | 0 | word32 finishedSz = 0; |
11242 | 0 | byte* secret; |
11243 | 0 | byte mac[WC_MAX_DIGEST_SIZE]; |
11244 | |
|
11245 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_DO); |
11246 | 0 | WOLFSSL_ENTER("DoTls13Finished"); |
11247 | |
|
11248 | 0 | #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH) |
11249 | | /* verify the client sent certificate if required */ |
11250 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && !ssl->options.resuming && |
11251 | 0 | (ssl->options.mutualAuth || ssl->options.failNoCert)) { |
11252 | | #ifdef OPENSSL_COMPATIBLE_DEFAULTS |
11253 | | if (ssl->options.isPSK) { |
11254 | | WOLFSSL_MSG("TLS v1.3 client used PSK but cert required. Allowing " |
11255 | | "for OpenSSL compatibility"); |
11256 | | } |
11257 | | else |
11258 | | #endif |
11259 | 0 | if ( |
11260 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
11261 | | !ssl->options.verifyPostHandshake && |
11262 | | #endif |
11263 | 0 | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
11264 | 0 | ret = NO_PEER_CERT; /* NO_PEER_VERIFY */ |
11265 | 0 | WOLFSSL_MSG("TLS v1.3 client did not present peer cert"); |
11266 | 0 | DoCertFatalAlert(ssl, ret); |
11267 | 0 | return ret; |
11268 | 0 | } |
11269 | 0 | } |
11270 | 0 | #endif |
11271 | | |
11272 | | /* check against totalSz */ |
11273 | 0 | if (*inOutIdx + size > totalSz) |
11274 | 0 | return BUFFER_E; |
11275 | | |
11276 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11277 | | ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz); |
11278 | | if (ret == 0) { |
11279 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11280 | | return ret; |
11281 | | } |
11282 | | if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) { |
11283 | | SendAlert(ssl, alert_fatal, decrypt_error); |
11284 | | return ret; |
11285 | | } |
11286 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11287 | | /* other errors */ |
11288 | | return ret; |
11289 | | } |
11290 | | ret = 0; |
11291 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11292 | | |
11293 | 0 | if (ssl->options.handShakeDone) { |
11294 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11295 | 0 | ssl->keys.client_write_MAC_secret, |
11296 | 0 | WOLFSSL_CLIENT_END); |
11297 | 0 | if (ret != 0) |
11298 | 0 | return ret; |
11299 | | |
11300 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11301 | 0 | } |
11302 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11303 | | /* All the handshake messages have been received to calculate |
11304 | | * client and server finished keys. |
11305 | | */ |
11306 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11307 | 0 | ssl->keys.client_write_MAC_secret, |
11308 | 0 | WOLFSSL_CLIENT_END); |
11309 | 0 | if (ret != 0) |
11310 | 0 | return ret; |
11311 | | |
11312 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11313 | 0 | ssl->keys.server_write_MAC_secret, |
11314 | 0 | WOLFSSL_SERVER_END); |
11315 | 0 | if (ret != 0) |
11316 | 0 | return ret; |
11317 | | |
11318 | 0 | secret = ssl->keys.server_write_MAC_secret; |
11319 | 0 | } |
11320 | 0 | else { |
11321 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11322 | 0 | } |
11323 | | |
11324 | 0 | if (sniff == NO_SNIFF) { |
11325 | |
|
11326 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz); |
11327 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11328 | | if (finishedSz > TLS_FINISHED_SZ_MAX) { |
11329 | | return BUFFER_ERROR; |
11330 | | } |
11331 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11332 | | XMEMCPY(ssl->serverFinished, mac, finishedSz); |
11333 | | ssl->serverFinished_len = (byte)finishedSz; |
11334 | | } |
11335 | | else { |
11336 | | XMEMCPY(ssl->clientFinished, mac, finishedSz); |
11337 | | ssl->clientFinished_len = (byte)finishedSz; |
11338 | | } |
11339 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11340 | 0 | if (ret != 0) |
11341 | 0 | return ret; |
11342 | 0 | if (size != finishedSz) |
11343 | 0 | return BUFFER_ERROR; |
11344 | 0 | } |
11345 | | |
11346 | | #ifdef WOLFSSL_CALLBACKS |
11347 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11348 | | if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo); |
11349 | | #endif |
11350 | | |
11351 | 0 | if (sniff == NO_SNIFF) { |
11352 | | /* Actually check verify data. */ |
11353 | 0 | if (size > WC_MAX_DIGEST_SIZE || |
11354 | 0 | ConstantCompare(input + *inOutIdx, mac, size) != 0){ |
11355 | 0 | WOLFSSL_MSG("Verify finished error on hashes"); |
11356 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
11357 | 0 | WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR); |
11358 | 0 | return VERIFY_FINISHED_ERROR; |
11359 | 0 | } |
11360 | 0 | } |
11361 | | |
11362 | | /* Force input exhaustion at ProcessReply by consuming padSz. */ |
11363 | 0 | *inOutIdx += size + ssl->keys.padSz; |
11364 | |
|
11365 | 0 | #ifndef NO_WOLFSSL_SERVER |
11366 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
11367 | 0 | !ssl->options.handShakeDone) { |
11368 | | #ifdef WOLFSSL_EARLY_DATA |
11369 | | if (ssl->earlyData != no_early_data) { |
11370 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0) |
11371 | | return ret; |
11372 | | } |
11373 | | #endif |
11374 | | /* Setup keys for application data messages from client. */ |
11375 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11376 | 0 | return ret; |
11377 | 0 | } |
11378 | 0 | #endif |
11379 | | |
11380 | 0 | #ifndef NO_WOLFSSL_CLIENT |
11381 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11382 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11383 | 0 | #endif |
11384 | 0 | #ifndef NO_WOLFSSL_SERVER |
11385 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11386 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11387 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
11388 | 0 | ssl->options.handShakeDone = 1; |
11389 | 0 | } |
11390 | 0 | #endif |
11391 | |
|
11392 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_EARLY_DATA) |
11393 | | if (ssl->options.dtls && ssl->earlyData > early_data_ext) { |
11394 | | /* DTLSv1.3 has no EndOfearlydata messages. We stop processing EarlyData |
11395 | | as soon we receive the client's finished message */ |
11396 | | ssl->earlyData = done_early_data; |
11397 | | } |
11398 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_EARLY_DATA */ |
11399 | | #if defined(WOLFSSL_QUIC) && defined(WOLFSSL_EARLY_DATA) |
11400 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData > early_data_ext) { |
11401 | | /* QUIC has no EndOfEarlyData messages. We stop processing EarlyData |
11402 | | as soon we receive the client's finished message */ |
11403 | | ssl->earlyData = done_early_data; |
11404 | | } |
11405 | | #endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */ |
11406 | |
|
11407 | 0 | WOLFSSL_LEAVE("DoTls13Finished", 0); |
11408 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_DO); |
11409 | |
|
11410 | 0 | return 0; |
11411 | 0 | } |
11412 | | |
11413 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
11414 | | /* Send the TLS v1.3 Finished message. |
11415 | | * |
11416 | | * ssl The SSL/TLS object. |
11417 | | * returns 0 on success, otherwise failure. |
11418 | | */ |
11419 | | static int SendTls13Finished(WOLFSSL* ssl) |
11420 | | { |
11421 | | byte finishedSz = ssl->specs.hash_size; |
11422 | | byte* input; |
11423 | | byte* output; |
11424 | | int ret; |
11425 | | int headerSz = HANDSHAKE_HEADER_SZ; |
11426 | | int outputSz; |
11427 | | byte* secret; |
11428 | | |
11429 | | #ifdef WOLFSSL_DTLS13 |
11430 | | int dtlsRet = 0, isDtls = 0; |
11431 | | #endif /* WOLFSSL_DTLS13 */ |
11432 | | |
11433 | | WOLFSSL_START(WC_FUNC_FINISHED_SEND); |
11434 | | WOLFSSL_ENTER("SendTls13Finished"); |
11435 | | |
11436 | | ssl->options.buildingMsg = 1; |
11437 | | #ifdef WOLFSSL_DTLS13 |
11438 | | if (ssl->options.dtls) { |
11439 | | headerSz = DTLS_HANDSHAKE_HEADER_SZ; |
11440 | | /* using isDtls instead of ssl->options.dtls will abide clang static |
11441 | | analyzer on using an uninitialized value */ |
11442 | | isDtls = 1; |
11443 | | } |
11444 | | #endif /* WOLFSSL_DTLS13 */ |
11445 | | |
11446 | | outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA; |
11447 | | /* Check buffers are big enough and grow if needed. */ |
11448 | | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11449 | | return ret; |
11450 | | |
11451 | | /* get output buffer */ |
11452 | | output = GetOutputBuffer(ssl); |
11453 | | input = output + RECORD_HEADER_SZ; |
11454 | | |
11455 | | #ifdef WOLFSSL_DTLS13 |
11456 | | if (isDtls) |
11457 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11458 | | #endif /* WOLFSSL_DTLS13 */ |
11459 | | |
11460 | | AddTls13HandShakeHeader(input, (word32)finishedSz, 0, (word32)finishedSz, |
11461 | | finished, ssl); |
11462 | | |
11463 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11464 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11465 | | ret = tsip_Tls13SendFinished(ssl, output, outputSz, input, 1); |
11466 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11467 | | return ret; |
11468 | | } |
11469 | | ret = 0; |
11470 | | } |
11471 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11472 | | |
11473 | | /* make finished hashes */ |
11474 | | if (ssl->options.handShakeDone) { |
11475 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11476 | | ssl->keys.client_write_MAC_secret, |
11477 | | WOLFSSL_CLIENT_END); |
11478 | | if (ret != 0) |
11479 | | return ret; |
11480 | | |
11481 | | secret = ssl->keys.client_write_MAC_secret; |
11482 | | } |
11483 | | else if (ssl->options.side == WOLFSSL_CLIENT_END) |
11484 | | secret = ssl->keys.client_write_MAC_secret; |
11485 | | else { |
11486 | | /* All the handshake messages have been done to calculate client and |
11487 | | * server finished keys. |
11488 | | */ |
11489 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11490 | | ssl->keys.client_write_MAC_secret, |
11491 | | WOLFSSL_SERVER_END); |
11492 | | if (ret != 0) |
11493 | | return ret; |
11494 | | |
11495 | | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11496 | | ssl->keys.server_write_MAC_secret, |
11497 | | WOLFSSL_CLIENT_END); |
11498 | | if (ret != 0) |
11499 | | return ret; |
11500 | | |
11501 | | secret = ssl->keys.server_write_MAC_secret; |
11502 | | } |
11503 | | ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL); |
11504 | | if (ret != 0) |
11505 | | return ret; |
11506 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11507 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11508 | | XMEMCPY(ssl->clientFinished, &input[headerSz], finishedSz); |
11509 | | ssl->clientFinished_len = finishedSz; |
11510 | | } |
11511 | | else { |
11512 | | XMEMCPY(ssl->serverFinished, &input[headerSz], finishedSz); |
11513 | | ssl->serverFinished_len = finishedSz; |
11514 | | } |
11515 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11516 | | |
11517 | | #ifdef WOLFSSL_DTLS13 |
11518 | | if (isDtls) { |
11519 | | dtlsRet = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11520 | | (word16)(Dtls13GetRlHeaderLength(ssl, 1) + headerSz + finishedSz), finished, |
11521 | | 1); |
11522 | | if (dtlsRet != 0 && dtlsRet != WC_NO_ERR_TRACE(WANT_WRITE)) |
11523 | | return ret; |
11524 | | |
11525 | | } else |
11526 | | #endif /* WOLFSSL_DTLS13 */ |
11527 | | { |
11528 | | /* This message is always encrypted. */ |
11529 | | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11530 | | headerSz + finishedSz, handshake, 1, 0, 0); |
11531 | | if (sendSz < 0) { |
11532 | | WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR); |
11533 | | return BUILD_MSG_ERROR; |
11534 | | } |
11535 | | |
11536 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11537 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11538 | | if (ssl->toInfoOn) { |
11539 | | ret = AddPacketInfo(ssl, "Finished", handshake, output, sendSz, |
11540 | | WRITE_PROTO, 0, ssl->heap); |
11541 | | if (ret != 0) |
11542 | | return ret; |
11543 | | } |
11544 | | #endif |
11545 | | |
11546 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11547 | | ssl->options.buildingMsg = 0; |
11548 | | } |
11549 | | |
11550 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11551 | | #ifdef WOLFSSL_EARLY_DATA |
11552 | | byte storeTrafficDecKeys = ssl->earlyData == no_early_data; |
11553 | | #endif |
11554 | | /* Can send application data now. */ |
11555 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
11556 | | return ret; |
11557 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
11558 | | ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz); |
11559 | | #ifdef WOLFSSL_EARLY_DATA |
11560 | | |
11561 | | #ifdef WOLFSSL_DTLS13 |
11562 | | /* DTLS13 dynamically change keys and it needs all |
11563 | | the keys in ssl->keys to save the keying material */ |
11564 | | if (isDtls) |
11565 | | storeTrafficDecKeys = 1; |
11566 | | #endif /* WOLFSSL_DTLS13 */ |
11567 | | |
11568 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11569 | | != 0) { |
11570 | | return ret; |
11571 | | } |
11572 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY, |
11573 | | storeTrafficDecKeys)) != 0) { |
11574 | | return ret; |
11575 | | } |
11576 | | #else |
11577 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE, |
11578 | | 1)) != 0) { |
11579 | | return ret; |
11580 | | } |
11581 | | #endif |
11582 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11583 | | return ret; |
11584 | | |
11585 | | #ifdef WOLFSSL_DTLS13 |
11586 | | if (isDtls) { |
11587 | | w64wrapper epochTraffic0; |
11588 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11589 | | ssl->dtls13Epoch = epochTraffic0; |
11590 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11591 | | |
11592 | | ret = Dtls13SetEpochKeys( |
11593 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11594 | | if (ret != 0) |
11595 | | return ret; |
11596 | | |
11597 | | } |
11598 | | #endif /* WOLFSSL_DTLS13 */ |
11599 | | |
11600 | | } |
11601 | | |
11602 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
11603 | | !ssl->options.handShakeDone) { |
11604 | | #ifdef WOLFSSL_EARLY_DATA |
11605 | | if (ssl->earlyData != no_early_data) { |
11606 | | if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_SIDE_ONLY, |
11607 | | 1)) != 0) { |
11608 | | return ret; |
11609 | | } |
11610 | | } |
11611 | | #endif |
11612 | | /* Setup keys for application data messages. */ |
11613 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11614 | | return ret; |
11615 | | |
11616 | | #if defined(HAVE_SESSION_TICKET) |
11617 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
11618 | | if (ret != 0) |
11619 | | return ret; |
11620 | | #endif |
11621 | | |
11622 | | #ifdef WOLFSSL_DTLS13 |
11623 | | if (isDtls) { |
11624 | | w64wrapper epochTraffic0; |
11625 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
11626 | | ssl->dtls13Epoch = epochTraffic0; |
11627 | | ssl->dtls13PeerEpoch = epochTraffic0; |
11628 | | |
11629 | | ret = Dtls13SetEpochKeys( |
11630 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
11631 | | if (ret != 0) |
11632 | | return ret; |
11633 | | |
11634 | | } |
11635 | | #endif /* WOLFSSL_DTLS13 */ |
11636 | | } |
11637 | | |
11638 | | #ifndef NO_WOLFSSL_CLIENT |
11639 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11640 | | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11641 | | ssl->options.handShakeState = HANDSHAKE_DONE; |
11642 | | ssl->options.handShakeDone = 1; |
11643 | | } |
11644 | | #endif |
11645 | | #ifndef NO_WOLFSSL_SERVER |
11646 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11647 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11648 | | } |
11649 | | #endif |
11650 | | |
11651 | | #ifdef WOLFSSL_DTLS13 |
11652 | | if (isDtls) { |
11653 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11654 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11655 | | |
11656 | | return dtlsRet; |
11657 | | } |
11658 | | #endif /* WOLFSSL_DTLS13 */ |
11659 | | |
11660 | | if ((ret = SendBuffered(ssl)) != 0) |
11661 | | return ret; |
11662 | | |
11663 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
11664 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
11665 | | |
11666 | | return ret; |
11667 | | } |
11668 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
11669 | | |
11670 | | /* handle generation TLS v1.3 key_update (24) */ |
11671 | | /* Send the TLS v1.3 KeyUpdate message. |
11672 | | * |
11673 | | * ssl The SSL/TLS object. |
11674 | | * returns 0 on success, otherwise failure. |
11675 | | */ |
11676 | | int SendTls13KeyUpdate(WOLFSSL* ssl) |
11677 | 0 | { |
11678 | 0 | byte* input; |
11679 | 0 | byte* output; |
11680 | 0 | int ret; |
11681 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
11682 | 0 | int outputSz; |
11683 | 0 | word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11684 | |
|
11685 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND); |
11686 | 0 | WOLFSSL_ENTER("SendTls13KeyUpdate"); |
11687 | |
|
11688 | | #ifdef WOLFSSL_DTLS13 |
11689 | | if (ssl->options.dtls) |
11690 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
11691 | | #endif /* WOLFSSL_DTLS13 */ |
11692 | |
|
11693 | 0 | outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA; |
11694 | | /* Check buffers are big enough and grow if needed. */ |
11695 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11696 | 0 | return ret; |
11697 | | |
11698 | | /* get output buffer */ |
11699 | 0 | output = GetOutputBuffer(ssl); |
11700 | 0 | input = output + RECORD_HEADER_SZ; |
11701 | |
|
11702 | | #ifdef WOLFSSL_DTLS13 |
11703 | | if (ssl->options.dtls) |
11704 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11705 | | #endif /* WOLFSSL_DTLS13 */ |
11706 | |
|
11707 | 0 | AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl); |
11708 | | |
11709 | | /* If: |
11710 | | * 1. I haven't sent a KeyUpdate requesting a response and |
11711 | | * 2. This isn't responding to peer KeyUpdate requiring a response then, |
11712 | | * I want a response. |
11713 | | */ |
11714 | 0 | ssl->keys.updateResponseReq = output[i++] = |
11715 | 0 | !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond; |
11716 | | /* Sent response, no longer need to respond. */ |
11717 | 0 | ssl->keys.keyUpdateRespond = 0; |
11718 | |
|
11719 | | #ifdef WOLFSSL_DTLS13 |
11720 | | if (ssl->options.dtls) { |
11721 | | ret = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11722 | | OPAQUE8_LEN + Dtls13GetRlHeaderLength(ssl, 1) + |
11723 | | DTLS_HANDSHAKE_HEADER_SZ, |
11724 | | key_update, 0); |
11725 | | } |
11726 | | else |
11727 | | #endif /* WOLFSSL_DTLS13 */ |
11728 | 0 | { |
11729 | | /* This message is always encrypted. */ |
11730 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
11731 | 0 | headerSz + OPAQUE8_LEN, handshake, 0, 0, 0); |
11732 | 0 | if (sendSz < 0) |
11733 | 0 | return BUILD_MSG_ERROR; |
11734 | | |
11735 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11736 | | if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate"); |
11737 | | if (ssl->toInfoOn) { |
11738 | | ret = AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz, |
11739 | | WRITE_PROTO, 0, ssl->heap); |
11740 | | if (ret != 0) |
11741 | | return ret; |
11742 | | } |
11743 | | #endif |
11744 | | |
11745 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
11746 | |
|
11747 | 0 | ret = SendBuffered(ssl); |
11748 | | |
11749 | |
|
11750 | 0 | if (ret != 0 && ret != WC_NO_ERR_TRACE(WANT_WRITE)) |
11751 | 0 | return ret; |
11752 | 0 | } |
11753 | | |
11754 | | /* In DTLS we must wait for the ack before setting up the new keys */ |
11755 | 0 | if (!ssl->options.dtls) { |
11756 | | |
11757 | | /* Future traffic uses new encryption keys. */ |
11758 | 0 | if ((ret = DeriveTls13Keys( |
11759 | 0 | ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
11760 | 0 | != 0) |
11761 | 0 | return ret; |
11762 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11763 | 0 | return ret; |
11764 | 0 | } |
11765 | | |
11766 | | |
11767 | 0 | WOLFSSL_LEAVE("SendTls13KeyUpdate", ret); |
11768 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND); |
11769 | |
|
11770 | 0 | return ret; |
11771 | 0 | } |
11772 | | |
11773 | | /* handle processing TLS v1.3 key_update (24) */ |
11774 | | /* Parse and handle a TLS v1.3 KeyUpdate message. |
11775 | | * |
11776 | | * ssl The SSL/TLS object. |
11777 | | * input The message buffer. |
11778 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11779 | | * On exit, the index of byte after the Finished message and padding. |
11780 | | * totalSz The length of the current handshake message. |
11781 | | * returns 0 on success and otherwise failure. |
11782 | | */ |
11783 | | static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11784 | | word32 totalSz) |
11785 | 0 | { |
11786 | 0 | int ret; |
11787 | 0 | word32 i = *inOutIdx; |
11788 | |
|
11789 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO); |
11790 | 0 | WOLFSSL_ENTER("DoTls13KeyUpdate"); |
11791 | | |
11792 | | /* check against totalSz */ |
11793 | 0 | if (OPAQUE8_LEN != totalSz) |
11794 | 0 | return BUFFER_E; |
11795 | | |
11796 | 0 | switch (input[i]) { |
11797 | 0 | case update_not_requested: |
11798 | | /* This message in response to any outstanding request. */ |
11799 | 0 | ssl->keys.keyUpdateRespond = 0; |
11800 | 0 | ssl->keys.updateResponseReq = 0; |
11801 | 0 | break; |
11802 | 0 | case update_requested: |
11803 | | /* New key update requiring a response. */ |
11804 | 0 | ssl->keys.keyUpdateRespond = 1; |
11805 | 0 | break; |
11806 | 0 | default: |
11807 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
11808 | 0 | return INVALID_PARAMETER; |
11809 | 0 | } |
11810 | | |
11811 | | /* Move index to byte after message. */ |
11812 | 0 | *inOutIdx += totalSz; |
11813 | | /* Always encrypted. */ |
11814 | 0 | *inOutIdx += ssl->keys.padSz; |
11815 | | |
11816 | | /* Future traffic uses new decryption keys. */ |
11817 | 0 | if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1)) |
11818 | 0 | != 0) { |
11819 | 0 | return ret; |
11820 | 0 | } |
11821 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11822 | 0 | return ret; |
11823 | | |
11824 | | #ifdef WOLFSSL_DTLS13 |
11825 | | if (ssl->options.dtls) { |
11826 | | w64Increment(&ssl->dtls13PeerEpoch); |
11827 | | |
11828 | | ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY); |
11829 | | if (ret != 0) |
11830 | | return ret; |
11831 | | } |
11832 | | #endif /* WOLFSSL_DTLS13 */ |
11833 | | |
11834 | 0 | if (ssl->keys.keyUpdateRespond) { |
11835 | |
|
11836 | | #ifdef WOLFSSL_DTLS13 |
11837 | | /* we already sent a keyUpdate (either in response to a previous |
11838 | | KeyUpdate or initiated by the application) and we are waiting for the |
11839 | | ack. We can't send a new KeyUpdate right away but to honor the RFC we |
11840 | | should send another KeyUpdate after the one in-flight is acked. We |
11841 | | don't do that as it looks redundant, it will make the code more |
11842 | | complex and I don't see a good use case for that. */ |
11843 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) { |
11844 | | ssl->keys.keyUpdateRespond = 0; |
11845 | | return 0; |
11846 | | } |
11847 | | #endif /* WOLFSSL_DTLS13 */ |
11848 | |
|
11849 | | #if defined(HAVE_WRITE_DUP) && defined(WOLFSSL_TLS13) |
11850 | | /* Read side cannot write; delegate the response to the write side. */ |
11851 | | if (ssl->dupWrite != NULL && ssl->dupSide == READ_DUP_SIDE) { |
11852 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
11853 | | return BAD_MUTEX_E; |
11854 | | ssl->dupWrite->keyUpdateRespond = 1; |
11855 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
11856 | | ssl->keys.keyUpdateRespond = 0; |
11857 | | return 0; |
11858 | | } |
11859 | | #endif /* HAVE_WRITE_DUP && WOLFSSL_TLS13 */ |
11860 | |
|
11861 | 0 | #ifndef WOLFSSL_RW_THREADED |
11862 | 0 | return SendTls13KeyUpdate(ssl); |
11863 | | #else |
11864 | | ssl->options.sendKeyUpdate = 1; |
11865 | | return 0; |
11866 | | #endif |
11867 | 0 | } |
11868 | | |
11869 | 0 | WOLFSSL_LEAVE("DoTls13KeyUpdate", ret); |
11870 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO); |
11871 | |
|
11872 | 0 | return 0; |
11873 | 0 | } |
11874 | | |
11875 | | #ifdef WOLFSSL_EARLY_DATA |
11876 | | #ifndef NO_WOLFSSL_CLIENT |
11877 | | /* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no |
11878 | | * more early application data. |
11879 | | * The encryption key now changes to the pre-calculated handshake key. |
11880 | | * |
11881 | | * ssl The SSL/TLS object. |
11882 | | * returns 0 on success and otherwise failure. |
11883 | | */ |
11884 | | static int SendTls13EndOfEarlyData(WOLFSSL* ssl) |
11885 | | { |
11886 | | byte* output; |
11887 | | int ret; |
11888 | | int sendSz; |
11889 | | word32 length; |
11890 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11891 | | |
11892 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11893 | | WOLFSSL_ENTER("SendTls13EndOfEarlyData"); |
11894 | | |
11895 | | length = 0; |
11896 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
11897 | | ssl->options.buildingMsg = 1; |
11898 | | |
11899 | | /* Check buffers are big enough and grow if needed. */ |
11900 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
11901 | | return ret; |
11902 | | |
11903 | | /* Get position in output buffer to write new message to. */ |
11904 | | output = GetOutputBuffer(ssl); |
11905 | | |
11906 | | /* Put the record and handshake headers on. */ |
11907 | | AddTls13Headers(output, length, end_of_early_data, ssl); |
11908 | | |
11909 | | /* This message is always encrypted. */ |
11910 | | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
11911 | | idx - RECORD_HEADER_SZ, handshake, 1, 0, 0); |
11912 | | if (sendSz < 0) |
11913 | | return sendSz; |
11914 | | |
11915 | | ssl->buffers.outputBuffer.length += sendSz; |
11916 | | |
11917 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
11918 | | return ret; |
11919 | | |
11920 | | ssl->options.buildingMsg = 0; |
11921 | | if (!ssl->options.groupMessages) |
11922 | | ret = SendBuffered(ssl); |
11923 | | |
11924 | | ssl->earlyData = done_early_data; |
11925 | | |
11926 | | WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret); |
11927 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND); |
11928 | | |
11929 | | return ret; |
11930 | | } |
11931 | | #endif /* !NO_WOLFSSL_CLIENT */ |
11932 | | |
11933 | | #ifndef NO_WOLFSSL_SERVER |
11934 | | /* handle processing of TLS 1.3 end_of_early_data (5) */ |
11935 | | /* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be |
11936 | | * no more early application data. |
11937 | | * The decryption key now changes to the pre-calculated handshake key. |
11938 | | * |
11939 | | * ssl The SSL/TLS object. |
11940 | | * returns 0 on success and otherwise failure. |
11941 | | */ |
11942 | | static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input, |
11943 | | word32* inOutIdx, word32 size) |
11944 | | { |
11945 | | int ret; |
11946 | | word32 begin = *inOutIdx; |
11947 | | |
11948 | | (void)input; |
11949 | | |
11950 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO); |
11951 | | WOLFSSL_ENTER("DoTls13EndOfEarlyData"); |
11952 | | |
11953 | | if ((*inOutIdx - begin) != size) |
11954 | | return BUFFER_ERROR; |
11955 | | |
11956 | | if (ssl->earlyData == no_early_data) { |
11957 | | WOLFSSL_MSG("EndOfEarlyData received unexpectedly"); |
11958 | | SendAlert(ssl, alert_fatal, unexpected_message); |
11959 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
11960 | | return OUT_OF_ORDER_E; |
11961 | | } |
11962 | | |
11963 | | ssl->earlyData = done_early_data; |
11964 | | |
11965 | | /* Always encrypted. */ |
11966 | | *inOutIdx += ssl->keys.padSz; |
11967 | | |
11968 | | ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY); |
11969 | | |
11970 | | WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret); |
11971 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO); |
11972 | | |
11973 | | return ret; |
11974 | | } |
11975 | | #endif /* !NO_WOLFSSL_SERVER */ |
11976 | | #endif /* WOLFSSL_EARLY_DATA */ |
11977 | | |
11978 | | #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
11979 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
11980 | | int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce, |
11981 | | byte len) |
11982 | | { |
11983 | | if (session->ticketNonce.data |
11984 | | != session->ticketNonce.dataStatic) { |
11985 | | XFREE(session->ticketNonce.data, session->heap, |
11986 | | DYNAMIC_TYPE_SESSION_TICK); |
11987 | | session->ticketNonce.data = session->ticketNonce.dataStatic; |
11988 | | session->ticketNonce.len = 0; |
11989 | | } |
11990 | | |
11991 | | if (len > MAX_TICKET_NONCE_STATIC_SZ) { |
11992 | | WOLFSSL_MSG("Using dynamic nonce buffer"); |
11993 | | session->ticketNonce.data = (byte*)XMALLOC(len, |
11994 | | session->heap, DYNAMIC_TYPE_SESSION_TICK); |
11995 | | if (session->ticketNonce.data == NULL) |
11996 | | return MEMORY_ERROR; |
11997 | | } |
11998 | | XMEMCPY(session->ticketNonce.data, nonce, len); |
11999 | | session->ticketNonce.len = len; |
12000 | | return 0; |
12001 | | } |
12002 | | #endif |
12003 | | #ifndef NO_WOLFSSL_CLIENT |
12004 | | /* Handle a New Session Ticket handshake message. |
12005 | | * Message contains the information required to perform resumption. |
12006 | | * |
12007 | | * ssl The SSL/TLS object. |
12008 | | * input The message buffer. |
12009 | | * inOutIdx On entry, the index into the message buffer of Finished. |
12010 | | * On exit, the index of byte after the Finished message and padding. |
12011 | | * size The length of the current handshake message. |
12012 | | * returns 0 on success, otherwise failure. |
12013 | | */ |
12014 | | static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input, |
12015 | | word32* inOutIdx, word32 size) |
12016 | 0 | { |
12017 | | #ifdef HAVE_SESSION_TICKET |
12018 | | int ret; |
12019 | | word32 begin = *inOutIdx; |
12020 | | word32 lifetime; |
12021 | | word32 ageAdd; |
12022 | | word16 length; |
12023 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
12024 | | word32 now; |
12025 | | #else |
12026 | | sword64 now; |
12027 | | #endif |
12028 | | const byte* nonce; |
12029 | | byte nonceLength; |
12030 | | |
12031 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO); |
12032 | | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
12033 | | |
12034 | | /* Lifetime hint. */ |
12035 | | if ((*inOutIdx - begin) + SESSION_HINT_SZ > size) |
12036 | | return BUFFER_ERROR; |
12037 | | ato32(input + *inOutIdx, &lifetime); |
12038 | | *inOutIdx += SESSION_HINT_SZ; |
12039 | | if (lifetime > MAX_LIFETIME) { |
12040 | | WOLFSSL_ERROR_VERBOSE(SERVER_HINT_ERROR); |
12041 | | return SERVER_HINT_ERROR; |
12042 | | } |
12043 | | |
12044 | | /* Age add. */ |
12045 | | if ((*inOutIdx - begin) + SESSION_ADD_SZ > size) |
12046 | | return BUFFER_ERROR; |
12047 | | ato32(input + *inOutIdx, &ageAdd); |
12048 | | *inOutIdx += SESSION_ADD_SZ; |
12049 | | |
12050 | | /* Ticket nonce. */ |
12051 | | if ((*inOutIdx - begin) + 1 > size) |
12052 | | return BUFFER_ERROR; |
12053 | | nonceLength = input[*inOutIdx]; |
12054 | | #if !defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
12055 | | (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) |
12056 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
12057 | | WOLFSSL_MSG("Nonce length not supported"); |
12058 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
12059 | | return INVALID_PARAMETER; |
12060 | | } |
12061 | | #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */ |
12062 | | *inOutIdx += 1; |
12063 | | if ((*inOutIdx - begin) + nonceLength > size) |
12064 | | return BUFFER_ERROR; |
12065 | | nonce = input + *inOutIdx; |
12066 | | *inOutIdx += nonceLength; |
12067 | | |
12068 | | /* Ticket length. */ |
12069 | | if ((*inOutIdx - begin) + LENGTH_SZ > size) |
12070 | | return BUFFER_ERROR; |
12071 | | ato16(input + *inOutIdx, &length); |
12072 | | *inOutIdx += LENGTH_SZ; |
12073 | | if ((*inOutIdx - begin) + length > size) |
12074 | | return BUFFER_ERROR; |
12075 | | |
12076 | | if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0) |
12077 | | return ret; |
12078 | | *inOutIdx += length; |
12079 | | |
12080 | | now = TimeNowInMilliseconds(); |
12081 | | if (now == 0) |
12082 | | return GETTIME_ERROR; |
12083 | | /* Copy in ticket data (server identity). */ |
12084 | | ssl->timeout = lifetime; |
12085 | | ssl->session->timeout = lifetime; |
12086 | | ssl->session->cipherSuite0 = ssl->options.cipherSuite0; |
12087 | | ssl->session->cipherSuite = ssl->options.cipherSuite; |
12088 | | ssl->session->ticketSeen = now; |
12089 | | ssl->session->ticketAdd = ageAdd; |
12090 | | #ifdef WOLFSSL_EARLY_DATA |
12091 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
12092 | | #endif |
12093 | | |
12094 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
12095 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
12096 | | ret = SessionTicketNoncePopulate(ssl->session, nonce, nonceLength); |
12097 | | if (ret != 0) |
12098 | | return ret; |
12099 | | #else |
12100 | | ssl->session->ticketNonce.len = nonceLength; |
12101 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
12102 | | ret = BUFFER_ERROR; |
12103 | | return ret; |
12104 | | } |
12105 | | if (nonceLength > 0) |
12106 | | XMEMCPY(ssl->session->ticketNonce.data, nonce, nonceLength); |
12107 | | #endif /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && FIPS_VERSION_GE(5,3) */ |
12108 | | |
12109 | | ssl->session->namedGroup = ssl->namedGroup; |
12110 | | |
12111 | | if ((*inOutIdx - begin) + EXTS_SZ > size) |
12112 | | return BUFFER_ERROR; |
12113 | | ato16(input + *inOutIdx, &length); |
12114 | | *inOutIdx += EXTS_SZ; |
12115 | | if ((*inOutIdx - begin) + length != size) |
12116 | | return BUFFER_ERROR; |
12117 | | #ifdef WOLFSSL_EARLY_DATA |
12118 | | ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket, |
12119 | | NULL); |
12120 | | if (ret != 0) |
12121 | | return ret; |
12122 | | #endif |
12123 | | *inOutIdx += length; |
12124 | | |
12125 | | SetupSession(ssl); |
12126 | | #ifndef NO_SESSION_CACHE |
12127 | | AddSession(ssl); |
12128 | | #endif |
12129 | | |
12130 | | /* Always encrypted. */ |
12131 | | *inOutIdx += ssl->keys.padSz; |
12132 | | |
12133 | | ssl->expect_session_ticket = 0; |
12134 | | #else |
12135 | 0 | (void)ssl; |
12136 | 0 | (void)input; |
12137 | |
|
12138 | 0 | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
12139 | |
|
12140 | 0 | *inOutIdx += size + ssl->keys.padSz; |
12141 | 0 | #endif /* HAVE_SESSION_TICKET */ |
12142 | |
|
12143 | 0 | WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0); |
12144 | 0 | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO); |
12145 | |
|
12146 | 0 | return 0; |
12147 | 0 | } |
12148 | | #endif /* NO_WOLFSSL_CLIENT */ |
12149 | | |
12150 | | #ifndef NO_WOLFSSL_SERVER |
12151 | | #ifdef HAVE_SESSION_TICKET |
12152 | | |
12153 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12154 | | /* Offset of the MAC size in the finished message. */ |
12155 | | #define FINISHED_MSG_SIZE_OFFSET 3 |
12156 | | |
12157 | | /* Calculate the resumption secret which includes the unseen client finished |
12158 | | * message. |
12159 | | * |
12160 | | * ssl The SSL/TLS object. |
12161 | | * returns 0 on success, otherwise failure. |
12162 | | */ |
12163 | | static int ExpectedResumptionSecret(WOLFSSL* ssl) |
12164 | | { |
12165 | | int ret; |
12166 | | word32 finishedSz = 0; |
12167 | | byte mac[WC_MAX_DIGEST_SIZE]; |
12168 | | Digest digest; |
12169 | | static byte header[] = { 0x14, 0x00, 0x00, 0x00 }; |
12170 | | |
12171 | | XMEMSET(&digest, 0, sizeof(Digest)); |
12172 | | |
12173 | | /* Copy the running hash so we can restore it after. */ |
12174 | | switch (ssl->specs.mac_algorithm) { |
12175 | | #ifndef NO_SHA256 |
12176 | | case sha256_mac: |
12177 | | ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256); |
12178 | | if (ret != 0) |
12179 | | return ret; |
12180 | | break; |
12181 | | #endif |
12182 | | #ifdef WOLFSSL_SHA384 |
12183 | | case sha384_mac: |
12184 | | ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384); |
12185 | | if (ret != 0) |
12186 | | return ret; |
12187 | | break; |
12188 | | #endif |
12189 | | #ifdef WOLFSSL_TLS13_SHA512 |
12190 | | case sha512_mac: |
12191 | | ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512); |
12192 | | if (ret != 0) |
12193 | | return ret; |
12194 | | break; |
12195 | | #endif |
12196 | | #ifdef WOLFSSL_SM3 |
12197 | | case sm3_mac: |
12198 | | ret = wc_Sm3Copy(&ssl->hsHashes->hashSm3, &digest.sm3); |
12199 | | if (ret != 0) |
12200 | | return ret; |
12201 | | break; |
12202 | | #endif |
12203 | | } |
12204 | | |
12205 | | /* Generate the Client's Finished message and hash it. */ |
12206 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac, |
12207 | | &finishedSz); |
12208 | | if (ret != 0) |
12209 | | return ret; |
12210 | | header[FINISHED_MSG_SIZE_OFFSET] = finishedSz; |
12211 | | #ifdef WOLFSSL_EARLY_DATA |
12212 | | if (ssl->earlyData != no_early_data) { |
12213 | | static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 }; |
12214 | | ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData)); |
12215 | | if (ret != 0) |
12216 | | return ret; |
12217 | | } |
12218 | | #endif |
12219 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
12220 | | return ret; |
12221 | | if ((ret = HashRaw(ssl, mac, finishedSz)) != 0) |
12222 | | return ret; |
12223 | | |
12224 | | if ((ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret)) != 0) |
12225 | | return ret; |
12226 | | |
12227 | | /* Restore the hash inline with currently seen messages. */ |
12228 | | switch (ssl->specs.mac_algorithm) { |
12229 | | #ifndef NO_SHA256 |
12230 | | case sha256_mac: |
12231 | | wc_Sha256Free(&ssl->hsHashes->hashSha256); |
12232 | | ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256); |
12233 | | wc_Sha256Free(&digest.sha256); |
12234 | | if (ret != 0) |
12235 | | return ret; |
12236 | | break; |
12237 | | #endif |
12238 | | #ifdef WOLFSSL_SHA384 |
12239 | | case sha384_mac: |
12240 | | wc_Sha384Free(&ssl->hsHashes->hashSha384); |
12241 | | ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384); |
12242 | | wc_Sha384Free(&digest.sha384); |
12243 | | if (ret != 0) |
12244 | | return ret; |
12245 | | break; |
12246 | | #endif |
12247 | | #ifdef WOLFSSL_TLS13_SHA512 |
12248 | | case sha512_mac: |
12249 | | wc_Sha512Free(&ssl->hsHashes->hashSha512); |
12250 | | ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512); |
12251 | | wc_Sha512Free(&digest.sha512); |
12252 | | if (ret != 0) |
12253 | | return ret; |
12254 | | break; |
12255 | | #endif |
12256 | | #ifdef WOLFSSL_SM3 |
12257 | | case sm3_mac: |
12258 | | wc_Sm3Free(&ssl->hsHashes->hashSm3); |
12259 | | ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3); |
12260 | | wc_Sm3Free(&digest.sm3); |
12261 | | if (ret != 0) |
12262 | | return ret; |
12263 | | break; |
12264 | | #endif |
12265 | | } |
12266 | | |
12267 | | return ret; |
12268 | | } |
12269 | | #endif |
12270 | | |
12271 | | /* Send New Session Ticket handshake message. |
12272 | | * Message contains the information required to perform resumption. |
12273 | | * |
12274 | | * ssl The SSL/TLS object. |
12275 | | * returns 0 on success, otherwise failure. |
12276 | | */ |
12277 | | static int SendTls13NewSessionTicket(WOLFSSL* ssl) |
12278 | | { |
12279 | | byte* output; |
12280 | | int ret; |
12281 | | word32 length; |
12282 | | int sendSz; |
12283 | | word16 extSz; |
12284 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
12285 | | |
12286 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12287 | | WOLFSSL_ENTER("SendTls13NewSessionTicket"); |
12288 | | |
12289 | | #ifdef WOLFSSL_DTLS13 |
12290 | | if (ssl->options.dtls) |
12291 | | idx = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
12292 | | #endif /* WOLFSSL_DTLS13 */ |
12293 | | |
12294 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12295 | | if (!ssl->msgsReceived.got_finished) { |
12296 | | if ((ret = ExpectedResumptionSecret(ssl)) != 0) |
12297 | | return ret; |
12298 | | } |
12299 | | #endif |
12300 | | |
12301 | | /* Start ticket nonce at 0 and go up to 255. */ |
12302 | | if (ssl->session->ticketNonce.len == 0) { |
12303 | | ssl->session->ticketNonce.len = DEF_TICKET_NONCE_SZ; |
12304 | | ssl->session->ticketNonce.data[0] = 0; |
12305 | | } |
12306 | | else |
12307 | | #ifdef WOLFSSL_ASYNC_CRYPT |
12308 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
12309 | | #endif |
12310 | | { |
12311 | | if (ssl->session->ticketNonce.data[0] == 255) { |
12312 | | /* RFC8446 Section 4.6.1: Each ticket must have a unique nonce |
12313 | | * value. As the nonce is only a single byte, we have to prevent |
12314 | | * the overflow and abort. */ |
12315 | | return SESSION_TICKET_NONCE_OVERFLOW; |
12316 | | } |
12317 | | else |
12318 | | ssl->session->ticketNonce.data[0]++; |
12319 | | } |
12320 | | |
12321 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12322 | | /* In this case we only send the ID as the ticket. Let's generate a new |
12323 | | * ID for the new ticket so that we don't overwrite any old ones */ |
12324 | | ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID, |
12325 | | ID_LEN); |
12326 | | if (ret != 0) |
12327 | | return ret; |
12328 | | ssl->session->haveAltSessionID = 1; |
12329 | | } |
12330 | | |
12331 | | if (!ssl->options.noTicketTls13) { |
12332 | | if ((ret = SetupTicket(ssl)) != 0) |
12333 | | return ret; |
12334 | | /* No need to create the ticket if we only send the ID */ |
12335 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == 0) { |
12336 | | if ((ret = CreateTicket(ssl)) != 0) |
12337 | | return ret; |
12338 | | } |
12339 | | } |
12340 | | |
12341 | | #ifdef WOLFSSL_EARLY_DATA |
12342 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
12343 | | if (ssl->session->maxEarlyDataSz > 0) |
12344 | | TLSX_EarlyData_Use(ssl, ssl->session->maxEarlyDataSz, 1); |
12345 | | extSz = 0; |
12346 | | ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz); |
12347 | | if (ret != 0) |
12348 | | return ret; |
12349 | | #else |
12350 | | extSz = EXTS_SZ; |
12351 | | #endif |
12352 | | /* Lifetime | Age Add | Ticket session ID | Extensions */ |
12353 | | length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ; |
12354 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) |
12355 | | length += ID_LEN + extSz; |
12356 | | else |
12357 | | length += ssl->session->ticketLen + extSz; |
12358 | | /* Nonce */ |
12359 | | length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ; |
12360 | | |
12361 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
12362 | | |
12363 | | /* Check buffers are big enough and grow if needed. */ |
12364 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
12365 | | return ret; |
12366 | | |
12367 | | /* Get position in output buffer to write new message to. */ |
12368 | | output = GetOutputBuffer(ssl); |
12369 | | |
12370 | | /* Put the record and handshake headers on. */ |
12371 | | AddTls13Headers(output, length, session_ticket, ssl); |
12372 | | |
12373 | | /* Lifetime hint */ |
12374 | | c32toa(ssl->ctx->ticketHint, output + idx); |
12375 | | idx += SESSION_HINT_SZ; |
12376 | | /* Age add - obfuscator */ |
12377 | | c32toa(ssl->session->ticketAdd, output + idx); |
12378 | | idx += SESSION_ADD_SZ; |
12379 | | |
12380 | | output[idx++] = ssl->session->ticketNonce.len; |
12381 | | output[idx++] = ssl->session->ticketNonce.data[0]; |
12382 | | |
12383 | | /* length */ |
12384 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12385 | | c16toa(ID_LEN, output + idx); |
12386 | | } |
12387 | | else { |
12388 | | c16toa(ssl->session->ticketLen, output + idx); |
12389 | | } |
12390 | | |
12391 | | idx += LENGTH_SZ; |
12392 | | /* ticket */ |
12393 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12394 | | if (ssl->session->haveAltSessionID) |
12395 | | XMEMCPY(output + idx, ssl->session->altSessionID, ID_LEN); |
12396 | | else |
12397 | | return BAD_FUNC_ARG; /* Should not happen */ |
12398 | | idx += ID_LEN; |
12399 | | } |
12400 | | else { |
12401 | | XMEMCPY(output + idx, ssl->session->ticket, ssl->session->ticketLen); |
12402 | | idx += ssl->session->ticketLen; |
12403 | | } |
12404 | | |
12405 | | #ifdef WOLFSSL_EARLY_DATA |
12406 | | extSz = 0; |
12407 | | ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz); |
12408 | | if (ret != 0) |
12409 | | return ret; |
12410 | | idx += extSz; |
12411 | | #else |
12412 | | /* No extension support - empty extensions. */ |
12413 | | c16toa(0, output + idx); |
12414 | | idx += EXTS_SZ; |
12415 | | #endif |
12416 | | |
12417 | | if (idx > WOLFSSL_MAX_16BIT || |
12418 | | sendSz > (int)WOLFSSL_MAX_16BIT) { |
12419 | | return BAD_LENGTH_E; |
12420 | | } |
12421 | | |
12422 | | ssl->options.haveSessionId = 1; |
12423 | | |
12424 | | SetupSession(ssl); |
12425 | | /* Only add to cache when support built in and when the ticket contains |
12426 | | * an ID. Otherwise we have no way to actually retrieve the ticket from the |
12427 | | * cache. */ |
12428 | | #if !defined(NO_SESSION_CACHE) && defined(WOLFSSL_TICKET_HAVE_ID) |
12429 | | AddSession(ssl); |
12430 | | #endif |
12431 | | |
12432 | | #ifdef WOLFSSL_DTLS13 |
12433 | | if (ssl->options.dtls) |
12434 | | return Dtls13HandshakeSend(ssl, output, (word16)sendSz, |
12435 | | (word16)idx, session_ticket, 0); |
12436 | | #endif /* WOLFSSL_DTLS13 */ |
12437 | | |
12438 | | /* This message is always encrypted. */ |
12439 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
12440 | | output + RECORD_HEADER_SZ, |
12441 | | (word16)idx - RECORD_HEADER_SZ, |
12442 | | handshake, 0, 0, 0); |
12443 | | if (sendSz < 0) |
12444 | | return sendSz; |
12445 | | |
12446 | | ssl->buffers.outputBuffer.length += sendSz; |
12447 | | |
12448 | | /* Always send as this is either directly after server's Finished or only |
12449 | | * message after client's Finished. |
12450 | | */ |
12451 | | ret = SendBuffered(ssl); |
12452 | | |
12453 | | WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0); |
12454 | | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12455 | | |
12456 | | return ret; |
12457 | | } |
12458 | | #endif /* HAVE_SESSION_TICKET */ |
12459 | | #endif /* NO_WOLFSSL_SERVER */ |
12460 | | |
12461 | | /* Make sure no duplicates, no fast forward, or other problems |
12462 | | * |
12463 | | * ssl The SSL/TLS object. |
12464 | | * type Type of handshake message received. |
12465 | | * returns 0 on success, otherwise failure. |
12466 | | */ |
12467 | | static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type) |
12468 | 0 | { |
12469 | | /* verify not a duplicate, mark received, check state */ |
12470 | 0 | switch (type) { |
12471 | | |
12472 | 0 | #ifndef NO_WOLFSSL_SERVER |
12473 | 0 | case client_hello: |
12474 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12475 | | /* Only valid when received on SERVER side. */ |
12476 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12477 | 0 | WOLFSSL_MSG("ClientHello received by client"); |
12478 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12479 | 0 | return SIDE_ERROR; |
12480 | 0 | } |
12481 | 0 | #endif |
12482 | | /* Check state. */ |
12483 | 0 | if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) { |
12484 | 0 | WOLFSSL_MSG("ClientHello received out of order"); |
12485 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12486 | 0 | return OUT_OF_ORDER_E; |
12487 | 0 | } |
12488 | | /* Check previously seen. */ |
12489 | | /* Initial and after HelloRetryRequest - no more than 2. */ |
12490 | 0 | if (ssl->msgsReceived.got_client_hello == 2) { |
12491 | 0 | WOLFSSL_MSG("Too many ClientHello received"); |
12492 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12493 | 0 | return DUPLICATE_MSG_E; |
12494 | 0 | } |
12495 | | /* Second only after HelloRetryRequest seen. */ |
12496 | 0 | if (ssl->msgsReceived.got_client_hello == 1 && |
12497 | 0 | ssl->options.serverState != |
12498 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
12499 | 0 | WOLFSSL_MSG("Duplicate ClientHello received"); |
12500 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12501 | 0 | return DUPLICATE_MSG_E; |
12502 | 0 | } |
12503 | 0 | ssl->msgsReceived.got_client_hello++; |
12504 | |
|
12505 | 0 | break; |
12506 | 0 | #endif |
12507 | | |
12508 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12509 | 0 | case server_hello: |
12510 | 0 | #ifndef NO_WOLFSSL_SERVER |
12511 | | /* Only valid when received on CLIENT side. */ |
12512 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12513 | 0 | WOLFSSL_MSG("ServerHello received by server"); |
12514 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12515 | 0 | return SIDE_ERROR; |
12516 | 0 | } |
12517 | 0 | #endif |
12518 | | /* Check state. */ |
12519 | 0 | if (ssl->options.serverState >= SERVER_HELLO_COMPLETE) { |
12520 | 0 | WOLFSSL_MSG("ServerHello received out of order"); |
12521 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12522 | 0 | return OUT_OF_ORDER_E; |
12523 | 0 | } |
12524 | | /* Check previously seen. */ |
12525 | | /* Only once after ClientHello. |
12526 | | * HelloRetryRequest has ServerHello type but count fixed up later |
12527 | | * - see DoTls13ServerHello(). |
12528 | | */ |
12529 | 0 | if (ssl->msgsReceived.got_server_hello) { |
12530 | 0 | WOLFSSL_MSG("Duplicate ServerHello received"); |
12531 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12532 | 0 | return DUPLICATE_MSG_E; |
12533 | 0 | } |
12534 | 0 | ssl->msgsReceived.got_server_hello = 1; |
12535 | |
|
12536 | 0 | break; |
12537 | 0 | #endif |
12538 | | |
12539 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12540 | 0 | case session_ticket: |
12541 | 0 | #ifndef NO_WOLFSSL_SERVER |
12542 | | /* Only valid when received on CLIENT side. */ |
12543 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12544 | 0 | WOLFSSL_MSG("NewSessionTicket received by server"); |
12545 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12546 | 0 | return SIDE_ERROR; |
12547 | 0 | } |
12548 | 0 | #endif |
12549 | | /* Check state. */ |
12550 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12551 | | /* Only allowed after server's Finished message. */ |
12552 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12553 | | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12554 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12555 | | return OUT_OF_ORDER_E; |
12556 | | } |
12557 | | #else |
12558 | | /* Only allowed after client's Finished message. */ |
12559 | 0 | if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
12560 | 0 | WOLFSSL_MSG("NewSessionTicket received out of order"); |
12561 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12562 | 0 | return OUT_OF_ORDER_E; |
12563 | 0 | } |
12564 | 0 | #endif |
12565 | | /* Many SessionTickets can be sent. */ |
12566 | 0 | ssl->msgsReceived.got_session_ticket = 1; |
12567 | |
|
12568 | 0 | break; |
12569 | 0 | #endif |
12570 | | |
12571 | 0 | #ifndef NO_WOLFSSL_SERVER |
12572 | | #ifdef WOLFSSL_EARLY_DATA |
12573 | | case end_of_early_data: |
12574 | | #ifndef NO_WOLFSSL_CLIENT |
12575 | | /* Only valid when received on SERVER side. */ |
12576 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12577 | | WOLFSSL_MSG("EndOfEarlyData received by client"); |
12578 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12579 | | return SIDE_ERROR; |
12580 | | } |
12581 | | #endif |
12582 | | /* Check state. */ |
12583 | | /* Only after server's Finished and before client's Finished. */ |
12584 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12585 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12586 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12587 | | return OUT_OF_ORDER_E; |
12588 | | } |
12589 | | if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) { |
12590 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
12591 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12592 | | return OUT_OF_ORDER_E; |
12593 | | } |
12594 | | /* Check previously seen. */ |
12595 | | if (ssl->msgsReceived.got_end_of_early_data) { |
12596 | | WOLFSSL_MSG("Too many EndOfEarlyData received"); |
12597 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12598 | | return DUPLICATE_MSG_E; |
12599 | | } |
12600 | | ssl->msgsReceived.got_end_of_early_data = 1; |
12601 | | |
12602 | | break; |
12603 | | #endif |
12604 | 0 | #endif |
12605 | | |
12606 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12607 | 0 | case encrypted_extensions: |
12608 | 0 | #ifndef NO_WOLFSSL_SERVER |
12609 | | /* Only valid when received on CLIENT side. */ |
12610 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12611 | 0 | WOLFSSL_MSG("EncryptedExtensions received by server"); |
12612 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12613 | 0 | return SIDE_ERROR; |
12614 | 0 | } |
12615 | 0 | #endif |
12616 | | /* Check state. */ |
12617 | | /* Must be received directly after ServerHello. |
12618 | | * DoTls13EncryptedExtensions() changes state to: |
12619 | | * SERVER_ENCRYPTED_EXTENSIONS_COMPLETE. |
12620 | | */ |
12621 | 0 | if (ssl->options.serverState != SERVER_HELLO_COMPLETE) { |
12622 | 0 | WOLFSSL_MSG("EncryptedExtensions received out of order"); |
12623 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12624 | 0 | return OUT_OF_ORDER_E; |
12625 | 0 | } |
12626 | | /* Check previously seen. */ |
12627 | 0 | if (ssl->msgsReceived.got_encrypted_extensions) { |
12628 | 0 | WOLFSSL_MSG("Duplicate EncryptedExtensions received"); |
12629 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12630 | 0 | return DUPLICATE_MSG_E; |
12631 | 0 | } |
12632 | 0 | ssl->msgsReceived.got_encrypted_extensions = 1; |
12633 | |
|
12634 | 0 | break; |
12635 | 0 | #endif |
12636 | | |
12637 | 0 | case certificate: |
12638 | | /* Valid on both sides. */ |
12639 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12640 | | /* Check state. */ |
12641 | | /* On client, seen after EncryptedExtension and CertificateRequest |
12642 | | * (if sent) and before CertificateVerify and Finished. |
12643 | | * DoTls13Certificate() sets serverState to SERVER_CERT_COMPLETE. |
12644 | | */ |
12645 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12646 | 0 | ssl->options.serverState != |
12647 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12648 | 0 | WOLFSSL_MSG("Certificate received out of order - Client"); |
12649 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12650 | 0 | return OUT_OF_ORDER_E; |
12651 | 0 | } |
12652 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12653 | | /* Server's authenticating with PSK must not send this. */ |
12654 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12655 | | ssl->options.serverState == SERVER_CERT_COMPLETE && |
12656 | | ssl->options.pskNegotiated) { |
12657 | | WOLFSSL_MSG("Certificate received while using PSK"); |
12658 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12659 | | return SANITY_MSG_E; |
12660 | | } |
12661 | | #endif |
12662 | 0 | #endif |
12663 | 0 | #ifndef NO_WOLFSSL_SERVER |
12664 | | /* Check state. */ |
12665 | | /* On Server, valid after ClientHello received and ServerFinished |
12666 | | * sent. */ |
12667 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
12668 | 0 | ssl->options.clientState != CLIENT_HELLO_COMPLETE && |
12669 | 0 | ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12670 | 0 | WOLFSSL_MSG("Certificate received out of order - Server"); |
12671 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12672 | 0 | return OUT_OF_ORDER_E; |
12673 | 0 | } |
12674 | 0 | #endif |
12675 | | /* Check previously seen. */ |
12676 | 0 | if (ssl->msgsReceived.got_certificate) { |
12677 | 0 | WOLFSSL_MSG("Duplicate Certificate received"); |
12678 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12679 | 0 | return DUPLICATE_MSG_E; |
12680 | 0 | } |
12681 | 0 | ssl->msgsReceived.got_certificate = 1; |
12682 | |
|
12683 | 0 | break; |
12684 | | |
12685 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12686 | 0 | case certificate_request: |
12687 | 0 | #ifndef NO_WOLFSSL_SERVER |
12688 | | /* Only valid when received on CLIENT side. */ |
12689 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12690 | 0 | WOLFSSL_MSG("CertificateRequest received by server"); |
12691 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12692 | 0 | return SIDE_ERROR; |
12693 | 0 | } |
12694 | 0 | #endif |
12695 | | /* Check state. */ |
12696 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12697 | | /* Only valid when sent after EncryptedExtensions and before |
12698 | | * Certificate. */ |
12699 | 0 | if (ssl->options.serverState != |
12700 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12701 | 0 | WOLFSSL_MSG("CertificateRequest received out of order"); |
12702 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12703 | 0 | return OUT_OF_ORDER_E; |
12704 | 0 | } |
12705 | | #else |
12706 | | /* Valid when sent after EncryptedExtensions and before Certificate |
12707 | | * and after both client and server have sent Finished (Post |
12708 | | * Handshake Authentication). */ |
12709 | | if (ssl->options.serverState != |
12710 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE && |
12711 | | (ssl->options.serverState < SERVER_FINISHED_COMPLETE || |
12712 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) { |
12713 | | WOLFSSL_MSG("CertificateRequest received out of order"); |
12714 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12715 | | return OUT_OF_ORDER_E; |
12716 | | } |
12717 | | #endif |
12718 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12719 | | /* Server's authenticating with PSK must not send this. */ |
12720 | | if (ssl->options.pskNegotiated) { |
12721 | | WOLFSSL_MSG("CertificateRequest received while using PSK"); |
12722 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12723 | | return SANITY_MSG_E; |
12724 | | } |
12725 | | #endif |
12726 | | /* Check previously seen. */ |
12727 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
12728 | | /* Only once during handshake. */ |
12729 | 0 | if (ssl->msgsReceived.got_certificate_request) { |
12730 | 0 | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12731 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12732 | 0 | return DUPLICATE_MSG_E; |
12733 | 0 | } |
12734 | | #else |
12735 | | /* Only once during handshake. */ |
12736 | | if (ssl->msgsReceived.got_certificate_request && |
12737 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE) { |
12738 | | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
12739 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12740 | | return DUPLICATE_MSG_E; |
12741 | | } |
12742 | | #endif |
12743 | 0 | ssl->msgsReceived.got_certificate_request = 1; |
12744 | |
|
12745 | 0 | break; |
12746 | 0 | #endif |
12747 | | |
12748 | 0 | case certificate_verify: |
12749 | | /* Valid on both sides. */ |
12750 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12751 | | /* Check state on client. |
12752 | | * Valid only directly after a Certificate message. */ |
12753 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12754 | 0 | if (ssl->options.serverState != SERVER_CERT_COMPLETE) { |
12755 | 0 | WOLFSSL_MSG("No Cert before CertVerify"); |
12756 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12757 | 0 | return OUT_OF_ORDER_E; |
12758 | 0 | } |
12759 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12760 | | /* Server's authenticating with PSK must not send this. */ |
12761 | | if (ssl->options.pskNegotiated) { |
12762 | | WOLFSSL_MSG("CertificateVerify received while using PSK"); |
12763 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12764 | | return SANITY_MSG_E; |
12765 | | } |
12766 | | #endif |
12767 | 0 | } |
12768 | 0 | #endif |
12769 | 0 | #ifndef NO_WOLFSSL_SERVER |
12770 | | /* Check state on server. */ |
12771 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12772 | | /* Server must have sent Finished message. */ |
12773 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12774 | 0 | WOLFSSL_MSG("CertificateVerify received out of order"); |
12775 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12776 | 0 | return OUT_OF_ORDER_E; |
12777 | 0 | } |
12778 | | /* Valid only directly after a Certificate message. */ |
12779 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12780 | 0 | WOLFSSL_MSG("CertificateVerify before ClientHello done"); |
12781 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12782 | 0 | return OUT_OF_ORDER_E; |
12783 | 0 | } |
12784 | 0 | if (!ssl->msgsReceived.got_certificate) { |
12785 | 0 | WOLFSSL_MSG("No Cert before CertificateVerify"); |
12786 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12787 | 0 | return OUT_OF_ORDER_E; |
12788 | 0 | } |
12789 | 0 | } |
12790 | 0 | #endif |
12791 | | /* Check previously seen. */ |
12792 | 0 | if (ssl->msgsReceived.got_certificate_verify) { |
12793 | 0 | WOLFSSL_MSG("Duplicate CertificateVerify received"); |
12794 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12795 | 0 | return DUPLICATE_MSG_E; |
12796 | 0 | } |
12797 | 0 | ssl->msgsReceived.got_certificate_verify = 1; |
12798 | |
|
12799 | 0 | break; |
12800 | | |
12801 | 0 | case finished: |
12802 | | /* Valid on both sides. */ |
12803 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12804 | | /* Check state on client. */ |
12805 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12806 | | /* After sending ClientHello */ |
12807 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12808 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12809 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12810 | 0 | return OUT_OF_ORDER_E; |
12811 | 0 | } |
12812 | | /* Must have seen certificate and verify from server except when |
12813 | | * using PSK. */ |
12814 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12815 | | if (ssl->options.pskNegotiated) { |
12816 | | if (ssl->options.serverState != |
12817 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
12818 | | WOLFSSL_MSG("Finished received out of order - PSK"); |
12819 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12820 | | return OUT_OF_ORDER_E; |
12821 | | } |
12822 | | } |
12823 | | else |
12824 | | #endif |
12825 | 0 | if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) { |
12826 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12827 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12828 | 0 | return OUT_OF_ORDER_E; |
12829 | 0 | } |
12830 | 0 | } |
12831 | 0 | #endif |
12832 | 0 | #ifndef NO_WOLFSSL_SERVER |
12833 | | /* Check state on server. */ |
12834 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12835 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
12836 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
12837 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12838 | 0 | return OUT_OF_ORDER_E; |
12839 | 0 | } |
12840 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
12841 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
12842 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12843 | 0 | return OUT_OF_ORDER_E; |
12844 | 0 | } |
12845 | | #ifdef WOLFSSL_EARLY_DATA |
12846 | | if (ssl->earlyData == process_early_data && |
12847 | | /* early data may be lost when using DTLS */ |
12848 | | !ssl->options.dtls |
12849 | | /* QUIC does not use EndOfEarlyData records */ |
12850 | | && !WOLFSSL_IS_QUIC(ssl)) { |
12851 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12852 | | return OUT_OF_ORDER_E; |
12853 | | } |
12854 | | #endif |
12855 | 0 | } |
12856 | 0 | #endif |
12857 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
12858 | | if (!ssl->options.pskNegotiated) |
12859 | | #endif |
12860 | 0 | { |
12861 | | /* Must have received a Certificate message from client if |
12862 | | * verifying the peer. Empty certificate message indicates |
12863 | | * no certificate available. |
12864 | | */ |
12865 | 0 | if (ssl->options.verifyPeer && |
12866 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
12867 | | !ssl->options.verifyPostHandshake && |
12868 | | #endif |
12869 | 0 | !ssl->msgsReceived.got_certificate) { |
12870 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12871 | 0 | "missing Certificate message"); |
12872 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12873 | 0 | return OUT_OF_ORDER_E; |
12874 | 0 | } |
12875 | | /* Mutual authentication on server requires a certificate from |
12876 | | * peer. Verify peer set on client side requires a certificate |
12877 | | * from peer as not doing PSK. |
12878 | | */ |
12879 | 0 | if ((ssl->options.mutualAuth || |
12880 | 0 | (ssl->options.side == WOLFSSL_CLIENT_END && |
12881 | 0 | ssl->options.verifyPeer)) && !ssl->options.havePeerCert) { |
12882 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12883 | 0 | "no valid certificate"); |
12884 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12885 | 0 | return OUT_OF_ORDER_E; |
12886 | 0 | } |
12887 | | /* Must have received a valid CertificateVerify if verifying |
12888 | | * peer and got a peer certificate. |
12889 | | */ |
12890 | 0 | if ((ssl->options.mutualAuth || ssl->options.verifyPeer) && |
12891 | 0 | ssl->options.havePeerCert && !ssl->options.havePeerVerify) { |
12892 | 0 | WOLFSSL_MSG("Finished received out of order - " |
12893 | 0 | "Certificate message but no CertificateVerify"); |
12894 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12895 | 0 | return OUT_OF_ORDER_E; |
12896 | 0 | } |
12897 | 0 | } |
12898 | | /* Check previously seen. */ |
12899 | 0 | if (ssl->msgsReceived.got_finished) { |
12900 | 0 | WOLFSSL_MSG("Duplicate Finished received"); |
12901 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12902 | 0 | return DUPLICATE_MSG_E; |
12903 | 0 | } |
12904 | 0 | ssl->msgsReceived.got_finished = 1; |
12905 | |
|
12906 | 0 | break; |
12907 | | |
12908 | 0 | case key_update: |
12909 | | /* Valid on both sides. */ |
12910 | | /* Check state. |
12911 | | * Client and server must have received finished message from other |
12912 | | * side. |
12913 | | */ |
12914 | 0 | if (!ssl->msgsReceived.got_finished) { |
12915 | 0 | WOLFSSL_MSG("No KeyUpdate before Finished"); |
12916 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12917 | 0 | return OUT_OF_ORDER_E; |
12918 | 0 | } |
12919 | | /* Multiple KeyUpdates can be sent. */ |
12920 | 0 | break; |
12921 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
12922 | | case hello_verify_request: |
12923 | | if (!ssl->options.dtls) { |
12924 | | WOLFSSL_MSG("HelloVerifyRequest when not in DTLS"); |
12925 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12926 | | return OUT_OF_ORDER_E; |
12927 | | } |
12928 | | if (ssl->msgsReceived.got_hello_verify_request) { |
12929 | | WOLFSSL_MSG("Duplicate HelloVerifyRequest received"); |
12930 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12931 | | return DUPLICATE_MSG_E; |
12932 | | } |
12933 | | ssl->msgsReceived.got_hello_verify_request = 1; |
12934 | | if (ssl->msgsReceived.got_hello_retry_request) { |
12935 | | WOLFSSL_MSG( |
12936 | | "Both HelloVerifyRequest and HelloRetryRequest received"); |
12937 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12938 | | return DUPLICATE_MSG_E; |
12939 | | } |
12940 | | if (ssl->options.serverState >= |
12941 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE || |
12942 | | ssl->options.connectState != CLIENT_HELLO_SENT) { |
12943 | | WOLFSSL_MSG("HelloVerifyRequest received out of order"); |
12944 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12945 | | return OUT_OF_ORDER_E; |
12946 | | } |
12947 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12948 | | WOLFSSL_MSG("HelloVerifyRequest received on the server"); |
12949 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12950 | | return SIDE_ERROR; |
12951 | | } |
12952 | | if (!ssl->options.downgrade || |
12953 | | ssl->options.minDowngrade < DTLSv1_2_MINOR) { |
12954 | | WOLFSSL_MSG( |
12955 | | "HelloVerifyRequest received but not DTLSv1.2 allowed"); |
12956 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
12957 | | return VERSION_ERROR; |
12958 | | } |
12959 | | break; |
12960 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_TLS12*/ |
12961 | | |
12962 | 0 | default: |
12963 | 0 | WOLFSSL_MSG("Unknown message type"); |
12964 | 0 | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
12965 | 0 | return SANITY_MSG_E; |
12966 | 0 | } |
12967 | | |
12968 | 0 | return 0; |
12969 | 0 | } |
12970 | | |
12971 | | /* Handle a type of handshake message that has been received. |
12972 | | * |
12973 | | * ssl The SSL/TLS object. |
12974 | | * input The message buffer. |
12975 | | * inOutIdx On entry, the index into the buffer of the current message. |
12976 | | * On exit, the index into the buffer of the next message. |
12977 | | * size The length of the current handshake message. |
12978 | | * totalSz Length of remaining data in the message buffer. |
12979 | | * returns 0 on success and otherwise failure. |
12980 | | */ |
12981 | | int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
12982 | | byte type, word32 size, word32 totalSz) |
12983 | | { |
12984 | | int ret = 0, tmp; |
12985 | | word32 inIdx = *inOutIdx; |
12986 | | int alertType; |
12987 | | #if defined(HAVE_ECH) && !defined(NO_WOLFSSL_SERVER) |
12988 | | TLSX* echX = NULL; |
12989 | | word32 echInOutIdx; |
12990 | | #endif |
12991 | | |
12992 | | (void)totalSz; |
12993 | | |
12994 | | WOLFSSL_ENTER("DoTls13HandShakeMsgType"); |
12995 | | |
12996 | | /* make sure we can read the message */ |
12997 | | if (*inOutIdx + size > totalSz) |
12998 | | return INCOMPLETE_DATA; |
12999 | | |
13000 | | /* sanity check msg received */ |
13001 | | if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) { |
13002 | | WOLFSSL_MSG("Sanity Check on handshake message type received failed"); |
13003 | | if (ret == WC_NO_ERR_TRACE(VERSION_ERROR)) |
13004 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
13005 | | else |
13006 | | SendAlert(ssl, alert_fatal, unexpected_message); |
13007 | | return ret; |
13008 | | } |
13009 | | |
13010 | | #if defined(WOLFSSL_CALLBACKS) |
13011 | | /* add name later, add on record and handshake header part back on */ |
13012 | | if (ssl->toInfoOn) { |
13013 | | ret = AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - |
13014 | | HANDSHAKE_HEADER_SZ, size + HANDSHAKE_HEADER_SZ, READ_PROTO, |
13015 | | RECORD_HEADER_SZ, ssl->heap); |
13016 | | if (ret != 0) |
13017 | | return ret; |
13018 | | AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo); |
13019 | | } |
13020 | | #endif |
13021 | | |
13022 | | if (ssl->options.handShakeState == HANDSHAKE_DONE && |
13023 | | type != session_ticket && type != certificate_request && |
13024 | | type != certificate && type != key_update && type != finished) { |
13025 | | WOLFSSL_MSG("HandShake message after handshake complete"); |
13026 | | SendAlert(ssl, alert_fatal, unexpected_message); |
13027 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13028 | | return OUT_OF_ORDER_E; |
13029 | | } |
13030 | | |
13031 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
13032 | | ssl->options.serverState == NULL_STATE && |
13033 | | type != server_hello && type != hello_retry_request |
13034 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
13035 | | && (!ssl->options.dtls || type != hello_verify_request) |
13036 | | #endif /* defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) */ |
13037 | | ) { |
13038 | | WOLFSSL_MSG("First server message not server hello"); |
13039 | | SendAlert(ssl, alert_fatal, unexpected_message); |
13040 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13041 | | return OUT_OF_ORDER_E; |
13042 | | } |
13043 | | |
13044 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
13045 | | ssl->options.clientState == NULL_STATE && type != client_hello) { |
13046 | | WOLFSSL_MSG("First client message not client hello"); |
13047 | | SendAlert(ssl, alert_fatal, unexpected_message); |
13048 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13049 | | return OUT_OF_ORDER_E; |
13050 | | } |
13051 | | |
13052 | | /* above checks handshake state */ |
13053 | | switch (type) { |
13054 | | #ifndef NO_WOLFSSL_CLIENT |
13055 | | /* Messages only received by client. */ |
13056 | | case server_hello: |
13057 | | WOLFSSL_MSG("processing server hello"); |
13058 | | ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type); |
13059 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
13060 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
13061 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
13062 | | if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) || |
13063 | | IsAtLeastTLSv1_3(ssl->version)) { |
13064 | | ssl->options.cacheMessages = 0; |
13065 | | if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) { |
13066 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
13067 | | XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES); |
13068 | | ssl->hsHashes->messages = NULL; |
13069 | | } |
13070 | | } |
13071 | | #endif |
13072 | | break; |
13073 | | |
13074 | | case encrypted_extensions: |
13075 | | WOLFSSL_MSG("processing encrypted extensions"); |
13076 | | ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size); |
13077 | | break; |
13078 | | |
13079 | | #ifndef NO_CERTS |
13080 | | case certificate_request: |
13081 | | WOLFSSL_MSG("processing certificate request"); |
13082 | | ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size); |
13083 | | break; |
13084 | | #endif |
13085 | | |
13086 | | case session_ticket: |
13087 | | WOLFSSL_MSG("processing new session ticket"); |
13088 | | ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size); |
13089 | | break; |
13090 | | #endif /* !NO_WOLFSSL_CLIENT */ |
13091 | | |
13092 | | #ifndef NO_WOLFSSL_SERVER |
13093 | | /* Messages only received by server. */ |
13094 | | case client_hello: |
13095 | | WOLFSSL_MSG("processing client hello"); |
13096 | | #if defined(HAVE_ECH) |
13097 | | /* keep the start idx so we can restore it for the inner call */ |
13098 | | echInOutIdx = *inOutIdx; |
13099 | | #endif |
13100 | | ret = DoTls13ClientHello(ssl, input, inOutIdx, size); |
13101 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
13102 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
13103 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
13104 | | if ((ssl->options.resuming || !ssl->options.verifyPeer || |
13105 | | !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) |
13106 | | #ifdef WOLFSSL_DTLS13 |
13107 | | && (!ssl->options.dtls) |
13108 | | #endif |
13109 | | ) { |
13110 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
13111 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E) && |
13112 | | ret != WC_NO_ERR_TRACE(OCSP_WANT_READ)) |
13113 | | #endif |
13114 | | { |
13115 | | ssl->options.cacheMessages = 0; |
13116 | | if ((ssl->hsHashes != NULL) && |
13117 | | (ssl->hsHashes->messages != NULL)) { |
13118 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
13119 | | XFREE(ssl->hsHashes->messages, ssl->heap, |
13120 | | DYNAMIC_TYPE_HASHES); |
13121 | | ssl->hsHashes->messages = NULL; |
13122 | | } |
13123 | | } |
13124 | | } |
13125 | | #endif |
13126 | | #if defined(HAVE_ECH) |
13127 | | if (ret == 0) { |
13128 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
13129 | | |
13130 | | if (echX != NULL && |
13131 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
13132 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
13133 | | byte copyRandom = ((WOLFSSL_ECH*)echX->data)->innerCount == 0; |
13134 | | /* reset the inOutIdx to the outer start */ |
13135 | | *inOutIdx = echInOutIdx; |
13136 | | /* call again with the inner hello */ |
13137 | | if (ret == 0) { |
13138 | | ((WOLFSSL_ECH*)echX->data)->sniState = ECH_INNER_SNI; |
13139 | | |
13140 | | ret = DoTls13ClientHello(ssl, |
13141 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello, |
13142 | | &echInOutIdx, |
13143 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen); |
13144 | | |
13145 | | ((WOLFSSL_ECH*)echX->data)->sniState = ECH_SNI_DONE; |
13146 | | } |
13147 | | /* if the inner ech parsed successfully we have successfully |
13148 | | * handled the hello and can skip the whole message */ |
13149 | | if (ret == 0) { |
13150 | | /* Copy inner client random for ECH acceptance calculation. |
13151 | | * Only on first inner ClientHello (before HRR), not CH2. */ |
13152 | | if (copyRandom) { |
13153 | | XMEMCPY(ssl->arrays->clientRandomInner, |
13154 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello + |
13155 | | HANDSHAKE_HEADER_SZ + VERSION_SZ, RAN_LEN); |
13156 | | } |
13157 | | *inOutIdx += size; |
13158 | | } |
13159 | | } |
13160 | | } |
13161 | | #endif /* HAVE_ECH */ |
13162 | | break; |
13163 | | |
13164 | | #ifdef WOLFSSL_EARLY_DATA |
13165 | | case end_of_early_data: |
13166 | | WOLFSSL_MSG("processing end of early data"); |
13167 | | ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size); |
13168 | | break; |
13169 | | #endif |
13170 | | #endif /* !NO_WOLFSSL_SERVER */ |
13171 | | |
13172 | | /* Messages received by both client and server. */ |
13173 | | #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \ |
13174 | | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
13175 | | case certificate: |
13176 | | WOLFSSL_MSG("processing certificate"); |
13177 | | ret = DoTls13Certificate(ssl, input, inOutIdx, size); |
13178 | | break; |
13179 | | #endif |
13180 | | |
13181 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
13182 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
13183 | | case certificate_verify: |
13184 | | WOLFSSL_MSG("processing certificate verify"); |
13185 | | ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size); |
13186 | | break; |
13187 | | #endif |
13188 | | case finished: |
13189 | | WOLFSSL_MSG("processing finished"); |
13190 | | ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF); |
13191 | | break; |
13192 | | |
13193 | | case key_update: |
13194 | | WOLFSSL_MSG("processing key update"); |
13195 | | ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size); |
13196 | | break; |
13197 | | |
13198 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
13199 | | !defined(NO_WOLFSSL_CLIENT) |
13200 | | case hello_verify_request: |
13201 | | WOLFSSL_MSG("processing hello verify request"); |
13202 | | ret = DoHelloVerifyRequest(ssl, input, inOutIdx, size); |
13203 | | break; |
13204 | | #endif |
13205 | | default: |
13206 | | WOLFSSL_MSG("Unknown handshake message type"); |
13207 | | ret = UNKNOWN_HANDSHAKE_TYPE; |
13208 | | break; |
13209 | | } |
13210 | | |
13211 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_ASYNC_IO) |
13212 | | /* if async, offset index so this msg will be processed again */ |
13213 | | /* NOTE: check this now before other calls can overwrite ret */ |
13214 | | if ((ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
13215 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) && *inOutIdx > 0) { |
13216 | | /* DTLS always stores a message in a buffer when async is enable, so we |
13217 | | * don't need to adjust for the extra bytes here (*inOutIdx is always |
13218 | | * == 0) */ |
13219 | | *inOutIdx -= HANDSHAKE_HEADER_SZ; |
13220 | | } |
13221 | | |
13222 | | /* make sure async error is cleared */ |
13223 | | if (ret == 0 && |
13224 | | (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E) || |
13225 | | ssl->error == WC_NO_ERR_TRACE(OCSP_WANT_READ))) { |
13226 | | ssl->error = 0; |
13227 | | } |
13228 | | #endif |
13229 | | if (ret == 0 && type != client_hello && type != session_ticket && |
13230 | | type != key_update) { |
13231 | | ret = HashInput(ssl, input + inIdx, (int)size); |
13232 | | } |
13233 | | |
13234 | | alertType = TranslateErrorToAlert(ret); |
13235 | | |
13236 | | if (alertType != invalid_alert) { |
13237 | | #ifdef WOLFSSL_DTLS13 |
13238 | | if (type == client_hello && ssl->options.dtls) |
13239 | | DtlsSetSeqNumForReply(ssl); |
13240 | | #endif |
13241 | | tmp = SendAlert(ssl, alert_fatal, alertType); |
13242 | | /* propagate socket error instead of tls error to be sure the error is |
13243 | | * not ignored by DTLS code */ |
13244 | | if (tmp == WC_NO_ERR_TRACE(SOCKET_ERROR_E)) |
13245 | | ret = SOCKET_ERROR_E; |
13246 | | } |
13247 | | |
13248 | | if (ret == 0 && ssl->options.tls1_3) { |
13249 | | /* Need to hash input message before deriving secrets. */ |
13250 | | #ifndef NO_WOLFSSL_CLIENT |
13251 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13252 | | if (type == server_hello) { |
13253 | | if ((ret = DeriveEarlySecret(ssl)) != 0) |
13254 | | return ret; |
13255 | | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
13256 | | return ret; |
13257 | | |
13258 | | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
13259 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
13260 | | return ret; |
13261 | | } |
13262 | | #ifdef WOLFSSL_EARLY_DATA |
13263 | | if (ssl->earlyData != no_early_data) { |
13264 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13265 | | return ret; |
13266 | | } |
13267 | | else |
13268 | | #endif |
13269 | | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
13270 | | return ret; |
13271 | | |
13272 | | #ifdef WOLFSSL_DTLS13 |
13273 | | if (ssl->options.dtls) { |
13274 | | w64wrapper epochHandshake; |
13275 | | epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
13276 | | ssl->dtls13Epoch = epochHandshake; |
13277 | | ssl->dtls13PeerEpoch = epochHandshake; |
13278 | | |
13279 | | ret = Dtls13SetEpochKeys( |
13280 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
13281 | | if (ret != 0) |
13282 | | return ret; |
13283 | | |
13284 | | } |
13285 | | #endif /* WOLFSSL_DTLS13 */ |
13286 | | } |
13287 | | |
13288 | | if (type == finished) { |
13289 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
13290 | | return ret; |
13291 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
13292 | | ForceZero(ssl->arrays->preMasterSecret, |
13293 | | ssl->arrays->preMasterSz); |
13294 | | #ifdef WOLFSSL_EARLY_DATA |
13295 | | #ifdef WOLFSSL_QUIC |
13296 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData != no_early_data) { |
13297 | | /* QUIC never sends/receives EndOfEarlyData, but having |
13298 | | * early data means the last encryption keys had not been |
13299 | | * set yet. */ |
13300 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13301 | | return ret; |
13302 | | } |
13303 | | #endif |
13304 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13305 | | ENCRYPT_AND_DECRYPT_SIDE, |
13306 | | ssl->earlyData == no_early_data)) != 0) { |
13307 | | return ret; |
13308 | | } |
13309 | | if (ssl->earlyData != no_early_data) { |
13310 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, |
13311 | | 1)) != 0) { |
13312 | | return ret; |
13313 | | } |
13314 | | } |
13315 | | #else |
13316 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13317 | | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
13318 | | return ret; |
13319 | | } |
13320 | | #endif |
13321 | | /* Setup keys for application data messages. */ |
13322 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13323 | | return ret; |
13324 | | } |
13325 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
13326 | | if (type == certificate_request && |
13327 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
13328 | | #if defined(HAVE_WRITE_DUP) |
13329 | | /* Read side cannot write; delegate the cert response to the |
13330 | | * write side by saving auth state in the shared WriteDup. */ |
13331 | | if (ssl->dupSide == READ_DUP_SIDE) { |
13332 | | if (ssl->dupWrite == NULL) |
13333 | | return BAD_STATE_E; |
13334 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
13335 | | return BAD_MUTEX_E; |
13336 | | /* Copy the current transcript so the write side can |
13337 | | * compute the correct Finished MAC. */ |
13338 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, |
13339 | | &ssl->dupWrite->postHandshakeHashState); |
13340 | | if (ret == 0) { |
13341 | | /* Copy the cert request context. */ |
13342 | | CertReqCtx** tail = &ssl->certReqCtx; |
13343 | | while (*tail != NULL) |
13344 | | tail = &(*tail)->next; |
13345 | | *tail = ssl->dupWrite->postHandshakeCertReqCtx; |
13346 | | ssl->dupWrite->postHandshakeCertReqCtx = ssl->certReqCtx; |
13347 | | ssl->certReqCtx = NULL; |
13348 | | ssl->dupWrite->postHandshakeSendVerify = |
13349 | | ssl->options.sendVerify; |
13350 | | ssl->dupWrite->postHandshakeSigAlgo = |
13351 | | ssl->options.sigAlgo; |
13352 | | ssl->dupWrite->postHandshakeHashAlgo = |
13353 | | ssl->options.hashAlgo; |
13354 | | ssl->dupWrite->postHandshakeAuthPending = 1; |
13355 | | } |
13356 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
13357 | | /* Leave ssl->options unchanged: read side must not reset |
13358 | | * its states or call wolfSSL_connect_TLSv13. */ |
13359 | | } |
13360 | | else |
13361 | | #endif /* HAVE_WRITE_DUP */ |
13362 | | { |
13363 | | /* reset handshake states */ |
13364 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
13365 | | ssl->options.connectState = FIRST_REPLY_DONE; |
13366 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13367 | | ssl->options.processReply = 0; /* doProcessInit */ |
13368 | | |
13369 | | /* |
13370 | | DTLSv1.3 note: We can't reset serverState to |
13371 | | SERVER_FINISHED_COMPLETE with the goal that this connect |
13372 | | blocks until the cert/cert_verify/finished flight gets ACKed |
13373 | | by the server. The problem is that we will invoke |
13374 | | ProcessReplyEx() in that case, but we came here from |
13375 | | ProcessReplyEx() and it is not re-entrant safe (the input |
13376 | | buffer would still have the certificate_request message). */ |
13377 | | |
13378 | | if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) { |
13379 | | ret = ssl->error; |
13380 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
13381 | | ret = POST_HAND_AUTH_ERROR; |
13382 | | } |
13383 | | } |
13384 | | } |
13385 | | #endif |
13386 | | } |
13387 | | #endif /* NO_WOLFSSL_CLIENT */ |
13388 | | |
13389 | | #ifndef NO_WOLFSSL_SERVER |
13390 | | #if defined(HAVE_SESSION_TICKET) |
13391 | | if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) { |
13392 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
13393 | | if (ret != 0) |
13394 | | return ret; |
13395 | | } |
13396 | | #endif |
13397 | | #endif /* NO_WOLFSSL_SERVER */ |
13398 | | } |
13399 | | |
13400 | | #ifdef WOLFSSL_DTLS13 |
13401 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) { |
13402 | | DtlsResetState(ssl); |
13403 | | if (DtlsIgnoreError(ret)) |
13404 | | ret = 0; |
13405 | | } |
13406 | | #endif |
13407 | | |
13408 | | WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret); |
13409 | | return ret; |
13410 | | } |
13411 | | |
13412 | | |
13413 | | /* Handle a handshake message that has been received. |
13414 | | * |
13415 | | * ssl The SSL/TLS object. |
13416 | | * input The message buffer. |
13417 | | * inOutIdx On entry, the index into the buffer of the current message. |
13418 | | * On exit, the index into the buffer of the next message. |
13419 | | * totalSz Length of remaining data in the message buffer. |
13420 | | * returns 0 on success and otherwise failure. |
13421 | | */ |
13422 | | int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
13423 | | word32 totalSz) |
13424 | 21.1k | { |
13425 | 21.1k | int ret = 0; |
13426 | 21.1k | word32 inputLength; |
13427 | 21.1k | byte type; |
13428 | 21.1k | word32 size = 0; |
13429 | | |
13430 | 21.1k | WOLFSSL_ENTER("DoTls13HandShakeMsg"); |
13431 | | |
13432 | 21.1k | if (ssl->arrays == NULL) { |
13433 | 0 | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13434 | 0 | totalSz) != 0) { |
13435 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13436 | 0 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13437 | 0 | return PARSE_ERROR; |
13438 | 0 | } |
13439 | | |
13440 | 0 | ret = EarlySanityCheckMsgReceived(ssl, type, size); |
13441 | 0 | if (ret != 0) { |
13442 | 0 | WOLFSSL_ERROR(ret); |
13443 | 0 | return ret; |
13444 | 0 | } |
13445 | | |
13446 | 0 | return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13447 | 0 | totalSz); |
13448 | 0 | } |
13449 | | |
13450 | 21.1k | inputLength = ssl->buffers.inputBuffer.length - *inOutIdx - ssl->keys.padSz; |
13451 | | |
13452 | | /* If there is a pending fragmented handshake message, |
13453 | | * pending message size will be non-zero. */ |
13454 | 21.1k | if (ssl->arrays->pendingMsgSz == 0) { |
13455 | | |
13456 | 12.4k | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13457 | 12.4k | totalSz) != 0) { |
13458 | 14 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13459 | 14 | return PARSE_ERROR; |
13460 | 14 | } |
13461 | | |
13462 | 12.4k | ret = EarlySanityCheckMsgReceived(ssl, type, |
13463 | 12.4k | min(inputLength - HANDSHAKE_HEADER_SZ, size)); |
13464 | 12.4k | if (ret != 0) { |
13465 | 110 | WOLFSSL_ERROR(ret); |
13466 | 110 | return ret; |
13467 | 110 | } |
13468 | | |
13469 | | /* Cap the maximum size of a handshake message to something reasonable. |
13470 | | * By default is the maximum size of a certificate message assuming |
13471 | | * nine 2048-bit RSA certificates in the chain. */ |
13472 | 12.3k | if (size > MAX_HANDSHAKE_SZ) { |
13473 | 71 | WOLFSSL_MSG("Handshake message too large"); |
13474 | 71 | WOLFSSL_ERROR_VERBOSE(HANDSHAKE_SIZE_ERROR); |
13475 | 71 | return HANDSHAKE_SIZE_ERROR; |
13476 | 71 | } |
13477 | | |
13478 | | /* size is the size of the certificate message payload */ |
13479 | 12.3k | if (inputLength - HANDSHAKE_HEADER_SZ < size) { |
13480 | 626 | ssl->arrays->pendingMsgType = type; |
13481 | 626 | ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ; |
13482 | 626 | ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ, |
13483 | 626 | ssl->heap, |
13484 | 626 | DYNAMIC_TYPE_ARRAYS); |
13485 | 626 | if (ssl->arrays->pendingMsg == NULL) |
13486 | 27 | return MEMORY_E; |
13487 | 599 | XMEMCPY(ssl->arrays->pendingMsg, |
13488 | 599 | input + *inOutIdx - HANDSHAKE_HEADER_SZ, |
13489 | 599 | inputLength); |
13490 | 599 | ssl->arrays->pendingMsgOffset = inputLength; |
13491 | 599 | *inOutIdx += inputLength + ssl->keys.padSz - HANDSHAKE_HEADER_SZ; |
13492 | 599 | return 0; |
13493 | 626 | } |
13494 | | |
13495 | 11.6k | ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13496 | 11.6k | totalSz); |
13497 | 11.6k | } |
13498 | 8.69k | else { |
13499 | 8.69k | if (inputLength + ssl->arrays->pendingMsgOffset > |
13500 | 8.69k | ssl->arrays->pendingMsgSz) { |
13501 | 54 | inputLength = ssl->arrays->pendingMsgSz - |
13502 | 54 | ssl->arrays->pendingMsgOffset; |
13503 | 54 | } |
13504 | | |
13505 | 8.69k | ret = EarlySanityCheckMsgReceived(ssl, ssl->arrays->pendingMsgType, |
13506 | 8.69k | inputLength); |
13507 | 8.69k | if (ret != 0) { |
13508 | 26 | WOLFSSL_ERROR(ret); |
13509 | 26 | return ret; |
13510 | 26 | } |
13511 | | |
13512 | 8.66k | XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset, |
13513 | 8.66k | input + *inOutIdx, inputLength); |
13514 | 8.66k | ssl->arrays->pendingMsgOffset += inputLength; |
13515 | 8.66k | *inOutIdx += inputLength + ssl->keys.padSz; |
13516 | | |
13517 | 8.66k | if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz) |
13518 | 68 | { |
13519 | 68 | word32 idx = 0; |
13520 | 68 | ret = DoTls13HandShakeMsgType(ssl, |
13521 | 68 | ssl->arrays->pendingMsg + HANDSHAKE_HEADER_SZ, |
13522 | 68 | &idx, ssl->arrays->pendingMsgType, |
13523 | 68 | ssl->arrays->pendingMsgSz - HANDSHAKE_HEADER_SZ, |
13524 | 68 | ssl->arrays->pendingMsgSz); |
13525 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
13526 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
13527 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) { |
13528 | | /* setup to process fragment again */ |
13529 | | ssl->arrays->pendingMsgOffset -= inputLength; |
13530 | | *inOutIdx -= inputLength + ssl->keys.padSz; |
13531 | | } |
13532 | | else |
13533 | | #endif |
13534 | 68 | { |
13535 | 68 | XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS); |
13536 | 68 | ssl->arrays->pendingMsg = NULL; |
13537 | 68 | ssl->arrays->pendingMsgSz = 0; |
13538 | 68 | } |
13539 | 68 | } |
13540 | 8.66k | } |
13541 | | |
13542 | 20.3k | WOLFSSL_LEAVE("DoTls13HandShakeMsg", ret); |
13543 | 20.3k | return ret; |
13544 | 21.1k | } |
13545 | | |
13546 | | #ifndef NO_WOLFSSL_CLIENT |
13547 | | |
13548 | | /* The client connecting to the server. |
13549 | | * The protocol version is expecting to be TLS v1.3. |
13550 | | * If the server downgrades, and older versions of the protocol are compiled |
13551 | | * in, the client will fallback to wolfSSL_connect(). |
13552 | | * Please see note at top of README if you get an error from connect. |
13553 | | * |
13554 | | * ssl The SSL/TLS object. |
13555 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
13556 | | * unrecoverable error occurs and 0 otherwise. |
13557 | | * For more error information use wolfSSL_get_error(). |
13558 | | */ |
13559 | | int wolfSSL_connect_TLSv13(WOLFSSL* ssl) |
13560 | 0 | { |
13561 | 0 | int advanceState; |
13562 | 0 | int ret = 0; |
13563 | |
|
13564 | 0 | WOLFSSL_ENTER("wolfSSL_connect_TLSv13"); |
13565 | |
|
13566 | 0 | #ifdef HAVE_ERRNO_H |
13567 | 0 | errno = 0; |
13568 | 0 | #endif |
13569 | |
|
13570 | 0 | if (ssl == NULL) |
13571 | 0 | return BAD_FUNC_ARG; |
13572 | | |
13573 | 0 | if (ssl->options.side != WOLFSSL_CLIENT_END) { |
13574 | 0 | ssl->error = SIDE_ERROR; |
13575 | 0 | WOLFSSL_ERROR(ssl->error); |
13576 | 0 | return WOLFSSL_FATAL_ERROR; |
13577 | 0 | } |
13578 | | |
13579 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
13580 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
13581 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
13582 | 0 | return ret; |
13583 | 0 | } |
13584 | | |
13585 | | #ifdef WOLFSSL_DTLS |
13586 | | if (ssl->version.major == DTLS_MAJOR) { |
13587 | | ssl->options.dtls = 1; |
13588 | | ssl->options.dtlsStateful = 1; |
13589 | | } |
13590 | | #endif |
13591 | | |
13592 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
13593 | | if ((ssl->ConnectFilter != NULL) && |
13594 | | (ssl->options.connectState == CONNECT_BEGIN)) |
13595 | | { |
13596 | | wolfSSL_netfilter_decision_t res; |
13597 | | if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) == |
13598 | | WOLFSSL_SUCCESS) && |
13599 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
13600 | | ssl->error = SOCKET_FILTERED_E; |
13601 | | WOLFSSL_ERROR(ssl->error); |
13602 | | return WOLFSSL_FATAL_ERROR; |
13603 | | } |
13604 | | } |
13605 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
13606 | | |
13607 | | /* fragOffset is non-zero when sending fragments. On the last |
13608 | | * fragment, fragOffset is zero again, and the state can be |
13609 | | * advanced. Also, only advance from states in which we send data */ |
13610 | 0 | advanceState = (ssl->options.connectState == CONNECT_BEGIN || |
13611 | 0 | ssl->options.connectState == HELLO_AGAIN || |
13612 | 0 | (ssl->options.connectState >= FIRST_REPLY_DONE && |
13613 | 0 | ssl->options.connectState <= FIRST_REPLY_FOURTH)); |
13614 | |
|
13615 | | #ifdef WOLFSSL_DTLS13 |
13616 | | if (ssl->options.dtls) |
13617 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
13618 | | && !ssl->dtls13SendingAckOrRtx; |
13619 | | #endif /* WOLFSSL_DTLS13 */ |
13620 | |
|
13621 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
13622 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13623 | | /* do not send buffered or advance state if last error was an |
13624 | | async pending operation */ |
13625 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
13626 | | #endif |
13627 | 0 | ) { |
13628 | 0 | if ((ret = SendBuffered(ssl)) == 0) { |
13629 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
13630 | 0 | if (advanceState) { |
13631 | | #ifdef WOLFSSL_DTLS13 |
13632 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) && |
13633 | | ssl->options.connectState == FIRST_REPLY_FOURTH) { |
13634 | | /* WAIT_FINISHED_ACK is a state added afterwards, but it |
13635 | | can't follow FIRST_REPLY_FOURTH in the enum order. Indeed |
13636 | | the value of the enum ConnectState is stored in |
13637 | | serialized session. This would make importing serialized |
13638 | | session from other wolfSSL version incompatible */ |
13639 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13640 | | } |
13641 | | else |
13642 | | #endif /* WOLFSSL_DTLS13 */ |
13643 | 0 | { |
13644 | 0 | ssl->options.connectState++; |
13645 | 0 | } |
13646 | 0 | WOLFSSL_MSG("connect state: " |
13647 | 0 | "Advanced from last buffered fragment send"); |
13648 | 0 | #ifdef WOLFSSL_ASYNC_IO |
13649 | 0 | FreeAsyncCtx(ssl, 0); |
13650 | 0 | #endif |
13651 | |
|
13652 | 0 | } |
13653 | 0 | } |
13654 | 0 | else { |
13655 | 0 | WOLFSSL_MSG("connect state: " |
13656 | 0 | "Not advanced, more fragments to send"); |
13657 | 0 | } |
13658 | | #ifdef WOLFSSL_DTLS13 |
13659 | | if (ssl->options.dtls) |
13660 | | ssl->dtls13SendingAckOrRtx = 0; |
13661 | | #endif /* WOLFSSL_DTLS13 */ |
13662 | |
|
13663 | 0 | } |
13664 | 0 | else { |
13665 | 0 | ssl->error = ret; |
13666 | 0 | WOLFSSL_ERROR(ssl->error); |
13667 | 0 | return WOLFSSL_FATAL_ERROR; |
13668 | 0 | } |
13669 | 0 | } |
13670 | | |
13671 | 0 | ret = RetrySendAlert(ssl); |
13672 | 0 | if (ret != 0) { |
13673 | 0 | ssl->error = ret; |
13674 | 0 | WOLFSSL_ERROR(ssl->error); |
13675 | 0 | return WOLFSSL_FATAL_ERROR; |
13676 | 0 | } |
13677 | | |
13678 | | #ifdef WOLFSSL_DTLS13 |
13679 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
13680 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
13681 | | WOLFSSL_ERROR(ssl->error); |
13682 | | return WOLFSSL_FATAL_ERROR; |
13683 | | } |
13684 | | |
13685 | | /* we sent all the fragments. Advance state. */ |
13686 | | ssl->options.connectState++; |
13687 | | } |
13688 | | #endif /* WOLFSSL_DTLS13 */ |
13689 | | |
13690 | 0 | switch (ssl->options.connectState) { |
13691 | | |
13692 | 0 | case CONNECT_BEGIN: |
13693 | | /* Always send client hello first. */ |
13694 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13695 | 0 | WOLFSSL_ERROR(ssl->error); |
13696 | 0 | return WOLFSSL_FATAL_ERROR; |
13697 | 0 | } |
13698 | | |
13699 | 0 | ssl->options.connectState = CLIENT_HELLO_SENT; |
13700 | 0 | WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); |
13701 | 0 | FALL_THROUGH; |
13702 | |
|
13703 | 0 | case CLIENT_HELLO_SENT: |
13704 | | #ifdef WOLFSSL_EARLY_DATA |
13705 | | if (ssl->earlyData != no_early_data && |
13706 | | ssl->options.handShakeState != CLIENT_HELLO_COMPLETE) { |
13707 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13708 | | if (!ssl->options.dtls && |
13709 | | ssl->options.tls13MiddleBoxCompat) { |
13710 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13711 | | WOLFSSL_ERROR(ssl->error); |
13712 | | return WOLFSSL_FATAL_ERROR; |
13713 | | } |
13714 | | ssl->options.sentChangeCipher = 1; |
13715 | | } |
13716 | | #endif |
13717 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13718 | | return WOLFSSL_SUCCESS; |
13719 | | } |
13720 | | #endif |
13721 | | /* Get the response/s from the server. */ |
13722 | 0 | while (ssl->options.serverState < |
13723 | 0 | SERVER_HELLOVERIFYREQUEST_COMPLETE) { |
13724 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13725 | 0 | WOLFSSL_ERROR(ssl->error); |
13726 | 0 | return WOLFSSL_FATAL_ERROR; |
13727 | 0 | } |
13728 | |
|
13729 | | #ifdef WOLFSSL_DTLS13 |
13730 | | if (ssl->options.dtls) { |
13731 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13732 | | WOLFSSL_ERROR(ssl->error); |
13733 | | return WOLFSSL_FATAL_ERROR; |
13734 | | } |
13735 | | } |
13736 | | #endif /* WOLFSSL_DTLS13 */ |
13737 | 0 | } |
13738 | | |
13739 | 0 | if (!ssl->options.tls1_3) { |
13740 | 0 | #ifndef WOLFSSL_NO_TLS12 |
13741 | 0 | if (ssl->options.downgrade) |
13742 | 0 | return wolfSSL_connect(ssl); |
13743 | 0 | #endif |
13744 | 0 | WOLFSSL_MSG("Client using higher version, fatal error"); |
13745 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
13746 | 0 | return VERSION_ERROR; |
13747 | 0 | } |
13748 | | |
13749 | 0 | ssl->options.connectState = HELLO_AGAIN; |
13750 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN"); |
13751 | 0 | FALL_THROUGH; |
13752 | |
|
13753 | 0 | case HELLO_AGAIN: |
13754 | |
|
13755 | 0 | if (ssl->options.serverState == |
13756 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
13757 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13758 | | if (!ssl->options.dtls && !ssl->options.sentChangeCipher |
13759 | | && ssl->options.tls13MiddleBoxCompat) { |
13760 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13761 | | WOLFSSL_ERROR(ssl->error); |
13762 | | return WOLFSSL_FATAL_ERROR; |
13763 | | } |
13764 | | ssl->options.sentChangeCipher = 1; |
13765 | | } |
13766 | | #endif |
13767 | | /* Try again with different security parameters. */ |
13768 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
13769 | 0 | WOLFSSL_ERROR(ssl->error); |
13770 | 0 | return WOLFSSL_FATAL_ERROR; |
13771 | 0 | } |
13772 | 0 | } |
13773 | | |
13774 | 0 | ssl->options.connectState = HELLO_AGAIN_REPLY; |
13775 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY"); |
13776 | 0 | FALL_THROUGH; |
13777 | |
|
13778 | 0 | case HELLO_AGAIN_REPLY: |
13779 | | /* Get the response/s from the server. */ |
13780 | 0 | while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13781 | | #ifdef WOLFSSL_DTLS13 |
13782 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
13783 | | #ifndef WOLFSSL_NO_TLS12 |
13784 | | if (ssl->options.downgrade) |
13785 | | return wolfSSL_connect(ssl); |
13786 | | #endif |
13787 | | } |
13788 | | #endif /* WOLFSSL_DTLS13 */ |
13789 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13790 | 0 | WOLFSSL_ERROR(ssl->error); |
13791 | 0 | return WOLFSSL_FATAL_ERROR; |
13792 | 0 | } |
13793 | |
|
13794 | | #ifdef WOLFSSL_DTLS13 |
13795 | | if (ssl->options.dtls) { |
13796 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13797 | | WOLFSSL_ERROR(ssl->error); |
13798 | | return WOLFSSL_FATAL_ERROR; |
13799 | | } |
13800 | | } |
13801 | | #endif /* WOLFSSL_DTLS13 */ |
13802 | 0 | } |
13803 | | |
13804 | 0 | ssl->options.connectState = FIRST_REPLY_DONE; |
13805 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_DONE"); |
13806 | 0 | FALL_THROUGH; |
13807 | |
|
13808 | 0 | case FIRST_REPLY_DONE: |
13809 | 0 | if (ssl->options.certOnly) |
13810 | 0 | return WOLFSSL_SUCCESS; |
13811 | | #ifdef WOLFSSL_EARLY_DATA |
13812 | | if (!ssl->options.dtls && ssl->earlyData != no_early_data |
13813 | | && !WOLFSSL_IS_QUIC(ssl)) { |
13814 | | if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) { |
13815 | | WOLFSSL_ERROR(ssl->error); |
13816 | | return WOLFSSL_FATAL_ERROR; |
13817 | | } |
13818 | | WOLFSSL_MSG("sent: end_of_early_data"); |
13819 | | } |
13820 | | #endif |
13821 | | |
13822 | 0 | ssl->options.connectState = FIRST_REPLY_FIRST; |
13823 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST"); |
13824 | 0 | FALL_THROUGH; |
13825 | |
|
13826 | 0 | case FIRST_REPLY_FIRST: |
13827 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
13828 | | if (!ssl->options.sentChangeCipher && !ssl->options.dtls |
13829 | | && ssl->options.tls13MiddleBoxCompat) { |
13830 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
13831 | | WOLFSSL_ERROR(ssl->error); |
13832 | | return WOLFSSL_FATAL_ERROR; |
13833 | | } |
13834 | | ssl->options.sentChangeCipher = 1; |
13835 | | } |
13836 | | #endif |
13837 | |
|
13838 | 0 | ssl->options.connectState = FIRST_REPLY_SECOND; |
13839 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND"); |
13840 | 0 | FALL_THROUGH; |
13841 | |
|
13842 | 0 | case FIRST_REPLY_SECOND: |
13843 | | /* CLIENT: check peer authentication. */ |
13844 | 0 | if (!ssl->options.peerAuthGood) { |
13845 | 0 | WOLFSSL_MSG("Server authentication did not happen"); |
13846 | 0 | WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR); |
13847 | 0 | return WOLFSSL_FATAL_ERROR; |
13848 | 0 | } |
13849 | 0 | #ifndef NO_CERTS |
13850 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13851 | 0 | ssl->error = SendTls13Certificate(ssl); |
13852 | 0 | if (ssl->error != 0) { |
13853 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13854 | 0 | WOLFSSL_ERROR(ssl->error); |
13855 | 0 | return WOLFSSL_FATAL_ERROR; |
13856 | 0 | } |
13857 | 0 | WOLFSSL_MSG("sent: certificate"); |
13858 | 0 | } |
13859 | 0 | #endif |
13860 | | |
13861 | 0 | ssl->options.connectState = FIRST_REPLY_THIRD; |
13862 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD"); |
13863 | 0 | FALL_THROUGH; |
13864 | |
|
13865 | 0 | case FIRST_REPLY_THIRD: |
13866 | 0 | #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
13867 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
13868 | 0 | defined(HAVE_FALCON) || defined(HAVE_DILITHIUM))) && \ |
13869 | 0 | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
13870 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
13871 | 0 | ssl->error = SendTls13CertificateVerify(ssl); |
13872 | 0 | if (ssl->error != 0) { |
13873 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13874 | 0 | WOLFSSL_ERROR(ssl->error); |
13875 | 0 | return WOLFSSL_FATAL_ERROR; |
13876 | 0 | } |
13877 | 0 | WOLFSSL_MSG("sent: certificate verify"); |
13878 | 0 | } |
13879 | 0 | #endif |
13880 | | |
13881 | 0 | ssl->options.connectState = FIRST_REPLY_FOURTH; |
13882 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH"); |
13883 | 0 | FALL_THROUGH; |
13884 | |
|
13885 | 0 | case FIRST_REPLY_FOURTH: |
13886 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
13887 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
13888 | 0 | WOLFSSL_ERROR(ssl->error); |
13889 | 0 | return WOLFSSL_FATAL_ERROR; |
13890 | 0 | } |
13891 | 0 | WOLFSSL_MSG("sent: finished"); |
13892 | |
|
13893 | | #ifdef WOLFSSL_DTLS13 |
13894 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
13895 | | WOLFSSL_MSG("connect state: WAIT_FINISHED_ACK"); |
13896 | | FALL_THROUGH; |
13897 | | |
13898 | | case WAIT_FINISHED_ACK: |
13899 | | if (ssl->options.dtls) { |
13900 | | while (ssl->options.serverState != SERVER_FINISHED_ACKED) { |
13901 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
13902 | | WOLFSSL_ERROR(ssl->error); |
13903 | | return WOLFSSL_FATAL_ERROR; |
13904 | | } |
13905 | | |
13906 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
13907 | | WOLFSSL_ERROR(ssl->error); |
13908 | | return WOLFSSL_FATAL_ERROR; |
13909 | | } |
13910 | | } |
13911 | | } |
13912 | | #endif /* WOLFSSL_DTLS13 */ |
13913 | 0 | ssl->options.connectState = FINISHED_DONE; |
13914 | 0 | WOLFSSL_MSG("connect state: FINISHED_DONE"); |
13915 | 0 | FALL_THROUGH; |
13916 | |
|
13917 | 0 | case FINISHED_DONE: |
13918 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
13919 | 0 | if (ssl->hsDoneCb != NULL) { |
13920 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
13921 | 0 | if (cbret < 0) { |
13922 | 0 | ssl->error = cbret; |
13923 | 0 | WOLFSSL_ERROR_VERBOSE(ssl->error); |
13924 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
13925 | 0 | return WOLFSSL_FATAL_ERROR; |
13926 | 0 | } |
13927 | 0 | } |
13928 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
13929 | | |
13930 | 0 | if (!ssl->options.keepResources) { |
13931 | 0 | FreeHandshakeResources(ssl); |
13932 | 0 | } |
13933 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
13934 | | /* Free the remaining async context if not using it for crypto */ |
13935 | 0 | FreeAsyncCtx(ssl, 1); |
13936 | 0 | #endif |
13937 | |
|
13938 | 0 | ssl->error = 0; /* clear the error */ |
13939 | |
|
13940 | 0 | WOLFSSL_LEAVE("wolfSSL_connect_TLSv13", WOLFSSL_SUCCESS); |
13941 | 0 | return WOLFSSL_SUCCESS; |
13942 | | |
13943 | 0 | default: |
13944 | 0 | WOLFSSL_MSG("Unknown connect state ERROR"); |
13945 | 0 | return WOLFSSL_FATAL_ERROR; /* unknown connect state */ |
13946 | 0 | } |
13947 | 0 | } |
13948 | | #endif |
13949 | | |
13950 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
13951 | | /* Send a cookie with the HelloRetryRequest to avoid storing state. |
13952 | | * |
13953 | | * ssl SSL/TLS object. |
13954 | | * secret Secret to use when generating integrity check for cookie. |
13955 | | * A value of NULL indicates to generate a new random secret. |
13956 | | * secretSz Size of secret data in bytes. |
13957 | | * Use a value of 0 to indicate use of default size. |
13958 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when |
13959 | | * called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
13960 | | */ |
13961 | | int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret, |
13962 | | unsigned int secretSz) |
13963 | | { |
13964 | | int ret; |
13965 | | |
13966 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
13967 | | return BAD_FUNC_ARG; |
13968 | | #ifndef NO_WOLFSSL_SERVER |
13969 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
13970 | | return SIDE_ERROR; |
13971 | | |
13972 | | if (secretSz == 0) { |
13973 | | #ifndef NO_SHA256 |
13974 | | secretSz = WC_SHA256_DIGEST_SIZE; |
13975 | | #elif defined(WOLFSSL_SHA384) |
13976 | | secretSz = WC_SHA384_DIGEST_SIZE; |
13977 | | #elif defined(WOLFSSL_TLS13_SHA512) |
13978 | | secretSz = WC_SHA512_DIGEST_SIZE; |
13979 | | #elif defined(WOLFSSL_SM3) |
13980 | | secretSz = WC_SM3_DIGEST_SIZE; |
13981 | | #else |
13982 | | #error "No digest to available to use with HMAC for cookies." |
13983 | | #endif /* NO_SHA */ |
13984 | | } |
13985 | | |
13986 | | if (secretSz != ssl->buffers.tls13CookieSecret.length) { |
13987 | | byte* newSecret; |
13988 | | |
13989 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
13990 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
13991 | | ssl->buffers.tls13CookieSecret.length); |
13992 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, |
13993 | | ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
13994 | | } |
13995 | | |
13996 | | newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
13997 | | DYNAMIC_TYPE_COOKIE_PWD); |
13998 | | if (newSecret == NULL) { |
13999 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
14000 | | ssl->buffers.tls13CookieSecret.length = 0; |
14001 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
14002 | | return MEMORY_ERROR; |
14003 | | } |
14004 | | ssl->buffers.tls13CookieSecret.buffer = newSecret; |
14005 | | ssl->buffers.tls13CookieSecret.length = secretSz; |
14006 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
14007 | | wc_MemZero_Add("wolfSSL_send_hrr_cookie secret", |
14008 | | ssl->buffers.tls13CookieSecret.buffer, |
14009 | | ssl->buffers.tls13CookieSecret.length); |
14010 | | #endif |
14011 | | } |
14012 | | |
14013 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
14014 | | if (secret == NULL) { |
14015 | | ret = wc_RNG_GenerateBlock(ssl->rng, |
14016 | | ssl->buffers.tls13CookieSecret.buffer, secretSz); |
14017 | | if (ret < 0) |
14018 | | return ret; |
14019 | | } |
14020 | | else |
14021 | | XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz); |
14022 | | |
14023 | | ssl->options.sendCookie = 1; |
14024 | | |
14025 | | ret = WOLFSSL_SUCCESS; |
14026 | | #else |
14027 | | (void)secret; |
14028 | | (void)secretSz; |
14029 | | |
14030 | | ret = SIDE_ERROR; |
14031 | | #endif |
14032 | | |
14033 | | return ret; |
14034 | | } |
14035 | | |
14036 | | int wolfSSL_disable_hrr_cookie(WOLFSSL* ssl) |
14037 | | { |
14038 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14039 | | return BAD_FUNC_ARG; |
14040 | | |
14041 | | #ifdef NO_WOLFSSL_SERVER |
14042 | | return SIDE_ERROR; |
14043 | | #else |
14044 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14045 | | return SIDE_ERROR; |
14046 | | |
14047 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
14048 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
14049 | | ssl->buffers.tls13CookieSecret.length); |
14050 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap, |
14051 | | DYNAMIC_TYPE_COOKIE_PWD); |
14052 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
14053 | | ssl->buffers.tls13CookieSecret.length = 0; |
14054 | | } |
14055 | | |
14056 | | ssl->options.sendCookie = 0; |
14057 | | return WOLFSSL_SUCCESS; |
14058 | | #endif /* NO_WOLFSSL_SERVER */ |
14059 | | } |
14060 | | |
14061 | | #endif /* defined(WOLFSSL_SEND_HRR_COOKIE) */ |
14062 | | |
14063 | | #ifdef HAVE_SUPPORTED_CURVES |
14064 | | /* Create a key share entry from group. |
14065 | | * Generates a key pair. |
14066 | | * |
14067 | | * ssl The SSL/TLS object. |
14068 | | * group The named group. |
14069 | | * returns 0 on success, otherwise failure. |
14070 | | * for async can return WC_PENDING_E and should be called again |
14071 | | */ |
14072 | | int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group) |
14073 | 0 | { |
14074 | 0 | int ret; |
14075 | |
|
14076 | 0 | if (ssl == NULL) |
14077 | 0 | return BAD_FUNC_ARG; |
14078 | | |
14079 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14080 | | ret = wolfSSL_AsyncPop(ssl, NULL); |
14081 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
14082 | | /* Check for error */ |
14083 | | if (ret < 0) |
14084 | | return ret; |
14085 | | } |
14086 | | #endif |
14087 | | |
14088 | 0 | #if defined(WOLFSSL_HAVE_MLKEM) |
14089 | 0 | if (WOLFSSL_NAMED_GROUP_IS_PQC(group) || |
14090 | 0 | WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group)) { |
14091 | |
|
14092 | 0 | if (ssl->ctx != NULL && ssl->ctx->method != NULL && |
14093 | 0 | !IsAtLeastTLSv1_3(ssl->version)) { |
14094 | 0 | return BAD_FUNC_ARG; |
14095 | 0 | } |
14096 | | |
14097 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14098 | | /* If I am the server of a KEM connection, do not do keygen because |
14099 | | * I'm going to encapsulate with the client's public key. Note that |
14100 | | * I might be the client and ssl->option.side has not been properly |
14101 | | * set yet. In that case the KeyGen operation will be deferred to |
14102 | | * connection time. */ |
14103 | 0 | return WOLFSSL_SUCCESS; |
14104 | 0 | } |
14105 | 0 | } |
14106 | 0 | #endif |
14107 | | #if defined(NO_TLS) |
14108 | | (void)ret; |
14109 | | (void)group; |
14110 | | #else |
14111 | | /* Check if the group is supported. */ |
14112 | 0 | if (!TLSX_IsGroupSupported(group)) { |
14113 | 0 | WOLFSSL_MSG("Group not supported."); |
14114 | 0 | return BAD_FUNC_ARG; |
14115 | 0 | } |
14116 | | |
14117 | 0 | ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions); |
14118 | 0 | if (ret != 0) |
14119 | 0 | return ret; |
14120 | 0 | #endif /* NO_TLS */ |
14121 | 0 | return WOLFSSL_SUCCESS; |
14122 | 0 | } |
14123 | | |
14124 | | /* Send no key share entries - use HelloRetryRequest to negotiate shared group. |
14125 | | * |
14126 | | * ssl The SSL/TLS object. |
14127 | | * returns 0 on success, otherwise failure. |
14128 | | */ |
14129 | | int wolfSSL_NoKeyShares(WOLFSSL* ssl) |
14130 | 0 | { |
14131 | 0 | int ret; |
14132 | |
|
14133 | 0 | if (ssl == NULL) |
14134 | 0 | return BAD_FUNC_ARG; |
14135 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
14136 | 0 | return SIDE_ERROR; |
14137 | | #if defined(NO_TLS) |
14138 | | (void)ret; |
14139 | | #else |
14140 | 0 | ret = TLSX_KeyShare_Empty(ssl); |
14141 | 0 | if (ret != 0) |
14142 | 0 | return ret; |
14143 | 0 | #endif /* NO_TLS */ |
14144 | 0 | return WOLFSSL_SUCCESS; |
14145 | 0 | } |
14146 | | #endif |
14147 | | |
14148 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
14149 | | int wolfSSL_UseCKS(WOLFSSL* ssl, byte *sigSpec, word16 sigSpecSz) |
14150 | | { |
14151 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->ctx->method->version) || |
14152 | | sigSpec == NULL || sigSpecSz == 0) |
14153 | | return BAD_FUNC_ARG; |
14154 | | |
14155 | | ssl->sigSpec = sigSpec; |
14156 | | ssl->sigSpecSz = sigSpecSz; |
14157 | | return WOLFSSL_SUCCESS; |
14158 | | } |
14159 | | |
14160 | | int wolfSSL_CTX_UseCKS(WOLFSSL_CTX* ctx, byte *sigSpec, word16 sigSpecSz) |
14161 | | { |
14162 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version) || |
14163 | | sigSpec == NULL || sigSpecSz == 0) |
14164 | | return BAD_FUNC_ARG; |
14165 | | |
14166 | | ctx->sigSpec = sigSpec; |
14167 | | ctx->sigSpecSz = sigSpecSz; |
14168 | | return WOLFSSL_SUCCESS; |
14169 | | } |
14170 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
14171 | | |
14172 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
14173 | | * |
14174 | | * ctx The SSL/TLS CTX object. |
14175 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14176 | | */ |
14177 | | int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx) |
14178 | 0 | { |
14179 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14180 | 0 | return BAD_FUNC_ARG; |
14181 | 0 | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14182 | 0 | return SIDE_ERROR; |
14183 | | |
14184 | | #ifdef HAVE_SESSION_TICKET |
14185 | | ctx->noTicketTls13 = 1; |
14186 | | #endif |
14187 | | |
14188 | 0 | return 0; |
14189 | 0 | } |
14190 | | |
14191 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
14192 | | * |
14193 | | * ssl The SSL/TLS object. |
14194 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on |
14195 | | * a client and 0 on success. |
14196 | | */ |
14197 | | int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl) |
14198 | 0 | { |
14199 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14200 | 0 | return BAD_FUNC_ARG; |
14201 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14202 | 0 | return SIDE_ERROR; |
14203 | | |
14204 | | #ifdef HAVE_SESSION_TICKET |
14205 | | ssl->options.noTicketTls13 = 1; |
14206 | | #endif |
14207 | | |
14208 | 0 | return 0; |
14209 | 0 | } |
14210 | | |
14211 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
14212 | | * |
14213 | | * ctx The SSL/TLS CTX object. |
14214 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14215 | | */ |
14216 | | int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx) |
14217 | 0 | { |
14218 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14219 | 0 | return BAD_FUNC_ARG; |
14220 | | |
14221 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14222 | | ctx->noPskDheKe = 1; |
14223 | | #endif |
14224 | | |
14225 | 0 | return 0; |
14226 | 0 | } |
14227 | | |
14228 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
14229 | | * |
14230 | | * ssl The SSL/TLS object. |
14231 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
14232 | | * success. |
14233 | | */ |
14234 | | int wolfSSL_no_dhe_psk(WOLFSSL* ssl) |
14235 | 0 | { |
14236 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14237 | 0 | return BAD_FUNC_ARG; |
14238 | | |
14239 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14240 | | ssl->options.noPskDheKe = 1; |
14241 | | #endif |
14242 | | |
14243 | 0 | return 0; |
14244 | 0 | } |
14245 | | |
14246 | | #ifdef HAVE_SUPPORTED_CURVES |
14247 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
14248 | | * |
14249 | | * ctx The SSL/TLS CTX object. |
14250 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14251 | | */ |
14252 | | int wolfSSL_CTX_only_dhe_psk(WOLFSSL_CTX* ctx) |
14253 | 0 | { |
14254 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14255 | 0 | return BAD_FUNC_ARG; |
14256 | | |
14257 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14258 | | ctx->onlyPskDheKe = 1; |
14259 | | #endif |
14260 | | |
14261 | 0 | return 0; |
14262 | 0 | } |
14263 | | |
14264 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
14265 | | * |
14266 | | * ssl The SSL/TLS object. |
14267 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
14268 | | * success. |
14269 | | */ |
14270 | | int wolfSSL_only_dhe_psk(WOLFSSL* ssl) |
14271 | 0 | { |
14272 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14273 | 0 | return BAD_FUNC_ARG; |
14274 | | |
14275 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14276 | | ssl->options.onlyPskDheKe = 1; |
14277 | | #endif |
14278 | | |
14279 | 0 | return 0; |
14280 | 0 | } |
14281 | | #endif /* HAVE_SUPPORTED_CURVES */ |
14282 | | |
14283 | | int Tls13UpdateKeys(WOLFSSL* ssl) |
14284 | 0 | { |
14285 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14286 | 0 | return BAD_FUNC_ARG; |
14287 | | |
14288 | | #ifdef WOLFSSL_DTLS13 |
14289 | | /* we are already waiting for the ack of a sent key update message. We can't |
14290 | | send another one before receiving its ack. Either wolfSSL_update_keys() |
14291 | | was invoked multiple times over a short period of time or we replied to a |
14292 | | KeyUpdate with update request. We'll just ignore sending this |
14293 | | KeyUpdate. */ |
14294 | | /* TODO: add WOLFSSL_ERROR_ALREADY_IN_PROGRESS type of error here */ |
14295 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) |
14296 | | return 0; |
14297 | | #endif /* WOLFSSL_DTLS13 */ |
14298 | | |
14299 | 0 | return SendTls13KeyUpdate(ssl); |
14300 | 0 | } |
14301 | | |
14302 | | /* Update the keys for encryption and decryption. |
14303 | | * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then |
14304 | | * calling wolfSSL_write() will have the message sent when ready. |
14305 | | * |
14306 | | * ssl The SSL/TLS object. |
14307 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14308 | | * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write, |
14309 | | * WOLFSSL_SUCCESS on success and otherwise failure. |
14310 | | */ |
14311 | | int wolfSSL_update_keys(WOLFSSL* ssl) |
14312 | 0 | { |
14313 | 0 | int ret; |
14314 | 0 | ret = Tls13UpdateKeys(ssl); |
14315 | 0 | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14316 | 0 | ret = WOLFSSL_ERROR_WANT_WRITE; |
14317 | 0 | else if (ret == 0) |
14318 | 0 | ret = WOLFSSL_SUCCESS; |
14319 | 0 | return ret; |
14320 | 0 | } |
14321 | | |
14322 | | /* Whether a response is waiting for key update request. |
14323 | | * |
14324 | | * ssl The SSL/TLS object. |
14325 | | * required 0 when no key update response required. |
14326 | | * 1 when no key update response required. |
14327 | | * return 0 on success. |
14328 | | * return BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3 |
14329 | | */ |
14330 | | int wolfSSL_key_update_response(WOLFSSL* ssl, int* required) |
14331 | 0 | { |
14332 | 0 | if (required == NULL || ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14333 | 0 | return BAD_FUNC_ARG; |
14334 | | |
14335 | 0 | *required = ssl->keys.updateResponseReq; |
14336 | |
|
14337 | 0 | return 0; |
14338 | 0 | } |
14339 | | |
14340 | | #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
14341 | | /* Allow post-handshake authentication in TLS v1.3 connections. |
14342 | | * |
14343 | | * ctx The SSL/TLS CTX object. |
14344 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and |
14345 | | * 0 on success. |
14346 | | */ |
14347 | | int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx) |
14348 | | { |
14349 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14350 | | return BAD_FUNC_ARG; |
14351 | | if (ctx->method->side == WOLFSSL_SERVER_END) |
14352 | | return SIDE_ERROR; |
14353 | | |
14354 | | ctx->postHandshakeAuth = 1; |
14355 | | |
14356 | | return 0; |
14357 | | } |
14358 | | |
14359 | | /* Allow post-handshake authentication in TLS v1.3 connection. |
14360 | | * |
14361 | | * ssl The SSL/TLS object. |
14362 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14363 | | * SIDE_ERROR when not a client and 0 on success. |
14364 | | */ |
14365 | | int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl) |
14366 | | { |
14367 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14368 | | return BAD_FUNC_ARG; |
14369 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
14370 | | return SIDE_ERROR; |
14371 | | |
14372 | | ssl->options.postHandshakeAuth = 1; |
14373 | | |
14374 | | return 0; |
14375 | | } |
14376 | | |
14377 | | /* Request a certificate of the client. |
14378 | | * Can be called any time after handshake completion. |
14379 | | * A maximum of 256 requests can be sent on a connection. |
14380 | | * |
14381 | | * ssl SSL/TLS object. |
14382 | | */ |
14383 | | int wolfSSL_request_certificate(WOLFSSL* ssl) |
14384 | | { |
14385 | | int ret; |
14386 | | #ifndef NO_WOLFSSL_SERVER |
14387 | | CertReqCtx* certReqCtx; |
14388 | | #endif |
14389 | | |
14390 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14391 | | return BAD_FUNC_ARG; |
14392 | | #ifndef NO_WOLFSSL_SERVER |
14393 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14394 | | return SIDE_ERROR; |
14395 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14396 | | return NOT_READY_ERROR; |
14397 | | if (!ssl->options.postHandshakeAuth) |
14398 | | return POST_HAND_AUTH_ERROR; |
14399 | | if (ssl->certReqCtx != NULL) { |
14400 | | if (ssl->certReqCtx->len != 1) |
14401 | | return BAD_STATE_E; |
14402 | | /* We support sending up to 255 certificate requests */ |
14403 | | if (ssl->certReqCtx->ctx == 255) |
14404 | | return BAD_STATE_E; |
14405 | | } |
14406 | | |
14407 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap, |
14408 | | DYNAMIC_TYPE_TMP_BUFFER); |
14409 | | if (certReqCtx == NULL) |
14410 | | return MEMORY_E; |
14411 | | XMEMSET(certReqCtx, 0, sizeof(CertReqCtx)); |
14412 | | certReqCtx->next = ssl->certReqCtx; |
14413 | | certReqCtx->len = 1; |
14414 | | if (certReqCtx->next != NULL) |
14415 | | certReqCtx->ctx = certReqCtx->next->ctx + 1; |
14416 | | ssl->certReqCtx = certReqCtx; |
14417 | | |
14418 | | ssl->msgsReceived.got_certificate = 0; |
14419 | | ssl->msgsReceived.got_certificate_verify = 0; |
14420 | | ssl->msgsReceived.got_finished = 0; |
14421 | | |
14422 | | ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len); |
14423 | | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14424 | | ret = WOLFSSL_ERROR_WANT_WRITE; |
14425 | | else if (ret == 0) |
14426 | | ret = WOLFSSL_SUCCESS; |
14427 | | #else |
14428 | | ret = SIDE_ERROR; |
14429 | | #endif |
14430 | | |
14431 | | return ret; |
14432 | | } |
14433 | | #endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */ |
14434 | | |
14435 | | #if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
14436 | | /* Get the preferred key exchange group. |
14437 | | * |
14438 | | * ssl The SSL/TLS object. |
14439 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, |
14440 | | * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete |
14441 | | * and group number on success. |
14442 | | */ |
14443 | | int wolfSSL_preferred_group(WOLFSSL* ssl) |
14444 | 0 | { |
14445 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14446 | 0 | return BAD_FUNC_ARG; |
14447 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14448 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
14449 | 0 | return SIDE_ERROR; |
14450 | 0 | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14451 | 0 | return NOT_READY_ERROR; |
14452 | | |
14453 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
14454 | | /* Return supported groups only. */ |
14455 | 0 | return TLSX_SupportedCurve_Preferred(ssl, 1); |
14456 | | #else |
14457 | | return 0; |
14458 | | #endif |
14459 | | #else |
14460 | | return SIDE_ERROR; |
14461 | | #endif |
14462 | 0 | } |
14463 | | #endif |
14464 | | |
14465 | | #ifndef NO_PSK |
14466 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14467 | | * against context object. |
14468 | | * |
14469 | | * @param [in, out] ctx SSL/TLS context object. |
14470 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14471 | | */ |
14472 | | void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, |
14473 | | wc_psk_client_cs_callback cb) |
14474 | | { |
14475 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_cs_callback"); |
14476 | | |
14477 | | if (ctx == NULL) |
14478 | | return; |
14479 | | |
14480 | | ctx->havePSK = 1; |
14481 | | ctx->client_psk_cs_cb = cb; |
14482 | | } |
14483 | | |
14484 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
14485 | | * against SSL object. |
14486 | | * |
14487 | | * @param [in, out] ssl SSL/TLS object. |
14488 | | * @param [in] cb Client PSK callback passed a cipher suite. |
14489 | | */ |
14490 | | void wolfSSL_set_psk_client_cs_callback(WOLFSSL* ssl, |
14491 | | wc_psk_client_cs_callback cb) |
14492 | | { |
14493 | | byte haveRSA = 1; |
14494 | | int keySz = 0; |
14495 | | |
14496 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_cs_callback"); |
14497 | | |
14498 | | if (ssl == NULL) |
14499 | | return; |
14500 | | |
14501 | | ssl->options.havePSK = 1; |
14502 | | ssl->options.client_psk_cs_cb = cb; |
14503 | | |
14504 | | #ifdef NO_RSA |
14505 | | haveRSA = 0; |
14506 | | #endif |
14507 | | #ifndef NO_CERTS |
14508 | | keySz = ssl->buffers.keySz; |
14509 | | #endif |
14510 | | if (AllocateSuites(ssl) != 0) |
14511 | | return; |
14512 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14513 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14514 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14515 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14516 | | } |
14517 | | |
14518 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14519 | | * against context object. |
14520 | | * |
14521 | | * @param [in, out] ctx SSL/TLS context object. |
14522 | | * @param [in] cb Client PSK callback returning cipher suite. |
14523 | | */ |
14524 | | void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, |
14525 | | wc_psk_client_tls13_callback cb) |
14526 | | { |
14527 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_tls13_callback"); |
14528 | | |
14529 | | if (ctx == NULL) |
14530 | | return; |
14531 | | |
14532 | | ctx->havePSK = 1; |
14533 | | ctx->client_psk_tls13_cb = cb; |
14534 | | } |
14535 | | |
14536 | | /* Set the PSK callback that returns the cipher suite for a client to use |
14537 | | * against SSL object. |
14538 | | * |
14539 | | * @param [in, out] ssl SSL/TLS object. |
14540 | | * @param [in] cb Client PSK callback returning cipher suite. |
14541 | | */ |
14542 | | void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl, |
14543 | | wc_psk_client_tls13_callback cb) |
14544 | | { |
14545 | | byte haveRSA = 1; |
14546 | | int keySz = 0; |
14547 | | |
14548 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_tls13_callback"); |
14549 | | |
14550 | | if (ssl == NULL) |
14551 | | return; |
14552 | | |
14553 | | ssl->options.havePSK = 1; |
14554 | | ssl->options.client_psk_tls13_cb = cb; |
14555 | | |
14556 | | #ifdef NO_RSA |
14557 | | haveRSA = 0; |
14558 | | #endif |
14559 | | #ifndef NO_CERTS |
14560 | | keySz = ssl->buffers.keySz; |
14561 | | #endif |
14562 | | if (AllocateSuites(ssl) != 0) |
14563 | | return; |
14564 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14565 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14566 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14567 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14568 | | } |
14569 | | |
14570 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14571 | | * against context object. |
14572 | | * |
14573 | | * @param [in, out] ctx SSL/TLS context object. |
14574 | | * @param [in] cb Server PSK callback returning cipher suite. |
14575 | | */ |
14576 | | void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, |
14577 | | wc_psk_server_tls13_callback cb) |
14578 | | { |
14579 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_tls13_callback"); |
14580 | | if (ctx == NULL) |
14581 | | return; |
14582 | | ctx->havePSK = 1; |
14583 | | ctx->server_psk_tls13_cb = cb; |
14584 | | } |
14585 | | |
14586 | | /* Set the PSK callback that returns the cipher suite for a server to use |
14587 | | * against SSL object. |
14588 | | * |
14589 | | * @param [in, out] ssl SSL/TLS object. |
14590 | | * @param [in] cb Server PSK callback returning cipher suite. |
14591 | | */ |
14592 | | void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl, |
14593 | | wc_psk_server_tls13_callback cb) |
14594 | | { |
14595 | | byte haveRSA = 1; |
14596 | | int keySz = 0; |
14597 | | |
14598 | | WOLFSSL_ENTER("wolfSSL_set_psk_server_tls13_callback"); |
14599 | | if (ssl == NULL) |
14600 | | return; |
14601 | | |
14602 | | ssl->options.havePSK = 1; |
14603 | | ssl->options.server_psk_tls13_cb = cb; |
14604 | | |
14605 | | #ifdef NO_RSA |
14606 | | haveRSA = 0; |
14607 | | #endif |
14608 | | #ifndef NO_CERTS |
14609 | | keySz = ssl->buffers.keySz; |
14610 | | #endif |
14611 | | if (AllocateSuites(ssl) != 0) |
14612 | | return; |
14613 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
14614 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
14615 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
14616 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
14617 | | } |
14618 | | |
14619 | | /* Get name of first supported cipher suite that uses the hash indicated. |
14620 | | * |
14621 | | * @param [in] ssl SSL/TLS object. |
14622 | | * @param [in] hash Name of hash algorithm. e.g. "SHA256", "SHA384" |
14623 | | * @return Name of cipher suite. |
14624 | | * @return NULL on failure. |
14625 | | */ |
14626 | | const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash) |
14627 | | { |
14628 | | const char* name = NULL; |
14629 | | byte mac = no_mac; |
14630 | | int i; |
14631 | | const Suites* suites = WOLFSSL_SUITES(ssl); |
14632 | | |
14633 | | if (XSTRCMP(hash, "SHA256") == 0) { |
14634 | | mac = sha256_mac; |
14635 | | } |
14636 | | else if (XSTRCMP(hash, "SHA384") == 0) { |
14637 | | mac = sha384_mac; |
14638 | | } |
14639 | | if (mac != no_mac) { |
14640 | | for (i = 0; i < suites->suiteSz; i += 2) { |
14641 | | if (SuiteMac(suites->suites + i) == mac) { |
14642 | | name = GetCipherNameInternal(suites->suites[i + 0], |
14643 | | suites->suites[i + 1]); |
14644 | | break; |
14645 | | } |
14646 | | } |
14647 | | } |
14648 | | return name; |
14649 | | } |
14650 | | #endif /* !NO_PSK */ |
14651 | | |
14652 | | |
14653 | | #ifndef NO_WOLFSSL_SERVER |
14654 | | |
14655 | | /* The server accepting a connection from a client. |
14656 | | * The protocol version is expecting to be TLS v1.3. |
14657 | | * If the client downgrades, and older versions of the protocol are compiled |
14658 | | * in, the server will fallback to wolfSSL_accept(). |
14659 | | * Please see note at top of README if you get an error from accept. |
14660 | | * |
14661 | | * ssl The SSL/TLS object. |
14662 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
14663 | | * unrecoverable error occurs and 0 otherwise. |
14664 | | * For more error information use wolfSSL_get_error(). |
14665 | | */ |
14666 | | int wolfSSL_accept_TLSv13(WOLFSSL* ssl) |
14667 | | { |
14668 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14669 | | word16 havePSK = 0; |
14670 | | #endif |
14671 | | int ret = 0; |
14672 | | |
14673 | | WOLFSSL_ENTER("wolfSSL_accept_TLSv13"); |
14674 | | |
14675 | | #ifdef HAVE_ERRNO_H |
14676 | | errno = 0; |
14677 | | #endif |
14678 | | |
14679 | | if (ssl == NULL) |
14680 | | return WOLFSSL_FATAL_ERROR; |
14681 | | |
14682 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
14683 | | havePSK = ssl->options.havePSK; |
14684 | | #endif |
14685 | | |
14686 | | if (ssl->options.side != WOLFSSL_SERVER_END) { |
14687 | | ssl->error = SIDE_ERROR; |
14688 | | WOLFSSL_ERROR(ssl->error); |
14689 | | return WOLFSSL_FATAL_ERROR; |
14690 | | } |
14691 | | |
14692 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
14693 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
14694 | | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
14695 | | return ret; |
14696 | | } |
14697 | | |
14698 | | #ifdef WOLFSSL_DTLS |
14699 | | if (ssl->version.major == DTLS_MAJOR) { |
14700 | | ssl->options.dtls = 1; |
14701 | | if (!IsDtlsNotSctpMode(ssl) || !ssl->options.sendCookie) |
14702 | | ssl->options.dtlsStateful = 1; |
14703 | | } |
14704 | | #endif |
14705 | | |
14706 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
14707 | | if ((ssl->AcceptFilter != NULL) && |
14708 | | ((ssl->options.acceptState == TLS13_ACCEPT_BEGIN) |
14709 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14710 | | || (ssl->options.acceptState == TLS13_ACCEPT_BEGIN_RENEG) |
14711 | | #endif |
14712 | | )) |
14713 | | { |
14714 | | wolfSSL_netfilter_decision_t res; |
14715 | | if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) == |
14716 | | WOLFSSL_SUCCESS) && |
14717 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
14718 | | ssl->error = SOCKET_FILTERED_E; |
14719 | | WOLFSSL_ERROR(ssl->error); |
14720 | | return WOLFSSL_FATAL_ERROR; |
14721 | | } |
14722 | | } |
14723 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
14724 | | |
14725 | | #ifndef NO_CERTS |
14726 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14727 | | if (!havePSK) |
14728 | | #endif |
14729 | | { |
14730 | | #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \ |
14731 | | defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY) |
14732 | | if (ssl->ctx->certSetupCb != NULL) { |
14733 | | WOLFSSL_MSG("CertSetupCb set. server cert and " |
14734 | | "key not checked"); |
14735 | | } |
14736 | | else |
14737 | | #endif |
14738 | | { |
14739 | | if (!ssl->buffers.certificate || |
14740 | | !ssl->buffers.certificate->buffer) { |
14741 | | |
14742 | | WOLFSSL_MSG("accept error: server cert required"); |
14743 | | ssl->error = NO_PRIVATE_KEY; |
14744 | | WOLFSSL_ERROR(ssl->error); |
14745 | | return WOLFSSL_FATAL_ERROR; |
14746 | | } |
14747 | | |
14748 | | if (!ssl->buffers.key || !ssl->buffers.key->buffer) { |
14749 | | /* allow no private key if using existing key */ |
14750 | | #ifdef WOLF_PRIVATE_KEY_ID |
14751 | | if (ssl->devId != INVALID_DEVID |
14752 | | #ifdef HAVE_PK_CALLBACKS |
14753 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
14754 | | #endif |
14755 | | ) { |
14756 | | WOLFSSL_MSG("Allowing no server private key (external)"); |
14757 | | } |
14758 | | else |
14759 | | #endif |
14760 | | { |
14761 | | WOLFSSL_MSG("accept error: server key required"); |
14762 | | ssl->error = NO_PRIVATE_KEY; |
14763 | | WOLFSSL_ERROR(ssl->error); |
14764 | | return WOLFSSL_FATAL_ERROR; |
14765 | | } |
14766 | | } |
14767 | | } |
14768 | | } |
14769 | | #endif /* NO_CERTS */ |
14770 | | |
14771 | | if (ssl->buffers.outputBuffer.length > 0 |
14772 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14773 | | /* do not send buffered or advance state if last error was an |
14774 | | async pending operation */ |
14775 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
14776 | | #endif |
14777 | | ) { |
14778 | | |
14779 | | /* fragOffset is non-zero when sending fragments. On the last |
14780 | | * fragment, fragOffset is zero again, and the state can be |
14781 | | * advanced. */ |
14782 | | int advanceState = |
14783 | | (ssl->options.acceptState == TLS13_ACCEPT_CLIENT_HELLO_DONE || |
14784 | | ssl->options.acceptState == |
14785 | | TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE || |
14786 | | ssl->options.acceptState == TLS13_ACCEPT_SECOND_REPLY_DONE || |
14787 | | ssl->options.acceptState == TLS13_SERVER_HELLO_SENT || |
14788 | | ssl->options.acceptState == TLS13_ACCEPT_THIRD_REPLY_DONE || |
14789 | | ssl->options.acceptState == TLS13_SERVER_EXTENSIONS_SENT || |
14790 | | ssl->options.acceptState == TLS13_CERT_REQ_SENT || |
14791 | | ssl->options.acceptState == TLS13_CERT_SENT || |
14792 | | ssl->options.acceptState == TLS13_CERT_VERIFY_SENT || |
14793 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_SENT || |
14794 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_DONE); |
14795 | | |
14796 | | #ifdef WOLFSSL_DTLS13 |
14797 | | if (ssl->options.dtls) |
14798 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
14799 | | && !ssl->dtls13SendingAckOrRtx; |
14800 | | #endif /* WOLFSSL_DTLS13 */ |
14801 | | |
14802 | | ret = SendBuffered(ssl); |
14803 | | if (ret == 0) { |
14804 | | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
14805 | | if (advanceState) { |
14806 | | ssl->options.acceptState++; |
14807 | | WOLFSSL_MSG("accept state: " |
14808 | | "Advanced from last buffered fragment send"); |
14809 | | #ifdef WOLFSSL_ASYNC_IO |
14810 | | FreeAsyncCtx(ssl, 0); |
14811 | | #endif |
14812 | | } |
14813 | | } |
14814 | | else { |
14815 | | WOLFSSL_MSG("accept state: " |
14816 | | "Not advanced, more fragments to send"); |
14817 | | } |
14818 | | |
14819 | | #ifdef WOLFSSL_DTLS13 |
14820 | | if (ssl->options.dtls) |
14821 | | ssl->dtls13SendingAckOrRtx = 0; |
14822 | | #endif /* WOLFSSL_DTLS13 */ |
14823 | | |
14824 | | } |
14825 | | else { |
14826 | | ssl->error = ret; |
14827 | | WOLFSSL_ERROR(ssl->error); |
14828 | | return WOLFSSL_FATAL_ERROR; |
14829 | | } |
14830 | | } |
14831 | | |
14832 | | ret = RetrySendAlert(ssl); |
14833 | | if (ret != 0) { |
14834 | | ssl->error = ret; |
14835 | | WOLFSSL_ERROR(ssl->error); |
14836 | | return WOLFSSL_FATAL_ERROR; |
14837 | | } |
14838 | | #ifdef WOLFSSL_DTLS13 |
14839 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
14840 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
14841 | | WOLFSSL_ERROR(ssl->error); |
14842 | | return WOLFSSL_FATAL_ERROR; |
14843 | | } |
14844 | | |
14845 | | /* we sent all the fragments. Advance state. */ |
14846 | | ssl->options.acceptState++; |
14847 | | } |
14848 | | #endif /* WOLFSSL_DTLS13 */ |
14849 | | |
14850 | | switch (ssl->options.acceptState) { |
14851 | | |
14852 | | #ifdef HAVE_SECURE_RENEGOTIATION |
14853 | | case TLS13_ACCEPT_BEGIN_RENEG: |
14854 | | #endif |
14855 | | case TLS13_ACCEPT_BEGIN : |
14856 | | /* get client_hello */ |
14857 | | |
14858 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14859 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14860 | | WOLFSSL_ERROR(ssl->error); |
14861 | | return WOLFSSL_FATAL_ERROR; |
14862 | | } |
14863 | | |
14864 | | #ifdef WOLFSSL_DTLS13 |
14865 | | if (ssl->options.dtls) { |
14866 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14867 | | WOLFSSL_ERROR(ssl->error); |
14868 | | return WOLFSSL_FATAL_ERROR; |
14869 | | } |
14870 | | } |
14871 | | #endif /* WOLFSSL_DTLS13 */ |
14872 | | |
14873 | | } |
14874 | | |
14875 | | ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE; |
14876 | | WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE"); |
14877 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
14878 | | return wolfSSL_accept(ssl); |
14879 | | FALL_THROUGH; |
14880 | | |
14881 | | case TLS13_ACCEPT_CLIENT_HELLO_DONE : |
14882 | | if (ssl->options.serverState == |
14883 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14884 | | if ((ssl->error = SendTls13ServerHello(ssl, |
14885 | | hello_retry_request)) != 0) { |
14886 | | WOLFSSL_ERROR(ssl->error); |
14887 | | return WOLFSSL_FATAL_ERROR; |
14888 | | } |
14889 | | } |
14890 | | |
14891 | | ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE; |
14892 | | WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE"); |
14893 | | FALL_THROUGH; |
14894 | | |
14895 | | case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE : |
14896 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
14897 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14898 | | && ssl->options.serverState == |
14899 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14900 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14901 | | WOLFSSL_ERROR(ssl->error); |
14902 | | return WOLFSSL_FATAL_ERROR; |
14903 | | } |
14904 | | ssl->options.sentChangeCipher = 1; |
14905 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
14906 | | } |
14907 | | #endif |
14908 | | ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE; |
14909 | | WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE"); |
14910 | | FALL_THROUGH; |
14911 | | |
14912 | | case TLS13_ACCEPT_FIRST_REPLY_DONE : |
14913 | | if (ssl->options.serverState == |
14914 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14915 | | ssl->options.clientState = CLIENT_HELLO_RETRY; |
14916 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14917 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14918 | | WOLFSSL_ERROR(ssl->error); |
14919 | | return WOLFSSL_FATAL_ERROR; |
14920 | | } |
14921 | | |
14922 | | #ifdef WOLFSSL_DTLS13 |
14923 | | if (ssl->options.dtls) { |
14924 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14925 | | WOLFSSL_ERROR(ssl->error); |
14926 | | return WOLFSSL_FATAL_ERROR; |
14927 | | } |
14928 | | } |
14929 | | #endif /* WOLFSSL_DTLS13 */ |
14930 | | |
14931 | | } |
14932 | | } |
14933 | | |
14934 | | ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE; |
14935 | | WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE"); |
14936 | | FALL_THROUGH; |
14937 | | |
14938 | | case TLS13_ACCEPT_SECOND_REPLY_DONE : |
14939 | | if (ssl->options.returnOnGoodCh) { |
14940 | | /* Higher level in stack wants us to return. Simulate a |
14941 | | * WANT_WRITE to accomplish this. */ |
14942 | | ssl->error = WANT_WRITE; |
14943 | | return WOLFSSL_FATAL_ERROR; |
14944 | | } |
14945 | | |
14946 | | if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) { |
14947 | | WOLFSSL_ERROR(ssl->error); |
14948 | | return WOLFSSL_FATAL_ERROR; |
14949 | | } |
14950 | | ssl->options.acceptState = TLS13_SERVER_HELLO_SENT; |
14951 | | WOLFSSL_MSG("accept state SERVER_HELLO_SENT"); |
14952 | | FALL_THROUGH; |
14953 | | |
14954 | | case TLS13_SERVER_HELLO_SENT : |
14955 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14956 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
14957 | | && !ssl->options.sentChangeCipher && !ssl->options.dtls) { |
14958 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14959 | | WOLFSSL_ERROR(ssl->error); |
14960 | | return WOLFSSL_FATAL_ERROR; |
14961 | | } |
14962 | | ssl->options.sentChangeCipher = 1; |
14963 | | } |
14964 | | #endif |
14965 | | |
14966 | | ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE; |
14967 | | WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE"); |
14968 | | FALL_THROUGH; |
14969 | | |
14970 | | case TLS13_ACCEPT_THIRD_REPLY_DONE : |
14971 | | #ifdef HAVE_SUPPORTED_CURVES |
14972 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14973 | | if (!ssl->options.noPskDheKe) |
14974 | | #endif |
14975 | | { |
14976 | | ssl->error = TLSX_KeyShare_DeriveSecret(ssl); |
14977 | | if (ssl->error != 0) |
14978 | | return WOLFSSL_FATAL_ERROR; |
14979 | | } |
14980 | | #endif |
14981 | | |
14982 | | if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) { |
14983 | | WOLFSSL_ERROR(ssl->error); |
14984 | | return WOLFSSL_FATAL_ERROR; |
14985 | | } |
14986 | | ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT; |
14987 | | WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT"); |
14988 | | FALL_THROUGH; |
14989 | | |
14990 | | case TLS13_SERVER_EXTENSIONS_SENT : |
14991 | | #ifndef NO_CERTS |
14992 | | if (!ssl->options.resuming) { |
14993 | | if (ssl->options.verifyPeer |
14994 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14995 | | && !ssl->options.verifyPostHandshake |
14996 | | #endif |
14997 | | ) { |
14998 | | ssl->error = SendTls13CertificateRequest(ssl, NULL, 0); |
14999 | | if (ssl->error != 0) { |
15000 | | WOLFSSL_ERROR(ssl->error); |
15001 | | return WOLFSSL_FATAL_ERROR; |
15002 | | } |
15003 | | } |
15004 | | else { |
15005 | | /* SERVER: Peer auth good if not verifying client. */ |
15006 | | ssl->options.peerAuthGood = 1; |
15007 | | } |
15008 | | } |
15009 | | #endif |
15010 | | ssl->options.acceptState = TLS13_CERT_REQ_SENT; |
15011 | | WOLFSSL_MSG("accept state CERT_REQ_SENT"); |
15012 | | FALL_THROUGH; |
15013 | | |
15014 | | case TLS13_CERT_REQ_SENT : |
15015 | | #ifndef NO_CERTS |
15016 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15017 | | if ((ssl->error = SendTls13Certificate(ssl)) != 0) { |
15018 | | WOLFSSL_ERROR(ssl->error); |
15019 | | return WOLFSSL_FATAL_ERROR; |
15020 | | } |
15021 | | } |
15022 | | #endif |
15023 | | ssl->options.acceptState = TLS13_CERT_SENT; |
15024 | | WOLFSSL_MSG("accept state CERT_SENT"); |
15025 | | FALL_THROUGH; |
15026 | | |
15027 | | case TLS13_CERT_SENT : |
15028 | | #if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
15029 | | defined(HAVE_ED25519) || defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
15030 | | defined(HAVE_DILITHIUM)) |
15031 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15032 | | if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) { |
15033 | | WOLFSSL_ERROR(ssl->error); |
15034 | | return WOLFSSL_FATAL_ERROR; |
15035 | | } |
15036 | | } |
15037 | | #endif |
15038 | | ssl->options.acceptState = TLS13_CERT_VERIFY_SENT; |
15039 | | WOLFSSL_MSG("accept state CERT_VERIFY_SENT"); |
15040 | | FALL_THROUGH; |
15041 | | |
15042 | | case TLS13_CERT_VERIFY_SENT : |
15043 | | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
15044 | | WOLFSSL_ERROR(ssl->error); |
15045 | | return WOLFSSL_FATAL_ERROR; |
15046 | | } |
15047 | | |
15048 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT; |
15049 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT"); |
15050 | | FALL_THROUGH; |
15051 | | |
15052 | | case TLS13_ACCEPT_FINISHED_SENT: |
15053 | | #ifdef WOLFSSL_EARLY_DATA |
15054 | | if (ssl->earlyData != no_early_data && |
15055 | | ssl->options.handShakeState != SERVER_FINISHED_COMPLETE) { |
15056 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
15057 | | return WOLFSSL_SUCCESS; |
15058 | | } |
15059 | | #endif |
15060 | | #ifdef HAVE_SESSION_TICKET |
15061 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
15062 | | if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 && |
15063 | | ssl->ctx->ticketEncCb != NULL) { |
15064 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15065 | | WOLFSSL_ERROR(ssl->error); |
15066 | | return WOLFSSL_FATAL_ERROR; |
15067 | | } |
15068 | | ssl->options.ticketsSent = 1; |
15069 | | } |
15070 | | #endif |
15071 | | #endif /* HAVE_SESSION_TICKET */ |
15072 | | ssl->options.acceptState = TLS13_PRE_TICKET_SENT; |
15073 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
15074 | | FALL_THROUGH; |
15075 | | |
15076 | | case TLS13_PRE_TICKET_SENT : |
15077 | | while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
15078 | | if ( (ssl->error = ProcessReply(ssl)) < 0) { |
15079 | | WOLFSSL_ERROR(ssl->error); |
15080 | | return WOLFSSL_FATAL_ERROR; |
15081 | | } |
15082 | | |
15083 | | #ifdef WOLFSSL_DTLS13 |
15084 | | if (ssl->options.dtls) { |
15085 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15086 | | WOLFSSL_ERROR(ssl->error); |
15087 | | return WOLFSSL_FATAL_ERROR; |
15088 | | } |
15089 | | } |
15090 | | #endif /* WOLFSSL_DTLS13 */ |
15091 | | } |
15092 | | |
15093 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE; |
15094 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE"); |
15095 | | FALL_THROUGH; |
15096 | | |
15097 | | case TLS13_ACCEPT_FINISHED_DONE : |
15098 | | /* SERVER: When not resuming and verifying peer but no certificate |
15099 | | * received and not failing when not received then peer auth good. |
15100 | | */ |
15101 | | if (!ssl->options.resuming && ssl->options.verifyPeer && |
15102 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
15103 | | !ssl->options.verifyPostHandshake && |
15104 | | #endif |
15105 | | !ssl->options.havePeerCert && !ssl->options.failNoCert) { |
15106 | | ssl->options.peerAuthGood = 1; |
15107 | | } |
15108 | | /* SERVER: check peer authentication. */ |
15109 | | if (!ssl->options.peerAuthGood) { |
15110 | | WOLFSSL_MSG("Client authentication did not happen"); |
15111 | | return WOLFSSL_FATAL_ERROR; |
15112 | | } |
15113 | | #ifdef HAVE_SESSION_TICKET |
15114 | | while (ssl->options.ticketsSent < ssl->options.maxTicketTls13) { |
15115 | | if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb |
15116 | | != NULL) { |
15117 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15118 | | WOLFSSL_ERROR(ssl->error); |
15119 | | return WOLFSSL_FATAL_ERROR; |
15120 | | } |
15121 | | } |
15122 | | ssl->options.ticketsSent++; |
15123 | | |
15124 | | /* only one session ticket is sent on session resumption */ |
15125 | | if (ssl->options.resuming) { |
15126 | | break; |
15127 | | } |
15128 | | } |
15129 | | #endif /* HAVE_SESSION_TICKET */ |
15130 | | ssl->options.acceptState = TLS13_TICKET_SENT; |
15131 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
15132 | | FALL_THROUGH; |
15133 | | |
15134 | | case TLS13_TICKET_SENT : |
15135 | | #ifndef NO_HANDSHAKE_DONE_CB |
15136 | | if (ssl->hsDoneCb) { |
15137 | | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
15138 | | if (cbret < 0) { |
15139 | | ssl->error = cbret; |
15140 | | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
15141 | | return WOLFSSL_FATAL_ERROR; |
15142 | | } |
15143 | | } |
15144 | | #endif /* NO_HANDSHAKE_DONE_CB */ |
15145 | | |
15146 | | if (!ssl->options.keepResources) { |
15147 | | FreeHandshakeResources(ssl); |
15148 | | } |
15149 | | |
15150 | | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
15151 | | /* Free the remaining async context if not using it for crypto */ |
15152 | | FreeAsyncCtx(ssl, 1); |
15153 | | #endif |
15154 | | |
15155 | | ssl->error = 0; /* clear the error */ |
15156 | | |
15157 | | WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS); |
15158 | | return WOLFSSL_SUCCESS; |
15159 | | |
15160 | | default: |
15161 | | WOLFSSL_MSG("Unknown accept state ERROR"); |
15162 | | return WOLFSSL_FATAL_ERROR; |
15163 | | } |
15164 | | } |
15165 | | #endif |
15166 | | |
15167 | | #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET) |
15168 | | /* Server sends a session ticket to the peer. |
15169 | | * |
15170 | | * RFC 8446, section 4.6.1, para 1. |
15171 | | * |
15172 | | * ssl The SSL/TLS object. |
15173 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15174 | | * SIDE_ERROR when not a server, |
15175 | | * NOT_READY_ERROR when handshake not complete, |
15176 | | * WOLFSSL_FATAL_ERROR when creating or sending message fails, and |
15177 | | * WOLFSSL_SUCCESS on success. |
15178 | | */ |
15179 | | int wolfSSL_send_SessionTicket(WOLFSSL* ssl) |
15180 | | { |
15181 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15182 | | return BAD_FUNC_ARG; |
15183 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15184 | | return SIDE_ERROR; |
15185 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
15186 | | return NOT_READY_ERROR; |
15187 | | |
15188 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15189 | | WOLFSSL_ERROR(ssl->error); |
15190 | | return WOLFSSL_FATAL_ERROR; |
15191 | | } |
15192 | | ssl->options.ticketsSent++; |
15193 | | |
15194 | | return WOLFSSL_SUCCESS; |
15195 | | } |
15196 | | #endif |
15197 | | |
15198 | | #ifdef WOLFSSL_EARLY_DATA |
15199 | | /* Sets the maximum amount of early data that can be seen by server when using |
15200 | | * session tickets for resumption. |
15201 | | * A value of zero indicates no early data is to be sent by client using session |
15202 | | * tickets. |
15203 | | * |
15204 | | * ctx The SSL/TLS CTX object. |
15205 | | * sz Maximum size of the early data. |
15206 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
15207 | | * 0 on success. |
15208 | | */ |
15209 | | int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz) |
15210 | | { |
15211 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
15212 | | return BAD_FUNC_ARG; |
15213 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
15214 | | return SIDE_ERROR; |
15215 | | |
15216 | | ctx->maxEarlyDataSz = sz; |
15217 | | |
15218 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
15219 | | /* 1 on success in OpenSSL*/ |
15220 | | return WOLFSSL_SUCCESS; |
15221 | | #else |
15222 | | return 0; |
15223 | | #endif |
15224 | | } |
15225 | | |
15226 | | /* Sets the maximum amount of early data that a client or server would like |
15227 | | * to exchange. Servers will advertise this value in session tickets sent |
15228 | | * to a client. |
15229 | | * A value of zero indicates no early data will be sent by a client, or |
15230 | | * no early data is accepted by a server (and announced as such in send out |
15231 | | * session tickets). |
15232 | | * |
15233 | | * ssl The SSL/TLS object. |
15234 | | * sz Maximum size of the early data. |
15235 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15236 | | * and 0 on success. |
15237 | | */ |
15238 | | int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz) |
15239 | | { |
15240 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15241 | | return BAD_FUNC_ARG; |
15242 | | |
15243 | | ssl->options.maxEarlyDataSz = sz; |
15244 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
15245 | | /* 1 on success in OpenSSL*/ |
15246 | | return WOLFSSL_SUCCESS; |
15247 | | #else |
15248 | | return 0; |
15249 | | #endif |
15250 | | } |
15251 | | |
15252 | | /* Gets the maximum amount of early data that can be seen by server when using |
15253 | | * session tickets for resumption. |
15254 | | * A value of zero indicates no early data is to be sent by client using session |
15255 | | * tickets. |
15256 | | * |
15257 | | * ctx The SSL/TLS CTX object. |
15258 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
15259 | | * returns the maximum amount of early data to be set |
15260 | | */ |
15261 | | int wolfSSL_CTX_get_max_early_data(WOLFSSL_CTX* ctx) |
15262 | | { |
15263 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
15264 | | return BAD_FUNC_ARG; |
15265 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
15266 | | return SIDE_ERROR; |
15267 | | |
15268 | | return ctx->maxEarlyDataSz; |
15269 | | } |
15270 | | |
15271 | | /* Gets the maximum amount of early data that can be seen by server when using |
15272 | | * session tickets for resumption. |
15273 | | * A value of zero indicates no early data is to be sent by client using session |
15274 | | * tickets. |
15275 | | * |
15276 | | * ssl The SSL/TLS object. |
15277 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15278 | | * SIDE_ERROR when not a server and |
15279 | | * returns the maximum amount of early data to be set |
15280 | | */ |
15281 | | int wolfSSL_get_max_early_data(WOLFSSL* ssl) |
15282 | | { |
15283 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15284 | | return BAD_FUNC_ARG; |
15285 | | |
15286 | | return ssl->options.maxEarlyDataSz; |
15287 | | } |
15288 | | |
15289 | | /* Write early data to the server. |
15290 | | * |
15291 | | * ssl The SSL/TLS object. |
15292 | | * data Early data to write |
15293 | | * sz The size of the early data in bytes. |
15294 | | * outSz The number of early data bytes written. |
15295 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
15296 | | * or not using TLS v1.3. SIDE ERROR when not a server. BAD_STATE_E if invoked |
15297 | | * without a valid session or without a valid PSK CB. |
15298 | | * Otherwise the number of early data bytes written. |
15299 | | */ |
15300 | | int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz) |
15301 | | { |
15302 | | int ret = 0; |
15303 | | |
15304 | | WOLFSSL_ENTER("wolfSSL_write_early_data"); |
15305 | | |
15306 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
15307 | | return BAD_FUNC_ARG; |
15308 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
15309 | | return BAD_FUNC_ARG; |
15310 | | |
15311 | | #ifndef NO_WOLFSSL_CLIENT |
15312 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
15313 | | return SIDE_ERROR; |
15314 | | |
15315 | | /* Early data requires PSK or session resumption */ |
15316 | | if (!EarlyDataPossible(ssl)) { |
15317 | | return BAD_STATE_E; |
15318 | | } |
15319 | | |
15320 | | if (ssl->options.handShakeState == NULL_STATE) { |
15321 | | /* avoid re-setting ssl->earlyData if we re-enter the function because |
15322 | | * of WC_PENDING_E, WANT_WRITE or WANT_READ */ |
15323 | | if (ssl->error == 0) |
15324 | | ssl->earlyData = expecting_early_data; |
15325 | | ret = wolfSSL_connect_TLSv13(ssl); |
15326 | | if (ret != WOLFSSL_SUCCESS) |
15327 | | return WOLFSSL_FATAL_ERROR; |
15328 | | /* on client side, status is set to rejected */ |
15329 | | /* until sever accepts the early data extension. */ |
15330 | | ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED; |
15331 | | } |
15332 | | if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) { |
15333 | | #ifdef OPENSSL_EXTRA |
15334 | | /* when processed early data exceeds max size */ |
15335 | | if (ssl->session->maxEarlyDataSz > 0 && |
15336 | | (ssl->earlyDataSz + sz > ssl->session->maxEarlyDataSz)) { |
15337 | | ssl->error = TOO_MUCH_EARLY_DATA; |
15338 | | return WOLFSSL_FATAL_ERROR; |
15339 | | } |
15340 | | #endif |
15341 | | ret = SendData(ssl, data, sz); |
15342 | | if (ret > 0) { |
15343 | | *outSz = ret; |
15344 | | /* store amount of processed early data from client */ |
15345 | | ssl->earlyDataSz += ret; |
15346 | | } |
15347 | | } |
15348 | | #else |
15349 | | return SIDE_ERROR; |
15350 | | #endif |
15351 | | |
15352 | | WOLFSSL_LEAVE("wolfSSL_write_early_data", ret); |
15353 | | |
15354 | | if (ret < 0) |
15355 | | ret = WOLFSSL_FATAL_ERROR; |
15356 | | return ret; |
15357 | | } |
15358 | | |
15359 | | /* Read the any early data from the client. |
15360 | | * |
15361 | | * ssl The SSL/TLS object. |
15362 | | * data Buffer to put the early data into. |
15363 | | * sz The size of the buffer in bytes. |
15364 | | * outSz The number of early data bytes read. |
15365 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
15366 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
15367 | | * early data bytes read. |
15368 | | */ |
15369 | | int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz) |
15370 | | { |
15371 | | int ret = 0; |
15372 | | |
15373 | | WOLFSSL_ENTER("wolfSSL_read_early_data"); |
15374 | | |
15375 | | |
15376 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
15377 | | return BAD_FUNC_ARG; |
15378 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
15379 | | return BAD_FUNC_ARG; |
15380 | | |
15381 | | *outSz = 0; |
15382 | | #ifndef NO_WOLFSSL_SERVER |
15383 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15384 | | return SIDE_ERROR; |
15385 | | |
15386 | | if (ssl->options.handShakeState == NULL_STATE) { |
15387 | | /* the server flight can return WANT_WRITE and we re-enter here after |
15388 | | * setting ssl->earlyData = process_early_data, set earlyData to |
15389 | | * expecting_early_data just once */ |
15390 | | if (ssl->earlyData < expecting_early_data) |
15391 | | ssl->earlyData = expecting_early_data; |
15392 | | /* this used to be: ret = wolfSSL_accept_TLSv13(ssl); |
15393 | | * However, wolfSSL_accept_TLSv13() expects a certificate to |
15394 | | * be installed already, which is not the case in servers |
15395 | | * such as HAProxy. They do it after inspecting the ClientHello. |
15396 | | * The common wolfssl_accept() allows that. */ |
15397 | | ret = wolfSSL_accept(ssl); |
15398 | | if (ret <= 0) |
15399 | | return WOLFSSL_FATAL_ERROR; |
15400 | | } |
15401 | | if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) { |
15402 | | ssl->options.clientInEarlyData = 1; |
15403 | | ret = ReceiveData(ssl, (byte*)data, (size_t)sz, FALSE); |
15404 | | ssl->options.clientInEarlyData = 0; |
15405 | | if (ret > 0) |
15406 | | *outSz = ret; |
15407 | | if (ssl->error == WC_NO_ERR_TRACE(APP_DATA_READY)) { |
15408 | | ret = 0; |
15409 | | ssl->error = WOLFSSL_ERROR_NONE; |
15410 | | #ifdef WOLFSSL_DTLS13 |
15411 | | if (ssl->options.dtls) { |
15412 | | ret = Dtls13DoScheduledWork(ssl); |
15413 | | if (ret < 0) { |
15414 | | ssl->error = ret; |
15415 | | WOLFSSL_ERROR(ssl->error); |
15416 | | return WOLFSSL_FATAL_ERROR; |
15417 | | } |
15418 | | } |
15419 | | #endif /* WOLFSSL_DTLS13 */ |
15420 | | } |
15421 | | } |
15422 | | #ifdef WOLFSSL_DTLS13 |
15423 | | else if (ssl->buffers.outputBuffer.length > 0 && |
15424 | | ssl->options.dtls && ssl->dtls13SendingAckOrRtx) { |
15425 | | ret = SendBuffered(ssl); |
15426 | | if (ret == 0) { |
15427 | | ssl->dtls13SendingAckOrRtx = 0; |
15428 | | } |
15429 | | else { |
15430 | | ssl->error = ret; |
15431 | | WOLFSSL_ERROR(ssl->error); |
15432 | | return WOLFSSL_FATAL_ERROR; |
15433 | | } |
15434 | | } |
15435 | | #endif /* WOLFSSL_DTLS13 */ |
15436 | | else |
15437 | | ret = 0; |
15438 | | #else |
15439 | | return SIDE_ERROR; |
15440 | | #endif |
15441 | | |
15442 | | WOLFSSL_LEAVE("wolfSSL_read_early_data", ret); |
15443 | | |
15444 | | if (ret < 0) |
15445 | | ret = WOLFSSL_FATAL_ERROR; |
15446 | | return ret; |
15447 | | } |
15448 | | |
15449 | | /* Returns early data status |
15450 | | * |
15451 | | * ssl The SSL/TLS object. |
15452 | | * returns WOLFSSL_EARLY_DATA_ACCEPTED if the data was accepted |
15453 | | * WOLFSSL_EARLY_DATA_REJECTED if the data was rejected |
15454 | | * WOLFSSL_EARLY_DATA_NOT_SENT if no early data was sent |
15455 | | */ |
15456 | | int wolfSSL_get_early_data_status(const WOLFSSL* ssl) |
15457 | | { |
15458 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15459 | | return BAD_FUNC_ARG; |
15460 | | |
15461 | | return ssl->earlyDataStatus; |
15462 | | } |
15463 | | #endif |
15464 | | |
15465 | | #ifdef HAVE_SECRET_CALLBACK |
15466 | | int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx) |
15467 | | { |
15468 | | WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb"); |
15469 | | if (ssl == NULL) |
15470 | | return WOLFSSL_FATAL_ERROR; |
15471 | | |
15472 | | ssl->tls13SecretCb = cb; |
15473 | | ssl->tls13SecretCtx = ctx; |
15474 | | |
15475 | | return WOLFSSL_SUCCESS; |
15476 | | } |
15477 | | |
15478 | | #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE) |
15479 | | int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret, |
15480 | | int secretSz, void* ctx) |
15481 | | { |
15482 | | int i; |
15483 | | const char* str = NULL; |
15484 | | byte clientRandom[RAN_LEN]; |
15485 | | int clientRandomSz; |
15486 | | XFILE fp; |
15487 | | |
15488 | | (void) ctx; |
15489 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15490 | | fp = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "ab"); |
15491 | | if (fp == XBADFILE) { |
15492 | | return BAD_FUNC_ARG; |
15493 | | } |
15494 | | #else |
15495 | | fp = stderr; |
15496 | | #endif |
15497 | | |
15498 | | clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom, |
15499 | | sizeof(clientRandom)); |
15500 | | |
15501 | | if (clientRandomSz <= 0) { |
15502 | | printf("Error getting server random %d\n", clientRandomSz); |
15503 | | return BAD_FUNC_ARG; |
15504 | | } |
15505 | | |
15506 | | #if 0 |
15507 | | printf("TLS Server Secret CB: Rand %d, Secret %d\n", |
15508 | | serverRandomSz, secretSz); |
15509 | | #endif |
15510 | | |
15511 | | switch (id) { |
15512 | | case CLIENT_EARLY_TRAFFIC_SECRET: |
15513 | | str = "CLIENT_EARLY_TRAFFIC_SECRET"; break; |
15514 | | case EARLY_EXPORTER_SECRET: |
15515 | | str = "EARLY_EXPORTER_SECRET"; break; |
15516 | | case CLIENT_HANDSHAKE_TRAFFIC_SECRET: |
15517 | | str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break; |
15518 | | case SERVER_HANDSHAKE_TRAFFIC_SECRET: |
15519 | | str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break; |
15520 | | case CLIENT_TRAFFIC_SECRET: |
15521 | | str = "CLIENT_TRAFFIC_SECRET_0"; break; |
15522 | | case SERVER_TRAFFIC_SECRET: |
15523 | | str = "SERVER_TRAFFIC_SECRET_0"; break; |
15524 | | case EXPORTER_SECRET: |
15525 | | str = "EXPORTER_SECRET"; break; |
15526 | | default: |
15527 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15528 | | XFCLOSE(fp); |
15529 | | #endif |
15530 | | return BAD_FUNC_ARG; |
15531 | | break; |
15532 | | } |
15533 | | |
15534 | | fprintf(fp, "%s ", str); |
15535 | | for (i = 0; i < (int)clientRandomSz; i++) { |
15536 | | fprintf(fp, "%02x", clientRandom[i]); |
15537 | | } |
15538 | | fprintf(fp, " "); |
15539 | | for (i = 0; i < secretSz; i++) { |
15540 | | fprintf(fp, "%02x", secret[i]); |
15541 | | } |
15542 | | fprintf(fp, "\n"); |
15543 | | |
15544 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
15545 | | XFCLOSE(fp); |
15546 | | #endif |
15547 | | |
15548 | | return 0; |
15549 | | } |
15550 | | #endif |
15551 | | #endif |
15552 | | |
15553 | | #undef ERROR_OUT |
15554 | | |
15555 | | #endif /* !WOLFCRYPT_ONLY */ |
15556 | | |
15557 | | #endif /* !NO_TLS && WOLFSSL_TLS13 */ |