/src/wolfssl-sp-math-all/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. Violates the |
45 | | * RFC 8446 Section 4.6.1 ordering requirement; for |
46 | | * interop with peers that expect the early ticket. |
47 | | * WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID: Echo legacy_session_id default: off |
48 | | * in DTLS 1.3. Violates RFC 9147 Section 5 ("DTLS |
49 | | * servers MUST NOT echo the legacy_session_id |
50 | | * value from the client"). A server built with this |
51 | | * option echoes the session ID, which a compliant |
52 | | * client rejects, so enable it only where the peer |
53 | | * is wolfSSL <= 5.9.0 or shares this option. |
54 | | * WOLFSSL_NO_CLIENT_AUTH: Disable TLS 1.3 client authentication default: off |
55 | | * WOLFSSL_NO_CLIENT_CERT_ERROR: Require client certificate default: off |
56 | | * WOLFSSL_CERT_SETUP_CB: Certificate setup callback default: off |
57 | | * WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION: Allow bad legacy version default: off |
58 | | * |
59 | | * Security: |
60 | | * WOLFSSL_BLIND_PRIVATE_KEY: Blind private key during signing default: off |
61 | | * WOLFSSL_CHECK_SIG_FAULTS: Verify signature after ECC signing default: off |
62 | | * to detect fault injection attacks |
63 | | * WOLFSSL_CIPHER_TEXT_CHECK: Verify ciphertext integrity default: off |
64 | | * |
65 | | * TLS 1.3 PSK: |
66 | | * WOLFSSL_PSK_ONE_ID: Single PSK identity per connect default: off |
67 | | * WOLFSSL_PSK_MULTI_ID_PER_CS: Multiple PSK IDs per cipher suite default: off |
68 | | * WOLFSSL_PRIORITIZE_PSK: Prioritize PSK over ciphersuite order default: off |
69 | | * |
70 | | * TLS 1.3 Session Tickets: |
71 | | * WOLFSSL_TICKET_HAVE_ID: Session tickets include ID default: off |
72 | | * Forced on when WOLFSSL_EARLY_DATA is set. |
73 | | * WOLFSSL_TICKET_NONCE_MALLOC: Dynamically allocate ticket nonce default: off |
74 | | * |
75 | | * TLS 1.3 Key Exchange: |
76 | | * HAVE_KEYING_MATERIAL: Export keying material (RFC 8446 7.5) default: off |
77 | | * WOLFSSL_HAVE_TLS_UNIQUE: Enable tls-unique channel binding default: off |
78 | | * |
79 | | * TLS 1.3 Hash/Signature: |
80 | | * WOLFSSL_TLS13_SHA512: Allow SHA-512 in TLS 1.3 handshake default: off |
81 | | * (no ciphersuite requires it currently) |
82 | | * WOLFSSL_ERROR_CODE_OPENSSL: Use OpenSSL-compatible error codes default: off |
83 | | * WOLFSSL_SSLKEYLOGFILE_OUTPUT: Set key log output file path default: off |
84 | | * WOLFSSL_SSLKEYLOGFILE_USE_ENV: Use SSLKEYLOGFILE env var path default: off |
85 | | * WOLFSSL_RW_THREADED: Enable read/write threading support default: off |
86 | | * WOLFSSL_ASYNC_IO: Enable async I/O operations default: off |
87 | | * WOLFSSL_NONBLOCK_OCSP: Non-blocking OCSP processing default: off |
88 | | * WOLFSSL_TLS_OCSP_MULTI: Multiple OCSP responses default: off |
89 | | * WOLFSSL_WOLFSENTRY_HOOKS: wolfSentry integration hooks default: off |
90 | | */ |
91 | | |
92 | | #if !defined(NO_TLS) && defined(WOLFSSL_TLS13) |
93 | | |
94 | | /* 0-RTT anti-replay eviction needs the session cache. */ |
95 | | #if defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET) && \ |
96 | | defined(NO_SESSION_CACHE) && !defined(NO_WOLFSSL_SERVER) && \ |
97 | | !defined(WOLFSSL_EARLY_DATA_NO_ANTI_REPLAY) |
98 | | #error "WOLFSSL_EARLY_DATA with tickets requires !NO_SESSION_CACHE, or " \ |
99 | | "define WOLFSSL_EARLY_DATA_NO_ANTI_REPLAY to opt out." |
100 | | #endif |
101 | | |
102 | | #ifndef WOLFCRYPT_ONLY |
103 | | |
104 | | #ifdef HAVE_ERRNO_H |
105 | | #include <errno.h> |
106 | | #endif |
107 | | |
108 | | #if defined(__MACH__) || defined(__FreeBSD__) || \ |
109 | | defined(__INCLUDE_NUTTX_CONFIG_H) || defined(WOLFSSL_RIOT_OS) |
110 | | #include <sys/time.h> |
111 | | #endif /* __MACH__ || __FreeBSD__ || |
112 | | __INCLUDE_NUTTX_CONFIG_H || WOLFSSL_RIOT_OS */ |
113 | | |
114 | | |
115 | | #include <wolfssl/internal.h> |
116 | | #include <wolfssl/error-ssl.h> |
117 | | #include <wolfssl/wolfcrypt/asn.h> |
118 | | #include <wolfssl/wolfcrypt/dh.h> |
119 | | #include <wolfssl/wolfcrypt/kdf.h> |
120 | | #include <wolfssl/wolfcrypt/signature.h> |
121 | | #ifdef NO_INLINE |
122 | | #include <wolfssl/wolfcrypt/misc.h> |
123 | | #else |
124 | | #define WOLFSSL_MISC_INCLUDED |
125 | | #include <wolfcrypt/src/misc.c> |
126 | | #endif |
127 | | |
128 | | #ifdef __sun |
129 | | #include <sys/filio.h> |
130 | | #endif |
131 | | |
132 | | #ifndef TRUE |
133 | | #define TRUE 1 |
134 | | #endif |
135 | | #ifndef FALSE |
136 | | #define FALSE 0 |
137 | | #endif |
138 | | |
139 | | #ifndef HAVE_AEAD |
140 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
141 | | #error "The build option HAVE_AEAD is required for TLS 1.3" |
142 | | #else |
143 | | #pragma \ |
144 | | message("error: The build option HAVE_AEAD is required for TLS 1.3") |
145 | | #endif |
146 | | #endif |
147 | | |
148 | | #ifndef HAVE_HKDF |
149 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
150 | | #error "The build option HAVE_HKDF is required for TLS 1.3" |
151 | | #else |
152 | | #pragma message("error: The build option HAVE_HKDF is required for TLS 1.3") |
153 | | #endif |
154 | | #endif |
155 | | |
156 | | #ifndef HAVE_TLS_EXTENSIONS |
157 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
158 | | #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3" |
159 | | #else |
160 | | #pragma message("error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3") |
161 | | #endif |
162 | | #endif |
163 | | |
164 | | |
165 | | /* Set ret to error value and jump to label. |
166 | | * |
167 | | * err The error value to set. |
168 | | * eLabel The label to jump to. |
169 | | */ |
170 | 0 | #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } |
171 | | |
172 | | /* Size of the TLS v1.3 label use when deriving keys. */ |
173 | 0 | #define TLS13_PROTOCOL_LABEL_SZ 6 |
174 | | /* The protocol label for TLS v1.3. */ |
175 | | static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 "; |
176 | | |
177 | | #ifdef WOLFSSL_DTLS13 |
178 | | #define DTLS13_PROTOCOL_LABEL_SZ 6 |
179 | | static const byte dtls13ProtocolLabel[DTLS13_PROTOCOL_LABEL_SZ + 1] = "dtls13"; |
180 | | #endif /* WOLFSSL_DTLS13 */ |
181 | | |
182 | | #if defined(HAVE_ECH) |
183 | | #define ECH_ACCEPT_CONFIRMATION_LABEL_SZ 23 |
184 | | #define ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ 27 |
185 | | static const byte |
186 | | echAcceptConfirmationLabel[ECH_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
187 | | "ech accept confirmation"; |
188 | | static const byte |
189 | | echHrrAcceptConfirmationLabel[ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
190 | | "hrr ech accept confirmation"; |
191 | | #endif |
192 | | |
193 | | #ifndef NO_CERTS |
194 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
195 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
196 | | defined(WOLFSSL_HAVE_SLHDSA) |
197 | | |
198 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash); |
199 | | |
200 | | #endif |
201 | | #endif |
202 | | |
203 | | /* Expand data using HMAC, salt and label and info. |
204 | | * TLS v1.3 defines this function. Use callback if available. */ |
205 | | static int Tls13HKDFExpandLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
206 | | const byte* prk, word32 prkLen, |
207 | | const byte* protocol, word32 protocolLen, |
208 | | const byte* label, word32 labelLen, |
209 | | const byte* info, word32 infoLen, |
210 | | int digest) |
211 | 4.95k | { |
212 | 4.95k | int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
213 | | |
214 | | #if defined(HAVE_PK_CALLBACKS) |
215 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
216 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
217 | | protocol, protocolLen, |
218 | | label, labelLen, |
219 | | info, infoLen, digest, |
220 | | WOLFSSL_CLIENT_END /* ignored */); |
221 | | } |
222 | | |
223 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
224 | | return ret; |
225 | | #endif |
226 | 4.95k | (void)ssl; |
227 | 4.95k | PRIVATE_KEY_UNLOCK(); |
228 | 4.95k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
229 | 4.95k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
230 | 4.95k | protocol, protocolLen, |
231 | 4.95k | label, labelLen, |
232 | 4.95k | info, infoLen, digest, |
233 | 4.95k | ssl->heap, ssl->devId); |
234 | | #else |
235 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
236 | | protocol, protocolLen, |
237 | | label, labelLen, |
238 | | info, infoLen, digest); |
239 | | #endif |
240 | 4.95k | PRIVATE_KEY_LOCK(); |
241 | 4.95k | return ret; |
242 | 4.95k | } |
243 | | |
244 | | /* Same as above, but pass in the side we are expanding for: |
245 | | * side: either WOLFSSL_CLIENT_END or WOLFSSL_SERVER_END. |
246 | | */ |
247 | | static int Tls13HKDFExpandKeyLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
248 | | const byte* prk, word32 prkLen, |
249 | | const byte* protocol, word32 protocolLen, |
250 | | const byte* label, word32 labelLen, |
251 | | const byte* info, word32 infoLen, |
252 | | int digest, int side) |
253 | 8.88k | { |
254 | 8.88k | int ret; |
255 | | #if defined(HAVE_PK_CALLBACKS) |
256 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
257 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
258 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
259 | | protocol, protocolLen, |
260 | | label, labelLen, |
261 | | info, infoLen, |
262 | | digest, side); |
263 | | } |
264 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
265 | | return ret; |
266 | | #endif |
267 | | |
268 | 8.88k | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
269 | 8.88k | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
270 | 8.88k | protocol, protocolLen, |
271 | 8.88k | label, labelLen, |
272 | 8.88k | info, infoLen, digest, |
273 | 8.88k | ssl->heap, ssl->devId); |
274 | | |
275 | | #elif defined(HAVE_FIPS) && defined(wc_Tls13_HKDF_Expand_Label) |
276 | | ret = wc_Tls13_HKDF_Expand_Label_fips(okm, okmLen, prk, prkLen, |
277 | | protocol, protocolLen, |
278 | | label, labelLen, |
279 | | info, infoLen, digest); |
280 | | #else |
281 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
282 | | protocol, protocolLen, |
283 | | label, labelLen, |
284 | | info, infoLen, digest); |
285 | | #endif |
286 | 8.88k | (void)ssl; |
287 | 8.88k | (void)side; |
288 | 8.88k | return ret; |
289 | 8.88k | } |
290 | | |
291 | | |
292 | | /* Derive a key from a message. |
293 | | * |
294 | | * ssl The SSL/TLS object. |
295 | | * output The buffer to hold the derived key. |
296 | | * outputLen The length of the derived key. |
297 | | * secret The secret used to derive the key (HMAC secret). |
298 | | * label The label used to distinguish the context. |
299 | | * labelLen The length of the label. |
300 | | * msg The message data to derive key from. |
301 | | * msgLen The length of the message data to derive key from. |
302 | | * hashAlgo The hash algorithm to use in the HMAC. |
303 | | * returns 0 on success, otherwise failure. |
304 | | */ |
305 | | static int DeriveKeyMsg(WOLFSSL* ssl, byte* output, int outputLen, |
306 | | const byte* secret, const byte* label, word32 labelLen, |
307 | | byte* msg, int msgLen, int hashAlgo) |
308 | | { |
309 | | byte hash[WC_MAX_DIGEST_SIZE]; |
310 | | Digest digest; |
311 | | word32 hashSz = 0; |
312 | | const byte* protocol; |
313 | | word32 protocolLen; |
314 | | int digestAlg = -1; |
315 | | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
316 | | |
317 | | switch (hashAlgo) { |
318 | | #ifndef NO_SHA256 |
319 | | case sha256_mac: |
320 | | ret = wc_InitSha256_ex(&digest.sha256, ssl->heap, ssl->devId); |
321 | | if (ret == 0) { |
322 | | ret = wc_Sha256Update(&digest.sha256, msg, (word32)msgLen); |
323 | | if (ret == 0) |
324 | | ret = wc_Sha256Final(&digest.sha256, hash); |
325 | | wc_Sha256Free(&digest.sha256); |
326 | | } |
327 | | hashSz = WC_SHA256_DIGEST_SIZE; |
328 | | digestAlg = WC_SHA256; |
329 | | break; |
330 | | #endif |
331 | | #ifdef WOLFSSL_SHA384 |
332 | | case sha384_mac: |
333 | | ret = wc_InitSha384_ex(&digest.sha384, ssl->heap, ssl->devId); |
334 | | if (ret == 0) { |
335 | | ret = wc_Sha384Update(&digest.sha384, msg, (word32)msgLen); |
336 | | if (ret == 0) |
337 | | ret = wc_Sha384Final(&digest.sha384, hash); |
338 | | wc_Sha384Free(&digest.sha384); |
339 | | } |
340 | | hashSz = WC_SHA384_DIGEST_SIZE; |
341 | | digestAlg = WC_SHA384; |
342 | | break; |
343 | | #endif |
344 | | #ifdef WOLFSSL_TLS13_SHA512 |
345 | | case sha512_mac: |
346 | | ret = wc_InitSha512_ex(&digest.sha512, ssl->heap, ssl->devId); |
347 | | if (ret == 0) { |
348 | | ret = wc_Sha512Update(&digest.sha512, msg, (word32)msgLen); |
349 | | if (ret == 0) |
350 | | ret = wc_Sha512Final(&digest.sha512, hash); |
351 | | wc_Sha512Free(&digest.sha512); |
352 | | } |
353 | | hashSz = WC_SHA512_DIGEST_SIZE; |
354 | | digestAlg = WC_SHA512; |
355 | | break; |
356 | | #endif |
357 | | #ifdef WOLFSSL_SM3 |
358 | | case sm3_mac: |
359 | | ret = wc_InitSm3(&digest.sm3, ssl->heap, ssl->devId); |
360 | | if (ret == 0) { |
361 | | ret = wc_Sm3Update(&digest.sm3, msg, (word32)msgLen); |
362 | | if (ret == 0) |
363 | | ret = wc_Sm3Final(&digest.sm3, hash); |
364 | | wc_Sm3Free(&digest.sm3); |
365 | | } |
366 | | hashSz = WC_SM3_DIGEST_SIZE; |
367 | | digestAlg = WC_SM3; |
368 | | break; |
369 | | #endif |
370 | | default: |
371 | | ret = BAD_FUNC_ARG; |
372 | | digestAlg = -1; |
373 | | break; |
374 | | } |
375 | | |
376 | | if (digestAlg < 0) |
377 | | return HASH_TYPE_E; |
378 | | |
379 | | if (ret != 0) |
380 | | return ret; |
381 | | |
382 | | switch (ssl->version.minor) { |
383 | | case TLSv1_3_MINOR: |
384 | | protocol = tls13ProtocolLabel; |
385 | | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
386 | | break; |
387 | | #ifdef WOLFSSL_DTLS13 |
388 | | case DTLSv1_3_MINOR: |
389 | | if (!ssl->options.dtls) |
390 | | return VERSION_ERROR; |
391 | | |
392 | | protocol = dtls13ProtocolLabel; |
393 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
394 | | break; |
395 | | #endif /* WOLFSSL_DTLS13 */ |
396 | | default: |
397 | | return VERSION_ERROR; |
398 | | } |
399 | | if (outputLen == -1) |
400 | | outputLen = (int)hashSz; |
401 | | |
402 | | ret = Tls13HKDFExpandLabel(ssl, output, (word32)outputLen, secret, hashSz, |
403 | | protocol, protocolLen, label, labelLen, |
404 | | hash, hashSz, digestAlg); |
405 | | return ret; |
406 | | } |
407 | | |
408 | | /* Derive a key. |
409 | | * |
410 | | * ssl The SSL/TLS object. |
411 | | * output The buffer to hold the derived key. |
412 | | * outputLen The length of the derived key. |
413 | | * secret The secret used to derive the key (HMAC secret). |
414 | | * label The label used to distinguish the context. |
415 | | * labelLen The length of the label. |
416 | | * hashAlgo The hash algorithm to use in the HMAC. |
417 | | * includeMsgs Whether to include a hash of the handshake messages so far. |
418 | | * side The side that we are deriving the secret for. |
419 | | * returns 0 on success, otherwise failure. |
420 | | */ |
421 | | int Tls13DeriveKey(WOLFSSL* ssl, byte* output, int outputLen, |
422 | | const byte* secret, const byte* label, word32 labelLen, |
423 | | int hashAlgo, int includeMsgs, int side) |
424 | 0 | { |
425 | 0 | int ret = 0; |
426 | 0 | byte hash[WC_MAX_DIGEST_SIZE]; |
427 | 0 | word32 hashSz = 0; |
428 | 0 | word32 hashOutSz = 0; |
429 | 0 | const byte* protocol; |
430 | 0 | word32 protocolLen; |
431 | 0 | int digestAlg = 0; |
432 | | |
433 | |
|
434 | 0 | switch (hashAlgo) { |
435 | 0 | #ifndef NO_SHA256 |
436 | 0 | case sha256_mac: |
437 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
438 | 0 | digestAlg = WC_SHA256; |
439 | 0 | if (includeMsgs) |
440 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
441 | 0 | break; |
442 | 0 | #endif |
443 | | |
444 | 0 | #ifdef WOLFSSL_SHA384 |
445 | 0 | case sha384_mac: |
446 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
447 | 0 | digestAlg = WC_SHA384; |
448 | 0 | if (includeMsgs) |
449 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
450 | 0 | break; |
451 | 0 | #endif |
452 | | |
453 | | #ifdef WOLFSSL_TLS13_SHA512 |
454 | | case sha512_mac: |
455 | | hashSz = WC_SHA512_DIGEST_SIZE; |
456 | | digestAlg = WC_SHA512; |
457 | | if (includeMsgs) |
458 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
459 | | break; |
460 | | #endif |
461 | | |
462 | 0 | #ifdef WOLFSSL_SM3 |
463 | 0 | case sm3_mac: |
464 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
465 | 0 | digestAlg = WC_SM3; |
466 | 0 | if (includeMsgs) |
467 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
468 | 0 | break; |
469 | 0 | #endif |
470 | | |
471 | 0 | default: |
472 | 0 | ret = HASH_TYPE_E; |
473 | 0 | break; |
474 | 0 | } |
475 | 0 | if (ret != 0) |
476 | 0 | return ret; |
477 | | |
478 | 0 | protocol = tls13ProtocolLabel; |
479 | 0 | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
480 | |
|
481 | | #ifdef WOLFSSL_DTLS13 |
482 | | if (ssl->options.dtls) { |
483 | | protocol = dtls13ProtocolLabel; |
484 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
485 | | } |
486 | | #endif /* WOLFSSL_DTLS13 */ |
487 | |
|
488 | 0 | if (outputLen == -1) { |
489 | 0 | outputLen = (int)hashSz; |
490 | 0 | } |
491 | 0 | if (includeMsgs) { |
492 | 0 | hashOutSz = hashSz; |
493 | 0 | } |
494 | 0 | else { |
495 | | /* Appease static analyzers by making sure hash is cleared, since it is |
496 | | * passed into expand key label where older wc_Tls13_HKDF_Expand_Label |
497 | | * will unconditionally try to call a memcpy on it, however length will |
498 | | * always be 0. */ |
499 | 0 | XMEMSET(hash, 0, sizeof(hash)); |
500 | 0 | hashOutSz = 0; |
501 | 0 | } |
502 | |
|
503 | 0 | PRIVATE_KEY_UNLOCK(); |
504 | 0 | ret = Tls13HKDFExpandKeyLabel(ssl, output, (word32)outputLen, secret, hashSz, |
505 | 0 | protocol, protocolLen, label, labelLen, |
506 | 0 | hash, hashOutSz, digestAlg, side); |
507 | 0 | PRIVATE_KEY_LOCK(); |
508 | |
|
509 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
510 | | wc_MemZero_Add("TLS 1.3 derived key", output, outputLen); |
511 | | #endif |
512 | 0 | return ret; |
513 | 0 | } |
514 | | |
515 | | /* Convert TLS mac ID to a hash algorithm ID |
516 | | * |
517 | | * mac Mac ID to convert |
518 | | * returns hash ID on success, or the NONE type. |
519 | | */ |
520 | | static WC_INLINE int mac2hash(int mac) |
521 | | { |
522 | | int hash; |
523 | | switch (mac) { |
524 | | #ifndef NO_SHA256 |
525 | | case sha256_mac: |
526 | | hash = WC_SHA256; |
527 | | break; |
528 | | #endif |
529 | | |
530 | | #ifdef WOLFSSL_SHA384 |
531 | | case sha384_mac: |
532 | | hash = WC_SHA384; |
533 | | break; |
534 | | #endif |
535 | | |
536 | | #ifdef WOLFSSL_TLS13_SHA512 |
537 | | case sha512_mac: |
538 | | hash = WC_SHA512; |
539 | | break; |
540 | | #endif |
541 | | |
542 | | #ifdef WOLFSSL_SM3 |
543 | | case sm3_mac: |
544 | | hash = WC_SM3; |
545 | | break; |
546 | | #endif |
547 | | |
548 | | default: |
549 | | hash = WC_HASH_TYPE_NONE; |
550 | | } |
551 | | return hash; |
552 | | } |
553 | | |
554 | | #ifndef NO_PSK |
555 | | /* The length of the binder key label. */ |
556 | | #define BINDER_KEY_LABEL_SZ 10 |
557 | | /* The binder key label. */ |
558 | | static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] = |
559 | | "ext binder"; |
560 | | |
561 | | /* Derive the binder key. |
562 | | * |
563 | | * ssl The SSL/TLS object. |
564 | | * key The derived key. |
565 | | * returns 0 on success, otherwise failure. |
566 | | */ |
567 | | static int DeriveBinderKey(WOLFSSL* ssl, byte* key) |
568 | | { |
569 | | WOLFSSL_MSG("Derive Binder Key"); |
570 | | if (ssl == NULL || ssl->arrays == NULL) { |
571 | | return BAD_FUNC_ARG; |
572 | | } |
573 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
574 | | binderKeyLabel, BINDER_KEY_LABEL_SZ, |
575 | | NULL, 0, ssl->specs.mac_algorithm); |
576 | | } |
577 | | #endif /* !NO_PSK */ |
578 | | |
579 | | #if defined(HAVE_SESSION_TICKET) && \ |
580 | | (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) |
581 | | /* The length of the binder key resume label. */ |
582 | | #define BINDER_KEY_RESUME_LABEL_SZ 10 |
583 | | /* The binder key resume label. */ |
584 | | static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] = |
585 | | "res binder"; |
586 | | |
587 | | /* Derive the binder resumption key. |
588 | | * |
589 | | * ssl The SSL/TLS object. |
590 | | * key The derived key. |
591 | | * returns 0 on success, otherwise failure. |
592 | | */ |
593 | | static int DeriveBinderKeyResume(WOLFSSL* ssl, byte* key) |
594 | | { |
595 | | WOLFSSL_MSG("Derive Binder Key - Resumption"); |
596 | | if (ssl == NULL || ssl->arrays == NULL) { |
597 | | return BAD_FUNC_ARG; |
598 | | } |
599 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
600 | | binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ, |
601 | | NULL, 0, ssl->specs.mac_algorithm); |
602 | | } |
603 | | #endif /* HAVE_SESSION_TICKET && (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) */ |
604 | | |
605 | | #ifdef WOLFSSL_EARLY_DATA |
606 | | |
607 | | /* The length of the early traffic label. */ |
608 | | #define EARLY_TRAFFIC_LABEL_SZ 11 |
609 | | /* The early traffic label. */ |
610 | | static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] = |
611 | | "c e traffic"; |
612 | | |
613 | | /* Derive the early traffic key. |
614 | | * |
615 | | * ssl The SSL/TLS object. |
616 | | * key The derived key. |
617 | | * side The side that we are deriving the secret for. |
618 | | * returns 0 on success, otherwise failure. |
619 | | */ |
620 | | static int DeriveEarlyTrafficSecret(WOLFSSL* ssl, byte* key, int side) |
621 | | { |
622 | | int ret; |
623 | | WOLFSSL_MSG("Derive Early Traffic Secret"); |
624 | | if (ssl == NULL || ssl->arrays == NULL) { |
625 | | return BAD_FUNC_ARG; |
626 | | } |
627 | | |
628 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
629 | | /* If this is called from a sniffer session with keylog file support, |
630 | | * obtain the appropriate secret from the callback */ |
631 | | if (ssl->snifferSecretCb != NULL) { |
632 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
633 | | SNIFFER_SECRET_CLIENT_EARLY_TRAFFIC_SECRET, |
634 | | key); |
635 | | } |
636 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
637 | | |
638 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->secret, |
639 | | earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ, |
640 | | ssl->specs.mac_algorithm, 1, side); |
641 | | #ifdef HAVE_SECRET_CALLBACK |
642 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
643 | | ret = ssl->tls13SecretCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
644 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
645 | | if (ret != 0) { |
646 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
647 | | return TLS13_SECRET_CB_E; |
648 | | } |
649 | | } |
650 | | #ifdef OPENSSL_EXTRA |
651 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
652 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
653 | | ssl->specs.hash_size, NULL); |
654 | | if (ret != 0) { |
655 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
656 | | return TLS13_SECRET_CB_E; |
657 | | } |
658 | | } |
659 | | #endif /* OPENSSL_EXTRA */ |
660 | | #endif /* HAVE_SECRET_CALLBACK */ |
661 | | return ret; |
662 | | } |
663 | | |
664 | | #endif |
665 | | |
666 | | /* The length of the client handshake label. */ |
667 | 559 | #define CLIENT_HANDSHAKE_LABEL_SZ 12 |
668 | | /* The client handshake label. */ |
669 | | static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] = |
670 | | "c hs traffic"; |
671 | | |
672 | | /* Derive the client handshake key. |
673 | | * |
674 | | * ssl The SSL/TLS object. |
675 | | * key The derived key. |
676 | | * returns 0 on success, otherwise failure. |
677 | | */ |
678 | | static int DeriveClientHandshakeSecret(WOLFSSL* ssl, byte* key) |
679 | 559 | { |
680 | 559 | int ret; |
681 | 559 | WOLFSSL_MSG("Derive Client Handshake Secret"); |
682 | 559 | if (ssl == NULL || ssl->arrays == NULL) { |
683 | 0 | return BAD_FUNC_ARG; |
684 | 0 | } |
685 | | |
686 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
687 | | /* If this is called from a sniffer session with keylog file support, |
688 | | * obtain the appropriate secret from the callback */ |
689 | | if (ssl->snifferSecretCb != NULL) { |
690 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
691 | | SNIFFER_SECRET_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
692 | | key); |
693 | | } |
694 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
695 | | |
696 | 559 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
697 | 559 | clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ, |
698 | 559 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
699 | | #ifdef HAVE_SECRET_CALLBACK |
700 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
701 | | ret = ssl->tls13SecretCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
702 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
703 | | if (ret != 0) { |
704 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
705 | | return TLS13_SECRET_CB_E; |
706 | | } |
707 | | } |
708 | | #ifdef OPENSSL_EXTRA |
709 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
710 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
711 | | ssl->specs.hash_size, NULL); |
712 | | if (ret != 0) { |
713 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
714 | | return TLS13_SECRET_CB_E; |
715 | | } |
716 | | } |
717 | | #endif /* OPENSSL_EXTRA */ |
718 | | #endif /* HAVE_SECRET_CALLBACK */ |
719 | 559 | return ret; |
720 | 559 | } |
721 | | |
722 | | /* The length of the server handshake label. */ |
723 | 548 | #define SERVER_HANDSHAKE_LABEL_SZ 12 |
724 | | /* The server handshake label. */ |
725 | | static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] = |
726 | | "s hs traffic"; |
727 | | |
728 | | /* Derive the server handshake key. |
729 | | * |
730 | | * ssl The SSL/TLS object. |
731 | | * key The derived key. |
732 | | * returns 0 on success, otherwise failure. |
733 | | */ |
734 | | static int DeriveServerHandshakeSecret(WOLFSSL* ssl, byte* key) |
735 | 548 | { |
736 | 548 | int ret; |
737 | 548 | WOLFSSL_MSG("Derive Server Handshake Secret"); |
738 | 548 | if (ssl == NULL || ssl->arrays == NULL) { |
739 | 0 | return BAD_FUNC_ARG; |
740 | 0 | } |
741 | | |
742 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
743 | | /* If this is called from a sniffer session with keylog file support, |
744 | | * obtain the appropriate secret from the callback */ |
745 | | if (ssl->snifferSecretCb != NULL) { |
746 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
747 | | SNIFFER_SECRET_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
748 | | key); |
749 | | } |
750 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
751 | | |
752 | 548 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
753 | 548 | serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ, |
754 | 548 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
755 | | |
756 | | #ifdef HAVE_SECRET_CALLBACK |
757 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
758 | | ret = ssl->tls13SecretCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
759 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
760 | | if (ret != 0) { |
761 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
762 | | return TLS13_SECRET_CB_E; |
763 | | } |
764 | | } |
765 | | #ifdef OPENSSL_EXTRA |
766 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
767 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
768 | | ssl->specs.hash_size, NULL); |
769 | | if (ret != 0) { |
770 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
771 | | return TLS13_SECRET_CB_E; |
772 | | } |
773 | | } |
774 | | #endif /* OPENSSL_EXTRA */ |
775 | | #endif /* HAVE_SECRET_CALLBACK */ |
776 | 548 | return ret; |
777 | 548 | } |
778 | | |
779 | | /* The length of the client application traffic label. */ |
780 | 208 | #define CLIENT_APP_LABEL_SZ 12 |
781 | | /* The client application traffic label. */ |
782 | | static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] = |
783 | | "c ap traffic"; |
784 | | |
785 | | /* Derive the client application traffic key. |
786 | | * |
787 | | * ssl The SSL/TLS object. |
788 | | * key The derived key. |
789 | | * returns 0 on success, otherwise failure. |
790 | | */ |
791 | | static int DeriveClientTrafficSecret(WOLFSSL* ssl, byte* key) |
792 | 208 | { |
793 | 208 | int ret; |
794 | 208 | WOLFSSL_MSG("Derive Client Traffic Secret"); |
795 | 208 | if (ssl == NULL || ssl->arrays == NULL) { |
796 | 0 | return BAD_FUNC_ARG; |
797 | 0 | } |
798 | | |
799 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
800 | | /* If this is called from a sniffer session with keylog file support, |
801 | | * obtain the appropriate secret from the callback */ |
802 | | if (ssl->snifferSecretCb != NULL) { |
803 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
804 | | SNIFFER_SECRET_CLIENT_TRAFFIC_SECRET, |
805 | | key); |
806 | | } |
807 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
808 | | |
809 | 208 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
810 | 208 | clientAppLabel, CLIENT_APP_LABEL_SZ, |
811 | 208 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
812 | | |
813 | | #ifdef HAVE_SECRET_CALLBACK |
814 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
815 | | ret = ssl->tls13SecretCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
816 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
817 | | if (ret != 0) { |
818 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
819 | | return TLS13_SECRET_CB_E; |
820 | | } |
821 | | } |
822 | | #ifdef OPENSSL_EXTRA |
823 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
824 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
825 | | ssl->specs.hash_size, NULL); |
826 | | if (ret != 0) { |
827 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
828 | | return TLS13_SECRET_CB_E; |
829 | | } |
830 | | } |
831 | | #endif /* OPENSSL_EXTRA */ |
832 | | #endif /* HAVE_SECRET_CALLBACK */ |
833 | 208 | return ret; |
834 | 208 | } |
835 | | |
836 | | /* The length of the server application traffic label. */ |
837 | 213 | #define SERVER_APP_LABEL_SZ 12 |
838 | | /* The server application traffic label. */ |
839 | | static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] = |
840 | | "s ap traffic"; |
841 | | |
842 | | /* Derive the server application traffic key. |
843 | | * |
844 | | * ssl The SSL/TLS object. |
845 | | * key The derived key. |
846 | | * returns 0 on success, otherwise failure. |
847 | | */ |
848 | | static int DeriveServerTrafficSecret(WOLFSSL* ssl, byte* key) |
849 | 213 | { |
850 | 213 | int ret; |
851 | 213 | WOLFSSL_MSG("Derive Server Traffic Secret"); |
852 | 213 | if (ssl == NULL || ssl->arrays == NULL) { |
853 | 0 | return BAD_FUNC_ARG; |
854 | 0 | } |
855 | | |
856 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
857 | | /* If this is called from a sniffer session with keylog file support, |
858 | | * obtain the appropriate secret from the callback */ |
859 | | if (ssl->snifferSecretCb != NULL) { |
860 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
861 | | SNIFFER_SECRET_SERVER_TRAFFIC_SECRET, |
862 | | key); |
863 | | } |
864 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
865 | | |
866 | 213 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
867 | 213 | serverAppLabel, SERVER_APP_LABEL_SZ, |
868 | 213 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
869 | | |
870 | | #ifdef HAVE_SECRET_CALLBACK |
871 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
872 | | ret = ssl->tls13SecretCb(ssl, SERVER_TRAFFIC_SECRET, key, |
873 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
874 | | if (ret != 0) { |
875 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
876 | | return TLS13_SECRET_CB_E; |
877 | | } |
878 | | } |
879 | | #ifdef OPENSSL_EXTRA |
880 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
881 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_TRAFFIC_SECRET, key, |
882 | | ssl->specs.hash_size, NULL); |
883 | | if (ret != 0) { |
884 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
885 | | return TLS13_SECRET_CB_E; |
886 | | } |
887 | | } |
888 | | #endif /* OPENSSL_EXTRA */ |
889 | | #endif /* HAVE_SECRET_CALLBACK */ |
890 | 213 | return ret; |
891 | 213 | } |
892 | | |
893 | | #ifdef HAVE_KEYING_MATERIAL |
894 | | /* The length of the exporter master secret label. */ |
895 | | #define EXPORTER_MASTER_LABEL_SZ 10 |
896 | | /* The exporter master secret label. */ |
897 | | static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] = |
898 | | "exp master"; |
899 | | |
900 | | /* Derive the exporter secret. |
901 | | * |
902 | | * ssl The SSL/TLS object. |
903 | | * key The derived key. |
904 | | * returns 0 on success, otherwise failure. |
905 | | */ |
906 | | static int DeriveExporterSecret(WOLFSSL* ssl, byte* key) |
907 | | { |
908 | | int ret; |
909 | | WOLFSSL_ENTER("Derive Exporter Secret"); |
910 | | if (ssl == NULL || ssl->arrays == NULL) { |
911 | | return BAD_FUNC_ARG; |
912 | | } |
913 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
914 | | exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ, |
915 | | ssl->specs.mac_algorithm, 1, 0 /* Unused */); |
916 | | #ifdef HAVE_SECRET_CALLBACK |
917 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
918 | | ret = ssl->tls13SecretCb(ssl, EXPORTER_SECRET, key, |
919 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
920 | | if (ret != 0) { |
921 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
922 | | return TLS13_SECRET_CB_E; |
923 | | } |
924 | | } |
925 | | #ifdef OPENSSL_EXTRA |
926 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
927 | | ret = ssl->tls13KeyLogCb(ssl, EXPORTER_SECRET, key, |
928 | | ssl->specs.hash_size, NULL); |
929 | | if (ret != 0) { |
930 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
931 | | return TLS13_SECRET_CB_E; |
932 | | } |
933 | | } |
934 | | #endif /* OPENSSL_EXTRA */ |
935 | | #endif /* HAVE_SECRET_CALLBACK */ |
936 | | return ret; |
937 | | } |
938 | | |
939 | | /* The length of the exporter label. */ |
940 | | #define EXPORTER_LABEL_SZ 8 |
941 | | /* The exporter label. */ |
942 | | static const byte exporterLabel[EXPORTER_LABEL_SZ + 1] = |
943 | | "exporter"; |
944 | | /* Hash("") */ |
945 | | #ifndef NO_SHA256 |
946 | | static const byte emptySHA256Hash[] = { |
947 | | 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, |
948 | | 0x99, 0x6F, 0xB9, 0x24, 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, |
949 | | 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 |
950 | | }; |
951 | | #endif |
952 | | #ifdef WOLFSSL_SHA384 |
953 | | static const byte emptySHA384Hash[] = { |
954 | | 0x38, 0xB0, 0x60, 0xA7, 0x51, 0xAC, 0x96, 0x38, 0x4C, 0xD9, 0x32, 0x7E, |
955 | | 0xB1, 0xB1, 0xE3, 0x6A, 0x21, 0xFD, 0xB7, 0x11, 0x14, 0xBE, 0x07, 0x43, |
956 | | 0x4C, 0x0C, 0xC7, 0xBF, 0x63, 0xF6, 0xE1, 0xDA, 0x27, 0x4E, 0xDE, 0xBF, |
957 | | 0xE7, 0x6F, 0x65, 0xFB, 0xD5, 0x1A, 0xD2, 0xF1, 0x48, 0x98, 0xB9, 0x5B |
958 | | }; |
959 | | #endif |
960 | | #ifdef WOLFSSL_TLS13_SHA512 |
961 | | static const byte emptySHA512Hash[] = { |
962 | | 0xCF, 0x83, 0xE1, 0x35, 0x7E, 0xEF, 0xB8, 0xBD, 0xF1, 0x54, 0x28, 0x50, |
963 | | 0xD6, 0x6D, 0x80, 0x07, 0xD6, 0x20, 0xE4, 0x05, 0x0B, 0x57, 0x15, 0xDC, |
964 | | 0x83, 0xF4, 0xA9, 0x21, 0xD3, 0x6C, 0xE9, 0xCE, 0x47, 0xD0, 0xD1, 0x3C, |
965 | | 0x5D, 0x85, 0xF2, 0xB0, 0xFF, 0x83, 0x18, 0xD2, 0x87, 0x7E, 0xEC, 0x2F, |
966 | | 0x63, 0xB9, 0x31, 0xBD, 0x47, 0x41, 0x7A, 0x81, 0xA5, 0x38, 0x32, 0x7A, |
967 | | 0xF9, 0x27, 0xDA, 0x3E |
968 | | }; |
969 | | #endif |
970 | | #ifdef WOLFSSL_SM3 |
971 | | static const byte emptySM3Hash[] = { |
972 | | 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F, 0x8E, 0x61, 0x19, 0x48, |
973 | | 0x31, 0xE8, 0x1A, 0x8F, 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74, |
974 | | 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B |
975 | | }; |
976 | | #endif |
977 | | /** |
978 | | * Implement section 7.5 of RFC 8446 |
979 | | * @return 0 on success |
980 | | * <0 on failure |
981 | | */ |
982 | | int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen, |
983 | | const char *label, size_t labelLen, |
984 | | const unsigned char *context, size_t contextLen) |
985 | | { |
986 | | int ret; |
987 | | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
988 | | word32 hashLen = 0; |
989 | | byte hashOut[WC_MAX_DIGEST_SIZE]; |
990 | | const byte* emptyHash = NULL; |
991 | | byte firstExpand[WC_MAX_DIGEST_SIZE]; |
992 | | const byte* protocol = tls13ProtocolLabel; |
993 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
994 | | |
995 | | if (ssl->options.dtls && ssl->version.minor != DTLSv1_3_MINOR) |
996 | | return VERSION_ERROR; |
997 | | |
998 | | if (!ssl->options.dtls && ssl->version.minor != TLSv1_3_MINOR) |
999 | | return VERSION_ERROR; |
1000 | | |
1001 | | #ifdef WOLFSSL_DTLS13 |
1002 | | if (ssl->options.dtls) { |
1003 | | protocol = dtls13ProtocolLabel; |
1004 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1005 | | } |
1006 | | #endif /* WOLFSSL_DTLS13 */ |
1007 | | |
1008 | | /* Sanity check contextLen to prevent truncation when cast to word32. */ |
1009 | | if (contextLen > WOLFSSL_MAX_32BIT) |
1010 | | return BAD_FUNC_ARG; |
1011 | | /* RFC 8446 HkdfLabel encodes the output length as a uint16, so requested |
1012 | | * lengths > 65535 cannot be represented and must be rejected. */ |
1013 | | if (outLen > WOLFSSL_MAX_16BIT) |
1014 | | return BAD_FUNC_ARG; |
1015 | | /* RFC 8446 HkdfLabel encodes the label length in a single byte, so |
1016 | | * anything > 255 cannot be represented and must be rejected. |
1017 | | * The protocol length is included in the label. */ |
1018 | | if ((labelLen + protocolLen) > WOLFSSL_MAX_8BIT) |
1019 | | return BAD_FUNC_ARG; |
1020 | | |
1021 | | switch (ssl->specs.mac_algorithm) { |
1022 | | #ifndef NO_SHA256 |
1023 | | case sha256_mac: |
1024 | | hashType = WC_HASH_TYPE_SHA256; |
1025 | | hashLen = WC_SHA256_DIGEST_SIZE; |
1026 | | emptyHash = emptySHA256Hash; |
1027 | | break; |
1028 | | #endif |
1029 | | |
1030 | | #ifdef WOLFSSL_SHA384 |
1031 | | case sha384_mac: |
1032 | | hashType = WC_HASH_TYPE_SHA384; |
1033 | | hashLen = WC_SHA384_DIGEST_SIZE; |
1034 | | emptyHash = emptySHA384Hash; |
1035 | | break; |
1036 | | #endif |
1037 | | |
1038 | | #ifdef WOLFSSL_TLS13_SHA512 |
1039 | | case sha512_mac: |
1040 | | hashType = WC_HASH_TYPE_SHA512; |
1041 | | hashLen = WC_SHA512_DIGEST_SIZE; |
1042 | | emptyHash = emptySHA512Hash; |
1043 | | break; |
1044 | | #endif |
1045 | | |
1046 | | #ifdef WOLFSSL_SM3 |
1047 | | case sm3_mac: |
1048 | | hashType = WC_HASH_TYPE_SM3; |
1049 | | hashLen = WC_SM3_DIGEST_SIZE; |
1050 | | emptyHash = emptySM3Hash; |
1051 | | break; |
1052 | | #endif |
1053 | | |
1054 | | default: |
1055 | | return BAD_FUNC_ARG; |
1056 | | } |
1057 | | |
1058 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1059 | | /* Poison and register firstExpand before it is written so that any path |
1060 | | * below (all of which funnel through cleanup) is covered. */ |
1061 | | XMEMSET(firstExpand, 0xff, sizeof(firstExpand)); |
1062 | | wc_MemZero_Add("Tls13_Exporter firstExpand", firstExpand, |
1063 | | sizeof(firstExpand)); |
1064 | | #endif |
1065 | | |
1066 | | /* Derive-Secret(Secret, label, "") */ |
1067 | | ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen, |
1068 | | ssl->arrays->exporterSecret, hashLen, |
1069 | | protocol, protocolLen, (byte*)label, (word32)labelLen, |
1070 | | emptyHash, hashLen, (int)hashType); |
1071 | | if (ret != 0) |
1072 | | goto cleanup; |
1073 | | |
1074 | | /* Hash(context_value) */ |
1075 | | ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE); |
1076 | | if (ret != 0) |
1077 | | goto cleanup; |
1078 | | |
1079 | | ret = Tls13HKDFExpandLabel(ssl, out, (word32)outLen, firstExpand, hashLen, |
1080 | | protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ, |
1081 | | hashOut, hashLen, (int)hashType); |
1082 | | |
1083 | | cleanup: |
1084 | | /* firstExpand is the per-label Derive-Secret PRK and hashOut holds |
1085 | | * Hash(context_value); wipe both before the stack frame is reclaimed. */ |
1086 | | ForceZero(firstExpand, sizeof(firstExpand)); |
1087 | | ForceZero(hashOut, sizeof(hashOut)); |
1088 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1089 | | wc_MemZero_Check(firstExpand, sizeof(firstExpand)); |
1090 | | #endif |
1091 | | return ret; |
1092 | | } |
1093 | | #endif |
1094 | | |
1095 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1096 | | /* The length of the resumption master secret label. */ |
1097 | | #define RESUME_MASTER_LABEL_SZ 10 |
1098 | | /* The resumption master secret label. */ |
1099 | | static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] = |
1100 | | "res master"; |
1101 | | |
1102 | | /* Derive the resumption secret. |
1103 | | * |
1104 | | * ssl The SSL/TLS object. |
1105 | | * key The derived key. |
1106 | | * returns 0 on success, otherwise failure. |
1107 | | */ |
1108 | | int DeriveResumptionSecret(WOLFSSL* ssl, byte* key) |
1109 | | { |
1110 | | byte* masterSecret; |
1111 | | |
1112 | | WOLFSSL_MSG("Derive Resumption Secret"); |
1113 | | if (ssl == NULL) { |
1114 | | return BAD_FUNC_ARG; |
1115 | | } |
1116 | | if (ssl->arrays != NULL) { |
1117 | | masterSecret = ssl->arrays->masterSecret; |
1118 | | } |
1119 | | else { |
1120 | | masterSecret = ssl->session->masterSecret; |
1121 | | } |
1122 | | return Tls13DeriveKey(ssl, key, -1, masterSecret, resumeMasterLabel, |
1123 | | RESUME_MASTER_LABEL_SZ, ssl->specs.mac_algorithm, 1, |
1124 | | 0 /* Unused */); |
1125 | | } |
1126 | | #endif |
1127 | | |
1128 | | /* Length of the finished label. */ |
1129 | 4.62k | #define FINISHED_LABEL_SZ 8 |
1130 | | /* Finished label for generating finished key. */ |
1131 | | static const byte finishedLabel[FINISHED_LABEL_SZ+1] = "finished"; |
1132 | | /* Derive the finished secret. |
1133 | | * |
1134 | | * ssl The SSL/TLS object. |
1135 | | * key The key to use with the HMAC. |
1136 | | * secret The derived secret. |
1137 | | * side The side that we are deriving the secret for. |
1138 | | * returns 0 on success, otherwise failure. |
1139 | | */ |
1140 | | static int DeriveFinishedSecret(WOLFSSL* ssl, byte* key, byte* secret, |
1141 | | int side) |
1142 | 4.62k | { |
1143 | 4.62k | WOLFSSL_MSG("Derive Finished Secret"); |
1144 | 4.62k | return Tls13DeriveKey(ssl, secret, -1, key, finishedLabel, |
1145 | 4.62k | FINISHED_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1146 | 4.62k | side); |
1147 | 4.62k | } |
1148 | | |
1149 | | /* The length of the application traffic label. */ |
1150 | 0 | #define APP_TRAFFIC_LABEL_SZ 11 |
1151 | | /* The application traffic label. */ |
1152 | | static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] = |
1153 | | "traffic upd"; |
1154 | | |
1155 | | /* Update the traffic secret. |
1156 | | * |
1157 | | * ssl The SSL/TLS object. |
1158 | | * secret The previous secret and derived secret. |
1159 | | * side The side that we are deriving the secret for. |
1160 | | * returns 0 on success, otherwise failure. |
1161 | | */ |
1162 | | static int DeriveTrafficSecret(WOLFSSL* ssl, byte* secret, int side) |
1163 | 0 | { |
1164 | 0 | WOLFSSL_MSG("Derive New Application Traffic Secret"); |
1165 | 0 | return Tls13DeriveKey(ssl, secret, -1, secret, |
1166 | 0 | appTrafficLabel, APP_TRAFFIC_LABEL_SZ, |
1167 | 0 | ssl->specs.mac_algorithm, 0, side); |
1168 | 0 | } |
1169 | | |
1170 | | |
1171 | | static int Tls13_HKDF_Extract(WOLFSSL *ssl, byte* prk, const byte* salt, |
1172 | | int saltLen, byte* ikm, int ikmLen, int digest) |
1173 | | { |
1174 | | int ret; |
1175 | | #ifdef HAVE_PK_CALLBACKS |
1176 | | void *cb_ctx = ssl->HkdfExtractCtx; |
1177 | | CallbackHKDFExtract cb = ssl->ctx->HkdfExtractCb; |
1178 | | if (cb != NULL) { |
1179 | | ret = cb(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, cb_ctx); |
1180 | | } |
1181 | | else |
1182 | | #endif |
1183 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1184 | | if ((int)ssl->arrays->psk_keySz < 0) { |
1185 | | ret = PSK_KEY_ERROR; |
1186 | | } |
1187 | | else |
1188 | | #endif |
1189 | | { |
1190 | | #if !defined(HAVE_FIPS) || \ |
1191 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
1192 | | ret = wc_Tls13_HKDF_Extract_ex(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, |
1193 | | ssl->heap, ssl->devId); |
1194 | | #else |
1195 | | ret = wc_Tls13_HKDF_Extract(prk, salt, saltLen, ikm, ikmLen, digest); |
1196 | | (void)ssl; |
1197 | | #endif |
1198 | | } |
1199 | | return ret; |
1200 | | } |
1201 | | |
1202 | | /* Derive the early secret using HKDF Extract. |
1203 | | * |
1204 | | * ssl The SSL/TLS object. |
1205 | | */ |
1206 | | int DeriveEarlySecret(WOLFSSL* ssl) |
1207 | 5.12k | { |
1208 | 5.12k | int ret; |
1209 | | |
1210 | 5.12k | WOLFSSL_MSG("Derive Early Secret"); |
1211 | 5.12k | if (ssl == NULL || ssl->arrays == NULL) { |
1212 | 0 | return BAD_FUNC_ARG; |
1213 | 0 | } |
1214 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1215 | | ret = tsip_Tls13DeriveEarlySecret(ssl); |
1216 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1217 | | return ret; |
1218 | | #endif |
1219 | 5.12k | PRIVATE_KEY_UNLOCK(); |
1220 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1221 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1222 | | ssl->arrays->psk_key, (int)ssl->arrays->psk_keySz, |
1223 | | mac2hash(ssl->specs.mac_algorithm)); |
1224 | | #else |
1225 | 5.12k | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1226 | 5.12k | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1227 | 5.12k | #endif |
1228 | 5.12k | PRIVATE_KEY_LOCK(); |
1229 | 5.12k | return ret; |
1230 | 5.12k | } |
1231 | | |
1232 | | /* The length of the derived label. */ |
1233 | 580 | #define DERIVED_LABEL_SZ 7 |
1234 | | /* The derived label. */ |
1235 | | static const byte derivedLabel[DERIVED_LABEL_SZ + 1] = |
1236 | | "derived"; |
1237 | | |
1238 | | /* Derive the handshake secret using HKDF Extract. |
1239 | | * |
1240 | | * ssl The SSL/TLS object. |
1241 | | */ |
1242 | | int DeriveHandshakeSecret(WOLFSSL* ssl) |
1243 | 580 | { |
1244 | 580 | byte key[WC_MAX_DIGEST_SIZE]; |
1245 | 580 | int ret; |
1246 | 580 | WOLFSSL_MSG("Derive Handshake Secret"); |
1247 | 580 | if (ssl == NULL || ssl->arrays == NULL) { |
1248 | 0 | return BAD_FUNC_ARG; |
1249 | 0 | } |
1250 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1251 | | ret = tsip_Tls13DeriveHandshakeSecret(ssl); |
1252 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1253 | | return ret; |
1254 | | #endif |
1255 | | |
1256 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1257 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1258 | 580 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
1259 | 580 | derivedLabel, DERIVED_LABEL_SZ, |
1260 | 580 | NULL, 0, ssl->specs.mac_algorithm); |
1261 | 580 | if (ret == 0) { |
1262 | 566 | PRIVATE_KEY_UNLOCK(); |
1263 | 566 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->preMasterSecret, |
1264 | 566 | key, ssl->specs.hash_size, |
1265 | 566 | ssl->arrays->preMasterSecret, (int)ssl->arrays->preMasterSz, |
1266 | 566 | mac2hash(ssl->specs.mac_algorithm)); |
1267 | 566 | PRIVATE_KEY_LOCK(); |
1268 | 566 | } |
1269 | | |
1270 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1271 | | wc_MemZero_Add("DeriveHandshakeSecret key", key, WC_MAX_DIGEST_SIZE); |
1272 | | #endif |
1273 | 580 | ForceZero(key, sizeof(key)); |
1274 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1275 | | wc_MemZero_Check(key, sizeof(key)); |
1276 | | #endif |
1277 | 580 | return ret; |
1278 | 580 | } |
1279 | | |
1280 | | /* Derive the master secret using HKDF Extract. |
1281 | | * |
1282 | | * ssl The SSL/TLS object. |
1283 | | */ |
1284 | | int DeriveMasterSecret(WOLFSSL* ssl) |
1285 | | { |
1286 | | byte key[WC_MAX_DIGEST_SIZE]; |
1287 | | int ret; |
1288 | | WOLFSSL_MSG("Derive Master Secret"); |
1289 | | if (ssl == NULL || ssl->arrays == NULL) { |
1290 | | return BAD_FUNC_ARG; |
1291 | | } |
1292 | | |
1293 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1294 | | ret = tsip_Tls13DeriveMasterSecret(ssl); |
1295 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1296 | | return ret; |
1297 | | #endif |
1298 | | |
1299 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1300 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1301 | | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret, |
1302 | | derivedLabel, DERIVED_LABEL_SZ, |
1303 | | NULL, 0, ssl->specs.mac_algorithm); |
1304 | | if (ret == 0) { |
1305 | | PRIVATE_KEY_UNLOCK(); |
1306 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->masterSecret, |
1307 | | key, ssl->specs.hash_size, |
1308 | | ssl->arrays->masterSecret, 0, |
1309 | | mac2hash(ssl->specs.mac_algorithm)); |
1310 | | PRIVATE_KEY_LOCK(); |
1311 | | } |
1312 | | |
1313 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1314 | | wc_MemZero_Add("DeriveMasterSecret key", key, WC_MAX_DIGEST_SIZE); |
1315 | | #endif |
1316 | | ForceZero(key, sizeof(key)); |
1317 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1318 | | wc_MemZero_Check(key, sizeof(key)); |
1319 | | #endif |
1320 | | |
1321 | | #ifdef HAVE_KEYING_MATERIAL |
1322 | | if (ret != 0) |
1323 | | return ret; |
1324 | | /* Calculate exporter secret only when saving arrays */ |
1325 | | if (ssl->options.saveArrays) |
1326 | | ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret); |
1327 | | #endif |
1328 | | |
1329 | | return ret; |
1330 | | } |
1331 | | |
1332 | | #if defined(HAVE_SESSION_TICKET) |
1333 | | /* Length of the resumption label. */ |
1334 | | #define RESUMPTION_LABEL_SZ 10 |
1335 | | /* Resumption label for generating PSK associated with the ticket. */ |
1336 | | static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption"; |
1337 | | |
1338 | | /* Derive the PSK associated with the ticket. |
1339 | | * |
1340 | | * ssl The SSL/TLS object. |
1341 | | * nonce The nonce to derive with. |
1342 | | * nonceLen The length of the nonce to derive with. |
1343 | | * secret The derived secret. |
1344 | | * returns 0 on success, otherwise failure. |
1345 | | */ |
1346 | | int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret) |
1347 | | { |
1348 | | int digestAlg; |
1349 | | /* Only one protocol version defined at this time. */ |
1350 | | const byte* protocol = tls13ProtocolLabel; |
1351 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
1352 | | int ret; |
1353 | | |
1354 | | WOLFSSL_MSG("Derive Resumption PSK"); |
1355 | | |
1356 | | #ifdef WOLFSSL_DTLS13 |
1357 | | if (ssl->options.dtls) { |
1358 | | protocol = dtls13ProtocolLabel; |
1359 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1360 | | } |
1361 | | #endif /* WOLFSSL_DTLS13 */ |
1362 | | |
1363 | | switch (ssl->specs.mac_algorithm) { |
1364 | | #ifndef NO_SHA256 |
1365 | | case sha256_mac: |
1366 | | digestAlg = WC_SHA256; |
1367 | | break; |
1368 | | #endif |
1369 | | |
1370 | | #ifdef WOLFSSL_SHA384 |
1371 | | case sha384_mac: |
1372 | | digestAlg = WC_SHA384; |
1373 | | break; |
1374 | | #endif |
1375 | | |
1376 | | #ifdef WOLFSSL_TLS13_SHA512 |
1377 | | case sha512_mac: |
1378 | | digestAlg = WC_SHA512; |
1379 | | break; |
1380 | | #endif |
1381 | | |
1382 | | #ifdef WOLFSSL_SM3 |
1383 | | case sm3_mac: |
1384 | | digestAlg = WC_SM3; |
1385 | | break; |
1386 | | #endif |
1387 | | |
1388 | | default: |
1389 | | return BAD_FUNC_ARG; |
1390 | | } |
1391 | | |
1392 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
1393 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
1394 | | PRIVATE_KEY_UNLOCK(); |
1395 | | ret = wc_Tls13_HKDF_Expand_Label_Alloc(secret, ssl->specs.hash_size, |
1396 | | ssl->session->masterSecret, ssl->specs.hash_size, protocol, protocolLen, |
1397 | | resumptionLabel, RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg, |
1398 | | ssl->heap); |
1399 | | PRIVATE_KEY_LOCK(); |
1400 | | #else |
1401 | | ret = Tls13HKDFExpandLabel(ssl, secret, ssl->specs.hash_size, |
1402 | | ssl->session->masterSecret, ssl->specs.hash_size, |
1403 | | protocol, protocolLen, resumptionLabel, |
1404 | | RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg); |
1405 | | #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */ |
1406 | | return ret; |
1407 | | } |
1408 | | #endif /* HAVE_SESSION_TICKET */ |
1409 | | |
1410 | | |
1411 | | /* Calculate the HMAC of message data to this point. |
1412 | | * |
1413 | | * ssl The SSL/TLS object. |
1414 | | * key The HMAC key. |
1415 | | * hash The hash result - verify data. |
1416 | | * returns length of verify data generated. |
1417 | | */ |
1418 | | static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash, |
1419 | | word32* pHashSz) |
1420 | 0 | { |
1421 | 0 | WC_DECLARE_VAR(verifyHmac, Hmac, 1, 0); |
1422 | 0 | int hashType = WC_SHA256; |
1423 | 0 | int hashSz = WC_SHA256_DIGEST_SIZE; |
1424 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
1425 | |
|
1426 | 0 | if (ssl == NULL || key == NULL || hash == NULL) { |
1427 | 0 | return BAD_FUNC_ARG; |
1428 | 0 | } |
1429 | | |
1430 | | /* Get the hash of the previous handshake messages. */ |
1431 | 0 | switch (ssl->specs.mac_algorithm) { |
1432 | 0 | #ifndef NO_SHA256 |
1433 | 0 | case sha256_mac: |
1434 | 0 | hashType = WC_SHA256; |
1435 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
1436 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
1437 | 0 | break; |
1438 | 0 | #endif /* !NO_SHA256 */ |
1439 | 0 | #ifdef WOLFSSL_SHA384 |
1440 | 0 | case sha384_mac: |
1441 | 0 | hashType = WC_SHA384; |
1442 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
1443 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
1444 | 0 | break; |
1445 | 0 | #endif /* WOLFSSL_SHA384 */ |
1446 | | #ifdef WOLFSSL_TLS13_SHA512 |
1447 | | case sha512_mac: |
1448 | | hashType = WC_SHA512; |
1449 | | hashSz = WC_SHA512_DIGEST_SIZE; |
1450 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
1451 | | break; |
1452 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
1453 | 0 | #ifdef WOLFSSL_SM3 |
1454 | 0 | case sm3_mac: |
1455 | 0 | hashType = WC_SM3; |
1456 | 0 | hashSz = WC_SM3_DIGEST_SIZE; |
1457 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
1458 | 0 | break; |
1459 | 0 | #endif /* WOLFSSL_SM3 */ |
1460 | 0 | default: |
1461 | 0 | ret = BAD_FUNC_ARG; |
1462 | 0 | break; |
1463 | 0 | } |
1464 | 0 | if (ret != 0) |
1465 | 0 | return ret; |
1466 | | |
1467 | | #ifdef WOLFSSL_DEBUG_TLS |
1468 | | WOLFSSL_MSG(" Key"); |
1469 | | WOLFSSL_BUFFER(key, ssl->specs.hash_size); |
1470 | | WOLFSSL_MSG(" Msg Hash"); |
1471 | | WOLFSSL_BUFFER(hash, hashSz); |
1472 | | #endif |
1473 | | |
1474 | 0 | WC_ALLOC_VAR_EX(verifyHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
1475 | 0 | return MEMORY_E); |
1476 | | |
1477 | | /* Calculate the verify data. */ |
1478 | 0 | ret = wc_HmacInit(verifyHmac, ssl->heap, ssl->devId); |
1479 | 0 | if (ret == 0) { |
1480 | 0 | ret = wc_HmacSetKey(verifyHmac, hashType, key, ssl->specs.hash_size); |
1481 | 0 | if (ret == 0) |
1482 | 0 | ret = wc_HmacUpdate(verifyHmac, hash, (word32)hashSz); |
1483 | 0 | if (ret == 0) |
1484 | 0 | ret = wc_HmacFinal(verifyHmac, hash); |
1485 | 0 | wc_HmacFree(verifyHmac); |
1486 | 0 | } |
1487 | |
|
1488 | 0 | WC_FREE_VAR_EX(verifyHmac, NULL, DYNAMIC_TYPE_HMAC); |
1489 | |
|
1490 | | #ifdef WOLFSSL_DEBUG_TLS |
1491 | | WOLFSSL_MSG(" Hash"); |
1492 | | WOLFSSL_BUFFER(hash, hashSz); |
1493 | | #endif |
1494 | |
|
1495 | 0 | if (pHashSz) |
1496 | 0 | *pHashSz = (word32)hashSz; |
1497 | |
|
1498 | 0 | return ret; |
1499 | 0 | } |
1500 | | |
1501 | | /* The length of the label to use when deriving keys. */ |
1502 | 0 | #define WRITE_KEY_LABEL_SZ 3 |
1503 | | /* The length of the label to use when deriving IVs. */ |
1504 | 0 | #define WRITE_IV_LABEL_SZ 2 |
1505 | | /* The label to use when deriving keys. */ |
1506 | | static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key"; |
1507 | | /* The label to use when deriving IVs. */ |
1508 | | static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv"; |
1509 | | |
1510 | | /* Derive the keys and IVs for TLS v1.3. |
1511 | | * |
1512 | | * ssl The SSL/TLS object. |
1513 | | * secret early_data_key when deriving the key and IV for encrypting early |
1514 | | * data application data and end_of_early_data messages. |
1515 | | * handshake_key when deriving keys and IVs for encrypting handshake |
1516 | | * messages. |
1517 | | * traffic_key when deriving first keys and IVs for encrypting |
1518 | | * traffic messages. |
1519 | | * update_traffic_key when deriving next keys and IVs for encrypting |
1520 | | * traffic messages. |
1521 | | * no_key when deriving keys and IVs from existing secrets without |
1522 | | * re-deriving the secrets. Used during early data transitions. |
1523 | | * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived. |
1524 | | * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived. |
1525 | | * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived. |
1526 | | * store 1 indicates to derive the keys and IVs from derived secret and |
1527 | | * store ready for provisioning. |
1528 | | * returns 0 on success, otherwise failure. |
1529 | | */ |
1530 | | int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store) |
1531 | 0 | { |
1532 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1533 | 0 | int i = 0; |
1534 | 0 | WC_DECLARE_VAR(key_dig, byte, MAX_PRF_DIG, 0); |
1535 | 0 | int provision; |
1536 | |
|
1537 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1538 | | ret = tsip_Tls13DeriveKeys(ssl, secret, side); |
1539 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1540 | | return ret; |
1541 | | } |
1542 | | ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1543 | | #endif |
1544 | |
|
1545 | 0 | WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap, |
1546 | 0 | DYNAMIC_TYPE_DIGEST, return MEMORY_E); |
1547 | | |
1548 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1549 | | XMEMSET(key_dig, 0xff, MAX_PRF_DIG); |
1550 | | wc_MemZero_Add("DeriveTls13Keys key_dig", key_dig, MAX_PRF_DIG); |
1551 | | #endif |
1552 | | |
1553 | 0 | if (side == ENCRYPT_AND_DECRYPT_SIDE) { |
1554 | 0 | provision = PROVISION_CLIENT_SERVER; |
1555 | 0 | } |
1556 | 0 | else { |
1557 | 0 | provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^ |
1558 | 0 | (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT : |
1559 | 0 | PROVISION_SERVER; |
1560 | 0 | } |
1561 | | |
1562 | | /* Derive the appropriate secret to use in the HKDF. */ |
1563 | 0 | switch (secret) { |
1564 | | #ifdef WOLFSSL_EARLY_DATA |
1565 | | case early_data_key: |
1566 | | ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret, |
1567 | | WOLFSSL_CLIENT_END); |
1568 | | if (ret != 0) |
1569 | | goto end; |
1570 | | break; |
1571 | | #endif |
1572 | | |
1573 | 0 | case handshake_key: |
1574 | 0 | if (provision & PROVISION_CLIENT) { |
1575 | 0 | ret = DeriveClientHandshakeSecret(ssl, |
1576 | 0 | ssl->clientSecret); |
1577 | 0 | if (ret != 0) |
1578 | 0 | goto end; |
1579 | 0 | } |
1580 | 0 | if (provision & PROVISION_SERVER) { |
1581 | 0 | ret = DeriveServerHandshakeSecret(ssl, |
1582 | 0 | ssl->serverSecret); |
1583 | 0 | if (ret != 0) |
1584 | 0 | goto end; |
1585 | 0 | } |
1586 | 0 | break; |
1587 | | |
1588 | 0 | case traffic_key: |
1589 | 0 | if (provision & PROVISION_CLIENT) { |
1590 | 0 | ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret); |
1591 | 0 | if (ret != 0) |
1592 | 0 | goto end; |
1593 | 0 | } |
1594 | 0 | if (provision & PROVISION_SERVER) { |
1595 | 0 | ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret); |
1596 | 0 | if (ret != 0) |
1597 | 0 | goto end; |
1598 | 0 | } |
1599 | 0 | break; |
1600 | | |
1601 | 0 | case update_traffic_key: |
1602 | 0 | if (provision & PROVISION_CLIENT) { |
1603 | 0 | ret = DeriveTrafficSecret(ssl, ssl->clientSecret, |
1604 | 0 | WOLFSSL_CLIENT_END); |
1605 | 0 | if (ret != 0) |
1606 | 0 | goto end; |
1607 | 0 | } |
1608 | 0 | if (provision & PROVISION_SERVER) { |
1609 | 0 | ret = DeriveTrafficSecret(ssl, ssl->serverSecret, |
1610 | 0 | WOLFSSL_SERVER_END); |
1611 | 0 | if (ret != 0) |
1612 | 0 | goto end; |
1613 | 0 | } |
1614 | 0 | break; |
1615 | | |
1616 | 0 | case no_key: |
1617 | | /* Called with early data to derive keys from existing secrets |
1618 | | * without re-deriving the secrets themselves. */ |
1619 | 0 | ret = 0; |
1620 | 0 | break; |
1621 | | |
1622 | 0 | default: |
1623 | 0 | ret = BAD_FUNC_ARG; |
1624 | 0 | break; |
1625 | 0 | } |
1626 | | |
1627 | | #ifdef WOLFSSL_QUIC |
1628 | | if (WOLFSSL_IS_QUIC(ssl)) { |
1629 | | ret = wolfSSL_quic_forward_secrets(ssl, secret, side); |
1630 | | if (ret != 0) |
1631 | | goto end; |
1632 | | } |
1633 | | #endif /* WOLFSSL_QUIC */ |
1634 | | |
1635 | 0 | if (!store) |
1636 | 0 | goto end; |
1637 | | |
1638 | | /* Key data = client key | server key | client IV | server IV */ |
1639 | | |
1640 | 0 | if (provision & PROVISION_CLIENT) { |
1641 | | /* Derive the client key. */ |
1642 | 0 | WOLFSSL_MSG("Derive Client Key"); |
1643 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1644 | 0 | ssl->clientSecret, writeKeyLabel, |
1645 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1646 | 0 | WOLFSSL_CLIENT_END); |
1647 | 0 | if (ret != 0) |
1648 | 0 | goto end; |
1649 | 0 | i += ssl->specs.key_size; |
1650 | 0 | } |
1651 | | |
1652 | 0 | if (provision & PROVISION_SERVER) { |
1653 | | /* Derive the server key. */ |
1654 | 0 | WOLFSSL_MSG("Derive Server Key"); |
1655 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1656 | 0 | ssl->serverSecret, writeKeyLabel, |
1657 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1658 | 0 | WOLFSSL_SERVER_END); |
1659 | 0 | if (ret != 0) |
1660 | 0 | goto end; |
1661 | 0 | i += ssl->specs.key_size; |
1662 | 0 | } |
1663 | | |
1664 | 0 | if (provision & PROVISION_CLIENT) { |
1665 | | /* Derive the client IV. */ |
1666 | 0 | WOLFSSL_MSG("Derive Client IV"); |
1667 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1668 | 0 | ssl->clientSecret, writeIVLabel, |
1669 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1670 | 0 | WOLFSSL_CLIENT_END); |
1671 | 0 | if (ret != 0) |
1672 | 0 | goto end; |
1673 | 0 | i += ssl->specs.iv_size; |
1674 | 0 | } |
1675 | | |
1676 | 0 | if (provision & PROVISION_SERVER) { |
1677 | | /* Derive the server IV. */ |
1678 | 0 | WOLFSSL_MSG("Derive Server IV"); |
1679 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1680 | 0 | ssl->serverSecret, writeIVLabel, |
1681 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1682 | 0 | WOLFSSL_SERVER_END); |
1683 | 0 | if (ret != 0) |
1684 | 0 | goto end; |
1685 | | /* Server IV is the last key material written to key_dig, so i is not |
1686 | | * advanced here; the whole buffer is zeroed at end regardless. */ |
1687 | 0 | } |
1688 | | |
1689 | | /* Store keys and IVs but don't activate them. */ |
1690 | 0 | ret = StoreKeys(ssl, key_dig, provision); |
1691 | |
|
1692 | | #ifdef WOLFSSL_DTLS13 |
1693 | | if (ret != 0) |
1694 | | goto end; |
1695 | | |
1696 | | if (ssl->options.dtls) { |
1697 | | w64wrapper epochNumber; |
1698 | | ret = Dtls13DeriveSnKeys(ssl, provision); |
1699 | | if (ret != 0) |
1700 | | goto end; |
1701 | | |
1702 | | switch (secret) { |
1703 | | case early_data_key: |
1704 | | epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA); |
1705 | | break; |
1706 | | case handshake_key: |
1707 | | epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
1708 | | break; |
1709 | | case traffic_key: |
1710 | | case no_key: |
1711 | | epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
1712 | | break; |
1713 | | case update_traffic_key: |
1714 | | if (side == ENCRYPT_SIDE_ONLY) { |
1715 | | epochNumber = ssl->dtls13Epoch; |
1716 | | } |
1717 | | else if (side == DECRYPT_SIDE_ONLY) { |
1718 | | epochNumber = ssl->dtls13PeerEpoch; |
1719 | | } |
1720 | | else { |
1721 | | ret = BAD_STATE_E; |
1722 | | goto end; |
1723 | | } |
1724 | | w64Increment(&epochNumber); |
1725 | | break; |
1726 | | default: |
1727 | | ret = BAD_STATE_E; |
1728 | | goto end; |
1729 | | } |
1730 | | ret = Dtls13NewEpoch(ssl, epochNumber, side); |
1731 | | if (ret != 0) |
1732 | | goto end; |
1733 | | } |
1734 | | |
1735 | | #endif /* WOLFSSL_DTLS13 */ |
1736 | |
|
1737 | 0 | end: |
1738 | | /* Zero the whole key_dig buffer (not just the i bytes derived) so no |
1739 | | * key-schedule material can linger in the unused tail. */ |
1740 | 0 | ForceZero(key_dig, MAX_PRF_DIG); |
1741 | 0 | #ifdef WOLFSSL_SMALL_STACK |
1742 | 0 | XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1743 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1744 | | wc_MemZero_Check(key_dig, MAX_PRF_DIG); |
1745 | | #endif |
1746 | |
|
1747 | 0 | if (ret != 0) { |
1748 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
1749 | 0 | } |
1750 | |
|
1751 | 0 | return ret; |
1752 | 0 | } |
1753 | | |
1754 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) || defined(WOLFSSL_DTLS13) |
1755 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
1756 | | #ifndef NO_ASN_TIME |
1757 | | #if defined(USER_TICKS) |
1758 | | #if 0 |
1759 | | word32 TimeNowInMilliseconds(void) |
1760 | | { |
1761 | | /* |
1762 | | write your own clock tick function if don't want gettimeofday() |
1763 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1764 | | */ |
1765 | | } |
1766 | | #endif |
1767 | | |
1768 | | #elif defined(TIME_OVERRIDES) |
1769 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1770 | | word32 TimeNowInMilliseconds(void) |
1771 | | { |
1772 | | return (word32) wc_Time(0) * 1000; |
1773 | | } |
1774 | | #else |
1775 | | #ifndef HAVE_TIME_T_TYPE |
1776 | | typedef long time_t; |
1777 | | #endif |
1778 | | extern time_t XTIME(time_t * timer); |
1779 | | |
1780 | | /* The time in milliseconds. |
1781 | | * Used for tickets to represent difference between when first seen and when |
1782 | | * sending. |
1783 | | * |
1784 | | * returns the time in milliseconds as a 32-bit value. |
1785 | | */ |
1786 | | word32 TimeNowInMilliseconds(void) |
1787 | | { |
1788 | | return (word32) XTIME(0) * 1000; |
1789 | | } |
1790 | | #endif |
1791 | | |
1792 | | #elif defined(XTIME_MS) |
1793 | | word32 TimeNowInMilliseconds(void) |
1794 | | { |
1795 | | return (word32)XTIME_MS(0); |
1796 | | } |
1797 | | |
1798 | | #elif defined(USE_WINDOWS_API) |
1799 | | /* The time in milliseconds. |
1800 | | * Used for tickets to represent difference between when first seen and when |
1801 | | * sending. |
1802 | | * |
1803 | | * returns the time in milliseconds as a 32-bit value. |
1804 | | */ |
1805 | | word32 TimeNowInMilliseconds(void) |
1806 | | { |
1807 | | static int init = 0; |
1808 | | static LARGE_INTEGER freq; |
1809 | | LARGE_INTEGER count; |
1810 | | |
1811 | | if (!init) { |
1812 | | QueryPerformanceFrequency(&freq); |
1813 | | init = 1; |
1814 | | } |
1815 | | |
1816 | | QueryPerformanceCounter(&count); |
1817 | | |
1818 | | return (word32)(count.QuadPart / (freq.QuadPart / 1000)); |
1819 | | } |
1820 | | |
1821 | | #elif defined(HAVE_RTP_SYS) |
1822 | | #include "rtptime.h" |
1823 | | |
1824 | | /* The time in milliseconds. |
1825 | | * Used for tickets to represent difference between when first seen and when |
1826 | | * sending. |
1827 | | * |
1828 | | * returns the time in milliseconds as a 32-bit value. |
1829 | | */ |
1830 | | word32 TimeNowInMilliseconds(void) |
1831 | | { |
1832 | | return (word32)rtp_get_system_sec() * 1000; |
1833 | | } |
1834 | | #elif defined(WOLFSSL_DEOS) |
1835 | | word32 TimeNowInMilliseconds(void) |
1836 | | { |
1837 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
1838 | | word32 *systemTickPtr = systemTickPointer(); |
1839 | | |
1840 | | return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000; |
1841 | | } |
1842 | | #elif defined(MICRIUM) |
1843 | | /* The time in milliseconds. |
1844 | | * Used for tickets to represent difference between when first seen and when |
1845 | | * sending. |
1846 | | * |
1847 | | * returns the time in milliseconds as a 32-bit value. |
1848 | | */ |
1849 | | word32 TimeNowInMilliseconds(void) |
1850 | | { |
1851 | | OS_TICK ticks = 0; |
1852 | | OS_ERR err; |
1853 | | |
1854 | | ticks = OSTimeGet(&err); |
1855 | | |
1856 | | return (word32) (ticks / OSCfg_TickRate_Hz) * 1000; |
1857 | | } |
1858 | | #elif defined(MICROCHIP_TCPIP_V5) |
1859 | | /* The time in milliseconds. |
1860 | | * Used for tickets to represent difference between when first seen and when |
1861 | | * sending. |
1862 | | * |
1863 | | * returns the time in milliseconds as a 32-bit value. |
1864 | | */ |
1865 | | word32 TimeNowInMilliseconds(void) |
1866 | | { |
1867 | | return (word32) (TickGet() / (TICKS_PER_SECOND / 1000)); |
1868 | | } |
1869 | | #elif defined(MICROCHIP_TCPIP) |
1870 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
1871 | | #include <system/tmr/sys_tmr.h> |
1872 | | |
1873 | | /* The time in milliseconds. |
1874 | | * Used for tickets to represent difference between when first seen and when |
1875 | | * sending. |
1876 | | * |
1877 | | * returns the time in milliseconds as a 32-bit value. |
1878 | | */ |
1879 | | word32 TimeNowInMilliseconds(void) |
1880 | | { |
1881 | | return (word32)(SYS_TMR_TickCountGet() / |
1882 | | (SYS_TMR_TickCounterFrequencyGet() / 1000)); |
1883 | | } |
1884 | | #else |
1885 | | /* The time in milliseconds. |
1886 | | * Used for tickets to represent difference between when first seen and when |
1887 | | * sending. |
1888 | | * |
1889 | | * returns the time in milliseconds as a 32-bit value. |
1890 | | */ |
1891 | | word32 TimeNowInMilliseconds(void) |
1892 | | { |
1893 | | return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000)); |
1894 | | } |
1895 | | |
1896 | | #endif |
1897 | | |
1898 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1899 | | /* The time in milliseconds. |
1900 | | * Used for tickets to represent difference between when first seen and when |
1901 | | * sending. |
1902 | | * |
1903 | | * returns the time in milliseconds as a 32-bit value. |
1904 | | */ |
1905 | | word32 TimeNowInMilliseconds(void) |
1906 | | { |
1907 | | TIME_STRUCT mqxTime; |
1908 | | |
1909 | | _time_get_elapsed(&mqxTime); |
1910 | | |
1911 | | return (word32) mqxTime.SECONDS * 1000; |
1912 | | } |
1913 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
1914 | | #include "include/task.h" |
1915 | | |
1916 | | /* The time in milliseconds. |
1917 | | * Used for tickets to represent difference between when first seen and when |
1918 | | * sending. |
1919 | | * |
1920 | | * returns the time in milliseconds as a 32-bit value. |
1921 | | */ |
1922 | | word32 TimeNowInMilliseconds(void) |
1923 | | { |
1924 | | return (unsigned int)(((float)xTaskGetTickCount()) / |
1925 | | (configTICK_RATE_HZ / 1000)); |
1926 | | } |
1927 | | #elif defined(FREESCALE_KSDK_BM) |
1928 | | #include "lwip/sys.h" /* lwIP */ |
1929 | | |
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 | | return sys_now(); |
1939 | | } |
1940 | | |
1941 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
1942 | | |
1943 | | word32 TimeNowInMilliseconds(void) |
1944 | | { |
1945 | | return (word32)osKernelGetTickCount(); |
1946 | | } |
1947 | | |
1948 | | #elif defined(WOLFSSL_TIRTOS) |
1949 | | /* The time in milliseconds. |
1950 | | * Used for tickets to represent difference between when first seen and when |
1951 | | * sending. |
1952 | | * |
1953 | | * returns the time in milliseconds as a 32-bit value. |
1954 | | */ |
1955 | | word32 TimeNowInMilliseconds(void) |
1956 | | { |
1957 | | return (word32) Seconds_get() * 1000; |
1958 | | } |
1959 | | #elif defined(WOLFSSL_UTASKER) |
1960 | | /* The time in milliseconds. |
1961 | | * Used for tickets to represent difference between when first seen and when |
1962 | | * sending. |
1963 | | * |
1964 | | * returns the time in milliseconds as a 32-bit value. |
1965 | | */ |
1966 | | word32 TimeNowInMilliseconds(void) |
1967 | | { |
1968 | | return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
1969 | | } |
1970 | | #elif defined(WOLFSSL_LINUXKM) |
1971 | | word32 TimeNowInMilliseconds(void) |
1972 | | { |
1973 | | s64 t; |
1974 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
1975 | | struct timespec ts; |
1976 | | getnstimeofday(&ts); |
1977 | | t = ts.tv_sec * (s64)1000; |
1978 | | t += ts.tv_nsec / (s64)1000000; |
1979 | | #else |
1980 | | struct timespec64 ts; |
1981 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
1982 | | ts = current_kernel_time64(); |
1983 | | #else |
1984 | | ktime_get_coarse_real_ts64(&ts); |
1985 | | #endif |
1986 | | t = ts.tv_sec * 1000L; |
1987 | | t += ts.tv_nsec / 1000000L; |
1988 | | #endif |
1989 | | return (word32)t; |
1990 | | } |
1991 | | #elif defined(WOLFSSL_QNX_CAAM) |
1992 | | word32 TimeNowInMilliseconds(void) |
1993 | | { |
1994 | | struct timespec now; |
1995 | | clock_gettime(CLOCK_REALTIME, &now); |
1996 | | return (word32)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
1997 | | } |
1998 | | #elif defined(FUSION_RTOS) |
1999 | | /* The time in milliseconds. |
2000 | | * Used for tickets to represent difference between when first seen and when |
2001 | | * sending. |
2002 | | * |
2003 | | * returns the time in milliseconds as a 32-bit value. |
2004 | | */ |
2005 | | word32 TimeNowInMilliseconds(void) |
2006 | | { |
2007 | | struct timeval now; |
2008 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2009 | | return 0; |
2010 | | |
2011 | | /* Convert to milliseconds number. */ |
2012 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
2013 | | } |
2014 | | #elif defined(WOLFSSL_ZEPHYR) |
2015 | | word32 TimeNowInMilliseconds(void) |
2016 | | { |
2017 | | int64_t t; |
2018 | | #if defined(CONFIG_ARCH_POSIX) |
2019 | | k_cpu_idle(); |
2020 | | #endif |
2021 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2022 | | return (word32)t; |
2023 | | } |
2024 | | #elif defined(FREERTOS) |
2025 | | word32 TimeNowInMilliseconds(void) |
2026 | | { |
2027 | | return (word32)((uint64_t)(xTaskGetTickCount() * 1000) / |
2028 | | configTICK_RATE_HZ); |
2029 | | } |
2030 | | #else |
2031 | | /* The time in milliseconds. |
2032 | | * Used for tickets to represent difference between when first seen and when |
2033 | | * sending. |
2034 | | * |
2035 | | * returns the time in milliseconds as a 32-bit value. |
2036 | | */ |
2037 | | word32 TimeNowInMilliseconds(void) |
2038 | | { |
2039 | | struct timeval now; |
2040 | | |
2041 | | if (gettimeofday(&now, 0) < 0) |
2042 | | return 0; |
2043 | | |
2044 | | /* Convert to milliseconds number. */ |
2045 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
2046 | | } |
2047 | | #endif |
2048 | | #else |
2049 | | /* user must supply time in milliseconds function: |
2050 | | * word32 TimeNowInMilliseconds(void); |
2051 | | * The response is milliseconds elapsed |
2052 | | */ |
2053 | | #endif /* !NO_ASN_TIME */ |
2054 | | #else |
2055 | | #ifndef NO_ASN_TIME |
2056 | | #if defined(USER_TICKS) |
2057 | | #if 0 |
2058 | | sword64 TimeNowInMilliseconds(void) |
2059 | | { |
2060 | | /* |
2061 | | write your own clock tick function if don't want gettimeofday() |
2062 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
2063 | | */ |
2064 | | } |
2065 | | #endif |
2066 | | |
2067 | | #elif defined(TIME_OVERRIDES) |
2068 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
2069 | | sword64 TimeNowInMilliseconds(void) |
2070 | | { |
2071 | | return (sword64) wc_Time(0) * 1000; |
2072 | | } |
2073 | | #else |
2074 | | #ifndef HAVE_TIME_T_TYPE |
2075 | | typedef long time_t; |
2076 | | #endif |
2077 | | extern time_t XTIME(time_t * timer); |
2078 | | |
2079 | | /* The time in milliseconds. |
2080 | | * Used for tickets to represent difference between when first seen and when |
2081 | | * sending. |
2082 | | * |
2083 | | * returns the time in milliseconds as a 32-bit value. |
2084 | | */ |
2085 | | sword64 TimeNowInMilliseconds(void) |
2086 | | { |
2087 | | return (sword64) XTIME(0) * 1000; |
2088 | | } |
2089 | | #endif |
2090 | | |
2091 | | #elif defined(XTIME_MS) |
2092 | | sword64 TimeNowInMilliseconds(void) |
2093 | | { |
2094 | | return (sword64)XTIME_MS(0); |
2095 | | } |
2096 | | |
2097 | | #elif defined(USE_WINDOWS_API) |
2098 | | /* The time in milliseconds. |
2099 | | * Used for tickets to represent difference between when first seen and when |
2100 | | * sending. |
2101 | | * |
2102 | | * returns the time in milliseconds as a 64-bit value. |
2103 | | */ |
2104 | | sword64 TimeNowInMilliseconds(void) |
2105 | | { |
2106 | | static int init = 0; |
2107 | | static LARGE_INTEGER freq; |
2108 | | LARGE_INTEGER count; |
2109 | | |
2110 | | if (!init) { |
2111 | | QueryPerformanceFrequency(&freq); |
2112 | | init = 1; |
2113 | | } |
2114 | | |
2115 | | QueryPerformanceCounter(&count); |
2116 | | |
2117 | | return (sword64)(count.QuadPart / (freq.QuadPart / 1000)); |
2118 | | } |
2119 | | |
2120 | | #elif defined(HAVE_RTP_SYS) |
2121 | | #include "rtptime.h" |
2122 | | |
2123 | | /* The time in milliseconds. |
2124 | | * Used for tickets to represent difference between when first seen and when |
2125 | | * sending. |
2126 | | * |
2127 | | * returns the time in milliseconds as a 64-bit value. |
2128 | | */ |
2129 | | sword64 TimeNowInMilliseconds(void) |
2130 | | { |
2131 | | return (sword64)rtp_get_system_sec() * 1000; |
2132 | | } |
2133 | | #elif defined(WOLFSSL_DEOS) |
2134 | | sword64 TimeNowInMilliseconds(void) |
2135 | | { |
2136 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2137 | | word32 *systemTickPtr = systemTickPointer(); |
2138 | | |
2139 | | return (sword64) (*systemTickPtr/systemTickTimeInHz) * 1000; |
2140 | | } |
2141 | | #elif defined(MICRIUM) |
2142 | | /* The time in milliseconds. |
2143 | | * Used for tickets to represent difference between when first seen and when |
2144 | | * sending. |
2145 | | * |
2146 | | * returns the time in milliseconds as a 64-bit value. |
2147 | | */ |
2148 | | sword64 TimeNowInMilliseconds(void) |
2149 | | { |
2150 | | OS_TICK ticks = 0; |
2151 | | OS_ERR err; |
2152 | | |
2153 | | ticks = OSTimeGet(&err); |
2154 | | |
2155 | | return (sword64) (ticks / OSCfg_TickRate_Hz) * 1000; |
2156 | | } |
2157 | | #elif defined(MICROCHIP_TCPIP_V5) |
2158 | | /* The time in milliseconds. |
2159 | | * Used for tickets to represent difference between when first seen and when |
2160 | | * sending. |
2161 | | * |
2162 | | * returns the time in milliseconds as a 64-bit value. |
2163 | | */ |
2164 | | sword64 TimeNowInMilliseconds(void) |
2165 | | { |
2166 | | return (sword64) (TickGet() / (TICKS_PER_SECOND / 1000)); |
2167 | | } |
2168 | | #elif defined(MICROCHIP_TCPIP) |
2169 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
2170 | | #include <system/tmr/sys_tmr.h> |
2171 | | |
2172 | | /* The time in milliseconds. |
2173 | | * Used for tickets to represent difference between when first seen and when |
2174 | | * sending. |
2175 | | * |
2176 | | * returns the time in milliseconds as a 64-bit value. |
2177 | | */ |
2178 | | sword64 TimeNowInMilliseconds(void) |
2179 | | { |
2180 | | return (sword64)SYS_TMR_TickCountGet() / |
2181 | | (SYS_TMR_TickCounterFrequencyGet() / 1000); |
2182 | | } |
2183 | | #else |
2184 | | /* The time in milliseconds. |
2185 | | * Used for tickets to represent difference between when first seen and when |
2186 | | * sending. |
2187 | | * |
2188 | | * returns the time in milliseconds as a 64-bit value. |
2189 | | */ |
2190 | | sword64 TimeNowInMilliseconds(void) |
2191 | | { |
2192 | | return (sword64)SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000); |
2193 | | } |
2194 | | |
2195 | | #endif |
2196 | | |
2197 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2198 | | /* The time in milliseconds. |
2199 | | * Used for tickets to represent difference between when first seen and when |
2200 | | * sending. |
2201 | | * |
2202 | | * returns the time in milliseconds as a 64-bit value. |
2203 | | */ |
2204 | | sword64 TimeNowInMilliseconds(void) |
2205 | | { |
2206 | | TIME_STRUCT mqxTime; |
2207 | | |
2208 | | _time_get_elapsed(&mqxTime); |
2209 | | |
2210 | | return (sword64) mqxTime.SECONDS * 1000; |
2211 | | } |
2212 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
2213 | | #include "include/task.h" |
2214 | | |
2215 | | /* The time in milliseconds. |
2216 | | * Used for tickets to represent difference between when first seen and when |
2217 | | * sending. |
2218 | | * |
2219 | | * returns the time in milliseconds as a 64-bit value. |
2220 | | */ |
2221 | | sword64 TimeNowInMilliseconds(void) |
2222 | | { |
2223 | | return (sword64)xTaskGetTickCount() / (configTICK_RATE_HZ / 1000); |
2224 | | } |
2225 | | #elif defined(FREESCALE_KSDK_BM) |
2226 | | #include "lwip/sys.h" /* lwIP */ |
2227 | | |
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 | | return sys_now(); |
2237 | | } |
2238 | | |
2239 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
2240 | | |
2241 | | sword64 TimeNowInMilliseconds(void) |
2242 | | { |
2243 | | return (sword64)osKernelGetTickCount(); |
2244 | | } |
2245 | | |
2246 | | #elif defined(WOLFSSL_TIRTOS) |
2247 | | /* The time in milliseconds. |
2248 | | * Used for tickets to represent difference between when first seen and when |
2249 | | * sending. |
2250 | | * |
2251 | | * returns the time in milliseconds as a 64-bit value. |
2252 | | */ |
2253 | | sword64 TimeNowInMilliseconds(void) |
2254 | | { |
2255 | | return (sword64) Seconds_get() * 1000; |
2256 | | } |
2257 | | #elif defined(WOLFSSL_UTASKER) |
2258 | | /* The time in milliseconds. |
2259 | | * Used for tickets to represent difference between when first seen and when |
2260 | | * sending. |
2261 | | * |
2262 | | * returns the time in milliseconds as a 64-bit value. |
2263 | | */ |
2264 | | sword64 TimeNowInMilliseconds(void) |
2265 | | { |
2266 | | return (sword64)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
2267 | | } |
2268 | | #elif defined(WOLFSSL_LINUXKM) |
2269 | | sword64 TimeNowInMilliseconds(void) |
2270 | | { |
2271 | | s64 t; |
2272 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2273 | | struct timespec ts; |
2274 | | getnstimeofday(&ts); |
2275 | | t = ts.tv_sec * (s64)1000; |
2276 | | t += ts.tv_nsec / (s64)1000000; |
2277 | | #else |
2278 | | struct timespec64 ts; |
2279 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2280 | | ts = current_kernel_time64(); |
2281 | | #else |
2282 | | ktime_get_coarse_real_ts64(&ts); |
2283 | | #endif |
2284 | | t = ts.tv_sec * 1000L; |
2285 | | t += ts.tv_nsec / 1000000L; |
2286 | | #endif |
2287 | | return (sword64)t; |
2288 | | } |
2289 | | #elif defined(WOLFSSL_QNX_CAAM) |
2290 | | sword64 TimeNowInMilliseconds(void) |
2291 | | { |
2292 | | struct timespec now; |
2293 | | clock_gettime(CLOCK_REALTIME, &now); |
2294 | | return (sword64)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
2295 | | } |
2296 | | #elif defined(FUSION_RTOS) |
2297 | | /* The time in milliseconds. |
2298 | | * Used for tickets to represent difference between when first seen and when |
2299 | | * sending. |
2300 | | * |
2301 | | * returns the time in milliseconds as a 64-bit value. |
2302 | | */ |
2303 | | sword64 TimeNowInMilliseconds(void) |
2304 | | { |
2305 | | struct timeval now; |
2306 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2307 | | return 0; |
2308 | | |
2309 | | /* Convert to milliseconds number. */ |
2310 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2311 | | } |
2312 | | #elif defined(WOLFSSL_ZEPHYR) |
2313 | | sword64 TimeNowInMilliseconds(void) |
2314 | | { |
2315 | | int64_t t; |
2316 | | #if defined(CONFIG_ARCH_POSIX) |
2317 | | k_cpu_idle(); |
2318 | | #endif |
2319 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2320 | | return (sword64)t; |
2321 | | } |
2322 | | #elif defined(FREERTOS) |
2323 | | sword64 TimeNowInMilliseconds(void) |
2324 | | { |
2325 | | return (sword64)((uint64_t)(xTaskGetTickCount() * 1000) / |
2326 | | configTICK_RATE_HZ); |
2327 | | } |
2328 | | #else |
2329 | | /* The time in milliseconds. |
2330 | | * Used for tickets to represent difference between when first seen and when |
2331 | | * sending. |
2332 | | * |
2333 | | * returns the time in milliseconds as a 64-bit value. |
2334 | | */ |
2335 | | sword64 TimeNowInMilliseconds(void) |
2336 | | { |
2337 | | struct timeval now; |
2338 | | |
2339 | | if (gettimeofday(&now, 0) < 0) |
2340 | | return 0; |
2341 | | |
2342 | | /* Convert to milliseconds number. */ |
2343 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2344 | | } |
2345 | | #endif |
2346 | | #else |
2347 | | /* user must supply time in milliseconds function: |
2348 | | * sword64 TimeNowInMilliseconds(void); |
2349 | | * The response is milliseconds elapsed |
2350 | | */ |
2351 | | #endif /* !NO_ASN_TIME */ |
2352 | | #endif /* WOLFSSL_32BIT_MILLI_TIME */ |
2353 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK || WOLFSSL_DTLS13 */ |
2354 | | |
2355 | | /* Add record layer header to message. |
2356 | | * |
2357 | | * output The buffer to write the record layer header into. |
2358 | | * length The length of the record data. |
2359 | | * type The type of record message. |
2360 | | * ssl The SSL/TLS object. |
2361 | | */ |
2362 | | static void AddTls13RecordHeader(byte* output, word32 length, byte type, |
2363 | | WOLFSSL* ssl) |
2364 | 5.64k | { |
2365 | 5.64k | RecordLayerHeader* rl; |
2366 | | |
2367 | 5.64k | rl = (RecordLayerHeader*)output; |
2368 | 5.64k | rl->type = type; |
2369 | 5.64k | rl->pvMajor = ssl->version.major; |
2370 | | /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */ |
2371 | 5.64k | rl->pvMinor = TLSv1_2_MINOR; |
2372 | 5.64k | c16toa((word16)length, rl->length); |
2373 | 5.64k | } |
2374 | | |
2375 | | /* Add handshake header to message. |
2376 | | * |
2377 | | * output The buffer to write the handshake header into. |
2378 | | * length The length of the handshake data. |
2379 | | * fragOffset The offset of the fragment data. (DTLS) |
2380 | | * fragLength The length of the fragment data. (DTLS) |
2381 | | * type The type of handshake message. |
2382 | | * ssl The SSL/TLS object. (DTLS) |
2383 | | */ |
2384 | | static void AddTls13HandShakeHeader(byte* output, word32 length, |
2385 | | word32 fragOffset, word32 fragLength, |
2386 | | byte type, WOLFSSL* ssl) |
2387 | | { |
2388 | | HandShakeHeader* hs; |
2389 | | (void)fragOffset; |
2390 | | (void)fragLength; |
2391 | | (void)ssl; |
2392 | | |
2393 | | #ifdef WOLFSSL_DTLS13 |
2394 | | /* message_hash type is used for a synthetic message that replaces the first |
2395 | | ClientHello in the hash transcript when using HelloRetryRequest. It will |
2396 | | never be transmitted and, as the DTLS-only fields must not be considered |
2397 | | when computing the hash transcript, we can avoid to use the DTLS |
2398 | | handshake header. */ |
2399 | | if (ssl->options.dtls && type != message_hash) { |
2400 | | Dtls13HandshakeAddHeader(ssl, output, (enum HandShakeType)type, length); |
2401 | | return; |
2402 | | } |
2403 | | #endif /* WOLFSSL_DTLS13 */ |
2404 | | |
2405 | | /* handshake header */ |
2406 | | hs = (HandShakeHeader*)output; |
2407 | | hs->type = type; |
2408 | | c32to24(length, hs->length); |
2409 | | } |
2410 | | |
2411 | | |
2412 | | /* Add both record layer and handshake header to message. |
2413 | | * |
2414 | | * output The buffer to write the headers into. |
2415 | | * length The length of the handshake data. |
2416 | | * type The type of record layer message. |
2417 | | * ssl The SSL/TLS object. (DTLS) |
2418 | | */ |
2419 | | static void AddTls13Headers(byte* output, word32 length, byte type, |
2420 | | WOLFSSL* ssl) |
2421 | | { |
2422 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2423 | | word32 outputAdj = RECORD_HEADER_SZ; |
2424 | | |
2425 | | #ifdef WOLFSSL_DTLS13 |
2426 | | if (ssl->options.dtls) { |
2427 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2428 | | return; |
2429 | | } |
2430 | | #endif /* WOLFSSL_DTLS13 */ |
2431 | | |
2432 | | AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl); |
2433 | | AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl); |
2434 | | } |
2435 | | |
2436 | | #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) \ |
2437 | | && !defined(NO_CERTS) |
2438 | | /* Add both record layer and fragment handshake header to message. |
2439 | | * |
2440 | | * output The buffer to write the headers into. |
2441 | | * fragOffset The offset of the fragment data. (DTLS) |
2442 | | * fragLength The length of the fragment data. (DTLS) |
2443 | | * length The length of the handshake data. |
2444 | | * type The type of record layer message. |
2445 | | * ssl The SSL/TLS object. (DTLS) |
2446 | | */ |
2447 | | static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset, |
2448 | | word32 length, byte type, WOLFSSL* ssl) |
2449 | | { |
2450 | | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2451 | | word32 outputAdj = RECORD_HEADER_SZ; |
2452 | | (void)fragSz; |
2453 | | |
2454 | | #ifdef WOLFSSL_DTLS13 |
2455 | | /* we ignore fragmentation fields here because fragmentation logic for |
2456 | | DTLS1.3 is inside dtls13_handshake_send(). */ |
2457 | | if (ssl->options.dtls) { |
2458 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2459 | | return; |
2460 | | } |
2461 | | #endif /* WOLFSSL_DTLS13 */ |
2462 | | |
2463 | | AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl); |
2464 | | AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz, |
2465 | | type, ssl); |
2466 | | } |
2467 | | #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !NO_CERTS */ |
2468 | | |
2469 | | /* Write the sequence number into the buffer. |
2470 | | * No DTLS v1.3 support. |
2471 | | * |
2472 | | * ssl The SSL/TLS object. |
2473 | | * verifyOrder Which set of sequence numbers to use. |
2474 | | * out The buffer to write into. |
2475 | | */ |
2476 | | static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out) |
2477 | 1.59k | { |
2478 | 1.59k | word32 seq[2] = {0, 0}; |
2479 | | |
2480 | 1.59k | if (ssl->options.dtls) { |
2481 | | #ifdef WOLFSSL_DTLS13 |
2482 | | Dtls13GetSeq(ssl, verifyOrder, seq, 1); |
2483 | | #endif /* WOLFSSL_DTLS13 */ |
2484 | 0 | } |
2485 | 1.59k | else if (verifyOrder == PEER_ORDER) { |
2486 | 100 | seq[0] = ssl->keys.peer_sequence_number_hi; |
2487 | 100 | seq[1] = ssl->keys.peer_sequence_number_lo++; |
2488 | | /* handle rollover */ |
2489 | 100 | if (seq[1] > ssl->keys.peer_sequence_number_lo) |
2490 | 0 | ssl->keys.peer_sequence_number_hi++; |
2491 | 100 | } |
2492 | 1.49k | else { |
2493 | 1.49k | seq[0] = ssl->keys.sequence_number_hi; |
2494 | 1.49k | seq[1] = ssl->keys.sequence_number_lo++; |
2495 | | /* handle rollover */ |
2496 | 1.49k | if (seq[1] > ssl->keys.sequence_number_lo) |
2497 | 0 | ssl->keys.sequence_number_hi++; |
2498 | 1.49k | } |
2499 | | #ifdef WOLFSSL_DEBUG_TLS |
2500 | | WOLFSSL_MSG_EX("TLS 1.3 Write Sequence %d %d", seq[0], seq[1]); |
2501 | | #endif |
2502 | | |
2503 | 1.59k | c32toa(seq[0], out); |
2504 | 1.59k | c32toa(seq[1], out + OPAQUE32_LEN); |
2505 | 1.59k | } |
2506 | | |
2507 | | /* Build the nonce for TLS v1.3 encryption and decryption. |
2508 | | * |
2509 | | * ssl The SSL/TLS object. |
2510 | | * nonce The nonce data to use when encrypting or decrypting. |
2511 | | * iv The derived IV. |
2512 | | * order The side on which the message is to be or was sent. |
2513 | | */ |
2514 | | static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv, |
2515 | | int ivSz, int order) |
2516 | 1.59k | { |
2517 | 1.59k | int seq_offset; |
2518 | | /* Ensure minimum nonce size for standard AEAD ciphers */ |
2519 | 1.59k | if (ivSz < AEAD_NONCE_SZ) |
2520 | 0 | ivSz = AEAD_NONCE_SZ; |
2521 | 1.59k | seq_offset = ivSz - SEQ_SZ; |
2522 | | /* The nonce is the IV with the sequence XORed into the last bytes. */ |
2523 | 1.59k | WriteSEQTls13(ssl, order, nonce + seq_offset); |
2524 | 1.59k | XMEMCPY(nonce, iv, seq_offset); |
2525 | 1.59k | xorbuf(nonce + seq_offset, iv + seq_offset, SEQ_SZ); |
2526 | 1.59k | } |
2527 | | |
2528 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2529 | | /* Encrypt with ChaCha20 and create authentication tag with Poly1305. |
2530 | | * |
2531 | | * ssl The SSL/TLS object. |
2532 | | * output The buffer to write encrypted data and authentication tag into. |
2533 | | * May be the same pointer as input. |
2534 | | * input The data to encrypt. |
2535 | | * sz The number of bytes to encrypt. |
2536 | | * nonce The nonce to use with ChaCha20. |
2537 | | * aad The additional authentication data. |
2538 | | * aadSz The size of the addition authentication data. |
2539 | | * tag The authentication tag buffer. |
2540 | | * returns 0 on success, otherwise failure. |
2541 | | */ |
2542 | | static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output, |
2543 | | const byte* input, word16 sz, byte* nonce, |
2544 | | const byte* aad, word16 aadSz, byte* tag) |
2545 | 453 | { |
2546 | | /* Persistent-key stitched helper: derives the per-record Poly1305 key from |
2547 | | * the keyed ChaCha, then encrypts and authenticates in one pass (the IFMA |
2548 | | * stitch for large records, else two-pass). TLS 1.3 is always RFC 8439. */ |
2549 | 453 | return wc_ChaCha20Poly1305_Encrypt_ex(ssl->encrypt.chacha, |
2550 | 453 | ssl->auth.poly1305, output, input, sz, nonce, tag, aad, aadSz); |
2551 | 453 | } |
2552 | | #endif |
2553 | | |
2554 | | #ifdef HAVE_NULL_CIPHER |
2555 | | /* Create authentication tag and copy data over input. |
2556 | | * |
2557 | | * ssl The SSL/TLS object. |
2558 | | * output The buffer to copy data into. |
2559 | | * May be the same pointer as input. |
2560 | | * input The data. |
2561 | | * sz The number of bytes of data. |
2562 | | * nonce The nonce to use with authentication. |
2563 | | * aad The additional authentication data. |
2564 | | * aadSz The size of the addition authentication data. |
2565 | | * tag The authentication tag buffer. |
2566 | | * returns 0 on success, otherwise failure. |
2567 | | */ |
2568 | | static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output, |
2569 | | const byte* input, word16 sz, |
2570 | | const byte* nonce, |
2571 | | const byte* aad, word16 aadSz, byte* tag) |
2572 | | { |
2573 | | int ret; |
2574 | | |
2575 | | /* HMAC: nonce | aad | input */ |
2576 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, ssl->specs.iv_size); |
2577 | | if (ret == 0) |
2578 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz); |
2579 | | if (ret == 0) |
2580 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz); |
2581 | | if (ret == 0) |
2582 | | ret = wc_HmacFinal(ssl->encrypt.hmac, tag); |
2583 | | /* Copy the input to output if not the same buffer */ |
2584 | | if (ret == 0 && output != input) |
2585 | | XMEMCPY(output, input, sz); |
2586 | | return ret; |
2587 | | } |
2588 | | #endif |
2589 | | |
2590 | | /* Encrypt data for TLS v1.3. |
2591 | | * |
2592 | | * ssl The SSL/TLS object. |
2593 | | * output The buffer to write encrypted data and authentication tag into. |
2594 | | * May be the same pointer as input. |
2595 | | * input The record header and data to encrypt. |
2596 | | * sz The number of bytes to encrypt. |
2597 | | * aad The additional authentication data. |
2598 | | * aadSz The size of the addition authentication data. |
2599 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
2600 | | * returns 0 on success, otherwise failure. |
2601 | | */ |
2602 | | static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, |
2603 | | word16 sz, const byte* aad, word16 aadSz, int asyncOkay) |
2604 | | { |
2605 | | int ret = 0; |
2606 | | word16 dataSz; |
2607 | | word16 macSz = ssl->specs.aead_mac_size; |
2608 | | word32 nonceSz = 0; |
2609 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2610 | | WC_ASYNC_DEV* asyncDev = NULL; |
2611 | | word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN; |
2612 | | #endif |
2613 | | |
2614 | | WOLFSSL_ENTER("EncryptTls13"); |
2615 | | if (sz < ssl->specs.aead_mac_size) |
2616 | | return BUFFER_E; |
2617 | | dataSz = sz - ssl->specs.aead_mac_size; |
2618 | | |
2619 | | (void)output; |
2620 | | (void)input; |
2621 | | (void)sz; |
2622 | | (void)dataSz; |
2623 | | (void)macSz; |
2624 | | (void)asyncOkay; |
2625 | | (void)nonceSz; |
2626 | | |
2627 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2628 | | if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2629 | | ssl->error = 0; /* clear async */ |
2630 | | } |
2631 | | #endif |
2632 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2633 | | ret = tsip_Tls13AesEncrypt(ssl, output, input, dataSz); |
2634 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2635 | | if (ret > 0) { |
2636 | | ret = 0; /* tsip_Tls13AesEncrypt returns output size */ |
2637 | | } |
2638 | | return ret; |
2639 | | } |
2640 | | ret = 0; |
2641 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
2642 | | |
2643 | | switch (ssl->encrypt.state) { |
2644 | | case CIPHER_STATE_BEGIN: |
2645 | | { |
2646 | | #ifdef WOLFSSL_DEBUG_TLS |
2647 | | WOLFSSL_MSG("Data to encrypt"); |
2648 | | WOLFSSL_BUFFER(input, dataSz); |
2649 | | WOLFSSL_MSG("Additional Authentication Data"); |
2650 | | WOLFSSL_BUFFER(aad, aadSz); |
2651 | | #endif |
2652 | | |
2653 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2654 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2655 | | dataSz >= sizeof(ssl->encrypt.sanityCheck)) { |
2656 | | XMEMCPY(ssl->encrypt.sanityCheck, input, |
2657 | | sizeof(ssl->encrypt.sanityCheck)); |
2658 | | } |
2659 | | #endif |
2660 | | |
2661 | | #ifdef CIPHER_NONCE |
2662 | | if (ssl->encrypt.nonce == NULL) { |
2663 | | ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
2664 | | ssl->heap, DYNAMIC_TYPE_CIPHER); |
2665 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2666 | | if (ssl->encrypt.nonce != NULL) { |
2667 | | wc_MemZero_Add("EncryptTls13 nonce", ssl->encrypt.nonce, |
2668 | | ssl->specs.iv_size); |
2669 | | } |
2670 | | #endif |
2671 | | } |
2672 | | if (ssl->encrypt.nonce == NULL) |
2673 | | return MEMORY_E; |
2674 | | |
2675 | | BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV, |
2676 | | ssl->specs.iv_size, CUR_ORDER); |
2677 | | #endif |
2678 | | |
2679 | | /* Advance state and proceed */ |
2680 | | ssl->encrypt.state = CIPHER_STATE_DO; |
2681 | | } |
2682 | | FALL_THROUGH; |
2683 | | |
2684 | | case CIPHER_STATE_DO: |
2685 | | { |
2686 | | switch (ssl->specs.bulk_cipher_algorithm) { |
2687 | | #ifdef BUILD_AESGCM |
2688 | | case wolfssl_aes_gcm: |
2689 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2690 | | /* initialize event */ |
2691 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2692 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2693 | | if (ret != 0) |
2694 | | break; |
2695 | | #endif |
2696 | | |
2697 | | nonceSz = AESGCM_NONCE_SZ; |
2698 | | |
2699 | | #if defined(HAVE_PK_CALLBACKS) |
2700 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2701 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2702 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2703 | | output, input, dataSz, |
2704 | | ssl->encrypt.nonce, nonceSz, |
2705 | | output + dataSz, macSz, |
2706 | | aad, aadSz); |
2707 | | } |
2708 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2709 | | #endif |
2710 | | { |
2711 | | |
2712 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2713 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2714 | | ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, |
2715 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2716 | | output + dataSz, macSz, aad, aadSz); |
2717 | | #else |
2718 | | ret = wc_AesGcmSetExtIV(ssl->encrypt.aes, |
2719 | | ssl->encrypt.nonce, nonceSz); |
2720 | | if (ret == 0) { |
2721 | | ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output, |
2722 | | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2723 | | output + dataSz, macSz, aad, aadSz); |
2724 | | } |
2725 | | #endif |
2726 | | } |
2727 | | break; |
2728 | | #endif |
2729 | | |
2730 | | #ifdef HAVE_AESCCM |
2731 | | case wolfssl_aes_ccm: |
2732 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2733 | | /* initialize event */ |
2734 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2735 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2736 | | if (ret != 0) |
2737 | | break; |
2738 | | #endif |
2739 | | |
2740 | | nonceSz = AESCCM_NONCE_SZ; |
2741 | | #if defined(HAVE_PK_CALLBACKS) |
2742 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2743 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2744 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2745 | | output, input, dataSz, |
2746 | | ssl->encrypt.nonce, nonceSz, |
2747 | | output + dataSz, macSz, |
2748 | | aad, aadSz); |
2749 | | } |
2750 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2751 | | #endif |
2752 | | { |
2753 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2754 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2755 | | ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, |
2756 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2757 | | output + dataSz, macSz, aad, aadSz); |
2758 | | #else |
2759 | | ret = wc_AesCcmSetNonce(ssl->encrypt.aes, |
2760 | | ssl->encrypt.nonce, nonceSz); |
2761 | | if (ret == 0) { |
2762 | | ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output, |
2763 | | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2764 | | output + dataSz, macSz, aad, aadSz); |
2765 | | } |
2766 | | #endif |
2767 | | } |
2768 | | break; |
2769 | | #endif |
2770 | | |
2771 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2772 | | case wolfssl_chacha: |
2773 | | ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz, |
2774 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2775 | | break; |
2776 | | #endif |
2777 | | |
2778 | | #ifdef WOLFSSL_SM4_GCM |
2779 | | case wolfssl_sm4_gcm: |
2780 | | nonceSz = SM4_GCM_NONCE_SZ; |
2781 | | ret = wc_Sm4GcmEncrypt(ssl->encrypt.sm4, output, input, |
2782 | | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2783 | | macSz, aad, aadSz); |
2784 | | break; |
2785 | | #endif |
2786 | | |
2787 | | #ifdef WOLFSSL_SM4_CCM |
2788 | | case wolfssl_sm4_ccm: |
2789 | | nonceSz = SM4_CCM_NONCE_SZ; |
2790 | | ret = wc_Sm4CcmEncrypt(ssl->encrypt.sm4, output, input, |
2791 | | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2792 | | macSz, aad, aadSz); |
2793 | | break; |
2794 | | #endif |
2795 | | |
2796 | | #ifdef HAVE_NULL_CIPHER |
2797 | | case wolfssl_cipher_null: |
2798 | | ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz, |
2799 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2800 | | break; |
2801 | | #endif |
2802 | | |
2803 | | default: |
2804 | | WOLFSSL_MSG("wolfSSL Encrypt programming error"); |
2805 | | return ENCRYPT_ERROR; |
2806 | | } |
2807 | | |
2808 | | /* Advance state */ |
2809 | | ssl->encrypt.state = CIPHER_STATE_END; |
2810 | | |
2811 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2812 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2813 | | /* if async is not okay, then block */ |
2814 | | if (!asyncOkay) { |
2815 | | ret = wc_AsyncWait(ret, asyncDev, event_flags); |
2816 | | } |
2817 | | else { |
2818 | | /* If pending, then leave and return will resume below */ |
2819 | | return wolfSSL_AsyncPush(ssl, asyncDev); |
2820 | | } |
2821 | | } |
2822 | | #endif |
2823 | | } |
2824 | | FALL_THROUGH; |
2825 | | |
2826 | | case CIPHER_STATE_END: |
2827 | | { |
2828 | | #ifdef WOLFSSL_DEBUG_TLS |
2829 | | #ifdef CIPHER_NONCE |
2830 | | WOLFSSL_MSG("Nonce"); |
2831 | | WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size); |
2832 | | #endif |
2833 | | WOLFSSL_MSG("Encrypted data"); |
2834 | | WOLFSSL_BUFFER(output, dataSz); |
2835 | | WOLFSSL_MSG("Authentication Tag"); |
2836 | | WOLFSSL_BUFFER(output + dataSz, macSz); |
2837 | | #endif |
2838 | | |
2839 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2840 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2841 | | dataSz >= sizeof(ssl->encrypt.sanityCheck) && |
2842 | | XMEMCMP(output, ssl->encrypt.sanityCheck, |
2843 | | sizeof(ssl->encrypt.sanityCheck)) == 0) { |
2844 | | |
2845 | | WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?"); |
2846 | | return ENCRYPT_ERROR; |
2847 | | } |
2848 | | ForceZero(ssl->encrypt.sanityCheck, |
2849 | | sizeof(ssl->encrypt.sanityCheck)); |
2850 | | #endif |
2851 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2852 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
2853 | | (output != input) && (ret == 0)) { |
2854 | | wc_MemZero_Add("TLS 1.3 Encrypt plaintext", input, sz); |
2855 | | } |
2856 | | #endif |
2857 | | |
2858 | | #ifdef CIPHER_NONCE |
2859 | | ForceZero(ssl->encrypt.nonce, ssl->specs.iv_size); |
2860 | | #endif |
2861 | | |
2862 | | break; |
2863 | | } |
2864 | | |
2865 | | default: |
2866 | | break; |
2867 | | } |
2868 | | |
2869 | | |
2870 | | /* Reset state */ |
2871 | | ssl->encrypt.state = CIPHER_STATE_BEGIN; |
2872 | | |
2873 | | return ret; |
2874 | | } |
2875 | | |
2876 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2877 | | /* Decrypt with ChaCha20 and check authentication tag with Poly1305. |
2878 | | * |
2879 | | * ssl The SSL/TLS object. |
2880 | | * output The buffer to write decrypted data into. |
2881 | | * May be the same pointer as input. |
2882 | | * input The data to decrypt. |
2883 | | * sz The number of bytes to decrypt. |
2884 | | * nonce The nonce to use with ChaCha20. |
2885 | | * aad The additional authentication data. |
2886 | | * aadSz The size of the addition authentication data. |
2887 | | * tagIn The authentication tag data from packet. |
2888 | | * returns 0 on success, otherwise failure. |
2889 | | */ |
2890 | | static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output, |
2891 | | const byte* input, word16 sz, byte* nonce, |
2892 | | const byte* aad, word16 aadSz, |
2893 | | const byte* tagIn) |
2894 | 21 | { |
2895 | 21 | int ret; |
2896 | | |
2897 | | /* Persistent-key stitched helper: verifies the Poly1305 tag over |
2898 | | * aad+ciphertext and decrypts in one pass (the IFMA decrypt stitch for |
2899 | | * large records, else two-pass); it zeroes output on tag mismatch. */ |
2900 | 21 | ret = wc_ChaCha20Poly1305_Decrypt_ex(ssl->decrypt.chacha, |
2901 | 21 | ssl->auth.poly1305, output, input, sz, nonce, tagIn, aad, aadSz); |
2902 | 21 | if (ret == WC_NO_ERR_TRACE(MAC_CMP_FAILED_E)) { |
2903 | 21 | WOLFSSL_MSG("MAC did not match"); |
2904 | 21 | ret = VERIFY_MAC_ERROR; |
2905 | 21 | } |
2906 | | |
2907 | 21 | return ret; |
2908 | 21 | } |
2909 | | #endif |
2910 | | |
2911 | | #ifdef HAVE_NULL_CIPHER |
2912 | | /* Check HMAC tag and copy over input. |
2913 | | * |
2914 | | * ssl The SSL/TLS object. |
2915 | | * output The buffer to copy data into. |
2916 | | * May be the same pointer as input. |
2917 | | * input The data. |
2918 | | * sz The number of bytes of data. |
2919 | | * nonce The nonce to use with authentication. |
2920 | | * aad The additional authentication data. |
2921 | | * aadSz The size of the addition authentication data. |
2922 | | * tagIn The authentication tag data from packet. |
2923 | | * returns 0 on success, otherwise failure. |
2924 | | */ |
2925 | | static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output, |
2926 | | const byte* input, word16 sz, |
2927 | | const byte* nonce, |
2928 | | const byte* aad, word16 aadSz, |
2929 | | const byte* tagIn) |
2930 | | { |
2931 | | int ret; |
2932 | | byte hmac[WC_MAX_DIGEST_SIZE]; |
2933 | | |
2934 | | /* HMAC: nonce | aad | input */ |
2935 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, ssl->specs.iv_size); |
2936 | | if (ret == 0) |
2937 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz); |
2938 | | if (ret == 0) |
2939 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz); |
2940 | | if (ret == 0) |
2941 | | ret = wc_HmacFinal(ssl->decrypt.hmac, hmac); |
2942 | | /* Check authentication tag matches */ |
2943 | | if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0) |
2944 | | ret = DECRYPT_ERROR; |
2945 | | /* Copy the input to output if not the same buffer */ |
2946 | | if (ret == 0 && output != input) |
2947 | | XMEMCPY(output, input, sz); |
2948 | | ForceZero(hmac, sizeof(hmac)); |
2949 | | return ret; |
2950 | | } |
2951 | | #endif |
2952 | | |
2953 | | /* Decrypt data for TLS v1.3. |
2954 | | * |
2955 | | * ssl The SSL/TLS object. |
2956 | | * output The buffer to write decrypted data into. |
2957 | | * May be the same pointer as input. |
2958 | | * input The data to decrypt and authentication tag. |
2959 | | * sz The length of the encrypted data plus authentication tag. |
2960 | | * aad The additional authentication data. |
2961 | | * aadSz The size of the addition authentication data. |
2962 | | * returns 0 on success, otherwise failure. |
2963 | | */ |
2964 | | int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz, |
2965 | | const byte* aad, word16 aadSz) |
2966 | 0 | { |
2967 | 0 | int ret = 0; |
2968 | 0 | word16 dataSz; |
2969 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2970 | 0 | word32 nonceSz = 0; |
2971 | |
|
2972 | 0 | WOLFSSL_ENTER("DecryptTls13"); |
2973 | 0 | if (sz < ssl->specs.aead_mac_size) { |
2974 | 0 | return BAD_FUNC_ARG; |
2975 | 0 | } |
2976 | 0 | dataSz = sz - ssl->specs.aead_mac_size; |
2977 | |
|
2978 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2979 | | ret = tsip_Tls13AesDecrypt(ssl, output, input, sz); |
2980 | | |
2981 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2982 | | #ifndef WOLFSSL_EARLY_DATA |
2983 | | if (ret < 0) { |
2984 | | ret = VERIFY_MAC_ERROR; |
2985 | | WOLFSSL_ERROR_VERBOSE(ret); |
2986 | | } |
2987 | | #endif |
2988 | | return ret; |
2989 | | } |
2990 | | #endif |
2991 | |
|
2992 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2993 | | ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state); |
2994 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
2995 | | /* check for still pending */ |
2996 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
2997 | | return ret; |
2998 | | |
2999 | | ssl->error = 0; /* clear async */ |
3000 | | |
3001 | | /* let failures through so CIPHER_STATE_END logic is run */ |
3002 | | } |
3003 | | else |
3004 | | #endif |
3005 | 0 | { |
3006 | | /* Reset state */ |
3007 | 0 | ret = 0; |
3008 | 0 | ssl->decrypt.state = CIPHER_STATE_BEGIN; |
3009 | 0 | } |
3010 | |
|
3011 | 0 | (void)output; |
3012 | 0 | (void)input; |
3013 | 0 | (void)sz; |
3014 | 0 | (void)dataSz; |
3015 | 0 | (void)macSz; |
3016 | 0 | (void)nonceSz; |
3017 | |
|
3018 | 0 | switch (ssl->decrypt.state) { |
3019 | 0 | case CIPHER_STATE_BEGIN: |
3020 | 0 | { |
3021 | | #ifdef WOLFSSL_DEBUG_TLS |
3022 | | WOLFSSL_MSG("Data to decrypt"); |
3023 | | WOLFSSL_BUFFER(input, dataSz); |
3024 | | WOLFSSL_MSG("Additional Authentication Data"); |
3025 | | WOLFSSL_BUFFER(aad, aadSz); |
3026 | | WOLFSSL_MSG("Authentication tag"); |
3027 | | WOLFSSL_BUFFER(input + dataSz, macSz); |
3028 | | #endif |
3029 | |
|
3030 | 0 | #ifdef CIPHER_NONCE |
3031 | 0 | if (ssl->decrypt.nonce == NULL) { |
3032 | 0 | ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
3033 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
3034 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3035 | | if (ssl->decrypt.nonce != NULL) { |
3036 | | wc_MemZero_Add("DecryptTls13 nonce", ssl->decrypt.nonce, |
3037 | | ssl->specs.iv_size); |
3038 | | } |
3039 | | #endif |
3040 | 0 | } |
3041 | 0 | if (ssl->decrypt.nonce == NULL) |
3042 | 0 | return MEMORY_E; |
3043 | | |
3044 | 0 | BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV, |
3045 | 0 | ssl->specs.iv_size, PEER_ORDER); |
3046 | 0 | #endif |
3047 | | |
3048 | | /* Advance state and proceed */ |
3049 | 0 | ssl->decrypt.state = CIPHER_STATE_DO; |
3050 | 0 | } |
3051 | 0 | FALL_THROUGH; |
3052 | |
|
3053 | 0 | case CIPHER_STATE_DO: |
3054 | 0 | { |
3055 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
3056 | 0 | #ifdef BUILD_AESGCM |
3057 | 0 | case wolfssl_aes_gcm: |
3058 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3059 | | /* initialize event */ |
3060 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3061 | | WC_ASYNC_FLAG_NONE); |
3062 | | if (ret != 0) |
3063 | | break; |
3064 | | #endif |
3065 | |
|
3066 | 0 | nonceSz = AESGCM_NONCE_SZ; |
3067 | |
|
3068 | | #if defined(HAVE_PK_CALLBACKS) |
3069 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3070 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3071 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3072 | | output, input, dataSz, |
3073 | | ssl->decrypt.nonce, nonceSz, |
3074 | | (byte *)(input + dataSz), macSz, |
3075 | | aad, aadSz); |
3076 | | } |
3077 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3078 | | #endif |
3079 | 0 | { |
3080 | |
|
3081 | 0 | ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, |
3082 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, |
3083 | 0 | input + dataSz, macSz, aad, aadSz); |
3084 | |
|
3085 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3086 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3087 | | ret = wolfSSL_AsyncPush(ssl, |
3088 | | &ssl->decrypt.aes->asyncDev); |
3089 | | } |
3090 | | #endif |
3091 | |
|
3092 | 0 | } |
3093 | 0 | break; |
3094 | 0 | #endif |
3095 | | |
3096 | 0 | #ifdef HAVE_AESCCM |
3097 | 0 | case wolfssl_aes_ccm: |
3098 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3099 | | /* initialize event */ |
3100 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3101 | | WC_ASYNC_FLAG_NONE); |
3102 | | if (ret != 0) |
3103 | | break; |
3104 | | #endif |
3105 | |
|
3106 | 0 | nonceSz = AESCCM_NONCE_SZ; |
3107 | | #if defined(HAVE_PK_CALLBACKS) |
3108 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3109 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3110 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3111 | | output, input, dataSz, |
3112 | | ssl->decrypt.nonce, nonceSz, |
3113 | | (byte *)(input + dataSz), macSz, |
3114 | | aad, aadSz); |
3115 | | } |
3116 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3117 | | #endif |
3118 | 0 | { |
3119 | 0 | ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, |
3120 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, |
3121 | 0 | input + dataSz, macSz, aad, aadSz); |
3122 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3123 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3124 | | ret = wolfSSL_AsyncPush(ssl, |
3125 | | &ssl->decrypt.aes->asyncDev); |
3126 | | } |
3127 | | #endif |
3128 | 0 | } |
3129 | 0 | break; |
3130 | 0 | #endif |
3131 | | |
3132 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
3133 | 0 | case wolfssl_chacha: |
3134 | 0 | ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz, |
3135 | 0 | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3136 | 0 | break; |
3137 | 0 | #endif |
3138 | | |
3139 | 0 | #ifdef WOLFSSL_SM4_GCM |
3140 | 0 | case wolfssl_sm4_gcm: |
3141 | 0 | nonceSz = SM4_GCM_NONCE_SZ; |
3142 | 0 | ret = wc_Sm4GcmDecrypt(ssl->decrypt.sm4, output, input, |
3143 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3144 | 0 | macSz, aad, aadSz); |
3145 | 0 | break; |
3146 | 0 | #endif |
3147 | | |
3148 | 0 | #ifdef WOLFSSL_SM4_CCM |
3149 | 0 | case wolfssl_sm4_ccm: |
3150 | 0 | nonceSz = SM4_CCM_NONCE_SZ; |
3151 | 0 | ret = wc_Sm4CcmDecrypt(ssl->decrypt.sm4, output, input, |
3152 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3153 | 0 | macSz, aad, aadSz); |
3154 | 0 | break; |
3155 | 0 | #endif |
3156 | | |
3157 | | #ifdef HAVE_NULL_CIPHER |
3158 | | case wolfssl_cipher_null: |
3159 | | ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz, |
3160 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3161 | | break; |
3162 | | #endif |
3163 | 0 | default: |
3164 | 0 | WOLFSSL_MSG("wolfSSL Decrypt programming error"); |
3165 | 0 | return DECRYPT_ERROR; |
3166 | 0 | } |
3167 | | |
3168 | | /* Advance state */ |
3169 | 0 | ssl->decrypt.state = CIPHER_STATE_END; |
3170 | |
|
3171 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3172 | | /* If pending, leave now */ |
3173 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3174 | | return ret; |
3175 | | } |
3176 | | #endif |
3177 | 0 | } |
3178 | 0 | FALL_THROUGH; |
3179 | |
|
3180 | 0 | case CIPHER_STATE_END: |
3181 | 0 | { |
3182 | | #ifdef WOLFSSL_DEBUG_TLS |
3183 | | #ifdef CIPHER_NONCE |
3184 | | WOLFSSL_MSG("Nonce"); |
3185 | | WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size); |
3186 | | #endif |
3187 | | WOLFSSL_MSG("Decrypted data"); |
3188 | | WOLFSSL_BUFFER(output, dataSz); |
3189 | | #endif |
3190 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3191 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
3192 | | (ret == 0)) { |
3193 | | wc_MemZero_Add("TLS 1.3 Decrypted data", output, sz); |
3194 | | } |
3195 | | #endif |
3196 | |
|
3197 | 0 | #ifdef CIPHER_NONCE |
3198 | 0 | ForceZero(ssl->decrypt.nonce, ssl->specs.iv_size); |
3199 | 0 | #endif |
3200 | |
|
3201 | 0 | break; |
3202 | 0 | } |
3203 | | |
3204 | 0 | default: |
3205 | 0 | break; |
3206 | 0 | } |
3207 | | |
3208 | 0 | if (ret < 0) { |
3209 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
3210 | 0 | } |
3211 | |
|
3212 | 0 | return ret; |
3213 | 0 | } |
3214 | | |
3215 | | /* Persistable BuildTls13Message arguments */ |
3216 | | typedef struct BuildMsg13Args { |
3217 | | word32 sz; |
3218 | | word32 idx; |
3219 | | word32 headerSz; |
3220 | | word16 size; |
3221 | | word32 paddingSz; |
3222 | | } BuildMsg13Args; |
3223 | | |
3224 | | static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs) |
3225 | 1.52k | { |
3226 | 1.52k | BuildMsg13Args* args = (BuildMsg13Args*)pArgs; |
3227 | | |
3228 | 1.52k | (void)ssl; |
3229 | 1.52k | (void)args; |
3230 | | |
3231 | | /* no allocations in BuildTls13Message */ |
3232 | 1.52k | } |
3233 | | |
3234 | | /* Build SSL Message, encrypted. |
3235 | | * TLS v1.3 encryption is AEAD only. |
3236 | | * |
3237 | | * ssl The SSL/TLS object. |
3238 | | * output The buffer to write record message to. |
3239 | | * outSz Size of the buffer being written into. |
3240 | | * input The record data to encrypt (excluding record header). |
3241 | | * inSz The size of the record data. |
3242 | | * type The recorder header content type. |
3243 | | * hashOutput Whether to hash the unencrypted record data. |
3244 | | * sizeOnly Only want the size of the record message. |
3245 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
3246 | | * returns the size of the encrypted record message or negative value on error. |
3247 | | */ |
3248 | | int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input, |
3249 | | int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay) |
3250 | 0 | { |
3251 | 0 | int ret; |
3252 | 0 | BuildMsg13Args* args; |
3253 | 0 | BuildMsg13Args lcl_args; |
3254 | |
|
3255 | 0 | WOLFSSL_ENTER("BuildTls13Message"); |
3256 | |
|
3257 | 0 | if (ssl == NULL) { |
3258 | 0 | return BAD_FUNC_ARG; |
3259 | 0 | } |
3260 | | |
3261 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3262 | | ret = WC_NO_PENDING_E; |
3263 | | if (asyncOkay) { |
3264 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
3265 | | |
3266 | | if (ssl->async == NULL) { |
3267 | | ssl->async = (struct WOLFSSL_ASYNC*) |
3268 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
3269 | | DYNAMIC_TYPE_ASYNC); |
3270 | | if (ssl->async == NULL) |
3271 | | return MEMORY_E; |
3272 | | } |
3273 | | args = (BuildMsg13Args*)ssl->async->args; |
3274 | | |
3275 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState); |
3276 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3277 | | /* Check for error */ |
3278 | | if (ret < 0) |
3279 | | goto exit_buildmsg; |
3280 | | } |
3281 | | } |
3282 | | else |
3283 | | #endif |
3284 | 0 | { |
3285 | 0 | args = &lcl_args; |
3286 | 0 | } |
3287 | | |
3288 | | /* Reset state */ |
3289 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3290 | | if (ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) |
3291 | | #endif |
3292 | 0 | { |
3293 | | /* Note: these hit ssl->options even for a sizeOnly probe, where every |
3294 | | * other result goes to lcl_args, so a probe destroys the resume point |
3295 | | * of a suspended asynchronous build. wolfssl_local_GetRecordSize() is |
3296 | | * the only sizeOnly caller and restores them; a new one must too. */ |
3297 | 0 | ret = 0; |
3298 | 0 | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3299 | 0 | XMEMSET(args, 0, sizeof(BuildMsg13Args)); |
3300 | |
|
3301 | 0 | args->headerSz = RECORD_HEADER_SZ; |
3302 | | #ifdef WOLFSSL_DTLS13 |
3303 | | if (ssl->options.dtls) |
3304 | | args->headerSz = Dtls13GetRlHeaderLength(ssl, 1); |
3305 | | #endif /* WOLFSSL_DTLS13 */ |
3306 | |
|
3307 | 0 | args->sz = args->headerSz + (word32)inSz; |
3308 | 0 | args->idx = args->headerSz; |
3309 | |
|
3310 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3311 | | if (asyncOkay) |
3312 | | ssl->async->freeArgs = FreeBuildMsg13Args; |
3313 | | #endif |
3314 | 0 | } |
3315 | |
|
3316 | 0 | switch (ssl->options.buildMsgState) { |
3317 | 0 | case BUILD_MSG_BEGIN: |
3318 | 0 | { |
3319 | | /* catch mistaken sizeOnly parameter */ |
3320 | 0 | if (sizeOnly) { |
3321 | 0 | if (output || input) { |
3322 | 0 | WOLFSSL_MSG("BuildTls13Message with sizeOnly " |
3323 | 0 | "doesn't need input or output"); |
3324 | 0 | return BAD_FUNC_ARG; |
3325 | 0 | } |
3326 | 0 | } |
3327 | 0 | else if (output == NULL || input == NULL) { |
3328 | 0 | return BAD_FUNC_ARG; |
3329 | 0 | } |
3330 | | |
3331 | | /* Record layer content type at the end of record data. */ |
3332 | 0 | args->sz++; |
3333 | | /* Authentication data at the end. */ |
3334 | 0 | args->sz += ssl->specs.aead_mac_size; |
3335 | | #ifdef WOLFSSL_DTLS13 |
3336 | | /* Pad to minimum length */ |
3337 | | if (ssl->options.dtls && |
3338 | | args->sz < (word32)Dtls13MinimumRecordLength(ssl)) { |
3339 | | args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz; |
3340 | | args->sz = Dtls13MinimumRecordLength(ssl); |
3341 | | } |
3342 | | #endif |
3343 | 0 | if (sizeOnly) |
3344 | 0 | return (int)args->sz; |
3345 | | |
3346 | 0 | if (args->sz > (word32)outSz) { |
3347 | 0 | WOLFSSL_MSG("Oops, want to write past output buffer size"); |
3348 | 0 | return BUFFER_E; |
3349 | 0 | } |
3350 | | |
3351 | | /* Record data length. */ |
3352 | 0 | args->size = (word16)(args->sz - args->headerSz); |
3353 | | /* Write/update the record header with the new size. |
3354 | | * Always have the content type as application data for encrypted |
3355 | | * messages in TLS v1.3. |
3356 | | */ |
3357 | |
|
3358 | 0 | if (ssl->options.dtls) { |
3359 | | #ifdef WOLFSSL_DTLS13 |
3360 | | Dtls13RlAddCiphertextHeader(ssl, output, args->size); |
3361 | | #endif /* WOLFSSL_DTLS13 */ |
3362 | 0 | } |
3363 | 0 | else { |
3364 | 0 | AddTls13RecordHeader(output, args->size, application_data, ssl); |
3365 | 0 | } |
3366 | | |
3367 | | /* TLS v1.3 can do in place encryption. */ |
3368 | 0 | if (input != output + args->idx) |
3369 | 0 | XMEMCPY(output + args->idx, input, (size_t)inSz); |
3370 | 0 | args->idx += (word32)inSz; |
3371 | |
|
3372 | 0 | ssl->options.buildMsgState = BUILD_MSG_HASH; |
3373 | 0 | } |
3374 | 0 | FALL_THROUGH; |
3375 | |
|
3376 | 0 | case BUILD_MSG_HASH: |
3377 | 0 | { |
3378 | 0 | if (hashOutput) { |
3379 | 0 | ret = HashOutput(ssl, output, (int)args->headerSz + inSz, 0); |
3380 | 0 | if (ret != 0) |
3381 | 0 | goto exit_buildmsg; |
3382 | 0 | } |
3383 | | |
3384 | | /* The real record content type goes at the end of the data. */ |
3385 | 0 | output[args->idx++] = (byte)type; |
3386 | | /* Double check that any necessary padding is zero'd out */ |
3387 | 0 | XMEMSET(output + args->idx, 0, args->paddingSz); |
3388 | 0 | args->idx += args->paddingSz; |
3389 | |
|
3390 | 0 | ssl->options.buildMsgState = BUILD_MSG_ENCRYPT; |
3391 | 0 | } |
3392 | 0 | FALL_THROUGH; |
3393 | |
|
3394 | 0 | case BUILD_MSG_ENCRYPT: |
3395 | 0 | { |
3396 | | #ifdef WOLFSSL_QUIC |
3397 | | if (WOLFSSL_IS_QUIC(ssl)) { |
3398 | | /* QUIC does not use encryption of the TLS Record Layer. |
3399 | | * Return the original length + added headers |
3400 | | * and restore it in the record header. */ |
3401 | | AddTls13RecordHeader(output, (word32)inSz, (byte)type, ssl); |
3402 | | ret = (int)args->headerSz + inSz; |
3403 | | goto exit_buildmsg; |
3404 | | } |
3405 | | #endif |
3406 | | #ifdef ATOMIC_USER |
3407 | | if (ssl->ctx->MacEncryptCb) { |
3408 | | /* User Record Layer Callback handling */ |
3409 | | byte* mac = output + args->idx; |
3410 | | output += args->headerSz; |
3411 | | |
3412 | | ret = ssl->ctx->MacEncryptCb(ssl, mac, output, (unsigned int)inSz, (byte)type, 0, |
3413 | | output, output, args->size, ssl->MacEncryptCtx); |
3414 | | } |
3415 | | else |
3416 | | #endif |
3417 | 0 | { |
3418 | 0 | const byte* aad = output; |
3419 | 0 | output += args->headerSz; |
3420 | 0 | ret = EncryptTls13(ssl, output, output, args->size, aad, |
3421 | 0 | (word16)args->headerSz, asyncOkay); |
3422 | 0 | if (ret != 0) { |
3423 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3424 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
3425 | | #endif |
3426 | 0 | { |
3427 | | /* Zeroize plaintext. */ |
3428 | 0 | ForceZero(output, args->size); |
3429 | 0 | } |
3430 | 0 | } |
3431 | | #ifdef WOLFSSL_DTLS13 |
3432 | | if (ret == 0 && ssl->options.dtls) { |
3433 | | /* AAD points to the header. Reuse the variable */ |
3434 | | ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, |
3435 | | (word16)args->sz); |
3436 | | } |
3437 | | #endif /* WOLFSSL_DTLS13 */ |
3438 | 0 | } |
3439 | 0 | break; |
3440 | 0 | } |
3441 | | |
3442 | 0 | default: |
3443 | 0 | break; |
3444 | 0 | } |
3445 | | |
3446 | 0 | exit_buildmsg: |
3447 | |
|
3448 | 0 | WOLFSSL_LEAVE("BuildTls13Message", ret); |
3449 | |
|
3450 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3451 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3452 | | return ret; |
3453 | | } |
3454 | | #endif |
3455 | | |
3456 | | /* make sure build message state is reset */ |
3457 | 0 | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3458 | | |
3459 | | /* return sz on success */ |
3460 | 0 | if (ret == 0) { |
3461 | 0 | ret = (int)args->sz; |
3462 | 0 | } |
3463 | 0 | else { |
3464 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
3465 | 0 | } |
3466 | | |
3467 | | /* Final cleanup */ |
3468 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3469 | | if (asyncOkay) |
3470 | | FreeAsyncCtx(ssl, 0); |
3471 | | else |
3472 | | #endif |
3473 | 0 | FreeBuildMsg13Args(ssl, args); |
3474 | |
|
3475 | 0 | return ret; |
3476 | 0 | } |
3477 | | |
3478 | | #if !defined(NO_WOLFSSL_CLIENT) || (!defined(NO_WOLFSSL_SERVER) && \ |
3479 | | (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
3480 | | (defined(WOLFSSL_PSK_ONE_ID) || defined(WOLFSSL_PRIORITIZE_PSK))) |
3481 | | /* Find the cipher suite in the suites set in the SSL. |
3482 | | * |
3483 | | * ssl SSL/TLS object. |
3484 | | * suite Cipher suite to look for. |
3485 | | * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise. |
3486 | | */ |
3487 | | int FindSuiteSSL(const WOLFSSL* ssl, byte* suite) |
3488 | 197 | { |
3489 | 197 | word16 i; |
3490 | 197 | const Suites* suites = WOLFSSL_SUITES(ssl); |
3491 | | |
3492 | 333 | for (i = 0; i < suites->suiteSz; i += 2) { |
3493 | 333 | if (suites->suites[i+0] == suite[0] && |
3494 | 323 | suites->suites[i+1] == suite[1]) { |
3495 | 197 | return 1; |
3496 | 197 | } |
3497 | 333 | } |
3498 | | |
3499 | 0 | return 0; |
3500 | 197 | } |
3501 | | #endif |
3502 | | |
3503 | | #ifndef NO_PSK |
3504 | | /* Get the MAC algorithm for the TLS 1.3 cipher suite. |
3505 | | * |
3506 | | * @param [in] suite. |
3507 | | * @return A value from wc_MACAlgorithm enumeration. |
3508 | | */ |
3509 | | byte SuiteMac(const byte* suite) |
3510 | | { |
3511 | | byte mac = no_mac; |
3512 | | |
3513 | | if (suite[0] == TLS13_BYTE) { |
3514 | | switch (suite[1]) { |
3515 | | #ifdef BUILD_TLS_AES_128_GCM_SHA256 |
3516 | | case TLS_AES_128_GCM_SHA256: |
3517 | | mac = sha256_mac; |
3518 | | break; |
3519 | | #endif |
3520 | | #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256 |
3521 | | case TLS_CHACHA20_POLY1305_SHA256: |
3522 | | mac = sha256_mac; |
3523 | | break; |
3524 | | #endif |
3525 | | #ifdef BUILD_TLS_AES_128_CCM_SHA256 |
3526 | | case TLS_AES_128_CCM_SHA256: |
3527 | | mac = sha256_mac; |
3528 | | break; |
3529 | | #endif |
3530 | | #ifdef BUILD_TLS_AES_128_CCM_8_SHA256 |
3531 | | case TLS_AES_128_CCM_8_SHA256: |
3532 | | mac = sha256_mac; |
3533 | | break; |
3534 | | #endif |
3535 | | #ifdef BUILD_TLS_AES_256_GCM_SHA384 |
3536 | | case TLS_AES_256_GCM_SHA384: |
3537 | | mac = sha384_mac; |
3538 | | break; |
3539 | | #endif |
3540 | | default: |
3541 | | break; |
3542 | | } |
3543 | | } |
3544 | | #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \ |
3545 | | defined(WOLFSSL_SM3) |
3546 | | else if (suite[0] == CIPHER_BYTE) { |
3547 | | switch (suite[1]) { |
3548 | | #ifdef BUILD_TLS_SM4_GCM_SM3 |
3549 | | case TLS_SM4_GCM_SM3: |
3550 | | mac = sm3_mac; |
3551 | | break; |
3552 | | #endif |
3553 | | #ifdef BUILD_TLS_SM4_CCM_SM3 |
3554 | | case TLS_SM4_CCM_SM3: |
3555 | | mac = sm3_mac; |
3556 | | break; |
3557 | | #endif |
3558 | | default: |
3559 | | break; |
3560 | | } |
3561 | | } |
3562 | | #endif |
3563 | | #ifdef HAVE_NULL_CIPHER |
3564 | | else if (suite[0] == ECC_BYTE) { |
3565 | | switch (suite[1]) { |
3566 | | #ifdef BUILD_TLS_SHA256_SHA256 |
3567 | | case TLS_SHA256_SHA256: |
3568 | | mac = sha256_mac; |
3569 | | break; |
3570 | | #endif |
3571 | | #ifdef BUILD_TLS_SHA384_SHA384 |
3572 | | case TLS_SHA384_SHA384: |
3573 | | mac = sha384_mac; |
3574 | | break; |
3575 | | #endif |
3576 | | default: |
3577 | | break; |
3578 | | } |
3579 | | } |
3580 | | #endif |
3581 | | |
3582 | | return mac; |
3583 | | } |
3584 | | #endif |
3585 | | |
3586 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3587 | | /* Create Cookie extension using the hash of the first ClientHello. |
3588 | | * |
3589 | | * ssl SSL/TLS object. |
3590 | | * hash The hash data. |
3591 | | * hashSz The size of the hash data in bytes. |
3592 | | * returns 0 on success, otherwise failure. |
3593 | | */ |
3594 | | int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz, |
3595 | | TLSX** exts, byte cipherSuite0, byte cipherSuite) |
3596 | | { |
3597 | | int ret; |
3598 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
3599 | | WC_DECLARE_VAR(cookieHmac, Hmac, 1, ssl->heap); |
3600 | | byte cookieType = 0; |
3601 | | byte macSz = 0; |
3602 | | byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2]; |
3603 | | TLSX* ext; |
3604 | | word16 cookieSz = 0; |
3605 | | |
3606 | | if (hash == NULL || hashSz == 0) { |
3607 | | return BAD_FUNC_ARG; |
3608 | | } |
3609 | | |
3610 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
3611 | | ssl->buffers.tls13CookieSecret.length == 0) { |
3612 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
3613 | | return COOKIE_ERROR; |
3614 | | } |
3615 | | |
3616 | | /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */ |
3617 | | cookie[cookieSz++] = (byte)hashSz; |
3618 | | XMEMCPY(cookie + cookieSz, hash, hashSz); |
3619 | | cookieSz += hashSz; |
3620 | | cookie[cookieSz++] = cipherSuite0; |
3621 | | cookie[cookieSz++] = cipherSuite; |
3622 | | if ((ext = TLSX_Find(*exts, TLSX_KEY_SHARE)) != NULL) { |
3623 | | KeyShareEntry* kse = (KeyShareEntry*)ext->data; |
3624 | | if (kse == NULL) { |
3625 | | WOLFSSL_MSG("KeyShareEntry can't be empty when negotiating " |
3626 | | "parameters"); |
3627 | | return BAD_STATE_E; |
3628 | | } |
3629 | | c16toa(kse->group, cookie + cookieSz); |
3630 | | cookieSz += OPAQUE16_LEN; |
3631 | | } |
3632 | | |
3633 | | #ifndef NO_SHA256 |
3634 | | cookieType = WC_SHA256; |
3635 | | macSz = WC_SHA256_DIGEST_SIZE; |
3636 | | #elif defined(WOLFSSL_SHA384) |
3637 | | cookieType = WC_SHA384; |
3638 | | macSz = WC_SHA384_DIGEST_SIZE; |
3639 | | #elif defined(WOLFSSL_TLS13_SHA512) |
3640 | | cookieType = WC_SHA512; |
3641 | | macSz = WC_SHA512_DIGEST_SIZE; |
3642 | | #elif defined(WOLFSSL_SM3) |
3643 | | cookieType = WC_SM3; |
3644 | | macSz = WC_SM3_DIGEST_SIZE; |
3645 | | #else |
3646 | | #error "No digest to available to use with HMAC for cookies." |
3647 | | #endif /* NO_SHA */ |
3648 | | |
3649 | | WC_ALLOC_VAR_EX(cookieHmac, Hmac, 1, ssl->heap, DYNAMIC_TYPE_HMAC, |
3650 | | return MEMORY_E); |
3651 | | |
3652 | | ret = wc_HmacInit(cookieHmac, ssl->heap, ssl->devId); |
3653 | | if (ret == 0) { |
3654 | | ret = wc_HmacSetKey(cookieHmac, cookieType, |
3655 | | ssl->buffers.tls13CookieSecret.buffer, |
3656 | | ssl->buffers.tls13CookieSecret.length); |
3657 | | } |
3658 | | if (ret == 0) |
3659 | | ret = wc_HmacUpdate(cookieHmac, cookie, cookieSz); |
3660 | | #ifdef WOLFSSL_DTLS13 |
3661 | | /* Tie cookie to peer address */ |
3662 | | if (ret == 0) { |
3663 | | /* peerLock not necessary. Still in handshake phase. */ |
3664 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
3665 | | ret = wc_HmacUpdate(cookieHmac, |
3666 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
3667 | | ssl->buffers.dtlsCtx.peer.sz); |
3668 | | } |
3669 | | } |
3670 | | #endif |
3671 | | if (ret == 0) |
3672 | | ret = wc_HmacFinal(cookieHmac, mac); |
3673 | | |
3674 | | wc_HmacFree(cookieHmac); |
3675 | | WC_FREE_VAR_EX(cookieHmac, ssl->heap, DYNAMIC_TYPE_HMAC); |
3676 | | if (ret != 0) |
3677 | | return ret; |
3678 | | |
3679 | | /* The cookie data is the hash and the integrity check. */ |
3680 | | return TLSX_Cookie_Use(ssl, cookie, cookieSz, mac, macSz, 1, exts); |
3681 | | } |
3682 | | #endif |
3683 | | |
3684 | | #ifdef WOLFSSL_DTLS13 |
3685 | | #define HRR_MAX_HS_HEADER_SZ DTLS_HANDSHAKE_HEADER_SZ |
3686 | | #else |
3687 | | #define HRR_MAX_HS_HEADER_SZ HANDSHAKE_HEADER_SZ |
3688 | | #endif /* WOLFSSL_DTLS13 */ |
3689 | | |
3690 | | static int CreateCookie(const WOLFSSL* ssl, byte** hash, byte* hashSz, |
3691 | | Hashes* hashes, TLSX** exts) |
3692 | | { |
3693 | | int ret = 0; |
3694 | | |
3695 | | (void)exts; |
3696 | | |
3697 | | *hash = NULL; |
3698 | | switch (ssl->specs.mac_algorithm) { |
3699 | | #ifndef NO_SHA256 |
3700 | | case sha256_mac: |
3701 | | *hash = hashes->sha256; |
3702 | | break; |
3703 | | #endif |
3704 | | #ifdef WOLFSSL_SHA384 |
3705 | | case sha384_mac: |
3706 | | *hash = hashes->sha384; |
3707 | | break; |
3708 | | #endif |
3709 | | #ifdef WOLFSSL_TLS13_SHA512 |
3710 | | case sha512_mac: |
3711 | | *hash = hashes->sha512; |
3712 | | break; |
3713 | | #endif |
3714 | | #ifdef WOLFSSL_SM3 |
3715 | | case sm3_mac: |
3716 | | *hash = hashes->sm3; |
3717 | | break; |
3718 | | #endif |
3719 | | } |
3720 | | *hashSz = ssl->specs.hash_size; |
3721 | | |
3722 | | /* check hash */ |
3723 | | if (*hash == NULL && *hashSz > 0) |
3724 | | return BAD_FUNC_ARG; |
3725 | | |
3726 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3727 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3728 | | ret = CreateCookieExt(ssl, *hash, *hashSz, exts, |
3729 | | ssl->options.cipherSuite0, ssl->options.cipherSuite); |
3730 | | #endif |
3731 | | return ret; |
3732 | | } |
3733 | | |
3734 | | /* Restart the handshake hash with a hash of the previous messages. |
3735 | | * |
3736 | | * ssl The SSL/TLS object. |
3737 | | * returns 0 on success, otherwise failure. |
3738 | | */ |
3739 | | int RestartHandshakeHash(WOLFSSL* ssl) |
3740 | 0 | { |
3741 | 0 | int ret; |
3742 | 0 | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
3743 | 0 | Hashes hashes; |
3744 | 0 | byte* hash = NULL; |
3745 | 0 | byte hashSz = 0; |
3746 | |
|
3747 | 0 | ret = BuildCertHashes(ssl, &hashes); |
3748 | 0 | if (ret != 0) |
3749 | 0 | return ret; |
3750 | 0 | ret = CreateCookie(ssl, &hash, &hashSz, &hashes, &ssl->extensions); |
3751 | 0 | if (ret != 0) |
3752 | 0 | return ret; |
3753 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3754 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3755 | | return 0; |
3756 | | #endif |
3757 | | |
3758 | 0 | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
3759 | |
|
3760 | | #ifdef WOLFSSL_DEBUG_TLS |
3761 | | WOLFSSL_MSG("Restart Hash"); |
3762 | | WOLFSSL_BUFFER(hash, hashSz); |
3763 | | #endif |
3764 | |
|
3765 | 0 | ret = InitHandshakeHashes(ssl); |
3766 | 0 | if (ret != 0) |
3767 | 0 | return ret; |
3768 | 0 | ret = HashRaw(ssl, header, sizeof(header)); |
3769 | 0 | if (ret != 0) |
3770 | 0 | return ret; |
3771 | 0 | return HashRaw(ssl, hash, hashSz); |
3772 | 0 | } |
3773 | | |
3774 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
3775 | | /* The value in the random field of a ServerHello to indicate |
3776 | | * HelloRetryRequest. |
3777 | | */ |
3778 | | static byte helloRetryRequestRandom[] = { |
3779 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
3780 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
3781 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
3782 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
3783 | | }; |
3784 | | #endif |
3785 | | |
3786 | | #ifdef HAVE_ECH |
3787 | | /* returns the index of the first supported cipher suite, -1 if none */ |
3788 | | int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config) |
3789 | | { |
3790 | | int i = 0; |
3791 | | |
3792 | | if (!wc_HpkeKemIsSupported(config->kemId)) { |
3793 | | WOLFSSL_MSG("ECH config: KEM not supported"); |
3794 | | return WOLFSSL_FATAL_ERROR; |
3795 | | } |
3796 | | |
3797 | | for (i = 0; i < config->numCipherSuites; i++) { |
3798 | | if (wc_HpkeKdfIsSupported(config->cipherSuites[i].kdfId) && |
3799 | | wc_HpkeAeadIsSupported(config->cipherSuites[i].aeadId)) { |
3800 | | return i; |
3801 | | } |
3802 | | } |
3803 | | |
3804 | | WOLFSSL_MSG("ECH config: KDF or AEAD not supported"); |
3805 | | return WOLFSSL_FATAL_ERROR; |
3806 | | } |
3807 | | |
3808 | | /* Hash the inner client hello, initializing the hsHashesEch field if needed. |
3809 | | * This should receive the client hello without outer_extensions 'encoding' |
3810 | | * |
3811 | | * ssl SSL/TLS object. |
3812 | | * ech ECH object. |
3813 | | * returns 0 on success and otherwise failure. |
3814 | | */ |
3815 | | static int EchHashHelloInner(WOLFSSL* ssl, WOLFSSL_ECH* ech) |
3816 | | { |
3817 | | int ret = 0; |
3818 | | int headerSz; |
3819 | | word32 realSz; |
3820 | | HS_Hashes* tmpHashes; |
3821 | | #ifndef NO_WOLFSSL_CLIENT |
3822 | | byte falseHeader[HRR_MAX_HS_HEADER_SZ]; |
3823 | | #endif |
3824 | | |
3825 | | if (ssl == NULL || ech == NULL) { |
3826 | | return BAD_FUNC_ARG; |
3827 | | } |
3828 | | |
3829 | | #ifdef WOLFSSL_DTLS13 |
3830 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3831 | | HANDSHAKE_HEADER_SZ; |
3832 | | #else |
3833 | | headerSz = HANDSHAKE_HEADER_SZ; |
3834 | | #endif |
3835 | | |
3836 | | realSz = ech->innerClientHelloLen; |
3837 | | |
3838 | | tmpHashes = ssl->hsHashes; |
3839 | | |
3840 | | ssl->hsHashes = ssl->hsHashesEch; |
3841 | | if (ssl->hsHashes == NULL) { |
3842 | | ret = InitHandshakeHashes(ssl); |
3843 | | if (ret == 0) { |
3844 | | ssl->hsHashesEch = ssl->hsHashes; |
3845 | | } |
3846 | | } |
3847 | | |
3848 | | if (ret == 0) { |
3849 | | #ifndef NO_WOLFSSL_CLIENT |
3850 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
3851 | | /* client-side: innerClientHello contains body only */ |
3852 | | AddTls13HandShakeHeader(falseHeader, realSz, 0, 0, client_hello, |
3853 | | ssl); |
3854 | | ret = HashRaw(ssl, falseHeader, headerSz); |
3855 | | if (ret == 0) { |
3856 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
3857 | | } |
3858 | | } |
3859 | | #endif |
3860 | | #ifndef NO_WOLFSSL_SERVER |
3861 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
3862 | | /* server-side: innerClientHello contains header + body */ |
3863 | | ret = HashRaw(ssl, ech->innerClientHello, headerSz + realSz); |
3864 | | } |
3865 | | #endif |
3866 | | } |
3867 | | |
3868 | | ssl->hsHashes = tmpHashes; |
3869 | | return ret; |
3870 | | } |
3871 | | |
3872 | | /* Calculate the 8 ECH confirmation bytes. |
3873 | | * |
3874 | | * ssl SSL/TLS object. |
3875 | | * label Ascii string describing ECH acceptance or rejection. |
3876 | | * labelSz Length of label excluding NULL character. |
3877 | | * input The buffer to calculate confirmation off of. |
3878 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
3879 | | * helloSz Size of hello message. |
3880 | | * isHrr Whether message is a HelloRetryRequest or not. |
3881 | | * acceptExpanded An 8 byte array to store calculated confirmation to. |
3882 | | * returns 0 on success and otherwise failure. |
3883 | | */ |
3884 | | static int EchCalcAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
3885 | | const byte* input, int acceptOffset, int helloSz, byte isHrr, |
3886 | | byte* acceptExpanded) |
3887 | | { |
3888 | | int ret = 0; |
3889 | | int digestType = 0; |
3890 | | int digestSize = 0; |
3891 | | int hashSz = 0; |
3892 | | int headerSz; |
3893 | | HS_Hashes* tmpHashes; |
3894 | | HS_Hashes* acceptHash = NULL; |
3895 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
3896 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
3897 | | byte clientHelloInnerHash[WC_MAX_DIGEST_SIZE]; |
3898 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
3899 | | byte messageHashHeader[HRR_MAX_HS_HEADER_SZ]; |
3900 | | |
3901 | | XMEMSET(zeros, 0, sizeof(zeros)); |
3902 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
3903 | | XMEMSET(clientHelloInnerHash, 0, sizeof(clientHelloInnerHash)); |
3904 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
3905 | | |
3906 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3907 | | wc_MemZero_Add("ECH PRK", expandLabelPrk, sizeof(expandLabelPrk)); |
3908 | | #endif |
3909 | | |
3910 | | tmpHashes = ssl->hsHashes; |
3911 | | ssl->hsHashes = ssl->hsHashesEch; |
3912 | | |
3913 | | #ifdef WOLFSSL_DTLS13 |
3914 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3915 | | HANDSHAKE_HEADER_SZ; |
3916 | | #else |
3917 | | headerSz = HANDSHAKE_HEADER_SZ; |
3918 | | #endif |
3919 | | |
3920 | | if (isHrr) { |
3921 | | /* the transcript hash of ClientHelloInner1 */ |
3922 | | ret = GetMsgHash(ssl, clientHelloInnerHash); |
3923 | | if (ret > 0) { |
3924 | | hashSz = ret; |
3925 | | ret = 0; |
3926 | | } |
3927 | | else if (ret == 0) { |
3928 | | ret = HASH_TYPE_E; |
3929 | | } |
3930 | | |
3931 | | /* restart ECH transcript hash, similar to RestartHandshakeHash but |
3932 | | * don't add a cookie */ |
3933 | | if (ret == 0) { |
3934 | | ret = InitHandshakeHashes(ssl); |
3935 | | ssl->hsHashesEch = ssl->hsHashes; |
3936 | | } |
3937 | | if (ret == 0) { |
3938 | | AddTls13HandShakeHeader(messageHashHeader, (word32)hashSz, 0, 0, |
3939 | | message_hash, ssl); |
3940 | | ret = HashRaw(ssl, messageHashHeader, headerSz); |
3941 | | } |
3942 | | if (ret == 0) { |
3943 | | ret = HashRaw(ssl, clientHelloInnerHash, (word32)hashSz); |
3944 | | } |
3945 | | } |
3946 | | |
3947 | | /* hash with zeros for confirmation computation */ |
3948 | | if (ret == 0) { |
3949 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashesEch, &acceptHash); |
3950 | | } |
3951 | | if (ret == 0) { |
3952 | | ssl->hsHashes = acceptHash; |
3953 | | ret = HashRaw(ssl, input, acceptOffset); |
3954 | | } |
3955 | | if (ret == 0) { |
3956 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
3957 | | } |
3958 | | if (ret == 0) { |
3959 | | ret = HashRaw(ssl, input + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
3960 | | helloSz + headerSz - (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
3961 | | } |
3962 | | |
3963 | | /* get the modified transcript hash */ |
3964 | | if (ret == 0) { |
3965 | | ret = GetMsgHash(ssl, transcriptEchConf); |
3966 | | if (ret > 0) { |
3967 | | ret = 0; |
3968 | | } |
3969 | | else if (ret == 0) { |
3970 | | ret = HASH_TYPE_E; |
3971 | | } |
3972 | | } |
3973 | | |
3974 | | /* pick the right type and size based on mac_algorithm */ |
3975 | | if (ret == 0) { |
3976 | | switch (ssl->specs.mac_algorithm) { |
3977 | | #ifndef NO_SHA256 |
3978 | | case sha256_mac: |
3979 | | digestType = WC_SHA256; |
3980 | | digestSize = WC_SHA256_DIGEST_SIZE; |
3981 | | break; |
3982 | | #endif /* !NO_SHA256 */ |
3983 | | #ifdef WOLFSSL_SHA384 |
3984 | | case sha384_mac: |
3985 | | digestType = WC_SHA384; |
3986 | | digestSize = WC_SHA384_DIGEST_SIZE; |
3987 | | break; |
3988 | | #endif /* WOLFSSL_SHA384 */ |
3989 | | #ifdef WOLFSSL_TLS13_SHA512 |
3990 | | case sha512_mac: |
3991 | | digestType = WC_SHA512; |
3992 | | digestSize = WC_SHA512_DIGEST_SIZE; |
3993 | | break; |
3994 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
3995 | | #ifdef WOLFSSL_SM3 |
3996 | | case sm3_mac: |
3997 | | digestType = WC_SM3; |
3998 | | digestSize = WC_SM3_DIGEST_SIZE; |
3999 | | break; |
4000 | | #endif /* WOLFSSL_SM3 */ |
4001 | | default: |
4002 | | ret = WOLFSSL_FATAL_ERROR; |
4003 | | break; |
4004 | | } |
4005 | | } |
4006 | | |
4007 | | /* extract clientRandomInner with a key of all zeros */ |
4008 | | if (ret == 0) { |
4009 | | PRIVATE_KEY_UNLOCK(); |
4010 | | #if !defined(HAVE_FIPS) || \ |
4011 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
4012 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
4013 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk, |
4014 | | ssl->heap, ssl->devId); |
4015 | | #else |
4016 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4017 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk); |
4018 | | #endif |
4019 | | PRIVATE_KEY_LOCK(); |
4020 | | } |
4021 | | |
4022 | | /* tls expand with the confirmation label */ |
4023 | | if (ret == 0) { |
4024 | | PRIVATE_KEY_UNLOCK(); |
4025 | | #ifdef WOLFSSL_DTLS13 |
4026 | | if (ssl->options.dtls) { |
4027 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4028 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4029 | | dtls13ProtocolLabel, DTLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4030 | | transcriptEchConf, (word32)digestSize, digestType, |
4031 | | WOLFSSL_SERVER_END); |
4032 | | } |
4033 | | else |
4034 | | #endif |
4035 | | { |
4036 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4037 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4038 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4039 | | transcriptEchConf, (word32)digestSize, digestType, |
4040 | | WOLFSSL_SERVER_END); |
4041 | | } |
4042 | | PRIVATE_KEY_LOCK(); |
4043 | | } |
4044 | | |
4045 | | if (acceptHash != NULL) { |
4046 | | ssl->hsHashes = acceptHash; |
4047 | | FreeHandshakeHashes(ssl); |
4048 | | } |
4049 | | |
4050 | | ssl->hsHashes = tmpHashes; |
4051 | | ForceZero(expandLabelPrk, sizeof(expandLabelPrk)); |
4052 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4053 | | wc_MemZero_Check(expandLabelPrk, sizeof(expandLabelPrk)); |
4054 | | #endif |
4055 | | return ret; |
4056 | | } |
4057 | | #endif |
4058 | | |
4059 | | #ifndef NO_WOLFSSL_CLIENT |
4060 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4061 | | #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_PSK_ONE_ID) && \ |
4062 | | !defined(NO_PSK) |
4063 | | /** |
4064 | | * convert mac algorithm to WOLFSSL_EVP_MD |
4065 | | * @param mac_alg mac algorithm |
4066 | | * @return const WOLFSSL_EVP_MD on successful, otherwise NULL |
4067 | | */ |
4068 | | static const WOLFSSL_EVP_MD* ssl_handshake_md(const byte mac_alg) |
4069 | | { |
4070 | | switch(mac_alg) { |
4071 | | case no_mac: |
4072 | | return NULL; |
4073 | | #ifndef NO_MD5 |
4074 | | case md5_mac: |
4075 | | return wolfSSL_EVP_md5(); |
4076 | | #endif |
4077 | | #ifndef NO_SHA |
4078 | | case sha_mac: |
4079 | | return wolfSSL_EVP_sha1(); |
4080 | | #endif |
4081 | | #ifdef WOLFSSL_SHA224 |
4082 | | case sha224_mac: |
4083 | | return wolfSSL_EVP_sha224(); |
4084 | | #endif |
4085 | | case sha256_mac: |
4086 | | return wolfSSL_EVP_sha256(); |
4087 | | #ifdef WOLFSSL_SHA384 |
4088 | | case sha384_mac: |
4089 | | return wolfSSL_EVP_sha384(); |
4090 | | #endif |
4091 | | #ifdef WOLFSSL_SHA512 |
4092 | | case sha512_mac: |
4093 | | return wolfSSL_EVP_sha512(); |
4094 | | #endif |
4095 | | case rmd_mac: |
4096 | | case blake2b_mac: |
4097 | | WOLFSSL_MSG("no suitable EVP_MD"); |
4098 | | return NULL; |
4099 | | default: |
4100 | | WOLFSSL_MSG("Unknown mac algorithm"); |
4101 | | return NULL; |
4102 | | } |
4103 | | } |
4104 | | #endif |
4105 | | /* Setup pre-shared key based on the details in the extension data. |
4106 | | * |
4107 | | * ssl SSL/TLS object. |
4108 | | * psk Pre-shared key extension data. |
4109 | | * clientHello Whether called from client_hello construction. |
4110 | | * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and |
4111 | | * other negative value on failure. |
4112 | | */ |
4113 | | static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello) |
4114 | | { |
4115 | | #if defined(HAVE_SESSION_TICKET) || !defined(WOLFSSL_PSK_ONE_ID) |
4116 | | int ret; |
4117 | | #endif |
4118 | | byte suite[2]; |
4119 | | |
4120 | | if (psk == NULL) |
4121 | | return BAD_FUNC_ARG; |
4122 | | |
4123 | | if (!HaveUniqueSessionObj(ssl)) { |
4124 | | WOLFSSL_MSG("Unable to have unique session object"); |
4125 | | WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR); |
4126 | | return MEMORY_ERROR; |
4127 | | } |
4128 | | |
4129 | | suite[0] = ssl->options.cipherSuite0; |
4130 | | suite[1] = ssl->options.cipherSuite; |
4131 | | |
4132 | | #ifdef HAVE_SESSION_TICKET |
4133 | | if (psk->resumption) { |
4134 | | if (clientHello) { |
4135 | | suite[0] = psk->cipherSuite0; |
4136 | | suite[1] = psk->cipherSuite; |
4137 | | |
4138 | | /* Ensure cipher suite is supported or changed suite to one with |
4139 | | * the same MAC algorithm. */ |
4140 | | if (!FindSuiteSSL(ssl, suite)) { |
4141 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4142 | | return PSK_KEY_ERROR; |
4143 | | } |
4144 | | |
4145 | | ssl->options.cipherSuite0 = suite[0]; |
4146 | | ssl->options.cipherSuite = suite[1]; |
4147 | | |
4148 | | /* Setting mac for binder and keys for deriving EarlyData. */ |
4149 | | ret = SetCipherSpecs(ssl); |
4150 | | if (ret != 0) |
4151 | | return ret; |
4152 | | } |
4153 | | |
4154 | | #ifdef WOLFSSL_EARLY_DATA |
4155 | | if (ssl->session->maxEarlyDataSz == 0) |
4156 | | ssl->earlyData = no_early_data; |
4157 | | #endif |
4158 | | /* Resumption PSK is master secret. */ |
4159 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
4160 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
4161 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
4162 | | return ret; |
4163 | | } |
4164 | | if (!clientHello) { |
4165 | | /* CLIENT: using secret in ticket for peer authentication. */ |
4166 | | ssl->options.peerAuthGood = 1; |
4167 | | } |
4168 | | } |
4169 | | #endif |
4170 | | #ifndef NO_PSK |
4171 | | if (!psk->resumption) { |
4172 | | /* Get the pre-shared key. */ |
4173 | | #ifndef WOLFSSL_PSK_ONE_ID |
4174 | | const char* cipherName = NULL; |
4175 | | #ifdef OPENSSL_EXTRA |
4176 | | WOLFSSL_SESSION* psksession = NULL; |
4177 | | #endif |
4178 | | |
4179 | | /* Set the client identity to use. */ |
4180 | | if (psk->identityLen > MAX_PSK_ID_LEN) |
4181 | | return PSK_KEY_ERROR; |
4182 | | XMEMSET(ssl->arrays->client_identity, 0, |
4183 | | sizeof(ssl->arrays->client_identity)); |
4184 | | XMEMCPY(ssl->arrays->client_identity, psk->identity, psk->identityLen); |
4185 | | |
4186 | | #ifdef WOLFSSL_DEBUG_TLS |
4187 | | WOLFSSL_MSG("PSK cipher suite:"); |
4188 | | WOLFSSL_MSG(GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4189 | | #endif |
4190 | | |
4191 | | /* Get the pre-shared key. */ |
4192 | | #ifdef OPENSSL_EXTRA |
4193 | | if (ssl->options.session_psk_cb != NULL) { |
4194 | | const unsigned char* id = NULL; |
4195 | | size_t idlen = 0; |
4196 | | const WOLFSSL_EVP_MD* handshake_md = NULL; |
4197 | | |
4198 | | if (ssl->msgsReceived.got_hello_retry_request >= 1) { |
4199 | | handshake_md = ssl_handshake_md(ssl->specs.mac_algorithm); |
4200 | | } |
4201 | | /* OpenSSL compatible callback that gets cached session. */ |
4202 | | if (ssl->options.session_psk_cb(ssl, handshake_md, &id, &idlen, |
4203 | | &psksession) == 0) { |
4204 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4205 | | WOLFSSL_MSG("psk session callback failed"); |
4206 | | return PSK_KEY_ERROR; |
4207 | | } |
4208 | | if (psksession != NULL) { |
4209 | | if (idlen > MAX_PSK_KEY_LEN) { |
4210 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4211 | | WOLFSSL_MSG("psk key length is too long"); |
4212 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4213 | | return PSK_KEY_ERROR; |
4214 | | } |
4215 | | |
4216 | | ssl->arrays->psk_keySz = (word32)idlen; |
4217 | | XMEMCPY(ssl->arrays->psk_key, id, idlen); |
4218 | | suite[0] = psksession->cipherSuite0; |
4219 | | suite[1] = psksession->cipherSuite; |
4220 | | /* Not needed anymore. */ |
4221 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4222 | | /* Leave pointer not NULL to indicate success with callback. */ |
4223 | | } |
4224 | | } |
4225 | | if (psksession != NULL) { |
4226 | | /* Don't try other callbacks - we have an answer. */ |
4227 | | } |
4228 | | else |
4229 | | #endif /* OPENSSL_EXTRA */ |
4230 | | if (ssl->options.client_psk_cs_cb != NULL) { |
4231 | | #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
4232 | | ssl->arrays->client_identity[0] = 0; |
4233 | | #endif |
4234 | | /* Lookup key again for next identity. */ |
4235 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cs_cb( |
4236 | | ssl, ssl->arrays->server_hint, |
4237 | | ssl->arrays->client_identity, MAX_PSK_ID_LEN, |
4238 | | ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4239 | | GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4240 | | if (clientHello) { |
4241 | | /* Use PSK cipher suite. */ |
4242 | | ssl->options.cipherSuite0 = psk->cipherSuite0; |
4243 | | ssl->options.cipherSuite = psk->cipherSuite; |
4244 | | } |
4245 | | else { |
4246 | | byte pskCS[2]; |
4247 | | pskCS[0] = psk->cipherSuite0; |
4248 | | pskCS[1] = psk->cipherSuite; |
4249 | | |
4250 | | /* Ensure PSK and negotiated cipher suites have same hash. */ |
4251 | | if (SuiteMac(pskCS) != SuiteMac(suite)) { |
4252 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4253 | | return PSK_KEY_ERROR; |
4254 | | } |
4255 | | /* Negotiated cipher suite is to be used - update PSK. */ |
4256 | | psk->cipherSuite0 = suite[0]; |
4257 | | psk->cipherSuite = suite[1]; |
4258 | | } |
4259 | | } |
4260 | | else if (ssl->options.client_psk_tls13_cb != NULL) { |
4261 | | byte cipherSuite0; |
4262 | | byte cipherSuite; |
4263 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
4264 | | |
4265 | | ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl, |
4266 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4267 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4268 | | &cipherName); |
4269 | | if (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
4270 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) { |
4271 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4272 | | return PSK_KEY_ERROR; |
4273 | | } |
4274 | | ssl->options.cipherSuite0 = cipherSuite0; |
4275 | | ssl->options.cipherSuite = cipherSuite; |
4276 | | (void)cipherSuiteFlags; |
4277 | | } |
4278 | | else { |
4279 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl, |
4280 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4281 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN); |
4282 | | ssl->options.cipherSuite0 = TLS13_BYTE; |
4283 | | ssl->options.cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
4284 | | } |
4285 | | if (ssl->arrays->psk_keySz == 0 || |
4286 | | (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN && |
4287 | | (int)ssl->arrays->psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK))) { |
4288 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4289 | | return PSK_KEY_ERROR; |
4290 | | } |
4291 | | |
4292 | | ret = SetCipherSpecs(ssl); |
4293 | | if (ret != 0) |
4294 | | return ret; |
4295 | | #else |
4296 | | /* PSK information loaded during setting of default TLS extensions. */ |
4297 | | #endif /* !WOLFSSL_PSK_ONE_ID */ |
4298 | | |
4299 | | if (!clientHello && (psk->cipherSuite0 != suite[0] || |
4300 | | psk->cipherSuite != suite[1])) { |
4301 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4302 | | return PSK_KEY_ERROR; |
4303 | | } |
4304 | | |
4305 | | if (!clientHello) { |
4306 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
4307 | | if (ssl->options.certWithExternPsk) { |
4308 | | /* Certificate authentication is still required. */ |
4309 | | ssl->options.peerAuthGood = 0; |
4310 | | } |
4311 | | else |
4312 | | #endif |
4313 | | { |
4314 | | /* CLIENT: using PSK for peer authentication. */ |
4315 | | ssl->options.peerAuthGood = 1; |
4316 | | } |
4317 | | } |
4318 | | } |
4319 | | #endif |
4320 | | |
4321 | | #ifdef HAVE_SUPPORTED_CURVES |
4322 | | if (!clientHello) { |
4323 | | TLSX* ext; |
4324 | | word32 modes; |
4325 | | KeyShareEntry* kse = NULL; |
4326 | | |
4327 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
4328 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
4329 | | if (ext == NULL) { |
4330 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4331 | | return PSK_KEY_ERROR; |
4332 | | } |
4333 | | modes = ext->val; |
4334 | | |
4335 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
4336 | | if (ext != NULL) { |
4337 | | kse = (KeyShareEntry*)ext->data; |
4338 | | } |
4339 | | /* Use (EC)DHE for forward-security if possible. */ |
4340 | | if (((modes & (1 << PSK_DHE_KE)) != 0) && (!ssl->options.noPskDheKe) && |
4341 | | (kse != NULL) && kse->derived) { |
4342 | | if ((kse->session != 0) && (kse->session != kse->group)) { |
4343 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4344 | | return PSK_KEY_ERROR; |
4345 | | } |
4346 | | } |
4347 | | else if (ssl->options.onlyPskDheKe || |
4348 | | (ssl->options.failNoPSK && !psk->resumption)) { |
4349 | | /* A mandatory external PSK (failNoPSK) must be combined with |
4350 | | * (EC)DHE for forward secrecy, so reject a pure psk_ke |
4351 | | * negotiation. Session-ticket resumption is exempt. */ |
4352 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4353 | | return PSK_KEY_ERROR; |
4354 | | } |
4355 | | else if (ssl->options.noPskDheKe) { |
4356 | | ssl->arrays->preMasterSz = 0; |
4357 | | } |
4358 | | } |
4359 | | else |
4360 | | #endif |
4361 | | if (ssl->options.noPskDheKe) { |
4362 | | ssl->arrays->preMasterSz = 0; |
4363 | | } |
4364 | | |
4365 | | /* Derive the early secret using the PSK. */ |
4366 | | return DeriveEarlySecret(ssl); |
4367 | | } |
4368 | | |
4369 | | /* Derive and write the binders into the ClientHello in space left when |
4370 | | * writing the Pre-Shared Key extension. |
4371 | | * |
4372 | | * ssl The SSL/TLS object. |
4373 | | * output The buffer containing the ClientHello. |
4374 | | * idx The index at the end of the completed ClientHello. |
4375 | | * returns 0 on success and otherwise failure. |
4376 | | */ |
4377 | | static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx) |
4378 | | { |
4379 | | int ret; |
4380 | | TLSX* ext; |
4381 | | PreSharedKey* current; |
4382 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
4383 | | word16 len; |
4384 | | |
4385 | | WOLFSSL_ENTER("WritePSKBinders"); |
4386 | | |
4387 | | if (idx > WOLFSSL_MAX_16BIT) { |
4388 | | return INPUT_SIZE_E; |
4389 | | } |
4390 | | |
4391 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
4392 | | if (ext == NULL) |
4393 | | return SANITY_MSG_E; |
4394 | | |
4395 | | /* Get the size of the binders to determine where to write binders. */ |
4396 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
4397 | | client_hello, &len); |
4398 | | if (ret < 0) |
4399 | | return ret; |
4400 | | idx -= len; |
4401 | | |
4402 | | /* Hash truncated ClientHello - up to binders. */ |
4403 | | #ifdef WOLFSSL_DTLS13 |
4404 | | if (ssl->options.dtls) |
4405 | | ret = Dtls13HashHandshake(ssl, output + Dtls13GetRlHeaderLength(ssl, 0), |
4406 | | (word16)idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4407 | | else |
4408 | | #endif /* WOLFSSL_DTLS13 */ |
4409 | | ret = HashOutput(ssl, output, (int)idx, 0); |
4410 | | |
4411 | | if (ret != 0) |
4412 | | return ret; |
4413 | | |
4414 | | current = (PreSharedKey*)ext->data; |
4415 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4416 | | if (current != NULL) { |
4417 | | wc_MemZero_Add("WritePSKBinders binderKey", binderKey, |
4418 | | sizeof(binderKey)); |
4419 | | } |
4420 | | #endif |
4421 | | /* Calculate the binder for each identity based on previous handshake data. |
4422 | | */ |
4423 | | while (current != NULL) { |
4424 | | if ((ret = SetupPskKey(ssl, current, 1)) != 0) |
4425 | | break; |
4426 | | |
4427 | | #ifdef HAVE_SESSION_TICKET |
4428 | | if (current->resumption) |
4429 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
4430 | | #endif |
4431 | | #ifndef NO_PSK |
4432 | | if (!current->resumption) |
4433 | | ret = DeriveBinderKey(ssl, binderKey); |
4434 | | #endif |
4435 | | if (ret != 0) |
4436 | | break; |
4437 | | |
4438 | | /* Derive the Finished message secret. */ |
4439 | | ret = DeriveFinishedSecret(ssl, binderKey, |
4440 | | ssl->keys.client_write_MAC_secret, |
4441 | | 0 /* neither end */); |
4442 | | if (ret != 0) |
4443 | | break; |
4444 | | |
4445 | | /* Build the HMAC of the handshake message data = binder. */ |
4446 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, |
4447 | | current->binder, ¤t->binderLen); |
4448 | | if (ret != 0) |
4449 | | break; |
4450 | | |
4451 | | current = current->next; |
4452 | | } |
4453 | | |
4454 | | ForceZero(binderKey, sizeof(binderKey)); |
4455 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4456 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
4457 | | #endif |
4458 | | if (ret != 0) |
4459 | | return ret; |
4460 | | |
4461 | | /* Data entered into extension, now write to message. */ |
4462 | | ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx, |
4463 | | client_hello, &len); |
4464 | | if (ret < 0) |
4465 | | return ret; |
4466 | | |
4467 | | /* Hash binders to complete the hash of the ClientHello. */ |
4468 | | ret = HashRaw(ssl, output + idx, len); |
4469 | | if (ret < 0) |
4470 | | return ret; |
4471 | | |
4472 | | #ifdef WOLFSSL_EARLY_DATA |
4473 | | if (ssl->earlyData != no_early_data) { |
4474 | | if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data, 1)) != 0) |
4475 | | return ret; |
4476 | | |
4477 | | /* Derive early data encryption key. */ |
4478 | | ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1); |
4479 | | if (ret != 0) |
4480 | | return ret; |
4481 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
4482 | | return ret; |
4483 | | |
4484 | | } |
4485 | | #endif |
4486 | | |
4487 | | WOLFSSL_LEAVE("WritePSKBinders", ret); |
4488 | | |
4489 | | return ret; |
4490 | | } |
4491 | | #endif |
4492 | | |
4493 | | static void GetTls13SessionId(WOLFSSL* ssl, byte* output, word32* idx) |
4494 | 8.49k | { |
4495 | 8.49k | if (ssl->session->sessionIDSz > 0) { |
4496 | | /* Session resumption for old versions of protocol. */ |
4497 | 0 | if (ssl->session->sessionIDSz <= ID_LEN) { |
4498 | 0 | if (output != NULL) |
4499 | 0 | output[*idx] = ssl->session->sessionIDSz; |
4500 | 0 | (*idx)++; |
4501 | 0 | if (output != NULL) { |
4502 | 0 | XMEMCPY(output + *idx, ssl->session->sessionID, |
4503 | 0 | ssl->session->sessionIDSz); |
4504 | 0 | } |
4505 | 0 | *idx += ssl->session->sessionIDSz; |
4506 | 0 | } |
4507 | 0 | else { |
4508 | | /* Invalid session ID length. Reset it. */ |
4509 | 0 | ssl->session->sessionIDSz = 0; |
4510 | 0 | if (output != NULL) |
4511 | 0 | output[*idx] = 0; |
4512 | 0 | (*idx)++; |
4513 | 0 | } |
4514 | 0 | } |
4515 | 8.49k | else { |
4516 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
4517 | | if (ssl->options.tls13MiddleBoxCompat) { |
4518 | | if (output != NULL) |
4519 | | output[*idx] = ID_LEN; |
4520 | | (*idx)++; |
4521 | | if (output != NULL) |
4522 | | XMEMCPY(output + *idx, ssl->arrays->clientRandom, ID_LEN); |
4523 | | *idx += ID_LEN; |
4524 | | } |
4525 | | else |
4526 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
4527 | 8.49k | { |
4528 | | /* TLS v1.3 does not use session id - 0 length. */ |
4529 | 8.49k | if (output != NULL) |
4530 | 4.20k | output[*idx] = 0; |
4531 | 8.49k | (*idx)++; |
4532 | 8.49k | } |
4533 | 8.49k | } |
4534 | 8.49k | } |
4535 | | |
4536 | | /* handle generation of TLS 1.3 client_hello (1) */ |
4537 | | /* Send a ClientHello message to the server. |
4538 | | * Include the information required to start a handshake with servers using |
4539 | | * protocol versions less than TLS v1.3. |
4540 | | * Only a client will send this message. |
4541 | | * |
4542 | | * ssl The SSL/TLS object. |
4543 | | * returns 0 on success and otherwise failure. |
4544 | | */ |
4545 | | |
4546 | | typedef struct Sch13Args { |
4547 | | byte* output; |
4548 | | word32 idx; |
4549 | | int sendSz; |
4550 | | word32 length; |
4551 | | #if defined(HAVE_ECH) |
4552 | | int clientRandomOffset; |
4553 | | word32 preXLength; |
4554 | | word32 expandedInnerLen; |
4555 | | WOLFSSL_ECH* ech; |
4556 | | #endif |
4557 | | } Sch13Args; |
4558 | | |
4559 | | #ifdef WOLFSSL_EARLY_DATA |
4560 | | /* Check if early data can potentially be sent. |
4561 | | * Returns 1 if early data is possible, 0 otherwise. |
4562 | | */ |
4563 | | static int EarlyDataPossible(WOLFSSL* ssl) |
4564 | | { |
4565 | | /* Need session resumption OR PSK callback configured */ |
4566 | | if (ssl->options.resuming) { |
4567 | | return 1; |
4568 | | } |
4569 | | #ifndef NO_PSK |
4570 | | if (ssl->options.client_psk_tls13_cb != NULL || |
4571 | | ssl->options.client_psk_cb != NULL) { |
4572 | | return 1; |
4573 | | } |
4574 | | #endif |
4575 | | return 0; |
4576 | | } |
4577 | | #endif /* WOLFSSL_EARLY_DATA */ |
4578 | | |
4579 | | int SendTls13ClientHello(WOLFSSL* ssl) |
4580 | 0 | { |
4581 | 0 | int ret; |
4582 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4583 | | Sch13Args* args = NULL; |
4584 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
4585 | | #else |
4586 | 0 | Sch13Args args[1]; |
4587 | 0 | #endif |
4588 | 0 | byte major, tls12minor; |
4589 | 0 | const Suites* suites; |
4590 | |
|
4591 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND); |
4592 | 0 | WOLFSSL_ENTER("SendTls13ClientHello"); |
4593 | |
|
4594 | 0 | if (ssl == NULL) { |
4595 | 0 | return BAD_FUNC_ARG; |
4596 | 0 | } |
4597 | | |
4598 | 0 | #if defined(HAVE_SECURE_RENEGOTIATION) || defined(HAVE_SERVER_RENEGOTIATION_INFO) |
4599 | | /* Re-establish renegotiation_info advertising for a reused object |
4600 | | * (wolfSSL_clear frees it) so a TLS 1.2 downgrade still enforces what the |
4601 | | * ClientHello advertised. Only when absent, to keep any existing state. */ |
4602 | 0 | if (ssl->secure_renegotiation == NULL) { |
4603 | 0 | ret = SetupClientSecureRenegotiation(ssl); |
4604 | 0 | if (ret != WOLFSSL_SUCCESS) |
4605 | 0 | return ret; |
4606 | 0 | } |
4607 | 0 | #endif |
4608 | | |
4609 | 0 | ssl->options.buildingMsg = 1; |
4610 | 0 | major = SSLv3_MAJOR; |
4611 | 0 | tls12minor = TLSv1_2_MINOR; |
4612 | |
|
4613 | | #ifdef WOLFSSL_DTLS13 |
4614 | | if (ssl->options.dtls) { |
4615 | | major = DTLS_MAJOR; |
4616 | | tls12minor = DTLSv1_2_MINOR; |
4617 | | } |
4618 | | #endif /* WOLFSSL_DTLS */ |
4619 | |
|
4620 | 0 | if (ssl->options.resuming && |
4621 | 0 | ssl->session->version.major != 0 && |
4622 | 0 | (ssl->session->version.major != ssl->version.major || |
4623 | 0 | ssl->session->version.minor != ssl->version.minor)) { |
4624 | 0 | #ifndef WOLFSSL_NO_TLS12 |
4625 | 0 | if (ssl->session->version.major == ssl->version.major && |
4626 | 0 | ssl->session->version.minor < ssl->version.minor) { |
4627 | | /* Cannot resume with a different protocol version. */ |
4628 | 0 | ssl->options.resuming = 0; |
4629 | 0 | ssl->version.major = ssl->session->version.major; |
4630 | 0 | ssl->version.minor = ssl->session->version.minor; |
4631 | 0 | return SendClientHello(ssl); |
4632 | 0 | } |
4633 | 0 | else |
4634 | 0 | #endif |
4635 | 0 | { |
4636 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
4637 | 0 | return VERSION_ERROR; |
4638 | 0 | } |
4639 | 0 | } |
4640 | | |
4641 | 0 | suites = WOLFSSL_SUITES(ssl); |
4642 | 0 | if (suites == NULL) { |
4643 | 0 | WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello"); |
4644 | 0 | return SUITES_ERROR; |
4645 | 0 | } |
4646 | | |
4647 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4648 | | if (ssl->async == NULL) { |
4649 | | ssl->async = (struct WOLFSSL_ASYNC*) |
4650 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
4651 | | DYNAMIC_TYPE_ASYNC); |
4652 | | if (ssl->async == NULL) |
4653 | | return MEMORY_E; |
4654 | | ssl->async->freeArgs = NULL; |
4655 | | } |
4656 | | args = (Sch13Args*)ssl->async->args; |
4657 | | |
4658 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
4659 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
4660 | | /* Check for error */ |
4661 | | if (ret < 0) |
4662 | | return ret; |
4663 | | } |
4664 | | else |
4665 | | #endif |
4666 | 0 | { |
4667 | | /* Reset state */ |
4668 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
4669 | 0 | XMEMSET(args, 0, sizeof(Sch13Args)); |
4670 | 0 | } |
4671 | |
|
4672 | 0 | switch (ssl->options.asyncState) { |
4673 | 0 | case TLS_ASYNC_BEGIN: |
4674 | 0 | { |
4675 | 0 | word32 sessIdSz = 0; |
4676 | |
|
4677 | 0 | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
4678 | |
|
4679 | | #ifdef WOLFSSL_DTLS13 |
4680 | | if (ssl->options.dtls) |
4681 | | args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4682 | | #endif /* WOLFSSL_DTLS13 */ |
4683 | | |
4684 | | /* Version | Random | Cipher Suites | Compression */ |
4685 | 0 | args->length = VERSION_SZ + RAN_LEN + suites->suiteSz + |
4686 | 0 | SUITE_LEN + COMP_LEN + ENUM_LEN; |
4687 | | #ifdef WOLFSSL_QUIC |
4688 | | if (WOLFSSL_IS_QUIC(ssl)) { |
4689 | | /* RFC 9001 ch. 8.4 sessionID in ClientHello MUST be 0 length */ |
4690 | | ssl->session->sessionIDSz = 0; |
4691 | | ssl->options.tls13MiddleBoxCompat = 0; |
4692 | | } |
4693 | | #endif |
4694 | | #ifdef WOLFSSL_DTLS13 |
4695 | | if (ssl->options.dtls) { |
4696 | | /* RFC 9147 Section 5: DTLS implementations do not use the |
4697 | | * TLS 1.3 "compatibility mode" */ |
4698 | | ssl->options.tls13MiddleBoxCompat = 0; |
4699 | | } |
4700 | | #endif |
4701 | 0 | GetTls13SessionId(ssl, NULL, &sessIdSz); |
4702 | 0 | args->length += (word16)sessIdSz; |
4703 | |
|
4704 | | #ifdef WOLFSSL_DTLS13 |
4705 | | if (ssl->options.dtls) { |
4706 | | /* legacy_cookie_id len */ |
4707 | | args->length += ENUM_LEN; |
4708 | | |
4709 | | /* server sent us an HelloVerifyRequest and we allow downgrade */ |
4710 | | if (ssl->arrays->cookieSz > 0 && ssl->options.downgrade) |
4711 | | args->length += ssl->arrays->cookieSz; |
4712 | | } |
4713 | | #endif /* WOLFSSL_DTLS13 */ |
4714 | | |
4715 | | /* Advance state and proceed */ |
4716 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
4717 | 0 | } /* case TLS_ASYNC_BEGIN */ |
4718 | 0 | FALL_THROUGH; |
4719 | |
|
4720 | 0 | case TLS_ASYNC_BUILD: |
4721 | 0 | case TLS_ASYNC_DO: |
4722 | 0 | { |
4723 | | /* Auto populate extensions supported unless user defined. */ |
4724 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0) |
4725 | 0 | return ret; |
4726 | | |
4727 | | /* Advance state and proceed */ |
4728 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
4729 | 0 | } /* case TLS_ASYNC_BUILD */ |
4730 | 0 | FALL_THROUGH; |
4731 | |
|
4732 | 0 | case TLS_ASYNC_FINALIZE: |
4733 | 0 | { |
4734 | | #ifdef WOLFSSL_EARLY_DATA |
4735 | | if (!EarlyDataPossible(ssl)) |
4736 | | ssl->earlyData = no_early_data; |
4737 | | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
4738 | | ssl->earlyData = no_early_data; |
4739 | | if (ssl->earlyData == no_early_data) |
4740 | | TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap); |
4741 | | if (ssl->earlyData != no_early_data && |
4742 | | (ret = TLSX_EarlyData_Use(ssl, 0, 0)) < 0) { |
4743 | | return ret; |
4744 | | } |
4745 | | #endif |
4746 | | #ifdef WOLFSSL_QUIC |
4747 | | if (WOLFSSL_IS_QUIC(ssl) && IsAtLeastTLSv1_3(ssl->version)) { |
4748 | | ret = wolfSSL_quic_add_transport_extensions(ssl, client_hello); |
4749 | | if (ret != 0) |
4750 | | return ret; |
4751 | | } |
4752 | | #endif |
4753 | | |
4754 | | /* find length of outer and inner */ |
4755 | | #if defined(HAVE_ECH) |
4756 | | if (!ssl->options.disableECH) { |
4757 | | TLSX* echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
4758 | | void* hostName = NULL; |
4759 | | word16 nameLen; |
4760 | | if (echX == NULL) |
4761 | | return WOLFSSL_FATAL_ERROR; |
4762 | | |
4763 | | args->ech = (WOLFSSL_ECH*)echX->data; |
4764 | | if (args->ech == NULL) |
4765 | | return WOLFSSL_FATAL_ERROR; |
4766 | | |
4767 | | /* if ECH was rejected by the HRR then the server MUST stop |
4768 | | * decrypting ECH, so send a GREASE ECH for the follow-up CH */ |
4769 | | if (ssl->echConfigs != NULL && !ssl->options.echAccepted && |
4770 | | ssl->options.serverState == |
4771 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
4772 | | args->ech->state = ECH_WRITE_GREASE; |
4773 | | } |
4774 | | |
4775 | | /* only prepare if we have a chance at acceptance (real ECH only) */ |
4776 | | if (ssl->echConfigs != NULL && |
4777 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4778 | | word32 encodedLen; |
4779 | | byte downgrade; |
4780 | | |
4781 | | /* ensure that a version less than TLS1.3 is never offered */ |
4782 | | downgrade = ssl->options.downgrade; |
4783 | | ssl->options.downgrade = 0; |
4784 | | |
4785 | | /* set the type to inner */ |
4786 | | args->ech->type = ECH_TYPE_INNER; |
4787 | | args->preXLength = args->length; |
4788 | | |
4789 | | /* get expanded inner size (used for transcript) */ |
4790 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4791 | | if (ret != 0) { |
4792 | | args->ech->type = ECH_TYPE_OUTER; |
4793 | | ssl->options.downgrade = downgrade; |
4794 | | return ret; |
4795 | | } |
4796 | | |
4797 | | /* args->expandedInnerLen carries the length for the hash */ |
4798 | | args->expandedInnerLen = args->length; |
4799 | | if (args->expandedInnerLen > 0xFFFF) { |
4800 | | args->ech->type = ECH_TYPE_OUTER; |
4801 | | ssl->options.downgrade = downgrade; |
4802 | | return BUFFER_E; |
4803 | | } |
4804 | | |
4805 | | /* get encoded inner size */ |
4806 | | args->ech->writeEncoded = 1; |
4807 | | encodedLen = args->preXLength; |
4808 | | ret = TLSX_GetRequestSize(ssl, client_hello, &encodedLen); |
4809 | | args->ech->writeEncoded = 0; |
4810 | | /* set the type to outer */ |
4811 | | args->ech->type = ECH_TYPE_OUTER; |
4812 | | ssl->options.downgrade = downgrade; |
4813 | | if (ret != 0) |
4814 | | return ret; |
4815 | | |
4816 | | /* calculate padding (RFC 9849, section 6.1.3) */ |
4817 | | nameLen = TLSX_SNI_GetRequest(ssl->extensions, |
4818 | | WOLFSSL_SNI_HOST_NAME, &hostName, 1); |
4819 | | if (nameLen == 0 && ssl->ctx != NULL) |
4820 | | nameLen = TLSX_SNI_GetRequest(ssl->ctx->extensions, |
4821 | | WOLFSSL_SNI_HOST_NAME, &hostName, 1); |
4822 | | |
4823 | | if (nameLen != 0) { |
4824 | | if (nameLen > args->ech->echConfig->maxNameLen) |
4825 | | args->ech->paddingLen = 0; |
4826 | | else |
4827 | | args->ech->paddingLen = |
4828 | | (word16)args->ech->echConfig->maxNameLen - nameLen; |
4829 | | } |
4830 | | else { |
4831 | | /* maxNameLen + length of the SNI extension */ |
4832 | | args->ech->paddingLen = args->ech->echConfig->maxNameLen + 9; |
4833 | | } |
4834 | | |
4835 | | /* innerClientHelloLen and padding are based on the |
4836 | | * encoded (sealed) inner */ |
4837 | | args->ech->paddingLen += |
4838 | | ECH_PADDING_TO_32(encodedLen + args->ech->paddingLen); |
4839 | | args->ech->innerClientHelloLen = encodedLen + |
4840 | | args->ech->paddingLen + args->ech->hpke->Nt; |
4841 | | |
4842 | | if (args->ech->innerClientHelloLen > 0xFFFF) |
4843 | | return BUFFER_E; |
4844 | | |
4845 | | /* restore the length to pre-ClientHelloInner computations */ |
4846 | | args->length = args->preXLength; |
4847 | | } |
4848 | | } |
4849 | | #endif |
4850 | |
|
4851 | 0 | { |
4852 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4853 | | word16 maxFrag = wolfssl_local_GetMaxPlaintextSize(ssl); |
4854 | | word16 lenWithoutExts = args->length; |
4855 | | #endif |
4856 | | |
4857 | | /* Include length of TLS extensions. */ |
4858 | 0 | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4859 | 0 | if (ret != 0) |
4860 | 0 | return ret; |
4861 | | |
4862 | | /* Total message size. */ |
4863 | 0 | args->sendSz = |
4864 | 0 | (int)(args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ); |
4865 | |
|
4866 | | #ifdef WOLFSSL_DTLS13 |
4867 | | if (ssl->options.dtls) |
4868 | | args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4869 | | #endif /* WOLFSSL_DTLS13 */ |
4870 | |
|
4871 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4872 | | /* Only empty the key share on the first CH; this avoids first CH |
4873 | | * fragmentation (wolfSSL refuses them) */ |
4874 | | if (ssl->options.dtls && args->sendSz > maxFrag && |
4875 | | ssl->options.serverState != |
4876 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
4877 | | /* Try again with an empty key share if we would be fragmenting */ |
4878 | | ret = TLSX_KeyShare_Empty(ssl); |
4879 | | if (ret != 0) |
4880 | | return ret; |
4881 | | args->length = lenWithoutExts; |
4882 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4883 | | if (ret != 0) |
4884 | | return ret; |
4885 | | args->sendSz = (int)(args->length + |
4886 | | DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ); |
4887 | | if (args->sendSz > maxFrag) { |
4888 | | WOLFSSL_MSG("Can't fit first CH in one fragment."); |
4889 | | return BUFFER_ERROR; |
4890 | | } |
4891 | | WOLFSSL_MSG("Sending empty key share so we don't fragment CH1"); |
4892 | | } |
4893 | | #endif |
4894 | 0 | } |
4895 | | |
4896 | | /* Check buffers are big enough and grow if needed. */ |
4897 | 0 | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) |
4898 | 0 | return ret; |
4899 | | |
4900 | | /* Get position in output buffer to write new message to. */ |
4901 | 0 | args->output = GetOutputBuffer(ssl); |
4902 | | |
4903 | | /* Put the record and handshake headers on. */ |
4904 | 0 | AddTls13Headers(args->output, args->length, client_hello, ssl); |
4905 | | |
4906 | | /* Protocol version - negotiation now in extension: supported_versions. */ |
4907 | 0 | args->output[args->idx++] = major; |
4908 | 0 | args->output[args->idx++] = tls12minor; |
4909 | | |
4910 | | /* Keep for downgrade. */ |
4911 | 0 | ssl->chVersion = ssl->version; |
4912 | |
|
4913 | 0 | if (ssl->arrays == NULL) { |
4914 | 0 | return BAD_FUNC_ARG; |
4915 | 0 | } |
4916 | | /* Client Random */ |
4917 | 0 | if (ssl->options.connectState == CONNECT_BEGIN) { |
4918 | 0 | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + args->idx, RAN_LEN); |
4919 | 0 | if (ret != 0) |
4920 | 0 | return ret; |
4921 | | |
4922 | | /* Store random for possible second ClientHello. */ |
4923 | 0 | XMEMCPY(ssl->arrays->clientRandom, args->output + args->idx, RAN_LEN); |
4924 | 0 | } |
4925 | 0 | else |
4926 | 0 | XMEMCPY(args->output + args->idx, ssl->arrays->clientRandom, RAN_LEN); |
4927 | | |
4928 | | #if defined(HAVE_ECH) |
4929 | | args->clientRandomOffset = (int)args->idx; |
4930 | | #endif |
4931 | | |
4932 | 0 | args->idx += RAN_LEN; |
4933 | |
|
4934 | 0 | GetTls13SessionId(ssl, args->output, &args->idx); |
4935 | |
|
4936 | | #ifdef WOLFSSL_DTLS13 |
4937 | | if (ssl->options.dtls) { |
4938 | | args->output[args->idx++] = ssl->arrays->cookieSz; |
4939 | | |
4940 | | if (ssl->arrays->cookieSz > 0) { |
4941 | | /* We have a cookie saved, so the server sent us an |
4942 | | * HelloVerifyRequest, it means it is a v1.2 server */ |
4943 | | if (!ssl->options.downgrade) |
4944 | | return VERSION_ERROR; |
4945 | | XMEMCPY(args->output + args->idx, ssl->arrays->cookie, |
4946 | | ssl->arrays->cookieSz); |
4947 | | args->idx += ssl->arrays->cookieSz; |
4948 | | } |
4949 | | } |
4950 | | #endif /* WOLFSSL_DTLS13 */ |
4951 | | |
4952 | | /* Cipher suites */ |
4953 | 0 | c16toa(suites->suiteSz, args->output + args->idx); |
4954 | 0 | args->idx += OPAQUE16_LEN; |
4955 | 0 | XMEMCPY(args->output + args->idx, &suites->suites, |
4956 | 0 | suites->suiteSz); |
4957 | 0 | args->idx += suites->suiteSz; |
4958 | | #ifdef WOLFSSL_DEBUG_TLS |
4959 | | { |
4960 | | int ii; |
4961 | | WOLFSSL_MSG("Ciphers:"); |
4962 | | for (ii = 0 ; ii < suites->suiteSz; ii += 2) { |
4963 | | WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0], |
4964 | | suites->suites[ii+1])); |
4965 | | } |
4966 | | } |
4967 | | #endif |
4968 | | |
4969 | | /* Compression not supported in TLS v1.3. */ |
4970 | 0 | args->output[args->idx++] = COMP_LEN; |
4971 | 0 | args->output[args->idx++] = NO_COMPRESSION; |
4972 | |
|
4973 | | #if defined(HAVE_ECH) |
4974 | | /* Build the expanded inner ClientHello */ |
4975 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
4976 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4977 | | byte downgrade; |
4978 | | |
4979 | | /* calculate maximum buffer size needed */ |
4980 | | word32 encodedBodyLen = args->ech->innerClientHelloLen - |
4981 | | args->ech->hpke->Nt; |
4982 | | word32 innerBufSize = args->expandedInnerLen; |
4983 | | if (encodedBodyLen > innerBufSize) |
4984 | | innerBufSize = encodedBodyLen; |
4985 | | |
4986 | | /* set the type to inner */ |
4987 | | args->ech->type = ECH_TYPE_INNER; |
4988 | | /* innerClientHello may already exist from hrr, free if it does */ |
4989 | | if (args->ech->innerClientHello != NULL) { |
4990 | | XFREE(args->ech->innerClientHello, ssl->heap, |
4991 | | DYNAMIC_TYPE_TMP_BUFFER); |
4992 | | } |
4993 | | /* allocate the inner */ |
4994 | | args->ech->innerClientHello = |
4995 | | (byte*)XMALLOC(innerBufSize, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
4996 | | if (args->ech->innerClientHello == NULL) { |
4997 | | args->ech->type = ECH_TYPE_OUTER; |
4998 | | return MEMORY_E; |
4999 | | } |
5000 | | /* copy everything before extensions into the innerClientHello |
5001 | | * ignore record and handshake headers */ |
5002 | | XMEMCPY(args->ech->innerClientHello, |
5003 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
5004 | | args->preXLength); |
5005 | | /* copy the client random to inner - only for first CH, not after HRR */ |
5006 | | if (!ssl->options.echAccepted) { |
5007 | | XMEMCPY(ssl->arrays->clientRandomInner, ssl->arrays->clientRandom, |
5008 | | RAN_LEN); |
5009 | | } |
5010 | | else { |
5011 | | /* After HRR, use the same inner random as CH1 */ |
5012 | | XMEMCPY(args->ech->innerClientHello + VERSION_SZ, |
5013 | | ssl->arrays->clientRandomInner, RAN_LEN); |
5014 | | } |
5015 | | /* change the outer client random */ |
5016 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + |
5017 | | args->clientRandomOffset, RAN_LEN); |
5018 | | if (ret != 0) { |
5019 | | args->ech->type = ECH_TYPE_OUTER; |
5020 | | return ret; |
5021 | | } |
5022 | | /* copy the new client random */ |
5023 | | XMEMCPY(ssl->arrays->clientRandom, args->output + |
5024 | | args->clientRandomOffset, RAN_LEN); |
5025 | | |
5026 | | /* ensure that a version less than TLS1.3 is never offered */ |
5027 | | downgrade = ssl->options.downgrade; |
5028 | | ssl->options.downgrade = 0; |
5029 | | |
5030 | | /* write the expanded extensions into the inner buffer */ |
5031 | | args->length = 0; |
5032 | | ret = TLSX_WriteRequest(ssl, |
5033 | | args->ech->innerClientHello + args->preXLength, client_hello, |
5034 | | &args->length); |
5035 | | if (ret != 0) { |
5036 | | args->ech->type = ECH_TYPE_OUTER; |
5037 | | ssl->options.downgrade = downgrade; |
5038 | | return ret; |
5039 | | } |
5040 | | |
5041 | | /* hash expanded form */ |
5042 | | args->ech->innerClientHelloLen = args->expandedInnerLen; |
5043 | | ret = EchHashHelloInner(ssl, args->ech); |
5044 | | args->ech->innerClientHelloLen = encodedBodyLen + args->ech->hpke->Nt; |
5045 | | if (ret != 0) { |
5046 | | args->ech->type = ECH_TYPE_OUTER; |
5047 | | ssl->options.downgrade = downgrade; |
5048 | | return ret; |
5049 | | } |
5050 | | |
5051 | | /* zero padding bytes sealed with the inner hello */ |
5052 | | XMEMSET(args->ech->innerClientHello + |
5053 | | args->ech->innerClientHelloLen - args->ech->hpke->Nt - |
5054 | | args->ech->paddingLen, 0, args->ech->paddingLen); |
5055 | | /* Rewrite inner buffer with the encoded form for sealing */ |
5056 | | args->ech->writeEncoded = 1; |
5057 | | args->length = 0; |
5058 | | ret = TLSX_WriteRequest(ssl, |
5059 | | args->ech->innerClientHello + args->preXLength, client_hello, |
5060 | | &args->length); |
5061 | | args->ech->writeEncoded = 0; |
5062 | | /* set the type to outer */ |
5063 | | args->ech->type = ECH_TYPE_OUTER; |
5064 | | ssl->options.downgrade = downgrade; |
5065 | | if (ret != 0) |
5066 | | return ret; |
5067 | | } |
5068 | | #endif |
5069 | | |
5070 | | /* Write out extensions for a request. */ |
5071 | 0 | args->length = 0; |
5072 | 0 | ret = TLSX_WriteRequest(ssl, args->output + args->idx, client_hello, |
5073 | 0 | &args->length); |
5074 | 0 | if (ret != 0) |
5075 | 0 | return ret; |
5076 | | |
5077 | 0 | args->idx += args->length; |
5078 | |
|
5079 | | #if defined(HAVE_ECH) |
5080 | | /* HPKE-seal inner hello and place into outer ECH extension's payload */ |
5081 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5082 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
5083 | | #if defined(WOLFSSL_TEST_ECH) |
5084 | | if (ssl->echInnerHelloCb != NULL) { |
5085 | | ret = ssl->echInnerHelloCb(args->ech->innerClientHello, |
5086 | | args->ech->innerClientHelloLen - args->ech->hpke->Nt); |
5087 | | if (ret != 0) |
5088 | | return ret; |
5089 | | } |
5090 | | #endif |
5091 | | ret = TLSX_FinalizeEch(ssl, args->ech, |
5092 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
5093 | | (word32)(args->sendSz - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ))); |
5094 | | |
5095 | | if (ret != 0) |
5096 | | return ret; |
5097 | | } |
5098 | | /* Mark CH1 done for any ECH extension (real or GREASE) */ |
5099 | | if (args->ech != NULL) |
5100 | | args->ech->innerCount = 1; |
5101 | | #endif |
5102 | |
|
5103 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5104 | | /* Resumption has a specific set of extensions and binder is calculated |
5105 | | * for each identity. |
5106 | | */ |
5107 | | if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY)) { |
5108 | | ret = WritePSKBinders(ssl, args->output, args->idx); |
5109 | | } |
5110 | | else |
5111 | | #endif |
5112 | 0 | { |
5113 | | #ifdef WOLFSSL_DTLS13 |
5114 | | if (ssl->options.dtls) |
5115 | | ret = Dtls13HashHandshake(ssl, |
5116 | | args->output + Dtls13GetRlHeaderLength(ssl, 0), |
5117 | | (word16)args->idx - Dtls13GetRlHeaderLength(ssl, 0)); |
5118 | | else |
5119 | | #endif /* WOLFSSL_DTLS13 */ |
5120 | 0 | { |
5121 | | /* compute the outer hash */ |
5122 | 0 | ret = HashOutput(ssl, args->output, (int)args->idx, 0); |
5123 | 0 | } |
5124 | 0 | } |
5125 | 0 | if (ret != 0) |
5126 | 0 | return ret; |
5127 | | |
5128 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
5129 | |
|
5130 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
5131 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
5132 | | if (ssl->toInfoOn) { |
5133 | | ret = AddPacketInfo(ssl, "ClientHello", handshake, args->output, |
5134 | | args->sendSz, WRITE_PROTO, 0, ssl->heap); |
5135 | | if (ret != 0) |
5136 | | return ret; |
5137 | | } |
5138 | | #endif |
5139 | |
|
5140 | 0 | ssl->options.buildingMsg = 0; |
5141 | | #ifdef WOLFSSL_DTLS13 |
5142 | | if (ssl->options.dtls) { |
5143 | | ret = Dtls13HandshakeSend(ssl, args->output, (word16)args->sendSz, |
5144 | | (word16)args->idx, client_hello, 0); |
5145 | | break; |
5146 | | } |
5147 | | #endif /* WOLFSSL_DTLS13 */ |
5148 | |
|
5149 | 0 | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
5150 | | |
5151 | | /* Advance state and proceed */ |
5152 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
5153 | 0 | } |
5154 | | /* case TLS_ASYNC_BUILD */ |
5155 | 0 | FALL_THROUGH; |
5156 | |
|
5157 | 0 | case TLS_ASYNC_END: |
5158 | 0 | { |
5159 | | #ifdef WOLFSSL_EARLY_DATA_GROUP |
5160 | | /* QUIC needs to forward records at their encryption level |
5161 | | * and is therefore unable to group here */ |
5162 | | if (ssl->earlyData == no_early_data || WOLFSSL_IS_QUIC(ssl)) |
5163 | | #endif |
5164 | 0 | ret = SendBuffered(ssl); |
5165 | |
|
5166 | 0 | break; |
5167 | 0 | } |
5168 | 0 | default: |
5169 | 0 | ret = INPUT_CASE_ERROR; |
5170 | 0 | } /* switch (ssl->options.asyncState) */ |
5171 | | |
5172 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5173 | | if (ret == 0) |
5174 | | FreeAsyncCtx(ssl, 0); |
5175 | | #endif |
5176 | | |
5177 | 0 | WOLFSSL_LEAVE("SendTls13ClientHello", ret); |
5178 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND); |
5179 | |
|
5180 | 0 | return ret; |
5181 | 0 | } |
5182 | | |
5183 | | #if defined(WOLFSSL_DTLS13) && !defined(NO_WOLFSSL_CLIENT) |
5184 | | static int Dtls13ClientDoDowngrade(WOLFSSL* ssl) |
5185 | | { |
5186 | | int ret; |
5187 | | if (ssl->dtls13ClientHello == NULL) |
5188 | | return BAD_STATE_E; |
5189 | | |
5190 | | /* v1.3 and v1.2 hash messages to compute the transcript hash. When we are |
5191 | | * using DTLSv1.3 we hash the first clientHello following v1.3 but the |
5192 | | * server can negotiate a lower version. So we need to re-hash the |
5193 | | * clientHello to adhere to DTLS <= v1.2 rules. */ |
5194 | | ret = InitHandshakeHashes(ssl); |
5195 | | if (ret != 0) |
5196 | | return ret; |
5197 | | ret = HashRaw(ssl, ssl->dtls13ClientHello, ssl->dtls13ClientHelloSz); |
5198 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5199 | | ssl->dtls13ClientHello = NULL; |
5200 | | ssl->dtls13ClientHelloSz = 0; |
5201 | | ssl->keys.dtls_sequence_number_hi = |
5202 | | (word16)w64GetHigh32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5203 | | ssl->keys.dtls_sequence_number_lo = |
5204 | | w64GetLow32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5205 | | return ret; |
5206 | | } |
5207 | | #endif /* WOLFSSL_DTLS13 && !NO_WOLFSSL_CLIENT*/ |
5208 | | |
5209 | | #if defined(HAVE_ECH) |
5210 | | /* Calculate ECH acceptance and verify the server accepted ECH. |
5211 | | * |
5212 | | * ssl SSL/TLS object. |
5213 | | * label Ascii string describing ECH acceptance type. |
5214 | | * labelSz Length of label excluding NULL character. |
5215 | | * input The buffer to calculate confirmation off of. |
5216 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
5217 | | * helloSz Size of hello message. |
5218 | | * returns 0 on success and otherwise failure. |
5219 | | */ |
5220 | | static int EchCheckAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
5221 | | const byte* input, int acceptOffset, int helloSz, byte msgType) |
5222 | | { |
5223 | | int ret = 0; |
5224 | | int headerSz; |
5225 | | HS_Hashes* tmpHashes; |
5226 | | byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ]; |
5227 | | |
5228 | | XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation)); |
5229 | | |
5230 | | #ifdef WOLFSSL_DTLS13 |
5231 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
5232 | | HANDSHAKE_HEADER_SZ; |
5233 | | #else |
5234 | | headerSz = HANDSHAKE_HEADER_SZ; |
5235 | | #endif |
5236 | | |
5237 | | ret = EchCalcAcceptance(ssl, label, labelSz, input, acceptOffset, helloSz, |
5238 | | msgType == hello_retry_request, acceptConfirmation); |
5239 | | |
5240 | | if (ret == 0) { |
5241 | | tmpHashes = ssl->hsHashes; |
5242 | | ssl->hsHashes = ssl->hsHashesEch; |
5243 | | |
5244 | | /* last 8 bytes must match the expand output */ |
5245 | | ret = ConstantCompare(acceptConfirmation, input + acceptOffset, |
5246 | | ECH_ACCEPT_CONFIRMATION_SZ); |
5247 | | |
5248 | | if (ret == 0) { |
5249 | | WOLFSSL_MSG("ECH accepted"); |
5250 | | ssl->options.echAccepted = 1; |
5251 | | |
5252 | | /* after HRR, hsHashesEch must contain: |
5253 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
5254 | | if (msgType == hello_retry_request) { |
5255 | | ret = HashRaw(ssl, input, helloSz + headerSz); |
5256 | | } |
5257 | | /* normal TLS code will calculate transcript of ServerHello */ |
5258 | | else { |
5259 | | ssl->hsHashes = tmpHashes; |
5260 | | FreeHandshakeHashes(ssl); |
5261 | | tmpHashes = ssl->hsHashesEch; |
5262 | | ssl->hsHashesEch = NULL; |
5263 | | } |
5264 | | } |
5265 | | else { |
5266 | | if (msgType != hello_retry_request && ssl->options.echAccepted) { |
5267 | | /* the SH has rejected ECH after the HRR has accepted it |
5268 | | * RFC 9849, section 6.1.5 */ |
5269 | | WOLFSSL_MSG("ECH rejected, but it was previously accepted..."); |
5270 | | ret = INVALID_PARAMETER; |
5271 | | } |
5272 | | else { |
5273 | | WOLFSSL_MSG("ECH rejected"); |
5274 | | ret = 0; |
5275 | | } |
5276 | | ssl->options.echAccepted = 0; |
5277 | | |
5278 | | /* ECH rejected, continue with outer transcript */ |
5279 | | FreeHandshakeHashes(ssl); |
5280 | | ssl->hsHashesEch = NULL; |
5281 | | } |
5282 | | |
5283 | | ssl->hsHashes = tmpHashes; |
5284 | | } |
5285 | | |
5286 | | /* Skip only when the HRR signals ECH acceptance |
5287 | | * -> CH2 still needs ech->extensions for inner/outer extension swap |
5288 | | * during write */ |
5289 | | if (ret == 0 && |
5290 | | (msgType != hello_retry_request || !ssl->options.echAccepted)) |
5291 | | ret = TLSX_EchReplaceExtensions(ssl, ssl->options.echAccepted); |
5292 | | |
5293 | | return ret; |
5294 | | } |
5295 | | #endif /* HAVE_ECH */ |
5296 | | |
5297 | | /* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */ |
5298 | | /* Handle the ServerHello message from the server. |
5299 | | * Only a client will receive this message. |
5300 | | * |
5301 | | * ssl The SSL/TLS object. |
5302 | | * input The message buffer. |
5303 | | * inOutIdx On entry, the index into the message buffer of ServerHello. |
5304 | | * On exit, the index of byte after the ServerHello message. |
5305 | | * helloSz The length of the current handshake message. |
5306 | | * returns 0 on success and otherwise failure. |
5307 | | */ |
5308 | | |
5309 | | typedef struct Dsh13Args { |
5310 | | ProtocolVersion pv; |
5311 | | word32 idx; |
5312 | | word32 begin; |
5313 | | const byte* sessId; |
5314 | | word16 totalExtSz; |
5315 | | byte sessIdSz; |
5316 | | byte extMsgType; |
5317 | | #if defined(HAVE_ECH) |
5318 | | TLSX* echX; |
5319 | | byte* acceptLabel; |
5320 | | word32 acceptOffset; |
5321 | | word16 acceptLabelSz; |
5322 | | #endif |
5323 | | } Dsh13Args; |
5324 | | |
5325 | | /* sessIdSz below bounds both the copy into arrays->sessionID and the comparison |
5326 | | * against arrays->clientRandom, so one check only covers both while these |
5327 | | * match. */ |
5328 | | wc_static_assert(ID_LEN == RAN_LEN); |
5329 | | |
5330 | | int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
5331 | | word32 helloSz, byte* extMsgType) |
5332 | 0 | { |
5333 | 0 | int ret; |
5334 | 0 | byte suite[2]; |
5335 | 0 | byte tls12minor; |
5336 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5337 | | Dsh13Args* args = NULL; |
5338 | | #else |
5339 | 0 | Dsh13Args args[1]; |
5340 | 0 | #endif |
5341 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5342 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
5343 | | #endif |
5344 | |
|
5345 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO); |
5346 | 0 | WOLFSSL_ENTER("DoTls13ServerHello"); |
5347 | |
|
5348 | 0 | if (ssl == NULL || ssl->arrays == NULL) |
5349 | 0 | return BAD_FUNC_ARG; |
5350 | | |
5351 | 0 | tls12minor = TLSv1_2_MINOR; |
5352 | |
|
5353 | | #ifdef WOLFSSL_DTLS13 |
5354 | | if (ssl->options.dtls) |
5355 | | tls12minor = DTLSv1_2_MINOR; |
5356 | | #endif /* WOLFSSL_DTLS13 */ |
5357 | |
|
5358 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5359 | | if (ssl->async == NULL) { |
5360 | | ssl->async = (struct WOLFSSL_ASYNC*) |
5361 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
5362 | | DYNAMIC_TYPE_ASYNC); |
5363 | | if (ssl->async == NULL) |
5364 | | return MEMORY_E; |
5365 | | ssl->async->freeArgs = NULL; |
5366 | | } |
5367 | | args = (Dsh13Args*)ssl->async->args; |
5368 | | |
5369 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
5370 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
5371 | | /* Check for error */ |
5372 | | if (ret < 0) { |
5373 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5374 | | /* Mark message as not received so it can process again */ |
5375 | | ssl->msgsReceived.got_server_hello = 0; |
5376 | | } |
5377 | | return ret; |
5378 | | } |
5379 | | } |
5380 | | else |
5381 | | #endif |
5382 | 0 | { |
5383 | | /* Reset state */ |
5384 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
5385 | 0 | XMEMSET(args, 0, sizeof(Dsh13Args)); |
5386 | 0 | } |
5387 | |
|
5388 | 0 | switch (ssl->options.asyncState) { |
5389 | 0 | case TLS_ASYNC_BEGIN: |
5390 | 0 | { |
5391 | 0 | byte b; |
5392 | | #ifdef WOLFSSL_CALLBACKS |
5393 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello"); |
5394 | | if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo); |
5395 | | #endif |
5396 | | |
5397 | | /* Protocol version length check. */ |
5398 | 0 | if (helloSz < OPAQUE16_LEN) |
5399 | 0 | return BUFFER_ERROR; |
5400 | | |
5401 | 0 | args->idx = *inOutIdx; |
5402 | 0 | args->begin = args->idx; |
5403 | | |
5404 | | /* Protocol version */ |
5405 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
5406 | 0 | args->idx += OPAQUE16_LEN; |
5407 | |
|
5408 | | #ifdef WOLFSSL_DTLS |
5409 | | if (ssl->options.dtls && |
5410 | | (args->pv.major != DTLS_MAJOR || args->pv.minor == DTLS_BOGUS_MINOR)) |
5411 | | return VERSION_ERROR; |
5412 | | #endif /* WOLFSSL_DTLS */ |
5413 | |
|
5414 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5415 | 0 | { |
5416 | 0 | byte wantDowngrade; |
5417 | |
|
5418 | 0 | wantDowngrade = args->pv.major == ssl->version.major && |
5419 | 0 | args->pv.minor < TLSv1_2_MINOR; |
5420 | |
|
5421 | | #ifdef WOLFSSL_DTLS13 |
5422 | | if (ssl->options.dtls) |
5423 | | wantDowngrade = args->pv.major == ssl->version.major && |
5424 | | args->pv.minor > DTLSv1_2_MINOR; |
5425 | | #endif /* WOLFSSL_DTLS13 */ |
5426 | |
|
5427 | 0 | if (wantDowngrade && ssl->options.downgrade) { |
5428 | | /* Force client hello version 1.2 to work for static RSA. */ |
5429 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5430 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5431 | 0 | ssl->options.tls1_3 = 0; |
5432 | |
|
5433 | | #ifdef WOLFSSL_DTLS13 |
5434 | | if (ssl->options.dtls) { |
5435 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5436 | | ssl->version.minor = DTLSv1_2_MINOR; |
5437 | | ret = Dtls13ClientDoDowngrade(ssl); |
5438 | | if (ret != 0) |
5439 | | return ret; |
5440 | | } |
5441 | | #endif /* WOLFSSL_DTLS13 */ |
5442 | |
|
5443 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5444 | 0 | } |
5445 | 0 | } |
5446 | 0 | #endif |
5447 | | |
5448 | 0 | if (args->pv.major != ssl->version.major || |
5449 | 0 | args->pv.minor != tls12minor) { |
5450 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5451 | 0 | return VERSION_ERROR; |
5452 | 0 | } |
5453 | | |
5454 | | /* Random and session id length check */ |
5455 | 0 | if ((args->idx - args->begin) + RAN_LEN + ENUM_LEN > helloSz) |
5456 | 0 | return BUFFER_ERROR; |
5457 | | |
5458 | | /* Check if hello retry request */ |
5459 | 0 | if (XMEMCMP(input + args->idx, helloRetryRequestRandom, RAN_LEN) == 0) { |
5460 | 0 | WOLFSSL_MSG("HelloRetryRequest format"); |
5461 | 0 | *extMsgType = hello_retry_request; |
5462 | |
|
5463 | 0 | if (ssl->msgsReceived.got_hello_verify_request) { |
5464 | 0 | WOLFSSL_MSG("Received HelloRetryRequest after a " |
5465 | 0 | "HelloVerifyRequest"); |
5466 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5467 | 0 | return VERSION_ERROR; |
5468 | 0 | } |
5469 | | |
5470 | | /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat. |
5471 | | * Found message to be a HelloRetryRequest. |
5472 | | * Don't allow more than one HelloRetryRequest or ServerHello. |
5473 | | */ |
5474 | 0 | if (ssl->msgsReceived.got_hello_retry_request) { |
5475 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
5476 | 0 | return DUPLICATE_MSG_E; |
5477 | 0 | } |
5478 | 0 | } |
5479 | 0 | args->extMsgType = *extMsgType; |
5480 | | |
5481 | | /* Server random - keep for debugging. */ |
5482 | 0 | XMEMCPY(ssl->arrays->serverRandom, input + args->idx, RAN_LEN); |
5483 | | #if defined(HAVE_ECH) |
5484 | | /* last 8 bytes of server random */ |
5485 | | args->acceptOffset = args->idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
5486 | | #endif |
5487 | 0 | args->idx += RAN_LEN; |
5488 | | |
5489 | | /* Session id */ |
5490 | 0 | args->sessIdSz = input[args->idx++]; |
5491 | 0 | if (args->sessIdSz > ID_LEN || |
5492 | 0 | ((args->idx - args->begin) + args->sessIdSz > helloSz)) |
5493 | 0 | return BUFFER_ERROR; |
5494 | 0 | args->sessId = input + args->idx; |
5495 | 0 | args->idx += args->sessIdSz; |
5496 | |
|
5497 | 0 | ssl->options.haveSessionId = 1; |
5498 | | |
5499 | | /* Ciphersuite and compression check */ |
5500 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz) |
5501 | 0 | return BUFFER_ERROR; |
5502 | | |
5503 | | /* Set the cipher suite from the message. */ |
5504 | 0 | ssl->options.cipherSuite0 = input[args->idx++]; |
5505 | 0 | ssl->options.cipherSuite = input[args->idx++]; |
5506 | 0 | if (*extMsgType == hello_retry_request) { |
5507 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
5508 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
5509 | 0 | } |
5510 | 0 | else if (ssl->msgsReceived.got_hello_retry_request && |
5511 | 0 | (ssl->options.hrrCipherSuite0 != ssl->options.cipherSuite0 || |
5512 | 0 | ssl->options.hrrCipherSuite != ssl->options.cipherSuite)) { |
5513 | 0 | WOLFSSL_MSG("Received ServerHello with different cipher suite than " |
5514 | 0 | "HelloRetryRequest"); |
5515 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5516 | 0 | return INVALID_PARAMETER; |
5517 | 0 | } |
5518 | | #ifdef WOLFSSL_DEBUG_TLS |
5519 | | WOLFSSL_MSG("Chosen cipher suite:"); |
5520 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
5521 | | ssl->options.cipherSuite)); |
5522 | | #endif |
5523 | | |
5524 | | /* Compression */ |
5525 | 0 | b = input[args->idx++]; |
5526 | 0 | if (b != 0) { |
5527 | 0 | WOLFSSL_MSG("Must be no compression types in list"); |
5528 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5529 | 0 | return INVALID_PARAMETER; |
5530 | 0 | } |
5531 | | |
5532 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) { |
5533 | | /* Fewer than OPAQUE16_LEN bytes remain after the compression method, so |
5534 | | * there is no complete extensions length field. */ |
5535 | 0 | if ((args->idx - args->begin) < helloSz) { |
5536 | | /* A partial extensions length field is genuinely malformed: report |
5537 | | * it as a decode error regardless of message type. */ |
5538 | 0 | WOLFSSL_MSG("Truncated extensions length in ServerHello"); |
5539 | 0 | return BUFFER_ERROR; |
5540 | 0 | } |
5541 | | |
5542 | | /* No extensions field at all. */ |
5543 | 0 | if (args->extMsgType == hello_retry_request) { |
5544 | | /* The sentinel Random (RFC 8446 4.1.3) identifies this as a TLS 1.3 |
5545 | | * HelloRetryRequest, which MUST carry supported_versions |
5546 | | * (4.2.1/9.2). Its complete absence is a missing mandatory |
5547 | | * extension, so - consistently with the extensions-present case |
5548 | | * handled later - report it as missing_extension (via |
5549 | | * INCOMPLETE_DATA), regardless of whether a downgrade would |
5550 | | * otherwise be allowed. Reject here before DoServerHello would |
5551 | | * reinterpret the sentinel as a plain TLS 1.2 ServerHello.Random. */ |
5552 | 0 | WOLFSSL_MSG("HelloRetryRequest with no supported_versions"); |
5553 | 0 | WOLFSSL_ERROR_VERBOSE(INCOMPLETE_DATA); |
5554 | 0 | return INCOMPLETE_DATA; |
5555 | 0 | } |
5556 | | |
5557 | 0 | if (!ssl->options.downgrade) { |
5558 | | /* A plain ServerHello with no extensions is not offering TLS 1.3 |
5559 | | * (no supported_versions extension - see RFC 8446 4.2.1) but TLS |
5560 | | * 1.2 or below. This is a well-formed message, so a TLS 1.3-only |
5561 | | * client (downgrade disabled) must reject it as a version mismatch, |
5562 | | * not as a malformed message. Returning VERSION_ERROR makes the |
5563 | | * caller send a protocol_version alert (RFC 8446 6.2) rather than |
5564 | | * decode_error. */ |
5565 | 0 | WOLFSSL_MSG("Server offered TLS 1.2 (no supported_versions ext) " |
5566 | 0 | "but downgrade not allowed"); |
5567 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5568 | 0 | return VERSION_ERROR; |
5569 | 0 | } |
5570 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5571 | | /* Force client hello version 1.2 to work for static RSA. */ |
5572 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5573 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5574 | |
|
5575 | | #ifdef WOLFSSL_DTLS13 |
5576 | | if (ssl->options.dtls) { |
5577 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5578 | | ssl->version.minor = DTLSv1_2_MINOR; |
5579 | | ssl->options.tls1_3 = 0; |
5580 | | ret = Dtls13ClientDoDowngrade(ssl); |
5581 | | if (ret != 0) |
5582 | | return ret; |
5583 | | } |
5584 | | #endif /* WOLFSSL_DTLS13 */ |
5585 | |
|
5586 | 0 | #endif |
5587 | 0 | ssl->options.haveEMS = 0; |
5588 | 0 | if (args->pv.minor < ssl->options.minDowngrade) { |
5589 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5590 | 0 | return VERSION_ERROR; |
5591 | 0 | } |
5592 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5593 | 0 | ssl->options.tls1_3 = 0; |
5594 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5595 | | #else |
5596 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5597 | | return VERSION_ERROR; |
5598 | | #endif |
5599 | 0 | } |
5600 | | |
5601 | 0 | if ((args->idx - args->begin) < helloSz) { |
5602 | 0 | int foundVersion; |
5603 | | |
5604 | | /* Get extension length and length check. */ |
5605 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
5606 | 0 | return BUFFER_ERROR; |
5607 | 0 | ato16(&input[args->idx], &args->totalExtSz); |
5608 | 0 | args->idx += OPAQUE16_LEN; |
5609 | 0 | if ((args->idx - args->begin) + args->totalExtSz > helloSz) |
5610 | 0 | return BUFFER_ERROR; |
5611 | | |
5612 | | /* Need to negotiate version first. */ |
5613 | 0 | if ((ret = TLSX_ParseVersion(ssl, input + args->idx, |
5614 | 0 | args->totalExtSz, *extMsgType, &foundVersion))) { |
5615 | 0 | return ret; |
5616 | 0 | } |
5617 | 0 | if (!foundVersion) { |
5618 | | /* RFC 8446 4.1.4: "The server's extensions MUST contain |
5619 | | * 'supported_versions'." (also 9.2: "supported_versions" is |
5620 | | * REQUIRED for all ... HelloRetryRequest messages). The HRR random |
5621 | | * unambiguously identifies a TLS 1.3 server, so its absence is not |
5622 | | * a downgrade attempt but a missing mandatory extension, which per |
5623 | | * the "missing_extension" alert definition must be reported as |
5624 | | * such. Return INCOMPLETE_DATA (which maps to a missing_extension |
5625 | | * alert) and let the caller emit the alert via |
5626 | | * TranslateErrorToAlert(). */ |
5627 | 0 | if (*extMsgType == hello_retry_request) { |
5628 | 0 | WOLFSSL_MSG("HelloRetryRequest missing supported_versions " |
5629 | 0 | "extension"); |
5630 | 0 | WOLFSSL_ERROR_VERBOSE(INCOMPLETE_DATA); |
5631 | 0 | return INCOMPLETE_DATA; |
5632 | 0 | } |
5633 | 0 | if (!ssl->options.downgrade) { |
5634 | 0 | WOLFSSL_MSG("Server trying to downgrade to version less than " |
5635 | 0 | "TLS v1.3"); |
5636 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5637 | 0 | return VERSION_ERROR; |
5638 | 0 | } |
5639 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \ |
5640 | | defined(WOLFSSL_WPAS_SMALL) |
5641 | | /* Check if client has disabled TLS 1.2 */ |
5642 | | if (args->pv.minor == TLSv1_2_MINOR && |
5643 | | (ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) |
5644 | | == WOLFSSL_OP_NO_TLSv1_2) |
5645 | | { |
5646 | | WOLFSSL_MSG("\tOption set to not allow TLSv1.2"); |
5647 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5648 | | return VERSION_ERROR; |
5649 | | } |
5650 | | #endif |
5651 | | |
5652 | 0 | if (!ssl->options.dtls && |
5653 | 0 | args->pv.minor < ssl->options.minDowngrade) { |
5654 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5655 | 0 | return VERSION_ERROR; |
5656 | 0 | } |
5657 | | |
5658 | 0 | if (ssl->options.dtls && |
5659 | 0 | args->pv.minor > ssl->options.minDowngrade) { |
5660 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5661 | 0 | return VERSION_ERROR; |
5662 | 0 | } |
5663 | | |
5664 | 0 | ssl->version.minor = args->pv.minor; |
5665 | 0 | ssl->options.tls1_3 = 0; |
5666 | |
|
5667 | | #ifdef WOLFSSL_DTLS13 |
5668 | | if (ssl->options.dtls) { |
5669 | | ret = Dtls13ClientDoDowngrade(ssl); |
5670 | | if (ret != 0) |
5671 | | return ret; |
5672 | | } |
5673 | | #endif /* WOLFSSL_DTLS13 */ |
5674 | 0 | } |
5675 | 0 | } |
5676 | | |
5677 | | #ifdef WOLFSSL_DTLS13 |
5678 | | /* we are sure that version is >= v1.3 now, we can get rid of buffered |
5679 | | * ClientHello that was buffered to re-compute the hash in case of |
5680 | | * downgrade */ |
5681 | | if (ssl->options.dtls && ssl->dtls13ClientHello != NULL) { |
5682 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5683 | | ssl->dtls13ClientHello = NULL; |
5684 | | ssl->dtls13ClientHelloSz = 0; |
5685 | | } |
5686 | | #endif /* WOLFSSL_DTLS13 */ |
5687 | | |
5688 | | /* Advance state and proceed */ |
5689 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
5690 | 0 | } /* case TLS_ASYNC_BEGIN */ |
5691 | 0 | FALL_THROUGH; |
5692 | |
|
5693 | 0 | case TLS_ASYNC_BUILD: |
5694 | 0 | case TLS_ASYNC_DO: |
5695 | 0 | { |
5696 | | /* restore message type */ |
5697 | 0 | *extMsgType = args->extMsgType; |
5698 | | |
5699 | | /* Parse and handle extensions, unless lower than TLS1.3. In that case, |
5700 | | * extensions will be parsed in DoServerHello. */ |
5701 | 0 | if (args->totalExtSz > 0 && IsAtLeastTLSv1_3(ssl->version)) { |
5702 | 0 | ret = TLSX_Parse(ssl, input + args->idx, args->totalExtSz, |
5703 | 0 | *extMsgType, NULL); |
5704 | 0 | if (ret != 0) { |
5705 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5706 | | /* Handle async operation */ |
5707 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5708 | | /* Mark message as not received so it can process again */ |
5709 | | ssl->msgsReceived.got_server_hello = 0; |
5710 | | } |
5711 | | #endif |
5712 | 0 | return ret; |
5713 | 0 | } |
5714 | | |
5715 | 0 | if (*extMsgType == hello_retry_request) { |
5716 | | /* Update counts to reflect change of message type. */ |
5717 | 0 | ssl->msgsReceived.got_hello_retry_request = 1; |
5718 | 0 | ssl->msgsReceived.got_server_hello = 0; |
5719 | 0 | } |
5720 | 0 | } |
5721 | | |
5722 | 0 | if (args->totalExtSz > 0) { |
5723 | 0 | args->idx += args->totalExtSz; |
5724 | 0 | } |
5725 | |
|
5726 | | #ifdef WOLFSSL_DTLS_CID |
5727 | | if (ssl->options.useDtlsCID && *extMsgType == server_hello) |
5728 | | DtlsCIDOnExtensionsParsed(ssl); |
5729 | | #endif /* WOLFSSL_DTLS_CID */ |
5730 | |
|
5731 | 0 | if (IsAtLeastTLSv1_3(ssl->version)) { |
5732 | 0 | *inOutIdx = args->idx; |
5733 | 0 | } |
5734 | |
|
5735 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5736 | |
|
5737 | | #ifdef HAVE_SECRET_CALLBACK |
5738 | | if (ssl->sessionSecretCb != NULL |
5739 | | #ifdef HAVE_SESSION_TICKET |
5740 | | && ssl->session->ticketLen > 0 |
5741 | | #endif |
5742 | | ) { |
5743 | | int secretSz = SECRET_LEN; |
5744 | | ret = ssl->sessionSecretCb(ssl, ssl->session->masterSecret, |
5745 | | &secretSz, ssl->sessionSecretCtx); |
5746 | | if (ret != 0 || secretSz != SECRET_LEN) { |
5747 | | WOLFSSL_ERROR_VERBOSE(SESSION_SECRET_CB_E); |
5748 | | return SESSION_SECRET_CB_E; |
5749 | | } |
5750 | | } |
5751 | | #endif /* HAVE_SECRET_CALLBACK */ |
5752 | | |
5753 | | /* Version only negotiated in extensions for TLS v1.3. |
5754 | | * Only now do we know how to deal with session id. |
5755 | | */ |
5756 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) { |
5757 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5758 | 0 | ssl->arrays->sessionIDSz = args->sessIdSz; |
5759 | |
|
5760 | 0 | if (ssl->arrays->sessionIDSz > ID_LEN) { |
5761 | 0 | WOLFSSL_MSG("Invalid session ID size"); |
5762 | 0 | ssl->arrays->sessionIDSz = 0; |
5763 | 0 | return BUFFER_ERROR; |
5764 | 0 | } |
5765 | 0 | else if (ssl->arrays->sessionIDSz) { |
5766 | 0 | XMEMCPY(ssl->arrays->sessionID, args->sessId, |
5767 | 0 | ssl->arrays->sessionIDSz); |
5768 | 0 | ssl->options.haveSessionId = 1; |
5769 | 0 | } |
5770 | | |
5771 | | /* Force client hello version 1.2 to work for static RSA. */ |
5772 | 0 | if (ssl->options.dtls) |
5773 | 0 | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5774 | 0 | else |
5775 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5776 | | /* Complete TLS v1.2 processing of ServerHello. */ |
5777 | 0 | ret = DoServerHello(ssl, input, inOutIdx, helloSz); |
5778 | | #else |
5779 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
5780 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5781 | | ret = VERSION_ERROR; |
5782 | | #endif |
5783 | |
|
5784 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5785 | |
|
5786 | 0 | return ret; |
5787 | 0 | } |
5788 | | |
5789 | | /* Advance state and proceed */ |
5790 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
5791 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
5792 | 0 | FALL_THROUGH; |
5793 | |
|
5794 | 0 | case TLS_ASYNC_FINALIZE: |
5795 | 0 | { |
5796 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
5797 | | if (ssl->options.tls13MiddleBoxCompat) { |
5798 | | if (args->sessIdSz == 0) { |
5799 | | WOLFSSL_MSG("args->sessIdSz == 0"); |
5800 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5801 | | return INVALID_PARAMETER; |
5802 | | } |
5803 | | if (ssl->session->sessionIDSz != 0) { |
5804 | | if (ssl->session->sessionIDSz != args->sessIdSz || |
5805 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5806 | | args->sessIdSz) != 0) { |
5807 | | WOLFSSL_MSG("session id doesn't match"); |
5808 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5809 | | return INVALID_PARAMETER; |
5810 | | } |
5811 | | } |
5812 | | else if (XMEMCMP(ssl->arrays->clientRandom, args->sessId, |
5813 | | args->sessIdSz) != 0) { |
5814 | | WOLFSSL_MSG("session id doesn't match client random"); |
5815 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5816 | | return INVALID_PARAMETER; |
5817 | | } |
5818 | | } |
5819 | | else |
5820 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
5821 | | #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13) |
5822 | | if (0 |
5823 | | #ifdef WOLFSSL_QUIC |
5824 | | || WOLFSSL_IS_QUIC(ssl) |
5825 | | #endif |
5826 | | #ifdef WOLFSSL_DTLS13 |
5827 | | || ssl->options.dtls |
5828 | | #endif |
5829 | | ) { |
5830 | | /* RFC 9147 Section 5 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC |
5831 | | * ServerHello must have empty legacy_session_id_echo. */ |
5832 | | int requireEmptyEcho = 1; |
5833 | | #ifdef WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID |
5834 | | /* Compat: a wolfSSL <= 5.9.0 DTLS 1.3 server echoes the client's |
5835 | | * legacy_session_id back instead of omitting it. */ |
5836 | | if (ssl->options.dtls) |
5837 | | requireEmptyEcho = 0; |
5838 | | #endif |
5839 | | if (requireEmptyEcho && args->sessIdSz != 0) { |
5840 | | WOLFSSL_MSG("args->sessIdSz != 0"); |
5841 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5842 | | return INVALID_PARAMETER; |
5843 | | } |
5844 | | #ifdef WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID |
5845 | | /* An echoing wolfSSL <= 5.9.0 server must still send back what was |
5846 | | * sent (RFC 8446 Section 4.1.3), so don't accept an arbitrary value. |
5847 | | * An empty echo is the compliant server case and stays acceptable. */ |
5848 | | if (!requireEmptyEcho && args->sessIdSz != 0 && |
5849 | | (args->sessIdSz != ssl->session->sessionIDSz || |
5850 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5851 | | args->sessIdSz) != 0)) { |
5852 | | WOLFSSL_MSG("Server sent different session id"); |
5853 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5854 | | return INVALID_PARAMETER; |
5855 | | } |
5856 | | #endif |
5857 | | } |
5858 | | else |
5859 | | #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */ |
5860 | 0 | if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 && |
5861 | 0 | XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0)) |
5862 | 0 | { |
5863 | 0 | WOLFSSL_MSG("Server sent different session id"); |
5864 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5865 | 0 | return INVALID_PARAMETER; |
5866 | 0 | } |
5867 | | |
5868 | 0 | ret = SetCipherSpecs(ssl); |
5869 | 0 | if (ret != 0) |
5870 | 0 | return ret; |
5871 | | |
5872 | | #ifdef HAVE_NULL_CIPHER |
5873 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
5874 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
5875 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
5876 | | ; |
5877 | | } |
5878 | | else |
5879 | | #endif |
5880 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
5881 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5882 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
5883 | 0 | ; /* Do nothing. */ |
5884 | 0 | } |
5885 | 0 | else |
5886 | 0 | #endif |
5887 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
5888 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5889 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
5890 | 0 | ; /* Do nothing. */ |
5891 | 0 | } |
5892 | 0 | else |
5893 | 0 | #endif |
5894 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
5895 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
5896 | 0 | WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet"); |
5897 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5898 | 0 | return INVALID_PARAMETER; |
5899 | 0 | } |
5900 | | |
5901 | 0 | suite[0] = ssl->options.cipherSuite0; |
5902 | 0 | suite[1] = ssl->options.cipherSuite; |
5903 | 0 | if (!FindSuiteSSL(ssl, suite)) { |
5904 | 0 | WOLFSSL_MSG("Cipher suite not supported on client"); |
5905 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5906 | 0 | return INVALID_PARAMETER; |
5907 | 0 | } |
5908 | | |
5909 | | #if defined(HAVE_ECH) |
5910 | | /* check for acceptConfirmation */ |
5911 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5912 | | ssl->hsHashesEch != NULL) { |
5913 | | args->echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
5914 | | if (args->echX == NULL || args->echX->data == NULL) |
5915 | | return WOLFSSL_FATAL_ERROR; |
5916 | | |
5917 | | if (args->extMsgType == hello_retry_request && |
5918 | | ((WOLFSSL_ECH*)args->echX->data)->confBuf == NULL) { |
5919 | | /* server rejected ECH, fall back to outer */ |
5920 | | Free_HS_Hashes(ssl->hsHashesEch, ssl->heap); |
5921 | | ssl->hsHashesEch = NULL; |
5922 | | /* EchCheckAcceptance is bypassed, so replace extensions now */ |
5923 | | ret = TLSX_EchReplaceExtensions(ssl, 0); |
5924 | | if (ret != 0) |
5925 | | return ret; |
5926 | | } |
5927 | | else { |
5928 | | /* account for hrr extension instead of server random */ |
5929 | | if (args->extMsgType == hello_retry_request) { |
5930 | | args->acceptOffset = |
5931 | | (word32)(((WOLFSSL_ECH*)args->echX->data)->confBuf - input); |
5932 | | args->acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
5933 | | args->acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
5934 | | } |
5935 | | else { |
5936 | | args->acceptLabel = (byte*)echAcceptConfirmationLabel; |
5937 | | args->acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
5938 | | } |
5939 | | /* check acceptance */ |
5940 | | if (ret == 0) { |
5941 | | ret = EchCheckAcceptance(ssl, args->acceptLabel, |
5942 | | args->acceptLabelSz, input, args->acceptOffset, helloSz, |
5943 | | args->extMsgType); |
5944 | | } |
5945 | | if (ret != 0) |
5946 | | return ret; |
5947 | | /* use the inner random for client random */ |
5948 | | if (args->extMsgType != hello_retry_request && |
5949 | | ssl->options.echAccepted) { |
5950 | | XMEMCPY(ssl->arrays->clientRandom, |
5951 | | ssl->arrays->clientRandomInner, RAN_LEN); |
5952 | | } |
5953 | | } |
5954 | | } |
5955 | | #endif /* HAVE_ECH */ |
5956 | | |
5957 | 0 | if (*extMsgType == server_hello) { |
5958 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5959 | | PreSharedKey* psk = NULL; |
5960 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
5961 | | if (ext != NULL) |
5962 | | psk = (PreSharedKey*)ext->data; |
5963 | | while (psk != NULL && !psk->chosen) |
5964 | | psk = psk->next; |
5965 | | if (psk == NULL) { |
5966 | | /* A mandatory PSK is satisfied by any PSK the server chose, |
5967 | | * including a resumption PSK - this matches the server-side |
5968 | | * failNoPSK semantics, where a negotiated PSK (external or |
5969 | | * resumption) is accepted. The error only fires when no PSK was |
5970 | | * chosen at all. havePSK is only set by an external-PSK callback, |
5971 | | * so a peer relying solely on session-ticket resumption is |
5972 | | * unaffected. */ |
5973 | | if (ssl->options.havePSK && ssl->options.failNoPSK) { |
5974 | | WOLFSSL_MSG("Server did not negotiate a mandatory PSK"); |
5975 | | WOLFSSL_ERROR_VERBOSE(PSK_MISSING_ERROR); |
5976 | | return PSK_MISSING_ERROR; |
5977 | | } |
5978 | | ssl->options.resuming = 0; |
5979 | | ssl->arrays->psk_keySz = 0; |
5980 | | XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); |
5981 | | } |
5982 | | else { |
5983 | | #if defined(HAVE_ECH) |
5984 | | /* do not resume when outerHandshake will be negotiated */ |
5985 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5986 | | !ssl->options.echAccepted) { |
5987 | | WOLFSSL_MSG("ECH rejected but server negotiated PSK"); |
5988 | | return INVALID_PARAMETER; |
5989 | | } |
5990 | | #endif |
5991 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
5992 | | if (ssl->options.certWithExternPsk && psk->resumption) { |
5993 | | /* RFC 9973 mode requires external PSK, not ticket resumption. */ |
5994 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
5995 | | return PSK_KEY_ERROR; |
5996 | | } |
5997 | | if (ssl->options.certWithExternPsk && ssl->options.shSentKeyShare == 0) { |
5998 | | /* RFC 9973 Sect. 3: cert_with_extern_psk requires psk_dhe_ke; |
5999 | | * a ServerHello without a key_share confirms only psk_ke. */ |
6000 | | WOLFSSL_MSG("cert_with_extern_psk: ServerHello missing key_share"); |
6001 | | WOLFSSL_ERROR_VERBOSE(EXT_MISSING); |
6002 | | return EXT_MISSING; |
6003 | | } |
6004 | | #endif |
6005 | | if ((ret = SetupPskKey(ssl, psk, 0)) != 0) |
6006 | | return ret; |
6007 | | ssl->options.pskNegotiated = 1; |
6008 | | } |
6009 | | #else |
6010 | | /* no resumption possible */ |
6011 | 0 | ssl->options.resuming = 0; |
6012 | 0 | #endif |
6013 | | |
6014 | | /* sanity check on PSK / KSE */ |
6015 | 0 | if ( |
6016 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
6017 | | ssl->options.pskNegotiated == 0 && |
6018 | | #endif |
6019 | 0 | (ssl->session->namedGroup == 0 || |
6020 | 0 | ssl->options.shSentKeyShare == 0)) { |
6021 | 0 | return EXT_MISSING; |
6022 | 0 | } |
6023 | | |
6024 | 0 | ssl->keys.encryptionOn = 1; |
6025 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
6026 | |
|
6027 | 0 | } |
6028 | 0 | else { |
6029 | | /* https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.4 |
6030 | | * Clients MUST abort the handshake with an |
6031 | | * "illegal_parameter" alert if the HelloRetryRequest would not result |
6032 | | * in any change in the ClientHello. |
6033 | | */ |
6034 | | /* Check if the HRR contained a cookie or a keyshare */ |
6035 | 0 | if (!ssl->options.hrrSentKeyShare |
6036 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
6037 | | && !ssl->options.hrrSentCookie |
6038 | | #endif |
6039 | 0 | ) { |
6040 | 0 | SendAlert(ssl, alert_fatal, illegal_parameter); |
6041 | 0 | return EXT_MISSING; |
6042 | 0 | } |
6043 | | |
6044 | 0 | ssl->options.tls1_3 = 1; |
6045 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
6046 | |
|
6047 | 0 | ret = RestartHandshakeHash(ssl); |
6048 | 0 | } |
6049 | | |
6050 | 0 | break; |
6051 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
6052 | 0 | default: |
6053 | 0 | ret = INPUT_CASE_ERROR; |
6054 | 0 | } /* switch (ssl->options.asyncState) */ |
6055 | | |
6056 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6057 | | if (ret == 0) |
6058 | | FreeAsyncCtx(ssl, 0); |
6059 | | #endif |
6060 | | |
6061 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
6062 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO); |
6063 | |
|
6064 | 0 | return ret; |
6065 | 0 | } |
6066 | | |
6067 | | /* handle processing TLS 1.3 encrypted_extensions (8) */ |
6068 | | /* Parse and handle an EncryptedExtensions message. |
6069 | | * Only a client will receive this message. |
6070 | | * |
6071 | | * ssl The SSL/TLS object. |
6072 | | * input The message buffer. |
6073 | | * inOutIdx On entry, the index into the message buffer of |
6074 | | * EncryptedExtensions. |
6075 | | * On exit, the index of byte after the EncryptedExtensions |
6076 | | * message. |
6077 | | * totalSz The length of the current handshake message. |
6078 | | * returns 0 on success and otherwise failure. |
6079 | | */ |
6080 | | static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input, |
6081 | | word32* inOutIdx, word32 totalSz) |
6082 | 0 | { |
6083 | 0 | int ret; |
6084 | 0 | word32 begin = *inOutIdx; |
6085 | 0 | word32 i = begin; |
6086 | 0 | word16 totalExtSz; |
6087 | |
|
6088 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
6089 | 0 | WOLFSSL_ENTER("DoTls13EncryptedExtensions"); |
6090 | |
|
6091 | | #ifdef WOLFSSL_CALLBACKS |
6092 | | if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions"); |
6093 | | if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo); |
6094 | | #endif |
6095 | | |
6096 | | /* Length field of extension data. */ |
6097 | 0 | if (totalSz < OPAQUE16_LEN) |
6098 | 0 | return BUFFER_ERROR; |
6099 | 0 | ato16(&input[i], &totalExtSz); |
6100 | 0 | i += OPAQUE16_LEN; |
6101 | | |
6102 | | /* Extension data. */ |
6103 | 0 | if (i - begin + totalExtSz != totalSz) |
6104 | 0 | return BUFFER_ERROR; |
6105 | 0 | if ((ret = TLSX_Parse(ssl, input + i, totalExtSz, encrypted_extensions, |
6106 | 0 | NULL))) { |
6107 | 0 | return ret; |
6108 | 0 | } |
6109 | | |
6110 | | /* Move index to byte after message. */ |
6111 | 0 | *inOutIdx = i + totalExtSz; |
6112 | |
|
6113 | | #ifdef WOLFSSL_EARLY_DATA |
6114 | | if (ssl->earlyData != no_early_data) { |
6115 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6116 | | if (ext == NULL || !ext->val) { |
6117 | | WOLFSSL_MSG("Early data rejected by server (no early_data " |
6118 | | "EncryptedExtensions response)"); |
6119 | | ssl->earlyData = no_early_data; |
6120 | | } |
6121 | | } |
6122 | | |
6123 | | if (ssl->earlyData == no_early_data) { |
6124 | | ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY); |
6125 | | if (ret != 0) |
6126 | | return ret; |
6127 | | } |
6128 | | #endif /* WOLFSSL_EARLY_DATA */ |
6129 | |
|
6130 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
6131 | |
|
6132 | 0 | WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret); |
6133 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
6134 | |
|
6135 | 0 | return ret; |
6136 | 0 | } |
6137 | | |
6138 | | #ifndef NO_CERTS |
6139 | | /* handle processing TLS v1.3 certificate_request (13) */ |
6140 | | /* Handle a TLS v1.3 CertificateRequest message. |
6141 | | * This message is always encrypted. |
6142 | | * Only a client will receive this message. |
6143 | | * |
6144 | | * ssl The SSL/TLS object. |
6145 | | * input The message buffer. |
6146 | | * inOutIdx On entry, the index into the message buffer of CertificateRequest. |
6147 | | * On exit, the index of byte after the CertificateRequest message. |
6148 | | * size The length of the current handshake message. |
6149 | | * returns 0 on success and otherwise failure. |
6150 | | */ |
6151 | | static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input, |
6152 | | word32* inOutIdx, word32 size) |
6153 | 0 | { |
6154 | 0 | word16 len; |
6155 | 0 | word32 begin = *inOutIdx; |
6156 | 0 | int ret = 0; |
6157 | 0 | Suites peerSuites; |
6158 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6159 | | word16 reqCtxLen; |
6160 | | const byte* reqCtxData; |
6161 | | #endif |
6162 | |
|
6163 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO); |
6164 | 0 | WOLFSSL_ENTER("DoTls13CertificateRequest"); |
6165 | |
|
6166 | 0 | XMEMSET(&peerSuites, 0, sizeof(Suites)); |
6167 | |
|
6168 | | #ifdef WOLFSSL_CALLBACKS |
6169 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest"); |
6170 | | if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo); |
6171 | | #endif |
6172 | |
|
6173 | 0 | if (OPAQUE8_LEN > size) |
6174 | 0 | return BUFFER_ERROR; |
6175 | | |
6176 | | /* Length of the request context. */ |
6177 | 0 | len = input[(*inOutIdx)++]; |
6178 | 0 | if ((*inOutIdx - begin) + len > size) |
6179 | 0 | return BUFFER_ERROR; |
6180 | | /* INVALID_PARAMETER does not map to illegal_parameter in the central |
6181 | | * alert path, so emit the alert explicitly before returning. */ |
6182 | 0 | if (ssl->options.connectState < FINISHED_DONE) { |
6183 | | /* RFC 8446 Section 4.3.2: in the handshake the context is zero |
6184 | | * length. */ |
6185 | 0 | if (len > 0) { |
6186 | 0 | SendAlert(ssl, alert_fatal, illegal_parameter); |
6187 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6188 | 0 | return INVALID_PARAMETER; |
6189 | 0 | } |
6190 | 0 | } |
6191 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6192 | | else if (len == 0) { |
6193 | | /* RFC 8446 Section 4.3.2: a post-handshake CertificateRequest context |
6194 | | * MUST be non-empty and unique for the connection. */ |
6195 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
6196 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6197 | | return INVALID_PARAMETER; |
6198 | | } |
6199 | | #endif |
6200 | | |
6201 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6202 | | /* Remember the request context bytes; the CertReqCtx allocation and |
6203 | | * linking into ssl->certReqCtx is deferred until after the rest of the |
6204 | | * message has been validated. |
6205 | | */ |
6206 | | reqCtxLen = len; |
6207 | | reqCtxData = input + *inOutIdx; |
6208 | | /* Reject a context that duplicates one still pending on the connection. */ |
6209 | | if (ssl->options.connectState >= FINISHED_DONE) { |
6210 | | CertReqCtx* dup; |
6211 | | for (dup = ssl->certReqCtx; dup != NULL; dup = dup->next) { |
6212 | | if (dup->len == reqCtxLen && |
6213 | | XMEMCMP(&dup->ctx, reqCtxData, reqCtxLen) == 0) { |
6214 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
6215 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6216 | | return INVALID_PARAMETER; |
6217 | | } |
6218 | | } |
6219 | | } |
6220 | | #endif |
6221 | 0 | *inOutIdx += len; |
6222 | | |
6223 | | /* TODO: Add support for more extensions: |
6224 | | * signed_certificate_timestamp, certificate_authorities, oid_filters. |
6225 | | */ |
6226 | | /* Certificate extensions */ |
6227 | 0 | if ((*inOutIdx - begin) + OPAQUE16_LEN > size) |
6228 | 0 | return BUFFER_ERROR; |
6229 | 0 | ato16(input + *inOutIdx, &len); |
6230 | 0 | *inOutIdx += OPAQUE16_LEN; |
6231 | 0 | if ((*inOutIdx - begin) + len > size) |
6232 | 0 | return BUFFER_ERROR; |
6233 | | /* RFC 9846 Section 4.4.2: CertificateRequest.extensions has a lower bound of |
6234 | | * 0, so an empty extensions block is parsed rather than rejected here. A |
6235 | | * request missing the mandatory signature_algorithms extension is caught by |
6236 | | * the check below. */ |
6237 | 0 | if ((ret = TLSX_Parse(ssl, input + *inOutIdx, len, certificate_request, |
6238 | 0 | &peerSuites))) { |
6239 | 0 | return ret; |
6240 | 0 | } |
6241 | 0 | *inOutIdx += len; |
6242 | | |
6243 | | /* No trailing bytes allowed (RFC 8446 4.3.2). */ |
6244 | 0 | if ((*inOutIdx - begin) != size) |
6245 | 0 | return BUFFER_ERROR; |
6246 | | |
6247 | | /* RFC 8446 Section 4.3.2: the signature_algorithms extension MUST be |
6248 | | * present in a CertificateRequest. */ |
6249 | 0 | if (peerSuites.hashSigAlgoSz == 0) { |
6250 | 0 | SendAlert(ssl, alert_fatal, missing_extension); |
6251 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6252 | 0 | return INVALID_PARAMETER; |
6253 | 0 | } |
6254 | | #ifdef WOLFSSL_CERT_SETUP_CB |
6255 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
6256 | | return ret; |
6257 | | #endif |
6258 | | |
6259 | | #if defined(HAVE_ECH) |
6260 | | /* RFC 9849 s6.1.7: ECH was offered but rejected by the server... |
6261 | | * the client MUST respond with an empty Certificate message. */ |
6262 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
6263 | | !ssl->options.echAccepted) { |
6264 | | ssl->options.sendVerify = SEND_BLANK_CERT; |
6265 | | } |
6266 | | else |
6267 | | #endif |
6268 | 0 | if ((ssl->buffers.certificate && ssl->buffers.certificate->buffer && |
6269 | 0 | ((ssl->buffers.key && ssl->buffers.key->buffer) |
6270 | | #ifdef HAVE_PK_CALLBACKS |
6271 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
6272 | | #endif |
6273 | 0 | )) |
6274 | | #ifdef OPENSSL_EXTRA |
6275 | | || ssl->ctx->certSetupCb != NULL |
6276 | | #endif |
6277 | 0 | ) { |
6278 | 0 | if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo, |
6279 | 0 | peerSuites.hashSigAlgoSz, 0) != 0) { |
6280 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6281 | 0 | return INVALID_PARAMETER; |
6282 | 0 | } |
6283 | 0 | ssl->options.sendVerify = SEND_CERT; |
6284 | 0 | } |
6285 | 0 | else { |
6286 | 0 | #ifndef WOLFSSL_NO_CLIENT_CERT_ERROR |
6287 | 0 | ssl->options.sendVerify = SEND_BLANK_CERT; |
6288 | | #else |
6289 | | WOLFSSL_MSG("Certificate required but none set on client"); |
6290 | | /* RFC 8446 Section 4.4.2.4: send certificate_required when a |
6291 | | * peer (here, the client) cannot provide a certificate that the |
6292 | | * other peer required. */ |
6293 | | SendAlert(ssl, alert_fatal, certificate_required); |
6294 | | WOLFSSL_ERROR_VERBOSE(NO_CERT_ERROR); |
6295 | | return NO_CERT_ERROR; |
6296 | | #endif |
6297 | 0 | } |
6298 | | |
6299 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6300 | | { |
6301 | | /* CertReqCtx has one byte at end for context value. |
6302 | | * Increase size to handle other implementations sending more than one byte. |
6303 | | * That is, allocate extra space, over one byte, to hold the context value. |
6304 | | */ |
6305 | | CertReqCtx* certReqCtx = (CertReqCtx*)XMALLOC( |
6306 | | sizeof(CertReqCtx) + (reqCtxLen == 0 ? 0 : reqCtxLen - 1), |
6307 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
6308 | | if (certReqCtx == NULL) |
6309 | | return MEMORY_E; |
6310 | | certReqCtx->next = ssl->certReqCtx; |
6311 | | certReqCtx->len = reqCtxLen; |
6312 | | XMEMCPY(&certReqCtx->ctx, reqCtxData, reqCtxLen); |
6313 | | ssl->certReqCtx = certReqCtx; |
6314 | | } |
6315 | | #endif |
6316 | | |
6317 | 0 | WOLFSSL_LEAVE("DoTls13CertificateRequest", ret); |
6318 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO); |
6319 | |
|
6320 | 0 | return ret; |
6321 | 0 | } |
6322 | | #endif /* !NO_CERTS */ |
6323 | | #endif /* !NO_WOLFSSL_CLIENT */ |
6324 | | |
6325 | | #ifndef NO_WOLFSSL_SERVER |
6326 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
6327 | | #ifndef NO_PSK |
6328 | | int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key, |
6329 | | word32* psk_keySz, const byte* suite, int* found, byte* foundSuite) |
6330 | | { |
6331 | | const char* cipherName = NULL; |
6332 | | byte cipherSuite0 = TLS13_BYTE; |
6333 | | byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
6334 | | int ret = 0; |
6335 | | |
6336 | | *found = 0; |
6337 | | (void)suite; |
6338 | | |
6339 | | if (ssl->options.server_psk_tls13_cb != NULL) { |
6340 | | *psk_keySz = ssl->options.server_psk_tls13_cb((WOLFSSL*)ssl, |
6341 | | (char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName); |
6342 | | if (*psk_keySz != 0) { |
6343 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
6344 | | *found = (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
6345 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0); |
6346 | | (void)cipherSuiteFlags; |
6347 | | } |
6348 | | } |
6349 | | if (*found == 0 && (ssl->options.server_psk_cb != NULL)) { |
6350 | | *psk_keySz = ssl->options.server_psk_cb((WOLFSSL*)ssl, |
6351 | | (char*)psk->identity, psk_key, |
6352 | | MAX_PSK_KEY_LEN); |
6353 | | *found = (*psk_keySz != 0); |
6354 | | } |
6355 | | if (*found) { |
6356 | | if (*psk_keySz > MAX_PSK_KEY_LEN && |
6357 | | (int)*psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK)) { |
6358 | | WOLFSSL_MSG("Key len too long in FindPsk()"); |
6359 | | ret = PSK_KEY_ERROR; |
6360 | | WOLFSSL_ERROR_VERBOSE(ret); |
6361 | | *found = 0; |
6362 | | } |
6363 | | if (ret == 0) { |
6364 | | #if !defined(WOLFSSL_PSK_ONE_ID) && !defined(WOLFSSL_PRIORITIZE_PSK) |
6365 | | /* Check whether PSK ciphersuite is in SSL. */ |
6366 | | *found = (suite[0] == cipherSuite0) && (suite[1] == cipherSuite); |
6367 | | #else |
6368 | | (void)suite; |
6369 | | /* Check whether PSK ciphersuite is in SSL. */ |
6370 | | { |
6371 | | byte s[2] = { |
6372 | | cipherSuite0, |
6373 | | cipherSuite, |
6374 | | }; |
6375 | | *found = FindSuiteSSL(ssl, s); |
6376 | | } |
6377 | | #endif |
6378 | | } |
6379 | | } |
6380 | | if (*found && foundSuite != NULL) { |
6381 | | foundSuite[0] = cipherSuite0; |
6382 | | foundSuite[1] = cipherSuite; |
6383 | | } |
6384 | | |
6385 | | return ret; |
6386 | | } |
6387 | | |
6388 | | /* Attempt to find the PSK (not session ticket) that matches. |
6389 | | * |
6390 | | * @param [in, out] ssl The SSL/TLS object. |
6391 | | * @param [in] psk A pre-shared key from the extension. |
6392 | | * @param [out] suite Cipher suite to use with PSK. |
6393 | | * @param [out] err Error code. |
6394 | | * PSK_KEY_ERROR when key is too big, |
6395 | | * UNSUPPORTED_SUITE on invalid suite. |
6396 | | * Other error when attempting to derive early secret. |
6397 | | * @return 1 when a match found - but check error code. |
6398 | | * @return 0 when no match found. |
6399 | | */ |
6400 | | static int FindPsk(WOLFSSL* ssl, PreSharedKey* psk, const byte* suite, int* err) |
6401 | | { |
6402 | | int ret = 0; |
6403 | | int found = 0; |
6404 | | byte foundSuite[SUITE_LEN]; |
6405 | | |
6406 | | WOLFSSL_ENTER("FindPsk"); |
6407 | | |
6408 | | XMEMSET(foundSuite, 0, sizeof(foundSuite)); |
6409 | | |
6410 | | ret = FindPskSuite(ssl, psk, ssl->arrays->psk_key, &ssl->arrays->psk_keySz, |
6411 | | suite, &found, foundSuite); |
6412 | | if (ret == 0 && found) { |
6413 | | /* This identity matched via external PSK callback, not ticket resume. */ |
6414 | | psk->resumption = 0; |
6415 | | /* Default to ciphersuite if cb doesn't specify. */ |
6416 | | ssl->options.resuming = 0; |
6417 | | /* Don't send certificate request when using PSK. */ |
6418 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6419 | | if (!ssl->options.certWithExternPsk) |
6420 | | #endif |
6421 | | ssl->options.verifyPeer = 0; |
6422 | | |
6423 | | /* obfuscated_ticket_age is not checked: RFC 8446 Section 4.2.11 |
6424 | | * requires servers to ignore it for an external identity. */ |
6425 | | /* Set PSK ciphersuite into SSL. */ |
6426 | | ssl->options.cipherSuite0 = foundSuite[0]; |
6427 | | ssl->options.cipherSuite = foundSuite[1]; |
6428 | | ret = SetCipherSpecs(ssl); |
6429 | | if (ret == 0) { |
6430 | | /* Derive the early secret using the PSK. */ |
6431 | | ret = DeriveEarlySecret(ssl); |
6432 | | } |
6433 | | if (ret == 0) { |
6434 | | /* PSK negotiation has succeeded */ |
6435 | | ssl->options.isPSK = 1; |
6436 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6437 | | if (!ssl->options.certWithExternPsk) |
6438 | | #endif |
6439 | | { |
6440 | | /* SERVER: using PSK for peer authentication. */ |
6441 | | ssl->options.peerAuthGood = 1; |
6442 | | } |
6443 | | } |
6444 | | } |
6445 | | |
6446 | | *err = ret; |
6447 | | WOLFSSL_LEAVE("FindPsk", found); |
6448 | | WOLFSSL_LEAVE("FindPsk", ret); |
6449 | | return found; |
6450 | | } |
6451 | | #endif /* !NO_PSK */ |
6452 | | |
6453 | | /* Handle any Pre-Shared Key (PSK) extension. |
6454 | | * Find a PSK that supports the cipher suite passed in. |
6455 | | * |
6456 | | * ssl SSL/TLS object. |
6457 | | * suite Cipher suite to find PSK for. |
6458 | | * usingPSK 1=Indicates handshake is using Pre-Shared Keys (2=Ephemeral) |
6459 | | * first Set to 1 if first in extension |
6460 | | * returns 0 on success and otherwise failure. |
6461 | | */ |
6462 | | static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz, |
6463 | | const byte* suite, int* usingPSK, int* first) |
6464 | | { |
6465 | | int ret = 0; |
6466 | | TLSX* ext; |
6467 | | PreSharedKey* current; |
6468 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
6469 | | byte binder[WC_MAX_DIGEST_SIZE]; |
6470 | | word32 binderLen; |
6471 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6472 | | int certWithExternOffered = 0; |
6473 | | #endif |
6474 | | |
6475 | | #ifdef NO_PSK |
6476 | | (void) suite; /* to avoid unused var warning when not used */ |
6477 | | #endif |
6478 | | |
6479 | | WOLFSSL_ENTER("DoPreSharedKeys"); |
6480 | | |
6481 | | (void)suite; |
6482 | | |
6483 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
6484 | | /* Poison and register binderKey up front; every exit below (including the |
6485 | | * error paths) funnels through the cleanup label which zeroes it. */ |
6486 | | XMEMSET(binderKey, 0xff, sizeof(binderKey)); |
6487 | | wc_MemZero_Add("DoPreSharedKeys binderKey", binderKey, sizeof(binderKey)); |
6488 | | #endif |
6489 | | |
6490 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6491 | | if (ext == NULL) { |
6492 | | WOLFSSL_MSG("No pre shared extension keys found"); |
6493 | | ret = BAD_FUNC_ARG; |
6494 | | goto cleanup; |
6495 | | } |
6496 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6497 | | certWithExternOffered = |
6498 | | TLSX_Find(ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK) != NULL; |
6499 | | #endif |
6500 | | |
6501 | | /* Look through all client's pre-shared keys for a match. */ |
6502 | | for (current = (PreSharedKey*)ext->data; current != NULL; |
6503 | | current = current->next) { |
6504 | | #ifndef NO_PSK |
6505 | | if (current->identityLen > MAX_PSK_ID_LEN) { |
6506 | | ret = BUFFER_ERROR; |
6507 | | goto cleanup; |
6508 | | } |
6509 | | XMEMCPY(ssl->arrays->client_identity, current->identity, |
6510 | | current->identityLen); |
6511 | | ssl->arrays->client_identity[current->identityLen] = '\0'; |
6512 | | #endif |
6513 | | |
6514 | | #ifdef HAVE_SESSION_TICKET |
6515 | | /* Decode the identity. */ |
6516 | | switch (current->decryptRet) { |
6517 | | case PSK_DECRYPT_NONE: |
6518 | | ret = DoClientTicket_ex(ssl, current, 1); |
6519 | | /* psk->sess may be set. Need to clean up later. */ |
6520 | | break; |
6521 | | case PSK_DECRYPT_OK: |
6522 | | ret = WOLFSSL_TICKET_RET_OK; |
6523 | | break; |
6524 | | case PSK_DECRYPT_CREATE: |
6525 | | ret = WOLFSSL_TICKET_RET_CREATE; |
6526 | | break; |
6527 | | case PSK_DECRYPT_FAIL: |
6528 | | ret = WOLFSSL_TICKET_RET_REJECT; |
6529 | | break; |
6530 | | } |
6531 | | |
6532 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6533 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
6534 | | goto cleanup; |
6535 | | #endif |
6536 | | |
6537 | | if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) { |
6538 | | current->sess_free_cb(ssl, current->sess, |
6539 | | ¤t->sess_free_cb_ctx); |
6540 | | current->sess = NULL; |
6541 | | current->sess_free_cb = NULL; |
6542 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6543 | | sizeof(psk_sess_free_cb_ctx)); |
6544 | | } |
6545 | | if (ret == WOLFSSL_TICKET_RET_OK) { |
6546 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6547 | | /* RFC 9973 Sect. 5.1: all PSKs listed alongside |
6548 | | * tls_cert_with_extern_psk MUST be external PSKs. A successfully |
6549 | | * decrypted session ticket identity is a resumption PSK, so the |
6550 | | * server MUST abort with illegal_parameter regardless of whether |
6551 | | * the ticket would otherwise be acceptable. Check here, before |
6552 | | * DoClientTicketFinalize, to avoid polluting ssl->session with |
6553 | | * ticket state that will not be used. */ |
6554 | | if (certWithExternOffered) { |
6555 | | if (current->sess_free_cb != NULL) { |
6556 | | current->sess_free_cb(ssl, current->sess, |
6557 | | ¤t->sess_free_cb_ctx); |
6558 | | current->sess = NULL; |
6559 | | current->sess_free_cb = NULL; |
6560 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6561 | | sizeof(psk_sess_free_cb_ctx)); |
6562 | | } |
6563 | | ret = PSK_KEY_ERROR; |
6564 | | WOLFSSL_ERROR_VERBOSE(ret); |
6565 | | goto cleanup; |
6566 | | } |
6567 | | #endif |
6568 | | ret = DoClientTicketCheck(ssl, current, ssl->timeout, suite); |
6569 | | #if defined(HAVE_SNI) || defined(HAVE_ALPN) |
6570 | | if (ret == 0) { |
6571 | | /* Decline this PSK if the SNI/ALPN bound to the ticket |
6572 | | * does not match the current connection. RFC 6066 Sect. |
6573 | | * 3 mandates this for SNI; wolfSSL applies the same |
6574 | | * policy to ALPN as defense in depth. Skipping the PSK |
6575 | | * (rather than aborting) lets the server try the next |
6576 | | * candidate or fall back to a full handshake naturally |
6577 | | * without unwinding committed PSK state. ALPN_Select |
6578 | | * has already run earlier in DoTls13ClientHello so the |
6579 | | * negotiated ALPN is available to TicketAlpnHash. */ |
6580 | | byte curHash[TICKET_BINDING_HASH_SZ]; |
6581 | | #ifdef HAVE_SNI |
6582 | | if (TicketSniHash(ssl, curHash) != 0 || |
6583 | | XMEMCMP(curHash, current->it->sniHash, |
6584 | | TICKET_BINDING_HASH_SZ) != 0) { |
6585 | | WOLFSSL_MSG("Ticket SNI mismatch, skipping PSK"); |
6586 | | ret = WOLFSSL_FATAL_ERROR; |
6587 | | } |
6588 | | #endif |
6589 | | #ifdef HAVE_ALPN |
6590 | | if (ret == 0 && |
6591 | | (TicketAlpnHash(ssl, curHash) != 0 || |
6592 | | XMEMCMP(curHash, current->it->alpnHash, |
6593 | | TICKET_BINDING_HASH_SZ) != 0)) { |
6594 | | WOLFSSL_MSG("Ticket ALPN mismatch, skipping PSK"); |
6595 | | ret = WOLFSSL_FATAL_ERROR; |
6596 | | } |
6597 | | #endif |
6598 | | } |
6599 | | #endif |
6600 | | if (ret == 0) |
6601 | | DoClientTicketFinalize(ssl, current->it, current->sess); |
6602 | | if (current->sess_free_cb != NULL) { |
6603 | | current->sess_free_cb(ssl, current->sess, |
6604 | | ¤t->sess_free_cb_ctx); |
6605 | | current->sess = NULL; |
6606 | | current->sess_free_cb = NULL; |
6607 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6608 | | sizeof(psk_sess_free_cb_ctx)); |
6609 | | } |
6610 | | if (ret != 0) |
6611 | | continue; |
6612 | | |
6613 | | /* SERVER: using secret in session ticket for peer auth. */ |
6614 | | ssl->options.peerAuthGood = 1; |
6615 | | |
6616 | | #ifdef WOLFSSL_EARLY_DATA |
6617 | | ssl->options.maxEarlyDataSz = ssl->session->maxEarlyDataSz; |
6618 | | #endif |
6619 | | /* Use the same cipher suite as before and set up for use. */ |
6620 | | ssl->options.cipherSuite0 = ssl->session->cipherSuite0; |
6621 | | ssl->options.cipherSuite = ssl->session->cipherSuite; |
6622 | | ret = SetCipherSpecs(ssl); |
6623 | | if (ret != 0) |
6624 | | goto cleanup; |
6625 | | |
6626 | | /* Resumption PSK is resumption master secret. */ |
6627 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
6628 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
6629 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
6630 | | goto cleanup; |
6631 | | } |
6632 | | |
6633 | | /* Derive the early secret using the PSK. */ |
6634 | | ret = DeriveEarlySecret(ssl); |
6635 | | if (ret != 0) |
6636 | | goto cleanup; |
6637 | | |
6638 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6639 | | ret = HashInput(ssl, input, (int)inputSz); |
6640 | | if (ret < 0) |
6641 | | goto cleanup; |
6642 | | |
6643 | | /* Derive the binder key to use with HMAC. */ |
6644 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
6645 | | if (ret != 0) |
6646 | | goto cleanup; |
6647 | | } |
6648 | | else |
6649 | | #endif /* HAVE_SESSION_TICKET */ |
6650 | | #ifndef NO_PSK |
6651 | | if (FindPsk(ssl, current, suite, &ret)) { |
6652 | | if (ret != 0) |
6653 | | goto cleanup; |
6654 | | |
6655 | | ret = HashInput(ssl, input, (int)inputSz); |
6656 | | if (ret < 0) |
6657 | | goto cleanup; |
6658 | | |
6659 | | /* Derive the binder key to use with HMAC. */ |
6660 | | ret = DeriveBinderKey(ssl, binderKey); |
6661 | | if (ret != 0) |
6662 | | goto cleanup; |
6663 | | } |
6664 | | else |
6665 | | #endif |
6666 | | { |
6667 | | continue; |
6668 | | } |
6669 | | |
6670 | | ssl->options.sendVerify = 0; |
6671 | | |
6672 | | /* Derive the Finished message secret. */ |
6673 | | ret = DeriveFinishedSecret(ssl, binderKey, |
6674 | | ssl->keys.client_write_MAC_secret, |
6675 | | 0 /* neither end */); |
6676 | | if (ret != 0) |
6677 | | goto cleanup; |
6678 | | |
6679 | | /* Derive the binder and compare with the one in the extension. */ |
6680 | | ret = BuildTls13HandshakeHmac(ssl, |
6681 | | ssl->keys.client_write_MAC_secret, binder, &binderLen); |
6682 | | if (ret != 0) |
6683 | | goto cleanup; |
6684 | | if (binderLen != current->binderLen || |
6685 | | ConstantCompare(binder, current->binder, |
6686 | | binderLen) != 0) { |
6687 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6688 | | ret = BAD_BINDER; |
6689 | | goto cleanup; |
6690 | | } |
6691 | | |
6692 | | /* This PSK works, no need to try any more. */ |
6693 | | current->chosen = 1; |
6694 | | ext->resp = 1; |
6695 | | break; |
6696 | | } |
6697 | | |
6698 | | if (current == NULL) { |
6699 | | ret = 0; |
6700 | | goto cleanup; |
6701 | | } |
6702 | | |
6703 | | *first = (current == ext->data); |
6704 | | *usingPSK = 1; |
6705 | | |
6706 | | cleanup: |
6707 | | ForceZero(binderKey, sizeof(binderKey)); |
6708 | | ForceZero(binder, sizeof(binder)); |
6709 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
6710 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
6711 | | #endif |
6712 | | WOLFSSL_LEAVE("DoPreSharedKeys", ret); |
6713 | | |
6714 | | return ret; |
6715 | | } |
6716 | | |
6717 | | /* Handle any Pre-Shared Key (PSK) extension. |
6718 | | * Must do this in ClientHello as it requires a hash of the truncated message. |
6719 | | * Don't know size of binders until Pre-Shared Key extension has been parsed. |
6720 | | * |
6721 | | * ssl SSL/TLS object. |
6722 | | * input ClientHello message. |
6723 | | * helloSz Size of the ClientHello message (including binders if present). |
6724 | | * clSuites Client's cipher suite list. |
6725 | | * usingPSK Indicates handshake is using Pre-Shared Keys. |
6726 | | */ |
6727 | | static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz, |
6728 | | Suites* clSuites, int* usingPSK) |
6729 | | { |
6730 | | int ret; |
6731 | | TLSX* ext; |
6732 | | word16 bindersLen; |
6733 | | int first = 0; |
6734 | | #ifndef WOLFSSL_PSK_ONE_ID |
6735 | | int i; |
6736 | | const Suites* suites; |
6737 | | #else |
6738 | | byte suite[2]; |
6739 | | #endif |
6740 | | |
6741 | | WOLFSSL_ENTER("CheckPreSharedKeys"); |
6742 | | |
6743 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6744 | | if (ext == NULL) { |
6745 | | #ifdef WOLFSSL_EARLY_DATA |
6746 | | ssl->earlyData = no_early_data; |
6747 | | #endif |
6748 | | if (usingPSK) |
6749 | | *usingPSK = 0; |
6750 | | |
6751 | | /* No PSK extension at all: if a mandatory external PSK is configured, |
6752 | | * refuse the connection rather than continue without one. havePSK is |
6753 | | * only set by an external-PSK callback, so a peer relying solely on |
6754 | | * session-ticket resumption is unaffected. */ |
6755 | | if (ssl->options.havePSK && ssl->options.failNoPSK) { |
6756 | | WOLFSSL_ERROR_VERBOSE(PSK_MISSING_ERROR); |
6757 | | return PSK_MISSING_ERROR; |
6758 | | } |
6759 | | |
6760 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6761 | | ret = HashInput(ssl, input, (int)helloSz); |
6762 | | return ret; |
6763 | | } |
6764 | | |
6765 | | /* Wire-order check that PSK was the last extension in ClientHello is |
6766 | | * performed in DoTls13ClientHello immediately after TLSX_Parse, since |
6767 | | * post-parse code (e.g. ALPN_Select via TLSX_SetALPN) may legitimately |
6768 | | * prepend new entries to ssl->extensions before this point and would |
6769 | | * otherwise trip a head-of-list check here. */ |
6770 | | |
6771 | | /* Assume we are going to resume with a pre-shared key. */ |
6772 | | ssl->options.resuming = 1; |
6773 | | |
6774 | | /* Find the pre-shared key extension and calculate hash of truncated |
6775 | | * ClientHello for binders. |
6776 | | */ |
6777 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
6778 | | client_hello, &bindersLen); |
6779 | | if (ret < 0) |
6780 | | return ret; |
6781 | | if (bindersLen > helloSz) |
6782 | | return BUFFER_ERROR; |
6783 | | |
6784 | | /* Refine list for PSK processing. */ |
6785 | | sslRefineSuites(ssl, clSuites); |
6786 | | #ifndef WOLFSSL_PSK_ONE_ID |
6787 | | if (usingPSK == NULL) |
6788 | | return BAD_FUNC_ARG; |
6789 | | |
6790 | | /* set after refineSuites, to avoid taking a stale ptr to ctx->Suites */ |
6791 | | suites = WOLFSSL_SUITES(ssl); |
6792 | | /* Server list has only common suites from refining in server or client |
6793 | | * order. */ |
6794 | | for (i = 0; !(*usingPSK) && i < suites->suiteSz; i += 2) { |
6795 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, |
6796 | | suites->suites + i, usingPSK, &first); |
6797 | | if (ret != 0) { |
6798 | | #ifdef HAVE_SESSION_TICKET |
6799 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6800 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
6801 | | #endif |
6802 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6803 | | #endif |
6804 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6805 | | return ret; |
6806 | | } |
6807 | | } |
6808 | | #ifdef HAVE_SESSION_TICKET |
6809 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6810 | | #endif |
6811 | | #else |
6812 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, suite, usingPSK, |
6813 | | &first); |
6814 | | if (ret != 0) { |
6815 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6816 | | return ret; |
6817 | | } |
6818 | | #endif |
6819 | | |
6820 | | if (!*usingPSK) { |
6821 | | /* No suitable PSK was negotiated. When a mandatory external PSK is |
6822 | | * configured, fail with a dedicated error instead of falling back to a |
6823 | | * certificate handshake. This must run before the no-certificate |
6824 | | * BAD_BINDER check below so a PSK-only server (no cert) still reports |
6825 | | * PSK_MISSING_ERROR. havePSK is only set by an external-PSK callback, so |
6826 | | * a peer relying solely on session-ticket resumption is unaffected. */ |
6827 | | if (ssl->options.havePSK && ssl->options.failNoPSK) { |
6828 | | WOLFSSL_ERROR_VERBOSE(PSK_MISSING_ERROR); |
6829 | | return PSK_MISSING_ERROR; |
6830 | | } |
6831 | | #ifndef NO_CERTS |
6832 | | if (ssl->buffers.certificate == NULL |
6833 | | #ifdef WOLFSSL_CERT_SETUP_CB |
6834 | | && ssl->ctx->certSetupCb == NULL |
6835 | | #endif |
6836 | | ) |
6837 | | #endif |
6838 | | { |
6839 | | /* Reused for identity protection: an unknown identity must look |
6840 | | * like a bad binder, so keep the error code shared. */ |
6841 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6842 | | return BAD_BINDER; |
6843 | | } |
6844 | | } |
6845 | | |
6846 | | if (*usingPSK) { |
6847 | | /* While verifying the selected PSK, we updated the |
6848 | | * handshake hash up to the binder bytes in the PSK extensions. |
6849 | | * Continuing, we need the rest of the ClientHello hashed as well. |
6850 | | */ |
6851 | | ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen); |
6852 | | } |
6853 | | else { |
6854 | | /* No suitable PSK found, Hash the complete ClientHello, |
6855 | | * as caller expect it after we return */ |
6856 | | ret = HashInput(ssl, input, (int)helloSz); |
6857 | | } |
6858 | | if (ret != 0) |
6859 | | return ret; |
6860 | | |
6861 | | if (*usingPSK != 0) { |
6862 | | word32 modes; |
6863 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6864 | | int usingCertWithExternPsk = 0; |
6865 | | TLSX* certExt = NULL; |
6866 | | TLSX* pskExt = NULL; |
6867 | | PreSharedKey* chosenPsk = NULL; |
6868 | | #endif |
6869 | | #ifdef WOLFSSL_EARLY_DATA |
6870 | | TLSX* extEarlyData; |
6871 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6872 | | int hasCertWithExternPsk = (TLSX_Find(ssl->extensions, |
6873 | | TLSX_CERT_WITH_EXTERN_PSK) != NULL); |
6874 | | #endif |
6875 | | |
6876 | | extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6877 | | if (extEarlyData != NULL) { |
6878 | | /* Check if accepting early data and first PSK. |
6879 | | * RFC 9973: early_data is not compatible with |
6880 | | * cert_with_extern_psk, so skip key derivation in that case. */ |
6881 | | if (ssl->earlyData != no_early_data && first |
6882 | | && ssl->options.maxEarlyDataSz > 0 |
6883 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6884 | | && !hasCertWithExternPsk |
6885 | | #endif |
6886 | | #if defined(HAVE_SESSION_TICKET) && !defined(NO_SESSION_CACHE) |
6887 | | /* RFC 8446 section 8: evict the session from the cache. |
6888 | | * Accept 0-RTT only when the eviction found the entry |
6889 | | * (single-use). */ |
6890 | | && wolfSSL_SSL_CTX_remove_session(ssl->ctx, ssl->session) |
6891 | | == 1 |
6892 | | #endif |
6893 | | ) { |
6894 | | extEarlyData->resp = 1; |
6895 | | |
6896 | | /* Derive early data decryption key. */ |
6897 | | ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, |
6898 | | 1); |
6899 | | if (ret != 0) |
6900 | | return ret; |
6901 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
6902 | | return ret; |
6903 | | |
6904 | | ssl->keys.encryptionOn = 1; |
6905 | | ssl->earlyData = process_early_data; |
6906 | | } |
6907 | | else |
6908 | | extEarlyData->resp = 0; |
6909 | | } |
6910 | | #endif |
6911 | | |
6912 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
6913 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
6914 | | if (ext == NULL) { |
6915 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6916 | | return MISSING_HANDSHAKE_DATA; |
6917 | | } |
6918 | | modes = ext->val; |
6919 | | |
6920 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6921 | | certExt = TLSX_Find(ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK); |
6922 | | if (certExt != NULL) { |
6923 | | pskExt = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6924 | | if (pskExt != NULL) |
6925 | | chosenPsk = (PreSharedKey*)pskExt->data; |
6926 | | while (chosenPsk != NULL && !chosenPsk->chosen) |
6927 | | chosenPsk = chosenPsk->next; |
6928 | | if (chosenPsk == NULL || chosenPsk->resumption) { |
6929 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6930 | | return PSK_KEY_ERROR; |
6931 | | } |
6932 | | if ((modes & (1 << PSK_DHE_KE)) == 0) { |
6933 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6934 | | return PSK_KEY_ERROR; |
6935 | | } |
6936 | | usingCertWithExternPsk = 1; |
6937 | | ssl->options.certWithExternPsk = 1; |
6938 | | if (clSuites->hashSigAlgoSz == 0) { |
6939 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6940 | | return MISSING_HANDSHAKE_DATA; |
6941 | | } |
6942 | | ret = PickHashSigAlgo(ssl, clSuites->hashSigAlgo, |
6943 | | clSuites->hashSigAlgoSz, 1); |
6944 | | if (ret != 0) |
6945 | | return ret; |
6946 | | ssl->options.sendVerify = SEND_CERT; |
6947 | | certExt->resp = 1; |
6948 | | #ifdef WOLFSSL_EARLY_DATA |
6949 | | /* RFC 9973: early_data is not compatible with |
6950 | | * cert_with_extern_psk. TLSX_Parse already rejects the |
6951 | | * combination in the ClientHello, but clear the response flag |
6952 | | * here as a defense-in-depth measure. */ |
6953 | | if (extEarlyData != NULL) { |
6954 | | WOLFSSL_MSG("Rejecting early data: " |
6955 | | "cert_with_extern_psk is not 0-RTT compatible"); |
6956 | | extEarlyData->resp = 0; |
6957 | | ssl->earlyData = no_early_data; |
6958 | | } |
6959 | | #endif |
6960 | | } |
6961 | | else { |
6962 | | ssl->options.certWithExternPsk = 0; |
6963 | | } |
6964 | | #endif |
6965 | | |
6966 | | #ifndef HAVE_SUPPORTED_CURVES |
6967 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6968 | | if (usingCertWithExternPsk) { |
6969 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6970 | | return PSK_KEY_ERROR; |
6971 | | } |
6972 | | #endif |
6973 | | #endif |
6974 | | #ifdef HAVE_SUPPORTED_CURVES |
6975 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
6976 | | /* Use (EC)DHE for forward-security if possible. */ |
6977 | | if (((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe && |
6978 | | ext != NULL) |
6979 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6980 | | || usingCertWithExternPsk |
6981 | | #endif |
6982 | | ) { |
6983 | | if (ext == NULL) { |
6984 | | WOLFSSL_ERROR_VERBOSE(EXT_MISSING); |
6985 | | return EXT_MISSING; |
6986 | | } |
6987 | | /* Resumption path uses previous session group. */ |
6988 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6989 | | if (!usingCertWithExternPsk) |
6990 | | #endif |
6991 | | ssl->namedGroup = ssl->session->namedGroup; |
6992 | | *usingPSK = 2; /* generate new ephemeral key */ |
6993 | | } |
6994 | | else if (ssl->options.onlyPskDheKe || |
6995 | | (ssl->options.failNoPSK && !ssl->options.resuming)) { |
6996 | | /* A mandatory external PSK (failNoPSK) must be combined with |
6997 | | * (EC)DHE for forward secrecy, so reject a pure psk_ke |
6998 | | * negotiation. Session-ticket resumption is exempt. */ |
6999 | | return PSK_KEY_ERROR; |
7000 | | } |
7001 | | else |
7002 | | #endif |
7003 | | { |
7004 | | if ((modes & (1 << PSK_KE)) == 0) { |
7005 | | WOLFSSL_MSG("psk_ke mode does not allow key share"); |
7006 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
7007 | | return PSK_KEY_ERROR; |
7008 | | } |
7009 | | ssl->options.noPskDheKe = 1; |
7010 | | ssl->arrays->preMasterSz = 0; |
7011 | | |
7012 | | *usingPSK = 1; |
7013 | | } |
7014 | | } |
7015 | | else { |
7016 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
7017 | | /* If no PSK is found, we remove the extension to make sure it |
7018 | | * is not sent back to the client */ |
7019 | | TLSX_Remove(&ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK, ssl->heap); |
7020 | | ssl->options.certWithExternPsk = 0; |
7021 | | #endif |
7022 | | /* No PSK negotiated; the check above already aborted when there is no |
7023 | | * certificate. Fall through so the trace is emitted here too. */ |
7024 | | } |
7025 | | |
7026 | | WOLFSSL_LEAVE("CheckPreSharedKeys", ret); |
7027 | | |
7028 | | return 0; |
7029 | | } |
7030 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
7031 | | |
7032 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7033 | | /* Compute the cookie integrity HMAC over the cookie data (and, for DTLS, the |
7034 | | * peer address) using the given secret and compare it in constant time against |
7035 | | * the MAC trailing the cookie. |
7036 | | * |
7037 | | * ssl SSL/TLS object. |
7038 | | * cookie The cookie data - hash and MAC. |
7039 | | * dataSz Length of the cookie data preceding the trailing MAC. |
7040 | | * secret Secret to key the HMAC with. |
7041 | | * secretSz Length of the secret in bytes. |
7042 | | * cookieType Digest type to use for the HMAC. |
7043 | | * macSz Length of the MAC in bytes. |
7044 | | * returns 0 on match, HRR_COOKIE_ERROR on mismatch, otherwise a negative error. |
7045 | | */ |
7046 | | static int TlsCheckCookieMac(const WOLFSSL* ssl, const byte* cookie, |
7047 | | word16 dataSz, const byte* secret, word32 secretSz, byte cookieType, |
7048 | | byte macSz) |
7049 | | { |
7050 | | int ret; |
7051 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
7052 | | WC_DECLARE_VAR(cookieHmac, Hmac, 1, ssl->heap); |
7053 | | |
7054 | | WC_ALLOC_VAR_EX(cookieHmac, Hmac, 1, ssl->heap, DYNAMIC_TYPE_HMAC, |
7055 | | return MEMORY_E); |
7056 | | |
7057 | | ret = wc_HmacInit(cookieHmac, ssl->heap, ssl->devId); |
7058 | | if (ret == 0) |
7059 | | ret = wc_HmacSetKey(cookieHmac, cookieType, secret, secretSz); |
7060 | | if (ret == 0) |
7061 | | ret = wc_HmacUpdate(cookieHmac, cookie, dataSz); |
7062 | | #ifdef WOLFSSL_DTLS13 |
7063 | | /* Tie cookie to peer address */ |
7064 | | if (ret == 0) { |
7065 | | /* peerLock not necessary. Still in handshake phase. */ |
7066 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
7067 | | ret = wc_HmacUpdate(cookieHmac, |
7068 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
7069 | | ssl->buffers.dtlsCtx.peer.sz); |
7070 | | } |
7071 | | } |
7072 | | #endif |
7073 | | if (ret == 0) |
7074 | | ret = wc_HmacFinal(cookieHmac, mac); |
7075 | | |
7076 | | wc_HmacFree(cookieHmac); |
7077 | | WC_FREE_VAR_EX(cookieHmac, ssl->heap, DYNAMIC_TYPE_HMAC); |
7078 | | if (ret != 0) |
7079 | | return ret; |
7080 | | |
7081 | | if (ConstantCompare(cookie + dataSz, mac, macSz) != 0) |
7082 | | return HRR_COOKIE_ERROR; |
7083 | | |
7084 | | return 0; |
7085 | | } |
7086 | | |
7087 | | /* Check that the Cookie data's integrity. |
7088 | | * |
7089 | | * ssl SSL/TLS object. |
7090 | | * cookie The cookie data - hash and MAC. |
7091 | | * cookieSz The length of the cookie data in bytes. |
7092 | | * returns Length of the hash on success, otherwise failure. |
7093 | | */ |
7094 | | int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz) |
7095 | | { |
7096 | | int ret; |
7097 | | byte cookieType = 0; |
7098 | | byte macSz = 0; |
7099 | | |
7100 | | #ifndef NO_SHA256 |
7101 | | cookieType = WC_SHA256; |
7102 | | macSz = WC_SHA256_DIGEST_SIZE; |
7103 | | #elif defined(WOLFSSL_SHA384) |
7104 | | cookieType = WC_SHA384; |
7105 | | macSz = WC_SHA384_DIGEST_SIZE; |
7106 | | #elif defined(WOLFSSL_TLS13_SHA512) |
7107 | | cookieType = WC_SHA512; |
7108 | | macSz = WC_SHA512_DIGEST_SIZE; |
7109 | | #elif defined(WOLFSSL_SM3) |
7110 | | cookieType = WC_SM3; |
7111 | | macSz = WC_SM3_DIGEST_SIZE; |
7112 | | #else |
7113 | | #error "No digest to available to use with HMAC for cookies." |
7114 | | #endif /* NO_SHA */ |
7115 | | |
7116 | | if ((ssl->buffers.tls13CookieSecret.buffer == NULL || |
7117 | | ssl->buffers.tls13CookieSecret.length == 0) |
7118 | | #ifdef WOLFSSL_DTLS13 |
7119 | | && (ssl->buffers.tls13CookieSecretSecondary.buffer == NULL || |
7120 | | ssl->buffers.tls13CookieSecretSecondary.length == 0) |
7121 | | #endif |
7122 | | ) { |
7123 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
7124 | | return COOKIE_ERROR; |
7125 | | } |
7126 | | |
7127 | | if (cookieSz < ssl->specs.hash_size + macSz) |
7128 | | return HRR_COOKIE_ERROR; |
7129 | | cookieSz -= macSz; |
7130 | | |
7131 | | /* Verify against the primary secret first. If that fails and a secondary |
7132 | | * (verify-only) secret is configured, try that too. This lets a stateless |
7133 | | * DTLS 1.3 server keep accepting cookies issued under the secret it held |
7134 | | * before an application-driven secret rotation. The secondary secret is |
7135 | | * DTLS 1.3 only, so its verify path is compiled in only for WOLFSSL_DTLS13. */ |
7136 | | ret = WC_NO_ERR_TRACE(HRR_COOKIE_ERROR); |
7137 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL && |
7138 | | ssl->buffers.tls13CookieSecret.length > 0) { |
7139 | | ret = TlsCheckCookieMac(ssl, cookie, cookieSz, |
7140 | | ssl->buffers.tls13CookieSecret.buffer, |
7141 | | ssl->buffers.tls13CookieSecret.length, cookieType, macSz); |
7142 | | if (ret != 0 && ret != WC_NO_ERR_TRACE(HRR_COOKIE_ERROR)) |
7143 | | return ret; |
7144 | | } |
7145 | | #ifdef WOLFSSL_DTLS13 |
7146 | | if (ret == WC_NO_ERR_TRACE(HRR_COOKIE_ERROR) && |
7147 | | ssl->buffers.tls13CookieSecretSecondary.buffer != NULL && |
7148 | | ssl->buffers.tls13CookieSecretSecondary.length > 0) { |
7149 | | ret = TlsCheckCookieMac(ssl, cookie, cookieSz, |
7150 | | ssl->buffers.tls13CookieSecretSecondary.buffer, |
7151 | | ssl->buffers.tls13CookieSecretSecondary.length, cookieType, macSz); |
7152 | | if (ret != 0 && ret != WC_NO_ERR_TRACE(HRR_COOKIE_ERROR)) |
7153 | | return ret; |
7154 | | } |
7155 | | #endif |
7156 | | |
7157 | | if (ret != 0) { |
7158 | | WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR); |
7159 | | return HRR_COOKIE_ERROR; |
7160 | | } |
7161 | | |
7162 | | return cookieSz; |
7163 | | } |
7164 | | |
7165 | | /* Length of the KeyShare Extension */ |
7166 | | #define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
7167 | | /* Length of the Supported Versions Extension */ |
7168 | | #define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
7169 | | /* Length of the Cookie Extension excluding cookie data */ |
7170 | | #define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
7171 | | /* PV | Random | Session Id | CipherSuite | Compression | Ext Len */ |
7172 | | #define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \ |
7173 | | SUITE_LEN + COMP_LEN + OPAQUE16_LEN) |
7174 | | /* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */ |
7175 | | #define MAX_HRR_SZ (HRR_MAX_HS_HEADER_SZ + \ |
7176 | | HRR_BODY_SZ + \ |
7177 | | HRR_KEY_SHARE_SZ + \ |
7178 | | HRR_VERSIONS_SZ + \ |
7179 | | HRR_COOKIE_HDR_SZ) |
7180 | | |
7181 | | |
7182 | | /* Restart the handshake hash from the cookie value. |
7183 | | * |
7184 | | * ssl SSL/TLS object. |
7185 | | * cookie Cookie data from client. |
7186 | | * returns 0 on success, otherwise failure. |
7187 | | */ |
7188 | | static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie) |
7189 | | { |
7190 | | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
7191 | | byte hrr[MAX_HRR_SZ] = {0}; |
7192 | | int hrrIdx; |
7193 | | word32 idx; |
7194 | | byte hashSz; |
7195 | | byte* cookieData; |
7196 | | word16 cookieDataSz; |
7197 | | word16 length; |
7198 | | int keyShareExt = 0; |
7199 | | int ret; |
7200 | | byte sessIdSz; |
7201 | | |
7202 | | ret = TlsCheckCookie(ssl, cookie->data, cookie->len); |
7203 | | if (ret < 0) |
7204 | | return ret; |
7205 | | cookieDataSz = (word16)ret; |
7206 | | hashSz = cookie->data[0]; |
7207 | | cookieData = cookie->data; |
7208 | | idx = OPAQUE8_LEN; |
7209 | | |
7210 | | /* Restart handshake hash with synthetic message hash. */ |
7211 | | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
7212 | | |
7213 | | if ((ret = InitHandshakeHashes(ssl)) != 0) |
7214 | | return ret; |
7215 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
7216 | | return ret; |
7217 | | #ifdef WOLFSSL_DEBUG_TLS |
7218 | | WOLFSSL_MSG("Restart Hash from Cookie"); |
7219 | | WOLFSSL_BUFFER(cookieData + idx, hashSz); |
7220 | | #endif |
7221 | | if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0) |
7222 | | return ret; |
7223 | | |
7224 | | /* Reconstruct the HelloRetryMessage for handshake hash. */ |
7225 | | sessIdSz = ssl->session->sessionIDSz; |
7226 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID) |
7227 | | /* RFC 9147 Section 5: a DTLS 1.3 server does not echo the session ID, so |
7228 | | * the reconstructed transcript must not carry one either. */ |
7229 | | if (ssl->options.dtls) |
7230 | | sessIdSz = 0; |
7231 | | #endif |
7232 | | length = HRR_BODY_SZ - ID_LEN + sessIdSz + |
7233 | | HRR_COOKIE_HDR_SZ + cookie->len; |
7234 | | length += HRR_VERSIONS_SZ; |
7235 | | /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */ |
7236 | | if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) { |
7237 | | keyShareExt = 1; |
7238 | | length += HRR_KEY_SHARE_SZ; |
7239 | | } |
7240 | | |
7241 | | AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl); |
7242 | | |
7243 | | idx += hashSz; |
7244 | | hrrIdx = HANDSHAKE_HEADER_SZ; |
7245 | | |
7246 | | #ifdef WOLFSSL_DTLS13 |
7247 | | if (ssl->options.dtls) |
7248 | | hrrIdx += DTLS_HANDSHAKE_EXTRA; |
7249 | | #endif /* WOLFSSL_DTLS13 */ |
7250 | | |
7251 | | /* The negotiated protocol version. */ |
7252 | | hrr[hrrIdx++] = ssl->version.major; |
7253 | | hrr[hrrIdx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
7254 | | |
7255 | | /* HelloRetryRequest message has fixed value for random. */ |
7256 | | XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN); |
7257 | | hrrIdx += RAN_LEN; |
7258 | | |
7259 | | hrr[hrrIdx++] = sessIdSz; |
7260 | | if (sessIdSz > 0) { |
7261 | | XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz); |
7262 | | hrrIdx += sessIdSz; |
7263 | | } |
7264 | | |
7265 | | /* Restore the cipher suite from the cookie. */ |
7266 | | ssl->options.hrrCipherSuite0 = cookieData[idx]; |
7267 | | hrr[hrrIdx++] = cookieData[idx++]; |
7268 | | ssl->options.hrrCipherSuite = cookieData[idx]; |
7269 | | hrr[hrrIdx++] = cookieData[idx++]; |
7270 | | |
7271 | | /* Compression not supported in TLS v1.3. */ |
7272 | | hrr[hrrIdx++] = 0; |
7273 | | |
7274 | | /* Extensions' length */ |
7275 | | length -= HRR_BODY_SZ - ID_LEN + sessIdSz; |
7276 | | c16toa(length, hrr + hrrIdx); |
7277 | | hrrIdx += 2; |
7278 | | |
7279 | | /* Optional KeyShare Extension */ |
7280 | | if (keyShareExt) { |
7281 | | c16toa(TLSX_KEY_SHARE, hrr + hrrIdx); |
7282 | | hrrIdx += 2; |
7283 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
7284 | | hrrIdx += 2; |
7285 | | /* Restore the HRR key share group from the cookie. */ |
7286 | | ato16(cookieData + idx, &ssl->hrr_keyshare_group); |
7287 | | hrr[hrrIdx++] = cookieData[idx++]; |
7288 | | hrr[hrrIdx++] = cookieData[idx++]; |
7289 | | } |
7290 | | c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx); |
7291 | | hrrIdx += 2; |
7292 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
7293 | | hrrIdx += 2; |
7294 | | #ifdef WOLFSSL_TLS13_DRAFT |
7295 | | hrr[hrrIdx++] = TLS_DRAFT_MAJOR; |
7296 | | hrr[hrrIdx++] = TLS_DRAFT_MINOR; |
7297 | | #else |
7298 | | hrr[hrrIdx++] = ssl->version.major; |
7299 | | hrr[hrrIdx++] = ssl->version.minor; |
7300 | | #endif |
7301 | | |
7302 | | /* Mandatory Cookie Extension */ |
7303 | | c16toa(TLSX_COOKIE, hrr + hrrIdx); |
7304 | | hrrIdx += 2; |
7305 | | c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx); |
7306 | | hrrIdx += 2; |
7307 | | c16toa(cookie->len, hrr + hrrIdx); |
7308 | | hrrIdx += 2; |
7309 | | |
7310 | | #ifdef WOLFSSL_DEBUG_TLS |
7311 | | WOLFSSL_MSG("Reconstructed HelloRetryRequest"); |
7312 | | WOLFSSL_BUFFER(hrr, hrrIdx); |
7313 | | WOLFSSL_MSG("Cookie"); |
7314 | | WOLFSSL_BUFFER(cookieData, cookie->len); |
7315 | | #endif |
7316 | | |
7317 | | #ifdef WOLFSSL_DTLS13 |
7318 | | if (ssl->options.dtls) { |
7319 | | ret = Dtls13HashHandshake(ssl, hrr, (word16)hrrIdx); |
7320 | | } |
7321 | | else |
7322 | | #endif /* WOLFSSL_DTLS13 */ |
7323 | | { |
7324 | | ret = HashRaw(ssl, hrr, hrrIdx); |
7325 | | } |
7326 | | |
7327 | | if (ret != 0) |
7328 | | return ret; |
7329 | | |
7330 | | return HashRaw(ssl, cookieData, cookie->len); |
7331 | | } |
7332 | | #endif |
7333 | | |
7334 | | /* Do SupportedVersion extension for TLS v1.3+ otherwise it is not. |
7335 | | * |
7336 | | * ssl The SSL/TLS object. |
7337 | | * input The message buffer. |
7338 | | * i The index into the message buffer of ClientHello. |
7339 | | * helloSz The length of the current handshake message. |
7340 | | * returns 0 on success and otherwise failure. |
7341 | | */ |
7342 | | static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i, |
7343 | | word32 helloSz, int* wantDowngrade) |
7344 | | { |
7345 | | int ret; |
7346 | | byte b; |
7347 | | word16 suiteSz; |
7348 | | word16 totalExtSz; |
7349 | | int foundVersion = 0; |
7350 | | |
7351 | | /* Client random */ |
7352 | | i += RAN_LEN; |
7353 | | |
7354 | | if (i > helloSz) |
7355 | | return BUFFER_ERROR; |
7356 | | /* Session id - not used in TLS v1.3 */ |
7357 | | if (helloSz - i < OPAQUE8_LEN) { |
7358 | | return BUFFER_ERROR; |
7359 | | } |
7360 | | b = input[i++]; |
7361 | | if (b > helloSz - i) { |
7362 | | return BUFFER_ERROR; |
7363 | | } |
7364 | | i += b; |
7365 | | #ifdef WOLFSSL_DTLS13 |
7366 | | if (ssl->options.dtls) { |
7367 | | /* legacy_cookie - not used in DTLS v1.3 */ |
7368 | | if (helloSz - i < OPAQUE8_LEN) { |
7369 | | return BUFFER_ERROR; |
7370 | | } |
7371 | | b = input[i++]; |
7372 | | if (b > helloSz - i) { |
7373 | | return BUFFER_ERROR; |
7374 | | } |
7375 | | i += b; |
7376 | | } |
7377 | | #endif /* WOLFSSL_DTLS13 */ |
7378 | | /* Cipher suites */ |
7379 | | if (helloSz - i < OPAQUE16_LEN) |
7380 | | return BUFFER_ERROR; |
7381 | | ato16(input + i, &suiteSz); |
7382 | | i += OPAQUE16_LEN; |
7383 | | if ((word32)suiteSz + OPAQUE8_LEN > helloSz - i) |
7384 | | return BUFFER_ERROR; |
7385 | | i += suiteSz; |
7386 | | /* Compression */ |
7387 | | b = input[i++]; |
7388 | | if (b > helloSz - i) |
7389 | | return BUFFER_ERROR; |
7390 | | i += b; |
7391 | | |
7392 | | /* TLS 1.3 must have extensions */ |
7393 | | if (i < helloSz) { |
7394 | | if (helloSz - i < OPAQUE16_LEN) |
7395 | | return BUFFER_ERROR; |
7396 | | ato16(&input[i], &totalExtSz); |
7397 | | i += OPAQUE16_LEN; |
7398 | | if (totalExtSz != helloSz - i) |
7399 | | return BUFFER_ERROR; |
7400 | | |
7401 | | /* Need to negotiate version first. */ |
7402 | | if ((ret = TLSX_ParseVersion(ssl, input + i, totalExtSz, client_hello, |
7403 | | &foundVersion))) { |
7404 | | return ret; |
7405 | | } |
7406 | | } |
7407 | | *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version); |
7408 | | |
7409 | | return 0; |
7410 | | } |
7411 | | |
7412 | | #ifdef HAVE_ECH |
7413 | | /* Calculate and write the 8 ECH confirmation bytes. |
7414 | | * Output into confirmation field on HRR and into ServerRandom on ServerHello. |
7415 | | * |
7416 | | * ssl SSL/TLS object. |
7417 | | * label Ascii string describing ECH acceptance or rejection. |
7418 | | * labelSz Length of label excluding NULL character. |
7419 | | * output The buffer to calculate/write confirmation from/to. |
7420 | | * acceptOffset Where the 8 ECH confirmation bytes should be placed. |
7421 | | * helloSz Size of hello message. |
7422 | | * msgType Type of message being written. |
7423 | | * returns 0 on success and otherwise failure. |
7424 | | */ |
7425 | | static int EchWriteAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
7426 | | byte* output, int acceptOffset, int helloSz, byte msgType) |
7427 | | { |
7428 | | int ret = 0; |
7429 | | int headerSz; |
7430 | | HS_Hashes* tmpHashes; |
7431 | | |
7432 | | #ifdef WOLFSSL_DTLS13 |
7433 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
7434 | | HANDSHAKE_HEADER_SZ; |
7435 | | #else |
7436 | | headerSz = HANDSHAKE_HEADER_SZ; |
7437 | | #endif |
7438 | | |
7439 | | ret = EchCalcAcceptance(ssl, label, labelSz, output, acceptOffset, |
7440 | | helloSz - headerSz, msgType == hello_retry_request, |
7441 | | output + acceptOffset); |
7442 | | |
7443 | | if (ret == 0) { |
7444 | | tmpHashes = ssl->hsHashes; |
7445 | | ssl->hsHashes = ssl->hsHashesEch; |
7446 | | |
7447 | | /* after HRR, hsHashesEch must contain: |
7448 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
7449 | | if (msgType == hello_retry_request) { |
7450 | | ret = HashRaw(ssl, output, helloSz); |
7451 | | } |
7452 | | /* normal TLS code will calculate transcript of ServerHello */ |
7453 | | else { |
7454 | | ssl->hsHashes = tmpHashes; |
7455 | | FreeHandshakeHashes(ssl); |
7456 | | tmpHashes = ssl->hsHashesEch; |
7457 | | ssl->hsHashesEch = NULL; |
7458 | | } |
7459 | | |
7460 | | ssl->hsHashes = tmpHashes; |
7461 | | } |
7462 | | |
7463 | | return ret; |
7464 | | } |
7465 | | #endif |
7466 | | |
7467 | | /* Handle a ClientHello handshake message. |
7468 | | * If the protocol version in the message is not TLS v1.3 or higher, use |
7469 | | * DoClientHello() |
7470 | | * Only a server will receive this message. |
7471 | | * |
7472 | | * ssl The SSL/TLS object. |
7473 | | * input The message buffer. |
7474 | | * inOutIdx On entry, the index into the message buffer of ClientHello. |
7475 | | * On exit, the index of byte after the ClientHello message and |
7476 | | * padding. |
7477 | | * helloSz The length of the current handshake message. |
7478 | | * returns 0 on success and otherwise failure. |
7479 | | */ |
7480 | | |
7481 | | typedef struct Dch13Args { |
7482 | | ProtocolVersion pv; |
7483 | | word32 idx; |
7484 | | word32 begin; |
7485 | | int usingPSK; |
7486 | | } Dch13Args; |
7487 | | |
7488 | | static void FreeDch13Args(WOLFSSL* ssl, void* pArgs) |
7489 | | { |
7490 | | /* openssl compat builds hang on to the client suites until WOLFSSL object |
7491 | | * is destroyed */ |
7492 | | #ifndef OPENSSL_EXTRA |
7493 | | if (ssl->clSuites) { |
7494 | | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
7495 | | ssl->clSuites = NULL; |
7496 | | } |
7497 | | #endif |
7498 | | (void)ssl; |
7499 | | (void)pArgs; |
7500 | | |
7501 | | } |
7502 | | |
7503 | | int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
7504 | | word32 helloSz) |
7505 | 0 | { |
7506 | 0 | int ret; |
7507 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7508 | | Dch13Args* args = NULL; |
7509 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
7510 | | #else |
7511 | 0 | Dch13Args args[1]; |
7512 | 0 | #endif |
7513 | | #if defined(HAVE_ECH) |
7514 | | TLSX* echX = NULL; |
7515 | | #endif |
7516 | |
|
7517 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO); |
7518 | 0 | WOLFSSL_ENTER("DoTls13ClientHello"); |
7519 | |
|
7520 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7521 | | if (ssl->async == NULL) { |
7522 | | ssl->async = (struct WOLFSSL_ASYNC*) |
7523 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
7524 | | DYNAMIC_TYPE_ASYNC); |
7525 | | if (ssl->async == NULL) |
7526 | | ERROR_OUT(MEMORY_E, exit_dch); |
7527 | | } |
7528 | | args = (Dch13Args*)ssl->async->args; |
7529 | | |
7530 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
7531 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
7532 | | /* Check for error */ |
7533 | | if (ret < 0) { |
7534 | | goto exit_dch; |
7535 | | } |
7536 | | } |
7537 | | else |
7538 | | #endif |
7539 | 0 | { |
7540 | | /* Reset state */ |
7541 | 0 | ret = WC_NO_ERR_TRACE(VERSION_ERROR); |
7542 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
7543 | 0 | XMEMSET(args, 0, sizeof(Dch13Args)); |
7544 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7545 | | ssl->async->freeArgs = FreeDch13Args; |
7546 | | #endif |
7547 | 0 | } |
7548 | |
|
7549 | 0 | switch (ssl->options.asyncState) { |
7550 | 0 | case TLS_ASYNC_BEGIN: |
7551 | 0 | { |
7552 | 0 | byte b; |
7553 | 0 | byte sessIdSz; |
7554 | 0 | int wantDowngrade = 0; |
7555 | 0 | word16 totalExtSz = 0; |
7556 | |
|
7557 | | #ifdef WOLFSSL_CALLBACKS |
7558 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
7559 | | if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo); |
7560 | | #endif |
7561 | | |
7562 | | /* do not change state in the SSL object before the next region of code |
7563 | | * to be able to statelessly compute a DTLS cookie */ |
7564 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
7565 | | /* Update the ssl->options.dtlsStateful setting `if` statement in |
7566 | | * wolfSSL_accept_TLSv13 when changing this one. */ |
7567 | | if (IsDtlsNotSctpMode(ssl) && ssl->options.sendCookie && |
7568 | | !ssl->options.dtlsStateful) { |
7569 | | DtlsSetSeqNumForReply(ssl); |
7570 | | ret = DoClientHelloStateless(ssl, input + *inOutIdx, helloSz, 0, NULL); |
7571 | | if (ret != 0 || !ssl->options.dtlsStateful) { |
7572 | | *inOutIdx += helloSz; |
7573 | | goto exit_dch; |
7574 | | } |
7575 | | if (ssl->chGoodCb != NULL) { |
7576 | | int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
7577 | | if (cbret < 0) { |
7578 | | ssl->error = cbret; |
7579 | | WOLFSSL_MSG("ClientHello Good Cb don't continue error"); |
7580 | | return WOLFSSL_FATAL_ERROR; |
7581 | | } |
7582 | | } |
7583 | | } |
7584 | | ssl->options.dtlsStateful = 1; |
7585 | | #endif /* WOLFSSL_DTLS */ |
7586 | |
|
7587 | 0 | args->idx = *inOutIdx; |
7588 | 0 | args->begin = args->idx; |
7589 | | |
7590 | | /* protocol version, random and session id length check */ |
7591 | 0 | if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz) { |
7592 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7593 | 0 | } |
7594 | | |
7595 | | /* Protocol version */ |
7596 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
7597 | 0 | ssl->chVersion = args->pv; /* store */ |
7598 | 0 | args->idx += OPAQUE16_LEN; |
7599 | | |
7600 | | |
7601 | | /* this check pass for DTLS Major (0xff) */ |
7602 | 0 | if (args->pv.major < SSLv3_MAJOR) { |
7603 | 0 | WOLFSSL_MSG("Legacy version field contains unsupported value"); |
7604 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7605 | 0 | } |
7606 | | |
7607 | | #ifdef WOLFSSL_DTLS13 |
7608 | | if (ssl->options.dtls && |
7609 | | args->pv.major == DTLS_MAJOR && args->pv.minor > DTLSv1_2_MINOR) { |
7610 | | wantDowngrade = 1; |
7611 | | ssl->version.minor = args->pv.minor; |
7612 | | } |
7613 | | #endif /* WOLFSSL_DTLS13 */ |
7614 | | |
7615 | 0 | if (!ssl->options.dtls) { |
7616 | 0 | #ifndef WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION |
7617 | | /* Check for TLS 1.3 version (0x0304) in legacy version field. RFC 8446 |
7618 | | * Section 4.2.1 allows this action: |
7619 | | * |
7620 | | * "Servers MAY abort the handshake upon receiving a ClientHello with |
7621 | | * legacy_version 0x0304 or later." |
7622 | | * |
7623 | | * Note that if WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION is defined then the |
7624 | | * semantics of RFC 5246 Appendix E will be followed. A ServerHello with |
7625 | | * version 1.2 will be sent. The same is true if TLS 1.3 is not enabled. |
7626 | | */ |
7627 | 0 | if (args->pv.major == SSLv3_MAJOR && args->pv.minor >= TLSv1_3_MINOR) { |
7628 | 0 | WOLFSSL_MSG("Legacy version field is TLS 1.3 or later. Aborting."); |
7629 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7630 | 0 | } |
7631 | 0 | #endif /* WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION */ |
7632 | | |
7633 | | /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */ |
7634 | 0 | if (args->pv.major > SSLv3_MAJOR || (args->pv.major == SSLv3_MAJOR && |
7635 | 0 | args->pv.minor >= TLSv1_3_MINOR)) { |
7636 | 0 | args->pv.major = SSLv3_MAJOR; |
7637 | 0 | args->pv.minor = TLSv1_2_MINOR; |
7638 | 0 | wantDowngrade = 1; |
7639 | 0 | ssl->version.minor = args->pv.minor; |
7640 | 0 | } |
7641 | | /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */ |
7642 | 0 | else if (args->pv.major == SSLv3_MAJOR && |
7643 | 0 | args->pv.minor < TLSv1_2_MINOR) { |
7644 | 0 | wantDowngrade = 1; |
7645 | 0 | ssl->version.minor = args->pv.minor; |
7646 | 0 | } |
7647 | 0 | } |
7648 | | |
7649 | 0 | if (!wantDowngrade) { |
7650 | 0 | ret = DoTls13SupportedVersions(ssl, input + args->begin, |
7651 | 0 | args->idx - args->begin, helloSz, &wantDowngrade); |
7652 | 0 | if (ret < 0) |
7653 | 0 | goto exit_dch; |
7654 | 0 | } |
7655 | | |
7656 | 0 | if (wantDowngrade) { |
7657 | 0 | #ifndef WOLFSSL_NO_TLS12 |
7658 | 0 | byte realMinor; |
7659 | 0 | #endif |
7660 | | #if defined(HAVE_ECH) |
7661 | | if (ssl->options.echProcessingInner) { |
7662 | | WOLFSSL_MSG("ECH: inner client hello does not support version " |
7663 | | "less than TLS v1.3"); |
7664 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7665 | | } |
7666 | | #endif |
7667 | 0 | #ifndef WOLFSSL_NO_TLS12 |
7668 | 0 | if (!ssl->options.downgrade) { |
7669 | 0 | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7670 | 0 | "TLS v1.3"); |
7671 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7672 | 0 | } |
7673 | | |
7674 | 0 | if ((!ssl->options.dtls |
7675 | 0 | && args->pv.minor < ssl->options.minDowngrade) || |
7676 | 0 | (ssl->options.dtls && args->pv.minor > ssl->options.minDowngrade)) { |
7677 | 0 | WOLFSSL_MSG("\tversion below minimum allowed, fatal error"); |
7678 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7679 | 0 | } |
7680 | | |
7681 | 0 | realMinor = ssl->version.minor; |
7682 | 0 | ssl->version.minor = args->pv.minor; |
7683 | 0 | ret = HashInput(ssl, input + args->begin, (int)helloSz); |
7684 | 0 | ssl->version.minor = realMinor; |
7685 | 0 | if (ret == 0) { |
7686 | 0 | ret = DoClientHello(ssl, input, inOutIdx, helloSz); |
7687 | 0 | } |
7688 | 0 | goto exit_dch; |
7689 | | #else |
7690 | | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7691 | | "TLS v1.3"); |
7692 | | ERROR_OUT(VERSION_ERROR, exit_dch); |
7693 | | #endif |
7694 | 0 | } |
7695 | | |
7696 | | /* From here on we are a TLS 1.3 ClientHello. */ |
7697 | | |
7698 | | /* Client random |
7699 | | * ECH Accepted -> This will fill with the innerClientRandom */ |
7700 | 0 | XMEMCPY(ssl->arrays->clientRandom, input + args->idx, RAN_LEN); |
7701 | 0 | args->idx += RAN_LEN; |
7702 | |
|
7703 | | #ifdef WOLFSSL_DEBUG_TLS |
7704 | | WOLFSSL_MSG("client random"); |
7705 | | WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN); |
7706 | | #endif |
7707 | |
|
7708 | 0 | sessIdSz = input[args->idx++]; |
7709 | 0 | if (sessIdSz > ID_LEN) |
7710 | 0 | { |
7711 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7712 | 0 | } |
7713 | | |
7714 | 0 | if (sessIdSz + args->idx > helloSz) |
7715 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7716 | |
|
7717 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID) |
7718 | | /* RFC 9147 Section 5: "DTLS servers MUST NOT echo the legacy_session_id |
7719 | | * value from the client." Don't store it so SendTls13ServerHello can't |
7720 | | * echo it. */ |
7721 | | if (ssl->options.dtls) { |
7722 | | ssl->session->sessionIDSz = 0; |
7723 | | } |
7724 | | else |
7725 | | #endif |
7726 | 0 | { |
7727 | 0 | ssl->session->sessionIDSz = sessIdSz; |
7728 | 0 | if (sessIdSz > 0) |
7729 | 0 | XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz); |
7730 | 0 | } |
7731 | 0 | args->idx += sessIdSz; |
7732 | |
|
7733 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
7734 | | /* RFC 8446 Appendix D.4: server MUST only send CCS if the client's |
7735 | | * ClientHello contains a non-empty legacy_session_id. */ |
7736 | | if (sessIdSz == 0) { |
7737 | | ssl->options.tls13MiddleBoxCompat = 0; |
7738 | | } |
7739 | | #endif |
7740 | |
|
7741 | | #ifdef WOLFSSL_DTLS13 |
7742 | | /* legacy_cookie */ |
7743 | | if (ssl->options.dtls) { |
7744 | | word32 rel = args->idx - args->begin; |
7745 | | byte cookieLen; |
7746 | | if (rel > helloSz || helloSz - rel < OPAQUE8_LEN) |
7747 | | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7748 | | /* https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 */ |
7749 | | cookieLen = input[args->idx++]; |
7750 | | if (cookieLen != 0) { |
7751 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7752 | | } |
7753 | | } |
7754 | | #endif /* WOLFSSL_DTLS13 */ |
7755 | |
|
7756 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
7757 | 0 | ssl->clSuites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap, |
7758 | 0 | DYNAMIC_TYPE_SUITES); |
7759 | 0 | if (ssl->clSuites == NULL) { |
7760 | 0 | ERROR_OUT(MEMORY_E, exit_dch); |
7761 | 0 | } |
7762 | | |
7763 | | /* Cipher suites */ |
7764 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7765 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7766 | 0 | ato16(&input[args->idx], &ssl->clSuites->suiteSz); |
7767 | 0 | args->idx += OPAQUE16_LEN; |
7768 | 0 | if ((ssl->clSuites->suiteSz % 2) != 0) { |
7769 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7770 | 0 | } |
7771 | | /* suites and compression length check */ |
7772 | 0 | if ((args->idx - args->begin) + ssl->clSuites->suiteSz + OPAQUE8_LEN > |
7773 | 0 | helloSz) { |
7774 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7775 | 0 | } |
7776 | 0 | if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) |
7777 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7778 | 0 | XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); |
7779 | 0 | args->idx += ssl->clSuites->suiteSz; |
7780 | 0 | ssl->clSuites->hashSigAlgoSz = 0; |
7781 | | |
7782 | | /* Compression */ |
7783 | 0 | b = input[args->idx++]; |
7784 | 0 | if ((args->idx - args->begin) + b > helloSz) |
7785 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7786 | 0 | if (b != COMP_LEN) { |
7787 | 0 | WOLFSSL_MSG("Must be one compression type in list"); |
7788 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7789 | 0 | } |
7790 | 0 | b = input[args->idx++]; |
7791 | 0 | if (b != NO_COMPRESSION) { |
7792 | 0 | WOLFSSL_MSG("Must be no compression type in list"); |
7793 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7794 | 0 | } |
7795 | | |
7796 | | /* Extensions */ |
7797 | 0 | if ((args->idx - args->begin) == helloSz) |
7798 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7799 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7800 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7801 | |
|
7802 | 0 | ato16(&input[args->idx], &totalExtSz); |
7803 | 0 | args->idx += OPAQUE16_LEN; |
7804 | 0 | if ((args->idx - args->begin) + totalExtSz > helloSz) |
7805 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7806 | | |
7807 | | /* Auto populate extensions supported unless user defined. */ |
7808 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0) |
7809 | 0 | goto exit_dch; |
7810 | | |
7811 | | #if defined(HAVE_ECH) |
7812 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7813 | | /* save the start of the buffer so we can use it when parsing ech */ |
7814 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7815 | | |
7816 | | if (echX == NULL) |
7817 | | ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch); |
7818 | | |
7819 | | ((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ; |
7820 | | ((WOLFSSL_ECH*)echX->data)->aadLen = helloSz; |
7821 | | } |
7822 | | #endif |
7823 | | |
7824 | | /* Parse extensions */ |
7825 | 0 | if ((ret = TLSX_Parse(ssl, input + args->idx, totalExtSz, client_hello, |
7826 | 0 | ssl->clSuites))) { |
7827 | 0 | goto exit_dch; |
7828 | 0 | } |
7829 | | |
7830 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7831 | | defined(HAVE_TLS_EXTENSIONS) |
7832 | | /* RFC 8446 Section 4.2.11: the pre_shared_key extension MUST be the |
7833 | | * last extension in the ClientHello. wolfSSL stores extensions in |
7834 | | * reverse wire order (TLSX_Push prepends), so a well-formed |
7835 | | * ClientHello with PSK leaves PSK at the head of ssl->extensions |
7836 | | * here, before any post-parse code (e.g. ALPN_Select) modifies the |
7837 | | * list. */ |
7838 | | { |
7839 | | TLSX* pskExt = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
7840 | | if (pskExt != NULL && ssl->extensions != pskExt) { |
7841 | | WOLFSSL_MSG("pre_shared_key extension was not last in " |
7842 | | "ClientHello"); |
7843 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
7844 | | ERROR_OUT(PSK_KEY_ERROR, exit_dch); |
7845 | | } |
7846 | | } |
7847 | | #endif |
7848 | | |
7849 | | #if defined(HAVE_ECH) |
7850 | | /* ECH accept/reject reconciliation is done at the end of TLSX_Parse. On |
7851 | | * acceptance the inner hello was decrypted, so jump to exit and let the |
7852 | | * caller re-invoke with the inner hello. */ |
7853 | | if (!ssl->options.echProcessingInner && echX != NULL && |
7854 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
7855 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7856 | | goto exit_dch; |
7857 | | } |
7858 | | #endif |
7859 | | |
7860 | 0 | #ifdef HAVE_SNI |
7861 | 0 | if ((ret = SNI_Callback(ssl)) != 0) |
7862 | 0 | goto exit_dch; |
7863 | 0 | ssl->options.side = WOLFSSL_SERVER_END; |
7864 | 0 | #endif |
7865 | |
|
7866 | 0 | args->idx += totalExtSz; |
7867 | 0 | ssl->options.haveSessionId = 1; |
7868 | 0 | ssl->options.sendVerify = SEND_CERT; |
7869 | |
|
7870 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7871 | | ssl->options.cookieGood = 0; |
7872 | | if (ssl->options.sendCookie && |
7873 | | (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE |
7874 | | #ifdef WOLFSSL_DTLS13 |
7875 | | /* Always check for a valid cookie since we may have already |
7876 | | * sent a HRR but we reset the state. */ |
7877 | | || ssl->options.dtls |
7878 | | #endif |
7879 | | )) { |
7880 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_COOKIE); |
7881 | | |
7882 | | if (ext != NULL) { |
7883 | | /* Ensure the cookie came from client and isn't the one in the |
7884 | | * response - HelloRetryRequest. |
7885 | | */ |
7886 | | if (ext->resp == 0) { |
7887 | | ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data); |
7888 | | if (ret != 0) |
7889 | | goto exit_dch; |
7890 | | /* Don't change state here as we may want to enter |
7891 | | * DoTls13ClientHello again. */ |
7892 | | ssl->options.cookieGood = 1; |
7893 | | } |
7894 | | else { |
7895 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7896 | | } |
7897 | | } |
7898 | | else { |
7899 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME) |
7900 | | /* Don't error out as we may be resuming. We confirm this later. */ |
7901 | | if (!ssl->options.dtls) |
7902 | | #endif |
7903 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7904 | | } |
7905 | | } |
7906 | | #endif |
7907 | |
|
7908 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7909 | 0 | if (ssl->hrr_keyshare_group != 0) { |
7910 | | /* |
7911 | | * https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8 |
7912 | | * when sending the new ClientHello, the client MUST |
7913 | | * replace the original "key_share" extension with one containing only |
7914 | | * a new KeyShareEntry for the group indicated in the selected_group |
7915 | | * field of the triggering HelloRetryRequest. |
7916 | | */ |
7917 | 0 | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7918 | 0 | if (extension != NULL) { |
7919 | 0 | KeyShareEntry* kse = (KeyShareEntry*)extension->data; |
7920 | | /* Exactly one KeyShareEntry with the HRR group must be present. */ |
7921 | 0 | if (kse == NULL || kse->next != NULL || |
7922 | 0 | kse->group != ssl->hrr_keyshare_group) { |
7923 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7924 | 0 | } |
7925 | 0 | } |
7926 | 0 | else |
7927 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7928 | 0 | } |
7929 | 0 | #endif |
7930 | | |
7931 | | #if defined(HAVE_ECH) |
7932 | | /* hash clientHelloInner to hsHashesEch */ |
7933 | | if (echX != NULL && ssl->ctx->echConfigs != NULL && |
7934 | | !ssl->options.disableECH && |
7935 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7936 | | ret = EchHashHelloInner(ssl, (WOLFSSL_ECH*)echX->data); |
7937 | | if (ret != 0) |
7938 | | goto exit_dch; |
7939 | | ((WOLFSSL_ECH*)echX->data)->innerCount = 1; |
7940 | | } |
7941 | | #endif |
7942 | | |
7943 | | #ifdef HAVE_ALPN |
7944 | | /* Select the ALPN protocol before PSK selection so that the |
7945 | | * selected value is available to the per-PSK SNI/ALPN binding check |
7946 | | * inside CheckPreSharedKeys/DoPreSharedKeys. ALPN_Select itself |
7947 | | * only inspects ssl->extensions and the app callback; it does not |
7948 | | * depend on any state set during PSK validation. */ |
7949 | | if ((ret = ALPN_Select(ssl)) != 0) |
7950 | | goto exit_dch; |
7951 | | #endif |
7952 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7953 | | defined(HAVE_TLS_EXTENSIONS) |
7954 | | ret = CheckPreSharedKeys(ssl, input + args->begin, helloSz, ssl->clSuites, |
7955 | | &args->usingPSK); |
7956 | | if (ret != 0) |
7957 | | goto exit_dch; |
7958 | | #else |
7959 | 0 | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7960 | 0 | goto exit_dch; |
7961 | 0 | #endif |
7962 | | |
7963 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7964 | | defined(HAVE_TLS_EXTENSIONS) |
7965 | | if (!args->usingPSK |
7966 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
7967 | | || ssl->options.certWithExternPsk |
7968 | | #endif |
7969 | | ) |
7970 | | #endif |
7971 | 0 | { |
7972 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7973 | | /* Not using PSK so don't require no KE. */ |
7974 | | ssl->options.noPskDheKe = 0; |
7975 | | #endif |
7976 | |
|
7977 | 0 | #ifndef NO_CERTS |
7978 | 0 | if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) { |
7979 | 0 | WOLFSSL_MSG("Client did not send a KeyShare extension"); |
7980 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7981 | 0 | } |
7982 | | /* Can't check ssl->extensions here as SigAlgs are unconditionally |
7983 | | set by TLSX_PopulateExtensions */ |
7984 | 0 | if (ssl->clSuites->hashSigAlgoSz == 0) { |
7985 | 0 | WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension"); |
7986 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7987 | 0 | } |
7988 | | #else |
7989 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7990 | | #endif |
7991 | 0 | } |
7992 | |
|
7993 | 0 | } /* case TLS_ASYNC_BEGIN */ |
7994 | 0 | FALL_THROUGH; |
7995 | |
|
7996 | 0 | case TLS_ASYNC_BUILD: |
7997 | | /* Advance state and proceed */ |
7998 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
7999 | 0 | FALL_THROUGH; |
8000 | |
|
8001 | 0 | case TLS_ASYNC_DO: |
8002 | 0 | { |
8003 | | #ifdef WOLFSSL_CERT_SETUP_CB |
8004 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
8005 | | goto exit_dch; |
8006 | | #endif |
8007 | 0 | #ifndef NO_CERTS |
8008 | 0 | if (!args->usingPSK) { |
8009 | 0 | if ((ret = MatchSuite(ssl, ssl->clSuites)) < 0) { |
8010 | | #ifdef WOLFSSL_ASYNC_CRYPT |
8011 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
8012 | | #endif |
8013 | 0 | WOLFSSL_MSG("Unsupported cipher suite, ClientHello 1.3"); |
8014 | 0 | goto exit_dch; |
8015 | 0 | } |
8016 | 0 | } |
8017 | 0 | #endif |
8018 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
8019 | 0 | if (args->usingPSK == 2) { |
8020 | | /* Pick key share and Generate a new key if not present. */ |
8021 | 0 | int doHelloRetry = 0; |
8022 | 0 | ret = TLSX_KeyShare_Establish(ssl, &doHelloRetry); |
8023 | 0 | if (doHelloRetry) { |
8024 | | /* Make sure we don't send HRR twice */ |
8025 | 0 | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
8026 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
8027 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
8028 | 0 | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
8029 | 0 | ret = 0; /* for hello_retry return 0 */ |
8030 | 0 | } |
8031 | 0 | if (ret != 0) |
8032 | 0 | goto exit_dch; |
8033 | 0 | } |
8034 | 0 | #endif |
8035 | | |
8036 | | /* Verify the cipher suite is the same as what was chosen in HRR. |
8037 | | * got_client_hello == 2 covers the stateful path. |
8038 | | * cookieGood covers the stateless DTLS path. */ |
8039 | 0 | if ((ssl->msgsReceived.got_client_hello == 2 |
8040 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
8041 | | || ssl->options.cookieGood |
8042 | | #endif |
8043 | 0 | ) && |
8044 | 0 | (ssl->options.cipherSuite0 != ssl->options.hrrCipherSuite0 || |
8045 | 0 | ssl->options.cipherSuite != ssl->options.hrrCipherSuite)) { |
8046 | 0 | WOLFSSL_MSG("Cipher suite in second ClientHello does not match " |
8047 | 0 | "HelloRetryRequest"); |
8048 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
8049 | 0 | } |
8050 | | |
8051 | | /* Advance state and proceed */ |
8052 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
8053 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
8054 | 0 | FALL_THROUGH; |
8055 | |
|
8056 | 0 | case TLS_ASYNC_VERIFY: |
8057 | 0 | { |
8058 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SUPPORTED_CURVES) |
8059 | | /* Check if the KeyShare calculations from the previous state are complete. |
8060 | | * wolfSSL_AsyncPop advances ssl->options.asyncState so we may end up here |
8061 | | * with a pending calculation. */ |
8062 | | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
8063 | | if (extension != NULL && extension->resp == 1) { |
8064 | | KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data; |
8065 | | if (serverKSE != NULL && |
8066 | | serverKSE->lastRet == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
8067 | | #if defined(WOLFSSL_HAVE_MLKEM) |
8068 | | if (WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(serverKSE->group)) { |
8069 | | ret = TLSX_KeyShare_HandlePqcHybridKeyServer(ssl, serverKSE, |
8070 | | serverKSE->ke, serverKSE->keLen); |
8071 | | } |
8072 | | else |
8073 | | #endif |
8074 | | { |
8075 | | ret = TLSX_KeyShare_GenKey(ssl, serverKSE); |
8076 | | } |
8077 | | if (ret != 0) |
8078 | | goto exit_dch; |
8079 | | } |
8080 | | } |
8081 | | #endif |
8082 | | /* Advance state and proceed */ |
8083 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
8084 | 0 | } |
8085 | 0 | FALL_THROUGH; |
8086 | |
|
8087 | 0 | case TLS_ASYNC_FINALIZE: |
8088 | 0 | { |
8089 | 0 | *inOutIdx = args->idx; |
8090 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
8091 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
8092 | | ssl->options.pskNegotiated = (args->usingPSK != 0); |
8093 | | #endif |
8094 | |
|
8095 | 0 | if (!args->usingPSK) { |
8096 | 0 | #ifndef NO_CERTS |
8097 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
8098 | | #ifdef HAVE_NULL_CIPHER |
8099 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
8100 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
8101 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
8102 | | ; |
8103 | | } |
8104 | | else |
8105 | | #endif |
8106 | 0 | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
8107 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
8108 | 0 | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
8109 | 0 | ; /* Do nothing. */ |
8110 | 0 | } |
8111 | 0 | else |
8112 | 0 | #endif |
8113 | 0 | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
8114 | 0 | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
8115 | 0 | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
8116 | 0 | ; /* Do nothing. */ |
8117 | 0 | } |
8118 | 0 | else |
8119 | 0 | #endif |
8120 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
8121 | 0 | WOLFSSL_MSG("Negotiated ciphersuite from lesser version than " |
8122 | 0 | "TLS v1.3"); |
8123 | 0 | ERROR_OUT(MATCH_SUITE_ERROR, exit_dch); |
8124 | 0 | } |
8125 | | |
8126 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
8127 | | if (ssl->options.resuming) { |
8128 | | ssl->options.resuming = 0; |
8129 | | ssl->arrays->psk_keySz = 0; |
8130 | | XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size); |
8131 | | } |
8132 | | #endif |
8133 | | |
8134 | | /* Derive early secret for handshake secret. */ |
8135 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
8136 | 0 | goto exit_dch; |
8137 | 0 | #endif /* !NO_CERTS */ |
8138 | 0 | } |
8139 | 0 | break; |
8140 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
8141 | 0 | default: |
8142 | 0 | ret = INPUT_CASE_ERROR; |
8143 | 0 | } /* switch (ssl->options.asyncState) */ |
8144 | | |
8145 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
8146 | | if (ret == 0 && ssl->options.sendCookie) { |
8147 | | if (ssl->options.cookieGood && |
8148 | | ssl->options.acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE) { |
8149 | | /* Processing second ClientHello. Clear HRR state. */ |
8150 | | ssl->options.serverState = NULL_STATE; |
8151 | | } |
8152 | | |
8153 | | if (ssl->options.cookieGood && |
8154 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
8155 | | /* If we already verified the peer with a cookie then we can't |
8156 | | * do another HRR for cipher negotiation. Send alert and restart |
8157 | | * the entire handshake. */ |
8158 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
8159 | | } |
8160 | | #ifdef WOLFSSL_DTLS13 |
8161 | | if (ssl->options.dtls && |
8162 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
8163 | | /* Cookie and key share negotiation should be handled in |
8164 | | * DoClientHelloStateless. If we enter here then something went |
8165 | | * wrong in our logic. */ |
8166 | | ERROR_OUT(BAD_HELLO, exit_dch); |
8167 | | } |
8168 | | #endif |
8169 | | /* Send a cookie */ |
8170 | | if (!ssl->options.cookieGood && |
8171 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
8172 | | #ifdef WOLFSSL_DTLS13 |
8173 | | if (ssl->options.dtls) { |
8174 | | #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME |
8175 | | /* We can skip cookie on resumption */ |
8176 | | if (!ssl->options.dtls || !ssl->options.dtls13NoHrrOnResume || |
8177 | | !args->usingPSK) |
8178 | | #endif |
8179 | | ERROR_OUT(BAD_HELLO, exit_dch); |
8180 | | } |
8181 | | else |
8182 | | #endif |
8183 | | { |
8184 | | /* Need to remove the keyshare ext if we found a common group |
8185 | | * and are not doing curve negotiation. */ |
8186 | | TLSX_Remove(&ssl->extensions, TLSX_KEY_SHARE, ssl->heap); |
8187 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
8188 | | } |
8189 | | |
8190 | | } |
8191 | | } |
8192 | | #endif /* WOLFSSL_DTLS13 */ |
8193 | | |
8194 | | #ifdef WOLFSSL_DTLS_CID |
8195 | | /* do not modify CID state if we are sending an HRR */ |
8196 | | if (ret == 0 && ssl->options.dtls && ssl->options.useDtlsCID && |
8197 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
8198 | | DtlsCIDOnExtensionsParsed(ssl); |
8199 | | #endif /* WOLFSSL_DTLS_CID */ |
8200 | | |
8201 | | |
8202 | | |
8203 | 0 | exit_dch: |
8204 | |
|
8205 | 0 | WOLFSSL_LEAVE("DoTls13ClientHello", ret); |
8206 | |
|
8207 | | #ifdef WOLFSSL_ASYNC_CRYPT |
8208 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
8209 | | ssl->msgsReceived.got_client_hello = 0; |
8210 | | return ret; |
8211 | | } |
8212 | | #endif |
8213 | |
|
8214 | 0 | FreeDch13Args(ssl, args); |
8215 | | #ifdef WOLFSSL_ASYNC_CRYPT |
8216 | | FreeAsyncCtx(ssl, 0); |
8217 | | #endif |
8218 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO); |
8219 | |
|
8220 | 0 | if (ret != 0) { |
8221 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
8222 | 0 | } |
8223 | |
|
8224 | | #if defined(HAVE_ECH) |
8225 | | if (ret == 0 && echX != NULL && |
8226 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
8227 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
8228 | | |
8229 | | /* add the header to the inner hello */ |
8230 | | AddTls13HandShakeHeader(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
8231 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen, 0, 0, |
8232 | | client_hello, ssl); |
8233 | | } |
8234 | | #endif |
8235 | |
|
8236 | 0 | return ret; |
8237 | 0 | } |
8238 | | |
8239 | | /* Send TLS v1.3 ServerHello message to client. |
8240 | | * Only a server will send this message. |
8241 | | * |
8242 | | * ssl The SSL/TLS object. |
8243 | | * returns 0 on success, otherwise failure. |
8244 | | */ |
8245 | | /* handle generation of TLS 1.3 server_hello (2) */ |
8246 | | int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType) |
8247 | 0 | { |
8248 | 0 | int ret; |
8249 | 0 | byte* output; |
8250 | 0 | word16 length; |
8251 | 0 | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
8252 | 0 | int sendSz; |
8253 | | #if defined(HAVE_ECH) |
8254 | | TLSX* echX = NULL; |
8255 | | byte* acceptLabel = (byte*)echAcceptConfirmationLabel; |
8256 | | word32 acceptOffset; |
8257 | | word16 acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
8258 | | #endif |
8259 | |
|
8260 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND); |
8261 | 0 | WOLFSSL_ENTER("SendTls13ServerHello"); |
8262 | | |
8263 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated in |
8264 | | * dtls.c */ |
8265 | 0 | if (extMsgType == hello_retry_request |
8266 | | #ifdef WOLFSSL_DTLS13 |
8267 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
8268 | | #endif |
8269 | 0 | ) { |
8270 | 0 | WOLFSSL_MSG("wolfSSL Sending HelloRetryRequest"); |
8271 | 0 | if ((ret = RestartHandshakeHash(ssl)) < 0) |
8272 | 0 | return ret; |
8273 | 0 | } |
8274 | | |
8275 | 0 | ssl->options.buildingMsg = 1; |
8276 | | #ifdef WOLFSSL_DTLS13 |
8277 | | if (ssl->options.dtls) |
8278 | | idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ; |
8279 | | #endif /* WOLFSSL_DTLS13 */ |
8280 | | |
8281 | | /* Protocol version, server random, session id, cipher suite, compression |
8282 | | * and extensions. |
8283 | | */ |
8284 | 0 | length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz + |
8285 | 0 | SUITE_LEN + COMP_LEN; |
8286 | 0 | ret = TLSX_GetResponseSize(ssl, extMsgType, &length); |
8287 | 0 | if (ret != 0) |
8288 | 0 | return ret; |
8289 | 0 | sendSz = (int)(idx + length); |
8290 | | |
8291 | | /* Check buffers are big enough and grow if needed. */ |
8292 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8293 | 0 | return ret; |
8294 | | |
8295 | | /* Get position in output buffer to write new message to. */ |
8296 | 0 | output = GetOutputBuffer(ssl); |
8297 | | |
8298 | | /* Put the record and handshake headers on. */ |
8299 | 0 | AddTls13Headers(output, length, server_hello, ssl); |
8300 | | |
8301 | | /* The protocol version must be TLS v1.2 for middleboxes. */ |
8302 | 0 | output[idx++] = ssl->version.major; |
8303 | 0 | output[idx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
8304 | |
|
8305 | 0 | if (extMsgType == server_hello) { |
8306 | | /* Generate server random. */ |
8307 | 0 | if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0) |
8308 | 0 | return ret; |
8309 | 0 | } |
8310 | 0 | else { |
8311 | | /* HelloRetryRequest message has fixed value for random. */ |
8312 | 0 | XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN); |
8313 | 0 | } |
8314 | | |
8315 | | #if defined(HAVE_ECH) |
8316 | | /* last 8 bytes of server random */ |
8317 | | acceptOffset = idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
8318 | | #endif |
8319 | | |
8320 | | /* Store in SSL for debugging. */ |
8321 | 0 | XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN); |
8322 | 0 | idx += RAN_LEN; |
8323 | |
|
8324 | | #ifdef WOLFSSL_DEBUG_TLS |
8325 | | WOLFSSL_MSG("Server random"); |
8326 | | WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN); |
8327 | | #endif |
8328 | |
|
8329 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_DTLS13_ECHO_LEGACY_SESSION_ID) |
8330 | | if (ssl->options.dtls) { |
8331 | | /* RFC 9147 Section 5: "DTLS servers MUST NOT echo the |
8332 | | * legacy_session_id value from the client." */ |
8333 | | output[idx++] = 0; |
8334 | | } |
8335 | | else |
8336 | | #endif |
8337 | 0 | { |
8338 | 0 | output[idx++] = ssl->session->sessionIDSz; |
8339 | 0 | if (ssl->session->sessionIDSz > 0) { |
8340 | 0 | XMEMCPY(output + idx, ssl->session->sessionID, |
8341 | 0 | ssl->session->sessionIDSz); |
8342 | 0 | idx += ssl->session->sessionIDSz; |
8343 | 0 | } |
8344 | 0 | } |
8345 | | |
8346 | | /* Chosen cipher suite */ |
8347 | 0 | output[idx++] = ssl->options.cipherSuite0; |
8348 | 0 | output[idx++] = ssl->options.cipherSuite; |
8349 | | #ifdef WOLFSSL_DEBUG_TLS |
8350 | | WOLFSSL_MSG("Chosen cipher suite:"); |
8351 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
8352 | | ssl->options.cipherSuite)); |
8353 | | #endif |
8354 | | |
8355 | | /* Compression not supported in TLS v1.3. */ |
8356 | 0 | output[idx++] = 0; |
8357 | | |
8358 | | /* Extensions */ |
8359 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL); |
8360 | 0 | if (ret != 0) |
8361 | 0 | return ret; |
8362 | | |
8363 | | /* When we send a HRR, we store the selected key share group to later check |
8364 | | * that the client uses the same group in the second ClientHello. |
8365 | | * |
8366 | | * In case of stateless DTLS, we do not store the group, however, as it is |
8367 | | * already stored in the cookie that is sent to the client. We later recover |
8368 | | * the group from the cookie to prevent storing a state in a stateless |
8369 | | * server. |
8370 | | * |
8371 | | * Similar logic holds for the hrrCipherSuite. */ |
8372 | 0 | if (extMsgType == hello_retry_request |
8373 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
8374 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
8375 | | #endif |
8376 | 0 | ) { |
8377 | 0 | TLSX* ksExt = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
8378 | 0 | if (ksExt != NULL) { |
8379 | 0 | KeyShareEntry* kse = (KeyShareEntry*)ksExt->data; |
8380 | 0 | if (kse != NULL) |
8381 | 0 | ssl->hrr_keyshare_group = kse->group; |
8382 | 0 | } |
8383 | |
|
8384 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
8385 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
8386 | 0 | } |
8387 | |
|
8388 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
8389 | | if (ssl->options.sendCookie && extMsgType == hello_retry_request) { |
8390 | | /* Reset the hashes from here. We will be able to restart the hashes |
8391 | | * from the cookie in RestartHandshakeHashWithCookie */ |
8392 | | #ifdef WOLFSSL_DTLS13 |
8393 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated |
8394 | | * in dtls.c */ |
8395 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) |
8396 | | ret = 0; |
8397 | | else |
8398 | | #endif |
8399 | | ret = InitHandshakeHashes(ssl); |
8400 | | } |
8401 | | else |
8402 | | #endif |
8403 | 0 | { |
8404 | | #ifdef WOLFSSL_DTLS13 |
8405 | | if (ssl->options.dtls) { |
8406 | | ret = Dtls13HashHandshake( |
8407 | | ssl, |
8408 | | output + Dtls13GetRlHeaderLength(ssl, 0) , |
8409 | | (word16)sendSz - Dtls13GetRlHeaderLength(ssl, 0)); |
8410 | | } |
8411 | | else |
8412 | | #endif /* WOLFSSL_DTLS13 */ |
8413 | 0 | { |
8414 | | #if defined(HAVE_ECH) |
8415 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
8416 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
8417 | | if (echX == NULL) |
8418 | | return WOLFSSL_FATAL_ERROR; |
8419 | | /* use hrr offset */ |
8420 | | if (extMsgType == hello_retry_request) { |
8421 | | acceptOffset = |
8422 | | (word32)(((WOLFSSL_ECH*)echX->data)->confBuf - output); |
8423 | | acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
8424 | | acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
8425 | | } |
8426 | | /* replace the last 8 bytes of server random with the accept */ |
8427 | | if (((WOLFSSL_ECH*)echX->data)->state == ECH_PARSED_INTERNAL) { |
8428 | | if (ret == 0) { |
8429 | | ret = EchWriteAcceptance(ssl, acceptLabel, |
8430 | | acceptLabelSz, output + RECORD_HEADER_SZ, |
8431 | | acceptOffset - RECORD_HEADER_SZ, |
8432 | | sendSz - RECORD_HEADER_SZ, extMsgType); |
8433 | | } |
8434 | | if (extMsgType == hello_retry_request) { |
8435 | | /* reset the ech state for round 2 */ |
8436 | | ((WOLFSSL_ECH*)echX->data)->state = ECH_WRITE_NONE; |
8437 | | /* inner hello no longer needed, free it */ |
8438 | | XFREE(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
8439 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
8440 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello = NULL; |
8441 | | } |
8442 | | else { |
8443 | | if (ret == 0) { |
8444 | | /* update serverRandom on success */ |
8445 | | XMEMCPY(ssl->arrays->serverRandom, |
8446 | | output + acceptOffset - |
8447 | | (RAN_LEN -ECH_ACCEPT_CONFIRMATION_SZ), RAN_LEN); |
8448 | | } |
8449 | | /* remove ech so we don't keep sending it in write */ |
8450 | | TLSX_Remove(&ssl->extensions, TLSX_ECH, ssl->heap); |
8451 | | } |
8452 | | } |
8453 | | } |
8454 | | #endif |
8455 | 0 | if (ret == 0) |
8456 | 0 | ret = HashOutput(ssl, output, sendSz, 0); |
8457 | 0 | } |
8458 | 0 | } |
8459 | |
|
8460 | 0 | if (ret != 0) |
8461 | 0 | return ret; |
8462 | | |
8463 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8464 | | if (ssl->hsInfoOn) |
8465 | | AddPacketName(ssl, "ServerHello"); |
8466 | | if (ssl->toInfoOn) { |
8467 | | ret = AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz, |
8468 | | WRITE_PROTO, 0, ssl->heap); |
8469 | | if (ret != 0) |
8470 | | return ret; |
8471 | | } |
8472 | | #endif |
8473 | | |
8474 | 0 | if (extMsgType == server_hello) |
8475 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
8476 | |
|
8477 | 0 | ssl->options.buildingMsg = 0; |
8478 | | #ifdef WOLFSSL_DTLS13 |
8479 | | if (ssl->options.dtls) { |
8480 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)sendSz, |
8481 | | (enum HandShakeType)extMsgType, 0); |
8482 | | |
8483 | | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
8484 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
8485 | | return ret; |
8486 | | } |
8487 | | #endif /* WOLFSSL_DTLS13 */ |
8488 | |
|
8489 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8490 | |
|
8491 | 0 | if (!ssl->options.groupMessages || extMsgType != server_hello) |
8492 | 0 | ret = SendBuffered(ssl); |
8493 | |
|
8494 | 0 | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
8495 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
8496 | |
|
8497 | 0 | return ret; |
8498 | 0 | } |
8499 | | |
8500 | | /* handle generation of TLS 1.3 encrypted_extensions (8) */ |
8501 | | /* Send the rest of the extensions encrypted under the handshake key. |
8502 | | * This message is always encrypted in TLS v1.3. |
8503 | | * Only a server will send this message. |
8504 | | * |
8505 | | * ssl The SSL/TLS object. |
8506 | | * returns 0 on success, otherwise failure. |
8507 | | */ |
8508 | | static int SendTls13EncryptedExtensions(WOLFSSL* ssl) |
8509 | 0 | { |
8510 | 0 | int ret; |
8511 | 0 | byte* output; |
8512 | 0 | word16 length = 0; |
8513 | 0 | word32 idx; |
8514 | 0 | int sendSz; |
8515 | |
|
8516 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8517 | 0 | WOLFSSL_ENTER("SendTls13EncryptedExtensions"); |
8518 | |
|
8519 | 0 | ssl->options.buildingMsg = 1; |
8520 | 0 | ssl->keys.encryptionOn = 1; |
8521 | |
|
8522 | | #ifdef WOLFSSL_DTLS13 |
8523 | | if (ssl->options.dtls) { |
8524 | | idx = Dtls13GetHeadersLength(ssl, encrypted_extensions); |
8525 | | } |
8526 | | else |
8527 | | #endif /* WOLFSSL_DTLS13 */ |
8528 | 0 | { |
8529 | 0 | idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
8530 | 0 | } |
8531 | |
|
8532 | 0 | #if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
8533 | 0 | if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0) |
8534 | 0 | return ret; |
8535 | 0 | #endif |
8536 | | |
8537 | | /* Derive the handshake secret now that we are at first message to be |
8538 | | * encrypted under the keys. |
8539 | | */ |
8540 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
8541 | 0 | return ret; |
8542 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
8543 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) |
8544 | 0 | return ret; |
8545 | | |
8546 | | /* Setup encrypt/decrypt keys for following messages. */ |
8547 | | #ifdef WOLFSSL_EARLY_DATA |
8548 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
8549 | | return ret; |
8550 | | if (ssl->earlyData != process_early_data) { |
8551 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
8552 | | return ret; |
8553 | | } |
8554 | | #else |
8555 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
8556 | 0 | return ret; |
8557 | 0 | #endif |
8558 | | #ifdef WOLFSSL_QUIC |
8559 | | if (IsAtLeastTLSv1_3(ssl->version) && WOLFSSL_IS_QUIC(ssl)) { |
8560 | | ret = wolfSSL_quic_add_transport_extensions(ssl, encrypted_extensions); |
8561 | | if (ret != 0) |
8562 | | return ret; |
8563 | | } |
8564 | | #endif |
8565 | | |
8566 | | #ifdef WOLFSSL_DTLS13 |
8567 | | if (ssl->options.dtls) { |
8568 | | w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
8569 | | ssl->dtls13Epoch = epochHandshake; |
8570 | | |
8571 | | ret = Dtls13SetEpochKeys( |
8572 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
8573 | | if (ret != 0) |
8574 | | return ret; |
8575 | | |
8576 | | } |
8577 | | #endif /* WOLFSSL_DTLS13 */ |
8578 | | |
8579 | 0 | ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length); |
8580 | 0 | if (ret != 0) |
8581 | 0 | return ret; |
8582 | | |
8583 | 0 | sendSz = (int)(idx + length); |
8584 | | /* Encryption always on. */ |
8585 | 0 | sendSz += MAX_MSG_EXTRA; |
8586 | | |
8587 | | /* Check buffers are big enough and grow if needed. */ |
8588 | 0 | ret = CheckAvailableSize(ssl, sendSz); |
8589 | 0 | if (ret != 0) |
8590 | 0 | return ret; |
8591 | | |
8592 | | /* Get position in output buffer to write new message to. */ |
8593 | 0 | output = GetOutputBuffer(ssl); |
8594 | | |
8595 | | /* Put the record and handshake headers on. */ |
8596 | 0 | AddTls13Headers(output, length, encrypted_extensions, ssl); |
8597 | |
|
8598 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL); |
8599 | 0 | if (ret != 0) |
8600 | 0 | return ret; |
8601 | 0 | idx += length; |
8602 | |
|
8603 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8604 | | if (ssl->hsInfoOn) |
8605 | | AddPacketName(ssl, "EncryptedExtensions"); |
8606 | | if (ssl->toInfoOn) { |
8607 | | ret = AddPacketInfo(ssl, "EncryptedExtensions", handshake, output, |
8608 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8609 | | if (ret != 0) |
8610 | | return ret; |
8611 | | } |
8612 | | #endif |
8613 | |
|
8614 | | #ifdef WOLFSSL_DTLS13 |
8615 | | if (ssl->options.dtls) { |
8616 | | ssl->options.buildingMsg = 0; |
8617 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)idx, |
8618 | | encrypted_extensions, 1); |
8619 | | |
8620 | | if (ret == 0) |
8621 | | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
8622 | | |
8623 | | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
8624 | | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8625 | | |
8626 | | return ret; |
8627 | | } |
8628 | | #endif /* WOLFSSL_DTLS13 */ |
8629 | | |
8630 | | /* This handshake message is always encrypted. */ |
8631 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
8632 | 0 | (int)(idx - RECORD_HEADER_SZ), |
8633 | 0 | handshake, 1, 0, 0); |
8634 | 0 | if (sendSz < 0) |
8635 | 0 | return sendSz; |
8636 | | |
8637 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8638 | 0 | ssl->options.buildingMsg = 0; |
8639 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
8640 | |
|
8641 | 0 | if (!ssl->options.groupMessages) |
8642 | 0 | ret = SendBuffered(ssl); |
8643 | | |
8644 | |
|
8645 | 0 | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
8646 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8647 | |
|
8648 | 0 | return ret; |
8649 | 0 | } |
8650 | | |
8651 | | #ifndef NO_CERTS |
8652 | | /* handle generation TLS v1.3 certificate_request (13) */ |
8653 | | /* Send the TLS v1.3 CertificateRequest message. |
8654 | | * This message is always encrypted in TLS v1.3. |
8655 | | * Only a server will send this message. |
8656 | | * |
8657 | | * ssl SSL/TLS object. |
8658 | | * reqCtx Request context. |
8659 | | * reqCtxLen Length of context. 0 when sending as part of handshake. |
8660 | | * returns 0 on success, otherwise failure. |
8661 | | */ |
8662 | | static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx, |
8663 | | word32 reqCtxLen) |
8664 | 0 | { |
8665 | 0 | byte* output; |
8666 | 0 | int ret; |
8667 | 0 | int sendSz; |
8668 | 0 | word32 i; |
8669 | 0 | word32 reqSz; |
8670 | 0 | SignatureAlgorithms* sa; |
8671 | |
|
8672 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8673 | 0 | WOLFSSL_ENTER("SendTls13CertificateRequest"); |
8674 | |
|
8675 | 0 | ssl->options.buildingMsg = 1; |
8676 | |
|
8677 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) |
8678 | 0 | return SIDE_ERROR; |
8679 | | |
8680 | | /* Use ssl->suites->hashSigAlgo so wolfSSL_set1_sigalgs_list() is honored. |
8681 | | * hashSigAlgoSz=0 makes GetSize/Write fall back to WOLFSSL_SUITES(ssl). */ |
8682 | 0 | sa = TLSX_SignatureAlgorithms_New(ssl, 0, ssl->heap); |
8683 | 0 | if (sa == NULL) |
8684 | 0 | return MEMORY_ERROR; |
8685 | 0 | ret = TLSX_Push(&ssl->extensions, TLSX_SIGNATURE_ALGORITHMS, sa, ssl->heap); |
8686 | 0 | if (ret != 0) { |
8687 | 0 | TLSX_SignatureAlgorithms_FreeAll(sa, ssl->heap); |
8688 | 0 | return ret; |
8689 | 0 | } |
8690 | | |
8691 | 0 | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
8692 | | #ifdef WOLFSSL_DTLS13 |
8693 | | if (ssl->options.dtls) |
8694 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
8695 | | #endif /* WOLFSSL_DTLS13 */ |
8696 | |
|
8697 | 0 | reqSz = (word16)(OPAQUE8_LEN + reqCtxLen); |
8698 | 0 | ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz); |
8699 | 0 | if (ret != 0) |
8700 | 0 | return ret; |
8701 | | |
8702 | 0 | sendSz = (int)(i + reqSz); |
8703 | | /* Always encrypted and make room for padding. */ |
8704 | 0 | sendSz += MAX_MSG_EXTRA; |
8705 | | |
8706 | | /* Check buffers are big enough and grow if needed. */ |
8707 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8708 | 0 | return ret; |
8709 | | |
8710 | | /* Get position in output buffer to write new message to. */ |
8711 | 0 | output = GetOutputBuffer(ssl); |
8712 | | |
8713 | | /* Put the record and handshake headers on. */ |
8714 | 0 | AddTls13Headers(output, reqSz, certificate_request, ssl); |
8715 | | |
8716 | | /* Certificate request context. */ |
8717 | 0 | output[i++] = (byte)reqCtxLen; |
8718 | 0 | if (reqCtxLen != 0) { |
8719 | 0 | XMEMCPY(output + i, reqCtx, reqCtxLen); |
8720 | 0 | i += reqCtxLen; |
8721 | 0 | } |
8722 | | |
8723 | | /* Certificate extensions. */ |
8724 | 0 | reqSz = 0; |
8725 | 0 | ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz); |
8726 | 0 | if (ret != 0) |
8727 | 0 | return ret; |
8728 | 0 | i += reqSz; |
8729 | |
|
8730 | | #ifdef WOLFSSL_DTLS13 |
8731 | | if (ssl->options.dtls) { |
8732 | | ssl->options.buildingMsg = 0; |
8733 | | ret = |
8734 | | Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
8735 | | certificate_request, 1); |
8736 | | |
8737 | | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8738 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8739 | | |
8740 | | return ret; |
8741 | | |
8742 | | } |
8743 | | #endif /* WOLFSSL_DTLS13 */ |
8744 | | |
8745 | | /* Always encrypted. */ |
8746 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
8747 | 0 | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
8748 | 0 | if (sendSz < 0) |
8749 | 0 | return sendSz; |
8750 | | |
8751 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8752 | | if (ssl->hsInfoOn) |
8753 | | AddPacketName(ssl, "CertificateRequest"); |
8754 | | if (ssl->toInfoOn) { |
8755 | | ret = AddPacketInfo(ssl, "CertificateRequest", handshake, output, |
8756 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8757 | | if (ret != 0) |
8758 | | return ret; |
8759 | | } |
8760 | | #endif |
8761 | | |
8762 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8763 | 0 | ssl->options.buildingMsg = 0; |
8764 | 0 | if (!ssl->options.groupMessages) |
8765 | 0 | ret = SendBuffered(ssl); |
8766 | |
|
8767 | 0 | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8768 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8769 | |
|
8770 | 0 | return ret; |
8771 | 0 | } |
8772 | | #endif /* NO_CERTS */ |
8773 | | #endif /* NO_WOLFSSL_SERVER */ |
8774 | | |
8775 | | #ifndef NO_CERTS |
8776 | | #if (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) && \ |
8777 | | (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8778 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
8779 | | defined(WOLFSSL_HAVE_SLHDSA)) |
8780 | | /* Encode the signature algorithm into buffer. |
8781 | | * |
8782 | | * hashalgo The hash algorithm. |
8783 | | * hsType The signature type. |
8784 | | * output The buffer to encode into. |
8785 | | */ |
8786 | | static WC_INLINE void EncodeSigAlg(const WOLFSSL * ssl, byte hashAlgo, |
8787 | | byte hsType, byte* output) |
8788 | | { |
8789 | | (void)ssl; |
8790 | | (void)hashAlgo; |
8791 | | switch (hsType) { |
8792 | | #ifdef HAVE_ECC |
8793 | | case ecc_dsa_sa_algo: |
8794 | | if (ssl->pkCurveOID == ECC_BRAINPOOLP256R1_OID) { |
8795 | | output[0] = NEW_SA_MAJOR; |
8796 | | output[1] = ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR; |
8797 | | } |
8798 | | else if (ssl->pkCurveOID == ECC_BRAINPOOLP384R1_OID) { |
8799 | | output[0] = NEW_SA_MAJOR; |
8800 | | output[1] = ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR; |
8801 | | } |
8802 | | else if (ssl->pkCurveOID == ECC_BRAINPOOLP512R1_OID) { |
8803 | | output[0] = NEW_SA_MAJOR; |
8804 | | output[1] = ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR; |
8805 | | } |
8806 | | else { |
8807 | | output[0] = hashAlgo; |
8808 | | output[1] = ecc_dsa_sa_algo; |
8809 | | } |
8810 | | break; |
8811 | | #endif |
8812 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8813 | | case sm2_sa_algo: |
8814 | | output[0] = SM2_SA_MAJOR; |
8815 | | output[1] = SM2_SA_MINOR; |
8816 | | break; |
8817 | | #endif |
8818 | | #ifdef HAVE_ED25519 |
8819 | | /* ED25519: 0x0807 */ |
8820 | | case ed25519_sa_algo: |
8821 | | output[0] = ED25519_SA_MAJOR; |
8822 | | output[1] = ED25519_SA_MINOR; |
8823 | | break; |
8824 | | #endif |
8825 | | #ifdef HAVE_ED448 |
8826 | | /* ED448: 0x0808 */ |
8827 | | case ed448_sa_algo: |
8828 | | output[0] = ED448_SA_MAJOR; |
8829 | | output[1] = ED448_SA_MINOR; |
8830 | | break; |
8831 | | #endif |
8832 | | #ifndef NO_RSA |
8833 | | /* PSS signatures: 0x080[4-6] or 0x080[9-B] */ |
8834 | | case rsa_pss_sa_algo: |
8835 | | output[0] = rsa_pss_sa_algo; |
8836 | | #ifdef WC_RSA_PSS |
8837 | | /* If the private key uses the RSA-PSS OID, and the peer supports |
8838 | | * the rsa_pss_pss_* signature algorithm in use, then report |
8839 | | * rsa_pss_pss_* rather than rsa_pss_rsae_*. */ |
8840 | | if (ssl->useRsaPss && |
8841 | | ((ssl->pssAlgo & (1U << hashAlgo)) != 0U) && |
8842 | | (sha256_mac <= hashAlgo) && (hashAlgo <= sha512_mac)) |
8843 | | { |
8844 | | output[1] = PSS_RSAE_TO_PSS_PSS(hashAlgo); |
8845 | | } |
8846 | | else |
8847 | | #endif |
8848 | | { |
8849 | | output[1] = hashAlgo; |
8850 | | } |
8851 | | break; |
8852 | | #endif |
8853 | | #ifdef HAVE_FALCON |
8854 | | case falcon_level1_sa_algo: |
8855 | | output[0] = FALCON_LEVEL1_SA_MAJOR; |
8856 | | output[1] = FALCON_LEVEL1_SA_MINOR; |
8857 | | break; |
8858 | | case falcon_level5_sa_algo: |
8859 | | output[0] = FALCON_LEVEL5_SA_MAJOR; |
8860 | | output[1] = FALCON_LEVEL5_SA_MINOR; |
8861 | | break; |
8862 | | #endif |
8863 | | #ifdef WOLFSSL_HAVE_MLDSA |
8864 | | case mldsa_44_sa_algo: |
8865 | | output[0] = MLDSA_44_SA_MAJOR; |
8866 | | output[1] = MLDSA_44_SA_MINOR; |
8867 | | break; |
8868 | | case mldsa_65_sa_algo: |
8869 | | output[0] = MLDSA_65_SA_MAJOR; |
8870 | | output[1] = MLDSA_65_SA_MINOR; |
8871 | | break; |
8872 | | case mldsa_87_sa_algo: |
8873 | | output[0] = MLDSA_87_SA_MAJOR; |
8874 | | output[1] = MLDSA_87_SA_MINOR; |
8875 | | break; |
8876 | | #endif |
8877 | | #ifdef WOLFSSL_HAVE_SLHDSA |
8878 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_128S) |
8879 | | case slhdsa_sha2_128s_sa_algo: |
8880 | | output[0] = SLHDSA_SA_MAJOR; |
8881 | | output[1] = SLHDSA_SHA2_128S_SA_MINOR; |
8882 | | break; |
8883 | | #endif |
8884 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_128F) |
8885 | | case slhdsa_sha2_128f_sa_algo: |
8886 | | output[0] = SLHDSA_SA_MAJOR; |
8887 | | output[1] = SLHDSA_SHA2_128F_SA_MINOR; |
8888 | | break; |
8889 | | #endif |
8890 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_192S) |
8891 | | case slhdsa_sha2_192s_sa_algo: |
8892 | | output[0] = SLHDSA_SA_MAJOR; |
8893 | | output[1] = SLHDSA_SHA2_192S_SA_MINOR; |
8894 | | break; |
8895 | | #endif |
8896 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_192F) |
8897 | | case slhdsa_sha2_192f_sa_algo: |
8898 | | output[0] = SLHDSA_SA_MAJOR; |
8899 | | output[1] = SLHDSA_SHA2_192F_SA_MINOR; |
8900 | | break; |
8901 | | #endif |
8902 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_256S) |
8903 | | case slhdsa_sha2_256s_sa_algo: |
8904 | | output[0] = SLHDSA_SA_MAJOR; |
8905 | | output[1] = SLHDSA_SHA2_256S_SA_MINOR; |
8906 | | break; |
8907 | | #endif |
8908 | | #if defined(WOLFSSL_SLHDSA_SHA2) && defined(WOLFSSL_SLHDSA_PARAM_SHA2_256F) |
8909 | | case slhdsa_sha2_256f_sa_algo: |
8910 | | output[0] = SLHDSA_SA_MAJOR; |
8911 | | output[1] = SLHDSA_SHA2_256F_SA_MINOR; |
8912 | | break; |
8913 | | #endif |
8914 | | #ifdef WOLFSSL_SLHDSA_PARAM_128S |
8915 | | case slhdsa_shake_128s_sa_algo: |
8916 | | output[0] = SLHDSA_SA_MAJOR; |
8917 | | output[1] = SLHDSA_SHAKE_128S_SA_MINOR; |
8918 | | break; |
8919 | | #endif |
8920 | | #ifdef WOLFSSL_SLHDSA_PARAM_128F |
8921 | | case slhdsa_shake_128f_sa_algo: |
8922 | | output[0] = SLHDSA_SA_MAJOR; |
8923 | | output[1] = SLHDSA_SHAKE_128F_SA_MINOR; |
8924 | | break; |
8925 | | #endif |
8926 | | #ifdef WOLFSSL_SLHDSA_PARAM_192S |
8927 | | case slhdsa_shake_192s_sa_algo: |
8928 | | output[0] = SLHDSA_SA_MAJOR; |
8929 | | output[1] = SLHDSA_SHAKE_192S_SA_MINOR; |
8930 | | break; |
8931 | | #endif |
8932 | | #ifdef WOLFSSL_SLHDSA_PARAM_192F |
8933 | | case slhdsa_shake_192f_sa_algo: |
8934 | | output[0] = SLHDSA_SA_MAJOR; |
8935 | | output[1] = SLHDSA_SHAKE_192F_SA_MINOR; |
8936 | | break; |
8937 | | #endif |
8938 | | #ifdef WOLFSSL_SLHDSA_PARAM_256S |
8939 | | case slhdsa_shake_256s_sa_algo: |
8940 | | output[0] = SLHDSA_SA_MAJOR; |
8941 | | output[1] = SLHDSA_SHAKE_256S_SA_MINOR; |
8942 | | break; |
8943 | | #endif |
8944 | | #ifdef WOLFSSL_SLHDSA_PARAM_256F |
8945 | | case slhdsa_shake_256f_sa_algo: |
8946 | | output[0] = SLHDSA_SA_MAJOR; |
8947 | | output[1] = SLHDSA_SHAKE_256F_SA_MINOR; |
8948 | | break; |
8949 | | #endif |
8950 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
8951 | | default: |
8952 | | break; |
8953 | | } |
8954 | | } |
8955 | | #endif |
8956 | | |
8957 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8958 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
8959 | | defined(WOLFSSL_HAVE_SLHDSA) |
8960 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8961 | | /* These match up with what the OQS team has defined. */ |
8962 | | #define HYBRID_SA_MAJOR 0xFE |
8963 | | #define HYBRID_P256_MLDSA_44_SA_MINOR 0xA1 |
8964 | | #define HYBRID_RSA3072_MLDSA_44_SA_MINOR 0xA2 |
8965 | | #define HYBRID_P384_MLDSA_65_SA_MINOR 0xA4 |
8966 | | #define HYBRID_P521_MLDSA_87_SA_MINOR 0xA6 |
8967 | | /* Falcon hybrid codepoints aligned with oqs-provider. */ |
8968 | | #define HYBRID_P256_FALCON_LEVEL1_SA_MINOR 0xD8 |
8969 | | #define HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR 0xD9 |
8970 | | #define HYBRID_P521_FALCON_LEVEL5_SA_MINOR 0xDB |
8971 | | |
8972 | | /* Custom defined ones for PQC first */ |
8973 | | #define HYBRID_MLDSA_44_P256_SA_MINOR 0xD1 |
8974 | | #define HYBRID_MLDSA_44_RSA3072_SA_MINOR 0xD2 |
8975 | | #define HYBRID_MLDSA_65_P384_SA_MINOR 0xD3 |
8976 | | #define HYBRID_MLDSA_87_P521_SA_MINOR 0xD4 |
8977 | | #define HYBRID_FALCON_LEVEL1_P256_SA_MINOR 0xD5 |
8978 | | #define HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR 0xD6 |
8979 | | #define HYBRID_FALCON_LEVEL5_P521_SA_MINOR 0xD7 |
8980 | | |
8981 | | |
8982 | | static void EncodeDualSigAlg(byte sigAlg, byte altSigAlg, byte* output) |
8983 | | { |
8984 | | /* Initialize output to error indicator. */ |
8985 | | output[0] = 0x0; |
8986 | | output[1] = 0x0; |
8987 | | |
8988 | | if (sigAlg == ecc_dsa_sa_algo && altSigAlg == mldsa_44_sa_algo) { |
8989 | | output[1] = HYBRID_P256_MLDSA_44_SA_MINOR; |
8990 | | } |
8991 | | else if (sigAlg == rsa_pss_sa_algo && |
8992 | | altSigAlg == mldsa_44_sa_algo) { |
8993 | | output[1] = HYBRID_RSA3072_MLDSA_44_SA_MINOR; |
8994 | | } |
8995 | | else if (sigAlg == ecc_dsa_sa_algo && |
8996 | | altSigAlg == mldsa_65_sa_algo) { |
8997 | | output[1] = HYBRID_P384_MLDSA_65_SA_MINOR; |
8998 | | } |
8999 | | else if (sigAlg == ecc_dsa_sa_algo && |
9000 | | altSigAlg == mldsa_87_sa_algo) { |
9001 | | output[1] = HYBRID_P521_MLDSA_87_SA_MINOR; |
9002 | | } |
9003 | | else if (sigAlg == ecc_dsa_sa_algo && |
9004 | | altSigAlg == falcon_level1_sa_algo) { |
9005 | | output[1] = HYBRID_P256_FALCON_LEVEL1_SA_MINOR; |
9006 | | } |
9007 | | else if (sigAlg == rsa_pss_sa_algo && |
9008 | | altSigAlg == falcon_level1_sa_algo) { |
9009 | | output[1] = HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR; |
9010 | | } |
9011 | | else if (sigAlg == ecc_dsa_sa_algo && |
9012 | | altSigAlg == falcon_level5_sa_algo) { |
9013 | | output[1] = HYBRID_P521_FALCON_LEVEL5_SA_MINOR; |
9014 | | } |
9015 | | else if (sigAlg == mldsa_44_sa_algo && |
9016 | | altSigAlg == ecc_dsa_sa_algo) { |
9017 | | output[1] = HYBRID_MLDSA_44_P256_SA_MINOR; |
9018 | | } |
9019 | | else if (sigAlg == mldsa_44_sa_algo && |
9020 | | altSigAlg == rsa_pss_sa_algo) { |
9021 | | output[1] = HYBRID_MLDSA_44_RSA3072_SA_MINOR; |
9022 | | } |
9023 | | else if (sigAlg == mldsa_65_sa_algo && |
9024 | | altSigAlg == ecc_dsa_sa_algo) { |
9025 | | output[1] = HYBRID_MLDSA_65_P384_SA_MINOR; |
9026 | | } |
9027 | | else if (sigAlg == mldsa_87_sa_algo && |
9028 | | altSigAlg == ecc_dsa_sa_algo) { |
9029 | | output[1] = HYBRID_MLDSA_87_P521_SA_MINOR; |
9030 | | } |
9031 | | else if (sigAlg == falcon_level1_sa_algo && |
9032 | | altSigAlg == ecc_dsa_sa_algo) { |
9033 | | output[1] = HYBRID_FALCON_LEVEL1_P256_SA_MINOR; |
9034 | | } |
9035 | | else if (sigAlg == falcon_level1_sa_algo && |
9036 | | altSigAlg == rsa_pss_sa_algo) { |
9037 | | output[1] = HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR; |
9038 | | } |
9039 | | else if (sigAlg == falcon_level5_sa_algo && |
9040 | | altSigAlg == ecc_dsa_sa_algo) { |
9041 | | output[1] = HYBRID_FALCON_LEVEL5_P521_SA_MINOR; |
9042 | | } |
9043 | | |
9044 | | if (output[1] != 0x0) { |
9045 | | output[0] = HYBRID_SA_MAJOR; |
9046 | | } |
9047 | | } |
9048 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9049 | | |
9050 | | static enum wc_MACAlgorithm GetNewSAHashAlgo(int typeIn) |
9051 | 0 | { |
9052 | 0 | switch (typeIn) { |
9053 | 0 | case RSA_PSS_RSAE_SHA256_MINOR: |
9054 | 0 | case RSA_PSS_PSS_SHA256_MINOR: |
9055 | 0 | case ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR: |
9056 | 0 | return sha256_mac; |
9057 | | |
9058 | 0 | case RSA_PSS_RSAE_SHA384_MINOR: |
9059 | 0 | case RSA_PSS_PSS_SHA384_MINOR: |
9060 | 0 | case ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR: |
9061 | 0 | return sha384_mac; |
9062 | | |
9063 | 0 | case RSA_PSS_RSAE_SHA512_MINOR: |
9064 | 0 | case RSA_PSS_PSS_SHA512_MINOR: |
9065 | 0 | case ED25519_SA_MINOR: |
9066 | 0 | case ED448_SA_MINOR: |
9067 | 0 | case ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR: |
9068 | 0 | return sha512_mac; |
9069 | 0 | default: |
9070 | 0 | return no_mac; |
9071 | 0 | } |
9072 | 0 | } |
9073 | | |
9074 | | /* Decode the signature algorithm. |
9075 | | * |
9076 | | * input The encoded signature algorithm. |
9077 | | * hashalgo The hash algorithm. |
9078 | | * hsType The signature type. |
9079 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
9080 | | */ |
9081 | | static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo, |
9082 | | byte* hsType) |
9083 | 0 | { |
9084 | 0 | int ret = 0; |
9085 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
9086 | | byte slhType; |
9087 | | #endif |
9088 | |
|
9089 | 0 | switch (input[0]) { |
9090 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
9091 | 0 | case SM2_SA_MAJOR: |
9092 | 0 | if (input[1] == SM2_SA_MINOR) { |
9093 | 0 | *hsType = sm2_sa_algo; |
9094 | 0 | *hashAlgo = sm3_mac; |
9095 | 0 | } |
9096 | 0 | else |
9097 | 0 | ret = INVALID_PARAMETER; |
9098 | 0 | break; |
9099 | 0 | #endif |
9100 | 0 | case NEW_SA_MAJOR: |
9101 | 0 | { |
9102 | 0 | enum wc_MACAlgorithm mac = GetNewSAHashAlgo(input[1]); |
9103 | 0 | *hashAlgo = (byte)mac; |
9104 | 0 | } |
9105 | | |
9106 | | /* PSS encryption: 0x080[4-6] */ |
9107 | 0 | if (input[1] >= RSA_PSS_RSAE_SHA256_MINOR && |
9108 | 0 | input[1] <= RSA_PSS_RSAE_SHA512_MINOR) { |
9109 | 0 | *hsType = input[0]; |
9110 | 0 | } |
9111 | | /* PSS signature: 0x080[9-B] */ |
9112 | 0 | else if (input[1] >= RSA_PSS_PSS_SHA256_MINOR && |
9113 | 0 | input[1] <= RSA_PSS_PSS_SHA512_MINOR) { |
9114 | 0 | *hsType = input[0]; |
9115 | 0 | } |
9116 | 0 | #ifdef HAVE_ED25519 |
9117 | | /* ED25519: 0x0807 */ |
9118 | 0 | else if (input[1] == ED25519_SA_MINOR) { |
9119 | 0 | *hsType = ed25519_sa_algo; |
9120 | | /* Hash performed as part of sign/verify operation. */ |
9121 | 0 | } |
9122 | 0 | #endif |
9123 | 0 | #ifdef HAVE_ED448 |
9124 | | /* ED448: 0x0808 */ |
9125 | 0 | else if (input[1] == ED448_SA_MINOR) { |
9126 | 0 | *hsType = ed448_sa_algo; |
9127 | | /* Hash performed as part of sign/verify operation. */ |
9128 | 0 | } |
9129 | 0 | #endif |
9130 | 0 | #ifdef HAVE_ECC_BRAINPOOL |
9131 | 0 | else if ((input[1] == ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR) || |
9132 | 0 | (input[1] == ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR) || |
9133 | 0 | (input[1] == ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR)) { |
9134 | 0 | *hsType = ecc_dsa_sa_algo; |
9135 | 0 | } |
9136 | 0 | #endif |
9137 | 0 | else |
9138 | 0 | ret = INVALID_PARAMETER; |
9139 | 0 | break; |
9140 | | #if defined(HAVE_FALCON) |
9141 | | case FALCON_SA_MAJOR: |
9142 | | if (input[1] == FALCON_LEVEL1_SA_MINOR) { |
9143 | | *hsType = falcon_level1_sa_algo; |
9144 | | /* Hash performed as part of sign/verify operation. */ |
9145 | | *hashAlgo = sha512_mac; |
9146 | | } else if (input[1] == FALCON_LEVEL5_SA_MINOR) { |
9147 | | *hsType = falcon_level5_sa_algo; |
9148 | | /* Hash performed as part of sign/verify operation. */ |
9149 | | *hashAlgo = sha512_mac; |
9150 | | } |
9151 | | else |
9152 | | ret = INVALID_PARAMETER; |
9153 | | break; |
9154 | | #endif /* HAVE_FALCON */ |
9155 | | #if defined(WOLFSSL_HAVE_MLDSA) || defined(WOLFSSL_HAVE_SLHDSA) |
9156 | | /* ML-DSA and SLH-DSA share the same major byte (0x09); their minor |
9157 | | * bytes are disjoint (ML-DSA 0x04-0x06, SLH-DSA 0x11-0x1C). */ |
9158 | | case MLDSA_SA_MAJOR: |
9159 | | ret = INVALID_PARAMETER; |
9160 | | #if defined(WOLFSSL_HAVE_MLDSA) |
9161 | | if (input[1] == MLDSA_44_SA_MINOR) { |
9162 | | *hsType = mldsa_44_sa_algo; |
9163 | | /* Hash performed as part of sign/verify operation. */ |
9164 | | *hashAlgo = sha512_mac; |
9165 | | ret = 0; |
9166 | | } else if (input[1] == MLDSA_65_SA_MINOR) { |
9167 | | *hsType = mldsa_65_sa_algo; |
9168 | | *hashAlgo = sha512_mac; |
9169 | | ret = 0; |
9170 | | } else if (input[1] == MLDSA_87_SA_MINOR) { |
9171 | | *hsType = mldsa_87_sa_algo; |
9172 | | *hashAlgo = sha512_mac; |
9173 | | ret = 0; |
9174 | | } |
9175 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
9176 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
9177 | | if (ret != 0) { |
9178 | | slhType = SlhDsaSigMinorToType(input[1]); |
9179 | | if (slhType != (byte)invalid_sa_algo) { |
9180 | | *hsType = slhType; |
9181 | | /* Hash performed as part of sign/verify operation. */ |
9182 | | *hashAlgo = sha512_mac; |
9183 | | ret = 0; |
9184 | | } |
9185 | | } |
9186 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
9187 | | break; |
9188 | | #endif /* WOLFSSL_HAVE_MLDSA || WOLFSSL_HAVE_SLHDSA */ |
9189 | 0 | default: |
9190 | 0 | *hashAlgo = input[0]; |
9191 | 0 | *hsType = input[1]; |
9192 | 0 | break; |
9193 | 0 | } |
9194 | | |
9195 | 0 | return ret; |
9196 | 0 | } |
9197 | | |
9198 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9199 | | /* Decode the hybrid signature algorithm. |
9200 | | * |
9201 | | * input The encoded signature algorithm. |
9202 | | * hashalgo The hash algorithm. |
9203 | | * hsType The signature type. |
9204 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
9205 | | */ |
9206 | | static WC_INLINE int DecodeTls13HybridSigAlg(byte* input, byte* hashAlg, |
9207 | | byte *sigAlg, byte *altSigAlg) |
9208 | | { |
9209 | | |
9210 | | if (input[0] != HYBRID_SA_MAJOR) { |
9211 | | return INVALID_PARAMETER; |
9212 | | } |
9213 | | |
9214 | | if (input[1] == HYBRID_P256_MLDSA_44_SA_MINOR) { |
9215 | | *sigAlg = ecc_dsa_sa_algo; |
9216 | | *hashAlg = sha256_mac; |
9217 | | *altSigAlg = mldsa_44_sa_algo; |
9218 | | } |
9219 | | else if (input[1] == HYBRID_RSA3072_MLDSA_44_SA_MINOR) { |
9220 | | *sigAlg = rsa_pss_sa_algo; |
9221 | | *hashAlg = sha256_mac; |
9222 | | *altSigAlg = mldsa_44_sa_algo; |
9223 | | } |
9224 | | else if (input[1] == HYBRID_P384_MLDSA_65_SA_MINOR) { |
9225 | | *sigAlg = ecc_dsa_sa_algo; |
9226 | | *hashAlg = sha384_mac; |
9227 | | *altSigAlg = mldsa_65_sa_algo; |
9228 | | } |
9229 | | else if (input[1] == HYBRID_P521_MLDSA_87_SA_MINOR) { |
9230 | | *sigAlg = ecc_dsa_sa_algo; |
9231 | | *hashAlg = sha512_mac; |
9232 | | *altSigAlg = mldsa_87_sa_algo; |
9233 | | } |
9234 | | else if (input[1] == HYBRID_P256_FALCON_LEVEL1_SA_MINOR) { |
9235 | | *sigAlg = ecc_dsa_sa_algo; |
9236 | | *hashAlg = sha256_mac; |
9237 | | *altSigAlg = falcon_level1_sa_algo; |
9238 | | } |
9239 | | else if (input[1] == HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR) { |
9240 | | *sigAlg = rsa_pss_sa_algo; |
9241 | | *hashAlg = sha256_mac; |
9242 | | *altSigAlg = falcon_level1_sa_algo; |
9243 | | } |
9244 | | else if (input[1] == HYBRID_P521_FALCON_LEVEL5_SA_MINOR) { |
9245 | | *sigAlg = ecc_dsa_sa_algo; |
9246 | | *hashAlg = sha512_mac; |
9247 | | *altSigAlg = falcon_level5_sa_algo; |
9248 | | } |
9249 | | else if (input[1] == HYBRID_MLDSA_44_P256_SA_MINOR) { |
9250 | | *sigAlg = mldsa_44_sa_algo; |
9251 | | *hashAlg = sha256_mac; |
9252 | | *altSigAlg = ecc_dsa_sa_algo; |
9253 | | } |
9254 | | else if (input[1] == HYBRID_MLDSA_44_RSA3072_SA_MINOR) { |
9255 | | *sigAlg = mldsa_44_sa_algo; |
9256 | | *hashAlg = sha256_mac; |
9257 | | *altSigAlg = rsa_pss_sa_algo; |
9258 | | } |
9259 | | else if (input[1] == HYBRID_MLDSA_65_P384_SA_MINOR) { |
9260 | | *sigAlg = mldsa_65_sa_algo; |
9261 | | *hashAlg = sha384_mac; |
9262 | | *altSigAlg = ecc_dsa_sa_algo; |
9263 | | } |
9264 | | else if (input[1] == HYBRID_MLDSA_87_P521_SA_MINOR) { |
9265 | | *sigAlg = mldsa_87_sa_algo; |
9266 | | *hashAlg = sha512_mac; |
9267 | | *altSigAlg = ecc_dsa_sa_algo; |
9268 | | } |
9269 | | else if (input[1] == HYBRID_FALCON_LEVEL1_P256_SA_MINOR) { |
9270 | | *sigAlg = falcon_level1_sa_algo; |
9271 | | *hashAlg = sha256_mac; |
9272 | | *altSigAlg = ecc_dsa_sa_algo; |
9273 | | } |
9274 | | else if (input[1] == HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR) { |
9275 | | *sigAlg = falcon_level1_sa_algo; |
9276 | | *hashAlg = sha256_mac; |
9277 | | *altSigAlg = rsa_pss_sa_algo; |
9278 | | } |
9279 | | else if (input[1] == HYBRID_FALCON_LEVEL5_P521_SA_MINOR) { |
9280 | | *sigAlg = falcon_level5_sa_algo; |
9281 | | *hashAlg = sha512_mac; |
9282 | | *altSigAlg = ecc_dsa_sa_algo; |
9283 | | } |
9284 | | else { |
9285 | | return INVALID_PARAMETER; |
9286 | | } |
9287 | | |
9288 | | return 0; |
9289 | | } |
9290 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9291 | | |
9292 | | /* Get the hash of the messages so far. |
9293 | | * |
9294 | | * ssl The SSL/TLS object. |
9295 | | * hash The buffer to write the hash to. |
9296 | | * returns the length of the hash. |
9297 | | */ |
9298 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash) |
9299 | 0 | { |
9300 | 0 | int ret = 0; |
9301 | 0 | switch (ssl->specs.mac_algorithm) { |
9302 | 0 | #ifndef NO_SHA256 |
9303 | 0 | case sha256_mac: |
9304 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
9305 | 0 | if (ret == 0) |
9306 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
9307 | 0 | break; |
9308 | 0 | #endif /* !NO_SHA256 */ |
9309 | 0 | #ifdef WOLFSSL_SHA384 |
9310 | 0 | case sha384_mac: |
9311 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
9312 | 0 | if (ret == 0) |
9313 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
9314 | 0 | break; |
9315 | 0 | #endif /* WOLFSSL_SHA384 */ |
9316 | | #ifdef WOLFSSL_TLS13_SHA512 |
9317 | | case sha512_mac: |
9318 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
9319 | | if (ret == 0) |
9320 | | ret = WC_SHA512_DIGEST_SIZE; |
9321 | | break; |
9322 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
9323 | 0 | #ifdef WOLFSSL_SM3 |
9324 | 0 | case sm3_mac: |
9325 | 0 | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
9326 | 0 | if (ret == 0) |
9327 | 0 | ret = WC_SM3_DIGEST_SIZE; |
9328 | 0 | break; |
9329 | 0 | #endif /* WOLFSSL_SM3 */ |
9330 | 0 | default: |
9331 | 0 | break; |
9332 | 0 | } |
9333 | 0 | return ret; |
9334 | 0 | } |
9335 | | |
9336 | | /* The server certificate verification label. */ |
9337 | | static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] = |
9338 | | "TLS 1.3, server CertificateVerify"; |
9339 | | /* The client certificate verification label. */ |
9340 | | static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] = |
9341 | | "TLS 1.3, client CertificateVerify"; |
9342 | | /* The prefix byte in the signature data. */ |
9343 | | #define SIGNING_DATA_PREFIX_BYTE 0x20 |
9344 | | |
9345 | | /* Create the signature data for TLS v1.3 certificate verification. |
9346 | | * |
9347 | | * ssl The SSL/TLS object. |
9348 | | * sigData The signature data. |
9349 | | * sigDataSz The length of the signature data. |
9350 | | * check Indicates this is a check not create. |
9351 | | */ |
9352 | | int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz, |
9353 | | int check) |
9354 | 368 | { |
9355 | 368 | word16 idx; |
9356 | 368 | int side = ssl->options.side; |
9357 | 368 | int ret; |
9358 | | |
9359 | | /* Signature Data = Prefix | Label | Handshake Hash */ |
9360 | 368 | XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ); |
9361 | 368 | idx = SIGNING_DATA_PREFIX_SZ; |
9362 | | |
9363 | 368 | if ((side == WOLFSSL_SERVER_END && check) || |
9364 | 368 | (side == WOLFSSL_CLIENT_END && !check)) { |
9365 | 0 | XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ); |
9366 | 0 | } |
9367 | 368 | if ((side == WOLFSSL_CLIENT_END && check) || |
9368 | 368 | (side == WOLFSSL_SERVER_END && !check)) { |
9369 | 368 | XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ); |
9370 | 368 | } |
9371 | 368 | idx += CERT_VFY_LABEL_SZ; |
9372 | | |
9373 | 368 | ret = GetMsgHash(ssl, &sigData[idx]); |
9374 | 368 | if (ret < 0) |
9375 | 4 | return ret; |
9376 | 364 | if (ret == 0) |
9377 | 0 | return HASH_TYPE_E; |
9378 | | |
9379 | 364 | *sigDataSz = (word16)(idx + ret); |
9380 | 364 | ret = 0; |
9381 | | |
9382 | 364 | return ret; |
9383 | 364 | } |
9384 | | |
9385 | | #ifndef NO_RSA |
9386 | | /* Encode the PKCS #1.5 RSA signature. |
9387 | | * |
9388 | | * sig The buffer to place the encoded signature into. |
9389 | | * sigData The data to be signed. |
9390 | | * sigDataSz The size of the data to be signed. |
9391 | | * hashAlgo The hash algorithm to use when signing. |
9392 | | * returns the length of the encoded signature or negative on error. |
9393 | | */ |
9394 | | int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz, |
9395 | | int sigAlgo, int hashAlgo) |
9396 | 362 | { |
9397 | 362 | Digest digest; |
9398 | 362 | int hashSz = 0; |
9399 | 362 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
9400 | 362 | byte* hash; |
9401 | | |
9402 | 362 | (void)sigAlgo; |
9403 | | |
9404 | 362 | hash = sig; |
9405 | | |
9406 | | /* Digest the signature data. */ |
9407 | 362 | switch (hashAlgo) { |
9408 | 0 | #ifndef NO_SHA256 |
9409 | 200 | case sha256_mac: |
9410 | 200 | ret = wc_InitSha256(&digest.sha256); |
9411 | 200 | if (ret == 0) { |
9412 | 200 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
9413 | 200 | if (ret == 0) |
9414 | 199 | ret = wc_Sha256Final(&digest.sha256, hash); |
9415 | 200 | wc_Sha256Free(&digest.sha256); |
9416 | 200 | } |
9417 | 200 | hashSz = WC_SHA256_DIGEST_SIZE; |
9418 | 200 | break; |
9419 | 0 | #endif |
9420 | 0 | #ifdef WOLFSSL_SHA384 |
9421 | 69 | case sha384_mac: |
9422 | 69 | ret = wc_InitSha384(&digest.sha384); |
9423 | 69 | if (ret == 0) { |
9424 | 69 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
9425 | 69 | if (ret == 0) |
9426 | 68 | ret = wc_Sha384Final(&digest.sha384, hash); |
9427 | 69 | wc_Sha384Free(&digest.sha384); |
9428 | 69 | } |
9429 | 69 | hashSz = WC_SHA384_DIGEST_SIZE; |
9430 | 69 | break; |
9431 | 0 | #endif |
9432 | 0 | #ifdef WOLFSSL_SHA512 |
9433 | 93 | case sha512_mac: |
9434 | 93 | ret = wc_InitSha512(&digest.sha512); |
9435 | 93 | if (ret == 0) { |
9436 | 93 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
9437 | 93 | if (ret == 0) |
9438 | 92 | ret = wc_Sha512Final(&digest.sha512, hash); |
9439 | 93 | wc_Sha512Free(&digest.sha512); |
9440 | 93 | } |
9441 | 93 | hashSz = WC_SHA512_DIGEST_SIZE; |
9442 | 93 | break; |
9443 | 0 | #endif |
9444 | 0 | default: |
9445 | 0 | ret = BAD_FUNC_ARG; |
9446 | 0 | break; |
9447 | | |
9448 | 362 | } |
9449 | | |
9450 | 362 | if (ret != 0) |
9451 | 6 | return ret; |
9452 | | |
9453 | 356 | return hashSz; |
9454 | 362 | } |
9455 | | #endif /* !NO_RSA */ |
9456 | | |
9457 | | #ifdef HAVE_ECC |
9458 | | /* Encode the ECC signature. |
9459 | | * |
9460 | | * sigData The data to be signed. |
9461 | | * sigDataSz The size of the data to be signed. |
9462 | | * hashAlgo The hash algorithm to use when signing. |
9463 | | * returns the length of the encoded signature or negative on error. |
9464 | | */ |
9465 | | static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo) |
9466 | 0 | { |
9467 | 0 | Digest digest; |
9468 | 0 | int hashSz = 0; |
9469 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
9470 | | |
9471 | | /* Digest the signature data. */ |
9472 | 0 | switch (hashAlgo) { |
9473 | 0 | #ifndef NO_SHA256 |
9474 | 0 | case sha256_mac: |
9475 | 0 | ret = wc_InitSha256(&digest.sha256); |
9476 | 0 | if (ret == 0) { |
9477 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
9478 | 0 | if (ret == 0) |
9479 | 0 | ret = wc_Sha256Final(&digest.sha256, sigData); |
9480 | 0 | wc_Sha256Free(&digest.sha256); |
9481 | 0 | } |
9482 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
9483 | 0 | break; |
9484 | 0 | #endif |
9485 | 0 | #ifdef WOLFSSL_SHA384 |
9486 | 0 | case sha384_mac: |
9487 | 0 | ret = wc_InitSha384(&digest.sha384); |
9488 | 0 | if (ret == 0) { |
9489 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
9490 | 0 | if (ret == 0) |
9491 | 0 | ret = wc_Sha384Final(&digest.sha384, sigData); |
9492 | 0 | wc_Sha384Free(&digest.sha384); |
9493 | 0 | } |
9494 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
9495 | 0 | break; |
9496 | 0 | #endif |
9497 | 0 | #ifdef WOLFSSL_SHA512 |
9498 | 0 | case sha512_mac: |
9499 | 0 | ret = wc_InitSha512(&digest.sha512); |
9500 | 0 | if (ret == 0) { |
9501 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
9502 | 0 | if (ret == 0) |
9503 | 0 | ret = wc_Sha512Final(&digest.sha512, sigData); |
9504 | 0 | wc_Sha512Free(&digest.sha512); |
9505 | 0 | } |
9506 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
9507 | 0 | break; |
9508 | 0 | #endif |
9509 | 0 | default: |
9510 | 0 | ret = BAD_FUNC_ARG; |
9511 | 0 | break; |
9512 | 0 | } |
9513 | | |
9514 | 0 | if (ret != 0) |
9515 | 0 | return ret; |
9516 | | |
9517 | 0 | return hashSz; |
9518 | 0 | } |
9519 | | #endif /* HAVE_ECC */ |
9520 | | |
9521 | | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
9522 | | /* Check that the decrypted signature matches the encoded signature |
9523 | | * based on the digest of the signature data. |
9524 | | * |
9525 | | * ssl The SSL/TLS object. |
9526 | | * sigAlgo The signature algorithm used to generate signature. |
9527 | | * hashAlgo The hash algorithm used to generate signature. |
9528 | | * decSig The decrypted signature. |
9529 | | * decSigSz The size of the decrypted signature. |
9530 | | * returns 0 on success, otherwise failure. |
9531 | | */ |
9532 | | static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo, |
9533 | | byte* decSig, word32 decSigSz) |
9534 | 0 | { |
9535 | 0 | int ret = 0; |
9536 | 0 | byte sigData[MAX_SIG_DATA_SZ]; |
9537 | 0 | word16 sigDataSz; |
9538 | |
|
9539 | 0 | ret = CreateSigData(ssl, sigData, &sigDataSz, 1); |
9540 | 0 | if (ret != 0) |
9541 | 0 | return ret; |
9542 | | |
9543 | 0 | if (sigAlgo == rsa_pss_sa_algo) { |
9544 | 0 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
9545 | 0 | word32 sigSz; |
9546 | |
|
9547 | 0 | ret = ConvertHashPss(hashAlgo, &hashType, NULL); |
9548 | 0 | if (ret < 0) |
9549 | 0 | return ret; |
9550 | | |
9551 | | /* PSS signature can be done in-place */ |
9552 | 0 | ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz, |
9553 | 0 | sigAlgo, hashAlgo); |
9554 | 0 | if (ret < 0) |
9555 | 0 | return ret; |
9556 | 0 | sigSz = (word32)ret; |
9557 | |
|
9558 | 0 | ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz, |
9559 | 0 | hashType); |
9560 | 0 | } |
9561 | | |
9562 | 0 | return ret; |
9563 | 0 | } |
9564 | | #endif /* !NO_RSA && WC_RSA_PSS */ |
9565 | | #endif /* !NO_RSA || HAVE_ECC */ |
9566 | | |
9567 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
9568 | | /* Get the next certificate from the list for writing into the TLS v1.3 |
9569 | | * Certificate message. |
9570 | | * |
9571 | | * data The certificate list. |
9572 | | * length The length of the certificate data in the list. |
9573 | | * idx The index of the next certificate. |
9574 | | * returns the length of the certificate data. 0 indicates no more certificates |
9575 | | * in the list. |
9576 | | */ |
9577 | | static word32 NextCert(byte* data, word32 length, word32* idx) |
9578 | 0 | { |
9579 | 0 | word32 len; |
9580 | | |
9581 | | /* Would index read past end of list? */ |
9582 | 0 | if (*idx + 3 > length) |
9583 | 0 | return 0; |
9584 | | |
9585 | | /* Length of the current ASN.1 encoded certificate. */ |
9586 | 0 | c24to32(data + *idx, &len); |
9587 | | /* Include the length field. */ |
9588 | 0 | len += 3; |
9589 | | |
9590 | | /* Ensure len does not overrun certificate list */ |
9591 | 0 | if (*idx + len > length) |
9592 | 0 | return 0; |
9593 | | |
9594 | | /* Move index to next certificate and return the current certificate's |
9595 | | * length. |
9596 | | */ |
9597 | 0 | *idx += len; |
9598 | 0 | return len; |
9599 | 0 | } |
9600 | | |
9601 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9602 | | /* Write certificate status request into certificate to buffer. |
9603 | | * |
9604 | | * ssl SSL/TLS object. |
9605 | | * certExts DerBuffer array. buffers written |
9606 | | * extSz word32 array. |
9607 | | * Length of the certificate status request data for the certificate. |
9608 | | * extSz_num number of the CSR written |
9609 | | * extIdx The index number of certificate status request data |
9610 | | * for the certificate. |
9611 | | * offset index offset |
9612 | | * returns Total number of bytes written on success or negative value on error. |
9613 | | */ |
9614 | | static int WriteCSRToBuffer(WOLFSSL* ssl, DerBuffer** certExts, |
9615 | | word16* extSz, word16 extSz_num) |
9616 | | { |
9617 | | int ret = 0; |
9618 | | TLSX* ext; |
9619 | | CertificateStatusRequest* csr; |
9620 | | word32 ex_offset = HELLO_EXT_TYPE_SZ + OPAQUE16_LEN /* extension type */ |
9621 | | + OPAQUE16_LEN /* extension length */; |
9622 | | word32 totalSz = 0; |
9623 | | word32 tmpSz; |
9624 | | word32 extIdx; |
9625 | | DerBuffer* der; |
9626 | | |
9627 | | if (extSz_num > MAX_CERT_EXTENSIONS) |
9628 | | return MAX_CERT_EXTENSIONS_ERR; |
9629 | | |
9630 | | ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
9631 | | csr = ext ? (CertificateStatusRequest*)ext->data : NULL; |
9632 | | |
9633 | | if (csr) { |
9634 | | for (extIdx = 0; extIdx < (word16)(extSz_num); extIdx++) { |
9635 | | tmpSz = TLSX_CSR_GetSize_ex(csr, 0, (int)extIdx); |
9636 | | |
9637 | | if (ssl->fragOffset != 0 && certExts[extIdx] != NULL) { |
9638 | | /* A fragmented send is being resumed and this buffer was |
9639 | | * written by the earlier call. extSz starts over on every |
9640 | | * call, so recover this entry's size from the length written |
9641 | | * into the buffer. */ |
9642 | | ato16(certExts[extIdx]->buffer, &extSz[extIdx]); |
9643 | | extSz[extIdx] += OPAQUE16_LEN; |
9644 | | } |
9645 | | else { |
9646 | | /* Not a resume, so anything still allocated here is left over |
9647 | | * from a completed message and must not be reused. */ |
9648 | | FreeDer(&certExts[extIdx]); |
9649 | | |
9650 | | if (tmpSz > (OPAQUE8_LEN + OPAQUE24_LEN)) { |
9651 | | /* csr extension is not zero */ |
9652 | | if (tmpSz > WOLFSSL_MAX_16BIT) |
9653 | | return BUFFER_E; |
9654 | | extSz[extIdx] = (word16)tmpSz; |
9655 | | |
9656 | | ret = AllocDer(&certExts[extIdx], extSz[extIdx] + ex_offset, |
9657 | | CERT_TYPE, ssl->heap); |
9658 | | if (ret < 0) |
9659 | | return ret; |
9660 | | der = certExts[extIdx]; |
9661 | | |
9662 | | /* write extension type */ |
9663 | | c16toa(ext->type, der->buffer |
9664 | | + OPAQUE16_LEN); |
9665 | | /* writes extension data length. */ |
9666 | | c16toa(extSz[extIdx], der->buffer |
9667 | | + HELLO_EXT_TYPE_SZ + OPAQUE16_LEN); |
9668 | | /* write extension data */ |
9669 | | extSz[extIdx] = (word16)TLSX_CSR_Write_ex(csr, |
9670 | | der->buffer + ex_offset, 0, extIdx); |
9671 | | /* add extension offset */ |
9672 | | extSz[extIdx] += (word16)ex_offset; |
9673 | | /* extension length */ |
9674 | | c16toa(extSz[extIdx] - OPAQUE16_LEN, |
9675 | | der->buffer); |
9676 | | } |
9677 | | } |
9678 | | totalSz += extSz[extIdx]; |
9679 | | } |
9680 | | } |
9681 | | else { |
9682 | | /* chain cert empty extension size */ |
9683 | | totalSz += OPAQUE16_LEN * extSz_num; |
9684 | | } |
9685 | | return (int)totalSz; |
9686 | | } |
9687 | | #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */ |
9688 | | /* Add certificate data and empty extension to output up to the fragment size. |
9689 | | * |
9690 | | * ssl SSL/TLS object. |
9691 | | * cert The certificate data to write out. |
9692 | | * len The length of the certificate data. |
9693 | | * extSz Length of the extension data with the certificate. |
9694 | | * idx The start of the certificate data to write out. |
9695 | | * fragSz The maximum size of this fragment. |
9696 | | * output The buffer to write to. |
9697 | | * extIdx The index number of the extension data with the certificate |
9698 | | * returns the number of bytes written. |
9699 | | */ |
9700 | | static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz, |
9701 | | word32 idx, word32 fragSz, byte* output, word16 extIdx) |
9702 | 399 | { |
9703 | 399 | word32 i = 0; |
9704 | 399 | word32 copySz = min(len - idx, fragSz); |
9705 | | |
9706 | 399 | if (idx < len) { |
9707 | 399 | XMEMCPY(output, cert + idx, copySz); |
9708 | 399 | i = copySz; |
9709 | 399 | if (copySz == fragSz) |
9710 | 0 | return i; |
9711 | 399 | } |
9712 | 399 | copySz = len + extSz - idx - i; |
9713 | | |
9714 | 399 | if (extSz == OPAQUE16_LEN) { |
9715 | 399 | if (copySz <= fragSz) { |
9716 | | /* Empty extension */ |
9717 | 399 | output[i++] = 0; |
9718 | 399 | output[i++] = 0; |
9719 | 399 | } |
9720 | 399 | } |
9721 | 0 | else { |
9722 | 0 | byte* certExts = ssl->buffers.certExts[extIdx]->buffer + idx + i - len; |
9723 | | /* Put out as much of the extensions' data as will fit in fragment. */ |
9724 | 0 | if (copySz > fragSz - i) |
9725 | 0 | copySz = fragSz - i; |
9726 | 0 | XMEMCPY(output + i, certExts, copySz); |
9727 | 0 | i += copySz; |
9728 | 0 | } |
9729 | | |
9730 | 399 | return i; |
9731 | 399 | } |
9732 | | |
9733 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9734 | | static int SetupOcspResp(WOLFSSL* ssl) |
9735 | | { |
9736 | | DecodedCert* cert = NULL; |
9737 | | CertificateStatusRequest* csr = NULL; |
9738 | | TLSX* extension = NULL; |
9739 | | int ret = 0; |
9740 | | OcspRequest* request = NULL; |
9741 | | byte ctxOwnsRequest = 0; |
9742 | | |
9743 | | extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
9744 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9745 | | /* During post-handshake client authentication the client must staple |
9746 | | * its own OCSP response, but the status_request extension it offered in |
9747 | | * the initial ClientHello is no longer present on ssl->extensions. |
9748 | | * Recreate it here (the request extension still lives on |
9749 | | * ctx->extensions). Pass ssl so csr->ssl is set, which the response |
9750 | | * size/write path requires. */ |
9751 | | if (extension == NULL && |
9752 | | ssl->options.side == WOLFSSL_CLIENT_END && |
9753 | | ssl->options.handShakeDone && |
9754 | | TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST) != NULL) { |
9755 | | ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, |
9756 | | WOLFSSL_CSR_OCSP, 0, ssl, ssl->heap, ssl->devId); |
9757 | | if (ret != WOLFSSL_SUCCESS) |
9758 | | return ret; |
9759 | | extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
9760 | | } |
9761 | | #endif |
9762 | | if (extension == NULL) |
9763 | | return 0; /* peer didn't signal ocsp support */ |
9764 | | csr = (CertificateStatusRequest*)extension->data; |
9765 | | if (csr == NULL) |
9766 | | return MEMORY_ERROR; |
9767 | | |
9768 | | if (SSL_CM(ssl) != NULL && |
9769 | | SSL_CM(ssl)->ocsp_stapling != NULL && |
9770 | | SSL_CM(ssl)->ocsp_stapling->statusCb != NULL) { |
9771 | | return TLSX_CSR_SetResponseWithStatusCB(ssl); |
9772 | | } |
9773 | | |
9774 | | if (ssl->buffers.certificate == NULL) { |
9775 | | WOLFSSL_MSG("Certificate buffer not set!"); |
9776 | | return BUFFER_ERROR; |
9777 | | } |
9778 | | cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap, |
9779 | | DYNAMIC_TYPE_DCERT); |
9780 | | if (cert == NULL) { |
9781 | | return MEMORY_E; |
9782 | | } |
9783 | | InitDecodedCert(cert, ssl->buffers.certificate->buffer, |
9784 | | ssl->buffers.certificate->length, ssl->heap); |
9785 | | ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, SSL_CM(ssl)); |
9786 | | if (ret != 0) { |
9787 | | FreeDecodedCert(cert); |
9788 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
9789 | | return ret; |
9790 | | } |
9791 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9792 | | /* On client PHA the same certificate is re-stapled every round; reuse |
9793 | | * request slot 0 instead of appending, else csr->requests grows until |
9794 | | * MAX_CERT_EXTENSIONS_ERR. */ |
9795 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->options.handShakeDone) { |
9796 | | ret = TLSX_CSR_InitRequest_ex(ssl->extensions, cert, ssl->heap, 0); |
9797 | | } |
9798 | | else |
9799 | | #endif |
9800 | | { |
9801 | | ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap); |
9802 | | } |
9803 | | FreeDecodedCert(cert); |
9804 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
9805 | | if (ret != 0 ) |
9806 | | return ret; |
9807 | | |
9808 | | /* Free previous OCSP response buffers to avoid leak on PHA reuse */ |
9809 | | { |
9810 | | int j; |
9811 | | for (j = 0; j < MAX_CERT_EXTENSIONS; j++) { |
9812 | | XFREE(csr->responses[j].buffer, ssl->heap, |
9813 | | DYNAMIC_TYPE_TMP_BUFFER); |
9814 | | csr->responses[j].buffer = NULL; |
9815 | | csr->responses[j].length = 0; |
9816 | | } |
9817 | | } |
9818 | | request = &csr->request.ocsp[0]; |
9819 | | ret = CreateOcspResponse(ssl, &request, &csr->responses[0], |
9820 | | &ctxOwnsRequest); |
9821 | | /* Only a successful call replaces "request", and only a request the CTX did |
9822 | | * not take ownership of is ours to free. Both are checked, matching the |
9823 | | * SendCertificateStatus() callers. */ |
9824 | | if (ret == 0 && request != &csr->request.ocsp[0] && !ctxOwnsRequest) { |
9825 | | /* request was allocated in CreateOcspResponse() */ |
9826 | | FreeOcspRequest(request); |
9827 | | XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
9828 | | } |
9829 | | if (ret != 0) |
9830 | | return ret; |
9831 | | |
9832 | | if (csr->responses[0].buffer) |
9833 | | extension->resp = 1; |
9834 | | #if defined(WOLFSSL_TLS_OCSP_MULTI) |
9835 | | /* process OCSP request in certificate chain */ |
9836 | | if ((ret = ProcessChainOCSPRequest(ssl)) != 0) { |
9837 | | WOLFSSL_MSG("Process Cert Chain OCSP request failed"); |
9838 | | WOLFSSL_ERROR_VERBOSE(ret); |
9839 | | return ret; |
9840 | | } |
9841 | | #endif |
9842 | | return ret; |
9843 | | } |
9844 | | #endif |
9845 | | |
9846 | | /* handle generation TLS v1.3 certificate (11) */ |
9847 | | /* Send the certificate for this end and any CAs that help with validation. |
9848 | | * This message is always encrypted in TLS v1.3. |
9849 | | * |
9850 | | * ssl The SSL/TLS object. |
9851 | | * returns 0 on success, otherwise failure. |
9852 | | */ |
9853 | | static int SendTls13Certificate(WOLFSSL* ssl) |
9854 | 0 | { |
9855 | 0 | int ret = 0; |
9856 | 0 | word32 certSz, certChainSz, headerSz, listSz, payloadSz; |
9857 | 0 | word16 extSz[MAX_CERT_EXTENSIONS]; |
9858 | 0 | word16 extIdx = 0; |
9859 | 0 | word32 maxFragment; |
9860 | 0 | word32 totalextSz = 0; |
9861 | 0 | word32 len = 0; |
9862 | 0 | word32 idx = 0; |
9863 | 0 | word32 offset = 0; |
9864 | 0 | word32 entrySz = 0; |
9865 | 0 | byte* p = NULL; |
9866 | 0 | byte certReqCtxLen = 0; |
9867 | 0 | sword32 length; |
9868 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9869 | | byte* certReqCtx = NULL; |
9870 | | #endif |
9871 | |
|
9872 | | #ifdef OPENSSL_EXTRA |
9873 | | WOLFSSL_X509* x509 = NULL; |
9874 | | WOLFSSL_EVP_PKEY* pkey = NULL; |
9875 | | #endif |
9876 | |
|
9877 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND); |
9878 | 0 | WOLFSSL_ENTER("SendTls13Certificate"); |
9879 | |
|
9880 | 0 | XMEMSET(extSz, 0, sizeof(extSz)); |
9881 | |
|
9882 | 0 | ssl->options.buildingMsg = 1; |
9883 | |
|
9884 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9885 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9886 | | certReqCtxLen = ssl->certReqCtx->len; |
9887 | | certReqCtx = &ssl->certReqCtx->ctx; |
9888 | | } |
9889 | | #endif |
9890 | |
|
9891 | | #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_SETUP_CB) |
9892 | | /* call client cert callback if no cert has been loaded */ |
9893 | | if ((ssl->ctx->CBClientCert != NULL) && |
9894 | | (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) { |
9895 | | ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey); |
9896 | | if (ret == 1) { |
9897 | | if ((wolfSSL_CTX_use_certificate(ssl->ctx, x509) == WOLFSSL_SUCCESS) && |
9898 | | (wolfSSL_CTX_use_PrivateKey(ssl->ctx, pkey) == WOLFSSL_SUCCESS)) { |
9899 | | ssl->options.sendVerify = SEND_CERT; |
9900 | | } |
9901 | | wolfSSL_X509_free(x509); |
9902 | | x509 = NULL; |
9903 | | wolfSSL_EVP_PKEY_free(pkey); |
9904 | | } |
9905 | | } |
9906 | | #endif |
9907 | |
|
9908 | 0 | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9909 | 0 | certSz = 0; |
9910 | 0 | certChainSz = 0; |
9911 | 0 | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ; |
9912 | 0 | length = (sword32)headerSz; |
9913 | 0 | listSz = 0; |
9914 | 0 | } |
9915 | 0 | else { |
9916 | 0 | if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) { |
9917 | 0 | WOLFSSL_MSG("Send Cert missing certificate buffer"); |
9918 | 0 | return NO_CERT_ERROR; |
9919 | 0 | } |
9920 | | /* Certificate Data */ |
9921 | 0 | certSz = ssl->buffers.certificate->length; |
9922 | 0 | if (ssl->buffers.certChainCnt > MAX_CHAIN_DEPTH) { |
9923 | 0 | WOLFSSL_MSG("Certificate chain count exceeds maximum depth"); |
9924 | 0 | return MAX_CHAIN_ERROR; |
9925 | 0 | } |
9926 | | /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */ |
9927 | 0 | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ + |
9928 | 0 | CERT_HEADER_SZ; |
9929 | | /* set empty extension as default */ |
9930 | 0 | for (extIdx = 0; extIdx < (word16)XELEM_CNT(extSz); extIdx++) |
9931 | 0 | extSz[extIdx] = OPAQUE16_LEN; |
9932 | |
|
9933 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9934 | | /* Staple our own OCSP response with the Certificate. Normally only the |
9935 | | * server staples; the client's CSR holds the server's response, so |
9936 | | * echoing it back is wrong. The exception is post-handshake auth (PHA), |
9937 | | * where the client sends its own Certificate and staples for it. */ |
9938 | | if (ssl->options.side == WOLFSSL_SERVER_END |
9939 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9940 | | || (ssl->options.side == WOLFSSL_CLIENT_END |
9941 | | && ssl->options.handShakeDone) |
9942 | | #endif |
9943 | | ) { |
9944 | | /* Build the responses once. A resumed send reuses them: looking |
9945 | | * them up again appends another set of requests to the extension |
9946 | | * until it overflows with MAX_CERT_EXTENSIONS_ERR. */ |
9947 | | if (ssl->fragOffset == 0) { |
9948 | | ret = SetupOcspResp(ssl); |
9949 | | if (ret != 0) |
9950 | | return ret; |
9951 | | } |
9952 | | |
9953 | | if ((1 + ssl->buffers.certChainCnt) > MAX_CERT_EXTENSIONS) |
9954 | | ret = MAX_CERT_EXTENSIONS_ERR; |
9955 | | if (ret == 0) |
9956 | | ret = WriteCSRToBuffer(ssl, &ssl->buffers.certExts[0], &extSz[0], |
9957 | | 1 /* +1 for leaf */ + (word16)ssl->buffers.certChainCnt); |
9958 | | if (ret < 0) |
9959 | | return ret; |
9960 | | totalextSz += ret; |
9961 | | ret = 0; /* Clear to signal no error */ |
9962 | | } |
9963 | | else |
9964 | | #endif |
9965 | 0 | { |
9966 | | /* Leaf cert empty extension size */ |
9967 | 0 | totalextSz += OPAQUE16_LEN; |
9968 | | /* chain cert empty extension size */ |
9969 | 0 | totalextSz += OPAQUE16_LEN * ssl->buffers.certChainCnt; |
9970 | 0 | } |
9971 | | |
9972 | | /* Length of message data with one certificate and extensions. */ |
9973 | 0 | length = (sword32)(headerSz + certSz + totalextSz); |
9974 | | /* Length of list data with one certificate and extensions. */ |
9975 | 0 | listSz = CERT_HEADER_SZ + certSz + totalextSz; |
9976 | | |
9977 | | /* Send rest of chain if sending cert (chain has leading size/s). */ |
9978 | 0 | if (certSz > 0 && ssl->buffers.certChainCnt > 0) { |
9979 | 0 | p = ssl->buffers.certChain->buffer; |
9980 | | /* Chain length including extensions. */ |
9981 | 0 | certChainSz = ssl->buffers.certChain->length; |
9982 | |
|
9983 | 0 | length += certChainSz; |
9984 | 0 | listSz += certChainSz; |
9985 | 0 | } |
9986 | 0 | else |
9987 | 0 | certChainSz = 0; |
9988 | 0 | } |
9989 | | |
9990 | 0 | payloadSz = (word32)length; |
9991 | |
|
9992 | 0 | if (ssl->fragOffset != 0) |
9993 | 0 | length -= (ssl->fragOffset + headerSz); |
9994 | |
|
9995 | 0 | maxFragment = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
9996 | |
|
9997 | 0 | extIdx = 0; |
9998 | | |
9999 | | /* Only ssl->fragOffset survives a WANT_WRITE, so a resume inside the chain |
10000 | | * has to rebuild the walk cursor from it. */ |
10001 | 0 | if (certChainSz > 0 && ssl->fragOffset >= certSz + extSz[0]) { |
10002 | 0 | word32 chainPos = ssl->fragOffset - (certSz + extSz[0]); |
10003 | |
|
10004 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
10005 | | /* The leaf is behind us and its buffer was rebuilt above. */ |
10006 | | FreeDer(&ssl->buffers.certExts[0]); |
10007 | | #endif |
10008 | |
|
10009 | 0 | while (chainPos > 0) { |
10010 | 0 | word32 prevIdx = idx; |
10011 | |
|
10012 | 0 | len = NextCert(ssl->buffers.certChain->buffer, |
10013 | 0 | ssl->buffers.certChain->length, &idx); |
10014 | 0 | if (len == 0) |
10015 | 0 | break; |
10016 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
10017 | | !defined(NO_WOLFSSL_SERVER) |
10018 | | if (extIdx + 1 < MAX_CERT_EXTENSIONS) |
10019 | | extIdx++; |
10020 | | #endif |
10021 | 0 | entrySz = len + extSz[extIdx]; |
10022 | |
|
10023 | 0 | if (chainPos < entrySz) { |
10024 | | /* Resume part way through this entry. */ |
10025 | 0 | p = ssl->buffers.certChain->buffer + prevIdx; |
10026 | 0 | offset = chainPos; |
10027 | 0 | chainPos = 0; |
10028 | 0 | } |
10029 | 0 | else { |
10030 | | /* Entry already sent in full; stay primed for the next one. */ |
10031 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
10032 | | !defined(NO_WOLFSSL_SERVER) |
10033 | | /* Its buffer was rebuilt above and nothing writes it again. */ |
10034 | | FreeDer(&ssl->buffers.certExts[extIdx]); |
10035 | | #endif |
10036 | 0 | chainPos -= entrySz; |
10037 | 0 | offset = 0; |
10038 | 0 | entrySz = 0; |
10039 | 0 | } |
10040 | 0 | } |
10041 | 0 | } |
10042 | |
|
10043 | 0 | while (length > 0 && ret == 0) { |
10044 | 0 | byte* output = NULL; |
10045 | 0 | word32 fragSz = 0; |
10046 | 0 | word32 i = RECORD_HEADER_SZ; |
10047 | 0 | int sendSz = RECORD_HEADER_SZ; |
10048 | |
|
10049 | | #ifdef WOLFSSL_DTLS13 |
10050 | | if (ssl->options.dtls) { |
10051 | | i = Dtls13GetRlHeaderLength(ssl, 1); |
10052 | | sendSz = (int)i; |
10053 | | } |
10054 | | #endif /* WOLFSSL_DTLS13 */ |
10055 | |
|
10056 | 0 | if (ssl->fragOffset == 0) { |
10057 | 0 | if (headerSz + certSz + totalextSz + certChainSz <= |
10058 | 0 | maxFragment - HANDSHAKE_HEADER_SZ) { |
10059 | 0 | fragSz = headerSz + certSz + totalextSz + certChainSz; |
10060 | 0 | } |
10061 | | #ifdef WOLFSSL_DTLS13 |
10062 | | else if (ssl->options.dtls){ |
10063 | | /* short-circuit the fragmentation logic here. DTLS |
10064 | | fragmentation will be done in dtls13HandshakeSend() */ |
10065 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
10066 | | } |
10067 | | #endif /* WOLFSSL_DTLS13 */ |
10068 | 0 | else { |
10069 | 0 | fragSz = maxFragment - HANDSHAKE_HEADER_SZ; |
10070 | 0 | } |
10071 | |
|
10072 | 0 | sendSz += fragSz + HANDSHAKE_HEADER_SZ; |
10073 | 0 | i += HANDSHAKE_HEADER_SZ; |
10074 | | #ifdef WOLFSSL_DTLS13 |
10075 | | if (ssl->options.dtls) { |
10076 | | sendSz += DTLS_HANDSHAKE_EXTRA; |
10077 | | i += DTLS_HANDSHAKE_EXTRA; |
10078 | | } |
10079 | | #endif /* WOLFSSL_DTLS13 */ |
10080 | 0 | } |
10081 | 0 | else { |
10082 | 0 | fragSz = min((word32)length, maxFragment); |
10083 | 0 | sendSz += fragSz; |
10084 | 0 | } |
10085 | |
|
10086 | 0 | sendSz += MAX_MSG_EXTRA; |
10087 | | |
10088 | | /* Check buffers are big enough and grow if needed. */ |
10089 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
10090 | 0 | return ret; |
10091 | | |
10092 | | /* Get position in output buffer to write new message to. */ |
10093 | 0 | output = GetOutputBuffer(ssl); |
10094 | |
|
10095 | 0 | if (ssl->fragOffset == 0) { |
10096 | 0 | AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl); |
10097 | | |
10098 | | /* Request context. */ |
10099 | 0 | output[i++] = certReqCtxLen; |
10100 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
10101 | | if (certReqCtxLen > 0) { |
10102 | | XMEMCPY(output + i, certReqCtx, certReqCtxLen); |
10103 | | i += certReqCtxLen; |
10104 | | } |
10105 | | #endif |
10106 | 0 | length -= OPAQUE8_LEN + certReqCtxLen; |
10107 | 0 | fragSz -= OPAQUE8_LEN + certReqCtxLen; |
10108 | | /* Certificate list length. */ |
10109 | 0 | c32to24(listSz, output + i); |
10110 | 0 | i += CERT_HEADER_SZ; |
10111 | 0 | length -= CERT_HEADER_SZ; |
10112 | 0 | fragSz -= CERT_HEADER_SZ; |
10113 | | /* Leaf certificate data length. */ |
10114 | 0 | if (certSz > 0) { |
10115 | 0 | c32to24(certSz, output + i); |
10116 | 0 | i += CERT_HEADER_SZ; |
10117 | 0 | length -= CERT_HEADER_SZ; |
10118 | 0 | fragSz -= CERT_HEADER_SZ; |
10119 | 0 | } |
10120 | 0 | } |
10121 | 0 | else |
10122 | 0 | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
10123 | |
|
10124 | 0 | if (extIdx == 0) { |
10125 | 0 | if (certSz > 0 && ssl->fragOffset < certSz + extSz[0]) { |
10126 | | /* Put in the leaf certificate with extensions. */ |
10127 | 0 | word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer, |
10128 | 0 | certSz, extSz[0], ssl->fragOffset, fragSz, |
10129 | 0 | output + i, 0); |
10130 | 0 | i += copySz; |
10131 | 0 | ssl->fragOffset += copySz; |
10132 | 0 | length -= copySz; |
10133 | 0 | fragSz -= copySz; |
10134 | 0 | if (ssl->fragOffset == certSz + extSz[0]) |
10135 | 0 | FreeDer(&ssl->buffers.certExts[0]); |
10136 | 0 | } |
10137 | 0 | } |
10138 | 0 | if (certChainSz > 0 && fragSz > 0) { |
10139 | | /* Put in the CA certificates with extensions. */ |
10140 | 0 | while (fragSz > 0) { |
10141 | 0 | word32 l; |
10142 | |
|
10143 | 0 | if (offset == entrySz) { |
10144 | | /* Find next CA certificate to write out. */ |
10145 | 0 | offset = 0; |
10146 | | /* Point to the start of current cert in chain buffer. */ |
10147 | 0 | p = ssl->buffers.certChain->buffer + idx; |
10148 | 0 | len = NextCert(ssl->buffers.certChain->buffer, |
10149 | 0 | ssl->buffers.certChain->length, &idx); |
10150 | 0 | if (len == 0) |
10151 | 0 | break; |
10152 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
10153 | | !defined(NO_WOLFSSL_SERVER) |
10154 | | if (extIdx + 1 < MAX_CERT_EXTENSIONS) |
10155 | | extIdx++; |
10156 | | #endif |
10157 | | /* Certificate and its extensions make up the entry. */ |
10158 | 0 | entrySz = len + extSz[extIdx]; |
10159 | 0 | } |
10160 | | /* Write out certificate and extension. */ |
10161 | 0 | l = AddCertExt(ssl, p, len, extSz[extIdx], offset, fragSz, |
10162 | 0 | output + i, extIdx); |
10163 | 0 | i += l; |
10164 | 0 | ssl->fragOffset += l; |
10165 | 0 | length -= l; |
10166 | 0 | fragSz -= l; |
10167 | 0 | offset += l; |
10168 | |
|
10169 | 0 | if (extIdx != 0 && extIdx < MAX_CERT_EXTENSIONS && |
10170 | 0 | ssl->buffers.certExts[extIdx] != NULL && |
10171 | 0 | offset == entrySz) { |
10172 | 0 | FreeDer(&ssl->buffers.certExts[extIdx]); |
10173 | 0 | } |
10174 | 0 | } |
10175 | 0 | } |
10176 | |
|
10177 | 0 | if ((int)i - RECORD_HEADER_SZ < 0) { |
10178 | 0 | WOLFSSL_MSG("Send Cert bad inputSz"); |
10179 | 0 | return BUFFER_E; |
10180 | 0 | } |
10181 | | |
10182 | | #ifdef WOLFSSL_DTLS13 |
10183 | | if (ssl->options.dtls) { |
10184 | | /* DTLS1.3 uses a separate variable and logic for fragments */ |
10185 | | ssl->options.buildingMsg = 0; |
10186 | | ssl->fragOffset = 0; |
10187 | | if ((word32)sendSz > WOLFSSL_MAX_16BIT || i > WOLFSSL_MAX_16BIT) { |
10188 | | WOLFSSL_MSG("Send Cert DTLS size exceeds word16"); |
10189 | | return BUFFER_E; |
10190 | | } |
10191 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
10192 | | certificate, 1); |
10193 | | } |
10194 | | else |
10195 | | #endif /* WOLFSSL_DTLS13 */ |
10196 | 0 | { |
10197 | | /* This message is always encrypted. */ |
10198 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, |
10199 | 0 | output + RECORD_HEADER_SZ, (int)(i - RECORD_HEADER_SZ), |
10200 | 0 | handshake, 1, |
10201 | 0 | 0, 0); |
10202 | 0 | if (sendSz < 0) |
10203 | 0 | return sendSz; |
10204 | | |
10205 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
10206 | | if (ssl->hsInfoOn) |
10207 | | AddPacketName(ssl, "Certificate"); |
10208 | | if (ssl->toInfoOn) { |
10209 | | ret = AddPacketInfo(ssl, "Certificate", handshake, output, |
10210 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
10211 | | if (ret != 0) |
10212 | | return ret; |
10213 | | } |
10214 | | #endif |
10215 | | |
10216 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
10217 | 0 | ssl->options.buildingMsg = 0; |
10218 | 0 | if (!ssl->options.groupMessages) |
10219 | 0 | ret = SendBuffered(ssl); |
10220 | 0 | } |
10221 | 0 | } |
10222 | | |
10223 | 0 | if (ret != WC_NO_ERR_TRACE(WANT_WRITE)) { |
10224 | | /* Clean up the fragment offset. */ |
10225 | 0 | ssl->options.buildingMsg = 0; |
10226 | 0 | ssl->fragOffset = 0; |
10227 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
10228 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
10229 | 0 | } |
10230 | |
|
10231 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
10232 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
10233 | | CertReqCtx* ctx = ssl->certReqCtx; |
10234 | | ssl->certReqCtx = ssl->certReqCtx->next; |
10235 | | XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
10236 | | } |
10237 | | #endif |
10238 | |
|
10239 | 0 | WOLFSSL_LEAVE("SendTls13Certificate", ret); |
10240 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND); |
10241 | |
|
10242 | 0 | return ret; |
10243 | 0 | } |
10244 | | |
10245 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
10246 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
10247 | | defined(WOLFSSL_HAVE_MLDSA) || defined(WOLFSSL_HAVE_SLHDSA)) && \ |
10248 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
10249 | | /* Members are grouped widest first so the struct carries no interior padding. |
10250 | | * Under WOLFSSL_ASYNC_CRYPT this must fit ssl->async->args (MAX_ASYNC_ARGS |
10251 | | * word32s), which the static assert in SendTls13CertificateVerify enforces. */ |
10252 | | typedef struct Scv13Args { |
10253 | | byte* output; /* not allocated */ |
10254 | | byte* verify; /* not allocated */ |
10255 | | byte* sigData; |
10256 | | #ifndef NO_RSA |
10257 | | byte* toSign; /* not allocated */ |
10258 | | #endif |
10259 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10260 | | byte* altSigData; |
10261 | | #endif |
10262 | | /* Fragmented CertificateVerify send cursor, used when the signature does |
10263 | | * not fit in a single TLS record (SLH-DSA and other oversized signatures). |
10264 | | * Kept in the async args so a WC_PENDING_E from the record AEAD under |
10265 | | * WOLFSSL_ASYNC_CRYPT resumes on the same fragment instead of re-emitting |
10266 | | * the records already committed to the output buffer. */ |
10267 | | byte* frag; /* body copy, freed by FreeScv13Args; NULL when idle */ |
10268 | | |
10269 | | word32 idx; |
10270 | | word32 sigLen; |
10271 | | int sendSz; |
10272 | | word32 length; |
10273 | | #ifndef NO_RSA |
10274 | | word32 toSignSz; |
10275 | | #endif |
10276 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10277 | | word32 altSigLen; /* Only used in the case of both native and alt. */ |
10278 | | #endif |
10279 | | word32 outputSz; /* reserved capacity of the output record buffer */ |
10280 | | word32 fragOffset; /* body bytes already committed to records */ |
10281 | | |
10282 | | word16 sigDataSz; |
10283 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10284 | | word16 altSigDataSz; |
10285 | | #endif |
10286 | | |
10287 | | byte sigAlgo; |
10288 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10289 | | byte altSigAlgo; |
10290 | | #endif |
10291 | | byte fragActive; /* current fragment laid out, record build may pend */ |
10292 | | } Scv13Args; |
10293 | | |
10294 | | static void FreeScv13Args(WOLFSSL* ssl, void* pArgs) |
10295 | 377 | { |
10296 | 377 | Scv13Args* args = (Scv13Args*)pArgs; |
10297 | | |
10298 | 377 | (void)ssl; |
10299 | | |
10300 | 377 | if (args && args->sigData) { |
10301 | 368 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10302 | 368 | args->sigData = NULL; |
10303 | 368 | } |
10304 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10305 | | if (args && args->altSigData != NULL) { |
10306 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10307 | | args->altSigData = NULL; |
10308 | | } |
10309 | | #endif |
10310 | 377 | if (args != NULL && args->frag != NULL) { |
10311 | 0 | XFREE(args->frag, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
10312 | 0 | args->frag = NULL; |
10313 | 0 | } |
10314 | 377 | } |
10315 | | |
10316 | | /* handle generation TLS v1.3 certificate_verify (15) */ |
10317 | | /* Send the TLS v1.3 CertificateVerify message. |
10318 | | * A hash of all the message so far is used. |
10319 | | * The signed data is: |
10320 | | * 0x20 * 64 | context string | 0x00 | hash of messages |
10321 | | * This message is always encrypted in TLS v1.3. |
10322 | | * |
10323 | | * ssl The SSL/TLS object. |
10324 | | * returns 0 on success, otherwise failure. |
10325 | | */ |
10326 | | static int SendTls13CertificateVerify(WOLFSSL* ssl) |
10327 | | { |
10328 | | int ret = 0; |
10329 | | #ifndef NO_RSA |
10330 | | /* Use this as a temporary buffer for RSA signature verification. */ |
10331 | | buffer* rsaSigBuf = &ssl->buffers.sig; |
10332 | | #endif |
10333 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10334 | | Scv13Args* args = NULL; |
10335 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
10336 | | #else |
10337 | | Scv13Args args[1]; |
10338 | | #endif |
10339 | | |
10340 | | #ifdef WOLFSSL_DTLS13 |
10341 | | int recordLayerHdrExtra; |
10342 | | #endif /* WOLFSSL_DTLS13 */ |
10343 | | |
10344 | | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
10345 | | WOLFSSL_ENTER("SendTls13CertificateVerify"); |
10346 | | |
10347 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10348 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
10349 | | #endif |
10350 | | |
10351 | | ssl->options.buildingMsg = 1; |
10352 | | |
10353 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10354 | | ret = tsip_Tls13SendCertVerify(ssl); |
10355 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10356 | | goto exit_scv; |
10357 | | } |
10358 | | ret = 0; |
10359 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
10360 | | |
10361 | | #ifdef WOLFSSL_DTLS13 |
10362 | | /* can be negative */ |
10363 | | if (ssl->options.dtls) |
10364 | | recordLayerHdrExtra = Dtls13GetRlHeaderLength(ssl, 1) - RECORD_HEADER_SZ; |
10365 | | else |
10366 | | recordLayerHdrExtra = 0; |
10367 | | |
10368 | | #endif /* WOLFSSL_DTLS13 */ |
10369 | | |
10370 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10371 | | if (ssl->async == NULL) { |
10372 | | ssl->async = (struct WOLFSSL_ASYNC*) |
10373 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
10374 | | DYNAMIC_TYPE_ASYNC); |
10375 | | if (ssl->async == NULL) |
10376 | | ERROR_OUT(MEMORY_E, exit_scv); |
10377 | | } |
10378 | | args = (Scv13Args*)ssl->async->args; |
10379 | | |
10380 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
10381 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
10382 | | /* Check for error */ |
10383 | | if (ret < 0) |
10384 | | goto exit_scv; |
10385 | | } |
10386 | | else |
10387 | | #endif |
10388 | | { |
10389 | | /* Reset state */ |
10390 | | ret = 0; |
10391 | | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
10392 | | XMEMSET(args, 0, sizeof(Scv13Args)); |
10393 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10394 | | ssl->async->freeArgs = FreeScv13Args; |
10395 | | #endif |
10396 | | } |
10397 | | |
10398 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
10399 | | /* Resuming a partially-sent streamed CertificateVerify (e.g. after a |
10400 | | * non-blocking WANT_WRITE): the signature is already assembled in |
10401 | | * ssl->buffers.certVerifyMsg and ssl->fragOffset marks how much has been |
10402 | | * committed to records, so skip re-signing and continue sending. */ |
10403 | | if (ssl->buffers.certVerifyMsg.buffer != NULL && ssl->fragOffset != 0) { |
10404 | | ssl->options.asyncState = TLS_ASYNC_END; |
10405 | | } |
10406 | | #endif |
10407 | | |
10408 | | switch(ssl->options.asyncState) |
10409 | | { |
10410 | | case TLS_ASYNC_BEGIN: |
10411 | | { |
10412 | | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
10413 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10414 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, |
10415 | | ssl->buffers.keyMask); |
10416 | | #endif |
10417 | | return 0; /* sent blank cert, can't verify */ |
10418 | | } |
10419 | | |
10420 | | /* The output buffer is reserved in TLS_ASYNC_BUILD, once the |
10421 | | * private key has been decoded and the actual signature size is |
10422 | | * known. This avoids reserving the worst-case WC_MAX_CERT_VERIFY_SZ |
10423 | | * (very large when SLH-DSA is enabled) for every algorithm. */ |
10424 | | |
10425 | | /* Advance state and proceed */ |
10426 | | ssl->options.asyncState = TLS_ASYNC_BUILD; |
10427 | | } /* case TLS_ASYNC_BEGIN */ |
10428 | | FALL_THROUGH; |
10429 | | |
10430 | | case TLS_ASYNC_BUILD: |
10431 | | { |
10432 | | int rem; |
10433 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
10434 | | word32 bodySz; |
10435 | | word32 maxFrag; |
10436 | | #endif |
10437 | | int doStream = 0; |
10438 | | |
10439 | | /* Decode the private key first so the output buffer can be sized |
10440 | | * to the actual signature length rather than the worst-case |
10441 | | * WC_MAX_CERT_VERIFY_SZ (very large when SLH-DSA is enabled). */ |
10442 | | if (ssl->buffers.key == NULL) { |
10443 | | #ifdef HAVE_PK_CALLBACKS |
10444 | | if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) |
10445 | | args->sigLen = (word32)GetPrivateKeySigSize(ssl); |
10446 | | else |
10447 | | #endif |
10448 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
10449 | | } |
10450 | | else { |
10451 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10452 | | if (ssl->sigSpec != NULL && |
10453 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10454 | | /* In the case of alternative, we swap in the alt. */ |
10455 | | if (ssl->buffers.altKey == NULL) { |
10456 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
10457 | | } |
10458 | | ssl->buffers.keyType = ssl->buffers.altKeyType; |
10459 | | ssl->buffers.keySz = ssl->buffers.altKeySz; |
10460 | | /* If we own it, free key before overriding it. */ |
10461 | | if (ssl->buffers.weOwnKey) { |
10462 | | FreeDer(&ssl->buffers.key); |
10463 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10464 | | FreeDer(&ssl->buffers.keyMask); |
10465 | | #endif |
10466 | | } |
10467 | | |
10468 | | /* Swap keys */ |
10469 | | ssl->buffers.key = ssl->buffers.altKey; |
10470 | | ssl->buffers.weOwnKey = ssl->buffers.weOwnAltKey; |
10471 | | |
10472 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10473 | | ssl->buffers.keyMask = ssl->buffers.altKeyMask; |
10474 | | /* Unblind the alternative key before decoding */ |
10475 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
10476 | | #endif |
10477 | | } |
10478 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10479 | | ret = DecodePrivateKey(ssl, &args->sigLen); |
10480 | | if (ret != 0) |
10481 | | goto exit_scv; |
10482 | | } |
10483 | | |
10484 | | if (args->sigLen == 0) { |
10485 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
10486 | | } |
10487 | | |
10488 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10489 | | if (ssl->peerSigSpec == NULL) { |
10490 | | /* The peer did not respond. We didn't send CKS or they don't |
10491 | | * support it. Either way, we do not need to handle dual |
10492 | | * key/sig case. */ |
10493 | | ssl->sigSpec = NULL; |
10494 | | ssl->sigSpecSz = 0; |
10495 | | } |
10496 | | |
10497 | | if (ssl->sigSpec != NULL && |
10498 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10499 | | /* The native was already decoded. Decode the alternative now |
10500 | | * too so its signature length is included when the output |
10501 | | * buffer is sized below. */ |
10502 | | if (ssl->buffers.altKey == NULL) { |
10503 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
10504 | | } |
10505 | | |
10506 | | /* After this call, args->altSigLen has the length we need for |
10507 | | * the alternative signature. */ |
10508 | | ret = DecodeAltPrivateKey(ssl, &args->altSigLen); |
10509 | | if (ret != 0) |
10510 | | goto exit_scv; |
10511 | | |
10512 | | if (ssl->buffers.altKeyType == ecc_dsa_sa_algo || |
10513 | | ssl->buffers.altKeyType == falcon_level1_sa_algo || |
10514 | | ssl->buffers.altKeyType == falcon_level5_sa_algo || |
10515 | | ssl->buffers.altKeyType == mldsa_44_sa_algo || |
10516 | | ssl->buffers.altKeyType == mldsa_65_sa_algo || |
10517 | | ssl->buffers.altKeyType == mldsa_87_sa_algo) { |
10518 | | args->altSigAlgo = ssl->buffers.altKeyType; |
10519 | | } |
10520 | | else if (ssl->buffers.altKeyType == rsa_sa_algo && |
10521 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10522 | | args->altSigAlgo = rsa_pss_sa_algo; |
10523 | | } |
10524 | | else { |
10525 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10526 | | } |
10527 | | } |
10528 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10529 | | |
10530 | | /* Decide how the CertificateVerify body will be emitted. When it |
10531 | | * exceeds a single record (e.g. a large SLH-DSA/ML-DSA signature, |
10532 | | * or any signature under a small max_fragment_length) on TLS 1.3, |
10533 | | * use the streaming path: generate the signature into a |
10534 | | * connection-level body buffer and send it one record at a time in |
10535 | | * TLS_ASYNC_END, so the shared output buffer never has to hold the |
10536 | | * whole signature. Otherwise reserve the output buffer for the |
10537 | | * whole message and build it in place. */ |
10538 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
10539 | | bodySz = HASH_SIG_SIZE + VERIFY_HEADER + (word32)args->sigLen; |
10540 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10541 | | /* A dual (BOTH) body carries a second length-prefixed signature. |
10542 | | * args->sigLen/altSigLen are upper bounds here for variable-length |
10543 | | * schemes (ECDSA, Falcon), so this may over-size the body buffer by |
10544 | | * a few bytes; the body is assembled contiguously with the actual |
10545 | | * lengths, the exact length is recorded in TLS_ASYNC_FINALIZE, and |
10546 | | * the trailing slack is never sent. */ |
10547 | | if (ssl->sigSpec != NULL && |
10548 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10549 | | bodySz += (word32)args->altSigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
10550 | | } |
10551 | | #endif |
10552 | | maxFrag = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
10553 | | if (!ssl->options.dtls && |
10554 | | (word32)HANDSHAKE_HEADER_SZ + bodySz > maxFrag) { |
10555 | | doStream = 1; |
10556 | | } |
10557 | | #endif /* WOLFSSL_TLS13_STREAM_CERT_VERIFY */ |
10558 | | |
10559 | | if (doStream) { |
10560 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
10561 | | /* Generate the body directly into the connection-level buffer. |
10562 | | * bodySz is exact here (a single fixed-or-max-length |
10563 | | * signature); the final length is recorded in |
10564 | | * TLS_ASYNC_FINALIZE. args->output stays NULL to mark the |
10565 | | * streaming path; the send loop reserves one record at a |
10566 | | * time. */ |
10567 | | XFREE(ssl->buffers.certVerifyMsg.buffer, ssl->heap, |
10568 | | DYNAMIC_TYPE_TMP_BUFFER); |
10569 | | ssl->buffers.certVerifyMsg.buffer = |
10570 | | (byte*)XMALLOC(bodySz, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
10571 | | if (ssl->buffers.certVerifyMsg.buffer == NULL) { |
10572 | | ssl->buffers.certVerifyMsg.length = 0; |
10573 | | ERROR_OUT(MEMORY_E, exit_scv); |
10574 | | } |
10575 | | ssl->buffers.certVerifyMsg.length = bodySz; |
10576 | | ssl->fragOffset = 0; |
10577 | | args->verify = ssl->buffers.certVerifyMsg.buffer; |
10578 | | args->idx = 0; |
10579 | | args->sendSz = (int)bodySz; |
10580 | | args->output = NULL; |
10581 | | args->outputSz = 0; |
10582 | | #endif /* WOLFSSL_TLS13_STREAM_CERT_VERIFY */ |
10583 | | } |
10584 | | else { |
10585 | | /* Reserve the output buffer, sized from the actual signature |
10586 | | * length(s) plus record/handshake framing and encryption |
10587 | | * slack. */ |
10588 | | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
10589 | | HASH_SIG_SIZE + VERIFY_HEADER + (int)args->sigLen; |
10590 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10591 | | /* A dual (BOTH) body carries a second signature behind its own |
10592 | | * length prefix, plus the combined length prefix. Matches the |
10593 | | * streaming bodySz above. */ |
10594 | | if (ssl->sigSpec != NULL && |
10595 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10596 | | args->sendSz += (int)args->altSigLen + OPAQUE16_LEN + |
10597 | | OPAQUE16_LEN; |
10598 | | } |
10599 | | else { |
10600 | | args->sendSz += (int)args->altSigLen; |
10601 | | } |
10602 | | #endif |
10603 | | #ifdef WOLFSSL_DTLS13 |
10604 | | if (ssl->options.dtls) |
10605 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10606 | | #endif /* WOLFSSL_DTLS13 */ |
10607 | | /* Framing and always-on encryption expansion. */ |
10608 | | args->sendSz += MAX_MSG_EXTRA; |
10609 | | |
10610 | | /* check for available size */ |
10611 | | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) { |
10612 | | goto exit_scv; |
10613 | | } |
10614 | | |
10615 | | /* get output buffer */ |
10616 | | args->output = GetOutputBuffer(ssl); |
10617 | | /* Remember the reserved capacity for BuildTls13Message / |
10618 | | * Dtls13HandshakeSend in TLS_ASYNC_END. */ |
10619 | | args->outputSz = (word32)args->sendSz; |
10620 | | |
10621 | | rem = (int)(ssl->buffers.outputBuffer.bufferSize |
10622 | | - ssl->buffers.outputBuffer.length |
10623 | | - RECORD_HEADER_SZ - HANDSHAKE_HEADER_SZ); |
10624 | | |
10625 | | /* idx is used to track verify pointer offset to output */ |
10626 | | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
10627 | | args->verify = |
10628 | | &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ]; |
10629 | | |
10630 | | #ifdef WOLFSSL_DTLS13 |
10631 | | if (ssl->options.dtls) { |
10632 | | rem -= recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10633 | | args->idx += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10634 | | args->verify += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10635 | | } |
10636 | | #endif /* WOLFSSL_DTLS13 */ |
10637 | | |
10638 | | if (rem < 0 || (int)args->sigLen > rem) { |
10639 | | ERROR_OUT(BUFFER_E, exit_scv); |
10640 | | } |
10641 | | } |
10642 | | |
10643 | | /* Add signature algorithm. */ |
10644 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) |
10645 | | args->sigAlgo = rsa_pss_sa_algo; |
10646 | | #ifdef HAVE_ECC |
10647 | | else if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10648 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10649 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10650 | | args->sigAlgo = sm2_sa_algo; |
10651 | | } |
10652 | | else |
10653 | | #endif |
10654 | | { |
10655 | | args->sigAlgo = ecc_dsa_sa_algo; |
10656 | | } |
10657 | | } |
10658 | | #endif |
10659 | | #ifdef HAVE_ED25519 |
10660 | | else if (ssl->hsType == DYNAMIC_TYPE_ED25519) |
10661 | | args->sigAlgo = ed25519_sa_algo; |
10662 | | #endif |
10663 | | #ifdef HAVE_ED448 |
10664 | | else if (ssl->hsType == DYNAMIC_TYPE_ED448) |
10665 | | args->sigAlgo = ed448_sa_algo; |
10666 | | #endif |
10667 | | #if defined(HAVE_FALCON) |
10668 | | else if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10669 | | args->sigAlgo = ssl->buffers.keyType; |
10670 | | } |
10671 | | #endif /* HAVE_FALCON */ |
10672 | | #if defined(WOLFSSL_HAVE_MLDSA) |
10673 | | else if (ssl->hsType == DYNAMIC_TYPE_MLDSA) { |
10674 | | args->sigAlgo = ssl->buffers.keyType; |
10675 | | } |
10676 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
10677 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
10678 | | else if (ssl->hsType == DYNAMIC_TYPE_SLHDSA) { |
10679 | | args->sigAlgo = ssl->buffers.keyType; |
10680 | | } |
10681 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
10682 | | else { |
10683 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10684 | | } |
10685 | | |
10686 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10687 | | if (ssl->sigSpec != NULL && |
10688 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10689 | | /* The alternative key was already decoded and its sig-alg |
10690 | | * determined above (when sizing the output buffer). Encode the |
10691 | | * dual (native + alternative) signature algorithm pair. */ |
10692 | | EncodeDualSigAlg(args->sigAlgo, args->altSigAlgo, args->verify); |
10693 | | if (args->verify[0] == 0) { |
10694 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10695 | | } |
10696 | | } |
10697 | | else |
10698 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10699 | | EncodeSigAlg(ssl, ssl->options.hashAlgo, args->sigAlgo, |
10700 | | args->verify); |
10701 | | |
10702 | | if (args->sigData == NULL) { |
10703 | | word32 sigLen = MAX_SIG_DATA_SZ; |
10704 | | if ((ssl->hsType == DYNAMIC_TYPE_RSA) && |
10705 | | (args->sigLen > MAX_SIG_DATA_SZ)) { |
10706 | | /* We store the RSA signature in the sigData buffer |
10707 | | * temporarily, hence its size must be fitting. */ |
10708 | | sigLen = args->sigLen; |
10709 | | } |
10710 | | args->sigData = (byte*)XMALLOC(sigLen, ssl->heap, |
10711 | | DYNAMIC_TYPE_SIGNATURE); |
10712 | | if (args->sigData == NULL) { |
10713 | | ERROR_OUT(MEMORY_E, exit_scv); |
10714 | | } |
10715 | | } |
10716 | | |
10717 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10718 | | if ((ssl->sigSpec != NULL) && |
10719 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
10720 | | (args->altSigData == NULL)) { |
10721 | | word32 sigLen = MAX_SIG_DATA_SZ; |
10722 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA && |
10723 | | args->altSigLen > MAX_SIG_DATA_SZ) { |
10724 | | /* We store the RSA signature in the sigData buffer |
10725 | | * temporarily, hence its size must be fitting. */ |
10726 | | sigLen = args->altSigLen; |
10727 | | } |
10728 | | args->altSigData = (byte*)XMALLOC(sigLen, ssl->heap, |
10729 | | DYNAMIC_TYPE_SIGNATURE); |
10730 | | if (args->altSigData == NULL) { |
10731 | | ERROR_OUT(MEMORY_E, exit_scv); |
10732 | | } |
10733 | | } |
10734 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10735 | | |
10736 | | /* Create the data to be signed. */ |
10737 | | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0); |
10738 | | if (ret != 0) |
10739 | | goto exit_scv; |
10740 | | |
10741 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10742 | | if ((ssl->sigSpec != NULL) && |
10743 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
10744 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
10745 | | args->altSigDataSz = args->sigDataSz; |
10746 | | } |
10747 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10748 | | |
10749 | | #ifndef NO_RSA |
10750 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
10751 | | /* build encoded signature buffer */ |
10752 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
10753 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, ssl->heap, |
10754 | | DYNAMIC_TYPE_SIGNATURE); |
10755 | | if (rsaSigBuf->buffer == NULL) { |
10756 | | ERROR_OUT(MEMORY_E, exit_scv); |
10757 | | } |
10758 | | |
10759 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, args->sigData, |
10760 | | args->sigDataSz, args->sigAlgo, ssl->options.hashAlgo); |
10761 | | if (ret < 0) |
10762 | | goto exit_scv; |
10763 | | rsaSigBuf->length = (unsigned int)ret; |
10764 | | ret = 0; |
10765 | | } |
10766 | | #endif /* !NO_RSA */ |
10767 | | #ifdef HAVE_ECC |
10768 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10769 | | args->sigLen = (word32)args->sendSz - args->idx - |
10770 | | HASH_SIG_SIZE - |
10771 | | VERIFY_HEADER; |
10772 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10773 | | if (ssl->buffers.keyType != sm2_sa_algo) |
10774 | | #endif |
10775 | | { |
10776 | | ret = CreateECCEncodedSig(args->sigData, |
10777 | | args->sigDataSz, ssl->options.hashAlgo); |
10778 | | if (ret < 0) |
10779 | | goto exit_scv; |
10780 | | args->sigDataSz = (word16)ret; |
10781 | | ret = 0; |
10782 | | } |
10783 | | } |
10784 | | #endif /* HAVE_ECC */ |
10785 | | #ifdef HAVE_ED25519 |
10786 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
10787 | | ret = Ed25519CheckPubKey(ssl); |
10788 | | if (ret < 0) { |
10789 | | ERROR_OUT(ret, exit_scv); |
10790 | | } |
10791 | | args->sigLen = ED25519_SIG_SIZE; |
10792 | | } |
10793 | | #endif /* HAVE_ED25519 */ |
10794 | | #ifdef HAVE_ED448 |
10795 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
10796 | | ret = Ed448CheckPubKey(ssl); |
10797 | | if (ret < 0) { |
10798 | | ERROR_OUT(ret, exit_scv); |
10799 | | } |
10800 | | args->sigLen = ED448_SIG_SIZE; |
10801 | | } |
10802 | | |
10803 | | #endif /* HAVE_ED448 */ |
10804 | | #if defined(HAVE_FALCON) |
10805 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10806 | | /* Per-key, not the family maximum: this is handed to the |
10807 | | * signer as the output capacity and the buffer above was |
10808 | | * reserved from this key's signature length. */ |
10809 | | int fSigSz = wc_falcon_sig_size((falcon_key*)ssl->hsKey); |
10810 | | if (fSigSz <= 0) { |
10811 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10812 | | } |
10813 | | args->sigLen = (word32)fSigSz; |
10814 | | } |
10815 | | #endif /* HAVE_FALCON */ |
10816 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN) |
10817 | | if (ssl->hsType == DYNAMIC_TYPE_MLDSA) { |
10818 | | int mSigSz = wc_MlDsaKey_SigSize((wc_MlDsaKey*)ssl->hsKey); |
10819 | | if (mSigSz <= 0) { |
10820 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10821 | | } |
10822 | | args->sigLen = (word32)mSigSz; |
10823 | | } |
10824 | | #endif /* WOLFSSL_HAVE_MLDSA && !WOLFSSL_MLDSA_NO_SIGN */ |
10825 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
10826 | | if (ssl->hsType == DYNAMIC_TYPE_SLHDSA) { |
10827 | | int slhSigSz = wc_SlhDsaKey_SigSize((SlhDsaKey*)ssl->hsKey); |
10828 | | if (slhSigSz <= 0) { |
10829 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10830 | | } |
10831 | | args->sigLen = (word32)slhSigSz; |
10832 | | } |
10833 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
10834 | | |
10835 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10836 | | if (ssl->sigSpec != NULL && |
10837 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10838 | | |
10839 | | #ifndef NO_RSA |
10840 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10841 | | /* build encoded signature buffer */ |
10842 | | XFREE(rsaSigBuf->buffer, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10843 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
10844 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, |
10845 | | ssl->heap, |
10846 | | DYNAMIC_TYPE_SIGNATURE); |
10847 | | if (rsaSigBuf->buffer == NULL) { |
10848 | | ERROR_OUT(MEMORY_E, exit_scv); |
10849 | | } |
10850 | | |
10851 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, |
10852 | | args->altSigData, args->altSigDataSz, |
10853 | | args->altSigAlgo, ssl->options.hashAlgo); |
10854 | | if (ret < 0) |
10855 | | goto exit_scv; |
10856 | | rsaSigBuf->length = ret; |
10857 | | ret = 0; |
10858 | | } |
10859 | | #endif /* !NO_RSA */ |
10860 | | #ifdef HAVE_ECC |
10861 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
10862 | | ret = CreateECCEncodedSig(args->altSigData, |
10863 | | args->altSigDataSz, ssl->options.hashAlgo); |
10864 | | if (ret < 0) |
10865 | | goto exit_scv; |
10866 | | args->altSigDataSz = (word16)ret; |
10867 | | ret = 0; |
10868 | | } |
10869 | | #endif /* HAVE_ECC */ |
10870 | | } |
10871 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10872 | | |
10873 | | /* Advance state and proceed */ |
10874 | | ssl->options.asyncState = TLS_ASYNC_DO; |
10875 | | } /* case TLS_ASYNC_BUILD */ |
10876 | | FALL_THROUGH; |
10877 | | |
10878 | | case TLS_ASYNC_DO: |
10879 | | { |
10880 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
10881 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10882 | | if (ssl->sigSpec != NULL && |
10883 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10884 | | /* As we have two signatures in the message, we store |
10885 | | * the length of each before the actual signature. This |
10886 | | * is necessary, as we could have two algorithms with |
10887 | | * variable length signatures. */ |
10888 | | sigOut += OPAQUE16_LEN; |
10889 | | } |
10890 | | #endif |
10891 | | /* Only the per-algorithm signing branches below consume this. */ |
10892 | | (void)sigOut; |
10893 | | #ifdef HAVE_ECC |
10894 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10895 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10896 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10897 | | ret = Sm2wSm3Sign(ssl, TLS13_SM2_SIG_ID, |
10898 | | TLS13_SM2_SIG_ID_SZ, args->sigData, args->sigDataSz, |
10899 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, NULL); |
10900 | | } |
10901 | | else |
10902 | | #endif |
10903 | | { |
10904 | | ret = EccSign(ssl, args->sigData, args->sigDataSz, |
10905 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, |
10906 | | #ifdef HAVE_PK_CALLBACKS |
10907 | | ssl->buffers.key |
10908 | | #else |
10909 | | NULL |
10910 | | #endif |
10911 | | ); |
10912 | | } |
10913 | | args->length = args->sigLen; |
10914 | | } |
10915 | | #endif /* HAVE_ECC */ |
10916 | | #ifdef HAVE_ED25519 |
10917 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
10918 | | ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz, |
10919 | | sigOut, &args->sigLen, (ed25519_key*)ssl->hsKey, |
10920 | | #ifdef HAVE_PK_CALLBACKS |
10921 | | ssl->buffers.key |
10922 | | #else |
10923 | | NULL |
10924 | | #endif |
10925 | | ); |
10926 | | args->length = args->sigLen; |
10927 | | } |
10928 | | #endif |
10929 | | #ifdef HAVE_ED448 |
10930 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
10931 | | ret = Ed448Sign(ssl, args->sigData, args->sigDataSz, |
10932 | | sigOut, &args->sigLen, (ed448_key*)ssl->hsKey, |
10933 | | #ifdef HAVE_PK_CALLBACKS |
10934 | | ssl->buffers.key |
10935 | | #else |
10936 | | NULL |
10937 | | #endif |
10938 | | ); |
10939 | | args->length = args->sigLen; |
10940 | | } |
10941 | | #endif |
10942 | | #if defined(HAVE_FALCON) |
10943 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10944 | | ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz, |
10945 | | sigOut, &args->sigLen, |
10946 | | (falcon_key*)ssl->hsKey, ssl->rng); |
10947 | | args->length = args->sigLen; |
10948 | | } |
10949 | | #endif /* HAVE_FALCON */ |
10950 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN) |
10951 | | if (ssl->hsType == DYNAMIC_TYPE_MLDSA) { |
10952 | | ret = wc_MlDsaKey_SignCtx((wc_MlDsaKey*)ssl->hsKey, NULL, 0, |
10953 | | sigOut, &args->sigLen, |
10954 | | args->sigData, args->sigDataSz, |
10955 | | ssl->rng); |
10956 | | args->length = args->sigLen; |
10957 | | } |
10958 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
10959 | | #if defined(WOLFSSL_HAVE_SLHDSA) && !defined(WOLFSSL_SLHDSA_VERIFY_ONLY) |
10960 | | if (ssl->hsType == DYNAMIC_TYPE_SLHDSA) { |
10961 | | /* The FIPS wrapper for wc_SlhDsaKey_Sign gates on the per-thread |
10962 | | * privateKeyReadEnable flag (unlike ML-DSA sign), returning |
10963 | | * FIPS_PRIVATE_KEY_LOCKED_E when the key is locked. Bracket the |
10964 | | * sign so it can read the SLH-DSA private key. */ |
10965 | | PRIVATE_KEY_UNLOCK(); |
10966 | | ret = wc_SlhDsaKey_Sign((SlhDsaKey*)ssl->hsKey, NULL, 0, |
10967 | | args->sigData, args->sigDataSz, |
10968 | | sigOut, &args->sigLen, ssl->rng); |
10969 | | PRIVATE_KEY_LOCK(); |
10970 | | args->length = args->sigLen; |
10971 | | } |
10972 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
10973 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
10974 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
10975 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
10976 | | args->toSign = rsaSigBuf->buffer; |
10977 | | args->toSignSz = (word32)rsaSigBuf->length; |
10978 | | #if defined(HAVE_PK_CALLBACKS) && \ |
10979 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
10980 | | /* Pass full data to sign (args->sigData), not hash of */ |
10981 | | if (ssl->ctx->RsaPssSignCb) { |
10982 | | args->toSign = args->sigData; |
10983 | | args->toSignSz = args->sigDataSz; |
10984 | | } |
10985 | | #endif |
10986 | | ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz, |
10987 | | sigOut, &args->sigLen, args->sigAlgo, |
10988 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
10989 | | ssl->buffers.key); |
10990 | | if (ret == 0) { |
10991 | | args->length = args->sigLen; |
10992 | | XMEMCPY(args->sigData, sigOut, args->sigLen); |
10993 | | } |
10994 | | } |
10995 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
10996 | | |
10997 | | /* Check for error */ |
10998 | | if (ret != 0) { |
10999 | | goto exit_scv; |
11000 | | } |
11001 | | |
11002 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11003 | | if (ssl->sigSpec != NULL && |
11004 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11005 | | /* Add signature length for the first signature. */ |
11006 | | c16toa((word16)args->sigLen, sigOut - OPAQUE16_LEN); |
11007 | | args->length += OPAQUE16_LEN; |
11008 | | |
11009 | | /* Advance our pointer to where we store the alt signature. |
11010 | | * We also add additional space for the length field of the |
11011 | | * second signature. */ |
11012 | | sigOut += args->sigLen + OPAQUE16_LEN; |
11013 | | |
11014 | | /* Generate the alternative signature */ |
11015 | | #ifdef HAVE_ECC |
11016 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
11017 | | ret = EccSign(ssl, args->altSigData, args->altSigDataSz, |
11018 | | sigOut, &args->altSigLen, |
11019 | | (ecc_key*)ssl->hsAltKey, |
11020 | | #ifdef HAVE_PK_CALLBACKS |
11021 | | ssl->buffers.altKey |
11022 | | #else |
11023 | | NULL |
11024 | | #endif |
11025 | | ); |
11026 | | } |
11027 | | #endif /* HAVE_ECC */ |
11028 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
11029 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
11030 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
11031 | | args->toSign = rsaSigBuf->buffer; |
11032 | | args->toSignSz = (word32)rsaSigBuf->length; |
11033 | | #if defined(HAVE_PK_CALLBACKS) && \ |
11034 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
11035 | | /* Pass full data to sign (args->altSigData), not hash of */ |
11036 | | if (ssl->ctx->RsaPssSignCb) { |
11037 | | args->toSign = args->altSigData; |
11038 | | args->toSignSz = (word32)args->altSigDataSz; |
11039 | | } |
11040 | | #endif |
11041 | | ret = RsaSign(ssl, (const byte*)args->toSign, |
11042 | | args->toSignSz, sigOut, &args->altSigLen, |
11043 | | args->altSigAlgo, ssl->options.hashAlgo, |
11044 | | (RsaKey*)ssl->hsAltKey, |
11045 | | ssl->buffers.altKey); |
11046 | | |
11047 | | if (ret == 0) { |
11048 | | XMEMCPY(args->altSigData, sigOut, args->altSigLen); |
11049 | | } |
11050 | | } |
11051 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
11052 | | #if defined(HAVE_FALCON) |
11053 | | if (ssl->hsAltType == DYNAMIC_TYPE_FALCON) { |
11054 | | ret = wc_falcon_sign_msg(args->altSigData, |
11055 | | args->altSigDataSz, sigOut, |
11056 | | &args->altSigLen, |
11057 | | (falcon_key*)ssl->hsAltKey, |
11058 | | ssl->rng); |
11059 | | } |
11060 | | #endif /* HAVE_FALCON */ |
11061 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_SIGN) |
11062 | | if (ssl->hsAltType == DYNAMIC_TYPE_MLDSA) { |
11063 | | ret = wc_MlDsaKey_SignCtx((wc_MlDsaKey*)ssl->hsAltKey, |
11064 | | NULL, 0, sigOut, &args->altSigLen, |
11065 | | args->altSigData, args->altSigDataSz, ssl->rng); |
11066 | | } |
11067 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
11068 | | |
11069 | | /* Check for error */ |
11070 | | if (ret != 0) { |
11071 | | goto exit_scv; |
11072 | | } |
11073 | | |
11074 | | /* Add signature length for the alternative signature. */ |
11075 | | c16toa((word16)args->altSigLen, sigOut - OPAQUE16_LEN); |
11076 | | |
11077 | | /* Add length of the alt sig to the total length */ |
11078 | | args->length += args->altSigLen + OPAQUE16_LEN; |
11079 | | } |
11080 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11081 | | |
11082 | | /* The TLS 1.3 CertificateVerify signature is length-prefixed with a |
11083 | | * 2-byte field (opaque signature<0..2^16-1>), so the body - a single |
11084 | | * signature, or the combined native + alternative signature with |
11085 | | * WOLFSSL_DUAL_ALG_CERTS - cannot exceed 65535 bytes. Reject rather |
11086 | | * than let c16toa truncate it into a malformed message. args->length |
11087 | | * is word32 so the dual-alg accumulation above cannot wrap before |
11088 | | * this check. */ |
11089 | | if (args->length > WOLFSSL_MAX_16BIT) { |
11090 | | ERROR_OUT(BUFFER_E, exit_scv); |
11091 | | } |
11092 | | |
11093 | | /* Add signature length. */ |
11094 | | c16toa((word16)args->length, args->verify + HASH_SIG_SIZE); |
11095 | | |
11096 | | /* Advance state and proceed */ |
11097 | | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
11098 | | } /* case TLS_ASYNC_DO */ |
11099 | | FALL_THROUGH; |
11100 | | |
11101 | | case TLS_ASYNC_VERIFY: |
11102 | | { |
11103 | | #ifndef NO_RSA |
11104 | | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
11105 | | /* check for signature faults */ |
11106 | | ret = VerifyRsaSign(ssl, args->sigData, args->sigLen, |
11107 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, args->sigAlgo, |
11108 | | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
11109 | | ssl->buffers.key); |
11110 | | } |
11111 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11112 | | if (ssl->sigSpec != NULL && |
11113 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11114 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
11115 | | /* check for signature faults */ |
11116 | | ret = VerifyRsaSign(ssl, args->altSigData, args->altSigLen, |
11117 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, |
11118 | | args->altSigAlgo, ssl->options.hashAlgo, |
11119 | | (RsaKey*)ssl->hsAltKey, ssl->buffers.altKey); |
11120 | | } |
11121 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11122 | | #endif /* !NO_RSA */ |
11123 | | #if defined(HAVE_ECC) && defined(WOLFSSL_CHECK_SIG_FAULTS) |
11124 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
11125 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
11126 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11127 | | if (ssl->sigSpec != NULL && |
11128 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11129 | | /* Add our length offset. */ |
11130 | | sigOut += OPAQUE16_LEN; |
11131 | | } |
11132 | | #endif |
11133 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
11134 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
11135 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
11136 | | TLS13_SM2_SIG_ID_SZ, |
11137 | | sigOut, args->sigLen, args->sigData, args->sigDataSz, |
11138 | | (ecc_key*)ssl->hsKey, NULL); |
11139 | | } |
11140 | | else |
11141 | | #endif |
11142 | | { |
11143 | | #ifdef HAVE_PK_CALLBACKS |
11144 | | buffer tmp; |
11145 | | |
11146 | | /* Private key may be held by the PK callback. */ |
11147 | | tmp.length = ssl->buffers.key ? |
11148 | | ssl->buffers.key->length : 0; |
11149 | | tmp.buffer = ssl->buffers.key ? |
11150 | | ssl->buffers.key->buffer : NULL; |
11151 | | #endif |
11152 | | ret = EccVerify(ssl, sigOut, args->sigLen, |
11153 | | args->sigData, args->sigDataSz, |
11154 | | (ecc_key*)ssl->hsKey, |
11155 | | #ifdef HAVE_PK_CALLBACKS |
11156 | | &tmp |
11157 | | #else |
11158 | | NULL |
11159 | | #endif |
11160 | | ); |
11161 | | } |
11162 | | } |
11163 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11164 | | if (ssl->sigSpec != NULL && |
11165 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11166 | | ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
11167 | | /* check for signature faults */ |
11168 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER + |
11169 | | args->sigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
11170 | | #ifdef HAVE_PK_CALLBACKS |
11171 | | buffer tmp; |
11172 | | |
11173 | | /* Private key may be held by the PK callback. */ |
11174 | | tmp.length = ssl->buffers.altKey ? |
11175 | | ssl->buffers.altKey->length : 0; |
11176 | | tmp.buffer = ssl->buffers.altKey ? |
11177 | | ssl->buffers.altKey->buffer : NULL; |
11178 | | #endif |
11179 | | ret = EccVerify(ssl, sigOut, args->altSigLen, |
11180 | | args->altSigData, args->altSigDataSz, |
11181 | | (ecc_key*)ssl->hsAltKey, |
11182 | | #ifdef HAVE_PK_CALLBACKS |
11183 | | &tmp |
11184 | | #else |
11185 | | NULL |
11186 | | #endif |
11187 | | ); |
11188 | | } |
11189 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11190 | | #endif /* HAVE_ECC && WOLFSSL_CHECK_SIG_FAULTS */ |
11191 | | |
11192 | | /* Check for error */ |
11193 | | if (ret != 0) { |
11194 | | goto exit_scv; |
11195 | | } |
11196 | | |
11197 | | /* Advance state and proceed */ |
11198 | | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
11199 | | } /* case TLS_ASYNC_VERIFY */ |
11200 | | FALL_THROUGH; |
11201 | | |
11202 | | case TLS_ASYNC_FINALIZE: |
11203 | | { |
11204 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
11205 | | if (ssl->buffers.certVerifyMsg.buffer != NULL) { |
11206 | | /* Streaming path: the assembled body sits in certVerifyMsg; |
11207 | | * record its final length (exact even for variable-length |
11208 | | * signatures). Per-fragment record/handshake headers are added |
11209 | | * in TLS_ASYNC_END. */ |
11210 | | ssl->buffers.certVerifyMsg.length = |
11211 | | (word32)args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
11212 | | } |
11213 | | #endif /* WOLFSSL_TLS13_STREAM_CERT_VERIFY */ |
11214 | | /* In-place path: put the record and handshake headers on now. |
11215 | | * args->output is NULL only on the streaming path. */ |
11216 | | if (args->output != NULL) { |
11217 | | AddTls13Headers(args->output, args->length + HASH_SIG_SIZE + |
11218 | | VERIFY_HEADER, certificate_verify, ssl); |
11219 | | |
11220 | | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
11221 | | args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
11222 | | #ifdef WOLFSSL_DTLS13 |
11223 | | if (ssl->options.dtls) |
11224 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
11225 | | #endif /* WOLFSSL_DTLS13 */ |
11226 | | } |
11227 | | /* Advance state and proceed */ |
11228 | | ssl->options.asyncState = TLS_ASYNC_END; |
11229 | | } /* case TLS_ASYNC_FINALIZE */ |
11230 | | FALL_THROUGH; |
11231 | | |
11232 | | case TLS_ASYNC_END: |
11233 | | { |
11234 | | /* Body of the handshake message: [sigAlg(2) | sigLen(2) | sig]. In |
11235 | | * the in-place path it sits at args->verify in the output buffer; |
11236 | | * in the streaming path it sits in ssl->buffers.certVerifyMsg. */ |
11237 | | word32 msgSz; |
11238 | | word32 maxFrag = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
11239 | | byte* output; |
11240 | | word32 fragSz; |
11241 | | word32 i; |
11242 | | int recSz; |
11243 | | int thisSendSz; |
11244 | | |
11245 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
11246 | | if (ssl->buffers.certVerifyMsg.buffer != NULL) { |
11247 | | /* Streamed send: the assembled body lives in |
11248 | | * ssl->buffers.certVerifyMsg and the send cursor in |
11249 | | * ssl->fragOffset - both connection-level - so a non-blocking |
11250 | | * WANT_WRITE resumes here (via TLS_ASYNC_END) without |
11251 | | * recomputing the signature. Emit one encrypted record per |
11252 | | * iteration and flush it, so the output buffer never holds more |
11253 | | * than a single fragment. Only the first record carries the |
11254 | | * handshake header; BuildTls13Message hashes each fragment's |
11255 | | * plaintext in order, keeping the transcript hash correct. The |
11256 | | * cursor is advanced before the flush because the record is |
11257 | | * already committed to the output buffer; the cursor tracks |
11258 | | * message-body -> record progress, not bytes on the wire, so a |
11259 | | * WANT_WRITE resumes on the next fragment. Advancing after the |
11260 | | * flush would rebuild and double-send the fragment on resume. |
11261 | | * This is not merely a convention: the accept/connect loop |
11262 | | * flushes pending output and returns WANT_WRITE without |
11263 | | * re-entering this function until the output buffer drains, so |
11264 | | * the committed record is never skipped or overwritten. |
11265 | | * certVerifyMsg is released on completion or error at exit_scv; |
11266 | | * it is kept across WANT_WRITE for resume. */ |
11267 | | msgSz = ssl->buffers.certVerifyMsg.length; |
11268 | | if (maxFrag <= HANDSHAKE_HEADER_SZ) { |
11269 | | ERROR_OUT(BUFFER_E, exit_scv); |
11270 | | } |
11271 | | while (ssl->fragOffset < msgSz && ret == 0) { |
11272 | | if (ssl->fragOffset == 0) { |
11273 | | fragSz = maxFrag - HANDSHAKE_HEADER_SZ; |
11274 | | if (fragSz > msgSz) |
11275 | | fragSz = msgSz; |
11276 | | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
11277 | | thisSendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
11278 | | (int)fragSz + MAX_MSG_EXTRA; |
11279 | | } |
11280 | | else { |
11281 | | fragSz = msgSz - ssl->fragOffset; |
11282 | | if (fragSz > maxFrag) |
11283 | | fragSz = maxFrag; |
11284 | | i = RECORD_HEADER_SZ; |
11285 | | thisSendSz = RECORD_HEADER_SZ + (int)fragSz + |
11286 | | MAX_MSG_EXTRA; |
11287 | | } |
11288 | | |
11289 | | if ((ret = CheckAvailableSize(ssl, thisSendSz)) != 0) { |
11290 | | goto exit_scv; |
11291 | | } |
11292 | | output = GetOutputBuffer(ssl); |
11293 | | |
11294 | | if (ssl->fragOffset == 0) { |
11295 | | AddTls13FragHeaders(output, fragSz, 0, msgSz, |
11296 | | certificate_verify, ssl); |
11297 | | } |
11298 | | else { |
11299 | | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
11300 | | } |
11301 | | XMEMCPY(output + i, |
11302 | | ssl->buffers.certVerifyMsg.buffer + ssl->fragOffset, |
11303 | | fragSz); |
11304 | | |
11305 | | /* This message is always encrypted. */ |
11306 | | recSz = BuildTls13Message(ssl, output, thisSendSz, |
11307 | | output + RECORD_HEADER_SZ, |
11308 | | (int)(i - RECORD_HEADER_SZ + fragSz), handshake, |
11309 | | 1, 0, 0); |
11310 | | if (recSz < 0) { |
11311 | | ret = recSz; |
11312 | | goto exit_scv; |
11313 | | } |
11314 | | |
11315 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11316 | | /* Trace the logical CertificateVerify once (first fragment), |
11317 | | * matching the single-record path. */ |
11318 | | if (ssl->fragOffset == 0) { |
11319 | | if (ssl->hsInfoOn) |
11320 | | AddPacketName(ssl, "CertificateVerify"); |
11321 | | if (ssl->toInfoOn) { |
11322 | | ret = AddPacketInfo(ssl, "CertificateVerify", |
11323 | | handshake, output, recSz, WRITE_PROTO, 0, |
11324 | | ssl->heap); |
11325 | | if (ret != 0) |
11326 | | goto exit_scv; |
11327 | | } |
11328 | | } |
11329 | | #endif |
11330 | | |
11331 | | ssl->buffers.outputBuffer.length += (word32)recSz; |
11332 | | ssl->fragOffset += fragSz; |
11333 | | /* Flush every fragment unconditionally - unlike the |
11334 | | * in-place path this cannot honor ssl->options.groupMessages |
11335 | | * (batch with later handshake messages), because streaming |
11336 | | * exists precisely to keep the output buffer bounded to a |
11337 | | * single fragment. */ |
11338 | | ret = SendBuffered(ssl); |
11339 | | } |
11340 | | |
11341 | | ssl->options.buildingMsg = 0; |
11342 | | break; |
11343 | | } |
11344 | | #endif /* WOLFSSL_TLS13_STREAM_CERT_VERIFY */ |
11345 | | |
11346 | | /* In-place path: the whole message is in args->output. */ |
11347 | | msgSz = (word32)args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
11348 | | |
11349 | | #ifdef WOLFSSL_DTLS13 |
11350 | | if (ssl->options.dtls) { |
11351 | | ssl->options.buildingMsg = 0; |
11352 | | |
11353 | | /* Dtls13HandshakeSend takes word16 output/send sizes, so the |
11354 | | * whole assembled CertificateVerify (signature plus framing) |
11355 | | * must fit in 16 bits. Every current SLH-DSA parameter set |
11356 | | * stays well under this (256f, the largest, is ~50KB), but |
11357 | | * guard the casts explicitly - mirroring the args->length check |
11358 | | * on the TLS path - so a future larger signature fails loudly |
11359 | | * instead of being silently truncated into a malformed record. |
11360 | | * outputSz is the reserved capacity and is >= sendSz, so it |
11361 | | * bounds both casts. */ |
11362 | | if (args->outputSz > WOLFSSL_MAX_16BIT) { |
11363 | | ERROR_OUT(BUFFER_E, exit_scv); |
11364 | | } |
11365 | | |
11366 | | ret = Dtls13HandshakeSend(ssl, args->output, |
11367 | | (word16)args->outputSz, |
11368 | | (word16)args->sendSz, certificate_verify, 1); |
11369 | | if (ret != 0) |
11370 | | goto exit_scv; |
11371 | | |
11372 | | break; |
11373 | | } |
11374 | | #endif /* WOLFSSL_DTLS13 */ |
11375 | | |
11376 | | if (HANDSHAKE_HEADER_SZ + msgSz <= maxFrag) { |
11377 | | /* Fits in a single record: the common path used by RSA, ECC, |
11378 | | * EdDSA and ML-DSA is left byte-for-byte unchanged. */ |
11379 | | |
11380 | | /* This message is always encrypted. */ |
11381 | | ret = BuildTls13Message(ssl, args->output, |
11382 | | (int)args->outputSz, |
11383 | | args->output + RECORD_HEADER_SZ, |
11384 | | args->sendSz - RECORD_HEADER_SZ, |
11385 | | handshake, 1, 0, 0); |
11386 | | |
11387 | | if (ret < 0) { |
11388 | | goto exit_scv; |
11389 | | } |
11390 | | else { |
11391 | | args->sendSz = ret; |
11392 | | ret = 0; |
11393 | | } |
11394 | | |
11395 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11396 | | if (ssl->hsInfoOn) |
11397 | | AddPacketName(ssl, "CertificateVerify"); |
11398 | | if (ssl->toInfoOn) { |
11399 | | ret = AddPacketInfo(ssl, "CertificateVerify", handshake, |
11400 | | args->output, args->sendSz, WRITE_PROTO, 0, |
11401 | | ssl->heap); |
11402 | | if (ret != 0) |
11403 | | goto exit_scv; |
11404 | | } |
11405 | | #endif |
11406 | | |
11407 | | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
11408 | | } |
11409 | | else { |
11410 | | /* Signature does not fit in a single TLS record (e.g. SLH-DSA, |
11411 | | * whose signatures range up to ~50KB). Fragment the handshake |
11412 | | * message across multiple encrypted records. Only the first |
11413 | | * fragment carries the 4-byte handshake header; the transcript |
11414 | | * hash is maintained correctly because BuildTls13Message hashes |
11415 | | * each fragment's plaintext in order. |
11416 | | * |
11417 | | * The fragmentation cursor lives in args (frag/fragOffset/ |
11418 | | * fragActive) so that a WC_PENDING_E from the record AEAD under |
11419 | | * WOLFSSL_ASYNC_CRYPT resumes on the same fragment rather than |
11420 | | * restarting at offset 0 and re-emitting committed records. */ |
11421 | | if (maxFrag <= HANDSHAKE_HEADER_SZ) { |
11422 | | ERROR_OUT(BUFFER_E, exit_scv); |
11423 | | } |
11424 | | |
11425 | | /* Copy the assembled body out of the output buffer once, before |
11426 | | * we begin overwriting it with per-record data. args->frag is |
11427 | | * preserved across async resumes and freed by FreeScv13Args. */ |
11428 | | if (args->frag == NULL) { |
11429 | | args->frag = (byte*)XMALLOC(msgSz, ssl->heap, |
11430 | | DYNAMIC_TYPE_TMP_BUFFER); |
11431 | | if (args->frag == NULL) { |
11432 | | ERROR_OUT(MEMORY_E, exit_scv); |
11433 | | } |
11434 | | XMEMCPY(args->frag, args->verify, msgSz); |
11435 | | args->fragOffset = 0; |
11436 | | args->fragActive = 0; |
11437 | | } |
11438 | | |
11439 | | while (args->fragOffset < msgSz && ret == 0) { |
11440 | | if (args->fragOffset == 0) { |
11441 | | fragSz = maxFrag - HANDSHAKE_HEADER_SZ; |
11442 | | if (fragSz > msgSz) |
11443 | | fragSz = msgSz; |
11444 | | thisSendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
11445 | | (int)fragSz + MAX_MSG_EXTRA; |
11446 | | } |
11447 | | else { |
11448 | | fragSz = msgSz - args->fragOffset; |
11449 | | if (fragSz > maxFrag) |
11450 | | fragSz = maxFrag; |
11451 | | thisSendSz = RECORD_HEADER_SZ + (int)fragSz + |
11452 | | MAX_MSG_EXTRA; |
11453 | | } |
11454 | | |
11455 | | if ((ret = CheckAvailableSize(ssl, thisSendSz)) != 0) { |
11456 | | goto exit_scv; |
11457 | | } |
11458 | | output = GetOutputBuffer(ssl); |
11459 | | |
11460 | | i = RECORD_HEADER_SZ; |
11461 | | if (args->fragOffset == 0) |
11462 | | i += HANDSHAKE_HEADER_SZ; |
11463 | | |
11464 | | /* Lay out this fragment's record header and plaintext once. |
11465 | | * On an async resume of a pending fragment they are already |
11466 | | * in place (outputBuffer.length was not advanced), so skip |
11467 | | * straight to re-driving BuildTls13Message. */ |
11468 | | if (!args->fragActive) { |
11469 | | if (args->fragOffset == 0) { |
11470 | | AddTls13FragHeaders(output, fragSz, 0, msgSz, |
11471 | | certificate_verify, ssl); |
11472 | | } |
11473 | | else { |
11474 | | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
11475 | | } |
11476 | | XMEMCPY(output + i, args->frag + args->fragOffset, |
11477 | | fragSz); |
11478 | | args->fragActive = 1; |
11479 | | } |
11480 | | i += fragSz; |
11481 | | |
11482 | | /* This message is always encrypted. */ |
11483 | | recSz = BuildTls13Message(ssl, output, thisSendSz, |
11484 | | output + RECORD_HEADER_SZ, |
11485 | | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
11486 | | if (recSz < 0) { |
11487 | | /* WC_PENDING_E leaves frag/fragOffset/fragActive intact |
11488 | | * for resume; real errors are cleaned up by |
11489 | | * FreeScv13Args at exit_scv. */ |
11490 | | ret = recSz; |
11491 | | goto exit_scv; |
11492 | | } |
11493 | | |
11494 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
11495 | | /* Trace the logical CertificateVerify once (on the first |
11496 | | * fragment), matching the single-record path rather than |
11497 | | * emitting one entry per record. */ |
11498 | | if (args->fragOffset == 0) { |
11499 | | if (ssl->hsInfoOn) |
11500 | | AddPacketName(ssl, "CertificateVerify"); |
11501 | | if (ssl->toInfoOn) { |
11502 | | ret = AddPacketInfo(ssl, "CertificateVerify", |
11503 | | handshake, output, recSz, WRITE_PROTO, 0, |
11504 | | ssl->heap); |
11505 | | if (ret != 0) |
11506 | | goto exit_scv; |
11507 | | } |
11508 | | } |
11509 | | #endif |
11510 | | |
11511 | | ssl->buffers.outputBuffer.length += (word32)recSz; |
11512 | | args->fragOffset += fragSz; |
11513 | | args->fragActive = 0; |
11514 | | } |
11515 | | } |
11516 | | |
11517 | | ssl->options.buildingMsg = 0; |
11518 | | if (!ssl->options.groupMessages) |
11519 | | ret = SendBuffered(ssl); |
11520 | | break; |
11521 | | } |
11522 | | default: |
11523 | | ret = INPUT_CASE_ERROR; |
11524 | | } /* switch(ssl->options.asyncState) */ |
11525 | | |
11526 | | exit_scv: |
11527 | | #ifdef WOLFSSL_TLS13_STREAM_CERT_VERIFY |
11528 | | /* A streamed CertificateVerify keeps its assembled body across a |
11529 | | * non-blocking WANT_WRITE so the send resumes without recomputing the |
11530 | | * signature; release it on completion or on any real error. This path is |
11531 | | * mutually exclusive with WOLFSSL_ASYNC_CRYPT (see the feature guard in |
11532 | | * internal.h), so ret is never WC_PENDING_E here and the buffer needs no |
11533 | | * retention across an async resume. */ |
11534 | | if (ret != WC_NO_ERR_TRACE(WANT_WRITE) && |
11535 | | ssl->buffers.certVerifyMsg.buffer != NULL) { |
11536 | | XFREE(ssl->buffers.certVerifyMsg.buffer, ssl->heap, |
11537 | | DYNAMIC_TYPE_TMP_BUFFER); |
11538 | | ssl->buffers.certVerifyMsg.buffer = NULL; |
11539 | | ssl->buffers.certVerifyMsg.length = 0; |
11540 | | ssl->fragOffset = 0; |
11541 | | } |
11542 | | #endif /* WOLFSSL_TLS13_STREAM_CERT_VERIFY */ |
11543 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
11544 | | if (ret == 0) { |
11545 | | ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
11546 | | &ssl->buffers.keyMask); |
11547 | | } |
11548 | | else { |
11549 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
11550 | | } |
11551 | | #endif |
11552 | | |
11553 | | WOLFSSL_LEAVE("SendTls13CertificateVerify", ret); |
11554 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
11555 | | |
11556 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11557 | | /* Handle async operation */ |
11558 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
11559 | | return ret; |
11560 | | } |
11561 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
11562 | | |
11563 | | /* Final cleanup */ |
11564 | | FreeScv13Args(ssl, args); |
11565 | | FreeKeyExchange(ssl); |
11566 | | #ifdef WOLFSSL_ASYNC_IO |
11567 | | /* Cleanup async */ |
11568 | | FreeAsyncCtx(ssl, 0); |
11569 | | #endif |
11570 | | |
11571 | | if (ret != 0) { |
11572 | | WOLFSSL_ERROR_VERBOSE(ret); |
11573 | | } |
11574 | | |
11575 | | return ret; |
11576 | | } |
11577 | | #endif |
11578 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
11579 | | |
11580 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) |
11581 | | /* handle processing TLS v1.3 certificate (11) */ |
11582 | | /* Parse and handle a TLS v1.3 Certificate message. |
11583 | | * |
11584 | | * ssl The SSL/TLS object. |
11585 | | * input The message buffer. |
11586 | | * inOutIdx On entry, the index into the message buffer of Certificate. |
11587 | | * On exit, the index of byte after the Certificate message. |
11588 | | * totalSz The length of the current handshake message. |
11589 | | * returns 0 on success and otherwise failure. |
11590 | | */ |
11591 | | static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
11592 | | word32 totalSz) |
11593 | 0 | { |
11594 | 0 | int ret = 0; |
11595 | |
|
11596 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_DO); |
11597 | 0 | WOLFSSL_ENTER("DoTls13Certificate"); |
11598 | |
|
11599 | | #ifdef WOLFSSL_DTLS13 |
11600 | | if (ssl->options.dtls && ssl->options.handShakeDone) { |
11601 | | /* certificate needs some special care after the handshake */ |
11602 | | ret = Dtls13RtxProcessingCertificate( |
11603 | | ssl, input + *inOutIdx, totalSz); |
11604 | | } |
11605 | | #endif /* WOLFSSL_DTLS13 */ |
11606 | |
|
11607 | 0 | if (ret == 0) |
11608 | 0 | ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz); |
11609 | 0 | if (ret == 0) { |
11610 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
11611 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11612 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
11613 | 0 | #endif |
11614 | | #if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
11615 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
11616 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
11617 | | /* reset handshake states */ |
11618 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11619 | | ssl->options.acceptState = TICKET_SENT; |
11620 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
11621 | | } |
11622 | | #endif |
11623 | 0 | } |
11624 | |
|
11625 | 0 | WOLFSSL_LEAVE("DoTls13Certificate", ret); |
11626 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_DO); |
11627 | |
|
11628 | 0 | return ret; |
11629 | 0 | } |
11630 | | #endif |
11631 | | |
11632 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
11633 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
11634 | | defined(WOLFSSL_HAVE_MLDSA) || defined(WOLFSSL_HAVE_SLHDSA)) && \ |
11635 | | !defined(NO_CERTS) |
11636 | | |
11637 | | typedef struct Dcv13Args { |
11638 | | byte* output; /* not allocated */ |
11639 | | word32 sendSz; |
11640 | | word16 sz; |
11641 | | word32 sigSz; |
11642 | | word32 idx; |
11643 | | word32 begin; |
11644 | | |
11645 | | byte* sigData; |
11646 | | word16 sigDataSz; |
11647 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11648 | | byte altSigAlgo; |
11649 | | byte* altSigData; |
11650 | | word32 altSigDataSz; |
11651 | | word32 altSignatureSz; |
11652 | | byte altPeerAuthGood; |
11653 | | #endif |
11654 | | } Dcv13Args; |
11655 | | |
11656 | | static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs) |
11657 | 0 | { |
11658 | 0 | Dcv13Args* args = (Dcv13Args*)pArgs; |
11659 | |
|
11660 | 0 | if (args && args->sigData != NULL) { |
11661 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
11662 | 0 | args->sigData = NULL; |
11663 | 0 | } |
11664 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11665 | | if (args && args->altSigData != NULL) { |
11666 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
11667 | | args->altSigData = NULL; |
11668 | | } |
11669 | | #endif |
11670 | 0 | (void)ssl; |
11671 | 0 | } |
11672 | | |
11673 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11674 | | #ifndef NO_RSA |
11675 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
11676 | | * RSA public key. Convert it into a usable public key. */ |
11677 | | static int decodeRsaKey(WOLFSSL* ssl) |
11678 | | { |
11679 | | int keyRet; |
11680 | | word32 tmpIdx = 0; |
11681 | | |
11682 | | if (ssl->peerRsaKeyPresent) |
11683 | | return INVALID_PARAMETER; |
11684 | | |
11685 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
11686 | | if (keyRet != 0) |
11687 | | return PEER_KEY_ERROR; |
11688 | | |
11689 | | ssl->peerRsaKeyPresent = 1; |
11690 | | keyRet = wc_RsaPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
11691 | | ssl->peerRsaKey, |
11692 | | ssl->peerCert.sapkiLen); |
11693 | | if (keyRet != 0) |
11694 | | return PEER_KEY_ERROR; |
11695 | | |
11696 | | return 0; |
11697 | | } |
11698 | | #endif /* !NO_RSA */ |
11699 | | |
11700 | | #ifdef HAVE_ECC |
11701 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
11702 | | * ECC public key. Convert it into a usable public key. */ |
11703 | | static int decodeEccKey(WOLFSSL* ssl) |
11704 | | { |
11705 | | int keyRet; |
11706 | | word32 tmpIdx = 0; |
11707 | | |
11708 | | if (ssl->peerEccDsaKeyPresent) |
11709 | | return INVALID_PARAMETER; |
11710 | | |
11711 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
11712 | | if (keyRet != 0) |
11713 | | return PEER_KEY_ERROR; |
11714 | | |
11715 | | ssl->peerEccDsaKeyPresent = 1; |
11716 | | keyRet = wc_EccPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
11717 | | ssl->peerEccDsaKey, |
11718 | | ssl->peerCert.sapkiLen); |
11719 | | if (keyRet != 0) |
11720 | | return PEER_KEY_ERROR; |
11721 | | |
11722 | | return 0; |
11723 | | } |
11724 | | #endif /* HAVE_ECC */ |
11725 | | |
11726 | | #ifdef WOLFSSL_HAVE_MLDSA |
11727 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
11728 | | * ML-DSA public key. Convert it into a usable public key. */ |
11729 | | static int decodeMlDsaKey(WOLFSSL* ssl, int level) |
11730 | | { |
11731 | | int keyRet; |
11732 | | word32 tmpIdx = 0; |
11733 | | |
11734 | | if (ssl->peerMlDsaKeyPresent) |
11735 | | return INVALID_PARAMETER; |
11736 | | |
11737 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_MLDSA, |
11738 | | (void**)&ssl->peerMlDsaKey); |
11739 | | if (keyRet != 0) |
11740 | | return PEER_KEY_ERROR; |
11741 | | |
11742 | | ssl->peerMlDsaKeyPresent = 1; |
11743 | | keyRet = wc_MlDsaKey_SetParams(ssl->peerMlDsaKey, level); |
11744 | | if (keyRet != 0) |
11745 | | return PEER_KEY_ERROR; |
11746 | | |
11747 | | keyRet = wc_MlDsaKey_PublicKeyDecode(ssl->peerMlDsaKey, |
11748 | | ssl->peerCert.sapkiDer, |
11749 | | ssl->peerCert.sapkiLen, &tmpIdx); |
11750 | | if (keyRet != 0) |
11751 | | return PEER_KEY_ERROR; |
11752 | | |
11753 | | return 0; |
11754 | | } |
11755 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
11756 | | |
11757 | | #ifdef HAVE_FALCON |
11758 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
11759 | | * falcon public key. Convert it into a usable public key. */ |
11760 | | static int decodeFalconKey(WOLFSSL* ssl, int level) |
11761 | | { |
11762 | | int keyRet; |
11763 | | word32 tmpIdx = 0; |
11764 | | |
11765 | | if (ssl->peerFalconKeyPresent) |
11766 | | return INVALID_PARAMETER; |
11767 | | |
11768 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey); |
11769 | | if (keyRet != 0) |
11770 | | return PEER_KEY_ERROR; |
11771 | | |
11772 | | ssl->peerFalconKeyPresent = 1; |
11773 | | keyRet = wc_falcon_set_level(ssl->peerFalconKey, level); |
11774 | | if (keyRet != 0) |
11775 | | return PEER_KEY_ERROR; |
11776 | | |
11777 | | keyRet = wc_Falcon_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
11778 | | ssl->peerFalconKey, |
11779 | | ssl->peerCert.sapkiLen); |
11780 | | if (keyRet != 0) |
11781 | | return PEER_KEY_ERROR; |
11782 | | |
11783 | | return 0; |
11784 | | } |
11785 | | #endif /* HAVE_FALCON */ |
11786 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11787 | | |
11788 | | /* handle processing TLS v1.3 certificate_verify (15) */ |
11789 | | /* Parse and handle a TLS v1.3 CertificateVerify message. |
11790 | | * |
11791 | | * ssl The SSL/TLS object. |
11792 | | * input The message buffer. |
11793 | | * inOutIdx On entry, the index into the message buffer of |
11794 | | * CertificateVerify. |
11795 | | * On exit, the index of byte after the CertificateVerify message. |
11796 | | * totalSz The length of the current handshake message. |
11797 | | * returns 0 on success and otherwise failure. |
11798 | | */ |
11799 | | static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input, |
11800 | | word32* inOutIdx, word32 totalSz) |
11801 | 0 | { |
11802 | 0 | int ret = 0; |
11803 | 0 | byte* sig = NULL; |
11804 | 0 | #ifndef NO_RSA |
11805 | | /* Use this as a temporary buffer for RSA signature verification. */ |
11806 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
11807 | 0 | #endif |
11808 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11809 | | Dcv13Args* args = NULL; |
11810 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
11811 | | #else |
11812 | 0 | Dcv13Args args[1]; |
11813 | 0 | #endif |
11814 | |
|
11815 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO); |
11816 | 0 | WOLFSSL_ENTER("DoTls13CertificateVerify"); |
11817 | |
|
11818 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11819 | | ret = tsip_Tls13CertificateVerify(ssl, input, inOutIdx, totalSz); |
11820 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11821 | | goto exit_dcv; |
11822 | | } |
11823 | | ret = 0; |
11824 | | #endif |
11825 | |
|
11826 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11827 | | if (ssl->async == NULL) { |
11828 | | ssl->async = (struct WOLFSSL_ASYNC*) |
11829 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
11830 | | DYNAMIC_TYPE_ASYNC); |
11831 | | if (ssl->async == NULL) |
11832 | | ERROR_OUT(MEMORY_E, exit_dcv); |
11833 | | } |
11834 | | args = (Dcv13Args*)ssl->async->args; |
11835 | | |
11836 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
11837 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
11838 | | /* Check for error */ |
11839 | | if (ret < 0) |
11840 | | goto exit_dcv; |
11841 | | } |
11842 | | else |
11843 | | #endif |
11844 | 0 | { |
11845 | | /* Reset state */ |
11846 | 0 | ret = 0; |
11847 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
11848 | 0 | XMEMSET(args, 0, sizeof(Dcv13Args)); |
11849 | 0 | ssl->options.peerHashAlgo = sha_mac; |
11850 | 0 | ssl->options.peerSigAlgo = anonymous_sa_algo; |
11851 | 0 | args->idx = *inOutIdx; |
11852 | 0 | args->begin = *inOutIdx; |
11853 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11854 | | ssl->async->freeArgs = FreeDcv13Args; |
11855 | | #endif |
11856 | 0 | } |
11857 | |
|
11858 | 0 | switch(ssl->options.asyncState) |
11859 | 0 | { |
11860 | 0 | case TLS_ASYNC_BEGIN: |
11861 | 0 | { |
11862 | | #ifdef WOLFSSL_CALLBACKS |
11863 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify"); |
11864 | | if (ssl->toInfoOn) AddLateName("CertificateVerify", |
11865 | | &ssl->timeoutInfo); |
11866 | | #endif |
11867 | | |
11868 | | /* Advance state and proceed */ |
11869 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
11870 | 0 | } /* case TLS_ASYNC_BEGIN */ |
11871 | 0 | FALL_THROUGH; |
11872 | |
|
11873 | 0 | case TLS_ASYNC_BUILD: |
11874 | 0 | { |
11875 | 0 | int validSigAlgo; |
11876 | 0 | const Suites* suites = WOLFSSL_SUITES(ssl); |
11877 | 0 | word16 i; |
11878 | | |
11879 | | /* Signature algorithm. */ |
11880 | 0 | if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) { |
11881 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11882 | 0 | } |
11883 | | |
11884 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11885 | | if (ssl->peerSigSpec == NULL) { |
11886 | | /* The peer did not respond. We didn't send CKS or they don't |
11887 | | * support it. Either way, we do not need to handle dual |
11888 | | * key/sig case. */ |
11889 | | ssl->sigSpec = NULL; |
11890 | | ssl->sigSpecSz = 0; |
11891 | | } |
11892 | | |
11893 | | /* If no CKS extension or either native or alternative, then just |
11894 | | * get a normal sigalgo. But if BOTH, then get the native and alt |
11895 | | * sig algos. */ |
11896 | | if (ssl->sigSpec == NULL || |
11897 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_NATIVE || |
11898 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
11899 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11900 | 0 | validSigAlgo = 0; |
11901 | 0 | for (i = 0; i < suites->hashSigAlgoSz; i += 2) { |
11902 | 0 | if ((suites->hashSigAlgo[i + 0] == input[args->idx + 0]) && |
11903 | 0 | (suites->hashSigAlgo[i + 1] == input[args->idx + 1])) { |
11904 | 0 | validSigAlgo = 1; |
11905 | 0 | break; |
11906 | 0 | } |
11907 | 0 | } |
11908 | 0 | if (!validSigAlgo) { |
11909 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
11910 | 0 | } |
11911 | | |
11912 | 0 | ret = DecodeTls13SigAlg(input + args->idx, |
11913 | 0 | &ssl->options.peerHashAlgo, &ssl->options.peerSigAlgo); |
11914 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11915 | | } |
11916 | | else { |
11917 | | ret = DecodeTls13HybridSigAlg(input + args->idx, |
11918 | | &ssl->options.peerHashAlgo, |
11919 | | &ssl->options.peerSigAlgo, |
11920 | | &args->altSigAlgo); |
11921 | | } |
11922 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11923 | |
|
11924 | 0 | if (ret < 0) |
11925 | 0 | goto exit_dcv; |
11926 | 0 | args->idx += OPAQUE16_LEN; |
11927 | | |
11928 | | /* Signature length. */ |
11929 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) { |
11930 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11931 | 0 | } |
11932 | 0 | ato16(input + args->idx, &args->sz); |
11933 | 0 | args->idx += OPAQUE16_LEN; |
11934 | | |
11935 | | /* Signature data. */ |
11936 | 0 | if ((args->idx - args->begin) + args->sz > totalSz) { |
11937 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11938 | 0 | } |
11939 | | |
11940 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11941 | | if ((ssl->sigSpec != NULL) && |
11942 | | (*ssl->sigSpec != WOLFSSL_CKS_SIGSPEC_NATIVE)) { |
11943 | | |
11944 | | word16 sa; |
11945 | | if (args->altSigAlgo == 0) |
11946 | | sa = ssl->options.peerSigAlgo; |
11947 | | else |
11948 | | sa = args->altSigAlgo; |
11949 | | |
11950 | | switch(sa) { |
11951 | | #ifndef NO_RSA |
11952 | | case rsa_pss_sa_algo: |
11953 | | ret = decodeRsaKey(ssl); |
11954 | | break; |
11955 | | #endif |
11956 | | #ifdef HAVE_ECC |
11957 | | case ecc_dsa_sa_algo: |
11958 | | ret = decodeEccKey(ssl); |
11959 | | break; |
11960 | | #endif |
11961 | | #ifdef WOLFSSL_HAVE_MLDSA |
11962 | | case mldsa_44_sa_algo: |
11963 | | ret = decodeMlDsaKey(ssl, WC_ML_DSA_44); |
11964 | | break; |
11965 | | case mldsa_65_sa_algo: |
11966 | | ret = decodeMlDsaKey(ssl, WC_ML_DSA_65); |
11967 | | break; |
11968 | | case mldsa_87_sa_algo: |
11969 | | ret = decodeMlDsaKey(ssl, WC_ML_DSA_87); |
11970 | | break; |
11971 | | #endif |
11972 | | #ifdef WOLFSSL_HAVE_SLHDSA |
11973 | | case slhdsa_sha2_128s_sa_algo: |
11974 | | case slhdsa_sha2_128f_sa_algo: |
11975 | | case slhdsa_sha2_192s_sa_algo: |
11976 | | case slhdsa_sha2_192f_sa_algo: |
11977 | | case slhdsa_sha2_256s_sa_algo: |
11978 | | case slhdsa_sha2_256f_sa_algo: |
11979 | | case slhdsa_shake_128s_sa_algo: |
11980 | | case slhdsa_shake_128f_sa_algo: |
11981 | | case slhdsa_shake_192s_sa_algo: |
11982 | | case slhdsa_shake_192f_sa_algo: |
11983 | | case slhdsa_shake_256s_sa_algo: |
11984 | | case slhdsa_shake_256f_sa_algo: |
11985 | | /* SLH-DSA is not supported as an alternative (dual-algorithm |
11986 | | * / CKS) key. The alternative-signature verification block |
11987 | | * in this function has no SLH-DSA case, so a decoded key |
11988 | | * would never be verified and the handshake would always be |
11989 | | * rejected at TLS_ASYNC_FINALIZE. Fail fast here instead of |
11990 | | * allocating a key that can never authenticate the peer. */ |
11991 | | ERROR_OUT(ALGO_ID_E, exit_dcv); |
11992 | | #endif |
11993 | | #ifdef HAVE_FALCON |
11994 | | case falcon_level1_sa_algo: |
11995 | | ret = decodeFalconKey(ssl, 1); |
11996 | | break; |
11997 | | case falcon_level5_sa_algo: |
11998 | | ret = decodeFalconKey(ssl, 5); |
11999 | | break; |
12000 | | #endif |
12001 | | default: |
12002 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
12003 | | } |
12004 | | |
12005 | | if (ret != 0) |
12006 | | ERROR_OUT(ret, exit_dcv); |
12007 | | |
12008 | | if (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
12009 | | /* Now swap in the alternative by removing the native. |
12010 | | * sa contains the alternative signature type. */ |
12011 | | #ifndef NO_RSA |
12012 | | if (ssl->peerRsaKeyPresent && sa != rsa_pss_sa_algo) { |
12013 | | FreeKey(ssl, DYNAMIC_TYPE_RSA, |
12014 | | (void**)&ssl->peerRsaKey); |
12015 | | ssl->peerRsaKeyPresent = 0; |
12016 | | } |
12017 | | #endif |
12018 | | #ifdef HAVE_ECC |
12019 | | else if (ssl->peerEccDsaKeyPresent && |
12020 | | sa != ecc_dsa_sa_algo) { |
12021 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
12022 | | (void**)&ssl->peerEccDsaKey); |
12023 | | ssl->peerEccDsaKeyPresent = 0; |
12024 | | } |
12025 | | #endif |
12026 | | #ifdef WOLFSSL_HAVE_MLDSA |
12027 | | else if (ssl->peerMlDsaKeyPresent && |
12028 | | sa != mldsa_44_sa_algo && |
12029 | | sa != mldsa_65_sa_algo && |
12030 | | sa != mldsa_87_sa_algo) { |
12031 | | FreeKey(ssl, DYNAMIC_TYPE_MLDSA, |
12032 | | (void**)&ssl->peerMlDsaKey); |
12033 | | ssl->peerMlDsaKeyPresent = 0; |
12034 | | } |
12035 | | #endif |
12036 | | #ifdef WOLFSSL_HAVE_SLHDSA |
12037 | | else if (ssl->peerSlhDsaKeyPresent && |
12038 | | !IsSlhDsaSigAlgo(sa)) { |
12039 | | FreeKey(ssl, DYNAMIC_TYPE_SLHDSA, |
12040 | | (void**)&ssl->peerSlhDsaKey); |
12041 | | ssl->peerSlhDsaKeyPresent = 0; |
12042 | | } |
12043 | | #endif |
12044 | | #ifdef HAVE_FALCON |
12045 | | else if (ssl->peerFalconKeyPresent && |
12046 | | sa != falcon_level1_sa_algo && |
12047 | | sa != falcon_level5_sa_algo) { |
12048 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
12049 | | (void**)&ssl->peerFalconKey); |
12050 | | ssl->peerFalconKeyPresent = 0; |
12051 | | } |
12052 | | #endif |
12053 | | else { |
12054 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
12055 | | } |
12056 | | } |
12057 | | } |
12058 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12059 | | |
12060 | | /* Check for public key of required type. */ |
12061 | | /* Assume invalid unless signature algo matches the key provided */ |
12062 | 0 | validSigAlgo = 0; |
12063 | 0 | #ifdef HAVE_ED25519 |
12064 | 0 | if (ssl->options.peerSigAlgo == ed25519_sa_algo) { |
12065 | 0 | WOLFSSL_MSG("Peer sent ED25519 sig"); |
12066 | 0 | validSigAlgo = (ssl->peerEd25519Key != NULL) && |
12067 | 0 | ssl->peerEd25519KeyPresent; |
12068 | 0 | } |
12069 | 0 | #endif |
12070 | 0 | #ifdef HAVE_ED448 |
12071 | 0 | if (ssl->options.peerSigAlgo == ed448_sa_algo) { |
12072 | 0 | WOLFSSL_MSG("Peer sent ED448 sig"); |
12073 | 0 | validSigAlgo = (ssl->peerEd448Key != NULL) && |
12074 | 0 | ssl->peerEd448KeyPresent; |
12075 | 0 | } |
12076 | 0 | #endif |
12077 | 0 | #ifdef HAVE_ECC |
12078 | 0 | if (ssl->options.peerSigAlgo == ecc_dsa_sa_algo) { |
12079 | 0 | WOLFSSL_MSG("Peer sent ECC sig"); |
12080 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
12081 | 0 | ssl->peerEccDsaKeyPresent; |
12082 | 0 | } |
12083 | 0 | #endif |
12084 | 0 | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
12085 | 0 | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
12086 | 0 | WOLFSSL_MSG("Peer sent SM2 sig"); |
12087 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
12088 | 0 | ssl->peerEccDsaKeyPresent; |
12089 | 0 | } |
12090 | 0 | #endif |
12091 | | #ifdef HAVE_FALCON |
12092 | | if (ssl->options.peerSigAlgo == falcon_level1_sa_algo) { |
12093 | | WOLFSSL_MSG("Peer sent Falcon Level 1 sig"); |
12094 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
12095 | | ssl->peerFalconKeyPresent; |
12096 | | } |
12097 | | if (ssl->options.peerSigAlgo == falcon_level5_sa_algo) { |
12098 | | WOLFSSL_MSG("Peer sent Falcon Level 5 sig"); |
12099 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
12100 | | ssl->peerFalconKeyPresent; |
12101 | | } |
12102 | | #endif |
12103 | | #ifdef WOLFSSL_HAVE_MLDSA |
12104 | | if (ssl->options.peerSigAlgo == mldsa_44_sa_algo) { |
12105 | | WOLFSSL_MSG("Peer sent ML-DSA Level 2 sig"); |
12106 | | validSigAlgo = (ssl->peerMlDsaKey != NULL) && |
12107 | | ssl->peerMlDsaKeyPresent; |
12108 | | } |
12109 | | if (ssl->options.peerSigAlgo == mldsa_65_sa_algo) { |
12110 | | WOLFSSL_MSG("Peer sent ML-DSA Level 3 sig"); |
12111 | | validSigAlgo = (ssl->peerMlDsaKey != NULL) && |
12112 | | ssl->peerMlDsaKeyPresent; |
12113 | | } |
12114 | | if (ssl->options.peerSigAlgo == mldsa_87_sa_algo) { |
12115 | | WOLFSSL_MSG("Peer sent ML-DSA Level 5 sig"); |
12116 | | validSigAlgo = (ssl->peerMlDsaKey != NULL) && |
12117 | | ssl->peerMlDsaKeyPresent; |
12118 | | } |
12119 | | #endif |
12120 | | #ifdef WOLFSSL_HAVE_SLHDSA |
12121 | | if (IsSlhDsaSigAlgo(ssl->options.peerSigAlgo)) { |
12122 | | WOLFSSL_MSG("Peer sent SLH-DSA sig"); |
12123 | | validSigAlgo = (ssl->peerSlhDsaKey != NULL) && |
12124 | | ssl->peerSlhDsaKeyPresent; |
12125 | | } |
12126 | | #endif |
12127 | 0 | #ifndef NO_RSA |
12128 | 0 | if (ssl->options.peerSigAlgo == rsa_sa_algo) { |
12129 | 0 | WOLFSSL_MSG("Peer sent PKCS#1.5 algo - not valid TLS 1.3"); |
12130 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
12131 | 0 | } |
12132 | 0 | if (ssl->options.peerSigAlgo == rsa_pss_sa_algo) { |
12133 | 0 | WOLFSSL_MSG("Peer sent RSA sig"); |
12134 | 0 | validSigAlgo = (ssl->peerRsaKey != NULL) && |
12135 | 0 | ssl->peerRsaKeyPresent; |
12136 | 0 | } |
12137 | 0 | #endif |
12138 | 0 | if (!validSigAlgo) { |
12139 | 0 | WOLFSSL_MSG("Sig algo doesn't correspond to certificate"); |
12140 | 0 | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
12141 | 0 | } |
12142 | | |
12143 | 0 | args->sigSz = args->sz; |
12144 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12145 | | if (ssl->sigSpec != NULL && |
12146 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
12147 | | /* In case we received two signatures, both of them are encoded |
12148 | | * with their size as 16-bit integeter prior in memory. Hence, |
12149 | | * we can decode both lengths here now. */ |
12150 | | word32 tmpIdx = args->idx; |
12151 | | word16 tmpSz = 0; |
12152 | | if (args->sz < OPAQUE16_LEN) { |
12153 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
12154 | | } |
12155 | | ato16(input + tmpIdx, &tmpSz); |
12156 | | args->sigSz = tmpSz; |
12157 | | |
12158 | | tmpIdx += OPAQUE16_LEN + args->sigSz; |
12159 | | if (tmpIdx - args->idx + OPAQUE16_LEN > args->sz) { |
12160 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
12161 | | } |
12162 | | ato16(input + tmpIdx, &tmpSz); |
12163 | | args->altSignatureSz = tmpSz; |
12164 | | |
12165 | | if (args->sz != (args->sigSz + args->altSignatureSz + |
12166 | | OPAQUE16_LEN + OPAQUE16_LEN)) { |
12167 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
12168 | | } |
12169 | | } |
12170 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12171 | |
|
12172 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
12173 | | /* In case we have to verify an RSA signature, we have to store the |
12174 | | * signature in the 'rsaSigBuf' structure for further processing. |
12175 | | */ |
12176 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
12177 | 0 | word32 sigSz = args->sigSz; |
12178 | 0 | sig = input + args->idx; |
12179 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12180 | | /* Check if our alternative signature was RSA */ |
12181 | | if (ssl->sigSpec != NULL && |
12182 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
12183 | | if (ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
12184 | | /* We have to skip the first signature (length field |
12185 | | * and signature itself) and the length field of the |
12186 | | * alternative signature. */ |
12187 | | sig += OPAQUE16_LEN + OPAQUE16_LEN + args->sigSz; |
12188 | | sigSz = args->altSignatureSz; |
12189 | | } |
12190 | | else { |
12191 | | /* We have to skip the length field */ |
12192 | | sig += OPAQUE16_LEN; |
12193 | | } |
12194 | | } |
12195 | | #endif |
12196 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(sigSz, ssl->heap, |
12197 | 0 | DYNAMIC_TYPE_SIGNATURE); |
12198 | 0 | if (rsaSigBuf->buffer == NULL) { |
12199 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
12200 | 0 | } |
12201 | 0 | rsaSigBuf->length = sigSz; |
12202 | 0 | XMEMCPY(rsaSigBuf->buffer, sig, rsaSigBuf->length); |
12203 | 0 | } |
12204 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
12205 | | |
12206 | 0 | args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
12207 | 0 | DYNAMIC_TYPE_SIGNATURE); |
12208 | 0 | if (args->sigData == NULL) { |
12209 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
12210 | 0 | } |
12211 | | |
12212 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1); |
12213 | 0 | if (ret < 0) |
12214 | 0 | goto exit_dcv; |
12215 | | |
12216 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12217 | | if ((ssl->sigSpec != NULL) && |
12218 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
12219 | | args->altSigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
12220 | | DYNAMIC_TYPE_SIGNATURE); |
12221 | | if (args->altSigData == NULL) { |
12222 | | ERROR_OUT(MEMORY_E, exit_dcv); |
12223 | | } |
12224 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
12225 | | args->altSigDataSz = args->sigDataSz; |
12226 | | } |
12227 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12228 | | |
12229 | 0 | #ifdef HAVE_ECC |
12230 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
12231 | 0 | (ssl->peerEccDsaKeyPresent)) { |
12232 | 0 | ret = CreateECCEncodedSig(args->sigData, |
12233 | 0 | args->sigDataSz, ssl->options.peerHashAlgo); |
12234 | 0 | if (ret < 0) |
12235 | 0 | goto exit_dcv; |
12236 | 0 | args->sigDataSz = (word16)ret; |
12237 | 0 | ret = 0; |
12238 | 0 | } |
12239 | | |
12240 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12241 | | if ((ssl->sigSpec != NULL) && |
12242 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
12243 | | (args->altSigAlgo == ecc_dsa_sa_algo) && |
12244 | | (ssl->peerEccDsaKeyPresent)) { |
12245 | | ret = CreateECCEncodedSig(args->altSigData, |
12246 | | args->altSigDataSz, ssl->options.peerHashAlgo); |
12247 | | if (ret < 0) |
12248 | | goto exit_dcv; |
12249 | | args->altSigDataSz = (word16)ret; |
12250 | | ret = 0; |
12251 | | } |
12252 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12253 | 0 | #endif /* HAVE_ECC */ |
12254 | | |
12255 | | /* Advance state and proceed */ |
12256 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
12257 | 0 | } /* case TLS_ASYNC_BUILD */ |
12258 | 0 | FALL_THROUGH; |
12259 | |
|
12260 | 0 | case TLS_ASYNC_DO: |
12261 | 0 | { |
12262 | 0 | sig = input + args->idx; |
12263 | 0 | (void)sig; |
12264 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12265 | | if (ssl->sigSpec != NULL && |
12266 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
12267 | | /* As we have two signatures in the message, we stored |
12268 | | * the length of each before the actual signature. This |
12269 | | * is necessary, as we could have two algorithms with |
12270 | | * variable length signatures. */ |
12271 | | sig += OPAQUE16_LEN; |
12272 | | } |
12273 | | #endif |
12274 | 0 | #ifndef NO_RSA |
12275 | 0 | if ((ssl->options.peerSigAlgo == rsa_pss_sa_algo) && |
12276 | 0 | (ssl->peerRsaKey != NULL) && (ssl->peerRsaKeyPresent != 0)) { |
12277 | 0 | WOLFSSL_MSG("Doing RSA peer cert verify"); |
12278 | 0 | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
12279 | 0 | (word32)rsaSigBuf->length, &args->output, |
12280 | 0 | ssl->options.peerSigAlgo, |
12281 | 0 | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
12282 | | #ifdef HAVE_PK_CALLBACKS |
12283 | | &ssl->buffers.peerRsaKey |
12284 | | #else |
12285 | 0 | NULL |
12286 | 0 | #endif |
12287 | 0 | ); |
12288 | 0 | if (ret >= 0) { |
12289 | 0 | args->sendSz = (word32)ret; |
12290 | 0 | ret = 0; |
12291 | 0 | } |
12292 | 0 | } |
12293 | 0 | #endif /* !NO_RSA */ |
12294 | 0 | #ifdef HAVE_ECC |
12295 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
12296 | 0 | ssl->peerEccDsaKeyPresent) { |
12297 | 0 | WOLFSSL_MSG("Doing ECC peer cert verify"); |
12298 | 0 | ret = EccVerify(ssl, sig, args->sigSz, |
12299 | 0 | args->sigData, args->sigDataSz, |
12300 | 0 | ssl->peerEccDsaKey, |
12301 | | #ifdef HAVE_PK_CALLBACKS |
12302 | | &ssl->buffers.peerEccDsaKey |
12303 | | #else |
12304 | 0 | NULL |
12305 | 0 | #endif |
12306 | 0 | ); |
12307 | |
|
12308 | 0 | if (ret >= 0) { |
12309 | | /* CLIENT/SERVER: data verified with public key from |
12310 | | * certificate. */ |
12311 | 0 | ssl->options.peerAuthGood = 1; |
12312 | |
|
12313 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
12314 | 0 | ssl->peerEccDsaKeyPresent = 0; |
12315 | 0 | } |
12316 | 0 | } |
12317 | 0 | #endif /* HAVE_ECC */ |
12318 | 0 | #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
12319 | 0 | if ((ssl->options.peerSigAlgo == sm2_sa_algo) && |
12320 | 0 | ssl->peerEccDsaKeyPresent) { |
12321 | 0 | WOLFSSL_MSG("Doing SM2/SM3 peer cert verify"); |
12322 | 0 | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, TLS13_SM2_SIG_ID_SZ, |
12323 | 0 | sig, args->sigSz, args->sigData, args->sigDataSz, |
12324 | 0 | ssl->peerEccDsaKey, NULL); |
12325 | 0 | if (ret >= 0) { |
12326 | | /* CLIENT/SERVER: data verified with public key from |
12327 | | * certificate. */ |
12328 | 0 | ssl->options.peerAuthGood = 1; |
12329 | |
|
12330 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
12331 | 0 | ssl->peerEccDsaKeyPresent = 0; |
12332 | 0 | } |
12333 | 0 | } |
12334 | 0 | #endif |
12335 | 0 | #ifdef HAVE_ED25519 |
12336 | 0 | if ((ssl->options.peerSigAlgo == ed25519_sa_algo) && |
12337 | 0 | (ssl->peerEd25519KeyPresent)) { |
12338 | 0 | WOLFSSL_MSG("Doing ED25519 peer cert verify"); |
12339 | 0 | ret = Ed25519Verify(ssl, sig, args->sigSz, |
12340 | 0 | args->sigData, args->sigDataSz, |
12341 | 0 | ssl->peerEd25519Key, |
12342 | | #ifdef HAVE_PK_CALLBACKS |
12343 | | &ssl->buffers.peerEd25519Key |
12344 | | #else |
12345 | 0 | NULL |
12346 | 0 | #endif |
12347 | 0 | ); |
12348 | |
|
12349 | 0 | if (ret >= 0) { |
12350 | | /* CLIENT/SERVER: data verified with public key from |
12351 | | * certificate. */ |
12352 | 0 | ssl->options.peerAuthGood = 1; |
12353 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED25519, |
12354 | 0 | (void**)&ssl->peerEd25519Key); |
12355 | 0 | ssl->peerEd25519KeyPresent = 0; |
12356 | 0 | } |
12357 | 0 | } |
12358 | 0 | #endif |
12359 | 0 | #ifdef HAVE_ED448 |
12360 | 0 | if ((ssl->options.peerSigAlgo == ed448_sa_algo) && |
12361 | 0 | (ssl->peerEd448KeyPresent)) { |
12362 | 0 | WOLFSSL_MSG("Doing ED448 peer cert verify"); |
12363 | 0 | ret = Ed448Verify(ssl, sig, args->sigSz, |
12364 | 0 | args->sigData, args->sigDataSz, |
12365 | 0 | ssl->peerEd448Key, |
12366 | | #ifdef HAVE_PK_CALLBACKS |
12367 | | &ssl->buffers.peerEd448Key |
12368 | | #else |
12369 | 0 | NULL |
12370 | 0 | #endif |
12371 | 0 | ); |
12372 | |
|
12373 | 0 | if (ret >= 0) { |
12374 | | /* CLIENT/SERVER: data verified with public key from |
12375 | | * certificate. */ |
12376 | 0 | ssl->options.peerAuthGood = 1; |
12377 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ED448, |
12378 | 0 | (void**)&ssl->peerEd448Key); |
12379 | 0 | ssl->peerEd448KeyPresent = 0; |
12380 | 0 | } |
12381 | 0 | } |
12382 | 0 | #endif |
12383 | | #if defined(HAVE_FALCON) |
12384 | | if (((ssl->options.peerSigAlgo == falcon_level1_sa_algo) || |
12385 | | (ssl->options.peerSigAlgo == falcon_level5_sa_algo)) && |
12386 | | (ssl->peerFalconKeyPresent)) { |
12387 | | int res = 0; |
12388 | | WOLFSSL_MSG("Doing Falcon peer cert verify"); |
12389 | | ret = wc_falcon_verify_msg(sig, args->sigSz, |
12390 | | args->sigData, args->sigDataSz, |
12391 | | &res, ssl->peerFalconKey); |
12392 | | |
12393 | | if ((ret >= 0) && (res == 1)) { |
12394 | | /* CLIENT/SERVER: data verified with public key from |
12395 | | * certificate. */ |
12396 | | ssl->options.peerAuthGood = 1; |
12397 | | |
12398 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
12399 | | (void**)&ssl->peerFalconKey); |
12400 | | ssl->peerFalconKeyPresent = 0; |
12401 | | } |
12402 | | else if ((ret >= 0) && (res == 0)) { |
12403 | | WOLFSSL_MSG("Falcon signature verification failed"); |
12404 | | ret = SIG_VERIFY_E; |
12405 | | } |
12406 | | } |
12407 | | #endif /* HAVE_FALCON */ |
12408 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_VERIFY) |
12409 | | if (((ssl->options.peerSigAlgo == mldsa_44_sa_algo) || |
12410 | | (ssl->options.peerSigAlgo == mldsa_65_sa_algo) || |
12411 | | (ssl->options.peerSigAlgo == mldsa_87_sa_algo)) && |
12412 | | (ssl->peerMlDsaKeyPresent)) { |
12413 | | int res = 0; |
12414 | | WOLFSSL_MSG("Doing ML-DSA peer cert verify"); |
12415 | | ret = wc_MlDsaKey_VerifyCtx(ssl->peerMlDsaKey, sig, args->sigSz, |
12416 | | NULL, 0, args->sigData, |
12417 | | args->sigDataSz, &res); |
12418 | | |
12419 | | if ((ret >= 0) && (res == 1)) { |
12420 | | /* CLIENT/SERVER: data verified with public key from |
12421 | | * certificate. */ |
12422 | | ssl->options.peerAuthGood = 1; |
12423 | | |
12424 | | FreeKey(ssl, DYNAMIC_TYPE_MLDSA, |
12425 | | (void**)&ssl->peerMlDsaKey); |
12426 | | ssl->peerMlDsaKeyPresent = 0; |
12427 | | } |
12428 | | else if ((ret >= 0) && (res == 0)) { |
12429 | | WOLFSSL_MSG("ML-DSA signature verification failed"); |
12430 | | ret = SIG_VERIFY_E; |
12431 | | } |
12432 | | } |
12433 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
12434 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
12435 | | if (IsSlhDsaSigAlgo(ssl->options.peerSigAlgo) && |
12436 | | (ssl->peerSlhDsaKeyPresent)) { |
12437 | | WOLFSSL_MSG("Doing SLH-DSA peer cert verify"); |
12438 | | |
12439 | | /* The advertised signature scheme must match the parameter set |
12440 | | * of the peer's certificate key (RFC 8446 4.4.3). The key |
12441 | | * params come from the certificate OID, so a mismatch means the |
12442 | | * peer is over-claiming its security level; reject it. */ |
12443 | | if ((ssl->peerSlhDsaKey->params == NULL) || |
12444 | | (SlhDsaParamToType((int)ssl->peerSlhDsaKey->params->param) |
12445 | | != ssl->options.peerSigAlgo)) { |
12446 | | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
12447 | | } |
12448 | | |
12449 | | /* wc_SlhDsaKey_Verify returns 0 for a valid signature. */ |
12450 | | ret = wc_SlhDsaKey_Verify(ssl->peerSlhDsaKey, NULL, 0, |
12451 | | args->sigData, args->sigDataSz, |
12452 | | sig, args->sigSz); |
12453 | | |
12454 | | if (ret == 0) { |
12455 | | /* CLIENT/SERVER: data verified with public key from |
12456 | | * certificate. */ |
12457 | | ssl->options.peerAuthGood = 1; |
12458 | | |
12459 | | FreeKey(ssl, DYNAMIC_TYPE_SLHDSA, |
12460 | | (void**)&ssl->peerSlhDsaKey); |
12461 | | ssl->peerSlhDsaKeyPresent = 0; |
12462 | | } |
12463 | | else { |
12464 | | /* wc_SlhDsaKey_Verify already returns SIG_VERIFY_E on a |
12465 | | * signature mismatch and propagates real errors verbatim |
12466 | | * (WC_PENDING_E on the async crypto-callback path, MEMORY_E, |
12467 | | * ...), so leave ret unchanged. Flattening everything to |
12468 | | * SIG_VERIFY_E would break async resume and mask |
12469 | | * diagnostics; the ML-DSA/Falcon blocks above likewise |
12470 | | * preserve non-completion return codes. */ |
12471 | | WOLFSSL_MSG("SLH-DSA signature verification failed"); |
12472 | | } |
12473 | | } |
12474 | | #endif /* WOLFSSL_HAVE_SLHDSA */ |
12475 | | |
12476 | | /* Check for error */ |
12477 | 0 | if (ret != 0) { |
12478 | 0 | goto exit_dcv; |
12479 | 0 | } |
12480 | | |
12481 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12482 | | if (ssl->sigSpec != NULL && |
12483 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
12484 | | /* Move forward to the alternative signature. */ |
12485 | | sig += args->sigSz + OPAQUE16_LEN; |
12486 | | |
12487 | | /* Verify the alternative signature */ |
12488 | | #ifndef NO_RSA |
12489 | | if ((args->altSigAlgo == rsa_pss_sa_algo) && |
12490 | | (ssl->peerRsaKey != NULL) && |
12491 | | (ssl->peerRsaKeyPresent != 0)) { |
12492 | | WOLFSSL_MSG("Doing RSA peer cert alt verify"); |
12493 | | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
12494 | | (word32)rsaSigBuf->length, |
12495 | | &args->output, args->altSigAlgo, |
12496 | | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
12497 | | #ifdef HAVE_PK_CALLBACKS |
12498 | | &ssl->buffers.peerRsaKey |
12499 | | #else |
12500 | | NULL |
12501 | | #endif |
12502 | | ); |
12503 | | if (ret >= 0) { |
12504 | | args->sendSz = ret; |
12505 | | ret = 0; |
12506 | | } |
12507 | | } |
12508 | | #endif /* !NO_RSA */ |
12509 | | #ifdef HAVE_ECC |
12510 | | if ((args->altSigAlgo == ecc_dsa_sa_algo) && |
12511 | | (ssl->peerEccDsaKeyPresent)) { |
12512 | | WOLFSSL_MSG("Doing ECC peer cert alt verify"); |
12513 | | ret = EccVerify(ssl, sig, args->altSignatureSz, |
12514 | | args->altSigData, args->altSigDataSz, |
12515 | | ssl->peerEccDsaKey, |
12516 | | #ifdef HAVE_PK_CALLBACKS |
12517 | | &ssl->buffers.peerEccDsaKey |
12518 | | #else |
12519 | | NULL |
12520 | | #endif |
12521 | | ); |
12522 | | |
12523 | | if (ret >= 0) { |
12524 | | /* CLIENT/SERVER: data verified with public key from |
12525 | | * certificate. */ |
12526 | | args->altPeerAuthGood = 1; |
12527 | | |
12528 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
12529 | | (void**)&ssl->peerEccDsaKey); |
12530 | | ssl->peerEccDsaKeyPresent = 0; |
12531 | | } |
12532 | | } |
12533 | | #endif /* HAVE_ECC */ |
12534 | | #if defined(HAVE_FALCON) |
12535 | | if (((args->altSigAlgo == falcon_level1_sa_algo) || |
12536 | | (args->altSigAlgo == falcon_level5_sa_algo)) && |
12537 | | (ssl->peerFalconKeyPresent)) { |
12538 | | int res = 0; |
12539 | | WOLFSSL_MSG("Doing Falcon peer cert alt verify"); |
12540 | | ret = wc_falcon_verify_msg(sig, args->altSignatureSz, |
12541 | | args->altSigData, args->altSigDataSz, |
12542 | | &res, ssl->peerFalconKey); |
12543 | | |
12544 | | if ((ret >= 0) && (res == 1)) { |
12545 | | /* CLIENT/SERVER: data verified with public key from |
12546 | | * certificate. */ |
12547 | | args->altPeerAuthGood = 1; |
12548 | | |
12549 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
12550 | | (void**)&ssl->peerFalconKey); |
12551 | | ssl->peerFalconKeyPresent = 0; |
12552 | | } |
12553 | | else if ((ret >= 0) && (res == 0)) { |
12554 | | WOLFSSL_MSG("Falcon signature verification failed"); |
12555 | | ret = SIG_VERIFY_E; |
12556 | | } |
12557 | | } |
12558 | | #endif /* HAVE_FALCON */ |
12559 | | #if defined(WOLFSSL_HAVE_MLDSA) && !defined(WOLFSSL_MLDSA_NO_VERIFY) |
12560 | | if (((args->altSigAlgo == mldsa_44_sa_algo) || |
12561 | | (args->altSigAlgo == mldsa_65_sa_algo) || |
12562 | | (args->altSigAlgo == mldsa_87_sa_algo)) && |
12563 | | (ssl->peerMlDsaKeyPresent)) { |
12564 | | int res = 0; |
12565 | | WOLFSSL_MSG("Doing ML-DSA peer cert alt verify"); |
12566 | | ret = wc_MlDsaKey_VerifyCtx(ssl->peerMlDsaKey, sig, |
12567 | | args->altSignatureSz, NULL, 0, |
12568 | | args->altSigData, |
12569 | | args->altSigDataSz, &res); |
12570 | | |
12571 | | if ((ret >= 0) && (res == 1)) { |
12572 | | /* CLIENT/SERVER: data verified with public key from |
12573 | | * certificate. */ |
12574 | | args->altPeerAuthGood = 1; |
12575 | | |
12576 | | FreeKey(ssl, DYNAMIC_TYPE_MLDSA, |
12577 | | (void**)&ssl->peerMlDsaKey); |
12578 | | ssl->peerMlDsaKeyPresent = 0; |
12579 | | } |
12580 | | else if ((ret >= 0) && (res == 0)) { |
12581 | | WOLFSSL_MSG("ML-DSA signature verification failed"); |
12582 | | ret = SIG_VERIFY_E; |
12583 | | } |
12584 | | } |
12585 | | #endif /* WOLFSSL_HAVE_MLDSA */ |
12586 | | |
12587 | | /* Check for error */ |
12588 | | if (ret != 0) { |
12589 | | goto exit_dcv; |
12590 | | } |
12591 | | } |
12592 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12593 | | |
12594 | | /* Advance state and proceed */ |
12595 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
12596 | 0 | } /* case TLS_ASYNC_DO */ |
12597 | 0 | FALL_THROUGH; |
12598 | |
|
12599 | 0 | case TLS_ASYNC_VERIFY: |
12600 | 0 | { |
12601 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
12602 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
12603 | 0 | int sigAlgo = ssl->options.peerSigAlgo; |
12604 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12605 | | /* Check if our alternative signature was RSA */ |
12606 | | if (ssl->sigSpec != NULL && |
12607 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
12608 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
12609 | | sigAlgo = args->altSigAlgo; |
12610 | | } |
12611 | | #endif |
12612 | 0 | ret = CheckRSASignature(ssl, sigAlgo, |
12613 | 0 | ssl->options.peerHashAlgo, args->output, args->sendSz); |
12614 | 0 | if (ret != 0) |
12615 | 0 | goto exit_dcv; |
12616 | | |
12617 | | /* CLIENT/SERVER: data verified with public key from |
12618 | | * certificate. */ |
12619 | 0 | ssl->peerRsaKeyPresent = 0; |
12620 | 0 | FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
12621 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12622 | | /* Check if our alternative signature was RSA */ |
12623 | | if (ssl->sigSpec != NULL && |
12624 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
12625 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
12626 | | args->altPeerAuthGood = 1; |
12627 | | } |
12628 | | else |
12629 | | #endif |
12630 | 0 | ssl->options.peerAuthGood = 1; |
12631 | 0 | } |
12632 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
12633 | | |
12634 | | /* Advance state and proceed */ |
12635 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
12636 | 0 | } /* case TLS_ASYNC_VERIFY */ |
12637 | 0 | FALL_THROUGH; |
12638 | |
|
12639 | 0 | case TLS_ASYNC_FINALIZE: |
12640 | 0 | { |
12641 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
12642 | | if (ssl->options.peerAuthGood && |
12643 | | ssl->sigSpec != NULL && |
12644 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
12645 | | ssl->options.peerAuthGood = args->altPeerAuthGood; |
12646 | | } |
12647 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
12648 | 0 | ssl->options.havePeerVerify = 1; |
12649 | | |
12650 | | /* Set final index */ |
12651 | 0 | args->idx += args->sz; |
12652 | 0 | *inOutIdx = args->idx; |
12653 | | |
12654 | | /* Advance state and proceed */ |
12655 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
12656 | |
|
12657 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
12658 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
12659 | 0 | ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE; |
12660 | 0 | #endif |
12661 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
12662 | 0 | FALL_THROUGH; |
12663 | |
|
12664 | 0 | case TLS_ASYNC_END: |
12665 | 0 | { |
12666 | 0 | break; |
12667 | 0 | } |
12668 | | |
12669 | 0 | default: |
12670 | 0 | ret = INPUT_CASE_ERROR; |
12671 | 0 | } /* switch(ssl->options.asyncState) */ |
12672 | | |
12673 | 0 | exit_dcv: |
12674 | |
|
12675 | 0 | WOLFSSL_LEAVE("DoTls13CertificateVerify", ret); |
12676 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO); |
12677 | |
|
12678 | | #ifdef WOLFSSL_ASYNC_CRYPT |
12679 | | /* Handle async operation */ |
12680 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
12681 | | /* Mark message as not received so it can process again */ |
12682 | | ssl->msgsReceived.got_certificate_verify = 0; |
12683 | | |
12684 | | return ret; |
12685 | | } |
12686 | | else |
12687 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
12688 | 0 | if (ret != 0) { |
12689 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
12690 | |
|
12691 | 0 | if (ret != WC_NO_ERR_TRACE(INVALID_PARAMETER)) { |
12692 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
12693 | 0 | } |
12694 | 0 | } |
12695 | | |
12696 | | /* Final cleanup */ |
12697 | 0 | FreeDcv13Args(ssl, args); |
12698 | 0 | FreeKeyExchange(ssl); |
12699 | 0 | #ifdef WOLFSSL_ASYNC_IO |
12700 | | /* Cleanup async */ |
12701 | 0 | FreeAsyncCtx(ssl, 0); |
12702 | 0 | #endif |
12703 | |
|
12704 | 0 | return ret; |
12705 | 0 | } |
12706 | | #endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 || HAVE_ED448 || |
12707 | | * HAVE_FALCON || WOLFSSL_HAVE_MLDSA || WOLFSSL_HAVE_SLHDSA */ |
12708 | | #endif /* !NO_CERTS */ |
12709 | | |
12710 | | /* Parse and handle a TLS v1.3 Finished message. |
12711 | | * |
12712 | | * ssl The SSL/TLS object. |
12713 | | * input The message buffer. |
12714 | | * inOutIdx On entry, the index into the message buffer of Finished. |
12715 | | * On exit, the index of byte after the Finished message and padding. |
12716 | | * size Length of message data. |
12717 | | * totalSz Length of remaining data in the message buffer. |
12718 | | * sniff Indicates whether we are sniffing packets. |
12719 | | * returns 0 on success and otherwise failure. |
12720 | | */ |
12721 | | int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
12722 | | word32 size, word32 totalSz, int sniff) |
12723 | 0 | { |
12724 | 0 | int ret; |
12725 | 0 | word32 finishedSz = 0; |
12726 | 0 | byte* secret; |
12727 | 0 | byte mac[WC_MAX_DIGEST_SIZE]; |
12728 | |
|
12729 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_DO); |
12730 | 0 | WOLFSSL_ENTER("DoTls13Finished"); |
12731 | |
|
12732 | 0 | #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH) |
12733 | | /* verify the client sent certificate if required */ |
12734 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && !ssl->options.resuming && |
12735 | 0 | (ssl->options.mutualAuth || ssl->options.failNoCert)) { |
12736 | | #ifdef OPENSSL_COMPATIBLE_DEFAULTS |
12737 | | if (ssl->options.isPSK) { |
12738 | | WOLFSSL_MSG("TLS v1.3 client used PSK but cert required. Allowing " |
12739 | | "for OpenSSL compatibility"); |
12740 | | } |
12741 | | else |
12742 | | #endif |
12743 | 0 | if ( |
12744 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
12745 | | /* Exempt only the initial handshake; a pending post-handshake |
12746 | | * CertificateRequest (certReqCtx != NULL) still requires a peer |
12747 | | * certificate and a valid CertificateVerify. */ |
12748 | | (!ssl->options.verifyPostHandshake || ssl->certReqCtx != NULL) && |
12749 | | #endif |
12750 | 0 | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
12751 | 0 | ret = NO_PEER_CERT; /* NO_PEER_VERIFY */ |
12752 | 0 | WOLFSSL_MSG("TLS v1.3 client did not present peer cert"); |
12753 | 0 | DoCertFatalAlert(ssl, ret); |
12754 | 0 | goto cleanup; |
12755 | 0 | } |
12756 | 0 | } |
12757 | 0 | #endif |
12758 | | |
12759 | | #if !defined(NO_CERTS) && !defined(NO_PSK) && \ |
12760 | | defined(WOLFSSL_CERT_WITH_EXTERN_PSK) |
12761 | | /* Verify the server sent a certificate if requested */ |
12762 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->options.pskNegotiated && |
12763 | | ssl->options.failNoCert) { |
12764 | | if ((TLSX_Find(ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK) != NULL) && |
12765 | | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
12766 | | ret = NO_PEER_CERT; |
12767 | | WOLFSSL_MSG("TLS v1.3 server did not present peer cert"); |
12768 | | DoCertFatalAlert(ssl, ret); |
12769 | | goto cleanup; |
12770 | | } |
12771 | | } |
12772 | | #endif |
12773 | | |
12774 | | /* check against totalSz */ |
12775 | 0 | if (*inOutIdx + size > totalSz) { |
12776 | 0 | ret = BUFFER_E; |
12777 | 0 | goto cleanup; |
12778 | 0 | } |
12779 | | |
12780 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
12781 | | ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz); |
12782 | | if (ret == 0) { |
12783 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
12784 | | goto cleanup; |
12785 | | } |
12786 | | if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) { |
12787 | | SendAlert(ssl, alert_fatal, decrypt_error); |
12788 | | goto cleanup; |
12789 | | } |
12790 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
12791 | | /* other errors */ |
12792 | | goto cleanup; |
12793 | | } |
12794 | | ret = 0; |
12795 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
12796 | | |
12797 | 0 | if (ssl->options.handShakeDone) { |
12798 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
12799 | 0 | ssl->keys.client_write_MAC_secret, |
12800 | 0 | WOLFSSL_CLIENT_END); |
12801 | 0 | if (ret != 0) |
12802 | 0 | goto cleanup; |
12803 | | |
12804 | 0 | secret = ssl->keys.client_write_MAC_secret; |
12805 | 0 | } |
12806 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12807 | | /* All the handshake messages have been received to calculate |
12808 | | * client and server finished keys. |
12809 | | */ |
12810 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
12811 | 0 | ssl->keys.client_write_MAC_secret, |
12812 | 0 | WOLFSSL_CLIENT_END); |
12813 | 0 | if (ret != 0) |
12814 | 0 | goto cleanup; |
12815 | | |
12816 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
12817 | 0 | ssl->keys.server_write_MAC_secret, |
12818 | 0 | WOLFSSL_SERVER_END); |
12819 | 0 | if (ret != 0) |
12820 | 0 | goto cleanup; |
12821 | | |
12822 | 0 | secret = ssl->keys.server_write_MAC_secret; |
12823 | 0 | } |
12824 | 0 | else { |
12825 | 0 | secret = ssl->keys.client_write_MAC_secret; |
12826 | 0 | } |
12827 | | |
12828 | 0 | if (sniff == NO_SNIFF) { |
12829 | |
|
12830 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz); |
12831 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
12832 | | if (finishedSz > TLS_FINISHED_SZ_MAX) { |
12833 | | ret = BUFFER_ERROR; |
12834 | | goto cleanup; |
12835 | | } |
12836 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12837 | | XMEMCPY(ssl->serverFinished, mac, finishedSz); |
12838 | | ssl->serverFinished_len = (byte)finishedSz; |
12839 | | } |
12840 | | else { |
12841 | | XMEMCPY(ssl->clientFinished, mac, finishedSz); |
12842 | | ssl->clientFinished_len = (byte)finishedSz; |
12843 | | } |
12844 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
12845 | 0 | if (ret != 0) |
12846 | 0 | goto cleanup; |
12847 | 0 | if (size != finishedSz) { |
12848 | 0 | ret = BUFFER_ERROR; |
12849 | 0 | goto cleanup; |
12850 | 0 | } |
12851 | 0 | } |
12852 | | |
12853 | | #ifdef WOLFSSL_CALLBACKS |
12854 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
12855 | | if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo); |
12856 | | #endif |
12857 | | |
12858 | 0 | if (sniff == NO_SNIFF) { |
12859 | | /* Actually check verify data. */ |
12860 | 0 | if (size > WC_MAX_DIGEST_SIZE || |
12861 | 0 | ConstantCompare(input + *inOutIdx, mac, size) != 0){ |
12862 | 0 | WOLFSSL_MSG("Verify finished error on hashes"); |
12863 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
12864 | 0 | WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR); |
12865 | 0 | ret = VERIFY_FINISHED_ERROR; |
12866 | 0 | goto cleanup; |
12867 | 0 | } |
12868 | 0 | } |
12869 | | |
12870 | 0 | *inOutIdx += size; |
12871 | |
|
12872 | 0 | #ifndef NO_WOLFSSL_SERVER |
12873 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
12874 | 0 | !ssl->options.handShakeDone) { |
12875 | | #ifdef WOLFSSL_EARLY_DATA |
12876 | | if (ssl->earlyData != no_early_data) { |
12877 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0) |
12878 | | goto cleanup; |
12879 | | } |
12880 | | #endif |
12881 | | /* Setup keys for application data messages from client. */ |
12882 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
12883 | 0 | goto cleanup; |
12884 | 0 | } |
12885 | 0 | #endif |
12886 | | |
12887 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12888 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
12889 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
12890 | 0 | #endif |
12891 | 0 | #ifndef NO_WOLFSSL_SERVER |
12892 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12893 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
12894 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
12895 | 0 | ssl->options.handShakeDone = 1; |
12896 | 0 | } |
12897 | 0 | #endif |
12898 | |
|
12899 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_EARLY_DATA) |
12900 | | if (ssl->options.dtls && ssl->earlyData > early_data_ext) { |
12901 | | /* DTLSv1.3 has no EndOfearlydata messages. We stop processing EarlyData |
12902 | | as soon we receive the client's finished message */ |
12903 | | ssl->earlyData = done_early_data; |
12904 | | } |
12905 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_EARLY_DATA */ |
12906 | | #if defined(WOLFSSL_QUIC) && defined(WOLFSSL_EARLY_DATA) |
12907 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData > early_data_ext) { |
12908 | | /* QUIC has no EndOfEarlyData messages. We stop processing EarlyData |
12909 | | as soon we receive the client's finished message */ |
12910 | | ssl->earlyData = done_early_data; |
12911 | | } |
12912 | | #endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */ |
12913 | |
|
12914 | 0 | ret = 0; |
12915 | 0 | cleanup: |
12916 | 0 | ForceZero(mac, sizeof(mac)); |
12917 | 0 | WOLFSSL_LEAVE("DoTls13Finished", ret); |
12918 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_DO); |
12919 | |
|
12920 | 0 | return ret; |
12921 | 0 | } |
12922 | | |
12923 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
12924 | | /* Send the TLS v1.3 Finished message. |
12925 | | * |
12926 | | * ssl The SSL/TLS object. |
12927 | | * returns 0 on success, otherwise failure. |
12928 | | */ |
12929 | | static int SendTls13Finished(WOLFSSL* ssl) |
12930 | | { |
12931 | | byte finishedSz = ssl->specs.hash_size; |
12932 | | byte* input; |
12933 | | byte* output; |
12934 | | int ret; |
12935 | | int headerSz = HANDSHAKE_HEADER_SZ; |
12936 | | int outputSz; |
12937 | | byte* secret; |
12938 | | |
12939 | | #ifdef WOLFSSL_DTLS13 |
12940 | | int dtlsRet = 0, isDtls = 0; |
12941 | | #endif /* WOLFSSL_DTLS13 */ |
12942 | | |
12943 | | WOLFSSL_START(WC_FUNC_FINISHED_SEND); |
12944 | | WOLFSSL_ENTER("SendTls13Finished"); |
12945 | | |
12946 | | ssl->options.buildingMsg = 1; |
12947 | | #ifdef WOLFSSL_DTLS13 |
12948 | | if (ssl->options.dtls) { |
12949 | | headerSz = DTLS_HANDSHAKE_HEADER_SZ; |
12950 | | /* using isDtls instead of ssl->options.dtls will abide clang static |
12951 | | analyzer on using an uninitialized value */ |
12952 | | isDtls = 1; |
12953 | | } |
12954 | | #endif /* WOLFSSL_DTLS13 */ |
12955 | | |
12956 | | outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA; |
12957 | | /* Check buffers are big enough and grow if needed. */ |
12958 | | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
12959 | | return ret; |
12960 | | |
12961 | | /* get output buffer */ |
12962 | | output = GetOutputBuffer(ssl); |
12963 | | input = output + RECORD_HEADER_SZ; |
12964 | | |
12965 | | #ifdef WOLFSSL_DTLS13 |
12966 | | if (isDtls) |
12967 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
12968 | | #endif /* WOLFSSL_DTLS13 */ |
12969 | | |
12970 | | AddTls13HandShakeHeader(input, (word32)finishedSz, 0, (word32)finishedSz, |
12971 | | finished, ssl); |
12972 | | |
12973 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
12974 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12975 | | ret = tsip_Tls13SendFinished(ssl, output, outputSz, input, 1); |
12976 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
12977 | | return ret; |
12978 | | } |
12979 | | ret = 0; |
12980 | | } |
12981 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
12982 | | |
12983 | | /* make finished hashes */ |
12984 | | if (ssl->options.handShakeDone) { |
12985 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
12986 | | ssl->keys.client_write_MAC_secret, |
12987 | | WOLFSSL_CLIENT_END); |
12988 | | if (ret != 0) |
12989 | | return ret; |
12990 | | |
12991 | | secret = ssl->keys.client_write_MAC_secret; |
12992 | | } |
12993 | | else if (ssl->options.side == WOLFSSL_CLIENT_END) |
12994 | | secret = ssl->keys.client_write_MAC_secret; |
12995 | | else { |
12996 | | /* All the handshake messages have been done to calculate client and |
12997 | | * server finished keys. |
12998 | | */ |
12999 | | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
13000 | | ssl->keys.client_write_MAC_secret, |
13001 | | WOLFSSL_CLIENT_END); |
13002 | | if (ret != 0) |
13003 | | return ret; |
13004 | | |
13005 | | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
13006 | | ssl->keys.server_write_MAC_secret, |
13007 | | WOLFSSL_SERVER_END); |
13008 | | if (ret != 0) |
13009 | | return ret; |
13010 | | |
13011 | | secret = ssl->keys.server_write_MAC_secret; |
13012 | | } |
13013 | | ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL); |
13014 | | if (ret != 0) |
13015 | | return ret; |
13016 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
13017 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13018 | | XMEMCPY(ssl->clientFinished, &input[headerSz], finishedSz); |
13019 | | ssl->clientFinished_len = finishedSz; |
13020 | | } |
13021 | | else { |
13022 | | XMEMCPY(ssl->serverFinished, &input[headerSz], finishedSz); |
13023 | | ssl->serverFinished_len = finishedSz; |
13024 | | } |
13025 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
13026 | | |
13027 | | #ifdef WOLFSSL_DTLS13 |
13028 | | if (isDtls) { |
13029 | | dtlsRet = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
13030 | | (word16)(Dtls13GetRlHeaderLength(ssl, 1) + headerSz + finishedSz), finished, |
13031 | | 1); |
13032 | | if (dtlsRet != 0 && dtlsRet != WC_NO_ERR_TRACE(WANT_WRITE)) |
13033 | | return dtlsRet; |
13034 | | |
13035 | | } else |
13036 | | #endif /* WOLFSSL_DTLS13 */ |
13037 | | { |
13038 | | /* This message is always encrypted. */ |
13039 | | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
13040 | | headerSz + finishedSz, handshake, 1, 0, 0); |
13041 | | if (sendSz < 0) { |
13042 | | WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR); |
13043 | | return BUILD_MSG_ERROR; |
13044 | | } |
13045 | | |
13046 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
13047 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
13048 | | if (ssl->toInfoOn) { |
13049 | | ret = AddPacketInfo(ssl, "Finished", handshake, output, sendSz, |
13050 | | WRITE_PROTO, 0, ssl->heap); |
13051 | | if (ret != 0) |
13052 | | return ret; |
13053 | | } |
13054 | | #endif |
13055 | | |
13056 | | ssl->buffers.outputBuffer.length += (word32)sendSz; |
13057 | | ssl->options.buildingMsg = 0; |
13058 | | } |
13059 | | |
13060 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13061 | | #ifdef WOLFSSL_EARLY_DATA |
13062 | | byte storeTrafficDecKeys = ssl->earlyData == no_early_data; |
13063 | | #endif |
13064 | | /* Can send application data now. */ |
13065 | | if ((ret = DeriveMasterSecret(ssl)) != 0) |
13066 | | return ret; |
13067 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
13068 | | ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz); |
13069 | | #ifdef WOLFSSL_EARLY_DATA |
13070 | | |
13071 | | #ifdef WOLFSSL_DTLS13 |
13072 | | /* DTLS13 dynamically change keys and it needs all |
13073 | | the keys in ssl->keys to save the keying material */ |
13074 | | if (isDtls) |
13075 | | storeTrafficDecKeys = 1; |
13076 | | #endif /* WOLFSSL_DTLS13 */ |
13077 | | |
13078 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
13079 | | != 0) { |
13080 | | return ret; |
13081 | | } |
13082 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY, |
13083 | | storeTrafficDecKeys)) != 0) { |
13084 | | return ret; |
13085 | | } |
13086 | | #else |
13087 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE, |
13088 | | 1)) != 0) { |
13089 | | return ret; |
13090 | | } |
13091 | | #endif |
13092 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13093 | | return ret; |
13094 | | |
13095 | | #ifdef WOLFSSL_DTLS13 |
13096 | | if (isDtls) { |
13097 | | w64wrapper epochTraffic0; |
13098 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
13099 | | ssl->dtls13Epoch = epochTraffic0; |
13100 | | ssl->dtls13PeerEpoch = epochTraffic0; |
13101 | | |
13102 | | ret = Dtls13SetEpochKeys( |
13103 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
13104 | | if (ret != 0) |
13105 | | return ret; |
13106 | | |
13107 | | } |
13108 | | #endif /* WOLFSSL_DTLS13 */ |
13109 | | |
13110 | | } |
13111 | | |
13112 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
13113 | | !ssl->options.handShakeDone) { |
13114 | | #ifdef WOLFSSL_EARLY_DATA |
13115 | | if (ssl->earlyData != no_early_data) { |
13116 | | if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_SIDE_ONLY, |
13117 | | 1)) != 0) { |
13118 | | return ret; |
13119 | | } |
13120 | | } |
13121 | | #endif |
13122 | | /* Setup keys for application data messages. */ |
13123 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13124 | | return ret; |
13125 | | |
13126 | | #if defined(HAVE_SESSION_TICKET) |
13127 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
13128 | | if (ret != 0) |
13129 | | return ret; |
13130 | | #endif |
13131 | | |
13132 | | #ifdef WOLFSSL_DTLS13 |
13133 | | if (isDtls) { |
13134 | | w64wrapper epochTraffic0; |
13135 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
13136 | | ssl->dtls13Epoch = epochTraffic0; |
13137 | | ssl->dtls13PeerEpoch = epochTraffic0; |
13138 | | |
13139 | | ret = Dtls13SetEpochKeys( |
13140 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
13141 | | if (ret != 0) |
13142 | | return ret; |
13143 | | |
13144 | | } |
13145 | | #endif /* WOLFSSL_DTLS13 */ |
13146 | | } |
13147 | | |
13148 | | #ifndef NO_WOLFSSL_CLIENT |
13149 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13150 | | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
13151 | | ssl->options.handShakeState = HANDSHAKE_DONE; |
13152 | | ssl->options.handShakeDone = 1; |
13153 | | } |
13154 | | #endif |
13155 | | #ifndef NO_WOLFSSL_SERVER |
13156 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13157 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
13158 | | } |
13159 | | #endif |
13160 | | |
13161 | | #ifdef WOLFSSL_DTLS13 |
13162 | | if (isDtls) { |
13163 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
13164 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
13165 | | |
13166 | | return dtlsRet; |
13167 | | } |
13168 | | #endif /* WOLFSSL_DTLS13 */ |
13169 | | |
13170 | | if ((ret = SendBuffered(ssl)) != 0) |
13171 | | return ret; |
13172 | | |
13173 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
13174 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
13175 | | |
13176 | | return ret; |
13177 | | } |
13178 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
13179 | | |
13180 | | /* handle generation TLS v1.3 key_update (24) */ |
13181 | | /* Send the TLS v1.3 KeyUpdate message. |
13182 | | * |
13183 | | * ssl The SSL/TLS object. |
13184 | | * returns 0 on success, otherwise failure. |
13185 | | */ |
13186 | | int SendTls13KeyUpdate(WOLFSSL* ssl) |
13187 | 0 | { |
13188 | 0 | byte* input; |
13189 | 0 | byte* output; |
13190 | 0 | int ret; |
13191 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
13192 | 0 | int outputSz; |
13193 | 0 | word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
13194 | |
|
13195 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND); |
13196 | 0 | WOLFSSL_ENTER("SendTls13KeyUpdate"); |
13197 | |
|
13198 | | #ifdef WOLFSSL_DTLS13 |
13199 | | if (ssl->options.dtls) { |
13200 | | /* RFC 9147 Section 4.2.1: do not send a KeyUpdate that would advance |
13201 | | * the sending epoch beyond 2^48-1. */ |
13202 | | if (w64GTE(ssl->dtls13Epoch, |
13203 | | w64From32(DTLS13_EPOCH_MAX_HI32, DTLS13_EPOCH_MAX_LO32))) { |
13204 | | WOLFSSL_MSG("DTLS 1.3 sending epoch at maximum; refusing KeyUpdate"); |
13205 | | return BAD_STATE_E; |
13206 | | } |
13207 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
13208 | | } |
13209 | | #endif /* WOLFSSL_DTLS13 */ |
13210 | |
|
13211 | 0 | if (!ssl->options.dtls) { |
13212 | | /* RFC 9846 Section 4.7.3: a sending implementation MUST NOT allow its |
13213 | | * number of key updates to exceed 2^48-1. Receivers MUST NOT enforce |
13214 | | * this on the peer. */ |
13215 | 0 | if (w64GTE(ssl->keys.keyUpdateCount, |
13216 | 0 | w64From32(TLS13_KEY_UPDATE_MAX_HI32, |
13217 | 0 | TLS13_KEY_UPDATE_MAX_LO32))) { |
13218 | 0 | WOLFSSL_MSG("TLS 1.3 key update count at maximum; refusing " |
13219 | 0 | "KeyUpdate"); |
13220 | 0 | return BAD_STATE_E; |
13221 | 0 | } |
13222 | 0 | } |
13223 | | |
13224 | 0 | outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA; |
13225 | | /* Check buffers are big enough and grow if needed. */ |
13226 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
13227 | 0 | return ret; |
13228 | | |
13229 | | /* get output buffer */ |
13230 | 0 | output = GetOutputBuffer(ssl); |
13231 | 0 | input = output + RECORD_HEADER_SZ; |
13232 | |
|
13233 | | #ifdef WOLFSSL_DTLS13 |
13234 | | if (ssl->options.dtls) |
13235 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
13236 | | #endif /* WOLFSSL_DTLS13 */ |
13237 | |
|
13238 | 0 | AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl); |
13239 | | |
13240 | | /* If: |
13241 | | * 1. I haven't sent a KeyUpdate requesting a response and |
13242 | | * 2. This isn't responding to peer KeyUpdate requiring a response then, |
13243 | | * I want a response. |
13244 | | */ |
13245 | 0 | ssl->keys.updateResponseReq = output[i++] = |
13246 | 0 | !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond; |
13247 | | /* Sent response, no longer need to respond. */ |
13248 | 0 | ssl->keys.keyUpdateRespond = 0; |
13249 | |
|
13250 | | #ifdef WOLFSSL_DTLS13 |
13251 | | if (ssl->options.dtls) { |
13252 | | ret = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
13253 | | OPAQUE8_LEN + Dtls13GetRlHeaderLength(ssl, 1) + |
13254 | | DTLS_HANDSHAKE_HEADER_SZ, |
13255 | | key_update, 0); |
13256 | | } |
13257 | | else |
13258 | | #endif /* WOLFSSL_DTLS13 */ |
13259 | 0 | { |
13260 | | /* This message is always encrypted. */ |
13261 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
13262 | 0 | headerSz + OPAQUE8_LEN, handshake, 0, 0, 0); |
13263 | 0 | if (sendSz < 0) |
13264 | 0 | return BUILD_MSG_ERROR; |
13265 | | |
13266 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
13267 | | if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate"); |
13268 | | if (ssl->toInfoOn) { |
13269 | | ret = AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz, |
13270 | | WRITE_PROTO, 0, ssl->heap); |
13271 | | if (ret != 0) |
13272 | | return ret; |
13273 | | } |
13274 | | #endif |
13275 | | |
13276 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
13277 | |
|
13278 | 0 | ret = SendBuffered(ssl); |
13279 | | |
13280 | |
|
13281 | 0 | if (ret != 0 && ret != WC_NO_ERR_TRACE(WANT_WRITE)) |
13282 | 0 | return ret; |
13283 | 0 | } |
13284 | | |
13285 | | /* In DTLS we must wait for the ack before setting up the new keys */ |
13286 | 0 | if (!ssl->options.dtls) { |
13287 | | |
13288 | | /* Future traffic uses new encryption keys. */ |
13289 | 0 | if ((ret = DeriveTls13Keys( |
13290 | 0 | ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
13291 | 0 | != 0) |
13292 | 0 | return ret; |
13293 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13294 | 0 | return ret; |
13295 | | |
13296 | | /* Count this key update against the RFC 9846 sender limit. */ |
13297 | 0 | w64Increment(&ssl->keys.keyUpdateCount); |
13298 | 0 | } |
13299 | | |
13300 | | |
13301 | 0 | WOLFSSL_LEAVE("SendTls13KeyUpdate", ret); |
13302 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND); |
13303 | |
|
13304 | 0 | return ret; |
13305 | 0 | } |
13306 | | |
13307 | | /* handle processing TLS v1.3 key_update (24) */ |
13308 | | /* Parse and handle a TLS v1.3 KeyUpdate message. |
13309 | | * |
13310 | | * ssl The SSL/TLS object. |
13311 | | * input The message buffer. |
13312 | | * inOutIdx On entry, the index into the message buffer of Finished. |
13313 | | * On exit, the index of byte after the Finished message and padding. |
13314 | | * totalSz The length of the current handshake message. |
13315 | | * returns 0 on success and otherwise failure. |
13316 | | */ |
13317 | | static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
13318 | | word32 totalSz) |
13319 | 0 | { |
13320 | 0 | int ret; |
13321 | 0 | word32 i = *inOutIdx; |
13322 | |
|
13323 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO); |
13324 | 0 | WOLFSSL_ENTER("DoTls13KeyUpdate"); |
13325 | | |
13326 | | /* check against totalSz */ |
13327 | 0 | if (OPAQUE8_LEN != totalSz) |
13328 | 0 | return BUFFER_E; |
13329 | | |
13330 | 0 | switch (input[i]) { |
13331 | 0 | case update_not_requested: |
13332 | | /* This message in response to any outstanding request. */ |
13333 | 0 | ssl->keys.keyUpdateRespond = 0; |
13334 | 0 | ssl->keys.updateResponseReq = 0; |
13335 | 0 | break; |
13336 | 0 | case update_requested: |
13337 | | /* New key update requiring a response. */ |
13338 | 0 | ssl->keys.keyUpdateRespond = 1; |
13339 | 0 | break; |
13340 | 0 | default: |
13341 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
13342 | 0 | return INVALID_PARAMETER; |
13343 | 0 | } |
13344 | | |
13345 | | /* Move index to byte after message. */ |
13346 | 0 | *inOutIdx += totalSz; |
13347 | | |
13348 | | /* Future traffic uses new decryption keys. */ |
13349 | 0 | if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1)) |
13350 | 0 | != 0) { |
13351 | 0 | return ret; |
13352 | 0 | } |
13353 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13354 | 0 | return ret; |
13355 | | |
13356 | | #ifdef WOLFSSL_DTLS13 |
13357 | | if (ssl->options.dtls) { |
13358 | | /* Increment on a local copy so ssl->dtls13PeerEpoch is left |
13359 | | * untouched when the check fails. */ |
13360 | | w64wrapper newEpoch = ssl->dtls13PeerEpoch; |
13361 | | w64Increment(&newEpoch); |
13362 | | |
13363 | | /* RFC 9147 Section 8: the 2^48-1 cap is sender-only; receivers MUST |
13364 | | * NOT enforce it. Guard only the wrap-to-zero (Section 4.2.1). */ |
13365 | | if (w64IsZero(newEpoch)) |
13366 | | return BAD_STATE_E; |
13367 | | |
13368 | | ssl->dtls13PeerEpoch = newEpoch; |
13369 | | |
13370 | | ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY); |
13371 | | if (ret != 0) |
13372 | | return ret; |
13373 | | } |
13374 | | #endif /* WOLFSSL_DTLS13 */ |
13375 | | |
13376 | 0 | if (ssl->keys.keyUpdateRespond) { |
13377 | |
|
13378 | | #ifdef WOLFSSL_DTLS13 |
13379 | | /* we already sent a keyUpdate (either in response to a previous |
13380 | | KeyUpdate or initiated by the application) and we are waiting for the |
13381 | | ack. We can't send a new KeyUpdate right away but to honor the RFC we |
13382 | | should send another KeyUpdate after the one in-flight is acked. We |
13383 | | don't do that as it looks redundant, it will make the code more |
13384 | | complex and I don't see a good use case for that. */ |
13385 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) { |
13386 | | ssl->keys.keyUpdateRespond = 0; |
13387 | | return 0; |
13388 | | } |
13389 | | #endif /* WOLFSSL_DTLS13 */ |
13390 | |
|
13391 | | #if defined(HAVE_WRITE_DUP) && defined(WOLFSSL_TLS13) |
13392 | | /* Read side cannot write; delegate the response to the write side. */ |
13393 | | if (ssl->dupWrite != NULL && ssl->dupSide == READ_DUP_SIDE) { |
13394 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
13395 | | return BAD_MUTEX_E; |
13396 | | ssl->dupWrite->keyUpdateRespond = 1; |
13397 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
13398 | | ssl->keys.keyUpdateRespond = 0; |
13399 | | return 0; |
13400 | | } |
13401 | | #endif /* HAVE_WRITE_DUP && WOLFSSL_TLS13 */ |
13402 | |
|
13403 | 0 | #ifndef WOLFSSL_RW_THREADED |
13404 | 0 | return SendTls13KeyUpdate(ssl); |
13405 | | #else |
13406 | | ssl->options.sendKeyUpdate = 1; |
13407 | | return 0; |
13408 | | #endif |
13409 | 0 | } |
13410 | | |
13411 | 0 | WOLFSSL_LEAVE("DoTls13KeyUpdate", ret); |
13412 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO); |
13413 | |
|
13414 | 0 | return 0; |
13415 | 0 | } |
13416 | | |
13417 | | #ifdef WOLFSSL_EARLY_DATA |
13418 | | #ifndef NO_WOLFSSL_CLIENT |
13419 | | /* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no |
13420 | | * more early application data. |
13421 | | * The encryption key now changes to the pre-calculated handshake key. |
13422 | | * |
13423 | | * ssl The SSL/TLS object. |
13424 | | * returns 0 on success and otherwise failure. |
13425 | | */ |
13426 | | static int SendTls13EndOfEarlyData(WOLFSSL* ssl) |
13427 | | { |
13428 | | byte* output; |
13429 | | int ret; |
13430 | | int sendSz; |
13431 | | word32 length; |
13432 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
13433 | | |
13434 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND); |
13435 | | WOLFSSL_ENTER("SendTls13EndOfEarlyData"); |
13436 | | |
13437 | | length = 0; |
13438 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
13439 | | ssl->options.buildingMsg = 1; |
13440 | | |
13441 | | /* Check buffers are big enough and grow if needed. */ |
13442 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
13443 | | return ret; |
13444 | | |
13445 | | /* Get position in output buffer to write new message to. */ |
13446 | | output = GetOutputBuffer(ssl); |
13447 | | |
13448 | | /* Put the record and handshake headers on. */ |
13449 | | AddTls13Headers(output, length, end_of_early_data, ssl); |
13450 | | |
13451 | | /* This message is always encrypted. */ |
13452 | | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
13453 | | idx - RECORD_HEADER_SZ, handshake, 1, 0, 0); |
13454 | | if (sendSz < 0) |
13455 | | return sendSz; |
13456 | | |
13457 | | ssl->buffers.outputBuffer.length += sendSz; |
13458 | | |
13459 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13460 | | return ret; |
13461 | | |
13462 | | ssl->options.buildingMsg = 0; |
13463 | | if (!ssl->options.groupMessages) |
13464 | | ret = SendBuffered(ssl); |
13465 | | |
13466 | | ssl->earlyData = done_early_data; |
13467 | | |
13468 | | WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret); |
13469 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND); |
13470 | | |
13471 | | return ret; |
13472 | | } |
13473 | | #endif /* !NO_WOLFSSL_CLIENT */ |
13474 | | |
13475 | | #ifndef NO_WOLFSSL_SERVER |
13476 | | /* handle processing of TLS 1.3 end_of_early_data (5) */ |
13477 | | /* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be |
13478 | | * no more early application data. |
13479 | | * The decryption key now changes to the pre-calculated handshake key. |
13480 | | * |
13481 | | * ssl The SSL/TLS object. |
13482 | | * returns 0 on success and otherwise failure. |
13483 | | */ |
13484 | | static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input, |
13485 | | word32* inOutIdx, word32 size) |
13486 | | { |
13487 | | int ret; |
13488 | | word32 begin = *inOutIdx; |
13489 | | |
13490 | | (void)input; |
13491 | | |
13492 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO); |
13493 | | WOLFSSL_ENTER("DoTls13EndOfEarlyData"); |
13494 | | |
13495 | | if ((*inOutIdx - begin) != size) |
13496 | | return BUFFER_ERROR; |
13497 | | |
13498 | | if (ssl->earlyData == no_early_data) { |
13499 | | WOLFSSL_MSG("EndOfEarlyData received unexpectedly"); |
13500 | | SendAlert(ssl, alert_fatal, unexpected_message); |
13501 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13502 | | return OUT_OF_ORDER_E; |
13503 | | } |
13504 | | |
13505 | | ssl->earlyData = done_early_data; |
13506 | | |
13507 | | ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY); |
13508 | | |
13509 | | WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret); |
13510 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO); |
13511 | | |
13512 | | return ret; |
13513 | | } |
13514 | | #endif /* !NO_WOLFSSL_SERVER */ |
13515 | | #endif /* WOLFSSL_EARLY_DATA */ |
13516 | | |
13517 | | #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
13518 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
13519 | | int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce, |
13520 | | byte len) |
13521 | | { |
13522 | | if (session->ticketNonce.data |
13523 | | != session->ticketNonce.dataStatic) { |
13524 | | XFREE(session->ticketNonce.data, session->heap, |
13525 | | DYNAMIC_TYPE_SESSION_TICK); |
13526 | | session->ticketNonce.data = session->ticketNonce.dataStatic; |
13527 | | session->ticketNonce.len = 0; |
13528 | | } |
13529 | | |
13530 | | if (len > MAX_TICKET_NONCE_STATIC_SZ) { |
13531 | | WOLFSSL_MSG("Using dynamic nonce buffer"); |
13532 | | session->ticketNonce.data = (byte*)XMALLOC(len, |
13533 | | session->heap, DYNAMIC_TYPE_SESSION_TICK); |
13534 | | if (session->ticketNonce.data == NULL) |
13535 | | return MEMORY_ERROR; |
13536 | | } |
13537 | | XMEMCPY(session->ticketNonce.data, nonce, len); |
13538 | | session->ticketNonce.len = len; |
13539 | | return 0; |
13540 | | } |
13541 | | #endif |
13542 | | #ifndef NO_WOLFSSL_CLIENT |
13543 | | /* Handle a New Session Ticket handshake message. |
13544 | | * Message contains the information required to perform resumption. |
13545 | | * |
13546 | | * ssl The SSL/TLS object. |
13547 | | * input The message buffer. |
13548 | | * inOutIdx On entry, the index into the message buffer of Finished. |
13549 | | * On exit, the index of byte after the Finished message and padding. |
13550 | | * size The length of the current handshake message. |
13551 | | * returns 0 on success, otherwise failure. |
13552 | | */ |
13553 | | static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input, |
13554 | | word32* inOutIdx, word32 size) |
13555 | 0 | { |
13556 | | #ifdef HAVE_SESSION_TICKET |
13557 | | int ret; |
13558 | | word32 begin = *inOutIdx; |
13559 | | word32 lifetime; |
13560 | | word32 ageAdd; |
13561 | | word16 length; |
13562 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
13563 | | word32 now; |
13564 | | #else |
13565 | | sword64 now; |
13566 | | #endif |
13567 | | const byte* nonce; |
13568 | | byte nonceLength; |
13569 | | |
13570 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO); |
13571 | | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
13572 | | |
13573 | | #ifdef HAVE_ECH |
13574 | | /* ignore session ticket when ECH is rejected */ |
13575 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
13576 | | !ssl->options.echAccepted) { |
13577 | | *inOutIdx += size + ssl->keys.padSz; |
13578 | | return 0; |
13579 | | } |
13580 | | #endif |
13581 | | |
13582 | | /* Lifetime hint. */ |
13583 | | if ((*inOutIdx - begin) + SESSION_HINT_SZ > size) |
13584 | | return BUFFER_ERROR; |
13585 | | ato32(input + *inOutIdx, &lifetime); |
13586 | | *inOutIdx += SESSION_HINT_SZ; |
13587 | | if (lifetime > MAX_LIFETIME) { |
13588 | | WOLFSSL_ERROR_VERBOSE(SERVER_HINT_ERROR); |
13589 | | return SERVER_HINT_ERROR; |
13590 | | } |
13591 | | |
13592 | | /* Age add. */ |
13593 | | if ((*inOutIdx - begin) + SESSION_ADD_SZ > size) |
13594 | | return BUFFER_ERROR; |
13595 | | ato32(input + *inOutIdx, &ageAdd); |
13596 | | *inOutIdx += SESSION_ADD_SZ; |
13597 | | |
13598 | | /* Ticket nonce. */ |
13599 | | if ((*inOutIdx - begin) + 1 > size) |
13600 | | return BUFFER_ERROR; |
13601 | | nonceLength = input[*inOutIdx]; |
13602 | | #if !defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
13603 | | (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) |
13604 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
13605 | | WOLFSSL_MSG("Nonce length not supported"); |
13606 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
13607 | | return INVALID_PARAMETER; |
13608 | | } |
13609 | | #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */ |
13610 | | *inOutIdx += 1; |
13611 | | if ((*inOutIdx - begin) + nonceLength > size) |
13612 | | return BUFFER_ERROR; |
13613 | | nonce = input + *inOutIdx; |
13614 | | *inOutIdx += nonceLength; |
13615 | | |
13616 | | /* Ticket length. */ |
13617 | | if ((*inOutIdx - begin) + LENGTH_SZ > size) |
13618 | | return BUFFER_ERROR; |
13619 | | ato16(input + *inOutIdx, &length); |
13620 | | *inOutIdx += LENGTH_SZ; |
13621 | | if ((*inOutIdx - begin) + length > size) |
13622 | | return BUFFER_ERROR; |
13623 | | /* note: we reject zero length ticket here, and not in SetTicket(), |
13624 | | * because zero length is valid for TLS 1.2 */ |
13625 | | if (length == 0) |
13626 | | return BUFFER_ERROR; |
13627 | | |
13628 | | if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0) |
13629 | | return ret; |
13630 | | *inOutIdx += length; |
13631 | | |
13632 | | now = TimeNowInMilliseconds(); |
13633 | | if (now == 0) |
13634 | | return GETTIME_ERROR; |
13635 | | /* Copy in ticket data (server identity). */ |
13636 | | ssl->timeout = lifetime; |
13637 | | ssl->session->timeout = lifetime; |
13638 | | ssl->session->cipherSuite0 = ssl->options.cipherSuite0; |
13639 | | ssl->session->cipherSuite = ssl->options.cipherSuite; |
13640 | | ssl->session->ticketSeen = now; |
13641 | | ssl->session->ticketAdd = ageAdd; |
13642 | | #ifdef WOLFSSL_EARLY_DATA |
13643 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
13644 | | #endif |
13645 | | |
13646 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
13647 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
13648 | | ret = SessionTicketNoncePopulate(ssl->session, nonce, nonceLength); |
13649 | | if (ret != 0) |
13650 | | return ret; |
13651 | | #else |
13652 | | ssl->session->ticketNonce.len = nonceLength; |
13653 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
13654 | | ret = BUFFER_ERROR; |
13655 | | return ret; |
13656 | | } |
13657 | | if (nonceLength > 0) |
13658 | | XMEMCPY(ssl->session->ticketNonce.data, nonce, nonceLength); |
13659 | | #endif /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && FIPS_VERSION_GE(5,3) */ |
13660 | | |
13661 | | ssl->session->namedGroup = ssl->namedGroup; |
13662 | | |
13663 | | if ((*inOutIdx - begin) + EXTS_SZ > size) |
13664 | | return BUFFER_ERROR; |
13665 | | ato16(input + *inOutIdx, &length); |
13666 | | *inOutIdx += EXTS_SZ; |
13667 | | if ((*inOutIdx - begin) + length != size) |
13668 | | return BUFFER_ERROR; |
13669 | | #ifdef WOLFSSL_EARLY_DATA |
13670 | | ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket, |
13671 | | NULL); |
13672 | | if (ret != 0) |
13673 | | return ret; |
13674 | | #endif |
13675 | | *inOutIdx += length; |
13676 | | |
13677 | | SetupSession(ssl); |
13678 | | #ifndef NO_SESSION_CACHE |
13679 | | AddSession(ssl); |
13680 | | #endif |
13681 | | |
13682 | | ssl->expect_session_ticket = 0; |
13683 | | #else |
13684 | 0 | (void)ssl; |
13685 | 0 | (void)input; |
13686 | |
|
13687 | 0 | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
13688 | |
|
13689 | 0 | *inOutIdx += size; |
13690 | 0 | #endif /* HAVE_SESSION_TICKET */ |
13691 | |
|
13692 | 0 | WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0); |
13693 | 0 | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO); |
13694 | |
|
13695 | 0 | return 0; |
13696 | 0 | } |
13697 | | #endif /* NO_WOLFSSL_CLIENT */ |
13698 | | |
13699 | | #ifndef NO_WOLFSSL_SERVER |
13700 | | #ifdef HAVE_SESSION_TICKET |
13701 | | |
13702 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
13703 | | /* Offset of the MAC size in the finished message. */ |
13704 | | #define FINISHED_MSG_SIZE_OFFSET 3 |
13705 | | |
13706 | | /* Calculate the resumption secret which includes the unseen client finished |
13707 | | * message. |
13708 | | * |
13709 | | * ssl The SSL/TLS object. |
13710 | | * returns 0 on success, otherwise failure. |
13711 | | */ |
13712 | | static int ExpectedResumptionSecret(WOLFSSL* ssl) |
13713 | | { |
13714 | | int ret; |
13715 | | word32 finishedSz = 0; |
13716 | | byte mac[WC_MAX_DIGEST_SIZE]; |
13717 | | Digest digest; |
13718 | | byte header[] = { 0x14, 0x00, 0x00, 0x00 }; |
13719 | | |
13720 | | XMEMSET(&digest, 0, sizeof(Digest)); |
13721 | | |
13722 | | /* Copy the running hash so we can restore it after. */ |
13723 | | switch (ssl->specs.mac_algorithm) { |
13724 | | #ifndef NO_SHA256 |
13725 | | case sha256_mac: |
13726 | | ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256); |
13727 | | if (ret != 0) |
13728 | | return ret; |
13729 | | break; |
13730 | | #endif |
13731 | | #ifdef WOLFSSL_SHA384 |
13732 | | case sha384_mac: |
13733 | | ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384); |
13734 | | if (ret != 0) |
13735 | | return ret; |
13736 | | break; |
13737 | | #endif |
13738 | | #ifdef WOLFSSL_TLS13_SHA512 |
13739 | | case sha512_mac: |
13740 | | ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512); |
13741 | | if (ret != 0) |
13742 | | return ret; |
13743 | | break; |
13744 | | #endif |
13745 | | #ifdef WOLFSSL_SM3 |
13746 | | case sm3_mac: |
13747 | | ret = wc_Sm3Copy(&ssl->hsHashes->hashSm3, &digest.sm3); |
13748 | | if (ret != 0) |
13749 | | return ret; |
13750 | | break; |
13751 | | #endif |
13752 | | } |
13753 | | |
13754 | | /* Generate the Client's Finished message and hash it. */ |
13755 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac, |
13756 | | &finishedSz); |
13757 | | if (ret != 0) |
13758 | | goto restore; |
13759 | | header[FINISHED_MSG_SIZE_OFFSET] = finishedSz; |
13760 | | #ifdef WOLFSSL_EARLY_DATA |
13761 | | if (ssl->earlyData != no_early_data) { |
13762 | | static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 }; |
13763 | | ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData)); |
13764 | | if (ret != 0) |
13765 | | goto restore; |
13766 | | } |
13767 | | #endif |
13768 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
13769 | | goto restore; |
13770 | | if ((ret = HashRaw(ssl, mac, finishedSz)) != 0) |
13771 | | goto restore; |
13772 | | |
13773 | | if ((ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret)) != 0) |
13774 | | goto restore; |
13775 | | |
13776 | | /* Restore the hash inline with currently seen messages. */ |
13777 | | restore: |
13778 | | switch (ssl->specs.mac_algorithm) { |
13779 | | #ifndef NO_SHA256 |
13780 | | case sha256_mac: |
13781 | | wc_Sha256Free(&ssl->hsHashes->hashSha256); |
13782 | | ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256); |
13783 | | wc_Sha256Free(&digest.sha256); |
13784 | | break; |
13785 | | #endif |
13786 | | #ifdef WOLFSSL_SHA384 |
13787 | | case sha384_mac: |
13788 | | wc_Sha384Free(&ssl->hsHashes->hashSha384); |
13789 | | ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384); |
13790 | | wc_Sha384Free(&digest.sha384); |
13791 | | break; |
13792 | | #endif |
13793 | | #ifdef WOLFSSL_TLS13_SHA512 |
13794 | | case sha512_mac: |
13795 | | wc_Sha512Free(&ssl->hsHashes->hashSha512); |
13796 | | ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512); |
13797 | | wc_Sha512Free(&digest.sha512); |
13798 | | break; |
13799 | | #endif |
13800 | | #ifdef WOLFSSL_SM3 |
13801 | | case sm3_mac: |
13802 | | wc_Sm3Free(&ssl->hsHashes->hashSm3); |
13803 | | ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3); |
13804 | | wc_Sm3Free(&digest.sm3); |
13805 | | break; |
13806 | | #endif |
13807 | | } |
13808 | | |
13809 | | ForceZero(mac, sizeof(mac)); |
13810 | | return ret; |
13811 | | } |
13812 | | #endif |
13813 | | |
13814 | | /* Send New Session Ticket handshake message. |
13815 | | * Message contains the information required to perform resumption. |
13816 | | * |
13817 | | * ssl The SSL/TLS object. |
13818 | | * returns 0 on success, otherwise failure. |
13819 | | */ |
13820 | | static int SendTls13NewSessionTicket(WOLFSSL* ssl) |
13821 | | { |
13822 | | byte* output; |
13823 | | int ret; |
13824 | | word32 length; |
13825 | | int sendSz; |
13826 | | word16 extSz; |
13827 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
13828 | | |
13829 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND); |
13830 | | WOLFSSL_ENTER("SendTls13NewSessionTicket"); |
13831 | | |
13832 | | if (DefTicketHintTooLarge(ssl)) { |
13833 | | WOLFSSL_MSG("Ticket hint exceeds half the ticket key lifetime; " |
13834 | | "skipping ticket"); |
13835 | | return 0; |
13836 | | } |
13837 | | |
13838 | | #ifdef WOLFSSL_DTLS13 |
13839 | | if (ssl->options.dtls) |
13840 | | idx = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
13841 | | #endif /* WOLFSSL_DTLS13 */ |
13842 | | |
13843 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
13844 | | if (!ssl->msgsReceived.got_finished) { |
13845 | | if ((ret = ExpectedResumptionSecret(ssl)) != 0) |
13846 | | return ret; |
13847 | | } |
13848 | | #endif |
13849 | | |
13850 | | /* Start ticket nonce at 0 and go up to 255. */ |
13851 | | if (ssl->session->ticketNonce.len == 0) { |
13852 | | ssl->session->ticketNonce.len = DEF_TICKET_NONCE_SZ; |
13853 | | ssl->session->ticketNonce.data[0] = 0; |
13854 | | } |
13855 | | else |
13856 | | #ifdef WOLFSSL_ASYNC_CRYPT |
13857 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
13858 | | #endif |
13859 | | { |
13860 | | if (ssl->session->ticketNonce.data[0] == 255) { |
13861 | | /* RFC8446 Section 4.6.1: Each ticket must have a unique nonce |
13862 | | * value. As the nonce is only a single byte, we have to prevent |
13863 | | * the overflow and abort. */ |
13864 | | return SESSION_TICKET_NONCE_OVERFLOW; |
13865 | | } |
13866 | | else |
13867 | | ssl->session->ticketNonce.data[0]++; |
13868 | | } |
13869 | | |
13870 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
13871 | | /* In this case we only send the ID as the ticket. Let's generate a new |
13872 | | * ID for the new ticket so that we don't overwrite any old ones */ |
13873 | | ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID, |
13874 | | ID_LEN); |
13875 | | if (ret != 0) |
13876 | | return ret; |
13877 | | ssl->session->haveAltSessionID = 1; |
13878 | | } |
13879 | | |
13880 | | if (!ssl->options.noTicketTls13) { |
13881 | | if ((ret = SetupTicket(ssl)) != 0) |
13882 | | return ret; |
13883 | | /* No need to create the ticket if we only send the ID */ |
13884 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == 0) { |
13885 | | if ((ret = CreateTicket(ssl)) != 0) |
13886 | | return ret; |
13887 | | } |
13888 | | } |
13889 | | |
13890 | | #ifdef WOLFSSL_EARLY_DATA |
13891 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
13892 | | if (ssl->session->maxEarlyDataSz > 0) |
13893 | | TLSX_EarlyData_Use(ssl, ssl->session->maxEarlyDataSz, 1); |
13894 | | extSz = 0; |
13895 | | ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz); |
13896 | | if (ret != 0) |
13897 | | return ret; |
13898 | | #else |
13899 | | extSz = EXTS_SZ; |
13900 | | #endif |
13901 | | /* Lifetime | Age Add | Ticket session ID | Extensions */ |
13902 | | length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ; |
13903 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) |
13904 | | length += ID_LEN + extSz; |
13905 | | else |
13906 | | length += ssl->session->ticketLen + extSz; |
13907 | | /* Nonce */ |
13908 | | length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ; |
13909 | | |
13910 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
13911 | | |
13912 | | /* Check buffers are big enough and grow if needed. */ |
13913 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
13914 | | return ret; |
13915 | | |
13916 | | /* Get position in output buffer to write new message to. */ |
13917 | | output = GetOutputBuffer(ssl); |
13918 | | |
13919 | | /* Put the record and handshake headers on. */ |
13920 | | AddTls13Headers(output, length, session_ticket, ssl); |
13921 | | |
13922 | | /* Lifetime hint */ |
13923 | | c32toa(ssl->ctx->ticketHint, output + idx); |
13924 | | idx += SESSION_HINT_SZ; |
13925 | | /* Age add - obfuscator */ |
13926 | | c32toa(ssl->session->ticketAdd, output + idx); |
13927 | | idx += SESSION_ADD_SZ; |
13928 | | |
13929 | | output[idx++] = ssl->session->ticketNonce.len; |
13930 | | output[idx++] = ssl->session->ticketNonce.data[0]; |
13931 | | |
13932 | | /* length */ |
13933 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
13934 | | c16toa(ID_LEN, output + idx); |
13935 | | } |
13936 | | else { |
13937 | | c16toa(ssl->session->ticketLen, output + idx); |
13938 | | } |
13939 | | |
13940 | | idx += LENGTH_SZ; |
13941 | | /* ticket */ |
13942 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
13943 | | if (ssl->session->haveAltSessionID) |
13944 | | XMEMCPY(output + idx, ssl->session->altSessionID, ID_LEN); |
13945 | | else |
13946 | | return BAD_FUNC_ARG; /* Should not happen */ |
13947 | | idx += ID_LEN; |
13948 | | } |
13949 | | else { |
13950 | | XMEMCPY(output + idx, ssl->session->ticket, ssl->session->ticketLen); |
13951 | | idx += ssl->session->ticketLen; |
13952 | | } |
13953 | | |
13954 | | #ifdef WOLFSSL_EARLY_DATA |
13955 | | extSz = 0; |
13956 | | ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz); |
13957 | | if (ret != 0) |
13958 | | return ret; |
13959 | | idx += extSz; |
13960 | | #else |
13961 | | /* No extension support - empty extensions. */ |
13962 | | c16toa(0, output + idx); |
13963 | | idx += EXTS_SZ; |
13964 | | #endif |
13965 | | |
13966 | | if (idx > WOLFSSL_MAX_16BIT || |
13967 | | sendSz > (int)WOLFSSL_MAX_16BIT) { |
13968 | | return BAD_LENGTH_E; |
13969 | | } |
13970 | | |
13971 | | ssl->options.haveSessionId = 1; |
13972 | | |
13973 | | SetupSession(ssl); |
13974 | | /* Only add to cache when support built in and when the ticket contains |
13975 | | * an ID. Otherwise we have no way to actually retrieve the ticket from the |
13976 | | * cache. */ |
13977 | | #if !defined(NO_SESSION_CACHE) && defined(WOLFSSL_TICKET_HAVE_ID) |
13978 | | AddSession(ssl); |
13979 | | #endif |
13980 | | |
13981 | | #ifdef WOLFSSL_DTLS13 |
13982 | | if (ssl->options.dtls) |
13983 | | return Dtls13HandshakeSend(ssl, output, (word16)sendSz, |
13984 | | (word16)idx, session_ticket, 0); |
13985 | | #endif /* WOLFSSL_DTLS13 */ |
13986 | | |
13987 | | /* This message is always encrypted. */ |
13988 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
13989 | | output + RECORD_HEADER_SZ, |
13990 | | (word16)idx - RECORD_HEADER_SZ, |
13991 | | handshake, 0, 0, 0); |
13992 | | if (sendSz < 0) |
13993 | | return sendSz; |
13994 | | |
13995 | | ssl->buffers.outputBuffer.length += sendSz; |
13996 | | |
13997 | | /* Always send as this is either directly after server's Finished or only |
13998 | | * message after client's Finished. |
13999 | | */ |
14000 | | ret = SendBuffered(ssl); |
14001 | | |
14002 | | WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0); |
14003 | | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND); |
14004 | | |
14005 | | return ret; |
14006 | | } |
14007 | | #endif /* HAVE_SESSION_TICKET */ |
14008 | | #endif /* NO_WOLFSSL_SERVER */ |
14009 | | |
14010 | | /* Make sure no duplicates, no fast forward, or other problems |
14011 | | * |
14012 | | * ssl The SSL/TLS object. |
14013 | | * type Type of handshake message received. |
14014 | | * returns 0 on success, otherwise failure. |
14015 | | */ |
14016 | | static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type) |
14017 | 0 | { |
14018 | | /* verify not a duplicate, mark received, check state */ |
14019 | 0 | switch (type) { |
14020 | | |
14021 | 0 | #ifndef NO_WOLFSSL_SERVER |
14022 | 0 | case client_hello: |
14023 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14024 | | /* Only valid when received on SERVER side. */ |
14025 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
14026 | 0 | WOLFSSL_MSG("ClientHello received by client"); |
14027 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14028 | 0 | return SIDE_ERROR; |
14029 | 0 | } |
14030 | 0 | #endif |
14031 | | /* Check state. */ |
14032 | 0 | if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) { |
14033 | 0 | WOLFSSL_MSG("ClientHello received out of order"); |
14034 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14035 | 0 | return OUT_OF_ORDER_E; |
14036 | 0 | } |
14037 | | /* Check previously seen. */ |
14038 | | /* Initial and after HelloRetryRequest - no more than 2. */ |
14039 | 0 | if (ssl->msgsReceived.got_client_hello == 2) { |
14040 | 0 | WOLFSSL_MSG("Too many ClientHello received"); |
14041 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14042 | 0 | return DUPLICATE_MSG_E; |
14043 | 0 | } |
14044 | | /* Second only after HelloRetryRequest seen. */ |
14045 | 0 | if (ssl->msgsReceived.got_client_hello == 1 && |
14046 | 0 | ssl->options.serverState != |
14047 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14048 | 0 | WOLFSSL_MSG("Duplicate ClientHello received"); |
14049 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14050 | 0 | return DUPLICATE_MSG_E; |
14051 | 0 | } |
14052 | 0 | ssl->msgsReceived.got_client_hello++; |
14053 | |
|
14054 | 0 | break; |
14055 | 0 | #endif |
14056 | | |
14057 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14058 | 0 | case server_hello: |
14059 | 0 | #ifndef NO_WOLFSSL_SERVER |
14060 | | /* Only valid when received on CLIENT side. */ |
14061 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14062 | 0 | WOLFSSL_MSG("ServerHello received by server"); |
14063 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14064 | 0 | return SIDE_ERROR; |
14065 | 0 | } |
14066 | 0 | #endif |
14067 | | /* Check state. */ |
14068 | 0 | if (ssl->options.serverState >= SERVER_HELLO_COMPLETE) { |
14069 | 0 | WOLFSSL_MSG("ServerHello received out of order"); |
14070 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14071 | 0 | return OUT_OF_ORDER_E; |
14072 | 0 | } |
14073 | | /* Check previously seen. */ |
14074 | | /* Only once after ClientHello. |
14075 | | * HelloRetryRequest has ServerHello type but count fixed up later |
14076 | | * - see DoTls13ServerHello(). |
14077 | | */ |
14078 | 0 | if (ssl->msgsReceived.got_server_hello) { |
14079 | 0 | WOLFSSL_MSG("Duplicate ServerHello received"); |
14080 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14081 | 0 | return DUPLICATE_MSG_E; |
14082 | 0 | } |
14083 | 0 | ssl->msgsReceived.got_server_hello = 1; |
14084 | |
|
14085 | 0 | break; |
14086 | 0 | #endif |
14087 | | |
14088 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14089 | 0 | case session_ticket: |
14090 | 0 | #ifndef NO_WOLFSSL_SERVER |
14091 | | /* Only valid when received on CLIENT side. */ |
14092 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14093 | 0 | WOLFSSL_MSG("NewSessionTicket received by server"); |
14094 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14095 | 0 | return SIDE_ERROR; |
14096 | 0 | } |
14097 | 0 | #endif |
14098 | | /* Check state. */ |
14099 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
14100 | | /* Only allowed after server's Finished message. */ |
14101 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14102 | | WOLFSSL_MSG("NewSessionTicket received out of order"); |
14103 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14104 | | return OUT_OF_ORDER_E; |
14105 | | } |
14106 | | #else |
14107 | | /* Only allowed after client's Finished message. */ |
14108 | 0 | if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
14109 | 0 | WOLFSSL_MSG("NewSessionTicket received out of order"); |
14110 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14111 | 0 | return OUT_OF_ORDER_E; |
14112 | 0 | } |
14113 | 0 | #endif |
14114 | | /* Many SessionTickets can be sent. */ |
14115 | 0 | ssl->msgsReceived.got_session_ticket = 1; |
14116 | |
|
14117 | 0 | break; |
14118 | 0 | #endif |
14119 | | |
14120 | 0 | #ifndef NO_WOLFSSL_SERVER |
14121 | | #ifdef WOLFSSL_EARLY_DATA |
14122 | | case end_of_early_data: |
14123 | | #ifndef NO_WOLFSSL_CLIENT |
14124 | | /* Only valid when received on SERVER side. */ |
14125 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
14126 | | WOLFSSL_MSG("EndOfEarlyData received by client"); |
14127 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14128 | | return SIDE_ERROR; |
14129 | | } |
14130 | | #endif |
14131 | | /* Check state. */ |
14132 | | /* Only after server's Finished and before client's Finished. */ |
14133 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14134 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
14135 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14136 | | return OUT_OF_ORDER_E; |
14137 | | } |
14138 | | if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) { |
14139 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
14140 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14141 | | return OUT_OF_ORDER_E; |
14142 | | } |
14143 | | /* Check previously seen. */ |
14144 | | if (ssl->msgsReceived.got_end_of_early_data) { |
14145 | | WOLFSSL_MSG("Too many EndOfEarlyData received"); |
14146 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14147 | | return DUPLICATE_MSG_E; |
14148 | | } |
14149 | | ssl->msgsReceived.got_end_of_early_data = 1; |
14150 | | |
14151 | | break; |
14152 | | #endif |
14153 | 0 | #endif |
14154 | | |
14155 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14156 | 0 | case encrypted_extensions: |
14157 | 0 | #ifndef NO_WOLFSSL_SERVER |
14158 | | /* Only valid when received on CLIENT side. */ |
14159 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14160 | 0 | WOLFSSL_MSG("EncryptedExtensions received by server"); |
14161 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14162 | 0 | return SIDE_ERROR; |
14163 | 0 | } |
14164 | 0 | #endif |
14165 | | /* Check state. */ |
14166 | | /* Must be received directly after ServerHello. |
14167 | | * DoTls13EncryptedExtensions() changes state to: |
14168 | | * SERVER_ENCRYPTED_EXTENSIONS_COMPLETE. |
14169 | | */ |
14170 | 0 | if (ssl->options.serverState != SERVER_HELLO_COMPLETE) { |
14171 | 0 | WOLFSSL_MSG("EncryptedExtensions received out of order"); |
14172 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14173 | 0 | return OUT_OF_ORDER_E; |
14174 | 0 | } |
14175 | | /* Check previously seen. */ |
14176 | 0 | if (ssl->msgsReceived.got_encrypted_extensions) { |
14177 | 0 | WOLFSSL_MSG("Duplicate EncryptedExtensions received"); |
14178 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14179 | 0 | return DUPLICATE_MSG_E; |
14180 | 0 | } |
14181 | 0 | ssl->msgsReceived.got_encrypted_extensions = 1; |
14182 | |
|
14183 | 0 | break; |
14184 | 0 | #endif |
14185 | | |
14186 | 0 | case certificate: |
14187 | | /* Valid on both sides. */ |
14188 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14189 | | /* Check state. */ |
14190 | | /* On client, seen after EncryptedExtension and CertificateRequest |
14191 | | * (if sent) and before CertificateVerify and Finished. |
14192 | | * DoTls13Certificate() sets serverState to SERVER_CERT_COMPLETE. |
14193 | | */ |
14194 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
14195 | 0 | ssl->options.serverState != |
14196 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
14197 | 0 | WOLFSSL_MSG("Certificate received out of order - Client"); |
14198 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14199 | 0 | return OUT_OF_ORDER_E; |
14200 | 0 | } |
14201 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14202 | | /* Server's authenticating with PSK must not send this. */ |
14203 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
14204 | | ssl->options.serverState == SERVER_CERT_COMPLETE && |
14205 | | ssl->options.pskNegotiated |
14206 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
14207 | | && !ssl->options.certWithExternPsk |
14208 | | #endif |
14209 | | ) { |
14210 | | WOLFSSL_MSG("Certificate received while using PSK"); |
14211 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14212 | | return SANITY_MSG_E; |
14213 | | } |
14214 | | #endif |
14215 | 0 | #endif |
14216 | 0 | #ifndef NO_WOLFSSL_SERVER |
14217 | | /* Check state. */ |
14218 | | /* On Server, valid after ClientHello received and ServerFinished |
14219 | | * sent. */ |
14220 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
14221 | 0 | ssl->options.clientState != CLIENT_HELLO_COMPLETE && |
14222 | 0 | ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14223 | 0 | WOLFSSL_MSG("Certificate received out of order - Server"); |
14224 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14225 | 0 | return OUT_OF_ORDER_E; |
14226 | 0 | } |
14227 | 0 | #endif |
14228 | | /* Check previously seen. */ |
14229 | 0 | if (ssl->msgsReceived.got_certificate) { |
14230 | 0 | WOLFSSL_MSG("Duplicate Certificate received"); |
14231 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14232 | 0 | return DUPLICATE_MSG_E; |
14233 | 0 | } |
14234 | 0 | ssl->msgsReceived.got_certificate = 1; |
14235 | |
|
14236 | 0 | break; |
14237 | | |
14238 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14239 | 0 | case certificate_request: |
14240 | 0 | #ifndef NO_WOLFSSL_SERVER |
14241 | | /* Only valid when received on CLIENT side. */ |
14242 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14243 | 0 | WOLFSSL_MSG("CertificateRequest received by server"); |
14244 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14245 | 0 | return SIDE_ERROR; |
14246 | 0 | } |
14247 | 0 | #endif |
14248 | | /* Check state. */ |
14249 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
14250 | | /* Only valid when sent after EncryptedExtensions and before |
14251 | | * Certificate. */ |
14252 | 0 | if (ssl->options.serverState != |
14253 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
14254 | 0 | WOLFSSL_MSG("CertificateRequest received out of order"); |
14255 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14256 | 0 | return OUT_OF_ORDER_E; |
14257 | 0 | } |
14258 | | #else |
14259 | | /* Valid when sent after EncryptedExtensions and before Certificate |
14260 | | * and after both client and server have sent Finished (Post |
14261 | | * Handshake Authentication). */ |
14262 | | if (ssl->options.serverState != |
14263 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE && |
14264 | | (ssl->options.serverState < SERVER_FINISHED_COMPLETE || |
14265 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) { |
14266 | | WOLFSSL_MSG("CertificateRequest received out of order"); |
14267 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14268 | | return OUT_OF_ORDER_E; |
14269 | | } |
14270 | | /* RFC 8446 4.6.2: A client that receives a post-handshake |
14271 | | * CertificateRequest message without having sent the |
14272 | | * "post_handshake_auth" extension MUST send an |
14273 | | * "unexpected_message" fatal alert. wolfSSL_allow_post_handshake_auth() |
14274 | | * must be called before wolfSSL_connect() so postHandshakeAuth |
14275 | | * reflects whether the extension was offered. */ |
14276 | | if (ssl->options.serverState >= SERVER_FINISHED_COMPLETE && |
14277 | | ssl->options.clientState == CLIENT_FINISHED_COMPLETE && |
14278 | | !ssl->options.postHandshakeAuth) { |
14279 | | WOLFSSL_MSG("Post-handshake CertificateRequest received " |
14280 | | "without having sent post_handshake_auth " |
14281 | | "extension"); |
14282 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14283 | | return OUT_OF_ORDER_E; |
14284 | | } |
14285 | | #endif |
14286 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14287 | | /* Server's authenticating with PSK must not send this. */ |
14288 | | if (ssl->options.pskNegotiated |
14289 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
14290 | | && !ssl->options.certWithExternPsk |
14291 | | #endif |
14292 | | ) { |
14293 | | WOLFSSL_MSG("CertificateRequest received while using PSK"); |
14294 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14295 | | return SANITY_MSG_E; |
14296 | | } |
14297 | | #endif |
14298 | | /* Check previously seen. */ |
14299 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
14300 | | /* Only once during handshake. */ |
14301 | 0 | if (ssl->msgsReceived.got_certificate_request) { |
14302 | 0 | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
14303 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14304 | 0 | return DUPLICATE_MSG_E; |
14305 | 0 | } |
14306 | | #else |
14307 | | /* Only once during handshake. */ |
14308 | | if (ssl->msgsReceived.got_certificate_request && |
14309 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE) { |
14310 | | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
14311 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14312 | | return DUPLICATE_MSG_E; |
14313 | | } |
14314 | | #endif |
14315 | 0 | ssl->msgsReceived.got_certificate_request = 1; |
14316 | |
|
14317 | 0 | break; |
14318 | 0 | #endif |
14319 | | |
14320 | 0 | case certificate_verify: |
14321 | | /* Valid on both sides. */ |
14322 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14323 | | /* Check state on client. |
14324 | | * Valid only directly after a Certificate message. */ |
14325 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
14326 | 0 | if (ssl->options.serverState != SERVER_CERT_COMPLETE) { |
14327 | 0 | WOLFSSL_MSG("No Cert before CertVerify"); |
14328 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14329 | 0 | return OUT_OF_ORDER_E; |
14330 | 0 | } |
14331 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14332 | | /* Server's authenticating with PSK must not send this. */ |
14333 | | if (ssl->options.pskNegotiated |
14334 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
14335 | | && !ssl->options.certWithExternPsk |
14336 | | #endif |
14337 | | ) { |
14338 | | WOLFSSL_MSG("CertificateVerify received while using PSK"); |
14339 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14340 | | return SANITY_MSG_E; |
14341 | | } |
14342 | | #endif |
14343 | 0 | } |
14344 | 0 | #endif |
14345 | 0 | #ifndef NO_WOLFSSL_SERVER |
14346 | | /* Check state on server. */ |
14347 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14348 | | /* Server must have sent Finished message. */ |
14349 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14350 | 0 | WOLFSSL_MSG("CertificateVerify received out of order"); |
14351 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14352 | 0 | return OUT_OF_ORDER_E; |
14353 | 0 | } |
14354 | | /* Valid only directly after a Certificate message. */ |
14355 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14356 | 0 | WOLFSSL_MSG("CertificateVerify before ClientHello done"); |
14357 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14358 | 0 | return OUT_OF_ORDER_E; |
14359 | 0 | } |
14360 | 0 | if (!ssl->msgsReceived.got_certificate) { |
14361 | 0 | WOLFSSL_MSG("No Cert before CertificateVerify"); |
14362 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14363 | 0 | return OUT_OF_ORDER_E; |
14364 | 0 | } |
14365 | 0 | } |
14366 | 0 | #endif |
14367 | | /* Check previously seen. */ |
14368 | 0 | if (ssl->msgsReceived.got_certificate_verify) { |
14369 | 0 | WOLFSSL_MSG("Duplicate CertificateVerify received"); |
14370 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14371 | 0 | return DUPLICATE_MSG_E; |
14372 | 0 | } |
14373 | 0 | ssl->msgsReceived.got_certificate_verify = 1; |
14374 | |
|
14375 | 0 | break; |
14376 | | |
14377 | 0 | case finished: |
14378 | | /* Valid on both sides. */ |
14379 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14380 | | /* Check state on client. */ |
14381 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
14382 | | /* After sending ClientHello */ |
14383 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14384 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
14385 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14386 | 0 | return OUT_OF_ORDER_E; |
14387 | 0 | } |
14388 | | /* Must have seen certificate and verify from server except when |
14389 | | * using PSK. */ |
14390 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14391 | | if (ssl->options.pskNegotiated) { |
14392 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
14393 | | if (ssl->options.certWithExternPsk) { |
14394 | | if (ssl->options.serverState != |
14395 | | SERVER_CERT_VERIFY_COMPLETE) { |
14396 | | WOLFSSL_MSG("Finished received out of order - " |
14397 | | "cert_with_extern_psk"); |
14398 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14399 | | return OUT_OF_ORDER_E; |
14400 | | } |
14401 | | } |
14402 | | else |
14403 | | #endif |
14404 | | { |
14405 | | if (ssl->options.serverState != |
14406 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
14407 | | WOLFSSL_MSG("Finished received out of order - PSK"); |
14408 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14409 | | return OUT_OF_ORDER_E; |
14410 | | } |
14411 | | } |
14412 | | } |
14413 | | else |
14414 | | #endif |
14415 | 0 | if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) { |
14416 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
14417 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14418 | 0 | return OUT_OF_ORDER_E; |
14419 | 0 | } |
14420 | 0 | } |
14421 | 0 | #endif |
14422 | 0 | #ifndef NO_WOLFSSL_SERVER |
14423 | | /* Check state on server. */ |
14424 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14425 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14426 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
14427 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14428 | 0 | return OUT_OF_ORDER_E; |
14429 | 0 | } |
14430 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
14431 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
14432 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14433 | 0 | return OUT_OF_ORDER_E; |
14434 | 0 | } |
14435 | | #ifdef WOLFSSL_EARLY_DATA |
14436 | | if (ssl->earlyData == process_early_data && |
14437 | | /* early data may be lost when using DTLS */ |
14438 | | !ssl->options.dtls |
14439 | | /* QUIC does not use EndOfEarlyData records */ |
14440 | | && !WOLFSSL_IS_QUIC(ssl)) { |
14441 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14442 | | return OUT_OF_ORDER_E; |
14443 | | } |
14444 | | #endif |
14445 | 0 | } |
14446 | 0 | #endif |
14447 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14448 | | if (!ssl->options.pskNegotiated |
14449 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
14450 | | || ssl->options.certWithExternPsk |
14451 | | #endif |
14452 | | ) |
14453 | | #endif |
14454 | 0 | { |
14455 | | /* Must have received a Certificate message from client if |
14456 | | * verifying the peer. Empty certificate message indicates |
14457 | | * no certificate available. |
14458 | | */ |
14459 | 0 | if (ssl->options.verifyPeer && |
14460 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
14461 | | /* The post-handshake-auth exemption is only valid during |
14462 | | * the initial handshake. On the server, once a |
14463 | | * post-handshake CertificateRequest is outstanding |
14464 | | * (certReqCtx != NULL), a Certificate is required again. |
14465 | | * Scoped to the server: certReqCtx means something |
14466 | | * different on the client (a received request) and the |
14467 | | * client does not process an inbound Finished in that |
14468 | | * state. Whether an empty Certificate is then accepted |
14469 | | * follows the verify mode (FAIL_IF_NO_PEER_CERT), exactly |
14470 | | * as for first-handshake client authentication. */ |
14471 | | (!ssl->options.verifyPostHandshake || |
14472 | | (ssl->options.side == WOLFSSL_SERVER_END && |
14473 | | ssl->certReqCtx != NULL)) && |
14474 | | #endif |
14475 | 0 | !ssl->msgsReceived.got_certificate) { |
14476 | 0 | WOLFSSL_MSG("Finished received out of order - " |
14477 | 0 | "missing Certificate message"); |
14478 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14479 | 0 | return OUT_OF_ORDER_E; |
14480 | 0 | } |
14481 | | /* Mutual authentication on server requires a certificate from |
14482 | | * peer. Verify peer set on client side requires a certificate |
14483 | | * from peer as not doing PSK. |
14484 | | */ |
14485 | 0 | if ((ssl->options.mutualAuth || |
14486 | 0 | (ssl->options.side == WOLFSSL_CLIENT_END && |
14487 | 0 | ssl->options.verifyPeer)) && !ssl->options.havePeerCert) { |
14488 | 0 | WOLFSSL_MSG("Finished received out of order - " |
14489 | 0 | "no valid certificate"); |
14490 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14491 | 0 | return OUT_OF_ORDER_E; |
14492 | 0 | } |
14493 | | /* Must have received a valid CertificateVerify if verifying |
14494 | | * peer and got a peer certificate. |
14495 | | */ |
14496 | 0 | if ((ssl->options.mutualAuth || ssl->options.verifyPeer) && |
14497 | 0 | ssl->options.havePeerCert && !ssl->options.havePeerVerify) { |
14498 | 0 | WOLFSSL_MSG("Finished received out of order - " |
14499 | 0 | "Certificate message but no CertificateVerify"); |
14500 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14501 | 0 | return OUT_OF_ORDER_E; |
14502 | 0 | } |
14503 | 0 | } |
14504 | | /* Check previously seen. */ |
14505 | 0 | if (ssl->msgsReceived.got_finished) { |
14506 | 0 | WOLFSSL_MSG("Duplicate Finished received"); |
14507 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14508 | 0 | return DUPLICATE_MSG_E; |
14509 | 0 | } |
14510 | 0 | ssl->msgsReceived.got_finished = 1; |
14511 | |
|
14512 | 0 | break; |
14513 | | |
14514 | 0 | case key_update: |
14515 | | /* Valid on both sides. */ |
14516 | | #ifdef WOLFSSL_QUIC |
14517 | | /* RFC 9001 Section 6: QUIC performs key updates at the QUIC |
14518 | | * packet-protection layer, so a TLS KeyUpdate message must be |
14519 | | * rejected as a fatal unexpected_message connection error. */ |
14520 | | if (WOLFSSL_IS_QUIC(ssl)) { |
14521 | | WOLFSSL_MSG("KeyUpdate received over QUIC"); |
14522 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14523 | | return SANITY_MSG_E; |
14524 | | } |
14525 | | #endif |
14526 | | /* Check state. |
14527 | | * Client and server must have received finished message from other |
14528 | | * side. |
14529 | | */ |
14530 | 0 | if (!ssl->msgsReceived.got_finished) { |
14531 | 0 | WOLFSSL_MSG("No KeyUpdate before Finished"); |
14532 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14533 | 0 | return OUT_OF_ORDER_E; |
14534 | 0 | } |
14535 | | /* Multiple KeyUpdates can be sent. */ |
14536 | 0 | break; |
14537 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
14538 | | case hello_verify_request: |
14539 | | if (!ssl->options.dtls) { |
14540 | | WOLFSSL_MSG("HelloVerifyRequest when not in DTLS"); |
14541 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14542 | | return OUT_OF_ORDER_E; |
14543 | | } |
14544 | | if (ssl->msgsReceived.got_hello_verify_request) { |
14545 | | WOLFSSL_MSG("Duplicate HelloVerifyRequest received"); |
14546 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14547 | | return DUPLICATE_MSG_E; |
14548 | | } |
14549 | | ssl->msgsReceived.got_hello_verify_request = 1; |
14550 | | if (ssl->msgsReceived.got_hello_retry_request) { |
14551 | | WOLFSSL_MSG( |
14552 | | "Both HelloVerifyRequest and HelloRetryRequest received"); |
14553 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
14554 | | return DUPLICATE_MSG_E; |
14555 | | } |
14556 | | if (ssl->options.serverState >= |
14557 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE || |
14558 | | ssl->options.connectState != CLIENT_HELLO_SENT) { |
14559 | | WOLFSSL_MSG("HelloVerifyRequest received out of order"); |
14560 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14561 | | return OUT_OF_ORDER_E; |
14562 | | } |
14563 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14564 | | WOLFSSL_MSG("HelloVerifyRequest received on the server"); |
14565 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
14566 | | return SIDE_ERROR; |
14567 | | } |
14568 | | if (!ssl->options.downgrade || |
14569 | | ssl->options.minDowngrade < DTLSv1_2_MINOR) { |
14570 | | WOLFSSL_MSG( |
14571 | | "HelloVerifyRequest received but not DTLSv1.2 allowed"); |
14572 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
14573 | | return VERSION_ERROR; |
14574 | | } |
14575 | | break; |
14576 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_TLS12*/ |
14577 | | |
14578 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS_CID) |
14579 | | case request_connection_id: |
14580 | | case new_connection_id: |
14581 | | { |
14582 | | CIDInfo* cidInfo = ssl->dtlsCidInfo; |
14583 | | |
14584 | | /* DTLS 1.3 only (RFC 9147) */ |
14585 | | if (!ssl->options.dtls) { |
14586 | | WOLFSSL_MSG("CID message received but not DTLS"); |
14587 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14588 | | return SANITY_MSG_E; |
14589 | | } |
14590 | | /* RFC 9147 Section 9: if CIDs were not negotiated, MUST abort |
14591 | | * with an unexpected_message alert */ |
14592 | | if (cidInfo == NULL || !cidInfo->negotiated) { |
14593 | | WOLFSSL_MSG("CID message received but CID not negotiated"); |
14594 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14595 | | return SANITY_MSG_E; |
14596 | | } |
14597 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) { |
14598 | | WOLFSSL_MSG("CID message received out of order"); |
14599 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14600 | | return OUT_OF_ORDER_E; |
14601 | | } |
14602 | | if (type == request_connection_id) { |
14603 | | /* the peer MUST NOT request CIDs while sending an empty |
14604 | | * CID itself */ |
14605 | | if (cidInfo->rx == NULL || cidInfo->rx->length == 0) { |
14606 | | WOLFSSL_MSG("RequestConnectionId from peer sending an " |
14607 | | "empty CID"); |
14608 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14609 | | return SANITY_MSG_E; |
14610 | | } |
14611 | | } |
14612 | | else { |
14613 | | /* the peer MUST NOT issue CIDs after negotiating receiving |
14614 | | * an empty CID */ |
14615 | | if (cidInfo->tx == NULL || cidInfo->tx->length == 0) { |
14616 | | WOLFSSL_MSG("NewConnectionId from peer that negotiated " |
14617 | | "an empty CID"); |
14618 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14619 | | return SANITY_MSG_E; |
14620 | | } |
14621 | | } |
14622 | | break; |
14623 | | } |
14624 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_DTLS_CID */ |
14625 | | |
14626 | 0 | default: |
14627 | 0 | WOLFSSL_MSG("Unknown message type"); |
14628 | 0 | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
14629 | 0 | return SANITY_MSG_E; |
14630 | 0 | } |
14631 | | |
14632 | 0 | return 0; |
14633 | 0 | } |
14634 | | |
14635 | | /* Handle a type of handshake message that has been received. |
14636 | | * |
14637 | | * ssl The SSL/TLS object. |
14638 | | * input The message buffer. |
14639 | | * inOutIdx On entry, the index into the buffer of the current message. |
14640 | | * On exit, the index into the buffer of the next message. |
14641 | | * size The length of the current handshake message. |
14642 | | * totalSz Length of remaining data in the message buffer. |
14643 | | * returns 0 on success and otherwise failure. |
14644 | | */ |
14645 | | int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
14646 | | byte type, word32 size, word32 totalSz) |
14647 | 0 | { |
14648 | 0 | int ret = 0, tmp; |
14649 | 0 | word32 inIdx = *inOutIdx; |
14650 | 0 | int alertType; |
14651 | | #if defined(HAVE_ECH) && !defined(NO_WOLFSSL_SERVER) |
14652 | | TLSX* echX = NULL; |
14653 | | word32 echInOutIdx; |
14654 | | #endif |
14655 | |
|
14656 | 0 | (void)totalSz; |
14657 | |
|
14658 | 0 | WOLFSSL_ENTER("DoTls13HandShakeMsgType"); |
14659 | | |
14660 | | /* make sure we can read the message */ |
14661 | 0 | if (*inOutIdx + size > totalSz) |
14662 | 0 | return INCOMPLETE_DATA; |
14663 | | |
14664 | | /* sanity check msg received */ |
14665 | 0 | if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) { |
14666 | 0 | WOLFSSL_MSG("Sanity Check on handshake message type received failed"); |
14667 | 0 | if (ret == WC_NO_ERR_TRACE(VERSION_ERROR)) |
14668 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
14669 | 0 | else |
14670 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
14671 | 0 | return ret; |
14672 | 0 | } |
14673 | | |
14674 | | #if defined(WOLFSSL_CALLBACKS) |
14675 | | /* add name later, add on record and handshake header part back on */ |
14676 | | if (ssl->toInfoOn) { |
14677 | | ret = AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - |
14678 | | HANDSHAKE_HEADER_SZ, size + HANDSHAKE_HEADER_SZ, READ_PROTO, |
14679 | | RECORD_HEADER_SZ, ssl->heap); |
14680 | | if (ret != 0) |
14681 | | return ret; |
14682 | | AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo); |
14683 | | } |
14684 | | #endif |
14685 | | |
14686 | 0 | if (ssl->options.handShakeState == HANDSHAKE_DONE && |
14687 | 0 | type != session_ticket && type != certificate_request && |
14688 | 0 | type != certificate && type != key_update && type != finished |
14689 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS_CID) |
14690 | | && type != request_connection_id && type != new_connection_id |
14691 | | #endif |
14692 | 0 | ) { |
14693 | 0 | WOLFSSL_MSG("HandShake message after handshake complete"); |
14694 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
14695 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14696 | 0 | return OUT_OF_ORDER_E; |
14697 | 0 | } |
14698 | | |
14699 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
14700 | 0 | ssl->options.serverState == NULL_STATE && |
14701 | 0 | type != server_hello && type != hello_retry_request |
14702 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
14703 | | && (!ssl->options.dtls || type != hello_verify_request) |
14704 | | #endif /* defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) */ |
14705 | 0 | ) { |
14706 | 0 | WOLFSSL_MSG("First server message not server hello"); |
14707 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
14708 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14709 | 0 | return OUT_OF_ORDER_E; |
14710 | 0 | } |
14711 | | |
14712 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
14713 | 0 | ssl->options.clientState == NULL_STATE && type != client_hello) { |
14714 | 0 | WOLFSSL_MSG("First client message not client hello"); |
14715 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
14716 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
14717 | 0 | return OUT_OF_ORDER_E; |
14718 | 0 | } |
14719 | | |
14720 | | /* above checks handshake state */ |
14721 | 0 | switch (type) { |
14722 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14723 | | /* Messages only received by client. */ |
14724 | 0 | case server_hello: |
14725 | 0 | WOLFSSL_MSG("processing server hello"); |
14726 | 0 | ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type); |
14727 | 0 | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
14728 | 0 | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
14729 | 0 | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
14730 | 0 | if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) || |
14731 | 0 | IsAtLeastTLSv1_3(ssl->version)) { |
14732 | 0 | ssl->options.cacheMessages = 0; |
14733 | 0 | if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) { |
14734 | 0 | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
14735 | 0 | XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES); |
14736 | 0 | ssl->hsHashes->messages = NULL; |
14737 | 0 | } |
14738 | 0 | } |
14739 | 0 | #endif |
14740 | 0 | break; |
14741 | | |
14742 | 0 | case encrypted_extensions: |
14743 | 0 | WOLFSSL_MSG("processing encrypted extensions"); |
14744 | 0 | ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size); |
14745 | 0 | break; |
14746 | | |
14747 | 0 | #ifndef NO_CERTS |
14748 | 0 | case certificate_request: |
14749 | 0 | WOLFSSL_MSG("processing certificate request"); |
14750 | 0 | ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size); |
14751 | 0 | break; |
14752 | 0 | #endif |
14753 | | |
14754 | 0 | case session_ticket: |
14755 | 0 | WOLFSSL_MSG("processing new session ticket"); |
14756 | 0 | ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size); |
14757 | 0 | break; |
14758 | 0 | #endif /* !NO_WOLFSSL_CLIENT */ |
14759 | | |
14760 | 0 | #ifndef NO_WOLFSSL_SERVER |
14761 | | /* Messages only received by server. */ |
14762 | 0 | case client_hello: |
14763 | 0 | WOLFSSL_MSG("processing client hello"); |
14764 | | #if defined(HAVE_ECH) |
14765 | | /* keep the start idx so we can restore it for the inner call */ |
14766 | | echInOutIdx = *inOutIdx; |
14767 | | #endif |
14768 | 0 | ret = DoTls13ClientHello(ssl, input, inOutIdx, size); |
14769 | 0 | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
14770 | 0 | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
14771 | 0 | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
14772 | 0 | if ((ssl->options.resuming || !ssl->options.verifyPeer || |
14773 | 0 | !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) |
14774 | | #ifdef WOLFSSL_DTLS13 |
14775 | | && (!ssl->options.dtls) |
14776 | | #endif |
14777 | 0 | ) { |
14778 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
14779 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E) && |
14780 | | ret != WC_NO_ERR_TRACE(OCSP_WANT_READ)) |
14781 | | #endif |
14782 | 0 | { |
14783 | 0 | ssl->options.cacheMessages = 0; |
14784 | 0 | if ((ssl->hsHashes != NULL) && |
14785 | 0 | (ssl->hsHashes->messages != NULL)) { |
14786 | 0 | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
14787 | 0 | XFREE(ssl->hsHashes->messages, ssl->heap, |
14788 | 0 | DYNAMIC_TYPE_HASHES); |
14789 | 0 | ssl->hsHashes->messages = NULL; |
14790 | 0 | } |
14791 | 0 | } |
14792 | 0 | } |
14793 | 0 | #endif |
14794 | | #if defined(HAVE_ECH) |
14795 | | if (ret == 0) { |
14796 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
14797 | | |
14798 | | if (echX != NULL && |
14799 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
14800 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
14801 | | byte copyRandom = ((WOLFSSL_ECH*)echX->data)->innerCount == 0; |
14802 | | /* reset the inOutIdx to the outer start */ |
14803 | | *inOutIdx = echInOutIdx; |
14804 | | /* call again with the inner hello */ |
14805 | | if (ret == 0) { |
14806 | | ssl->options.echProcessingInner = 1; |
14807 | | ret = DoTls13ClientHello(ssl, |
14808 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello, |
14809 | | &echInOutIdx, |
14810 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen); |
14811 | | ssl->options.echProcessingInner = 0; |
14812 | | } |
14813 | | if (ret == 0 && ((WOLFSSL_ECH*)echX->data)->state != |
14814 | | ECH_PARSED_INTERNAL) { |
14815 | | WOLFSSL_MSG("ECH: inner ClientHello missing ECH extension"); |
14816 | | ret = INVALID_PARAMETER; |
14817 | | } |
14818 | | /* if the inner ech parsed successfully we have successfully |
14819 | | * handled the hello and can skip the whole message */ |
14820 | | if (ret == 0) { |
14821 | | /* Copy inner client random for ECH acceptance calculation. |
14822 | | * Only on first inner ClientHello (before HRR), not CH2. */ |
14823 | | if (copyRandom) { |
14824 | | XMEMCPY(ssl->arrays->clientRandomInner, |
14825 | | ssl->arrays->clientRandom, RAN_LEN); |
14826 | | } |
14827 | | *inOutIdx += size; |
14828 | | } |
14829 | | } |
14830 | | } |
14831 | | #endif /* HAVE_ECH */ |
14832 | 0 | break; |
14833 | | |
14834 | | #ifdef WOLFSSL_EARLY_DATA |
14835 | | case end_of_early_data: |
14836 | | WOLFSSL_MSG("processing end of early data"); |
14837 | | ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size); |
14838 | | break; |
14839 | | #endif |
14840 | 0 | #endif /* !NO_WOLFSSL_SERVER */ |
14841 | | |
14842 | | /* Messages received by both client and server. */ |
14843 | 0 | #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \ |
14844 | 0 | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
14845 | 0 | case certificate: |
14846 | 0 | WOLFSSL_MSG("processing certificate"); |
14847 | 0 | ret = DoTls13Certificate(ssl, input, inOutIdx, size); |
14848 | 0 | break; |
14849 | 0 | #endif |
14850 | | |
14851 | 0 | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
14852 | 0 | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
14853 | 0 | defined(WOLFSSL_HAVE_MLDSA) || defined(WOLFSSL_HAVE_SLHDSA)) && \ |
14854 | 0 | !defined(NO_CERTS) |
14855 | 0 | case certificate_verify: |
14856 | 0 | WOLFSSL_MSG("processing certificate verify"); |
14857 | 0 | ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size); |
14858 | 0 | break; |
14859 | 0 | #endif |
14860 | 0 | case finished: |
14861 | 0 | WOLFSSL_MSG("processing finished"); |
14862 | 0 | ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF); |
14863 | 0 | break; |
14864 | | |
14865 | 0 | case key_update: |
14866 | 0 | WOLFSSL_MSG("processing key update"); |
14867 | 0 | ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size); |
14868 | 0 | break; |
14869 | | |
14870 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS_CID) |
14871 | | case request_connection_id: |
14872 | | WOLFSSL_MSG("processing request connection id"); |
14873 | | ret = DoDtls13RequestConnectionId(ssl, input, inOutIdx, size); |
14874 | | break; |
14875 | | |
14876 | | case new_connection_id: |
14877 | | WOLFSSL_MSG("processing new connection id"); |
14878 | | ret = DoDtls13NewConnectionId(ssl, input, inOutIdx, size); |
14879 | | break; |
14880 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_DTLS_CID */ |
14881 | | |
14882 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
14883 | | !defined(NO_WOLFSSL_CLIENT) |
14884 | | case hello_verify_request: |
14885 | | WOLFSSL_MSG("processing hello verify request"); |
14886 | | ret = DoHelloVerifyRequest(ssl, input, inOutIdx, size); |
14887 | | break; |
14888 | | #endif |
14889 | 0 | default: |
14890 | 0 | WOLFSSL_MSG("Unknown handshake message type"); |
14891 | 0 | ret = UNKNOWN_HANDSHAKE_TYPE; |
14892 | 0 | break; |
14893 | 0 | } |
14894 | | |
14895 | 0 | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_ASYNC_IO) |
14896 | | /* if async, offset index so this msg will be processed again */ |
14897 | | /* NOTE: check this now before other calls can overwrite ret */ |
14898 | 0 | if ((ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
14899 | 0 | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) && *inOutIdx > 0) { |
14900 | | /* DTLS always stores a message in a buffer when async is enable, so we |
14901 | | * don't need to adjust for the extra bytes here (*inOutIdx is always |
14902 | | * == 0) */ |
14903 | 0 | *inOutIdx -= HANDSHAKE_HEADER_SZ; |
14904 | 0 | } |
14905 | | |
14906 | | /* make sure async error is cleared */ |
14907 | 0 | if (ret == 0 && |
14908 | 0 | (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E) || |
14909 | 0 | ssl->error == WC_NO_ERR_TRACE(OCSP_WANT_READ))) { |
14910 | 0 | ssl->error = 0; |
14911 | 0 | } |
14912 | 0 | #endif |
14913 | | #if defined(HAVE_WRITE_DUP) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
14914 | | /* Read side: a fresh PHA CertificateRequest. Resume from the transcript |
14915 | | * the write side published after the previous PHA response, so this CR is |
14916 | | * hashed onto the same base the server has. */ |
14917 | | if (ret == 0 && type == certificate_request && |
14918 | | ssl->options.side == WOLFSSL_CLIENT_END && |
14919 | | ssl->dupSide == READ_DUP_SIDE && |
14920 | | ssl->options.handShakeState == HANDSHAKE_DONE && |
14921 | | ssl->dupWrite != NULL) { |
14922 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
14923 | | return BAD_MUTEX_E; |
14924 | | if (ssl->dupWrite->postHandshakeSyncedHashState != NULL) { |
14925 | | FreeHandshakeHashes(ssl); |
14926 | | ssl->hsHashes = ssl->dupWrite->postHandshakeSyncedHashState; |
14927 | | ssl->dupWrite->postHandshakeSyncedHashState = NULL; |
14928 | | } |
14929 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
14930 | | } |
14931 | | #endif /* HAVE_WRITE_DUP && WOLFSSL_POST_HANDSHAKE_AUTH */ |
14932 | 0 | if (ret == 0 && type != client_hello && type != session_ticket && |
14933 | 0 | type != key_update |
14934 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS_CID) |
14935 | | && type != request_connection_id && type != new_connection_id |
14936 | | #endif |
14937 | 0 | ) { |
14938 | 0 | ret = HashInput(ssl, input + inIdx, (int)size); |
14939 | 0 | } |
14940 | |
|
14941 | 0 | alertType = TranslateErrorToAlert(ret); |
14942 | | |
14943 | | /* Skip when a fatal alert already went out for this error. The message |
14944 | | * handlers reached above send their own, more specific alert before |
14945 | | * returning (a protocol_version from the version checks in DoClientHello, |
14946 | | * decode_error, illegal_parameter, inappropriate_fallback), and a second |
14947 | | * fatal alert on a connection that is already being torn down is not a |
14948 | | * message the peer can act on. Matches the guard in |
14949 | | * SendFatalAlertOnly(). */ |
14950 | 0 | if (alertType != invalid_alert && |
14951 | 0 | ssl->alert_history.last_tx.level != alert_fatal) { |
14952 | | #ifdef WOLFSSL_DTLS13 |
14953 | | if (type == client_hello && ssl->options.dtls) |
14954 | | DtlsSetSeqNumForReply(ssl); |
14955 | | #endif |
14956 | 0 | tmp = SendAlert(ssl, alert_fatal, alertType); |
14957 | | /* propagate socket error instead of tls error to be sure the error is |
14958 | | * not ignored by DTLS code */ |
14959 | 0 | if (tmp == WC_NO_ERR_TRACE(SOCKET_ERROR_E)) |
14960 | 0 | ret = SOCKET_ERROR_E; |
14961 | 0 | } |
14962 | |
|
14963 | 0 | if (ret == 0 && ssl->options.tls1_3) { |
14964 | | /* Need to hash input message before deriving secrets. */ |
14965 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14966 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
14967 | 0 | if (type == server_hello) { |
14968 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
14969 | 0 | return ret; |
14970 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
14971 | 0 | return ret; |
14972 | | |
14973 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
14974 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
14975 | 0 | return ret; |
14976 | 0 | } |
14977 | | #ifdef WOLFSSL_EARLY_DATA |
14978 | | if (ssl->earlyData != no_early_data) { |
14979 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
14980 | | return ret; |
14981 | | } |
14982 | | else |
14983 | | #endif |
14984 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
14985 | 0 | return ret; |
14986 | |
|
14987 | | #ifdef WOLFSSL_DTLS13 |
14988 | | if (ssl->options.dtls) { |
14989 | | w64wrapper epochHandshake; |
14990 | | epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
14991 | | ssl->dtls13Epoch = epochHandshake; |
14992 | | ssl->dtls13PeerEpoch = epochHandshake; |
14993 | | |
14994 | | ret = Dtls13SetEpochKeys( |
14995 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
14996 | | if (ret != 0) |
14997 | | return ret; |
14998 | | |
14999 | | } |
15000 | | #endif /* WOLFSSL_DTLS13 */ |
15001 | 0 | } |
15002 | | |
15003 | 0 | if (type == finished) { |
15004 | 0 | if ((ret = DeriveMasterSecret(ssl)) != 0) |
15005 | 0 | return ret; |
15006 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
15007 | 0 | ForceZero(ssl->arrays->preMasterSecret, |
15008 | 0 | ssl->arrays->preMasterSz); |
15009 | | #ifdef WOLFSSL_EARLY_DATA |
15010 | | #ifdef WOLFSSL_QUIC |
15011 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData != no_early_data) { |
15012 | | /* QUIC never sends/receives EndOfEarlyData, but having |
15013 | | * early data means the last encryption keys had not been |
15014 | | * set yet. */ |
15015 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
15016 | | return ret; |
15017 | | } |
15018 | | #endif |
15019 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
15020 | | ENCRYPT_AND_DECRYPT_SIDE, |
15021 | | ssl->earlyData == no_early_data)) != 0) { |
15022 | | return ret; |
15023 | | } |
15024 | | if (ssl->earlyData != no_early_data) { |
15025 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, |
15026 | | 1)) != 0) { |
15027 | | return ret; |
15028 | | } |
15029 | | } |
15030 | | #else |
15031 | 0 | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
15032 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
15033 | 0 | return ret; |
15034 | 0 | } |
15035 | 0 | #endif |
15036 | | /* Setup keys for application data messages. */ |
15037 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
15038 | 0 | return ret; |
15039 | 0 | } |
15040 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
15041 | | if (type == certificate_request && |
15042 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
15043 | | #if defined(HAVE_WRITE_DUP) |
15044 | | /* Read side cannot write; delegate the cert response to the |
15045 | | * write side by saving auth state in the shared WriteDup. */ |
15046 | | if (ssl->dupSide == READ_DUP_SIDE) { |
15047 | | if (ssl->dupWrite == NULL) |
15048 | | return BAD_STATE_E; |
15049 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
15050 | | return BAD_MUTEX_E; |
15051 | | /* Copy the current transcript so the write side can |
15052 | | * compute the correct Finished MAC. */ |
15053 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, |
15054 | | &ssl->dupWrite->postHandshakeHashState); |
15055 | | if (ret == 0) { |
15056 | | /* Copy the cert request context. */ |
15057 | | CertReqCtx** tail = &ssl->certReqCtx; |
15058 | | while (*tail != NULL) |
15059 | | tail = &(*tail)->next; |
15060 | | *tail = ssl->dupWrite->postHandshakeCertReqCtx; |
15061 | | ssl->dupWrite->postHandshakeCertReqCtx = ssl->certReqCtx; |
15062 | | ssl->certReqCtx = NULL; |
15063 | | ssl->dupWrite->postHandshakeSendVerify = |
15064 | | ssl->options.sendVerify; |
15065 | | ssl->dupWrite->postHandshakeSigAlgo = |
15066 | | ssl->options.sigAlgo; |
15067 | | ssl->dupWrite->postHandshakeHashAlgo = |
15068 | | ssl->options.hashAlgo; |
15069 | | ssl->dupWrite->postHandshakeAuthPending = 1; |
15070 | | } |
15071 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
15072 | | /* Leave ssl->options unchanged: read side must not reset |
15073 | | * its states or call wolfSSL_connect_TLSv13. */ |
15074 | | } |
15075 | | else |
15076 | | #endif /* HAVE_WRITE_DUP */ |
15077 | | { |
15078 | | /* reset handshake states */ |
15079 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
15080 | | ssl->options.connectState = FIRST_REPLY_DONE; |
15081 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
15082 | | ssl->options.processReply = 0; /* doProcessInit */ |
15083 | | |
15084 | | /* |
15085 | | DTLSv1.3 note: We can't reset serverState to |
15086 | | SERVER_FINISHED_COMPLETE with the goal that this connect |
15087 | | blocks until the cert/cert_verify/finished flight gets ACKed |
15088 | | by the server. The problem is that we will invoke |
15089 | | ProcessReplyEx() in that case, but we came here from |
15090 | | ProcessReplyEx() and it is not re-entrant safe (the input |
15091 | | buffer would still have the certificate_request message). */ |
15092 | | |
15093 | | if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) { |
15094 | | ret = ssl->error; |
15095 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
15096 | | ret = POST_HAND_AUTH_ERROR; |
15097 | | } |
15098 | | } |
15099 | | } |
15100 | | #endif |
15101 | 0 | } |
15102 | 0 | #endif /* NO_WOLFSSL_CLIENT */ |
15103 | |
|
15104 | 0 | #ifndef NO_WOLFSSL_SERVER |
15105 | | #if defined(HAVE_SESSION_TICKET) |
15106 | | if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) { |
15107 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
15108 | | if (ret != 0) |
15109 | | return ret; |
15110 | | } |
15111 | | #endif |
15112 | 0 | #endif /* NO_WOLFSSL_SERVER */ |
15113 | 0 | } |
15114 | | |
15115 | | #ifdef WOLFSSL_DTLS13 |
15116 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) { |
15117 | | DtlsResetState(ssl); |
15118 | | if (DtlsIgnoreError(ret)) |
15119 | | ret = 0; |
15120 | | } |
15121 | | #endif |
15122 | | |
15123 | 0 | WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret); |
15124 | 0 | return ret; |
15125 | 0 | } |
15126 | | |
15127 | | |
15128 | | /* Handle a handshake message that has been received. |
15129 | | * |
15130 | | * ssl The SSL/TLS object. |
15131 | | * input The message buffer. |
15132 | | * inOutIdx On entry, the index into the buffer of the current message. |
15133 | | * On exit, the index into the buffer of the next message. |
15134 | | * totalSz Length of remaining data in the message buffer. |
15135 | | * returns 0 on success and otherwise failure. |
15136 | | */ |
15137 | | int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
15138 | | word32 totalSz) |
15139 | 25.2k | { |
15140 | 25.2k | int ret = 0; |
15141 | 25.2k | word32 inputLength; |
15142 | 25.2k | byte type; |
15143 | 25.2k | word32 size = 0; |
15144 | | |
15145 | 25.2k | WOLFSSL_ENTER("DoTls13HandShakeMsg"); |
15146 | | |
15147 | | /* totalSz is now curStartIdx + curSize (content-only, padSz already |
15148 | | * subtracted in ProcessReply). */ |
15149 | 25.2k | if (*inOutIdx > totalSz) |
15150 | 0 | return BUFFER_ERROR; |
15151 | 25.2k | inputLength = totalSz - *inOutIdx; |
15152 | | |
15153 | | /* If there is a pending fragmented handshake message, |
15154 | | * pending message size will be non-zero. */ |
15155 | 25.2k | if (ssl->pendingMsgSz == 0) { |
15156 | | |
15157 | 14.1k | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
15158 | 14.1k | totalSz) != 0) { |
15159 | 15 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
15160 | 15 | return PARSE_ERROR; |
15161 | 15 | } |
15162 | | |
15163 | 14.1k | ret = EarlySanityCheckMsgReceived(ssl, type, |
15164 | 14.1k | (inputLength > HANDSHAKE_HEADER_SZ) ? |
15165 | 13.6k | min(inputLength - HANDSHAKE_HEADER_SZ, size) : 0); |
15166 | 14.1k | if (ret != 0) { |
15167 | 153 | WOLFSSL_ERROR(ret); |
15168 | 153 | return ret; |
15169 | 153 | } |
15170 | | |
15171 | | /* Cap the maximum size of a handshake message to something reasonable. |
15172 | | * By default is the maximum size of a certificate message assuming |
15173 | | * nine 2048-bit RSA certificates in the chain. */ |
15174 | 13.9k | if (size > MAX_HANDSHAKE_SZ) { |
15175 | 113 | WOLFSSL_MSG("Handshake message too large"); |
15176 | 113 | WOLFSSL_ERROR_VERBOSE(HANDSHAKE_SIZE_ERROR); |
15177 | 113 | return HANDSHAKE_SIZE_ERROR; |
15178 | 113 | } |
15179 | | |
15180 | | /* size is the size of the certificate message payload */ |
15181 | 13.8k | if (inputLength - HANDSHAKE_HEADER_SZ < size) { |
15182 | | /* Commit pending state only after the allocation succeeds. */ |
15183 | 747 | ssl->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ, |
15184 | 747 | ssl->heap, DYNAMIC_TYPE_ARRAYS); |
15185 | 747 | if (ssl->pendingMsg == NULL) |
15186 | 20 | return MEMORY_E; |
15187 | 727 | ssl->pendingMsgType = type; |
15188 | 727 | ssl->pendingMsgSz = size + HANDSHAKE_HEADER_SZ; |
15189 | 727 | XMEMCPY(ssl->pendingMsg, |
15190 | 727 | input + *inOutIdx - HANDSHAKE_HEADER_SZ, |
15191 | 727 | inputLength); |
15192 | 727 | ssl->pendingMsgOffset = inputLength; |
15193 | 727 | *inOutIdx += inputLength - HANDSHAKE_HEADER_SZ; |
15194 | 727 | return 0; |
15195 | 747 | } |
15196 | | |
15197 | 13.1k | ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
15198 | 13.1k | totalSz); |
15199 | 13.1k | } |
15200 | 11.0k | else { |
15201 | 11.0k | if (inputLength + ssl->pendingMsgOffset > ssl->pendingMsgSz) { |
15202 | 51 | inputLength = ssl->pendingMsgSz - ssl->pendingMsgOffset; |
15203 | 51 | } |
15204 | | |
15205 | 11.0k | ret = EarlySanityCheckMsgReceived(ssl, ssl->pendingMsgType, |
15206 | 11.0k | inputLength); |
15207 | 11.0k | if (ret != 0) { |
15208 | 24 | WOLFSSL_ERROR(ret); |
15209 | 24 | return ret; |
15210 | 24 | } |
15211 | | |
15212 | 11.0k | XMEMCPY(ssl->pendingMsg + ssl->pendingMsgOffset, |
15213 | 11.0k | input + *inOutIdx, inputLength); |
15214 | 11.0k | ssl->pendingMsgOffset += inputLength; |
15215 | 11.0k | *inOutIdx += inputLength; |
15216 | | |
15217 | 11.0k | if (ssl->pendingMsgOffset == ssl->pendingMsgSz) |
15218 | 57 | { |
15219 | 57 | word32 idx = 0; |
15220 | 57 | ret = DoTls13HandShakeMsgType(ssl, |
15221 | 57 | ssl->pendingMsg + HANDSHAKE_HEADER_SZ, |
15222 | 57 | &idx, ssl->pendingMsgType, |
15223 | 57 | ssl->pendingMsgSz - HANDSHAKE_HEADER_SZ, |
15224 | 57 | ssl->pendingMsgSz); |
15225 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
15226 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
15227 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) { |
15228 | | /* setup to process fragment again */ |
15229 | | ssl->pendingMsgOffset -= inputLength; |
15230 | | *inOutIdx -= inputLength; |
15231 | | } |
15232 | | else |
15233 | | #endif |
15234 | 57 | { |
15235 | 57 | XFREE(ssl->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS); |
15236 | 57 | ssl->pendingMsg = NULL; |
15237 | 57 | ssl->pendingMsgSz = 0; |
15238 | 57 | } |
15239 | 57 | } |
15240 | 11.0k | } |
15241 | | |
15242 | 24.1k | WOLFSSL_LEAVE("DoTls13HandShakeMsg", ret); |
15243 | 24.1k | return ret; |
15244 | 25.2k | } |
15245 | | |
15246 | | #ifndef NO_WOLFSSL_CLIENT |
15247 | | |
15248 | | /* The client connecting to the server. |
15249 | | * The protocol version is expecting to be TLS v1.3. |
15250 | | * If the server downgrades, and older versions of the protocol are compiled |
15251 | | * in, the client will fallback to wolfSSL_connect(). |
15252 | | * Please see note at top of README if you get an error from connect. |
15253 | | * |
15254 | | * ssl The SSL/TLS object. |
15255 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
15256 | | * unrecoverable error occurs and 0 otherwise. |
15257 | | * For more error information use wolfSSL_get_error(). |
15258 | | */ |
15259 | | int wolfSSL_connect_TLSv13(WOLFSSL* ssl) |
15260 | 0 | { |
15261 | 0 | int advanceState; |
15262 | 0 | int ret = 0; |
15263 | |
|
15264 | 0 | WOLFSSL_ENTER("wolfSSL_connect_TLSv13"); |
15265 | |
|
15266 | 0 | #ifdef HAVE_ERRNO_H |
15267 | 0 | errno = 0; |
15268 | 0 | #endif |
15269 | |
|
15270 | 0 | if (ssl == NULL) |
15271 | 0 | return BAD_FUNC_ARG; |
15272 | | |
15273 | 0 | if (ssl->options.side != WOLFSSL_CLIENT_END) { |
15274 | 0 | ssl->error = SIDE_ERROR; |
15275 | 0 | WOLFSSL_ERROR(ssl->error); |
15276 | 0 | return WOLFSSL_FATAL_ERROR; |
15277 | 0 | } |
15278 | | |
15279 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
15280 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
15281 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
15282 | 0 | return ret; |
15283 | 0 | } |
15284 | | |
15285 | | #ifdef WOLFSSL_DTLS |
15286 | | if (ssl->version.major == DTLS_MAJOR) { |
15287 | | ssl->options.dtls = 1; |
15288 | | ssl->options.dtlsStateful = 1; |
15289 | | } |
15290 | | #endif |
15291 | | |
15292 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
15293 | | if ((ssl->ConnectFilter != NULL) && |
15294 | | (ssl->options.connectState == CONNECT_BEGIN)) |
15295 | | { |
15296 | | wolfSSL_netfilter_decision_t res; |
15297 | | if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) == |
15298 | | WOLFSSL_SUCCESS) && |
15299 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
15300 | | ssl->error = SOCKET_FILTERED_E; |
15301 | | WOLFSSL_ERROR(ssl->error); |
15302 | | return WOLFSSL_FATAL_ERROR; |
15303 | | } |
15304 | | } |
15305 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
15306 | | |
15307 | | /* fragOffset is non-zero when sending fragments. On the last |
15308 | | * fragment, fragOffset is zero again, and the state can be |
15309 | | * advanced. Also, only advance from states in which we send data */ |
15310 | 0 | advanceState = (ssl->options.connectState == CONNECT_BEGIN || |
15311 | 0 | ssl->options.connectState == HELLO_AGAIN || |
15312 | 0 | (ssl->options.connectState >= FIRST_REPLY_DONE && |
15313 | 0 | ssl->options.connectState <= FIRST_REPLY_FOURTH)); |
15314 | |
|
15315 | | #ifdef WOLFSSL_DTLS13 |
15316 | | if (ssl->options.dtls) |
15317 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
15318 | | && !ssl->dtls13SendingAckOrRtx; |
15319 | | #endif /* WOLFSSL_DTLS13 */ |
15320 | |
|
15321 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
15322 | | #ifdef WOLFSSL_ASYNC_CRYPT |
15323 | | /* do not send buffered or advance state if last error was an |
15324 | | async pending operation */ |
15325 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
15326 | | #endif |
15327 | 0 | ) { |
15328 | 0 | if ((ret = SendBuffered(ssl)) == 0) { |
15329 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
15330 | 0 | if (advanceState) { |
15331 | | #ifdef WOLFSSL_DTLS13 |
15332 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) && |
15333 | | ssl->options.connectState == FIRST_REPLY_FOURTH) { |
15334 | | /* WAIT_FINISHED_ACK is a state added afterwards, but it |
15335 | | can't follow FIRST_REPLY_FOURTH in the enum order. Indeed |
15336 | | the value of the enum ConnectState is stored in |
15337 | | serialized session. This would make importing serialized |
15338 | | session from other wolfSSL version incompatible */ |
15339 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
15340 | | } |
15341 | | else |
15342 | | #endif /* WOLFSSL_DTLS13 */ |
15343 | 0 | { |
15344 | 0 | ssl->options.connectState++; |
15345 | 0 | } |
15346 | 0 | WOLFSSL_MSG("connect state: " |
15347 | 0 | "Advanced from last buffered fragment send"); |
15348 | 0 | #ifdef WOLFSSL_ASYNC_IO |
15349 | 0 | FreeAsyncCtx(ssl, 0); |
15350 | 0 | #endif |
15351 | |
|
15352 | 0 | } |
15353 | 0 | } |
15354 | 0 | else { |
15355 | 0 | WOLFSSL_MSG("connect state: " |
15356 | 0 | "Not advanced, more fragments to send"); |
15357 | 0 | } |
15358 | | #ifdef WOLFSSL_DTLS13 |
15359 | | if (ssl->options.dtls) |
15360 | | ssl->dtls13SendingAckOrRtx = 0; |
15361 | | #endif /* WOLFSSL_DTLS13 */ |
15362 | |
|
15363 | 0 | } |
15364 | 0 | else { |
15365 | 0 | ssl->error = ret; |
15366 | 0 | WOLFSSL_ERROR(ssl->error); |
15367 | 0 | return WOLFSSL_FATAL_ERROR; |
15368 | 0 | } |
15369 | 0 | } |
15370 | | |
15371 | 0 | ret = RetrySendAlert(ssl); |
15372 | 0 | if (ret != 0) { |
15373 | 0 | ssl->error = ret; |
15374 | 0 | WOLFSSL_ERROR(ssl->error); |
15375 | 0 | return WOLFSSL_FATAL_ERROR; |
15376 | 0 | } |
15377 | | |
15378 | | #ifdef WOLFSSL_DTLS13 |
15379 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
15380 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
15381 | | WOLFSSL_ERROR(ssl->error); |
15382 | | return WOLFSSL_FATAL_ERROR; |
15383 | | } |
15384 | | |
15385 | | /* we sent all the fragments. Advance state. */ |
15386 | | ssl->options.connectState++; |
15387 | | } |
15388 | | #endif /* WOLFSSL_DTLS13 */ |
15389 | | |
15390 | 0 | switch (ssl->options.connectState) { |
15391 | | |
15392 | 0 | case CONNECT_BEGIN: |
15393 | | /* Always send client hello first. */ |
15394 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
15395 | 0 | WOLFSSL_ERROR(ssl->error); |
15396 | 0 | return WOLFSSL_FATAL_ERROR; |
15397 | 0 | } |
15398 | | |
15399 | 0 | ssl->options.connectState = CLIENT_HELLO_SENT; |
15400 | 0 | WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); |
15401 | 0 | FALL_THROUGH; |
15402 | |
|
15403 | 0 | case CLIENT_HELLO_SENT: |
15404 | | #ifdef WOLFSSL_EARLY_DATA |
15405 | | if (ssl->earlyData != no_early_data && |
15406 | | ssl->options.handShakeState != CLIENT_HELLO_COMPLETE) { |
15407 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
15408 | | if (!ssl->options.dtls && |
15409 | | ssl->options.tls13MiddleBoxCompat) { |
15410 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
15411 | | WOLFSSL_ERROR(ssl->error); |
15412 | | return WOLFSSL_FATAL_ERROR; |
15413 | | } |
15414 | | ssl->options.sentChangeCipher = 1; |
15415 | | } |
15416 | | #endif |
15417 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
15418 | | return WOLFSSL_SUCCESS; |
15419 | | } |
15420 | | #endif |
15421 | | /* Get the response/s from the server. */ |
15422 | 0 | while (ssl->options.serverState < |
15423 | 0 | SERVER_HELLOVERIFYREQUEST_COMPLETE) { |
15424 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
15425 | 0 | WOLFSSL_ERROR(ssl->error); |
15426 | 0 | return WOLFSSL_FATAL_ERROR; |
15427 | 0 | } |
15428 | |
|
15429 | | #ifdef WOLFSSL_DTLS13 |
15430 | | if (ssl->options.dtls) { |
15431 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15432 | | WOLFSSL_ERROR(ssl->error); |
15433 | | return WOLFSSL_FATAL_ERROR; |
15434 | | } |
15435 | | } |
15436 | | #endif /* WOLFSSL_DTLS13 */ |
15437 | 0 | } |
15438 | | |
15439 | 0 | if (!ssl->options.tls1_3) { |
15440 | 0 | #ifndef WOLFSSL_NO_TLS12 |
15441 | 0 | if (ssl->options.downgrade) |
15442 | 0 | return wolfSSL_connect(ssl); |
15443 | 0 | #endif |
15444 | 0 | WOLFSSL_MSG("Client using higher version, fatal error"); |
15445 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
15446 | 0 | return VERSION_ERROR; |
15447 | 0 | } |
15448 | | |
15449 | 0 | ssl->options.connectState = HELLO_AGAIN; |
15450 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN"); |
15451 | 0 | FALL_THROUGH; |
15452 | |
|
15453 | 0 | case HELLO_AGAIN: |
15454 | |
|
15455 | 0 | if (ssl->options.serverState == |
15456 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
15457 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
15458 | | if (!ssl->options.dtls && !ssl->options.sentChangeCipher |
15459 | | && ssl->options.tls13MiddleBoxCompat) { |
15460 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
15461 | | WOLFSSL_ERROR(ssl->error); |
15462 | | return WOLFSSL_FATAL_ERROR; |
15463 | | } |
15464 | | ssl->options.sentChangeCipher = 1; |
15465 | | } |
15466 | | #endif |
15467 | | /* Try again with different security parameters. */ |
15468 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
15469 | 0 | WOLFSSL_ERROR(ssl->error); |
15470 | 0 | return WOLFSSL_FATAL_ERROR; |
15471 | 0 | } |
15472 | 0 | } |
15473 | | |
15474 | 0 | ssl->options.connectState = HELLO_AGAIN_REPLY; |
15475 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY"); |
15476 | 0 | FALL_THROUGH; |
15477 | |
|
15478 | 0 | case HELLO_AGAIN_REPLY: |
15479 | | /* Get the response/s from the server. */ |
15480 | 0 | while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
15481 | | #ifdef WOLFSSL_DTLS13 |
15482 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
15483 | | #ifndef WOLFSSL_NO_TLS12 |
15484 | | if (ssl->options.downgrade) |
15485 | | return wolfSSL_connect(ssl); |
15486 | | #endif |
15487 | | } |
15488 | | #endif /* WOLFSSL_DTLS13 */ |
15489 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
15490 | 0 | WOLFSSL_ERROR(ssl->error); |
15491 | 0 | return WOLFSSL_FATAL_ERROR; |
15492 | 0 | } |
15493 | |
|
15494 | | #ifdef WOLFSSL_DTLS13 |
15495 | | if (ssl->options.dtls) { |
15496 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15497 | | WOLFSSL_ERROR(ssl->error); |
15498 | | return WOLFSSL_FATAL_ERROR; |
15499 | | } |
15500 | | } |
15501 | | #endif /* WOLFSSL_DTLS13 */ |
15502 | 0 | } |
15503 | | |
15504 | 0 | ssl->options.connectState = FIRST_REPLY_DONE; |
15505 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_DONE"); |
15506 | 0 | FALL_THROUGH; |
15507 | |
|
15508 | 0 | case FIRST_REPLY_DONE: |
15509 | 0 | if (ssl->options.certOnly) |
15510 | 0 | return WOLFSSL_SUCCESS; |
15511 | | #ifdef WOLFSSL_EARLY_DATA |
15512 | | if (!ssl->options.dtls && ssl->earlyData != no_early_data |
15513 | | && !WOLFSSL_IS_QUIC(ssl)) { |
15514 | | if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) { |
15515 | | WOLFSSL_ERROR(ssl->error); |
15516 | | return WOLFSSL_FATAL_ERROR; |
15517 | | } |
15518 | | WOLFSSL_MSG("sent: end_of_early_data"); |
15519 | | } |
15520 | | #endif |
15521 | | |
15522 | 0 | ssl->options.connectState = FIRST_REPLY_FIRST; |
15523 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST"); |
15524 | 0 | FALL_THROUGH; |
15525 | |
|
15526 | 0 | case FIRST_REPLY_FIRST: |
15527 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
15528 | | if (!ssl->options.sentChangeCipher && !ssl->options.dtls |
15529 | | && ssl->options.tls13MiddleBoxCompat) { |
15530 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
15531 | | WOLFSSL_ERROR(ssl->error); |
15532 | | return WOLFSSL_FATAL_ERROR; |
15533 | | } |
15534 | | ssl->options.sentChangeCipher = 1; |
15535 | | } |
15536 | | #endif |
15537 | |
|
15538 | 0 | ssl->options.connectState = FIRST_REPLY_SECOND; |
15539 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND"); |
15540 | 0 | FALL_THROUGH; |
15541 | |
|
15542 | 0 | case FIRST_REPLY_SECOND: |
15543 | | /* CLIENT: check peer authentication. */ |
15544 | 0 | if (!ssl->options.peerAuthGood) { |
15545 | 0 | WOLFSSL_MSG("Server authentication did not happen"); |
15546 | 0 | WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR); |
15547 | 0 | return WOLFSSL_FATAL_ERROR; |
15548 | 0 | } |
15549 | 0 | #ifndef NO_CERTS |
15550 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15551 | 0 | ssl->error = SendTls13Certificate(ssl); |
15552 | 0 | if (ssl->error != 0) { |
15553 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
15554 | 0 | WOLFSSL_ERROR(ssl->error); |
15555 | 0 | return WOLFSSL_FATAL_ERROR; |
15556 | 0 | } |
15557 | 0 | WOLFSSL_MSG("sent: certificate"); |
15558 | 0 | } |
15559 | 0 | #endif |
15560 | | |
15561 | 0 | ssl->options.connectState = FIRST_REPLY_THIRD; |
15562 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD"); |
15563 | 0 | FALL_THROUGH; |
15564 | |
|
15565 | 0 | case FIRST_REPLY_THIRD: |
15566 | 0 | #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
15567 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
15568 | 0 | defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
15569 | 0 | defined(WOLFSSL_HAVE_SLHDSA))) && \ |
15570 | 0 | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
15571 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15572 | 0 | ssl->error = SendTls13CertificateVerify(ssl); |
15573 | 0 | if (ssl->error != 0) { |
15574 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
15575 | 0 | WOLFSSL_ERROR(ssl->error); |
15576 | 0 | return WOLFSSL_FATAL_ERROR; |
15577 | 0 | } |
15578 | 0 | WOLFSSL_MSG("sent: certificate verify"); |
15579 | 0 | } |
15580 | 0 | #endif |
15581 | | |
15582 | 0 | ssl->options.connectState = FIRST_REPLY_FOURTH; |
15583 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH"); |
15584 | 0 | FALL_THROUGH; |
15585 | |
|
15586 | 0 | case FIRST_REPLY_FOURTH: |
15587 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
15588 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
15589 | 0 | WOLFSSL_ERROR(ssl->error); |
15590 | 0 | return WOLFSSL_FATAL_ERROR; |
15591 | 0 | } |
15592 | 0 | WOLFSSL_MSG("sent: finished"); |
15593 | |
|
15594 | | #ifdef WOLFSSL_DTLS13 |
15595 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
15596 | | WOLFSSL_MSG("connect state: WAIT_FINISHED_ACK"); |
15597 | | FALL_THROUGH; |
15598 | | |
15599 | | case WAIT_FINISHED_ACK: |
15600 | | if (ssl->options.dtls) { |
15601 | | while (ssl->options.serverState != SERVER_FINISHED_ACKED) { |
15602 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
15603 | | WOLFSSL_ERROR(ssl->error); |
15604 | | return WOLFSSL_FATAL_ERROR; |
15605 | | } |
15606 | | |
15607 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15608 | | WOLFSSL_ERROR(ssl->error); |
15609 | | return WOLFSSL_FATAL_ERROR; |
15610 | | } |
15611 | | } |
15612 | | } |
15613 | | #endif /* WOLFSSL_DTLS13 */ |
15614 | 0 | ssl->options.connectState = FINISHED_DONE; |
15615 | 0 | WOLFSSL_MSG("connect state: FINISHED_DONE"); |
15616 | 0 | FALL_THROUGH; |
15617 | |
|
15618 | 0 | case FINISHED_DONE: |
15619 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
15620 | 0 | if (ssl->hsDoneCb != NULL) { |
15621 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
15622 | 0 | if (cbret < 0) { |
15623 | 0 | ssl->error = cbret; |
15624 | 0 | WOLFSSL_ERROR_VERBOSE(ssl->error); |
15625 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
15626 | 0 | return WOLFSSL_FATAL_ERROR; |
15627 | 0 | } |
15628 | 0 | } |
15629 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
15630 | | |
15631 | | #if defined(HAVE_ECH) |
15632 | | /* RFC 9849 s6.1.6: if we offered ECH but the server rejected it, |
15633 | | * send ech_required alert and abort before returning to the app */ |
15634 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
15635 | | !ssl->options.echAccepted) { |
15636 | | if (ssl->echRetryConfigs != NULL) { |
15637 | | ssl->options.echRetryConfigsAccepted = 1; |
15638 | | } |
15639 | | SendAlert(ssl, alert_fatal, ech_required); |
15640 | | ssl->error = ECH_REQUIRED_E; |
15641 | | WOLFSSL_ERROR_VERBOSE(ECH_REQUIRED_E); |
15642 | | return WOLFSSL_FATAL_ERROR; |
15643 | | } |
15644 | | #endif /* HAVE_ECH */ |
15645 | | |
15646 | 0 | if (!ssl->options.keepResources) { |
15647 | 0 | FreeHandshakeResources(ssl); |
15648 | 0 | } |
15649 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
15650 | | /* Free the remaining async context if not using it for crypto */ |
15651 | 0 | FreeAsyncCtx(ssl, 1); |
15652 | 0 | #endif |
15653 | |
|
15654 | 0 | ssl->error = 0; /* clear the error */ |
15655 | |
|
15656 | 0 | WOLFSSL_LEAVE("wolfSSL_connect_TLSv13", WOLFSSL_SUCCESS); |
15657 | 0 | return WOLFSSL_SUCCESS; |
15658 | | |
15659 | 0 | default: |
15660 | 0 | WOLFSSL_MSG("Unknown connect state ERROR"); |
15661 | 0 | return WOLFSSL_FATAL_ERROR; /* unknown connect state */ |
15662 | 0 | } |
15663 | 0 | } |
15664 | | #endif |
15665 | | |
15666 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
15667 | | /* Send a cookie with the HelloRetryRequest to avoid storing state. |
15668 | | * |
15669 | | * ssl SSL/TLS object. |
15670 | | * secret Secret to use when generating integrity check for cookie. |
15671 | | * A value of NULL indicates to generate a new random secret. |
15672 | | * secretSz Size of secret data in bytes. |
15673 | | * Use a value of 0 to indicate use of default size. |
15674 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when |
15675 | | * called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
15676 | | */ |
15677 | | int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret, |
15678 | | unsigned int secretSz) |
15679 | | { |
15680 | | int ret; |
15681 | | |
15682 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15683 | | return BAD_FUNC_ARG; |
15684 | | #ifndef NO_WOLFSSL_SERVER |
15685 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15686 | | return SIDE_ERROR; |
15687 | | |
15688 | | if (secretSz == 0) { |
15689 | | #ifndef NO_SHA256 |
15690 | | secretSz = WC_SHA256_DIGEST_SIZE; |
15691 | | #elif defined(WOLFSSL_SHA384) |
15692 | | secretSz = WC_SHA384_DIGEST_SIZE; |
15693 | | #elif defined(WOLFSSL_TLS13_SHA512) |
15694 | | secretSz = WC_SHA512_DIGEST_SIZE; |
15695 | | #elif defined(WOLFSSL_SM3) |
15696 | | secretSz = WC_SM3_DIGEST_SIZE; |
15697 | | #else |
15698 | | #error "No digest to available to use with HMAC for cookies." |
15699 | | #endif /* NO_SHA */ |
15700 | | } |
15701 | | |
15702 | | if (secretSz != ssl->buffers.tls13CookieSecret.length) { |
15703 | | byte* newSecret; |
15704 | | |
15705 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
15706 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
15707 | | ssl->buffers.tls13CookieSecret.length); |
15708 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, |
15709 | | ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
15710 | | } |
15711 | | |
15712 | | newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
15713 | | DYNAMIC_TYPE_COOKIE_PWD); |
15714 | | if (newSecret == NULL) { |
15715 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
15716 | | ssl->buffers.tls13CookieSecret.length = 0; |
15717 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
15718 | | return MEMORY_ERROR; |
15719 | | } |
15720 | | ssl->buffers.tls13CookieSecret.buffer = newSecret; |
15721 | | ssl->buffers.tls13CookieSecret.length = secretSz; |
15722 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
15723 | | wc_MemZero_Add("wolfSSL_send_hrr_cookie secret", |
15724 | | ssl->buffers.tls13CookieSecret.buffer, |
15725 | | ssl->buffers.tls13CookieSecret.length); |
15726 | | #endif |
15727 | | } |
15728 | | |
15729 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
15730 | | if (secret == NULL) { |
15731 | | ret = wc_RNG_GenerateBlock(ssl->rng, |
15732 | | ssl->buffers.tls13CookieSecret.buffer, secretSz); |
15733 | | if (ret < 0) |
15734 | | return ret; |
15735 | | } |
15736 | | else |
15737 | | XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz); |
15738 | | |
15739 | | ssl->options.sendCookie = 1; |
15740 | | |
15741 | | ret = WOLFSSL_SUCCESS; |
15742 | | #else |
15743 | | (void)secret; |
15744 | | (void)secretSz; |
15745 | | |
15746 | | ret = SIDE_ERROR; |
15747 | | #endif |
15748 | | |
15749 | | return ret; |
15750 | | } |
15751 | | |
15752 | | int wolfSSL_disable_hrr_cookie(WOLFSSL* ssl) |
15753 | | { |
15754 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15755 | | return BAD_FUNC_ARG; |
15756 | | |
15757 | | #ifdef NO_WOLFSSL_SERVER |
15758 | | return SIDE_ERROR; |
15759 | | #else |
15760 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15761 | | return SIDE_ERROR; |
15762 | | |
15763 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
15764 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
15765 | | ssl->buffers.tls13CookieSecret.length); |
15766 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap, |
15767 | | DYNAMIC_TYPE_COOKIE_PWD); |
15768 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
15769 | | ssl->buffers.tls13CookieSecret.length = 0; |
15770 | | } |
15771 | | |
15772 | | if (ssl->buffers.tls13CookieSecretSecondary.buffer != NULL) { |
15773 | | ForceZero(ssl->buffers.tls13CookieSecretSecondary.buffer, |
15774 | | ssl->buffers.tls13CookieSecretSecondary.length); |
15775 | | XFREE(ssl->buffers.tls13CookieSecretSecondary.buffer, ssl->heap, |
15776 | | DYNAMIC_TYPE_COOKIE_PWD); |
15777 | | ssl->buffers.tls13CookieSecretSecondary.buffer = NULL; |
15778 | | ssl->buffers.tls13CookieSecretSecondary.length = 0; |
15779 | | } |
15780 | | |
15781 | | ssl->options.sendCookie = 0; |
15782 | | return WOLFSSL_SUCCESS; |
15783 | | #endif /* NO_WOLFSSL_SERVER */ |
15784 | | } |
15785 | | |
15786 | | /* Set a secondary HelloRetryRequest cookie secret used only when verifying a |
15787 | | * received cookie, and only if the primary secret (set by |
15788 | | * wolfSSL_send_hrr_cookie()) fails to verify it. |
15789 | | * |
15790 | | * This supports an application-driven cookie-secret rotation on a stateless |
15791 | | * DTLS 1.3 server: after rotating the primary secret, install the previous |
15792 | | * secret here so that cookies already issued under it are still accepted for |
15793 | | * an overlap window. It is never used to issue cookies. |
15794 | | * |
15795 | | * This API is DTLS only - TLS 1.3 over a reliable transport does not operate |
15796 | | * statelessly across the HelloRetryRequest exchange, so a secondary cookie |
15797 | | * secret has no use there. |
15798 | | * |
15799 | | * ssl SSL/TLS object. |
15800 | | * secret Secondary secret to verify cookies against. A value of NULL (or a |
15801 | | * secretSz of 0) clears any previously set secondary secret. |
15802 | | * secretSz Size of secret data in bytes. |
15803 | | * returns BAD_FUNC_ARG when ssl is NULL, not TLS v1.3 or not DTLS; SIDE_ERROR |
15804 | | * when called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
15805 | | */ |
15806 | | int wolfSSL_set_hrr_cookie_secret_secondary(WOLFSSL* ssl, |
15807 | | const unsigned char* secret, unsigned int secretSz) |
15808 | | { |
15809 | | int ret; |
15810 | | |
15811 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15812 | | return BAD_FUNC_ARG; |
15813 | | #ifndef NO_WOLFSSL_SERVER |
15814 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15815 | | return SIDE_ERROR; |
15816 | | /* DTLS only - TLS 1.3 does not verify cookies statelessly. */ |
15817 | | if (!ssl->options.dtls) { |
15818 | | WOLFSSL_MSG("Secondary HRR cookie secret is DTLS only"); |
15819 | | return BAD_FUNC_ARG; |
15820 | | } |
15821 | | |
15822 | | /* Clear any existing secondary secret. */ |
15823 | | if (ssl->buffers.tls13CookieSecretSecondary.buffer != NULL) { |
15824 | | ForceZero(ssl->buffers.tls13CookieSecretSecondary.buffer, |
15825 | | ssl->buffers.tls13CookieSecretSecondary.length); |
15826 | | XFREE(ssl->buffers.tls13CookieSecretSecondary.buffer, ssl->heap, |
15827 | | DYNAMIC_TYPE_COOKIE_PWD); |
15828 | | ssl->buffers.tls13CookieSecretSecondary.buffer = NULL; |
15829 | | ssl->buffers.tls13CookieSecretSecondary.length = 0; |
15830 | | } |
15831 | | |
15832 | | /* A NULL/empty secret just clears the secondary secret. */ |
15833 | | if (secret == NULL || secretSz == 0) { |
15834 | | ret = WOLFSSL_SUCCESS; |
15835 | | } |
15836 | | else { |
15837 | | byte* newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
15838 | | DYNAMIC_TYPE_COOKIE_PWD); |
15839 | | if (newSecret == NULL) { |
15840 | | WOLFSSL_MSG("couldn't allocate secondary cookie secret"); |
15841 | | ret = MEMORY_ERROR; |
15842 | | } |
15843 | | else { |
15844 | | XMEMCPY(newSecret, secret, secretSz); |
15845 | | ssl->buffers.tls13CookieSecretSecondary.buffer = newSecret; |
15846 | | ssl->buffers.tls13CookieSecretSecondary.length = secretSz; |
15847 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
15848 | | wc_MemZero_Add("wolfSSL_set_hrr_cookie_secret_secondary secret", |
15849 | | ssl->buffers.tls13CookieSecretSecondary.buffer, |
15850 | | ssl->buffers.tls13CookieSecretSecondary.length); |
15851 | | #endif |
15852 | | ret = WOLFSSL_SUCCESS; |
15853 | | } |
15854 | | } |
15855 | | #else |
15856 | | (void)secret; |
15857 | | (void)secretSz; |
15858 | | |
15859 | | ret = SIDE_ERROR; |
15860 | | #endif |
15861 | | |
15862 | | return ret; |
15863 | | } |
15864 | | |
15865 | | #endif /* defined(WOLFSSL_SEND_HRR_COOKIE) */ |
15866 | | |
15867 | | #ifdef HAVE_SUPPORTED_CURVES |
15868 | | /* Create a key share entry from group. |
15869 | | * Generates a key pair. |
15870 | | * |
15871 | | * ssl The SSL/TLS object. |
15872 | | * group The named group. |
15873 | | * returns 0 on success, otherwise failure. |
15874 | | * for async can return WC_PENDING_E and should be called again |
15875 | | */ |
15876 | | int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group) |
15877 | 0 | { |
15878 | 0 | int ret; |
15879 | |
|
15880 | 0 | if (ssl == NULL) |
15881 | 0 | return BAD_FUNC_ARG; |
15882 | | |
15883 | | #ifdef WOLFSSL_ASYNC_CRYPT |
15884 | | ret = wolfSSL_AsyncPop(ssl, NULL); |
15885 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
15886 | | /* Check for error */ |
15887 | | if (ret < 0) |
15888 | | return ret; |
15889 | | } |
15890 | | #endif |
15891 | | |
15892 | 0 | #if defined(WOLFSSL_HAVE_MLKEM) |
15893 | 0 | if (WOLFSSL_NAMED_GROUP_IS_PQC(group) || |
15894 | 0 | WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group)) { |
15895 | |
|
15896 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) { |
15897 | 0 | return BAD_FUNC_ARG; |
15898 | 0 | } |
15899 | | |
15900 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
15901 | | /* If I am the server of a KEM connection, do not do keygen because |
15902 | | * I'm going to encapsulate with the client's public key. Note that |
15903 | | * I might be the client and ssl->option.side has not been properly |
15904 | | * set yet. In that case the KeyGen operation will be deferred to |
15905 | | * connection time. */ |
15906 | 0 | return WOLFSSL_SUCCESS; |
15907 | 0 | } |
15908 | 0 | } |
15909 | 0 | #endif |
15910 | | #if defined(NO_TLS) |
15911 | | (void)ret; |
15912 | | (void)group; |
15913 | | #else |
15914 | | /* Check if the group is supported. */ |
15915 | 0 | if (!TLSX_IsGroupSupported(group, ssl->options.side)) { |
15916 | 0 | WOLFSSL_MSG("Group not supported."); |
15917 | 0 | return BAD_FUNC_ARG; |
15918 | 0 | } |
15919 | | |
15920 | 0 | ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions); |
15921 | 0 | if (ret != 0) |
15922 | 0 | return ret; |
15923 | 0 | #endif /* NO_TLS */ |
15924 | 0 | return WOLFSSL_SUCCESS; |
15925 | 0 | } |
15926 | | |
15927 | | /* Send no key share entries - use HelloRetryRequest to negotiate shared group. |
15928 | | * |
15929 | | * ssl The SSL/TLS object. |
15930 | | * returns 0 on success, otherwise failure. |
15931 | | */ |
15932 | | int wolfSSL_NoKeyShares(WOLFSSL* ssl) |
15933 | 0 | { |
15934 | 0 | int ret; |
15935 | |
|
15936 | 0 | if (ssl == NULL) |
15937 | 0 | return BAD_FUNC_ARG; |
15938 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
15939 | 0 | return SIDE_ERROR; |
15940 | | #if defined(NO_TLS) |
15941 | | (void)ret; |
15942 | | #else |
15943 | 0 | ret = TLSX_KeyShare_Empty(ssl); |
15944 | 0 | if (ret != 0) |
15945 | 0 | return ret; |
15946 | 0 | #endif /* NO_TLS */ |
15947 | 0 | return WOLFSSL_SUCCESS; |
15948 | 0 | } |
15949 | | #endif |
15950 | | |
15951 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
15952 | | int wolfSSL_UseCKS(WOLFSSL* ssl, byte *sigSpec, word16 sigSpecSz) |
15953 | | { |
15954 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->ctx->method->version) || |
15955 | | sigSpec == NULL || sigSpecSz == 0) |
15956 | | return BAD_FUNC_ARG; |
15957 | | |
15958 | | ssl->sigSpec = sigSpec; |
15959 | | ssl->sigSpecSz = sigSpecSz; |
15960 | | return WOLFSSL_SUCCESS; |
15961 | | } |
15962 | | |
15963 | | int wolfSSL_CTX_UseCKS(WOLFSSL_CTX* ctx, byte *sigSpec, word16 sigSpecSz) |
15964 | | { |
15965 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version) || |
15966 | | sigSpec == NULL || sigSpecSz == 0) |
15967 | | return BAD_FUNC_ARG; |
15968 | | |
15969 | | ctx->sigSpec = sigSpec; |
15970 | | ctx->sigSpecSz = sigSpecSz; |
15971 | | return WOLFSSL_SUCCESS; |
15972 | | } |
15973 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
15974 | | |
15975 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
15976 | | * |
15977 | | * ctx The SSL/TLS CTX object. |
15978 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
15979 | | */ |
15980 | | int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx) |
15981 | 0 | { |
15982 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
15983 | 0 | return BAD_FUNC_ARG; |
15984 | 0 | if (ctx->method->side == WOLFSSL_CLIENT_END) |
15985 | 0 | return SIDE_ERROR; |
15986 | | |
15987 | | #ifdef HAVE_SESSION_TICKET |
15988 | | ctx->noTicketTls13 = 1; |
15989 | | #endif |
15990 | | |
15991 | 0 | return 0; |
15992 | 0 | } |
15993 | | |
15994 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
15995 | | * |
15996 | | * ssl The SSL/TLS object. |
15997 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on |
15998 | | * a client and 0 on success. |
15999 | | */ |
16000 | | int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl) |
16001 | 0 | { |
16002 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16003 | 0 | return BAD_FUNC_ARG; |
16004 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
16005 | 0 | return SIDE_ERROR; |
16006 | | |
16007 | | #ifdef HAVE_SESSION_TICKET |
16008 | | ssl->options.noTicketTls13 = 1; |
16009 | | #endif |
16010 | | |
16011 | 0 | return 0; |
16012 | 0 | } |
16013 | | |
16014 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
16015 | | * |
16016 | | * ctx The SSL/TLS CTX object. |
16017 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
16018 | | */ |
16019 | | int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx) |
16020 | 0 | { |
16021 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
16022 | 0 | return BAD_FUNC_ARG; |
16023 | | |
16024 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16025 | | ctx->noPskDheKe = 1; |
16026 | | #endif |
16027 | | |
16028 | 0 | return 0; |
16029 | 0 | } |
16030 | | |
16031 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
16032 | | * |
16033 | | * ssl The SSL/TLS object. |
16034 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
16035 | | * success. |
16036 | | */ |
16037 | | int wolfSSL_no_dhe_psk(WOLFSSL* ssl) |
16038 | 0 | { |
16039 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16040 | 0 | return BAD_FUNC_ARG; |
16041 | | |
16042 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16043 | | ssl->options.noPskDheKe = 1; |
16044 | | #endif |
16045 | | |
16046 | 0 | return 0; |
16047 | 0 | } |
16048 | | |
16049 | | #ifdef HAVE_SUPPORTED_CURVES |
16050 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
16051 | | * |
16052 | | * ctx The SSL/TLS CTX object. |
16053 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
16054 | | */ |
16055 | | int wolfSSL_CTX_only_dhe_psk(WOLFSSL_CTX* ctx) |
16056 | 0 | { |
16057 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
16058 | 0 | return BAD_FUNC_ARG; |
16059 | | |
16060 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16061 | | ctx->onlyPskDheKe = 1; |
16062 | | #endif |
16063 | | |
16064 | 0 | return 0; |
16065 | 0 | } |
16066 | | |
16067 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
16068 | | * |
16069 | | * ssl The SSL/TLS object. |
16070 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
16071 | | * success. |
16072 | | */ |
16073 | | int wolfSSL_only_dhe_psk(WOLFSSL* ssl) |
16074 | 0 | { |
16075 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16076 | 0 | return BAD_FUNC_ARG; |
16077 | | |
16078 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16079 | | ssl->options.onlyPskDheKe = 1; |
16080 | | #endif |
16081 | | |
16082 | 0 | return 0; |
16083 | 0 | } |
16084 | | #endif /* HAVE_SUPPORTED_CURVES */ |
16085 | | |
16086 | | /* Require that an external Pre-Shared Key is negotiated for the handshake to |
16087 | | * succeed. TLS 1.3 / DTLS 1.3 only - in (D)TLS 1.2 the use of a PSK is |
16088 | | * determined by the negotiated cipher suite, so a mandatory PSK is configured |
16089 | | * there by restricting the cipher suite list to PSK suites. |
16090 | | * |
16091 | | * ctx The SSL/TLS CTX object. |
16092 | | * returns BAD_FUNC_ARG when ctx is NULL or not at least TLS v1.3, 0 on success. |
16093 | | */ |
16094 | | int wolfSSL_CTX_require_psk(WOLFSSL_CTX* ctx) |
16095 | 0 | { |
16096 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
16097 | 0 | return BAD_FUNC_ARG; |
16098 | | |
16099 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16100 | | ctx->failNoPSK = 1; |
16101 | | /* The requirement can only be enforced for (D)TLS 1.3, so keep it |
16102 | | * fail-closed by disabling a version downgrade. Otherwise a |
16103 | | * downgrade-capable context (e.g. from a v23 method) could silently fall |
16104 | | * back to (D)TLS 1.2 and complete without any PSK. */ |
16105 | | ctx->method->downgrade = 0; |
16106 | | #endif |
16107 | | |
16108 | 0 | return 0; |
16109 | 0 | } |
16110 | | |
16111 | | /* Require that an external Pre-Shared Key is negotiated for the handshake to |
16112 | | * succeed. See wolfSSL_CTX_require_psk(). |
16113 | | * |
16114 | | * ssl The SSL/TLS object. |
16115 | | * returns BAD_FUNC_ARG when ssl is NULL or not at least TLS v1.3, 0 on success. |
16116 | | */ |
16117 | | int wolfSSL_require_psk(WOLFSSL* ssl) |
16118 | 0 | { |
16119 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16120 | 0 | return BAD_FUNC_ARG; |
16121 | | |
16122 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16123 | | ssl->options.failNoPSK = 1; |
16124 | | /* See wolfSSL_CTX_require_psk() - keep the requirement fail-closed by |
16125 | | * disabling a version downgrade to (D)TLS 1.2. */ |
16126 | | ssl->options.downgrade = 0; |
16127 | | #endif |
16128 | | |
16129 | 0 | return 0; |
16130 | 0 | } |
16131 | | |
16132 | | int Tls13UpdateKeys(WOLFSSL* ssl) |
16133 | 0 | { |
16134 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16135 | 0 | return BAD_FUNC_ARG; |
16136 | | |
16137 | | #ifdef WOLFSSL_QUIC |
16138 | | /* RFC 9001 Section 6: a QUIC connection must not send a TLS KeyUpdate; |
16139 | | * key updates are handled at the QUIC packet-protection layer. */ |
16140 | | if (WOLFSSL_IS_QUIC(ssl)) |
16141 | | return BAD_FUNC_ARG; |
16142 | | #endif |
16143 | | |
16144 | | #ifdef WOLFSSL_DTLS13 |
16145 | | /* we are already waiting for the ack of a sent key update message. We can't |
16146 | | send another one before receiving its ack. Either wolfSSL_update_keys() |
16147 | | was invoked multiple times over a short period of time or we replied to a |
16148 | | KeyUpdate with update request. We'll just ignore sending this |
16149 | | KeyUpdate. */ |
16150 | | /* TODO: add WOLFSSL_ERROR_ALREADY_IN_PROGRESS type of error here */ |
16151 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) |
16152 | | return 0; |
16153 | | #endif /* WOLFSSL_DTLS13 */ |
16154 | | |
16155 | 0 | return SendTls13KeyUpdate(ssl); |
16156 | 0 | } |
16157 | | |
16158 | | /* Update the keys for encryption and decryption. |
16159 | | * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then |
16160 | | * calling wolfSSL_write() will have the message sent when ready. |
16161 | | * |
16162 | | * ssl The SSL/TLS object. |
16163 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or running over |
16164 | | * QUIC (RFC 9001 handles key updates at the QUIC packet-protection layer), |
16165 | | * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write, |
16166 | | * WOLFSSL_SUCCESS on success and otherwise failure. |
16167 | | */ |
16168 | | int wolfSSL_update_keys(WOLFSSL* ssl) |
16169 | 0 | { |
16170 | 0 | int ret; |
16171 | 0 | ret = Tls13UpdateKeys(ssl); |
16172 | 0 | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
16173 | 0 | ret = WOLFSSL_ERROR_WANT_WRITE; |
16174 | 0 | else if (ret == 0) |
16175 | 0 | ret = WOLFSSL_SUCCESS; |
16176 | 0 | return ret; |
16177 | 0 | } |
16178 | | |
16179 | | /* Whether a response is waiting for key update request. |
16180 | | * |
16181 | | * ssl The SSL/TLS object. |
16182 | | * required 0 when no key update response required. |
16183 | | * 1 when no key update response required. |
16184 | | * return 0 on success. |
16185 | | * return BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3 |
16186 | | */ |
16187 | | int wolfSSL_key_update_response(WOLFSSL* ssl, int* required) |
16188 | 0 | { |
16189 | 0 | if (required == NULL || ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16190 | 0 | return BAD_FUNC_ARG; |
16191 | | |
16192 | 0 | *required = ssl->keys.updateResponseReq; |
16193 | |
|
16194 | 0 | return 0; |
16195 | 0 | } |
16196 | | |
16197 | | #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
16198 | | /* Allow post-handshake authentication in TLS v1.3 connections. |
16199 | | * |
16200 | | * ctx The SSL/TLS CTX object. |
16201 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and |
16202 | | * 0 on success. |
16203 | | */ |
16204 | | int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx) |
16205 | | { |
16206 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
16207 | | return BAD_FUNC_ARG; |
16208 | | if (ctx->method->side == WOLFSSL_SERVER_END) |
16209 | | return SIDE_ERROR; |
16210 | | |
16211 | | ctx->postHandshakeAuth = 1; |
16212 | | |
16213 | | return 0; |
16214 | | } |
16215 | | |
16216 | | /* Allow post-handshake authentication in TLS v1.3 connection. |
16217 | | * |
16218 | | * ssl The SSL/TLS object. |
16219 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
16220 | | * SIDE_ERROR when not a client, BAD_STATE_E when called after the handshake |
16221 | | * has started, and 0 on success. |
16222 | | * |
16223 | | * Must be called before wolfSSL_connect() so the post_handshake_auth |
16224 | | * extension can be included in the ClientHello. |
16225 | | */ |
16226 | | int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl) |
16227 | | { |
16228 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16229 | | return BAD_FUNC_ARG; |
16230 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
16231 | | return SIDE_ERROR; |
16232 | | if (ssl->options.handShakeState != NULL_STATE) |
16233 | | return BAD_STATE_E; |
16234 | | |
16235 | | ssl->options.postHandshakeAuth = 1; |
16236 | | |
16237 | | return 0; |
16238 | | } |
16239 | | |
16240 | | /* Request a certificate of the client. |
16241 | | * Can be called any time after handshake completion. |
16242 | | * A maximum of 256 requests can be sent on a connection. |
16243 | | * |
16244 | | * ssl SSL/TLS object. |
16245 | | */ |
16246 | | int wolfSSL_request_certificate(WOLFSSL* ssl) |
16247 | | { |
16248 | | int ret; |
16249 | | #ifndef NO_WOLFSSL_SERVER |
16250 | | CertReqCtx* certReqCtx; |
16251 | | #endif |
16252 | | |
16253 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16254 | | return BAD_FUNC_ARG; |
16255 | | #ifndef NO_WOLFSSL_SERVER |
16256 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
16257 | | return SIDE_ERROR; |
16258 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
16259 | | return NOT_READY_ERROR; |
16260 | | if (!ssl->options.postHandshakeAuth) |
16261 | | return POST_HAND_AUTH_ERROR; |
16262 | | if (ssl->certReqCtx != NULL) { |
16263 | | if (ssl->certReqCtx->len != 1) |
16264 | | return BAD_STATE_E; |
16265 | | /* We support sending up to 255 certificate requests */ |
16266 | | if (ssl->certReqCtx->ctx == 255) |
16267 | | return BAD_STATE_E; |
16268 | | } |
16269 | | |
16270 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap, |
16271 | | DYNAMIC_TYPE_TMP_BUFFER); |
16272 | | if (certReqCtx == NULL) |
16273 | | return MEMORY_E; |
16274 | | XMEMSET(certReqCtx, 0, sizeof(CertReqCtx)); |
16275 | | certReqCtx->next = ssl->certReqCtx; |
16276 | | certReqCtx->len = 1; |
16277 | | if (certReqCtx->next != NULL) |
16278 | | certReqCtx->ctx = certReqCtx->next->ctx + 1; |
16279 | | ssl->certReqCtx = certReqCtx; |
16280 | | |
16281 | | ssl->msgsReceived.got_certificate = 0; |
16282 | | ssl->msgsReceived.got_certificate_verify = 0; |
16283 | | ssl->msgsReceived.got_finished = 0; |
16284 | | |
16285 | | ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len); |
16286 | | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
16287 | | ret = WOLFSSL_ERROR_WANT_WRITE; |
16288 | | else if (ret == 0) |
16289 | | ret = WOLFSSL_SUCCESS; |
16290 | | #else |
16291 | | ret = SIDE_ERROR; |
16292 | | #endif |
16293 | | |
16294 | | return ret; |
16295 | | } |
16296 | | #endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */ |
16297 | | |
16298 | | #if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
16299 | | /* Get the preferred key exchange group. |
16300 | | * |
16301 | | * ssl The SSL/TLS object. |
16302 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, |
16303 | | * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete |
16304 | | * and group number on success. |
16305 | | */ |
16306 | | int wolfSSL_preferred_group(WOLFSSL* ssl) |
16307 | 0 | { |
16308 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16309 | 0 | return BAD_FUNC_ARG; |
16310 | 0 | #ifndef NO_WOLFSSL_CLIENT |
16311 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
16312 | 0 | return SIDE_ERROR; |
16313 | 0 | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
16314 | 0 | return NOT_READY_ERROR; |
16315 | | |
16316 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
16317 | | /* Return supported groups only. */ |
16318 | 0 | return TLSX_SupportedCurve_Preferred(ssl, 1); |
16319 | | #else |
16320 | | return 0; |
16321 | | #endif |
16322 | | #else |
16323 | | return SIDE_ERROR; |
16324 | | #endif |
16325 | 0 | } |
16326 | | #endif |
16327 | | |
16328 | | #ifndef NO_PSK |
16329 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
16330 | | * against context object. |
16331 | | * |
16332 | | * @param [in, out] ctx SSL/TLS context object. |
16333 | | * @param [in] cb Client PSK callback passed a cipher suite. |
16334 | | */ |
16335 | | void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, |
16336 | | wc_psk_client_cs_callback cb) |
16337 | | { |
16338 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_cs_callback"); |
16339 | | |
16340 | | if (ctx == NULL) |
16341 | | return; |
16342 | | |
16343 | | ctx->havePSK = 1; |
16344 | | ctx->client_psk_cs_cb = cb; |
16345 | | } |
16346 | | |
16347 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
16348 | | * against SSL object. |
16349 | | * |
16350 | | * @param [in, out] ssl SSL/TLS object. |
16351 | | * @param [in] cb Client PSK callback passed a cipher suite. |
16352 | | */ |
16353 | | void wolfSSL_set_psk_client_cs_callback(WOLFSSL* ssl, |
16354 | | wc_psk_client_cs_callback cb) |
16355 | | { |
16356 | | byte haveRSA = 1; |
16357 | | int keySz = 0; |
16358 | | |
16359 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_cs_callback"); |
16360 | | |
16361 | | if (ssl == NULL) |
16362 | | return; |
16363 | | |
16364 | | ssl->options.havePSK = 1; |
16365 | | ssl->options.client_psk_cs_cb = cb; |
16366 | | |
16367 | | #ifdef NO_RSA |
16368 | | haveRSA = 0; |
16369 | | #endif |
16370 | | #ifndef NO_CERTS |
16371 | | keySz = ssl->buffers.keySz; |
16372 | | #endif |
16373 | | if (AllocateSuites(ssl) != 0) |
16374 | | return; |
16375 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
16376 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
16377 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
16378 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
16379 | | } |
16380 | | |
16381 | | /* Set the PSK callback that returns the cipher suite for a client to use |
16382 | | * against context object. |
16383 | | * |
16384 | | * @param [in, out] ctx SSL/TLS context object. |
16385 | | * @param [in] cb Client PSK callback returning cipher suite. |
16386 | | */ |
16387 | | void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, |
16388 | | wc_psk_client_tls13_callback cb) |
16389 | | { |
16390 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_tls13_callback"); |
16391 | | |
16392 | | if (ctx == NULL) |
16393 | | return; |
16394 | | |
16395 | | ctx->havePSK = 1; |
16396 | | ctx->client_psk_tls13_cb = cb; |
16397 | | } |
16398 | | |
16399 | | /* Set the PSK callback that returns the cipher suite for a client to use |
16400 | | * against SSL object. |
16401 | | * |
16402 | | * @param [in, out] ssl SSL/TLS object. |
16403 | | * @param [in] cb Client PSK callback returning cipher suite. |
16404 | | */ |
16405 | | void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl, |
16406 | | wc_psk_client_tls13_callback cb) |
16407 | | { |
16408 | | byte haveRSA = 1; |
16409 | | int keySz = 0; |
16410 | | |
16411 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_tls13_callback"); |
16412 | | |
16413 | | if (ssl == NULL) |
16414 | | return; |
16415 | | |
16416 | | ssl->options.havePSK = 1; |
16417 | | ssl->options.client_psk_tls13_cb = cb; |
16418 | | |
16419 | | #ifdef NO_RSA |
16420 | | haveRSA = 0; |
16421 | | #endif |
16422 | | #ifndef NO_CERTS |
16423 | | keySz = ssl->buffers.keySz; |
16424 | | #endif |
16425 | | if (AllocateSuites(ssl) != 0) |
16426 | | return; |
16427 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
16428 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
16429 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
16430 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
16431 | | } |
16432 | | |
16433 | | /* Set the PSK callback that returns the cipher suite for a server to use |
16434 | | * against context object. |
16435 | | * |
16436 | | * @param [in, out] ctx SSL/TLS context object. |
16437 | | * @param [in] cb Server PSK callback returning cipher suite. |
16438 | | */ |
16439 | | void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, |
16440 | | wc_psk_server_tls13_callback cb) |
16441 | | { |
16442 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_tls13_callback"); |
16443 | | if (ctx == NULL) |
16444 | | return; |
16445 | | ctx->havePSK = 1; |
16446 | | ctx->server_psk_tls13_cb = cb; |
16447 | | } |
16448 | | |
16449 | | /* Set the PSK callback that returns the cipher suite for a server to use |
16450 | | * against SSL object. |
16451 | | * |
16452 | | * @param [in, out] ssl SSL/TLS object. |
16453 | | * @param [in] cb Server PSK callback returning cipher suite. |
16454 | | */ |
16455 | | void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl, |
16456 | | wc_psk_server_tls13_callback cb) |
16457 | | { |
16458 | | byte haveRSA = 1; |
16459 | | int keySz = 0; |
16460 | | |
16461 | | WOLFSSL_ENTER("wolfSSL_set_psk_server_tls13_callback"); |
16462 | | if (ssl == NULL) |
16463 | | return; |
16464 | | |
16465 | | ssl->options.havePSK = 1; |
16466 | | ssl->options.server_psk_tls13_cb = cb; |
16467 | | |
16468 | | #ifdef NO_RSA |
16469 | | haveRSA = 0; |
16470 | | #endif |
16471 | | #ifndef NO_CERTS |
16472 | | keySz = ssl->buffers.keySz; |
16473 | | #endif |
16474 | | if (AllocateSuites(ssl) != 0) |
16475 | | return; |
16476 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
16477 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
16478 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
16479 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
16480 | | } |
16481 | | |
16482 | | /* Get name of first supported cipher suite that uses the hash indicated. |
16483 | | * |
16484 | | * @param [in] ssl SSL/TLS object. |
16485 | | * @param [in] hash Name of hash algorithm. e.g. "SHA256", "SHA384" |
16486 | | * @return Name of cipher suite. |
16487 | | * @return NULL on failure. |
16488 | | */ |
16489 | | const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash) |
16490 | | { |
16491 | | const char* name = NULL; |
16492 | | byte mac = no_mac; |
16493 | | int i; |
16494 | | const Suites* suites; |
16495 | | |
16496 | | if (hash == NULL || ssl == NULL || |
16497 | | (ssl->suites == NULL && ssl->ctx == NULL)) |
16498 | | return NULL; |
16499 | | |
16500 | | suites = WOLFSSL_SUITES(ssl); |
16501 | | if (suites == NULL) |
16502 | | return NULL; |
16503 | | |
16504 | | if (XSTRCMP(hash, "SHA256") == 0) { |
16505 | | mac = sha256_mac; |
16506 | | } |
16507 | | else if (XSTRCMP(hash, "SHA384") == 0) { |
16508 | | mac = sha384_mac; |
16509 | | } |
16510 | | if (mac != no_mac) { |
16511 | | for (i = 0; i < suites->suiteSz; i += 2) { |
16512 | | if (SuiteMac(suites->suites + i) == mac) { |
16513 | | name = GetCipherNameInternal(suites->suites[i + 0], |
16514 | | suites->suites[i + 1]); |
16515 | | break; |
16516 | | } |
16517 | | } |
16518 | | } |
16519 | | return name; |
16520 | | } |
16521 | | #endif /* !NO_PSK */ |
16522 | | |
16523 | | |
16524 | | #ifndef NO_WOLFSSL_SERVER |
16525 | | |
16526 | | /* The server accepting a connection from a client. |
16527 | | * The protocol version is expecting to be TLS v1.3. |
16528 | | * If the client downgrades, and older versions of the protocol are compiled |
16529 | | * in, the server will fallback to wolfSSL_accept(). |
16530 | | * Please see note at top of README if you get an error from accept. |
16531 | | * |
16532 | | * ssl The SSL/TLS object. |
16533 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
16534 | | * unrecoverable error occurs and 0 otherwise. |
16535 | | * For more error information use wolfSSL_get_error(). |
16536 | | */ |
16537 | | int wolfSSL_accept_TLSv13(WOLFSSL* ssl) |
16538 | | { |
16539 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
16540 | | word16 havePSK = 0; |
16541 | | #endif |
16542 | | int ret = 0; |
16543 | | |
16544 | | WOLFSSL_ENTER("wolfSSL_accept_TLSv13"); |
16545 | | |
16546 | | #ifdef HAVE_ERRNO_H |
16547 | | errno = 0; |
16548 | | #endif |
16549 | | |
16550 | | if (ssl == NULL) |
16551 | | return WOLFSSL_FATAL_ERROR; |
16552 | | |
16553 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
16554 | | havePSK = ssl->options.havePSK; |
16555 | | #endif |
16556 | | |
16557 | | if (ssl->options.side != WOLFSSL_SERVER_END) { |
16558 | | ssl->error = SIDE_ERROR; |
16559 | | WOLFSSL_ERROR(ssl->error); |
16560 | | return WOLFSSL_FATAL_ERROR; |
16561 | | } |
16562 | | |
16563 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
16564 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
16565 | | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
16566 | | return ret; |
16567 | | } |
16568 | | |
16569 | | #ifdef WOLFSSL_DTLS |
16570 | | if (ssl->version.major == DTLS_MAJOR) { |
16571 | | ssl->options.dtls = 1; |
16572 | | if (!IsDtlsNotSctpMode(ssl) || !ssl->options.sendCookie) |
16573 | | ssl->options.dtlsStateful = 1; |
16574 | | } |
16575 | | #endif |
16576 | | |
16577 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
16578 | | if ((ssl->AcceptFilter != NULL) && |
16579 | | ((ssl->options.acceptState == TLS13_ACCEPT_BEGIN) |
16580 | | #ifdef HAVE_SECURE_RENEGOTIATION |
16581 | | || (ssl->options.acceptState == TLS13_ACCEPT_BEGIN_RENEG) |
16582 | | #endif |
16583 | | )) |
16584 | | { |
16585 | | wolfSSL_netfilter_decision_t res; |
16586 | | if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) == |
16587 | | WOLFSSL_SUCCESS) && |
16588 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
16589 | | ssl->error = SOCKET_FILTERED_E; |
16590 | | WOLFSSL_ERROR(ssl->error); |
16591 | | return WOLFSSL_FATAL_ERROR; |
16592 | | } |
16593 | | } |
16594 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
16595 | | |
16596 | | #ifndef NO_CERTS |
16597 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16598 | | if (!havePSK) |
16599 | | #endif |
16600 | | { |
16601 | | #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \ |
16602 | | defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY) |
16603 | | if (ssl->ctx->certSetupCb != NULL) { |
16604 | | WOLFSSL_MSG("CertSetupCb set. server cert and " |
16605 | | "key not checked"); |
16606 | | } |
16607 | | else |
16608 | | #endif |
16609 | | { |
16610 | | if (!ssl->buffers.certificate || |
16611 | | !ssl->buffers.certificate->buffer) { |
16612 | | |
16613 | | WOLFSSL_MSG("accept error: server cert required"); |
16614 | | ssl->error = NO_PRIVATE_KEY; |
16615 | | WOLFSSL_ERROR(ssl->error); |
16616 | | return WOLFSSL_FATAL_ERROR; |
16617 | | } |
16618 | | |
16619 | | if (!ssl->buffers.key || !ssl->buffers.key->buffer) { |
16620 | | /* allow no private key if using existing key */ |
16621 | | #ifdef WOLF_PRIVATE_KEY_ID |
16622 | | if (ssl->devId != INVALID_DEVID |
16623 | | #ifdef HAVE_PK_CALLBACKS |
16624 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
16625 | | #endif |
16626 | | ) { |
16627 | | WOLFSSL_MSG("Allowing no server private key (external)"); |
16628 | | } |
16629 | | else |
16630 | | #endif |
16631 | | { |
16632 | | WOLFSSL_MSG("accept error: server key required"); |
16633 | | ssl->error = NO_PRIVATE_KEY; |
16634 | | WOLFSSL_ERROR(ssl->error); |
16635 | | return WOLFSSL_FATAL_ERROR; |
16636 | | } |
16637 | | } |
16638 | | } |
16639 | | } |
16640 | | #endif /* NO_CERTS */ |
16641 | | |
16642 | | if (ssl->buffers.outputBuffer.length > 0 |
16643 | | #ifdef WOLFSSL_ASYNC_CRYPT |
16644 | | /* do not send buffered or advance state if last error was an |
16645 | | async pending operation */ |
16646 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
16647 | | #endif |
16648 | | ) { |
16649 | | |
16650 | | /* fragOffset is non-zero when sending fragments. On the last |
16651 | | * fragment, fragOffset is zero again, and the state can be |
16652 | | * advanced. */ |
16653 | | int advanceState = |
16654 | | (ssl->options.acceptState == TLS13_ACCEPT_CLIENT_HELLO_DONE || |
16655 | | ssl->options.acceptState == |
16656 | | TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE || |
16657 | | ssl->options.acceptState == TLS13_ACCEPT_SECOND_REPLY_DONE || |
16658 | | ssl->options.acceptState == TLS13_SERVER_HELLO_SENT || |
16659 | | ssl->options.acceptState == TLS13_ACCEPT_THIRD_REPLY_DONE || |
16660 | | ssl->options.acceptState == TLS13_SERVER_EXTENSIONS_SENT || |
16661 | | ssl->options.acceptState == TLS13_CERT_REQ_SENT || |
16662 | | ssl->options.acceptState == TLS13_CERT_SENT || |
16663 | | ssl->options.acceptState == TLS13_CERT_VERIFY_SENT || |
16664 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_SENT || |
16665 | | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_DONE); |
16666 | | |
16667 | | #ifdef WOLFSSL_DTLS13 |
16668 | | if (ssl->options.dtls) |
16669 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
16670 | | && !ssl->dtls13SendingAckOrRtx; |
16671 | | #endif /* WOLFSSL_DTLS13 */ |
16672 | | |
16673 | | ret = SendBuffered(ssl); |
16674 | | if (ret == 0) { |
16675 | | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
16676 | | if (advanceState) { |
16677 | | ssl->options.acceptState++; |
16678 | | WOLFSSL_MSG("accept state: " |
16679 | | "Advanced from last buffered fragment send"); |
16680 | | #ifdef WOLFSSL_ASYNC_IO |
16681 | | FreeAsyncCtx(ssl, 0); |
16682 | | #endif |
16683 | | } |
16684 | | } |
16685 | | else { |
16686 | | WOLFSSL_MSG("accept state: " |
16687 | | "Not advanced, more fragments to send"); |
16688 | | } |
16689 | | |
16690 | | #ifdef WOLFSSL_DTLS13 |
16691 | | if (ssl->options.dtls) |
16692 | | ssl->dtls13SendingAckOrRtx = 0; |
16693 | | #endif /* WOLFSSL_DTLS13 */ |
16694 | | |
16695 | | } |
16696 | | else { |
16697 | | ssl->error = ret; |
16698 | | WOLFSSL_ERROR(ssl->error); |
16699 | | return WOLFSSL_FATAL_ERROR; |
16700 | | } |
16701 | | } |
16702 | | |
16703 | | ret = RetrySendAlert(ssl); |
16704 | | if (ret != 0) { |
16705 | | ssl->error = ret; |
16706 | | WOLFSSL_ERROR(ssl->error); |
16707 | | return WOLFSSL_FATAL_ERROR; |
16708 | | } |
16709 | | #ifdef WOLFSSL_DTLS13 |
16710 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
16711 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
16712 | | WOLFSSL_ERROR(ssl->error); |
16713 | | return WOLFSSL_FATAL_ERROR; |
16714 | | } |
16715 | | |
16716 | | /* we sent all the fragments. Advance state. */ |
16717 | | ssl->options.acceptState++; |
16718 | | } |
16719 | | #endif /* WOLFSSL_DTLS13 */ |
16720 | | |
16721 | | switch (ssl->options.acceptState) { |
16722 | | |
16723 | | #ifdef HAVE_SECURE_RENEGOTIATION |
16724 | | case TLS13_ACCEPT_BEGIN_RENEG: |
16725 | | #endif |
16726 | | case TLS13_ACCEPT_BEGIN : |
16727 | | /* get client_hello */ |
16728 | | |
16729 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
16730 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
16731 | | WOLFSSL_ERROR(ssl->error); |
16732 | | return WOLFSSL_FATAL_ERROR; |
16733 | | } |
16734 | | |
16735 | | #ifdef WOLFSSL_DTLS13 |
16736 | | if (ssl->options.dtls) { |
16737 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
16738 | | WOLFSSL_ERROR(ssl->error); |
16739 | | return WOLFSSL_FATAL_ERROR; |
16740 | | } |
16741 | | } |
16742 | | #endif /* WOLFSSL_DTLS13 */ |
16743 | | |
16744 | | } |
16745 | | |
16746 | | ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE; |
16747 | | WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE"); |
16748 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
16749 | | return wolfSSL_accept(ssl); |
16750 | | FALL_THROUGH; |
16751 | | |
16752 | | case TLS13_ACCEPT_CLIENT_HELLO_DONE : |
16753 | | if (ssl->options.serverState == |
16754 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
16755 | | if ((ssl->error = SendTls13ServerHello(ssl, |
16756 | | hello_retry_request)) != 0) { |
16757 | | WOLFSSL_ERROR(ssl->error); |
16758 | | return WOLFSSL_FATAL_ERROR; |
16759 | | } |
16760 | | } |
16761 | | |
16762 | | ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE; |
16763 | | WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE"); |
16764 | | FALL_THROUGH; |
16765 | | |
16766 | | case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE : |
16767 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
16768 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
16769 | | && ssl->options.serverState == |
16770 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
16771 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
16772 | | WOLFSSL_ERROR(ssl->error); |
16773 | | return WOLFSSL_FATAL_ERROR; |
16774 | | } |
16775 | | ssl->options.sentChangeCipher = 1; |
16776 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
16777 | | } |
16778 | | #endif |
16779 | | ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE; |
16780 | | WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE"); |
16781 | | FALL_THROUGH; |
16782 | | |
16783 | | case TLS13_ACCEPT_FIRST_REPLY_DONE : |
16784 | | if (ssl->options.serverState == |
16785 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
16786 | | ssl->options.clientState = CLIENT_HELLO_RETRY; |
16787 | | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
16788 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
16789 | | WOLFSSL_ERROR(ssl->error); |
16790 | | return WOLFSSL_FATAL_ERROR; |
16791 | | } |
16792 | | |
16793 | | #ifdef WOLFSSL_DTLS13 |
16794 | | if (ssl->options.dtls) { |
16795 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
16796 | | WOLFSSL_ERROR(ssl->error); |
16797 | | return WOLFSSL_FATAL_ERROR; |
16798 | | } |
16799 | | } |
16800 | | #endif /* WOLFSSL_DTLS13 */ |
16801 | | |
16802 | | } |
16803 | | } |
16804 | | |
16805 | | ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE; |
16806 | | WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE"); |
16807 | | FALL_THROUGH; |
16808 | | |
16809 | | case TLS13_ACCEPT_SECOND_REPLY_DONE : |
16810 | | if (ssl->options.returnOnGoodCh) { |
16811 | | /* Higher level in stack wants us to return. Simulate a |
16812 | | * WANT_WRITE to accomplish this. */ |
16813 | | ssl->error = WANT_WRITE; |
16814 | | return WOLFSSL_FATAL_ERROR; |
16815 | | } |
16816 | | |
16817 | | if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) { |
16818 | | WOLFSSL_ERROR(ssl->error); |
16819 | | return WOLFSSL_FATAL_ERROR; |
16820 | | } |
16821 | | ssl->options.acceptState = TLS13_SERVER_HELLO_SENT; |
16822 | | WOLFSSL_MSG("accept state SERVER_HELLO_SENT"); |
16823 | | FALL_THROUGH; |
16824 | | |
16825 | | case TLS13_SERVER_HELLO_SENT : |
16826 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
16827 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
16828 | | && !ssl->options.sentChangeCipher && !ssl->options.dtls) { |
16829 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
16830 | | WOLFSSL_ERROR(ssl->error); |
16831 | | return WOLFSSL_FATAL_ERROR; |
16832 | | } |
16833 | | ssl->options.sentChangeCipher = 1; |
16834 | | } |
16835 | | #endif |
16836 | | |
16837 | | ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE; |
16838 | | WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE"); |
16839 | | FALL_THROUGH; |
16840 | | |
16841 | | case TLS13_ACCEPT_THIRD_REPLY_DONE : |
16842 | | #ifdef HAVE_SUPPORTED_CURVES |
16843 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
16844 | | if (!ssl->options.noPskDheKe) |
16845 | | #endif |
16846 | | { |
16847 | | ssl->error = TLSX_KeyShare_DeriveSecret(ssl); |
16848 | | if (ssl->error != 0) |
16849 | | return WOLFSSL_FATAL_ERROR; |
16850 | | } |
16851 | | #endif |
16852 | | |
16853 | | if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) { |
16854 | | WOLFSSL_ERROR(ssl->error); |
16855 | | return WOLFSSL_FATAL_ERROR; |
16856 | | } |
16857 | | ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT; |
16858 | | WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT"); |
16859 | | FALL_THROUGH; |
16860 | | |
16861 | | case TLS13_SERVER_EXTENSIONS_SENT : |
16862 | | #ifndef NO_CERTS |
16863 | | if (!ssl->options.resuming) { |
16864 | | if (ssl->options.verifyPeer |
16865 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
16866 | | && !ssl->options.verifyPostHandshake |
16867 | | #endif |
16868 | | ) { |
16869 | | ssl->error = SendTls13CertificateRequest(ssl, NULL, 0); |
16870 | | if (ssl->error != 0) { |
16871 | | WOLFSSL_ERROR(ssl->error); |
16872 | | return WOLFSSL_FATAL_ERROR; |
16873 | | } |
16874 | | } |
16875 | | else { |
16876 | | /* SERVER: Peer auth good if not verifying client. */ |
16877 | | ssl->options.peerAuthGood = 1; |
16878 | | } |
16879 | | } |
16880 | | #endif |
16881 | | ssl->options.acceptState = TLS13_CERT_REQ_SENT; |
16882 | | WOLFSSL_MSG("accept state CERT_REQ_SENT"); |
16883 | | FALL_THROUGH; |
16884 | | |
16885 | | case TLS13_CERT_REQ_SENT : |
16886 | | #ifndef NO_CERTS |
16887 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
16888 | | if ((ssl->error = SendTls13Certificate(ssl)) != 0) { |
16889 | | WOLFSSL_ERROR(ssl->error); |
16890 | | return WOLFSSL_FATAL_ERROR; |
16891 | | } |
16892 | | } |
16893 | | #endif |
16894 | | ssl->options.acceptState = TLS13_CERT_SENT; |
16895 | | WOLFSSL_MSG("accept state CERT_SENT"); |
16896 | | FALL_THROUGH; |
16897 | | |
16898 | | case TLS13_CERT_SENT : |
16899 | | #if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
16900 | | defined(HAVE_ED25519) || defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
16901 | | defined(WOLFSSL_HAVE_MLDSA) || defined(WOLFSSL_HAVE_SLHDSA)) |
16902 | | if (!ssl->options.resuming && ssl->options.sendVerify) { |
16903 | | if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) { |
16904 | | WOLFSSL_ERROR(ssl->error); |
16905 | | return WOLFSSL_FATAL_ERROR; |
16906 | | } |
16907 | | } |
16908 | | #endif |
16909 | | ssl->options.acceptState = TLS13_CERT_VERIFY_SENT; |
16910 | | WOLFSSL_MSG("accept state CERT_VERIFY_SENT"); |
16911 | | FALL_THROUGH; |
16912 | | |
16913 | | case TLS13_CERT_VERIFY_SENT : |
16914 | | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
16915 | | WOLFSSL_ERROR(ssl->error); |
16916 | | return WOLFSSL_FATAL_ERROR; |
16917 | | } |
16918 | | |
16919 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT; |
16920 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT"); |
16921 | | FALL_THROUGH; |
16922 | | |
16923 | | case TLS13_ACCEPT_FINISHED_SENT: |
16924 | | #ifdef WOLFSSL_EARLY_DATA |
16925 | | if (ssl->earlyData != no_early_data && |
16926 | | ssl->options.handShakeState != SERVER_FINISHED_COMPLETE) { |
16927 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
16928 | | return WOLFSSL_SUCCESS; |
16929 | | } |
16930 | | #endif |
16931 | | #ifdef HAVE_SESSION_TICKET |
16932 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
16933 | | if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 && |
16934 | | ssl->ctx->ticketEncCb != NULL && |
16935 | | ssl->options.maxTicketTls13 > 0) { |
16936 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
16937 | | WOLFSSL_ERROR(ssl->error); |
16938 | | return WOLFSSL_FATAL_ERROR; |
16939 | | } |
16940 | | ssl->options.ticketsSent = 1; |
16941 | | } |
16942 | | #endif |
16943 | | #endif /* HAVE_SESSION_TICKET */ |
16944 | | ssl->options.acceptState = TLS13_PRE_TICKET_SENT; |
16945 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
16946 | | FALL_THROUGH; |
16947 | | |
16948 | | case TLS13_PRE_TICKET_SENT : |
16949 | | while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
16950 | | if ( (ssl->error = ProcessReply(ssl)) < 0) { |
16951 | | WOLFSSL_ERROR(ssl->error); |
16952 | | return WOLFSSL_FATAL_ERROR; |
16953 | | } |
16954 | | |
16955 | | #ifdef WOLFSSL_DTLS13 |
16956 | | if (ssl->options.dtls) { |
16957 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
16958 | | WOLFSSL_ERROR(ssl->error); |
16959 | | return WOLFSSL_FATAL_ERROR; |
16960 | | } |
16961 | | } |
16962 | | #endif /* WOLFSSL_DTLS13 */ |
16963 | | } |
16964 | | |
16965 | | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE; |
16966 | | WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE"); |
16967 | | FALL_THROUGH; |
16968 | | |
16969 | | case TLS13_ACCEPT_FINISHED_DONE : |
16970 | | /* SERVER: When not resuming and verifying peer but no certificate |
16971 | | * received and not failing when not received then peer auth good. |
16972 | | */ |
16973 | | if (!ssl->options.resuming && ssl->options.verifyPeer && |
16974 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
16975 | | !ssl->options.verifyPostHandshake && |
16976 | | #endif |
16977 | | !ssl->options.havePeerCert && !ssl->options.failNoCert) { |
16978 | | ssl->options.peerAuthGood = 1; |
16979 | | } |
16980 | | /* SERVER: check peer authentication. */ |
16981 | | if (!ssl->options.peerAuthGood) { |
16982 | | WOLFSSL_MSG("Client authentication did not happen"); |
16983 | | return WOLFSSL_FATAL_ERROR; |
16984 | | } |
16985 | | #ifdef HAVE_SESSION_TICKET |
16986 | | while (ssl->options.ticketsSent < ssl->options.maxTicketTls13) { |
16987 | | if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb |
16988 | | != NULL) { |
16989 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
16990 | | WOLFSSL_ERROR(ssl->error); |
16991 | | return WOLFSSL_FATAL_ERROR; |
16992 | | } |
16993 | | } |
16994 | | ssl->options.ticketsSent++; |
16995 | | |
16996 | | /* only one session ticket is sent on session resumption */ |
16997 | | if (ssl->options.resuming) { |
16998 | | break; |
16999 | | } |
17000 | | } |
17001 | | #endif /* HAVE_SESSION_TICKET */ |
17002 | | ssl->options.acceptState = TLS13_TICKET_SENT; |
17003 | | WOLFSSL_MSG("accept state TICKET_SENT"); |
17004 | | FALL_THROUGH; |
17005 | | |
17006 | | case TLS13_TICKET_SENT : |
17007 | | #ifndef NO_HANDSHAKE_DONE_CB |
17008 | | if (ssl->hsDoneCb) { |
17009 | | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
17010 | | if (cbret < 0) { |
17011 | | ssl->error = cbret; |
17012 | | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
17013 | | return WOLFSSL_FATAL_ERROR; |
17014 | | } |
17015 | | } |
17016 | | #endif /* NO_HANDSHAKE_DONE_CB */ |
17017 | | |
17018 | | if (!ssl->options.keepResources) { |
17019 | | FreeHandshakeResources(ssl); |
17020 | | } |
17021 | | |
17022 | | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
17023 | | /* Free the remaining async context if not using it for crypto */ |
17024 | | FreeAsyncCtx(ssl, 1); |
17025 | | #endif |
17026 | | |
17027 | | ssl->error = 0; /* clear the error */ |
17028 | | |
17029 | | WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS); |
17030 | | return WOLFSSL_SUCCESS; |
17031 | | |
17032 | | default: |
17033 | | WOLFSSL_MSG("Unknown accept state ERROR"); |
17034 | | return WOLFSSL_FATAL_ERROR; |
17035 | | } |
17036 | | } |
17037 | | #endif |
17038 | | |
17039 | | #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET) |
17040 | | /* Server sends a session ticket to the peer. |
17041 | | * |
17042 | | * RFC 8446, section 4.6.1, para 1. |
17043 | | * |
17044 | | * ssl The SSL/TLS object. |
17045 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
17046 | | * SIDE_ERROR when not a server, |
17047 | | * NOT_READY_ERROR when handshake not complete, |
17048 | | * WOLFSSL_FATAL_ERROR when creating or sending message fails, and |
17049 | | * WOLFSSL_SUCCESS on success. |
17050 | | */ |
17051 | | int wolfSSL_send_SessionTicket(WOLFSSL* ssl) |
17052 | | { |
17053 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
17054 | | return BAD_FUNC_ARG; |
17055 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
17056 | | return SIDE_ERROR; |
17057 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
17058 | | return NOT_READY_ERROR; |
17059 | | |
17060 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
17061 | | WOLFSSL_ERROR(ssl->error); |
17062 | | return WOLFSSL_FATAL_ERROR; |
17063 | | } |
17064 | | ssl->options.ticketsSent++; |
17065 | | |
17066 | | return WOLFSSL_SUCCESS; |
17067 | | } |
17068 | | #endif |
17069 | | |
17070 | | #ifdef WOLFSSL_EARLY_DATA |
17071 | | /* Sets the maximum amount of early data that can be seen by server when using |
17072 | | * session tickets for resumption. |
17073 | | * A value of zero indicates no early data is to be sent by client using session |
17074 | | * tickets. |
17075 | | * |
17076 | | * The default value is zero: per RFC 8446 Appendix E.5, TLS implementations |
17077 | | * "MUST NOT enable 0-RTT (either sending or accepting) unless specifically |
17078 | | * requested by the application." Servers must explicitly opt in by calling |
17079 | | * this function (or the per-SSL equivalent) with a non-zero value. |
17080 | | * |
17081 | | * ctx The SSL/TLS CTX object. |
17082 | | * sz Maximum size of the early data. |
17083 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
17084 | | * 0 on success. |
17085 | | */ |
17086 | | int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz) |
17087 | | { |
17088 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
17089 | | return BAD_FUNC_ARG; |
17090 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
17091 | | return SIDE_ERROR; |
17092 | | |
17093 | | ctx->maxEarlyDataSz = sz; |
17094 | | |
17095 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
17096 | | /* 1 on success in OpenSSL*/ |
17097 | | return WOLFSSL_SUCCESS; |
17098 | | #else |
17099 | | return 0; |
17100 | | #endif |
17101 | | } |
17102 | | |
17103 | | /* Sets the maximum amount of early data that a client or server would like |
17104 | | * to exchange. Servers will advertise this value in session tickets sent |
17105 | | * to a client. |
17106 | | * A value of zero indicates no early data will be sent by a client, or |
17107 | | * no early data is accepted by a server (and announced as such in send out |
17108 | | * session tickets). |
17109 | | * |
17110 | | * ssl The SSL/TLS object. |
17111 | | * sz Maximum size of the early data. |
17112 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
17113 | | * and 0 on success. |
17114 | | */ |
17115 | | int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz) |
17116 | | { |
17117 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
17118 | | return BAD_FUNC_ARG; |
17119 | | |
17120 | | ssl->options.maxEarlyDataSz = sz; |
17121 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
17122 | | /* 1 on success in OpenSSL*/ |
17123 | | return WOLFSSL_SUCCESS; |
17124 | | #else |
17125 | | return 0; |
17126 | | #endif |
17127 | | } |
17128 | | |
17129 | | /* Gets the maximum amount of early data that can be seen by server when using |
17130 | | * session tickets for resumption. |
17131 | | * A value of zero indicates no early data is to be sent by client using session |
17132 | | * tickets. |
17133 | | * |
17134 | | * ctx The SSL/TLS CTX object. |
17135 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
17136 | | * returns the maximum amount of early data to be set |
17137 | | */ |
17138 | | int wolfSSL_CTX_get_max_early_data(WOLFSSL_CTX* ctx) |
17139 | | { |
17140 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
17141 | | return BAD_FUNC_ARG; |
17142 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
17143 | | return SIDE_ERROR; |
17144 | | |
17145 | | return ctx->maxEarlyDataSz; |
17146 | | } |
17147 | | |
17148 | | /* Gets the maximum amount of early data that can be seen by server when using |
17149 | | * session tickets for resumption. |
17150 | | * A value of zero indicates no early data is to be sent by client using session |
17151 | | * tickets. |
17152 | | * |
17153 | | * ssl The SSL/TLS object. |
17154 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
17155 | | * SIDE_ERROR when not a server and |
17156 | | * returns the maximum amount of early data to be set |
17157 | | */ |
17158 | | int wolfSSL_get_max_early_data(WOLFSSL* ssl) |
17159 | | { |
17160 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
17161 | | return BAD_FUNC_ARG; |
17162 | | |
17163 | | return ssl->options.maxEarlyDataSz; |
17164 | | } |
17165 | | |
17166 | | /* Write early data to the server. |
17167 | | * |
17168 | | * ssl The SSL/TLS object. |
17169 | | * data Early data to write |
17170 | | * sz The size of the early data in bytes. |
17171 | | * outSz The number of early data bytes written. |
17172 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
17173 | | * or not using TLS v1.3. SIDE ERROR when not a server. BAD_STATE_E if invoked |
17174 | | * without a valid session or without a valid PSK CB. |
17175 | | * Otherwise the number of early data bytes written. |
17176 | | */ |
17177 | | int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz) |
17178 | | { |
17179 | | int ret = 0; |
17180 | | |
17181 | | WOLFSSL_ENTER("wolfSSL_write_early_data"); |
17182 | | |
17183 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
17184 | | return BAD_FUNC_ARG; |
17185 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
17186 | | return BAD_FUNC_ARG; |
17187 | | |
17188 | | *outSz = 0; |
17189 | | |
17190 | | #ifndef NO_WOLFSSL_CLIENT |
17191 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
17192 | | return SIDE_ERROR; |
17193 | | |
17194 | | /* Early data requires PSK or session resumption */ |
17195 | | if (!EarlyDataPossible(ssl)) { |
17196 | | return BAD_STATE_E; |
17197 | | } |
17198 | | |
17199 | | if (ssl->options.handShakeState == NULL_STATE) { |
17200 | | /* avoid re-setting ssl->earlyData if we re-enter the function because |
17201 | | * of WC_PENDING_E, WANT_WRITE or WANT_READ */ |
17202 | | if (ssl->error == 0) |
17203 | | ssl->earlyData = expecting_early_data; |
17204 | | ret = wolfSSL_connect_TLSv13(ssl); |
17205 | | if (ret != WOLFSSL_SUCCESS) |
17206 | | return WOLFSSL_FATAL_ERROR; |
17207 | | /* on client side, status is set to rejected */ |
17208 | | /* until sever accepts the early data extension. */ |
17209 | | ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED; |
17210 | | } |
17211 | | if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) { |
17212 | | #ifdef OPENSSL_EXTRA |
17213 | | /* when processed early data exceeds max size */ |
17214 | | if (ssl->session->maxEarlyDataSz > 0 && |
17215 | | (ssl->earlyDataSz + sz > ssl->session->maxEarlyDataSz)) { |
17216 | | ssl->error = TOO_MUCH_EARLY_DATA; |
17217 | | return WOLFSSL_FATAL_ERROR; |
17218 | | } |
17219 | | #endif |
17220 | | ret = SendData(ssl, data, sz); |
17221 | | if (ret > 0) { |
17222 | | *outSz = ret; |
17223 | | /* store amount of processed early data from client */ |
17224 | | ssl->earlyDataSz += ret; |
17225 | | } |
17226 | | } |
17227 | | #else |
17228 | | return SIDE_ERROR; |
17229 | | #endif |
17230 | | |
17231 | | WOLFSSL_LEAVE("wolfSSL_write_early_data", ret); |
17232 | | |
17233 | | if (ret < 0) |
17234 | | ret = WOLFSSL_FATAL_ERROR; |
17235 | | return ret; |
17236 | | } |
17237 | | |
17238 | | /* Read the any early data from the client. |
17239 | | * |
17240 | | * ssl The SSL/TLS object. |
17241 | | * data Buffer to put the early data into. |
17242 | | * sz The size of the buffer in bytes. |
17243 | | * outSz The number of early data bytes read. |
17244 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
17245 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
17246 | | * early data bytes read. |
17247 | | */ |
17248 | | int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz) |
17249 | | { |
17250 | | int ret = 0; |
17251 | | |
17252 | | WOLFSSL_ENTER("wolfSSL_read_early_data"); |
17253 | | |
17254 | | |
17255 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
17256 | | return BAD_FUNC_ARG; |
17257 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
17258 | | return BAD_FUNC_ARG; |
17259 | | |
17260 | | *outSz = 0; |
17261 | | #ifndef NO_WOLFSSL_SERVER |
17262 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
17263 | | return SIDE_ERROR; |
17264 | | |
17265 | | if (ssl->options.handShakeState == NULL_STATE) { |
17266 | | /* the server flight can return WANT_WRITE and we re-enter here after |
17267 | | * setting ssl->earlyData = process_early_data, set earlyData to |
17268 | | * expecting_early_data just once */ |
17269 | | if (ssl->earlyData < expecting_early_data) |
17270 | | ssl->earlyData = expecting_early_data; |
17271 | | /* this used to be: ret = wolfSSL_accept_TLSv13(ssl); |
17272 | | * However, wolfSSL_accept_TLSv13() expects a certificate to |
17273 | | * be installed already, which is not the case in servers |
17274 | | * such as HAProxy. They do it after inspecting the ClientHello. |
17275 | | * The common wolfssl_accept() allows that. */ |
17276 | | ret = wolfSSL_accept(ssl); |
17277 | | if (ret <= 0) |
17278 | | return WOLFSSL_FATAL_ERROR; |
17279 | | } |
17280 | | if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) { |
17281 | | ssl->options.clientInEarlyData = 1; |
17282 | | ret = ReceiveData(ssl, (byte*)data, (size_t)sz, FALSE); |
17283 | | ssl->options.clientInEarlyData = 0; |
17284 | | if (ret > 0) |
17285 | | *outSz = ret; |
17286 | | if (ssl->error == WC_NO_ERR_TRACE(APP_DATA_READY)) { |
17287 | | ret = 0; |
17288 | | ssl->error = WOLFSSL_ERROR_NONE; |
17289 | | #ifdef WOLFSSL_DTLS13 |
17290 | | if (ssl->options.dtls) { |
17291 | | ret = Dtls13DoScheduledWork(ssl); |
17292 | | if (ret < 0) { |
17293 | | ssl->error = ret; |
17294 | | WOLFSSL_ERROR(ssl->error); |
17295 | | return WOLFSSL_FATAL_ERROR; |
17296 | | } |
17297 | | } |
17298 | | #endif /* WOLFSSL_DTLS13 */ |
17299 | | } |
17300 | | } |
17301 | | #ifdef WOLFSSL_DTLS13 |
17302 | | else if (ssl->buffers.outputBuffer.length > 0 && |
17303 | | ssl->options.dtls && ssl->dtls13SendingAckOrRtx) { |
17304 | | ret = SendBuffered(ssl); |
17305 | | if (ret == 0) { |
17306 | | ssl->dtls13SendingAckOrRtx = 0; |
17307 | | } |
17308 | | else { |
17309 | | ssl->error = ret; |
17310 | | WOLFSSL_ERROR(ssl->error); |
17311 | | return WOLFSSL_FATAL_ERROR; |
17312 | | } |
17313 | | } |
17314 | | #endif /* WOLFSSL_DTLS13 */ |
17315 | | else |
17316 | | ret = 0; |
17317 | | #else |
17318 | | return SIDE_ERROR; |
17319 | | #endif |
17320 | | |
17321 | | WOLFSSL_LEAVE("wolfSSL_read_early_data", ret); |
17322 | | |
17323 | | if (ret < 0) |
17324 | | ret = WOLFSSL_FATAL_ERROR; |
17325 | | return ret; |
17326 | | } |
17327 | | |
17328 | | /* Returns early data status |
17329 | | * |
17330 | | * ssl The SSL/TLS object. |
17331 | | * returns WOLFSSL_EARLY_DATA_ACCEPTED if the data was accepted |
17332 | | * WOLFSSL_EARLY_DATA_REJECTED if the data was rejected |
17333 | | * WOLFSSL_EARLY_DATA_NOT_SENT if no early data was sent |
17334 | | */ |
17335 | | int wolfSSL_get_early_data_status(const WOLFSSL* ssl) |
17336 | | { |
17337 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
17338 | | return BAD_FUNC_ARG; |
17339 | | |
17340 | | return ssl->earlyDataStatus; |
17341 | | } |
17342 | | #endif |
17343 | | |
17344 | | #ifdef HAVE_SECRET_CALLBACK |
17345 | | int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx) |
17346 | | { |
17347 | | WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb"); |
17348 | | if (ssl == NULL) |
17349 | | return WOLFSSL_FATAL_ERROR; |
17350 | | |
17351 | | ssl->tls13SecretCb = cb; |
17352 | | ssl->tls13SecretCtx = ctx; |
17353 | | |
17354 | | return WOLFSSL_SUCCESS; |
17355 | | } |
17356 | | |
17357 | | #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE) |
17358 | | int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret, |
17359 | | int secretSz, void* ctx) |
17360 | | { |
17361 | | int i; |
17362 | | const char* str = NULL; |
17363 | | byte clientRandom[RAN_LEN]; |
17364 | | int clientRandomSz; |
17365 | | XFILE fp; |
17366 | | #if defined(WOLFSSL_SSLKEYLOGFILE_OUTPUT) && defined(WOLFSSL_SSLKEYLOGFILE_USE_ENV) |
17367 | | const char* keyLogFile; |
17368 | | #endif |
17369 | | |
17370 | | (void) ctx; |
17371 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
17372 | | #ifdef WOLFSSL_SSLKEYLOGFILE_USE_ENV |
17373 | | /* RFC 9850: prefer the SSLKEYLOGFILE environment variable so tools such as |
17374 | | * curl and Wireshark can share the path, else use the compile-time path. |
17375 | | * XGETENV resolves to NULL where environment access is unavailable. Opt-in |
17376 | | * so a build with the variable exported for other applications is not |
17377 | | * affected. */ |
17378 | | keyLogFile = XGETENV("SSLKEYLOGFILE"); |
17379 | | if (keyLogFile == NULL || keyLogFile[0] == '\0') |
17380 | | keyLogFile = WOLFSSL_SSLKEYLOGFILE_OUTPUT; |
17381 | | fp = XFOPEN(keyLogFile, "ab"); |
17382 | | #else |
17383 | | fp = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "ab"); |
17384 | | #endif |
17385 | | if (fp == XBADFILE) { |
17386 | | return BAD_FUNC_ARG; |
17387 | | } |
17388 | | #else |
17389 | | fp = stderr; |
17390 | | #endif |
17391 | | |
17392 | | clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom, |
17393 | | sizeof(clientRandom)); |
17394 | | |
17395 | | if (clientRandomSz <= 0) { |
17396 | | printf("Error getting server random %d\n", clientRandomSz); |
17397 | | return BAD_FUNC_ARG; |
17398 | | } |
17399 | | |
17400 | | #if 0 |
17401 | | printf("TLS Server Secret CB: Rand %d, Secret %d\n", |
17402 | | serverRandomSz, secretSz); |
17403 | | #endif |
17404 | | |
17405 | | switch (id) { |
17406 | | case CLIENT_EARLY_TRAFFIC_SECRET: |
17407 | | str = "CLIENT_EARLY_TRAFFIC_SECRET"; break; |
17408 | | case EARLY_EXPORTER_SECRET: |
17409 | | str = "EARLY_EXPORTER_SECRET"; break; |
17410 | | case CLIENT_HANDSHAKE_TRAFFIC_SECRET: |
17411 | | str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break; |
17412 | | case SERVER_HANDSHAKE_TRAFFIC_SECRET: |
17413 | | str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break; |
17414 | | case CLIENT_TRAFFIC_SECRET: |
17415 | | str = "CLIENT_TRAFFIC_SECRET_0"; break; |
17416 | | case SERVER_TRAFFIC_SECRET: |
17417 | | str = "SERVER_TRAFFIC_SECRET_0"; break; |
17418 | | case EXPORTER_SECRET: |
17419 | | str = "EXPORTER_SECRET"; break; |
17420 | | #ifdef HAVE_ECH |
17421 | | case ECH_SECRET: |
17422 | | str = "ECH_SECRET"; break; |
17423 | | case ECH_CONFIG: |
17424 | | str = "ECH_CONFIG"; break; |
17425 | | #endif |
17426 | | default: |
17427 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
17428 | | XFCLOSE(fp); |
17429 | | #endif |
17430 | | return BAD_FUNC_ARG; |
17431 | | break; |
17432 | | } |
17433 | | |
17434 | | fprintf(fp, "%s ", str); |
17435 | | for (i = 0; i < (int)clientRandomSz; i++) { |
17436 | | fprintf(fp, "%02x", clientRandom[i]); |
17437 | | } |
17438 | | fprintf(fp, " "); |
17439 | | for (i = 0; i < secretSz; i++) { |
17440 | | fprintf(fp, "%02x", secret[i]); |
17441 | | } |
17442 | | fprintf(fp, "\n"); |
17443 | | |
17444 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
17445 | | XFCLOSE(fp); |
17446 | | #endif |
17447 | | |
17448 | | return 0; |
17449 | | } |
17450 | | #endif |
17451 | | #endif |
17452 | | |
17453 | | #undef ERROR_OUT |
17454 | | |
17455 | | #endif /* !WOLFCRYPT_ONLY */ |
17456 | | |
17457 | | #endif /* !NO_TLS && WOLFSSL_TLS13 */ |