Line | Count | Source |
1 | | /* tls13.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
23 | | |
24 | | /* |
25 | | * TLS 1.3-Specific Build Options: |
26 | | * (See tls.c for generic TLS options: extensions, curves, callbacks, etc.) |
27 | | * |
28 | | * Protocol: |
29 | | * WOLFSSL_TLS13: Enable TLS 1.3 protocol default: on |
30 | | * WOLFSSL_TLS13_DRAFT: Enable TLS 1.3 draft version support default: off |
31 | | * WOLFSSL_QUIC: Enable QUIC protocol support (TLS 1.3) default: off |
32 | | * WOLFSSL_DTLS13_NO_HRR_ON_RESUME: Skip HRR on DTLS 1.3 resume default: off |
33 | | * WOLFSSL_DTLS_CH_FRAG: Enable DTLS 1.3 ClientHello frag default: off |
34 | | * |
35 | | * Handshake: |
36 | | * WOLFSSL_TLS13_MIDDLEBOX_COMPAT: Enable middlebox compatibility default: on |
37 | | * Sends ChangeCipherSpec and includes session id |
38 | | * WOLFSSL_SEND_HRR_COOKIE: Send cookie in HelloRetryRequest default: off |
39 | | * for stateless ClientHello tracking |
40 | | * WOLFSSL_EARLY_DATA: Allow 0-RTT early data default: off |
41 | | * WOLFSSL_EARLY_DATA_GROUP: Group early data with ClientHello default: off |
42 | | * WOLFSSL_POST_HANDSHAKE_AUTH: Post-handshake client auth default: off |
43 | | * WOLFSSL_TLS13_TICKET_BEFORE_FINISHED: Send NewSessionTicket default: off |
44 | | * before client Finished message |
45 | | * WOLFSSL_NO_CLIENT_AUTH: Disable TLS 1.3 client authentication default: off |
46 | | * WOLFSSL_NO_CLIENT_CERT_ERROR: Require client certificate default: off |
47 | | * WOLFSSL_CERT_SETUP_CB: Certificate setup callback default: off |
48 | | * WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION: Allow bad legacy version default: off |
49 | | * |
50 | | * Security: |
51 | | * WOLFSSL_BLIND_PRIVATE_KEY: Blind private key during signing default: off |
52 | | * WOLFSSL_CHECK_SIG_FAULTS: Verify signature after ECC signing default: off |
53 | | * to detect fault injection attacks |
54 | | * WOLFSSL_CIPHER_TEXT_CHECK: Verify ciphertext integrity default: off |
55 | | * |
56 | | * TLS 1.3 PSK: |
57 | | * WOLFSSL_PSK_ONE_ID: Single PSK identity per connect default: off |
58 | | * WOLFSSL_PSK_MULTI_ID_PER_CS: Multiple PSK IDs per cipher suite default: off |
59 | | * WOLFSSL_PRIORITIZE_PSK: Prioritize PSK over ciphersuite order default: off |
60 | | * WOLFSSL_PSK_ID_PROTECTION: Enable PSK identity protection default: off |
61 | | * |
62 | | * TLS 1.3 Session Tickets: |
63 | | * WOLFSSL_TICKET_HAVE_ID: Session tickets include ID default: off |
64 | | * Forced on when WOLFSSL_EARLY_DATA is set. |
65 | | * WOLFSSL_TICKET_NONCE_MALLOC: Dynamically allocate ticket nonce default: off |
66 | | * |
67 | | * TLS 1.3 Key Exchange: |
68 | | * HAVE_KEYING_MATERIAL: Export keying material (RFC 8446 7.5) default: off |
69 | | * WOLFSSL_HAVE_TLS_UNIQUE: Enable tls-unique channel binding default: off |
70 | | * |
71 | | * TLS 1.3 Hash/Signature: |
72 | | * WOLFSSL_TLS13_SHA512: Allow SHA-512 in TLS 1.3 handshake default: off |
73 | | * (no ciphersuite requires it currently) |
74 | | * WOLFSSL_ERROR_CODE_OPENSSL: Use OpenSSL-compatible error codes default: off |
75 | | * WOLFSSL_SSLKEYLOGFILE_OUTPUT: Set key log output file path default: off |
76 | | * WOLFSSL_RW_THREADED: Enable read/write threading support default: off |
77 | | * WOLFSSL_ASYNC_IO: Enable async I/O operations default: off |
78 | | * WOLFSSL_NONBLOCK_OCSP: Non-blocking OCSP processing default: off |
79 | | * WOLFSSL_TLS_OCSP_MULTI: Multiple OCSP responses default: off |
80 | | * WOLFSSL_WOLFSENTRY_HOOKS: wolfSentry integration hooks default: off |
81 | | */ |
82 | | |
83 | | #if !defined(NO_TLS) && defined(WOLFSSL_TLS13) |
84 | | |
85 | | /* 0-RTT anti-replay eviction needs the session cache. */ |
86 | | #if defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET) && \ |
87 | | defined(NO_SESSION_CACHE) && !defined(NO_WOLFSSL_SERVER) && \ |
88 | | !defined(WOLFSSL_EARLY_DATA_NO_ANTI_REPLAY) |
89 | | #error "WOLFSSL_EARLY_DATA with tickets requires !NO_SESSION_CACHE, or " \ |
90 | | "define WOLFSSL_EARLY_DATA_NO_ANTI_REPLAY to opt out." |
91 | | #endif |
92 | | |
93 | | #ifndef WOLFCRYPT_ONLY |
94 | | |
95 | | #ifdef HAVE_ERRNO_H |
96 | | #include <errno.h> |
97 | | #endif |
98 | | |
99 | | #if defined(__MACH__) || defined(__FreeBSD__) || \ |
100 | | defined(__INCLUDE_NUTTX_CONFIG_H) || defined(WOLFSSL_RIOT_OS) |
101 | | #include <sys/time.h> |
102 | | #endif /* __MACH__ || __FreeBSD__ || |
103 | | __INCLUDE_NUTTX_CONFIG_H || WOLFSSL_RIOT_OS */ |
104 | | |
105 | | |
106 | | #include <wolfssl/internal.h> |
107 | | #include <wolfssl/error-ssl.h> |
108 | | #include <wolfssl/wolfcrypt/asn.h> |
109 | | #include <wolfssl/wolfcrypt/dh.h> |
110 | | #include <wolfssl/wolfcrypt/kdf.h> |
111 | | #include <wolfssl/wolfcrypt/signature.h> |
112 | | #ifdef NO_INLINE |
113 | | #include <wolfssl/wolfcrypt/misc.h> |
114 | | #else |
115 | | #define WOLFSSL_MISC_INCLUDED |
116 | | #include <wolfcrypt/src/misc.c> |
117 | | #endif |
118 | | |
119 | | #ifdef __sun |
120 | | #include <sys/filio.h> |
121 | | #endif |
122 | | |
123 | | #ifndef TRUE |
124 | | #define TRUE 1 |
125 | | #endif |
126 | | #ifndef FALSE |
127 | | #define FALSE 0 |
128 | | #endif |
129 | | |
130 | | #ifndef HAVE_AEAD |
131 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
132 | | #error "The build option HAVE_AEAD is required for TLS 1.3" |
133 | | #else |
134 | | #pragma \ |
135 | | message("error: The build option HAVE_AEAD is required for TLS 1.3") |
136 | | #endif |
137 | | #endif |
138 | | |
139 | | #ifndef HAVE_HKDF |
140 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
141 | | #error "The build option HAVE_HKDF is required for TLS 1.3" |
142 | | #else |
143 | | #pragma message("error: The build option HAVE_HKDF is required for TLS 1.3") |
144 | | #endif |
145 | | #endif |
146 | | |
147 | | #ifndef HAVE_TLS_EXTENSIONS |
148 | | #if !defined(_MSC_VER) && !defined(__TASKING__) |
149 | | #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3" |
150 | | #else |
151 | | #pragma message("error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3") |
152 | | #endif |
153 | | #endif |
154 | | |
155 | | |
156 | | /* Set ret to error value and jump to label. |
157 | | * |
158 | | * err The error value to set. |
159 | | * eLabel The label to jump to. |
160 | | */ |
161 | 0 | #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } |
162 | | |
163 | | /* Size of the TLS v1.3 label use when deriving keys. */ |
164 | 0 | #define TLS13_PROTOCOL_LABEL_SZ 6 |
165 | | /* The protocol label for TLS v1.3. */ |
166 | | static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 "; |
167 | | |
168 | | #ifdef WOLFSSL_DTLS13 |
169 | | #define DTLS13_PROTOCOL_LABEL_SZ 6 |
170 | | static const byte dtls13ProtocolLabel[DTLS13_PROTOCOL_LABEL_SZ + 1] = "dtls13"; |
171 | | #endif /* WOLFSSL_DTLS13 */ |
172 | | |
173 | | #if defined(HAVE_ECH) |
174 | | #define ECH_ACCEPT_CONFIRMATION_LABEL_SZ 23 |
175 | | #define ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ 27 |
176 | | static const byte |
177 | | echAcceptConfirmationLabel[ECH_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
178 | | "ech accept confirmation"; |
179 | | static const byte |
180 | | echHrrAcceptConfirmationLabel[ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ + 1] = |
181 | | "hrr ech accept confirmation"; |
182 | | #endif |
183 | | |
184 | | #ifndef NO_CERTS |
185 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
186 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
187 | | |
188 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash); |
189 | | |
190 | | #endif |
191 | | #endif |
192 | | |
193 | | /* Expand data using HMAC, salt and label and info. |
194 | | * TLS v1.3 defines this function. Use callback if available. */ |
195 | | static int Tls13HKDFExpandLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
196 | | const byte* prk, word32 prkLen, |
197 | | const byte* protocol, word32 protocolLen, |
198 | | const byte* label, word32 labelLen, |
199 | | const byte* info, word32 infoLen, |
200 | | int digest) |
201 | 0 | { |
202 | 0 | int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
203 | |
|
204 | | #if defined(HAVE_PK_CALLBACKS) |
205 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
206 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
207 | | protocol, protocolLen, |
208 | | label, labelLen, |
209 | | info, infoLen, digest, |
210 | | WOLFSSL_CLIENT_END /* ignored */); |
211 | | } |
212 | | |
213 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
214 | | return ret; |
215 | | #endif |
216 | 0 | (void)ssl; |
217 | 0 | PRIVATE_KEY_UNLOCK(); |
218 | 0 | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
219 | 0 | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
220 | 0 | protocol, protocolLen, |
221 | 0 | label, labelLen, |
222 | 0 | info, infoLen, digest, |
223 | 0 | ssl->heap, ssl->devId); |
224 | | #else |
225 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
226 | | protocol, protocolLen, |
227 | | label, labelLen, |
228 | | info, infoLen, digest); |
229 | | #endif |
230 | 0 | PRIVATE_KEY_LOCK(); |
231 | 0 | return ret; |
232 | 0 | } |
233 | | |
234 | | /* Same as above, but pass in the side we are expanding for: |
235 | | * side: either WOLFSSL_CLIENT_END or WOLFSSL_SERVER_END. |
236 | | */ |
237 | | static int Tls13HKDFExpandKeyLabel(WOLFSSL* ssl, byte* okm, word32 okmLen, |
238 | | const byte* prk, word32 prkLen, |
239 | | const byte* protocol, word32 protocolLen, |
240 | | const byte* label, word32 labelLen, |
241 | | const byte* info, word32 infoLen, |
242 | | int digest, int side) |
243 | 0 | { |
244 | 0 | int ret; |
245 | | #if defined(HAVE_PK_CALLBACKS) |
246 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
247 | | if (ssl->ctx && ssl->ctx->HKDFExpandLabelCb) { |
248 | | ret = ssl->ctx->HKDFExpandLabelCb(okm, okmLen, prk, prkLen, |
249 | | protocol, protocolLen, |
250 | | label, labelLen, |
251 | | info, infoLen, |
252 | | digest, side); |
253 | | } |
254 | | if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
255 | | return ret; |
256 | | #endif |
257 | |
|
258 | 0 | #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
259 | 0 | ret = wc_Tls13_HKDF_Expand_Label_ex(okm, okmLen, prk, prkLen, |
260 | 0 | protocol, protocolLen, |
261 | 0 | label, labelLen, |
262 | 0 | info, infoLen, digest, |
263 | 0 | ssl->heap, ssl->devId); |
264 | |
|
265 | | #elif defined(HAVE_FIPS) && defined(wc_Tls13_HKDF_Expand_Label) |
266 | | ret = wc_Tls13_HKDF_Expand_Label_fips(okm, okmLen, prk, prkLen, |
267 | | protocol, protocolLen, |
268 | | label, labelLen, |
269 | | info, infoLen, digest); |
270 | | #else |
271 | | ret = wc_Tls13_HKDF_Expand_Label(okm, okmLen, prk, prkLen, |
272 | | protocol, protocolLen, |
273 | | label, labelLen, |
274 | | info, infoLen, digest); |
275 | | #endif |
276 | 0 | (void)ssl; |
277 | 0 | (void)side; |
278 | 0 | return ret; |
279 | 0 | } |
280 | | |
281 | | |
282 | | /* Derive a key from a message. |
283 | | * |
284 | | * ssl The SSL/TLS object. |
285 | | * output The buffer to hold the derived key. |
286 | | * outputLen The length of the derived key. |
287 | | * secret The secret used to derive the key (HMAC secret). |
288 | | * label The label used to distinguish the context. |
289 | | * labelLen The length of the label. |
290 | | * msg The message data to derive key from. |
291 | | * msgLen The length of the message data to derive key from. |
292 | | * hashAlgo The hash algorithm to use in the HMAC. |
293 | | * returns 0 on success, otherwise failure. |
294 | | */ |
295 | | static int DeriveKeyMsg(WOLFSSL* ssl, byte* output, int outputLen, |
296 | | const byte* secret, const byte* label, word32 labelLen, |
297 | | byte* msg, int msgLen, int hashAlgo) |
298 | 0 | { |
299 | 0 | byte hash[WC_MAX_DIGEST_SIZE]; |
300 | 0 | Digest digest; |
301 | 0 | word32 hashSz = 0; |
302 | 0 | const byte* protocol; |
303 | 0 | word32 protocolLen; |
304 | 0 | int digestAlg = -1; |
305 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
306 | |
|
307 | 0 | switch (hashAlgo) { |
308 | 0 | #ifndef NO_SHA256 |
309 | 0 | case sha256_mac: |
310 | 0 | ret = wc_InitSha256_ex(&digest.sha256, ssl->heap, ssl->devId); |
311 | 0 | if (ret == 0) { |
312 | 0 | ret = wc_Sha256Update(&digest.sha256, msg, (word32)msgLen); |
313 | 0 | if (ret == 0) |
314 | 0 | ret = wc_Sha256Final(&digest.sha256, hash); |
315 | 0 | wc_Sha256Free(&digest.sha256); |
316 | 0 | } |
317 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
318 | 0 | digestAlg = WC_SHA256; |
319 | 0 | break; |
320 | 0 | #endif |
321 | 0 | #ifdef WOLFSSL_SHA384 |
322 | 0 | case sha384_mac: |
323 | 0 | ret = wc_InitSha384_ex(&digest.sha384, ssl->heap, ssl->devId); |
324 | 0 | if (ret == 0) { |
325 | 0 | ret = wc_Sha384Update(&digest.sha384, msg, (word32)msgLen); |
326 | 0 | if (ret == 0) |
327 | 0 | ret = wc_Sha384Final(&digest.sha384, hash); |
328 | 0 | wc_Sha384Free(&digest.sha384); |
329 | 0 | } |
330 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
331 | 0 | digestAlg = WC_SHA384; |
332 | 0 | break; |
333 | 0 | #endif |
334 | | #ifdef WOLFSSL_TLS13_SHA512 |
335 | | case sha512_mac: |
336 | | ret = wc_InitSha512_ex(&digest.sha512, ssl->heap, ssl->devId); |
337 | | if (ret == 0) { |
338 | | ret = wc_Sha512Update(&digest.sha512, msg, (word32)msgLen); |
339 | | if (ret == 0) |
340 | | ret = wc_Sha512Final(&digest.sha512, hash); |
341 | | wc_Sha512Free(&digest.sha512); |
342 | | } |
343 | | hashSz = WC_SHA512_DIGEST_SIZE; |
344 | | digestAlg = WC_SHA512; |
345 | | break; |
346 | | #endif |
347 | | #ifdef WOLFSSL_SM3 |
348 | | case sm3_mac: |
349 | | ret = wc_InitSm3(&digest.sm3, ssl->heap, ssl->devId); |
350 | | if (ret == 0) { |
351 | | ret = wc_Sm3Update(&digest.sm3, msg, (word32)msgLen); |
352 | | if (ret == 0) |
353 | | ret = wc_Sm3Final(&digest.sm3, hash); |
354 | | wc_Sm3Free(&digest.sm3); |
355 | | } |
356 | | hashSz = WC_SM3_DIGEST_SIZE; |
357 | | digestAlg = WC_SM3; |
358 | | break; |
359 | | #endif |
360 | 0 | default: |
361 | 0 | ret = BAD_FUNC_ARG; |
362 | 0 | digestAlg = -1; |
363 | 0 | break; |
364 | 0 | } |
365 | | |
366 | 0 | if (digestAlg < 0) |
367 | 0 | return HASH_TYPE_E; |
368 | | |
369 | 0 | if (ret != 0) |
370 | 0 | return ret; |
371 | | |
372 | 0 | switch (ssl->version.minor) { |
373 | 0 | case TLSv1_3_MINOR: |
374 | 0 | protocol = tls13ProtocolLabel; |
375 | 0 | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
376 | 0 | break; |
377 | | #ifdef WOLFSSL_DTLS13 |
378 | | case DTLSv1_3_MINOR: |
379 | | if (!ssl->options.dtls) |
380 | | return VERSION_ERROR; |
381 | | |
382 | | protocol = dtls13ProtocolLabel; |
383 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
384 | | break; |
385 | | #endif /* WOLFSSL_DTLS13 */ |
386 | 0 | default: |
387 | 0 | return VERSION_ERROR; |
388 | 0 | } |
389 | 0 | if (outputLen == -1) |
390 | 0 | outputLen = (int)hashSz; |
391 | |
|
392 | 0 | ret = Tls13HKDFExpandLabel(ssl, output, (word32)outputLen, secret, hashSz, |
393 | 0 | protocol, protocolLen, label, labelLen, |
394 | 0 | hash, hashSz, digestAlg); |
395 | 0 | return ret; |
396 | 0 | } |
397 | | |
398 | | /* Derive a key. |
399 | | * |
400 | | * ssl The SSL/TLS object. |
401 | | * output The buffer to hold the derived key. |
402 | | * outputLen The length of the derived key. |
403 | | * secret The secret used to derive the key (HMAC secret). |
404 | | * label The label used to distinguish the context. |
405 | | * labelLen The length of the label. |
406 | | * hashAlgo The hash algorithm to use in the HMAC. |
407 | | * includeMsgs Whether to include a hash of the handshake messages so far. |
408 | | * side The side that we are deriving the secret for. |
409 | | * returns 0 on success, otherwise failure. |
410 | | */ |
411 | | int Tls13DeriveKey(WOLFSSL* ssl, byte* output, int outputLen, |
412 | | const byte* secret, const byte* label, word32 labelLen, |
413 | | int hashAlgo, int includeMsgs, int side) |
414 | 0 | { |
415 | 0 | int ret = 0; |
416 | 0 | byte hash[WC_MAX_DIGEST_SIZE]; |
417 | 0 | word32 hashSz = 0; |
418 | 0 | word32 hashOutSz = 0; |
419 | 0 | const byte* protocol; |
420 | 0 | word32 protocolLen; |
421 | 0 | int digestAlg = 0; |
422 | | |
423 | |
|
424 | 0 | switch (hashAlgo) { |
425 | 0 | #ifndef NO_SHA256 |
426 | 0 | case sha256_mac: |
427 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
428 | 0 | digestAlg = WC_SHA256; |
429 | 0 | if (includeMsgs) |
430 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
431 | 0 | break; |
432 | 0 | #endif |
433 | | |
434 | 0 | #ifdef WOLFSSL_SHA384 |
435 | 0 | case sha384_mac: |
436 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
437 | 0 | digestAlg = WC_SHA384; |
438 | 0 | if (includeMsgs) |
439 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
440 | 0 | break; |
441 | 0 | #endif |
442 | | |
443 | | #ifdef WOLFSSL_TLS13_SHA512 |
444 | | case sha512_mac: |
445 | | hashSz = WC_SHA512_DIGEST_SIZE; |
446 | | digestAlg = WC_SHA512; |
447 | | if (includeMsgs) |
448 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
449 | | break; |
450 | | #endif |
451 | | |
452 | | #ifdef WOLFSSL_SM3 |
453 | | case sm3_mac: |
454 | | hashSz = WC_SM3_DIGEST_SIZE; |
455 | | digestAlg = WC_SM3; |
456 | | if (includeMsgs) |
457 | | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
458 | | break; |
459 | | #endif |
460 | | |
461 | 0 | default: |
462 | 0 | ret = HASH_TYPE_E; |
463 | 0 | break; |
464 | 0 | } |
465 | 0 | if (ret != 0) |
466 | 0 | return ret; |
467 | | |
468 | 0 | protocol = tls13ProtocolLabel; |
469 | 0 | protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
470 | |
|
471 | | #ifdef WOLFSSL_DTLS13 |
472 | | if (ssl->options.dtls) { |
473 | | protocol = dtls13ProtocolLabel; |
474 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
475 | | } |
476 | | #endif /* WOLFSSL_DTLS13 */ |
477 | |
|
478 | 0 | if (outputLen == -1) { |
479 | 0 | outputLen = (int)hashSz; |
480 | 0 | } |
481 | 0 | if (includeMsgs) { |
482 | 0 | hashOutSz = hashSz; |
483 | 0 | } |
484 | 0 | else { |
485 | | /* Appease static analyzers by making sure hash is cleared, since it is |
486 | | * passed into expand key label where older wc_Tls13_HKDF_Expand_Label |
487 | | * will unconditionally try to call a memcpy on it, however length will |
488 | | * always be 0. */ |
489 | 0 | XMEMSET(hash, 0, sizeof(hash)); |
490 | 0 | hashOutSz = 0; |
491 | 0 | } |
492 | |
|
493 | 0 | PRIVATE_KEY_UNLOCK(); |
494 | 0 | ret = Tls13HKDFExpandKeyLabel(ssl, output, (word32)outputLen, secret, hashSz, |
495 | 0 | protocol, protocolLen, label, labelLen, |
496 | 0 | hash, hashOutSz, digestAlg, side); |
497 | 0 | PRIVATE_KEY_LOCK(); |
498 | |
|
499 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
500 | | wc_MemZero_Add("TLS 1.3 derived key", output, outputLen); |
501 | | #endif |
502 | 0 | return ret; |
503 | 0 | } |
504 | | |
505 | | /* Convert TLS mac ID to a hash algorithm ID |
506 | | * |
507 | | * mac Mac ID to convert |
508 | | * returns hash ID on success, or the NONE type. |
509 | | */ |
510 | | static WC_INLINE int mac2hash(int mac) |
511 | 0 | { |
512 | 0 | int hash; |
513 | 0 | switch (mac) { |
514 | 0 | #ifndef NO_SHA256 |
515 | 0 | case sha256_mac: |
516 | 0 | hash = WC_SHA256; |
517 | 0 | break; |
518 | 0 | #endif |
519 | | |
520 | 0 | #ifdef WOLFSSL_SHA384 |
521 | 0 | case sha384_mac: |
522 | 0 | hash = WC_SHA384; |
523 | 0 | break; |
524 | 0 | #endif |
525 | | |
526 | | #ifdef WOLFSSL_TLS13_SHA512 |
527 | | case sha512_mac: |
528 | | hash = WC_SHA512; |
529 | | break; |
530 | | #endif |
531 | | |
532 | | #ifdef WOLFSSL_SM3 |
533 | | case sm3_mac: |
534 | | hash = WC_SM3; |
535 | | break; |
536 | | #endif |
537 | | |
538 | 0 | default: |
539 | 0 | hash = WC_HASH_TYPE_NONE; |
540 | 0 | } |
541 | 0 | return hash; |
542 | 0 | } |
543 | | |
544 | | #ifndef NO_PSK |
545 | | /* The length of the binder key label. */ |
546 | | #define BINDER_KEY_LABEL_SZ 10 |
547 | | /* The binder key label. */ |
548 | | static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] = |
549 | | "ext binder"; |
550 | | |
551 | | /* Derive the binder key. |
552 | | * |
553 | | * ssl The SSL/TLS object. |
554 | | * key The derived key. |
555 | | * returns 0 on success, otherwise failure. |
556 | | */ |
557 | | static int DeriveBinderKey(WOLFSSL* ssl, byte* key) |
558 | | { |
559 | | WOLFSSL_MSG("Derive Binder Key"); |
560 | | if (ssl == NULL || ssl->arrays == NULL) { |
561 | | return BAD_FUNC_ARG; |
562 | | } |
563 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
564 | | binderKeyLabel, BINDER_KEY_LABEL_SZ, |
565 | | NULL, 0, ssl->specs.mac_algorithm); |
566 | | } |
567 | | #endif /* !NO_PSK */ |
568 | | |
569 | | #if defined(HAVE_SESSION_TICKET) && \ |
570 | | (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) |
571 | | /* The length of the binder key resume label. */ |
572 | | #define BINDER_KEY_RESUME_LABEL_SZ 10 |
573 | | /* The binder key resume label. */ |
574 | | static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] = |
575 | | "res binder"; |
576 | | |
577 | | /* Derive the binder resumption key. |
578 | | * |
579 | | * ssl The SSL/TLS object. |
580 | | * key The derived key. |
581 | | * returns 0 on success, otherwise failure. |
582 | | */ |
583 | | static int DeriveBinderKeyResume(WOLFSSL* ssl, byte* key) |
584 | | { |
585 | | WOLFSSL_MSG("Derive Binder Key - Resumption"); |
586 | | if (ssl == NULL || ssl->arrays == NULL) { |
587 | | return BAD_FUNC_ARG; |
588 | | } |
589 | | return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
590 | | binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ, |
591 | | NULL, 0, ssl->specs.mac_algorithm); |
592 | | } |
593 | | #endif /* HAVE_SESSION_TICKET && (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) */ |
594 | | |
595 | | #ifdef WOLFSSL_EARLY_DATA |
596 | | |
597 | | /* The length of the early traffic label. */ |
598 | | #define EARLY_TRAFFIC_LABEL_SZ 11 |
599 | | /* The early traffic label. */ |
600 | | static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] = |
601 | | "c e traffic"; |
602 | | |
603 | | /* Derive the early traffic key. |
604 | | * |
605 | | * ssl The SSL/TLS object. |
606 | | * key The derived key. |
607 | | * side The side that we are deriving the secret for. |
608 | | * returns 0 on success, otherwise failure. |
609 | | */ |
610 | | static int DeriveEarlyTrafficSecret(WOLFSSL* ssl, byte* key, int side) |
611 | | { |
612 | | int ret; |
613 | | WOLFSSL_MSG("Derive Early Traffic Secret"); |
614 | | if (ssl == NULL || ssl->arrays == NULL) { |
615 | | return BAD_FUNC_ARG; |
616 | | } |
617 | | |
618 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
619 | | /* If this is called from a sniffer session with keylog file support, |
620 | | * obtain the appropriate secret from the callback */ |
621 | | if (ssl->snifferSecretCb != NULL) { |
622 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
623 | | SNIFFER_SECRET_CLIENT_EARLY_TRAFFIC_SECRET, |
624 | | key); |
625 | | } |
626 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
627 | | |
628 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->secret, |
629 | | earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ, |
630 | | ssl->specs.mac_algorithm, 1, side); |
631 | | #ifdef HAVE_SECRET_CALLBACK |
632 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
633 | | ret = ssl->tls13SecretCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
634 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
635 | | if (ret != 0) { |
636 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
637 | | return TLS13_SECRET_CB_E; |
638 | | } |
639 | | } |
640 | | #ifdef OPENSSL_EXTRA |
641 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
642 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key, |
643 | | ssl->specs.hash_size, NULL); |
644 | | if (ret != 0) { |
645 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
646 | | return TLS13_SECRET_CB_E; |
647 | | } |
648 | | } |
649 | | #endif /* OPENSSL_EXTRA */ |
650 | | #endif /* HAVE_SECRET_CALLBACK */ |
651 | | return ret; |
652 | | } |
653 | | |
654 | | #endif |
655 | | |
656 | | /* The length of the client handshake label. */ |
657 | 0 | #define CLIENT_HANDSHAKE_LABEL_SZ 12 |
658 | | /* The client handshake label. */ |
659 | | static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] = |
660 | | "c hs traffic"; |
661 | | |
662 | | /* Derive the client handshake key. |
663 | | * |
664 | | * ssl The SSL/TLS object. |
665 | | * key The derived key. |
666 | | * returns 0 on success, otherwise failure. |
667 | | */ |
668 | | static int DeriveClientHandshakeSecret(WOLFSSL* ssl, byte* key) |
669 | 0 | { |
670 | 0 | int ret; |
671 | 0 | WOLFSSL_MSG("Derive Client Handshake Secret"); |
672 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
673 | 0 | return BAD_FUNC_ARG; |
674 | 0 | } |
675 | | |
676 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
677 | | /* If this is called from a sniffer session with keylog file support, |
678 | | * obtain the appropriate secret from the callback */ |
679 | | if (ssl->snifferSecretCb != NULL) { |
680 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
681 | | SNIFFER_SECRET_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
682 | | key); |
683 | | } |
684 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
685 | | |
686 | 0 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
687 | 0 | clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ, |
688 | 0 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
689 | | #ifdef HAVE_SECRET_CALLBACK |
690 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
691 | | ret = ssl->tls13SecretCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
692 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
693 | | if (ret != 0) { |
694 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
695 | | return TLS13_SECRET_CB_E; |
696 | | } |
697 | | } |
698 | | #ifdef OPENSSL_EXTRA |
699 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
700 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key, |
701 | | ssl->specs.hash_size, NULL); |
702 | | if (ret != 0) { |
703 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
704 | | return TLS13_SECRET_CB_E; |
705 | | } |
706 | | } |
707 | | #endif /* OPENSSL_EXTRA */ |
708 | | #endif /* HAVE_SECRET_CALLBACK */ |
709 | 0 | return ret; |
710 | 0 | } |
711 | | |
712 | | /* The length of the server handshake label. */ |
713 | 0 | #define SERVER_HANDSHAKE_LABEL_SZ 12 |
714 | | /* The server handshake label. */ |
715 | | static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] = |
716 | | "s hs traffic"; |
717 | | |
718 | | /* Derive the server handshake key. |
719 | | * |
720 | | * ssl The SSL/TLS object. |
721 | | * key The derived key. |
722 | | * returns 0 on success, otherwise failure. |
723 | | */ |
724 | | static int DeriveServerHandshakeSecret(WOLFSSL* ssl, byte* key) |
725 | 0 | { |
726 | 0 | int ret; |
727 | 0 | WOLFSSL_MSG("Derive Server Handshake Secret"); |
728 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
729 | 0 | return BAD_FUNC_ARG; |
730 | 0 | } |
731 | | |
732 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
733 | | /* If this is called from a sniffer session with keylog file support, |
734 | | * obtain the appropriate secret from the callback */ |
735 | | if (ssl->snifferSecretCb != NULL) { |
736 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
737 | | SNIFFER_SECRET_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
738 | | key); |
739 | | } |
740 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
741 | | |
742 | 0 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret, |
743 | 0 | serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ, |
744 | 0 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
745 | |
|
746 | | #ifdef HAVE_SECRET_CALLBACK |
747 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
748 | | ret = ssl->tls13SecretCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
749 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
750 | | if (ret != 0) { |
751 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
752 | | return TLS13_SECRET_CB_E; |
753 | | } |
754 | | } |
755 | | #ifdef OPENSSL_EXTRA |
756 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
757 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key, |
758 | | ssl->specs.hash_size, NULL); |
759 | | if (ret != 0) { |
760 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
761 | | return TLS13_SECRET_CB_E; |
762 | | } |
763 | | } |
764 | | #endif /* OPENSSL_EXTRA */ |
765 | | #endif /* HAVE_SECRET_CALLBACK */ |
766 | 0 | return ret; |
767 | 0 | } |
768 | | |
769 | | /* The length of the client application traffic label. */ |
770 | 0 | #define CLIENT_APP_LABEL_SZ 12 |
771 | | /* The client application traffic label. */ |
772 | | static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] = |
773 | | "c ap traffic"; |
774 | | |
775 | | /* Derive the client application traffic key. |
776 | | * |
777 | | * ssl The SSL/TLS object. |
778 | | * key The derived key. |
779 | | * returns 0 on success, otherwise failure. |
780 | | */ |
781 | | static int DeriveClientTrafficSecret(WOLFSSL* ssl, byte* key) |
782 | 0 | { |
783 | 0 | int ret; |
784 | 0 | WOLFSSL_MSG("Derive Client Traffic Secret"); |
785 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
786 | 0 | return BAD_FUNC_ARG; |
787 | 0 | } |
788 | | |
789 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
790 | | /* If this is called from a sniffer session with keylog file support, |
791 | | * obtain the appropriate secret from the callback */ |
792 | | if (ssl->snifferSecretCb != NULL) { |
793 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
794 | | SNIFFER_SECRET_CLIENT_TRAFFIC_SECRET, |
795 | | key); |
796 | | } |
797 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
798 | | |
799 | 0 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
800 | 0 | clientAppLabel, CLIENT_APP_LABEL_SZ, |
801 | 0 | ssl->specs.mac_algorithm, 1, WOLFSSL_CLIENT_END); |
802 | |
|
803 | | #ifdef HAVE_SECRET_CALLBACK |
804 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
805 | | ret = ssl->tls13SecretCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
806 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
807 | | if (ret != 0) { |
808 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
809 | | return TLS13_SECRET_CB_E; |
810 | | } |
811 | | } |
812 | | #ifdef OPENSSL_EXTRA |
813 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
814 | | ret = ssl->tls13KeyLogCb(ssl, CLIENT_TRAFFIC_SECRET, key, |
815 | | ssl->specs.hash_size, NULL); |
816 | | if (ret != 0) { |
817 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
818 | | return TLS13_SECRET_CB_E; |
819 | | } |
820 | | } |
821 | | #endif /* OPENSSL_EXTRA */ |
822 | | #endif /* HAVE_SECRET_CALLBACK */ |
823 | 0 | return ret; |
824 | 0 | } |
825 | | |
826 | | /* The length of the server application traffic label. */ |
827 | 0 | #define SERVER_APP_LABEL_SZ 12 |
828 | | /* The server application traffic label. */ |
829 | | static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] = |
830 | | "s ap traffic"; |
831 | | |
832 | | /* Derive the server application traffic key. |
833 | | * |
834 | | * ssl The SSL/TLS object. |
835 | | * key The derived key. |
836 | | * returns 0 on success, otherwise failure. |
837 | | */ |
838 | | static int DeriveServerTrafficSecret(WOLFSSL* ssl, byte* key) |
839 | 0 | { |
840 | 0 | int ret; |
841 | 0 | WOLFSSL_MSG("Derive Server Traffic Secret"); |
842 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
843 | 0 | return BAD_FUNC_ARG; |
844 | 0 | } |
845 | | |
846 | | #if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE) |
847 | | /* If this is called from a sniffer session with keylog file support, |
848 | | * obtain the appropriate secret from the callback */ |
849 | | if (ssl->snifferSecretCb != NULL) { |
850 | | return ssl->snifferSecretCb(ssl->arrays->clientRandom, |
851 | | SNIFFER_SECRET_SERVER_TRAFFIC_SECRET, |
852 | | key); |
853 | | } |
854 | | #endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */ |
855 | | |
856 | 0 | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
857 | 0 | serverAppLabel, SERVER_APP_LABEL_SZ, |
858 | 0 | ssl->specs.mac_algorithm, 1, WOLFSSL_SERVER_END); |
859 | |
|
860 | | #ifdef HAVE_SECRET_CALLBACK |
861 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
862 | | ret = ssl->tls13SecretCb(ssl, SERVER_TRAFFIC_SECRET, key, |
863 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
864 | | if (ret != 0) { |
865 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
866 | | return TLS13_SECRET_CB_E; |
867 | | } |
868 | | } |
869 | | #ifdef OPENSSL_EXTRA |
870 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
871 | | ret = ssl->tls13KeyLogCb(ssl, SERVER_TRAFFIC_SECRET, key, |
872 | | ssl->specs.hash_size, NULL); |
873 | | if (ret != 0) { |
874 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
875 | | return TLS13_SECRET_CB_E; |
876 | | } |
877 | | } |
878 | | #endif /* OPENSSL_EXTRA */ |
879 | | #endif /* HAVE_SECRET_CALLBACK */ |
880 | 0 | return ret; |
881 | 0 | } |
882 | | |
883 | | #ifdef HAVE_KEYING_MATERIAL |
884 | | /* The length of the exporter master secret label. */ |
885 | | #define EXPORTER_MASTER_LABEL_SZ 10 |
886 | | /* The exporter master secret label. */ |
887 | | static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] = |
888 | | "exp master"; |
889 | | |
890 | | /* Derive the exporter secret. |
891 | | * |
892 | | * ssl The SSL/TLS object. |
893 | | * key The derived key. |
894 | | * returns 0 on success, otherwise failure. |
895 | | */ |
896 | | static int DeriveExporterSecret(WOLFSSL* ssl, byte* key) |
897 | | { |
898 | | int ret; |
899 | | WOLFSSL_ENTER("Derive Exporter Secret"); |
900 | | if (ssl == NULL || ssl->arrays == NULL) { |
901 | | return BAD_FUNC_ARG; |
902 | | } |
903 | | ret = Tls13DeriveKey(ssl, key, -1, ssl->arrays->masterSecret, |
904 | | exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ, |
905 | | ssl->specs.mac_algorithm, 1, 0 /* Unused */); |
906 | | #ifdef HAVE_SECRET_CALLBACK |
907 | | if (ret == 0 && ssl->tls13SecretCb != NULL) { |
908 | | ret = ssl->tls13SecretCb(ssl, EXPORTER_SECRET, key, |
909 | | ssl->specs.hash_size, ssl->tls13SecretCtx); |
910 | | if (ret != 0) { |
911 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
912 | | return TLS13_SECRET_CB_E; |
913 | | } |
914 | | } |
915 | | #ifdef OPENSSL_EXTRA |
916 | | if (ret == 0 && ssl->tls13KeyLogCb != NULL) { |
917 | | ret = ssl->tls13KeyLogCb(ssl, EXPORTER_SECRET, key, |
918 | | ssl->specs.hash_size, NULL); |
919 | | if (ret != 0) { |
920 | | WOLFSSL_ERROR_VERBOSE(TLS13_SECRET_CB_E); |
921 | | return TLS13_SECRET_CB_E; |
922 | | } |
923 | | } |
924 | | #endif /* OPENSSL_EXTRA */ |
925 | | #endif /* HAVE_SECRET_CALLBACK */ |
926 | | return ret; |
927 | | } |
928 | | |
929 | | /* The length of the exporter label. */ |
930 | | #define EXPORTER_LABEL_SZ 8 |
931 | | /* The exporter label. */ |
932 | | static const byte exporterLabel[EXPORTER_LABEL_SZ + 1] = |
933 | | "exporter"; |
934 | | /* Hash("") */ |
935 | | #ifndef NO_SHA256 |
936 | | static const byte emptySHA256Hash[] = { |
937 | | 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, |
938 | | 0x99, 0x6F, 0xB9, 0x24, 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, |
939 | | 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 |
940 | | }; |
941 | | #endif |
942 | | #ifdef WOLFSSL_SHA384 |
943 | | static const byte emptySHA384Hash[] = { |
944 | | 0x38, 0xB0, 0x60, 0xA7, 0x51, 0xAC, 0x96, 0x38, 0x4C, 0xD9, 0x32, 0x7E, |
945 | | 0xB1, 0xB1, 0xE3, 0x6A, 0x21, 0xFD, 0xB7, 0x11, 0x14, 0xBE, 0x07, 0x43, |
946 | | 0x4C, 0x0C, 0xC7, 0xBF, 0x63, 0xF6, 0xE1, 0xDA, 0x27, 0x4E, 0xDE, 0xBF, |
947 | | 0xE7, 0x6F, 0x65, 0xFB, 0xD5, 0x1A, 0xD2, 0xF1, 0x48, 0x98, 0xB9, 0x5B |
948 | | }; |
949 | | #endif |
950 | | #ifdef WOLFSSL_TLS13_SHA512 |
951 | | static const byte emptySHA512Hash[] = { |
952 | | 0xCF, 0x83, 0xE1, 0x35, 0x7E, 0xEF, 0xB8, 0xBD, 0xF1, 0x54, 0x28, 0x50, |
953 | | 0xD6, 0x6D, 0x80, 0x07, 0xD6, 0x20, 0xE4, 0x05, 0x0B, 0x57, 0x15, 0xDC, |
954 | | 0x83, 0xF4, 0xA9, 0x21, 0xD3, 0x6C, 0xE9, 0xCE, 0x47, 0xD0, 0xD1, 0x3C, |
955 | | 0x5D, 0x85, 0xF2, 0xB0, 0xFF, 0x83, 0x18, 0xD2, 0x87, 0x7E, 0xEC, 0x2F, |
956 | | 0x63, 0xB9, 0x31, 0xBD, 0x47, 0x41, 0x7A, 0x81, 0xA5, 0x38, 0x32, 0x7A, |
957 | | 0xF9, 0x27, 0xDA, 0x3E |
958 | | }; |
959 | | #endif |
960 | | #ifdef WOLFSSL_SM3 |
961 | | static const byte emptySM3Hash[] = { |
962 | | 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F, 0x8E, 0x61, 0x19, 0x48, |
963 | | 0x31, 0xE8, 0x1A, 0x8F, 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74, |
964 | | 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B |
965 | | }; |
966 | | #endif |
967 | | /** |
968 | | * Implement section 7.5 of RFC 8446 |
969 | | * @return 0 on success |
970 | | * <0 on failure |
971 | | */ |
972 | | int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen, |
973 | | const char *label, size_t labelLen, |
974 | | const unsigned char *context, size_t contextLen) |
975 | | { |
976 | | int ret; |
977 | | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
978 | | word32 hashLen = 0; |
979 | | byte hashOut[WC_MAX_DIGEST_SIZE]; |
980 | | const byte* emptyHash = NULL; |
981 | | byte firstExpand[WC_MAX_DIGEST_SIZE]; |
982 | | const byte* protocol = tls13ProtocolLabel; |
983 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
984 | | |
985 | | if (ssl->options.dtls && ssl->version.minor != DTLSv1_3_MINOR) |
986 | | return VERSION_ERROR; |
987 | | |
988 | | if (!ssl->options.dtls && ssl->version.minor != TLSv1_3_MINOR) |
989 | | return VERSION_ERROR; |
990 | | |
991 | | #ifdef WOLFSSL_DTLS13 |
992 | | if (ssl->options.dtls) { |
993 | | protocol = dtls13ProtocolLabel; |
994 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
995 | | } |
996 | | #endif /* WOLFSSL_DTLS13 */ |
997 | | |
998 | | /* Sanity check contextLen to prevent truncation when cast to word32. */ |
999 | | if (contextLen > WOLFSSL_MAX_32BIT) |
1000 | | return BAD_FUNC_ARG; |
1001 | | /* RFC 8446 HkdfLabel encodes the output length as a uint16, so requested |
1002 | | * lengths > 65535 cannot be represented and must be rejected. */ |
1003 | | if (outLen > WOLFSSL_MAX_16BIT) |
1004 | | return BAD_FUNC_ARG; |
1005 | | /* RFC 8446 HkdfLabel encodes the label length in a single byte, so |
1006 | | * anything > 255 cannot be represented and must be rejected. |
1007 | | * The protocol length is included in the label. */ |
1008 | | if ((labelLen + protocolLen) > WOLFSSL_MAX_8BIT) |
1009 | | return BAD_FUNC_ARG; |
1010 | | |
1011 | | switch (ssl->specs.mac_algorithm) { |
1012 | | #ifndef NO_SHA256 |
1013 | | case sha256_mac: |
1014 | | hashType = WC_HASH_TYPE_SHA256; |
1015 | | hashLen = WC_SHA256_DIGEST_SIZE; |
1016 | | emptyHash = emptySHA256Hash; |
1017 | | break; |
1018 | | #endif |
1019 | | |
1020 | | #ifdef WOLFSSL_SHA384 |
1021 | | case sha384_mac: |
1022 | | hashType = WC_HASH_TYPE_SHA384; |
1023 | | hashLen = WC_SHA384_DIGEST_SIZE; |
1024 | | emptyHash = emptySHA384Hash; |
1025 | | break; |
1026 | | #endif |
1027 | | |
1028 | | #ifdef WOLFSSL_TLS13_SHA512 |
1029 | | case sha512_mac: |
1030 | | hashType = WC_HASH_TYPE_SHA512; |
1031 | | hashLen = WC_SHA512_DIGEST_SIZE; |
1032 | | emptyHash = emptySHA512Hash; |
1033 | | break; |
1034 | | #endif |
1035 | | |
1036 | | #ifdef WOLFSSL_SM3 |
1037 | | case sm3_mac: |
1038 | | hashType = WC_HASH_TYPE_SM3; |
1039 | | hashLen = WC_SM3_DIGEST_SIZE; |
1040 | | emptyHash = emptySM3Hash; |
1041 | | break; |
1042 | | #endif |
1043 | | |
1044 | | default: |
1045 | | return BAD_FUNC_ARG; |
1046 | | } |
1047 | | |
1048 | | /* Derive-Secret(Secret, label, "") */ |
1049 | | ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen, |
1050 | | ssl->arrays->exporterSecret, hashLen, |
1051 | | protocol, protocolLen, (byte*)label, (word32)labelLen, |
1052 | | emptyHash, hashLen, (int)hashType); |
1053 | | if (ret != 0) |
1054 | | return ret; |
1055 | | |
1056 | | /* Hash(context_value) */ |
1057 | | ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE); |
1058 | | if (ret != 0) |
1059 | | return ret; |
1060 | | |
1061 | | ret = Tls13HKDFExpandLabel(ssl, out, (word32)outLen, firstExpand, hashLen, |
1062 | | protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ, |
1063 | | hashOut, hashLen, (int)hashType); |
1064 | | |
1065 | | return ret; |
1066 | | } |
1067 | | #endif |
1068 | | |
1069 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1070 | | /* The length of the resumption master secret label. */ |
1071 | | #define RESUME_MASTER_LABEL_SZ 10 |
1072 | | /* The resumption master secret label. */ |
1073 | | static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] = |
1074 | | "res master"; |
1075 | | |
1076 | | /* Derive the resumption secret. |
1077 | | * |
1078 | | * ssl The SSL/TLS object. |
1079 | | * key The derived key. |
1080 | | * returns 0 on success, otherwise failure. |
1081 | | */ |
1082 | | int DeriveResumptionSecret(WOLFSSL* ssl, byte* key) |
1083 | | { |
1084 | | byte* masterSecret; |
1085 | | |
1086 | | WOLFSSL_MSG("Derive Resumption Secret"); |
1087 | | if (ssl == NULL) { |
1088 | | return BAD_FUNC_ARG; |
1089 | | } |
1090 | | if (ssl->arrays != NULL) { |
1091 | | masterSecret = ssl->arrays->masterSecret; |
1092 | | } |
1093 | | else { |
1094 | | masterSecret = ssl->session->masterSecret; |
1095 | | } |
1096 | | return Tls13DeriveKey(ssl, key, -1, masterSecret, resumeMasterLabel, |
1097 | | RESUME_MASTER_LABEL_SZ, ssl->specs.mac_algorithm, 1, |
1098 | | 0 /* Unused */); |
1099 | | } |
1100 | | #endif |
1101 | | |
1102 | | /* Length of the finished label. */ |
1103 | 0 | #define FINISHED_LABEL_SZ 8 |
1104 | | /* Finished label for generating finished key. */ |
1105 | | static const byte finishedLabel[FINISHED_LABEL_SZ+1] = "finished"; |
1106 | | /* Derive the finished secret. |
1107 | | * |
1108 | | * ssl The SSL/TLS object. |
1109 | | * key The key to use with the HMAC. |
1110 | | * secret The derived secret. |
1111 | | * side The side that we are deriving the secret for. |
1112 | | * returns 0 on success, otherwise failure. |
1113 | | */ |
1114 | | static int DeriveFinishedSecret(WOLFSSL* ssl, byte* key, byte* secret, |
1115 | | int side) |
1116 | 0 | { |
1117 | 0 | WOLFSSL_MSG("Derive Finished Secret"); |
1118 | 0 | return Tls13DeriveKey(ssl, secret, -1, key, finishedLabel, |
1119 | 0 | FINISHED_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1120 | 0 | side); |
1121 | 0 | } |
1122 | | |
1123 | | /* The length of the application traffic label. */ |
1124 | 0 | #define APP_TRAFFIC_LABEL_SZ 11 |
1125 | | /* The application traffic label. */ |
1126 | | static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] = |
1127 | | "traffic upd"; |
1128 | | |
1129 | | /* Update the traffic secret. |
1130 | | * |
1131 | | * ssl The SSL/TLS object. |
1132 | | * secret The previous secret and derived secret. |
1133 | | * side The side that we are deriving the secret for. |
1134 | | * returns 0 on success, otherwise failure. |
1135 | | */ |
1136 | | static int DeriveTrafficSecret(WOLFSSL* ssl, byte* secret, int side) |
1137 | 0 | { |
1138 | 0 | WOLFSSL_MSG("Derive New Application Traffic Secret"); |
1139 | 0 | return Tls13DeriveKey(ssl, secret, -1, secret, |
1140 | 0 | appTrafficLabel, APP_TRAFFIC_LABEL_SZ, |
1141 | 0 | ssl->specs.mac_algorithm, 0, side); |
1142 | 0 | } |
1143 | | |
1144 | | |
1145 | | static int Tls13_HKDF_Extract(WOLFSSL *ssl, byte* prk, const byte* salt, |
1146 | | int saltLen, byte* ikm, int ikmLen, int digest) |
1147 | 0 | { |
1148 | 0 | int ret; |
1149 | | #ifdef HAVE_PK_CALLBACKS |
1150 | | void *cb_ctx = ssl->HkdfExtractCtx; |
1151 | | CallbackHKDFExtract cb = ssl->ctx->HkdfExtractCb; |
1152 | | if (cb != NULL) { |
1153 | | ret = cb(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, cb_ctx); |
1154 | | } |
1155 | | else |
1156 | | #endif |
1157 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1158 | | if ((int)ssl->arrays->psk_keySz < 0) { |
1159 | | ret = PSK_KEY_ERROR; |
1160 | | } |
1161 | | else |
1162 | | #endif |
1163 | 0 | { |
1164 | 0 | #if !defined(HAVE_FIPS) || \ |
1165 | 0 | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
1166 | 0 | ret = wc_Tls13_HKDF_Extract_ex(prk, salt, (word32)saltLen, ikm, (word32)ikmLen, digest, |
1167 | 0 | ssl->heap, ssl->devId); |
1168 | | #else |
1169 | | ret = wc_Tls13_HKDF_Extract(prk, salt, saltLen, ikm, ikmLen, digest); |
1170 | | (void)ssl; |
1171 | | #endif |
1172 | 0 | } |
1173 | 0 | return ret; |
1174 | 0 | } |
1175 | | |
1176 | | /* Derive the early secret using HKDF Extract. |
1177 | | * |
1178 | | * ssl The SSL/TLS object. |
1179 | | */ |
1180 | | int DeriveEarlySecret(WOLFSSL* ssl) |
1181 | 0 | { |
1182 | 0 | int ret; |
1183 | |
|
1184 | 0 | WOLFSSL_MSG("Derive Early Secret"); |
1185 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
1186 | 0 | return BAD_FUNC_ARG; |
1187 | 0 | } |
1188 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1189 | | ret = tsip_Tls13DeriveEarlySecret(ssl); |
1190 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1191 | | return ret; |
1192 | | #endif |
1193 | 0 | PRIVATE_KEY_UNLOCK(); |
1194 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
1195 | | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1196 | | ssl->arrays->psk_key, (int)ssl->arrays->psk_keySz, |
1197 | | mac2hash(ssl->specs.mac_algorithm)); |
1198 | | #else |
1199 | 0 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->secret, NULL, 0, |
1200 | 0 | ssl->arrays->masterSecret, 0, mac2hash(ssl->specs.mac_algorithm)); |
1201 | 0 | #endif |
1202 | 0 | PRIVATE_KEY_LOCK(); |
1203 | 0 | return ret; |
1204 | 0 | } |
1205 | | |
1206 | | /* The length of the derived label. */ |
1207 | 0 | #define DERIVED_LABEL_SZ 7 |
1208 | | /* The derived label. */ |
1209 | | static const byte derivedLabel[DERIVED_LABEL_SZ + 1] = |
1210 | | "derived"; |
1211 | | |
1212 | | /* Derive the handshake secret using HKDF Extract. |
1213 | | * |
1214 | | * ssl The SSL/TLS object. |
1215 | | */ |
1216 | | int DeriveHandshakeSecret(WOLFSSL* ssl) |
1217 | 0 | { |
1218 | 0 | byte key[WC_MAX_DIGEST_SIZE]; |
1219 | 0 | int ret; |
1220 | 0 | WOLFSSL_MSG("Derive Handshake Secret"); |
1221 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
1222 | 0 | return BAD_FUNC_ARG; |
1223 | 0 | } |
1224 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1225 | | ret = tsip_Tls13DeriveHandshakeSecret(ssl); |
1226 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1227 | | return ret; |
1228 | | #endif |
1229 | | |
1230 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1231 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1232 | 0 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret, |
1233 | 0 | derivedLabel, DERIVED_LABEL_SZ, |
1234 | 0 | NULL, 0, ssl->specs.mac_algorithm); |
1235 | 0 | if (ret == 0) { |
1236 | 0 | PRIVATE_KEY_UNLOCK(); |
1237 | 0 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->preMasterSecret, |
1238 | 0 | key, ssl->specs.hash_size, |
1239 | 0 | ssl->arrays->preMasterSecret, (int)ssl->arrays->preMasterSz, |
1240 | 0 | mac2hash(ssl->specs.mac_algorithm)); |
1241 | 0 | PRIVATE_KEY_LOCK(); |
1242 | 0 | } |
1243 | |
|
1244 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1245 | | wc_MemZero_Add("DeriveHandshakeSecret key", key, WC_MAX_DIGEST_SIZE); |
1246 | | #endif |
1247 | 0 | ForceZero(key, sizeof(key)); |
1248 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1249 | | wc_MemZero_Check(key, sizeof(key)); |
1250 | | #endif |
1251 | 0 | return ret; |
1252 | 0 | } |
1253 | | |
1254 | | /* Derive the master secret using HKDF Extract. |
1255 | | * |
1256 | | * ssl The SSL/TLS object. |
1257 | | */ |
1258 | | int DeriveMasterSecret(WOLFSSL* ssl) |
1259 | 0 | { |
1260 | 0 | byte key[WC_MAX_DIGEST_SIZE]; |
1261 | 0 | int ret; |
1262 | 0 | WOLFSSL_MSG("Derive Master Secret"); |
1263 | 0 | if (ssl == NULL || ssl->arrays == NULL) { |
1264 | 0 | return BAD_FUNC_ARG; |
1265 | 0 | } |
1266 | | |
1267 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1268 | | ret = tsip_Tls13DeriveMasterSecret(ssl); |
1269 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1270 | | return ret; |
1271 | | #endif |
1272 | | |
1273 | | /* Derive-Secret(., "derived", "") per RFC 8446 Section 7.1. |
1274 | | * Empty hash (NULL, 0) is required by the TLS 1.3 key schedule. */ |
1275 | 0 | ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret, |
1276 | 0 | derivedLabel, DERIVED_LABEL_SZ, |
1277 | 0 | NULL, 0, ssl->specs.mac_algorithm); |
1278 | 0 | if (ret == 0) { |
1279 | 0 | PRIVATE_KEY_UNLOCK(); |
1280 | 0 | ret = Tls13_HKDF_Extract(ssl, ssl->arrays->masterSecret, |
1281 | 0 | key, ssl->specs.hash_size, |
1282 | 0 | ssl->arrays->masterSecret, 0, |
1283 | 0 | mac2hash(ssl->specs.mac_algorithm)); |
1284 | 0 | PRIVATE_KEY_LOCK(); |
1285 | 0 | } |
1286 | |
|
1287 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1288 | | wc_MemZero_Add("DeriveMasterSecret key", key, WC_MAX_DIGEST_SIZE); |
1289 | | #endif |
1290 | 0 | ForceZero(key, sizeof(key)); |
1291 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1292 | | wc_MemZero_Check(key, sizeof(key)); |
1293 | | #endif |
1294 | |
|
1295 | | #ifdef HAVE_KEYING_MATERIAL |
1296 | | if (ret != 0) |
1297 | | return ret; |
1298 | | /* Calculate exporter secret only when saving arrays */ |
1299 | | if (ssl->options.saveArrays) |
1300 | | ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret); |
1301 | | #endif |
1302 | |
|
1303 | 0 | return ret; |
1304 | 0 | } |
1305 | | |
1306 | | #if defined(HAVE_SESSION_TICKET) |
1307 | | /* Length of the resumption label. */ |
1308 | | #define RESUMPTION_LABEL_SZ 10 |
1309 | | /* Resumption label for generating PSK associated with the ticket. */ |
1310 | | static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption"; |
1311 | | |
1312 | | /* Derive the PSK associated with the ticket. |
1313 | | * |
1314 | | * ssl The SSL/TLS object. |
1315 | | * nonce The nonce to derive with. |
1316 | | * nonceLen The length of the nonce to derive with. |
1317 | | * secret The derived secret. |
1318 | | * returns 0 on success, otherwise failure. |
1319 | | */ |
1320 | | int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret) |
1321 | | { |
1322 | | int digestAlg; |
1323 | | /* Only one protocol version defined at this time. */ |
1324 | | const byte* protocol = tls13ProtocolLabel; |
1325 | | word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ; |
1326 | | int ret; |
1327 | | |
1328 | | WOLFSSL_MSG("Derive Resumption PSK"); |
1329 | | |
1330 | | #ifdef WOLFSSL_DTLS13 |
1331 | | if (ssl->options.dtls) { |
1332 | | protocol = dtls13ProtocolLabel; |
1333 | | protocolLen = DTLS13_PROTOCOL_LABEL_SZ; |
1334 | | } |
1335 | | #endif /* WOLFSSL_DTLS13 */ |
1336 | | |
1337 | | switch (ssl->specs.mac_algorithm) { |
1338 | | #ifndef NO_SHA256 |
1339 | | case sha256_mac: |
1340 | | digestAlg = WC_SHA256; |
1341 | | break; |
1342 | | #endif |
1343 | | |
1344 | | #ifdef WOLFSSL_SHA384 |
1345 | | case sha384_mac: |
1346 | | digestAlg = WC_SHA384; |
1347 | | break; |
1348 | | #endif |
1349 | | |
1350 | | #ifdef WOLFSSL_TLS13_SHA512 |
1351 | | case sha512_mac: |
1352 | | digestAlg = WC_SHA512; |
1353 | | break; |
1354 | | #endif |
1355 | | |
1356 | | #ifdef WOLFSSL_SM3 |
1357 | | case sm3_mac: |
1358 | | digestAlg = WC_SM3; |
1359 | | break; |
1360 | | #endif |
1361 | | |
1362 | | default: |
1363 | | return BAD_FUNC_ARG; |
1364 | | } |
1365 | | |
1366 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
1367 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
1368 | | PRIVATE_KEY_UNLOCK(); |
1369 | | ret = wc_Tls13_HKDF_Expand_Label_Alloc(secret, ssl->specs.hash_size, |
1370 | | ssl->session->masterSecret, ssl->specs.hash_size, protocol, protocolLen, |
1371 | | resumptionLabel, RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg, |
1372 | | ssl->heap); |
1373 | | PRIVATE_KEY_LOCK(); |
1374 | | #else |
1375 | | ret = Tls13HKDFExpandLabel(ssl, secret, ssl->specs.hash_size, |
1376 | | ssl->session->masterSecret, ssl->specs.hash_size, |
1377 | | protocol, protocolLen, resumptionLabel, |
1378 | | RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg); |
1379 | | #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */ |
1380 | | return ret; |
1381 | | } |
1382 | | #endif /* HAVE_SESSION_TICKET */ |
1383 | | |
1384 | | |
1385 | | /* Calculate the HMAC of message data to this point. |
1386 | | * |
1387 | | * ssl The SSL/TLS object. |
1388 | | * key The HMAC key. |
1389 | | * hash The hash result - verify data. |
1390 | | * returns length of verify data generated. |
1391 | | */ |
1392 | | static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash, |
1393 | | word32* pHashSz) |
1394 | 0 | { |
1395 | 0 | WC_DECLARE_VAR(verifyHmac, Hmac, 1, 0); |
1396 | 0 | int hashType = WC_SHA256; |
1397 | 0 | int hashSz = WC_SHA256_DIGEST_SIZE; |
1398 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
1399 | |
|
1400 | 0 | if (ssl == NULL || key == NULL || hash == NULL) { |
1401 | 0 | return BAD_FUNC_ARG; |
1402 | 0 | } |
1403 | | |
1404 | | /* Get the hash of the previous handshake messages. */ |
1405 | 0 | switch (ssl->specs.mac_algorithm) { |
1406 | 0 | #ifndef NO_SHA256 |
1407 | 0 | case sha256_mac: |
1408 | 0 | hashType = WC_SHA256; |
1409 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
1410 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
1411 | 0 | break; |
1412 | 0 | #endif /* !NO_SHA256 */ |
1413 | 0 | #ifdef WOLFSSL_SHA384 |
1414 | 0 | case sha384_mac: |
1415 | 0 | hashType = WC_SHA384; |
1416 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
1417 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
1418 | 0 | break; |
1419 | 0 | #endif /* WOLFSSL_SHA384 */ |
1420 | | #ifdef WOLFSSL_TLS13_SHA512 |
1421 | | case sha512_mac: |
1422 | | hashType = WC_SHA512; |
1423 | | hashSz = WC_SHA512_DIGEST_SIZE; |
1424 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
1425 | | break; |
1426 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
1427 | | #ifdef WOLFSSL_SM3 |
1428 | | case sm3_mac: |
1429 | | hashType = WC_SM3; |
1430 | | hashSz = WC_SM3_DIGEST_SIZE; |
1431 | | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
1432 | | break; |
1433 | | #endif /* WOLFSSL_SM3 */ |
1434 | 0 | default: |
1435 | 0 | ret = BAD_FUNC_ARG; |
1436 | 0 | break; |
1437 | 0 | } |
1438 | 0 | if (ret != 0) |
1439 | 0 | return ret; |
1440 | | |
1441 | | #ifdef WOLFSSL_DEBUG_TLS |
1442 | | WOLFSSL_MSG(" Key"); |
1443 | | WOLFSSL_BUFFER(key, ssl->specs.hash_size); |
1444 | | WOLFSSL_MSG(" Msg Hash"); |
1445 | | WOLFSSL_BUFFER(hash, hashSz); |
1446 | | #endif |
1447 | | |
1448 | 0 | WC_ALLOC_VAR_EX(verifyHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
1449 | 0 | return MEMORY_E); |
1450 | | |
1451 | | /* Calculate the verify data. */ |
1452 | 0 | ret = wc_HmacInit(verifyHmac, ssl->heap, ssl->devId); |
1453 | 0 | if (ret == 0) { |
1454 | 0 | ret = wc_HmacSetKey(verifyHmac, hashType, key, ssl->specs.hash_size); |
1455 | 0 | if (ret == 0) |
1456 | 0 | ret = wc_HmacUpdate(verifyHmac, hash, (word32)hashSz); |
1457 | 0 | if (ret == 0) |
1458 | 0 | ret = wc_HmacFinal(verifyHmac, hash); |
1459 | 0 | wc_HmacFree(verifyHmac); |
1460 | 0 | } |
1461 | |
|
1462 | 0 | WC_FREE_VAR_EX(verifyHmac, NULL, DYNAMIC_TYPE_HMAC); |
1463 | |
|
1464 | | #ifdef WOLFSSL_DEBUG_TLS |
1465 | | WOLFSSL_MSG(" Hash"); |
1466 | | WOLFSSL_BUFFER(hash, hashSz); |
1467 | | #endif |
1468 | |
|
1469 | 0 | if (pHashSz) |
1470 | 0 | *pHashSz = (word32)hashSz; |
1471 | |
|
1472 | 0 | return ret; |
1473 | 0 | } |
1474 | | |
1475 | | /* The length of the label to use when deriving keys. */ |
1476 | 0 | #define WRITE_KEY_LABEL_SZ 3 |
1477 | | /* The length of the label to use when deriving IVs. */ |
1478 | 0 | #define WRITE_IV_LABEL_SZ 2 |
1479 | | /* The label to use when deriving keys. */ |
1480 | | static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key"; |
1481 | | /* The label to use when deriving IVs. */ |
1482 | | static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv"; |
1483 | | |
1484 | | /* Derive the keys and IVs for TLS v1.3. |
1485 | | * |
1486 | | * ssl The SSL/TLS object. |
1487 | | * secret early_data_key when deriving the key and IV for encrypting early |
1488 | | * data application data and end_of_early_data messages. |
1489 | | * handshake_key when deriving keys and IVs for encrypting handshake |
1490 | | * messages. |
1491 | | * traffic_key when deriving first keys and IVs for encrypting |
1492 | | * traffic messages. |
1493 | | * update_traffic_key when deriving next keys and IVs for encrypting |
1494 | | * traffic messages. |
1495 | | * no_key when deriving keys and IVs from existing secrets without |
1496 | | * re-deriving the secrets. Used during early data transitions. |
1497 | | * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived. |
1498 | | * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived. |
1499 | | * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived. |
1500 | | * store 1 indicates to derive the keys and IVs from derived secret and |
1501 | | * store ready for provisioning. |
1502 | | * returns 0 on success, otherwise failure. |
1503 | | */ |
1504 | | int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store) |
1505 | 0 | { |
1506 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1507 | 0 | int i = 0; |
1508 | 0 | WC_DECLARE_VAR(key_dig, byte, MAX_PRF_DIG, 0); |
1509 | 0 | int provision; |
1510 | |
|
1511 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
1512 | | ret = tsip_Tls13DeriveKeys(ssl, secret, side); |
1513 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1514 | | return ret; |
1515 | | } |
1516 | | ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); /* Assume failure */ |
1517 | | #endif |
1518 | |
|
1519 | 0 | WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap, |
1520 | 0 | DYNAMIC_TYPE_DIGEST, return MEMORY_E); |
1521 | |
|
1522 | 0 | if (side == ENCRYPT_AND_DECRYPT_SIDE) { |
1523 | 0 | provision = PROVISION_CLIENT_SERVER; |
1524 | 0 | } |
1525 | 0 | else { |
1526 | 0 | provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^ |
1527 | 0 | (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT : |
1528 | 0 | PROVISION_SERVER; |
1529 | 0 | } |
1530 | | |
1531 | | /* Derive the appropriate secret to use in the HKDF. */ |
1532 | 0 | switch (secret) { |
1533 | | #ifdef WOLFSSL_EARLY_DATA |
1534 | | case early_data_key: |
1535 | | ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret, |
1536 | | WOLFSSL_CLIENT_END); |
1537 | | if (ret != 0) |
1538 | | goto end; |
1539 | | break; |
1540 | | #endif |
1541 | | |
1542 | 0 | case handshake_key: |
1543 | 0 | if (provision & PROVISION_CLIENT) { |
1544 | 0 | ret = DeriveClientHandshakeSecret(ssl, |
1545 | 0 | ssl->clientSecret); |
1546 | 0 | if (ret != 0) |
1547 | 0 | goto end; |
1548 | 0 | } |
1549 | 0 | if (provision & PROVISION_SERVER) { |
1550 | 0 | ret = DeriveServerHandshakeSecret(ssl, |
1551 | 0 | ssl->serverSecret); |
1552 | 0 | if (ret != 0) |
1553 | 0 | goto end; |
1554 | 0 | } |
1555 | 0 | break; |
1556 | | |
1557 | 0 | case traffic_key: |
1558 | 0 | if (provision & PROVISION_CLIENT) { |
1559 | 0 | ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret); |
1560 | 0 | if (ret != 0) |
1561 | 0 | goto end; |
1562 | 0 | } |
1563 | 0 | if (provision & PROVISION_SERVER) { |
1564 | 0 | ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret); |
1565 | 0 | if (ret != 0) |
1566 | 0 | goto end; |
1567 | 0 | } |
1568 | 0 | break; |
1569 | | |
1570 | 0 | case update_traffic_key: |
1571 | 0 | if (provision & PROVISION_CLIENT) { |
1572 | 0 | ret = DeriveTrafficSecret(ssl, ssl->clientSecret, |
1573 | 0 | WOLFSSL_CLIENT_END); |
1574 | 0 | if (ret != 0) |
1575 | 0 | goto end; |
1576 | 0 | } |
1577 | 0 | if (provision & PROVISION_SERVER) { |
1578 | 0 | ret = DeriveTrafficSecret(ssl, ssl->serverSecret, |
1579 | 0 | WOLFSSL_SERVER_END); |
1580 | 0 | if (ret != 0) |
1581 | 0 | goto end; |
1582 | 0 | } |
1583 | 0 | break; |
1584 | | |
1585 | 0 | case no_key: |
1586 | | /* Called with early data to derive keys from existing secrets |
1587 | | * without re-deriving the secrets themselves. */ |
1588 | 0 | ret = 0; |
1589 | 0 | break; |
1590 | | |
1591 | 0 | default: |
1592 | 0 | ret = BAD_FUNC_ARG; |
1593 | 0 | break; |
1594 | 0 | } |
1595 | | |
1596 | | #ifdef WOLFSSL_QUIC |
1597 | | if (WOLFSSL_IS_QUIC(ssl)) { |
1598 | | ret = wolfSSL_quic_forward_secrets(ssl, secret, side); |
1599 | | if (ret != 0) |
1600 | | goto end; |
1601 | | } |
1602 | | #endif /* WOLFSSL_QUIC */ |
1603 | | |
1604 | 0 | if (!store) |
1605 | 0 | goto end; |
1606 | | |
1607 | | /* Key data = client key | server key | client IV | server IV */ |
1608 | | |
1609 | 0 | if (provision & PROVISION_CLIENT) { |
1610 | | /* Derive the client key. */ |
1611 | 0 | WOLFSSL_MSG("Derive Client Key"); |
1612 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1613 | 0 | ssl->clientSecret, writeKeyLabel, |
1614 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1615 | 0 | WOLFSSL_CLIENT_END); |
1616 | 0 | if (ret != 0) |
1617 | 0 | goto end; |
1618 | 0 | i += ssl->specs.key_size; |
1619 | 0 | } |
1620 | | |
1621 | 0 | if (provision & PROVISION_SERVER) { |
1622 | | /* Derive the server key. */ |
1623 | 0 | WOLFSSL_MSG("Derive Server Key"); |
1624 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.key_size, |
1625 | 0 | ssl->serverSecret, writeKeyLabel, |
1626 | 0 | WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1627 | 0 | WOLFSSL_SERVER_END); |
1628 | 0 | if (ret != 0) |
1629 | 0 | goto end; |
1630 | 0 | i += ssl->specs.key_size; |
1631 | 0 | } |
1632 | | |
1633 | 0 | if (provision & PROVISION_CLIENT) { |
1634 | | /* Derive the client IV. */ |
1635 | 0 | WOLFSSL_MSG("Derive Client IV"); |
1636 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1637 | 0 | ssl->clientSecret, writeIVLabel, |
1638 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1639 | 0 | WOLFSSL_CLIENT_END); |
1640 | 0 | if (ret != 0) |
1641 | 0 | goto end; |
1642 | 0 | i += ssl->specs.iv_size; |
1643 | 0 | } |
1644 | | |
1645 | 0 | if (provision & PROVISION_SERVER) { |
1646 | | /* Derive the server IV. */ |
1647 | 0 | WOLFSSL_MSG("Derive Server IV"); |
1648 | 0 | ret = Tls13DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size, |
1649 | 0 | ssl->serverSecret, writeIVLabel, |
1650 | 0 | WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0, |
1651 | 0 | WOLFSSL_SERVER_END); |
1652 | 0 | if (ret != 0) |
1653 | 0 | goto end; |
1654 | 0 | i += ssl->specs.iv_size; |
1655 | 0 | } |
1656 | | |
1657 | | /* Store keys and IVs but don't activate them. */ |
1658 | 0 | ret = StoreKeys(ssl, key_dig, provision); |
1659 | |
|
1660 | | #ifdef WOLFSSL_DTLS13 |
1661 | | if (ret != 0) |
1662 | | goto end; |
1663 | | |
1664 | | if (ssl->options.dtls) { |
1665 | | w64wrapper epochNumber; |
1666 | | ret = Dtls13DeriveSnKeys(ssl, provision); |
1667 | | if (ret != 0) |
1668 | | goto end; |
1669 | | |
1670 | | switch (secret) { |
1671 | | case early_data_key: |
1672 | | epochNumber = w64From32(0, DTLS13_EPOCH_EARLYDATA); |
1673 | | break; |
1674 | | case handshake_key: |
1675 | | epochNumber = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
1676 | | break; |
1677 | | case traffic_key: |
1678 | | case no_key: |
1679 | | epochNumber = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
1680 | | break; |
1681 | | case update_traffic_key: |
1682 | | if (side == ENCRYPT_SIDE_ONLY) { |
1683 | | epochNumber = ssl->dtls13Epoch; |
1684 | | } |
1685 | | else if (side == DECRYPT_SIDE_ONLY) { |
1686 | | epochNumber = ssl->dtls13PeerEpoch; |
1687 | | } |
1688 | | else { |
1689 | | ret = BAD_STATE_E; |
1690 | | goto end; |
1691 | | } |
1692 | | w64Increment(&epochNumber); |
1693 | | break; |
1694 | | default: |
1695 | | ret = BAD_STATE_E; |
1696 | | goto end; |
1697 | | } |
1698 | | ret = Dtls13NewEpoch(ssl, epochNumber, side); |
1699 | | if (ret != 0) |
1700 | | goto end; |
1701 | | } |
1702 | | |
1703 | | #endif /* WOLFSSL_DTLS13 */ |
1704 | |
|
1705 | 0 | end: |
1706 | 0 | ForceZero(key_dig, (word32)i); |
1707 | | #ifdef WOLFSSL_SMALL_STACK |
1708 | | XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST); |
1709 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
1710 | | wc_MemZero_Check(key_dig, MAX_PRF_DIG); |
1711 | | #endif |
1712 | |
|
1713 | 0 | if (ret != 0) { |
1714 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
1715 | 0 | } |
1716 | |
|
1717 | 0 | return ret; |
1718 | 0 | } |
1719 | | |
1720 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) || defined(WOLFSSL_DTLS13) |
1721 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
1722 | | #ifndef NO_ASN_TIME |
1723 | | #if defined(USER_TICKS) |
1724 | | #if 0 |
1725 | | word32 TimeNowInMilliseconds(void) |
1726 | | { |
1727 | | /* |
1728 | | write your own clock tick function if don't want gettimeofday() |
1729 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
1730 | | */ |
1731 | | } |
1732 | | #endif |
1733 | | |
1734 | | #elif defined(TIME_OVERRIDES) |
1735 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
1736 | | word32 TimeNowInMilliseconds(void) |
1737 | | { |
1738 | | return (word32) wc_Time(0) * 1000; |
1739 | | } |
1740 | | #else |
1741 | | #ifndef HAVE_TIME_T_TYPE |
1742 | | typedef long time_t; |
1743 | | #endif |
1744 | | extern time_t XTIME(time_t * timer); |
1745 | | |
1746 | | /* The time in milliseconds. |
1747 | | * Used for tickets to represent difference between when first seen and when |
1748 | | * sending. |
1749 | | * |
1750 | | * returns the time in milliseconds as a 32-bit value. |
1751 | | */ |
1752 | | word32 TimeNowInMilliseconds(void) |
1753 | | { |
1754 | | return (word32) XTIME(0) * 1000; |
1755 | | } |
1756 | | #endif |
1757 | | |
1758 | | #elif defined(XTIME_MS) |
1759 | | word32 TimeNowInMilliseconds(void) |
1760 | | { |
1761 | | return (word32)XTIME_MS(0); |
1762 | | } |
1763 | | |
1764 | | #elif defined(USE_WINDOWS_API) |
1765 | | /* The time in milliseconds. |
1766 | | * Used for tickets to represent difference between when first seen and when |
1767 | | * sending. |
1768 | | * |
1769 | | * returns the time in milliseconds as a 32-bit value. |
1770 | | */ |
1771 | | word32 TimeNowInMilliseconds(void) |
1772 | | { |
1773 | | static int init = 0; |
1774 | | static LARGE_INTEGER freq; |
1775 | | LARGE_INTEGER count; |
1776 | | |
1777 | | if (!init) { |
1778 | | QueryPerformanceFrequency(&freq); |
1779 | | init = 1; |
1780 | | } |
1781 | | |
1782 | | QueryPerformanceCounter(&count); |
1783 | | |
1784 | | return (word32)(count.QuadPart / (freq.QuadPart / 1000)); |
1785 | | } |
1786 | | |
1787 | | #elif defined(HAVE_RTP_SYS) |
1788 | | #include "rtptime.h" |
1789 | | |
1790 | | /* The time in milliseconds. |
1791 | | * Used for tickets to represent difference between when first seen and when |
1792 | | * sending. |
1793 | | * |
1794 | | * returns the time in milliseconds as a 32-bit value. |
1795 | | */ |
1796 | | word32 TimeNowInMilliseconds(void) |
1797 | | { |
1798 | | return (word32)rtp_get_system_sec() * 1000; |
1799 | | } |
1800 | | #elif defined(WOLFSSL_DEOS) |
1801 | | word32 TimeNowInMilliseconds(void) |
1802 | | { |
1803 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
1804 | | word32 *systemTickPtr = systemTickPointer(); |
1805 | | |
1806 | | return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000; |
1807 | | } |
1808 | | #elif defined(MICRIUM) |
1809 | | /* The time in milliseconds. |
1810 | | * Used for tickets to represent difference between when first seen and when |
1811 | | * sending. |
1812 | | * |
1813 | | * returns the time in milliseconds as a 32-bit value. |
1814 | | */ |
1815 | | word32 TimeNowInMilliseconds(void) |
1816 | | { |
1817 | | OS_TICK ticks = 0; |
1818 | | OS_ERR err; |
1819 | | |
1820 | | ticks = OSTimeGet(&err); |
1821 | | |
1822 | | return (word32) (ticks / OSCfg_TickRate_Hz) * 1000; |
1823 | | } |
1824 | | #elif defined(MICROCHIP_TCPIP_V5) |
1825 | | /* The time in milliseconds. |
1826 | | * Used for tickets to represent difference between when first seen and when |
1827 | | * sending. |
1828 | | * |
1829 | | * returns the time in milliseconds as a 32-bit value. |
1830 | | */ |
1831 | | word32 TimeNowInMilliseconds(void) |
1832 | | { |
1833 | | return (word32) (TickGet() / (TICKS_PER_SECOND / 1000)); |
1834 | | } |
1835 | | #elif defined(MICROCHIP_TCPIP) |
1836 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
1837 | | #include <system/tmr/sys_tmr.h> |
1838 | | |
1839 | | /* The time in milliseconds. |
1840 | | * Used for tickets to represent difference between when first seen and when |
1841 | | * sending. |
1842 | | * |
1843 | | * returns the time in milliseconds as a 32-bit value. |
1844 | | */ |
1845 | | word32 TimeNowInMilliseconds(void) |
1846 | | { |
1847 | | return (word32)(SYS_TMR_TickCountGet() / |
1848 | | (SYS_TMR_TickCounterFrequencyGet() / 1000)); |
1849 | | } |
1850 | | #else |
1851 | | /* The time in milliseconds. |
1852 | | * Used for tickets to represent difference between when first seen and when |
1853 | | * sending. |
1854 | | * |
1855 | | * returns the time in milliseconds as a 32-bit value. |
1856 | | */ |
1857 | | word32 TimeNowInMilliseconds(void) |
1858 | | { |
1859 | | return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000)); |
1860 | | } |
1861 | | |
1862 | | #endif |
1863 | | |
1864 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1865 | | /* The time in milliseconds. |
1866 | | * Used for tickets to represent difference between when first seen and when |
1867 | | * sending. |
1868 | | * |
1869 | | * returns the time in milliseconds as a 32-bit value. |
1870 | | */ |
1871 | | word32 TimeNowInMilliseconds(void) |
1872 | | { |
1873 | | TIME_STRUCT mqxTime; |
1874 | | |
1875 | | _time_get_elapsed(&mqxTime); |
1876 | | |
1877 | | return (word32) mqxTime.SECONDS * 1000; |
1878 | | } |
1879 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
1880 | | #include "include/task.h" |
1881 | | |
1882 | | /* The time in milliseconds. |
1883 | | * Used for tickets to represent difference between when first seen and when |
1884 | | * sending. |
1885 | | * |
1886 | | * returns the time in milliseconds as a 32-bit value. |
1887 | | */ |
1888 | | word32 TimeNowInMilliseconds(void) |
1889 | | { |
1890 | | return (unsigned int)(((float)xTaskGetTickCount()) / |
1891 | | (configTICK_RATE_HZ / 1000)); |
1892 | | } |
1893 | | #elif defined(FREESCALE_KSDK_BM) |
1894 | | #include "lwip/sys.h" /* lwIP */ |
1895 | | |
1896 | | /* The time in milliseconds. |
1897 | | * Used for tickets to represent difference between when first seen and when |
1898 | | * sending. |
1899 | | * |
1900 | | * returns the time in milliseconds as a 32-bit value. |
1901 | | */ |
1902 | | word32 TimeNowInMilliseconds(void) |
1903 | | { |
1904 | | return sys_now(); |
1905 | | } |
1906 | | |
1907 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
1908 | | |
1909 | | word32 TimeNowInMilliseconds(void) |
1910 | | { |
1911 | | return (word32)osKernelGetTickCount(); |
1912 | | } |
1913 | | |
1914 | | #elif defined(WOLFSSL_TIRTOS) |
1915 | | /* The time in milliseconds. |
1916 | | * Used for tickets to represent difference between when first seen and when |
1917 | | * sending. |
1918 | | * |
1919 | | * returns the time in milliseconds as a 32-bit value. |
1920 | | */ |
1921 | | word32 TimeNowInMilliseconds(void) |
1922 | | { |
1923 | | return (word32) Seconds_get() * 1000; |
1924 | | } |
1925 | | #elif defined(WOLFSSL_UTASKER) |
1926 | | /* The time in milliseconds. |
1927 | | * Used for tickets to represent difference between when first seen and when |
1928 | | * sending. |
1929 | | * |
1930 | | * returns the time in milliseconds as a 32-bit value. |
1931 | | */ |
1932 | | word32 TimeNowInMilliseconds(void) |
1933 | | { |
1934 | | return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
1935 | | } |
1936 | | #elif defined(WOLFSSL_LINUXKM) |
1937 | | word32 TimeNowInMilliseconds(void) |
1938 | | { |
1939 | | s64 t; |
1940 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
1941 | | struct timespec ts; |
1942 | | getnstimeofday(&ts); |
1943 | | t = ts.tv_sec * (s64)1000; |
1944 | | t += ts.tv_nsec / (s64)1000000; |
1945 | | #else |
1946 | | struct timespec64 ts; |
1947 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
1948 | | ts = current_kernel_time64(); |
1949 | | #else |
1950 | | ktime_get_coarse_real_ts64(&ts); |
1951 | | #endif |
1952 | | t = ts.tv_sec * 1000L; |
1953 | | t += ts.tv_nsec / 1000000L; |
1954 | | #endif |
1955 | | return (word32)t; |
1956 | | } |
1957 | | #elif defined(WOLFSSL_QNX_CAAM) |
1958 | | word32 TimeNowInMilliseconds(void) |
1959 | | { |
1960 | | struct timespec now; |
1961 | | clock_gettime(CLOCK_REALTIME, &now); |
1962 | | return (word32)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
1963 | | } |
1964 | | #elif defined(FUSION_RTOS) |
1965 | | /* The time in milliseconds. |
1966 | | * Used for tickets to represent difference between when first seen and when |
1967 | | * sending. |
1968 | | * |
1969 | | * returns the time in milliseconds as a 32-bit value. |
1970 | | */ |
1971 | | word32 TimeNowInMilliseconds(void) |
1972 | | { |
1973 | | struct timeval now; |
1974 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
1975 | | return 0; |
1976 | | |
1977 | | /* Convert to milliseconds number. */ |
1978 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
1979 | | } |
1980 | | #elif defined(WOLFSSL_ZEPHYR) |
1981 | | word32 TimeNowInMilliseconds(void) |
1982 | | { |
1983 | | int64_t t; |
1984 | | #if defined(CONFIG_ARCH_POSIX) |
1985 | | k_cpu_idle(); |
1986 | | #endif |
1987 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
1988 | | return (word32)t; |
1989 | | } |
1990 | | #elif defined(FREERTOS) |
1991 | | word32 TimeNowInMilliseconds(void) |
1992 | | { |
1993 | | return (word32)((uint64_t)(xTaskGetTickCount() * 1000) / |
1994 | | configTICK_RATE_HZ); |
1995 | | } |
1996 | | #else |
1997 | | /* The time in milliseconds. |
1998 | | * Used for tickets to represent difference between when first seen and when |
1999 | | * sending. |
2000 | | * |
2001 | | * returns the time in milliseconds as a 32-bit value. |
2002 | | */ |
2003 | | word32 TimeNowInMilliseconds(void) |
2004 | | { |
2005 | | struct timeval now; |
2006 | | |
2007 | | if (gettimeofday(&now, 0) < 0) |
2008 | | return 0; |
2009 | | |
2010 | | /* Convert to milliseconds number. */ |
2011 | | return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000); |
2012 | | } |
2013 | | #endif |
2014 | | #else |
2015 | | /* user must supply time in milliseconds function: |
2016 | | * word32 TimeNowInMilliseconds(void); |
2017 | | * The response is milliseconds elapsed |
2018 | | */ |
2019 | | #endif /* !NO_ASN_TIME */ |
2020 | | #else |
2021 | | #ifndef NO_ASN_TIME |
2022 | | #if defined(USER_TICKS) |
2023 | | #if 0 |
2024 | | sword64 TimeNowInMilliseconds(void) |
2025 | | { |
2026 | | /* |
2027 | | write your own clock tick function if don't want gettimeofday() |
2028 | | needs millisecond accuracy but doesn't have to correlated to EPOCH |
2029 | | */ |
2030 | | } |
2031 | | #endif |
2032 | | |
2033 | | #elif defined(TIME_OVERRIDES) |
2034 | | #if !defined(NO_ASN) && !defined(NO_ASN_TIME) |
2035 | | sword64 TimeNowInMilliseconds(void) |
2036 | | { |
2037 | | return (sword64) wc_Time(0) * 1000; |
2038 | | } |
2039 | | #else |
2040 | | #ifndef HAVE_TIME_T_TYPE |
2041 | | typedef long time_t; |
2042 | | #endif |
2043 | | extern time_t XTIME(time_t * timer); |
2044 | | |
2045 | | /* The time in milliseconds. |
2046 | | * Used for tickets to represent difference between when first seen and when |
2047 | | * sending. |
2048 | | * |
2049 | | * returns the time in milliseconds as a 32-bit value. |
2050 | | */ |
2051 | | sword64 TimeNowInMilliseconds(void) |
2052 | | { |
2053 | | return (sword64) XTIME(0) * 1000; |
2054 | | } |
2055 | | #endif |
2056 | | |
2057 | | #elif defined(XTIME_MS) |
2058 | | sword64 TimeNowInMilliseconds(void) |
2059 | | { |
2060 | | return (sword64)XTIME_MS(0); |
2061 | | } |
2062 | | |
2063 | | #elif defined(USE_WINDOWS_API) |
2064 | | /* The time in milliseconds. |
2065 | | * Used for tickets to represent difference between when first seen and when |
2066 | | * sending. |
2067 | | * |
2068 | | * returns the time in milliseconds as a 64-bit value. |
2069 | | */ |
2070 | | sword64 TimeNowInMilliseconds(void) |
2071 | | { |
2072 | | static int init = 0; |
2073 | | static LARGE_INTEGER freq; |
2074 | | LARGE_INTEGER count; |
2075 | | |
2076 | | if (!init) { |
2077 | | QueryPerformanceFrequency(&freq); |
2078 | | init = 1; |
2079 | | } |
2080 | | |
2081 | | QueryPerformanceCounter(&count); |
2082 | | |
2083 | | return (sword64)(count.QuadPart / (freq.QuadPart / 1000)); |
2084 | | } |
2085 | | |
2086 | | #elif defined(HAVE_RTP_SYS) |
2087 | | #include "rtptime.h" |
2088 | | |
2089 | | /* The time in milliseconds. |
2090 | | * Used for tickets to represent difference between when first seen and when |
2091 | | * sending. |
2092 | | * |
2093 | | * returns the time in milliseconds as a 64-bit value. |
2094 | | */ |
2095 | | sword64 TimeNowInMilliseconds(void) |
2096 | | { |
2097 | | return (sword64)rtp_get_system_sec() * 1000; |
2098 | | } |
2099 | | #elif defined(WOLFSSL_DEOS) |
2100 | | sword64 TimeNowInMilliseconds(void) |
2101 | | { |
2102 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2103 | | word32 *systemTickPtr = systemTickPointer(); |
2104 | | |
2105 | | return (sword64) (*systemTickPtr/systemTickTimeInHz) * 1000; |
2106 | | } |
2107 | | #elif defined(MICRIUM) |
2108 | | /* The time in milliseconds. |
2109 | | * Used for tickets to represent difference between when first seen and when |
2110 | | * sending. |
2111 | | * |
2112 | | * returns the time in milliseconds as a 64-bit value. |
2113 | | */ |
2114 | | sword64 TimeNowInMilliseconds(void) |
2115 | | { |
2116 | | OS_TICK ticks = 0; |
2117 | | OS_ERR err; |
2118 | | |
2119 | | ticks = OSTimeGet(&err); |
2120 | | |
2121 | | return (sword64) (ticks / OSCfg_TickRate_Hz) * 1000; |
2122 | | } |
2123 | | #elif defined(MICROCHIP_TCPIP_V5) |
2124 | | /* The time in milliseconds. |
2125 | | * Used for tickets to represent difference between when first seen and when |
2126 | | * sending. |
2127 | | * |
2128 | | * returns the time in milliseconds as a 64-bit value. |
2129 | | */ |
2130 | | sword64 TimeNowInMilliseconds(void) |
2131 | | { |
2132 | | return (sword64) (TickGet() / (TICKS_PER_SECOND / 1000)); |
2133 | | } |
2134 | | #elif defined(MICROCHIP_TCPIP) |
2135 | | #if defined(MICROCHIP_MPLAB_HARMONY) |
2136 | | #include <system/tmr/sys_tmr.h> |
2137 | | |
2138 | | /* The time in milliseconds. |
2139 | | * Used for tickets to represent difference between when first seen and when |
2140 | | * sending. |
2141 | | * |
2142 | | * returns the time in milliseconds as a 64-bit value. |
2143 | | */ |
2144 | | sword64 TimeNowInMilliseconds(void) |
2145 | | { |
2146 | | return (sword64)SYS_TMR_TickCountGet() / |
2147 | | (SYS_TMR_TickCounterFrequencyGet() / 1000); |
2148 | | } |
2149 | | #else |
2150 | | /* The time in milliseconds. |
2151 | | * Used for tickets to represent difference between when first seen and when |
2152 | | * sending. |
2153 | | * |
2154 | | * returns the time in milliseconds as a 64-bit value. |
2155 | | */ |
2156 | | sword64 TimeNowInMilliseconds(void) |
2157 | | { |
2158 | | return (sword64)SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000); |
2159 | | } |
2160 | | |
2161 | | #endif |
2162 | | |
2163 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2164 | | /* The time in milliseconds. |
2165 | | * Used for tickets to represent difference between when first seen and when |
2166 | | * sending. |
2167 | | * |
2168 | | * returns the time in milliseconds as a 64-bit value. |
2169 | | */ |
2170 | | sword64 TimeNowInMilliseconds(void) |
2171 | | { |
2172 | | TIME_STRUCT mqxTime; |
2173 | | |
2174 | | _time_get_elapsed(&mqxTime); |
2175 | | |
2176 | | return (sword64) mqxTime.SECONDS * 1000; |
2177 | | } |
2178 | | #elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS) |
2179 | | #include "include/task.h" |
2180 | | |
2181 | | /* The time in milliseconds. |
2182 | | * Used for tickets to represent difference between when first seen and when |
2183 | | * sending. |
2184 | | * |
2185 | | * returns the time in milliseconds as a 64-bit value. |
2186 | | */ |
2187 | | sword64 TimeNowInMilliseconds(void) |
2188 | | { |
2189 | | return (sword64)xTaskGetTickCount() / (configTICK_RATE_HZ / 1000); |
2190 | | } |
2191 | | #elif defined(FREESCALE_KSDK_BM) |
2192 | | #include "lwip/sys.h" /* lwIP */ |
2193 | | |
2194 | | /* The time in milliseconds. |
2195 | | * Used for tickets to represent difference between when first seen and when |
2196 | | * sending. |
2197 | | * |
2198 | | * returns the time in milliseconds as a 64-bit value. |
2199 | | */ |
2200 | | sword64 TimeNowInMilliseconds(void) |
2201 | | { |
2202 | | return sys_now(); |
2203 | | } |
2204 | | |
2205 | | #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2) |
2206 | | |
2207 | | sword64 TimeNowInMilliseconds(void) |
2208 | | { |
2209 | | return (sword64)osKernelGetTickCount(); |
2210 | | } |
2211 | | |
2212 | | #elif defined(WOLFSSL_TIRTOS) |
2213 | | /* The time in milliseconds. |
2214 | | * Used for tickets to represent difference between when first seen and when |
2215 | | * sending. |
2216 | | * |
2217 | | * returns the time in milliseconds as a 64-bit value. |
2218 | | */ |
2219 | | sword64 TimeNowInMilliseconds(void) |
2220 | | { |
2221 | | return (sword64) Seconds_get() * 1000; |
2222 | | } |
2223 | | #elif defined(WOLFSSL_UTASKER) |
2224 | | /* The time in milliseconds. |
2225 | | * Used for tickets to represent difference between when first seen and when |
2226 | | * sending. |
2227 | | * |
2228 | | * returns the time in milliseconds as a 64-bit value. |
2229 | | */ |
2230 | | sword64 TimeNowInMilliseconds(void) |
2231 | | { |
2232 | | return (sword64)(uTaskerSystemTick / (TICK_RESOLUTION / 1000)); |
2233 | | } |
2234 | | #elif defined(WOLFSSL_LINUXKM) |
2235 | | sword64 TimeNowInMilliseconds(void) |
2236 | | { |
2237 | | s64 t; |
2238 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2239 | | struct timespec ts; |
2240 | | getnstimeofday(&ts); |
2241 | | t = ts.tv_sec * (s64)1000; |
2242 | | t += ts.tv_nsec / (s64)1000000; |
2243 | | #else |
2244 | | struct timespec64 ts; |
2245 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2246 | | ts = current_kernel_time64(); |
2247 | | #else |
2248 | | ktime_get_coarse_real_ts64(&ts); |
2249 | | #endif |
2250 | | t = ts.tv_sec * 1000L; |
2251 | | t += ts.tv_nsec / 1000000L; |
2252 | | #endif |
2253 | | return (sword64)t; |
2254 | | } |
2255 | | #elif defined(WOLFSSL_QNX_CAAM) |
2256 | | sword64 TimeNowInMilliseconds(void) |
2257 | | { |
2258 | | struct timespec now; |
2259 | | clock_gettime(CLOCK_REALTIME, &now); |
2260 | | return (sword64)(now.tv_sec * 1000 + now.tv_nsec / 1000000); |
2261 | | } |
2262 | | #elif defined(FUSION_RTOS) |
2263 | | /* The time in milliseconds. |
2264 | | * Used for tickets to represent difference between when first seen and when |
2265 | | * sending. |
2266 | | * |
2267 | | * returns the time in milliseconds as a 64-bit value. |
2268 | | */ |
2269 | | sword64 TimeNowInMilliseconds(void) |
2270 | | { |
2271 | | struct timeval now; |
2272 | | if (FCL_GETTIMEOFDAY(&now, 0) < 0) |
2273 | | return 0; |
2274 | | |
2275 | | /* Convert to milliseconds number. */ |
2276 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2277 | | } |
2278 | | #elif defined(WOLFSSL_ZEPHYR) |
2279 | | sword64 TimeNowInMilliseconds(void) |
2280 | | { |
2281 | | int64_t t; |
2282 | | #if defined(CONFIG_ARCH_POSIX) |
2283 | | k_cpu_idle(); |
2284 | | #endif |
2285 | | t = k_uptime_get(); /* returns current uptime in milliseconds */ |
2286 | | return (sword64)t; |
2287 | | } |
2288 | | #elif defined(FREERTOS) |
2289 | | sword64 TimeNowInMilliseconds(void) |
2290 | | { |
2291 | | return (sword64)((uint64_t)(xTaskGetTickCount() * 1000) / |
2292 | | configTICK_RATE_HZ); |
2293 | | } |
2294 | | #else |
2295 | | /* The time in milliseconds. |
2296 | | * Used for tickets to represent difference between when first seen and when |
2297 | | * sending. |
2298 | | * |
2299 | | * returns the time in milliseconds as a 64-bit value. |
2300 | | */ |
2301 | | sword64 TimeNowInMilliseconds(void) |
2302 | | { |
2303 | | struct timeval now; |
2304 | | |
2305 | | if (gettimeofday(&now, 0) < 0) |
2306 | | return 0; |
2307 | | |
2308 | | /* Convert to milliseconds number. */ |
2309 | | return (sword64)now.tv_sec * 1000 + now.tv_usec / 1000; |
2310 | | } |
2311 | | #endif |
2312 | | #else |
2313 | | /* user must supply time in milliseconds function: |
2314 | | * sword64 TimeNowInMilliseconds(void); |
2315 | | * The response is milliseconds elapsed |
2316 | | */ |
2317 | | #endif /* !NO_ASN_TIME */ |
2318 | | #endif /* WOLFSSL_32BIT_MILLI_TIME */ |
2319 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK || WOLFSSL_DTLS13 */ |
2320 | | |
2321 | | /* Add record layer header to message. |
2322 | | * |
2323 | | * output The buffer to write the record layer header into. |
2324 | | * length The length of the record data. |
2325 | | * type The type of record message. |
2326 | | * ssl The SSL/TLS object. |
2327 | | */ |
2328 | | static void AddTls13RecordHeader(byte* output, word32 length, byte type, |
2329 | | WOLFSSL* ssl) |
2330 | 0 | { |
2331 | 0 | RecordLayerHeader* rl; |
2332 | |
|
2333 | 0 | rl = (RecordLayerHeader*)output; |
2334 | 0 | rl->type = type; |
2335 | 0 | rl->pvMajor = ssl->version.major; |
2336 | | /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */ |
2337 | 0 | rl->pvMinor = TLSv1_2_MINOR; |
2338 | 0 | c16toa((word16)length, rl->length); |
2339 | 0 | } |
2340 | | |
2341 | | /* Add handshake header to message. |
2342 | | * |
2343 | | * output The buffer to write the handshake header into. |
2344 | | * length The length of the handshake data. |
2345 | | * fragOffset The offset of the fragment data. (DTLS) |
2346 | | * fragLength The length of the fragment data. (DTLS) |
2347 | | * type The type of handshake message. |
2348 | | * ssl The SSL/TLS object. (DTLS) |
2349 | | */ |
2350 | | static void AddTls13HandShakeHeader(byte* output, word32 length, |
2351 | | word32 fragOffset, word32 fragLength, |
2352 | | byte type, WOLFSSL* ssl) |
2353 | 0 | { |
2354 | 0 | HandShakeHeader* hs; |
2355 | 0 | (void)fragOffset; |
2356 | 0 | (void)fragLength; |
2357 | 0 | (void)ssl; |
2358 | |
|
2359 | | #ifdef WOLFSSL_DTLS13 |
2360 | | /* message_hash type is used for a synthetic message that replaces the first |
2361 | | ClientHello in the hash transcript when using HelloRetryRequest. It will |
2362 | | never be transmitted and, as the DTLS-only fields must not be considered |
2363 | | when computing the hash transcript, we can avoid to use the DTLS |
2364 | | handshake header. */ |
2365 | | if (ssl->options.dtls && type != message_hash) { |
2366 | | Dtls13HandshakeAddHeader(ssl, output, (enum HandShakeType)type, length); |
2367 | | return; |
2368 | | } |
2369 | | #endif /* WOLFSSL_DTLS13 */ |
2370 | | |
2371 | | /* handshake header */ |
2372 | 0 | hs = (HandShakeHeader*)output; |
2373 | 0 | hs->type = type; |
2374 | 0 | c32to24(length, hs->length); |
2375 | 0 | } |
2376 | | |
2377 | | |
2378 | | /* Add both record layer and handshake header to message. |
2379 | | * |
2380 | | * output The buffer to write the headers into. |
2381 | | * length The length of the handshake data. |
2382 | | * type The type of record layer message. |
2383 | | * ssl The SSL/TLS object. (DTLS) |
2384 | | */ |
2385 | | static void AddTls13Headers(byte* output, word32 length, byte type, |
2386 | | WOLFSSL* ssl) |
2387 | 0 | { |
2388 | 0 | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2389 | 0 | word32 outputAdj = RECORD_HEADER_SZ; |
2390 | |
|
2391 | | #ifdef WOLFSSL_DTLS13 |
2392 | | if (ssl->options.dtls) { |
2393 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2394 | | return; |
2395 | | } |
2396 | | #endif /* WOLFSSL_DTLS13 */ |
2397 | |
|
2398 | 0 | AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl); |
2399 | 0 | AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl); |
2400 | 0 | } |
2401 | | |
2402 | | #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) \ |
2403 | | && !defined(NO_CERTS) |
2404 | | /* Add both record layer and fragment handshake header to message. |
2405 | | * |
2406 | | * output The buffer to write the headers into. |
2407 | | * fragOffset The offset of the fragment data. (DTLS) |
2408 | | * fragLength The length of the fragment data. (DTLS) |
2409 | | * length The length of the handshake data. |
2410 | | * type The type of record layer message. |
2411 | | * ssl The SSL/TLS object. (DTLS) |
2412 | | */ |
2413 | | static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset, |
2414 | | word32 length, byte type, WOLFSSL* ssl) |
2415 | 0 | { |
2416 | 0 | word32 lengthAdj = HANDSHAKE_HEADER_SZ; |
2417 | 0 | word32 outputAdj = RECORD_HEADER_SZ; |
2418 | 0 | (void)fragSz; |
2419 | |
|
2420 | | #ifdef WOLFSSL_DTLS13 |
2421 | | /* we ignore fragmentation fields here because fragmentation logic for |
2422 | | DTLS1.3 is inside dtls13_handshake_send(). */ |
2423 | | if (ssl->options.dtls) { |
2424 | | Dtls13AddHeaders(output, length, (enum HandShakeType)type, ssl); |
2425 | | return; |
2426 | | } |
2427 | | #endif /* WOLFSSL_DTLS13 */ |
2428 | |
|
2429 | 0 | AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl); |
2430 | 0 | AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz, |
2431 | 0 | type, ssl); |
2432 | 0 | } |
2433 | | #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !NO_CERTS */ |
2434 | | |
2435 | | /* Write the sequence number into the buffer. |
2436 | | * No DTLS v1.3 support. |
2437 | | * |
2438 | | * ssl The SSL/TLS object. |
2439 | | * verifyOrder Which set of sequence numbers to use. |
2440 | | * out The buffer to write into. |
2441 | | */ |
2442 | | static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out) |
2443 | 0 | { |
2444 | 0 | word32 seq[2] = {0, 0}; |
2445 | |
|
2446 | 0 | if (ssl->options.dtls) { |
2447 | | #ifdef WOLFSSL_DTLS13 |
2448 | | Dtls13GetSeq(ssl, verifyOrder, seq, 1); |
2449 | | #endif /* WOLFSSL_DTLS13 */ |
2450 | 0 | } |
2451 | 0 | else if (verifyOrder == PEER_ORDER) { |
2452 | 0 | seq[0] = ssl->keys.peer_sequence_number_hi; |
2453 | 0 | seq[1] = ssl->keys.peer_sequence_number_lo++; |
2454 | | /* handle rollover */ |
2455 | 0 | if (seq[1] > ssl->keys.peer_sequence_number_lo) |
2456 | 0 | ssl->keys.peer_sequence_number_hi++; |
2457 | 0 | } |
2458 | 0 | else { |
2459 | 0 | seq[0] = ssl->keys.sequence_number_hi; |
2460 | 0 | seq[1] = ssl->keys.sequence_number_lo++; |
2461 | | /* handle rollover */ |
2462 | 0 | if (seq[1] > ssl->keys.sequence_number_lo) |
2463 | 0 | ssl->keys.sequence_number_hi++; |
2464 | 0 | } |
2465 | | #ifdef WOLFSSL_DEBUG_TLS |
2466 | | WOLFSSL_MSG_EX("TLS 1.3 Write Sequence %d %d", seq[0], seq[1]); |
2467 | | #endif |
2468 | |
|
2469 | 0 | c32toa(seq[0], out); |
2470 | 0 | c32toa(seq[1], out + OPAQUE32_LEN); |
2471 | 0 | } |
2472 | | |
2473 | | /* Build the nonce for TLS v1.3 encryption and decryption. |
2474 | | * |
2475 | | * ssl The SSL/TLS object. |
2476 | | * nonce The nonce data to use when encrypting or decrypting. |
2477 | | * iv The derived IV. |
2478 | | * order The side on which the message is to be or was sent. |
2479 | | */ |
2480 | | static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv, |
2481 | | int ivSz, int order) |
2482 | 0 | { |
2483 | 0 | int seq_offset; |
2484 | | /* Ensure minimum nonce size for standard AEAD ciphers */ |
2485 | 0 | if (ivSz < AEAD_NONCE_SZ) |
2486 | 0 | ivSz = AEAD_NONCE_SZ; |
2487 | 0 | seq_offset = ivSz - SEQ_SZ; |
2488 | | /* The nonce is the IV with the sequence XORed into the last bytes. */ |
2489 | 0 | WriteSEQTls13(ssl, order, nonce + seq_offset); |
2490 | 0 | XMEMCPY(nonce, iv, seq_offset); |
2491 | 0 | xorbuf(nonce + seq_offset, iv + seq_offset, SEQ_SZ); |
2492 | 0 | } |
2493 | | |
2494 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2495 | | /* Encrypt with ChaCha20 and create authentication tag with Poly1305. |
2496 | | * |
2497 | | * ssl The SSL/TLS object. |
2498 | | * output The buffer to write encrypted data and authentication tag into. |
2499 | | * May be the same pointer as input. |
2500 | | * input The data to encrypt. |
2501 | | * sz The number of bytes to encrypt. |
2502 | | * nonce The nonce to use with ChaCha20. |
2503 | | * aad The additional authentication data. |
2504 | | * aadSz The size of the addition authentication data. |
2505 | | * tag The authentication tag buffer. |
2506 | | * returns 0 on success, otherwise failure. |
2507 | | */ |
2508 | | static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output, |
2509 | | const byte* input, word16 sz, byte* nonce, |
2510 | | const byte* aad, word16 aadSz, byte* tag) |
2511 | 0 | { |
2512 | 0 | int ret = 0; |
2513 | 0 | byte poly[CHACHA20_256_KEY_SIZE]; |
2514 | | |
2515 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2516 | 0 | XMEMSET(poly, 0, sizeof(poly)); |
2517 | | |
2518 | | /* Set the nonce for ChaCha and get Poly1305 key. */ |
2519 | 0 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0); |
2520 | 0 | if (ret != 0) |
2521 | 0 | return ret; |
2522 | | /* Create Poly1305 key using ChaCha20 keystream. */ |
2523 | 0 | ret = wc_Chacha_Process(ssl->encrypt.chacha, poly, poly, sizeof(poly)); |
2524 | 0 | if (ret != 0) |
2525 | 0 | return ret; |
2526 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2527 | | wc_MemZero_Add("ChaCha20Poly1305_Encrypt poly", poly, sizeof(poly)); |
2528 | | #endif |
2529 | 0 | ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1); |
2530 | 0 | if (ret != 0) |
2531 | 0 | return ret; |
2532 | | /* Encrypt the plain text. */ |
2533 | 0 | ret = wc_Chacha_Process(ssl->encrypt.chacha, output, input, sz); |
2534 | 0 | if (ret != 0) { |
2535 | 0 | ForceZero(poly, sizeof(poly)); |
2536 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2537 | | wc_MemZero_Check(poly, sizeof(poly)); |
2538 | | #endif |
2539 | 0 | return ret; |
2540 | 0 | } |
2541 | | |
2542 | | /* Set key for Poly1305. */ |
2543 | 0 | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2544 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2545 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2546 | | wc_MemZero_Check(poly, sizeof(poly)); |
2547 | | #endif |
2548 | 0 | if (ret != 0) |
2549 | 0 | return ret; |
2550 | | /* Add authentication code of encrypted data to end. */ |
2551 | 0 | ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, output, sz, tag, |
2552 | 0 | POLY1305_AUTH_SZ); |
2553 | |
|
2554 | 0 | return ret; |
2555 | 0 | } |
2556 | | #endif |
2557 | | |
2558 | | #ifdef HAVE_NULL_CIPHER |
2559 | | /* Create authentication tag and copy data over input. |
2560 | | * |
2561 | | * ssl The SSL/TLS object. |
2562 | | * output The buffer to copy data into. |
2563 | | * May be the same pointer as input. |
2564 | | * input The data. |
2565 | | * sz The number of bytes of data. |
2566 | | * nonce The nonce to use with authentication. |
2567 | | * aad The additional authentication data. |
2568 | | * aadSz The size of the addition authentication data. |
2569 | | * tag The authentication tag buffer. |
2570 | | * returns 0 on success, otherwise failure. |
2571 | | */ |
2572 | | static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output, |
2573 | | const byte* input, word16 sz, |
2574 | | const byte* nonce, |
2575 | | const byte* aad, word16 aadSz, byte* tag) |
2576 | | { |
2577 | | int ret; |
2578 | | |
2579 | | /* HMAC: nonce | aad | input */ |
2580 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, ssl->specs.iv_size); |
2581 | | if (ret == 0) |
2582 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz); |
2583 | | if (ret == 0) |
2584 | | ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz); |
2585 | | if (ret == 0) |
2586 | | ret = wc_HmacFinal(ssl->encrypt.hmac, tag); |
2587 | | /* Copy the input to output if not the same buffer */ |
2588 | | if (ret == 0 && output != input) |
2589 | | XMEMCPY(output, input, sz); |
2590 | | return ret; |
2591 | | } |
2592 | | #endif |
2593 | | |
2594 | | /* Encrypt data for TLS v1.3. |
2595 | | * |
2596 | | * ssl The SSL/TLS object. |
2597 | | * output The buffer to write encrypted data and authentication tag into. |
2598 | | * May be the same pointer as input. |
2599 | | * input The record header and data to encrypt. |
2600 | | * sz The number of bytes to encrypt. |
2601 | | * aad The additional authentication data. |
2602 | | * aadSz The size of the addition authentication data. |
2603 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
2604 | | * returns 0 on success, otherwise failure. |
2605 | | */ |
2606 | | static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input, |
2607 | | word16 sz, const byte* aad, word16 aadSz, int asyncOkay) |
2608 | 0 | { |
2609 | 0 | int ret = 0; |
2610 | 0 | word16 dataSz; |
2611 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
2612 | 0 | word32 nonceSz = 0; |
2613 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2614 | | WC_ASYNC_DEV* asyncDev = NULL; |
2615 | | word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN; |
2616 | | #endif |
2617 | |
|
2618 | 0 | WOLFSSL_ENTER("EncryptTls13"); |
2619 | 0 | if (sz < ssl->specs.aead_mac_size) |
2620 | 0 | return BUFFER_E; |
2621 | 0 | dataSz = sz - ssl->specs.aead_mac_size; |
2622 | |
|
2623 | 0 | (void)output; |
2624 | 0 | (void)input; |
2625 | 0 | (void)sz; |
2626 | 0 | (void)dataSz; |
2627 | 0 | (void)macSz; |
2628 | 0 | (void)asyncOkay; |
2629 | 0 | (void)nonceSz; |
2630 | |
|
2631 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2632 | | if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2633 | | ssl->error = 0; /* clear async */ |
2634 | | } |
2635 | | #endif |
2636 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
2637 | | ret = tsip_Tls13AesEncrypt(ssl, output, input, dataSz); |
2638 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
2639 | | if (ret > 0) { |
2640 | | ret = 0; /* tsip_Tls13AesEncrypt returns output size */ |
2641 | | } |
2642 | | return ret; |
2643 | | } |
2644 | | ret = 0; |
2645 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
2646 | |
|
2647 | 0 | switch (ssl->encrypt.state) { |
2648 | 0 | case CIPHER_STATE_BEGIN: |
2649 | 0 | { |
2650 | | #ifdef WOLFSSL_DEBUG_TLS |
2651 | | WOLFSSL_MSG("Data to encrypt"); |
2652 | | WOLFSSL_BUFFER(input, dataSz); |
2653 | | WOLFSSL_MSG("Additional Authentication Data"); |
2654 | | WOLFSSL_BUFFER(aad, aadSz); |
2655 | | #endif |
2656 | |
|
2657 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2658 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2659 | | dataSz >= sizeof(ssl->encrypt.sanityCheck)) { |
2660 | | XMEMCPY(ssl->encrypt.sanityCheck, input, |
2661 | | sizeof(ssl->encrypt.sanityCheck)); |
2662 | | } |
2663 | | #endif |
2664 | |
|
2665 | 0 | #ifdef CIPHER_NONCE |
2666 | 0 | if (ssl->encrypt.nonce == NULL) { |
2667 | 0 | ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
2668 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
2669 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2670 | | if (ssl->encrypt.nonce != NULL) { |
2671 | | wc_MemZero_Add("EncryptTls13 nonce", ssl->encrypt.nonce, |
2672 | | ssl->specs.iv_size); |
2673 | | } |
2674 | | #endif |
2675 | 0 | } |
2676 | 0 | if (ssl->encrypt.nonce == NULL) |
2677 | 0 | return MEMORY_E; |
2678 | | |
2679 | 0 | BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV, |
2680 | 0 | ssl->specs.iv_size, CUR_ORDER); |
2681 | 0 | #endif |
2682 | | |
2683 | | /* Advance state and proceed */ |
2684 | 0 | ssl->encrypt.state = CIPHER_STATE_DO; |
2685 | 0 | } |
2686 | 0 | FALL_THROUGH; |
2687 | |
|
2688 | 0 | case CIPHER_STATE_DO: |
2689 | 0 | { |
2690 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
2691 | 0 | #ifdef BUILD_AESGCM |
2692 | 0 | case wolfssl_aes_gcm: |
2693 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2694 | | /* initialize event */ |
2695 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2696 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2697 | | if (ret != 0) |
2698 | | break; |
2699 | | #endif |
2700 | |
|
2701 | 0 | nonceSz = AESGCM_NONCE_SZ; |
2702 | |
|
2703 | | #if defined(HAVE_PK_CALLBACKS) |
2704 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2705 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2706 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2707 | | output, input, dataSz, |
2708 | | ssl->encrypt.nonce, nonceSz, |
2709 | | output + dataSz, macSz, |
2710 | | aad, aadSz); |
2711 | | } |
2712 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2713 | | #endif |
2714 | 0 | { |
2715 | |
|
2716 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2717 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2718 | | ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input, |
2719 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2720 | | output + dataSz, macSz, aad, aadSz); |
2721 | | #else |
2722 | 0 | ret = wc_AesGcmSetExtIV(ssl->encrypt.aes, |
2723 | 0 | ssl->encrypt.nonce, nonceSz); |
2724 | 0 | if (ret == 0) { |
2725 | 0 | ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output, |
2726 | 0 | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2727 | 0 | output + dataSz, macSz, aad, aadSz); |
2728 | 0 | } |
2729 | 0 | #endif |
2730 | 0 | } |
2731 | 0 | break; |
2732 | 0 | #endif |
2733 | | |
2734 | | #ifdef HAVE_AESCCM |
2735 | | case wolfssl_aes_ccm: |
2736 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2737 | | /* initialize event */ |
2738 | | asyncDev = &ssl->encrypt.aes->asyncDev; |
2739 | | ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags); |
2740 | | if (ret != 0) |
2741 | | break; |
2742 | | #endif |
2743 | | |
2744 | | nonceSz = AESCCM_NONCE_SZ; |
2745 | | #if defined(HAVE_PK_CALLBACKS) |
2746 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
2747 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
2748 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 1, |
2749 | | output, input, dataSz, |
2750 | | ssl->encrypt.nonce, nonceSz, |
2751 | | output + dataSz, macSz, |
2752 | | aad, aadSz); |
2753 | | } |
2754 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
2755 | | #endif |
2756 | | { |
2757 | | #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \ |
2758 | | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) |
2759 | | ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input, |
2760 | | dataSz, ssl->encrypt.nonce, nonceSz, |
2761 | | output + dataSz, macSz, aad, aadSz); |
2762 | | #else |
2763 | | ret = wc_AesCcmSetNonce(ssl->encrypt.aes, |
2764 | | ssl->encrypt.nonce, nonceSz); |
2765 | | if (ret == 0) { |
2766 | | ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output, |
2767 | | input, dataSz, ssl->encrypt.nonce, nonceSz, |
2768 | | output + dataSz, macSz, aad, aadSz); |
2769 | | } |
2770 | | #endif |
2771 | | } |
2772 | | break; |
2773 | | #endif |
2774 | | |
2775 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2776 | 0 | case wolfssl_chacha: |
2777 | 0 | ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz, |
2778 | 0 | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2779 | 0 | break; |
2780 | 0 | #endif |
2781 | | |
2782 | | #ifdef WOLFSSL_SM4_GCM |
2783 | | case wolfssl_sm4_gcm: |
2784 | | nonceSz = SM4_GCM_NONCE_SZ; |
2785 | | ret = wc_Sm4GcmEncrypt(ssl->encrypt.sm4, output, input, |
2786 | | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2787 | | macSz, aad, aadSz); |
2788 | | break; |
2789 | | #endif |
2790 | | |
2791 | | #ifdef WOLFSSL_SM4_CCM |
2792 | | case wolfssl_sm4_ccm: |
2793 | | nonceSz = SM4_CCM_NONCE_SZ; |
2794 | | ret = wc_Sm4CcmEncrypt(ssl->encrypt.sm4, output, input, |
2795 | | dataSz, ssl->encrypt.nonce, nonceSz, output + dataSz, |
2796 | | macSz, aad, aadSz); |
2797 | | break; |
2798 | | #endif |
2799 | | |
2800 | | #ifdef HAVE_NULL_CIPHER |
2801 | | case wolfssl_cipher_null: |
2802 | | ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz, |
2803 | | ssl->encrypt.nonce, aad, aadSz, output + dataSz); |
2804 | | break; |
2805 | | #endif |
2806 | | |
2807 | 0 | default: |
2808 | 0 | WOLFSSL_MSG("wolfSSL Encrypt programming error"); |
2809 | 0 | return ENCRYPT_ERROR; |
2810 | 0 | } |
2811 | | |
2812 | | /* Advance state */ |
2813 | 0 | ssl->encrypt.state = CIPHER_STATE_END; |
2814 | |
|
2815 | | #ifdef WOLFSSL_ASYNC_CRYPT |
2816 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
2817 | | /* if async is not okay, then block */ |
2818 | | if (!asyncOkay) { |
2819 | | ret = wc_AsyncWait(ret, asyncDev, event_flags); |
2820 | | } |
2821 | | else { |
2822 | | /* If pending, then leave and return will resume below */ |
2823 | | return wolfSSL_AsyncPush(ssl, asyncDev); |
2824 | | } |
2825 | | } |
2826 | | #endif |
2827 | 0 | } |
2828 | 0 | FALL_THROUGH; |
2829 | |
|
2830 | 0 | case CIPHER_STATE_END: |
2831 | 0 | { |
2832 | | #ifdef WOLFSSL_DEBUG_TLS |
2833 | | #ifdef CIPHER_NONCE |
2834 | | WOLFSSL_MSG("Nonce"); |
2835 | | WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size); |
2836 | | #endif |
2837 | | WOLFSSL_MSG("Encrypted data"); |
2838 | | WOLFSSL_BUFFER(output, dataSz); |
2839 | | WOLFSSL_MSG("Authentication Tag"); |
2840 | | WOLFSSL_BUFFER(output + dataSz, macSz); |
2841 | | #endif |
2842 | |
|
2843 | | #ifdef WOLFSSL_CIPHER_TEXT_CHECK |
2844 | | if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null && |
2845 | | dataSz >= sizeof(ssl->encrypt.sanityCheck) && |
2846 | | XMEMCMP(output, ssl->encrypt.sanityCheck, |
2847 | | sizeof(ssl->encrypt.sanityCheck)) == 0) { |
2848 | | |
2849 | | WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?"); |
2850 | | return ENCRYPT_ERROR; |
2851 | | } |
2852 | | ForceZero(ssl->encrypt.sanityCheck, |
2853 | | sizeof(ssl->encrypt.sanityCheck)); |
2854 | | #endif |
2855 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2856 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
2857 | | (output != input) && (ret == 0)) { |
2858 | | wc_MemZero_Add("TLS 1.3 Encrypt plaintext", input, sz); |
2859 | | } |
2860 | | #endif |
2861 | |
|
2862 | 0 | #ifdef CIPHER_NONCE |
2863 | 0 | ForceZero(ssl->encrypt.nonce, ssl->specs.iv_size); |
2864 | 0 | #endif |
2865 | |
|
2866 | 0 | break; |
2867 | 0 | } |
2868 | | |
2869 | 0 | default: |
2870 | 0 | break; |
2871 | 0 | } |
2872 | | |
2873 | | |
2874 | | /* Reset state */ |
2875 | 0 | ssl->encrypt.state = CIPHER_STATE_BEGIN; |
2876 | |
|
2877 | 0 | return ret; |
2878 | 0 | } |
2879 | | |
2880 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2881 | | /* Decrypt with ChaCha20 and check authentication tag with Poly1305. |
2882 | | * |
2883 | | * ssl The SSL/TLS object. |
2884 | | * output The buffer to write decrypted data into. |
2885 | | * May be the same pointer as input. |
2886 | | * input The data to decrypt. |
2887 | | * sz The number of bytes to decrypt. |
2888 | | * nonce The nonce to use with ChaCha20. |
2889 | | * aad The additional authentication data. |
2890 | | * aadSz The size of the addition authentication data. |
2891 | | * tagIn The authentication tag data from packet. |
2892 | | * returns 0 on success, otherwise failure. |
2893 | | */ |
2894 | | static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output, |
2895 | | const byte* input, word16 sz, byte* nonce, |
2896 | | const byte* aad, word16 aadSz, |
2897 | | const byte* tagIn) |
2898 | 0 | { |
2899 | 0 | int ret; |
2900 | 0 | byte tag[POLY1305_AUTH_SZ]; |
2901 | 0 | byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */ |
2902 | | |
2903 | | /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */ |
2904 | 0 | XMEMSET(poly, 0, sizeof(poly)); |
2905 | | |
2906 | | /* Set nonce and get Poly1305 key. */ |
2907 | 0 | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0); |
2908 | 0 | if (ret != 0) |
2909 | 0 | return ret; |
2910 | | /* Use ChaCha20 keystream to get Poly1305 key for tag. */ |
2911 | 0 | ret = wc_Chacha_Process(ssl->decrypt.chacha, poly, poly, sizeof(poly)); |
2912 | 0 | if (ret != 0) |
2913 | 0 | return ret; |
2914 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2915 | | wc_MemZero_Add("ChaCha20Poly1305_Decrypt poly", poly, sizeof(poly)); |
2916 | | #endif |
2917 | 0 | ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1); |
2918 | 0 | if (ret != 0) { |
2919 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2920 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2921 | | wc_MemZero_Check(poly, sizeof(poly)); |
2922 | | #endif |
2923 | 0 | return ret; |
2924 | 0 | } |
2925 | | |
2926 | | /* Set key for Poly1305. */ |
2927 | 0 | ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly)); |
2928 | 0 | ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */ |
2929 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2930 | | wc_MemZero_Check(poly, sizeof(poly)); |
2931 | | #endif |
2932 | 0 | if (ret != 0) |
2933 | 0 | return ret; |
2934 | | /* Generate authentication tag for encrypted data. */ |
2935 | 0 | if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, aad, aadSz, input, sz, tag, |
2936 | 0 | sizeof(tag))) != 0) { |
2937 | 0 | return ret; |
2938 | 0 | } |
2939 | | |
2940 | | /* Check tag sent along with packet. */ |
2941 | 0 | if (ConstantCompare(tagIn, tag, POLY1305_AUTH_SZ) != 0) { |
2942 | 0 | WOLFSSL_MSG("MAC did not match"); |
2943 | 0 | return VERIFY_MAC_ERROR; |
2944 | 0 | } |
2945 | | |
2946 | | /* If the tag was good decrypt message. */ |
2947 | 0 | ret = wc_Chacha_Process(ssl->decrypt.chacha, output, input, sz); |
2948 | |
|
2949 | 0 | return ret; |
2950 | 0 | } |
2951 | | #endif |
2952 | | |
2953 | | #ifdef HAVE_NULL_CIPHER |
2954 | | /* Check HMAC tag and copy over input. |
2955 | | * |
2956 | | * ssl The SSL/TLS object. |
2957 | | * output The buffer to copy data into. |
2958 | | * May be the same pointer as input. |
2959 | | * input The data. |
2960 | | * sz The number of bytes of data. |
2961 | | * nonce The nonce to use with authentication. |
2962 | | * aad The additional authentication data. |
2963 | | * aadSz The size of the addition authentication data. |
2964 | | * tagIn The authentication tag data from packet. |
2965 | | * returns 0 on success, otherwise failure. |
2966 | | */ |
2967 | | static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output, |
2968 | | const byte* input, word16 sz, |
2969 | | const byte* nonce, |
2970 | | const byte* aad, word16 aadSz, |
2971 | | const byte* tagIn) |
2972 | | { |
2973 | | int ret; |
2974 | | byte hmac[WC_MAX_DIGEST_SIZE]; |
2975 | | |
2976 | | /* HMAC: nonce | aad | input */ |
2977 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, ssl->specs.iv_size); |
2978 | | if (ret == 0) |
2979 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz); |
2980 | | if (ret == 0) |
2981 | | ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz); |
2982 | | if (ret == 0) |
2983 | | ret = wc_HmacFinal(ssl->decrypt.hmac, hmac); |
2984 | | /* Check authentication tag matches */ |
2985 | | if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0) |
2986 | | ret = DECRYPT_ERROR; |
2987 | | /* Copy the input to output if not the same buffer */ |
2988 | | if (ret == 0 && output != input) |
2989 | | XMEMCPY(output, input, sz); |
2990 | | ForceZero(hmac, sizeof(hmac)); |
2991 | | return ret; |
2992 | | } |
2993 | | #endif |
2994 | | |
2995 | | /* Decrypt data for TLS v1.3. |
2996 | | * |
2997 | | * ssl The SSL/TLS object. |
2998 | | * output The buffer to write decrypted data into. |
2999 | | * May be the same pointer as input. |
3000 | | * input The data to decrypt and authentication tag. |
3001 | | * sz The length of the encrypted data plus authentication tag. |
3002 | | * aad The additional authentication data. |
3003 | | * aadSz The size of the addition authentication data. |
3004 | | * returns 0 on success, otherwise failure. |
3005 | | */ |
3006 | | int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz, |
3007 | | const byte* aad, word16 aadSz) |
3008 | 0 | { |
3009 | 0 | int ret = 0; |
3010 | 0 | word16 dataSz; |
3011 | 0 | word16 macSz = ssl->specs.aead_mac_size; |
3012 | 0 | word32 nonceSz = 0; |
3013 | |
|
3014 | 0 | WOLFSSL_ENTER("DecryptTls13"); |
3015 | 0 | if (sz < ssl->specs.aead_mac_size) { |
3016 | 0 | return BAD_FUNC_ARG; |
3017 | 0 | } |
3018 | 0 | dataSz = sz - ssl->specs.aead_mac_size; |
3019 | |
|
3020 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
3021 | | ret = tsip_Tls13AesDecrypt(ssl, output, input, sz); |
3022 | | |
3023 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
3024 | | #ifndef WOLFSSL_EARLY_DATA |
3025 | | if (ret < 0) { |
3026 | | ret = VERIFY_MAC_ERROR; |
3027 | | WOLFSSL_ERROR_VERBOSE(ret); |
3028 | | } |
3029 | | #endif |
3030 | | return ret; |
3031 | | } |
3032 | | #endif |
3033 | |
|
3034 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3035 | | ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state); |
3036 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3037 | | /* check for still pending */ |
3038 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
3039 | | return ret; |
3040 | | |
3041 | | ssl->error = 0; /* clear async */ |
3042 | | |
3043 | | /* let failures through so CIPHER_STATE_END logic is run */ |
3044 | | } |
3045 | | else |
3046 | | #endif |
3047 | 0 | { |
3048 | | /* Reset state */ |
3049 | 0 | ret = 0; |
3050 | 0 | ssl->decrypt.state = CIPHER_STATE_BEGIN; |
3051 | 0 | } |
3052 | |
|
3053 | 0 | (void)output; |
3054 | 0 | (void)input; |
3055 | 0 | (void)sz; |
3056 | 0 | (void)dataSz; |
3057 | 0 | (void)macSz; |
3058 | 0 | (void)nonceSz; |
3059 | |
|
3060 | 0 | switch (ssl->decrypt.state) { |
3061 | 0 | case CIPHER_STATE_BEGIN: |
3062 | 0 | { |
3063 | | #ifdef WOLFSSL_DEBUG_TLS |
3064 | | WOLFSSL_MSG("Data to decrypt"); |
3065 | | WOLFSSL_BUFFER(input, dataSz); |
3066 | | WOLFSSL_MSG("Additional Authentication Data"); |
3067 | | WOLFSSL_BUFFER(aad, aadSz); |
3068 | | WOLFSSL_MSG("Authentication tag"); |
3069 | | WOLFSSL_BUFFER(input + dataSz, macSz); |
3070 | | #endif |
3071 | |
|
3072 | 0 | #ifdef CIPHER_NONCE |
3073 | 0 | if (ssl->decrypt.nonce == NULL) { |
3074 | 0 | ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_MAX_IMP_SZ, |
3075 | 0 | ssl->heap, DYNAMIC_TYPE_CIPHER); |
3076 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3077 | | if (ssl->decrypt.nonce != NULL) { |
3078 | | wc_MemZero_Add("DecryptTls13 nonce", ssl->decrypt.nonce, |
3079 | | ssl->specs.iv_size); |
3080 | | } |
3081 | | #endif |
3082 | 0 | } |
3083 | 0 | if (ssl->decrypt.nonce == NULL) |
3084 | 0 | return MEMORY_E; |
3085 | | |
3086 | 0 | BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV, |
3087 | 0 | ssl->specs.iv_size, PEER_ORDER); |
3088 | 0 | #endif |
3089 | | |
3090 | | /* Advance state and proceed */ |
3091 | 0 | ssl->decrypt.state = CIPHER_STATE_DO; |
3092 | 0 | } |
3093 | 0 | FALL_THROUGH; |
3094 | |
|
3095 | 0 | case CIPHER_STATE_DO: |
3096 | 0 | { |
3097 | 0 | switch (ssl->specs.bulk_cipher_algorithm) { |
3098 | 0 | #ifdef BUILD_AESGCM |
3099 | 0 | case wolfssl_aes_gcm: |
3100 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3101 | | /* initialize event */ |
3102 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3103 | | WC_ASYNC_FLAG_NONE); |
3104 | | if (ret != 0) |
3105 | | break; |
3106 | | #endif |
3107 | |
|
3108 | 0 | nonceSz = AESGCM_NONCE_SZ; |
3109 | |
|
3110 | | #if defined(HAVE_PK_CALLBACKS) |
3111 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3112 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3113 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3114 | | output, input, dataSz, |
3115 | | ssl->decrypt.nonce, nonceSz, |
3116 | | (byte *)(input + dataSz), macSz, |
3117 | | aad, aadSz); |
3118 | | } |
3119 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3120 | | #endif |
3121 | 0 | { |
3122 | |
|
3123 | 0 | ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input, |
3124 | 0 | dataSz, ssl->decrypt.nonce, nonceSz, |
3125 | 0 | input + dataSz, macSz, aad, aadSz); |
3126 | |
|
3127 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3128 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3129 | | ret = wolfSSL_AsyncPush(ssl, |
3130 | | &ssl->decrypt.aes->asyncDev); |
3131 | | } |
3132 | | #endif |
3133 | |
|
3134 | 0 | } |
3135 | 0 | break; |
3136 | 0 | #endif |
3137 | | |
3138 | | #ifdef HAVE_AESCCM |
3139 | | case wolfssl_aes_ccm: |
3140 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3141 | | /* initialize event */ |
3142 | | ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev, |
3143 | | WC_ASYNC_FLAG_NONE); |
3144 | | if (ret != 0) |
3145 | | break; |
3146 | | #endif |
3147 | | |
3148 | | nonceSz = AESCCM_NONCE_SZ; |
3149 | | #if defined(HAVE_PK_CALLBACKS) |
3150 | | ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN); |
3151 | | if (ssl->ctx && ssl->ctx->PerformTlsRecordProcessingCb) { |
3152 | | ret = ssl->ctx->PerformTlsRecordProcessingCb(ssl, 0, |
3153 | | output, input, dataSz, |
3154 | | ssl->decrypt.nonce, nonceSz, |
3155 | | (byte *)(input + dataSz), macSz, |
3156 | | aad, aadSz); |
3157 | | } |
3158 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) |
3159 | | #endif |
3160 | | { |
3161 | | ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input, |
3162 | | dataSz, ssl->decrypt.nonce, nonceSz, |
3163 | | input + dataSz, macSz, aad, aadSz); |
3164 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3165 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3166 | | ret = wolfSSL_AsyncPush(ssl, |
3167 | | &ssl->decrypt.aes->asyncDev); |
3168 | | } |
3169 | | #endif |
3170 | | } |
3171 | | break; |
3172 | | #endif |
3173 | | |
3174 | 0 | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
3175 | 0 | case wolfssl_chacha: |
3176 | 0 | ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz, |
3177 | 0 | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3178 | 0 | break; |
3179 | 0 | #endif |
3180 | | |
3181 | | #ifdef WOLFSSL_SM4_GCM |
3182 | | case wolfssl_sm4_gcm: |
3183 | | nonceSz = SM4_GCM_NONCE_SZ; |
3184 | | ret = wc_Sm4GcmDecrypt(ssl->decrypt.sm4, output, input, |
3185 | | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3186 | | macSz, aad, aadSz); |
3187 | | break; |
3188 | | #endif |
3189 | | |
3190 | | #ifdef WOLFSSL_SM4_CCM |
3191 | | case wolfssl_sm4_ccm: |
3192 | | nonceSz = SM4_CCM_NONCE_SZ; |
3193 | | ret = wc_Sm4CcmDecrypt(ssl->decrypt.sm4, output, input, |
3194 | | dataSz, ssl->decrypt.nonce, nonceSz, input + dataSz, |
3195 | | macSz, aad, aadSz); |
3196 | | break; |
3197 | | #endif |
3198 | | |
3199 | | #ifdef HAVE_NULL_CIPHER |
3200 | | case wolfssl_cipher_null: |
3201 | | ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz, |
3202 | | ssl->decrypt.nonce, aad, aadSz, input + dataSz); |
3203 | | break; |
3204 | | #endif |
3205 | 0 | default: |
3206 | 0 | WOLFSSL_MSG("wolfSSL Decrypt programming error"); |
3207 | 0 | return DECRYPT_ERROR; |
3208 | 0 | } |
3209 | | |
3210 | | /* Advance state */ |
3211 | 0 | ssl->decrypt.state = CIPHER_STATE_END; |
3212 | |
|
3213 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3214 | | /* If pending, leave now */ |
3215 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3216 | | return ret; |
3217 | | } |
3218 | | #endif |
3219 | 0 | } |
3220 | 0 | FALL_THROUGH; |
3221 | |
|
3222 | 0 | case CIPHER_STATE_END: |
3223 | 0 | { |
3224 | | #ifdef WOLFSSL_DEBUG_TLS |
3225 | | #ifdef CIPHER_NONCE |
3226 | | WOLFSSL_MSG("Nonce"); |
3227 | | WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size); |
3228 | | #endif |
3229 | | WOLFSSL_MSG("Decrypted data"); |
3230 | | WOLFSSL_BUFFER(output, dataSz); |
3231 | | #endif |
3232 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3233 | | if ((ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) && |
3234 | | (ret == 0)) { |
3235 | | wc_MemZero_Add("TLS 1.3 Decrypted data", output, sz); |
3236 | | } |
3237 | | #endif |
3238 | |
|
3239 | 0 | #ifdef CIPHER_NONCE |
3240 | 0 | ForceZero(ssl->decrypt.nonce, ssl->specs.iv_size); |
3241 | 0 | #endif |
3242 | |
|
3243 | 0 | break; |
3244 | 0 | } |
3245 | | |
3246 | 0 | default: |
3247 | 0 | break; |
3248 | 0 | } |
3249 | | |
3250 | 0 | if (ret < 0) { |
3251 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
3252 | 0 | } |
3253 | |
|
3254 | 0 | return ret; |
3255 | 0 | } |
3256 | | |
3257 | | /* Persistable BuildTls13Message arguments */ |
3258 | | typedef struct BuildMsg13Args { |
3259 | | word32 sz; |
3260 | | word32 idx; |
3261 | | word32 headerSz; |
3262 | | word16 size; |
3263 | | word32 paddingSz; |
3264 | | } BuildMsg13Args; |
3265 | | |
3266 | | static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs) |
3267 | 0 | { |
3268 | 0 | BuildMsg13Args* args = (BuildMsg13Args*)pArgs; |
3269 | |
|
3270 | 0 | (void)ssl; |
3271 | 0 | (void)args; |
3272 | | |
3273 | | /* no allocations in BuildTls13Message */ |
3274 | 0 | } |
3275 | | |
3276 | | /* Build SSL Message, encrypted. |
3277 | | * TLS v1.3 encryption is AEAD only. |
3278 | | * |
3279 | | * ssl The SSL/TLS object. |
3280 | | * output The buffer to write record message to. |
3281 | | * outSz Size of the buffer being written into. |
3282 | | * input The record data to encrypt (excluding record header). |
3283 | | * inSz The size of the record data. |
3284 | | * type The recorder header content type. |
3285 | | * hashOutput Whether to hash the unencrypted record data. |
3286 | | * sizeOnly Only want the size of the record message. |
3287 | | * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto |
3288 | | * returns the size of the encrypted record message or negative value on error. |
3289 | | */ |
3290 | | int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input, |
3291 | | int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay) |
3292 | 0 | { |
3293 | 0 | int ret; |
3294 | 0 | BuildMsg13Args* args; |
3295 | 0 | BuildMsg13Args lcl_args; |
3296 | |
|
3297 | 0 | WOLFSSL_ENTER("BuildTls13Message"); |
3298 | |
|
3299 | 0 | if (ssl == NULL) { |
3300 | 0 | return BAD_FUNC_ARG; |
3301 | 0 | } |
3302 | | |
3303 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3304 | | ret = WC_NO_PENDING_E; |
3305 | | if (asyncOkay) { |
3306 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
3307 | | |
3308 | | if (ssl->async == NULL) { |
3309 | | ssl->async = (struct WOLFSSL_ASYNC*) |
3310 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
3311 | | DYNAMIC_TYPE_ASYNC); |
3312 | | if (ssl->async == NULL) |
3313 | | return MEMORY_E; |
3314 | | } |
3315 | | args = (BuildMsg13Args*)ssl->async->args; |
3316 | | |
3317 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState); |
3318 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
3319 | | /* Check for error */ |
3320 | | if (ret < 0) |
3321 | | goto exit_buildmsg; |
3322 | | } |
3323 | | } |
3324 | | else |
3325 | | #endif |
3326 | 0 | { |
3327 | 0 | args = &lcl_args; |
3328 | 0 | } |
3329 | | |
3330 | | /* Reset state */ |
3331 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3332 | | if (ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) |
3333 | | #endif |
3334 | 0 | { |
3335 | 0 | ret = 0; |
3336 | 0 | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3337 | 0 | XMEMSET(args, 0, sizeof(BuildMsg13Args)); |
3338 | |
|
3339 | 0 | args->headerSz = RECORD_HEADER_SZ; |
3340 | | #ifdef WOLFSSL_DTLS13 |
3341 | | if (ssl->options.dtls) |
3342 | | args->headerSz = Dtls13GetRlHeaderLength(ssl, 1); |
3343 | | #endif /* WOLFSSL_DTLS13 */ |
3344 | |
|
3345 | 0 | args->sz = args->headerSz + (word32)inSz; |
3346 | 0 | args->idx = args->headerSz; |
3347 | |
|
3348 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3349 | | if (asyncOkay) |
3350 | | ssl->async->freeArgs = FreeBuildMsg13Args; |
3351 | | #endif |
3352 | 0 | } |
3353 | |
|
3354 | 0 | switch (ssl->options.buildMsgState) { |
3355 | 0 | case BUILD_MSG_BEGIN: |
3356 | 0 | { |
3357 | | /* catch mistaken sizeOnly parameter */ |
3358 | 0 | if (sizeOnly) { |
3359 | 0 | if (output || input) { |
3360 | 0 | WOLFSSL_MSG("BuildTls13Message with sizeOnly " |
3361 | 0 | "doesn't need input or output"); |
3362 | 0 | return BAD_FUNC_ARG; |
3363 | 0 | } |
3364 | 0 | } |
3365 | 0 | else if (output == NULL || input == NULL) { |
3366 | 0 | return BAD_FUNC_ARG; |
3367 | 0 | } |
3368 | | |
3369 | | /* Record layer content type at the end of record data. */ |
3370 | 0 | args->sz++; |
3371 | | /* Authentication data at the end. */ |
3372 | 0 | args->sz += ssl->specs.aead_mac_size; |
3373 | | #ifdef WOLFSSL_DTLS13 |
3374 | | /* Pad to minimum length */ |
3375 | | if (ssl->options.dtls && |
3376 | | args->sz < (word32)Dtls13MinimumRecordLength(ssl)) { |
3377 | | args->paddingSz = Dtls13MinimumRecordLength(ssl) - args->sz; |
3378 | | args->sz = Dtls13MinimumRecordLength(ssl); |
3379 | | } |
3380 | | #endif |
3381 | 0 | if (sizeOnly) |
3382 | 0 | return (int)args->sz; |
3383 | | |
3384 | 0 | if (args->sz > (word32)outSz) { |
3385 | 0 | WOLFSSL_MSG("Oops, want to write past output buffer size"); |
3386 | 0 | return BUFFER_E; |
3387 | 0 | } |
3388 | | |
3389 | | /* Record data length. */ |
3390 | 0 | args->size = (word16)(args->sz - args->headerSz); |
3391 | | /* Write/update the record header with the new size. |
3392 | | * Always have the content type as application data for encrypted |
3393 | | * messages in TLS v1.3. |
3394 | | */ |
3395 | |
|
3396 | 0 | if (ssl->options.dtls) { |
3397 | | #ifdef WOLFSSL_DTLS13 |
3398 | | Dtls13RlAddCiphertextHeader(ssl, output, args->size); |
3399 | | #endif /* WOLFSSL_DTLS13 */ |
3400 | 0 | } |
3401 | 0 | else { |
3402 | 0 | AddTls13RecordHeader(output, args->size, application_data, ssl); |
3403 | 0 | } |
3404 | | |
3405 | | /* TLS v1.3 can do in place encryption. */ |
3406 | 0 | if (input != output + args->idx) |
3407 | 0 | XMEMCPY(output + args->idx, input, (size_t)inSz); |
3408 | 0 | args->idx += (word32)inSz; |
3409 | |
|
3410 | 0 | ssl->options.buildMsgState = BUILD_MSG_HASH; |
3411 | 0 | } |
3412 | 0 | FALL_THROUGH; |
3413 | |
|
3414 | 0 | case BUILD_MSG_HASH: |
3415 | 0 | { |
3416 | 0 | if (hashOutput) { |
3417 | 0 | ret = HashOutput(ssl, output, (int)args->headerSz + inSz, 0); |
3418 | 0 | if (ret != 0) |
3419 | 0 | goto exit_buildmsg; |
3420 | 0 | } |
3421 | | |
3422 | | /* The real record content type goes at the end of the data. */ |
3423 | 0 | output[args->idx++] = (byte)type; |
3424 | | /* Double check that any necessary padding is zero'd out */ |
3425 | 0 | XMEMSET(output + args->idx, 0, args->paddingSz); |
3426 | 0 | args->idx += args->paddingSz; |
3427 | |
|
3428 | 0 | ssl->options.buildMsgState = BUILD_MSG_ENCRYPT; |
3429 | 0 | } |
3430 | 0 | FALL_THROUGH; |
3431 | |
|
3432 | 0 | case BUILD_MSG_ENCRYPT: |
3433 | 0 | { |
3434 | | #ifdef WOLFSSL_QUIC |
3435 | | if (WOLFSSL_IS_QUIC(ssl)) { |
3436 | | /* QUIC does not use encryption of the TLS Record Layer. |
3437 | | * Return the original length + added headers |
3438 | | * and restore it in the record header. */ |
3439 | | AddTls13RecordHeader(output, (word32)inSz, (byte)type, ssl); |
3440 | | ret = (int)args->headerSz + inSz; |
3441 | | goto exit_buildmsg; |
3442 | | } |
3443 | | #endif |
3444 | | #ifdef ATOMIC_USER |
3445 | | if (ssl->ctx->MacEncryptCb) { |
3446 | | /* User Record Layer Callback handling */ |
3447 | | byte* mac = output + args->idx; |
3448 | | output += args->headerSz; |
3449 | | |
3450 | | ret = ssl->ctx->MacEncryptCb(ssl, mac, output, (unsigned int)inSz, (byte)type, 0, |
3451 | | output, output, args->size, ssl->MacEncryptCtx); |
3452 | | } |
3453 | | else |
3454 | | #endif |
3455 | 0 | { |
3456 | 0 | const byte* aad = output; |
3457 | 0 | output += args->headerSz; |
3458 | 0 | ret = EncryptTls13(ssl, output, output, args->size, aad, |
3459 | 0 | (word16)args->headerSz, asyncOkay); |
3460 | 0 | if (ret != 0) { |
3461 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3462 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
3463 | | #endif |
3464 | 0 | { |
3465 | | /* Zeroize plaintext. */ |
3466 | 0 | ForceZero(output, args->size); |
3467 | 0 | } |
3468 | 0 | } |
3469 | | #ifdef WOLFSSL_DTLS13 |
3470 | | if (ret == 0 && ssl->options.dtls) { |
3471 | | /* AAD points to the header. Reuse the variable */ |
3472 | | ret = Dtls13EncryptRecordNumber(ssl, (byte*)aad, |
3473 | | (word16)args->sz); |
3474 | | } |
3475 | | #endif /* WOLFSSL_DTLS13 */ |
3476 | 0 | } |
3477 | 0 | break; |
3478 | 0 | } |
3479 | | |
3480 | 0 | default: |
3481 | 0 | break; |
3482 | 0 | } |
3483 | | |
3484 | 0 | exit_buildmsg: |
3485 | |
|
3486 | 0 | WOLFSSL_LEAVE("BuildTls13Message", ret); |
3487 | |
|
3488 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3489 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
3490 | | return ret; |
3491 | | } |
3492 | | #endif |
3493 | | |
3494 | | /* make sure build message state is reset */ |
3495 | 0 | ssl->options.buildMsgState = BUILD_MSG_BEGIN; |
3496 | | |
3497 | | /* return sz on success */ |
3498 | 0 | if (ret == 0) { |
3499 | 0 | ret = (int)args->sz; |
3500 | 0 | } |
3501 | 0 | else { |
3502 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
3503 | 0 | } |
3504 | | |
3505 | | /* Final cleanup */ |
3506 | | #ifdef WOLFSSL_ASYNC_CRYPT |
3507 | | if (asyncOkay) |
3508 | | FreeAsyncCtx(ssl, 0); |
3509 | | else |
3510 | | #endif |
3511 | 0 | FreeBuildMsg13Args(ssl, args); |
3512 | |
|
3513 | 0 | return ret; |
3514 | 0 | } |
3515 | | |
3516 | | #if !defined(NO_WOLFSSL_CLIENT) || (!defined(NO_WOLFSSL_SERVER) && \ |
3517 | | (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
3518 | | (defined(WOLFSSL_PSK_ONE_ID) || defined(WOLFSSL_PRIORITIZE_PSK))) |
3519 | | /* Find the cipher suite in the suites set in the SSL. |
3520 | | * |
3521 | | * ssl SSL/TLS object. |
3522 | | * suite Cipher suite to look for. |
3523 | | * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise. |
3524 | | */ |
3525 | | int FindSuiteSSL(const WOLFSSL* ssl, byte* suite) |
3526 | 0 | { |
3527 | 0 | word16 i; |
3528 | 0 | const Suites* suites = WOLFSSL_SUITES(ssl); |
3529 | |
|
3530 | 0 | for (i = 0; i < suites->suiteSz; i += 2) { |
3531 | 0 | if (suites->suites[i+0] == suite[0] && |
3532 | 0 | suites->suites[i+1] == suite[1]) { |
3533 | 0 | return 1; |
3534 | 0 | } |
3535 | 0 | } |
3536 | | |
3537 | 0 | return 0; |
3538 | 0 | } |
3539 | | #endif |
3540 | | |
3541 | | #ifndef NO_PSK |
3542 | | /* Get the MAC algorithm for the TLS 1.3 cipher suite. |
3543 | | * |
3544 | | * @param [in] suite. |
3545 | | * @return A value from wc_MACAlgorithm enumeration. |
3546 | | */ |
3547 | | byte SuiteMac(const byte* suite) |
3548 | | { |
3549 | | byte mac = no_mac; |
3550 | | |
3551 | | if (suite[0] == TLS13_BYTE) { |
3552 | | switch (suite[1]) { |
3553 | | #ifdef BUILD_TLS_AES_128_GCM_SHA256 |
3554 | | case TLS_AES_128_GCM_SHA256: |
3555 | | mac = sha256_mac; |
3556 | | break; |
3557 | | #endif |
3558 | | #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256 |
3559 | | case TLS_CHACHA20_POLY1305_SHA256: |
3560 | | mac = sha256_mac; |
3561 | | break; |
3562 | | #endif |
3563 | | #ifdef BUILD_TLS_AES_128_CCM_SHA256 |
3564 | | case TLS_AES_128_CCM_SHA256: |
3565 | | mac = sha256_mac; |
3566 | | break; |
3567 | | #endif |
3568 | | #ifdef BUILD_TLS_AES_128_CCM_8_SHA256 |
3569 | | case TLS_AES_128_CCM_8_SHA256: |
3570 | | mac = sha256_mac; |
3571 | | break; |
3572 | | #endif |
3573 | | #ifdef BUILD_TLS_AES_256_GCM_SHA384 |
3574 | | case TLS_AES_256_GCM_SHA384: |
3575 | | mac = sha384_mac; |
3576 | | break; |
3577 | | #endif |
3578 | | default: |
3579 | | break; |
3580 | | } |
3581 | | } |
3582 | | #if (defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM)) && \ |
3583 | | defined(WOLFSSL_SM3) |
3584 | | else if (suite[0] == CIPHER_BYTE) { |
3585 | | switch (suite[1]) { |
3586 | | #ifdef BUILD_TLS_SM4_GCM_SM3 |
3587 | | case TLS_SM4_GCM_SM3: |
3588 | | mac = sm3_mac; |
3589 | | break; |
3590 | | #endif |
3591 | | #ifdef BUILD_TLS_SM4_CCM_SM3 |
3592 | | case TLS_SM4_CCM_SM3: |
3593 | | mac = sm3_mac; |
3594 | | break; |
3595 | | #endif |
3596 | | default: |
3597 | | break; |
3598 | | } |
3599 | | } |
3600 | | #endif |
3601 | | #ifdef HAVE_NULL_CIPHER |
3602 | | else if (suite[0] == ECC_BYTE) { |
3603 | | switch (suite[1]) { |
3604 | | #ifdef BUILD_TLS_SHA256_SHA256 |
3605 | | case TLS_SHA256_SHA256: |
3606 | | mac = sha256_mac; |
3607 | | break; |
3608 | | #endif |
3609 | | #ifdef BUILD_TLS_SHA384_SHA384 |
3610 | | case TLS_SHA384_SHA384: |
3611 | | mac = sha384_mac; |
3612 | | break; |
3613 | | #endif |
3614 | | default: |
3615 | | break; |
3616 | | } |
3617 | | } |
3618 | | #endif |
3619 | | |
3620 | | return mac; |
3621 | | } |
3622 | | #endif |
3623 | | |
3624 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3625 | | /* Create Cookie extension using the hash of the first ClientHello. |
3626 | | * |
3627 | | * ssl SSL/TLS object. |
3628 | | * hash The hash data. |
3629 | | * hashSz The size of the hash data in bytes. |
3630 | | * returns 0 on success, otherwise failure. |
3631 | | */ |
3632 | | int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz, |
3633 | | TLSX** exts, byte cipherSuite0, byte cipherSuite) |
3634 | | { |
3635 | | int ret; |
3636 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
3637 | | WC_DECLARE_VAR(cookieHmac, Hmac, 1, ssl->heap); |
3638 | | byte cookieType = 0; |
3639 | | byte macSz = 0; |
3640 | | byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2]; |
3641 | | TLSX* ext; |
3642 | | word16 cookieSz = 0; |
3643 | | |
3644 | | if (hash == NULL || hashSz == 0) { |
3645 | | return BAD_FUNC_ARG; |
3646 | | } |
3647 | | |
3648 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
3649 | | ssl->buffers.tls13CookieSecret.length == 0) { |
3650 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
3651 | | return COOKIE_ERROR; |
3652 | | } |
3653 | | |
3654 | | /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */ |
3655 | | cookie[cookieSz++] = (byte)hashSz; |
3656 | | XMEMCPY(cookie + cookieSz, hash, hashSz); |
3657 | | cookieSz += hashSz; |
3658 | | cookie[cookieSz++] = cipherSuite0; |
3659 | | cookie[cookieSz++] = cipherSuite; |
3660 | | if ((ext = TLSX_Find(*exts, TLSX_KEY_SHARE)) != NULL) { |
3661 | | KeyShareEntry* kse = (KeyShareEntry*)ext->data; |
3662 | | if (kse == NULL) { |
3663 | | WOLFSSL_MSG("KeyShareEntry can't be empty when negotiating " |
3664 | | "parameters"); |
3665 | | return BAD_STATE_E; |
3666 | | } |
3667 | | c16toa(kse->group, cookie + cookieSz); |
3668 | | cookieSz += OPAQUE16_LEN; |
3669 | | } |
3670 | | |
3671 | | #ifndef NO_SHA256 |
3672 | | cookieType = WC_SHA256; |
3673 | | macSz = WC_SHA256_DIGEST_SIZE; |
3674 | | #elif defined(WOLFSSL_SHA384) |
3675 | | cookieType = WC_SHA384; |
3676 | | macSz = WC_SHA384_DIGEST_SIZE; |
3677 | | #elif defined(WOLFSSL_TLS13_SHA512) |
3678 | | cookieType = WC_SHA512; |
3679 | | macSz = WC_SHA512_DIGEST_SIZE; |
3680 | | #elif defined(WOLFSSL_SM3) |
3681 | | cookieType = WC_SM3; |
3682 | | macSz = WC_SM3_DIGEST_SIZE; |
3683 | | #else |
3684 | | #error "No digest to available to use with HMAC for cookies." |
3685 | | #endif /* NO_SHA */ |
3686 | | |
3687 | | WC_ALLOC_VAR_EX(cookieHmac, Hmac, 1, ssl->heap, DYNAMIC_TYPE_HMAC, |
3688 | | return MEMORY_E); |
3689 | | |
3690 | | ret = wc_HmacInit(cookieHmac, ssl->heap, ssl->devId); |
3691 | | if (ret == 0) { |
3692 | | ret = wc_HmacSetKey(cookieHmac, cookieType, |
3693 | | ssl->buffers.tls13CookieSecret.buffer, |
3694 | | ssl->buffers.tls13CookieSecret.length); |
3695 | | } |
3696 | | if (ret == 0) |
3697 | | ret = wc_HmacUpdate(cookieHmac, cookie, cookieSz); |
3698 | | #ifdef WOLFSSL_DTLS13 |
3699 | | /* Tie cookie to peer address */ |
3700 | | if (ret == 0) { |
3701 | | /* peerLock not necessary. Still in handshake phase. */ |
3702 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
3703 | | ret = wc_HmacUpdate(cookieHmac, |
3704 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
3705 | | ssl->buffers.dtlsCtx.peer.sz); |
3706 | | } |
3707 | | } |
3708 | | #endif |
3709 | | if (ret == 0) |
3710 | | ret = wc_HmacFinal(cookieHmac, mac); |
3711 | | |
3712 | | wc_HmacFree(cookieHmac); |
3713 | | WC_FREE_VAR_EX(cookieHmac, ssl->heap, DYNAMIC_TYPE_HMAC); |
3714 | | if (ret != 0) |
3715 | | return ret; |
3716 | | |
3717 | | /* The cookie data is the hash and the integrity check. */ |
3718 | | return TLSX_Cookie_Use(ssl, cookie, cookieSz, mac, macSz, 1, exts); |
3719 | | } |
3720 | | #endif |
3721 | | |
3722 | | #ifdef WOLFSSL_DTLS13 |
3723 | | #define HRR_MAX_HS_HEADER_SZ DTLS_HANDSHAKE_HEADER_SZ |
3724 | | #else |
3725 | | #define HRR_MAX_HS_HEADER_SZ HANDSHAKE_HEADER_SZ |
3726 | | #endif /* WOLFSSL_DTLS13 */ |
3727 | | |
3728 | | static int CreateCookie(const WOLFSSL* ssl, byte** hash, byte* hashSz, |
3729 | | Hashes* hashes, TLSX** exts) |
3730 | 0 | { |
3731 | 0 | int ret = 0; |
3732 | |
|
3733 | 0 | (void)exts; |
3734 | |
|
3735 | 0 | *hash = NULL; |
3736 | 0 | switch (ssl->specs.mac_algorithm) { |
3737 | 0 | #ifndef NO_SHA256 |
3738 | 0 | case sha256_mac: |
3739 | 0 | *hash = hashes->sha256; |
3740 | 0 | break; |
3741 | 0 | #endif |
3742 | 0 | #ifdef WOLFSSL_SHA384 |
3743 | 0 | case sha384_mac: |
3744 | 0 | *hash = hashes->sha384; |
3745 | 0 | break; |
3746 | 0 | #endif |
3747 | | #ifdef WOLFSSL_TLS13_SHA512 |
3748 | | case sha512_mac: |
3749 | | *hash = hashes->sha512; |
3750 | | break; |
3751 | | #endif |
3752 | | #ifdef WOLFSSL_SM3 |
3753 | | case sm3_mac: |
3754 | | *hash = hashes->sm3; |
3755 | | break; |
3756 | | #endif |
3757 | 0 | } |
3758 | 0 | *hashSz = ssl->specs.hash_size; |
3759 | | |
3760 | | /* check hash */ |
3761 | 0 | if (*hash == NULL && *hashSz > 0) |
3762 | 0 | return BAD_FUNC_ARG; |
3763 | | |
3764 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3765 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3766 | | ret = CreateCookieExt(ssl, *hash, *hashSz, exts, |
3767 | | ssl->options.cipherSuite0, ssl->options.cipherSuite); |
3768 | | #endif |
3769 | 0 | return ret; |
3770 | 0 | } |
3771 | | |
3772 | | /* Restart the handshake hash with a hash of the previous messages. |
3773 | | * |
3774 | | * ssl The SSL/TLS object. |
3775 | | * returns 0 on success, otherwise failure. |
3776 | | */ |
3777 | | int RestartHandshakeHash(WOLFSSL* ssl) |
3778 | 0 | { |
3779 | 0 | int ret; |
3780 | 0 | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
3781 | 0 | Hashes hashes; |
3782 | 0 | byte* hash = NULL; |
3783 | 0 | byte hashSz = 0; |
3784 | |
|
3785 | 0 | ret = BuildCertHashes(ssl, &hashes); |
3786 | 0 | if (ret != 0) |
3787 | 0 | return ret; |
3788 | 0 | ret = CreateCookie(ssl, &hash, &hashSz, &hashes, &ssl->extensions); |
3789 | 0 | if (ret != 0) |
3790 | 0 | return ret; |
3791 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER) |
3792 | | if (ssl->options.sendCookie && ssl->options.side == WOLFSSL_SERVER_END) |
3793 | | return 0; |
3794 | | #endif |
3795 | | |
3796 | 0 | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
3797 | |
|
3798 | | #ifdef WOLFSSL_DEBUG_TLS |
3799 | | WOLFSSL_MSG("Restart Hash"); |
3800 | | WOLFSSL_BUFFER(hash, hashSz); |
3801 | | #endif |
3802 | |
|
3803 | 0 | ret = InitHandshakeHashes(ssl); |
3804 | 0 | if (ret != 0) |
3805 | 0 | return ret; |
3806 | 0 | ret = HashRaw(ssl, header, sizeof(header)); |
3807 | 0 | if (ret != 0) |
3808 | 0 | return ret; |
3809 | 0 | return HashRaw(ssl, hash, hashSz); |
3810 | 0 | } |
3811 | | |
3812 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
3813 | | /* The value in the random field of a ServerHello to indicate |
3814 | | * HelloRetryRequest. |
3815 | | */ |
3816 | | static byte helloRetryRequestRandom[] = { |
3817 | | 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, |
3818 | | 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, |
3819 | | 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, |
3820 | | 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C |
3821 | | }; |
3822 | | #endif |
3823 | | |
3824 | | #ifdef HAVE_ECH |
3825 | | /* returns the index of the first supported cipher suite, -1 if none */ |
3826 | | int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config) |
3827 | | { |
3828 | | int i = 0; |
3829 | | |
3830 | | if (!wc_HpkeKemIsSupported(config->kemId)) { |
3831 | | WOLFSSL_MSG("ECH config: KEM not supported"); |
3832 | | return WOLFSSL_FATAL_ERROR; |
3833 | | } |
3834 | | |
3835 | | for (i = 0; i < config->numCipherSuites; i++) { |
3836 | | if (wc_HpkeKdfIsSupported(config->cipherSuites[i].kdfId) && |
3837 | | wc_HpkeAeadIsSupported(config->cipherSuites[i].aeadId)) { |
3838 | | return i; |
3839 | | } |
3840 | | } |
3841 | | |
3842 | | WOLFSSL_MSG("ECH config: KDF or AEAD not supported"); |
3843 | | return WOLFSSL_FATAL_ERROR; |
3844 | | } |
3845 | | |
3846 | | /* Hash the inner client hello, initializing the hsHashesEch field if needed. |
3847 | | * This should receive the client hello without outer_extensions 'encoding' |
3848 | | * |
3849 | | * ssl SSL/TLS object. |
3850 | | * ech ECH object. |
3851 | | * returns 0 on success and otherwise failure. |
3852 | | */ |
3853 | | static int EchHashHelloInner(WOLFSSL* ssl, WOLFSSL_ECH* ech) |
3854 | | { |
3855 | | int ret = 0; |
3856 | | int headerSz; |
3857 | | word32 realSz; |
3858 | | HS_Hashes* tmpHashes; |
3859 | | #ifndef NO_WOLFSSL_CLIENT |
3860 | | byte falseHeader[HRR_MAX_HS_HEADER_SZ]; |
3861 | | #endif |
3862 | | |
3863 | | if (ssl == NULL || ech == NULL) { |
3864 | | return BAD_FUNC_ARG; |
3865 | | } |
3866 | | |
3867 | | #ifdef WOLFSSL_DTLS13 |
3868 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3869 | | HANDSHAKE_HEADER_SZ; |
3870 | | #else |
3871 | | headerSz = HANDSHAKE_HEADER_SZ; |
3872 | | #endif |
3873 | | |
3874 | | realSz = ech->innerClientHelloLen; |
3875 | | |
3876 | | tmpHashes = ssl->hsHashes; |
3877 | | |
3878 | | ssl->hsHashes = ssl->hsHashesEch; |
3879 | | if (ssl->hsHashes == NULL) { |
3880 | | ret = InitHandshakeHashes(ssl); |
3881 | | if (ret == 0) { |
3882 | | ssl->hsHashesEch = ssl->hsHashes; |
3883 | | } |
3884 | | } |
3885 | | |
3886 | | if (ret == 0) { |
3887 | | #ifndef NO_WOLFSSL_CLIENT |
3888 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
3889 | | /* client-side: innerClientHello contains body only */ |
3890 | | AddTls13HandShakeHeader(falseHeader, realSz, 0, 0, client_hello, |
3891 | | ssl); |
3892 | | ret = HashRaw(ssl, falseHeader, headerSz); |
3893 | | if (ret == 0) { |
3894 | | ret = HashRaw(ssl, ech->innerClientHello, realSz); |
3895 | | } |
3896 | | } |
3897 | | #endif |
3898 | | #ifndef NO_WOLFSSL_SERVER |
3899 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
3900 | | /* server-side: innerClientHello contains header + body */ |
3901 | | ret = HashRaw(ssl, ech->innerClientHello, headerSz + realSz); |
3902 | | } |
3903 | | #endif |
3904 | | } |
3905 | | |
3906 | | ssl->hsHashes = tmpHashes; |
3907 | | return ret; |
3908 | | } |
3909 | | |
3910 | | /* Calculate the 8 ECH confirmation bytes. |
3911 | | * |
3912 | | * ssl SSL/TLS object. |
3913 | | * label Ascii string describing ECH acceptance or rejection. |
3914 | | * labelSz Length of label excluding NULL character. |
3915 | | * input The buffer to calculate confirmation off of. |
3916 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
3917 | | * helloSz Size of hello message. |
3918 | | * isHrr Whether message is a HelloRetryRequest or not. |
3919 | | * acceptExpanded An 8 byte array to store calculated confirmation to. |
3920 | | * returns 0 on success and otherwise failure. |
3921 | | */ |
3922 | | static int EchCalcAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
3923 | | const byte* input, int acceptOffset, int helloSz, byte isHrr, |
3924 | | byte* acceptExpanded) |
3925 | | { |
3926 | | int ret = 0; |
3927 | | int digestType = 0; |
3928 | | int digestSize = 0; |
3929 | | int hashSz = 0; |
3930 | | int headerSz; |
3931 | | HS_Hashes* tmpHashes; |
3932 | | HS_Hashes* acceptHash = NULL; |
3933 | | byte zeros[WC_MAX_DIGEST_SIZE]; |
3934 | | byte transcriptEchConf[WC_MAX_DIGEST_SIZE]; |
3935 | | byte clientHelloInnerHash[WC_MAX_DIGEST_SIZE]; |
3936 | | byte expandLabelPrk[WC_MAX_DIGEST_SIZE]; |
3937 | | byte messageHashHeader[HRR_MAX_HS_HEADER_SZ]; |
3938 | | |
3939 | | XMEMSET(zeros, 0, sizeof(zeros)); |
3940 | | XMEMSET(transcriptEchConf, 0, sizeof(transcriptEchConf)); |
3941 | | XMEMSET(clientHelloInnerHash, 0, sizeof(clientHelloInnerHash)); |
3942 | | XMEMSET(expandLabelPrk, 0, sizeof(expandLabelPrk)); |
3943 | | |
3944 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
3945 | | wc_MemZero_Add("ECH PRK", expandLabelPrk, sizeof(expandLabelPrk)); |
3946 | | #endif |
3947 | | |
3948 | | tmpHashes = ssl->hsHashes; |
3949 | | ssl->hsHashes = ssl->hsHashesEch; |
3950 | | |
3951 | | #ifdef WOLFSSL_DTLS13 |
3952 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
3953 | | HANDSHAKE_HEADER_SZ; |
3954 | | #else |
3955 | | headerSz = HANDSHAKE_HEADER_SZ; |
3956 | | #endif |
3957 | | |
3958 | | if (isHrr) { |
3959 | | /* the transcript hash of ClientHelloInner1 */ |
3960 | | ret = GetMsgHash(ssl, clientHelloInnerHash); |
3961 | | if (ret > 0) { |
3962 | | hashSz = ret; |
3963 | | ret = 0; |
3964 | | } |
3965 | | else if (ret == 0) { |
3966 | | ret = HASH_TYPE_E; |
3967 | | } |
3968 | | |
3969 | | /* restart ECH transcript hash, similar to RestartHandshakeHash but |
3970 | | * don't add a cookie */ |
3971 | | if (ret == 0) { |
3972 | | ret = InitHandshakeHashes(ssl); |
3973 | | } |
3974 | | if (ret == 0) { |
3975 | | ssl->hsHashesEch = ssl->hsHashes; |
3976 | | AddTls13HandShakeHeader(messageHashHeader, (word32)hashSz, 0, 0, |
3977 | | message_hash, ssl); |
3978 | | ret = HashRaw(ssl, messageHashHeader, headerSz); |
3979 | | } |
3980 | | if (ret == 0) { |
3981 | | ret = HashRaw(ssl, clientHelloInnerHash, (word32)hashSz); |
3982 | | } |
3983 | | } |
3984 | | |
3985 | | /* hash with zeros for confirmation computation */ |
3986 | | if (ret == 0) { |
3987 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashesEch, &acceptHash); |
3988 | | } |
3989 | | if (ret == 0) { |
3990 | | ssl->hsHashes = acceptHash; |
3991 | | ret = HashRaw(ssl, input, acceptOffset); |
3992 | | } |
3993 | | if (ret == 0) { |
3994 | | ret = HashRaw(ssl, zeros, ECH_ACCEPT_CONFIRMATION_SZ); |
3995 | | } |
3996 | | if (ret == 0) { |
3997 | | ret = HashRaw(ssl, input + acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ, |
3998 | | helloSz + headerSz - (acceptOffset + ECH_ACCEPT_CONFIRMATION_SZ)); |
3999 | | } |
4000 | | |
4001 | | /* get the modified transcript hash */ |
4002 | | if (ret == 0) { |
4003 | | ret = GetMsgHash(ssl, transcriptEchConf); |
4004 | | if (ret > 0) { |
4005 | | ret = 0; |
4006 | | } |
4007 | | else if (ret == 0) { |
4008 | | ret = HASH_TYPE_E; |
4009 | | } |
4010 | | } |
4011 | | |
4012 | | /* pick the right type and size based on mac_algorithm */ |
4013 | | if (ret == 0) { |
4014 | | switch (ssl->specs.mac_algorithm) { |
4015 | | #ifndef NO_SHA256 |
4016 | | case sha256_mac: |
4017 | | digestType = WC_SHA256; |
4018 | | digestSize = WC_SHA256_DIGEST_SIZE; |
4019 | | break; |
4020 | | #endif /* !NO_SHA256 */ |
4021 | | #ifdef WOLFSSL_SHA384 |
4022 | | case sha384_mac: |
4023 | | digestType = WC_SHA384; |
4024 | | digestSize = WC_SHA384_DIGEST_SIZE; |
4025 | | break; |
4026 | | #endif /* WOLFSSL_SHA384 */ |
4027 | | #ifdef WOLFSSL_TLS13_SHA512 |
4028 | | case sha512_mac: |
4029 | | digestType = WC_SHA512; |
4030 | | digestSize = WC_SHA512_DIGEST_SIZE; |
4031 | | break; |
4032 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
4033 | | #ifdef WOLFSSL_SM3 |
4034 | | case sm3_mac: |
4035 | | digestType = WC_SM3; |
4036 | | digestSize = WC_SM3_DIGEST_SIZE; |
4037 | | break; |
4038 | | #endif /* WOLFSSL_SM3 */ |
4039 | | default: |
4040 | | ret = WOLFSSL_FATAL_ERROR; |
4041 | | break; |
4042 | | } |
4043 | | } |
4044 | | |
4045 | | /* extract clientRandomInner with a key of all zeros */ |
4046 | | if (ret == 0) { |
4047 | | PRIVATE_KEY_UNLOCK(); |
4048 | | #if !defined(HAVE_FIPS) || \ |
4049 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)) |
4050 | | ret = wc_HKDF_Extract_ex(digestType, zeros, (word32)digestSize, |
4051 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk, |
4052 | | ssl->heap, ssl->devId); |
4053 | | #else |
4054 | | ret = wc_HKDF_Extract(digestType, zeros, digestSize, |
4055 | | ssl->arrays->clientRandomInner, RAN_LEN, expandLabelPrk); |
4056 | | #endif |
4057 | | PRIVATE_KEY_LOCK(); |
4058 | | } |
4059 | | |
4060 | | /* tls expand with the confirmation label */ |
4061 | | if (ret == 0) { |
4062 | | PRIVATE_KEY_UNLOCK(); |
4063 | | #ifdef WOLFSSL_DTLS13 |
4064 | | if (ssl->options.dtls) { |
4065 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4066 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4067 | | dtls13ProtocolLabel, DTLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4068 | | transcriptEchConf, (word32)digestSize, digestType, |
4069 | | WOLFSSL_SERVER_END); |
4070 | | } |
4071 | | else |
4072 | | #endif |
4073 | | { |
4074 | | ret = Tls13HKDFExpandKeyLabel(ssl, acceptExpanded, |
4075 | | ECH_ACCEPT_CONFIRMATION_SZ, expandLabelPrk, (word32)digestSize, |
4076 | | tls13ProtocolLabel, TLS13_PROTOCOL_LABEL_SZ, label, labelSz, |
4077 | | transcriptEchConf, (word32)digestSize, digestType, |
4078 | | WOLFSSL_SERVER_END); |
4079 | | } |
4080 | | PRIVATE_KEY_LOCK(); |
4081 | | } |
4082 | | |
4083 | | if (acceptHash != NULL) { |
4084 | | ssl->hsHashes = acceptHash; |
4085 | | FreeHandshakeHashes(ssl); |
4086 | | } |
4087 | | |
4088 | | ssl->hsHashes = tmpHashes; |
4089 | | ForceZero(expandLabelPrk, sizeof(expandLabelPrk)); |
4090 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4091 | | wc_MemZero_Check(expandLabelPrk, sizeof(expandLabelPrk)); |
4092 | | #endif |
4093 | | return ret; |
4094 | | } |
4095 | | #endif |
4096 | | |
4097 | | #ifndef NO_WOLFSSL_CLIENT |
4098 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
4099 | | #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_PSK_ONE_ID) && \ |
4100 | | !defined(NO_PSK) |
4101 | | /** |
4102 | | * convert mac algorithm to WOLFSSL_EVP_MD |
4103 | | * @param mac_alg mac algorithm |
4104 | | * @return const WOLFSSL_EVP_MD on successful, otherwise NULL |
4105 | | */ |
4106 | | static const WOLFSSL_EVP_MD* ssl_handshake_md(const byte mac_alg) |
4107 | | { |
4108 | | switch(mac_alg) { |
4109 | | case no_mac: |
4110 | | return NULL; |
4111 | | #ifndef NO_MD5 |
4112 | | case md5_mac: |
4113 | | return wolfSSL_EVP_md5(); |
4114 | | #endif |
4115 | | #ifndef NO_SHA |
4116 | | case sha_mac: |
4117 | | return wolfSSL_EVP_sha1(); |
4118 | | #endif |
4119 | | #ifdef WOLFSSL_SHA224 |
4120 | | case sha224_mac: |
4121 | | return wolfSSL_EVP_sha224(); |
4122 | | #endif |
4123 | | case sha256_mac: |
4124 | | return wolfSSL_EVP_sha256(); |
4125 | | #ifdef WOLFSSL_SHA384 |
4126 | | case sha384_mac: |
4127 | | return wolfSSL_EVP_sha384(); |
4128 | | #endif |
4129 | | #ifdef WOLFSSL_SHA512 |
4130 | | case sha512_mac: |
4131 | | return wolfSSL_EVP_sha512(); |
4132 | | #endif |
4133 | | case rmd_mac: |
4134 | | case blake2b_mac: |
4135 | | WOLFSSL_MSG("no suitable EVP_MD"); |
4136 | | return NULL; |
4137 | | default: |
4138 | | WOLFSSL_MSG("Unknown mac algorithm"); |
4139 | | return NULL; |
4140 | | } |
4141 | | } |
4142 | | #endif |
4143 | | /* Setup pre-shared key based on the details in the extension data. |
4144 | | * |
4145 | | * ssl SSL/TLS object. |
4146 | | * psk Pre-shared key extension data. |
4147 | | * clientHello Whether called from client_hello construction. |
4148 | | * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and |
4149 | | * other negative value on failure. |
4150 | | */ |
4151 | | static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello) |
4152 | | { |
4153 | | #if defined(HAVE_SESSION_TICKET) || !defined(WOLFSSL_PSK_ONE_ID) |
4154 | | int ret; |
4155 | | #endif |
4156 | | byte suite[2]; |
4157 | | |
4158 | | if (psk == NULL) |
4159 | | return BAD_FUNC_ARG; |
4160 | | |
4161 | | if (!HaveUniqueSessionObj(ssl)) { |
4162 | | WOLFSSL_MSG("Unable to have unique session object"); |
4163 | | WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR); |
4164 | | return MEMORY_ERROR; |
4165 | | } |
4166 | | |
4167 | | suite[0] = ssl->options.cipherSuite0; |
4168 | | suite[1] = ssl->options.cipherSuite; |
4169 | | |
4170 | | #ifdef HAVE_SESSION_TICKET |
4171 | | if (psk->resumption) { |
4172 | | if (clientHello) { |
4173 | | suite[0] = psk->cipherSuite0; |
4174 | | suite[1] = psk->cipherSuite; |
4175 | | |
4176 | | /* Ensure cipher suite is supported or changed suite to one with |
4177 | | * the same MAC algorithm. */ |
4178 | | if (!FindSuiteSSL(ssl, suite)) { |
4179 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4180 | | return PSK_KEY_ERROR; |
4181 | | } |
4182 | | |
4183 | | ssl->options.cipherSuite0 = suite[0]; |
4184 | | ssl->options.cipherSuite = suite[1]; |
4185 | | |
4186 | | /* Setting mac for binder and keys for deriving EarlyData. */ |
4187 | | ret = SetCipherSpecs(ssl); |
4188 | | if (ret != 0) |
4189 | | return ret; |
4190 | | } |
4191 | | |
4192 | | #ifdef WOLFSSL_EARLY_DATA |
4193 | | if (ssl->session->maxEarlyDataSz == 0) |
4194 | | ssl->earlyData = no_early_data; |
4195 | | #endif |
4196 | | /* Resumption PSK is master secret. */ |
4197 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
4198 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
4199 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
4200 | | return ret; |
4201 | | } |
4202 | | if (!clientHello) { |
4203 | | /* CLIENT: using secret in ticket for peer authentication. */ |
4204 | | ssl->options.peerAuthGood = 1; |
4205 | | } |
4206 | | } |
4207 | | #endif |
4208 | | #ifndef NO_PSK |
4209 | | if (!psk->resumption) { |
4210 | | /* Get the pre-shared key. */ |
4211 | | #ifndef WOLFSSL_PSK_ONE_ID |
4212 | | const char* cipherName = NULL; |
4213 | | #ifdef OPENSSL_EXTRA |
4214 | | WOLFSSL_SESSION* psksession = NULL; |
4215 | | #endif |
4216 | | |
4217 | | /* Set the client identity to use. */ |
4218 | | if (psk->identityLen > MAX_PSK_ID_LEN) |
4219 | | return PSK_KEY_ERROR; |
4220 | | XMEMSET(ssl->arrays->client_identity, 0, |
4221 | | sizeof(ssl->arrays->client_identity)); |
4222 | | XMEMCPY(ssl->arrays->client_identity, psk->identity, psk->identityLen); |
4223 | | |
4224 | | #ifdef WOLFSSL_DEBUG_TLS |
4225 | | WOLFSSL_MSG("PSK cipher suite:"); |
4226 | | WOLFSSL_MSG(GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4227 | | #endif |
4228 | | |
4229 | | /* Get the pre-shared key. */ |
4230 | | #ifdef OPENSSL_EXTRA |
4231 | | if (ssl->options.session_psk_cb != NULL) { |
4232 | | const unsigned char* id = NULL; |
4233 | | size_t idlen = 0; |
4234 | | const WOLFSSL_EVP_MD* handshake_md = NULL; |
4235 | | |
4236 | | if (ssl->msgsReceived.got_hello_retry_request >= 1) { |
4237 | | handshake_md = ssl_handshake_md(ssl->specs.mac_algorithm); |
4238 | | } |
4239 | | /* OpenSSL compatible callback that gets cached session. */ |
4240 | | if (ssl->options.session_psk_cb(ssl, handshake_md, &id, &idlen, |
4241 | | &psksession) == 0) { |
4242 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4243 | | WOLFSSL_MSG("psk session callback failed"); |
4244 | | return PSK_KEY_ERROR; |
4245 | | } |
4246 | | if (psksession != NULL) { |
4247 | | if (idlen > MAX_PSK_KEY_LEN) { |
4248 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4249 | | WOLFSSL_MSG("psk key length is too long"); |
4250 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4251 | | return PSK_KEY_ERROR; |
4252 | | } |
4253 | | |
4254 | | ssl->arrays->psk_keySz = (word32)idlen; |
4255 | | XMEMCPY(ssl->arrays->psk_key, id, idlen); |
4256 | | suite[0] = psksession->cipherSuite0; |
4257 | | suite[1] = psksession->cipherSuite; |
4258 | | /* Not needed anymore. */ |
4259 | | wolfSSL_FreeSession(ssl->ctx, psksession); |
4260 | | /* Leave pointer not NULL to indicate success with callback. */ |
4261 | | } |
4262 | | } |
4263 | | if (psksession != NULL) { |
4264 | | /* Don't try other callbacks - we have an answer. */ |
4265 | | } |
4266 | | else |
4267 | | #endif /* OPENSSL_EXTRA */ |
4268 | | if (ssl->options.client_psk_cs_cb != NULL) { |
4269 | | #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
4270 | | ssl->arrays->client_identity[0] = 0; |
4271 | | #endif |
4272 | | /* Lookup key again for next identity. */ |
4273 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cs_cb( |
4274 | | ssl, ssl->arrays->server_hint, |
4275 | | ssl->arrays->client_identity, MAX_PSK_ID_LEN, |
4276 | | ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4277 | | GetCipherNameInternal(psk->cipherSuite0, psk->cipherSuite)); |
4278 | | if (clientHello) { |
4279 | | /* Use PSK cipher suite. */ |
4280 | | ssl->options.cipherSuite0 = psk->cipherSuite0; |
4281 | | ssl->options.cipherSuite = psk->cipherSuite; |
4282 | | } |
4283 | | else { |
4284 | | byte pskCS[2]; |
4285 | | pskCS[0] = psk->cipherSuite0; |
4286 | | pskCS[1] = psk->cipherSuite; |
4287 | | |
4288 | | /* Ensure PSK and negotiated cipher suites have same hash. */ |
4289 | | if (SuiteMac(pskCS) != SuiteMac(suite)) { |
4290 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4291 | | return PSK_KEY_ERROR; |
4292 | | } |
4293 | | /* Negotiated cipher suite is to be used - update PSK. */ |
4294 | | psk->cipherSuite0 = suite[0]; |
4295 | | psk->cipherSuite = suite[1]; |
4296 | | } |
4297 | | } |
4298 | | else if (ssl->options.client_psk_tls13_cb != NULL) { |
4299 | | byte cipherSuite0; |
4300 | | byte cipherSuite; |
4301 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
4302 | | |
4303 | | ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl, |
4304 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4305 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN, |
4306 | | &cipherName); |
4307 | | if (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
4308 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) != 0) { |
4309 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4310 | | return PSK_KEY_ERROR; |
4311 | | } |
4312 | | ssl->options.cipherSuite0 = cipherSuite0; |
4313 | | ssl->options.cipherSuite = cipherSuite; |
4314 | | (void)cipherSuiteFlags; |
4315 | | } |
4316 | | else { |
4317 | | ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl, |
4318 | | ssl->arrays->server_hint, ssl->arrays->client_identity, |
4319 | | MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN); |
4320 | | ssl->options.cipherSuite0 = TLS13_BYTE; |
4321 | | ssl->options.cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
4322 | | } |
4323 | | if (ssl->arrays->psk_keySz == 0 || |
4324 | | (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN && |
4325 | | (int)ssl->arrays->psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK))) { |
4326 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4327 | | return PSK_KEY_ERROR; |
4328 | | } |
4329 | | |
4330 | | ret = SetCipherSpecs(ssl); |
4331 | | if (ret != 0) |
4332 | | return ret; |
4333 | | #else |
4334 | | /* PSK information loaded during setting of default TLS extensions. */ |
4335 | | #endif /* !WOLFSSL_PSK_ONE_ID */ |
4336 | | |
4337 | | if (!clientHello && (psk->cipherSuite0 != suite[0] || |
4338 | | psk->cipherSuite != suite[1])) { |
4339 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4340 | | return PSK_KEY_ERROR; |
4341 | | } |
4342 | | |
4343 | | if (!clientHello) { |
4344 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
4345 | | if (ssl->options.certWithExternPsk) { |
4346 | | /* Certificate authentication is still required. */ |
4347 | | ssl->options.peerAuthGood = 0; |
4348 | | } |
4349 | | else |
4350 | | #endif |
4351 | | { |
4352 | | /* CLIENT: using PSK for peer authentication. */ |
4353 | | ssl->options.peerAuthGood = 1; |
4354 | | } |
4355 | | } |
4356 | | } |
4357 | | #endif |
4358 | | |
4359 | | #ifdef HAVE_SUPPORTED_CURVES |
4360 | | if (!clientHello) { |
4361 | | TLSX* ext; |
4362 | | word32 modes; |
4363 | | KeyShareEntry* kse = NULL; |
4364 | | |
4365 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
4366 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
4367 | | if (ext == NULL) { |
4368 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4369 | | return PSK_KEY_ERROR; |
4370 | | } |
4371 | | modes = ext->val; |
4372 | | |
4373 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
4374 | | if (ext != NULL) { |
4375 | | kse = (KeyShareEntry*)ext->data; |
4376 | | } |
4377 | | /* Use (EC)DHE for forward-security if possible. */ |
4378 | | if (((modes & (1 << PSK_DHE_KE)) != 0) && (!ssl->options.noPskDheKe) && |
4379 | | (kse != NULL) && kse->derived) { |
4380 | | if ((kse->session != 0) && (kse->session != kse->group)) { |
4381 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4382 | | return PSK_KEY_ERROR; |
4383 | | } |
4384 | | } |
4385 | | else if (ssl->options.onlyPskDheKe) { |
4386 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
4387 | | return PSK_KEY_ERROR; |
4388 | | } |
4389 | | else if (ssl->options.noPskDheKe) { |
4390 | | ssl->arrays->preMasterSz = 0; |
4391 | | } |
4392 | | } |
4393 | | else |
4394 | | #endif |
4395 | | if (ssl->options.noPskDheKe) { |
4396 | | ssl->arrays->preMasterSz = 0; |
4397 | | } |
4398 | | |
4399 | | /* Derive the early secret using the PSK. */ |
4400 | | return DeriveEarlySecret(ssl); |
4401 | | } |
4402 | | |
4403 | | /* Derive and write the binders into the ClientHello in space left when |
4404 | | * writing the Pre-Shared Key extension. |
4405 | | * |
4406 | | * ssl The SSL/TLS object. |
4407 | | * output The buffer containing the ClientHello. |
4408 | | * idx The index at the end of the completed ClientHello. |
4409 | | * returns 0 on success and otherwise failure. |
4410 | | */ |
4411 | | static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx) |
4412 | | { |
4413 | | int ret; |
4414 | | TLSX* ext; |
4415 | | PreSharedKey* current; |
4416 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
4417 | | word16 len; |
4418 | | |
4419 | | WOLFSSL_ENTER("WritePSKBinders"); |
4420 | | |
4421 | | if (idx > WOLFSSL_MAX_16BIT) { |
4422 | | return INPUT_SIZE_E; |
4423 | | } |
4424 | | |
4425 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
4426 | | if (ext == NULL) |
4427 | | return SANITY_MSG_E; |
4428 | | |
4429 | | /* Get the size of the binders to determine where to write binders. */ |
4430 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
4431 | | client_hello, &len); |
4432 | | if (ret < 0) |
4433 | | return ret; |
4434 | | idx -= len; |
4435 | | |
4436 | | /* Hash truncated ClientHello - up to binders. */ |
4437 | | #ifdef WOLFSSL_DTLS13 |
4438 | | if (ssl->options.dtls) |
4439 | | ret = Dtls13HashHandshake(ssl, output + Dtls13GetRlHeaderLength(ssl, 0), |
4440 | | (word16)idx - Dtls13GetRlHeaderLength(ssl, 0)); |
4441 | | else |
4442 | | #endif /* WOLFSSL_DTLS13 */ |
4443 | | ret = HashOutput(ssl, output, (int)idx, 0); |
4444 | | |
4445 | | if (ret != 0) |
4446 | | return ret; |
4447 | | |
4448 | | current = (PreSharedKey*)ext->data; |
4449 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4450 | | if (current != NULL) { |
4451 | | wc_MemZero_Add("WritePSKBinders binderKey", binderKey, |
4452 | | sizeof(binderKey)); |
4453 | | } |
4454 | | #endif |
4455 | | /* Calculate the binder for each identity based on previous handshake data. |
4456 | | */ |
4457 | | while (current != NULL) { |
4458 | | if ((ret = SetupPskKey(ssl, current, 1)) != 0) |
4459 | | break; |
4460 | | |
4461 | | #ifdef HAVE_SESSION_TICKET |
4462 | | if (current->resumption) |
4463 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
4464 | | #endif |
4465 | | #ifndef NO_PSK |
4466 | | if (!current->resumption) |
4467 | | ret = DeriveBinderKey(ssl, binderKey); |
4468 | | #endif |
4469 | | if (ret != 0) |
4470 | | break; |
4471 | | |
4472 | | /* Derive the Finished message secret. */ |
4473 | | ret = DeriveFinishedSecret(ssl, binderKey, |
4474 | | ssl->keys.client_write_MAC_secret, |
4475 | | 0 /* neither end */); |
4476 | | if (ret != 0) |
4477 | | break; |
4478 | | |
4479 | | /* Build the HMAC of the handshake message data = binder. */ |
4480 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, |
4481 | | current->binder, ¤t->binderLen); |
4482 | | if (ret != 0) |
4483 | | break; |
4484 | | |
4485 | | current = current->next; |
4486 | | } |
4487 | | |
4488 | | ForceZero(binderKey, sizeof(binderKey)); |
4489 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
4490 | | wc_MemZero_Check(binderKey, sizeof(binderKey)); |
4491 | | #endif |
4492 | | if (ret != 0) |
4493 | | return ret; |
4494 | | |
4495 | | /* Data entered into extension, now write to message. */ |
4496 | | ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx, |
4497 | | client_hello, &len); |
4498 | | if (ret < 0) |
4499 | | return ret; |
4500 | | |
4501 | | /* Hash binders to complete the hash of the ClientHello. */ |
4502 | | ret = HashRaw(ssl, output + idx, len); |
4503 | | if (ret < 0) |
4504 | | return ret; |
4505 | | |
4506 | | #ifdef WOLFSSL_EARLY_DATA |
4507 | | if (ssl->earlyData != no_early_data) { |
4508 | | if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data, 1)) != 0) |
4509 | | return ret; |
4510 | | |
4511 | | /* Derive early data encryption key. */ |
4512 | | ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1); |
4513 | | if (ret != 0) |
4514 | | return ret; |
4515 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
4516 | | return ret; |
4517 | | |
4518 | | } |
4519 | | #endif |
4520 | | |
4521 | | WOLFSSL_LEAVE("WritePSKBinders", ret); |
4522 | | |
4523 | | return ret; |
4524 | | } |
4525 | | #endif |
4526 | | |
4527 | | static void GetTls13SessionId(WOLFSSL* ssl, byte* output, word32* idx) |
4528 | 0 | { |
4529 | 0 | if (ssl->session->sessionIDSz > 0) { |
4530 | | /* Session resumption for old versions of protocol. */ |
4531 | 0 | if (ssl->session->sessionIDSz <= ID_LEN) { |
4532 | 0 | if (output != NULL) |
4533 | 0 | output[*idx] = ssl->session->sessionIDSz; |
4534 | 0 | (*idx)++; |
4535 | 0 | if (output != NULL) { |
4536 | 0 | XMEMCPY(output + *idx, ssl->session->sessionID, |
4537 | 0 | ssl->session->sessionIDSz); |
4538 | 0 | } |
4539 | 0 | *idx += ssl->session->sessionIDSz; |
4540 | 0 | } |
4541 | 0 | else { |
4542 | | /* Invalid session ID length. Reset it. */ |
4543 | 0 | ssl->session->sessionIDSz = 0; |
4544 | 0 | if (output != NULL) |
4545 | 0 | output[*idx] = 0; |
4546 | 0 | (*idx)++; |
4547 | 0 | } |
4548 | 0 | } |
4549 | 0 | else { |
4550 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
4551 | | if (ssl->options.tls13MiddleBoxCompat) { |
4552 | | if (output != NULL) |
4553 | | output[*idx] = ID_LEN; |
4554 | | (*idx)++; |
4555 | | if (output != NULL) |
4556 | | XMEMCPY(output + *idx, ssl->arrays->clientRandom, ID_LEN); |
4557 | | *idx += ID_LEN; |
4558 | | } |
4559 | | else |
4560 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
4561 | 0 | { |
4562 | | /* TLS v1.3 does not use session id - 0 length. */ |
4563 | 0 | if (output != NULL) |
4564 | 0 | output[*idx] = 0; |
4565 | 0 | (*idx)++; |
4566 | 0 | } |
4567 | 0 | } |
4568 | 0 | } |
4569 | | |
4570 | | /* handle generation of TLS 1.3 client_hello (1) */ |
4571 | | /* Send a ClientHello message to the server. |
4572 | | * Include the information required to start a handshake with servers using |
4573 | | * protocol versions less than TLS v1.3. |
4574 | | * Only a client will send this message. |
4575 | | * |
4576 | | * ssl The SSL/TLS object. |
4577 | | * returns 0 on success and otherwise failure. |
4578 | | */ |
4579 | | |
4580 | | typedef struct Sch13Args { |
4581 | | byte* output; |
4582 | | word32 idx; |
4583 | | int sendSz; |
4584 | | word32 length; |
4585 | | #if defined(HAVE_ECH) |
4586 | | int clientRandomOffset; |
4587 | | int preXLength; |
4588 | | word32 expandedInnerLen; |
4589 | | WOLFSSL_ECH* ech; |
4590 | | #endif |
4591 | | } Sch13Args; |
4592 | | |
4593 | | #ifdef WOLFSSL_EARLY_DATA |
4594 | | /* Check if early data can potentially be sent. |
4595 | | * Returns 1 if early data is possible, 0 otherwise. |
4596 | | */ |
4597 | | static int EarlyDataPossible(WOLFSSL* ssl) |
4598 | | { |
4599 | | /* Need session resumption OR PSK callback configured */ |
4600 | | if (ssl->options.resuming) { |
4601 | | return 1; |
4602 | | } |
4603 | | #ifndef NO_PSK |
4604 | | if (ssl->options.client_psk_tls13_cb != NULL || |
4605 | | ssl->options.client_psk_cb != NULL) { |
4606 | | return 1; |
4607 | | } |
4608 | | #endif |
4609 | | return 0; |
4610 | | } |
4611 | | #endif /* WOLFSSL_EARLY_DATA */ |
4612 | | |
4613 | | int SendTls13ClientHello(WOLFSSL* ssl) |
4614 | 0 | { |
4615 | 0 | int ret; |
4616 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4617 | | Sch13Args* args = NULL; |
4618 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
4619 | | #else |
4620 | 0 | Sch13Args args[1]; |
4621 | 0 | #endif |
4622 | 0 | byte major, tls12minor; |
4623 | 0 | const Suites* suites; |
4624 | |
|
4625 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND); |
4626 | 0 | WOLFSSL_ENTER("SendTls13ClientHello"); |
4627 | |
|
4628 | 0 | if (ssl == NULL) { |
4629 | 0 | return BAD_FUNC_ARG; |
4630 | 0 | } |
4631 | | |
4632 | 0 | ssl->options.buildingMsg = 1; |
4633 | 0 | major = SSLv3_MAJOR; |
4634 | 0 | tls12minor = TLSv1_2_MINOR; |
4635 | |
|
4636 | | #ifdef WOLFSSL_DTLS13 |
4637 | | if (ssl->options.dtls) { |
4638 | | major = DTLS_MAJOR; |
4639 | | tls12minor = DTLSv1_2_MINOR; |
4640 | | } |
4641 | | #endif /* WOLFSSL_DTLS */ |
4642 | |
|
4643 | 0 | if (ssl->options.resuming && |
4644 | 0 | ssl->session->version.major != 0 && |
4645 | 0 | (ssl->session->version.major != ssl->version.major || |
4646 | 0 | ssl->session->version.minor != ssl->version.minor)) { |
4647 | 0 | #ifndef WOLFSSL_NO_TLS12 |
4648 | 0 | if (ssl->session->version.major == ssl->version.major && |
4649 | 0 | ssl->session->version.minor < ssl->version.minor) { |
4650 | | /* Cannot resume with a different protocol version. */ |
4651 | 0 | ssl->options.resuming = 0; |
4652 | 0 | ssl->version.major = ssl->session->version.major; |
4653 | 0 | ssl->version.minor = ssl->session->version.minor; |
4654 | 0 | return SendClientHello(ssl); |
4655 | 0 | } |
4656 | 0 | else |
4657 | 0 | #endif |
4658 | 0 | { |
4659 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
4660 | 0 | return VERSION_ERROR; |
4661 | 0 | } |
4662 | 0 | } |
4663 | | |
4664 | 0 | suites = WOLFSSL_SUITES(ssl); |
4665 | 0 | if (suites == NULL) { |
4666 | 0 | WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello"); |
4667 | 0 | return SUITES_ERROR; |
4668 | 0 | } |
4669 | | |
4670 | | #ifdef WOLFSSL_ASYNC_CRYPT |
4671 | | if (ssl->async == NULL) { |
4672 | | ssl->async = (struct WOLFSSL_ASYNC*) |
4673 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
4674 | | DYNAMIC_TYPE_ASYNC); |
4675 | | if (ssl->async == NULL) |
4676 | | return MEMORY_E; |
4677 | | ssl->async->freeArgs = NULL; |
4678 | | } |
4679 | | args = (Sch13Args*)ssl->async->args; |
4680 | | |
4681 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
4682 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
4683 | | /* Check for error */ |
4684 | | if (ret < 0) |
4685 | | return ret; |
4686 | | } |
4687 | | else |
4688 | | #endif |
4689 | 0 | { |
4690 | | /* Reset state */ |
4691 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
4692 | 0 | XMEMSET(args, 0, sizeof(Sch13Args)); |
4693 | 0 | } |
4694 | |
|
4695 | 0 | switch (ssl->options.asyncState) { |
4696 | 0 | case TLS_ASYNC_BEGIN: |
4697 | 0 | { |
4698 | 0 | word32 sessIdSz = 0; |
4699 | |
|
4700 | 0 | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
4701 | |
|
4702 | | #ifdef WOLFSSL_DTLS13 |
4703 | | if (ssl->options.dtls) |
4704 | | args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4705 | | #endif /* WOLFSSL_DTLS13 */ |
4706 | | |
4707 | | /* Version | Random | Cipher Suites | Compression */ |
4708 | 0 | args->length = VERSION_SZ + RAN_LEN + suites->suiteSz + |
4709 | 0 | SUITE_LEN + COMP_LEN + ENUM_LEN; |
4710 | | #ifdef WOLFSSL_QUIC |
4711 | | if (WOLFSSL_IS_QUIC(ssl)) { |
4712 | | /* RFC 9001 ch. 8.4 sessionID in ClientHello MUST be 0 length */ |
4713 | | ssl->session->sessionIDSz = 0; |
4714 | | ssl->options.tls13MiddleBoxCompat = 0; |
4715 | | } |
4716 | | #endif |
4717 | | #ifdef WOLFSSL_DTLS13 |
4718 | | if (ssl->options.dtls) { |
4719 | | /* RFC 9147 Section 5: DTLS implementations do not use the |
4720 | | * TLS 1.3 "compatibility mode" */ |
4721 | | ssl->options.tls13MiddleBoxCompat = 0; |
4722 | | } |
4723 | | #endif |
4724 | 0 | GetTls13SessionId(ssl, NULL, &sessIdSz); |
4725 | 0 | args->length += (word16)sessIdSz; |
4726 | |
|
4727 | | #ifdef WOLFSSL_DTLS13 |
4728 | | if (ssl->options.dtls) { |
4729 | | /* legacy_cookie_id len */ |
4730 | | args->length += ENUM_LEN; |
4731 | | |
4732 | | /* server sent us an HelloVerifyRequest and we allow downgrade */ |
4733 | | if (ssl->arrays->cookieSz > 0 && ssl->options.downgrade) |
4734 | | args->length += ssl->arrays->cookieSz; |
4735 | | } |
4736 | | #endif /* WOLFSSL_DTLS13 */ |
4737 | | |
4738 | | /* Advance state and proceed */ |
4739 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
4740 | 0 | } /* case TLS_ASYNC_BEGIN */ |
4741 | 0 | FALL_THROUGH; |
4742 | |
|
4743 | 0 | case TLS_ASYNC_BUILD: |
4744 | 0 | case TLS_ASYNC_DO: |
4745 | 0 | { |
4746 | | /* Auto populate extensions supported unless user defined. */ |
4747 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0) |
4748 | 0 | return ret; |
4749 | | |
4750 | | /* Advance state and proceed */ |
4751 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
4752 | 0 | } /* case TLS_ASYNC_BUILD */ |
4753 | 0 | FALL_THROUGH; |
4754 | |
|
4755 | 0 | case TLS_ASYNC_FINALIZE: |
4756 | 0 | { |
4757 | | #ifdef WOLFSSL_EARLY_DATA |
4758 | | if (!EarlyDataPossible(ssl)) |
4759 | | ssl->earlyData = no_early_data; |
4760 | | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
4761 | | ssl->earlyData = no_early_data; |
4762 | | if (ssl->earlyData == no_early_data) |
4763 | | TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap); |
4764 | | if (ssl->earlyData != no_early_data && |
4765 | | (ret = TLSX_EarlyData_Use(ssl, 0, 0)) < 0) { |
4766 | | return ret; |
4767 | | } |
4768 | | #endif |
4769 | | #ifdef WOLFSSL_QUIC |
4770 | | if (WOLFSSL_IS_QUIC(ssl) && IsAtLeastTLSv1_3(ssl->version)) { |
4771 | | ret = wolfSSL_quic_add_transport_extensions(ssl, client_hello); |
4772 | | if (ret != 0) |
4773 | | return ret; |
4774 | | } |
4775 | | #endif |
4776 | | |
4777 | | /* find length of outer and inner */ |
4778 | | #if defined(HAVE_ECH) |
4779 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH) { |
4780 | | TLSX* echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
4781 | | if (echX == NULL) |
4782 | | return WOLFSSL_FATAL_ERROR; |
4783 | | |
4784 | | args->ech = (WOLFSSL_ECH*)echX->data; |
4785 | | if (args->ech == NULL) |
4786 | | return WOLFSSL_FATAL_ERROR; |
4787 | | |
4788 | | /* only prepare if we have a chance at acceptance */ |
4789 | | if (ssl->options.echAccepted || args->ech->innerCount == 0) { |
4790 | | word32 encodedLen; |
4791 | | byte downgrade; |
4792 | | |
4793 | | /* ensure that a version less than TLS1.3 is never offered */ |
4794 | | downgrade = ssl->options.downgrade; |
4795 | | ssl->options.downgrade = 0; |
4796 | | |
4797 | | /* set the type to inner */ |
4798 | | args->ech->type = ECH_TYPE_INNER; |
4799 | | args->preXLength = (int)args->length; |
4800 | | |
4801 | | /* get expanded inner size (used for transcript) */ |
4802 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4803 | | if (ret != 0) { |
4804 | | args->ech->type = ECH_TYPE_OUTER; |
4805 | | ssl->options.downgrade = downgrade; |
4806 | | return ret; |
4807 | | } |
4808 | | |
4809 | | /* args->expandedInnerLen carries the length for the hash */ |
4810 | | args->expandedInnerLen = args->length; |
4811 | | if (args->expandedInnerLen > 0xFFFF) { |
4812 | | args->ech->type = ECH_TYPE_OUTER; |
4813 | | ssl->options.downgrade = downgrade; |
4814 | | return BUFFER_E; |
4815 | | } |
4816 | | |
4817 | | /* get encoded inner size */ |
4818 | | args->ech->writeEncoded = 1; |
4819 | | encodedLen = args->preXLength; |
4820 | | ret = TLSX_GetRequestSize(ssl, client_hello, &encodedLen); |
4821 | | args->ech->writeEncoded = 0; |
4822 | | /* set the type to outer */ |
4823 | | args->ech->type = ECH_TYPE_OUTER; |
4824 | | ssl->options.downgrade = downgrade; |
4825 | | if (ret != 0) |
4826 | | return ret; |
4827 | | |
4828 | | /* calculate padding (RFC 9849, section 6.1.3) */ |
4829 | | if (args->ech->privateName != NULL) { |
4830 | | word16 nameLen = (word16)XSTRLEN(args->ech->privateName); |
4831 | | if (nameLen > args->ech->echConfig->maxNameLen) |
4832 | | args->ech->paddingLen = 0; |
4833 | | else |
4834 | | args->ech->paddingLen = |
4835 | | (word16)args->ech->echConfig->maxNameLen - nameLen; |
4836 | | } |
4837 | | else { |
4838 | | args->ech->paddingLen = args->ech->echConfig->maxNameLen + 9; |
4839 | | } |
4840 | | |
4841 | | /* innerClientHelloLen and padding are based on the |
4842 | | * encoded (sealed) inner */ |
4843 | | args->ech->paddingLen += 31 - |
4844 | | ((encodedLen + args->ech->paddingLen - 1) % 32); |
4845 | | args->ech->innerClientHelloLen = encodedLen + |
4846 | | args->ech->paddingLen + args->ech->hpke->Nt; |
4847 | | |
4848 | | if (args->ech->innerClientHelloLen > 0xFFFF) |
4849 | | return BUFFER_E; |
4850 | | |
4851 | | /* restore the length to pre-ClientHelloInner computations */ |
4852 | | args->length = (word32)args->preXLength; |
4853 | | } |
4854 | | } |
4855 | | #endif |
4856 | |
|
4857 | 0 | { |
4858 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4859 | | word16 maxFrag = wolfssl_local_GetMaxPlaintextSize(ssl); |
4860 | | word16 lenWithoutExts = args->length; |
4861 | | #endif |
4862 | | |
4863 | | /* Include length of TLS extensions. */ |
4864 | 0 | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4865 | 0 | if (ret != 0) |
4866 | 0 | return ret; |
4867 | | |
4868 | | /* Total message size. */ |
4869 | 0 | args->sendSz = |
4870 | 0 | (int)(args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ); |
4871 | |
|
4872 | | #ifdef WOLFSSL_DTLS13 |
4873 | | if (ssl->options.dtls) |
4874 | | args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA; |
4875 | | #endif /* WOLFSSL_DTLS13 */ |
4876 | |
|
4877 | | #ifdef WOLFSSL_DTLS_CH_FRAG |
4878 | | if (ssl->options.dtls && args->sendSz > maxFrag && |
4879 | | TLSX_Find(ssl->extensions, TLSX_COOKIE) == NULL) { |
4880 | | /* Try again with an empty key share if we would be fragmenting |
4881 | | * without a cookie */ |
4882 | | ret = TLSX_KeyShare_Empty(ssl); |
4883 | | if (ret != 0) |
4884 | | return ret; |
4885 | | args->length = lenWithoutExts; |
4886 | | ret = TLSX_GetRequestSize(ssl, client_hello, &args->length); |
4887 | | if (ret != 0) |
4888 | | return ret; |
4889 | | args->sendSz = (int)(args->length + |
4890 | | DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ); |
4891 | | if (args->sendSz > maxFrag) { |
4892 | | WOLFSSL_MSG("Can't fit first CH in one fragment."); |
4893 | | return BUFFER_ERROR; |
4894 | | } |
4895 | | WOLFSSL_MSG("Sending empty key share so we don't fragment CH1"); |
4896 | | } |
4897 | | #endif |
4898 | 0 | } |
4899 | | |
4900 | | /* Check buffers are big enough and grow if needed. */ |
4901 | 0 | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) |
4902 | 0 | return ret; |
4903 | | |
4904 | | /* Get position in output buffer to write new message to. */ |
4905 | 0 | args->output = GetOutputBuffer(ssl); |
4906 | | |
4907 | | /* Put the record and handshake headers on. */ |
4908 | 0 | AddTls13Headers(args->output, args->length, client_hello, ssl); |
4909 | | |
4910 | | /* Protocol version - negotiation now in extension: supported_versions. */ |
4911 | 0 | args->output[args->idx++] = major; |
4912 | 0 | args->output[args->idx++] = tls12minor; |
4913 | | |
4914 | | /* Keep for downgrade. */ |
4915 | 0 | ssl->chVersion = ssl->version; |
4916 | |
|
4917 | 0 | if (ssl->arrays == NULL) { |
4918 | 0 | return BAD_FUNC_ARG; |
4919 | 0 | } |
4920 | | /* Client Random */ |
4921 | 0 | if (ssl->options.connectState == CONNECT_BEGIN) { |
4922 | 0 | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + args->idx, RAN_LEN); |
4923 | 0 | if (ret != 0) |
4924 | 0 | return ret; |
4925 | | |
4926 | | /* Store random for possible second ClientHello. */ |
4927 | 0 | XMEMCPY(ssl->arrays->clientRandom, args->output + args->idx, RAN_LEN); |
4928 | 0 | } |
4929 | 0 | else |
4930 | 0 | XMEMCPY(args->output + args->idx, ssl->arrays->clientRandom, RAN_LEN); |
4931 | | |
4932 | | #if defined(HAVE_ECH) |
4933 | | args->clientRandomOffset = (int)args->idx; |
4934 | | #endif |
4935 | | |
4936 | 0 | args->idx += RAN_LEN; |
4937 | |
|
4938 | 0 | GetTls13SessionId(ssl, args->output, &args->idx); |
4939 | |
|
4940 | | #ifdef WOLFSSL_DTLS13 |
4941 | | if (ssl->options.dtls) { |
4942 | | args->output[args->idx++] = ssl->arrays->cookieSz; |
4943 | | |
4944 | | if (ssl->arrays->cookieSz > 0) { |
4945 | | /* We have a cookie saved, so the server sent us an |
4946 | | * HelloVerifyRequest, it means it is a v1.2 server */ |
4947 | | if (!ssl->options.downgrade) |
4948 | | return VERSION_ERROR; |
4949 | | XMEMCPY(args->output + args->idx, ssl->arrays->cookie, |
4950 | | ssl->arrays->cookieSz); |
4951 | | args->idx += ssl->arrays->cookieSz; |
4952 | | } |
4953 | | } |
4954 | | #endif /* WOLFSSL_DTLS13 */ |
4955 | | |
4956 | | /* Cipher suites */ |
4957 | 0 | c16toa(suites->suiteSz, args->output + args->idx); |
4958 | 0 | args->idx += OPAQUE16_LEN; |
4959 | 0 | XMEMCPY(args->output + args->idx, &suites->suites, |
4960 | 0 | suites->suiteSz); |
4961 | 0 | args->idx += suites->suiteSz; |
4962 | | #ifdef WOLFSSL_DEBUG_TLS |
4963 | | { |
4964 | | int ii; |
4965 | | WOLFSSL_MSG("Ciphers:"); |
4966 | | for (ii = 0 ; ii < suites->suiteSz; ii += 2) { |
4967 | | WOLFSSL_MSG(GetCipherNameInternal(suites->suites[ii+0], |
4968 | | suites->suites[ii+1])); |
4969 | | } |
4970 | | } |
4971 | | #endif |
4972 | | |
4973 | | /* Compression not supported in TLS v1.3. */ |
4974 | 0 | args->output[args->idx++] = COMP_LEN; |
4975 | 0 | args->output[args->idx++] = NO_COMPRESSION; |
4976 | |
|
4977 | | #if defined(HAVE_ECH) |
4978 | | /* Build the expanded inner ClientHello */ |
4979 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
4980 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
4981 | | byte downgrade; |
4982 | | |
4983 | | /* calculate maximum buffer size needed */ |
4984 | | word32 encodedBodyLen = args->ech->innerClientHelloLen - |
4985 | | args->ech->hpke->Nt; |
4986 | | word32 innerBufSize = args->expandedInnerLen; |
4987 | | if (encodedBodyLen > innerBufSize) |
4988 | | innerBufSize = encodedBodyLen; |
4989 | | |
4990 | | /* set the type to inner */ |
4991 | | args->ech->type = ECH_TYPE_INNER; |
4992 | | /* innerClientHello may already exist from hrr, free if it does */ |
4993 | | if (args->ech->innerClientHello != NULL) { |
4994 | | XFREE(args->ech->innerClientHello, ssl->heap, |
4995 | | DYNAMIC_TYPE_TMP_BUFFER); |
4996 | | } |
4997 | | /* allocate the inner */ |
4998 | | args->ech->innerClientHello = |
4999 | | (byte*)XMALLOC(innerBufSize, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
5000 | | if (args->ech->innerClientHello == NULL) { |
5001 | | args->ech->type = ECH_TYPE_OUTER; |
5002 | | return MEMORY_E; |
5003 | | } |
5004 | | /* copy everything before extensions into the innerClientHello |
5005 | | * ignore record and handshake headers */ |
5006 | | XMEMCPY(args->ech->innerClientHello, |
5007 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
5008 | | args->preXLength); |
5009 | | /* copy the client random to inner - only for first CH, not after HRR */ |
5010 | | if (!ssl->options.echAccepted) { |
5011 | | XMEMCPY(ssl->arrays->clientRandomInner, ssl->arrays->clientRandom, |
5012 | | RAN_LEN); |
5013 | | } |
5014 | | else { |
5015 | | /* After HRR, use the same inner random as CH1 */ |
5016 | | XMEMCPY(args->ech->innerClientHello + VERSION_SZ, |
5017 | | ssl->arrays->clientRandomInner, RAN_LEN); |
5018 | | } |
5019 | | /* change the outer client random */ |
5020 | | ret = wc_RNG_GenerateBlock(ssl->rng, args->output + |
5021 | | args->clientRandomOffset, RAN_LEN); |
5022 | | if (ret != 0) { |
5023 | | args->ech->type = ECH_TYPE_OUTER; |
5024 | | return ret; |
5025 | | } |
5026 | | /* copy the new client random */ |
5027 | | XMEMCPY(ssl->arrays->clientRandom, args->output + |
5028 | | args->clientRandomOffset, RAN_LEN); |
5029 | | |
5030 | | /* ensure that a version less than TLS1.3 is never offered */ |
5031 | | downgrade = ssl->options.downgrade; |
5032 | | ssl->options.downgrade = 0; |
5033 | | |
5034 | | /* write the expanded extensions into the inner buffer */ |
5035 | | args->length = 0; |
5036 | | ret = TLSX_WriteRequest(ssl, |
5037 | | args->ech->innerClientHello + args->preXLength, client_hello, |
5038 | | &args->length); |
5039 | | if (ret != 0) { |
5040 | | args->ech->type = ECH_TYPE_OUTER; |
5041 | | ssl->options.downgrade = downgrade; |
5042 | | return ret; |
5043 | | } |
5044 | | |
5045 | | /* hash expanded form */ |
5046 | | args->ech->innerClientHelloLen = args->expandedInnerLen; |
5047 | | ret = EchHashHelloInner(ssl, args->ech); |
5048 | | args->ech->innerClientHelloLen = encodedBodyLen + args->ech->hpke->Nt; |
5049 | | if (ret != 0) { |
5050 | | args->ech->type = ECH_TYPE_OUTER; |
5051 | | ssl->options.downgrade = downgrade; |
5052 | | return ret; |
5053 | | } |
5054 | | |
5055 | | /* zero padding bytes sealed with the inner hello */ |
5056 | | XMEMSET(args->ech->innerClientHello + |
5057 | | args->ech->innerClientHelloLen - args->ech->hpke->Nt - |
5058 | | args->ech->paddingLen, 0, args->ech->paddingLen); |
5059 | | /* Rewrite inner buffer with the encoded form for sealing */ |
5060 | | args->ech->writeEncoded = 1; |
5061 | | args->length = 0; |
5062 | | ret = TLSX_WriteRequest(ssl, |
5063 | | args->ech->innerClientHello + args->preXLength, client_hello, |
5064 | | &args->length); |
5065 | | args->ech->writeEncoded = 0; |
5066 | | /* set the type to outer */ |
5067 | | args->ech->type = ECH_TYPE_OUTER; |
5068 | | ssl->options.downgrade = downgrade; |
5069 | | if (ret != 0) |
5070 | | return ret; |
5071 | | } |
5072 | | #endif |
5073 | | |
5074 | | /* Write out extensions for a request. */ |
5075 | 0 | args->length = 0; |
5076 | 0 | ret = TLSX_WriteRequest(ssl, args->output + args->idx, client_hello, |
5077 | 0 | &args->length); |
5078 | 0 | if (ret != 0) |
5079 | 0 | return ret; |
5080 | | |
5081 | 0 | args->idx += args->length; |
5082 | |
|
5083 | | #if defined(HAVE_ECH) |
5084 | | /* HPKE-seal inner hello and place into outer ECH extension's payload */ |
5085 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5086 | | (ssl->options.echAccepted || args->ech->innerCount == 0)) { |
5087 | | #if defined(WOLFSSL_TEST_ECH) |
5088 | | if (ssl->echInnerHelloCb != NULL) { |
5089 | | ret = ssl->echInnerHelloCb(args->ech->innerClientHello, |
5090 | | args->ech->innerClientHelloLen - args->ech->hpke->Nt); |
5091 | | if (ret != 0) |
5092 | | return ret; |
5093 | | } |
5094 | | #endif |
5095 | | ret = TLSX_FinalizeEch(args->ech, |
5096 | | args->output + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ, |
5097 | | (word32)(args->sendSz - (RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ))); |
5098 | | |
5099 | | if (ret != 0) |
5100 | | return ret; |
5101 | | |
5102 | | /* innerCount gates HRR re-prep and the server's copyRandom logic. */ |
5103 | | args->ech->innerCount = 1; |
5104 | | } |
5105 | | #endif |
5106 | |
|
5107 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5108 | | /* Resumption has a specific set of extensions and binder is calculated |
5109 | | * for each identity. |
5110 | | */ |
5111 | | if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY)) { |
5112 | | ret = WritePSKBinders(ssl, args->output, args->idx); |
5113 | | } |
5114 | | else |
5115 | | #endif |
5116 | 0 | { |
5117 | | #ifdef WOLFSSL_DTLS13 |
5118 | | if (ssl->options.dtls) |
5119 | | ret = Dtls13HashHandshake(ssl, |
5120 | | args->output + Dtls13GetRlHeaderLength(ssl, 0), |
5121 | | (word16)args->idx - Dtls13GetRlHeaderLength(ssl, 0)); |
5122 | | else |
5123 | | #endif /* WOLFSSL_DTLS13 */ |
5124 | 0 | { |
5125 | | /* compute the outer hash */ |
5126 | 0 | ret = HashOutput(ssl, args->output, (int)args->idx, 0); |
5127 | 0 | } |
5128 | 0 | } |
5129 | 0 | if (ret != 0) |
5130 | 0 | return ret; |
5131 | | |
5132 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
5133 | |
|
5134 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
5135 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
5136 | | if (ssl->toInfoOn) { |
5137 | | ret = AddPacketInfo(ssl, "ClientHello", handshake, args->output, |
5138 | | args->sendSz, WRITE_PROTO, 0, ssl->heap); |
5139 | | if (ret != 0) |
5140 | | return ret; |
5141 | | } |
5142 | | #endif |
5143 | |
|
5144 | 0 | ssl->options.buildingMsg = 0; |
5145 | | #ifdef WOLFSSL_DTLS13 |
5146 | | if (ssl->options.dtls) { |
5147 | | ret = Dtls13HandshakeSend(ssl, args->output, (word16)args->sendSz, |
5148 | | (word16)args->idx, client_hello, 0); |
5149 | | break; |
5150 | | } |
5151 | | #endif /* WOLFSSL_DTLS13 */ |
5152 | |
|
5153 | 0 | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
5154 | | |
5155 | | /* Advance state and proceed */ |
5156 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
5157 | 0 | } |
5158 | | /* case TLS_ASYNC_BUILD */ |
5159 | 0 | FALL_THROUGH; |
5160 | |
|
5161 | 0 | case TLS_ASYNC_END: |
5162 | 0 | { |
5163 | | #ifdef WOLFSSL_EARLY_DATA_GROUP |
5164 | | /* QUIC needs to forward records at their encryption level |
5165 | | * and is therefore unable to group here */ |
5166 | | if (ssl->earlyData == no_early_data || WOLFSSL_IS_QUIC(ssl)) |
5167 | | #endif |
5168 | 0 | ret = SendBuffered(ssl); |
5169 | |
|
5170 | 0 | break; |
5171 | 0 | } |
5172 | 0 | default: |
5173 | 0 | ret = INPUT_CASE_ERROR; |
5174 | 0 | } /* switch (ssl->options.asyncState) */ |
5175 | | |
5176 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5177 | | if (ret == 0) |
5178 | | FreeAsyncCtx(ssl, 0); |
5179 | | #endif |
5180 | | |
5181 | 0 | WOLFSSL_LEAVE("SendTls13ClientHello", ret); |
5182 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND); |
5183 | |
|
5184 | 0 | return ret; |
5185 | 0 | } |
5186 | | |
5187 | | #if defined(WOLFSSL_DTLS13) && !defined(NO_WOLFSSL_CLIENT) |
5188 | | static int Dtls13ClientDoDowngrade(WOLFSSL* ssl) |
5189 | | { |
5190 | | int ret; |
5191 | | if (ssl->dtls13ClientHello == NULL) |
5192 | | return BAD_STATE_E; |
5193 | | |
5194 | | /* v1.3 and v1.2 hash messages to compute the transcript hash. When we are |
5195 | | * using DTLSv1.3 we hash the first clientHello following v1.3 but the |
5196 | | * server can negotiate a lower version. So we need to re-hash the |
5197 | | * clientHello to adhere to DTLS <= v1.2 rules. */ |
5198 | | ret = InitHandshakeHashes(ssl); |
5199 | | if (ret != 0) |
5200 | | return ret; |
5201 | | ret = HashRaw(ssl, ssl->dtls13ClientHello, ssl->dtls13ClientHelloSz); |
5202 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5203 | | ssl->dtls13ClientHello = NULL; |
5204 | | ssl->dtls13ClientHelloSz = 0; |
5205 | | ssl->keys.dtls_sequence_number_hi = |
5206 | | (word16)w64GetHigh32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5207 | | ssl->keys.dtls_sequence_number_lo = |
5208 | | w64GetLow32(ssl->dtls13EncryptEpoch->nextSeqNumber); |
5209 | | return ret; |
5210 | | } |
5211 | | #endif /* WOLFSSL_DTLS13 && !NO_WOLFSSL_CLIENT*/ |
5212 | | |
5213 | | #if defined(HAVE_ECH) |
5214 | | /* Calculate ECH acceptance and verify the server accepted ECH. |
5215 | | * |
5216 | | * ssl SSL/TLS object. |
5217 | | * label Ascii string describing ECH acceptance type. |
5218 | | * labelSz Length of label excluding NULL character. |
5219 | | * input The buffer to calculate confirmation off of. |
5220 | | * acceptOffset Where the 8 ECH confirmation bytes start. |
5221 | | * helloSz Size of hello message. |
5222 | | * returns 0 on success and otherwise failure. |
5223 | | */ |
5224 | | static int EchCheckAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
5225 | | const byte* input, int acceptOffset, int helloSz, byte msgType) |
5226 | | { |
5227 | | int ret = 0; |
5228 | | int headerSz; |
5229 | | HS_Hashes* tmpHashes; |
5230 | | byte acceptConfirmation[ECH_ACCEPT_CONFIRMATION_SZ]; |
5231 | | |
5232 | | XMEMSET(acceptConfirmation, 0, sizeof(acceptConfirmation)); |
5233 | | |
5234 | | #ifdef WOLFSSL_DTLS13 |
5235 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
5236 | | HANDSHAKE_HEADER_SZ; |
5237 | | #else |
5238 | | headerSz = HANDSHAKE_HEADER_SZ; |
5239 | | #endif |
5240 | | |
5241 | | ret = EchCalcAcceptance(ssl, label, labelSz, input, acceptOffset, helloSz, |
5242 | | msgType == hello_retry_request, acceptConfirmation); |
5243 | | |
5244 | | if (ret == 0) { |
5245 | | tmpHashes = ssl->hsHashes; |
5246 | | ssl->hsHashes = ssl->hsHashesEch; |
5247 | | |
5248 | | /* last 8 bytes must match the expand output */ |
5249 | | ret = ConstantCompare(acceptConfirmation, input + acceptOffset, |
5250 | | ECH_ACCEPT_CONFIRMATION_SZ); |
5251 | | |
5252 | | if (ret == 0) { |
5253 | | WOLFSSL_MSG("ECH accepted"); |
5254 | | ssl->options.echAccepted = 1; |
5255 | | |
5256 | | /* after HRR, hsHashesEch must contain: |
5257 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
5258 | | if (msgType == hello_retry_request) { |
5259 | | ret = HashRaw(ssl, input, helloSz + headerSz); |
5260 | | } |
5261 | | /* normal TLS code will calculate transcript of ServerHello */ |
5262 | | else { |
5263 | | ssl->hsHashes = tmpHashes; |
5264 | | FreeHandshakeHashes(ssl); |
5265 | | tmpHashes = ssl->hsHashesEch; |
5266 | | ssl->hsHashesEch = NULL; |
5267 | | } |
5268 | | } |
5269 | | else { |
5270 | | if (msgType != hello_retry_request && ssl->options.echAccepted) { |
5271 | | /* the SH has rejected ECH after the HRR has accepted it |
5272 | | * RFC 9849, section 6.1.5 */ |
5273 | | WOLFSSL_MSG("ECH rejected, but it was previously accepted..."); |
5274 | | ret = INVALID_PARAMETER; |
5275 | | } |
5276 | | else { |
5277 | | WOLFSSL_MSG("ECH rejected"); |
5278 | | ret = 0; |
5279 | | } |
5280 | | ssl->options.echAccepted = 0; |
5281 | | |
5282 | | /* ECH rejected, continue with outer transcript */ |
5283 | | FreeHandshakeHashes(ssl); |
5284 | | ssl->hsHashesEch = NULL; |
5285 | | } |
5286 | | |
5287 | | ssl->hsHashes = tmpHashes; |
5288 | | } |
5289 | | |
5290 | | return ret; |
5291 | | } |
5292 | | #endif /* HAVE_ECH */ |
5293 | | |
5294 | | /* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */ |
5295 | | /* Handle the ServerHello message from the server. |
5296 | | * Only a client will receive this message. |
5297 | | * |
5298 | | * ssl The SSL/TLS object. |
5299 | | * input The message buffer. |
5300 | | * inOutIdx On entry, the index into the message buffer of ServerHello. |
5301 | | * On exit, the index of byte after the ServerHello message. |
5302 | | * helloSz The length of the current handshake message. |
5303 | | * returns 0 on success and otherwise failure. |
5304 | | */ |
5305 | | |
5306 | | typedef struct Dsh13Args { |
5307 | | ProtocolVersion pv; |
5308 | | word32 idx; |
5309 | | word32 begin; |
5310 | | const byte* sessId; |
5311 | | word16 totalExtSz; |
5312 | | byte sessIdSz; |
5313 | | byte extMsgType; |
5314 | | #if defined(HAVE_ECH) |
5315 | | TLSX* echX; |
5316 | | byte* acceptLabel; |
5317 | | word32 acceptOffset; |
5318 | | word16 acceptLabelSz; |
5319 | | #endif |
5320 | | } Dsh13Args; |
5321 | | |
5322 | | int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
5323 | | word32 helloSz, byte* extMsgType) |
5324 | 0 | { |
5325 | 0 | int ret; |
5326 | 0 | byte suite[2]; |
5327 | 0 | byte tls12minor; |
5328 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5329 | | Dsh13Args* args = NULL; |
5330 | | #else |
5331 | 0 | Dsh13Args args[1]; |
5332 | 0 | #endif |
5333 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5334 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
5335 | | #endif |
5336 | |
|
5337 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO); |
5338 | 0 | WOLFSSL_ENTER("DoTls13ServerHello"); |
5339 | |
|
5340 | 0 | if (ssl == NULL || ssl->arrays == NULL) |
5341 | 0 | return BAD_FUNC_ARG; |
5342 | | |
5343 | 0 | tls12minor = TLSv1_2_MINOR; |
5344 | |
|
5345 | | #ifdef WOLFSSL_DTLS13 |
5346 | | if (ssl->options.dtls) |
5347 | | tls12minor = DTLSv1_2_MINOR; |
5348 | | #endif /* WOLFSSL_DTLS13 */ |
5349 | |
|
5350 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5351 | | if (ssl->async == NULL) { |
5352 | | ssl->async = (struct WOLFSSL_ASYNC*) |
5353 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
5354 | | DYNAMIC_TYPE_ASYNC); |
5355 | | if (ssl->async == NULL) |
5356 | | return MEMORY_E; |
5357 | | ssl->async->freeArgs = NULL; |
5358 | | } |
5359 | | args = (Dsh13Args*)ssl->async->args; |
5360 | | |
5361 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
5362 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
5363 | | /* Check for error */ |
5364 | | if (ret < 0) { |
5365 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5366 | | /* Mark message as not received so it can process again */ |
5367 | | ssl->msgsReceived.got_server_hello = 0; |
5368 | | } |
5369 | | return ret; |
5370 | | } |
5371 | | } |
5372 | | else |
5373 | | #endif |
5374 | 0 | { |
5375 | | /* Reset state */ |
5376 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
5377 | 0 | XMEMSET(args, 0, sizeof(Dsh13Args)); |
5378 | 0 | } |
5379 | |
|
5380 | 0 | switch (ssl->options.asyncState) { |
5381 | 0 | case TLS_ASYNC_BEGIN: |
5382 | 0 | { |
5383 | 0 | byte b; |
5384 | | #ifdef WOLFSSL_CALLBACKS |
5385 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello"); |
5386 | | if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo); |
5387 | | #endif |
5388 | | |
5389 | | /* Protocol version length check. */ |
5390 | 0 | if (helloSz < OPAQUE16_LEN) |
5391 | 0 | return BUFFER_ERROR; |
5392 | | |
5393 | 0 | args->idx = *inOutIdx; |
5394 | 0 | args->begin = args->idx; |
5395 | | |
5396 | | /* Protocol version */ |
5397 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
5398 | 0 | args->idx += OPAQUE16_LEN; |
5399 | |
|
5400 | | #ifdef WOLFSSL_DTLS |
5401 | | if (ssl->options.dtls && |
5402 | | (args->pv.major != DTLS_MAJOR || args->pv.minor == DTLS_BOGUS_MINOR)) |
5403 | | return VERSION_ERROR; |
5404 | | #endif /* WOLFSSL_DTLS */ |
5405 | |
|
5406 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5407 | 0 | { |
5408 | 0 | byte wantDowngrade; |
5409 | |
|
5410 | 0 | wantDowngrade = args->pv.major == ssl->version.major && |
5411 | 0 | args->pv.minor < TLSv1_2_MINOR; |
5412 | |
|
5413 | | #ifdef WOLFSSL_DTLS13 |
5414 | | if (ssl->options.dtls) |
5415 | | wantDowngrade = args->pv.major == ssl->version.major && |
5416 | | args->pv.minor > DTLSv1_2_MINOR; |
5417 | | #endif /* WOLFSSL_DTLS13 */ |
5418 | |
|
5419 | 0 | if (wantDowngrade && ssl->options.downgrade) { |
5420 | | /* Force client hello version 1.2 to work for static RSA. */ |
5421 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5422 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5423 | 0 | ssl->options.tls1_3 = 0; |
5424 | |
|
5425 | | #ifdef WOLFSSL_DTLS13 |
5426 | | if (ssl->options.dtls) { |
5427 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5428 | | ssl->version.minor = DTLSv1_2_MINOR; |
5429 | | ret = Dtls13ClientDoDowngrade(ssl); |
5430 | | if (ret != 0) |
5431 | | return ret; |
5432 | | } |
5433 | | #endif /* WOLFSSL_DTLS13 */ |
5434 | |
|
5435 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5436 | 0 | } |
5437 | 0 | } |
5438 | 0 | #endif |
5439 | | |
5440 | 0 | if (args->pv.major != ssl->version.major || |
5441 | 0 | args->pv.minor != tls12minor) { |
5442 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5443 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5444 | 0 | return VERSION_ERROR; |
5445 | 0 | } |
5446 | | |
5447 | | /* Random and session id length check */ |
5448 | 0 | if ((args->idx - args->begin) + RAN_LEN + ENUM_LEN > helloSz) |
5449 | 0 | return BUFFER_ERROR; |
5450 | | |
5451 | | /* Check if hello retry request */ |
5452 | 0 | if (XMEMCMP(input + args->idx, helloRetryRequestRandom, RAN_LEN) == 0) { |
5453 | 0 | WOLFSSL_MSG("HelloRetryRequest format"); |
5454 | 0 | *extMsgType = hello_retry_request; |
5455 | |
|
5456 | 0 | if (ssl->msgsReceived.got_hello_verify_request) { |
5457 | 0 | WOLFSSL_MSG("Received HelloRetryRequest after a " |
5458 | 0 | "HelloVerifyRequest"); |
5459 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5460 | 0 | return VERSION_ERROR; |
5461 | 0 | } |
5462 | | |
5463 | | /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat. |
5464 | | * Found message to be a HelloRetryRequest. |
5465 | | * Don't allow more than one HelloRetryRequest or ServerHello. |
5466 | | */ |
5467 | 0 | if (ssl->msgsReceived.got_hello_retry_request) { |
5468 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
5469 | 0 | return DUPLICATE_MSG_E; |
5470 | 0 | } |
5471 | 0 | } |
5472 | 0 | args->extMsgType = *extMsgType; |
5473 | | |
5474 | | /* Server random - keep for debugging. */ |
5475 | 0 | XMEMCPY(ssl->arrays->serverRandom, input + args->idx, RAN_LEN); |
5476 | | #if defined(HAVE_ECH) |
5477 | | /* last 8 bytes of server random */ |
5478 | | args->acceptOffset = args->idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
5479 | | #endif |
5480 | 0 | args->idx += RAN_LEN; |
5481 | | |
5482 | | /* Session id */ |
5483 | 0 | args->sessIdSz = input[args->idx++]; |
5484 | 0 | if (args->sessIdSz > ID_LEN || args->sessIdSz > RAN_LEN || |
5485 | 0 | ((args->idx - args->begin) + args->sessIdSz > helloSz)) |
5486 | 0 | return BUFFER_ERROR; |
5487 | 0 | args->sessId = input + args->idx; |
5488 | 0 | args->idx += args->sessIdSz; |
5489 | |
|
5490 | 0 | ssl->options.haveSessionId = 1; |
5491 | | |
5492 | | /* Ciphersuite and compression check */ |
5493 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz) |
5494 | 0 | return BUFFER_ERROR; |
5495 | | |
5496 | | /* Set the cipher suite from the message. */ |
5497 | 0 | ssl->options.cipherSuite0 = input[args->idx++]; |
5498 | 0 | ssl->options.cipherSuite = input[args->idx++]; |
5499 | 0 | if (*extMsgType == hello_retry_request) { |
5500 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
5501 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
5502 | 0 | } |
5503 | 0 | else if (ssl->msgsReceived.got_hello_retry_request && |
5504 | 0 | (ssl->options.hrrCipherSuite0 != ssl->options.cipherSuite0 || |
5505 | 0 | ssl->options.hrrCipherSuite != ssl->options.cipherSuite)) { |
5506 | 0 | WOLFSSL_MSG("Received ServerHello with different cipher suite than " |
5507 | 0 | "HelloRetryRequest"); |
5508 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5509 | 0 | return INVALID_PARAMETER; |
5510 | 0 | } |
5511 | | #ifdef WOLFSSL_DEBUG_TLS |
5512 | | WOLFSSL_MSG("Chosen cipher suite:"); |
5513 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
5514 | | ssl->options.cipherSuite)); |
5515 | | #endif |
5516 | | |
5517 | | /* Compression */ |
5518 | 0 | b = input[args->idx++]; |
5519 | 0 | if (b != 0) { |
5520 | 0 | WOLFSSL_MSG("Must be no compression types in list"); |
5521 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5522 | 0 | return INVALID_PARAMETER; |
5523 | 0 | } |
5524 | | |
5525 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) { |
5526 | 0 | if (!ssl->options.downgrade) |
5527 | 0 | return BUFFER_ERROR; |
5528 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5529 | | /* Force client hello version 1.2 to work for static RSA. */ |
5530 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5531 | 0 | ssl->version.minor = TLSv1_2_MINOR; |
5532 | |
|
5533 | | #ifdef WOLFSSL_DTLS13 |
5534 | | if (ssl->options.dtls) { |
5535 | | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5536 | | ssl->version.minor = DTLSv1_2_MINOR; |
5537 | | ssl->options.tls1_3 = 0; |
5538 | | ret = Dtls13ClientDoDowngrade(ssl); |
5539 | | if (ret != 0) |
5540 | | return ret; |
5541 | | } |
5542 | | #endif /* WOLFSSL_DTLS13 */ |
5543 | |
|
5544 | 0 | #endif |
5545 | 0 | ssl->options.haveEMS = 0; |
5546 | 0 | if (args->pv.minor < ssl->options.minDowngrade) { |
5547 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5548 | 0 | return VERSION_ERROR; |
5549 | 0 | } |
5550 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5551 | 0 | ssl->options.tls1_3 = 0; |
5552 | 0 | return DoServerHello(ssl, input, inOutIdx, helloSz); |
5553 | | #else |
5554 | | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5555 | | return VERSION_ERROR; |
5556 | | #endif |
5557 | 0 | } |
5558 | | |
5559 | 0 | if ((args->idx - args->begin) < helloSz) { |
5560 | 0 | int foundVersion; |
5561 | | |
5562 | | /* Get extension length and length check. */ |
5563 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
5564 | 0 | return BUFFER_ERROR; |
5565 | 0 | ato16(&input[args->idx], &args->totalExtSz); |
5566 | 0 | args->idx += OPAQUE16_LEN; |
5567 | 0 | if ((args->idx - args->begin) + args->totalExtSz > helloSz) |
5568 | 0 | return BUFFER_ERROR; |
5569 | | |
5570 | | /* Need to negotiate version first. */ |
5571 | 0 | if ((ret = TLSX_ParseVersion(ssl, input + args->idx, |
5572 | 0 | args->totalExtSz, *extMsgType, &foundVersion))) { |
5573 | 0 | return ret; |
5574 | 0 | } |
5575 | 0 | if (!foundVersion) { |
5576 | 0 | if (!ssl->options.downgrade) { |
5577 | 0 | WOLFSSL_MSG("Server trying to downgrade to version less than " |
5578 | 0 | "TLS v1.3"); |
5579 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5580 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5581 | 0 | return VERSION_ERROR; |
5582 | 0 | } |
5583 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \ |
5584 | | defined(WOLFSSL_WPAS_SMALL) |
5585 | | /* Check if client has disabled TLS 1.2 */ |
5586 | | if (args->pv.minor == TLSv1_2_MINOR && |
5587 | | (ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) |
5588 | | == WOLFSSL_OP_NO_TLSv1_2) |
5589 | | { |
5590 | | WOLFSSL_MSG("\tOption set to not allow TLSv1.2"); |
5591 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5592 | | return VERSION_ERROR; |
5593 | | } |
5594 | | #endif |
5595 | | |
5596 | 0 | if (!ssl->options.dtls && |
5597 | 0 | args->pv.minor < ssl->options.minDowngrade) { |
5598 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5599 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5600 | 0 | return VERSION_ERROR; |
5601 | 0 | } |
5602 | | |
5603 | 0 | if (ssl->options.dtls && |
5604 | 0 | args->pv.minor > ssl->options.minDowngrade) { |
5605 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
5606 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5607 | 0 | return VERSION_ERROR; |
5608 | 0 | } |
5609 | | |
5610 | 0 | ssl->version.minor = args->pv.minor; |
5611 | 0 | ssl->options.tls1_3 = 0; |
5612 | |
|
5613 | | #ifdef WOLFSSL_DTLS13 |
5614 | | if (ssl->options.dtls) { |
5615 | | ret = Dtls13ClientDoDowngrade(ssl); |
5616 | | if (ret != 0) |
5617 | | return ret; |
5618 | | } |
5619 | | #endif /* WOLFSSL_DTLS13 */ |
5620 | 0 | } |
5621 | 0 | } |
5622 | | |
5623 | | #ifdef WOLFSSL_DTLS13 |
5624 | | /* we are sure that version is >= v1.3 now, we can get rid of buffered |
5625 | | * ClientHello that was buffered to re-compute the hash in case of |
5626 | | * downgrade */ |
5627 | | if (ssl->options.dtls && ssl->dtls13ClientHello != NULL) { |
5628 | | XFREE(ssl->dtls13ClientHello, ssl->heap, DYNAMIC_TYPE_DTLS_MSG); |
5629 | | ssl->dtls13ClientHello = NULL; |
5630 | | ssl->dtls13ClientHelloSz = 0; |
5631 | | } |
5632 | | #endif /* WOLFSSL_DTLS13 */ |
5633 | | |
5634 | | /* Advance state and proceed */ |
5635 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
5636 | 0 | } /* case TLS_ASYNC_BEGIN */ |
5637 | 0 | FALL_THROUGH; |
5638 | |
|
5639 | 0 | case TLS_ASYNC_BUILD: |
5640 | 0 | case TLS_ASYNC_DO: |
5641 | 0 | { |
5642 | | /* restore message type */ |
5643 | 0 | *extMsgType = args->extMsgType; |
5644 | | |
5645 | | /* Parse and handle extensions, unless lower than TLS1.3. In that case, |
5646 | | * extensions will be parsed in DoServerHello. */ |
5647 | 0 | if (args->totalExtSz > 0 && IsAtLeastTLSv1_3(ssl->version)) { |
5648 | 0 | ret = TLSX_Parse(ssl, input + args->idx, args->totalExtSz, |
5649 | 0 | *extMsgType, NULL); |
5650 | 0 | if (ret != 0) { |
5651 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5652 | | /* Handle async operation */ |
5653 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
5654 | | /* Mark message as not received so it can process again */ |
5655 | | ssl->msgsReceived.got_server_hello = 0; |
5656 | | } |
5657 | | #endif |
5658 | 0 | return ret; |
5659 | 0 | } |
5660 | | |
5661 | 0 | if (*extMsgType == hello_retry_request) { |
5662 | | /* Update counts to reflect change of message type. */ |
5663 | 0 | ssl->msgsReceived.got_hello_retry_request = 1; |
5664 | 0 | ssl->msgsReceived.got_server_hello = 0; |
5665 | 0 | } |
5666 | 0 | } |
5667 | | |
5668 | 0 | if (args->totalExtSz > 0) { |
5669 | 0 | args->idx += args->totalExtSz; |
5670 | 0 | } |
5671 | |
|
5672 | | #ifdef WOLFSSL_DTLS_CID |
5673 | | if (ssl->options.useDtlsCID && *extMsgType == server_hello) |
5674 | | DtlsCIDOnExtensionsParsed(ssl); |
5675 | | #endif /* WOLFSSL_DTLS_CID */ |
5676 | |
|
5677 | 0 | if (IsAtLeastTLSv1_3(ssl->version)) { |
5678 | 0 | *inOutIdx = args->idx; |
5679 | 0 | } |
5680 | |
|
5681 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5682 | |
|
5683 | | #ifdef HAVE_SECRET_CALLBACK |
5684 | | if (ssl->sessionSecretCb != NULL |
5685 | | #ifdef HAVE_SESSION_TICKET |
5686 | | && ssl->session->ticketLen > 0 |
5687 | | #endif |
5688 | | ) { |
5689 | | int secretSz = SECRET_LEN; |
5690 | | ret = ssl->sessionSecretCb(ssl, ssl->session->masterSecret, |
5691 | | &secretSz, ssl->sessionSecretCtx); |
5692 | | if (ret != 0 || secretSz != SECRET_LEN) { |
5693 | | WOLFSSL_ERROR_VERBOSE(SESSION_SECRET_CB_E); |
5694 | | return SESSION_SECRET_CB_E; |
5695 | | } |
5696 | | } |
5697 | | #endif /* HAVE_SECRET_CALLBACK */ |
5698 | | |
5699 | | /* Version only negotiated in extensions for TLS v1.3. |
5700 | | * Only now do we know how to deal with session id. |
5701 | | */ |
5702 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) { |
5703 | 0 | #ifndef WOLFSSL_NO_TLS12 |
5704 | 0 | ssl->arrays->sessionIDSz = args->sessIdSz; |
5705 | |
|
5706 | 0 | if (ssl->arrays->sessionIDSz > ID_LEN) { |
5707 | 0 | WOLFSSL_MSG("Invalid session ID size"); |
5708 | 0 | ssl->arrays->sessionIDSz = 0; |
5709 | 0 | return BUFFER_ERROR; |
5710 | 0 | } |
5711 | 0 | else if (ssl->arrays->sessionIDSz) { |
5712 | 0 | XMEMCPY(ssl->arrays->sessionID, args->sessId, |
5713 | 0 | ssl->arrays->sessionIDSz); |
5714 | 0 | ssl->options.haveSessionId = 1; |
5715 | 0 | } |
5716 | | |
5717 | | /* Force client hello version 1.2 to work for static RSA. */ |
5718 | 0 | if (ssl->options.dtls) |
5719 | 0 | ssl->chVersion.minor = DTLSv1_2_MINOR; |
5720 | 0 | else |
5721 | 0 | ssl->chVersion.minor = TLSv1_2_MINOR; |
5722 | | /* Complete TLS v1.2 processing of ServerHello. */ |
5723 | 0 | ret = DoServerHello(ssl, input, inOutIdx, helloSz); |
5724 | | #else |
5725 | | WOLFSSL_MSG("Client using higher version, fatal error"); |
5726 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
5727 | | ret = VERSION_ERROR; |
5728 | | #endif |
5729 | |
|
5730 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5731 | |
|
5732 | 0 | return ret; |
5733 | 0 | } |
5734 | | |
5735 | | /* Advance state and proceed */ |
5736 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
5737 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
5738 | 0 | FALL_THROUGH; |
5739 | |
|
5740 | 0 | case TLS_ASYNC_FINALIZE: |
5741 | 0 | { |
5742 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
5743 | | if (ssl->options.tls13MiddleBoxCompat) { |
5744 | | if (args->sessIdSz == 0) { |
5745 | | WOLFSSL_MSG("args->sessIdSz == 0"); |
5746 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5747 | | return INVALID_PARAMETER; |
5748 | | } |
5749 | | if (ssl->session->sessionIDSz != 0) { |
5750 | | if (ssl->session->sessionIDSz != args->sessIdSz || |
5751 | | XMEMCMP(ssl->session->sessionID, args->sessId, |
5752 | | args->sessIdSz) != 0) { |
5753 | | WOLFSSL_MSG("session id doesn't match"); |
5754 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5755 | | return INVALID_PARAMETER; |
5756 | | } |
5757 | | } |
5758 | | else if (XMEMCMP(ssl->arrays->clientRandom, args->sessId, |
5759 | | args->sessIdSz) != 0) { |
5760 | | WOLFSSL_MSG("session id doesn't match client random"); |
5761 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5762 | | return INVALID_PARAMETER; |
5763 | | } |
5764 | | } |
5765 | | else |
5766 | | #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */ |
5767 | | #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13) |
5768 | | if (0 |
5769 | | #ifdef WOLFSSL_QUIC |
5770 | | || WOLFSSL_IS_QUIC(ssl) |
5771 | | #endif |
5772 | | #ifdef WOLFSSL_DTLS13 |
5773 | | || ssl->options.dtls |
5774 | | #endif |
5775 | | ) { |
5776 | | /* RFC 9147 Section 5.3 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC |
5777 | | * ServerHello must have empty legacy_session_id_echo. */ |
5778 | | if (args->sessIdSz != 0) { |
5779 | | WOLFSSL_MSG("args->sessIdSz != 0"); |
5780 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5781 | | return INVALID_PARAMETER; |
5782 | | } |
5783 | | } |
5784 | | else |
5785 | | #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */ |
5786 | 0 | if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 && |
5787 | 0 | XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0)) |
5788 | 0 | { |
5789 | 0 | WOLFSSL_MSG("Server sent different session id"); |
5790 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5791 | 0 | return INVALID_PARAMETER; |
5792 | 0 | } |
5793 | | |
5794 | 0 | ret = SetCipherSpecs(ssl); |
5795 | 0 | if (ret != 0) |
5796 | 0 | return ret; |
5797 | | |
5798 | | #ifdef HAVE_NULL_CIPHER |
5799 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
5800 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
5801 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
5802 | | ; |
5803 | | } |
5804 | | else |
5805 | | #endif |
5806 | | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
5807 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5808 | | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
5809 | | ; /* Do nothing. */ |
5810 | | } |
5811 | | else |
5812 | | #endif |
5813 | | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
5814 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
5815 | | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
5816 | | ; /* Do nothing. */ |
5817 | | } |
5818 | | else |
5819 | | #endif |
5820 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
5821 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
5822 | 0 | WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet"); |
5823 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5824 | 0 | return INVALID_PARAMETER; |
5825 | 0 | } |
5826 | | |
5827 | 0 | suite[0] = ssl->options.cipherSuite0; |
5828 | 0 | suite[1] = ssl->options.cipherSuite; |
5829 | 0 | if (!FindSuiteSSL(ssl, suite)) { |
5830 | 0 | WOLFSSL_MSG("Cipher suite not supported on client"); |
5831 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
5832 | 0 | return INVALID_PARAMETER; |
5833 | 0 | } |
5834 | | |
5835 | | #if defined(HAVE_ECH) |
5836 | | /* check for acceptConfirmation */ |
5837 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5838 | | ssl->hsHashesEch != NULL) { |
5839 | | args->echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
5840 | | if (args->echX == NULL || args->echX->data == NULL) |
5841 | | return WOLFSSL_FATAL_ERROR; |
5842 | | |
5843 | | if (args->extMsgType == hello_retry_request && |
5844 | | ((WOLFSSL_ECH*)args->echX->data)->confBuf == NULL) { |
5845 | | /* server rejected ECH, fallback to outer */ |
5846 | | Free_HS_Hashes(ssl->hsHashesEch, ssl->heap); |
5847 | | ssl->hsHashesEch = NULL; |
5848 | | } |
5849 | | else { |
5850 | | /* account for hrr extension instead of server random */ |
5851 | | if (args->extMsgType == hello_retry_request) { |
5852 | | args->acceptOffset = |
5853 | | (word32)(((WOLFSSL_ECH*)args->echX->data)->confBuf - input); |
5854 | | args->acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
5855 | | args->acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
5856 | | } |
5857 | | else { |
5858 | | args->acceptLabel = (byte*)echAcceptConfirmationLabel; |
5859 | | args->acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
5860 | | } |
5861 | | /* check acceptance */ |
5862 | | if (ret == 0) { |
5863 | | ret = EchCheckAcceptance(ssl, args->acceptLabel, |
5864 | | args->acceptLabelSz, input, args->acceptOffset, helloSz, |
5865 | | args->extMsgType); |
5866 | | } |
5867 | | if (ret != 0) |
5868 | | return ret; |
5869 | | /* use the inner random for client random */ |
5870 | | if (args->extMsgType != hello_retry_request) { |
5871 | | XMEMCPY(ssl->arrays->clientRandom, |
5872 | | ssl->arrays->clientRandomInner, RAN_LEN); |
5873 | | } |
5874 | | } |
5875 | | } |
5876 | | #endif /* HAVE_ECH */ |
5877 | | |
5878 | 0 | if (*extMsgType == server_hello) { |
5879 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5880 | | PreSharedKey* psk = NULL; |
5881 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
5882 | | if (ext != NULL) |
5883 | | psk = (PreSharedKey*)ext->data; |
5884 | | while (psk != NULL && !psk->chosen) |
5885 | | psk = psk->next; |
5886 | | if (psk == NULL) { |
5887 | | ssl->options.resuming = 0; |
5888 | | ssl->arrays->psk_keySz = 0; |
5889 | | XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN); |
5890 | | } |
5891 | | else { |
5892 | | #if defined(HAVE_ECH) |
5893 | | /* do not resume when outerHandshake will be negotiated */ |
5894 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
5895 | | !ssl->options.echAccepted) { |
5896 | | WOLFSSL_MSG("ECH rejected but server negotiated PSK"); |
5897 | | return INVALID_PARAMETER; |
5898 | | } |
5899 | | #endif |
5900 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
5901 | | if (ssl->options.certWithExternPsk && psk->resumption) { |
5902 | | /* RFC8773bis mode requires external PSK, not ticket resumption. */ |
5903 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
5904 | | return PSK_KEY_ERROR; |
5905 | | } |
5906 | | if (ssl->options.certWithExternPsk && ssl->options.shSentKeyShare == 0) { |
5907 | | /* RFC8773bis Sec. 3: cert_with_extern_psk requires psk_dhe_ke; |
5908 | | * a ServerHello without a key_share confirms only psk_ke. */ |
5909 | | WOLFSSL_MSG("cert_with_extern_psk: ServerHello missing key_share"); |
5910 | | WOLFSSL_ERROR_VERBOSE(EXT_MISSING); |
5911 | | return EXT_MISSING; |
5912 | | } |
5913 | | #endif |
5914 | | if ((ret = SetupPskKey(ssl, psk, 0)) != 0) |
5915 | | return ret; |
5916 | | ssl->options.pskNegotiated = 1; |
5917 | | } |
5918 | | #else |
5919 | | /* no resumption possible */ |
5920 | 0 | ssl->options.resuming = 0; |
5921 | 0 | #endif |
5922 | | |
5923 | | /* sanity check on PSK / KSE */ |
5924 | 0 | if ( |
5925 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
5926 | | ssl->options.pskNegotiated == 0 && |
5927 | | #endif |
5928 | 0 | (ssl->session->namedGroup == 0 || |
5929 | 0 | ssl->options.shSentKeyShare == 0)) { |
5930 | 0 | return EXT_MISSING; |
5931 | 0 | } |
5932 | | |
5933 | 0 | ssl->keys.encryptionOn = 1; |
5934 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
5935 | |
|
5936 | 0 | } |
5937 | 0 | else { |
5938 | | /* https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.4 |
5939 | | * Clients MUST abort the handshake with an |
5940 | | * "illegal_parameter" alert if the HelloRetryRequest would not result |
5941 | | * in any change in the ClientHello. |
5942 | | */ |
5943 | | /* Check if the HRR contained a cookie or a keyshare */ |
5944 | 0 | if (!ssl->options.hrrSentKeyShare |
5945 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
5946 | | && !ssl->options.hrrSentCookie |
5947 | | #endif |
5948 | 0 | ) { |
5949 | 0 | SendAlert(ssl, alert_fatal, illegal_parameter); |
5950 | 0 | return EXT_MISSING; |
5951 | 0 | } |
5952 | | |
5953 | 0 | ssl->options.tls1_3 = 1; |
5954 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
5955 | |
|
5956 | 0 | ret = RestartHandshakeHash(ssl); |
5957 | 0 | } |
5958 | | |
5959 | 0 | break; |
5960 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
5961 | 0 | default: |
5962 | 0 | ret = INPUT_CASE_ERROR; |
5963 | 0 | } /* switch (ssl->options.asyncState) */ |
5964 | | |
5965 | | #ifdef WOLFSSL_ASYNC_CRYPT |
5966 | | if (ret == 0) |
5967 | | FreeAsyncCtx(ssl, 0); |
5968 | | #endif |
5969 | | |
5970 | 0 | WOLFSSL_LEAVE("DoTls13ServerHello", ret); |
5971 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO); |
5972 | |
|
5973 | 0 | return ret; |
5974 | 0 | } |
5975 | | |
5976 | | /* handle processing TLS 1.3 encrypted_extensions (8) */ |
5977 | | /* Parse and handle an EncryptedExtensions message. |
5978 | | * Only a client will receive this message. |
5979 | | * |
5980 | | * ssl The SSL/TLS object. |
5981 | | * input The message buffer. |
5982 | | * inOutIdx On entry, the index into the message buffer of |
5983 | | * EncryptedExtensions. |
5984 | | * On exit, the index of byte after the EncryptedExtensions |
5985 | | * message. |
5986 | | * totalSz The length of the current handshake message. |
5987 | | * returns 0 on success and otherwise failure. |
5988 | | */ |
5989 | | static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input, |
5990 | | word32* inOutIdx, word32 totalSz) |
5991 | 0 | { |
5992 | 0 | int ret; |
5993 | 0 | word32 begin = *inOutIdx; |
5994 | 0 | word32 i = begin; |
5995 | 0 | word16 totalExtSz; |
5996 | |
|
5997 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
5998 | 0 | WOLFSSL_ENTER("DoTls13EncryptedExtensions"); |
5999 | |
|
6000 | | #ifdef WOLFSSL_CALLBACKS |
6001 | | if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions"); |
6002 | | if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo); |
6003 | | #endif |
6004 | | |
6005 | | /* Length field of extension data. */ |
6006 | 0 | if (totalSz < OPAQUE16_LEN) |
6007 | 0 | return BUFFER_ERROR; |
6008 | 0 | ato16(&input[i], &totalExtSz); |
6009 | 0 | i += OPAQUE16_LEN; |
6010 | | |
6011 | | /* Extension data. */ |
6012 | 0 | if (i - begin + totalExtSz > totalSz) |
6013 | 0 | return BUFFER_ERROR; |
6014 | 0 | if ((ret = TLSX_Parse(ssl, input + i, totalExtSz, encrypted_extensions, |
6015 | 0 | NULL))) { |
6016 | 0 | return ret; |
6017 | 0 | } |
6018 | | |
6019 | | /* Move index to byte after message. */ |
6020 | 0 | *inOutIdx = i + totalExtSz; |
6021 | | |
6022 | | /* Always encrypted. */ |
6023 | 0 | *inOutIdx += ssl->keys.padSz; |
6024 | |
|
6025 | | #ifdef WOLFSSL_EARLY_DATA |
6026 | | if (ssl->earlyData != no_early_data) { |
6027 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6028 | | if (ext == NULL || !ext->val) { |
6029 | | WOLFSSL_MSG("Early data rejected by server (no early_data " |
6030 | | "EncryptedExtensions response)"); |
6031 | | ssl->earlyData = no_early_data; |
6032 | | } |
6033 | | } |
6034 | | |
6035 | | if (ssl->earlyData == no_early_data) { |
6036 | | ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY); |
6037 | | if (ret != 0) |
6038 | | return ret; |
6039 | | } |
6040 | | #endif /* WOLFSSL_EARLY_DATA */ |
6041 | |
|
6042 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
6043 | |
|
6044 | 0 | WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret); |
6045 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO); |
6046 | |
|
6047 | 0 | return ret; |
6048 | 0 | } |
6049 | | |
6050 | | #ifndef NO_CERTS |
6051 | | /* handle processing TLS v1.3 certificate_request (13) */ |
6052 | | /* Handle a TLS v1.3 CertificateRequest message. |
6053 | | * This message is always encrypted. |
6054 | | * Only a client will receive this message. |
6055 | | * |
6056 | | * ssl The SSL/TLS object. |
6057 | | * input The message buffer. |
6058 | | * inOutIdx On entry, the index into the message buffer of CertificateRequest. |
6059 | | * On exit, the index of byte after the CertificateRequest message. |
6060 | | * size The length of the current handshake message. |
6061 | | * returns 0 on success and otherwise failure. |
6062 | | */ |
6063 | | static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input, |
6064 | | word32* inOutIdx, word32 size) |
6065 | 0 | { |
6066 | 0 | word16 len; |
6067 | 0 | word32 begin = *inOutIdx; |
6068 | 0 | int ret = 0; |
6069 | 0 | Suites peerSuites; |
6070 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6071 | | word16 reqCtxLen; |
6072 | | const byte* reqCtxData; |
6073 | | #endif |
6074 | |
|
6075 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO); |
6076 | 0 | WOLFSSL_ENTER("DoTls13CertificateRequest"); |
6077 | |
|
6078 | 0 | XMEMSET(&peerSuites, 0, sizeof(Suites)); |
6079 | |
|
6080 | | #ifdef WOLFSSL_CALLBACKS |
6081 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest"); |
6082 | | if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo); |
6083 | | #endif |
6084 | |
|
6085 | 0 | if (OPAQUE8_LEN > size) |
6086 | 0 | return BUFFER_ERROR; |
6087 | | |
6088 | | /* Length of the request context. */ |
6089 | 0 | len = input[(*inOutIdx)++]; |
6090 | 0 | if ((*inOutIdx - begin) + len > size) |
6091 | 0 | return BUFFER_ERROR; |
6092 | 0 | if (ssl->options.connectState < FINISHED_DONE && len > 0) |
6093 | 0 | return BUFFER_ERROR; |
6094 | | |
6095 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6096 | | /* Remember the request context bytes; the CertReqCtx allocation and |
6097 | | * linking into ssl->certReqCtx is deferred until after the rest of the |
6098 | | * message has been validated. |
6099 | | */ |
6100 | | reqCtxLen = len; |
6101 | | reqCtxData = input + *inOutIdx; |
6102 | | #endif |
6103 | 0 | *inOutIdx += len; |
6104 | | |
6105 | | /* TODO: Add support for more extensions: |
6106 | | * signed_certificate_timestamp, certificate_authorities, oid_filters. |
6107 | | */ |
6108 | | /* Certificate extensions */ |
6109 | 0 | if ((*inOutIdx - begin) + OPAQUE16_LEN > size) |
6110 | 0 | return BUFFER_ERROR; |
6111 | 0 | ato16(input + *inOutIdx, &len); |
6112 | 0 | *inOutIdx += OPAQUE16_LEN; |
6113 | 0 | if ((*inOutIdx - begin) + len > size) |
6114 | 0 | return BUFFER_ERROR; |
6115 | 0 | if (len == 0) |
6116 | 0 | return INVALID_PARAMETER; |
6117 | 0 | if ((ret = TLSX_Parse(ssl, input + *inOutIdx, len, certificate_request, |
6118 | 0 | &peerSuites))) { |
6119 | 0 | return ret; |
6120 | 0 | } |
6121 | 0 | *inOutIdx += len; |
6122 | |
|
6123 | | #ifdef WOLFSSL_CERT_SETUP_CB |
6124 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
6125 | | return ret; |
6126 | | #endif |
6127 | |
|
6128 | | #if defined(HAVE_ECH) |
6129 | | /* RFC 9849 s6.1.7: ECH was offered but rejected by the server... |
6130 | | * the client MUST respond with an empty Certificate message. */ |
6131 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
6132 | | !ssl->options.echAccepted) { |
6133 | | ssl->options.sendVerify = SEND_BLANK_CERT; |
6134 | | } |
6135 | | else |
6136 | | #endif |
6137 | 0 | if ((ssl->buffers.certificate && ssl->buffers.certificate->buffer && |
6138 | 0 | ((ssl->buffers.key && ssl->buffers.key->buffer) |
6139 | | #ifdef HAVE_PK_CALLBACKS |
6140 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
6141 | | #endif |
6142 | 0 | )) |
6143 | | #ifdef OPENSSL_EXTRA |
6144 | | || ssl->ctx->certSetupCb != NULL |
6145 | | #endif |
6146 | 0 | ) { |
6147 | 0 | if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo, |
6148 | 0 | peerSuites.hashSigAlgoSz, 0) != 0) { |
6149 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
6150 | 0 | return INVALID_PARAMETER; |
6151 | 0 | } |
6152 | 0 | ssl->options.sendVerify = SEND_CERT; |
6153 | 0 | } |
6154 | 0 | else { |
6155 | 0 | #ifndef WOLFSSL_NO_CLIENT_CERT_ERROR |
6156 | 0 | ssl->options.sendVerify = SEND_BLANK_CERT; |
6157 | | #else |
6158 | | WOLFSSL_MSG("Certificate required but none set on client"); |
6159 | | SendAlert(ssl, alert_fatal, illegal_parameter); |
6160 | | WOLFSSL_ERROR_VERBOSE(NO_CERT_ERROR); |
6161 | | return NO_CERT_ERROR; |
6162 | | #endif |
6163 | 0 | } |
6164 | | |
6165 | | /* This message is always encrypted so add encryption padding. */ |
6166 | 0 | *inOutIdx += ssl->keys.padSz; |
6167 | |
|
6168 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
6169 | | { |
6170 | | /* CertReqCtx has one byte at end for context value. |
6171 | | * Increase size to handle other implementations sending more than one byte. |
6172 | | * That is, allocate extra space, over one byte, to hold the context value. |
6173 | | */ |
6174 | | CertReqCtx* certReqCtx = (CertReqCtx*)XMALLOC( |
6175 | | sizeof(CertReqCtx) + (reqCtxLen == 0 ? 0 : reqCtxLen - 1), |
6176 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
6177 | | if (certReqCtx == NULL) |
6178 | | return MEMORY_E; |
6179 | | certReqCtx->next = ssl->certReqCtx; |
6180 | | certReqCtx->len = reqCtxLen; |
6181 | | XMEMCPY(&certReqCtx->ctx, reqCtxData, reqCtxLen); |
6182 | | ssl->certReqCtx = certReqCtx; |
6183 | | } |
6184 | | #endif |
6185 | |
|
6186 | 0 | WOLFSSL_LEAVE("DoTls13CertificateRequest", ret); |
6187 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO); |
6188 | |
|
6189 | 0 | return ret; |
6190 | 0 | } |
6191 | | #endif /* !NO_CERTS */ |
6192 | | #endif /* !NO_WOLFSSL_CLIENT */ |
6193 | | |
6194 | | #ifndef NO_WOLFSSL_SERVER |
6195 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
6196 | | #ifndef NO_PSK |
6197 | | int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk, byte* psk_key, |
6198 | | word32* psk_keySz, const byte* suite, int* found, byte* foundSuite) |
6199 | | { |
6200 | | const char* cipherName = NULL; |
6201 | | byte cipherSuite0 = TLS13_BYTE; |
6202 | | byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER; |
6203 | | int ret = 0; |
6204 | | |
6205 | | *found = 0; |
6206 | | (void)suite; |
6207 | | |
6208 | | if (ssl->options.server_psk_tls13_cb != NULL) { |
6209 | | *psk_keySz = ssl->options.server_psk_tls13_cb((WOLFSSL*)ssl, |
6210 | | (char*)psk->identity, psk_key, MAX_PSK_KEY_LEN, &cipherName); |
6211 | | if (*psk_keySz != 0) { |
6212 | | int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE; |
6213 | | *found = (GetCipherSuiteFromName(cipherName, &cipherSuite0, |
6214 | | &cipherSuite, NULL, NULL, &cipherSuiteFlags) == 0); |
6215 | | (void)cipherSuiteFlags; |
6216 | | } |
6217 | | } |
6218 | | if (*found == 0 && (ssl->options.server_psk_cb != NULL)) { |
6219 | | *psk_keySz = ssl->options.server_psk_cb((WOLFSSL*)ssl, |
6220 | | (char*)psk->identity, psk_key, |
6221 | | MAX_PSK_KEY_LEN); |
6222 | | *found = (*psk_keySz != 0); |
6223 | | } |
6224 | | if (*found) { |
6225 | | if (*psk_keySz > MAX_PSK_KEY_LEN && |
6226 | | (int)*psk_keySz != WC_NO_ERR_TRACE(USE_HW_PSK)) { |
6227 | | WOLFSSL_MSG("Key len too long in FindPsk()"); |
6228 | | ret = PSK_KEY_ERROR; |
6229 | | WOLFSSL_ERROR_VERBOSE(ret); |
6230 | | *found = 0; |
6231 | | } |
6232 | | if (ret == 0) { |
6233 | | #if !defined(WOLFSSL_PSK_ONE_ID) && !defined(WOLFSSL_PRIORITIZE_PSK) |
6234 | | /* Check whether PSK ciphersuite is in SSL. */ |
6235 | | *found = (suite[0] == cipherSuite0) && (suite[1] == cipherSuite); |
6236 | | #else |
6237 | | (void)suite; |
6238 | | /* Check whether PSK ciphersuite is in SSL. */ |
6239 | | { |
6240 | | byte s[2] = { |
6241 | | cipherSuite0, |
6242 | | cipherSuite, |
6243 | | }; |
6244 | | *found = FindSuiteSSL(ssl, s); |
6245 | | } |
6246 | | #endif |
6247 | | } |
6248 | | } |
6249 | | if (*found && foundSuite != NULL) { |
6250 | | foundSuite[0] = cipherSuite0; |
6251 | | foundSuite[1] = cipherSuite; |
6252 | | } |
6253 | | |
6254 | | return ret; |
6255 | | } |
6256 | | |
6257 | | /* Attempt to find the PSK (not session ticket) that matches. |
6258 | | * |
6259 | | * @param [in, out] ssl The SSL/TLS object. |
6260 | | * @param [in] psk A pre-shared key from the extension. |
6261 | | * @param [out] suite Cipher suite to use with PSK. |
6262 | | * @param [out] err Error code. |
6263 | | * PSK_KEY_ERROR when key is too big or ticket age is |
6264 | | * invalid, |
6265 | | * UNSUPPORTED_SUITE on invalid suite. |
6266 | | * Other error when attempting to derive early secret. |
6267 | | * @return 1 when a match found - but check error code. |
6268 | | * @return 0 when no match found. |
6269 | | */ |
6270 | | static int FindPsk(WOLFSSL* ssl, PreSharedKey* psk, const byte* suite, int* err) |
6271 | | { |
6272 | | int ret = 0; |
6273 | | int found = 0; |
6274 | | byte foundSuite[SUITE_LEN]; |
6275 | | |
6276 | | WOLFSSL_ENTER("FindPsk"); |
6277 | | |
6278 | | XMEMSET(foundSuite, 0, sizeof(foundSuite)); |
6279 | | |
6280 | | ret = FindPskSuite(ssl, psk, ssl->arrays->psk_key, &ssl->arrays->psk_keySz, |
6281 | | suite, &found, foundSuite); |
6282 | | if (ret == 0 && found) { |
6283 | | /* This identity matched via external PSK callback, not ticket resume. */ |
6284 | | psk->resumption = 0; |
6285 | | /* Default to ciphersuite if cb doesn't specify. */ |
6286 | | ssl->options.resuming = 0; |
6287 | | /* Don't send certificate request when using PSK. */ |
6288 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6289 | | if (!ssl->options.certWithExternPsk) |
6290 | | #endif |
6291 | | ssl->options.verifyPeer = 0; |
6292 | | |
6293 | | /* PSK age is always zero. */ |
6294 | | if (psk->ticketAge != 0) { |
6295 | | ret = PSK_KEY_ERROR; |
6296 | | WOLFSSL_ERROR_VERBOSE(ret); |
6297 | | } |
6298 | | if (ret == 0) { |
6299 | | /* Set PSK ciphersuite into SSL. */ |
6300 | | ssl->options.cipherSuite0 = foundSuite[0]; |
6301 | | ssl->options.cipherSuite = foundSuite[1]; |
6302 | | ret = SetCipherSpecs(ssl); |
6303 | | } |
6304 | | if (ret == 0) { |
6305 | | /* Derive the early secret using the PSK. */ |
6306 | | ret = DeriveEarlySecret(ssl); |
6307 | | } |
6308 | | if (ret == 0) { |
6309 | | /* PSK negotiation has succeeded */ |
6310 | | ssl->options.isPSK = 1; |
6311 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6312 | | if (!ssl->options.certWithExternPsk) |
6313 | | #endif |
6314 | | { |
6315 | | /* SERVER: using PSK for peer authentication. */ |
6316 | | ssl->options.peerAuthGood = 1; |
6317 | | } |
6318 | | } |
6319 | | } |
6320 | | |
6321 | | *err = ret; |
6322 | | WOLFSSL_LEAVE("FindPsk", found); |
6323 | | WOLFSSL_LEAVE("FindPsk", ret); |
6324 | | return found; |
6325 | | } |
6326 | | #endif /* !NO_PSK */ |
6327 | | |
6328 | | /* Handle any Pre-Shared Key (PSK) extension. |
6329 | | * Find a PSK that supports the cipher suite passed in. |
6330 | | * |
6331 | | * ssl SSL/TLS object. |
6332 | | * suite Cipher suite to find PSK for. |
6333 | | * usingPSK 1=Indicates handshake is using Pre-Shared Keys (2=Ephemeral) |
6334 | | * first Set to 1 if first in extension |
6335 | | * returns 0 on success and otherwise failure. |
6336 | | */ |
6337 | | static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz, |
6338 | | const byte* suite, int* usingPSK, int* first) |
6339 | | { |
6340 | | int ret = 0; |
6341 | | TLSX* ext; |
6342 | | PreSharedKey* current; |
6343 | | byte binderKey[WC_MAX_DIGEST_SIZE]; |
6344 | | byte binder[WC_MAX_DIGEST_SIZE]; |
6345 | | word32 binderLen; |
6346 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6347 | | int certWithExternOffered = 0; |
6348 | | #endif |
6349 | | |
6350 | | #ifdef NO_PSK |
6351 | | (void) suite; /* to avoid unused var warning when not used */ |
6352 | | #endif |
6353 | | |
6354 | | WOLFSSL_ENTER("DoPreSharedKeys"); |
6355 | | |
6356 | | (void)suite; |
6357 | | |
6358 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6359 | | if (ext == NULL) { |
6360 | | WOLFSSL_MSG("No pre shared extension keys found"); |
6361 | | ret = BAD_FUNC_ARG; |
6362 | | goto cleanup; |
6363 | | } |
6364 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6365 | | certWithExternOffered = |
6366 | | TLSX_Find(ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK) != NULL; |
6367 | | #endif |
6368 | | |
6369 | | /* Look through all client's pre-shared keys for a match. */ |
6370 | | for (current = (PreSharedKey*)ext->data; current != NULL; |
6371 | | current = current->next) { |
6372 | | #ifndef NO_PSK |
6373 | | if (current->identityLen > MAX_PSK_ID_LEN) { |
6374 | | ret = BUFFER_ERROR; |
6375 | | goto cleanup; |
6376 | | } |
6377 | | XMEMCPY(ssl->arrays->client_identity, current->identity, |
6378 | | current->identityLen); |
6379 | | ssl->arrays->client_identity[current->identityLen] = '\0'; |
6380 | | #endif |
6381 | | |
6382 | | #ifdef HAVE_SESSION_TICKET |
6383 | | /* Decode the identity. */ |
6384 | | switch (current->decryptRet) { |
6385 | | case PSK_DECRYPT_NONE: |
6386 | | ret = DoClientTicket_ex(ssl, current, 1); |
6387 | | /* psk->sess may be set. Need to clean up later. */ |
6388 | | break; |
6389 | | case PSK_DECRYPT_OK: |
6390 | | ret = WOLFSSL_TICKET_RET_OK; |
6391 | | break; |
6392 | | case PSK_DECRYPT_CREATE: |
6393 | | ret = WOLFSSL_TICKET_RET_CREATE; |
6394 | | break; |
6395 | | case PSK_DECRYPT_FAIL: |
6396 | | ret = WOLFSSL_TICKET_RET_REJECT; |
6397 | | break; |
6398 | | } |
6399 | | |
6400 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6401 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) |
6402 | | goto cleanup; |
6403 | | #endif |
6404 | | |
6405 | | if (ret != WOLFSSL_TICKET_RET_OK && current->sess_free_cb != NULL) { |
6406 | | current->sess_free_cb(ssl, current->sess, |
6407 | | ¤t->sess_free_cb_ctx); |
6408 | | current->sess = NULL; |
6409 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6410 | | sizeof(psk_sess_free_cb_ctx)); |
6411 | | } |
6412 | | if (ret == WOLFSSL_TICKET_RET_OK) { |
6413 | | #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(HAVE_SESSION_TICKET) |
6414 | | /* RFC 8773bis Sect. 5.1: all PSKs listed alongside |
6415 | | * tls_cert_with_extern_psk MUST be external PSKs. A successfully |
6416 | | * decrypted session ticket identity is a resumption PSK, so the |
6417 | | * server MUST abort with illegal_parameter regardless of whether |
6418 | | * the ticket would otherwise be acceptable. Check here, before |
6419 | | * DoClientTicketFinalize, to avoid polluting ssl->session with |
6420 | | * ticket state that will not be used. */ |
6421 | | if (certWithExternOffered) { |
6422 | | if (current->sess_free_cb != NULL) { |
6423 | | current->sess_free_cb(ssl, current->sess, |
6424 | | ¤t->sess_free_cb_ctx); |
6425 | | current->sess = NULL; |
6426 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6427 | | sizeof(psk_sess_free_cb_ctx)); |
6428 | | } |
6429 | | ret = PSK_KEY_ERROR; |
6430 | | WOLFSSL_ERROR_VERBOSE(ret); |
6431 | | goto cleanup; |
6432 | | } |
6433 | | #endif |
6434 | | ret = DoClientTicketCheck(ssl, current, ssl->timeout, suite); |
6435 | | if (ret == 0) |
6436 | | DoClientTicketFinalize(ssl, current->it, current->sess); |
6437 | | if (current->sess_free_cb != NULL) { |
6438 | | current->sess_free_cb(ssl, current->sess, |
6439 | | ¤t->sess_free_cb_ctx); |
6440 | | current->sess = NULL; |
6441 | | XMEMSET(¤t->sess_free_cb_ctx, 0, |
6442 | | sizeof(psk_sess_free_cb_ctx)); |
6443 | | } |
6444 | | if (ret != 0) |
6445 | | continue; |
6446 | | |
6447 | | /* SERVER: using secret in session ticket for peer auth. */ |
6448 | | ssl->options.peerAuthGood = 1; |
6449 | | |
6450 | | #ifdef WOLFSSL_EARLY_DATA |
6451 | | ssl->options.maxEarlyDataSz = ssl->session->maxEarlyDataSz; |
6452 | | #endif |
6453 | | /* Use the same cipher suite as before and set up for use. */ |
6454 | | ssl->options.cipherSuite0 = ssl->session->cipherSuite0; |
6455 | | ssl->options.cipherSuite = ssl->session->cipherSuite; |
6456 | | ret = SetCipherSpecs(ssl); |
6457 | | if (ret != 0) |
6458 | | goto cleanup; |
6459 | | |
6460 | | /* Resumption PSK is resumption master secret. */ |
6461 | | ssl->arrays->psk_keySz = ssl->specs.hash_size; |
6462 | | if ((ret = DeriveResumptionPSK(ssl, ssl->session->ticketNonce.data, |
6463 | | ssl->session->ticketNonce.len, ssl->arrays->psk_key)) != 0) { |
6464 | | goto cleanup; |
6465 | | } |
6466 | | |
6467 | | /* Derive the early secret using the PSK. */ |
6468 | | ret = DeriveEarlySecret(ssl); |
6469 | | if (ret != 0) |
6470 | | goto cleanup; |
6471 | | |
6472 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6473 | | ret = HashInput(ssl, input, (int)inputSz); |
6474 | | if (ret < 0) |
6475 | | goto cleanup; |
6476 | | |
6477 | | /* Derive the binder key to use with HMAC. */ |
6478 | | ret = DeriveBinderKeyResume(ssl, binderKey); |
6479 | | if (ret != 0) |
6480 | | goto cleanup; |
6481 | | } |
6482 | | else |
6483 | | #endif /* HAVE_SESSION_TICKET */ |
6484 | | #ifndef NO_PSK |
6485 | | if (FindPsk(ssl, current, suite, &ret)) { |
6486 | | if (ret != 0) |
6487 | | goto cleanup; |
6488 | | |
6489 | | ret = HashInput(ssl, input, (int)inputSz); |
6490 | | if (ret < 0) |
6491 | | goto cleanup; |
6492 | | |
6493 | | /* Derive the binder key to use with HMAC. */ |
6494 | | ret = DeriveBinderKey(ssl, binderKey); |
6495 | | if (ret != 0) |
6496 | | goto cleanup; |
6497 | | } |
6498 | | else |
6499 | | #endif |
6500 | | { |
6501 | | continue; |
6502 | | } |
6503 | | |
6504 | | ssl->options.sendVerify = 0; |
6505 | | |
6506 | | /* Derive the Finished message secret. */ |
6507 | | ret = DeriveFinishedSecret(ssl, binderKey, |
6508 | | ssl->keys.client_write_MAC_secret, |
6509 | | 0 /* neither end */); |
6510 | | if (ret != 0) |
6511 | | goto cleanup; |
6512 | | |
6513 | | /* Derive the binder and compare with the one in the extension. */ |
6514 | | ret = BuildTls13HandshakeHmac(ssl, |
6515 | | ssl->keys.client_write_MAC_secret, binder, &binderLen); |
6516 | | if (ret != 0) |
6517 | | goto cleanup; |
6518 | | if (binderLen != current->binderLen || |
6519 | | ConstantCompare(binder, current->binder, |
6520 | | binderLen) != 0) { |
6521 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6522 | | ret = BAD_BINDER; |
6523 | | goto cleanup; |
6524 | | } |
6525 | | |
6526 | | /* This PSK works, no need to try any more. */ |
6527 | | current->chosen = 1; |
6528 | | ext->resp = 1; |
6529 | | break; |
6530 | | } |
6531 | | |
6532 | | if (current == NULL) { |
6533 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6534 | | #ifndef NO_CERTS |
6535 | | if (ssl->buffers.certChainCnt != 0) { |
6536 | | ret = 0; |
6537 | | goto cleanup; |
6538 | | } |
6539 | | #endif |
6540 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6541 | | ret = BAD_BINDER; |
6542 | | goto cleanup; |
6543 | | #else |
6544 | | ret = 0; |
6545 | | goto cleanup; |
6546 | | #endif |
6547 | | } |
6548 | | |
6549 | | *first = (current == ext->data); |
6550 | | *usingPSK = 1; |
6551 | | |
6552 | | cleanup: |
6553 | | ForceZero(binderKey, sizeof(binderKey)); |
6554 | | ForceZero(binder, sizeof(binder)); |
6555 | | WOLFSSL_LEAVE("DoPreSharedKeys", ret); |
6556 | | |
6557 | | return ret; |
6558 | | } |
6559 | | |
6560 | | /* Handle any Pre-Shared Key (PSK) extension. |
6561 | | * Must do this in ClientHello as it requires a hash of the truncated message. |
6562 | | * Don't know size of binders until Pre-Shared Key extension has been parsed. |
6563 | | * |
6564 | | * ssl SSL/TLS object. |
6565 | | * input ClientHello message. |
6566 | | * helloSz Size of the ClientHello message (including binders if present). |
6567 | | * clSuites Client's cipher suite list. |
6568 | | * usingPSK Indicates handshake is using Pre-Shared Keys. |
6569 | | */ |
6570 | | static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz, |
6571 | | Suites* clSuites, int* usingPSK) |
6572 | | { |
6573 | | int ret; |
6574 | | TLSX* ext; |
6575 | | word16 bindersLen; |
6576 | | int first = 0; |
6577 | | #ifndef WOLFSSL_PSK_ONE_ID |
6578 | | int i; |
6579 | | const Suites* suites; |
6580 | | #else |
6581 | | byte suite[2]; |
6582 | | #endif |
6583 | | |
6584 | | WOLFSSL_ENTER("CheckPreSharedKeys"); |
6585 | | |
6586 | | ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6587 | | if (ext == NULL) { |
6588 | | #ifdef WOLFSSL_EARLY_DATA |
6589 | | ssl->earlyData = no_early_data; |
6590 | | #endif |
6591 | | if (usingPSK) |
6592 | | *usingPSK = 0; |
6593 | | /* Hash data up to binders for deriving binders in PSK extension. */ |
6594 | | ret = HashInput(ssl, input, (int)helloSz); |
6595 | | return ret; |
6596 | | } |
6597 | | |
6598 | | /* Extensions pushed on stack/list and PSK must be last. */ |
6599 | | if (ssl->extensions != ext) { |
6600 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6601 | | return PSK_KEY_ERROR; |
6602 | | } |
6603 | | |
6604 | | /* Assume we are going to resume with a pre-shared key. */ |
6605 | | ssl->options.resuming = 1; |
6606 | | |
6607 | | /* Find the pre-shared key extension and calculate hash of truncated |
6608 | | * ClientHello for binders. |
6609 | | */ |
6610 | | ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data, |
6611 | | client_hello, &bindersLen); |
6612 | | if (ret < 0) |
6613 | | return ret; |
6614 | | if (bindersLen > helloSz) |
6615 | | return BUFFER_ERROR; |
6616 | | |
6617 | | /* Refine list for PSK processing. */ |
6618 | | sslRefineSuites(ssl, clSuites); |
6619 | | #ifndef WOLFSSL_PSK_ONE_ID |
6620 | | if (usingPSK == NULL) |
6621 | | return BAD_FUNC_ARG; |
6622 | | |
6623 | | /* set after refineSuites, to avoid taking a stale ptr to ctx->Suites */ |
6624 | | suites = WOLFSSL_SUITES(ssl); |
6625 | | /* Server list has only common suites from refining in server or client |
6626 | | * order. */ |
6627 | | for (i = 0; !(*usingPSK) && i < suites->suiteSz; i += 2) { |
6628 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, |
6629 | | suites->suites + i, usingPSK, &first); |
6630 | | if (ret != 0) { |
6631 | | #ifdef HAVE_SESSION_TICKET |
6632 | | #ifdef WOLFSSL_ASYNC_CRYPT |
6633 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
6634 | | #endif |
6635 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6636 | | #endif |
6637 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6638 | | return ret; |
6639 | | } |
6640 | | } |
6641 | | #ifdef HAVE_SESSION_TICKET |
6642 | | CleanupClientTickets((PreSharedKey*)ext->data); |
6643 | | #endif |
6644 | | #else |
6645 | | ret = DoPreSharedKeys(ssl, input, helloSz - bindersLen, suite, usingPSK, |
6646 | | &first); |
6647 | | if (ret != 0) { |
6648 | | WOLFSSL_MSG_EX("DoPreSharedKeys: %d", ret); |
6649 | | return ret; |
6650 | | } |
6651 | | #endif |
6652 | | |
6653 | | if (*usingPSK) { |
6654 | | /* While verifying the selected PSK, we updated the |
6655 | | * handshake hash up to the binder bytes in the PSK extensions. |
6656 | | * Continuing, we need the rest of the ClientHello hashed as well. |
6657 | | */ |
6658 | | ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen); |
6659 | | } |
6660 | | else { |
6661 | | /* No suitable PSK found, Hash the complete ClientHello, |
6662 | | * as caller expect it after we return */ |
6663 | | ret = HashInput(ssl, input, (int)helloSz); |
6664 | | } |
6665 | | if (ret != 0) |
6666 | | return ret; |
6667 | | |
6668 | | if (*usingPSK != 0) { |
6669 | | word32 modes; |
6670 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6671 | | int usingCertWithExternPsk = 0; |
6672 | | TLSX* certExt = NULL; |
6673 | | TLSX* pskExt = NULL; |
6674 | | PreSharedKey* chosenPsk = NULL; |
6675 | | #endif |
6676 | | #ifdef WOLFSSL_EARLY_DATA |
6677 | | TLSX* extEarlyData; |
6678 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6679 | | int hasCertWithExternPsk = (TLSX_Find(ssl->extensions, |
6680 | | TLSX_CERT_WITH_EXTERN_PSK) != NULL); |
6681 | | #endif |
6682 | | |
6683 | | extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA); |
6684 | | if (extEarlyData != NULL) { |
6685 | | /* Check if accepting early data and first PSK. |
6686 | | * RFC 8773bis: early_data is not compatible with |
6687 | | * cert_with_extern_psk, so skip key derivation in that case. */ |
6688 | | if (ssl->earlyData != no_early_data && first |
6689 | | && ssl->options.maxEarlyDataSz > 0 |
6690 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6691 | | && !hasCertWithExternPsk |
6692 | | #endif |
6693 | | #if defined(HAVE_SESSION_TICKET) && !defined(NO_SESSION_CACHE) |
6694 | | /* RFC 8446 section 8: evict the session from the cache. |
6695 | | * Accept 0-RTT only when the eviction found the entry |
6696 | | * (single-use). */ |
6697 | | && wolfSSL_SSL_CTX_remove_session(ssl->ctx, ssl->session) |
6698 | | == 1 |
6699 | | #endif |
6700 | | ) { |
6701 | | extEarlyData->resp = 1; |
6702 | | |
6703 | | /* Derive early data decryption key. */ |
6704 | | ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, |
6705 | | 1); |
6706 | | if (ret != 0) |
6707 | | return ret; |
6708 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
6709 | | return ret; |
6710 | | |
6711 | | ssl->keys.encryptionOn = 1; |
6712 | | ssl->earlyData = process_early_data; |
6713 | | } |
6714 | | else |
6715 | | extEarlyData->resp = 0; |
6716 | | } |
6717 | | #endif |
6718 | | |
6719 | | /* Get the PSK key exchange modes the client wants to negotiate. */ |
6720 | | ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES); |
6721 | | if (ext == NULL) { |
6722 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6723 | | return MISSING_HANDSHAKE_DATA; |
6724 | | } |
6725 | | modes = ext->val; |
6726 | | |
6727 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6728 | | certExt = TLSX_Find(ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK); |
6729 | | if (certExt != NULL) { |
6730 | | pskExt = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY); |
6731 | | if (pskExt != NULL) |
6732 | | chosenPsk = (PreSharedKey*)pskExt->data; |
6733 | | while (chosenPsk != NULL && !chosenPsk->chosen) |
6734 | | chosenPsk = chosenPsk->next; |
6735 | | if (chosenPsk == NULL || chosenPsk->resumption) { |
6736 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6737 | | return PSK_KEY_ERROR; |
6738 | | } |
6739 | | if ((modes & (1 << PSK_DHE_KE)) == 0) { |
6740 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6741 | | return PSK_KEY_ERROR; |
6742 | | } |
6743 | | usingCertWithExternPsk = 1; |
6744 | | ssl->options.certWithExternPsk = 1; |
6745 | | if (clSuites->hashSigAlgoSz == 0) { |
6746 | | WOLFSSL_ERROR_VERBOSE(MISSING_HANDSHAKE_DATA); |
6747 | | return MISSING_HANDSHAKE_DATA; |
6748 | | } |
6749 | | ret = PickHashSigAlgo(ssl, clSuites->hashSigAlgo, |
6750 | | clSuites->hashSigAlgoSz, 1); |
6751 | | if (ret != 0) |
6752 | | return ret; |
6753 | | ssl->options.sendVerify = SEND_CERT; |
6754 | | certExt->resp = 1; |
6755 | | #ifdef WOLFSSL_EARLY_DATA |
6756 | | /* RFC 8773bis: early_data is not compatible with |
6757 | | * cert_with_extern_psk. TLSX_Parse already rejects the |
6758 | | * combination in the ClientHello, but clear the response flag |
6759 | | * here as a defense-in-depth measure. */ |
6760 | | if (extEarlyData != NULL) { |
6761 | | WOLFSSL_MSG("Rejecting early data: " |
6762 | | "cert_with_extern_psk is not 0-RTT compatible"); |
6763 | | extEarlyData->resp = 0; |
6764 | | ssl->earlyData = no_early_data; |
6765 | | } |
6766 | | #endif |
6767 | | } |
6768 | | else { |
6769 | | ssl->options.certWithExternPsk = 0; |
6770 | | } |
6771 | | #endif |
6772 | | |
6773 | | #ifndef HAVE_SUPPORTED_CURVES |
6774 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6775 | | if (usingCertWithExternPsk) { |
6776 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6777 | | return PSK_KEY_ERROR; |
6778 | | } |
6779 | | #endif |
6780 | | #endif |
6781 | | #ifdef HAVE_SUPPORTED_CURVES |
6782 | | ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
6783 | | /* Use (EC)DHE for forward-security if possible. */ |
6784 | | if (((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe && |
6785 | | ext != NULL) |
6786 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6787 | | || usingCertWithExternPsk |
6788 | | #endif |
6789 | | ) { |
6790 | | if (ext == NULL) { |
6791 | | WOLFSSL_ERROR_VERBOSE(EXT_MISSING); |
6792 | | return EXT_MISSING; |
6793 | | } |
6794 | | /* Resumption path uses previous session group. */ |
6795 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6796 | | if (!usingCertWithExternPsk) |
6797 | | #endif |
6798 | | ssl->namedGroup = ssl->session->namedGroup; |
6799 | | *usingPSK = 2; /* generate new ephemeral key */ |
6800 | | } |
6801 | | else if (ssl->options.onlyPskDheKe) { |
6802 | | return PSK_KEY_ERROR; |
6803 | | } |
6804 | | else |
6805 | | #endif |
6806 | | { |
6807 | | if ((modes & (1 << PSK_KE)) == 0) { |
6808 | | WOLFSSL_MSG("psk_ke mode does not allow key share"); |
6809 | | WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR); |
6810 | | return PSK_KEY_ERROR; |
6811 | | } |
6812 | | ssl->options.noPskDheKe = 1; |
6813 | | ssl->arrays->preMasterSz = 0; |
6814 | | |
6815 | | *usingPSK = 1; |
6816 | | } |
6817 | | } |
6818 | | else { |
6819 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
6820 | | TLSX_Remove(&ssl->extensions, TLSX_CERT_WITH_EXTERN_PSK, ssl->heap); |
6821 | | ssl->options.certWithExternPsk = 0; |
6822 | | #endif |
6823 | | #ifdef WOLFSSL_PSK_ID_PROTECTION |
6824 | | #ifndef NO_CERTS |
6825 | | if (ssl->buffers.certChainCnt != 0) |
6826 | | return 0; |
6827 | | #endif |
6828 | | WOLFSSL_ERROR_VERBOSE(BAD_BINDER); |
6829 | | return BAD_BINDER; |
6830 | | #endif |
6831 | | } |
6832 | | |
6833 | | WOLFSSL_LEAVE("CheckPreSharedKeys", ret); |
6834 | | |
6835 | | return 0; |
6836 | | } |
6837 | | #endif /* HAVE_SESSION_TICKET || !NO_PSK */ |
6838 | | |
6839 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
6840 | | /* Check that the Cookie data's integrity. |
6841 | | * |
6842 | | * ssl SSL/TLS object. |
6843 | | * cookie The cookie data - hash and MAC. |
6844 | | * cookieSz The length of the cookie data in bytes. |
6845 | | * returns Length of the hash on success, otherwise failure. |
6846 | | */ |
6847 | | int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz) |
6848 | | { |
6849 | | int ret; |
6850 | | byte mac[WC_MAX_DIGEST_SIZE] = {0}; |
6851 | | WC_DECLARE_VAR(cookieHmac, Hmac, 1, ssl->heap); |
6852 | | byte cookieType = 0; |
6853 | | byte macSz = 0; |
6854 | | |
6855 | | if (ssl->buffers.tls13CookieSecret.buffer == NULL || |
6856 | | ssl->buffers.tls13CookieSecret.length == 0) { |
6857 | | WOLFSSL_MSG("Missing DTLS 1.3 cookie secret"); |
6858 | | return COOKIE_ERROR; |
6859 | | } |
6860 | | |
6861 | | #ifndef NO_SHA256 |
6862 | | cookieType = WC_SHA256; |
6863 | | macSz = WC_SHA256_DIGEST_SIZE; |
6864 | | #elif defined(WOLFSSL_SHA384) |
6865 | | cookieType = WC_SHA384; |
6866 | | macSz = WC_SHA384_DIGEST_SIZE; |
6867 | | #elif defined(WOLFSSL_TLS13_SHA512) |
6868 | | cookieType = WC_SHA512; |
6869 | | macSz = WC_SHA512_DIGEST_SIZE; |
6870 | | #elif defined(WOLFSSL_SM3) |
6871 | | cookieType = WC_SM3; |
6872 | | macSz = WC_SM3_DIGEST_SIZE; |
6873 | | #else |
6874 | | #error "No digest to available to use with HMAC for cookies." |
6875 | | #endif /* NO_SHA */ |
6876 | | |
6877 | | if (cookieSz < ssl->specs.hash_size + macSz) |
6878 | | return HRR_COOKIE_ERROR; |
6879 | | cookieSz -= macSz; |
6880 | | |
6881 | | WC_ALLOC_VAR_EX(cookieHmac, Hmac, 1, ssl->heap, DYNAMIC_TYPE_HMAC, |
6882 | | return MEMORY_E); |
6883 | | |
6884 | | ret = wc_HmacInit(cookieHmac, ssl->heap, ssl->devId); |
6885 | | if (ret == 0) { |
6886 | | ret = wc_HmacSetKey(cookieHmac, cookieType, |
6887 | | ssl->buffers.tls13CookieSecret.buffer, |
6888 | | ssl->buffers.tls13CookieSecret.length); |
6889 | | } |
6890 | | if (ret == 0) |
6891 | | ret = wc_HmacUpdate(cookieHmac, cookie, cookieSz); |
6892 | | #ifdef WOLFSSL_DTLS13 |
6893 | | /* Tie cookie to peer address */ |
6894 | | if (ret == 0) { |
6895 | | /* peerLock not necessary. Still in handshake phase. */ |
6896 | | if (ssl->options.dtls && ssl->buffers.dtlsCtx.peer.sz > 0) { |
6897 | | ret = wc_HmacUpdate(cookieHmac, |
6898 | | (byte*)ssl->buffers.dtlsCtx.peer.sa, |
6899 | | ssl->buffers.dtlsCtx.peer.sz); |
6900 | | } |
6901 | | } |
6902 | | #endif |
6903 | | if (ret == 0) |
6904 | | ret = wc_HmacFinal(cookieHmac, mac); |
6905 | | |
6906 | | wc_HmacFree(cookieHmac); |
6907 | | WC_FREE_VAR_EX(cookieHmac, ssl->heap, DYNAMIC_TYPE_HMAC); |
6908 | | if (ret != 0) |
6909 | | return ret; |
6910 | | |
6911 | | if (ConstantCompare(cookie + cookieSz, mac, macSz) != 0) { |
6912 | | WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR); |
6913 | | return HRR_COOKIE_ERROR; |
6914 | | } |
6915 | | return cookieSz; |
6916 | | } |
6917 | | |
6918 | | /* Length of the KeyShare Extension */ |
6919 | | #define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6920 | | /* Length of the Supported Versions Extension */ |
6921 | | #define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6922 | | /* Length of the Cookie Extension excluding cookie data */ |
6923 | | #define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN) |
6924 | | /* PV | Random | Session Id | CipherSuite | Compression | Ext Len */ |
6925 | | #define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \ |
6926 | | SUITE_LEN + COMP_LEN + OPAQUE16_LEN) |
6927 | | /* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */ |
6928 | | #define MAX_HRR_SZ (HRR_MAX_HS_HEADER_SZ + \ |
6929 | | HRR_BODY_SZ + \ |
6930 | | HRR_KEY_SHARE_SZ + \ |
6931 | | HRR_VERSIONS_SZ + \ |
6932 | | HRR_COOKIE_HDR_SZ) |
6933 | | |
6934 | | |
6935 | | /* Restart the handshake hash from the cookie value. |
6936 | | * |
6937 | | * ssl SSL/TLS object. |
6938 | | * cookie Cookie data from client. |
6939 | | * returns 0 on success, otherwise failure. |
6940 | | */ |
6941 | | static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie) |
6942 | | { |
6943 | | byte header[HANDSHAKE_HEADER_SZ] = {0}; |
6944 | | byte hrr[MAX_HRR_SZ] = {0}; |
6945 | | int hrrIdx; |
6946 | | word32 idx; |
6947 | | byte hashSz; |
6948 | | byte* cookieData; |
6949 | | word16 cookieDataSz; |
6950 | | word16 length; |
6951 | | int keyShareExt = 0; |
6952 | | int ret; |
6953 | | byte sessIdSz; |
6954 | | |
6955 | | ret = TlsCheckCookie(ssl, cookie->data, cookie->len); |
6956 | | if (ret < 0) |
6957 | | return ret; |
6958 | | cookieDataSz = (word16)ret; |
6959 | | hashSz = cookie->data[0]; |
6960 | | cookieData = cookie->data; |
6961 | | idx = OPAQUE8_LEN; |
6962 | | |
6963 | | /* Restart handshake hash with synthetic message hash. */ |
6964 | | AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl); |
6965 | | |
6966 | | if ((ret = InitHandshakeHashes(ssl)) != 0) |
6967 | | return ret; |
6968 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
6969 | | return ret; |
6970 | | #ifdef WOLFSSL_DEBUG_TLS |
6971 | | WOLFSSL_MSG("Restart Hash from Cookie"); |
6972 | | WOLFSSL_BUFFER(cookieData + idx, hashSz); |
6973 | | #endif |
6974 | | if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0) |
6975 | | return ret; |
6976 | | |
6977 | | /* Reconstruct the HelloRetryMessage for handshake hash. */ |
6978 | | sessIdSz = ssl->session->sessionIDSz; |
6979 | | #ifdef WOLFSSL_DTLS13 |
6980 | | /* RFC 9147 Section 5.3: DTLS 1.3 must use empty legacy_session_id. */ |
6981 | | if (ssl->options.dtls) |
6982 | | sessIdSz = 0; |
6983 | | #endif |
6984 | | length = HRR_BODY_SZ - ID_LEN + sessIdSz + |
6985 | | HRR_COOKIE_HDR_SZ + cookie->len; |
6986 | | length += HRR_VERSIONS_SZ; |
6987 | | /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */ |
6988 | | if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) { |
6989 | | keyShareExt = 1; |
6990 | | length += HRR_KEY_SHARE_SZ; |
6991 | | } |
6992 | | |
6993 | | AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl); |
6994 | | |
6995 | | idx += hashSz; |
6996 | | hrrIdx = HANDSHAKE_HEADER_SZ; |
6997 | | |
6998 | | #ifdef WOLFSSL_DTLS13 |
6999 | | if (ssl->options.dtls) |
7000 | | hrrIdx += DTLS_HANDSHAKE_EXTRA; |
7001 | | #endif /* WOLFSSL_DTLS13 */ |
7002 | | |
7003 | | /* The negotiated protocol version. */ |
7004 | | hrr[hrrIdx++] = ssl->version.major; |
7005 | | hrr[hrrIdx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
7006 | | |
7007 | | /* HelloRetryRequest message has fixed value for random. */ |
7008 | | XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN); |
7009 | | hrrIdx += RAN_LEN; |
7010 | | |
7011 | | hrr[hrrIdx++] = sessIdSz; |
7012 | | if (sessIdSz > 0) { |
7013 | | XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz); |
7014 | | hrrIdx += sessIdSz; |
7015 | | } |
7016 | | |
7017 | | /* Restore the cipher suite from the cookie. */ |
7018 | | ssl->options.hrrCipherSuite0 = cookieData[idx]; |
7019 | | hrr[hrrIdx++] = cookieData[idx++]; |
7020 | | ssl->options.hrrCipherSuite = cookieData[idx]; |
7021 | | hrr[hrrIdx++] = cookieData[idx++]; |
7022 | | |
7023 | | /* Compression not supported in TLS v1.3. */ |
7024 | | hrr[hrrIdx++] = 0; |
7025 | | |
7026 | | /* Extensions' length */ |
7027 | | length -= HRR_BODY_SZ - ID_LEN + sessIdSz; |
7028 | | c16toa(length, hrr + hrrIdx); |
7029 | | hrrIdx += 2; |
7030 | | |
7031 | | /* Optional KeyShare Extension */ |
7032 | | if (keyShareExt) { |
7033 | | c16toa(TLSX_KEY_SHARE, hrr + hrrIdx); |
7034 | | hrrIdx += 2; |
7035 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
7036 | | hrrIdx += 2; |
7037 | | /* Restore the HRR key share group from the cookie. */ |
7038 | | ato16(cookieData + idx, &ssl->hrr_keyshare_group); |
7039 | | hrr[hrrIdx++] = cookieData[idx++]; |
7040 | | hrr[hrrIdx++] = cookieData[idx++]; |
7041 | | } |
7042 | | c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx); |
7043 | | hrrIdx += 2; |
7044 | | c16toa(OPAQUE16_LEN, hrr + hrrIdx); |
7045 | | hrrIdx += 2; |
7046 | | #ifdef WOLFSSL_TLS13_DRAFT |
7047 | | hrr[hrrIdx++] = TLS_DRAFT_MAJOR; |
7048 | | hrr[hrrIdx++] = TLS_DRAFT_MINOR; |
7049 | | #else |
7050 | | hrr[hrrIdx++] = ssl->version.major; |
7051 | | hrr[hrrIdx++] = ssl->version.minor; |
7052 | | #endif |
7053 | | |
7054 | | /* Mandatory Cookie Extension */ |
7055 | | c16toa(TLSX_COOKIE, hrr + hrrIdx); |
7056 | | hrrIdx += 2; |
7057 | | c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx); |
7058 | | hrrIdx += 2; |
7059 | | c16toa(cookie->len, hrr + hrrIdx); |
7060 | | hrrIdx += 2; |
7061 | | |
7062 | | #ifdef WOLFSSL_DEBUG_TLS |
7063 | | WOLFSSL_MSG("Reconstructed HelloRetryRequest"); |
7064 | | WOLFSSL_BUFFER(hrr, hrrIdx); |
7065 | | WOLFSSL_MSG("Cookie"); |
7066 | | WOLFSSL_BUFFER(cookieData, cookie->len); |
7067 | | #endif |
7068 | | |
7069 | | #ifdef WOLFSSL_DTLS13 |
7070 | | if (ssl->options.dtls) { |
7071 | | ret = Dtls13HashHandshake(ssl, hrr, (word16)hrrIdx); |
7072 | | } |
7073 | | else |
7074 | | #endif /* WOLFSSL_DTLS13 */ |
7075 | | { |
7076 | | ret = HashRaw(ssl, hrr, hrrIdx); |
7077 | | } |
7078 | | |
7079 | | if (ret != 0) |
7080 | | return ret; |
7081 | | |
7082 | | return HashRaw(ssl, cookieData, cookie->len); |
7083 | | } |
7084 | | #endif |
7085 | | |
7086 | | /* Do SupportedVersion extension for TLS v1.3+ otherwise it is not. |
7087 | | * |
7088 | | * ssl The SSL/TLS object. |
7089 | | * input The message buffer. |
7090 | | * i The index into the message buffer of ClientHello. |
7091 | | * helloSz The length of the current handshake message. |
7092 | | * returns 0 on success and otherwise failure. |
7093 | | */ |
7094 | | static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i, |
7095 | | word32 helloSz, int* wantDowngrade) |
7096 | 0 | { |
7097 | 0 | int ret; |
7098 | 0 | byte b; |
7099 | 0 | word16 suiteSz; |
7100 | 0 | word16 totalExtSz; |
7101 | 0 | int foundVersion = 0; |
7102 | | |
7103 | | /* Client random */ |
7104 | 0 | i += RAN_LEN; |
7105 | | /* Session id - not used in TLS v1.3 */ |
7106 | 0 | b = input[i++]; |
7107 | 0 | if (i + b > helloSz) { |
7108 | 0 | return BUFFER_ERROR; |
7109 | 0 | } |
7110 | 0 | i += b; |
7111 | | #ifdef WOLFSSL_DTLS13 |
7112 | | if (ssl->options.dtls) { |
7113 | | /* legacy_cookie - not used in DTLS v1.3 */ |
7114 | | b = input[i++]; |
7115 | | if (i + b > helloSz) { |
7116 | | return BUFFER_ERROR; |
7117 | | } |
7118 | | i += b; |
7119 | | } |
7120 | | #endif /* WOLFSSL_DTLS13 */ |
7121 | | /* Cipher suites */ |
7122 | 0 | if (i + OPAQUE16_LEN > helloSz) |
7123 | 0 | return BUFFER_ERROR; |
7124 | 0 | ato16(input + i, &suiteSz); |
7125 | 0 | i += OPAQUE16_LEN; |
7126 | 0 | if (i + suiteSz + 1 > helloSz) |
7127 | 0 | return BUFFER_ERROR; |
7128 | 0 | i += suiteSz; |
7129 | | /* Compression */ |
7130 | 0 | b = input[i++]; |
7131 | 0 | if (i + b > helloSz) |
7132 | 0 | return BUFFER_ERROR; |
7133 | 0 | i += b; |
7134 | | |
7135 | | /* TLS 1.3 must have extensions */ |
7136 | 0 | if (i < helloSz) { |
7137 | 0 | if (i + OPAQUE16_LEN > helloSz) |
7138 | 0 | return BUFFER_ERROR; |
7139 | 0 | ato16(&input[i], &totalExtSz); |
7140 | 0 | i += OPAQUE16_LEN; |
7141 | 0 | if (totalExtSz != helloSz - i) |
7142 | 0 | return BUFFER_ERROR; |
7143 | | |
7144 | | /* Need to negotiate version first. */ |
7145 | 0 | if ((ret = TLSX_ParseVersion(ssl, input + i, totalExtSz, client_hello, |
7146 | 0 | &foundVersion))) { |
7147 | 0 | return ret; |
7148 | 0 | } |
7149 | 0 | } |
7150 | 0 | *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version); |
7151 | |
|
7152 | 0 | return 0; |
7153 | 0 | } |
7154 | | |
7155 | | #ifdef HAVE_ECH |
7156 | | /* Calculate and write the 8 ECH confirmation bytes. |
7157 | | * Output into confirmation field on HRR and into ServerRandom on ServerHello. |
7158 | | * |
7159 | | * ssl SSL/TLS object. |
7160 | | * label Ascii string describing ECH acceptance or rejection. |
7161 | | * labelSz Length of label excluding NULL character. |
7162 | | * output The buffer to calculate/write confirmation from/to. |
7163 | | * acceptOffset Where the 8 ECH confirmation bytes should be placed. |
7164 | | * helloSz Size of hello message. |
7165 | | * msgType Type of message being written. |
7166 | | * returns 0 on success and otherwise failure. |
7167 | | */ |
7168 | | static int EchWriteAcceptance(WOLFSSL* ssl, byte* label, word16 labelSz, |
7169 | | byte* output, int acceptOffset, int helloSz, byte msgType) |
7170 | | { |
7171 | | int ret = 0; |
7172 | | int headerSz; |
7173 | | HS_Hashes* tmpHashes; |
7174 | | |
7175 | | #ifdef WOLFSSL_DTLS13 |
7176 | | headerSz = ssl->options.dtls ? DTLS13_HANDSHAKE_HEADER_SZ : |
7177 | | HANDSHAKE_HEADER_SZ; |
7178 | | #else |
7179 | | headerSz = HANDSHAKE_HEADER_SZ; |
7180 | | #endif |
7181 | | |
7182 | | ret = EchCalcAcceptance(ssl, label, labelSz, output, acceptOffset, |
7183 | | helloSz - headerSz, msgType == hello_retry_request, |
7184 | | output + acceptOffset); |
7185 | | |
7186 | | if (ret == 0) { |
7187 | | tmpHashes = ssl->hsHashes; |
7188 | | ssl->hsHashes = ssl->hsHashesEch; |
7189 | | |
7190 | | /* after HRR, hsHashesEch must contain: |
7191 | | * message_hash(ClientHelloInner1) || HRR (actual, not zeros) */ |
7192 | | if (msgType == hello_retry_request) { |
7193 | | ret = HashRaw(ssl, output, helloSz); |
7194 | | } |
7195 | | /* normal TLS code will calculate transcript of ServerHello */ |
7196 | | else { |
7197 | | ssl->hsHashes = tmpHashes; |
7198 | | FreeHandshakeHashes(ssl); |
7199 | | tmpHashes = ssl->hsHashesEch; |
7200 | | ssl->hsHashesEch = NULL; |
7201 | | } |
7202 | | |
7203 | | ssl->hsHashes = tmpHashes; |
7204 | | } |
7205 | | |
7206 | | return ret; |
7207 | | } |
7208 | | #endif |
7209 | | |
7210 | | /* Handle a ClientHello handshake message. |
7211 | | * If the protocol version in the message is not TLS v1.3 or higher, use |
7212 | | * DoClientHello() |
7213 | | * Only a server will receive this message. |
7214 | | * |
7215 | | * ssl The SSL/TLS object. |
7216 | | * input The message buffer. |
7217 | | * inOutIdx On entry, the index into the message buffer of ClientHello. |
7218 | | * On exit, the index of byte after the ClientHello message and |
7219 | | * padding. |
7220 | | * helloSz The length of the current handshake message. |
7221 | | * returns 0 on success and otherwise failure. |
7222 | | */ |
7223 | | |
7224 | | typedef struct Dch13Args { |
7225 | | ProtocolVersion pv; |
7226 | | word32 idx; |
7227 | | word32 begin; |
7228 | | int usingPSK; |
7229 | | } Dch13Args; |
7230 | | |
7231 | | static void FreeDch13Args(WOLFSSL* ssl, void* pArgs) |
7232 | 0 | { |
7233 | | /* openssl compat builds hang on to the client suites until WOLFSSL object |
7234 | | * is destroyed */ |
7235 | 0 | #ifndef OPENSSL_EXTRA |
7236 | 0 | if (ssl->clSuites) { |
7237 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
7238 | 0 | ssl->clSuites = NULL; |
7239 | 0 | } |
7240 | 0 | #endif |
7241 | 0 | (void)ssl; |
7242 | 0 | (void)pArgs; |
7243 | |
|
7244 | 0 | } |
7245 | | |
7246 | | int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
7247 | | word32 helloSz) |
7248 | 0 | { |
7249 | 0 | int ret; |
7250 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7251 | | Dch13Args* args = NULL; |
7252 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
7253 | | #else |
7254 | 0 | Dch13Args args[1]; |
7255 | 0 | #endif |
7256 | | #if defined(HAVE_ECH) |
7257 | | TLSX* echX = NULL; |
7258 | | #endif |
7259 | |
|
7260 | 0 | WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO); |
7261 | 0 | WOLFSSL_ENTER("DoTls13ClientHello"); |
7262 | |
|
7263 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7264 | | if (ssl->async == NULL) { |
7265 | | ssl->async = (struct WOLFSSL_ASYNC*) |
7266 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
7267 | | DYNAMIC_TYPE_ASYNC); |
7268 | | if (ssl->async == NULL) |
7269 | | ERROR_OUT(MEMORY_E, exit_dch); |
7270 | | } |
7271 | | args = (Dch13Args*)ssl->async->args; |
7272 | | |
7273 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
7274 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
7275 | | /* Check for error */ |
7276 | | if (ret < 0) { |
7277 | | goto exit_dch; |
7278 | | } |
7279 | | } |
7280 | | else |
7281 | | #endif |
7282 | 0 | { |
7283 | | /* Reset state */ |
7284 | 0 | ret = WC_NO_ERR_TRACE(VERSION_ERROR); |
7285 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
7286 | 0 | XMEMSET(args, 0, sizeof(Dch13Args)); |
7287 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7288 | | ssl->async->freeArgs = FreeDch13Args; |
7289 | | #endif |
7290 | 0 | } |
7291 | |
|
7292 | 0 | switch (ssl->options.asyncState) { |
7293 | 0 | case TLS_ASYNC_BEGIN: |
7294 | 0 | { |
7295 | 0 | byte b; |
7296 | 0 | byte sessIdSz; |
7297 | 0 | int wantDowngrade = 0; |
7298 | 0 | word16 totalExtSz = 0; |
7299 | |
|
7300 | | #ifdef WOLFSSL_CALLBACKS |
7301 | | if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello"); |
7302 | | if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo); |
7303 | | #endif |
7304 | | |
7305 | | /* do not change state in the SSL object before the next region of code |
7306 | | * to be able to statelessly compute a DTLS cookie */ |
7307 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
7308 | | /* Update the ssl->options.dtlsStateful setting `if` statement in |
7309 | | * wolfSSL_accept_TLSv13 when changing this one. */ |
7310 | | if (IsDtlsNotSctpMode(ssl) && ssl->options.sendCookie && |
7311 | | !ssl->options.dtlsStateful) { |
7312 | | DtlsSetSeqNumForReply(ssl); |
7313 | | ret = DoClientHelloStateless(ssl, input + *inOutIdx, helloSz, 0, NULL); |
7314 | | if (ret != 0 || !ssl->options.dtlsStateful) { |
7315 | | *inOutIdx += helloSz; |
7316 | | goto exit_dch; |
7317 | | } |
7318 | | if (ssl->chGoodCb != NULL) { |
7319 | | int cbret = ssl->chGoodCb(ssl, ssl->chGoodCtx); |
7320 | | if (cbret < 0) { |
7321 | | ssl->error = cbret; |
7322 | | WOLFSSL_MSG("ClientHello Good Cb don't continue error"); |
7323 | | return WOLFSSL_FATAL_ERROR; |
7324 | | } |
7325 | | } |
7326 | | } |
7327 | | ssl->options.dtlsStateful = 1; |
7328 | | #endif /* WOLFSSL_DTLS */ |
7329 | |
|
7330 | 0 | args->idx = *inOutIdx; |
7331 | 0 | args->begin = args->idx; |
7332 | | |
7333 | | /* protocol version, random and session id length check */ |
7334 | 0 | if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz) { |
7335 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7336 | 0 | } |
7337 | | |
7338 | | /* Protocol version */ |
7339 | 0 | XMEMCPY(&args->pv, input + args->idx, OPAQUE16_LEN); |
7340 | 0 | ssl->chVersion = args->pv; /* store */ |
7341 | 0 | args->idx += OPAQUE16_LEN; |
7342 | | |
7343 | | |
7344 | | /* this check pass for DTLS Major (0xff) */ |
7345 | 0 | if (args->pv.major < SSLv3_MAJOR) { |
7346 | 0 | WOLFSSL_MSG("Legacy version field contains unsupported value"); |
7347 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7348 | 0 | } |
7349 | | |
7350 | | #ifdef WOLFSSL_DTLS13 |
7351 | | if (ssl->options.dtls && |
7352 | | args->pv.major == DTLS_MAJOR && args->pv.minor > DTLSv1_2_MINOR) { |
7353 | | wantDowngrade = 1; |
7354 | | ssl->version.minor = args->pv.minor; |
7355 | | } |
7356 | | #endif /* WOLFSSL_DTLS13 */ |
7357 | | |
7358 | 0 | if (!ssl->options.dtls) { |
7359 | 0 | #ifndef WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION |
7360 | | /* Check for TLS 1.3 version (0x0304) in legacy version field. RFC 8446 |
7361 | | * Section 4.2.1 allows this action: |
7362 | | * |
7363 | | * "Servers MAY abort the handshake upon receiving a ClientHello with |
7364 | | * legacy_version 0x0304 or later." |
7365 | | * |
7366 | | * Note that if WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION is defined then the |
7367 | | * semantics of RFC 5246 Appendix E will be followed. A ServerHello with |
7368 | | * version 1.2 will be sent. The same is true if TLS 1.3 is not enabled. |
7369 | | */ |
7370 | 0 | if (args->pv.major == SSLv3_MAJOR && args->pv.minor >= TLSv1_3_MINOR) { |
7371 | 0 | WOLFSSL_MSG("Legacy version field is TLS 1.3 or later. Aborting."); |
7372 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7373 | 0 | } |
7374 | 0 | #endif /* WOLFSSL_ALLOW_BAD_TLS_LEGACY_VERSION */ |
7375 | | |
7376 | | /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */ |
7377 | 0 | if (args->pv.major > SSLv3_MAJOR || (args->pv.major == SSLv3_MAJOR && |
7378 | 0 | args->pv.minor >= TLSv1_3_MINOR)) { |
7379 | 0 | args->pv.major = SSLv3_MAJOR; |
7380 | 0 | args->pv.minor = TLSv1_2_MINOR; |
7381 | 0 | wantDowngrade = 1; |
7382 | 0 | ssl->version.minor = args->pv.minor; |
7383 | 0 | } |
7384 | | /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */ |
7385 | 0 | else if (args->pv.major == SSLv3_MAJOR && |
7386 | 0 | args->pv.minor < TLSv1_2_MINOR) { |
7387 | 0 | wantDowngrade = 1; |
7388 | 0 | ssl->version.minor = args->pv.minor; |
7389 | 0 | } |
7390 | 0 | } |
7391 | | |
7392 | 0 | if (!wantDowngrade) { |
7393 | 0 | ret = DoTls13SupportedVersions(ssl, input + args->begin, |
7394 | 0 | args->idx - args->begin, helloSz, &wantDowngrade); |
7395 | 0 | if (ret < 0) |
7396 | 0 | goto exit_dch; |
7397 | 0 | } |
7398 | | |
7399 | 0 | if (wantDowngrade) { |
7400 | 0 | #ifndef WOLFSSL_NO_TLS12 |
7401 | 0 | byte realMinor; |
7402 | 0 | #endif |
7403 | | #if defined(HAVE_ECH) |
7404 | | if (ssl->options.echProcessingInner) { |
7405 | | WOLFSSL_MSG("ECH: inner client hello does not support version " |
7406 | | "less than TLS v1.3"); |
7407 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7408 | | } |
7409 | | #endif |
7410 | 0 | #ifndef WOLFSSL_NO_TLS12 |
7411 | 0 | if (!ssl->options.downgrade) { |
7412 | 0 | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7413 | 0 | "TLS v1.3"); |
7414 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7415 | 0 | } |
7416 | | |
7417 | 0 | if ((!ssl->options.dtls |
7418 | 0 | && args->pv.minor < ssl->options.minDowngrade) || |
7419 | 0 | (ssl->options.dtls && args->pv.minor > ssl->options.minDowngrade)) { |
7420 | 0 | WOLFSSL_MSG("\tversion below minimum allowed, fatal error"); |
7421 | 0 | ERROR_OUT(VERSION_ERROR, exit_dch); |
7422 | 0 | } |
7423 | | |
7424 | 0 | realMinor = ssl->version.minor; |
7425 | 0 | ssl->version.minor = args->pv.minor; |
7426 | 0 | ret = HashInput(ssl, input + args->begin, (int)helloSz); |
7427 | 0 | ssl->version.minor = realMinor; |
7428 | 0 | if (ret == 0) { |
7429 | 0 | ret = DoClientHello(ssl, input, inOutIdx, helloSz); |
7430 | 0 | } |
7431 | 0 | goto exit_dch; |
7432 | | #else |
7433 | | WOLFSSL_MSG("Client trying to connect with lesser version than " |
7434 | | "TLS v1.3"); |
7435 | | ERROR_OUT(VERSION_ERROR, exit_dch); |
7436 | | #endif |
7437 | 0 | } |
7438 | | |
7439 | | /* From here on we are a TLS 1.3 ClientHello. */ |
7440 | | |
7441 | | /* Client random */ |
7442 | 0 | XMEMCPY(ssl->arrays->clientRandom, input + args->idx, RAN_LEN); |
7443 | 0 | args->idx += RAN_LEN; |
7444 | |
|
7445 | | #ifdef WOLFSSL_DEBUG_TLS |
7446 | | WOLFSSL_MSG("client random"); |
7447 | | WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN); |
7448 | | #endif |
7449 | |
|
7450 | 0 | sessIdSz = input[args->idx++]; |
7451 | 0 | if (sessIdSz > ID_LEN) |
7452 | 0 | { |
7453 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7454 | 0 | } |
7455 | | |
7456 | 0 | if (sessIdSz + args->idx > helloSz) |
7457 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7458 | |
|
7459 | | #ifdef WOLFSSL_DTLS13 |
7460 | | /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty |
7461 | | * legacy_session_id_echo. Don't store the client's value so it |
7462 | | * won't be echoed in SendTls13ServerHello. */ |
7463 | | if (ssl->options.dtls) { |
7464 | | ssl->session->sessionIDSz = 0; |
7465 | | } |
7466 | | else |
7467 | | #endif |
7468 | 0 | { |
7469 | 0 | ssl->session->sessionIDSz = sessIdSz; |
7470 | 0 | if (sessIdSz > 0) |
7471 | 0 | XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz); |
7472 | 0 | } |
7473 | 0 | args->idx += sessIdSz; |
7474 | |
|
7475 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
7476 | | /* RFC 8446 Appendix D.4: server MUST only send CCS if the client's |
7477 | | * ClientHello contains a non-empty legacy_session_id. */ |
7478 | | if (sessIdSz == 0) { |
7479 | | ssl->options.tls13MiddleBoxCompat = 0; |
7480 | | } |
7481 | | #endif |
7482 | |
|
7483 | | #ifdef WOLFSSL_DTLS13 |
7484 | | /* legacy_cookie */ |
7485 | | if (ssl->options.dtls) { |
7486 | | /* https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 */ |
7487 | | byte cookieLen = input[args->idx++]; |
7488 | | if (cookieLen != 0) { |
7489 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7490 | | } |
7491 | | } |
7492 | | #endif /* WOLFSSL_DTLS13 */ |
7493 | |
|
7494 | 0 | XFREE(ssl->clSuites, ssl->heap, DYNAMIC_TYPE_SUITES); |
7495 | 0 | ssl->clSuites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap, |
7496 | 0 | DYNAMIC_TYPE_SUITES); |
7497 | 0 | if (ssl->clSuites == NULL) { |
7498 | 0 | ERROR_OUT(MEMORY_E, exit_dch); |
7499 | 0 | } |
7500 | | |
7501 | | /* Cipher suites */ |
7502 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7503 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7504 | 0 | ato16(&input[args->idx], &ssl->clSuites->suiteSz); |
7505 | 0 | args->idx += OPAQUE16_LEN; |
7506 | 0 | if ((ssl->clSuites->suiteSz % 2) != 0) { |
7507 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7508 | 0 | } |
7509 | | /* suites and compression length check */ |
7510 | 0 | if ((args->idx - args->begin) + ssl->clSuites->suiteSz + OPAQUE8_LEN > |
7511 | 0 | helloSz) { |
7512 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7513 | 0 | } |
7514 | 0 | if (ssl->clSuites->suiteSz > WOLFSSL_MAX_SUITE_SZ) |
7515 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7516 | 0 | XMEMCPY(ssl->clSuites->suites, input + args->idx, ssl->clSuites->suiteSz); |
7517 | 0 | args->idx += ssl->clSuites->suiteSz; |
7518 | 0 | ssl->clSuites->hashSigAlgoSz = 0; |
7519 | | |
7520 | | /* Compression */ |
7521 | 0 | b = input[args->idx++]; |
7522 | 0 | if ((args->idx - args->begin) + b > helloSz) |
7523 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7524 | 0 | if (b != COMP_LEN) { |
7525 | 0 | WOLFSSL_MSG("Must be one compression type in list"); |
7526 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7527 | 0 | } |
7528 | 0 | b = input[args->idx++]; |
7529 | 0 | if (b != NO_COMPRESSION) { |
7530 | 0 | WOLFSSL_MSG("Must be no compression type in list"); |
7531 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7532 | 0 | } |
7533 | | |
7534 | | /* Extensions */ |
7535 | 0 | if ((args->idx - args->begin) == helloSz) |
7536 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7537 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > helloSz) |
7538 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7539 | |
|
7540 | 0 | ato16(&input[args->idx], &totalExtSz); |
7541 | 0 | args->idx += OPAQUE16_LEN; |
7542 | 0 | if ((args->idx - args->begin) + totalExtSz > helloSz) |
7543 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dch); |
7544 | | |
7545 | | /* Auto populate extensions supported unless user defined. */ |
7546 | 0 | if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0) |
7547 | 0 | goto exit_dch; |
7548 | | |
7549 | | #if defined(HAVE_ECH) |
7550 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
7551 | | /* save the start of the buffer so we can use it when parsing ech */ |
7552 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
7553 | | |
7554 | | if (echX == NULL) |
7555 | | ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch); |
7556 | | |
7557 | | ((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ; |
7558 | | ((WOLFSSL_ECH*)echX->data)->aadLen = helloSz; |
7559 | | } |
7560 | | #endif |
7561 | | |
7562 | | /* Parse extensions */ |
7563 | 0 | if ((ret = TLSX_Parse(ssl, input + args->idx, totalExtSz, client_hello, |
7564 | 0 | ssl->clSuites))) { |
7565 | 0 | goto exit_dch; |
7566 | 0 | } |
7567 | | |
7568 | | #if defined(HAVE_ECH) |
7569 | | if (!ssl->options.echProcessingInner && echX != NULL && |
7570 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE) { |
7571 | | if (((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7572 | | /* Client sent real ECH and inner hello was decrypted, jump to |
7573 | | * exit so the caller can re-invoke with the inner hello */ |
7574 | | goto exit_dch; |
7575 | | } |
7576 | | else { |
7577 | | /* If ECH was accepted in ClientHello1 then ClientHello2 MUST |
7578 | | * contain an ECH extension */ |
7579 | | if (ssl->options.serverState == |
7580 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE && |
7581 | | ssl->options.echAccepted) { |
7582 | | WOLFSSL_MSG("Client did not send an EncryptedClientHello " |
7583 | | "extension"); |
7584 | | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7585 | | } |
7586 | | /* Server has ECH but client did not send ECH. Clear the |
7587 | | * response flag so the empty ECH extension is not written |
7588 | | * in EncryptedExtensions. */ |
7589 | | echX->resp = 0; |
7590 | | } |
7591 | | } |
7592 | | #endif |
7593 | | |
7594 | 0 | #ifdef HAVE_SNI |
7595 | 0 | if ((ret = SNI_Callback(ssl)) != 0) |
7596 | 0 | goto exit_dch; |
7597 | 0 | ssl->options.side = WOLFSSL_SERVER_END; |
7598 | 0 | #endif |
7599 | |
|
7600 | 0 | args->idx += totalExtSz; |
7601 | 0 | ssl->options.haveSessionId = 1; |
7602 | 0 | ssl->options.sendVerify = SEND_CERT; |
7603 | |
|
7604 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
7605 | | ssl->options.cookieGood = 0; |
7606 | | if (ssl->options.sendCookie && |
7607 | | (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE |
7608 | | #ifdef WOLFSSL_DTLS13 |
7609 | | /* Always check for a valid cookie since we may have already |
7610 | | * sent a HRR but we reset the state. */ |
7611 | | || ssl->options.dtls |
7612 | | #endif |
7613 | | )) { |
7614 | | TLSX* ext = TLSX_Find(ssl->extensions, TLSX_COOKIE); |
7615 | | |
7616 | | if (ext != NULL) { |
7617 | | /* Ensure the cookie came from client and isn't the one in the |
7618 | | * response - HelloRetryRequest. |
7619 | | */ |
7620 | | if (ext->resp == 0) { |
7621 | | ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data); |
7622 | | if (ret != 0) |
7623 | | goto exit_dch; |
7624 | | /* Don't change state here as we may want to enter |
7625 | | * DoTls13ClientHello again. */ |
7626 | | ssl->options.cookieGood = 1; |
7627 | | } |
7628 | | else { |
7629 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7630 | | } |
7631 | | } |
7632 | | else { |
7633 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_DTLS13_NO_HRR_ON_RESUME) |
7634 | | /* Don't error out as we may be resuming. We confirm this later. */ |
7635 | | if (!ssl->options.dtls) |
7636 | | #endif |
7637 | | ERROR_OUT(HRR_COOKIE_ERROR, exit_dch); |
7638 | | } |
7639 | | } |
7640 | | #endif |
7641 | |
|
7642 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7643 | 0 | if (ssl->hrr_keyshare_group != 0) { |
7644 | | /* |
7645 | | * https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8 |
7646 | | * when sending the new ClientHello, the client MUST |
7647 | | * replace the original "key_share" extension with one containing only |
7648 | | * a new KeyShareEntry for the group indicated in the selected_group |
7649 | | * field of the triggering HelloRetryRequest. |
7650 | | */ |
7651 | 0 | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7652 | 0 | if (extension != NULL) { |
7653 | 0 | KeyShareEntry* kse = (KeyShareEntry*)extension->data; |
7654 | | /* Exactly one KeyShareEntry with the HRR group must be present. */ |
7655 | 0 | if (kse == NULL || kse->next != NULL || |
7656 | 0 | kse->group != ssl->hrr_keyshare_group) { |
7657 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7658 | 0 | } |
7659 | 0 | } |
7660 | 0 | else |
7661 | 0 | ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch); |
7662 | 0 | } |
7663 | 0 | #endif |
7664 | | |
7665 | | #if defined(HAVE_ECH) |
7666 | | /* hash clientHelloInner to hsHashesEch */ |
7667 | | if (echX != NULL && ssl->ctx->echConfigs != NULL && |
7668 | | !ssl->options.disableECH && |
7669 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7670 | | ret = EchHashHelloInner(ssl, (WOLFSSL_ECH*)echX->data); |
7671 | | if (ret != 0) |
7672 | | goto exit_dch; |
7673 | | ((WOLFSSL_ECH*)echX->data)->innerCount = 1; |
7674 | | } |
7675 | | #endif |
7676 | | |
7677 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7678 | | defined(HAVE_TLS_EXTENSIONS) |
7679 | | ret = CheckPreSharedKeys(ssl, input + args->begin, helloSz, ssl->clSuites, |
7680 | | &args->usingPSK); |
7681 | | if (ret != 0) |
7682 | | goto exit_dch; |
7683 | | #else |
7684 | 0 | if ((ret = HashInput(ssl, input + args->begin, (int)helloSz)) != 0) |
7685 | 0 | goto exit_dch; |
7686 | 0 | #endif |
7687 | | |
7688 | | #if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \ |
7689 | | defined(HAVE_TLS_EXTENSIONS) |
7690 | | if (!args->usingPSK |
7691 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
7692 | | || ssl->options.certWithExternPsk |
7693 | | #endif |
7694 | | ) |
7695 | | #endif |
7696 | 0 | { |
7697 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7698 | | /* Not using PSK so don't require no KE. */ |
7699 | | ssl->options.noPskDheKe = 0; |
7700 | | #endif |
7701 | |
|
7702 | 0 | #ifndef NO_CERTS |
7703 | 0 | if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) { |
7704 | 0 | WOLFSSL_MSG("Client did not send a KeyShare extension"); |
7705 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7706 | 0 | } |
7707 | | /* Can't check ssl->extensions here as SigAlgs are unconditionally |
7708 | | set by TLSX_PopulateExtensions */ |
7709 | 0 | if (ssl->clSuites->hashSigAlgoSz == 0) { |
7710 | 0 | WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension"); |
7711 | 0 | ERROR_OUT(INCOMPLETE_DATA, exit_dch); |
7712 | 0 | } |
7713 | | #else |
7714 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7715 | | #endif |
7716 | 0 | } |
7717 | |
|
7718 | | #ifdef HAVE_ALPN |
7719 | | /* With PSK and all other things validated, it's time to |
7720 | | * select the ALPN protocol, if so requested */ |
7721 | | if ((ret = ALPN_Select(ssl)) != 0) |
7722 | | goto exit_dch; |
7723 | | #endif |
7724 | | #if defined(HAVE_SESSION_TICKET) && (defined(HAVE_SNI) || defined(HAVE_ALPN)) |
7725 | | if ((ret = VerifyTicketBinding(ssl)) != 0) |
7726 | | goto exit_dch; |
7727 | | #endif |
7728 | 0 | } /* case TLS_ASYNC_BEGIN */ |
7729 | 0 | FALL_THROUGH; |
7730 | |
|
7731 | 0 | case TLS_ASYNC_BUILD: |
7732 | | /* Advance state and proceed */ |
7733 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
7734 | 0 | FALL_THROUGH; |
7735 | |
|
7736 | 0 | case TLS_ASYNC_DO: |
7737 | 0 | { |
7738 | | #ifdef WOLFSSL_CERT_SETUP_CB |
7739 | | if ((ret = CertSetupCbWrapper(ssl)) != 0) |
7740 | | goto exit_dch; |
7741 | | #endif |
7742 | 0 | #ifndef NO_CERTS |
7743 | 0 | if (!args->usingPSK) { |
7744 | 0 | if ((ret = MatchSuite(ssl, ssl->clSuites)) < 0) { |
7745 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7746 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7747 | | #endif |
7748 | 0 | WOLFSSL_MSG("Unsupported cipher suite, ClientHello 1.3"); |
7749 | 0 | goto exit_dch; |
7750 | 0 | } |
7751 | 0 | } |
7752 | 0 | #endif |
7753 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
7754 | 0 | if (args->usingPSK == 2) { |
7755 | | /* Pick key share and Generate a new key if not present. */ |
7756 | 0 | int doHelloRetry = 0; |
7757 | 0 | ret = TLSX_KeyShare_Establish(ssl, &doHelloRetry); |
7758 | 0 | if (doHelloRetry) { |
7759 | | /* Make sure we don't send HRR twice */ |
7760 | 0 | if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7761 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7762 | 0 | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7763 | 0 | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
7764 | 0 | ret = 0; /* for hello_retry return 0 */ |
7765 | 0 | } |
7766 | 0 | if (ret != 0) |
7767 | 0 | goto exit_dch; |
7768 | 0 | } |
7769 | 0 | #endif |
7770 | | |
7771 | | /* Verify the cipher suite is the same as what was chosen in HRR. |
7772 | | * got_client_hello == 2 covers the stateful path. |
7773 | | * cookieGood covers the stateless DTLS path. */ |
7774 | 0 | if ((ssl->msgsReceived.got_client_hello == 2 |
7775 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7776 | | || ssl->options.cookieGood |
7777 | | #endif |
7778 | 0 | ) && |
7779 | 0 | (ssl->options.cipherSuite0 != ssl->options.hrrCipherSuite0 || |
7780 | 0 | ssl->options.cipherSuite != ssl->options.hrrCipherSuite)) { |
7781 | 0 | WOLFSSL_MSG("Cipher suite in second ClientHello does not match " |
7782 | 0 | "HelloRetryRequest"); |
7783 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7784 | 0 | } |
7785 | | |
7786 | | /* Advance state and proceed */ |
7787 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
7788 | 0 | } /* case TLS_ASYNC_BUILD || TLS_ASYNC_DO */ |
7789 | 0 | FALL_THROUGH; |
7790 | |
|
7791 | 0 | case TLS_ASYNC_VERIFY: |
7792 | 0 | { |
7793 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SUPPORTED_CURVES) |
7794 | | /* Check if the KeyShare calculations from the previous state are complete. |
7795 | | * wolfSSL_AsyncPop advances ssl->options.asyncState so we may end up here |
7796 | | * with a pending calculation. */ |
7797 | | TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
7798 | | if (extension != NULL && extension->resp == 1) { |
7799 | | KeyShareEntry* serverKSE = (KeyShareEntry*)extension->data; |
7800 | | if (serverKSE != NULL && |
7801 | | serverKSE->lastRet == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7802 | | #if defined(WOLFSSL_HAVE_MLKEM) |
7803 | | if (WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(serverKSE->group)) { |
7804 | | ret = TLSX_KeyShare_HandlePqcHybridKeyServer(ssl, serverKSE, |
7805 | | serverKSE->ke, serverKSE->keLen); |
7806 | | } |
7807 | | else |
7808 | | #endif |
7809 | | { |
7810 | | ret = TLSX_KeyShare_GenKey(ssl, serverKSE); |
7811 | | } |
7812 | | if (ret != 0) |
7813 | | goto exit_dch; |
7814 | | } |
7815 | | } |
7816 | | #endif |
7817 | | /* Advance state and proceed */ |
7818 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
7819 | 0 | } |
7820 | 0 | FALL_THROUGH; |
7821 | |
|
7822 | 0 | case TLS_ASYNC_FINALIZE: |
7823 | 0 | { |
7824 | 0 | *inOutIdx = args->idx; |
7825 | 0 | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
7826 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7827 | | ssl->options.pskNegotiated = (args->usingPSK != 0); |
7828 | | #endif |
7829 | |
|
7830 | 0 | if (!args->usingPSK) { |
7831 | 0 | #ifndef NO_CERTS |
7832 | | /* Check that the negotiated ciphersuite matches protocol version. */ |
7833 | | #ifdef HAVE_NULL_CIPHER |
7834 | | if (ssl->options.cipherSuite0 == ECC_BYTE && |
7835 | | (ssl->options.cipherSuite == TLS_SHA256_SHA256 || |
7836 | | ssl->options.cipherSuite == TLS_SHA384_SHA384)) { |
7837 | | ; |
7838 | | } |
7839 | | else |
7840 | | #endif |
7841 | | #if defined(WOLFSSL_SM4_GCM) && defined(WOLFSSL_SM3) |
7842 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7843 | | ssl->options.cipherSuite == TLS_SM4_GCM_SM3) { |
7844 | | ; /* Do nothing. */ |
7845 | | } |
7846 | | else |
7847 | | #endif |
7848 | | #if defined(WOLFSSL_SM4_CCM) && defined(WOLFSSL_SM3) |
7849 | | if (ssl->options.cipherSuite0 == CIPHER_BYTE && |
7850 | | ssl->options.cipherSuite == TLS_SM4_CCM_SM3) { |
7851 | | ; /* Do nothing. */ |
7852 | | } |
7853 | | else |
7854 | | #endif |
7855 | 0 | if (ssl->options.cipherSuite0 != TLS13_BYTE) { |
7856 | 0 | WOLFSSL_MSG("Negotiated ciphersuite from lesser version than " |
7857 | 0 | "TLS v1.3"); |
7858 | 0 | ERROR_OUT(MATCH_SUITE_ERROR, exit_dch); |
7859 | 0 | } |
7860 | | |
7861 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
7862 | | if (ssl->options.resuming) { |
7863 | | ssl->options.resuming = 0; |
7864 | | ssl->arrays->psk_keySz = 0; |
7865 | | XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size); |
7866 | | } |
7867 | | #endif |
7868 | | |
7869 | | /* Derive early secret for handshake secret. */ |
7870 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
7871 | 0 | goto exit_dch; |
7872 | 0 | #endif /* !NO_CERTS */ |
7873 | 0 | } |
7874 | 0 | break; |
7875 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
7876 | 0 | default: |
7877 | 0 | ret = INPUT_CASE_ERROR; |
7878 | 0 | } /* switch (ssl->options.asyncState) */ |
7879 | | |
7880 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
7881 | | if (ret == 0 && ssl->options.sendCookie) { |
7882 | | if (ssl->options.cookieGood && |
7883 | | ssl->options.acceptState == TLS13_ACCEPT_FIRST_REPLY_DONE) { |
7884 | | /* Processing second ClientHello. Clear HRR state. */ |
7885 | | ssl->options.serverState = NULL_STATE; |
7886 | | } |
7887 | | |
7888 | | if (ssl->options.cookieGood && |
7889 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7890 | | /* If we already verified the peer with a cookie then we can't |
7891 | | * do another HRR for cipher negotiation. Send alert and restart |
7892 | | * the entire handshake. */ |
7893 | | ERROR_OUT(INVALID_PARAMETER, exit_dch); |
7894 | | } |
7895 | | #ifdef WOLFSSL_DTLS13 |
7896 | | if (ssl->options.dtls && |
7897 | | ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7898 | | /* Cookie and key share negotiation should be handled in |
7899 | | * DoClientHelloStateless. If we enter here then something went |
7900 | | * wrong in our logic. */ |
7901 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7902 | | } |
7903 | | #endif |
7904 | | /* Send a cookie */ |
7905 | | if (!ssl->options.cookieGood && |
7906 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
7907 | | #ifdef WOLFSSL_DTLS13 |
7908 | | if (ssl->options.dtls) { |
7909 | | #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME |
7910 | | /* We can skip cookie on resumption */ |
7911 | | if (!ssl->options.dtls || !ssl->options.dtls13NoHrrOnResume || |
7912 | | !args->usingPSK) |
7913 | | #endif |
7914 | | ERROR_OUT(BAD_HELLO, exit_dch); |
7915 | | } |
7916 | | else |
7917 | | #endif |
7918 | | { |
7919 | | /* Need to remove the keyshare ext if we found a common group |
7920 | | * and are not doing curve negotiation. */ |
7921 | | TLSX_Remove(&ssl->extensions, TLSX_KEY_SHARE, ssl->heap); |
7922 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
7923 | | } |
7924 | | |
7925 | | } |
7926 | | } |
7927 | | #endif /* WOLFSSL_DTLS13 */ |
7928 | | |
7929 | | #ifdef WOLFSSL_DTLS_CID |
7930 | | /* do not modify CID state if we are sending an HRR */ |
7931 | | if (ret == 0 && ssl->options.dtls && ssl->options.useDtlsCID && |
7932 | | ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE) |
7933 | | DtlsCIDOnExtensionsParsed(ssl); |
7934 | | #endif /* WOLFSSL_DTLS_CID */ |
7935 | | |
7936 | | |
7937 | | |
7938 | 0 | exit_dch: |
7939 | |
|
7940 | 0 | WOLFSSL_LEAVE("DoTls13ClientHello", ret); |
7941 | |
|
7942 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7943 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
7944 | | ssl->msgsReceived.got_client_hello = 0; |
7945 | | return ret; |
7946 | | } |
7947 | | #endif |
7948 | |
|
7949 | 0 | FreeDch13Args(ssl, args); |
7950 | | #ifdef WOLFSSL_ASYNC_CRYPT |
7951 | | FreeAsyncCtx(ssl, 0); |
7952 | | #endif |
7953 | 0 | WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO); |
7954 | |
|
7955 | 0 | if (ret != 0) { |
7956 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
7957 | 0 | } |
7958 | |
|
7959 | | #if defined(HAVE_ECH) |
7960 | | if (ret == 0 && echX != NULL && |
7961 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
7962 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
7963 | | |
7964 | | /* add the header to the inner hello */ |
7965 | | AddTls13HandShakeHeader(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
7966 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen, 0, 0, |
7967 | | client_hello, ssl); |
7968 | | } |
7969 | | #endif |
7970 | |
|
7971 | 0 | return ret; |
7972 | 0 | } |
7973 | | |
7974 | | /* Send TLS v1.3 ServerHello message to client. |
7975 | | * Only a server will send this message. |
7976 | | * |
7977 | | * ssl The SSL/TLS object. |
7978 | | * returns 0 on success, otherwise failure. |
7979 | | */ |
7980 | | /* handle generation of TLS 1.3 server_hello (2) */ |
7981 | | int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType) |
7982 | 0 | { |
7983 | 0 | int ret; |
7984 | 0 | byte* output; |
7985 | 0 | word16 length; |
7986 | 0 | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
7987 | 0 | int sendSz; |
7988 | | #if defined(HAVE_ECH) |
7989 | | TLSX* echX = NULL; |
7990 | | byte* acceptLabel = (byte*)echAcceptConfirmationLabel; |
7991 | | word32 acceptOffset; |
7992 | | word16 acceptLabelSz = ECH_ACCEPT_CONFIRMATION_LABEL_SZ; |
7993 | | #endif |
7994 | |
|
7995 | 0 | WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND); |
7996 | 0 | WOLFSSL_ENTER("SendTls13ServerHello"); |
7997 | | |
7998 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated in |
7999 | | * dtls.c */ |
8000 | 0 | if (extMsgType == hello_retry_request |
8001 | | #ifdef WOLFSSL_DTLS13 |
8002 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
8003 | | #endif |
8004 | 0 | ) { |
8005 | 0 | WOLFSSL_MSG("wolfSSL Sending HelloRetryRequest"); |
8006 | 0 | if ((ret = RestartHandshakeHash(ssl)) < 0) |
8007 | 0 | return ret; |
8008 | 0 | } |
8009 | | |
8010 | 0 | ssl->options.buildingMsg = 1; |
8011 | | #ifdef WOLFSSL_DTLS13 |
8012 | | if (ssl->options.dtls) |
8013 | | idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ; |
8014 | | #endif /* WOLFSSL_DTLS13 */ |
8015 | | |
8016 | | /* Protocol version, server random, session id, cipher suite, compression |
8017 | | * and extensions. |
8018 | | */ |
8019 | 0 | length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz + |
8020 | 0 | SUITE_LEN + COMP_LEN; |
8021 | 0 | ret = TLSX_GetResponseSize(ssl, extMsgType, &length); |
8022 | 0 | if (ret != 0) |
8023 | 0 | return ret; |
8024 | 0 | sendSz = (int)(idx + length); |
8025 | | |
8026 | | /* Check buffers are big enough and grow if needed. */ |
8027 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8028 | 0 | return ret; |
8029 | | |
8030 | | /* Get position in output buffer to write new message to. */ |
8031 | 0 | output = GetOutputBuffer(ssl); |
8032 | | |
8033 | | /* Put the record and handshake headers on. */ |
8034 | 0 | AddTls13Headers(output, length, server_hello, ssl); |
8035 | | |
8036 | | /* The protocol version must be TLS v1.2 for middleboxes. */ |
8037 | 0 | output[idx++] = ssl->version.major; |
8038 | 0 | output[idx++] = ssl->options.dtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR; |
8039 | |
|
8040 | 0 | if (extMsgType == server_hello) { |
8041 | | /* Generate server random. */ |
8042 | 0 | if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0) |
8043 | 0 | return ret; |
8044 | 0 | } |
8045 | 0 | else { |
8046 | | /* HelloRetryRequest message has fixed value for random. */ |
8047 | 0 | XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN); |
8048 | 0 | } |
8049 | | |
8050 | | #if defined(HAVE_ECH) |
8051 | | /* last 8 bytes of server random */ |
8052 | | acceptOffset = idx + RAN_LEN - ECH_ACCEPT_CONFIRMATION_SZ; |
8053 | | #endif |
8054 | | |
8055 | | /* Store in SSL for debugging. */ |
8056 | 0 | XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN); |
8057 | 0 | idx += RAN_LEN; |
8058 | |
|
8059 | | #ifdef WOLFSSL_DEBUG_TLS |
8060 | | WOLFSSL_MSG("Server random"); |
8061 | | WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN); |
8062 | | #endif |
8063 | |
|
8064 | | #ifdef WOLFSSL_DTLS13 |
8065 | | if (ssl->options.dtls) { |
8066 | | /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty |
8067 | | * legacy_session_id_echo. */ |
8068 | | output[idx++] = 0; |
8069 | | } |
8070 | | else |
8071 | | #endif |
8072 | 0 | { |
8073 | 0 | output[idx++] = ssl->session->sessionIDSz; |
8074 | 0 | if (ssl->session->sessionIDSz > 0) { |
8075 | 0 | XMEMCPY(output + idx, ssl->session->sessionID, |
8076 | 0 | ssl->session->sessionIDSz); |
8077 | 0 | idx += ssl->session->sessionIDSz; |
8078 | 0 | } |
8079 | 0 | } |
8080 | | |
8081 | | /* Chosen cipher suite */ |
8082 | 0 | output[idx++] = ssl->options.cipherSuite0; |
8083 | 0 | output[idx++] = ssl->options.cipherSuite; |
8084 | | #ifdef WOLFSSL_DEBUG_TLS |
8085 | | WOLFSSL_MSG("Chosen cipher suite:"); |
8086 | | WOLFSSL_MSG(GetCipherNameInternal(ssl->options.cipherSuite0, |
8087 | | ssl->options.cipherSuite)); |
8088 | | #endif |
8089 | | |
8090 | | /* Compression not supported in TLS v1.3. */ |
8091 | 0 | output[idx++] = 0; |
8092 | | |
8093 | | /* Extensions */ |
8094 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL); |
8095 | 0 | if (ret != 0) |
8096 | 0 | return ret; |
8097 | | |
8098 | | /* When we send a HRR, we store the selected key share group to later check |
8099 | | * that the client uses the same group in the second ClientHello. |
8100 | | * |
8101 | | * In case of stateless DTLS, we do not store the group, however, as it is |
8102 | | * already stored in the cookie that is sent to the client. We later recover |
8103 | | * the group from the cookie to prevent storing a state in a stateless |
8104 | | * server. |
8105 | | * |
8106 | | * Similar logic holds for the hrrCipherSuite. */ |
8107 | 0 | if (extMsgType == hello_retry_request |
8108 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE) |
8109 | | && (!ssl->options.dtls || ssl->options.dtlsStateful) |
8110 | | #endif |
8111 | 0 | ) { |
8112 | 0 | TLSX* ksExt = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE); |
8113 | 0 | if (ksExt != NULL) { |
8114 | 0 | KeyShareEntry* kse = (KeyShareEntry*)ksExt->data; |
8115 | 0 | if (kse != NULL) |
8116 | 0 | ssl->hrr_keyshare_group = kse->group; |
8117 | 0 | } |
8118 | |
|
8119 | 0 | ssl->options.hrrCipherSuite0 = ssl->options.cipherSuite0; |
8120 | 0 | ssl->options.hrrCipherSuite = ssl->options.cipherSuite; |
8121 | 0 | } |
8122 | |
|
8123 | | #ifdef WOLFSSL_SEND_HRR_COOKIE |
8124 | | if (ssl->options.sendCookie && extMsgType == hello_retry_request) { |
8125 | | /* Reset the hashes from here. We will be able to restart the hashes |
8126 | | * from the cookie in RestartHandshakeHashWithCookie */ |
8127 | | #ifdef WOLFSSL_DTLS13 |
8128 | | /* When ssl->options.dtlsStateful is not set then cookie is calculated |
8129 | | * in dtls.c */ |
8130 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) |
8131 | | ret = 0; |
8132 | | else |
8133 | | #endif |
8134 | | ret = InitHandshakeHashes(ssl); |
8135 | | } |
8136 | | else |
8137 | | #endif |
8138 | 0 | { |
8139 | | #ifdef WOLFSSL_DTLS13 |
8140 | | if (ssl->options.dtls) { |
8141 | | ret = Dtls13HashHandshake( |
8142 | | ssl, |
8143 | | output + Dtls13GetRlHeaderLength(ssl, 0) , |
8144 | | (word16)sendSz - Dtls13GetRlHeaderLength(ssl, 0)); |
8145 | | } |
8146 | | else |
8147 | | #endif /* WOLFSSL_DTLS13 */ |
8148 | 0 | { |
8149 | | #if defined(HAVE_ECH) |
8150 | | if (ssl->ctx->echConfigs != NULL && !ssl->options.disableECH) { |
8151 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
8152 | | if (echX == NULL) |
8153 | | return WOLFSSL_FATAL_ERROR; |
8154 | | /* use hrr offset */ |
8155 | | if (extMsgType == hello_retry_request) { |
8156 | | acceptOffset = |
8157 | | (word32)(((WOLFSSL_ECH*)echX->data)->confBuf - output); |
8158 | | acceptLabel = (byte*)echHrrAcceptConfirmationLabel; |
8159 | | acceptLabelSz = ECH_HRR_ACCEPT_CONFIRMATION_LABEL_SZ; |
8160 | | } |
8161 | | /* replace the last 8 bytes of server random with the accept */ |
8162 | | if (((WOLFSSL_ECH*)echX->data)->state == ECH_PARSED_INTERNAL) { |
8163 | | if (ret == 0) { |
8164 | | ret = EchWriteAcceptance(ssl, acceptLabel, |
8165 | | acceptLabelSz, output + RECORD_HEADER_SZ, |
8166 | | acceptOffset - RECORD_HEADER_SZ, |
8167 | | sendSz - RECORD_HEADER_SZ, extMsgType); |
8168 | | } |
8169 | | if (extMsgType == hello_retry_request) { |
8170 | | /* reset the ech state for round 2 */ |
8171 | | ((WOLFSSL_ECH*)echX->data)->state = ECH_WRITE_NONE; |
8172 | | /* inner hello no longer needed, free it */ |
8173 | | XFREE(((WOLFSSL_ECH*)echX->data)->innerClientHello, |
8174 | | ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
8175 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello = NULL; |
8176 | | } |
8177 | | else { |
8178 | | if (ret == 0) { |
8179 | | /* update serverRandom on success */ |
8180 | | XMEMCPY(ssl->arrays->serverRandom, |
8181 | | output + acceptOffset - |
8182 | | (RAN_LEN -ECH_ACCEPT_CONFIRMATION_SZ), RAN_LEN); |
8183 | | } |
8184 | | /* remove ech so we don't keep sending it in write */ |
8185 | | TLSX_Remove(&ssl->extensions, TLSX_ECH, ssl->heap); |
8186 | | } |
8187 | | } |
8188 | | } |
8189 | | #endif |
8190 | 0 | if (ret == 0) |
8191 | 0 | ret = HashOutput(ssl, output, sendSz, 0); |
8192 | 0 | } |
8193 | 0 | } |
8194 | |
|
8195 | 0 | if (ret != 0) |
8196 | 0 | return ret; |
8197 | | |
8198 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8199 | | if (ssl->hsInfoOn) |
8200 | | AddPacketName(ssl, "ServerHello"); |
8201 | | if (ssl->toInfoOn) { |
8202 | | ret = AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz, |
8203 | | WRITE_PROTO, 0, ssl->heap); |
8204 | | if (ret != 0) |
8205 | | return ret; |
8206 | | } |
8207 | | #endif |
8208 | | |
8209 | 0 | if (extMsgType == server_hello) |
8210 | 0 | ssl->options.serverState = SERVER_HELLO_COMPLETE; |
8211 | |
|
8212 | 0 | ssl->options.buildingMsg = 0; |
8213 | | #ifdef WOLFSSL_DTLS13 |
8214 | | if (ssl->options.dtls) { |
8215 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)sendSz, |
8216 | | (enum HandShakeType)extMsgType, 0); |
8217 | | |
8218 | | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
8219 | | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
8220 | | return ret; |
8221 | | } |
8222 | | #endif /* WOLFSSL_DTLS13 */ |
8223 | |
|
8224 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8225 | |
|
8226 | 0 | if (!ssl->options.groupMessages || extMsgType != server_hello) |
8227 | 0 | ret = SendBuffered(ssl); |
8228 | |
|
8229 | 0 | WOLFSSL_LEAVE("SendTls13ServerHello", ret); |
8230 | 0 | WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND); |
8231 | |
|
8232 | 0 | return ret; |
8233 | 0 | } |
8234 | | |
8235 | | /* handle generation of TLS 1.3 encrypted_extensions (8) */ |
8236 | | /* Send the rest of the extensions encrypted under the handshake key. |
8237 | | * This message is always encrypted in TLS v1.3. |
8238 | | * Only a server will send this message. |
8239 | | * |
8240 | | * ssl The SSL/TLS object. |
8241 | | * returns 0 on success, otherwise failure. |
8242 | | */ |
8243 | | static int SendTls13EncryptedExtensions(WOLFSSL* ssl) |
8244 | 0 | { |
8245 | 0 | int ret; |
8246 | 0 | byte* output; |
8247 | 0 | word16 length = 0; |
8248 | 0 | word32 idx; |
8249 | 0 | int sendSz; |
8250 | |
|
8251 | 0 | WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8252 | 0 | WOLFSSL_ENTER("SendTls13EncryptedExtensions"); |
8253 | |
|
8254 | 0 | ssl->options.buildingMsg = 1; |
8255 | 0 | ssl->keys.encryptionOn = 1; |
8256 | |
|
8257 | | #ifdef WOLFSSL_DTLS13 |
8258 | | if (ssl->options.dtls) { |
8259 | | idx = Dtls13GetHeadersLength(ssl, encrypted_extensions); |
8260 | | } |
8261 | | else |
8262 | | #endif /* WOLFSSL_DTLS13 */ |
8263 | 0 | { |
8264 | 0 | idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
8265 | 0 | } |
8266 | |
|
8267 | 0 | #if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
8268 | 0 | if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0) |
8269 | 0 | return ret; |
8270 | 0 | #endif |
8271 | | |
8272 | | /* Derive the handshake secret now that we are at first message to be |
8273 | | * encrypted under the keys. |
8274 | | */ |
8275 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
8276 | 0 | return ret; |
8277 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
8278 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) |
8279 | 0 | return ret; |
8280 | | |
8281 | | /* Setup encrypt/decrypt keys for following messages. */ |
8282 | | #ifdef WOLFSSL_EARLY_DATA |
8283 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
8284 | | return ret; |
8285 | | if (ssl->earlyData != process_early_data) { |
8286 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
8287 | | return ret; |
8288 | | } |
8289 | | #else |
8290 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
8291 | 0 | return ret; |
8292 | 0 | #endif |
8293 | | #ifdef WOLFSSL_QUIC |
8294 | | if (IsAtLeastTLSv1_3(ssl->version) && WOLFSSL_IS_QUIC(ssl)) { |
8295 | | ret = wolfSSL_quic_add_transport_extensions(ssl, encrypted_extensions); |
8296 | | if (ret != 0) |
8297 | | return ret; |
8298 | | } |
8299 | | #endif |
8300 | | |
8301 | | #ifdef WOLFSSL_DTLS13 |
8302 | | if (ssl->options.dtls) { |
8303 | | w64wrapper epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
8304 | | ssl->dtls13Epoch = epochHandshake; |
8305 | | |
8306 | | ret = Dtls13SetEpochKeys( |
8307 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
8308 | | if (ret != 0) |
8309 | | return ret; |
8310 | | |
8311 | | } |
8312 | | #endif /* WOLFSSL_DTLS13 */ |
8313 | | |
8314 | 0 | ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length); |
8315 | 0 | if (ret != 0) |
8316 | 0 | return ret; |
8317 | | |
8318 | 0 | sendSz = (int)(idx + length); |
8319 | | /* Encryption always on. */ |
8320 | 0 | sendSz += MAX_MSG_EXTRA; |
8321 | | |
8322 | | /* Check buffers are big enough and grow if needed. */ |
8323 | 0 | ret = CheckAvailableSize(ssl, sendSz); |
8324 | 0 | if (ret != 0) |
8325 | 0 | return ret; |
8326 | | |
8327 | | /* Get position in output buffer to write new message to. */ |
8328 | 0 | output = GetOutputBuffer(ssl); |
8329 | | |
8330 | | /* Put the record and handshake headers on. */ |
8331 | 0 | AddTls13Headers(output, length, encrypted_extensions, ssl); |
8332 | |
|
8333 | 0 | ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL); |
8334 | 0 | if (ret != 0) |
8335 | 0 | return ret; |
8336 | 0 | idx += length; |
8337 | |
|
8338 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8339 | | if (ssl->hsInfoOn) |
8340 | | AddPacketName(ssl, "EncryptedExtensions"); |
8341 | | if (ssl->toInfoOn) { |
8342 | | ret = AddPacketInfo(ssl, "EncryptedExtensions", handshake, output, |
8343 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8344 | | if (ret != 0) |
8345 | | return ret; |
8346 | | } |
8347 | | #endif |
8348 | |
|
8349 | | #ifdef WOLFSSL_DTLS13 |
8350 | | if (ssl->options.dtls) { |
8351 | | ssl->options.buildingMsg = 0; |
8352 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)idx, |
8353 | | encrypted_extensions, 1); |
8354 | | |
8355 | | if (ret == 0) |
8356 | | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
8357 | | |
8358 | | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
8359 | | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8360 | | |
8361 | | return ret; |
8362 | | } |
8363 | | #endif /* WOLFSSL_DTLS13 */ |
8364 | | |
8365 | | /* This handshake message is always encrypted. */ |
8366 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
8367 | 0 | (int)(idx - RECORD_HEADER_SZ), |
8368 | 0 | handshake, 1, 0, 0); |
8369 | 0 | if (sendSz < 0) |
8370 | 0 | return sendSz; |
8371 | | |
8372 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8373 | 0 | ssl->options.buildingMsg = 0; |
8374 | 0 | ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE; |
8375 | |
|
8376 | 0 | if (!ssl->options.groupMessages) |
8377 | 0 | ret = SendBuffered(ssl); |
8378 | | |
8379 | |
|
8380 | 0 | WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret); |
8381 | 0 | WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND); |
8382 | |
|
8383 | 0 | return ret; |
8384 | 0 | } |
8385 | | |
8386 | | #ifndef NO_CERTS |
8387 | | /* handle generation TLS v1.3 certificate_request (13) */ |
8388 | | /* Send the TLS v1.3 CertificateRequest message. |
8389 | | * This message is always encrypted in TLS v1.3. |
8390 | | * Only a server will send this message. |
8391 | | * |
8392 | | * ssl SSL/TLS object. |
8393 | | * reqCtx Request context. |
8394 | | * reqCtxLen Length of context. 0 when sending as part of handshake. |
8395 | | * returns 0 on success, otherwise failure. |
8396 | | */ |
8397 | | static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx, |
8398 | | word32 reqCtxLen) |
8399 | 0 | { |
8400 | 0 | byte* output; |
8401 | 0 | int ret; |
8402 | 0 | int sendSz; |
8403 | 0 | word32 i; |
8404 | 0 | word32 reqSz; |
8405 | 0 | SignatureAlgorithms* sa; |
8406 | |
|
8407 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8408 | 0 | WOLFSSL_ENTER("SendTls13CertificateRequest"); |
8409 | |
|
8410 | 0 | ssl->options.buildingMsg = 1; |
8411 | |
|
8412 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) |
8413 | 0 | return SIDE_ERROR; |
8414 | | |
8415 | | /* Use ssl->suites->hashSigAlgo so wolfSSL_set1_sigalgs_list() is honored. |
8416 | | * hashSigAlgoSz=0 makes GetSize/Write fall back to WOLFSSL_SUITES(ssl). */ |
8417 | 0 | sa = TLSX_SignatureAlgorithms_New(ssl, 0, ssl->heap); |
8418 | 0 | if (sa == NULL) |
8419 | 0 | return MEMORY_ERROR; |
8420 | 0 | ret = TLSX_Push(&ssl->extensions, TLSX_SIGNATURE_ALGORITHMS, sa, ssl->heap); |
8421 | 0 | if (ret != 0) { |
8422 | 0 | TLSX_SignatureAlgorithms_FreeAll(sa, ssl->heap); |
8423 | 0 | return ret; |
8424 | 0 | } |
8425 | | |
8426 | 0 | i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
8427 | | #ifdef WOLFSSL_DTLS13 |
8428 | | if (ssl->options.dtls) |
8429 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
8430 | | #endif /* WOLFSSL_DTLS13 */ |
8431 | |
|
8432 | 0 | reqSz = (word16)(OPAQUE8_LEN + reqCtxLen); |
8433 | 0 | ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz); |
8434 | 0 | if (ret != 0) |
8435 | 0 | return ret; |
8436 | | |
8437 | 0 | sendSz = (int)(i + reqSz); |
8438 | | /* Always encrypted and make room for padding. */ |
8439 | 0 | sendSz += MAX_MSG_EXTRA; |
8440 | | |
8441 | | /* Check buffers are big enough and grow if needed. */ |
8442 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
8443 | 0 | return ret; |
8444 | | |
8445 | | /* Get position in output buffer to write new message to. */ |
8446 | 0 | output = GetOutputBuffer(ssl); |
8447 | | |
8448 | | /* Put the record and handshake headers on. */ |
8449 | 0 | AddTls13Headers(output, reqSz, certificate_request, ssl); |
8450 | | |
8451 | | /* Certificate request context. */ |
8452 | 0 | output[i++] = (byte)reqCtxLen; |
8453 | 0 | if (reqCtxLen != 0) { |
8454 | 0 | XMEMCPY(output + i, reqCtx, reqCtxLen); |
8455 | 0 | i += reqCtxLen; |
8456 | 0 | } |
8457 | | |
8458 | | /* Certificate extensions. */ |
8459 | 0 | reqSz = 0; |
8460 | 0 | ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz); |
8461 | 0 | if (ret != 0) |
8462 | 0 | return ret; |
8463 | 0 | i += reqSz; |
8464 | |
|
8465 | | #ifdef WOLFSSL_DTLS13 |
8466 | | if (ssl->options.dtls) { |
8467 | | ssl->options.buildingMsg = 0; |
8468 | | ret = |
8469 | | Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
8470 | | certificate_request, 1); |
8471 | | |
8472 | | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8473 | | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8474 | | |
8475 | | return ret; |
8476 | | |
8477 | | } |
8478 | | #endif /* WOLFSSL_DTLS13 */ |
8479 | | |
8480 | | /* Always encrypted. */ |
8481 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
8482 | 0 | (int)(i - RECORD_HEADER_SZ), handshake, 1, 0, 0); |
8483 | 0 | if (sendSz < 0) |
8484 | 0 | return sendSz; |
8485 | | |
8486 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
8487 | | if (ssl->hsInfoOn) |
8488 | | AddPacketName(ssl, "CertificateRequest"); |
8489 | | if (ssl->toInfoOn) { |
8490 | | ret = AddPacketInfo(ssl, "CertificateRequest", handshake, output, |
8491 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
8492 | | if (ret != 0) |
8493 | | return ret; |
8494 | | } |
8495 | | #endif |
8496 | | |
8497 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
8498 | 0 | ssl->options.buildingMsg = 0; |
8499 | 0 | if (!ssl->options.groupMessages) |
8500 | 0 | ret = SendBuffered(ssl); |
8501 | |
|
8502 | 0 | WOLFSSL_LEAVE("SendTls13CertificateRequest", ret); |
8503 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND); |
8504 | |
|
8505 | 0 | return ret; |
8506 | 0 | } |
8507 | | #endif /* NO_CERTS */ |
8508 | | #endif /* NO_WOLFSSL_SERVER */ |
8509 | | |
8510 | | #ifndef NO_CERTS |
8511 | | #if (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) && \ |
8512 | | (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8513 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)) |
8514 | | /* Encode the signature algorithm into buffer. |
8515 | | * |
8516 | | * hashalgo The hash algorithm. |
8517 | | * hsType The signature type. |
8518 | | * output The buffer to encode into. |
8519 | | */ |
8520 | | static WC_INLINE void EncodeSigAlg(const WOLFSSL * ssl, byte hashAlgo, |
8521 | | byte hsType, byte* output) |
8522 | 0 | { |
8523 | 0 | (void)ssl; |
8524 | 0 | switch (hsType) { |
8525 | 0 | #ifdef HAVE_ECC |
8526 | 0 | case ecc_dsa_sa_algo: |
8527 | 0 | if (ssl->pkCurveOID == ECC_BRAINPOOLP256R1_OID) { |
8528 | 0 | output[0] = NEW_SA_MAJOR; |
8529 | 0 | output[1] = ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR; |
8530 | 0 | } |
8531 | 0 | else if (ssl->pkCurveOID == ECC_BRAINPOOLP384R1_OID) { |
8532 | 0 | output[0] = NEW_SA_MAJOR; |
8533 | 0 | output[1] = ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR; |
8534 | 0 | } |
8535 | 0 | else if (ssl->pkCurveOID == ECC_BRAINPOOLP512R1_OID) { |
8536 | 0 | output[0] = NEW_SA_MAJOR; |
8537 | 0 | output[1] = ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR; |
8538 | 0 | } |
8539 | 0 | else { |
8540 | 0 | output[0] = hashAlgo; |
8541 | 0 | output[1] = ecc_dsa_sa_algo; |
8542 | 0 | } |
8543 | 0 | break; |
8544 | 0 | #endif |
8545 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8546 | | case sm2_sa_algo: |
8547 | | output[0] = SM2_SA_MAJOR; |
8548 | | output[1] = SM2_SA_MINOR; |
8549 | | break; |
8550 | | #endif |
8551 | | #ifdef HAVE_ED25519 |
8552 | | /* ED25519: 0x0807 */ |
8553 | | case ed25519_sa_algo: |
8554 | | output[0] = ED25519_SA_MAJOR; |
8555 | | output[1] = ED25519_SA_MINOR; |
8556 | | (void)hashAlgo; |
8557 | | break; |
8558 | | #endif |
8559 | | #ifdef HAVE_ED448 |
8560 | | /* ED448: 0x0808 */ |
8561 | | case ed448_sa_algo: |
8562 | | output[0] = ED448_SA_MAJOR; |
8563 | | output[1] = ED448_SA_MINOR; |
8564 | | (void)hashAlgo; |
8565 | | break; |
8566 | | #endif |
8567 | 0 | #ifndef NO_RSA |
8568 | | /* PSS signatures: 0x080[4-6] or 0x080[9-B] */ |
8569 | 0 | case rsa_pss_sa_algo: |
8570 | 0 | output[0] = rsa_pss_sa_algo; |
8571 | 0 | #ifdef WC_RSA_PSS |
8572 | | /* If the private key uses the RSA-PSS OID, and the peer supports |
8573 | | * the rsa_pss_pss_* signature algorithm in use, then report |
8574 | | * rsa_pss_pss_* rather than rsa_pss_rsae_*. */ |
8575 | 0 | if (ssl->useRsaPss && |
8576 | 0 | ((ssl->pssAlgo & (1U << hashAlgo)) != 0U) && |
8577 | 0 | (sha256_mac <= hashAlgo) && (hashAlgo <= sha512_mac)) |
8578 | 0 | { |
8579 | 0 | output[1] = PSS_RSAE_TO_PSS_PSS(hashAlgo); |
8580 | 0 | } |
8581 | 0 | else |
8582 | 0 | #endif |
8583 | 0 | { |
8584 | 0 | output[1] = hashAlgo; |
8585 | 0 | } |
8586 | 0 | break; |
8587 | 0 | #endif |
8588 | | #ifdef HAVE_FALCON |
8589 | | case falcon_level1_sa_algo: |
8590 | | output[0] = FALCON_LEVEL1_SA_MAJOR; |
8591 | | output[1] = FALCON_LEVEL1_SA_MINOR; |
8592 | | break; |
8593 | | case falcon_level5_sa_algo: |
8594 | | output[0] = FALCON_LEVEL5_SA_MAJOR; |
8595 | | output[1] = FALCON_LEVEL5_SA_MINOR; |
8596 | | break; |
8597 | | #endif |
8598 | | #ifdef HAVE_DILITHIUM |
8599 | | case dilithium_level2_sa_algo: |
8600 | | output[0] = DILITHIUM_LEVEL2_SA_MAJOR; |
8601 | | output[1] = DILITHIUM_LEVEL2_SA_MINOR; |
8602 | | break; |
8603 | | case dilithium_level3_sa_algo: |
8604 | | output[0] = DILITHIUM_LEVEL3_SA_MAJOR; |
8605 | | output[1] = DILITHIUM_LEVEL3_SA_MINOR; |
8606 | | break; |
8607 | | case dilithium_level5_sa_algo: |
8608 | | output[0] = DILITHIUM_LEVEL5_SA_MAJOR; |
8609 | | output[1] = DILITHIUM_LEVEL5_SA_MINOR; |
8610 | | break; |
8611 | | #endif |
8612 | 0 | default: |
8613 | 0 | break; |
8614 | 0 | } |
8615 | 0 | } |
8616 | | #endif |
8617 | | |
8618 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
8619 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
8620 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8621 | | /* These match up with what the OQS team has defined. */ |
8622 | | #define HYBRID_SA_MAJOR 0xFE |
8623 | | #define HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR 0xA1 |
8624 | | #define HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR 0xA2 |
8625 | | #define HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR 0xA4 |
8626 | | #define HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR 0xA6 |
8627 | | /* Falcon hybrid codepoints aligned with oqs-provider. */ |
8628 | | #define HYBRID_P256_FALCON_LEVEL1_SA_MINOR 0xD8 |
8629 | | #define HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR 0xD9 |
8630 | | #define HYBRID_P521_FALCON_LEVEL5_SA_MINOR 0xDB |
8631 | | |
8632 | | /* Custom defined ones for PQC first */ |
8633 | | #define HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR 0xD1 |
8634 | | #define HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR 0xD2 |
8635 | | #define HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR 0xD3 |
8636 | | #define HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR 0xD4 |
8637 | | #define HYBRID_FALCON_LEVEL1_P256_SA_MINOR 0xD5 |
8638 | | #define HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR 0xD6 |
8639 | | #define HYBRID_FALCON_LEVEL5_P521_SA_MINOR 0xD7 |
8640 | | |
8641 | | |
8642 | | static void EncodeDualSigAlg(byte sigAlg, byte altSigAlg, byte* output) |
8643 | | { |
8644 | | /* Initialize output to error indicator. */ |
8645 | | output[0] = 0x0; |
8646 | | output[1] = 0x0; |
8647 | | |
8648 | | if (sigAlg == ecc_dsa_sa_algo && altSigAlg == dilithium_level2_sa_algo) { |
8649 | | output[1] = HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR; |
8650 | | } |
8651 | | else if (sigAlg == rsa_pss_sa_algo && |
8652 | | altSigAlg == dilithium_level2_sa_algo) { |
8653 | | output[1] = HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR; |
8654 | | } |
8655 | | else if (sigAlg == ecc_dsa_sa_algo && |
8656 | | altSigAlg == dilithium_level3_sa_algo) { |
8657 | | output[1] = HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR; |
8658 | | } |
8659 | | else if (sigAlg == ecc_dsa_sa_algo && |
8660 | | altSigAlg == dilithium_level5_sa_algo) { |
8661 | | output[1] = HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR; |
8662 | | } |
8663 | | else if (sigAlg == ecc_dsa_sa_algo && |
8664 | | altSigAlg == falcon_level1_sa_algo) { |
8665 | | output[1] = HYBRID_P256_FALCON_LEVEL1_SA_MINOR; |
8666 | | } |
8667 | | else if (sigAlg == rsa_pss_sa_algo && |
8668 | | altSigAlg == falcon_level1_sa_algo) { |
8669 | | output[1] = HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR; |
8670 | | } |
8671 | | else if (sigAlg == ecc_dsa_sa_algo && |
8672 | | altSigAlg == falcon_level5_sa_algo) { |
8673 | | output[1] = HYBRID_P521_FALCON_LEVEL5_SA_MINOR; |
8674 | | } |
8675 | | else if (sigAlg == dilithium_level2_sa_algo && |
8676 | | altSigAlg == ecc_dsa_sa_algo) { |
8677 | | output[1] = HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR; |
8678 | | } |
8679 | | else if (sigAlg == dilithium_level2_sa_algo && |
8680 | | altSigAlg == rsa_pss_sa_algo) { |
8681 | | output[1] = HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR; |
8682 | | } |
8683 | | else if (sigAlg == dilithium_level3_sa_algo && |
8684 | | altSigAlg == ecc_dsa_sa_algo) { |
8685 | | output[1] = HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR; |
8686 | | } |
8687 | | else if (sigAlg == dilithium_level5_sa_algo && |
8688 | | altSigAlg == ecc_dsa_sa_algo) { |
8689 | | output[1] = HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR; |
8690 | | } |
8691 | | else if (sigAlg == falcon_level1_sa_algo && |
8692 | | altSigAlg == ecc_dsa_sa_algo) { |
8693 | | output[1] = HYBRID_FALCON_LEVEL1_P256_SA_MINOR; |
8694 | | } |
8695 | | else if (sigAlg == falcon_level1_sa_algo && |
8696 | | altSigAlg == rsa_pss_sa_algo) { |
8697 | | output[1] = HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR; |
8698 | | } |
8699 | | else if (sigAlg == falcon_level5_sa_algo && |
8700 | | altSigAlg == ecc_dsa_sa_algo) { |
8701 | | output[1] = HYBRID_FALCON_LEVEL5_P521_SA_MINOR; |
8702 | | } |
8703 | | |
8704 | | if (output[1] != 0x0) { |
8705 | | output[0] = HYBRID_SA_MAJOR; |
8706 | | } |
8707 | | } |
8708 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8709 | | |
8710 | | static enum wc_MACAlgorithm GetNewSAHashAlgo(int typeIn) |
8711 | 0 | { |
8712 | 0 | switch (typeIn) { |
8713 | 0 | case RSA_PSS_RSAE_SHA256_MINOR: |
8714 | 0 | case RSA_PSS_PSS_SHA256_MINOR: |
8715 | 0 | case ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR: |
8716 | 0 | return sha256_mac; |
8717 | | |
8718 | 0 | case RSA_PSS_RSAE_SHA384_MINOR: |
8719 | 0 | case RSA_PSS_PSS_SHA384_MINOR: |
8720 | 0 | case ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR: |
8721 | 0 | return sha384_mac; |
8722 | | |
8723 | 0 | case RSA_PSS_RSAE_SHA512_MINOR: |
8724 | 0 | case RSA_PSS_PSS_SHA512_MINOR: |
8725 | 0 | case ED25519_SA_MINOR: |
8726 | 0 | case ED448_SA_MINOR: |
8727 | 0 | case ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR: |
8728 | 0 | return sha512_mac; |
8729 | 0 | default: |
8730 | 0 | return no_mac; |
8731 | 0 | } |
8732 | 0 | } |
8733 | | |
8734 | | /* Decode the signature algorithm. |
8735 | | * |
8736 | | * input The encoded signature algorithm. |
8737 | | * hashalgo The hash algorithm. |
8738 | | * hsType The signature type. |
8739 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8740 | | */ |
8741 | | static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo, |
8742 | | byte* hsType) |
8743 | 0 | { |
8744 | 0 | int ret = 0; |
8745 | |
|
8746 | 0 | switch (input[0]) { |
8747 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
8748 | | case SM2_SA_MAJOR: |
8749 | | if (input[1] == SM2_SA_MINOR) { |
8750 | | *hsType = sm2_sa_algo; |
8751 | | *hashAlgo = sm3_mac; |
8752 | | } |
8753 | | else |
8754 | | ret = INVALID_PARAMETER; |
8755 | | break; |
8756 | | #endif |
8757 | 0 | case NEW_SA_MAJOR: |
8758 | 0 | { |
8759 | 0 | enum wc_MACAlgorithm mac = GetNewSAHashAlgo(input[1]); |
8760 | 0 | *hashAlgo = (byte)mac; |
8761 | 0 | } |
8762 | | |
8763 | | /* PSS encryption: 0x080[4-6] */ |
8764 | 0 | if (input[1] >= RSA_PSS_RSAE_SHA256_MINOR && |
8765 | 0 | input[1] <= RSA_PSS_RSAE_SHA512_MINOR) { |
8766 | 0 | *hsType = input[0]; |
8767 | 0 | } |
8768 | | /* PSS signature: 0x080[9-B] */ |
8769 | 0 | else if (input[1] >= RSA_PSS_PSS_SHA256_MINOR && |
8770 | 0 | input[1] <= RSA_PSS_PSS_SHA512_MINOR) { |
8771 | 0 | *hsType = input[0]; |
8772 | 0 | } |
8773 | | #ifdef HAVE_ED25519 |
8774 | | /* ED25519: 0x0807 */ |
8775 | | else if (input[1] == ED25519_SA_MINOR) { |
8776 | | *hsType = ed25519_sa_algo; |
8777 | | /* Hash performed as part of sign/verify operation. */ |
8778 | | } |
8779 | | #endif |
8780 | | #ifdef HAVE_ED448 |
8781 | | /* ED448: 0x0808 */ |
8782 | | else if (input[1] == ED448_SA_MINOR) { |
8783 | | *hsType = ed448_sa_algo; |
8784 | | /* Hash performed as part of sign/verify operation. */ |
8785 | | } |
8786 | | #endif |
8787 | | #ifdef HAVE_ECC_BRAINPOOL |
8788 | | else if ((input[1] == ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR) || |
8789 | | (input[1] == ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR) || |
8790 | | (input[1] == ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR)) { |
8791 | | *hsType = ecc_dsa_sa_algo; |
8792 | | } |
8793 | | #endif |
8794 | 0 | else |
8795 | 0 | ret = INVALID_PARAMETER; |
8796 | 0 | break; |
8797 | | #if defined(HAVE_FALCON) |
8798 | | case FALCON_SA_MAJOR: |
8799 | | if (input[1] == FALCON_LEVEL1_SA_MINOR) { |
8800 | | *hsType = falcon_level1_sa_algo; |
8801 | | /* Hash performed as part of sign/verify operation. */ |
8802 | | *hashAlgo = sha512_mac; |
8803 | | } else if (input[1] == FALCON_LEVEL5_SA_MINOR) { |
8804 | | *hsType = falcon_level5_sa_algo; |
8805 | | /* Hash performed as part of sign/verify operation. */ |
8806 | | *hashAlgo = sha512_mac; |
8807 | | } |
8808 | | else |
8809 | | ret = INVALID_PARAMETER; |
8810 | | break; |
8811 | | #endif /* HAVE_FALCON */ |
8812 | | #if defined(HAVE_DILITHIUM) |
8813 | | case DILITHIUM_SA_MAJOR: |
8814 | | if (input[1] == DILITHIUM_LEVEL2_SA_MINOR) { |
8815 | | *hsType = dilithium_level2_sa_algo; |
8816 | | /* Hash performed as part of sign/verify operation. */ |
8817 | | *hashAlgo = sha512_mac; |
8818 | | } else if (input[1] == DILITHIUM_LEVEL3_SA_MINOR) { |
8819 | | *hsType = dilithium_level3_sa_algo; |
8820 | | /* Hash performed as part of sign/verify operation. */ |
8821 | | *hashAlgo = sha512_mac; |
8822 | | } else if (input[1] == DILITHIUM_LEVEL5_SA_MINOR) { |
8823 | | *hsType = dilithium_level5_sa_algo; |
8824 | | /* Hash performed as part of sign/verify operation. */ |
8825 | | *hashAlgo = sha512_mac; |
8826 | | } |
8827 | | else |
8828 | | { |
8829 | | ret = INVALID_PARAMETER; |
8830 | | } |
8831 | | break; |
8832 | | #endif /* HAVE_DILITHIUM */ |
8833 | 0 | default: |
8834 | 0 | *hashAlgo = input[0]; |
8835 | 0 | *hsType = input[1]; |
8836 | 0 | break; |
8837 | 0 | } |
8838 | | |
8839 | 0 | return ret; |
8840 | 0 | } |
8841 | | |
8842 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
8843 | | /* Decode the hybrid signature algorithm. |
8844 | | * |
8845 | | * input The encoded signature algorithm. |
8846 | | * hashalgo The hash algorithm. |
8847 | | * hsType The signature type. |
8848 | | * returns INVALID_PARAMETER if not recognized and 0 otherwise. |
8849 | | */ |
8850 | | static WC_INLINE int DecodeTls13HybridSigAlg(byte* input, byte* hashAlg, |
8851 | | byte *sigAlg, byte *altSigAlg) |
8852 | | { |
8853 | | |
8854 | | if (input[0] != HYBRID_SA_MAJOR) { |
8855 | | return INVALID_PARAMETER; |
8856 | | } |
8857 | | |
8858 | | if (input[1] == HYBRID_P256_DILITHIUM_LEVEL2_SA_MINOR) { |
8859 | | *sigAlg = ecc_dsa_sa_algo; |
8860 | | *hashAlg = sha256_mac; |
8861 | | *altSigAlg = dilithium_level2_sa_algo; |
8862 | | } |
8863 | | else if (input[1] == HYBRID_RSA3072_DILITHIUM_LEVEL2_SA_MINOR) { |
8864 | | *sigAlg = rsa_pss_sa_algo; |
8865 | | *hashAlg = sha256_mac; |
8866 | | *altSigAlg = dilithium_level2_sa_algo; |
8867 | | } |
8868 | | else if (input[1] == HYBRID_P384_DILITHIUM_LEVEL3_SA_MINOR) { |
8869 | | *sigAlg = ecc_dsa_sa_algo; |
8870 | | *hashAlg = sha384_mac; |
8871 | | *altSigAlg = dilithium_level3_sa_algo; |
8872 | | } |
8873 | | else if (input[1] == HYBRID_P521_DILITHIUM_LEVEL5_SA_MINOR) { |
8874 | | *sigAlg = ecc_dsa_sa_algo; |
8875 | | *hashAlg = sha512_mac; |
8876 | | *altSigAlg = dilithium_level5_sa_algo; |
8877 | | } |
8878 | | else if (input[1] == HYBRID_P256_FALCON_LEVEL1_SA_MINOR) { |
8879 | | *sigAlg = ecc_dsa_sa_algo; |
8880 | | *hashAlg = sha256_mac; |
8881 | | *altSigAlg = falcon_level1_sa_algo; |
8882 | | } |
8883 | | else if (input[1] == HYBRID_RSA3072_FALCON_LEVEL1_SA_MINOR) { |
8884 | | *sigAlg = rsa_pss_sa_algo; |
8885 | | *hashAlg = sha256_mac; |
8886 | | *altSigAlg = falcon_level1_sa_algo; |
8887 | | } |
8888 | | else if (input[1] == HYBRID_P521_FALCON_LEVEL5_SA_MINOR) { |
8889 | | *sigAlg = ecc_dsa_sa_algo; |
8890 | | *hashAlg = sha512_mac; |
8891 | | *altSigAlg = falcon_level5_sa_algo; |
8892 | | } |
8893 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_P256_SA_MINOR) { |
8894 | | *sigAlg = dilithium_level2_sa_algo; |
8895 | | *hashAlg = sha256_mac; |
8896 | | *altSigAlg = ecc_dsa_sa_algo; |
8897 | | } |
8898 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL2_RSA3072_SA_MINOR) { |
8899 | | *sigAlg = dilithium_level2_sa_algo; |
8900 | | *hashAlg = sha256_mac; |
8901 | | *altSigAlg = rsa_pss_sa_algo; |
8902 | | } |
8903 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL3_P384_SA_MINOR) { |
8904 | | *sigAlg = dilithium_level3_sa_algo; |
8905 | | *hashAlg = sha384_mac; |
8906 | | *altSigAlg = ecc_dsa_sa_algo; |
8907 | | } |
8908 | | else if (input[1] == HYBRID_DILITHIUM_LEVEL5_P521_SA_MINOR) { |
8909 | | *sigAlg = dilithium_level5_sa_algo; |
8910 | | *hashAlg = sha512_mac; |
8911 | | *altSigAlg = ecc_dsa_sa_algo; |
8912 | | } |
8913 | | else if (input[1] == HYBRID_FALCON_LEVEL1_P256_SA_MINOR) { |
8914 | | *sigAlg = falcon_level1_sa_algo; |
8915 | | *hashAlg = sha256_mac; |
8916 | | *altSigAlg = ecc_dsa_sa_algo; |
8917 | | } |
8918 | | else if (input[1] == HYBRID_FALCON_LEVEL1_RSA3072_SA_MINOR) { |
8919 | | *sigAlg = falcon_level1_sa_algo; |
8920 | | *hashAlg = sha256_mac; |
8921 | | *altSigAlg = rsa_pss_sa_algo; |
8922 | | } |
8923 | | else if (input[1] == HYBRID_FALCON_LEVEL5_P521_SA_MINOR) { |
8924 | | *sigAlg = falcon_level5_sa_algo; |
8925 | | *hashAlg = sha512_mac; |
8926 | | *altSigAlg = ecc_dsa_sa_algo; |
8927 | | } |
8928 | | else { |
8929 | | return INVALID_PARAMETER; |
8930 | | } |
8931 | | |
8932 | | return 0; |
8933 | | } |
8934 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
8935 | | |
8936 | | /* Get the hash of the messages so far. |
8937 | | * |
8938 | | * ssl The SSL/TLS object. |
8939 | | * hash The buffer to write the hash to. |
8940 | | * returns the length of the hash. |
8941 | | */ |
8942 | | static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash) |
8943 | 0 | { |
8944 | 0 | int ret = 0; |
8945 | 0 | switch (ssl->specs.mac_algorithm) { |
8946 | 0 | #ifndef NO_SHA256 |
8947 | 0 | case sha256_mac: |
8948 | 0 | ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash); |
8949 | 0 | if (ret == 0) |
8950 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
8951 | 0 | break; |
8952 | 0 | #endif /* !NO_SHA256 */ |
8953 | 0 | #ifdef WOLFSSL_SHA384 |
8954 | 0 | case sha384_mac: |
8955 | 0 | ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash); |
8956 | 0 | if (ret == 0) |
8957 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
8958 | 0 | break; |
8959 | 0 | #endif /* WOLFSSL_SHA384 */ |
8960 | | #ifdef WOLFSSL_TLS13_SHA512 |
8961 | | case sha512_mac: |
8962 | | ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash); |
8963 | | if (ret == 0) |
8964 | | ret = WC_SHA512_DIGEST_SIZE; |
8965 | | break; |
8966 | | #endif /* WOLFSSL_TLS13_SHA512 */ |
8967 | | #ifdef WOLFSSL_SM3 |
8968 | | case sm3_mac: |
8969 | | ret = wc_Sm3GetHash(&ssl->hsHashes->hashSm3, hash); |
8970 | | if (ret == 0) |
8971 | | ret = WC_SM3_DIGEST_SIZE; |
8972 | | break; |
8973 | | #endif /* WOLFSSL_SM3 */ |
8974 | 0 | default: |
8975 | 0 | break; |
8976 | 0 | } |
8977 | 0 | return ret; |
8978 | 0 | } |
8979 | | |
8980 | | /* The server certificate verification label. */ |
8981 | | static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8982 | | "TLS 1.3, server CertificateVerify"; |
8983 | | /* The client certificate verification label. */ |
8984 | | static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] = |
8985 | | "TLS 1.3, client CertificateVerify"; |
8986 | | /* The prefix byte in the signature data. */ |
8987 | | #define SIGNING_DATA_PREFIX_BYTE 0x20 |
8988 | | |
8989 | | /* Create the signature data for TLS v1.3 certificate verification. |
8990 | | * |
8991 | | * ssl The SSL/TLS object. |
8992 | | * sigData The signature data. |
8993 | | * sigDataSz The length of the signature data. |
8994 | | * check Indicates this is a check not create. |
8995 | | */ |
8996 | | int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz, |
8997 | | int check) |
8998 | 0 | { |
8999 | 0 | word16 idx; |
9000 | 0 | int side = ssl->options.side; |
9001 | 0 | int ret; |
9002 | | |
9003 | | /* Signature Data = Prefix | Label | Handshake Hash */ |
9004 | 0 | XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ); |
9005 | 0 | idx = SIGNING_DATA_PREFIX_SZ; |
9006 | |
|
9007 | 0 | if ((side == WOLFSSL_SERVER_END && check) || |
9008 | 0 | (side == WOLFSSL_CLIENT_END && !check)) { |
9009 | 0 | XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ); |
9010 | 0 | } |
9011 | 0 | if ((side == WOLFSSL_CLIENT_END && check) || |
9012 | 0 | (side == WOLFSSL_SERVER_END && !check)) { |
9013 | 0 | XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ); |
9014 | 0 | } |
9015 | 0 | idx += CERT_VFY_LABEL_SZ; |
9016 | |
|
9017 | 0 | ret = GetMsgHash(ssl, &sigData[idx]); |
9018 | 0 | if (ret < 0) |
9019 | 0 | return ret; |
9020 | 0 | if (ret == 0) |
9021 | 0 | return HASH_TYPE_E; |
9022 | | |
9023 | 0 | *sigDataSz = (word16)(idx + ret); |
9024 | 0 | ret = 0; |
9025 | |
|
9026 | 0 | return ret; |
9027 | 0 | } |
9028 | | |
9029 | | #ifndef NO_RSA |
9030 | | /* Encode the PKCS #1.5 RSA signature. |
9031 | | * |
9032 | | * sig The buffer to place the encoded signature into. |
9033 | | * sigData The data to be signed. |
9034 | | * sigDataSz The size of the data to be signed. |
9035 | | * hashAlgo The hash algorithm to use when signing. |
9036 | | * returns the length of the encoded signature or negative on error. |
9037 | | */ |
9038 | | int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz, |
9039 | | int sigAlgo, int hashAlgo) |
9040 | 0 | { |
9041 | 0 | Digest digest; |
9042 | 0 | int hashSz = 0; |
9043 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
9044 | 0 | byte* hash; |
9045 | |
|
9046 | 0 | (void)sigAlgo; |
9047 | |
|
9048 | 0 | hash = sig; |
9049 | | |
9050 | | /* Digest the signature data. */ |
9051 | 0 | switch (hashAlgo) { |
9052 | 0 | #ifndef NO_SHA256 |
9053 | 0 | case sha256_mac: |
9054 | 0 | ret = wc_InitSha256(&digest.sha256); |
9055 | 0 | if (ret == 0) { |
9056 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
9057 | 0 | if (ret == 0) |
9058 | 0 | ret = wc_Sha256Final(&digest.sha256, hash); |
9059 | 0 | wc_Sha256Free(&digest.sha256); |
9060 | 0 | } |
9061 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
9062 | 0 | break; |
9063 | 0 | #endif |
9064 | 0 | #ifdef WOLFSSL_SHA384 |
9065 | 0 | case sha384_mac: |
9066 | 0 | ret = wc_InitSha384(&digest.sha384); |
9067 | 0 | if (ret == 0) { |
9068 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
9069 | 0 | if (ret == 0) |
9070 | 0 | ret = wc_Sha384Final(&digest.sha384, hash); |
9071 | 0 | wc_Sha384Free(&digest.sha384); |
9072 | 0 | } |
9073 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
9074 | 0 | break; |
9075 | 0 | #endif |
9076 | 0 | #ifdef WOLFSSL_SHA512 |
9077 | 0 | case sha512_mac: |
9078 | 0 | ret = wc_InitSha512(&digest.sha512); |
9079 | 0 | if (ret == 0) { |
9080 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
9081 | 0 | if (ret == 0) |
9082 | 0 | ret = wc_Sha512Final(&digest.sha512, hash); |
9083 | 0 | wc_Sha512Free(&digest.sha512); |
9084 | 0 | } |
9085 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
9086 | 0 | break; |
9087 | 0 | #endif |
9088 | 0 | default: |
9089 | 0 | ret = BAD_FUNC_ARG; |
9090 | 0 | break; |
9091 | |
|
9092 | 0 | } |
9093 | | |
9094 | 0 | if (ret != 0) |
9095 | 0 | return ret; |
9096 | | |
9097 | 0 | return hashSz; |
9098 | 0 | } |
9099 | | #endif /* !NO_RSA */ |
9100 | | |
9101 | | #ifdef HAVE_ECC |
9102 | | /* Encode the ECC signature. |
9103 | | * |
9104 | | * sigData The data to be signed. |
9105 | | * sigDataSz The size of the data to be signed. |
9106 | | * hashAlgo The hash algorithm to use when signing. |
9107 | | * returns the length of the encoded signature or negative on error. |
9108 | | */ |
9109 | | static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo) |
9110 | 0 | { |
9111 | 0 | Digest digest; |
9112 | 0 | int hashSz = 0; |
9113 | 0 | int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG); |
9114 | | |
9115 | | /* Digest the signature data. */ |
9116 | 0 | switch (hashAlgo) { |
9117 | 0 | #ifndef NO_SHA256 |
9118 | 0 | case sha256_mac: |
9119 | 0 | ret = wc_InitSha256(&digest.sha256); |
9120 | 0 | if (ret == 0) { |
9121 | 0 | ret = wc_Sha256Update(&digest.sha256, sigData, (word32)sigDataSz); |
9122 | 0 | if (ret == 0) |
9123 | 0 | ret = wc_Sha256Final(&digest.sha256, sigData); |
9124 | 0 | wc_Sha256Free(&digest.sha256); |
9125 | 0 | } |
9126 | 0 | hashSz = WC_SHA256_DIGEST_SIZE; |
9127 | 0 | break; |
9128 | 0 | #endif |
9129 | 0 | #ifdef WOLFSSL_SHA384 |
9130 | 0 | case sha384_mac: |
9131 | 0 | ret = wc_InitSha384(&digest.sha384); |
9132 | 0 | if (ret == 0) { |
9133 | 0 | ret = wc_Sha384Update(&digest.sha384, sigData, (word32)sigDataSz); |
9134 | 0 | if (ret == 0) |
9135 | 0 | ret = wc_Sha384Final(&digest.sha384, sigData); |
9136 | 0 | wc_Sha384Free(&digest.sha384); |
9137 | 0 | } |
9138 | 0 | hashSz = WC_SHA384_DIGEST_SIZE; |
9139 | 0 | break; |
9140 | 0 | #endif |
9141 | 0 | #ifdef WOLFSSL_SHA512 |
9142 | 0 | case sha512_mac: |
9143 | 0 | ret = wc_InitSha512(&digest.sha512); |
9144 | 0 | if (ret == 0) { |
9145 | 0 | ret = wc_Sha512Update(&digest.sha512, sigData, (word32)sigDataSz); |
9146 | 0 | if (ret == 0) |
9147 | 0 | ret = wc_Sha512Final(&digest.sha512, sigData); |
9148 | 0 | wc_Sha512Free(&digest.sha512); |
9149 | 0 | } |
9150 | 0 | hashSz = WC_SHA512_DIGEST_SIZE; |
9151 | 0 | break; |
9152 | 0 | #endif |
9153 | 0 | default: |
9154 | 0 | ret = BAD_FUNC_ARG; |
9155 | 0 | break; |
9156 | 0 | } |
9157 | | |
9158 | 0 | if (ret != 0) |
9159 | 0 | return ret; |
9160 | | |
9161 | 0 | return hashSz; |
9162 | 0 | } |
9163 | | #endif /* HAVE_ECC */ |
9164 | | |
9165 | | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
9166 | | /* Check that the decrypted signature matches the encoded signature |
9167 | | * based on the digest of the signature data. |
9168 | | * |
9169 | | * ssl The SSL/TLS object. |
9170 | | * sigAlgo The signature algorithm used to generate signature. |
9171 | | * hashAlgo The hash algorithm used to generate signature. |
9172 | | * decSig The decrypted signature. |
9173 | | * decSigSz The size of the decrypted signature. |
9174 | | * returns 0 on success, otherwise failure. |
9175 | | */ |
9176 | | static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo, |
9177 | | byte* decSig, word32 decSigSz) |
9178 | 0 | { |
9179 | 0 | int ret = 0; |
9180 | 0 | byte sigData[MAX_SIG_DATA_SZ]; |
9181 | 0 | word16 sigDataSz; |
9182 | |
|
9183 | 0 | ret = CreateSigData(ssl, sigData, &sigDataSz, 1); |
9184 | 0 | if (ret != 0) |
9185 | 0 | return ret; |
9186 | | |
9187 | 0 | if (sigAlgo == rsa_pss_sa_algo) { |
9188 | 0 | enum wc_HashType hashType = WC_HASH_TYPE_NONE; |
9189 | 0 | word32 sigSz; |
9190 | |
|
9191 | 0 | ret = ConvertHashPss(hashAlgo, &hashType, NULL); |
9192 | 0 | if (ret < 0) |
9193 | 0 | return ret; |
9194 | | |
9195 | | /* PSS signature can be done in-place */ |
9196 | 0 | ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz, |
9197 | 0 | sigAlgo, hashAlgo); |
9198 | 0 | if (ret < 0) |
9199 | 0 | return ret; |
9200 | 0 | sigSz = (word32)ret; |
9201 | |
|
9202 | 0 | ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz, |
9203 | 0 | hashType); |
9204 | 0 | } |
9205 | | |
9206 | 0 | return ret; |
9207 | 0 | } |
9208 | | #endif /* !NO_RSA && WC_RSA_PSS */ |
9209 | | #endif /* !NO_RSA || HAVE_ECC */ |
9210 | | |
9211 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
9212 | | /* Get the next certificate from the list for writing into the TLS v1.3 |
9213 | | * Certificate message. |
9214 | | * |
9215 | | * data The certificate list. |
9216 | | * length The length of the certificate data in the list. |
9217 | | * idx The index of the next certificate. |
9218 | | * returns the length of the certificate data. 0 indicates no more certificates |
9219 | | * in the list. |
9220 | | */ |
9221 | | static word32 NextCert(byte* data, word32 length, word32* idx) |
9222 | 0 | { |
9223 | 0 | word32 len; |
9224 | | |
9225 | | /* Would index read past end of list? */ |
9226 | 0 | if (*idx + 3 > length) |
9227 | 0 | return 0; |
9228 | | |
9229 | | /* Length of the current ASN.1 encoded certificate. */ |
9230 | 0 | c24to32(data + *idx, &len); |
9231 | | /* Include the length field. */ |
9232 | 0 | len += 3; |
9233 | | |
9234 | | /* Ensure len does not overrun certificate list */ |
9235 | 0 | if (*idx + len > length) |
9236 | 0 | return 0; |
9237 | | |
9238 | | /* Move index to next certificate and return the current certificate's |
9239 | | * length. |
9240 | | */ |
9241 | 0 | *idx += len; |
9242 | 0 | return len; |
9243 | 0 | } |
9244 | | |
9245 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9246 | | /* Write certificate status request into certificate to buffer. |
9247 | | * |
9248 | | * ssl SSL/TLS object. |
9249 | | * certExts DerBuffer array. buffers written |
9250 | | * extSz word32 array. |
9251 | | * Length of the certificate status request data for the certificate. |
9252 | | * extSz_num number of the CSR written |
9253 | | * extIdx The index number of certificate status request data |
9254 | | * for the certificate. |
9255 | | * offset index offset |
9256 | | * returns Total number of bytes written on success or negative value on error. |
9257 | | */ |
9258 | | static int WriteCSRToBuffer(WOLFSSL* ssl, DerBuffer** certExts, |
9259 | | word16* extSz, word16 extSz_num) |
9260 | | { |
9261 | | int ret = 0; |
9262 | | TLSX* ext; |
9263 | | CertificateStatusRequest* csr; |
9264 | | word32 ex_offset = HELLO_EXT_TYPE_SZ + OPAQUE16_LEN /* extension type */ |
9265 | | + OPAQUE16_LEN /* extension length */; |
9266 | | word32 totalSz = 0; |
9267 | | word32 tmpSz; |
9268 | | word32 extIdx; |
9269 | | DerBuffer* der; |
9270 | | |
9271 | | if (extSz_num > MAX_CERT_EXTENSIONS) |
9272 | | return MAX_CERT_EXTENSIONS_ERR; |
9273 | | |
9274 | | ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
9275 | | csr = ext ? (CertificateStatusRequest*)ext->data : NULL; |
9276 | | |
9277 | | if (csr) { |
9278 | | for (extIdx = 0; extIdx < (word16)(extSz_num); extIdx++) { |
9279 | | tmpSz = TLSX_CSR_GetSize_ex(csr, 0, (int)extIdx); |
9280 | | |
9281 | | if (tmpSz > (OPAQUE8_LEN + OPAQUE24_LEN) && |
9282 | | certExts[extIdx] == NULL) { |
9283 | | /* csr extension is not zero */ |
9284 | | if (tmpSz > WOLFSSL_MAX_16BIT) |
9285 | | return BUFFER_E; |
9286 | | extSz[extIdx] = (word16)tmpSz; |
9287 | | |
9288 | | ret = AllocDer(&certExts[extIdx], extSz[extIdx] + ex_offset, |
9289 | | CERT_TYPE, ssl->heap); |
9290 | | if (ret < 0) |
9291 | | return ret; |
9292 | | der = certExts[extIdx]; |
9293 | | |
9294 | | /* write extension type */ |
9295 | | c16toa(ext->type, der->buffer |
9296 | | + OPAQUE16_LEN); |
9297 | | /* writes extension data length. */ |
9298 | | c16toa(extSz[extIdx], der->buffer |
9299 | | + HELLO_EXT_TYPE_SZ + OPAQUE16_LEN); |
9300 | | /* write extension data */ |
9301 | | extSz[extIdx] = (word16)TLSX_CSR_Write_ex(csr, |
9302 | | der->buffer + ex_offset, 0, extIdx); |
9303 | | /* add extension offset */ |
9304 | | extSz[extIdx] += (word16)ex_offset; |
9305 | | /* extension length */ |
9306 | | c16toa(extSz[extIdx] - OPAQUE16_LEN, |
9307 | | der->buffer); |
9308 | | } |
9309 | | totalSz += extSz[extIdx]; |
9310 | | } |
9311 | | } |
9312 | | else { |
9313 | | /* chain cert empty extension size */ |
9314 | | totalSz += OPAQUE16_LEN * extSz_num; |
9315 | | } |
9316 | | return (int)totalSz; |
9317 | | } |
9318 | | #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */ |
9319 | | /* Add certificate data and empty extension to output up to the fragment size. |
9320 | | * |
9321 | | * ssl SSL/TLS object. |
9322 | | * cert The certificate data to write out. |
9323 | | * len The length of the certificate data. |
9324 | | * extSz Length of the extension data with the certificate. |
9325 | | * idx The start of the certificate data to write out. |
9326 | | * fragSz The maximum size of this fragment. |
9327 | | * output The buffer to write to. |
9328 | | * extIdx The index number of the extension data with the certificate |
9329 | | * returns the number of bytes written. |
9330 | | */ |
9331 | | static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz, |
9332 | | word32 idx, word32 fragSz, byte* output, word16 extIdx) |
9333 | 0 | { |
9334 | 0 | word32 i = 0; |
9335 | 0 | word32 copySz = min(len - idx, fragSz); |
9336 | |
|
9337 | 0 | if (idx < len) { |
9338 | 0 | XMEMCPY(output, cert + idx, copySz); |
9339 | 0 | i = copySz; |
9340 | 0 | if (copySz == fragSz) |
9341 | 0 | return i; |
9342 | 0 | } |
9343 | 0 | copySz = len + extSz - idx - i; |
9344 | |
|
9345 | 0 | if (extSz == OPAQUE16_LEN) { |
9346 | 0 | if (copySz <= fragSz) { |
9347 | | /* Empty extension */ |
9348 | 0 | output[i++] = 0; |
9349 | 0 | output[i++] = 0; |
9350 | 0 | } |
9351 | 0 | } |
9352 | 0 | else { |
9353 | 0 | byte* certExts = ssl->buffers.certExts[extIdx]->buffer + idx + i - len; |
9354 | | /* Put out as much of the extensions' data as will fit in fragment. */ |
9355 | 0 | if (copySz > fragSz - i) |
9356 | 0 | copySz = fragSz - i; |
9357 | 0 | XMEMCPY(output + i, certExts, copySz); |
9358 | 0 | i += copySz; |
9359 | 0 | } |
9360 | |
|
9361 | 0 | return i; |
9362 | 0 | } |
9363 | | |
9364 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9365 | | static int SetupOcspResp(WOLFSSL* ssl) |
9366 | | { |
9367 | | DecodedCert* cert = NULL; |
9368 | | CertificateStatusRequest* csr = NULL; |
9369 | | TLSX* extension = NULL; |
9370 | | int ret = 0; |
9371 | | OcspRequest* request = NULL; |
9372 | | |
9373 | | extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); |
9374 | | if (extension == NULL) |
9375 | | return 0; /* peer didn't signal ocsp support */ |
9376 | | csr = (CertificateStatusRequest*)extension->data; |
9377 | | if (csr == NULL) |
9378 | | return MEMORY_ERROR; |
9379 | | |
9380 | | if (SSL_CM(ssl) != NULL && |
9381 | | SSL_CM(ssl)->ocsp_stapling != NULL && |
9382 | | SSL_CM(ssl)->ocsp_stapling->statusCb != NULL) { |
9383 | | return TLSX_CSR_SetResponseWithStatusCB(ssl); |
9384 | | } |
9385 | | |
9386 | | if (ssl->buffers.certificate == NULL) { |
9387 | | WOLFSSL_MSG("Certificate buffer not set!"); |
9388 | | return BUFFER_ERROR; |
9389 | | } |
9390 | | cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap, |
9391 | | DYNAMIC_TYPE_DCERT); |
9392 | | if (cert == NULL) { |
9393 | | return MEMORY_E; |
9394 | | } |
9395 | | InitDecodedCert(cert, ssl->buffers.certificate->buffer, |
9396 | | ssl->buffers.certificate->length, ssl->heap); |
9397 | | ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, SSL_CM(ssl)); |
9398 | | if (ret != 0) { |
9399 | | FreeDecodedCert(cert); |
9400 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
9401 | | return ret; |
9402 | | } |
9403 | | ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap); |
9404 | | FreeDecodedCert(cert); |
9405 | | XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); |
9406 | | if (ret != 0 ) |
9407 | | return ret; |
9408 | | |
9409 | | request = &csr->request.ocsp[0]; |
9410 | | ret = CreateOcspResponse(ssl, &request, &csr->responses[0]); |
9411 | | if (request != &csr->request.ocsp[0] && |
9412 | | ssl->buffers.weOwnCert) { |
9413 | | /* request will be allocated in CreateOcspResponse() */ |
9414 | | FreeOcspRequest(request); |
9415 | | XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
9416 | | } |
9417 | | if (ret != 0) |
9418 | | return ret; |
9419 | | |
9420 | | if (csr->responses[0].buffer) |
9421 | | extension->resp = 1; |
9422 | | #if defined(WOLFSSL_TLS_OCSP_MULTI) |
9423 | | /* process OCSP request in certificate chain */ |
9424 | | if ((ret = ProcessChainOCSPRequest(ssl)) != 0) { |
9425 | | WOLFSSL_MSG("Process Cert Chain OCSP request failed"); |
9426 | | WOLFSSL_ERROR_VERBOSE(ret); |
9427 | | return ret; |
9428 | | } |
9429 | | #endif |
9430 | | return ret; |
9431 | | } |
9432 | | #endif |
9433 | | |
9434 | | /* handle generation TLS v1.3 certificate (11) */ |
9435 | | /* Send the certificate for this end and any CAs that help with validation. |
9436 | | * This message is always encrypted in TLS v1.3. |
9437 | | * |
9438 | | * ssl The SSL/TLS object. |
9439 | | * returns 0 on success, otherwise failure. |
9440 | | */ |
9441 | | static int SendTls13Certificate(WOLFSSL* ssl) |
9442 | 0 | { |
9443 | 0 | int ret = 0; |
9444 | 0 | word32 certSz, certChainSz, headerSz, listSz, payloadSz; |
9445 | 0 | word16 extSz[MAX_CERT_EXTENSIONS]; |
9446 | 0 | word16 extIdx = 0; |
9447 | 0 | word32 maxFragment; |
9448 | 0 | word32 totalextSz = 0; |
9449 | 0 | word32 len = 0; |
9450 | 0 | word32 idx = 0; |
9451 | 0 | word32 offset = OPAQUE16_LEN; |
9452 | 0 | byte* p = NULL; |
9453 | 0 | byte certReqCtxLen = 0; |
9454 | 0 | sword32 length; |
9455 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9456 | | byte* certReqCtx = NULL; |
9457 | | #endif |
9458 | |
|
9459 | | #ifdef OPENSSL_EXTRA |
9460 | | WOLFSSL_X509* x509 = NULL; |
9461 | | WOLFSSL_EVP_PKEY* pkey = NULL; |
9462 | | #endif |
9463 | |
|
9464 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND); |
9465 | 0 | WOLFSSL_ENTER("SendTls13Certificate"); |
9466 | |
|
9467 | 0 | XMEMSET(extSz, 0, sizeof(extSz)); |
9468 | |
|
9469 | 0 | ssl->options.buildingMsg = 1; |
9470 | |
|
9471 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9472 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9473 | | certReqCtxLen = ssl->certReqCtx->len; |
9474 | | certReqCtx = &ssl->certReqCtx->ctx; |
9475 | | } |
9476 | | #endif |
9477 | |
|
9478 | | #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_SETUP_CB) |
9479 | | /* call client cert callback if no cert has been loaded */ |
9480 | | if ((ssl->ctx->CBClientCert != NULL) && |
9481 | | (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) { |
9482 | | ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey); |
9483 | | if (ret == 1) { |
9484 | | if ((wolfSSL_CTX_use_certificate(ssl->ctx, x509) == WOLFSSL_SUCCESS) && |
9485 | | (wolfSSL_CTX_use_PrivateKey(ssl->ctx, pkey) == WOLFSSL_SUCCESS)) { |
9486 | | ssl->options.sendVerify = SEND_CERT; |
9487 | | } |
9488 | | wolfSSL_X509_free(x509); |
9489 | | x509 = NULL; |
9490 | | wolfSSL_EVP_PKEY_free(pkey); |
9491 | | } |
9492 | | } |
9493 | | #endif |
9494 | |
|
9495 | 0 | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9496 | 0 | certSz = 0; |
9497 | 0 | certChainSz = 0; |
9498 | 0 | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ; |
9499 | 0 | length = (sword32)headerSz; |
9500 | 0 | listSz = 0; |
9501 | 0 | } |
9502 | 0 | else { |
9503 | 0 | if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) { |
9504 | 0 | WOLFSSL_MSG("Send Cert missing certificate buffer"); |
9505 | 0 | return NO_CERT_ERROR; |
9506 | 0 | } |
9507 | | /* Certificate Data */ |
9508 | 0 | certSz = ssl->buffers.certificate->length; |
9509 | | /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */ |
9510 | 0 | headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ + |
9511 | 0 | CERT_HEADER_SZ; |
9512 | | /* set empty extension as default */ |
9513 | 0 | for (extIdx = 0; extIdx < (word16)XELEM_CNT(extSz); extIdx++) |
9514 | 0 | extSz[extIdx] = OPAQUE16_LEN; |
9515 | |
|
9516 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(NO_WOLFSSL_SERVER) |
9517 | | /* We only send CSR on the server side. On client side, the CSR data |
9518 | | * is populated with the server response. We would be sending the server |
9519 | | * its own stapling data. */ |
9520 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
9521 | | ret = SetupOcspResp(ssl); |
9522 | | if (ret != 0) |
9523 | | return ret; |
9524 | | |
9525 | | if ((1 + ssl->buffers.certChainCnt) > MAX_CERT_EXTENSIONS) |
9526 | | ret = MAX_CERT_EXTENSIONS_ERR; |
9527 | | if (ret == 0) |
9528 | | ret = WriteCSRToBuffer(ssl, &ssl->buffers.certExts[0], &extSz[0], |
9529 | | 1 /* +1 for leaf */ + (word16)ssl->buffers.certChainCnt); |
9530 | | if (ret < 0) |
9531 | | return ret; |
9532 | | totalextSz += ret; |
9533 | | ret = 0; /* Clear to signal no error */ |
9534 | | } |
9535 | | else |
9536 | | #endif |
9537 | 0 | { |
9538 | | /* Leaf cert empty extension size */ |
9539 | 0 | totalextSz += OPAQUE16_LEN; |
9540 | | /* chain cert empty extension size */ |
9541 | 0 | totalextSz += OPAQUE16_LEN * ssl->buffers.certChainCnt; |
9542 | 0 | } |
9543 | | |
9544 | | /* Length of message data with one certificate and extensions. */ |
9545 | 0 | length = (sword32)(headerSz + certSz + totalextSz); |
9546 | | /* Length of list data with one certificate and extensions. */ |
9547 | 0 | listSz = CERT_HEADER_SZ + certSz + totalextSz; |
9548 | | |
9549 | | /* Send rest of chain if sending cert (chain has leading size/s). */ |
9550 | 0 | if (certSz > 0 && ssl->buffers.certChainCnt > 0) { |
9551 | 0 | p = ssl->buffers.certChain->buffer; |
9552 | | /* Chain length including extensions. */ |
9553 | 0 | certChainSz = ssl->buffers.certChain->length; |
9554 | |
|
9555 | 0 | length += certChainSz; |
9556 | 0 | listSz += certChainSz; |
9557 | 0 | } |
9558 | 0 | else |
9559 | 0 | certChainSz = 0; |
9560 | 0 | } |
9561 | | |
9562 | 0 | payloadSz = (word32)length; |
9563 | |
|
9564 | 0 | if (ssl->fragOffset != 0) |
9565 | 0 | length -= (ssl->fragOffset + headerSz); |
9566 | |
|
9567 | 0 | maxFragment = (word32)wolfssl_local_GetMaxPlaintextSize(ssl); |
9568 | |
|
9569 | 0 | extIdx = 0; |
9570 | |
|
9571 | 0 | while (length > 0 && ret == 0) { |
9572 | 0 | byte* output = NULL; |
9573 | 0 | word32 fragSz = 0; |
9574 | 0 | word32 i = RECORD_HEADER_SZ; |
9575 | 0 | int sendSz = RECORD_HEADER_SZ; |
9576 | |
|
9577 | | #ifdef WOLFSSL_DTLS13 |
9578 | | if (ssl->options.dtls) { |
9579 | | i = Dtls13GetRlHeaderLength(ssl, 1); |
9580 | | sendSz = (int)i; |
9581 | | } |
9582 | | #endif /* WOLFSSL_DTLS13 */ |
9583 | |
|
9584 | 0 | if (ssl->fragOffset == 0) { |
9585 | 0 | if (headerSz + certSz + totalextSz + certChainSz <= |
9586 | 0 | maxFragment - HANDSHAKE_HEADER_SZ) { |
9587 | 0 | fragSz = headerSz + certSz + totalextSz + certChainSz; |
9588 | 0 | } |
9589 | | #ifdef WOLFSSL_DTLS13 |
9590 | | else if (ssl->options.dtls){ |
9591 | | /* short-circuit the fragmentation logic here. DTLS |
9592 | | fragmentation will be done in dtls13HandshakeSend() */ |
9593 | | fragSz = headerSz + certSz + totalextSz + certChainSz; |
9594 | | } |
9595 | | #endif /* WOLFSSL_DTLS13 */ |
9596 | 0 | else { |
9597 | 0 | fragSz = maxFragment - HANDSHAKE_HEADER_SZ; |
9598 | 0 | } |
9599 | |
|
9600 | 0 | sendSz += fragSz + HANDSHAKE_HEADER_SZ; |
9601 | 0 | i += HANDSHAKE_HEADER_SZ; |
9602 | | #ifdef WOLFSSL_DTLS13 |
9603 | | if (ssl->options.dtls) { |
9604 | | sendSz += DTLS_HANDSHAKE_EXTRA; |
9605 | | i += DTLS_HANDSHAKE_EXTRA; |
9606 | | } |
9607 | | #endif /* WOLFSSL_DTLS13 */ |
9608 | 0 | } |
9609 | 0 | else { |
9610 | 0 | fragSz = min((word32)length, maxFragment); |
9611 | 0 | sendSz += fragSz; |
9612 | 0 | } |
9613 | |
|
9614 | 0 | sendSz += MAX_MSG_EXTRA; |
9615 | | |
9616 | | /* Check buffers are big enough and grow if needed. */ |
9617 | 0 | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
9618 | 0 | return ret; |
9619 | | |
9620 | | /* Get position in output buffer to write new message to. */ |
9621 | 0 | output = GetOutputBuffer(ssl); |
9622 | |
|
9623 | 0 | if (ssl->fragOffset == 0) { |
9624 | 0 | AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl); |
9625 | | |
9626 | | /* Request context. */ |
9627 | 0 | output[i++] = certReqCtxLen; |
9628 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9629 | | if (certReqCtxLen > 0) { |
9630 | | XMEMCPY(output + i, certReqCtx, certReqCtxLen); |
9631 | | i += certReqCtxLen; |
9632 | | } |
9633 | | #endif |
9634 | 0 | length -= OPAQUE8_LEN + certReqCtxLen; |
9635 | 0 | fragSz -= OPAQUE8_LEN + certReqCtxLen; |
9636 | | /* Certificate list length. */ |
9637 | 0 | c32to24(listSz, output + i); |
9638 | 0 | i += CERT_HEADER_SZ; |
9639 | 0 | length -= CERT_HEADER_SZ; |
9640 | 0 | fragSz -= CERT_HEADER_SZ; |
9641 | | /* Leaf certificate data length. */ |
9642 | 0 | if (certSz > 0) { |
9643 | 0 | c32to24(certSz, output + i); |
9644 | 0 | i += CERT_HEADER_SZ; |
9645 | 0 | length -= CERT_HEADER_SZ; |
9646 | 0 | fragSz -= CERT_HEADER_SZ; |
9647 | 0 | } |
9648 | 0 | } |
9649 | 0 | else |
9650 | 0 | AddTls13RecordHeader(output, fragSz, handshake, ssl); |
9651 | |
|
9652 | 0 | if (extIdx == 0) { |
9653 | 0 | if (certSz > 0 && ssl->fragOffset < certSz + extSz[0]) { |
9654 | | /* Put in the leaf certificate with extensions. */ |
9655 | 0 | word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer, |
9656 | 0 | certSz, extSz[0], ssl->fragOffset, fragSz, |
9657 | 0 | output + i, 0); |
9658 | 0 | i += copySz; |
9659 | 0 | ssl->fragOffset += copySz; |
9660 | 0 | length -= copySz; |
9661 | 0 | fragSz -= copySz; |
9662 | 0 | if (ssl->fragOffset == certSz + extSz[0]) |
9663 | 0 | FreeDer(&ssl->buffers.certExts[0]); |
9664 | 0 | } |
9665 | 0 | } |
9666 | 0 | if (certChainSz > 0 && fragSz > 0) { |
9667 | | /* Put in the CA certificates with extensions. */ |
9668 | 0 | while (fragSz > 0) { |
9669 | 0 | word32 l; |
9670 | |
|
9671 | 0 | if (offset == len + OPAQUE16_LEN) { |
9672 | | /* Find next CA certificate to write out. */ |
9673 | 0 | offset = 0; |
9674 | | /* Point to the start of current cert in chain buffer. */ |
9675 | 0 | p = ssl->buffers.certChain->buffer + idx; |
9676 | 0 | len = NextCert(ssl->buffers.certChain->buffer, |
9677 | 0 | ssl->buffers.certChain->length, &idx); |
9678 | 0 | if (len == 0) |
9679 | 0 | break; |
9680 | | #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \ |
9681 | | !defined(NO_WOLFSSL_SERVER) |
9682 | | if (extIdx + 1 < MAX_CERT_EXTENSIONS) |
9683 | | extIdx++; |
9684 | | #endif |
9685 | 0 | } |
9686 | | /* Write out certificate and extension. */ |
9687 | 0 | l = AddCertExt(ssl, p, len, extSz[extIdx], offset, fragSz, |
9688 | 0 | output + i, extIdx); |
9689 | 0 | i += l; |
9690 | 0 | ssl->fragOffset += l; |
9691 | 0 | length -= l; |
9692 | 0 | fragSz -= l; |
9693 | 0 | offset += l; |
9694 | |
|
9695 | 0 | if (extIdx != 0 && extIdx < MAX_CERT_EXTENSIONS && |
9696 | 0 | ssl->buffers.certExts[extIdx] != NULL && |
9697 | 0 | offset == len + extSz[extIdx]) { |
9698 | 0 | FreeDer(&ssl->buffers.certExts[extIdx]); |
9699 | | /* for next chain cert */ |
9700 | 0 | len += extSz[extIdx] - OPAQUE16_LEN; |
9701 | 0 | } |
9702 | 0 | } |
9703 | 0 | } |
9704 | |
|
9705 | 0 | if ((int)i - RECORD_HEADER_SZ < 0) { |
9706 | 0 | WOLFSSL_MSG("Send Cert bad inputSz"); |
9707 | 0 | return BUFFER_E; |
9708 | 0 | } |
9709 | | |
9710 | | #ifdef WOLFSSL_DTLS13 |
9711 | | if (ssl->options.dtls) { |
9712 | | /* DTLS1.3 uses a separate variable and logic for fragments */ |
9713 | | ssl->options.buildingMsg = 0; |
9714 | | ssl->fragOffset = 0; |
9715 | | if ((word32)sendSz > WOLFSSL_MAX_16BIT || i > WOLFSSL_MAX_16BIT) { |
9716 | | WOLFSSL_MSG("Send Cert DTLS size exceeds word16"); |
9717 | | return BUFFER_E; |
9718 | | } |
9719 | | ret = Dtls13HandshakeSend(ssl, output, (word16)sendSz, (word16)i, |
9720 | | certificate, 1); |
9721 | | } |
9722 | | else |
9723 | | #endif /* WOLFSSL_DTLS13 */ |
9724 | 0 | { |
9725 | | /* This message is always encrypted. */ |
9726 | 0 | sendSz = BuildTls13Message(ssl, output, sendSz, |
9727 | 0 | output + RECORD_HEADER_SZ, (int)(i - RECORD_HEADER_SZ), |
9728 | 0 | handshake, 1, |
9729 | 0 | 0, 0); |
9730 | 0 | if (sendSz < 0) |
9731 | 0 | return sendSz; |
9732 | | |
9733 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
9734 | | if (ssl->hsInfoOn) |
9735 | | AddPacketName(ssl, "Certificate"); |
9736 | | if (ssl->toInfoOn) { |
9737 | | ret = AddPacketInfo(ssl, "Certificate", handshake, output, |
9738 | | sendSz, WRITE_PROTO, 0, ssl->heap); |
9739 | | if (ret != 0) |
9740 | | return ret; |
9741 | | } |
9742 | | #endif |
9743 | | |
9744 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
9745 | 0 | ssl->options.buildingMsg = 0; |
9746 | 0 | if (!ssl->options.groupMessages) |
9747 | 0 | ret = SendBuffered(ssl); |
9748 | 0 | } |
9749 | 0 | } |
9750 | | |
9751 | 0 | if (ret != WC_NO_ERR_TRACE(WANT_WRITE)) { |
9752 | | /* Clean up the fragment offset. */ |
9753 | 0 | ssl->options.buildingMsg = 0; |
9754 | 0 | ssl->fragOffset = 0; |
9755 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
9756 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
9757 | 0 | } |
9758 | |
|
9759 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
9760 | | if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) { |
9761 | | CertReqCtx* ctx = ssl->certReqCtx; |
9762 | | ssl->certReqCtx = ssl->certReqCtx->next; |
9763 | | XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
9764 | | } |
9765 | | #endif |
9766 | |
|
9767 | 0 | WOLFSSL_LEAVE("SendTls13Certificate", ret); |
9768 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND); |
9769 | |
|
9770 | 0 | return ret; |
9771 | 0 | } |
9772 | | |
9773 | | #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
9774 | | defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
9775 | | defined(HAVE_DILITHIUM)) && \ |
9776 | | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
9777 | | typedef struct Scv13Args { |
9778 | | byte* output; /* not allocated */ |
9779 | | byte* verify; /* not allocated */ |
9780 | | word32 idx; |
9781 | | word32 sigLen; |
9782 | | int sendSz; |
9783 | | word16 length; |
9784 | | |
9785 | | byte sigAlgo; |
9786 | | byte* sigData; |
9787 | | word16 sigDataSz; |
9788 | | #ifndef NO_RSA |
9789 | | byte* toSign; /* not allocated */ |
9790 | | word32 toSignSz; |
9791 | | #endif |
9792 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9793 | | byte altSigAlgo; |
9794 | | word32 altSigLen; /* Only used in the case of both native and alt. */ |
9795 | | byte* altSigData; |
9796 | | word16 altSigDataSz; |
9797 | | #endif |
9798 | | } Scv13Args; |
9799 | | |
9800 | | static void FreeScv13Args(WOLFSSL* ssl, void* pArgs) |
9801 | 0 | { |
9802 | 0 | Scv13Args* args = (Scv13Args*)pArgs; |
9803 | |
|
9804 | 0 | (void)ssl; |
9805 | |
|
9806 | 0 | if (args && args->sigData) { |
9807 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9808 | 0 | args->sigData = NULL; |
9809 | 0 | } |
9810 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9811 | | if (args && args->altSigData != NULL) { |
9812 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
9813 | | args->altSigData = NULL; |
9814 | | } |
9815 | | #endif |
9816 | 0 | } |
9817 | | |
9818 | | /* handle generation TLS v1.3 certificate_verify (15) */ |
9819 | | /* Send the TLS v1.3 CertificateVerify message. |
9820 | | * A hash of all the message so far is used. |
9821 | | * The signed data is: |
9822 | | * 0x20 * 64 | context string | 0x00 | hash of messages |
9823 | | * This message is always encrypted in TLS v1.3. |
9824 | | * |
9825 | | * ssl The SSL/TLS object. |
9826 | | * returns 0 on success, otherwise failure. |
9827 | | */ |
9828 | | static int SendTls13CertificateVerify(WOLFSSL* ssl) |
9829 | 0 | { |
9830 | 0 | int ret = 0; |
9831 | 0 | #ifndef NO_RSA |
9832 | | /* Use this as a temporary buffer for RSA signature verification. */ |
9833 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
9834 | 0 | #endif |
9835 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9836 | | Scv13Args* args = NULL; |
9837 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
9838 | | #else |
9839 | 0 | Scv13Args args[1]; |
9840 | 0 | #endif |
9841 | |
|
9842 | | #ifdef WOLFSSL_DTLS13 |
9843 | | int recordLayerHdrExtra; |
9844 | | #endif /* WOLFSSL_DTLS13 */ |
9845 | |
|
9846 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
9847 | 0 | WOLFSSL_ENTER("SendTls13CertificateVerify"); |
9848 | |
|
9849 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9850 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9851 | | #endif |
9852 | |
|
9853 | 0 | ssl->options.buildingMsg = 1; |
9854 | |
|
9855 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
9856 | | ret = tsip_Tls13SendCertVerify(ssl); |
9857 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
9858 | | goto exit_scv; |
9859 | | } |
9860 | | ret = 0; |
9861 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
9862 | |
|
9863 | | #ifdef WOLFSSL_DTLS13 |
9864 | | /* can be negative */ |
9865 | | if (ssl->options.dtls) |
9866 | | recordLayerHdrExtra = Dtls13GetRlHeaderLength(ssl, 1) - RECORD_HEADER_SZ; |
9867 | | else |
9868 | | recordLayerHdrExtra = 0; |
9869 | | |
9870 | | #endif /* WOLFSSL_DTLS13 */ |
9871 | |
|
9872 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9873 | | if (ssl->async == NULL) { |
9874 | | ssl->async = (struct WOLFSSL_ASYNC*) |
9875 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
9876 | | DYNAMIC_TYPE_ASYNC); |
9877 | | if (ssl->async == NULL) |
9878 | | ERROR_OUT(MEMORY_E, exit_scv); |
9879 | | } |
9880 | | args = (Scv13Args*)ssl->async->args; |
9881 | | |
9882 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
9883 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
9884 | | /* Check for error */ |
9885 | | if (ret < 0) |
9886 | | goto exit_scv; |
9887 | | } |
9888 | | else |
9889 | | #endif |
9890 | 0 | { |
9891 | | /* Reset state */ |
9892 | 0 | ret = 0; |
9893 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
9894 | 0 | XMEMSET(args, 0, sizeof(Scv13Args)); |
9895 | | #ifdef WOLFSSL_ASYNC_CRYPT |
9896 | | ssl->async->freeArgs = FreeScv13Args; |
9897 | | #endif |
9898 | 0 | } |
9899 | |
|
9900 | 0 | switch(ssl->options.asyncState) |
9901 | 0 | { |
9902 | 0 | case TLS_ASYNC_BEGIN: |
9903 | 0 | { |
9904 | 0 | if (ssl->options.sendVerify == SEND_BLANK_CERT) { |
9905 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9906 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, |
9907 | | ssl->buffers.keyMask); |
9908 | | #endif |
9909 | 0 | return 0; /* sent blank cert, can't verify */ |
9910 | 0 | } |
9911 | | |
9912 | 0 | args->sendSz = WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA; |
9913 | | /* Always encrypted. */ |
9914 | 0 | args->sendSz += MAX_MSG_EXTRA; |
9915 | | |
9916 | | /* check for available size */ |
9917 | 0 | if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) { |
9918 | 0 | goto exit_scv; |
9919 | 0 | } |
9920 | | |
9921 | | /* get output buffer */ |
9922 | 0 | args->output = GetOutputBuffer(ssl); |
9923 | | |
9924 | | /* Advance state and proceed */ |
9925 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
9926 | 0 | } /* case TLS_ASYNC_BEGIN */ |
9927 | 0 | FALL_THROUGH; |
9928 | |
|
9929 | 0 | case TLS_ASYNC_BUILD: |
9930 | 0 | { |
9931 | 0 | int rem = (int)(ssl->buffers.outputBuffer.bufferSize |
9932 | 0 | - ssl->buffers.outputBuffer.length |
9933 | 0 | - RECORD_HEADER_SZ - HANDSHAKE_HEADER_SZ); |
9934 | | |
9935 | | /* idx is used to track verify pointer offset to output */ |
9936 | 0 | args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
9937 | 0 | args->verify = |
9938 | 0 | &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ]; |
9939 | |
|
9940 | | #ifdef WOLFSSL_DTLS13 |
9941 | | if (ssl->options.dtls) { |
9942 | | rem -= recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9943 | | args->idx += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9944 | | args->verify += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
9945 | | } |
9946 | | #endif /* WOLFSSL_DTLS13 */ |
9947 | |
|
9948 | 0 | if (ssl->buffers.key == NULL) { |
9949 | | #ifdef HAVE_PK_CALLBACKS |
9950 | | if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) |
9951 | | args->sigLen = (word16)GetPrivateKeySigSize(ssl); |
9952 | | else |
9953 | | #endif |
9954 | 0 | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9955 | 0 | } |
9956 | 0 | else { |
9957 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
9958 | | if (ssl->sigSpec != NULL && |
9959 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
9960 | | /* In the case of alternative, we swap in the alt. */ |
9961 | | if (ssl->buffers.altKey == NULL) { |
9962 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9963 | | } |
9964 | | ssl->buffers.keyType = ssl->buffers.altKeyType; |
9965 | | ssl->buffers.keySz = ssl->buffers.altKeySz; |
9966 | | /* If we own it, free key before overriding it. */ |
9967 | | if (ssl->buffers.weOwnKey) { |
9968 | | FreeDer(&ssl->buffers.key); |
9969 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9970 | | FreeDer(&ssl->buffers.keyMask); |
9971 | | #endif |
9972 | | } |
9973 | | |
9974 | | /* Swap keys */ |
9975 | | ssl->buffers.key = ssl->buffers.altKey; |
9976 | | ssl->buffers.weOwnKey = ssl->buffers.weOwnAltKey; |
9977 | | |
9978 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
9979 | | ssl->buffers.keyMask = ssl->buffers.altKeyMask; |
9980 | | /* Unblind the alternative key before decoding */ |
9981 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
9982 | | #endif |
9983 | | } |
9984 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
9985 | 0 | ret = DecodePrivateKey(ssl, &args->sigLen); |
9986 | 0 | if (ret != 0) |
9987 | 0 | goto exit_scv; |
9988 | 0 | } |
9989 | | |
9990 | 0 | if (rem < 0 || (int)args->sigLen > rem) { |
9991 | 0 | ERROR_OUT(BUFFER_E, exit_scv); |
9992 | 0 | } |
9993 | | |
9994 | 0 | if (args->sigLen == 0) { |
9995 | 0 | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
9996 | 0 | } |
9997 | | |
9998 | | /* Add signature algorithm. */ |
9999 | 0 | if (ssl->hsType == DYNAMIC_TYPE_RSA) |
10000 | 0 | args->sigAlgo = rsa_pss_sa_algo; |
10001 | 0 | #ifdef HAVE_ECC |
10002 | 0 | else if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10003 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10004 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10005 | | args->sigAlgo = sm2_sa_algo; |
10006 | | } |
10007 | | else |
10008 | | #endif |
10009 | 0 | { |
10010 | 0 | args->sigAlgo = ecc_dsa_sa_algo; |
10011 | 0 | } |
10012 | 0 | } |
10013 | 0 | #endif |
10014 | | #ifdef HAVE_ED25519 |
10015 | | else if (ssl->hsType == DYNAMIC_TYPE_ED25519) |
10016 | | args->sigAlgo = ed25519_sa_algo; |
10017 | | #endif |
10018 | | #ifdef HAVE_ED448 |
10019 | | else if (ssl->hsType == DYNAMIC_TYPE_ED448) |
10020 | | args->sigAlgo = ed448_sa_algo; |
10021 | | #endif |
10022 | | #if defined(HAVE_FALCON) |
10023 | | else if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10024 | | args->sigAlgo = ssl->buffers.keyType; |
10025 | | } |
10026 | | #endif /* HAVE_FALCON */ |
10027 | | #if defined(HAVE_DILITHIUM) |
10028 | | else if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
10029 | | args->sigAlgo = ssl->buffers.keyType; |
10030 | | } |
10031 | | #endif /* HAVE_DILITHIUM */ |
10032 | 0 | else { |
10033 | 0 | ERROR_OUT(ALGO_ID_E, exit_scv); |
10034 | 0 | } |
10035 | | |
10036 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10037 | | if (ssl->peerSigSpec == NULL) { |
10038 | | /* The peer did not respond. We didn't send CKS or they don't |
10039 | | * support it. Either way, we do not need to handle dual |
10040 | | * key/sig case. */ |
10041 | | ssl->sigSpec = NULL; |
10042 | | ssl->sigSpecSz = 0; |
10043 | | } |
10044 | | |
10045 | | if (ssl->sigSpec != NULL && |
10046 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10047 | | /* The native was already decoded. Now we need to do the |
10048 | | * alternative. Note that no swap was done because this case is |
10049 | | * both native and alternative, not just alternative. */ |
10050 | | if (ssl->buffers.altKey == NULL) { |
10051 | | ERROR_OUT(NO_PRIVATE_KEY, exit_scv); |
10052 | | } |
10053 | | |
10054 | | /* After this call, args->altSigLen has the length we need for |
10055 | | * the alternative signature. */ |
10056 | | ret = DecodeAltPrivateKey(ssl, &args->altSigLen); |
10057 | | if (ret != 0) |
10058 | | goto exit_scv; |
10059 | | |
10060 | | if (ssl->buffers.altKeyType == ecc_dsa_sa_algo || |
10061 | | ssl->buffers.altKeyType == falcon_level1_sa_algo || |
10062 | | ssl->buffers.altKeyType == falcon_level5_sa_algo || |
10063 | | ssl->buffers.altKeyType == dilithium_level2_sa_algo || |
10064 | | ssl->buffers.altKeyType == dilithium_level3_sa_algo || |
10065 | | ssl->buffers.altKeyType == dilithium_level5_sa_algo) { |
10066 | | args->altSigAlgo = ssl->buffers.altKeyType; |
10067 | | } |
10068 | | else if (ssl->buffers.altKeyType == rsa_sa_algo && |
10069 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10070 | | args->altSigAlgo = rsa_pss_sa_algo; |
10071 | | } |
10072 | | else { |
10073 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10074 | | } |
10075 | | |
10076 | | EncodeDualSigAlg(args->sigAlgo, args->altSigAlgo, args->verify); |
10077 | | if (args->verify[0] == 0) { |
10078 | | ERROR_OUT(ALGO_ID_E, exit_scv); |
10079 | | } |
10080 | | } |
10081 | | else |
10082 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10083 | 0 | EncodeSigAlg(ssl, ssl->options.hashAlgo, args->sigAlgo, |
10084 | 0 | args->verify); |
10085 | |
|
10086 | 0 | if (args->sigData == NULL) { |
10087 | 0 | word32 sigLen = MAX_SIG_DATA_SZ; |
10088 | 0 | if ((ssl->hsType == DYNAMIC_TYPE_RSA) && |
10089 | 0 | (args->sigLen > MAX_SIG_DATA_SZ)) { |
10090 | | /* We store the RSA signature in the sigData buffer |
10091 | | * temporarily, hence its size must be fitting. */ |
10092 | 0 | sigLen = args->sigLen; |
10093 | 0 | } |
10094 | 0 | args->sigData = (byte*)XMALLOC(sigLen, ssl->heap, |
10095 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10096 | 0 | if (args->sigData == NULL) { |
10097 | 0 | ERROR_OUT(MEMORY_E, exit_scv); |
10098 | 0 | } |
10099 | 0 | } |
10100 | | |
10101 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10102 | | if ((ssl->sigSpec != NULL) && |
10103 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
10104 | | (args->altSigData == NULL)) { |
10105 | | word32 sigLen = MAX_SIG_DATA_SZ; |
10106 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA && |
10107 | | args->altSigLen > MAX_SIG_DATA_SZ) { |
10108 | | /* We store the RSA signature in the sigData buffer |
10109 | | * temporarily, hence its size must be fitting. */ |
10110 | | sigLen = args->altSigLen; |
10111 | | } |
10112 | | args->altSigData = (byte*)XMALLOC(sigLen, ssl->heap, |
10113 | | DYNAMIC_TYPE_SIGNATURE); |
10114 | | if (args->altSigData == NULL) { |
10115 | | ERROR_OUT(MEMORY_E, exit_scv); |
10116 | | } |
10117 | | } |
10118 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10119 | | |
10120 | | /* Create the data to be signed. */ |
10121 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0); |
10122 | 0 | if (ret != 0) |
10123 | 0 | goto exit_scv; |
10124 | | |
10125 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10126 | | if ((ssl->sigSpec != NULL) && |
10127 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
10128 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
10129 | | args->altSigDataSz = args->sigDataSz; |
10130 | | } |
10131 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10132 | | |
10133 | 0 | #ifndef NO_RSA |
10134 | 0 | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
10135 | | /* build encoded signature buffer */ |
10136 | 0 | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
10137 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, ssl->heap, |
10138 | 0 | DYNAMIC_TYPE_SIGNATURE); |
10139 | 0 | if (rsaSigBuf->buffer == NULL) { |
10140 | 0 | ERROR_OUT(MEMORY_E, exit_scv); |
10141 | 0 | } |
10142 | | |
10143 | 0 | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, args->sigData, |
10144 | 0 | args->sigDataSz, args->sigAlgo, ssl->options.hashAlgo); |
10145 | 0 | if (ret < 0) |
10146 | 0 | goto exit_scv; |
10147 | 0 | rsaSigBuf->length = (unsigned int)ret; |
10148 | 0 | ret = 0; |
10149 | 0 | } |
10150 | 0 | #endif /* !NO_RSA */ |
10151 | 0 | #ifdef HAVE_ECC |
10152 | 0 | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10153 | 0 | args->sigLen = (word32)args->sendSz - args->idx - |
10154 | 0 | HASH_SIG_SIZE - |
10155 | 0 | VERIFY_HEADER; |
10156 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10157 | | if (ssl->buffers.keyType != sm2_sa_algo) |
10158 | | #endif |
10159 | 0 | { |
10160 | 0 | ret = CreateECCEncodedSig(args->sigData, |
10161 | 0 | args->sigDataSz, ssl->options.hashAlgo); |
10162 | 0 | if (ret < 0) |
10163 | 0 | goto exit_scv; |
10164 | 0 | args->sigDataSz = (word16)ret; |
10165 | 0 | ret = 0; |
10166 | 0 | } |
10167 | 0 | } |
10168 | 0 | #endif /* HAVE_ECC */ |
10169 | | #ifdef HAVE_ED25519 |
10170 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
10171 | | ret = Ed25519CheckPubKey(ssl); |
10172 | | if (ret < 0) { |
10173 | | ERROR_OUT(ret, exit_scv); |
10174 | | } |
10175 | | args->sigLen = ED25519_SIG_SIZE; |
10176 | | } |
10177 | | #endif /* HAVE_ED25519 */ |
10178 | | #ifdef HAVE_ED448 |
10179 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
10180 | | ret = Ed448CheckPubKey(ssl); |
10181 | | if (ret < 0) { |
10182 | | ERROR_OUT(ret, exit_scv); |
10183 | | } |
10184 | | args->sigLen = ED448_SIG_SIZE; |
10185 | | } |
10186 | | |
10187 | | #endif /* HAVE_ED448 */ |
10188 | | #if defined(HAVE_FALCON) |
10189 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10190 | | args->sigLen = FALCON_MAX_SIG_SIZE; |
10191 | | } |
10192 | | #endif /* HAVE_FALCON */ |
10193 | | #if defined(HAVE_DILITHIUM) |
10194 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
10195 | | args->sigLen = DILITHIUM_MAX_SIG_SIZE; |
10196 | | } |
10197 | | #endif /* HAVE_DILITHIUM */ |
10198 | | |
10199 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10200 | | if (ssl->sigSpec != NULL && |
10201 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10202 | | |
10203 | | #ifndef NO_RSA |
10204 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10205 | | /* build encoded signature buffer */ |
10206 | | XFREE(rsaSigBuf->buffer, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10207 | | rsaSigBuf->length = WC_MAX_DIGEST_SIZE; |
10208 | | rsaSigBuf->buffer = (byte*)XMALLOC(rsaSigBuf->length, |
10209 | | ssl->heap, |
10210 | | DYNAMIC_TYPE_SIGNATURE); |
10211 | | if (rsaSigBuf->buffer == NULL) { |
10212 | | ERROR_OUT(MEMORY_E, exit_scv); |
10213 | | } |
10214 | | |
10215 | | ret = CreateRSAEncodedSig(rsaSigBuf->buffer, |
10216 | | args->altSigData, args->altSigDataSz, |
10217 | | args->altSigAlgo, ssl->options.hashAlgo); |
10218 | | if (ret < 0) |
10219 | | goto exit_scv; |
10220 | | rsaSigBuf->length = ret; |
10221 | | ret = 0; |
10222 | | } |
10223 | | #endif /* !NO_RSA */ |
10224 | | #ifdef HAVE_ECC |
10225 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
10226 | | ret = CreateECCEncodedSig(args->altSigData, |
10227 | | args->altSigDataSz, ssl->options.hashAlgo); |
10228 | | if (ret < 0) |
10229 | | goto exit_scv; |
10230 | | args->altSigDataSz = (word16)ret; |
10231 | | ret = 0; |
10232 | | } |
10233 | | #endif /* HAVE_ECC */ |
10234 | | } |
10235 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10236 | | |
10237 | | /* Advance state and proceed */ |
10238 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
10239 | 0 | } /* case TLS_ASYNC_BUILD */ |
10240 | 0 | FALL_THROUGH; |
10241 | |
|
10242 | 0 | case TLS_ASYNC_DO: |
10243 | 0 | { |
10244 | 0 | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
10245 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10246 | | if (ssl->sigSpec != NULL && |
10247 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10248 | | /* As we have two signatures in the message, we store |
10249 | | * the length of each before the actual signature. This |
10250 | | * is necessary, as we could have two algorithms with |
10251 | | * variable length signatures. */ |
10252 | | sigOut += OPAQUE16_LEN; |
10253 | | } |
10254 | | #endif |
10255 | 0 | #ifdef HAVE_ECC |
10256 | 0 | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10257 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10258 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10259 | | ret = Sm2wSm3Sign(ssl, TLS13_SM2_SIG_ID, |
10260 | | TLS13_SM2_SIG_ID_SZ, args->sigData, args->sigDataSz, |
10261 | | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, NULL); |
10262 | | } |
10263 | | else |
10264 | | #endif |
10265 | 0 | { |
10266 | 0 | ret = EccSign(ssl, args->sigData, args->sigDataSz, |
10267 | 0 | sigOut, &args->sigLen, (ecc_key*)ssl->hsKey, |
10268 | | #ifdef HAVE_PK_CALLBACKS |
10269 | | ssl->buffers.key |
10270 | | #else |
10271 | 0 | NULL |
10272 | 0 | #endif |
10273 | 0 | ); |
10274 | 0 | } |
10275 | 0 | args->length = (word16)args->sigLen; |
10276 | 0 | } |
10277 | 0 | #endif /* HAVE_ECC */ |
10278 | | #ifdef HAVE_ED25519 |
10279 | | if (ssl->hsType == DYNAMIC_TYPE_ED25519) { |
10280 | | ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz, |
10281 | | sigOut, &args->sigLen, (ed25519_key*)ssl->hsKey, |
10282 | | #ifdef HAVE_PK_CALLBACKS |
10283 | | ssl->buffers.key |
10284 | | #else |
10285 | | NULL |
10286 | | #endif |
10287 | | ); |
10288 | | args->length = (word16)args->sigLen; |
10289 | | } |
10290 | | #endif |
10291 | | #ifdef HAVE_ED448 |
10292 | | if (ssl->hsType == DYNAMIC_TYPE_ED448) { |
10293 | | ret = Ed448Sign(ssl, args->sigData, args->sigDataSz, |
10294 | | sigOut, &args->sigLen, (ed448_key*)ssl->hsKey, |
10295 | | #ifdef HAVE_PK_CALLBACKS |
10296 | | ssl->buffers.key |
10297 | | #else |
10298 | | NULL |
10299 | | #endif |
10300 | | ); |
10301 | | args->length = (word16)args->sigLen; |
10302 | | } |
10303 | | #endif |
10304 | | #if defined(HAVE_FALCON) |
10305 | | if (ssl->hsType == DYNAMIC_TYPE_FALCON) { |
10306 | | ret = wc_falcon_sign_msg(args->sigData, args->sigDataSz, |
10307 | | sigOut, &args->sigLen, |
10308 | | (falcon_key*)ssl->hsKey, ssl->rng); |
10309 | | args->length = (word16)args->sigLen; |
10310 | | } |
10311 | | #endif /* HAVE_FALCON */ |
10312 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
10313 | | if (ssl->hsType == DYNAMIC_TYPE_DILITHIUM) { |
10314 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->sigData, |
10315 | | args->sigDataSz, sigOut, |
10316 | | &args->sigLen, |
10317 | | (dilithium_key*)ssl->hsKey, |
10318 | | ssl->rng); |
10319 | | args->length = (word16)args->sigLen; |
10320 | | } |
10321 | | #endif /* HAVE_DILITHIUM */ |
10322 | 0 | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
10323 | 0 | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
10324 | 0 | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
10325 | 0 | args->toSign = rsaSigBuf->buffer; |
10326 | 0 | args->toSignSz = (word32)rsaSigBuf->length; |
10327 | | #if defined(HAVE_PK_CALLBACKS) && \ |
10328 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
10329 | | /* Pass full data to sign (args->sigData), not hash of */ |
10330 | | if (ssl->ctx->RsaPssSignCb) { |
10331 | | args->toSign = args->sigData; |
10332 | | args->toSignSz = args->sigDataSz; |
10333 | | } |
10334 | | #endif |
10335 | 0 | ret = RsaSign(ssl, (const byte*)args->toSign, args->toSignSz, |
10336 | 0 | sigOut, &args->sigLen, args->sigAlgo, |
10337 | 0 | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
10338 | 0 | ssl->buffers.key); |
10339 | 0 | if (ret == 0) { |
10340 | 0 | args->length = (word16)args->sigLen; |
10341 | 0 | XMEMCPY(args->sigData, sigOut, args->sigLen); |
10342 | 0 | } |
10343 | 0 | } |
10344 | 0 | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
10345 | | |
10346 | | /* Check for error */ |
10347 | 0 | if (ret != 0) { |
10348 | 0 | goto exit_scv; |
10349 | 0 | } |
10350 | | |
10351 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10352 | | if (ssl->sigSpec != NULL && |
10353 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10354 | | /* Add signature length for the first signature. */ |
10355 | | c16toa((word16)args->sigLen, sigOut - OPAQUE16_LEN); |
10356 | | args->length += OPAQUE16_LEN; |
10357 | | |
10358 | | /* Advance our pointer to where we store the alt signature. |
10359 | | * We also add additional space for the length field of the |
10360 | | * second signature. */ |
10361 | | sigOut += args->sigLen + OPAQUE16_LEN; |
10362 | | |
10363 | | /* Generate the alternative signature */ |
10364 | | #ifdef HAVE_ECC |
10365 | | if (ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
10366 | | ret = EccSign(ssl, args->altSigData, args->altSigDataSz, |
10367 | | sigOut, &args->altSigLen, |
10368 | | (ecc_key*)ssl->hsAltKey, |
10369 | | #ifdef HAVE_PK_CALLBACKS |
10370 | | ssl->buffers.altKey |
10371 | | #else |
10372 | | NULL |
10373 | | #endif |
10374 | | ); |
10375 | | } |
10376 | | #endif /* HAVE_ECC */ |
10377 | | #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ |
10378 | | !defined(WOLFSSL_RSA_VERIFY_ONLY) |
10379 | | if (ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10380 | | args->toSign = rsaSigBuf->buffer; |
10381 | | args->toSignSz = (word32)rsaSigBuf->length; |
10382 | | #if defined(HAVE_PK_CALLBACKS) && \ |
10383 | | defined(TLS13_RSA_PSS_SIGN_CB_NO_PREHASH) |
10384 | | /* Pass full data to sign (args->altSigData), not hash of */ |
10385 | | if (ssl->ctx->RsaPssSignCb) { |
10386 | | args->toSign = args->altSigData; |
10387 | | args->toSignSz = (word32)args->altSigDataSz; |
10388 | | } |
10389 | | #endif |
10390 | | ret = RsaSign(ssl, (const byte*)args->toSign, |
10391 | | args->toSignSz, sigOut, &args->altSigLen, |
10392 | | args->altSigAlgo, ssl->options.hashAlgo, |
10393 | | (RsaKey*)ssl->hsAltKey, |
10394 | | ssl->buffers.altKey); |
10395 | | |
10396 | | if (ret == 0) { |
10397 | | XMEMCPY(args->altSigData, sigOut, args->altSigLen); |
10398 | | } |
10399 | | } |
10400 | | #endif /* !NO_RSA && !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */ |
10401 | | #if defined(HAVE_FALCON) |
10402 | | if (ssl->hsAltType == DYNAMIC_TYPE_FALCON) { |
10403 | | ret = wc_falcon_sign_msg(args->altSigData, |
10404 | | args->altSigDataSz, sigOut, |
10405 | | &args->altSigLen, |
10406 | | (falcon_key*)ssl->hsAltKey, |
10407 | | ssl->rng); |
10408 | | } |
10409 | | #endif /* HAVE_FALCON */ |
10410 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_SIGN) |
10411 | | if (ssl->hsAltType == DYNAMIC_TYPE_DILITHIUM) { |
10412 | | ret = wc_dilithium_sign_ctx_msg(NULL, 0, args->altSigData, |
10413 | | args->altSigDataSz, sigOut, &args->altSigLen, |
10414 | | (dilithium_key*)ssl->hsAltKey, ssl->rng); |
10415 | | } |
10416 | | #endif /* HAVE_DILITHIUM */ |
10417 | | |
10418 | | /* Check for error */ |
10419 | | if (ret != 0) { |
10420 | | goto exit_scv; |
10421 | | } |
10422 | | |
10423 | | /* Add signature length for the alternative signature. */ |
10424 | | c16toa((word16)args->altSigLen, sigOut - OPAQUE16_LEN); |
10425 | | |
10426 | | /* Add length of the alt sig to the total length */ |
10427 | | args->length += args->altSigLen + OPAQUE16_LEN; |
10428 | | } |
10429 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10430 | | |
10431 | | /* Add signature length. */ |
10432 | 0 | c16toa(args->length, args->verify + HASH_SIG_SIZE); |
10433 | | |
10434 | | /* Advance state and proceed */ |
10435 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
10436 | 0 | } /* case TLS_ASYNC_DO */ |
10437 | 0 | FALL_THROUGH; |
10438 | |
|
10439 | 0 | case TLS_ASYNC_VERIFY: |
10440 | 0 | { |
10441 | 0 | #ifndef NO_RSA |
10442 | 0 | if (ssl->hsType == DYNAMIC_TYPE_RSA) { |
10443 | | /* check for signature faults */ |
10444 | 0 | ret = VerifyRsaSign(ssl, args->sigData, args->sigLen, |
10445 | 0 | rsaSigBuf->buffer, (word32)rsaSigBuf->length, args->sigAlgo, |
10446 | 0 | ssl->options.hashAlgo, (RsaKey*)ssl->hsKey, |
10447 | 0 | ssl->buffers.key); |
10448 | 0 | } |
10449 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10450 | | if (ssl->sigSpec != NULL && |
10451 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10452 | | ssl->hsAltType == DYNAMIC_TYPE_RSA) { |
10453 | | /* check for signature faults */ |
10454 | | ret = VerifyRsaSign(ssl, args->altSigData, args->altSigLen, |
10455 | | rsaSigBuf->buffer, (word32)rsaSigBuf->length, |
10456 | | args->altSigAlgo, ssl->options.hashAlgo, |
10457 | | (RsaKey*)ssl->hsAltKey, ssl->buffers.altKey); |
10458 | | } |
10459 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10460 | 0 | #endif /* !NO_RSA */ |
10461 | | #if defined(HAVE_ECC) && defined(WOLFSSL_CHECK_SIG_FAULTS) |
10462 | | if (ssl->hsType == DYNAMIC_TYPE_ECC) { |
10463 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER; |
10464 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10465 | | if (ssl->sigSpec != NULL && |
10466 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
10467 | | /* Add our length offset. */ |
10468 | | sigOut += OPAQUE16_LEN; |
10469 | | } |
10470 | | #endif |
10471 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
10472 | | if (ssl->buffers.keyType == sm2_sa_algo) { |
10473 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, |
10474 | | TLS13_SM2_SIG_ID_SZ, |
10475 | | sigOut, args->sigLen, args->sigData, args->sigDataSz, |
10476 | | (ecc_key*)ssl->hsKey, NULL); |
10477 | | } |
10478 | | else |
10479 | | #endif |
10480 | | { |
10481 | | #ifdef HAVE_PK_CALLBACKS |
10482 | | buffer tmp; |
10483 | | |
10484 | | tmp.length = ssl->buffers.key->length; |
10485 | | tmp.buffer = ssl->buffers.key->buffer; |
10486 | | #endif |
10487 | | ret = EccVerify(ssl, sigOut, args->sigLen, |
10488 | | args->sigData, args->sigDataSz, |
10489 | | (ecc_key*)ssl->hsKey, |
10490 | | #ifdef HAVE_PK_CALLBACKS |
10491 | | &tmp |
10492 | | #else |
10493 | | NULL |
10494 | | #endif |
10495 | | ); |
10496 | | } |
10497 | | } |
10498 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10499 | | if (ssl->sigSpec != NULL && |
10500 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
10501 | | ssl->hsAltType == DYNAMIC_TYPE_ECC) { |
10502 | | /* check for signature faults */ |
10503 | | byte* sigOut = args->verify + HASH_SIG_SIZE + VERIFY_HEADER + |
10504 | | args->sigLen + OPAQUE16_LEN + OPAQUE16_LEN; |
10505 | | #ifdef HAVE_PK_CALLBACKS |
10506 | | buffer tmp; |
10507 | | tmp.length = ssl->buffers.altKey->length; |
10508 | | tmp.buffer = ssl->buffers.altKey->buffer; |
10509 | | #endif |
10510 | | ret = EccVerify(ssl, sigOut, args->altSigLen, |
10511 | | args->altSigData, args->altSigDataSz, |
10512 | | (ecc_key*)ssl->hsAltKey, |
10513 | | #ifdef HAVE_PK_CALLBACKS |
10514 | | &tmp |
10515 | | #else |
10516 | | NULL |
10517 | | #endif |
10518 | | ); |
10519 | | } |
10520 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10521 | | #endif /* HAVE_ECC && WOLFSSL_CHECK_SIG_FAULTS */ |
10522 | | |
10523 | | /* Check for error */ |
10524 | 0 | if (ret != 0) { |
10525 | 0 | goto exit_scv; |
10526 | 0 | } |
10527 | | |
10528 | | /* Advance state and proceed */ |
10529 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
10530 | 0 | } /* case TLS_ASYNC_VERIFY */ |
10531 | 0 | FALL_THROUGH; |
10532 | |
|
10533 | 0 | case TLS_ASYNC_FINALIZE: |
10534 | 0 | { |
10535 | | /* Put the record and handshake headers on. */ |
10536 | 0 | AddTls13Headers(args->output, args->length + HASH_SIG_SIZE + |
10537 | 0 | VERIFY_HEADER, certificate_verify, ssl); |
10538 | |
|
10539 | 0 | args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + |
10540 | 0 | args->length + HASH_SIG_SIZE + VERIFY_HEADER; |
10541 | | #ifdef WOLFSSL_DTLS13 |
10542 | | if (ssl->options.dtls) |
10543 | | args->sendSz += recordLayerHdrExtra + DTLS_HANDSHAKE_EXTRA; |
10544 | | |
10545 | | #endif /* WOLFSSL_DTLS13 */ |
10546 | | /* Advance state and proceed */ |
10547 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
10548 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
10549 | 0 | FALL_THROUGH; |
10550 | |
|
10551 | 0 | case TLS_ASYNC_END: |
10552 | 0 | { |
10553 | | #ifdef WOLFSSL_DTLS13 |
10554 | | if (ssl->options.dtls) { |
10555 | | ssl->options.buildingMsg = 0; |
10556 | | ret = Dtls13HandshakeSend(ssl, args->output, |
10557 | | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA + MAX_MSG_EXTRA, |
10558 | | (word16)args->sendSz, certificate_verify, 1); |
10559 | | if (ret != 0) |
10560 | | goto exit_scv; |
10561 | | |
10562 | | break; |
10563 | | } |
10564 | | #endif /* WOLFSSL_DTLS13 */ |
10565 | | |
10566 | | /* This message is always encrypted. */ |
10567 | 0 | ret = BuildTls13Message(ssl, args->output, |
10568 | 0 | WC_MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA, |
10569 | 0 | args->output + RECORD_HEADER_SZ, |
10570 | 0 | args->sendSz - RECORD_HEADER_SZ, handshake, |
10571 | 0 | 1, 0, 0); |
10572 | |
|
10573 | 0 | if (ret < 0) { |
10574 | 0 | goto exit_scv; |
10575 | 0 | } |
10576 | 0 | else { |
10577 | 0 | args->sendSz = ret; |
10578 | 0 | ret = 0; |
10579 | 0 | } |
10580 | | |
10581 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
10582 | | if (ssl->hsInfoOn) |
10583 | | AddPacketName(ssl, "CertificateVerify"); |
10584 | | if (ssl->toInfoOn) { |
10585 | | ret = AddPacketInfo(ssl, "CertificateVerify", handshake, |
10586 | | args->output, args->sendSz, WRITE_PROTO, 0, |
10587 | | ssl->heap); |
10588 | | if (ret != 0) |
10589 | | goto exit_scv; |
10590 | | } |
10591 | | #endif |
10592 | | |
10593 | 0 | ssl->buffers.outputBuffer.length += (word32)args->sendSz; |
10594 | 0 | ssl->options.buildingMsg = 0; |
10595 | 0 | if (!ssl->options.groupMessages) |
10596 | 0 | ret = SendBuffered(ssl); |
10597 | 0 | break; |
10598 | 0 | } |
10599 | 0 | default: |
10600 | 0 | ret = INPUT_CASE_ERROR; |
10601 | 0 | } /* switch(ssl->options.asyncState) */ |
10602 | | |
10603 | 0 | exit_scv: |
10604 | | #ifdef WOLFSSL_BLIND_PRIVATE_KEY |
10605 | | if (ret == 0) { |
10606 | | ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key, |
10607 | | &ssl->buffers.keyMask); |
10608 | | } |
10609 | | else { |
10610 | | wolfssl_priv_der_blind_toggle(ssl->buffers.key, ssl->buffers.keyMask); |
10611 | | } |
10612 | | #endif |
10613 | |
|
10614 | 0 | WOLFSSL_LEAVE("SendTls13CertificateVerify", ret); |
10615 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND); |
10616 | |
|
10617 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10618 | | /* Handle async operation */ |
10619 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
10620 | | return ret; |
10621 | | } |
10622 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
10623 | | |
10624 | | /* Final cleanup */ |
10625 | 0 | FreeScv13Args(ssl, args); |
10626 | 0 | FreeKeyExchange(ssl); |
10627 | 0 | #ifdef WOLFSSL_ASYNC_IO |
10628 | | /* Cleanup async */ |
10629 | 0 | FreeAsyncCtx(ssl, 0); |
10630 | 0 | #endif |
10631 | |
|
10632 | 0 | if (ret != 0) { |
10633 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
10634 | 0 | } |
10635 | |
|
10636 | 0 | return ret; |
10637 | 0 | } |
10638 | | #endif |
10639 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
10640 | | |
10641 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) |
10642 | | /* handle processing TLS v1.3 certificate (11) */ |
10643 | | /* Parse and handle a TLS v1.3 Certificate message. |
10644 | | * |
10645 | | * ssl The SSL/TLS object. |
10646 | | * input The message buffer. |
10647 | | * inOutIdx On entry, the index into the message buffer of Certificate. |
10648 | | * On exit, the index of byte after the Certificate message. |
10649 | | * totalSz The length of the current handshake message. |
10650 | | * returns 0 on success and otherwise failure. |
10651 | | */ |
10652 | | static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
10653 | | word32 totalSz) |
10654 | 0 | { |
10655 | 0 | int ret = 0; |
10656 | |
|
10657 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_DO); |
10658 | 0 | WOLFSSL_ENTER("DoTls13Certificate"); |
10659 | |
|
10660 | | #ifdef WOLFSSL_DTLS13 |
10661 | | if (ssl->options.dtls && ssl->options.handShakeDone) { |
10662 | | /* certificate needs some special care after the handshake */ |
10663 | | ret = Dtls13RtxProcessingCertificate( |
10664 | | ssl, input + *inOutIdx, totalSz); |
10665 | | } |
10666 | | #endif /* WOLFSSL_DTLS13 */ |
10667 | |
|
10668 | 0 | if (ret == 0) |
10669 | 0 | ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz); |
10670 | 0 | if (ret == 0) { |
10671 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
10672 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
10673 | 0 | ssl->options.serverState = SERVER_CERT_COMPLETE; |
10674 | 0 | #endif |
10675 | | #if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
10676 | | if (ssl->options.side == WOLFSSL_SERVER_END && |
10677 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
10678 | | /* reset handshake states */ |
10679 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
10680 | | ssl->options.acceptState = TICKET_SENT; |
10681 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
10682 | | } |
10683 | | #endif |
10684 | 0 | } |
10685 | |
|
10686 | 0 | WOLFSSL_LEAVE("DoTls13Certificate", ret); |
10687 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_DO); |
10688 | |
|
10689 | 0 | return ret; |
10690 | 0 | } |
10691 | | #endif |
10692 | | |
10693 | | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
10694 | | defined(HAVE_ED448) |
10695 | | |
10696 | | typedef struct Dcv13Args { |
10697 | | byte* output; /* not allocated */ |
10698 | | word32 sendSz; |
10699 | | word16 sz; |
10700 | | word32 sigSz; |
10701 | | word32 idx; |
10702 | | word32 begin; |
10703 | | |
10704 | | byte* sigData; |
10705 | | word16 sigDataSz; |
10706 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10707 | | byte altSigAlgo; |
10708 | | byte* altSigData; |
10709 | | word32 altSigDataSz; |
10710 | | word32 altSignatureSz; |
10711 | | byte altPeerAuthGood; |
10712 | | #endif |
10713 | | } Dcv13Args; |
10714 | | |
10715 | | static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs) |
10716 | 0 | { |
10717 | 0 | Dcv13Args* args = (Dcv13Args*)pArgs; |
10718 | |
|
10719 | 0 | if (args && args->sigData != NULL) { |
10720 | 0 | XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10721 | 0 | args->sigData = NULL; |
10722 | 0 | } |
10723 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10724 | | if (args && args->altSigData != NULL) { |
10725 | | XFREE(args->altSigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE); |
10726 | | args->altSigData = NULL; |
10727 | | } |
10728 | | #endif |
10729 | 0 | (void)ssl; |
10730 | 0 | } |
10731 | | |
10732 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10733 | | #ifndef NO_RSA |
10734 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10735 | | * RSA public key. Convert it into a usable public key. */ |
10736 | | static int decodeRsaKey(WOLFSSL* ssl) |
10737 | | { |
10738 | | int keyRet; |
10739 | | word32 tmpIdx = 0; |
10740 | | |
10741 | | if (ssl->peerRsaKeyPresent) |
10742 | | return INVALID_PARAMETER; |
10743 | | |
10744 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
10745 | | if (keyRet != 0) |
10746 | | return PEER_KEY_ERROR; |
10747 | | |
10748 | | ssl->peerRsaKeyPresent = 1; |
10749 | | keyRet = wc_RsaPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10750 | | ssl->peerRsaKey, |
10751 | | ssl->peerCert.sapkiLen); |
10752 | | if (keyRet != 0) |
10753 | | return PEER_KEY_ERROR; |
10754 | | |
10755 | | return 0; |
10756 | | } |
10757 | | #endif /* !NO_RSA */ |
10758 | | |
10759 | | #ifdef HAVE_ECC |
10760 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10761 | | * ECC public key. Convert it into a usable public key. */ |
10762 | | static int decodeEccKey(WOLFSSL* ssl) |
10763 | | { |
10764 | | int keyRet; |
10765 | | word32 tmpIdx = 0; |
10766 | | |
10767 | | if (ssl->peerEccDsaKeyPresent) |
10768 | | return INVALID_PARAMETER; |
10769 | | |
10770 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
10771 | | if (keyRet != 0) |
10772 | | return PEER_KEY_ERROR; |
10773 | | |
10774 | | ssl->peerEccDsaKeyPresent = 1; |
10775 | | keyRet = wc_EccPublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10776 | | ssl->peerEccDsaKey, |
10777 | | ssl->peerCert.sapkiLen); |
10778 | | if (keyRet != 0) |
10779 | | return PEER_KEY_ERROR; |
10780 | | |
10781 | | return 0; |
10782 | | } |
10783 | | #endif /* HAVE_ECC */ |
10784 | | |
10785 | | #ifdef HAVE_DILITHIUM |
10786 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10787 | | * dilithium public key. Convert it into a usable public key. */ |
10788 | | static int decodeDilithiumKey(WOLFSSL* ssl, int level) |
10789 | | { |
10790 | | int keyRet; |
10791 | | word32 tmpIdx = 0; |
10792 | | |
10793 | | if (ssl->peerDilithiumKeyPresent) |
10794 | | return INVALID_PARAMETER; |
10795 | | |
10796 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
10797 | | (void**)&ssl->peerDilithiumKey); |
10798 | | if (keyRet != 0) |
10799 | | return PEER_KEY_ERROR; |
10800 | | |
10801 | | ssl->peerDilithiumKeyPresent = 1; |
10802 | | keyRet = wc_dilithium_set_level(ssl->peerDilithiumKey, level); |
10803 | | if (keyRet != 0) |
10804 | | return PEER_KEY_ERROR; |
10805 | | |
10806 | | keyRet = wc_Dilithium_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10807 | | ssl->peerDilithiumKey, |
10808 | | ssl->peerCert.sapkiLen); |
10809 | | if (keyRet != 0) |
10810 | | return PEER_KEY_ERROR; |
10811 | | |
10812 | | return 0; |
10813 | | } |
10814 | | #endif /* HAVE_DILITHIUM */ |
10815 | | |
10816 | | #ifdef HAVE_FALCON |
10817 | | /* ssl->peerCert->sapkiDer is the alternative public key. Hopefully it is a |
10818 | | * falcon public key. Convert it into a usable public key. */ |
10819 | | static int decodeFalconKey(WOLFSSL* ssl, int level) |
10820 | | { |
10821 | | int keyRet; |
10822 | | word32 tmpIdx = 0; |
10823 | | |
10824 | | if (ssl->peerFalconKeyPresent) |
10825 | | return INVALID_PARAMETER; |
10826 | | |
10827 | | keyRet = AllocKey(ssl, DYNAMIC_TYPE_FALCON, (void**)&ssl->peerFalconKey); |
10828 | | if (keyRet != 0) |
10829 | | return PEER_KEY_ERROR; |
10830 | | |
10831 | | ssl->peerFalconKeyPresent = 1; |
10832 | | keyRet = wc_falcon_set_level(ssl->peerFalconKey, level); |
10833 | | if (keyRet != 0) |
10834 | | return PEER_KEY_ERROR; |
10835 | | |
10836 | | keyRet = wc_Falcon_PublicKeyDecode(ssl->peerCert.sapkiDer, &tmpIdx, |
10837 | | ssl->peerFalconKey, |
10838 | | ssl->peerCert.sapkiLen); |
10839 | | if (keyRet != 0) |
10840 | | return PEER_KEY_ERROR; |
10841 | | |
10842 | | return 0; |
10843 | | } |
10844 | | #endif /* HAVE_FALCON */ |
10845 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10846 | | |
10847 | | /* handle processing TLS v1.3 certificate_verify (15) */ |
10848 | | /* Parse and handle a TLS v1.3 CertificateVerify message. |
10849 | | * |
10850 | | * ssl The SSL/TLS object. |
10851 | | * input The message buffer. |
10852 | | * inOutIdx On entry, the index into the message buffer of |
10853 | | * CertificateVerify. |
10854 | | * On exit, the index of byte after the CertificateVerify message. |
10855 | | * totalSz The length of the current handshake message. |
10856 | | * returns 0 on success and otherwise failure. |
10857 | | */ |
10858 | | static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input, |
10859 | | word32* inOutIdx, word32 totalSz) |
10860 | 0 | { |
10861 | 0 | int ret = 0; |
10862 | 0 | byte* sig = NULL; |
10863 | 0 | #ifndef NO_RSA |
10864 | | /* Use this as a temporary buffer for RSA signature verification. */ |
10865 | 0 | buffer* rsaSigBuf = &ssl->buffers.sig; |
10866 | 0 | #endif |
10867 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10868 | | Dcv13Args* args = NULL; |
10869 | | WOLFSSL_ASSERT_SIZEOF_GE(ssl->async->args, *args); |
10870 | | #else |
10871 | 0 | Dcv13Args args[1]; |
10872 | 0 | #endif |
10873 | |
|
10874 | 0 | WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO); |
10875 | 0 | WOLFSSL_ENTER("DoTls13CertificateVerify"); |
10876 | |
|
10877 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
10878 | | ret = tsip_Tls13CertificateVerify(ssl, input, inOutIdx, totalSz); |
10879 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
10880 | | goto exit_dcv; |
10881 | | } |
10882 | | ret = 0; |
10883 | | #endif |
10884 | |
|
10885 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10886 | | if (ssl->async == NULL) { |
10887 | | ssl->async = (struct WOLFSSL_ASYNC*) |
10888 | | XMALLOC(sizeof(struct WOLFSSL_ASYNC), ssl->heap, |
10889 | | DYNAMIC_TYPE_ASYNC); |
10890 | | if (ssl->async == NULL) |
10891 | | ERROR_OUT(MEMORY_E, exit_dcv); |
10892 | | } |
10893 | | args = (Dcv13Args*)ssl->async->args; |
10894 | | |
10895 | | ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState); |
10896 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
10897 | | /* Check for error */ |
10898 | | if (ret < 0) |
10899 | | goto exit_dcv; |
10900 | | } |
10901 | | else |
10902 | | #endif |
10903 | 0 | { |
10904 | | /* Reset state */ |
10905 | 0 | ret = 0; |
10906 | 0 | ssl->options.asyncState = TLS_ASYNC_BEGIN; |
10907 | 0 | XMEMSET(args, 0, sizeof(Dcv13Args)); |
10908 | 0 | ssl->options.peerHashAlgo = sha_mac; |
10909 | 0 | ssl->options.peerSigAlgo = anonymous_sa_algo; |
10910 | 0 | args->idx = *inOutIdx; |
10911 | 0 | args->begin = *inOutIdx; |
10912 | | #ifdef WOLFSSL_ASYNC_CRYPT |
10913 | | ssl->async->freeArgs = FreeDcv13Args; |
10914 | | #endif |
10915 | 0 | } |
10916 | |
|
10917 | 0 | switch(ssl->options.asyncState) |
10918 | 0 | { |
10919 | 0 | case TLS_ASYNC_BEGIN: |
10920 | 0 | { |
10921 | | #ifdef WOLFSSL_CALLBACKS |
10922 | | if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify"); |
10923 | | if (ssl->toInfoOn) AddLateName("CertificateVerify", |
10924 | | &ssl->timeoutInfo); |
10925 | | #endif |
10926 | | |
10927 | | /* Advance state and proceed */ |
10928 | 0 | ssl->options.asyncState = TLS_ASYNC_BUILD; |
10929 | 0 | } /* case TLS_ASYNC_BEGIN */ |
10930 | 0 | FALL_THROUGH; |
10931 | |
|
10932 | 0 | case TLS_ASYNC_BUILD: |
10933 | 0 | { |
10934 | 0 | int validSigAlgo; |
10935 | 0 | const Suites* suites = WOLFSSL_SUITES(ssl); |
10936 | 0 | word16 i; |
10937 | | |
10938 | | /* Signature algorithm. */ |
10939 | 0 | if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) { |
10940 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10941 | 0 | } |
10942 | | |
10943 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10944 | | if (ssl->peerSigSpec == NULL) { |
10945 | | /* The peer did not respond. We didn't send CKS or they don't |
10946 | | * support it. Either way, we do not need to handle dual |
10947 | | * key/sig case. */ |
10948 | | ssl->sigSpec = NULL; |
10949 | | ssl->sigSpecSz = 0; |
10950 | | } |
10951 | | |
10952 | | /* If no CKS extension or either native or alternative, then just |
10953 | | * get a normal sigalgo. But if BOTH, then get the native and alt |
10954 | | * sig algos. */ |
10955 | | if (ssl->sigSpec == NULL || |
10956 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_NATIVE || |
10957 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
10958 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10959 | 0 | validSigAlgo = 0; |
10960 | 0 | for (i = 0; i < suites->hashSigAlgoSz; i += 2) { |
10961 | 0 | if ((suites->hashSigAlgo[i + 0] == input[args->idx + 0]) && |
10962 | 0 | (suites->hashSigAlgo[i + 1] == input[args->idx + 1])) { |
10963 | 0 | validSigAlgo = 1; |
10964 | 0 | break; |
10965 | 0 | } |
10966 | 0 | } |
10967 | 0 | if (!validSigAlgo) { |
10968 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
10969 | 0 | } |
10970 | | |
10971 | 0 | ret = DecodeTls13SigAlg(input + args->idx, |
10972 | 0 | &ssl->options.peerHashAlgo, &ssl->options.peerSigAlgo); |
10973 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
10974 | | } |
10975 | | else { |
10976 | | ret = DecodeTls13HybridSigAlg(input + args->idx, |
10977 | | &ssl->options.peerHashAlgo, |
10978 | | &ssl->options.peerSigAlgo, |
10979 | | &args->altSigAlgo); |
10980 | | } |
10981 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
10982 | |
|
10983 | 0 | if (ret < 0) |
10984 | 0 | goto exit_dcv; |
10985 | 0 | args->idx += OPAQUE16_LEN; |
10986 | | |
10987 | | /* Signature length. */ |
10988 | 0 | if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) { |
10989 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10990 | 0 | } |
10991 | 0 | ato16(input + args->idx, &args->sz); |
10992 | 0 | args->idx += OPAQUE16_LEN; |
10993 | | |
10994 | | /* Signature data. */ |
10995 | 0 | if ((args->idx - args->begin) + args->sz > totalSz) { |
10996 | 0 | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
10997 | 0 | } |
10998 | | |
10999 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11000 | | if ((ssl->sigSpec != NULL) && |
11001 | | (*ssl->sigSpec != WOLFSSL_CKS_SIGSPEC_NATIVE)) { |
11002 | | |
11003 | | word16 sa; |
11004 | | if (args->altSigAlgo == 0) |
11005 | | sa = ssl->options.peerSigAlgo; |
11006 | | else |
11007 | | sa = args->altSigAlgo; |
11008 | | |
11009 | | switch(sa) { |
11010 | | #ifndef NO_RSA |
11011 | | case rsa_pss_sa_algo: |
11012 | | ret = decodeRsaKey(ssl); |
11013 | | break; |
11014 | | #endif |
11015 | | #ifdef HAVE_ECC |
11016 | | case ecc_dsa_sa_algo: |
11017 | | ret = decodeEccKey(ssl); |
11018 | | break; |
11019 | | #endif |
11020 | | #ifdef HAVE_DILITHIUM |
11021 | | case dilithium_level2_sa_algo: |
11022 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_44); |
11023 | | break; |
11024 | | case dilithium_level3_sa_algo: |
11025 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_65); |
11026 | | break; |
11027 | | case dilithium_level5_sa_algo: |
11028 | | ret = decodeDilithiumKey(ssl, WC_ML_DSA_87); |
11029 | | break; |
11030 | | #endif |
11031 | | #ifdef HAVE_FALCON |
11032 | | case falcon_level1_sa_algo: |
11033 | | ret = decodeFalconKey(ssl, 1); |
11034 | | break; |
11035 | | case falcon_level5_sa_algo: |
11036 | | ret = decodeFalconKey(ssl, 5); |
11037 | | break; |
11038 | | #endif |
11039 | | default: |
11040 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
11041 | | } |
11042 | | |
11043 | | if (ret != 0) |
11044 | | ERROR_OUT(ret, exit_dcv); |
11045 | | |
11046 | | if (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_ALTERNATIVE) { |
11047 | | /* Now swap in the alternative by removing the native. |
11048 | | * sa contains the alternative signature type. */ |
11049 | | #ifndef NO_RSA |
11050 | | if (ssl->peerRsaKeyPresent && sa != rsa_pss_sa_algo) { |
11051 | | FreeKey(ssl, DYNAMIC_TYPE_RSA, |
11052 | | (void**)&ssl->peerRsaKey); |
11053 | | ssl->peerRsaKeyPresent = 0; |
11054 | | } |
11055 | | #endif |
11056 | | #ifdef HAVE_ECC |
11057 | | else if (ssl->peerEccDsaKeyPresent && |
11058 | | sa != ecc_dsa_sa_algo) { |
11059 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
11060 | | (void**)&ssl->peerEccDsaKey); |
11061 | | ssl->peerEccDsaKeyPresent = 0; |
11062 | | } |
11063 | | #endif |
11064 | | #ifdef HAVE_DILITHIUM |
11065 | | else if (ssl->peerDilithiumKeyPresent && |
11066 | | sa != dilithium_level2_sa_algo && |
11067 | | sa != dilithium_level3_sa_algo && |
11068 | | sa != dilithium_level5_sa_algo) { |
11069 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
11070 | | (void**)&ssl->peerDilithiumKey); |
11071 | | ssl->peerDilithiumKeyPresent = 0; |
11072 | | } |
11073 | | #endif |
11074 | | #ifdef HAVE_FALCON |
11075 | | else if (ssl->peerFalconKeyPresent && |
11076 | | sa != falcon_level1_sa_algo && |
11077 | | sa != falcon_level5_sa_algo) { |
11078 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
11079 | | (void**)&ssl->peerFalconKey); |
11080 | | ssl->peerFalconKeyPresent = 0; |
11081 | | } |
11082 | | #endif |
11083 | | else { |
11084 | | ERROR_OUT(PEER_KEY_ERROR, exit_dcv); |
11085 | | } |
11086 | | } |
11087 | | } |
11088 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11089 | | |
11090 | | /* Check for public key of required type. */ |
11091 | | /* Assume invalid unless signature algo matches the key provided */ |
11092 | 0 | validSigAlgo = 0; |
11093 | | #ifdef HAVE_ED25519 |
11094 | | if (ssl->options.peerSigAlgo == ed25519_sa_algo) { |
11095 | | WOLFSSL_MSG("Peer sent ED25519 sig"); |
11096 | | validSigAlgo = (ssl->peerEd25519Key != NULL) && |
11097 | | ssl->peerEd25519KeyPresent; |
11098 | | } |
11099 | | #endif |
11100 | | #ifdef HAVE_ED448 |
11101 | | if (ssl->options.peerSigAlgo == ed448_sa_algo) { |
11102 | | WOLFSSL_MSG("Peer sent ED448 sig"); |
11103 | | validSigAlgo = (ssl->peerEd448Key != NULL) && |
11104 | | ssl->peerEd448KeyPresent; |
11105 | | } |
11106 | | #endif |
11107 | 0 | #ifdef HAVE_ECC |
11108 | 0 | if (ssl->options.peerSigAlgo == ecc_dsa_sa_algo) { |
11109 | 0 | WOLFSSL_MSG("Peer sent ECC sig"); |
11110 | 0 | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
11111 | 0 | ssl->peerEccDsaKeyPresent; |
11112 | 0 | } |
11113 | 0 | #endif |
11114 | | #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
11115 | | if (ssl->options.peerSigAlgo == sm2_sa_algo) { |
11116 | | WOLFSSL_MSG("Peer sent SM2 sig"); |
11117 | | validSigAlgo = (ssl->peerEccDsaKey != NULL) && |
11118 | | ssl->peerEccDsaKeyPresent; |
11119 | | } |
11120 | | #endif |
11121 | | #ifdef HAVE_FALCON |
11122 | | if (ssl->options.peerSigAlgo == falcon_level1_sa_algo) { |
11123 | | WOLFSSL_MSG("Peer sent Falcon Level 1 sig"); |
11124 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
11125 | | ssl->peerFalconKeyPresent; |
11126 | | } |
11127 | | if (ssl->options.peerSigAlgo == falcon_level5_sa_algo) { |
11128 | | WOLFSSL_MSG("Peer sent Falcon Level 5 sig"); |
11129 | | validSigAlgo = (ssl->peerFalconKey != NULL) && |
11130 | | ssl->peerFalconKeyPresent; |
11131 | | } |
11132 | | #endif |
11133 | | #ifdef HAVE_DILITHIUM |
11134 | | if (ssl->options.peerSigAlgo == dilithium_level2_sa_algo) { |
11135 | | WOLFSSL_MSG("Peer sent Dilithium Level 2 sig"); |
11136 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
11137 | | ssl->peerDilithiumKeyPresent; |
11138 | | } |
11139 | | if (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) { |
11140 | | WOLFSSL_MSG("Peer sent Dilithium Level 3 sig"); |
11141 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
11142 | | ssl->peerDilithiumKeyPresent; |
11143 | | } |
11144 | | if (ssl->options.peerSigAlgo == dilithium_level5_sa_algo) { |
11145 | | WOLFSSL_MSG("Peer sent Dilithium Level 5 sig"); |
11146 | | validSigAlgo = (ssl->peerDilithiumKey != NULL) && |
11147 | | ssl->peerDilithiumKeyPresent; |
11148 | | } |
11149 | | #endif |
11150 | 0 | #ifndef NO_RSA |
11151 | 0 | if (ssl->options.peerSigAlgo == rsa_sa_algo) { |
11152 | 0 | WOLFSSL_MSG("Peer sent PKCS#1.5 algo - not valid TLS 1.3"); |
11153 | 0 | ERROR_OUT(INVALID_PARAMETER, exit_dcv); |
11154 | 0 | } |
11155 | 0 | if (ssl->options.peerSigAlgo == rsa_pss_sa_algo) { |
11156 | 0 | WOLFSSL_MSG("Peer sent RSA sig"); |
11157 | 0 | validSigAlgo = (ssl->peerRsaKey != NULL) && |
11158 | 0 | ssl->peerRsaKeyPresent; |
11159 | 0 | } |
11160 | 0 | #endif |
11161 | 0 | if (!validSigAlgo) { |
11162 | 0 | WOLFSSL_MSG("Sig algo doesn't correspond to certificate"); |
11163 | 0 | ERROR_OUT(SIG_VERIFY_E, exit_dcv); |
11164 | 0 | } |
11165 | | |
11166 | 0 | args->sigSz = args->sz; |
11167 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11168 | | if (ssl->sigSpec != NULL && |
11169 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11170 | | /* In case we received two signatures, both of them are encoded |
11171 | | * with their size as 16-bit integeter prior in memory. Hence, |
11172 | | * we can decode both lengths here now. */ |
11173 | | word32 tmpIdx = args->idx; |
11174 | | word16 tmpSz = 0; |
11175 | | if (args->sz < OPAQUE16_LEN) { |
11176 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11177 | | } |
11178 | | ato16(input + tmpIdx, &tmpSz); |
11179 | | args->sigSz = tmpSz; |
11180 | | |
11181 | | tmpIdx += OPAQUE16_LEN + args->sigSz; |
11182 | | if (tmpIdx - args->idx + OPAQUE16_LEN > args->sz) { |
11183 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11184 | | } |
11185 | | ato16(input + tmpIdx, &tmpSz); |
11186 | | args->altSignatureSz = tmpSz; |
11187 | | |
11188 | | if (args->sz != (args->sigSz + args->altSignatureSz + |
11189 | | OPAQUE16_LEN + OPAQUE16_LEN)) { |
11190 | | ERROR_OUT(BUFFER_ERROR, exit_dcv); |
11191 | | } |
11192 | | } |
11193 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11194 | |
|
11195 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
11196 | | /* In case we have to verify an RSA signature, we have to store the |
11197 | | * signature in the 'rsaSigBuf' structure for further processing. |
11198 | | */ |
11199 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
11200 | 0 | word32 sigSz = args->sigSz; |
11201 | 0 | sig = input + args->idx; |
11202 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11203 | | /* Check if our alternative signature was RSA */ |
11204 | | if (ssl->sigSpec != NULL && |
11205 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11206 | | if (ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
11207 | | /* We have to skip the first signature (length field |
11208 | | * and signature itself) and the length field of the |
11209 | | * alternative signature. */ |
11210 | | sig += OPAQUE16_LEN + OPAQUE16_LEN + args->sigSz; |
11211 | | sigSz = args->altSignatureSz; |
11212 | | } |
11213 | | else { |
11214 | | /* We have to skip the length field */ |
11215 | | sig += OPAQUE16_LEN; |
11216 | | } |
11217 | | } |
11218 | | #endif |
11219 | 0 | rsaSigBuf->buffer = (byte*)XMALLOC(sigSz, ssl->heap, |
11220 | 0 | DYNAMIC_TYPE_SIGNATURE); |
11221 | 0 | if (rsaSigBuf->buffer == NULL) { |
11222 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
11223 | 0 | } |
11224 | 0 | rsaSigBuf->length = sigSz; |
11225 | 0 | XMEMCPY(rsaSigBuf->buffer, sig, rsaSigBuf->length); |
11226 | 0 | } |
11227 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
11228 | | |
11229 | 0 | args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
11230 | 0 | DYNAMIC_TYPE_SIGNATURE); |
11231 | 0 | if (args->sigData == NULL) { |
11232 | 0 | ERROR_OUT(MEMORY_E, exit_dcv); |
11233 | 0 | } |
11234 | | |
11235 | 0 | ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1); |
11236 | 0 | if (ret < 0) |
11237 | 0 | goto exit_dcv; |
11238 | | |
11239 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11240 | | if ((ssl->sigSpec != NULL) && |
11241 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH)) { |
11242 | | args->altSigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap, |
11243 | | DYNAMIC_TYPE_SIGNATURE); |
11244 | | if (args->altSigData == NULL) { |
11245 | | ERROR_OUT(MEMORY_E, exit_dcv); |
11246 | | } |
11247 | | XMEMCPY(args->altSigData, args->sigData, args->sigDataSz); |
11248 | | args->altSigDataSz = args->sigDataSz; |
11249 | | } |
11250 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11251 | | |
11252 | 0 | #ifdef HAVE_ECC |
11253 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
11254 | 0 | (ssl->peerEccDsaKeyPresent)) { |
11255 | 0 | ret = CreateECCEncodedSig(args->sigData, |
11256 | 0 | args->sigDataSz, ssl->options.peerHashAlgo); |
11257 | 0 | if (ret < 0) |
11258 | 0 | goto exit_dcv; |
11259 | 0 | args->sigDataSz = (word16)ret; |
11260 | 0 | ret = 0; |
11261 | 0 | } |
11262 | | |
11263 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11264 | | if ((ssl->sigSpec != NULL) && |
11265 | | (*ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) && |
11266 | | (args->altSigAlgo == ecc_dsa_sa_algo) && |
11267 | | (ssl->peerEccDsaKeyPresent)) { |
11268 | | ret = CreateECCEncodedSig(args->altSigData, |
11269 | | args->altSigDataSz, ssl->options.peerHashAlgo); |
11270 | | if (ret < 0) |
11271 | | goto exit_dcv; |
11272 | | args->altSigDataSz = (word16)ret; |
11273 | | ret = 0; |
11274 | | } |
11275 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11276 | 0 | #endif /* HAVE_ECC */ |
11277 | | |
11278 | | /* Advance state and proceed */ |
11279 | 0 | ssl->options.asyncState = TLS_ASYNC_DO; |
11280 | 0 | } /* case TLS_ASYNC_BUILD */ |
11281 | 0 | FALL_THROUGH; |
11282 | |
|
11283 | 0 | case TLS_ASYNC_DO: |
11284 | 0 | { |
11285 | 0 | sig = input + args->idx; |
11286 | 0 | (void)sig; |
11287 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11288 | | if (ssl->sigSpec != NULL && |
11289 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11290 | | /* As we have two signatures in the message, we stored |
11291 | | * the length of each before the actual signature. This |
11292 | | * is necessary, as we could have two algorithms with |
11293 | | * variable length signatures. */ |
11294 | | sig += OPAQUE16_LEN; |
11295 | | } |
11296 | | #endif |
11297 | 0 | #ifndef NO_RSA |
11298 | 0 | if ((ssl->options.peerSigAlgo == rsa_pss_sa_algo) && |
11299 | 0 | (ssl->peerRsaKey != NULL) && (ssl->peerRsaKeyPresent != 0)) { |
11300 | 0 | WOLFSSL_MSG("Doing RSA peer cert verify"); |
11301 | 0 | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
11302 | 0 | (word32)rsaSigBuf->length, &args->output, |
11303 | 0 | ssl->options.peerSigAlgo, |
11304 | 0 | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
11305 | | #ifdef HAVE_PK_CALLBACKS |
11306 | | &ssl->buffers.peerRsaKey |
11307 | | #else |
11308 | 0 | NULL |
11309 | 0 | #endif |
11310 | 0 | ); |
11311 | 0 | if (ret >= 0) { |
11312 | 0 | args->sendSz = (word32)ret; |
11313 | 0 | ret = 0; |
11314 | 0 | } |
11315 | 0 | } |
11316 | 0 | #endif /* !NO_RSA */ |
11317 | 0 | #ifdef HAVE_ECC |
11318 | 0 | if ((ssl->options.peerSigAlgo == ecc_dsa_sa_algo) && |
11319 | 0 | ssl->peerEccDsaKeyPresent) { |
11320 | 0 | WOLFSSL_MSG("Doing ECC peer cert verify"); |
11321 | 0 | ret = EccVerify(ssl, sig, args->sigSz, |
11322 | 0 | args->sigData, args->sigDataSz, |
11323 | 0 | ssl->peerEccDsaKey, |
11324 | | #ifdef HAVE_PK_CALLBACKS |
11325 | | &ssl->buffers.peerEccDsaKey |
11326 | | #else |
11327 | 0 | NULL |
11328 | 0 | #endif |
11329 | 0 | ); |
11330 | |
|
11331 | 0 | if (ret >= 0) { |
11332 | | /* CLIENT/SERVER: data verified with public key from |
11333 | | * certificate. */ |
11334 | 0 | ssl->options.peerAuthGood = 1; |
11335 | |
|
11336 | 0 | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
11337 | 0 | ssl->peerEccDsaKeyPresent = 0; |
11338 | 0 | } |
11339 | 0 | } |
11340 | 0 | #endif /* HAVE_ECC */ |
11341 | | #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) |
11342 | | if ((ssl->options.peerSigAlgo == sm2_sa_algo) && |
11343 | | ssl->peerEccDsaKeyPresent) { |
11344 | | WOLFSSL_MSG("Doing SM2/SM3 peer cert verify"); |
11345 | | ret = Sm2wSm3Verify(ssl, TLS13_SM2_SIG_ID, TLS13_SM2_SIG_ID_SZ, |
11346 | | sig, args->sigSz, args->sigData, args->sigDataSz, |
11347 | | ssl->peerEccDsaKey, NULL); |
11348 | | if (ret >= 0) { |
11349 | | /* CLIENT/SERVER: data verified with public key from |
11350 | | * certificate. */ |
11351 | | ssl->options.peerAuthGood = 1; |
11352 | | |
11353 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey); |
11354 | | ssl->peerEccDsaKeyPresent = 0; |
11355 | | } |
11356 | | } |
11357 | | #endif |
11358 | | #ifdef HAVE_ED25519 |
11359 | | if ((ssl->options.peerSigAlgo == ed25519_sa_algo) && |
11360 | | (ssl->peerEd25519KeyPresent)) { |
11361 | | WOLFSSL_MSG("Doing ED25519 peer cert verify"); |
11362 | | ret = Ed25519Verify(ssl, sig, args->sigSz, |
11363 | | args->sigData, args->sigDataSz, |
11364 | | ssl->peerEd25519Key, |
11365 | | #ifdef HAVE_PK_CALLBACKS |
11366 | | &ssl->buffers.peerEd25519Key |
11367 | | #else |
11368 | | NULL |
11369 | | #endif |
11370 | | ); |
11371 | | |
11372 | | if (ret >= 0) { |
11373 | | /* CLIENT/SERVER: data verified with public key from |
11374 | | * certificate. */ |
11375 | | ssl->options.peerAuthGood = 1; |
11376 | | FreeKey(ssl, DYNAMIC_TYPE_ED25519, |
11377 | | (void**)&ssl->peerEd25519Key); |
11378 | | ssl->peerEd25519KeyPresent = 0; |
11379 | | } |
11380 | | } |
11381 | | #endif |
11382 | | #ifdef HAVE_ED448 |
11383 | | if ((ssl->options.peerSigAlgo == ed448_sa_algo) && |
11384 | | (ssl->peerEd448KeyPresent)) { |
11385 | | WOLFSSL_MSG("Doing ED448 peer cert verify"); |
11386 | | ret = Ed448Verify(ssl, sig, args->sigSz, |
11387 | | args->sigData, args->sigDataSz, |
11388 | | ssl->peerEd448Key, |
11389 | | #ifdef HAVE_PK_CALLBACKS |
11390 | | &ssl->buffers.peerEd448Key |
11391 | | #else |
11392 | | NULL |
11393 | | #endif |
11394 | | ); |
11395 | | |
11396 | | if (ret >= 0) { |
11397 | | /* CLIENT/SERVER: data verified with public key from |
11398 | | * certificate. */ |
11399 | | ssl->options.peerAuthGood = 1; |
11400 | | FreeKey(ssl, DYNAMIC_TYPE_ED448, |
11401 | | (void**)&ssl->peerEd448Key); |
11402 | | ssl->peerEd448KeyPresent = 0; |
11403 | | } |
11404 | | } |
11405 | | #endif |
11406 | | #if defined(HAVE_FALCON) |
11407 | | if (((ssl->options.peerSigAlgo == falcon_level1_sa_algo) || |
11408 | | (ssl->options.peerSigAlgo == falcon_level5_sa_algo)) && |
11409 | | (ssl->peerFalconKeyPresent)) { |
11410 | | int res = 0; |
11411 | | WOLFSSL_MSG("Doing Falcon peer cert verify"); |
11412 | | ret = wc_falcon_verify_msg(sig, args->sigSz, |
11413 | | args->sigData, args->sigDataSz, |
11414 | | &res, ssl->peerFalconKey); |
11415 | | |
11416 | | if ((ret >= 0) && (res == 1)) { |
11417 | | /* CLIENT/SERVER: data verified with public key from |
11418 | | * certificate. */ |
11419 | | ssl->options.peerAuthGood = 1; |
11420 | | |
11421 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
11422 | | (void**)&ssl->peerFalconKey); |
11423 | | ssl->peerFalconKeyPresent = 0; |
11424 | | } |
11425 | | else if ((ret >= 0) && (res == 0)) { |
11426 | | WOLFSSL_MSG("Falcon signature verification failed"); |
11427 | | ret = SIG_VERIFY_E; |
11428 | | } |
11429 | | } |
11430 | | #endif /* HAVE_FALCON */ |
11431 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
11432 | | if (((ssl->options.peerSigAlgo == dilithium_level2_sa_algo) || |
11433 | | (ssl->options.peerSigAlgo == dilithium_level3_sa_algo) || |
11434 | | (ssl->options.peerSigAlgo == dilithium_level5_sa_algo)) && |
11435 | | (ssl->peerDilithiumKeyPresent)) { |
11436 | | int res = 0; |
11437 | | WOLFSSL_MSG("Doing Dilithium peer cert verify"); |
11438 | | ret = wc_dilithium_verify_ctx_msg(sig, args->sigSz, NULL, 0, |
11439 | | args->sigData, args->sigDataSz, |
11440 | | &res, ssl->peerDilithiumKey); |
11441 | | |
11442 | | if ((ret >= 0) && (res == 1)) { |
11443 | | /* CLIENT/SERVER: data verified with public key from |
11444 | | * certificate. */ |
11445 | | ssl->options.peerAuthGood = 1; |
11446 | | |
11447 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
11448 | | (void**)&ssl->peerDilithiumKey); |
11449 | | ssl->peerDilithiumKeyPresent = 0; |
11450 | | } |
11451 | | else if ((ret >= 0) && (res == 0)) { |
11452 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
11453 | | ret = SIG_VERIFY_E; |
11454 | | } |
11455 | | } |
11456 | | #endif /* HAVE_DILITHIUM */ |
11457 | | |
11458 | | /* Check for error */ |
11459 | 0 | if (ret != 0) { |
11460 | 0 | goto exit_dcv; |
11461 | 0 | } |
11462 | | |
11463 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11464 | | if (ssl->sigSpec != NULL && |
11465 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11466 | | /* Move forward to the alternative signature. */ |
11467 | | sig += args->sigSz + OPAQUE16_LEN; |
11468 | | |
11469 | | /* Verify the alternative signature */ |
11470 | | #ifndef NO_RSA |
11471 | | if ((args->altSigAlgo == rsa_pss_sa_algo) && |
11472 | | (ssl->peerRsaKey != NULL) && |
11473 | | (ssl->peerRsaKeyPresent != 0)) { |
11474 | | WOLFSSL_MSG("Doing RSA peer cert alt verify"); |
11475 | | ret = RsaVerify(ssl, rsaSigBuf->buffer, |
11476 | | (word32)rsaSigBuf->length, |
11477 | | &args->output, args->altSigAlgo, |
11478 | | ssl->options.peerHashAlgo, ssl->peerRsaKey, |
11479 | | #ifdef HAVE_PK_CALLBACKS |
11480 | | &ssl->buffers.peerRsaKey |
11481 | | #else |
11482 | | NULL |
11483 | | #endif |
11484 | | ); |
11485 | | if (ret >= 0) { |
11486 | | args->sendSz = ret; |
11487 | | ret = 0; |
11488 | | } |
11489 | | } |
11490 | | #endif /* !NO_RSA */ |
11491 | | #ifdef HAVE_ECC |
11492 | | if ((args->altSigAlgo == ecc_dsa_sa_algo) && |
11493 | | (ssl->peerEccDsaKeyPresent)) { |
11494 | | WOLFSSL_MSG("Doing ECC peer cert alt verify"); |
11495 | | ret = EccVerify(ssl, sig, args->altSignatureSz, |
11496 | | args->altSigData, args->altSigDataSz, |
11497 | | ssl->peerEccDsaKey, |
11498 | | #ifdef HAVE_PK_CALLBACKS |
11499 | | &ssl->buffers.peerEccDsaKey |
11500 | | #else |
11501 | | NULL |
11502 | | #endif |
11503 | | ); |
11504 | | |
11505 | | if (ret >= 0) { |
11506 | | /* CLIENT/SERVER: data verified with public key from |
11507 | | * certificate. */ |
11508 | | args->altPeerAuthGood = 1; |
11509 | | |
11510 | | FreeKey(ssl, DYNAMIC_TYPE_ECC, |
11511 | | (void**)&ssl->peerEccDsaKey); |
11512 | | ssl->peerEccDsaKeyPresent = 0; |
11513 | | } |
11514 | | } |
11515 | | #endif /* HAVE_ECC */ |
11516 | | #if defined(HAVE_FALCON) |
11517 | | if (((args->altSigAlgo == falcon_level1_sa_algo) || |
11518 | | (args->altSigAlgo == falcon_level5_sa_algo)) && |
11519 | | (ssl->peerFalconKeyPresent)) { |
11520 | | int res = 0; |
11521 | | WOLFSSL_MSG("Doing Falcon peer cert alt verify"); |
11522 | | ret = wc_falcon_verify_msg(sig, args->altSignatureSz, |
11523 | | args->altSigData, args->altSigDataSz, |
11524 | | &res, ssl->peerFalconKey); |
11525 | | |
11526 | | if ((ret >= 0) && (res == 1)) { |
11527 | | /* CLIENT/SERVER: data verified with public key from |
11528 | | * certificate. */ |
11529 | | args->altPeerAuthGood = 1; |
11530 | | |
11531 | | FreeKey(ssl, DYNAMIC_TYPE_FALCON, |
11532 | | (void**)&ssl->peerFalconKey); |
11533 | | ssl->peerFalconKeyPresent = 0; |
11534 | | } |
11535 | | else if ((ret >= 0) && (res == 0)) { |
11536 | | WOLFSSL_MSG("Falcon signature verification failed"); |
11537 | | ret = SIG_VERIFY_E; |
11538 | | } |
11539 | | } |
11540 | | #endif /* HAVE_FALCON */ |
11541 | | #if defined(HAVE_DILITHIUM) && !defined(WOLFSSL_DILITHIUM_NO_VERIFY) |
11542 | | if (((args->altSigAlgo == dilithium_level2_sa_algo) || |
11543 | | (args->altSigAlgo == dilithium_level3_sa_algo) || |
11544 | | (args->altSigAlgo == dilithium_level5_sa_algo)) && |
11545 | | (ssl->peerDilithiumKeyPresent)) { |
11546 | | int res = 0; |
11547 | | WOLFSSL_MSG("Doing Dilithium peer cert alt verify"); |
11548 | | ret = wc_dilithium_verify_ctx_msg(sig, args->altSignatureSz, |
11549 | | NULL, 0, args->altSigData, |
11550 | | args->altSigDataSz, &res, |
11551 | | ssl->peerDilithiumKey); |
11552 | | |
11553 | | if ((ret >= 0) && (res == 1)) { |
11554 | | /* CLIENT/SERVER: data verified with public key from |
11555 | | * certificate. */ |
11556 | | args->altPeerAuthGood = 1; |
11557 | | |
11558 | | FreeKey(ssl, DYNAMIC_TYPE_DILITHIUM, |
11559 | | (void**)&ssl->peerDilithiumKey); |
11560 | | ssl->peerDilithiumKeyPresent = 0; |
11561 | | } |
11562 | | else if ((ret >= 0) && (res == 0)) { |
11563 | | WOLFSSL_MSG("Dilithium signature verification failed"); |
11564 | | ret = SIG_VERIFY_E; |
11565 | | } |
11566 | | } |
11567 | | #endif /* HAVE_DILITHIUM */ |
11568 | | |
11569 | | /* Check for error */ |
11570 | | if (ret != 0) { |
11571 | | goto exit_dcv; |
11572 | | } |
11573 | | } |
11574 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11575 | | |
11576 | | /* Advance state and proceed */ |
11577 | 0 | ssl->options.asyncState = TLS_ASYNC_VERIFY; |
11578 | 0 | } /* case TLS_ASYNC_DO */ |
11579 | 0 | FALL_THROUGH; |
11580 | |
|
11581 | 0 | case TLS_ASYNC_VERIFY: |
11582 | 0 | { |
11583 | 0 | #if !defined(NO_RSA) && defined(WC_RSA_PSS) |
11584 | 0 | if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) { |
11585 | 0 | int sigAlgo = ssl->options.peerSigAlgo; |
11586 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11587 | | /* Check if our alternative signature was RSA */ |
11588 | | if (ssl->sigSpec != NULL && |
11589 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11590 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
11591 | | sigAlgo = args->altSigAlgo; |
11592 | | } |
11593 | | #endif |
11594 | 0 | ret = CheckRSASignature(ssl, sigAlgo, |
11595 | 0 | ssl->options.peerHashAlgo, args->output, args->sendSz); |
11596 | 0 | if (ret != 0) |
11597 | 0 | goto exit_dcv; |
11598 | | |
11599 | | /* CLIENT/SERVER: data verified with public key from |
11600 | | * certificate. */ |
11601 | 0 | ssl->peerRsaKeyPresent = 0; |
11602 | 0 | FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey); |
11603 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11604 | | /* Check if our alternative signature was RSA */ |
11605 | | if (ssl->sigSpec != NULL && |
11606 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH && |
11607 | | ssl->options.peerSigAlgo != rsa_pss_sa_algo) { |
11608 | | args->altPeerAuthGood = 1; |
11609 | | } |
11610 | | else |
11611 | | #endif |
11612 | 0 | ssl->options.peerAuthGood = 1; |
11613 | 0 | } |
11614 | 0 | #endif /* !NO_RSA && WC_RSA_PSS */ |
11615 | | |
11616 | | /* Advance state and proceed */ |
11617 | 0 | ssl->options.asyncState = TLS_ASYNC_FINALIZE; |
11618 | 0 | } /* case TLS_ASYNC_VERIFY */ |
11619 | 0 | FALL_THROUGH; |
11620 | |
|
11621 | 0 | case TLS_ASYNC_FINALIZE: |
11622 | 0 | { |
11623 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
11624 | | if (ssl->options.peerAuthGood && |
11625 | | ssl->sigSpec != NULL && |
11626 | | *ssl->sigSpec == WOLFSSL_CKS_SIGSPEC_BOTH) { |
11627 | | ssl->options.peerAuthGood = args->altPeerAuthGood; |
11628 | | } |
11629 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
11630 | 0 | ssl->options.havePeerVerify = 1; |
11631 | | |
11632 | | /* Set final index */ |
11633 | 0 | args->idx += args->sz; |
11634 | 0 | *inOutIdx = args->idx; |
11635 | | |
11636 | | /* Encryption is always on: add padding */ |
11637 | 0 | *inOutIdx += ssl->keys.padSz; |
11638 | | |
11639 | | /* Advance state and proceed */ |
11640 | 0 | ssl->options.asyncState = TLS_ASYNC_END; |
11641 | |
|
11642 | 0 | #if !defined(NO_WOLFSSL_CLIENT) |
11643 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11644 | 0 | ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE; |
11645 | 0 | #endif |
11646 | 0 | } /* case TLS_ASYNC_FINALIZE */ |
11647 | 0 | FALL_THROUGH; |
11648 | |
|
11649 | 0 | case TLS_ASYNC_END: |
11650 | 0 | { |
11651 | 0 | break; |
11652 | 0 | } |
11653 | | |
11654 | 0 | default: |
11655 | 0 | ret = INPUT_CASE_ERROR; |
11656 | 0 | } /* switch(ssl->options.asyncState) */ |
11657 | | |
11658 | 0 | exit_dcv: |
11659 | |
|
11660 | 0 | WOLFSSL_LEAVE("DoTls13CertificateVerify", ret); |
11661 | 0 | WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO); |
11662 | |
|
11663 | | #ifdef WOLFSSL_ASYNC_CRYPT |
11664 | | /* Handle async operation */ |
11665 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) { |
11666 | | /* Mark message as not received so it can process again */ |
11667 | | ssl->msgsReceived.got_certificate_verify = 0; |
11668 | | |
11669 | | return ret; |
11670 | | } |
11671 | | else |
11672 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
11673 | 0 | if (ret != 0) { |
11674 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
11675 | |
|
11676 | 0 | if (ret != WC_NO_ERR_TRACE(INVALID_PARAMETER)) { |
11677 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
11678 | 0 | } |
11679 | 0 | } |
11680 | | |
11681 | | /* Final cleanup */ |
11682 | 0 | FreeDcv13Args(ssl, args); |
11683 | 0 | FreeKeyExchange(ssl); |
11684 | 0 | #ifdef WOLFSSL_ASYNC_IO |
11685 | | /* Cleanup async */ |
11686 | 0 | FreeAsyncCtx(ssl, 0); |
11687 | 0 | #endif |
11688 | |
|
11689 | 0 | return ret; |
11690 | 0 | } |
11691 | | #endif /* !NO_RSA || HAVE_ECC */ |
11692 | | #endif /* !NO_CERTS */ |
11693 | | |
11694 | | /* Parse and handle a TLS v1.3 Finished message. |
11695 | | * |
11696 | | * ssl The SSL/TLS object. |
11697 | | * input The message buffer. |
11698 | | * inOutIdx On entry, the index into the message buffer of Finished. |
11699 | | * On exit, the index of byte after the Finished message and padding. |
11700 | | * size Length of message data. |
11701 | | * totalSz Length of remaining data in the message buffer. |
11702 | | * sniff Indicates whether we are sniffing packets. |
11703 | | * returns 0 on success and otherwise failure. |
11704 | | */ |
11705 | | int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
11706 | | word32 size, word32 totalSz, int sniff) |
11707 | 0 | { |
11708 | 0 | int ret; |
11709 | 0 | word32 finishedSz = 0; |
11710 | 0 | byte* secret; |
11711 | 0 | byte mac[WC_MAX_DIGEST_SIZE]; |
11712 | |
|
11713 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_DO); |
11714 | 0 | WOLFSSL_ENTER("DoTls13Finished"); |
11715 | |
|
11716 | 0 | #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH) |
11717 | | /* verify the client sent certificate if required */ |
11718 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && !ssl->options.resuming && |
11719 | 0 | (ssl->options.mutualAuth || ssl->options.failNoCert)) { |
11720 | | #ifdef OPENSSL_COMPATIBLE_DEFAULTS |
11721 | | if (ssl->options.isPSK) { |
11722 | | WOLFSSL_MSG("TLS v1.3 client used PSK but cert required. Allowing " |
11723 | | "for OpenSSL compatibility"); |
11724 | | } |
11725 | | else |
11726 | | #endif |
11727 | 0 | if ( |
11728 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
11729 | | !ssl->options.verifyPostHandshake && |
11730 | | #endif |
11731 | 0 | (!ssl->options.havePeerCert || !ssl->options.havePeerVerify)) { |
11732 | 0 | ret = NO_PEER_CERT; /* NO_PEER_VERIFY */ |
11733 | 0 | WOLFSSL_MSG("TLS v1.3 client did not present peer cert"); |
11734 | 0 | DoCertFatalAlert(ssl, ret); |
11735 | 0 | goto cleanup; |
11736 | 0 | } |
11737 | 0 | } |
11738 | 0 | #endif |
11739 | | |
11740 | | /* check against totalSz */ |
11741 | 0 | if (*inOutIdx + size > totalSz) { |
11742 | 0 | ret = BUFFER_E; |
11743 | 0 | goto cleanup; |
11744 | 0 | } |
11745 | | |
11746 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11747 | | ret = tsip_Tls13HandleFinished(ssl, input, inOutIdx, size, totalSz); |
11748 | | if (ret == 0) { |
11749 | | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11750 | | goto cleanup; |
11751 | | } |
11752 | | if (ret == WC_NO_ERR_TRACE(VERIFY_FINISHED_ERROR)) { |
11753 | | SendAlert(ssl, alert_fatal, decrypt_error); |
11754 | | goto cleanup; |
11755 | | } |
11756 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11757 | | /* other errors */ |
11758 | | goto cleanup; |
11759 | | } |
11760 | | ret = 0; |
11761 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11762 | | |
11763 | 0 | if (ssl->options.handShakeDone) { |
11764 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11765 | 0 | ssl->keys.client_write_MAC_secret, |
11766 | 0 | WOLFSSL_CLIENT_END); |
11767 | 0 | if (ret != 0) |
11768 | 0 | goto cleanup; |
11769 | | |
11770 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11771 | 0 | } |
11772 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11773 | | /* All the handshake messages have been received to calculate |
11774 | | * client and server finished keys. |
11775 | | */ |
11776 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11777 | 0 | ssl->keys.client_write_MAC_secret, |
11778 | 0 | WOLFSSL_CLIENT_END); |
11779 | 0 | if (ret != 0) |
11780 | 0 | goto cleanup; |
11781 | | |
11782 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11783 | 0 | ssl->keys.server_write_MAC_secret, |
11784 | 0 | WOLFSSL_SERVER_END); |
11785 | 0 | if (ret != 0) |
11786 | 0 | goto cleanup; |
11787 | | |
11788 | 0 | secret = ssl->keys.server_write_MAC_secret; |
11789 | 0 | } |
11790 | 0 | else { |
11791 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11792 | 0 | } |
11793 | | |
11794 | 0 | if (sniff == NO_SNIFF) { |
11795 | |
|
11796 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz); |
11797 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11798 | | if (finishedSz > TLS_FINISHED_SZ_MAX) { |
11799 | | ret = BUFFER_ERROR; |
11800 | | goto cleanup; |
11801 | | } |
11802 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11803 | | XMEMCPY(ssl->serverFinished, mac, finishedSz); |
11804 | | ssl->serverFinished_len = (byte)finishedSz; |
11805 | | } |
11806 | | else { |
11807 | | XMEMCPY(ssl->clientFinished, mac, finishedSz); |
11808 | | ssl->clientFinished_len = (byte)finishedSz; |
11809 | | } |
11810 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11811 | 0 | if (ret != 0) |
11812 | 0 | goto cleanup; |
11813 | 0 | if (size != finishedSz) { |
11814 | 0 | ret = BUFFER_ERROR; |
11815 | 0 | goto cleanup; |
11816 | 0 | } |
11817 | 0 | } |
11818 | | |
11819 | | #ifdef WOLFSSL_CALLBACKS |
11820 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
11821 | | if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo); |
11822 | | #endif |
11823 | | |
11824 | 0 | if (sniff == NO_SNIFF) { |
11825 | | /* Actually check verify data. */ |
11826 | 0 | if (size > WC_MAX_DIGEST_SIZE || |
11827 | 0 | ConstantCompare(input + *inOutIdx, mac, size) != 0){ |
11828 | 0 | WOLFSSL_MSG("Verify finished error on hashes"); |
11829 | 0 | SendAlert(ssl, alert_fatal, decrypt_error); |
11830 | 0 | WOLFSSL_ERROR_VERBOSE(VERIFY_FINISHED_ERROR); |
11831 | 0 | ret = VERIFY_FINISHED_ERROR; |
11832 | 0 | goto cleanup; |
11833 | 0 | } |
11834 | 0 | } |
11835 | | |
11836 | | /* Force input exhaustion at ProcessReply by consuming padSz. */ |
11837 | 0 | *inOutIdx += size + ssl->keys.padSz; |
11838 | |
|
11839 | 0 | #ifndef NO_WOLFSSL_SERVER |
11840 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
11841 | 0 | !ssl->options.handShakeDone) { |
11842 | | #ifdef WOLFSSL_EARLY_DATA |
11843 | | if (ssl->earlyData != no_early_data) { |
11844 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0) |
11845 | | goto cleanup; |
11846 | | } |
11847 | | #endif |
11848 | | /* Setup keys for application data messages from client. */ |
11849 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
11850 | 0 | goto cleanup; |
11851 | 0 | } |
11852 | 0 | #endif |
11853 | | |
11854 | 0 | #ifndef NO_WOLFSSL_CLIENT |
11855 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
11856 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
11857 | 0 | #endif |
11858 | 0 | #ifndef NO_WOLFSSL_SERVER |
11859 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
11860 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
11861 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
11862 | 0 | ssl->options.handShakeDone = 1; |
11863 | 0 | } |
11864 | 0 | #endif |
11865 | |
|
11866 | | #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_EARLY_DATA) |
11867 | | if (ssl->options.dtls && ssl->earlyData > early_data_ext) { |
11868 | | /* DTLSv1.3 has no EndOfearlydata messages. We stop processing EarlyData |
11869 | | as soon we receive the client's finished message */ |
11870 | | ssl->earlyData = done_early_data; |
11871 | | } |
11872 | | #endif /* WOLFSSL_DTLS13 && WOLFSSL_EARLY_DATA */ |
11873 | | #if defined(WOLFSSL_QUIC) && defined(WOLFSSL_EARLY_DATA) |
11874 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData > early_data_ext) { |
11875 | | /* QUIC has no EndOfEarlyData messages. We stop processing EarlyData |
11876 | | as soon we receive the client's finished message */ |
11877 | | ssl->earlyData = done_early_data; |
11878 | | } |
11879 | | #endif /* WOLFSSL_QUIC && WOLFSSL_EARLY_DATA */ |
11880 | |
|
11881 | 0 | ret = 0; |
11882 | 0 | cleanup: |
11883 | 0 | ForceZero(mac, sizeof(mac)); |
11884 | 0 | WOLFSSL_LEAVE("DoTls13Finished", ret); |
11885 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_DO); |
11886 | |
|
11887 | 0 | return ret; |
11888 | 0 | } |
11889 | | |
11890 | | #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) |
11891 | | /* Send the TLS v1.3 Finished message. |
11892 | | * |
11893 | | * ssl The SSL/TLS object. |
11894 | | * returns 0 on success, otherwise failure. |
11895 | | */ |
11896 | | static int SendTls13Finished(WOLFSSL* ssl) |
11897 | 0 | { |
11898 | 0 | byte finishedSz = ssl->specs.hash_size; |
11899 | 0 | byte* input; |
11900 | 0 | byte* output; |
11901 | 0 | int ret; |
11902 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
11903 | 0 | int outputSz; |
11904 | 0 | byte* secret; |
11905 | |
|
11906 | | #ifdef WOLFSSL_DTLS13 |
11907 | | int dtlsRet = 0, isDtls = 0; |
11908 | | #endif /* WOLFSSL_DTLS13 */ |
11909 | |
|
11910 | 0 | WOLFSSL_START(WC_FUNC_FINISHED_SEND); |
11911 | 0 | WOLFSSL_ENTER("SendTls13Finished"); |
11912 | |
|
11913 | 0 | ssl->options.buildingMsg = 1; |
11914 | | #ifdef WOLFSSL_DTLS13 |
11915 | | if (ssl->options.dtls) { |
11916 | | headerSz = DTLS_HANDSHAKE_HEADER_SZ; |
11917 | | /* using isDtls instead of ssl->options.dtls will abide clang static |
11918 | | analyzer on using an uninitialized value */ |
11919 | | isDtls = 1; |
11920 | | } |
11921 | | #endif /* WOLFSSL_DTLS13 */ |
11922 | |
|
11923 | 0 | outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA; |
11924 | | /* Check buffers are big enough and grow if needed. */ |
11925 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
11926 | 0 | return ret; |
11927 | | |
11928 | | /* get output buffer */ |
11929 | 0 | output = GetOutputBuffer(ssl); |
11930 | 0 | input = output + RECORD_HEADER_SZ; |
11931 | |
|
11932 | | #ifdef WOLFSSL_DTLS13 |
11933 | | if (isDtls) |
11934 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
11935 | | #endif /* WOLFSSL_DTLS13 */ |
11936 | |
|
11937 | 0 | AddTls13HandShakeHeader(input, (word32)finishedSz, 0, (word32)finishedSz, |
11938 | 0 | finished, ssl); |
11939 | |
|
11940 | | #if defined(WOLFSSL_RENESAS_TSIP_TLS) |
11941 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11942 | | ret = tsip_Tls13SendFinished(ssl, output, outputSz, input, 1); |
11943 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
11944 | | return ret; |
11945 | | } |
11946 | | ret = 0; |
11947 | | } |
11948 | | #endif /* WOLFSSL_RENESAS_TSIP_TLS */ |
11949 | | |
11950 | | /* make finished hashes */ |
11951 | 0 | if (ssl->options.handShakeDone) { |
11952 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11953 | 0 | ssl->keys.client_write_MAC_secret, |
11954 | 0 | WOLFSSL_CLIENT_END); |
11955 | 0 | if (ret != 0) |
11956 | 0 | return ret; |
11957 | | |
11958 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11959 | 0 | } |
11960 | 0 | else if (ssl->options.side == WOLFSSL_CLIENT_END) |
11961 | 0 | secret = ssl->keys.client_write_MAC_secret; |
11962 | 0 | else { |
11963 | | /* All the handshake messages have been done to calculate client and |
11964 | | * server finished keys. |
11965 | | */ |
11966 | 0 | ret = DeriveFinishedSecret(ssl, ssl->clientSecret, |
11967 | 0 | ssl->keys.client_write_MAC_secret, |
11968 | 0 | WOLFSSL_CLIENT_END); |
11969 | 0 | if (ret != 0) |
11970 | 0 | return ret; |
11971 | | |
11972 | 0 | ret = DeriveFinishedSecret(ssl, ssl->serverSecret, |
11973 | 0 | ssl->keys.server_write_MAC_secret, |
11974 | 0 | WOLFSSL_SERVER_END); |
11975 | 0 | if (ret != 0) |
11976 | 0 | return ret; |
11977 | | |
11978 | 0 | secret = ssl->keys.server_write_MAC_secret; |
11979 | 0 | } |
11980 | 0 | ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL); |
11981 | 0 | if (ret != 0) |
11982 | 0 | return ret; |
11983 | | #ifdef WOLFSSL_HAVE_TLS_UNIQUE |
11984 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
11985 | | XMEMCPY(ssl->clientFinished, &input[headerSz], finishedSz); |
11986 | | ssl->clientFinished_len = finishedSz; |
11987 | | } |
11988 | | else { |
11989 | | XMEMCPY(ssl->serverFinished, &input[headerSz], finishedSz); |
11990 | | ssl->serverFinished_len = finishedSz; |
11991 | | } |
11992 | | #endif /* WOLFSSL_HAVE_TLS_UNIQUE */ |
11993 | | |
11994 | | #ifdef WOLFSSL_DTLS13 |
11995 | | if (isDtls) { |
11996 | | dtlsRet = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
11997 | | (word16)(Dtls13GetRlHeaderLength(ssl, 1) + headerSz + finishedSz), finished, |
11998 | | 1); |
11999 | | if (dtlsRet != 0 && dtlsRet != WC_NO_ERR_TRACE(WANT_WRITE)) |
12000 | | return dtlsRet; |
12001 | | |
12002 | | } else |
12003 | | #endif /* WOLFSSL_DTLS13 */ |
12004 | 0 | { |
12005 | | /* This message is always encrypted. */ |
12006 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
12007 | 0 | headerSz + finishedSz, handshake, 1, 0, 0); |
12008 | 0 | if (sendSz < 0) { |
12009 | 0 | WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR); |
12010 | 0 | return BUILD_MSG_ERROR; |
12011 | 0 | } |
12012 | | |
12013 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
12014 | | if (ssl->hsInfoOn) AddPacketName(ssl, "Finished"); |
12015 | | if (ssl->toInfoOn) { |
12016 | | ret = AddPacketInfo(ssl, "Finished", handshake, output, sendSz, |
12017 | | WRITE_PROTO, 0, ssl->heap); |
12018 | | if (ret != 0) |
12019 | | return ret; |
12020 | | } |
12021 | | #endif |
12022 | | |
12023 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
12024 | 0 | ssl->options.buildingMsg = 0; |
12025 | 0 | } |
12026 | | |
12027 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12028 | | #ifdef WOLFSSL_EARLY_DATA |
12029 | | byte storeTrafficDecKeys = ssl->earlyData == no_early_data; |
12030 | | #endif |
12031 | | /* Can send application data now. */ |
12032 | 0 | if ((ret = DeriveMasterSecret(ssl)) != 0) |
12033 | 0 | return ret; |
12034 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
12035 | 0 | ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz); |
12036 | | #ifdef WOLFSSL_EARLY_DATA |
12037 | | |
12038 | | #ifdef WOLFSSL_DTLS13 |
12039 | | /* DTLS13 dynamically change keys and it needs all |
12040 | | the keys in ssl->keys to save the keying material */ |
12041 | | if (isDtls) |
12042 | | storeTrafficDecKeys = 1; |
12043 | | #endif /* WOLFSSL_DTLS13 */ |
12044 | | |
12045 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
12046 | | != 0) { |
12047 | | return ret; |
12048 | | } |
12049 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY, |
12050 | | storeTrafficDecKeys)) != 0) { |
12051 | | return ret; |
12052 | | } |
12053 | | #else |
12054 | 0 | if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE, |
12055 | 0 | 1)) != 0) { |
12056 | 0 | return ret; |
12057 | 0 | } |
12058 | 0 | #endif |
12059 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
12060 | 0 | return ret; |
12061 | |
|
12062 | | #ifdef WOLFSSL_DTLS13 |
12063 | | if (isDtls) { |
12064 | | w64wrapper epochTraffic0; |
12065 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
12066 | | ssl->dtls13Epoch = epochTraffic0; |
12067 | | ssl->dtls13PeerEpoch = epochTraffic0; |
12068 | | |
12069 | | ret = Dtls13SetEpochKeys( |
12070 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
12071 | | if (ret != 0) |
12072 | | return ret; |
12073 | | |
12074 | | } |
12075 | | #endif /* WOLFSSL_DTLS13 */ |
12076 | |
|
12077 | 0 | } |
12078 | | |
12079 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
12080 | 0 | !ssl->options.handShakeDone) { |
12081 | | #ifdef WOLFSSL_EARLY_DATA |
12082 | | if (ssl->earlyData != no_early_data) { |
12083 | | if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_SIDE_ONLY, |
12084 | | 1)) != 0) { |
12085 | | return ret; |
12086 | | } |
12087 | | } |
12088 | | #endif |
12089 | | /* Setup keys for application data messages. */ |
12090 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
12091 | 0 | return ret; |
12092 | |
|
12093 | | #if defined(HAVE_SESSION_TICKET) |
12094 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
12095 | | if (ret != 0) |
12096 | | return ret; |
12097 | | #endif |
12098 | |
|
12099 | | #ifdef WOLFSSL_DTLS13 |
12100 | | if (isDtls) { |
12101 | | w64wrapper epochTraffic0; |
12102 | | epochTraffic0 = w64From32(0, DTLS13_EPOCH_TRAFFIC0); |
12103 | | ssl->dtls13Epoch = epochTraffic0; |
12104 | | ssl->dtls13PeerEpoch = epochTraffic0; |
12105 | | |
12106 | | ret = Dtls13SetEpochKeys( |
12107 | | ssl, epochTraffic0, ENCRYPT_AND_DECRYPT_SIDE); |
12108 | | if (ret != 0) |
12109 | | return ret; |
12110 | | |
12111 | | } |
12112 | | #endif /* WOLFSSL_DTLS13 */ |
12113 | 0 | } |
12114 | | |
12115 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12116 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12117 | 0 | ssl->options.clientState = CLIENT_FINISHED_COMPLETE; |
12118 | 0 | ssl->options.handShakeState = HANDSHAKE_DONE; |
12119 | 0 | ssl->options.handShakeDone = 1; |
12120 | 0 | } |
12121 | 0 | #endif |
12122 | 0 | #ifndef NO_WOLFSSL_SERVER |
12123 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12124 | 0 | ssl->options.serverState = SERVER_FINISHED_COMPLETE; |
12125 | 0 | } |
12126 | 0 | #endif |
12127 | |
|
12128 | | #ifdef WOLFSSL_DTLS13 |
12129 | | if (isDtls) { |
12130 | | WOLFSSL_LEAVE("SendTls13Finished", ret); |
12131 | | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
12132 | | |
12133 | | return dtlsRet; |
12134 | | } |
12135 | | #endif /* WOLFSSL_DTLS13 */ |
12136 | |
|
12137 | 0 | if ((ret = SendBuffered(ssl)) != 0) |
12138 | 0 | return ret; |
12139 | | |
12140 | 0 | WOLFSSL_LEAVE("SendTls13Finished", ret); |
12141 | 0 | WOLFSSL_END(WC_FUNC_FINISHED_SEND); |
12142 | |
|
12143 | 0 | return ret; |
12144 | 0 | } |
12145 | | #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */ |
12146 | | |
12147 | | /* handle generation TLS v1.3 key_update (24) */ |
12148 | | /* Send the TLS v1.3 KeyUpdate message. |
12149 | | * |
12150 | | * ssl The SSL/TLS object. |
12151 | | * returns 0 on success, otherwise failure. |
12152 | | */ |
12153 | | int SendTls13KeyUpdate(WOLFSSL* ssl) |
12154 | 0 | { |
12155 | 0 | byte* input; |
12156 | 0 | byte* output; |
12157 | 0 | int ret; |
12158 | 0 | int headerSz = HANDSHAKE_HEADER_SZ; |
12159 | 0 | int outputSz; |
12160 | 0 | word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
12161 | |
|
12162 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND); |
12163 | 0 | WOLFSSL_ENTER("SendTls13KeyUpdate"); |
12164 | |
|
12165 | | #ifdef WOLFSSL_DTLS13 |
12166 | | if (ssl->options.dtls) |
12167 | | i = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
12168 | | #endif /* WOLFSSL_DTLS13 */ |
12169 | |
|
12170 | 0 | outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA; |
12171 | | /* Check buffers are big enough and grow if needed. */ |
12172 | 0 | if ((ret = CheckAvailableSize(ssl, outputSz)) != 0) |
12173 | 0 | return ret; |
12174 | | |
12175 | | /* get output buffer */ |
12176 | 0 | output = GetOutputBuffer(ssl); |
12177 | 0 | input = output + RECORD_HEADER_SZ; |
12178 | |
|
12179 | | #ifdef WOLFSSL_DTLS13 |
12180 | | if (ssl->options.dtls) |
12181 | | input = output + Dtls13GetRlHeaderLength(ssl, 1); |
12182 | | #endif /* WOLFSSL_DTLS13 */ |
12183 | |
|
12184 | 0 | AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl); |
12185 | | |
12186 | | /* If: |
12187 | | * 1. I haven't sent a KeyUpdate requesting a response and |
12188 | | * 2. This isn't responding to peer KeyUpdate requiring a response then, |
12189 | | * I want a response. |
12190 | | */ |
12191 | 0 | ssl->keys.updateResponseReq = output[i++] = |
12192 | 0 | !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond; |
12193 | | /* Sent response, no longer need to respond. */ |
12194 | 0 | ssl->keys.keyUpdateRespond = 0; |
12195 | |
|
12196 | | #ifdef WOLFSSL_DTLS13 |
12197 | | if (ssl->options.dtls) { |
12198 | | ret = Dtls13HandshakeSend(ssl, output, (word16)outputSz, |
12199 | | OPAQUE8_LEN + Dtls13GetRlHeaderLength(ssl, 1) + |
12200 | | DTLS_HANDSHAKE_HEADER_SZ, |
12201 | | key_update, 0); |
12202 | | } |
12203 | | else |
12204 | | #endif /* WOLFSSL_DTLS13 */ |
12205 | 0 | { |
12206 | | /* This message is always encrypted. */ |
12207 | 0 | int sendSz = BuildTls13Message(ssl, output, outputSz, input, |
12208 | 0 | headerSz + OPAQUE8_LEN, handshake, 0, 0, 0); |
12209 | 0 | if (sendSz < 0) |
12210 | 0 | return BUILD_MSG_ERROR; |
12211 | | |
12212 | | #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA) |
12213 | | if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate"); |
12214 | | if (ssl->toInfoOn) { |
12215 | | ret = AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz, |
12216 | | WRITE_PROTO, 0, ssl->heap); |
12217 | | if (ret != 0) |
12218 | | return ret; |
12219 | | } |
12220 | | #endif |
12221 | | |
12222 | 0 | ssl->buffers.outputBuffer.length += (word32)sendSz; |
12223 | |
|
12224 | 0 | ret = SendBuffered(ssl); |
12225 | | |
12226 | |
|
12227 | 0 | if (ret != 0 && ret != WC_NO_ERR_TRACE(WANT_WRITE)) |
12228 | 0 | return ret; |
12229 | 0 | } |
12230 | | |
12231 | | /* In DTLS we must wait for the ack before setting up the new keys */ |
12232 | 0 | if (!ssl->options.dtls) { |
12233 | | |
12234 | | /* Future traffic uses new encryption keys. */ |
12235 | 0 | if ((ret = DeriveTls13Keys( |
12236 | 0 | ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1)) |
12237 | 0 | != 0) |
12238 | 0 | return ret; |
12239 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
12240 | 0 | return ret; |
12241 | 0 | } |
12242 | | |
12243 | | |
12244 | 0 | WOLFSSL_LEAVE("SendTls13KeyUpdate", ret); |
12245 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND); |
12246 | |
|
12247 | 0 | return ret; |
12248 | 0 | } |
12249 | | |
12250 | | /* handle processing TLS v1.3 key_update (24) */ |
12251 | | /* Parse and handle a TLS v1.3 KeyUpdate message. |
12252 | | * |
12253 | | * ssl The SSL/TLS object. |
12254 | | * input The message buffer. |
12255 | | * inOutIdx On entry, the index into the message buffer of Finished. |
12256 | | * On exit, the index of byte after the Finished message and padding. |
12257 | | * totalSz The length of the current handshake message. |
12258 | | * returns 0 on success and otherwise failure. |
12259 | | */ |
12260 | | static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx, |
12261 | | word32 totalSz) |
12262 | 0 | { |
12263 | 0 | int ret; |
12264 | 0 | word32 i = *inOutIdx; |
12265 | |
|
12266 | 0 | WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO); |
12267 | 0 | WOLFSSL_ENTER("DoTls13KeyUpdate"); |
12268 | | |
12269 | | /* check against totalSz */ |
12270 | 0 | if (OPAQUE8_LEN != totalSz) |
12271 | 0 | return BUFFER_E; |
12272 | | |
12273 | 0 | switch (input[i]) { |
12274 | 0 | case update_not_requested: |
12275 | | /* This message in response to any outstanding request. */ |
12276 | 0 | ssl->keys.keyUpdateRespond = 0; |
12277 | 0 | ssl->keys.updateResponseReq = 0; |
12278 | 0 | break; |
12279 | 0 | case update_requested: |
12280 | | /* New key update requiring a response. */ |
12281 | 0 | ssl->keys.keyUpdateRespond = 1; |
12282 | 0 | break; |
12283 | 0 | default: |
12284 | 0 | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
12285 | 0 | return INVALID_PARAMETER; |
12286 | 0 | } |
12287 | | |
12288 | | /* Move index to byte after message. */ |
12289 | 0 | *inOutIdx += totalSz; |
12290 | | /* Always encrypted. */ |
12291 | 0 | *inOutIdx += ssl->keys.padSz; |
12292 | | |
12293 | | /* Future traffic uses new decryption keys. */ |
12294 | 0 | if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1)) |
12295 | 0 | != 0) { |
12296 | 0 | return ret; |
12297 | 0 | } |
12298 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
12299 | 0 | return ret; |
12300 | | |
12301 | | #ifdef WOLFSSL_DTLS13 |
12302 | | if (ssl->options.dtls) { |
12303 | | w64Increment(&ssl->dtls13PeerEpoch); |
12304 | | |
12305 | | ret = Dtls13SetEpochKeys(ssl, ssl->dtls13PeerEpoch, DECRYPT_SIDE_ONLY); |
12306 | | if (ret != 0) |
12307 | | return ret; |
12308 | | } |
12309 | | #endif /* WOLFSSL_DTLS13 */ |
12310 | | |
12311 | 0 | if (ssl->keys.keyUpdateRespond) { |
12312 | |
|
12313 | | #ifdef WOLFSSL_DTLS13 |
12314 | | /* we already sent a keyUpdate (either in response to a previous |
12315 | | KeyUpdate or initiated by the application) and we are waiting for the |
12316 | | ack. We can't send a new KeyUpdate right away but to honor the RFC we |
12317 | | should send another KeyUpdate after the one in-flight is acked. We |
12318 | | don't do that as it looks redundant, it will make the code more |
12319 | | complex and I don't see a good use case for that. */ |
12320 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) { |
12321 | | ssl->keys.keyUpdateRespond = 0; |
12322 | | return 0; |
12323 | | } |
12324 | | #endif /* WOLFSSL_DTLS13 */ |
12325 | |
|
12326 | | #if defined(HAVE_WRITE_DUP) && defined(WOLFSSL_TLS13) |
12327 | | /* Read side cannot write; delegate the response to the write side. */ |
12328 | | if (ssl->dupWrite != NULL && ssl->dupSide == READ_DUP_SIDE) { |
12329 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
12330 | | return BAD_MUTEX_E; |
12331 | | ssl->dupWrite->keyUpdateRespond = 1; |
12332 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
12333 | | ssl->keys.keyUpdateRespond = 0; |
12334 | | return 0; |
12335 | | } |
12336 | | #endif /* HAVE_WRITE_DUP && WOLFSSL_TLS13 */ |
12337 | |
|
12338 | 0 | #ifndef WOLFSSL_RW_THREADED |
12339 | 0 | return SendTls13KeyUpdate(ssl); |
12340 | | #else |
12341 | | ssl->options.sendKeyUpdate = 1; |
12342 | | return 0; |
12343 | | #endif |
12344 | 0 | } |
12345 | | |
12346 | 0 | WOLFSSL_LEAVE("DoTls13KeyUpdate", ret); |
12347 | 0 | WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO); |
12348 | |
|
12349 | 0 | return 0; |
12350 | 0 | } |
12351 | | |
12352 | | #ifdef WOLFSSL_EARLY_DATA |
12353 | | #ifndef NO_WOLFSSL_CLIENT |
12354 | | /* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no |
12355 | | * more early application data. |
12356 | | * The encryption key now changes to the pre-calculated handshake key. |
12357 | | * |
12358 | | * ssl The SSL/TLS object. |
12359 | | * returns 0 on success and otherwise failure. |
12360 | | */ |
12361 | | static int SendTls13EndOfEarlyData(WOLFSSL* ssl) |
12362 | | { |
12363 | | byte* output; |
12364 | | int ret; |
12365 | | int sendSz; |
12366 | | word32 length; |
12367 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
12368 | | |
12369 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND); |
12370 | | WOLFSSL_ENTER("SendTls13EndOfEarlyData"); |
12371 | | |
12372 | | length = 0; |
12373 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
12374 | | ssl->options.buildingMsg = 1; |
12375 | | |
12376 | | /* Check buffers are big enough and grow if needed. */ |
12377 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
12378 | | return ret; |
12379 | | |
12380 | | /* Get position in output buffer to write new message to. */ |
12381 | | output = GetOutputBuffer(ssl); |
12382 | | |
12383 | | /* Put the record and handshake headers on. */ |
12384 | | AddTls13Headers(output, length, end_of_early_data, ssl); |
12385 | | |
12386 | | /* This message is always encrypted. */ |
12387 | | sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ, |
12388 | | idx - RECORD_HEADER_SZ, handshake, 1, 0, 0); |
12389 | | if (sendSz < 0) |
12390 | | return sendSz; |
12391 | | |
12392 | | ssl->buffers.outputBuffer.length += sendSz; |
12393 | | |
12394 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
12395 | | return ret; |
12396 | | |
12397 | | ssl->options.buildingMsg = 0; |
12398 | | if (!ssl->options.groupMessages) |
12399 | | ret = SendBuffered(ssl); |
12400 | | |
12401 | | ssl->earlyData = done_early_data; |
12402 | | |
12403 | | WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret); |
12404 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND); |
12405 | | |
12406 | | return ret; |
12407 | | } |
12408 | | #endif /* !NO_WOLFSSL_CLIENT */ |
12409 | | |
12410 | | #ifndef NO_WOLFSSL_SERVER |
12411 | | /* handle processing of TLS 1.3 end_of_early_data (5) */ |
12412 | | /* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be |
12413 | | * no more early application data. |
12414 | | * The decryption key now changes to the pre-calculated handshake key. |
12415 | | * |
12416 | | * ssl The SSL/TLS object. |
12417 | | * returns 0 on success and otherwise failure. |
12418 | | */ |
12419 | | static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input, |
12420 | | word32* inOutIdx, word32 size) |
12421 | | { |
12422 | | int ret; |
12423 | | word32 begin = *inOutIdx; |
12424 | | |
12425 | | (void)input; |
12426 | | |
12427 | | WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO); |
12428 | | WOLFSSL_ENTER("DoTls13EndOfEarlyData"); |
12429 | | |
12430 | | if ((*inOutIdx - begin) != size) |
12431 | | return BUFFER_ERROR; |
12432 | | |
12433 | | if (ssl->earlyData == no_early_data) { |
12434 | | WOLFSSL_MSG("EndOfEarlyData received unexpectedly"); |
12435 | | SendAlert(ssl, alert_fatal, unexpected_message); |
12436 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12437 | | return OUT_OF_ORDER_E; |
12438 | | } |
12439 | | |
12440 | | ssl->earlyData = done_early_data; |
12441 | | |
12442 | | /* Always encrypted. */ |
12443 | | *inOutIdx += ssl->keys.padSz; |
12444 | | |
12445 | | ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY); |
12446 | | |
12447 | | WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret); |
12448 | | WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO); |
12449 | | |
12450 | | return ret; |
12451 | | } |
12452 | | #endif /* !NO_WOLFSSL_SERVER */ |
12453 | | #endif /* WOLFSSL_EARLY_DATA */ |
12454 | | |
12455 | | #if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
12456 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
12457 | | int SessionTicketNoncePopulate(WOLFSSL_SESSION *session, const byte *nonce, |
12458 | | byte len) |
12459 | | { |
12460 | | if (session->ticketNonce.data |
12461 | | != session->ticketNonce.dataStatic) { |
12462 | | XFREE(session->ticketNonce.data, session->heap, |
12463 | | DYNAMIC_TYPE_SESSION_TICK); |
12464 | | session->ticketNonce.data = session->ticketNonce.dataStatic; |
12465 | | session->ticketNonce.len = 0; |
12466 | | } |
12467 | | |
12468 | | if (len > MAX_TICKET_NONCE_STATIC_SZ) { |
12469 | | WOLFSSL_MSG("Using dynamic nonce buffer"); |
12470 | | session->ticketNonce.data = (byte*)XMALLOC(len, |
12471 | | session->heap, DYNAMIC_TYPE_SESSION_TICK); |
12472 | | if (session->ticketNonce.data == NULL) |
12473 | | return MEMORY_ERROR; |
12474 | | } |
12475 | | XMEMCPY(session->ticketNonce.data, nonce, len); |
12476 | | session->ticketNonce.len = len; |
12477 | | return 0; |
12478 | | } |
12479 | | #endif |
12480 | | #ifndef NO_WOLFSSL_CLIENT |
12481 | | /* Handle a New Session Ticket handshake message. |
12482 | | * Message contains the information required to perform resumption. |
12483 | | * |
12484 | | * ssl The SSL/TLS object. |
12485 | | * input The message buffer. |
12486 | | * inOutIdx On entry, the index into the message buffer of Finished. |
12487 | | * On exit, the index of byte after the Finished message and padding. |
12488 | | * size The length of the current handshake message. |
12489 | | * returns 0 on success, otherwise failure. |
12490 | | */ |
12491 | | static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input, |
12492 | | word32* inOutIdx, word32 size) |
12493 | 0 | { |
12494 | | #ifdef HAVE_SESSION_TICKET |
12495 | | int ret; |
12496 | | word32 begin = *inOutIdx; |
12497 | | word32 lifetime; |
12498 | | word32 ageAdd; |
12499 | | word16 length; |
12500 | | #ifdef WOLFSSL_32BIT_MILLI_TIME |
12501 | | word32 now; |
12502 | | #else |
12503 | | sword64 now; |
12504 | | #endif |
12505 | | const byte* nonce; |
12506 | | byte nonceLength; |
12507 | | |
12508 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO); |
12509 | | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
12510 | | |
12511 | | #ifdef HAVE_ECH |
12512 | | /* ignore session ticket when ECH is rejected */ |
12513 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
12514 | | !ssl->options.echAccepted) { |
12515 | | *inOutIdx += size + ssl->keys.padSz; |
12516 | | return 0; |
12517 | | } |
12518 | | #endif |
12519 | | |
12520 | | /* Lifetime hint. */ |
12521 | | if ((*inOutIdx - begin) + SESSION_HINT_SZ > size) |
12522 | | return BUFFER_ERROR; |
12523 | | ato32(input + *inOutIdx, &lifetime); |
12524 | | *inOutIdx += SESSION_HINT_SZ; |
12525 | | if (lifetime > MAX_LIFETIME) { |
12526 | | WOLFSSL_ERROR_VERBOSE(SERVER_HINT_ERROR); |
12527 | | return SERVER_HINT_ERROR; |
12528 | | } |
12529 | | |
12530 | | /* Age add. */ |
12531 | | if ((*inOutIdx - begin) + SESSION_ADD_SZ > size) |
12532 | | return BUFFER_ERROR; |
12533 | | ato32(input + *inOutIdx, &ageAdd); |
12534 | | *inOutIdx += SESSION_ADD_SZ; |
12535 | | |
12536 | | /* Ticket nonce. */ |
12537 | | if ((*inOutIdx - begin) + 1 > size) |
12538 | | return BUFFER_ERROR; |
12539 | | nonceLength = input[*inOutIdx]; |
12540 | | #if !defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
12541 | | (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) |
12542 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
12543 | | WOLFSSL_MSG("Nonce length not supported"); |
12544 | | WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER); |
12545 | | return INVALID_PARAMETER; |
12546 | | } |
12547 | | #endif /* WOLFSSL_TICKET_NONCE_MALLOC && FIPS_VERSION_GE(5,3) */ |
12548 | | *inOutIdx += 1; |
12549 | | if ((*inOutIdx - begin) + nonceLength > size) |
12550 | | return BUFFER_ERROR; |
12551 | | nonce = input + *inOutIdx; |
12552 | | *inOutIdx += nonceLength; |
12553 | | |
12554 | | /* Ticket length. */ |
12555 | | if ((*inOutIdx - begin) + LENGTH_SZ > size) |
12556 | | return BUFFER_ERROR; |
12557 | | ato16(input + *inOutIdx, &length); |
12558 | | *inOutIdx += LENGTH_SZ; |
12559 | | if ((*inOutIdx - begin) + length > size) |
12560 | | return BUFFER_ERROR; |
12561 | | |
12562 | | if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0) |
12563 | | return ret; |
12564 | | *inOutIdx += length; |
12565 | | |
12566 | | now = TimeNowInMilliseconds(); |
12567 | | if (now == 0) |
12568 | | return GETTIME_ERROR; |
12569 | | /* Copy in ticket data (server identity). */ |
12570 | | ssl->timeout = lifetime; |
12571 | | ssl->session->timeout = lifetime; |
12572 | | ssl->session->cipherSuite0 = ssl->options.cipherSuite0; |
12573 | | ssl->session->cipherSuite = ssl->options.cipherSuite; |
12574 | | ssl->session->ticketSeen = now; |
12575 | | ssl->session->ticketAdd = ageAdd; |
12576 | | #ifdef WOLFSSL_EARLY_DATA |
12577 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
12578 | | #endif |
12579 | | |
12580 | | #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \ |
12581 | | (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) |
12582 | | ret = SessionTicketNoncePopulate(ssl->session, nonce, nonceLength); |
12583 | | if (ret != 0) |
12584 | | return ret; |
12585 | | #else |
12586 | | ssl->session->ticketNonce.len = nonceLength; |
12587 | | if (nonceLength > MAX_TICKET_NONCE_STATIC_SZ) { |
12588 | | ret = BUFFER_ERROR; |
12589 | | return ret; |
12590 | | } |
12591 | | if (nonceLength > 0) |
12592 | | XMEMCPY(ssl->session->ticketNonce.data, nonce, nonceLength); |
12593 | | #endif /* defined(WOLFSSL_TICKET_NONCE_MALLOC) && FIPS_VERSION_GE(5,3) */ |
12594 | | |
12595 | | ssl->session->namedGroup = ssl->namedGroup; |
12596 | | |
12597 | | if ((*inOutIdx - begin) + EXTS_SZ > size) |
12598 | | return BUFFER_ERROR; |
12599 | | ato16(input + *inOutIdx, &length); |
12600 | | *inOutIdx += EXTS_SZ; |
12601 | | if ((*inOutIdx - begin) + length != size) |
12602 | | return BUFFER_ERROR; |
12603 | | #ifdef WOLFSSL_EARLY_DATA |
12604 | | ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket, |
12605 | | NULL); |
12606 | | if (ret != 0) |
12607 | | return ret; |
12608 | | #endif |
12609 | | *inOutIdx += length; |
12610 | | |
12611 | | SetupSession(ssl); |
12612 | | #ifndef NO_SESSION_CACHE |
12613 | | AddSession(ssl); |
12614 | | #endif |
12615 | | |
12616 | | /* Always encrypted. */ |
12617 | | *inOutIdx += ssl->keys.padSz; |
12618 | | |
12619 | | ssl->expect_session_ticket = 0; |
12620 | | #else |
12621 | 0 | (void)ssl; |
12622 | 0 | (void)input; |
12623 | |
|
12624 | 0 | WOLFSSL_ENTER("DoTls13NewSessionTicket"); |
12625 | |
|
12626 | 0 | *inOutIdx += size + ssl->keys.padSz; |
12627 | 0 | #endif /* HAVE_SESSION_TICKET */ |
12628 | |
|
12629 | 0 | WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0); |
12630 | 0 | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO); |
12631 | |
|
12632 | 0 | return 0; |
12633 | 0 | } |
12634 | | #endif /* NO_WOLFSSL_CLIENT */ |
12635 | | |
12636 | | #ifndef NO_WOLFSSL_SERVER |
12637 | | #ifdef HAVE_SESSION_TICKET |
12638 | | |
12639 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12640 | | /* Offset of the MAC size in the finished message. */ |
12641 | | #define FINISHED_MSG_SIZE_OFFSET 3 |
12642 | | |
12643 | | /* Calculate the resumption secret which includes the unseen client finished |
12644 | | * message. |
12645 | | * |
12646 | | * ssl The SSL/TLS object. |
12647 | | * returns 0 on success, otherwise failure. |
12648 | | */ |
12649 | | static int ExpectedResumptionSecret(WOLFSSL* ssl) |
12650 | | { |
12651 | | int ret; |
12652 | | word32 finishedSz = 0; |
12653 | | byte mac[WC_MAX_DIGEST_SIZE]; |
12654 | | Digest digest; |
12655 | | byte header[] = { 0x14, 0x00, 0x00, 0x00 }; |
12656 | | |
12657 | | XMEMSET(&digest, 0, sizeof(Digest)); |
12658 | | |
12659 | | /* Copy the running hash so we can restore it after. */ |
12660 | | switch (ssl->specs.mac_algorithm) { |
12661 | | #ifndef NO_SHA256 |
12662 | | case sha256_mac: |
12663 | | ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256); |
12664 | | if (ret != 0) |
12665 | | return ret; |
12666 | | break; |
12667 | | #endif |
12668 | | #ifdef WOLFSSL_SHA384 |
12669 | | case sha384_mac: |
12670 | | ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384); |
12671 | | if (ret != 0) |
12672 | | return ret; |
12673 | | break; |
12674 | | #endif |
12675 | | #ifdef WOLFSSL_TLS13_SHA512 |
12676 | | case sha512_mac: |
12677 | | ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512); |
12678 | | if (ret != 0) |
12679 | | return ret; |
12680 | | break; |
12681 | | #endif |
12682 | | #ifdef WOLFSSL_SM3 |
12683 | | case sm3_mac: |
12684 | | ret = wc_Sm3Copy(&ssl->hsHashes->hashSm3, &digest.sm3); |
12685 | | if (ret != 0) |
12686 | | return ret; |
12687 | | break; |
12688 | | #endif |
12689 | | } |
12690 | | |
12691 | | /* Generate the Client's Finished message and hash it. */ |
12692 | | ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac, |
12693 | | &finishedSz); |
12694 | | if (ret != 0) |
12695 | | goto restore; |
12696 | | header[FINISHED_MSG_SIZE_OFFSET] = finishedSz; |
12697 | | #ifdef WOLFSSL_EARLY_DATA |
12698 | | if (ssl->earlyData != no_early_data) { |
12699 | | static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 }; |
12700 | | ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData)); |
12701 | | if (ret != 0) |
12702 | | goto restore; |
12703 | | } |
12704 | | #endif |
12705 | | if ((ret = HashRaw(ssl, header, sizeof(header))) != 0) |
12706 | | goto restore; |
12707 | | if ((ret = HashRaw(ssl, mac, finishedSz)) != 0) |
12708 | | goto restore; |
12709 | | |
12710 | | if ((ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret)) != 0) |
12711 | | goto restore; |
12712 | | |
12713 | | /* Restore the hash inline with currently seen messages. */ |
12714 | | restore: |
12715 | | switch (ssl->specs.mac_algorithm) { |
12716 | | #ifndef NO_SHA256 |
12717 | | case sha256_mac: |
12718 | | wc_Sha256Free(&ssl->hsHashes->hashSha256); |
12719 | | ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256); |
12720 | | wc_Sha256Free(&digest.sha256); |
12721 | | break; |
12722 | | #endif |
12723 | | #ifdef WOLFSSL_SHA384 |
12724 | | case sha384_mac: |
12725 | | wc_Sha384Free(&ssl->hsHashes->hashSha384); |
12726 | | ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384); |
12727 | | wc_Sha384Free(&digest.sha384); |
12728 | | break; |
12729 | | #endif |
12730 | | #ifdef WOLFSSL_TLS13_SHA512 |
12731 | | case sha512_mac: |
12732 | | wc_Sha512Free(&ssl->hsHashes->hashSha512); |
12733 | | ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha512); |
12734 | | wc_Sha512Free(&digest.sha512); |
12735 | | break; |
12736 | | #endif |
12737 | | #ifdef WOLFSSL_SM3 |
12738 | | case sm3_mac: |
12739 | | wc_Sm3Free(&ssl->hsHashes->hashSm3); |
12740 | | ret = wc_Sm3Copy(&digest.sm3, &ssl->hsHashes->hashSm3); |
12741 | | wc_Sm3Free(&digest.sm3); |
12742 | | break; |
12743 | | #endif |
12744 | | } |
12745 | | |
12746 | | ForceZero(mac, sizeof(mac)); |
12747 | | return ret; |
12748 | | } |
12749 | | #endif |
12750 | | |
12751 | | /* Send New Session Ticket handshake message. |
12752 | | * Message contains the information required to perform resumption. |
12753 | | * |
12754 | | * ssl The SSL/TLS object. |
12755 | | * returns 0 on success, otherwise failure. |
12756 | | */ |
12757 | | static int SendTls13NewSessionTicket(WOLFSSL* ssl) |
12758 | | { |
12759 | | byte* output; |
12760 | | int ret; |
12761 | | word32 length; |
12762 | | int sendSz; |
12763 | | word16 extSz; |
12764 | | word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ; |
12765 | | |
12766 | | WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12767 | | WOLFSSL_ENTER("SendTls13NewSessionTicket"); |
12768 | | |
12769 | | #ifdef WOLFSSL_DTLS13 |
12770 | | if (ssl->options.dtls) |
12771 | | idx = Dtls13GetRlHeaderLength(ssl, 1) + DTLS_HANDSHAKE_HEADER_SZ; |
12772 | | #endif /* WOLFSSL_DTLS13 */ |
12773 | | |
12774 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
12775 | | if (!ssl->msgsReceived.got_finished) { |
12776 | | if ((ret = ExpectedResumptionSecret(ssl)) != 0) |
12777 | | return ret; |
12778 | | } |
12779 | | #endif |
12780 | | |
12781 | | /* Start ticket nonce at 0 and go up to 255. */ |
12782 | | if (ssl->session->ticketNonce.len == 0) { |
12783 | | ssl->session->ticketNonce.len = DEF_TICKET_NONCE_SZ; |
12784 | | ssl->session->ticketNonce.data[0] = 0; |
12785 | | } |
12786 | | else |
12787 | | #ifdef WOLFSSL_ASYNC_CRYPT |
12788 | | if (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E)) |
12789 | | #endif |
12790 | | { |
12791 | | if (ssl->session->ticketNonce.data[0] == 255) { |
12792 | | /* RFC8446 Section 4.6.1: Each ticket must have a unique nonce |
12793 | | * value. As the nonce is only a single byte, we have to prevent |
12794 | | * the overflow and abort. */ |
12795 | | return SESSION_TICKET_NONCE_OVERFLOW; |
12796 | | } |
12797 | | else |
12798 | | ssl->session->ticketNonce.data[0]++; |
12799 | | } |
12800 | | |
12801 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12802 | | /* In this case we only send the ID as the ticket. Let's generate a new |
12803 | | * ID for the new ticket so that we don't overwrite any old ones */ |
12804 | | ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID, |
12805 | | ID_LEN); |
12806 | | if (ret != 0) |
12807 | | return ret; |
12808 | | ssl->session->haveAltSessionID = 1; |
12809 | | } |
12810 | | |
12811 | | if (!ssl->options.noTicketTls13) { |
12812 | | if ((ret = SetupTicket(ssl)) != 0) |
12813 | | return ret; |
12814 | | /* No need to create the ticket if we only send the ID */ |
12815 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == 0) { |
12816 | | if ((ret = CreateTicket(ssl)) != 0) |
12817 | | return ret; |
12818 | | } |
12819 | | } |
12820 | | |
12821 | | #ifdef WOLFSSL_EARLY_DATA |
12822 | | ssl->session->maxEarlyDataSz = ssl->options.maxEarlyDataSz; |
12823 | | if (ssl->session->maxEarlyDataSz > 0) |
12824 | | TLSX_EarlyData_Use(ssl, ssl->session->maxEarlyDataSz, 1); |
12825 | | extSz = 0; |
12826 | | ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz); |
12827 | | if (ret != 0) |
12828 | | return ret; |
12829 | | #else |
12830 | | extSz = EXTS_SZ; |
12831 | | #endif |
12832 | | /* Lifetime | Age Add | Ticket session ID | Extensions */ |
12833 | | length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ; |
12834 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) |
12835 | | length += ID_LEN + extSz; |
12836 | | else |
12837 | | length += ssl->session->ticketLen + extSz; |
12838 | | /* Nonce */ |
12839 | | length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ; |
12840 | | |
12841 | | sendSz = (int)(idx + length + MAX_MSG_EXTRA); |
12842 | | |
12843 | | /* Check buffers are big enough and grow if needed. */ |
12844 | | if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) |
12845 | | return ret; |
12846 | | |
12847 | | /* Get position in output buffer to write new message to. */ |
12848 | | output = GetOutputBuffer(ssl); |
12849 | | |
12850 | | /* Put the record and handshake headers on. */ |
12851 | | AddTls13Headers(output, length, session_ticket, ssl); |
12852 | | |
12853 | | /* Lifetime hint */ |
12854 | | c32toa(ssl->ctx->ticketHint, output + idx); |
12855 | | idx += SESSION_HINT_SZ; |
12856 | | /* Age add - obfuscator */ |
12857 | | c32toa(ssl->session->ticketAdd, output + idx); |
12858 | | idx += SESSION_ADD_SZ; |
12859 | | |
12860 | | output[idx++] = ssl->session->ticketNonce.len; |
12861 | | output[idx++] = ssl->session->ticketNonce.data[0]; |
12862 | | |
12863 | | /* length */ |
12864 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12865 | | c16toa(ID_LEN, output + idx); |
12866 | | } |
12867 | | else { |
12868 | | c16toa(ssl->session->ticketLen, output + idx); |
12869 | | } |
12870 | | |
12871 | | idx += LENGTH_SZ; |
12872 | | /* ticket */ |
12873 | | if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) { |
12874 | | if (ssl->session->haveAltSessionID) |
12875 | | XMEMCPY(output + idx, ssl->session->altSessionID, ID_LEN); |
12876 | | else |
12877 | | return BAD_FUNC_ARG; /* Should not happen */ |
12878 | | idx += ID_LEN; |
12879 | | } |
12880 | | else { |
12881 | | XMEMCPY(output + idx, ssl->session->ticket, ssl->session->ticketLen); |
12882 | | idx += ssl->session->ticketLen; |
12883 | | } |
12884 | | |
12885 | | #ifdef WOLFSSL_EARLY_DATA |
12886 | | extSz = 0; |
12887 | | ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz); |
12888 | | if (ret != 0) |
12889 | | return ret; |
12890 | | idx += extSz; |
12891 | | #else |
12892 | | /* No extension support - empty extensions. */ |
12893 | | c16toa(0, output + idx); |
12894 | | idx += EXTS_SZ; |
12895 | | #endif |
12896 | | |
12897 | | if (idx > WOLFSSL_MAX_16BIT || |
12898 | | sendSz > (int)WOLFSSL_MAX_16BIT) { |
12899 | | return BAD_LENGTH_E; |
12900 | | } |
12901 | | |
12902 | | ssl->options.haveSessionId = 1; |
12903 | | |
12904 | | SetupSession(ssl); |
12905 | | /* Only add to cache when support built in and when the ticket contains |
12906 | | * an ID. Otherwise we have no way to actually retrieve the ticket from the |
12907 | | * cache. */ |
12908 | | #if !defined(NO_SESSION_CACHE) && defined(WOLFSSL_TICKET_HAVE_ID) |
12909 | | AddSession(ssl); |
12910 | | #endif |
12911 | | |
12912 | | #ifdef WOLFSSL_DTLS13 |
12913 | | if (ssl->options.dtls) |
12914 | | return Dtls13HandshakeSend(ssl, output, (word16)sendSz, |
12915 | | (word16)idx, session_ticket, 0); |
12916 | | #endif /* WOLFSSL_DTLS13 */ |
12917 | | |
12918 | | /* This message is always encrypted. */ |
12919 | | sendSz = BuildTls13Message(ssl, output, sendSz, |
12920 | | output + RECORD_HEADER_SZ, |
12921 | | (word16)idx - RECORD_HEADER_SZ, |
12922 | | handshake, 0, 0, 0); |
12923 | | if (sendSz < 0) |
12924 | | return sendSz; |
12925 | | |
12926 | | ssl->buffers.outputBuffer.length += sendSz; |
12927 | | |
12928 | | /* Always send as this is either directly after server's Finished or only |
12929 | | * message after client's Finished. |
12930 | | */ |
12931 | | ret = SendBuffered(ssl); |
12932 | | |
12933 | | WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0); |
12934 | | WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND); |
12935 | | |
12936 | | return ret; |
12937 | | } |
12938 | | #endif /* HAVE_SESSION_TICKET */ |
12939 | | #endif /* NO_WOLFSSL_SERVER */ |
12940 | | |
12941 | | /* Make sure no duplicates, no fast forward, or other problems |
12942 | | * |
12943 | | * ssl The SSL/TLS object. |
12944 | | * type Type of handshake message received. |
12945 | | * returns 0 on success, otherwise failure. |
12946 | | */ |
12947 | | static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type) |
12948 | 0 | { |
12949 | | /* verify not a duplicate, mark received, check state */ |
12950 | 0 | switch (type) { |
12951 | | |
12952 | 0 | #ifndef NO_WOLFSSL_SERVER |
12953 | 0 | case client_hello: |
12954 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12955 | | /* Only valid when received on SERVER side. */ |
12956 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
12957 | 0 | WOLFSSL_MSG("ClientHello received by client"); |
12958 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12959 | 0 | return SIDE_ERROR; |
12960 | 0 | } |
12961 | 0 | #endif |
12962 | | /* Check state. */ |
12963 | 0 | if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) { |
12964 | 0 | WOLFSSL_MSG("ClientHello received out of order"); |
12965 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
12966 | 0 | return OUT_OF_ORDER_E; |
12967 | 0 | } |
12968 | | /* Check previously seen. */ |
12969 | | /* Initial and after HelloRetryRequest - no more than 2. */ |
12970 | 0 | if (ssl->msgsReceived.got_client_hello == 2) { |
12971 | 0 | WOLFSSL_MSG("Too many ClientHello received"); |
12972 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12973 | 0 | return DUPLICATE_MSG_E; |
12974 | 0 | } |
12975 | | /* Second only after HelloRetryRequest seen. */ |
12976 | 0 | if (ssl->msgsReceived.got_client_hello == 1 && |
12977 | 0 | ssl->options.serverState != |
12978 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
12979 | 0 | WOLFSSL_MSG("Duplicate ClientHello received"); |
12980 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
12981 | 0 | return DUPLICATE_MSG_E; |
12982 | 0 | } |
12983 | 0 | ssl->msgsReceived.got_client_hello++; |
12984 | |
|
12985 | 0 | break; |
12986 | 0 | #endif |
12987 | | |
12988 | 0 | #ifndef NO_WOLFSSL_CLIENT |
12989 | 0 | case server_hello: |
12990 | 0 | #ifndef NO_WOLFSSL_SERVER |
12991 | | /* Only valid when received on CLIENT side. */ |
12992 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
12993 | 0 | WOLFSSL_MSG("ServerHello received by server"); |
12994 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
12995 | 0 | return SIDE_ERROR; |
12996 | 0 | } |
12997 | 0 | #endif |
12998 | | /* Check state. */ |
12999 | 0 | if (ssl->options.serverState >= SERVER_HELLO_COMPLETE) { |
13000 | 0 | WOLFSSL_MSG("ServerHello received out of order"); |
13001 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13002 | 0 | return OUT_OF_ORDER_E; |
13003 | 0 | } |
13004 | | /* Check previously seen. */ |
13005 | | /* Only once after ClientHello. |
13006 | | * HelloRetryRequest has ServerHello type but count fixed up later |
13007 | | * - see DoTls13ServerHello(). |
13008 | | */ |
13009 | 0 | if (ssl->msgsReceived.got_server_hello) { |
13010 | 0 | WOLFSSL_MSG("Duplicate ServerHello received"); |
13011 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13012 | 0 | return DUPLICATE_MSG_E; |
13013 | 0 | } |
13014 | 0 | ssl->msgsReceived.got_server_hello = 1; |
13015 | |
|
13016 | 0 | break; |
13017 | 0 | #endif |
13018 | | |
13019 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13020 | 0 | case session_ticket: |
13021 | 0 | #ifndef NO_WOLFSSL_SERVER |
13022 | | /* Only valid when received on CLIENT side. */ |
13023 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13024 | 0 | WOLFSSL_MSG("NewSessionTicket received by server"); |
13025 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
13026 | 0 | return SIDE_ERROR; |
13027 | 0 | } |
13028 | 0 | #endif |
13029 | | /* Check state. */ |
13030 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
13031 | | /* Only allowed after server's Finished message. */ |
13032 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13033 | | WOLFSSL_MSG("NewSessionTicket received out of order"); |
13034 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13035 | | return OUT_OF_ORDER_E; |
13036 | | } |
13037 | | #else |
13038 | | /* Only allowed after client's Finished message. */ |
13039 | 0 | if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
13040 | 0 | WOLFSSL_MSG("NewSessionTicket received out of order"); |
13041 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13042 | 0 | return OUT_OF_ORDER_E; |
13043 | 0 | } |
13044 | 0 | #endif |
13045 | | /* Many SessionTickets can be sent. */ |
13046 | 0 | ssl->msgsReceived.got_session_ticket = 1; |
13047 | |
|
13048 | 0 | break; |
13049 | 0 | #endif |
13050 | | |
13051 | 0 | #ifndef NO_WOLFSSL_SERVER |
13052 | | #ifdef WOLFSSL_EARLY_DATA |
13053 | | case end_of_early_data: |
13054 | | #ifndef NO_WOLFSSL_CLIENT |
13055 | | /* Only valid when received on SERVER side. */ |
13056 | | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13057 | | WOLFSSL_MSG("EndOfEarlyData received by client"); |
13058 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
13059 | | return SIDE_ERROR; |
13060 | | } |
13061 | | #endif |
13062 | | /* Check state. */ |
13063 | | /* Only after server's Finished and before client's Finished. */ |
13064 | | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13065 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
13066 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13067 | | return OUT_OF_ORDER_E; |
13068 | | } |
13069 | | if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) { |
13070 | | WOLFSSL_MSG("EndOfEarlyData received out of order"); |
13071 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13072 | | return OUT_OF_ORDER_E; |
13073 | | } |
13074 | | /* Check previously seen. */ |
13075 | | if (ssl->msgsReceived.got_end_of_early_data) { |
13076 | | WOLFSSL_MSG("Too many EndOfEarlyData received"); |
13077 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13078 | | return DUPLICATE_MSG_E; |
13079 | | } |
13080 | | ssl->msgsReceived.got_end_of_early_data = 1; |
13081 | | |
13082 | | break; |
13083 | | #endif |
13084 | 0 | #endif |
13085 | | |
13086 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13087 | 0 | case encrypted_extensions: |
13088 | 0 | #ifndef NO_WOLFSSL_SERVER |
13089 | | /* Only valid when received on CLIENT side. */ |
13090 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13091 | 0 | WOLFSSL_MSG("EncryptedExtensions received by server"); |
13092 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
13093 | 0 | return SIDE_ERROR; |
13094 | 0 | } |
13095 | 0 | #endif |
13096 | | /* Check state. */ |
13097 | | /* Must be received directly after ServerHello. |
13098 | | * DoTls13EncryptedExtensions() changes state to: |
13099 | | * SERVER_ENCRYPTED_EXTENSIONS_COMPLETE. |
13100 | | */ |
13101 | 0 | if (ssl->options.serverState != SERVER_HELLO_COMPLETE) { |
13102 | 0 | WOLFSSL_MSG("EncryptedExtensions received out of order"); |
13103 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13104 | 0 | return OUT_OF_ORDER_E; |
13105 | 0 | } |
13106 | | /* Check previously seen. */ |
13107 | 0 | if (ssl->msgsReceived.got_encrypted_extensions) { |
13108 | 0 | WOLFSSL_MSG("Duplicate EncryptedExtensions received"); |
13109 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13110 | 0 | return DUPLICATE_MSG_E; |
13111 | 0 | } |
13112 | 0 | ssl->msgsReceived.got_encrypted_extensions = 1; |
13113 | |
|
13114 | 0 | break; |
13115 | 0 | #endif |
13116 | | |
13117 | 0 | case certificate: |
13118 | | /* Valid on both sides. */ |
13119 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13120 | | /* Check state. */ |
13121 | | /* On client, seen after EncryptedExtension and CertificateRequest |
13122 | | * (if sent) and before CertificateVerify and Finished. |
13123 | | * DoTls13Certificate() sets serverState to SERVER_CERT_COMPLETE. |
13124 | | */ |
13125 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
13126 | 0 | ssl->options.serverState != |
13127 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
13128 | 0 | WOLFSSL_MSG("Certificate received out of order - Client"); |
13129 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13130 | 0 | return OUT_OF_ORDER_E; |
13131 | 0 | } |
13132 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13133 | | /* Server's authenticating with PSK must not send this. */ |
13134 | | if (ssl->options.side == WOLFSSL_CLIENT_END && |
13135 | | ssl->options.serverState == SERVER_CERT_COMPLETE && |
13136 | | ssl->options.pskNegotiated |
13137 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
13138 | | && !ssl->options.certWithExternPsk |
13139 | | #endif |
13140 | | ) { |
13141 | | WOLFSSL_MSG("Certificate received while using PSK"); |
13142 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
13143 | | return SANITY_MSG_E; |
13144 | | } |
13145 | | #endif |
13146 | 0 | #endif |
13147 | 0 | #ifndef NO_WOLFSSL_SERVER |
13148 | | /* Check state. */ |
13149 | | /* On Server, valid after ClientHello received and ServerFinished |
13150 | | * sent. */ |
13151 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
13152 | 0 | ssl->options.clientState != CLIENT_HELLO_COMPLETE && |
13153 | 0 | ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13154 | 0 | WOLFSSL_MSG("Certificate received out of order - Server"); |
13155 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13156 | 0 | return OUT_OF_ORDER_E; |
13157 | 0 | } |
13158 | 0 | #endif |
13159 | | /* Check previously seen. */ |
13160 | 0 | if (ssl->msgsReceived.got_certificate) { |
13161 | 0 | WOLFSSL_MSG("Duplicate Certificate received"); |
13162 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13163 | 0 | return DUPLICATE_MSG_E; |
13164 | 0 | } |
13165 | 0 | ssl->msgsReceived.got_certificate = 1; |
13166 | |
|
13167 | 0 | break; |
13168 | | |
13169 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13170 | 0 | case certificate_request: |
13171 | 0 | #ifndef NO_WOLFSSL_SERVER |
13172 | | /* Only valid when received on CLIENT side. */ |
13173 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13174 | 0 | WOLFSSL_MSG("CertificateRequest received by server"); |
13175 | 0 | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
13176 | 0 | return SIDE_ERROR; |
13177 | 0 | } |
13178 | 0 | #endif |
13179 | | /* Check state. */ |
13180 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
13181 | | /* Only valid when sent after EncryptedExtensions and before |
13182 | | * Certificate. */ |
13183 | 0 | if (ssl->options.serverState != |
13184 | 0 | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
13185 | 0 | WOLFSSL_MSG("CertificateRequest received out of order"); |
13186 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13187 | 0 | return OUT_OF_ORDER_E; |
13188 | 0 | } |
13189 | | #else |
13190 | | /* Valid when sent after EncryptedExtensions and before Certificate |
13191 | | * and after both client and server have sent Finished (Post |
13192 | | * Handshake Authentication). */ |
13193 | | if (ssl->options.serverState != |
13194 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE && |
13195 | | (ssl->options.serverState < SERVER_FINISHED_COMPLETE || |
13196 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) { |
13197 | | WOLFSSL_MSG("CertificateRequest received out of order"); |
13198 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13199 | | return OUT_OF_ORDER_E; |
13200 | | } |
13201 | | #endif |
13202 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13203 | | /* Server's authenticating with PSK must not send this. */ |
13204 | | if (ssl->options.pskNegotiated |
13205 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
13206 | | && !ssl->options.certWithExternPsk |
13207 | | #endif |
13208 | | ) { |
13209 | | WOLFSSL_MSG("CertificateRequest received while using PSK"); |
13210 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
13211 | | return SANITY_MSG_E; |
13212 | | } |
13213 | | #endif |
13214 | | /* Check previously seen. */ |
13215 | 0 | #ifndef WOLFSSL_POST_HANDSHAKE_AUTH |
13216 | | /* Only once during handshake. */ |
13217 | 0 | if (ssl->msgsReceived.got_certificate_request) { |
13218 | 0 | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
13219 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13220 | 0 | return DUPLICATE_MSG_E; |
13221 | 0 | } |
13222 | | #else |
13223 | | /* Only once during handshake. */ |
13224 | | if (ssl->msgsReceived.got_certificate_request && |
13225 | | ssl->options.clientState != CLIENT_FINISHED_COMPLETE) { |
13226 | | WOLFSSL_MSG("Duplicate CertificateRequest received"); |
13227 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13228 | | return DUPLICATE_MSG_E; |
13229 | | } |
13230 | | #endif |
13231 | 0 | ssl->msgsReceived.got_certificate_request = 1; |
13232 | |
|
13233 | 0 | break; |
13234 | 0 | #endif |
13235 | | |
13236 | 0 | case certificate_verify: |
13237 | | /* Valid on both sides. */ |
13238 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13239 | | /* Check state on client. |
13240 | | * Valid only directly after a Certificate message. */ |
13241 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13242 | 0 | if (ssl->options.serverState != SERVER_CERT_COMPLETE) { |
13243 | 0 | WOLFSSL_MSG("No Cert before CertVerify"); |
13244 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13245 | 0 | return OUT_OF_ORDER_E; |
13246 | 0 | } |
13247 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13248 | | /* Server's authenticating with PSK must not send this. */ |
13249 | | if (ssl->options.pskNegotiated |
13250 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
13251 | | && !ssl->options.certWithExternPsk |
13252 | | #endif |
13253 | | ) { |
13254 | | WOLFSSL_MSG("CertificateVerify received while using PSK"); |
13255 | | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
13256 | | return SANITY_MSG_E; |
13257 | | } |
13258 | | #endif |
13259 | 0 | } |
13260 | 0 | #endif |
13261 | 0 | #ifndef NO_WOLFSSL_SERVER |
13262 | | /* Check state on server. */ |
13263 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13264 | | /* Server must have sent Finished message. */ |
13265 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13266 | 0 | WOLFSSL_MSG("CertificateVerify received out of order"); |
13267 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13268 | 0 | return OUT_OF_ORDER_E; |
13269 | 0 | } |
13270 | | /* Valid only directly after a Certificate message. */ |
13271 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
13272 | 0 | WOLFSSL_MSG("CertificateVerify before ClientHello done"); |
13273 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13274 | 0 | return OUT_OF_ORDER_E; |
13275 | 0 | } |
13276 | 0 | if (!ssl->msgsReceived.got_certificate) { |
13277 | 0 | WOLFSSL_MSG("No Cert before CertificateVerify"); |
13278 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13279 | 0 | return OUT_OF_ORDER_E; |
13280 | 0 | } |
13281 | 0 | } |
13282 | 0 | #endif |
13283 | | /* Check previously seen. */ |
13284 | 0 | if (ssl->msgsReceived.got_certificate_verify) { |
13285 | 0 | WOLFSSL_MSG("Duplicate CertificateVerify received"); |
13286 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13287 | 0 | return DUPLICATE_MSG_E; |
13288 | 0 | } |
13289 | 0 | ssl->msgsReceived.got_certificate_verify = 1; |
13290 | |
|
13291 | 0 | break; |
13292 | | |
13293 | 0 | case finished: |
13294 | | /* Valid on both sides. */ |
13295 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13296 | | /* Check state on client. */ |
13297 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13298 | | /* After sending ClientHello */ |
13299 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
13300 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
13301 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13302 | 0 | return OUT_OF_ORDER_E; |
13303 | 0 | } |
13304 | | /* Must have seen certificate and verify from server except when |
13305 | | * using PSK. */ |
13306 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13307 | | if (ssl->options.pskNegotiated) { |
13308 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
13309 | | if (ssl->options.certWithExternPsk) { |
13310 | | if (ssl->options.serverState != |
13311 | | SERVER_CERT_VERIFY_COMPLETE) { |
13312 | | WOLFSSL_MSG("Finished received out of order - " |
13313 | | "cert_with_extern_psk"); |
13314 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13315 | | return OUT_OF_ORDER_E; |
13316 | | } |
13317 | | } |
13318 | | else |
13319 | | #endif |
13320 | | { |
13321 | | if (ssl->options.serverState != |
13322 | | SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) { |
13323 | | WOLFSSL_MSG("Finished received out of order - PSK"); |
13324 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13325 | | return OUT_OF_ORDER_E; |
13326 | | } |
13327 | | } |
13328 | | } |
13329 | | else |
13330 | | #endif |
13331 | 0 | if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) { |
13332 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
13333 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13334 | 0 | return OUT_OF_ORDER_E; |
13335 | 0 | } |
13336 | 0 | } |
13337 | 0 | #endif |
13338 | 0 | #ifndef NO_WOLFSSL_SERVER |
13339 | | /* Check state on server. */ |
13340 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13341 | 0 | if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
13342 | 0 | WOLFSSL_MSG("Finished received out of order - serverState"); |
13343 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13344 | 0 | return OUT_OF_ORDER_E; |
13345 | 0 | } |
13346 | 0 | if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
13347 | 0 | WOLFSSL_MSG("Finished received out of order - clientState"); |
13348 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13349 | 0 | return OUT_OF_ORDER_E; |
13350 | 0 | } |
13351 | | #ifdef WOLFSSL_EARLY_DATA |
13352 | | if (ssl->earlyData == process_early_data && |
13353 | | /* early data may be lost when using DTLS */ |
13354 | | !ssl->options.dtls |
13355 | | /* QUIC does not use EndOfEarlyData records */ |
13356 | | && !WOLFSSL_IS_QUIC(ssl)) { |
13357 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13358 | | return OUT_OF_ORDER_E; |
13359 | | } |
13360 | | #endif |
13361 | 0 | } |
13362 | 0 | #endif |
13363 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
13364 | | if (!ssl->options.pskNegotiated |
13365 | | #ifdef WOLFSSL_CERT_WITH_EXTERN_PSK |
13366 | | || ssl->options.certWithExternPsk |
13367 | | #endif |
13368 | | ) |
13369 | | #endif |
13370 | 0 | { |
13371 | | /* Must have received a Certificate message from client if |
13372 | | * verifying the peer. Empty certificate message indicates |
13373 | | * no certificate available. |
13374 | | */ |
13375 | 0 | if (ssl->options.verifyPeer && |
13376 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
13377 | | !ssl->options.verifyPostHandshake && |
13378 | | #endif |
13379 | 0 | !ssl->msgsReceived.got_certificate) { |
13380 | 0 | WOLFSSL_MSG("Finished received out of order - " |
13381 | 0 | "missing Certificate message"); |
13382 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13383 | 0 | return OUT_OF_ORDER_E; |
13384 | 0 | } |
13385 | | /* Mutual authentication on server requires a certificate from |
13386 | | * peer. Verify peer set on client side requires a certificate |
13387 | | * from peer as not doing PSK. |
13388 | | */ |
13389 | 0 | if ((ssl->options.mutualAuth || |
13390 | 0 | (ssl->options.side == WOLFSSL_CLIENT_END && |
13391 | 0 | ssl->options.verifyPeer)) && !ssl->options.havePeerCert) { |
13392 | 0 | WOLFSSL_MSG("Finished received out of order - " |
13393 | 0 | "no valid certificate"); |
13394 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13395 | 0 | return OUT_OF_ORDER_E; |
13396 | 0 | } |
13397 | | /* Must have received a valid CertificateVerify if verifying |
13398 | | * peer and got a peer certificate. |
13399 | | */ |
13400 | 0 | if ((ssl->options.mutualAuth || ssl->options.verifyPeer) && |
13401 | 0 | ssl->options.havePeerCert && !ssl->options.havePeerVerify) { |
13402 | 0 | WOLFSSL_MSG("Finished received out of order - " |
13403 | 0 | "Certificate message but no CertificateVerify"); |
13404 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13405 | 0 | return OUT_OF_ORDER_E; |
13406 | 0 | } |
13407 | 0 | } |
13408 | | /* Check previously seen. */ |
13409 | 0 | if (ssl->msgsReceived.got_finished) { |
13410 | 0 | WOLFSSL_MSG("Duplicate Finished received"); |
13411 | 0 | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13412 | 0 | return DUPLICATE_MSG_E; |
13413 | 0 | } |
13414 | 0 | ssl->msgsReceived.got_finished = 1; |
13415 | |
|
13416 | 0 | break; |
13417 | | |
13418 | 0 | case key_update: |
13419 | | /* Valid on both sides. */ |
13420 | | /* Check state. |
13421 | | * Client and server must have received finished message from other |
13422 | | * side. |
13423 | | */ |
13424 | 0 | if (!ssl->msgsReceived.got_finished) { |
13425 | 0 | WOLFSSL_MSG("No KeyUpdate before Finished"); |
13426 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13427 | 0 | return OUT_OF_ORDER_E; |
13428 | 0 | } |
13429 | | /* Multiple KeyUpdates can be sent. */ |
13430 | 0 | break; |
13431 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
13432 | | case hello_verify_request: |
13433 | | if (!ssl->options.dtls) { |
13434 | | WOLFSSL_MSG("HelloVerifyRequest when not in DTLS"); |
13435 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13436 | | return OUT_OF_ORDER_E; |
13437 | | } |
13438 | | if (ssl->msgsReceived.got_hello_verify_request) { |
13439 | | WOLFSSL_MSG("Duplicate HelloVerifyRequest received"); |
13440 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13441 | | return DUPLICATE_MSG_E; |
13442 | | } |
13443 | | ssl->msgsReceived.got_hello_verify_request = 1; |
13444 | | if (ssl->msgsReceived.got_hello_retry_request) { |
13445 | | WOLFSSL_MSG( |
13446 | | "Both HelloVerifyRequest and HelloRetryRequest received"); |
13447 | | WOLFSSL_ERROR_VERBOSE(DUPLICATE_MSG_E); |
13448 | | return DUPLICATE_MSG_E; |
13449 | | } |
13450 | | if (ssl->options.serverState >= |
13451 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE || |
13452 | | ssl->options.connectState != CLIENT_HELLO_SENT) { |
13453 | | WOLFSSL_MSG("HelloVerifyRequest received out of order"); |
13454 | | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13455 | | return OUT_OF_ORDER_E; |
13456 | | } |
13457 | | if (ssl->options.side == WOLFSSL_SERVER_END) { |
13458 | | WOLFSSL_MSG("HelloVerifyRequest received on the server"); |
13459 | | WOLFSSL_ERROR_VERBOSE(SIDE_ERROR); |
13460 | | return SIDE_ERROR; |
13461 | | } |
13462 | | if (!ssl->options.downgrade || |
13463 | | ssl->options.minDowngrade < DTLSv1_2_MINOR) { |
13464 | | WOLFSSL_MSG( |
13465 | | "HelloVerifyRequest received but not DTLSv1.2 allowed"); |
13466 | | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
13467 | | return VERSION_ERROR; |
13468 | | } |
13469 | | break; |
13470 | | #endif /* WOLFSSL_DTLS13 && !WOLFSSL_NO_TLS12*/ |
13471 | | |
13472 | 0 | default: |
13473 | 0 | WOLFSSL_MSG("Unknown message type"); |
13474 | 0 | WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E); |
13475 | 0 | return SANITY_MSG_E; |
13476 | 0 | } |
13477 | | |
13478 | 0 | return 0; |
13479 | 0 | } |
13480 | | |
13481 | | /* Handle a type of handshake message that has been received. |
13482 | | * |
13483 | | * ssl The SSL/TLS object. |
13484 | | * input The message buffer. |
13485 | | * inOutIdx On entry, the index into the buffer of the current message. |
13486 | | * On exit, the index into the buffer of the next message. |
13487 | | * size The length of the current handshake message. |
13488 | | * totalSz Length of remaining data in the message buffer. |
13489 | | * returns 0 on success and otherwise failure. |
13490 | | */ |
13491 | | int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
13492 | | byte type, word32 size, word32 totalSz) |
13493 | 0 | { |
13494 | 0 | int ret = 0, tmp; |
13495 | 0 | word32 inIdx = *inOutIdx; |
13496 | 0 | int alertType; |
13497 | | #if defined(HAVE_ECH) && !defined(NO_WOLFSSL_SERVER) |
13498 | | TLSX* echX = NULL; |
13499 | | word32 echInOutIdx; |
13500 | | #endif |
13501 | |
|
13502 | 0 | (void)totalSz; |
13503 | |
|
13504 | 0 | WOLFSSL_ENTER("DoTls13HandShakeMsgType"); |
13505 | | |
13506 | | /* make sure we can read the message */ |
13507 | 0 | if (*inOutIdx + size > totalSz) |
13508 | 0 | return INCOMPLETE_DATA; |
13509 | | |
13510 | | /* sanity check msg received */ |
13511 | 0 | if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) { |
13512 | 0 | WOLFSSL_MSG("Sanity Check on handshake message type received failed"); |
13513 | 0 | if (ret == WC_NO_ERR_TRACE(VERSION_ERROR)) |
13514 | 0 | SendAlert(ssl, alert_fatal, wolfssl_alert_protocol_version); |
13515 | 0 | else |
13516 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13517 | 0 | return ret; |
13518 | 0 | } |
13519 | | |
13520 | | #if defined(WOLFSSL_CALLBACKS) |
13521 | | /* add name later, add on record and handshake header part back on */ |
13522 | | if (ssl->toInfoOn) { |
13523 | | ret = AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - |
13524 | | HANDSHAKE_HEADER_SZ, size + HANDSHAKE_HEADER_SZ, READ_PROTO, |
13525 | | RECORD_HEADER_SZ, ssl->heap); |
13526 | | if (ret != 0) |
13527 | | return ret; |
13528 | | AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo); |
13529 | | } |
13530 | | #endif |
13531 | | |
13532 | 0 | if (ssl->options.handShakeState == HANDSHAKE_DONE && |
13533 | 0 | type != session_ticket && type != certificate_request && |
13534 | 0 | type != certificate && type != key_update && type != finished) { |
13535 | 0 | WOLFSSL_MSG("HandShake message after handshake complete"); |
13536 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13537 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13538 | 0 | return OUT_OF_ORDER_E; |
13539 | 0 | } |
13540 | | |
13541 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END && |
13542 | 0 | ssl->options.serverState == NULL_STATE && |
13543 | 0 | type != server_hello && type != hello_retry_request |
13544 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) |
13545 | | && (!ssl->options.dtls || type != hello_verify_request) |
13546 | | #endif /* defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) */ |
13547 | 0 | ) { |
13548 | 0 | WOLFSSL_MSG("First server message not server hello"); |
13549 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13550 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13551 | 0 | return OUT_OF_ORDER_E; |
13552 | 0 | } |
13553 | | |
13554 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END && |
13555 | 0 | ssl->options.clientState == NULL_STATE && type != client_hello) { |
13556 | 0 | WOLFSSL_MSG("First client message not client hello"); |
13557 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13558 | 0 | WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E); |
13559 | 0 | return OUT_OF_ORDER_E; |
13560 | 0 | } |
13561 | | |
13562 | | /* above checks handshake state */ |
13563 | 0 | switch (type) { |
13564 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13565 | | /* Messages only received by client. */ |
13566 | 0 | case server_hello: |
13567 | 0 | WOLFSSL_MSG("processing server hello"); |
13568 | 0 | ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type); |
13569 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
13570 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
13571 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
13572 | | if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) || |
13573 | | IsAtLeastTLSv1_3(ssl->version)) { |
13574 | | ssl->options.cacheMessages = 0; |
13575 | | if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) { |
13576 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
13577 | | XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES); |
13578 | | ssl->hsHashes->messages = NULL; |
13579 | | } |
13580 | | } |
13581 | | #endif |
13582 | 0 | break; |
13583 | | |
13584 | 0 | case encrypted_extensions: |
13585 | 0 | WOLFSSL_MSG("processing encrypted extensions"); |
13586 | 0 | ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size); |
13587 | 0 | break; |
13588 | | |
13589 | 0 | #ifndef NO_CERTS |
13590 | 0 | case certificate_request: |
13591 | 0 | WOLFSSL_MSG("processing certificate request"); |
13592 | 0 | ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size); |
13593 | 0 | break; |
13594 | 0 | #endif |
13595 | | |
13596 | 0 | case session_ticket: |
13597 | 0 | WOLFSSL_MSG("processing new session ticket"); |
13598 | 0 | ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size); |
13599 | 0 | break; |
13600 | 0 | #endif /* !NO_WOLFSSL_CLIENT */ |
13601 | | |
13602 | 0 | #ifndef NO_WOLFSSL_SERVER |
13603 | | /* Messages only received by server. */ |
13604 | 0 | case client_hello: |
13605 | 0 | WOLFSSL_MSG("processing client hello"); |
13606 | | #if defined(HAVE_ECH) |
13607 | | /* keep the start idx so we can restore it for the inner call */ |
13608 | | echInOutIdx = *inOutIdx; |
13609 | | #endif |
13610 | 0 | ret = DoTls13ClientHello(ssl, input, inOutIdx, size); |
13611 | | #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ |
13612 | | ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ |
13613 | | (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) |
13614 | | if ((ssl->options.resuming || !ssl->options.verifyPeer || |
13615 | | !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) |
13616 | | #ifdef WOLFSSL_DTLS13 |
13617 | | && (!ssl->options.dtls) |
13618 | | #endif |
13619 | | ) { |
13620 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
13621 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E) && |
13622 | | ret != WC_NO_ERR_TRACE(OCSP_WANT_READ)) |
13623 | | #endif |
13624 | | { |
13625 | | ssl->options.cacheMessages = 0; |
13626 | | if ((ssl->hsHashes != NULL) && |
13627 | | (ssl->hsHashes->messages != NULL)) { |
13628 | | ForceZero(ssl->hsHashes->messages, ssl->hsHashes->length); |
13629 | | XFREE(ssl->hsHashes->messages, ssl->heap, |
13630 | | DYNAMIC_TYPE_HASHES); |
13631 | | ssl->hsHashes->messages = NULL; |
13632 | | } |
13633 | | } |
13634 | | } |
13635 | | #endif |
13636 | | #if defined(HAVE_ECH) |
13637 | | if (ret == 0) { |
13638 | | echX = TLSX_Find(ssl->extensions, TLSX_ECH); |
13639 | | |
13640 | | if (echX != NULL && |
13641 | | ((WOLFSSL_ECH*)echX->data)->state == ECH_WRITE_NONE && |
13642 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello != NULL) { |
13643 | | byte copyRandom = ((WOLFSSL_ECH*)echX->data)->innerCount == 0; |
13644 | | /* reset the inOutIdx to the outer start */ |
13645 | | *inOutIdx = echInOutIdx; |
13646 | | /* call again with the inner hello */ |
13647 | | if (ret == 0) { |
13648 | | if (((WOLFSSL_ECH*)echX->data)->sniState == ECH_OUTER_SNI) { |
13649 | | ((WOLFSSL_ECH*)echX->data)->sniState = ECH_INNER_SNI; |
13650 | | } |
13651 | | |
13652 | | ssl->options.echProcessingInner = 1; |
13653 | | ret = DoTls13ClientHello(ssl, |
13654 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello, |
13655 | | &echInOutIdx, |
13656 | | ((WOLFSSL_ECH*)echX->data)->innerClientHelloLen); |
13657 | | ssl->options.echProcessingInner = 0; |
13658 | | |
13659 | | ((WOLFSSL_ECH*)echX->data)->sniState = ECH_SNI_DONE; |
13660 | | } |
13661 | | if (ret == 0 && ((WOLFSSL_ECH*)echX->data)->state != |
13662 | | ECH_PARSED_INTERNAL) { |
13663 | | WOLFSSL_MSG("ECH: inner ClientHello missing ECH extension"); |
13664 | | ret = INVALID_PARAMETER; |
13665 | | } |
13666 | | /* if the inner ech parsed successfully we have successfully |
13667 | | * handled the hello and can skip the whole message */ |
13668 | | if (ret == 0) { |
13669 | | /* Copy inner client random for ECH acceptance calculation. |
13670 | | * Only on first inner ClientHello (before HRR), not CH2. */ |
13671 | | if (copyRandom) { |
13672 | | XMEMCPY(ssl->arrays->clientRandomInner, |
13673 | | ((WOLFSSL_ECH*)echX->data)->innerClientHello + |
13674 | | HANDSHAKE_HEADER_SZ + VERSION_SZ, RAN_LEN); |
13675 | | } |
13676 | | *inOutIdx += size; |
13677 | | } |
13678 | | } |
13679 | | } |
13680 | | #endif /* HAVE_ECH */ |
13681 | 0 | break; |
13682 | | |
13683 | | #ifdef WOLFSSL_EARLY_DATA |
13684 | | case end_of_early_data: |
13685 | | WOLFSSL_MSG("processing end of early data"); |
13686 | | ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size); |
13687 | | break; |
13688 | | #endif |
13689 | 0 | #endif /* !NO_WOLFSSL_SERVER */ |
13690 | | |
13691 | | /* Messages received by both client and server. */ |
13692 | 0 | #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \ |
13693 | 0 | !defined(WOLFSSL_NO_CLIENT_AUTH)) |
13694 | 0 | case certificate: |
13695 | 0 | WOLFSSL_MSG("processing certificate"); |
13696 | 0 | ret = DoTls13Certificate(ssl, input, inOutIdx, size); |
13697 | 0 | break; |
13698 | 0 | #endif |
13699 | | |
13700 | 0 | #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \ |
13701 | 0 | defined(HAVE_ED448) || defined(HAVE_FALCON) || defined(HAVE_DILITHIUM) |
13702 | 0 | case certificate_verify: |
13703 | 0 | WOLFSSL_MSG("processing certificate verify"); |
13704 | 0 | ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size); |
13705 | 0 | break; |
13706 | 0 | #endif |
13707 | 0 | case finished: |
13708 | 0 | WOLFSSL_MSG("processing finished"); |
13709 | 0 | ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF); |
13710 | 0 | break; |
13711 | | |
13712 | 0 | case key_update: |
13713 | 0 | WOLFSSL_MSG("processing key update"); |
13714 | 0 | ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size); |
13715 | 0 | break; |
13716 | | |
13717 | | #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_NO_TLS12) && \ |
13718 | | !defined(NO_WOLFSSL_CLIENT) |
13719 | | case hello_verify_request: |
13720 | | WOLFSSL_MSG("processing hello verify request"); |
13721 | | ret = DoHelloVerifyRequest(ssl, input, inOutIdx, size); |
13722 | | break; |
13723 | | #endif |
13724 | 0 | default: |
13725 | 0 | WOLFSSL_MSG("Unknown handshake message type"); |
13726 | 0 | ret = UNKNOWN_HANDSHAKE_TYPE; |
13727 | 0 | break; |
13728 | 0 | } |
13729 | | |
13730 | 0 | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_ASYNC_IO) |
13731 | | /* if async, offset index so this msg will be processed again */ |
13732 | | /* NOTE: check this now before other calls can overwrite ret */ |
13733 | 0 | if ((ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
13734 | 0 | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) && *inOutIdx > 0) { |
13735 | | /* DTLS always stores a message in a buffer when async is enable, so we |
13736 | | * don't need to adjust for the extra bytes here (*inOutIdx is always |
13737 | | * == 0) */ |
13738 | 0 | *inOutIdx -= HANDSHAKE_HEADER_SZ; |
13739 | 0 | } |
13740 | | |
13741 | | /* make sure async error is cleared */ |
13742 | 0 | if (ret == 0 && |
13743 | 0 | (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E) || |
13744 | 0 | ssl->error == WC_NO_ERR_TRACE(OCSP_WANT_READ))) { |
13745 | 0 | ssl->error = 0; |
13746 | 0 | } |
13747 | 0 | #endif |
13748 | 0 | if (ret == 0 && type != client_hello && type != session_ticket && |
13749 | 0 | type != key_update) { |
13750 | 0 | ret = HashInput(ssl, input + inIdx, (int)size); |
13751 | 0 | } |
13752 | |
|
13753 | 0 | alertType = TranslateErrorToAlert(ret); |
13754 | |
|
13755 | 0 | if (alertType != invalid_alert) { |
13756 | | #ifdef WOLFSSL_DTLS13 |
13757 | | if (type == client_hello && ssl->options.dtls) |
13758 | | DtlsSetSeqNumForReply(ssl); |
13759 | | #endif |
13760 | 0 | tmp = SendAlert(ssl, alert_fatal, alertType); |
13761 | | /* propagate socket error instead of tls error to be sure the error is |
13762 | | * not ignored by DTLS code */ |
13763 | 0 | if (tmp == WC_NO_ERR_TRACE(SOCKET_ERROR_E)) |
13764 | 0 | ret = SOCKET_ERROR_E; |
13765 | 0 | } |
13766 | |
|
13767 | 0 | if (ret == 0 && ssl->options.tls1_3) { |
13768 | | /* Need to hash input message before deriving secrets. */ |
13769 | 0 | #ifndef NO_WOLFSSL_CLIENT |
13770 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) { |
13771 | 0 | if (type == server_hello) { |
13772 | 0 | if ((ret = DeriveEarlySecret(ssl)) != 0) |
13773 | 0 | return ret; |
13774 | 0 | if ((ret = DeriveHandshakeSecret(ssl)) != 0) |
13775 | 0 | return ret; |
13776 | | |
13777 | 0 | if ((ret = DeriveTls13Keys(ssl, handshake_key, |
13778 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
13779 | 0 | return ret; |
13780 | 0 | } |
13781 | | #ifdef WOLFSSL_EARLY_DATA |
13782 | | if (ssl->earlyData != no_early_data) { |
13783 | | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13784 | | return ret; |
13785 | | } |
13786 | | else |
13787 | | #endif |
13788 | 0 | if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0) |
13789 | 0 | return ret; |
13790 | |
|
13791 | | #ifdef WOLFSSL_DTLS13 |
13792 | | if (ssl->options.dtls) { |
13793 | | w64wrapper epochHandshake; |
13794 | | epochHandshake = w64From32(0, DTLS13_EPOCH_HANDSHAKE); |
13795 | | ssl->dtls13Epoch = epochHandshake; |
13796 | | ssl->dtls13PeerEpoch = epochHandshake; |
13797 | | |
13798 | | ret = Dtls13SetEpochKeys( |
13799 | | ssl, epochHandshake, ENCRYPT_AND_DECRYPT_SIDE); |
13800 | | if (ret != 0) |
13801 | | return ret; |
13802 | | |
13803 | | } |
13804 | | #endif /* WOLFSSL_DTLS13 */ |
13805 | 0 | } |
13806 | | |
13807 | 0 | if (type == finished) { |
13808 | 0 | if ((ret = DeriveMasterSecret(ssl)) != 0) |
13809 | 0 | return ret; |
13810 | | /* Last use of preMasterSecret - zeroize as soon as possible. */ |
13811 | 0 | ForceZero(ssl->arrays->preMasterSecret, |
13812 | 0 | ssl->arrays->preMasterSz); |
13813 | | #ifdef WOLFSSL_EARLY_DATA |
13814 | | #ifdef WOLFSSL_QUIC |
13815 | | if (WOLFSSL_IS_QUIC(ssl) && ssl->earlyData != no_early_data) { |
13816 | | /* QUIC never sends/receives EndOfEarlyData, but having |
13817 | | * early data means the last encryption keys had not been |
13818 | | * set yet. */ |
13819 | | if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0) |
13820 | | return ret; |
13821 | | } |
13822 | | #endif |
13823 | | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13824 | | ENCRYPT_AND_DECRYPT_SIDE, |
13825 | | ssl->earlyData == no_early_data)) != 0) { |
13826 | | return ret; |
13827 | | } |
13828 | | if (ssl->earlyData != no_early_data) { |
13829 | | if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, |
13830 | | 1)) != 0) { |
13831 | | return ret; |
13832 | | } |
13833 | | } |
13834 | | #else |
13835 | 0 | if ((ret = DeriveTls13Keys(ssl, traffic_key, |
13836 | 0 | ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) { |
13837 | 0 | return ret; |
13838 | 0 | } |
13839 | 0 | #endif |
13840 | | /* Setup keys for application data messages. */ |
13841 | 0 | if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0) |
13842 | 0 | return ret; |
13843 | 0 | } |
13844 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
13845 | | if (type == certificate_request && |
13846 | | ssl->options.handShakeState == HANDSHAKE_DONE) { |
13847 | | #if defined(HAVE_WRITE_DUP) |
13848 | | /* Read side cannot write; delegate the cert response to the |
13849 | | * write side by saving auth state in the shared WriteDup. */ |
13850 | | if (ssl->dupSide == READ_DUP_SIDE) { |
13851 | | if (ssl->dupWrite == NULL) |
13852 | | return BAD_STATE_E; |
13853 | | if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) |
13854 | | return BAD_MUTEX_E; |
13855 | | /* Copy the current transcript so the write side can |
13856 | | * compute the correct Finished MAC. */ |
13857 | | ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, |
13858 | | &ssl->dupWrite->postHandshakeHashState); |
13859 | | if (ret == 0) { |
13860 | | /* Copy the cert request context. */ |
13861 | | CertReqCtx** tail = &ssl->certReqCtx; |
13862 | | while (*tail != NULL) |
13863 | | tail = &(*tail)->next; |
13864 | | *tail = ssl->dupWrite->postHandshakeCertReqCtx; |
13865 | | ssl->dupWrite->postHandshakeCertReqCtx = ssl->certReqCtx; |
13866 | | ssl->certReqCtx = NULL; |
13867 | | ssl->dupWrite->postHandshakeSendVerify = |
13868 | | ssl->options.sendVerify; |
13869 | | ssl->dupWrite->postHandshakeSigAlgo = |
13870 | | ssl->options.sigAlgo; |
13871 | | ssl->dupWrite->postHandshakeHashAlgo = |
13872 | | ssl->options.hashAlgo; |
13873 | | ssl->dupWrite->postHandshakeAuthPending = 1; |
13874 | | } |
13875 | | wc_UnLockMutex(&ssl->dupWrite->dupMutex); |
13876 | | /* Leave ssl->options unchanged: read side must not reset |
13877 | | * its states or call wolfSSL_connect_TLSv13. */ |
13878 | | } |
13879 | | else |
13880 | | #endif /* HAVE_WRITE_DUP */ |
13881 | | { |
13882 | | /* reset handshake states */ |
13883 | | ssl->options.clientState = CLIENT_HELLO_COMPLETE; |
13884 | | ssl->options.connectState = FIRST_REPLY_DONE; |
13885 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
13886 | | ssl->options.processReply = 0; /* doProcessInit */ |
13887 | | |
13888 | | /* |
13889 | | DTLSv1.3 note: We can't reset serverState to |
13890 | | SERVER_FINISHED_COMPLETE with the goal that this connect |
13891 | | blocks until the cert/cert_verify/finished flight gets ACKed |
13892 | | by the server. The problem is that we will invoke |
13893 | | ProcessReplyEx() in that case, but we came here from |
13894 | | ProcessReplyEx() and it is not re-entrant safe (the input |
13895 | | buffer would still have the certificate_request message). */ |
13896 | | |
13897 | | if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) { |
13898 | | ret = ssl->error; |
13899 | | if (ret != WC_NO_ERR_TRACE(WC_PENDING_E)) |
13900 | | ret = POST_HAND_AUTH_ERROR; |
13901 | | } |
13902 | | } |
13903 | | } |
13904 | | #endif |
13905 | 0 | } |
13906 | 0 | #endif /* NO_WOLFSSL_CLIENT */ |
13907 | |
|
13908 | 0 | #ifndef NO_WOLFSSL_SERVER |
13909 | | #if defined(HAVE_SESSION_TICKET) |
13910 | | if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) { |
13911 | | ret = DeriveResumptionSecret(ssl, ssl->session->masterSecret); |
13912 | | if (ret != 0) |
13913 | | return ret; |
13914 | | } |
13915 | | #endif |
13916 | 0 | #endif /* NO_WOLFSSL_SERVER */ |
13917 | 0 | } |
13918 | | |
13919 | | #ifdef WOLFSSL_DTLS13 |
13920 | | if (ssl->options.dtls && !ssl->options.dtlsStateful) { |
13921 | | DtlsResetState(ssl); |
13922 | | if (DtlsIgnoreError(ret)) |
13923 | | ret = 0; |
13924 | | } |
13925 | | #endif |
13926 | | |
13927 | 0 | WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret); |
13928 | 0 | return ret; |
13929 | 0 | } |
13930 | | |
13931 | | |
13932 | | /* Handle a handshake message that has been received. |
13933 | | * |
13934 | | * ssl The SSL/TLS object. |
13935 | | * input The message buffer. |
13936 | | * inOutIdx On entry, the index into the buffer of the current message. |
13937 | | * On exit, the index into the buffer of the next message. |
13938 | | * totalSz Length of remaining data in the message buffer. |
13939 | | * returns 0 on success and otherwise failure. |
13940 | | */ |
13941 | | int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx, |
13942 | | word32 totalSz) |
13943 | 0 | { |
13944 | 0 | int ret = 0; |
13945 | 0 | word32 inputLength; |
13946 | 0 | byte type; |
13947 | 0 | word32 size = 0; |
13948 | |
|
13949 | 0 | WOLFSSL_ENTER("DoTls13HandShakeMsg"); |
13950 | |
|
13951 | 0 | if (ssl->arrays == NULL) { |
13952 | 0 | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13953 | 0 | totalSz) != 0) { |
13954 | 0 | SendAlert(ssl, alert_fatal, unexpected_message); |
13955 | 0 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13956 | 0 | return PARSE_ERROR; |
13957 | 0 | } |
13958 | | |
13959 | 0 | ret = EarlySanityCheckMsgReceived(ssl, type, size); |
13960 | 0 | if (ret != 0) { |
13961 | 0 | WOLFSSL_ERROR(ret); |
13962 | 0 | return ret; |
13963 | 0 | } |
13964 | | |
13965 | 0 | return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
13966 | 0 | totalSz); |
13967 | 0 | } |
13968 | | |
13969 | 0 | inputLength = ssl->buffers.inputBuffer.length - *inOutIdx - ssl->keys.padSz; |
13970 | | |
13971 | | /* If there is a pending fragmented handshake message, |
13972 | | * pending message size will be non-zero. */ |
13973 | 0 | if (ssl->arrays->pendingMsgSz == 0) { |
13974 | |
|
13975 | 0 | if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size, |
13976 | 0 | totalSz) != 0) { |
13977 | 0 | WOLFSSL_ERROR_VERBOSE(PARSE_ERROR); |
13978 | 0 | return PARSE_ERROR; |
13979 | 0 | } |
13980 | | |
13981 | 0 | ret = EarlySanityCheckMsgReceived(ssl, type, |
13982 | 0 | (inputLength > HANDSHAKE_HEADER_SZ) ? |
13983 | 0 | min(inputLength - HANDSHAKE_HEADER_SZ, size) : 0); |
13984 | 0 | if (ret != 0) { |
13985 | 0 | WOLFSSL_ERROR(ret); |
13986 | 0 | return ret; |
13987 | 0 | } |
13988 | | |
13989 | | /* Cap the maximum size of a handshake message to something reasonable. |
13990 | | * By default is the maximum size of a certificate message assuming |
13991 | | * nine 2048-bit RSA certificates in the chain. */ |
13992 | 0 | if (size > MAX_HANDSHAKE_SZ) { |
13993 | 0 | WOLFSSL_MSG("Handshake message too large"); |
13994 | 0 | WOLFSSL_ERROR_VERBOSE(HANDSHAKE_SIZE_ERROR); |
13995 | 0 | return HANDSHAKE_SIZE_ERROR; |
13996 | 0 | } |
13997 | | |
13998 | | /* size is the size of the certificate message payload */ |
13999 | 0 | if (inputLength - HANDSHAKE_HEADER_SZ < size) { |
14000 | 0 | ssl->arrays->pendingMsgType = type; |
14001 | 0 | ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ; |
14002 | 0 | ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ, |
14003 | 0 | ssl->heap, |
14004 | 0 | DYNAMIC_TYPE_ARRAYS); |
14005 | 0 | if (ssl->arrays->pendingMsg == NULL) |
14006 | 0 | return MEMORY_E; |
14007 | 0 | XMEMCPY(ssl->arrays->pendingMsg, |
14008 | 0 | input + *inOutIdx - HANDSHAKE_HEADER_SZ, |
14009 | 0 | inputLength); |
14010 | 0 | ssl->arrays->pendingMsgOffset = inputLength; |
14011 | 0 | *inOutIdx += inputLength + ssl->keys.padSz - HANDSHAKE_HEADER_SZ; |
14012 | 0 | return 0; |
14013 | 0 | } |
14014 | | |
14015 | 0 | ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size, |
14016 | 0 | totalSz); |
14017 | 0 | } |
14018 | 0 | else { |
14019 | 0 | if (inputLength + ssl->arrays->pendingMsgOffset > |
14020 | 0 | ssl->arrays->pendingMsgSz) { |
14021 | 0 | inputLength = ssl->arrays->pendingMsgSz - |
14022 | 0 | ssl->arrays->pendingMsgOffset; |
14023 | 0 | } |
14024 | |
|
14025 | 0 | ret = EarlySanityCheckMsgReceived(ssl, ssl->arrays->pendingMsgType, |
14026 | 0 | inputLength); |
14027 | 0 | if (ret != 0) { |
14028 | 0 | WOLFSSL_ERROR(ret); |
14029 | 0 | return ret; |
14030 | 0 | } |
14031 | | |
14032 | 0 | XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset, |
14033 | 0 | input + *inOutIdx, inputLength); |
14034 | 0 | ssl->arrays->pendingMsgOffset += inputLength; |
14035 | 0 | *inOutIdx += inputLength + ssl->keys.padSz; |
14036 | |
|
14037 | 0 | if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz) |
14038 | 0 | { |
14039 | 0 | word32 idx = 0; |
14040 | 0 | ret = DoTls13HandShakeMsgType(ssl, |
14041 | 0 | ssl->arrays->pendingMsg + HANDSHAKE_HEADER_SZ, |
14042 | 0 | &idx, ssl->arrays->pendingMsgType, |
14043 | 0 | ssl->arrays->pendingMsgSz - HANDSHAKE_HEADER_SZ, |
14044 | 0 | ssl->arrays->pendingMsgSz); |
14045 | | #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP) |
14046 | | if (ret == WC_NO_ERR_TRACE(WC_PENDING_E) || |
14047 | | ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) { |
14048 | | /* setup to process fragment again */ |
14049 | | ssl->arrays->pendingMsgOffset -= inputLength; |
14050 | | *inOutIdx -= inputLength + ssl->keys.padSz; |
14051 | | } |
14052 | | else |
14053 | | #endif |
14054 | 0 | { |
14055 | 0 | XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS); |
14056 | 0 | ssl->arrays->pendingMsg = NULL; |
14057 | 0 | ssl->arrays->pendingMsgSz = 0; |
14058 | 0 | } |
14059 | 0 | } |
14060 | 0 | } |
14061 | | |
14062 | 0 | WOLFSSL_LEAVE("DoTls13HandShakeMsg", ret); |
14063 | 0 | return ret; |
14064 | 0 | } |
14065 | | |
14066 | | #ifndef NO_WOLFSSL_CLIENT |
14067 | | |
14068 | | /* The client connecting to the server. |
14069 | | * The protocol version is expecting to be TLS v1.3. |
14070 | | * If the server downgrades, and older versions of the protocol are compiled |
14071 | | * in, the client will fallback to wolfSSL_connect(). |
14072 | | * Please see note at top of README if you get an error from connect. |
14073 | | * |
14074 | | * ssl The SSL/TLS object. |
14075 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
14076 | | * unrecoverable error occurs and 0 otherwise. |
14077 | | * For more error information use wolfSSL_get_error(). |
14078 | | */ |
14079 | | int wolfSSL_connect_TLSv13(WOLFSSL* ssl) |
14080 | 0 | { |
14081 | 0 | int advanceState; |
14082 | 0 | int ret = 0; |
14083 | |
|
14084 | 0 | WOLFSSL_ENTER("wolfSSL_connect_TLSv13"); |
14085 | |
|
14086 | 0 | #ifdef HAVE_ERRNO_H |
14087 | 0 | errno = 0; |
14088 | 0 | #endif |
14089 | |
|
14090 | 0 | if (ssl == NULL) |
14091 | 0 | return BAD_FUNC_ARG; |
14092 | | |
14093 | 0 | if (ssl->options.side != WOLFSSL_CLIENT_END) { |
14094 | 0 | ssl->error = SIDE_ERROR; |
14095 | 0 | WOLFSSL_ERROR(ssl->error); |
14096 | 0 | return WOLFSSL_FATAL_ERROR; |
14097 | 0 | } |
14098 | | |
14099 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
14100 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
14101 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
14102 | 0 | return ret; |
14103 | 0 | } |
14104 | | |
14105 | | #ifdef WOLFSSL_DTLS |
14106 | | if (ssl->version.major == DTLS_MAJOR) { |
14107 | | ssl->options.dtls = 1; |
14108 | | ssl->options.dtlsStateful = 1; |
14109 | | } |
14110 | | #endif |
14111 | | |
14112 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
14113 | | if ((ssl->ConnectFilter != NULL) && |
14114 | | (ssl->options.connectState == CONNECT_BEGIN)) |
14115 | | { |
14116 | | wolfSSL_netfilter_decision_t res; |
14117 | | if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) == |
14118 | | WOLFSSL_SUCCESS) && |
14119 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
14120 | | ssl->error = SOCKET_FILTERED_E; |
14121 | | WOLFSSL_ERROR(ssl->error); |
14122 | | return WOLFSSL_FATAL_ERROR; |
14123 | | } |
14124 | | } |
14125 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
14126 | | |
14127 | | /* fragOffset is non-zero when sending fragments. On the last |
14128 | | * fragment, fragOffset is zero again, and the state can be |
14129 | | * advanced. Also, only advance from states in which we send data */ |
14130 | 0 | advanceState = (ssl->options.connectState == CONNECT_BEGIN || |
14131 | 0 | ssl->options.connectState == HELLO_AGAIN || |
14132 | 0 | (ssl->options.connectState >= FIRST_REPLY_DONE && |
14133 | 0 | ssl->options.connectState <= FIRST_REPLY_FOURTH)); |
14134 | |
|
14135 | | #ifdef WOLFSSL_DTLS13 |
14136 | | if (ssl->options.dtls) |
14137 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
14138 | | && !ssl->dtls13SendingAckOrRtx; |
14139 | | #endif /* WOLFSSL_DTLS13 */ |
14140 | |
|
14141 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
14142 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14143 | | /* do not send buffered or advance state if last error was an |
14144 | | async pending operation */ |
14145 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
14146 | | #endif |
14147 | 0 | ) { |
14148 | 0 | if ((ret = SendBuffered(ssl)) == 0) { |
14149 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
14150 | 0 | if (advanceState) { |
14151 | | #ifdef WOLFSSL_DTLS13 |
14152 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version) && |
14153 | | ssl->options.connectState == FIRST_REPLY_FOURTH) { |
14154 | | /* WAIT_FINISHED_ACK is a state added afterwards, but it |
14155 | | can't follow FIRST_REPLY_FOURTH in the enum order. Indeed |
14156 | | the value of the enum ConnectState is stored in |
14157 | | serialized session. This would make importing serialized |
14158 | | session from other wolfSSL version incompatible */ |
14159 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
14160 | | } |
14161 | | else |
14162 | | #endif /* WOLFSSL_DTLS13 */ |
14163 | 0 | { |
14164 | 0 | ssl->options.connectState++; |
14165 | 0 | } |
14166 | 0 | WOLFSSL_MSG("connect state: " |
14167 | 0 | "Advanced from last buffered fragment send"); |
14168 | 0 | #ifdef WOLFSSL_ASYNC_IO |
14169 | 0 | FreeAsyncCtx(ssl, 0); |
14170 | 0 | #endif |
14171 | |
|
14172 | 0 | } |
14173 | 0 | } |
14174 | 0 | else { |
14175 | 0 | WOLFSSL_MSG("connect state: " |
14176 | 0 | "Not advanced, more fragments to send"); |
14177 | 0 | } |
14178 | | #ifdef WOLFSSL_DTLS13 |
14179 | | if (ssl->options.dtls) |
14180 | | ssl->dtls13SendingAckOrRtx = 0; |
14181 | | #endif /* WOLFSSL_DTLS13 */ |
14182 | |
|
14183 | 0 | } |
14184 | 0 | else { |
14185 | 0 | ssl->error = ret; |
14186 | 0 | WOLFSSL_ERROR(ssl->error); |
14187 | 0 | return WOLFSSL_FATAL_ERROR; |
14188 | 0 | } |
14189 | 0 | } |
14190 | | |
14191 | 0 | ret = RetrySendAlert(ssl); |
14192 | 0 | if (ret != 0) { |
14193 | 0 | ssl->error = ret; |
14194 | 0 | WOLFSSL_ERROR(ssl->error); |
14195 | 0 | return WOLFSSL_FATAL_ERROR; |
14196 | 0 | } |
14197 | | |
14198 | | #ifdef WOLFSSL_DTLS13 |
14199 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
14200 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
14201 | | WOLFSSL_ERROR(ssl->error); |
14202 | | return WOLFSSL_FATAL_ERROR; |
14203 | | } |
14204 | | |
14205 | | /* we sent all the fragments. Advance state. */ |
14206 | | ssl->options.connectState++; |
14207 | | } |
14208 | | #endif /* WOLFSSL_DTLS13 */ |
14209 | | |
14210 | 0 | switch (ssl->options.connectState) { |
14211 | | |
14212 | 0 | case CONNECT_BEGIN: |
14213 | | /* Always send client hello first. */ |
14214 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
14215 | 0 | WOLFSSL_ERROR(ssl->error); |
14216 | 0 | return WOLFSSL_FATAL_ERROR; |
14217 | 0 | } |
14218 | | |
14219 | 0 | ssl->options.connectState = CLIENT_HELLO_SENT; |
14220 | 0 | WOLFSSL_MSG("TLSv13 connect state: CLIENT_HELLO_SENT"); |
14221 | 0 | FALL_THROUGH; |
14222 | |
|
14223 | 0 | case CLIENT_HELLO_SENT: |
14224 | | #ifdef WOLFSSL_EARLY_DATA |
14225 | | if (ssl->earlyData != no_early_data && |
14226 | | ssl->options.handShakeState != CLIENT_HELLO_COMPLETE) { |
14227 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14228 | | if (!ssl->options.dtls && |
14229 | | ssl->options.tls13MiddleBoxCompat) { |
14230 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14231 | | WOLFSSL_ERROR(ssl->error); |
14232 | | return WOLFSSL_FATAL_ERROR; |
14233 | | } |
14234 | | ssl->options.sentChangeCipher = 1; |
14235 | | } |
14236 | | #endif |
14237 | | ssl->options.handShakeState = CLIENT_HELLO_COMPLETE; |
14238 | | return WOLFSSL_SUCCESS; |
14239 | | } |
14240 | | #endif |
14241 | | /* Get the response/s from the server. */ |
14242 | 0 | while (ssl->options.serverState < |
14243 | 0 | SERVER_HELLOVERIFYREQUEST_COMPLETE) { |
14244 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14245 | 0 | WOLFSSL_ERROR(ssl->error); |
14246 | 0 | return WOLFSSL_FATAL_ERROR; |
14247 | 0 | } |
14248 | |
|
14249 | | #ifdef WOLFSSL_DTLS13 |
14250 | | if (ssl->options.dtls) { |
14251 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14252 | | WOLFSSL_ERROR(ssl->error); |
14253 | | return WOLFSSL_FATAL_ERROR; |
14254 | | } |
14255 | | } |
14256 | | #endif /* WOLFSSL_DTLS13 */ |
14257 | 0 | } |
14258 | | |
14259 | 0 | if (!ssl->options.tls1_3) { |
14260 | 0 | #ifndef WOLFSSL_NO_TLS12 |
14261 | 0 | if (ssl->options.downgrade) |
14262 | 0 | return wolfSSL_connect(ssl); |
14263 | 0 | #endif |
14264 | 0 | WOLFSSL_MSG("Client using higher version, fatal error"); |
14265 | 0 | WOLFSSL_ERROR_VERBOSE(VERSION_ERROR); |
14266 | 0 | return VERSION_ERROR; |
14267 | 0 | } |
14268 | | |
14269 | 0 | ssl->options.connectState = HELLO_AGAIN; |
14270 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN"); |
14271 | 0 | FALL_THROUGH; |
14272 | |
|
14273 | 0 | case HELLO_AGAIN: |
14274 | |
|
14275 | 0 | if (ssl->options.serverState == |
14276 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
14277 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14278 | | if (!ssl->options.dtls && !ssl->options.sentChangeCipher |
14279 | | && ssl->options.tls13MiddleBoxCompat) { |
14280 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14281 | | WOLFSSL_ERROR(ssl->error); |
14282 | | return WOLFSSL_FATAL_ERROR; |
14283 | | } |
14284 | | ssl->options.sentChangeCipher = 1; |
14285 | | } |
14286 | | #endif |
14287 | | /* Try again with different security parameters. */ |
14288 | 0 | if ((ssl->error = SendTls13ClientHello(ssl)) != 0) { |
14289 | 0 | WOLFSSL_ERROR(ssl->error); |
14290 | 0 | return WOLFSSL_FATAL_ERROR; |
14291 | 0 | } |
14292 | 0 | } |
14293 | | |
14294 | 0 | ssl->options.connectState = HELLO_AGAIN_REPLY; |
14295 | 0 | WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY"); |
14296 | 0 | FALL_THROUGH; |
14297 | |
|
14298 | 0 | case HELLO_AGAIN_REPLY: |
14299 | | /* Get the response/s from the server. */ |
14300 | 0 | while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) { |
14301 | | #ifdef WOLFSSL_DTLS13 |
14302 | | if (!IsAtLeastTLSv1_3(ssl->version)) { |
14303 | | #ifndef WOLFSSL_NO_TLS12 |
14304 | | if (ssl->options.downgrade) |
14305 | | return wolfSSL_connect(ssl); |
14306 | | #endif |
14307 | | } |
14308 | | #endif /* WOLFSSL_DTLS13 */ |
14309 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14310 | 0 | WOLFSSL_ERROR(ssl->error); |
14311 | 0 | return WOLFSSL_FATAL_ERROR; |
14312 | 0 | } |
14313 | |
|
14314 | | #ifdef WOLFSSL_DTLS13 |
14315 | | if (ssl->options.dtls) { |
14316 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14317 | | WOLFSSL_ERROR(ssl->error); |
14318 | | return WOLFSSL_FATAL_ERROR; |
14319 | | } |
14320 | | } |
14321 | | #endif /* WOLFSSL_DTLS13 */ |
14322 | 0 | } |
14323 | | |
14324 | 0 | ssl->options.connectState = FIRST_REPLY_DONE; |
14325 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_DONE"); |
14326 | 0 | FALL_THROUGH; |
14327 | |
|
14328 | 0 | case FIRST_REPLY_DONE: |
14329 | 0 | if (ssl->options.certOnly) |
14330 | 0 | return WOLFSSL_SUCCESS; |
14331 | | #ifdef WOLFSSL_EARLY_DATA |
14332 | | if (!ssl->options.dtls && ssl->earlyData != no_early_data |
14333 | | && !WOLFSSL_IS_QUIC(ssl)) { |
14334 | | if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) { |
14335 | | WOLFSSL_ERROR(ssl->error); |
14336 | | return WOLFSSL_FATAL_ERROR; |
14337 | | } |
14338 | | WOLFSSL_MSG("sent: end_of_early_data"); |
14339 | | } |
14340 | | #endif |
14341 | | |
14342 | 0 | ssl->options.connectState = FIRST_REPLY_FIRST; |
14343 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST"); |
14344 | 0 | FALL_THROUGH; |
14345 | |
|
14346 | 0 | case FIRST_REPLY_FIRST: |
14347 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
14348 | | if (!ssl->options.sentChangeCipher && !ssl->options.dtls |
14349 | | && ssl->options.tls13MiddleBoxCompat) { |
14350 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
14351 | | WOLFSSL_ERROR(ssl->error); |
14352 | | return WOLFSSL_FATAL_ERROR; |
14353 | | } |
14354 | | ssl->options.sentChangeCipher = 1; |
14355 | | } |
14356 | | #endif |
14357 | |
|
14358 | 0 | ssl->options.connectState = FIRST_REPLY_SECOND; |
14359 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND"); |
14360 | 0 | FALL_THROUGH; |
14361 | |
|
14362 | 0 | case FIRST_REPLY_SECOND: |
14363 | | /* CLIENT: check peer authentication. */ |
14364 | 0 | if (!ssl->options.peerAuthGood) { |
14365 | 0 | WOLFSSL_MSG("Server authentication did not happen"); |
14366 | 0 | WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR); |
14367 | 0 | return WOLFSSL_FATAL_ERROR; |
14368 | 0 | } |
14369 | 0 | #ifndef NO_CERTS |
14370 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14371 | 0 | ssl->error = SendTls13Certificate(ssl); |
14372 | 0 | if (ssl->error != 0) { |
14373 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
14374 | 0 | WOLFSSL_ERROR(ssl->error); |
14375 | 0 | return WOLFSSL_FATAL_ERROR; |
14376 | 0 | } |
14377 | 0 | WOLFSSL_MSG("sent: certificate"); |
14378 | 0 | } |
14379 | 0 | #endif |
14380 | | |
14381 | 0 | ssl->options.connectState = FIRST_REPLY_THIRD; |
14382 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD"); |
14383 | 0 | FALL_THROUGH; |
14384 | |
|
14385 | 0 | case FIRST_REPLY_THIRD: |
14386 | 0 | #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
14387 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || \ |
14388 | 0 | defined(HAVE_FALCON) || defined(HAVE_DILITHIUM))) && \ |
14389 | 0 | (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)) |
14390 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
14391 | 0 | ssl->error = SendTls13CertificateVerify(ssl); |
14392 | 0 | if (ssl->error != 0) { |
14393 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
14394 | 0 | WOLFSSL_ERROR(ssl->error); |
14395 | 0 | return WOLFSSL_FATAL_ERROR; |
14396 | 0 | } |
14397 | 0 | WOLFSSL_MSG("sent: certificate verify"); |
14398 | 0 | } |
14399 | 0 | #endif |
14400 | | |
14401 | 0 | ssl->options.connectState = FIRST_REPLY_FOURTH; |
14402 | 0 | WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH"); |
14403 | 0 | FALL_THROUGH; |
14404 | |
|
14405 | 0 | case FIRST_REPLY_FOURTH: |
14406 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
14407 | 0 | wolfssl_local_MaybeCheckAlertOnErr(ssl, ssl->error); |
14408 | 0 | WOLFSSL_ERROR(ssl->error); |
14409 | 0 | return WOLFSSL_FATAL_ERROR; |
14410 | 0 | } |
14411 | 0 | WOLFSSL_MSG("sent: finished"); |
14412 | |
|
14413 | | #ifdef WOLFSSL_DTLS13 |
14414 | | ssl->options.connectState = WAIT_FINISHED_ACK; |
14415 | | WOLFSSL_MSG("connect state: WAIT_FINISHED_ACK"); |
14416 | | FALL_THROUGH; |
14417 | | |
14418 | | case WAIT_FINISHED_ACK: |
14419 | | if (ssl->options.dtls) { |
14420 | | while (ssl->options.serverState != SERVER_FINISHED_ACKED) { |
14421 | | if ((ssl->error = ProcessReply(ssl)) < 0) { |
14422 | | WOLFSSL_ERROR(ssl->error); |
14423 | | return WOLFSSL_FATAL_ERROR; |
14424 | | } |
14425 | | |
14426 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
14427 | | WOLFSSL_ERROR(ssl->error); |
14428 | | return WOLFSSL_FATAL_ERROR; |
14429 | | } |
14430 | | } |
14431 | | } |
14432 | | #endif /* WOLFSSL_DTLS13 */ |
14433 | 0 | ssl->options.connectState = FINISHED_DONE; |
14434 | 0 | WOLFSSL_MSG("connect state: FINISHED_DONE"); |
14435 | 0 | FALL_THROUGH; |
14436 | |
|
14437 | 0 | case FINISHED_DONE: |
14438 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
14439 | 0 | if (ssl->hsDoneCb != NULL) { |
14440 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
14441 | 0 | if (cbret < 0) { |
14442 | 0 | ssl->error = cbret; |
14443 | 0 | WOLFSSL_ERROR_VERBOSE(ssl->error); |
14444 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
14445 | 0 | return WOLFSSL_FATAL_ERROR; |
14446 | 0 | } |
14447 | 0 | } |
14448 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
14449 | | |
14450 | | #if defined(HAVE_ECH) |
14451 | | /* RFC 9849 s6.1.6: if we offered ECH but the server rejected it, |
14452 | | * send ech_required alert and abort before returning to the app */ |
14453 | | if (ssl->echConfigs != NULL && !ssl->options.disableECH && |
14454 | | !ssl->options.echAccepted) { |
14455 | | if (ssl->echRetryConfigs != NULL) { |
14456 | | ssl->options.echRetryConfigsAccepted = 1; |
14457 | | } |
14458 | | SendAlert(ssl, alert_fatal, ech_required); |
14459 | | ssl->error = ECH_REQUIRED_E; |
14460 | | WOLFSSL_ERROR_VERBOSE(ECH_REQUIRED_E); |
14461 | | return WOLFSSL_FATAL_ERROR; |
14462 | | } |
14463 | | #endif /* HAVE_ECH */ |
14464 | | |
14465 | 0 | if (!ssl->options.keepResources) { |
14466 | 0 | FreeHandshakeResources(ssl); |
14467 | 0 | } |
14468 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
14469 | | /* Free the remaining async context if not using it for crypto */ |
14470 | 0 | FreeAsyncCtx(ssl, 1); |
14471 | 0 | #endif |
14472 | |
|
14473 | 0 | ssl->error = 0; /* clear the error */ |
14474 | |
|
14475 | 0 | WOLFSSL_LEAVE("wolfSSL_connect_TLSv13", WOLFSSL_SUCCESS); |
14476 | 0 | return WOLFSSL_SUCCESS; |
14477 | | |
14478 | 0 | default: |
14479 | 0 | WOLFSSL_MSG("Unknown connect state ERROR"); |
14480 | 0 | return WOLFSSL_FATAL_ERROR; /* unknown connect state */ |
14481 | 0 | } |
14482 | 0 | } |
14483 | | #endif |
14484 | | |
14485 | | #if defined(WOLFSSL_SEND_HRR_COOKIE) |
14486 | | /* Send a cookie with the HelloRetryRequest to avoid storing state. |
14487 | | * |
14488 | | * ssl SSL/TLS object. |
14489 | | * secret Secret to use when generating integrity check for cookie. |
14490 | | * A value of NULL indicates to generate a new random secret. |
14491 | | * secretSz Size of secret data in bytes. |
14492 | | * Use a value of 0 to indicate use of default size. |
14493 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when |
14494 | | * called on a client; WOLFSSL_SUCCESS on success and otherwise failure. |
14495 | | */ |
14496 | | int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret, |
14497 | | unsigned int secretSz) |
14498 | | { |
14499 | | int ret; |
14500 | | |
14501 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14502 | | return BAD_FUNC_ARG; |
14503 | | #ifndef NO_WOLFSSL_SERVER |
14504 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14505 | | return SIDE_ERROR; |
14506 | | |
14507 | | if (secretSz == 0) { |
14508 | | #ifndef NO_SHA256 |
14509 | | secretSz = WC_SHA256_DIGEST_SIZE; |
14510 | | #elif defined(WOLFSSL_SHA384) |
14511 | | secretSz = WC_SHA384_DIGEST_SIZE; |
14512 | | #elif defined(WOLFSSL_TLS13_SHA512) |
14513 | | secretSz = WC_SHA512_DIGEST_SIZE; |
14514 | | #elif defined(WOLFSSL_SM3) |
14515 | | secretSz = WC_SM3_DIGEST_SIZE; |
14516 | | #else |
14517 | | #error "No digest to available to use with HMAC for cookies." |
14518 | | #endif /* NO_SHA */ |
14519 | | } |
14520 | | |
14521 | | if (secretSz != ssl->buffers.tls13CookieSecret.length) { |
14522 | | byte* newSecret; |
14523 | | |
14524 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
14525 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
14526 | | ssl->buffers.tls13CookieSecret.length); |
14527 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, |
14528 | | ssl->heap, DYNAMIC_TYPE_COOKIE_PWD); |
14529 | | } |
14530 | | |
14531 | | newSecret = (byte*)XMALLOC(secretSz, ssl->heap, |
14532 | | DYNAMIC_TYPE_COOKIE_PWD); |
14533 | | if (newSecret == NULL) { |
14534 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
14535 | | ssl->buffers.tls13CookieSecret.length = 0; |
14536 | | WOLFSSL_MSG("couldn't allocate new cookie secret"); |
14537 | | return MEMORY_ERROR; |
14538 | | } |
14539 | | ssl->buffers.tls13CookieSecret.buffer = newSecret; |
14540 | | ssl->buffers.tls13CookieSecret.length = secretSz; |
14541 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
14542 | | wc_MemZero_Add("wolfSSL_send_hrr_cookie secret", |
14543 | | ssl->buffers.tls13CookieSecret.buffer, |
14544 | | ssl->buffers.tls13CookieSecret.length); |
14545 | | #endif |
14546 | | } |
14547 | | |
14548 | | /* If the supplied secret is NULL, randomly generate a new secret. */ |
14549 | | if (secret == NULL) { |
14550 | | ret = wc_RNG_GenerateBlock(ssl->rng, |
14551 | | ssl->buffers.tls13CookieSecret.buffer, secretSz); |
14552 | | if (ret < 0) |
14553 | | return ret; |
14554 | | } |
14555 | | else |
14556 | | XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz); |
14557 | | |
14558 | | ssl->options.sendCookie = 1; |
14559 | | |
14560 | | ret = WOLFSSL_SUCCESS; |
14561 | | #else |
14562 | | (void)secret; |
14563 | | (void)secretSz; |
14564 | | |
14565 | | ret = SIDE_ERROR; |
14566 | | #endif |
14567 | | |
14568 | | return ret; |
14569 | | } |
14570 | | |
14571 | | int wolfSSL_disable_hrr_cookie(WOLFSSL* ssl) |
14572 | | { |
14573 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14574 | | return BAD_FUNC_ARG; |
14575 | | |
14576 | | #ifdef NO_WOLFSSL_SERVER |
14577 | | return SIDE_ERROR; |
14578 | | #else |
14579 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14580 | | return SIDE_ERROR; |
14581 | | |
14582 | | if (ssl->buffers.tls13CookieSecret.buffer != NULL) { |
14583 | | ForceZero(ssl->buffers.tls13CookieSecret.buffer, |
14584 | | ssl->buffers.tls13CookieSecret.length); |
14585 | | XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap, |
14586 | | DYNAMIC_TYPE_COOKIE_PWD); |
14587 | | ssl->buffers.tls13CookieSecret.buffer = NULL; |
14588 | | ssl->buffers.tls13CookieSecret.length = 0; |
14589 | | } |
14590 | | |
14591 | | ssl->options.sendCookie = 0; |
14592 | | return WOLFSSL_SUCCESS; |
14593 | | #endif /* NO_WOLFSSL_SERVER */ |
14594 | | } |
14595 | | |
14596 | | #endif /* defined(WOLFSSL_SEND_HRR_COOKIE) */ |
14597 | | |
14598 | | #ifdef HAVE_SUPPORTED_CURVES |
14599 | | /* Create a key share entry from group. |
14600 | | * Generates a key pair. |
14601 | | * |
14602 | | * ssl The SSL/TLS object. |
14603 | | * group The named group. |
14604 | | * returns 0 on success, otherwise failure. |
14605 | | * for async can return WC_PENDING_E and should be called again |
14606 | | */ |
14607 | | int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group) |
14608 | 0 | { |
14609 | 0 | int ret; |
14610 | |
|
14611 | 0 | if (ssl == NULL) |
14612 | 0 | return BAD_FUNC_ARG; |
14613 | | |
14614 | | #ifdef WOLFSSL_ASYNC_CRYPT |
14615 | | ret = wolfSSL_AsyncPop(ssl, NULL); |
14616 | | if (ret != WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
14617 | | /* Check for error */ |
14618 | | if (ret < 0) |
14619 | | return ret; |
14620 | | } |
14621 | | #endif |
14622 | | |
14623 | 0 | #if defined(WOLFSSL_HAVE_MLKEM) |
14624 | 0 | if (WOLFSSL_NAMED_GROUP_IS_PQC(group) || |
14625 | 0 | WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group)) { |
14626 | |
|
14627 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) { |
14628 | 0 | return BAD_FUNC_ARG; |
14629 | 0 | } |
14630 | | |
14631 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) { |
14632 | | /* If I am the server of a KEM connection, do not do keygen because |
14633 | | * I'm going to encapsulate with the client's public key. Note that |
14634 | | * I might be the client and ssl->option.side has not been properly |
14635 | | * set yet. In that case the KeyGen operation will be deferred to |
14636 | | * connection time. */ |
14637 | 0 | return WOLFSSL_SUCCESS; |
14638 | 0 | } |
14639 | 0 | } |
14640 | 0 | #endif |
14641 | | #if defined(NO_TLS) |
14642 | | (void)ret; |
14643 | | (void)group; |
14644 | | #else |
14645 | | /* Check if the group is supported. */ |
14646 | 0 | if (!TLSX_IsGroupSupported(group)) { |
14647 | 0 | WOLFSSL_MSG("Group not supported."); |
14648 | 0 | return BAD_FUNC_ARG; |
14649 | 0 | } |
14650 | | |
14651 | 0 | ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL, &ssl->extensions); |
14652 | 0 | if (ret != 0) |
14653 | 0 | return ret; |
14654 | 0 | #endif /* NO_TLS */ |
14655 | 0 | return WOLFSSL_SUCCESS; |
14656 | 0 | } |
14657 | | |
14658 | | /* Send no key share entries - use HelloRetryRequest to negotiate shared group. |
14659 | | * |
14660 | | * ssl The SSL/TLS object. |
14661 | | * returns 0 on success, otherwise failure. |
14662 | | */ |
14663 | | int wolfSSL_NoKeyShares(WOLFSSL* ssl) |
14664 | 0 | { |
14665 | 0 | int ret; |
14666 | |
|
14667 | 0 | if (ssl == NULL) |
14668 | 0 | return BAD_FUNC_ARG; |
14669 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
14670 | 0 | return SIDE_ERROR; |
14671 | | #if defined(NO_TLS) |
14672 | | (void)ret; |
14673 | | #else |
14674 | 0 | ret = TLSX_KeyShare_Empty(ssl); |
14675 | 0 | if (ret != 0) |
14676 | 0 | return ret; |
14677 | 0 | #endif /* NO_TLS */ |
14678 | 0 | return WOLFSSL_SUCCESS; |
14679 | 0 | } |
14680 | | #endif |
14681 | | |
14682 | | #ifdef WOLFSSL_DUAL_ALG_CERTS |
14683 | | int wolfSSL_UseCKS(WOLFSSL* ssl, byte *sigSpec, word16 sigSpecSz) |
14684 | | { |
14685 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->ctx->method->version) || |
14686 | | sigSpec == NULL || sigSpecSz == 0) |
14687 | | return BAD_FUNC_ARG; |
14688 | | |
14689 | | ssl->sigSpec = sigSpec; |
14690 | | ssl->sigSpecSz = sigSpecSz; |
14691 | | return WOLFSSL_SUCCESS; |
14692 | | } |
14693 | | |
14694 | | int wolfSSL_CTX_UseCKS(WOLFSSL_CTX* ctx, byte *sigSpec, word16 sigSpecSz) |
14695 | | { |
14696 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version) || |
14697 | | sigSpec == NULL || sigSpecSz == 0) |
14698 | | return BAD_FUNC_ARG; |
14699 | | |
14700 | | ctx->sigSpec = sigSpec; |
14701 | | ctx->sigSpecSz = sigSpecSz; |
14702 | | return WOLFSSL_SUCCESS; |
14703 | | } |
14704 | | #endif /* WOLFSSL_DUAL_ALG_CERTS */ |
14705 | | |
14706 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
14707 | | * |
14708 | | * ctx The SSL/TLS CTX object. |
14709 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14710 | | */ |
14711 | | int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx) |
14712 | 0 | { |
14713 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14714 | 0 | return BAD_FUNC_ARG; |
14715 | 0 | if (ctx->method->side == WOLFSSL_CLIENT_END) |
14716 | 0 | return SIDE_ERROR; |
14717 | | |
14718 | | #ifdef HAVE_SESSION_TICKET |
14719 | | ctx->noTicketTls13 = 1; |
14720 | | #endif |
14721 | | |
14722 | 0 | return 0; |
14723 | 0 | } |
14724 | | |
14725 | | /* Do not send a ticket after TLS v1.3 handshake for resumption. |
14726 | | * |
14727 | | * ssl The SSL/TLS object. |
14728 | | * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on |
14729 | | * a client and 0 on success. |
14730 | | */ |
14731 | | int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl) |
14732 | 0 | { |
14733 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14734 | 0 | return BAD_FUNC_ARG; |
14735 | 0 | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14736 | 0 | return SIDE_ERROR; |
14737 | | |
14738 | | #ifdef HAVE_SESSION_TICKET |
14739 | | ssl->options.noTicketTls13 = 1; |
14740 | | #endif |
14741 | | |
14742 | 0 | return 0; |
14743 | 0 | } |
14744 | | |
14745 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
14746 | | * |
14747 | | * ctx The SSL/TLS CTX object. |
14748 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14749 | | */ |
14750 | | int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx) |
14751 | 0 | { |
14752 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14753 | 0 | return BAD_FUNC_ARG; |
14754 | | |
14755 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14756 | | ctx->noPskDheKe = 1; |
14757 | | #endif |
14758 | | |
14759 | 0 | return 0; |
14760 | 0 | } |
14761 | | |
14762 | | /* Disallow (EC)DHE key exchange when using pre-shared keys. |
14763 | | * |
14764 | | * ssl The SSL/TLS object. |
14765 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
14766 | | * success. |
14767 | | */ |
14768 | | int wolfSSL_no_dhe_psk(WOLFSSL* ssl) |
14769 | 0 | { |
14770 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14771 | 0 | return BAD_FUNC_ARG; |
14772 | | |
14773 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14774 | | ssl->options.noPskDheKe = 1; |
14775 | | #endif |
14776 | | |
14777 | 0 | return 0; |
14778 | 0 | } |
14779 | | |
14780 | | #ifdef HAVE_SUPPORTED_CURVES |
14781 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
14782 | | * |
14783 | | * ctx The SSL/TLS CTX object. |
14784 | | * returns BAD_FUNC_ARG when ctx is NULL and 0 on success. |
14785 | | */ |
14786 | | int wolfSSL_CTX_only_dhe_psk(WOLFSSL_CTX* ctx) |
14787 | 0 | { |
14788 | 0 | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14789 | 0 | return BAD_FUNC_ARG; |
14790 | | |
14791 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14792 | | ctx->onlyPskDheKe = 1; |
14793 | | #endif |
14794 | | |
14795 | 0 | return 0; |
14796 | 0 | } |
14797 | | |
14798 | | /* Only allow (EC)DHE key exchange when using pre-shared keys. |
14799 | | * |
14800 | | * ssl The SSL/TLS object. |
14801 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on |
14802 | | * success. |
14803 | | */ |
14804 | | int wolfSSL_only_dhe_psk(WOLFSSL* ssl) |
14805 | 0 | { |
14806 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14807 | 0 | return BAD_FUNC_ARG; |
14808 | | |
14809 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
14810 | | ssl->options.onlyPskDheKe = 1; |
14811 | | #endif |
14812 | | |
14813 | 0 | return 0; |
14814 | 0 | } |
14815 | | #endif /* HAVE_SUPPORTED_CURVES */ |
14816 | | |
14817 | | int Tls13UpdateKeys(WOLFSSL* ssl) |
14818 | 0 | { |
14819 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14820 | 0 | return BAD_FUNC_ARG; |
14821 | | |
14822 | | #ifdef WOLFSSL_DTLS13 |
14823 | | /* we are already waiting for the ack of a sent key update message. We can't |
14824 | | send another one before receiving its ack. Either wolfSSL_update_keys() |
14825 | | was invoked multiple times over a short period of time or we replied to a |
14826 | | KeyUpdate with update request. We'll just ignore sending this |
14827 | | KeyUpdate. */ |
14828 | | /* TODO: add WOLFSSL_ERROR_ALREADY_IN_PROGRESS type of error here */ |
14829 | | if (ssl->options.dtls && ssl->dtls13WaitKeyUpdateAck) |
14830 | | return 0; |
14831 | | #endif /* WOLFSSL_DTLS13 */ |
14832 | | |
14833 | 0 | return SendTls13KeyUpdate(ssl); |
14834 | 0 | } |
14835 | | |
14836 | | /* Update the keys for encryption and decryption. |
14837 | | * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then |
14838 | | * calling wolfSSL_write() will have the message sent when ready. |
14839 | | * |
14840 | | * ssl The SSL/TLS object. |
14841 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14842 | | * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write, |
14843 | | * WOLFSSL_SUCCESS on success and otherwise failure. |
14844 | | */ |
14845 | | int wolfSSL_update_keys(WOLFSSL* ssl) |
14846 | 0 | { |
14847 | 0 | int ret; |
14848 | 0 | ret = Tls13UpdateKeys(ssl); |
14849 | 0 | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14850 | 0 | ret = WOLFSSL_ERROR_WANT_WRITE; |
14851 | 0 | else if (ret == 0) |
14852 | 0 | ret = WOLFSSL_SUCCESS; |
14853 | 0 | return ret; |
14854 | 0 | } |
14855 | | |
14856 | | /* Whether a response is waiting for key update request. |
14857 | | * |
14858 | | * ssl The SSL/TLS object. |
14859 | | * required 0 when no key update response required. |
14860 | | * 1 when no key update response required. |
14861 | | * return 0 on success. |
14862 | | * return BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3 |
14863 | | */ |
14864 | | int wolfSSL_key_update_response(WOLFSSL* ssl, int* required) |
14865 | 0 | { |
14866 | 0 | if (required == NULL || ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14867 | 0 | return BAD_FUNC_ARG; |
14868 | | |
14869 | 0 | *required = ssl->keys.updateResponseReq; |
14870 | |
|
14871 | 0 | return 0; |
14872 | 0 | } |
14873 | | |
14874 | | #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) |
14875 | | /* Allow post-handshake authentication in TLS v1.3 connections. |
14876 | | * |
14877 | | * ctx The SSL/TLS CTX object. |
14878 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and |
14879 | | * 0 on success. |
14880 | | */ |
14881 | | int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx) |
14882 | | { |
14883 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
14884 | | return BAD_FUNC_ARG; |
14885 | | if (ctx->method->side == WOLFSSL_SERVER_END) |
14886 | | return SIDE_ERROR; |
14887 | | |
14888 | | ctx->postHandshakeAuth = 1; |
14889 | | |
14890 | | return 0; |
14891 | | } |
14892 | | |
14893 | | /* Allow post-handshake authentication in TLS v1.3 connection. |
14894 | | * |
14895 | | * ssl The SSL/TLS object. |
14896 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
14897 | | * SIDE_ERROR when not a client and 0 on success. |
14898 | | */ |
14899 | | int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl) |
14900 | | { |
14901 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14902 | | return BAD_FUNC_ARG; |
14903 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
14904 | | return SIDE_ERROR; |
14905 | | |
14906 | | ssl->options.postHandshakeAuth = 1; |
14907 | | |
14908 | | return 0; |
14909 | | } |
14910 | | |
14911 | | /* Request a certificate of the client. |
14912 | | * Can be called any time after handshake completion. |
14913 | | * A maximum of 256 requests can be sent on a connection. |
14914 | | * |
14915 | | * ssl SSL/TLS object. |
14916 | | */ |
14917 | | int wolfSSL_request_certificate(WOLFSSL* ssl) |
14918 | | { |
14919 | | int ret; |
14920 | | #ifndef NO_WOLFSSL_SERVER |
14921 | | CertReqCtx* certReqCtx; |
14922 | | #endif |
14923 | | |
14924 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14925 | | return BAD_FUNC_ARG; |
14926 | | #ifndef NO_WOLFSSL_SERVER |
14927 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
14928 | | return SIDE_ERROR; |
14929 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14930 | | return NOT_READY_ERROR; |
14931 | | if (!ssl->options.postHandshakeAuth) |
14932 | | return POST_HAND_AUTH_ERROR; |
14933 | | if (ssl->certReqCtx != NULL) { |
14934 | | if (ssl->certReqCtx->len != 1) |
14935 | | return BAD_STATE_E; |
14936 | | /* We support sending up to 255 certificate requests */ |
14937 | | if (ssl->certReqCtx->ctx == 255) |
14938 | | return BAD_STATE_E; |
14939 | | } |
14940 | | |
14941 | | certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap, |
14942 | | DYNAMIC_TYPE_TMP_BUFFER); |
14943 | | if (certReqCtx == NULL) |
14944 | | return MEMORY_E; |
14945 | | XMEMSET(certReqCtx, 0, sizeof(CertReqCtx)); |
14946 | | certReqCtx->next = ssl->certReqCtx; |
14947 | | certReqCtx->len = 1; |
14948 | | if (certReqCtx->next != NULL) |
14949 | | certReqCtx->ctx = certReqCtx->next->ctx + 1; |
14950 | | ssl->certReqCtx = certReqCtx; |
14951 | | |
14952 | | ssl->msgsReceived.got_certificate = 0; |
14953 | | ssl->msgsReceived.got_certificate_verify = 0; |
14954 | | ssl->msgsReceived.got_finished = 0; |
14955 | | |
14956 | | ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len); |
14957 | | if (ret == WC_NO_ERR_TRACE(WANT_WRITE)) |
14958 | | ret = WOLFSSL_ERROR_WANT_WRITE; |
14959 | | else if (ret == 0) |
14960 | | ret = WOLFSSL_SUCCESS; |
14961 | | #else |
14962 | | ret = SIDE_ERROR; |
14963 | | #endif |
14964 | | |
14965 | | return ret; |
14966 | | } |
14967 | | #endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */ |
14968 | | |
14969 | | #if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT) |
14970 | | /* Get the preferred key exchange group. |
14971 | | * |
14972 | | * ssl The SSL/TLS object. |
14973 | | * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, |
14974 | | * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete |
14975 | | * and group number on success. |
14976 | | */ |
14977 | | int wolfSSL_preferred_group(WOLFSSL* ssl) |
14978 | 0 | { |
14979 | 0 | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
14980 | 0 | return BAD_FUNC_ARG; |
14981 | 0 | #ifndef NO_WOLFSSL_CLIENT |
14982 | 0 | if (ssl->options.side == WOLFSSL_SERVER_END) |
14983 | 0 | return SIDE_ERROR; |
14984 | 0 | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
14985 | 0 | return NOT_READY_ERROR; |
14986 | | |
14987 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
14988 | | /* Return supported groups only. */ |
14989 | 0 | return TLSX_SupportedCurve_Preferred(ssl, 1); |
14990 | | #else |
14991 | | return 0; |
14992 | | #endif |
14993 | | #else |
14994 | | return SIDE_ERROR; |
14995 | | #endif |
14996 | 0 | } |
14997 | | #endif |
14998 | | |
14999 | | #ifndef NO_PSK |
15000 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
15001 | | * against context object. |
15002 | | * |
15003 | | * @param [in, out] ctx SSL/TLS context object. |
15004 | | * @param [in] cb Client PSK callback passed a cipher suite. |
15005 | | */ |
15006 | | void wolfSSL_CTX_set_psk_client_cs_callback(WOLFSSL_CTX* ctx, |
15007 | | wc_psk_client_cs_callback cb) |
15008 | | { |
15009 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_cs_callback"); |
15010 | | |
15011 | | if (ctx == NULL) |
15012 | | return; |
15013 | | |
15014 | | ctx->havePSK = 1; |
15015 | | ctx->client_psk_cs_cb = cb; |
15016 | | } |
15017 | | |
15018 | | /* Set the PSK callback, that is passed the cipher suite, for a client to use |
15019 | | * against SSL object. |
15020 | | * |
15021 | | * @param [in, out] ssl SSL/TLS object. |
15022 | | * @param [in] cb Client PSK callback passed a cipher suite. |
15023 | | */ |
15024 | | void wolfSSL_set_psk_client_cs_callback(WOLFSSL* ssl, |
15025 | | wc_psk_client_cs_callback cb) |
15026 | | { |
15027 | | byte haveRSA = 1; |
15028 | | int keySz = 0; |
15029 | | |
15030 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_cs_callback"); |
15031 | | |
15032 | | if (ssl == NULL) |
15033 | | return; |
15034 | | |
15035 | | ssl->options.havePSK = 1; |
15036 | | ssl->options.client_psk_cs_cb = cb; |
15037 | | |
15038 | | #ifdef NO_RSA |
15039 | | haveRSA = 0; |
15040 | | #endif |
15041 | | #ifndef NO_CERTS |
15042 | | keySz = ssl->buffers.keySz; |
15043 | | #endif |
15044 | | if (AllocateSuites(ssl) != 0) |
15045 | | return; |
15046 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
15047 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
15048 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
15049 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
15050 | | } |
15051 | | |
15052 | | /* Set the PSK callback that returns the cipher suite for a client to use |
15053 | | * against context object. |
15054 | | * |
15055 | | * @param [in, out] ctx SSL/TLS context object. |
15056 | | * @param [in] cb Client PSK callback returning cipher suite. |
15057 | | */ |
15058 | | void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, |
15059 | | wc_psk_client_tls13_callback cb) |
15060 | | { |
15061 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_tls13_callback"); |
15062 | | |
15063 | | if (ctx == NULL) |
15064 | | return; |
15065 | | |
15066 | | ctx->havePSK = 1; |
15067 | | ctx->client_psk_tls13_cb = cb; |
15068 | | } |
15069 | | |
15070 | | /* Set the PSK callback that returns the cipher suite for a client to use |
15071 | | * against SSL object. |
15072 | | * |
15073 | | * @param [in, out] ssl SSL/TLS object. |
15074 | | * @param [in] cb Client PSK callback returning cipher suite. |
15075 | | */ |
15076 | | void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl, |
15077 | | wc_psk_client_tls13_callback cb) |
15078 | | { |
15079 | | byte haveRSA = 1; |
15080 | | int keySz = 0; |
15081 | | |
15082 | | WOLFSSL_ENTER("wolfSSL_set_psk_client_tls13_callback"); |
15083 | | |
15084 | | if (ssl == NULL) |
15085 | | return; |
15086 | | |
15087 | | ssl->options.havePSK = 1; |
15088 | | ssl->options.client_psk_tls13_cb = cb; |
15089 | | |
15090 | | #ifdef NO_RSA |
15091 | | haveRSA = 0; |
15092 | | #endif |
15093 | | #ifndef NO_CERTS |
15094 | | keySz = ssl->buffers.keySz; |
15095 | | #endif |
15096 | | if (AllocateSuites(ssl) != 0) |
15097 | | return; |
15098 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
15099 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
15100 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
15101 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
15102 | | } |
15103 | | |
15104 | | /* Set the PSK callback that returns the cipher suite for a server to use |
15105 | | * against context object. |
15106 | | * |
15107 | | * @param [in, out] ctx SSL/TLS context object. |
15108 | | * @param [in] cb Server PSK callback returning cipher suite. |
15109 | | */ |
15110 | | void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, |
15111 | | wc_psk_server_tls13_callback cb) |
15112 | | { |
15113 | | WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_tls13_callback"); |
15114 | | if (ctx == NULL) |
15115 | | return; |
15116 | | ctx->havePSK = 1; |
15117 | | ctx->server_psk_tls13_cb = cb; |
15118 | | } |
15119 | | |
15120 | | /* Set the PSK callback that returns the cipher suite for a server to use |
15121 | | * against SSL object. |
15122 | | * |
15123 | | * @param [in, out] ssl SSL/TLS object. |
15124 | | * @param [in] cb Server PSK callback returning cipher suite. |
15125 | | */ |
15126 | | void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl, |
15127 | | wc_psk_server_tls13_callback cb) |
15128 | | { |
15129 | | byte haveRSA = 1; |
15130 | | int keySz = 0; |
15131 | | |
15132 | | WOLFSSL_ENTER("wolfSSL_set_psk_server_tls13_callback"); |
15133 | | if (ssl == NULL) |
15134 | | return; |
15135 | | |
15136 | | ssl->options.havePSK = 1; |
15137 | | ssl->options.server_psk_tls13_cb = cb; |
15138 | | |
15139 | | #ifdef NO_RSA |
15140 | | haveRSA = 0; |
15141 | | #endif |
15142 | | #ifndef NO_CERTS |
15143 | | keySz = ssl->buffers.keySz; |
15144 | | #endif |
15145 | | if (AllocateSuites(ssl) != 0) |
15146 | | return; |
15147 | | InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE, |
15148 | | ssl->options.haveDH, ssl->options.haveECDSAsig, |
15149 | | ssl->options.haveECC, TRUE, ssl->options.haveStaticECC, |
15150 | | ssl->options.useAnon, TRUE, TRUE, TRUE, TRUE, ssl->options.side); |
15151 | | } |
15152 | | |
15153 | | /* Get name of first supported cipher suite that uses the hash indicated. |
15154 | | * |
15155 | | * @param [in] ssl SSL/TLS object. |
15156 | | * @param [in] hash Name of hash algorithm. e.g. "SHA256", "SHA384" |
15157 | | * @return Name of cipher suite. |
15158 | | * @return NULL on failure. |
15159 | | */ |
15160 | | const char* wolfSSL_get_cipher_name_by_hash(WOLFSSL* ssl, const char* hash) |
15161 | | { |
15162 | | const char* name = NULL; |
15163 | | byte mac = no_mac; |
15164 | | int i; |
15165 | | const Suites* suites; |
15166 | | |
15167 | | if (hash == NULL || ssl == NULL || |
15168 | | (ssl->suites == NULL && ssl->ctx == NULL)) |
15169 | | return NULL; |
15170 | | |
15171 | | suites = WOLFSSL_SUITES(ssl); |
15172 | | if (suites == NULL) |
15173 | | return NULL; |
15174 | | |
15175 | | if (XSTRCMP(hash, "SHA256") == 0) { |
15176 | | mac = sha256_mac; |
15177 | | } |
15178 | | else if (XSTRCMP(hash, "SHA384") == 0) { |
15179 | | mac = sha384_mac; |
15180 | | } |
15181 | | if (mac != no_mac) { |
15182 | | for (i = 0; i < suites->suiteSz; i += 2) { |
15183 | | if (SuiteMac(suites->suites + i) == mac) { |
15184 | | name = GetCipherNameInternal(suites->suites[i + 0], |
15185 | | suites->suites[i + 1]); |
15186 | | break; |
15187 | | } |
15188 | | } |
15189 | | } |
15190 | | return name; |
15191 | | } |
15192 | | #endif /* !NO_PSK */ |
15193 | | |
15194 | | |
15195 | | #ifndef NO_WOLFSSL_SERVER |
15196 | | |
15197 | | /* The server accepting a connection from a client. |
15198 | | * The protocol version is expecting to be TLS v1.3. |
15199 | | * If the client downgrades, and older versions of the protocol are compiled |
15200 | | * in, the server will fallback to wolfSSL_accept(). |
15201 | | * Please see note at top of README if you get an error from accept. |
15202 | | * |
15203 | | * ssl The SSL/TLS object. |
15204 | | * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when |
15205 | | * unrecoverable error occurs and 0 otherwise. |
15206 | | * For more error information use wolfSSL_get_error(). |
15207 | | */ |
15208 | | int wolfSSL_accept_TLSv13(WOLFSSL* ssl) |
15209 | 0 | { |
15210 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
15211 | | word16 havePSK = 0; |
15212 | | #endif |
15213 | 0 | int ret = 0; |
15214 | |
|
15215 | 0 | WOLFSSL_ENTER("wolfSSL_accept_TLSv13"); |
15216 | |
|
15217 | 0 | #ifdef HAVE_ERRNO_H |
15218 | 0 | errno = 0; |
15219 | 0 | #endif |
15220 | |
|
15221 | 0 | if (ssl == NULL) |
15222 | 0 | return WOLFSSL_FATAL_ERROR; |
15223 | | |
15224 | | #if !defined(NO_CERTS) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) |
15225 | | havePSK = ssl->options.havePSK; |
15226 | | #endif |
15227 | | |
15228 | 0 | if (ssl->options.side != WOLFSSL_SERVER_END) { |
15229 | 0 | ssl->error = SIDE_ERROR; |
15230 | 0 | WOLFSSL_ERROR(ssl->error); |
15231 | 0 | return WOLFSSL_FATAL_ERROR; |
15232 | 0 | } |
15233 | | |
15234 | | /* make sure this wolfSSL object has arrays and rng setup. Protects |
15235 | | * case where the WOLFSSL object is reused via wolfSSL_clear() */ |
15236 | 0 | if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) { |
15237 | 0 | return ret; |
15238 | 0 | } |
15239 | | |
15240 | | #ifdef WOLFSSL_DTLS |
15241 | | if (ssl->version.major == DTLS_MAJOR) { |
15242 | | ssl->options.dtls = 1; |
15243 | | if (!IsDtlsNotSctpMode(ssl) || !ssl->options.sendCookie) |
15244 | | ssl->options.dtlsStateful = 1; |
15245 | | } |
15246 | | #endif |
15247 | | |
15248 | | #ifdef WOLFSSL_WOLFSENTRY_HOOKS |
15249 | | if ((ssl->AcceptFilter != NULL) && |
15250 | | ((ssl->options.acceptState == TLS13_ACCEPT_BEGIN) |
15251 | | #ifdef HAVE_SECURE_RENEGOTIATION |
15252 | | || (ssl->options.acceptState == TLS13_ACCEPT_BEGIN_RENEG) |
15253 | | #endif |
15254 | | )) |
15255 | | { |
15256 | | wolfSSL_netfilter_decision_t res; |
15257 | | if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) == |
15258 | | WOLFSSL_SUCCESS) && |
15259 | | (res == WOLFSSL_NETFILTER_REJECT)) { |
15260 | | ssl->error = SOCKET_FILTERED_E; |
15261 | | WOLFSSL_ERROR(ssl->error); |
15262 | | return WOLFSSL_FATAL_ERROR; |
15263 | | } |
15264 | | } |
15265 | | #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ |
15266 | | |
15267 | 0 | #ifndef NO_CERTS |
15268 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
15269 | | if (!havePSK) |
15270 | | #endif |
15271 | 0 | { |
15272 | | #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \ |
15273 | | defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY) |
15274 | | if (ssl->ctx->certSetupCb != NULL) { |
15275 | | WOLFSSL_MSG("CertSetupCb set. server cert and " |
15276 | | "key not checked"); |
15277 | | } |
15278 | | else |
15279 | | #endif |
15280 | 0 | { |
15281 | 0 | if (!ssl->buffers.certificate || |
15282 | 0 | !ssl->buffers.certificate->buffer) { |
15283 | |
|
15284 | 0 | WOLFSSL_MSG("accept error: server cert required"); |
15285 | 0 | ssl->error = NO_PRIVATE_KEY; |
15286 | 0 | WOLFSSL_ERROR(ssl->error); |
15287 | 0 | return WOLFSSL_FATAL_ERROR; |
15288 | 0 | } |
15289 | | |
15290 | 0 | if (!ssl->buffers.key || !ssl->buffers.key->buffer) { |
15291 | | /* allow no private key if using existing key */ |
15292 | | #ifdef WOLF_PRIVATE_KEY_ID |
15293 | | if (ssl->devId != INVALID_DEVID |
15294 | | #ifdef HAVE_PK_CALLBACKS |
15295 | | || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx) |
15296 | | #endif |
15297 | | ) { |
15298 | | WOLFSSL_MSG("Allowing no server private key (external)"); |
15299 | | } |
15300 | | else |
15301 | | #endif |
15302 | 0 | { |
15303 | 0 | WOLFSSL_MSG("accept error: server key required"); |
15304 | 0 | ssl->error = NO_PRIVATE_KEY; |
15305 | 0 | WOLFSSL_ERROR(ssl->error); |
15306 | 0 | return WOLFSSL_FATAL_ERROR; |
15307 | 0 | } |
15308 | 0 | } |
15309 | 0 | } |
15310 | 0 | } |
15311 | 0 | #endif /* NO_CERTS */ |
15312 | | |
15313 | 0 | if (ssl->buffers.outputBuffer.length > 0 |
15314 | | #ifdef WOLFSSL_ASYNC_CRYPT |
15315 | | /* do not send buffered or advance state if last error was an |
15316 | | async pending operation */ |
15317 | | && ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E) |
15318 | | #endif |
15319 | 0 | ) { |
15320 | | |
15321 | | /* fragOffset is non-zero when sending fragments. On the last |
15322 | | * fragment, fragOffset is zero again, and the state can be |
15323 | | * advanced. */ |
15324 | 0 | int advanceState = |
15325 | 0 | (ssl->options.acceptState == TLS13_ACCEPT_CLIENT_HELLO_DONE || |
15326 | 0 | ssl->options.acceptState == |
15327 | 0 | TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE || |
15328 | 0 | ssl->options.acceptState == TLS13_ACCEPT_SECOND_REPLY_DONE || |
15329 | 0 | ssl->options.acceptState == TLS13_SERVER_HELLO_SENT || |
15330 | 0 | ssl->options.acceptState == TLS13_ACCEPT_THIRD_REPLY_DONE || |
15331 | 0 | ssl->options.acceptState == TLS13_SERVER_EXTENSIONS_SENT || |
15332 | 0 | ssl->options.acceptState == TLS13_CERT_REQ_SENT || |
15333 | 0 | ssl->options.acceptState == TLS13_CERT_SENT || |
15334 | 0 | ssl->options.acceptState == TLS13_CERT_VERIFY_SENT || |
15335 | 0 | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_SENT || |
15336 | 0 | ssl->options.acceptState == TLS13_ACCEPT_FINISHED_DONE); |
15337 | |
|
15338 | | #ifdef WOLFSSL_DTLS13 |
15339 | | if (ssl->options.dtls) |
15340 | | advanceState = advanceState && !ssl->dtls13SendingFragments |
15341 | | && !ssl->dtls13SendingAckOrRtx; |
15342 | | #endif /* WOLFSSL_DTLS13 */ |
15343 | |
|
15344 | 0 | ret = SendBuffered(ssl); |
15345 | 0 | if (ret == 0) { |
15346 | 0 | if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) { |
15347 | 0 | if (advanceState) { |
15348 | 0 | ssl->options.acceptState++; |
15349 | 0 | WOLFSSL_MSG("accept state: " |
15350 | 0 | "Advanced from last buffered fragment send"); |
15351 | 0 | #ifdef WOLFSSL_ASYNC_IO |
15352 | 0 | FreeAsyncCtx(ssl, 0); |
15353 | 0 | #endif |
15354 | 0 | } |
15355 | 0 | } |
15356 | 0 | else { |
15357 | 0 | WOLFSSL_MSG("accept state: " |
15358 | 0 | "Not advanced, more fragments to send"); |
15359 | 0 | } |
15360 | |
|
15361 | | #ifdef WOLFSSL_DTLS13 |
15362 | | if (ssl->options.dtls) |
15363 | | ssl->dtls13SendingAckOrRtx = 0; |
15364 | | #endif /* WOLFSSL_DTLS13 */ |
15365 | |
|
15366 | 0 | } |
15367 | 0 | else { |
15368 | 0 | ssl->error = ret; |
15369 | 0 | WOLFSSL_ERROR(ssl->error); |
15370 | 0 | return WOLFSSL_FATAL_ERROR; |
15371 | 0 | } |
15372 | 0 | } |
15373 | | |
15374 | 0 | ret = RetrySendAlert(ssl); |
15375 | 0 | if (ret != 0) { |
15376 | 0 | ssl->error = ret; |
15377 | 0 | WOLFSSL_ERROR(ssl->error); |
15378 | 0 | return WOLFSSL_FATAL_ERROR; |
15379 | 0 | } |
15380 | | #ifdef WOLFSSL_DTLS13 |
15381 | | if (ssl->options.dtls && ssl->dtls13SendingFragments) { |
15382 | | if ((ssl->error = Dtls13FragmentsContinue(ssl)) != 0) { |
15383 | | WOLFSSL_ERROR(ssl->error); |
15384 | | return WOLFSSL_FATAL_ERROR; |
15385 | | } |
15386 | | |
15387 | | /* we sent all the fragments. Advance state. */ |
15388 | | ssl->options.acceptState++; |
15389 | | } |
15390 | | #endif /* WOLFSSL_DTLS13 */ |
15391 | | |
15392 | 0 | switch (ssl->options.acceptState) { |
15393 | | |
15394 | | #ifdef HAVE_SECURE_RENEGOTIATION |
15395 | | case TLS13_ACCEPT_BEGIN_RENEG: |
15396 | | #endif |
15397 | 0 | case TLS13_ACCEPT_BEGIN : |
15398 | | /* get client_hello */ |
15399 | |
|
15400 | 0 | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
15401 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
15402 | 0 | WOLFSSL_ERROR(ssl->error); |
15403 | 0 | return WOLFSSL_FATAL_ERROR; |
15404 | 0 | } |
15405 | |
|
15406 | | #ifdef WOLFSSL_DTLS13 |
15407 | | if (ssl->options.dtls) { |
15408 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15409 | | WOLFSSL_ERROR(ssl->error); |
15410 | | return WOLFSSL_FATAL_ERROR; |
15411 | | } |
15412 | | } |
15413 | | #endif /* WOLFSSL_DTLS13 */ |
15414 | |
|
15415 | 0 | } |
15416 | | |
15417 | 0 | ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE; |
15418 | 0 | WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE"); |
15419 | 0 | if (!IsAtLeastTLSv1_3(ssl->version)) |
15420 | 0 | return wolfSSL_accept(ssl); |
15421 | 0 | FALL_THROUGH; |
15422 | |
|
15423 | 0 | case TLS13_ACCEPT_CLIENT_HELLO_DONE : |
15424 | 0 | if (ssl->options.serverState == |
15425 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
15426 | 0 | if ((ssl->error = SendTls13ServerHello(ssl, |
15427 | 0 | hello_retry_request)) != 0) { |
15428 | 0 | WOLFSSL_ERROR(ssl->error); |
15429 | 0 | return WOLFSSL_FATAL_ERROR; |
15430 | 0 | } |
15431 | 0 | } |
15432 | | |
15433 | 0 | ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE; |
15434 | 0 | WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE"); |
15435 | 0 | FALL_THROUGH; |
15436 | |
|
15437 | 0 | case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE : |
15438 | | #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT |
15439 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
15440 | | && ssl->options.serverState == |
15441 | | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
15442 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
15443 | | WOLFSSL_ERROR(ssl->error); |
15444 | | return WOLFSSL_FATAL_ERROR; |
15445 | | } |
15446 | | ssl->options.sentChangeCipher = 1; |
15447 | | ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE; |
15448 | | } |
15449 | | #endif |
15450 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE; |
15451 | 0 | WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE"); |
15452 | 0 | FALL_THROUGH; |
15453 | |
|
15454 | 0 | case TLS13_ACCEPT_FIRST_REPLY_DONE : |
15455 | 0 | if (ssl->options.serverState == |
15456 | 0 | SERVER_HELLO_RETRY_REQUEST_COMPLETE) { |
15457 | 0 | ssl->options.clientState = CLIENT_HELLO_RETRY; |
15458 | 0 | while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) { |
15459 | 0 | if ((ssl->error = ProcessReply(ssl)) < 0) { |
15460 | 0 | WOLFSSL_ERROR(ssl->error); |
15461 | 0 | return WOLFSSL_FATAL_ERROR; |
15462 | 0 | } |
15463 | |
|
15464 | | #ifdef WOLFSSL_DTLS13 |
15465 | | if (ssl->options.dtls) { |
15466 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15467 | | WOLFSSL_ERROR(ssl->error); |
15468 | | return WOLFSSL_FATAL_ERROR; |
15469 | | } |
15470 | | } |
15471 | | #endif /* WOLFSSL_DTLS13 */ |
15472 | |
|
15473 | 0 | } |
15474 | 0 | } |
15475 | | |
15476 | 0 | ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE; |
15477 | 0 | WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE"); |
15478 | 0 | FALL_THROUGH; |
15479 | |
|
15480 | 0 | case TLS13_ACCEPT_SECOND_REPLY_DONE : |
15481 | 0 | if (ssl->options.returnOnGoodCh) { |
15482 | | /* Higher level in stack wants us to return. Simulate a |
15483 | | * WANT_WRITE to accomplish this. */ |
15484 | 0 | ssl->error = WANT_WRITE; |
15485 | 0 | return WOLFSSL_FATAL_ERROR; |
15486 | 0 | } |
15487 | | |
15488 | 0 | if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) { |
15489 | 0 | WOLFSSL_ERROR(ssl->error); |
15490 | 0 | return WOLFSSL_FATAL_ERROR; |
15491 | 0 | } |
15492 | 0 | ssl->options.acceptState = TLS13_SERVER_HELLO_SENT; |
15493 | 0 | WOLFSSL_MSG("accept state SERVER_HELLO_SENT"); |
15494 | 0 | FALL_THROUGH; |
15495 | |
|
15496 | 0 | case TLS13_SERVER_HELLO_SENT : |
15497 | | #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT) |
15498 | | if (!ssl->options.dtls && ssl->options.tls13MiddleBoxCompat |
15499 | | && !ssl->options.sentChangeCipher && !ssl->options.dtls) { |
15500 | | if ((ssl->error = SendChangeCipher(ssl)) != 0) { |
15501 | | WOLFSSL_ERROR(ssl->error); |
15502 | | return WOLFSSL_FATAL_ERROR; |
15503 | | } |
15504 | | ssl->options.sentChangeCipher = 1; |
15505 | | } |
15506 | | #endif |
15507 | |
|
15508 | 0 | ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE; |
15509 | 0 | WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE"); |
15510 | 0 | FALL_THROUGH; |
15511 | |
|
15512 | 0 | case TLS13_ACCEPT_THIRD_REPLY_DONE : |
15513 | 0 | #ifdef HAVE_SUPPORTED_CURVES |
15514 | | #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) |
15515 | | if (!ssl->options.noPskDheKe) |
15516 | | #endif |
15517 | 0 | { |
15518 | 0 | ssl->error = TLSX_KeyShare_DeriveSecret(ssl); |
15519 | 0 | if (ssl->error != 0) |
15520 | 0 | return WOLFSSL_FATAL_ERROR; |
15521 | 0 | } |
15522 | 0 | #endif |
15523 | | |
15524 | 0 | if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) { |
15525 | 0 | WOLFSSL_ERROR(ssl->error); |
15526 | 0 | return WOLFSSL_FATAL_ERROR; |
15527 | 0 | } |
15528 | 0 | ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT; |
15529 | 0 | WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT"); |
15530 | 0 | FALL_THROUGH; |
15531 | |
|
15532 | 0 | case TLS13_SERVER_EXTENSIONS_SENT : |
15533 | 0 | #ifndef NO_CERTS |
15534 | 0 | if (!ssl->options.resuming) { |
15535 | 0 | if (ssl->options.verifyPeer |
15536 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
15537 | | && !ssl->options.verifyPostHandshake |
15538 | | #endif |
15539 | 0 | ) { |
15540 | 0 | ssl->error = SendTls13CertificateRequest(ssl, NULL, 0); |
15541 | 0 | if (ssl->error != 0) { |
15542 | 0 | WOLFSSL_ERROR(ssl->error); |
15543 | 0 | return WOLFSSL_FATAL_ERROR; |
15544 | 0 | } |
15545 | 0 | } |
15546 | 0 | else { |
15547 | | /* SERVER: Peer auth good if not verifying client. */ |
15548 | 0 | ssl->options.peerAuthGood = 1; |
15549 | 0 | } |
15550 | 0 | } |
15551 | 0 | #endif |
15552 | 0 | ssl->options.acceptState = TLS13_CERT_REQ_SENT; |
15553 | 0 | WOLFSSL_MSG("accept state CERT_REQ_SENT"); |
15554 | 0 | FALL_THROUGH; |
15555 | |
|
15556 | 0 | case TLS13_CERT_REQ_SENT : |
15557 | 0 | #ifndef NO_CERTS |
15558 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15559 | 0 | if ((ssl->error = SendTls13Certificate(ssl)) != 0) { |
15560 | 0 | WOLFSSL_ERROR(ssl->error); |
15561 | 0 | return WOLFSSL_FATAL_ERROR; |
15562 | 0 | } |
15563 | 0 | } |
15564 | 0 | #endif |
15565 | 0 | ssl->options.acceptState = TLS13_CERT_SENT; |
15566 | 0 | WOLFSSL_MSG("accept state CERT_SENT"); |
15567 | 0 | FALL_THROUGH; |
15568 | |
|
15569 | 0 | case TLS13_CERT_SENT : |
15570 | 0 | #if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \ |
15571 | 0 | defined(HAVE_ED25519) || defined(HAVE_ED448) || defined(HAVE_FALCON) || \ |
15572 | 0 | defined(HAVE_DILITHIUM)) |
15573 | 0 | if (!ssl->options.resuming && ssl->options.sendVerify) { |
15574 | 0 | if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) { |
15575 | 0 | WOLFSSL_ERROR(ssl->error); |
15576 | 0 | return WOLFSSL_FATAL_ERROR; |
15577 | 0 | } |
15578 | 0 | } |
15579 | 0 | #endif |
15580 | 0 | ssl->options.acceptState = TLS13_CERT_VERIFY_SENT; |
15581 | 0 | WOLFSSL_MSG("accept state CERT_VERIFY_SENT"); |
15582 | 0 | FALL_THROUGH; |
15583 | |
|
15584 | 0 | case TLS13_CERT_VERIFY_SENT : |
15585 | 0 | if ((ssl->error = SendTls13Finished(ssl)) != 0) { |
15586 | 0 | WOLFSSL_ERROR(ssl->error); |
15587 | 0 | return WOLFSSL_FATAL_ERROR; |
15588 | 0 | } |
15589 | | |
15590 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT; |
15591 | 0 | WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT"); |
15592 | 0 | FALL_THROUGH; |
15593 | |
|
15594 | 0 | case TLS13_ACCEPT_FINISHED_SENT: |
15595 | | #ifdef WOLFSSL_EARLY_DATA |
15596 | | if (ssl->earlyData != no_early_data && |
15597 | | ssl->options.handShakeState != SERVER_FINISHED_COMPLETE) { |
15598 | | ssl->options.handShakeState = SERVER_FINISHED_COMPLETE; |
15599 | | return WOLFSSL_SUCCESS; |
15600 | | } |
15601 | | #endif |
15602 | | #ifdef HAVE_SESSION_TICKET |
15603 | | #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED |
15604 | | if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 && |
15605 | | ssl->ctx->ticketEncCb != NULL && |
15606 | | ssl->options.maxTicketTls13 > 0) { |
15607 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15608 | | WOLFSSL_ERROR(ssl->error); |
15609 | | return WOLFSSL_FATAL_ERROR; |
15610 | | } |
15611 | | ssl->options.ticketsSent = 1; |
15612 | | } |
15613 | | #endif |
15614 | | #endif /* HAVE_SESSION_TICKET */ |
15615 | 0 | ssl->options.acceptState = TLS13_PRE_TICKET_SENT; |
15616 | 0 | WOLFSSL_MSG("accept state TICKET_SENT"); |
15617 | 0 | FALL_THROUGH; |
15618 | |
|
15619 | 0 | case TLS13_PRE_TICKET_SENT : |
15620 | 0 | while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) { |
15621 | 0 | if ( (ssl->error = ProcessReply(ssl)) < 0) { |
15622 | 0 | WOLFSSL_ERROR(ssl->error); |
15623 | 0 | return WOLFSSL_FATAL_ERROR; |
15624 | 0 | } |
15625 | |
|
15626 | | #ifdef WOLFSSL_DTLS13 |
15627 | | if (ssl->options.dtls) { |
15628 | | if ((ssl->error = Dtls13DoScheduledWork(ssl)) < 0) { |
15629 | | WOLFSSL_ERROR(ssl->error); |
15630 | | return WOLFSSL_FATAL_ERROR; |
15631 | | } |
15632 | | } |
15633 | | #endif /* WOLFSSL_DTLS13 */ |
15634 | 0 | } |
15635 | | |
15636 | 0 | ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE; |
15637 | 0 | WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE"); |
15638 | 0 | FALL_THROUGH; |
15639 | |
|
15640 | 0 | case TLS13_ACCEPT_FINISHED_DONE : |
15641 | | /* SERVER: When not resuming and verifying peer but no certificate |
15642 | | * received and not failing when not received then peer auth good. |
15643 | | */ |
15644 | 0 | if (!ssl->options.resuming && ssl->options.verifyPeer && |
15645 | | #ifdef WOLFSSL_POST_HANDSHAKE_AUTH |
15646 | | !ssl->options.verifyPostHandshake && |
15647 | | #endif |
15648 | 0 | !ssl->options.havePeerCert && !ssl->options.failNoCert) { |
15649 | 0 | ssl->options.peerAuthGood = 1; |
15650 | 0 | } |
15651 | | /* SERVER: check peer authentication. */ |
15652 | 0 | if (!ssl->options.peerAuthGood) { |
15653 | 0 | WOLFSSL_MSG("Client authentication did not happen"); |
15654 | 0 | return WOLFSSL_FATAL_ERROR; |
15655 | 0 | } |
15656 | | #ifdef HAVE_SESSION_TICKET |
15657 | | while (ssl->options.ticketsSent < ssl->options.maxTicketTls13) { |
15658 | | if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb |
15659 | | != NULL) { |
15660 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15661 | | WOLFSSL_ERROR(ssl->error); |
15662 | | return WOLFSSL_FATAL_ERROR; |
15663 | | } |
15664 | | } |
15665 | | ssl->options.ticketsSent++; |
15666 | | |
15667 | | /* only one session ticket is sent on session resumption */ |
15668 | | if (ssl->options.resuming) { |
15669 | | break; |
15670 | | } |
15671 | | } |
15672 | | #endif /* HAVE_SESSION_TICKET */ |
15673 | 0 | ssl->options.acceptState = TLS13_TICKET_SENT; |
15674 | 0 | WOLFSSL_MSG("accept state TICKET_SENT"); |
15675 | 0 | FALL_THROUGH; |
15676 | |
|
15677 | 0 | case TLS13_TICKET_SENT : |
15678 | 0 | #ifndef NO_HANDSHAKE_DONE_CB |
15679 | 0 | if (ssl->hsDoneCb) { |
15680 | 0 | int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx); |
15681 | 0 | if (cbret < 0) { |
15682 | 0 | ssl->error = cbret; |
15683 | 0 | WOLFSSL_MSG("HandShake Done Cb don't continue error"); |
15684 | 0 | return WOLFSSL_FATAL_ERROR; |
15685 | 0 | } |
15686 | 0 | } |
15687 | 0 | #endif /* NO_HANDSHAKE_DONE_CB */ |
15688 | | |
15689 | 0 | if (!ssl->options.keepResources) { |
15690 | 0 | FreeHandshakeResources(ssl); |
15691 | 0 | } |
15692 | |
|
15693 | 0 | #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT) |
15694 | | /* Free the remaining async context if not using it for crypto */ |
15695 | 0 | FreeAsyncCtx(ssl, 1); |
15696 | 0 | #endif |
15697 | |
|
15698 | 0 | ssl->error = 0; /* clear the error */ |
15699 | |
|
15700 | 0 | WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS); |
15701 | 0 | return WOLFSSL_SUCCESS; |
15702 | | |
15703 | 0 | default: |
15704 | 0 | WOLFSSL_MSG("Unknown accept state ERROR"); |
15705 | 0 | return WOLFSSL_FATAL_ERROR; |
15706 | 0 | } |
15707 | 0 | } |
15708 | | #endif |
15709 | | |
15710 | | #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET) |
15711 | | /* Server sends a session ticket to the peer. |
15712 | | * |
15713 | | * RFC 8446, section 4.6.1, para 1. |
15714 | | * |
15715 | | * ssl The SSL/TLS object. |
15716 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15717 | | * SIDE_ERROR when not a server, |
15718 | | * NOT_READY_ERROR when handshake not complete, |
15719 | | * WOLFSSL_FATAL_ERROR when creating or sending message fails, and |
15720 | | * WOLFSSL_SUCCESS on success. |
15721 | | */ |
15722 | | int wolfSSL_send_SessionTicket(WOLFSSL* ssl) |
15723 | | { |
15724 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15725 | | return BAD_FUNC_ARG; |
15726 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15727 | | return SIDE_ERROR; |
15728 | | if (ssl->options.handShakeState != HANDSHAKE_DONE) |
15729 | | return NOT_READY_ERROR; |
15730 | | |
15731 | | if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) { |
15732 | | WOLFSSL_ERROR(ssl->error); |
15733 | | return WOLFSSL_FATAL_ERROR; |
15734 | | } |
15735 | | ssl->options.ticketsSent++; |
15736 | | |
15737 | | return WOLFSSL_SUCCESS; |
15738 | | } |
15739 | | #endif |
15740 | | |
15741 | | #ifdef WOLFSSL_EARLY_DATA |
15742 | | /* Sets the maximum amount of early data that can be seen by server when using |
15743 | | * session tickets for resumption. |
15744 | | * A value of zero indicates no early data is to be sent by client using session |
15745 | | * tickets. |
15746 | | * |
15747 | | * The default value is zero: per RFC 8446 Appendix E.5, TLS implementations |
15748 | | * "MUST NOT enable 0-RTT (either sending or accepting) unless specifically |
15749 | | * requested by the application." Servers must explicitly opt in by calling |
15750 | | * this function (or the per-SSL equivalent) with a non-zero value. |
15751 | | * |
15752 | | * ctx The SSL/TLS CTX object. |
15753 | | * sz Maximum size of the early data. |
15754 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
15755 | | * 0 on success. |
15756 | | */ |
15757 | | int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz) |
15758 | | { |
15759 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
15760 | | return BAD_FUNC_ARG; |
15761 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
15762 | | return SIDE_ERROR; |
15763 | | |
15764 | | ctx->maxEarlyDataSz = sz; |
15765 | | |
15766 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
15767 | | /* 1 on success in OpenSSL*/ |
15768 | | return WOLFSSL_SUCCESS; |
15769 | | #else |
15770 | | return 0; |
15771 | | #endif |
15772 | | } |
15773 | | |
15774 | | /* Sets the maximum amount of early data that a client or server would like |
15775 | | * to exchange. Servers will advertise this value in session tickets sent |
15776 | | * to a client. |
15777 | | * A value of zero indicates no early data will be sent by a client, or |
15778 | | * no early data is accepted by a server (and announced as such in send out |
15779 | | * session tickets). |
15780 | | * |
15781 | | * ssl The SSL/TLS object. |
15782 | | * sz Maximum size of the early data. |
15783 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15784 | | * and 0 on success. |
15785 | | */ |
15786 | | int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz) |
15787 | | { |
15788 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15789 | | return BAD_FUNC_ARG; |
15790 | | |
15791 | | ssl->options.maxEarlyDataSz = sz; |
15792 | | #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_ERROR_CODE_OPENSSL) |
15793 | | /* 1 on success in OpenSSL*/ |
15794 | | return WOLFSSL_SUCCESS; |
15795 | | #else |
15796 | | return 0; |
15797 | | #endif |
15798 | | } |
15799 | | |
15800 | | /* Gets the maximum amount of early data that can be seen by server when using |
15801 | | * session tickets for resumption. |
15802 | | * A value of zero indicates no early data is to be sent by client using session |
15803 | | * tickets. |
15804 | | * |
15805 | | * ctx The SSL/TLS CTX object. |
15806 | | * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and |
15807 | | * returns the maximum amount of early data to be set |
15808 | | */ |
15809 | | int wolfSSL_CTX_get_max_early_data(WOLFSSL_CTX* ctx) |
15810 | | { |
15811 | | if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version)) |
15812 | | return BAD_FUNC_ARG; |
15813 | | if (ctx->method->side == WOLFSSL_CLIENT_END) |
15814 | | return SIDE_ERROR; |
15815 | | |
15816 | | return ctx->maxEarlyDataSz; |
15817 | | } |
15818 | | |
15819 | | /* Gets the maximum amount of early data that can be seen by server when using |
15820 | | * session tickets for resumption. |
15821 | | * A value of zero indicates no early data is to be sent by client using session |
15822 | | * tickets. |
15823 | | * |
15824 | | * ssl The SSL/TLS object. |
15825 | | * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3, |
15826 | | * SIDE_ERROR when not a server and |
15827 | | * returns the maximum amount of early data to be set |
15828 | | */ |
15829 | | int wolfSSL_get_max_early_data(WOLFSSL* ssl) |
15830 | | { |
15831 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
15832 | | return BAD_FUNC_ARG; |
15833 | | |
15834 | | return ssl->options.maxEarlyDataSz; |
15835 | | } |
15836 | | |
15837 | | /* Write early data to the server. |
15838 | | * |
15839 | | * ssl The SSL/TLS object. |
15840 | | * data Early data to write |
15841 | | * sz The size of the early data in bytes. |
15842 | | * outSz The number of early data bytes written. |
15843 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
15844 | | * or not using TLS v1.3. SIDE ERROR when not a server. BAD_STATE_E if invoked |
15845 | | * without a valid session or without a valid PSK CB. |
15846 | | * Otherwise the number of early data bytes written. |
15847 | | */ |
15848 | | int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz) |
15849 | | { |
15850 | | int ret = 0; |
15851 | | |
15852 | | WOLFSSL_ENTER("wolfSSL_write_early_data"); |
15853 | | |
15854 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
15855 | | return BAD_FUNC_ARG; |
15856 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
15857 | | return BAD_FUNC_ARG; |
15858 | | |
15859 | | *outSz = 0; |
15860 | | |
15861 | | #ifndef NO_WOLFSSL_CLIENT |
15862 | | if (ssl->options.side == WOLFSSL_SERVER_END) |
15863 | | return SIDE_ERROR; |
15864 | | |
15865 | | /* Early data requires PSK or session resumption */ |
15866 | | if (!EarlyDataPossible(ssl)) { |
15867 | | return BAD_STATE_E; |
15868 | | } |
15869 | | |
15870 | | if (ssl->options.handShakeState == NULL_STATE) { |
15871 | | /* avoid re-setting ssl->earlyData if we re-enter the function because |
15872 | | * of WC_PENDING_E, WANT_WRITE or WANT_READ */ |
15873 | | if (ssl->error == 0) |
15874 | | ssl->earlyData = expecting_early_data; |
15875 | | ret = wolfSSL_connect_TLSv13(ssl); |
15876 | | if (ret != WOLFSSL_SUCCESS) |
15877 | | return WOLFSSL_FATAL_ERROR; |
15878 | | /* on client side, status is set to rejected */ |
15879 | | /* until sever accepts the early data extension. */ |
15880 | | ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED; |
15881 | | } |
15882 | | if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) { |
15883 | | #ifdef OPENSSL_EXTRA |
15884 | | /* when processed early data exceeds max size */ |
15885 | | if (ssl->session->maxEarlyDataSz > 0 && |
15886 | | (ssl->earlyDataSz + sz > ssl->session->maxEarlyDataSz)) { |
15887 | | ssl->error = TOO_MUCH_EARLY_DATA; |
15888 | | return WOLFSSL_FATAL_ERROR; |
15889 | | } |
15890 | | #endif |
15891 | | ret = SendData(ssl, data, sz); |
15892 | | if (ret > 0) { |
15893 | | *outSz = ret; |
15894 | | /* store amount of processed early data from client */ |
15895 | | ssl->earlyDataSz += ret; |
15896 | | } |
15897 | | } |
15898 | | #else |
15899 | | return SIDE_ERROR; |
15900 | | #endif |
15901 | | |
15902 | | WOLFSSL_LEAVE("wolfSSL_write_early_data", ret); |
15903 | | |
15904 | | if (ret < 0) |
15905 | | ret = WOLFSSL_FATAL_ERROR; |
15906 | | return ret; |
15907 | | } |
15908 | | |
15909 | | /* Read the any early data from the client. |
15910 | | * |
15911 | | * ssl The SSL/TLS object. |
15912 | | * data Buffer to put the early data into. |
15913 | | * sz The size of the buffer in bytes. |
15914 | | * outSz The number of early data bytes read. |
15915 | | * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative; |
15916 | | * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of |
15917 | | * early data bytes read. |
15918 | | */ |
15919 | | int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz) |
15920 | | { |
15921 | | int ret = 0; |
15922 | | |
15923 | | WOLFSSL_ENTER("wolfSSL_read_early_data"); |
15924 | | |
15925 | | |
15926 | | if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL) |
15927 | | return BAD_FUNC_ARG; |
15928 | | if (!IsAtLeastTLSv1_3(ssl->version)) |
15929 | | return BAD_FUNC_ARG; |
15930 | | |
15931 | | *outSz = 0; |
15932 | | #ifndef NO_WOLFSSL_SERVER |
15933 | | if (ssl->options.side == WOLFSSL_CLIENT_END) |
15934 | | return SIDE_ERROR; |
15935 | | |
15936 | | if (ssl->options.handShakeState == NULL_STATE) { |
15937 | | /* the server flight can return WANT_WRITE and we re-enter here after |
15938 | | * setting ssl->earlyData = process_early_data, set earlyData to |
15939 | | * expecting_early_data just once */ |
15940 | | if (ssl->earlyData < expecting_early_data) |
15941 | | ssl->earlyData = expecting_early_data; |
15942 | | /* this used to be: ret = wolfSSL_accept_TLSv13(ssl); |
15943 | | * However, wolfSSL_accept_TLSv13() expects a certificate to |
15944 | | * be installed already, which is not the case in servers |
15945 | | * such as HAProxy. They do it after inspecting the ClientHello. |
15946 | | * The common wolfssl_accept() allows that. */ |
15947 | | ret = wolfSSL_accept(ssl); |
15948 | | if (ret <= 0) |
15949 | | return WOLFSSL_FATAL_ERROR; |
15950 | | } |
15951 | | if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) { |
15952 | | ssl->options.clientInEarlyData = 1; |
15953 | | ret = ReceiveData(ssl, (byte*)data, (size_t)sz, FALSE); |
15954 | | ssl->options.clientInEarlyData = 0; |
15955 | | if (ret > 0) |
15956 | | *outSz = ret; |
15957 | | if (ssl->error == WC_NO_ERR_TRACE(APP_DATA_READY)) { |
15958 | | ret = 0; |
15959 | | ssl->error = WOLFSSL_ERROR_NONE; |
15960 | | #ifdef WOLFSSL_DTLS13 |
15961 | | if (ssl->options.dtls) { |
15962 | | ret = Dtls13DoScheduledWork(ssl); |
15963 | | if (ret < 0) { |
15964 | | ssl->error = ret; |
15965 | | WOLFSSL_ERROR(ssl->error); |
15966 | | return WOLFSSL_FATAL_ERROR; |
15967 | | } |
15968 | | } |
15969 | | #endif /* WOLFSSL_DTLS13 */ |
15970 | | } |
15971 | | } |
15972 | | #ifdef WOLFSSL_DTLS13 |
15973 | | else if (ssl->buffers.outputBuffer.length > 0 && |
15974 | | ssl->options.dtls && ssl->dtls13SendingAckOrRtx) { |
15975 | | ret = SendBuffered(ssl); |
15976 | | if (ret == 0) { |
15977 | | ssl->dtls13SendingAckOrRtx = 0; |
15978 | | } |
15979 | | else { |
15980 | | ssl->error = ret; |
15981 | | WOLFSSL_ERROR(ssl->error); |
15982 | | return WOLFSSL_FATAL_ERROR; |
15983 | | } |
15984 | | } |
15985 | | #endif /* WOLFSSL_DTLS13 */ |
15986 | | else |
15987 | | ret = 0; |
15988 | | #else |
15989 | | return SIDE_ERROR; |
15990 | | #endif |
15991 | | |
15992 | | WOLFSSL_LEAVE("wolfSSL_read_early_data", ret); |
15993 | | |
15994 | | if (ret < 0) |
15995 | | ret = WOLFSSL_FATAL_ERROR; |
15996 | | return ret; |
15997 | | } |
15998 | | |
15999 | | /* Returns early data status |
16000 | | * |
16001 | | * ssl The SSL/TLS object. |
16002 | | * returns WOLFSSL_EARLY_DATA_ACCEPTED if the data was accepted |
16003 | | * WOLFSSL_EARLY_DATA_REJECTED if the data was rejected |
16004 | | * WOLFSSL_EARLY_DATA_NOT_SENT if no early data was sent |
16005 | | */ |
16006 | | int wolfSSL_get_early_data_status(const WOLFSSL* ssl) |
16007 | | { |
16008 | | if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version)) |
16009 | | return BAD_FUNC_ARG; |
16010 | | |
16011 | | return ssl->earlyDataStatus; |
16012 | | } |
16013 | | #endif |
16014 | | |
16015 | | #ifdef HAVE_SECRET_CALLBACK |
16016 | | int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx) |
16017 | | { |
16018 | | WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb"); |
16019 | | if (ssl == NULL) |
16020 | | return WOLFSSL_FATAL_ERROR; |
16021 | | |
16022 | | ssl->tls13SecretCb = cb; |
16023 | | ssl->tls13SecretCtx = ctx; |
16024 | | |
16025 | | return WOLFSSL_SUCCESS; |
16026 | | } |
16027 | | |
16028 | | #if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE) |
16029 | | int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret, |
16030 | | int secretSz, void* ctx) |
16031 | | { |
16032 | | int i; |
16033 | | const char* str = NULL; |
16034 | | byte clientRandom[RAN_LEN]; |
16035 | | int clientRandomSz; |
16036 | | XFILE fp; |
16037 | | |
16038 | | (void) ctx; |
16039 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
16040 | | fp = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "ab"); |
16041 | | if (fp == XBADFILE) { |
16042 | | return BAD_FUNC_ARG; |
16043 | | } |
16044 | | #else |
16045 | | fp = stderr; |
16046 | | #endif |
16047 | | |
16048 | | clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom, |
16049 | | sizeof(clientRandom)); |
16050 | | |
16051 | | if (clientRandomSz <= 0) { |
16052 | | printf("Error getting server random %d\n", clientRandomSz); |
16053 | | return BAD_FUNC_ARG; |
16054 | | } |
16055 | | |
16056 | | #if 0 |
16057 | | printf("TLS Server Secret CB: Rand %d, Secret %d\n", |
16058 | | serverRandomSz, secretSz); |
16059 | | #endif |
16060 | | |
16061 | | switch (id) { |
16062 | | case CLIENT_EARLY_TRAFFIC_SECRET: |
16063 | | str = "CLIENT_EARLY_TRAFFIC_SECRET"; break; |
16064 | | case EARLY_EXPORTER_SECRET: |
16065 | | str = "EARLY_EXPORTER_SECRET"; break; |
16066 | | case CLIENT_HANDSHAKE_TRAFFIC_SECRET: |
16067 | | str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break; |
16068 | | case SERVER_HANDSHAKE_TRAFFIC_SECRET: |
16069 | | str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break; |
16070 | | case CLIENT_TRAFFIC_SECRET: |
16071 | | str = "CLIENT_TRAFFIC_SECRET_0"; break; |
16072 | | case SERVER_TRAFFIC_SECRET: |
16073 | | str = "SERVER_TRAFFIC_SECRET_0"; break; |
16074 | | case EXPORTER_SECRET: |
16075 | | str = "EXPORTER_SECRET"; break; |
16076 | | default: |
16077 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
16078 | | XFCLOSE(fp); |
16079 | | #endif |
16080 | | return BAD_FUNC_ARG; |
16081 | | break; |
16082 | | } |
16083 | | |
16084 | | fprintf(fp, "%s ", str); |
16085 | | for (i = 0; i < (int)clientRandomSz; i++) { |
16086 | | fprintf(fp, "%02x", clientRandom[i]); |
16087 | | } |
16088 | | fprintf(fp, " "); |
16089 | | for (i = 0; i < secretSz; i++) { |
16090 | | fprintf(fp, "%02x", secret[i]); |
16091 | | } |
16092 | | fprintf(fp, "\n"); |
16093 | | |
16094 | | #ifdef WOLFSSL_SSLKEYLOGFILE_OUTPUT |
16095 | | XFCLOSE(fp); |
16096 | | #endif |
16097 | | |
16098 | | return 0; |
16099 | | } |
16100 | | #endif |
16101 | | #endif |
16102 | | |
16103 | | #undef ERROR_OUT |
16104 | | |
16105 | | #endif /* !WOLFCRYPT_ONLY */ |
16106 | | |
16107 | | #endif /* !NO_TLS && WOLFSSL_TLS13 */ |