Coverage Report

Created: 2026-05-18 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-sp-math-all-8bit/wolfssl/internal.h
Line
Count
Source
1
/* internal.h
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
23
24
#ifndef WOLFSSL_INT_H
25
#define WOLFSSL_INT_H
26
27
#include <wolfssl/wolfcrypt/types.h>
28
#include <wolfssl/ssl.h>
29
#include <wolfssl/wolfio.h>
30
#ifdef HAVE_CRL
31
    #include <wolfssl/crl.h>
32
#endif
33
#include <wolfssl/wolfcrypt/random.h>
34
#ifndef NO_DES3
35
    #include <wolfssl/wolfcrypt/des3.h>
36
#endif
37
#ifdef HAVE_CHACHA
38
    #include <wolfssl/wolfcrypt/chacha.h>
39
#endif
40
#ifndef NO_ASN
41
    #include <wolfssl/wolfcrypt/asn.h>
42
    #include <wolfssl/wolfcrypt/pkcs12.h>
43
#endif
44
#ifndef NO_MD5
45
    #include <wolfssl/wolfcrypt/md5.h>
46
#endif
47
#ifndef NO_SHA
48
    #include <wolfssl/wolfcrypt/sha.h>
49
#endif
50
#ifndef NO_AES
51
    #include <wolfssl/wolfcrypt/aes.h>
52
#endif
53
#ifdef HAVE_POLY1305
54
    #include <wolfssl/wolfcrypt/poly1305.h>
55
#endif
56
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && defined(OPENSSL_EXTRA)
57
    #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
58
#endif
59
#ifdef HAVE_ARIA
60
    #include <wolfssl/wolfcrypt/port/aria/aria-crypt.h>
61
#endif
62
#ifdef HAVE_CAMELLIA
63
    #include <wolfssl/wolfcrypt/camellia.h>
64
#endif
65
#ifdef WOLFSSL_SM4
66
    #include <wolfssl/wolfcrypt/sm4.h>
67
#endif
68
#include <wolfssl/wolfcrypt/logging.h>
69
#ifndef NO_HMAC
70
    #include <wolfssl/wolfcrypt/hmac.h>
71
#endif
72
#ifndef NO_RC4
73
    #include <wolfssl/wolfcrypt/arc4.h>
74
#endif
75
#ifndef NO_SHA256
76
    #include <wolfssl/wolfcrypt/sha256.h>
77
#endif
78
#if defined(WOLFSSL_SHA384)
79
    #include <wolfssl/wolfcrypt/sha512.h>
80
#endif
81
#ifdef HAVE_OCSP
82
    #include <wolfssl/ocsp.h>
83
#endif
84
#ifdef WOLFSSL_QUIC
85
    #include <wolfssl/quic.h>
86
#endif
87
#ifdef WOLFSSL_SHA384
88
    #include <wolfssl/wolfcrypt/sha512.h>
89
#endif
90
#ifdef WOLFSSL_SHA512
91
    #include <wolfssl/wolfcrypt/sha512.h>
92
#endif
93
#ifdef WOLFSSL_SM3
94
    #include <wolfssl/wolfcrypt/sm3.h>
95
#endif
96
#ifdef HAVE_AESGCM
97
    #include <wolfssl/wolfcrypt/sha512.h>
98
#endif
99
#ifdef WOLFSSL_RIPEMD
100
    #include <wolfssl/wolfcrypt/ripemd.h>
101
#endif
102
#ifndef NO_RSA
103
    #include <wolfssl/wolfcrypt/rsa.h>
104
#endif
105
#ifdef HAVE_ECC
106
    #include <wolfssl/wolfcrypt/ecc.h>
107
#endif
108
#ifdef WOLFSSL_SM2
109
    #include <wolfssl/wolfcrypt/sm2.h>
110
#endif
111
#ifndef NO_DH
112
    #include <wolfssl/wolfcrypt/dh.h>
113
#endif
114
#ifdef HAVE_ED25519
115
    #include <wolfssl/wolfcrypt/ed25519.h>
116
#endif
117
#ifdef HAVE_CURVE25519
118
    #include <wolfssl/wolfcrypt/curve25519.h>
119
#endif
120
#ifdef HAVE_ED448
121
    #include <wolfssl/wolfcrypt/ed448.h>
122
#endif
123
#ifdef HAVE_CURVE448
124
    #include <wolfssl/wolfcrypt/curve448.h>
125
#endif
126
#ifdef HAVE_FALCON
127
    #include <wolfssl/wolfcrypt/falcon.h>
128
#endif
129
#ifdef HAVE_DILITHIUM
130
    #include <wolfssl/wolfcrypt/dilithium.h>
131
#endif
132
#ifdef HAVE_HKDF
133
    #include <wolfssl/wolfcrypt/kdf.h>
134
#endif
135
#ifndef WOLFSSL_NO_DEF_TICKET_ENC_CB
136
    #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \
137
        !defined(WOLFSSL_TICKET_ENC_AES128_GCM) && \
138
        !defined(WOLFSSL_TICKET_ENC_AES256_GCM)
139
        #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
140
    #else
141
        #include <wolfssl/wolfcrypt/aes.h>
142
    #endif
143
#endif
144
145
#include <wolfssl/wolfcrypt/wc_encrypt.h>
146
#include <wolfssl/wolfcrypt/hash.h>
147
148
#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
149
    #include <wolfssl/callbacks.h>
150
#endif
151
#ifdef WOLFSSL_CALLBACKS
152
    #include <signal.h>
153
#endif
154
155
#ifdef WOLFSSL_APACHE_MYNEWT
156
    #if !defined(WOLFSSL_LWIP)
157
        void mynewt_ctx_clear(void *ctx);
158
        void* mynewt_ctx_new();
159
    #endif
160
#endif
161
162
#if !defined(WOLFCRYPT_ONLY) && !defined(INT_MAX)
163
    /* Needed for TLS/DTLS limit checking (Added in 91aad90c59 Jan 24, 2025) */
164
    #include <limits.h>
165
#endif
166
167
168
#ifdef HAVE_LIBZ
169
    #include "zlib.h"
170
#endif
171
172
#ifdef WOLFSSL_ASYNC_CRYPT
173
    #include <wolfssl/wolfcrypt/async.h>
174
#endif
175
176
#ifdef OPENSSL_EXTRA
177
    #ifdef WOLFCRYPT_HAVE_SRP
178
        #include <wolfssl/wolfcrypt/srp.h>
179
    #endif
180
#endif
181
182
#ifdef _MSC_VER
183
    /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
184
    #pragma warning(disable: 4996)
185
#endif
186
187
#ifdef NO_SHA
188
    #define WC_SHA_DIGEST_SIZE 20
189
#endif
190
191
#ifdef NO_SHA256
192
    #define WC_SHA256_DIGEST_SIZE 32
193
#endif
194
195
#ifdef NO_MD5
196
    #define WC_MD5_DIGEST_SIZE 16
197
#endif
198
199
#ifdef WOLFSSL_IOTSAFE
200
    #include <wolfssl/wolfcrypt/port/iotsafe/iotsafe.h>
201
#endif
202
203
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
204
    #include <wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h>
205
#endif
206
207
#include <wolfssl/wolfcrypt/hpke.h>
208
209
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE)
210
#include <wolfssl/sniffer.h>
211
#endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */
212
213
#ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION
214
    #include <CoreFoundation/CoreFoundation.h>
215
#endif /* WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION */
216
217
#ifdef __cplusplus
218
    extern "C" {
219
#endif
220
221
/* Define or comment out the cipher suites you'd like to be compiled in
222
   make sure to use at least one BUILD_SSL_xxx or BUILD_TLS_xxx is defined
223
224
   When adding cipher suites, add name to cipher_names, idx to cipher_name_idx
225
226
   Now that there is a maximum strength crypto build, the following BUILD_XXX
227
   flags need to be divided into two groups selected by WOLFSSL_MAX_STRENGTH.
228
   Those that do not use Perfect Forward Security and do not use AEAD ciphers
229
   need to be switched off. Allowed suites use (EC)DHE, AES-GCM|CCM, or
230
   CHACHA-POLY.
231
*/
232
233
/* Check that if WOLFSSL_MAX_STRENGTH is set that all the required options are
234
 * not turned off. */
235
#if defined(WOLFSSL_MAX_STRENGTH) && \
236
    ((!defined(HAVE_ECC) && (defined(NO_DH) || defined(NO_RSA))) || \
237
     (!defined(HAVE_AESGCM) && !defined(HAVE_AESCCM) && \
238
      (!defined(HAVE_POLY1305) || !defined(HAVE_CHACHA))) || \
239
     (defined(NO_SHA256) && !defined(WOLFSSL_SHA384)) || \
240
     !defined(NO_OLD_TLS))
241
242
    #error "You are trying to build max strength with requirements disabled."
243
#endif
244
245
#ifndef WOLFSSL_NO_TLS12
246
247
#ifndef WOLFSSL_MAX_STRENGTH
248
249
#ifdef WOLFSSL_AEAD_ONLY
250
    /* AES CBC ciphers are not allowed in AEAD only mode */
251
    #undef HAVE_AES_CBC
252
#endif
253
254
/* When adding new ciphersuites, make sure that they have appropriate
255
 * guards for WOLFSSL_HARDEN_TLS. */
256
#if defined(WOLFSSL_HARDEN_TLS) && \
257
    !defined(WOLFSSL_HARDEN_TLS_ALLOW_ALL_CIPHERSUITES)
258
/* Use a separate define (undef'ed later) to simplify macro logic. */
259
#define WSSL_HARDEN_TLS WOLFSSL_HARDEN_TLS
260
#define NO_TLS_DH
261
#endif
262
263
#ifndef WOLFSSL_AEAD_ONLY
264
    #if !defined(NO_RSA) && !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
265
        /* MUST NOT negotiate RC4 cipher suites
266
         * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
267
        #if defined(WOLFSSL_STATIC_RSA)
268
            #if !defined(NO_SHA)
269
                #define BUILD_SSL_RSA_WITH_RC4_128_SHA
270
            #endif
271
            #if !defined(NO_MD5)
272
                #define BUILD_SSL_RSA_WITH_RC4_128_MD5
273
            #endif
274
        #endif
275
    #endif
276
277
    #if !defined(NO_RSA) && !defined(NO_DES3) && !defined(NO_DES3_TLS_SUITES)
278
        #if !defined(NO_SHA)
279
            #if defined(WOLFSSL_STATIC_RSA)
280
                #define BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
281
            #endif
282
        #endif
283
    #endif
284
#endif /* !WOLFSSL_AEAD_ONLY */
285
286
    #if !defined(NO_RSA) && !defined(NO_AES) && !defined(NO_TLS)
287
        #if !defined(NO_SHA) && defined(HAVE_AES_CBC)
288
            #if defined(WOLFSSL_STATIC_RSA)
289
                #ifdef WOLFSSL_AES_128
290
                    #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
291
                #endif
292
                #ifdef WOLFSSL_AES_256
293
                    #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
294
                #endif
295
            #endif
296
        #endif
297
        #if defined(WOLFSSL_STATIC_RSA)
298
            #if !defined (NO_SHA256) && defined(HAVE_AES_CBC)
299
                #ifdef WOLFSSL_AES_128
300
                    #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
301
                #endif
302
                #ifdef WOLFSSL_AES_256
303
                    #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
304
                #endif
305
            #endif
306
            #if defined (HAVE_AESGCM)
307
                #ifdef WOLFSSL_AES_128
308
                    #define BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
309
                #endif
310
                #if defined (WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
311
                    #define BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
312
                #endif
313
            #endif
314
            #if defined (HAVE_AESCCM)
315
                #ifdef WOLFSSL_AES_128
316
                    #define BUILD_TLS_RSA_WITH_AES_128_CCM_8
317
                #endif
318
                #ifdef WOLFSSL_AES_256
319
                    #define BUILD_TLS_RSA_WITH_AES_256_CCM_8
320
                #endif
321
            #endif
322
        #endif
323
    #endif
324
325
    #if defined(HAVE_CAMELLIA) && !defined(NO_TLS) && !defined(NO_CAMELLIA_CBC)
326
        #ifndef NO_RSA
327
          #if defined(WOLFSSL_STATIC_RSA)
328
            #if !defined(NO_SHA)
329
                #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
330
                #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
331
            #endif
332
            #ifndef NO_SHA256
333
                #define BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
334
                #define BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
335
            #endif
336
          #endif
337
            #if !defined(NO_DH) && !defined(NO_TLS_DH)
338
              /* SHOULD NOT negotiate cipher suites based on ephemeral
339
               * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
340
               * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
341
              #if !defined(NO_SHA)
342
                #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
343
                #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
344
              #endif
345
                #ifndef NO_SHA256
346
                    #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
347
                    #define BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
348
                #endif
349
            #endif
350
        #endif
351
    #endif
352
353
#if defined(WOLFSSL_STATIC_PSK)
354
    #if !defined(NO_PSK) && !defined(NO_AES) && !defined(NO_TLS)
355
        #if !defined(NO_SHA)
356
            #ifdef WOLFSSL_AES_128
357
                #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
358
            #endif
359
            #ifdef WOLFSSL_AES_256
360
                #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
361
            #endif
362
        #endif
363
        #ifndef NO_SHA256
364
            #ifdef WOLFSSL_AES_128
365
                #ifdef HAVE_AES_CBC
366
                    #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
367
                #endif
368
                #ifdef HAVE_AESGCM
369
                    #define BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
370
                #endif
371
            #endif /* WOLFSSL_AES_128 */
372
            #ifdef HAVE_AESCCM
373
                #ifdef WOLFSSL_AES_128
374
                    #define BUILD_TLS_PSK_WITH_AES_128_CCM_8
375
                    #define BUILD_TLS_PSK_WITH_AES_128_CCM
376
                #endif
377
                #ifdef WOLFSSL_AES_256
378
                    #define BUILD_TLS_PSK_WITH_AES_256_CCM_8
379
                    #define BUILD_TLS_PSK_WITH_AES_256_CCM
380
                #endif
381
            #endif
382
        #endif
383
        #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
384
            #ifdef HAVE_AES_CBC
385
                #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
386
            #endif
387
            #ifdef HAVE_AESGCM
388
                #define BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
389
            #endif
390
        #endif
391
    #endif
392
#endif
393
394
    #if !defined(NO_TLS) && defined(HAVE_NULL_CIPHER)
395
        #if !defined(NO_RSA)
396
            #if defined(WOLFSSL_STATIC_RSA)
397
                #ifndef NO_MD5
398
                    #define BUILD_TLS_RSA_WITH_NULL_MD5
399
                #endif
400
                #if !defined(NO_SHA)
401
                    #define BUILD_TLS_RSA_WITH_NULL_SHA
402
                #endif
403
                #ifndef NO_SHA256
404
                    #define BUILD_TLS_RSA_WITH_NULL_SHA256
405
                #endif
406
            #endif
407
        #endif
408
        #if !defined(NO_PSK) && defined(WOLFSSL_STATIC_PSK)
409
            #if !defined(NO_SHA)
410
                #define BUILD_TLS_PSK_WITH_NULL_SHA
411
            #endif
412
            #ifndef NO_SHA256
413
                #define BUILD_TLS_PSK_WITH_NULL_SHA256
414
            #endif
415
            #ifdef WOLFSSL_SHA384
416
                #define BUILD_TLS_PSK_WITH_NULL_SHA384
417
            #endif
418
        #endif
419
    #endif
420
421
    #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
422
        !defined(NO_RSA) && !defined(NO_TLS_DH)
423
        /* SHOULD NOT negotiate cipher suites based on ephemeral
424
         * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
425
         * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
426
427
        #if !defined(NO_SHA)
428
            #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
429
                #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
430
            #endif
431
            #if defined(WOLFSSL_AES_256) && defined(HAVE_AES_CBC)
432
                #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
433
            #endif
434
            #if !defined(NO_DES3) && !defined(NO_DES3_TLS_SUITES)
435
                #define BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
436
            #endif
437
        #endif
438
        #if !defined(NO_SHA256) && defined(HAVE_AES_CBC)
439
            #ifdef WOLFSSL_AES_128
440
                #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
441
            #endif
442
            #ifdef WOLFSSL_AES_256
443
                #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
444
            #endif
445
        #endif
446
    #endif
447
448
    #if defined(HAVE_ANON) && !defined(NO_TLS) && !defined(NO_DH) && \
449
        !defined(NO_AES)
450
        #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && \
451
                defined(WOLFSSL_AES_128)
452
            #define BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
453
        #endif
454
        #if defined(WOLFSSL_SHA384) && defined(HAVE_AESGCM) && \
455
                defined(WOLFSSL_AES_256)
456
            #define BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
457
        #endif
458
    #endif
459
460
    #if !defined(NO_DH) && !defined(NO_PSK) && !defined(NO_TLS) && \
461
        !defined(NO_TLS_DH)
462
        /* SHOULD NOT negotiate cipher suites based on ephemeral
463
         * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
464
         * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
465
        #ifndef NO_SHA256
466
            #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && \
467
                                                           defined(HAVE_AES_CBC)
468
                #define BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
469
            #endif
470
            #ifdef HAVE_NULL_CIPHER
471
                #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
472
            #endif
473
        #endif
474
        #ifdef WOLFSSL_SHA384
475
            #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && \
476
                                                           defined(HAVE_AES_CBC)
477
                #define BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
478
            #endif
479
            #ifdef HAVE_NULL_CIPHER
480
                #define BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
481
            #endif
482
        #endif
483
    #endif
484
485
    #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
486
                                     defined(HAVE_CURVE448)) && !defined(NO_TLS)
487
        #if !defined(NO_AES)
488
            #if !defined(NO_SHA) && defined(HAVE_AES_CBC)
489
                #if !defined(NO_RSA)
490
                    #ifdef WOLFSSL_AES_128
491
                        #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
492
                    #endif
493
                    #ifdef WOLFSSL_AES_256
494
                        #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
495
                    #endif
496
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
497
                        #ifdef WOLFSSL_AES_128
498
                            #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
499
                        #endif
500
                        #ifdef WOLFSSL_AES_256
501
                            #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
502
                        #endif
503
                    #endif
504
                #endif
505
506
                #if defined(HAVE_ECC) || \
507
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
508
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
509
                    #ifdef WOLFSSL_AES_128
510
                        #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
511
                    #endif
512
                    #ifdef WOLFSSL_AES_256
513
                        #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
514
                    #endif
515
                #endif
516
517
                #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
518
                    #ifdef WOLFSSL_AES_128
519
                        #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
520
                    #endif
521
                    #ifdef WOLFSSL_AES_256
522
                        #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
523
                    #endif
524
                #endif
525
            #endif /* NO_SHA */
526
            #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
527
                                                           defined(HAVE_AES_CBC)
528
                #if !defined(NO_RSA)
529
                    #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
530
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
531
                        #define BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
532
                    #endif
533
                #endif
534
                #if defined(HAVE_ECC) || \
535
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
536
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
537
                    #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
538
                #endif
539
                #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
540
                    #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
541
                #endif
542
            #endif
543
544
            #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
545
                                                           defined(HAVE_AES_CBC)
546
                #if !defined(NO_RSA)
547
                    #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
548
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
549
                        #define BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
550
                    #endif
551
                #endif
552
                #if defined(HAVE_ECC) || \
553
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
554
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
555
                    #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
556
                #endif
557
                #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
558
                    #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
559
                #endif
560
            #endif
561
562
            #if defined (HAVE_AESGCM)
563
                #if !defined(NO_RSA)
564
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
565
                        #ifdef WOLFSSL_AES_128
566
                            #define BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
567
                        #endif
568
                    #endif
569
                    #if defined(WOLFSSL_SHA384)
570
                        #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
571
                            #ifdef WOLFSSL_AES_256
572
                                #define BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
573
                            #endif
574
                        #endif
575
                    #endif
576
                #endif
577
578
                #if defined(WOLFSSL_STATIC_DH) && defined(WOLFSSL_AES_128) && \
579
                                                               defined(HAVE_ECC)
580
                    #define BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
581
                #endif
582
583
                #if defined(WOLFSSL_SHA384)
584
                    #if defined(WOLFSSL_STATIC_DH) && \
585
                                   defined(WOLFSSL_AES_256) && defined(HAVE_ECC)
586
                        #define BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
587
                    #endif
588
                #endif
589
            #endif
590
        #endif /* NO_AES */
591
        #ifdef HAVE_ARIA
592
            #define BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256
593
            #define BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384
594
        #endif /* HAVE_ARIA */
595
        #if !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
596
            /* MUST NOT negotiate RC4 cipher suites
597
             * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
598
            #if !defined(NO_SHA)
599
                #if !defined(NO_RSA)
600
                    #ifndef WOLFSSL_AEAD_ONLY
601
                        #define BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
602
                    #endif
603
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
604
                        #define BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
605
                    #endif
606
                #endif
607
608
                #if defined(HAVE_ECC) || \
609
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
610
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
611
                    #ifndef WOLFSSL_AEAD_ONLY
612
                        #define BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
613
                    #endif
614
                #endif
615
                #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
616
                    #define BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
617
                #endif
618
            #endif
619
        #endif
620
        #if !defined(NO_DES3) && !(defined(WSSL_HARDEN_TLS) && \
621
                                           WSSL_HARDEN_TLS > 112) && \
622
            !defined(NO_DES3_TLS_SUITES)
623
            /* 3DES offers only 112 bits of security.
624
             * Using guidance from section 5.6.1
625
             * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
626
            #ifndef NO_SHA
627
                #if !defined(NO_RSA)
628
                    #define BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
629
                    #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
630
                        #define BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
631
                    #endif
632
                #endif
633
634
                #if defined(HAVE_ECC) || \
635
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
636
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
637
                    #define BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
638
                #endif
639
                #if defined(WOLFSSL_STATIC_DH) && defined(HAVE_ECC)
640
                    #define BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
641
                #endif
642
            #endif /* NO_SHA */
643
        #endif
644
        #if defined(HAVE_NULL_CIPHER)
645
            #if !defined(NO_SHA)
646
                #if defined(HAVE_ECC) || \
647
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
648
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
649
                    #define BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
650
                #endif
651
            #endif
652
            #if !defined(NO_PSK) && !defined(NO_SHA256)
653
                #define BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
654
            #endif
655
        #endif
656
        #if !defined(NO_PSK) && !defined(NO_SHA256) && !defined(NO_AES) && \
657
            defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
658
            #define BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
659
        #endif
660
        #if !defined(NO_PSK) && !defined(NO_SHA256) && !defined(NO_AES) && \
661
            defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM)
662
            #define BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256
663
        #endif
664
    #endif
665
    #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
666
        #if !defined(NO_OLD_POLY1305)
667
        #if defined(HAVE_ECC) || \
668
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
669
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
670
            #define BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
671
        #endif
672
        #if !defined(NO_RSA) && defined(HAVE_ECC)
673
            #define BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
674
        #endif
675
        #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
676
            /* SHOULD NOT negotiate cipher suites based on ephemeral
677
             * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
678
             * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
679
            #define BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
680
        #endif
681
        #endif /* NO_OLD_POLY1305 */
682
        #if !defined(NO_PSK)
683
            #define BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
684
            #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
685
                                                             defined(HAVE_ED448)
686
                #define BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
687
            #endif
688
            #if !defined(NO_DH) && !defined(NO_TLS_DH)
689
                /* SHOULD NOT negotiate cipher suites based on ephemeral
690
                 * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
691
                 * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
692
                #define BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
693
            #endif
694
        #endif /* !NO_PSK */
695
    #endif
696
697
#endif /* !WOLFSSL_MAX_STRENGTH */
698
699
#if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
700
    !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
701
    /* SHOULD NOT negotiate cipher suites based on ephemeral
702
     * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
703
     * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
704
705
    #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
706
        #define BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
707
    #endif
708
709
    #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
710
        #define BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
711
    #endif
712
#endif
713
714
#if !defined(NO_DH) && !defined(NO_PSK) && !defined(NO_TLS) && \
715
    !defined(NO_TLS_DH)
716
    /* SHOULD NOT negotiate cipher suites based on ephemeral
717
     * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
718
     * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
719
    #ifndef NO_SHA256
720
        #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
721
            #define BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
722
        #endif
723
        #ifdef HAVE_AESCCM
724
            #ifdef WOLFSSL_AES_128
725
                #define BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
726
            #endif
727
            #ifdef WOLFSSL_AES_256
728
                #define BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
729
            #endif
730
        #endif
731
    #endif
732
    #if defined(WOLFSSL_SHA384) && defined(HAVE_AESGCM) && \
733
        defined(WOLFSSL_AES_256)
734
        #define BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
735
    #endif
736
#endif
737
738
#if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
739
                                         && !defined(NO_TLS) && !defined(NO_AES)
740
    #ifdef HAVE_AESGCM
741
        #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
742
            #if defined(HAVE_ECC) || \
743
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
744
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
745
                #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
746
            #endif
747
            #ifndef NO_RSA
748
                #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
749
            #endif
750
        #endif
751
        #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
752
            #if defined(HAVE_ECC) || \
753
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
754
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
755
                #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
756
            #endif
757
            #ifndef NO_RSA
758
                #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
759
            #endif
760
        #endif
761
    #endif
762
    #if defined(HAVE_AESCCM) && !defined(NO_SHA256)
763
        #if defined(HAVE_ECC) || \
764
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
765
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
766
            #ifdef WOLFSSL_AES_128
767
                #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
768
                #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
769
            #endif
770
            #ifdef WOLFSSL_AES_256
771
                #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
772
            #endif
773
        #endif
774
    #endif
775
#endif
776
777
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
778
    #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
779
        #if defined(HAVE_ECC) || \
780
                        (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) || \
781
                        (defined(HAVE_CURVE448) && defined(HAVE_ED448))
782
            #define BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
783
        #endif
784
        #ifndef NO_RSA
785
            #define BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
786
        #endif
787
    #endif
788
    #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
789
        /* SHOULD NOT negotiate cipher suites based on ephemeral
790
         * finite-field Diffie-Hellman key agreement (i.e., "TLS_DHE_*"
791
         * suites). https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
792
        #define BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
793
    #endif
794
#endif
795
796
    #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
797
        #ifdef WOLFSSL_SM4_CBC
798
            #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
799
        #endif
800
        #ifdef WOLFSSL_SM4_GCM
801
            #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3
802
        #endif
803
        #ifdef WOLFSSL_SM4_CCM
804
            #define BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3
805
        #endif
806
    #endif
807
#endif
808
809
#if defined(WOLFSSL_TLS13)
810
    #ifdef HAVE_AESGCM
811
        #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
812
            #define BUILD_TLS_AES_128_GCM_SHA256
813
        #endif
814
        #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
815
            #define BUILD_TLS_AES_256_GCM_SHA384
816
        #endif
817
    #endif
818
819
    #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
820
        #ifndef NO_SHA256
821
            #define BUILD_TLS_CHACHA20_POLY1305_SHA256
822
        #endif
823
    #endif
824
825
    #ifdef HAVE_AESCCM
826
        #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
827
            #define BUILD_TLS_AES_128_CCM_SHA256
828
            #define BUILD_TLS_AES_128_CCM_8_SHA256
829
        #endif
830
    #endif
831
    #ifdef HAVE_NULL_CIPHER
832
        #ifndef NO_SHA256
833
            #define BUILD_TLS_SHA256_SHA256
834
        #endif
835
        #ifdef WOLFSSL_SHA384
836
            #define BUILD_TLS_SHA384_SHA384
837
        #endif
838
    #endif
839
840
    #ifdef WOLFSSL_SM3
841
        #ifdef WOLFSSL_SM4_GCM
842
            #define BUILD_TLS_SM4_GCM_SM3
843
        #endif
844
845
        #ifdef WOLFSSL_SM4_CCM
846
            #define BUILD_TLS_SM4_CCM_SM3
847
        #endif
848
    #endif
849
#endif
850
851
#if !defined(WOLFCRYPT_ONLY) && defined(NO_PSK) && \
852
    (defined(NO_DH) || !defined(HAVE_ANON)) && \
853
    defined(NO_RSA) && !defined(HAVE_ECC) && \
854
    !defined(HAVE_ED25519) && !defined(HAVE_ED448)
855
   #error "No cipher suites available with this build"
856
#endif
857
858
#ifdef WOLFSSL_MULTICAST
859
    #if defined(HAVE_NULL_CIPHER) && !defined(NO_SHA256)
860
        #define BUILD_WDM_WITH_NULL_SHA256
861
    #endif
862
#endif
863
864
#if defined(BUILD_SSL_RSA_WITH_RC4_128_SHA) || \
865
    defined(BUILD_SSL_RSA_WITH_RC4_128_MD5)
866
    #define BUILD_ARC4
867
#endif
868
869
#if defined(BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA)
870
    #define BUILD_DES3
871
#endif
872
873
#if defined(BUILD_TLS_RSA_WITH_AES_128_CBC_SHA) || \
874
    defined(BUILD_TLS_RSA_WITH_AES_256_CBC_SHA) || \
875
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256) || \
876
    defined(BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256)
877
    #undef  BUILD_AES
878
    #define BUILD_AES
879
#endif
880
881
#if defined(BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256) || \
882
    defined(BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256) || \
883
    defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) || \
884
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) || \
885
    defined(BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256) || \
886
    defined(BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256) || \
887
    defined(BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384) || \
888
    defined(BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384) || \
889
    defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) || \
890
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) || \
891
    defined(BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384) || \
892
    defined(BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384) || \
893
    defined(BUILD_TLS_AES_128_GCM_SHA256) || \
894
    defined(BUILD_TLS_AES_256_GCM_SHA384)
895
    #define BUILD_AESGCM
896
#else
897
    /* No AES-GCM cipher suites available with build */
898
    #define NO_AESGCM_AEAD
899
#endif
900
901
#if defined(BUILD_TLS_RSA_WITH_AES_128_CCM_8) || \
902
    defined(BUILD_TLS_RSA_WITH_AES_256_CCM_8) || \
903
    defined(BUILD_TLS_PSK_WITH_AES_128_CCM_8) || \
904
    defined(BUILD_TLS_PSK_WITH_AES_128_CCM) || \
905
    defined(BUILD_TLS_PSK_WITH_AES_256_CCM_8) || \
906
    defined(BUILD_TLS_PSK_WITH_AES_256_CCM) || \
907
    defined(BUILD_TLS_DHE_PSK_WITH_AES_128_CCM) || \
908
    defined(BUILD_TLS_DHE_PSK_WITH_AES_256_CCM) || \
909
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM) || \
910
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8) || \
911
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8) || \
912
    defined(BUILD_TLS_AES_128_CCM_SHA256) || \
913
    defined(BUILD_TLS_AES_128_CCM_8_SHA256)
914
    #define BUILD_AESCCM
915
#else
916
    /* No AES-CCM cipher suites available with build */
917
    #define NO_AESCCM_AEAD
918
#endif
919
920
#if defined(BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256) || \
921
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384)
922
    #define BUILD_ARIA
923
#endif
924
925
#if defined(BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) || \
926
    defined(BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
927
    defined(BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
928
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) || \
929
    defined(BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
930
    defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) || \
931
    defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256) || \
932
    defined(BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
933
    defined(BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256) || \
934
    defined(BUILD_TLS_CHACHA20_POLY1305_SHA256)
935
    /* Have an available ChaCha Poly cipher suite */
936
#else
937
    /* No ChaCha Poly cipher suites available with build */
938
    #define NO_CHAPOL_AEAD
939
#endif
940
941
#ifdef NO_DES3
942
    #define DES_BLOCK_SIZE 8
943
#else
944
    #undef  BUILD_DES3
945
    #define BUILD_DES3
946
#endif
947
948
#if defined(NO_AES) || !defined(HAVE_AES_DECRYPT)
949
    #undef WC_AES_BLOCK_SIZE
950
    #define WC_AES_BLOCK_SIZE 16
951
    #undef  BUILD_AES
952
#else
953
    #undef  BUILD_AES
954
    #define BUILD_AES
955
#endif
956
957
#if !defined(NO_RC4) && !defined(WSSL_HARDEN_TLS)
958
    /* MUST NOT negotiate RC4 cipher suites
959
     * https://www.rfc-editor.org/rfc/rfc9325#section-4.1 */
960
    #undef  BUILD_ARC4
961
    #define BUILD_ARC4
962
#endif
963
964
#ifdef HAVE_CHACHA
965
0
    #define CHACHA20_BLOCK_SIZE 16
966
#endif
967
968
#if defined(WOLFSSL_MAX_STRENGTH) || \
969
    (defined(HAVE_AESGCM) && !defined(NO_AESGCM_AEAD)) || \
970
     defined(HAVE_AESCCM) || \
971
     defined(HAVE_ARIA) || \
972
    (defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \
973
     !defined(NO_CHAPOL_AEAD)) || \
974
    defined(WOLFSSL_SM4_GCM) || defined(WOLFSSL_SM4_CCM) || \
975
    (defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER))
976
977
    #define HAVE_AEAD
978
#endif
979
980
#if defined(WOLFSSL_MAX_STRENGTH) || \
981
    defined(HAVE_ECC) || !defined(NO_DH)
982
983
    #define HAVE_PFS
984
#endif
985
986
#ifdef WSSL_HARDEN_TLS
987
    #ifdef HAVE_NULL_CIPHER
988
        #error "NULL ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
989
    #endif
990
    #ifdef WOLFSSL_STATIC_RSA
991
        #error "Static RSA ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
992
    #endif
993
    #ifdef WOLFSSL_STATIC_DH
994
        #error "Static DH ciphers not allowed https://www.rfc-editor.org/rfc/rfc9325#section-4.1"
995
    #endif
996
    #ifdef HAVE_ANON
997
        #error "At least the server side has to be authenticated"
998
    #endif
999
#endif
1000
1001
#undef WSSL_HARDEN_TLS
1002
1003
/* CA Names feature */
1004
#if !defined(WOLFSSL_NO_CA_NAMES) && defined(OPENSSL_EXTRA)
1005
    #define SSL_CLIENT_CA_NAMES(ssl) ((ssl)->client_ca_names != NULL ? \
1006
        (ssl)->client_ca_names : \
1007
        (ssl)->ctx->client_ca_names)
1008
    #define SSL_CA_NAMES(ssl) ((ssl)->ca_names != NULL ? \
1009
        (ssl)->ca_names : \
1010
        (ssl)->ctx->ca_names)
1011
    /* On the server, client_ca_names has priority over ca_names if both are
1012
     * set. This mimics OpenSSL's API:
1013
     * https://docs.openssl.org/3.6/man3/SSL_CTX_set0_CA_list/ */
1014
    #define SSL_PRIORITY_CA_NAMES(ssl) \
1015
        (((ssl)->options.side == WOLFSSL_SERVER_END && \
1016
        SSL_CLIENT_CA_NAMES(ssl) != NULL) ? \
1017
            SSL_CLIENT_CA_NAMES(ssl) : \
1018
            SSL_CA_NAMES(ssl))
1019
#else
1020
    #undef  WOLFSSL_NO_CA_NAMES
1021
    #define WOLFSSL_NO_CA_NAMES
1022
#endif
1023
1024
1025
/* actual cipher values, 2nd byte */
1026
enum {
1027
    TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x16,
1028
    TLS_DHE_RSA_WITH_AES_256_CBC_SHA  = 0x39,
1029
    TLS_DHE_RSA_WITH_AES_128_CBC_SHA  = 0x33,
1030
    TLS_DH_anon_WITH_AES_128_CBC_SHA  = 0x34,
1031
    TLS_RSA_WITH_AES_256_CBC_SHA      = 0x35,
1032
    TLS_RSA_WITH_AES_128_CBC_SHA      = 0x2F,
1033
    TLS_RSA_WITH_NULL_MD5             = 0x01,
1034
    TLS_RSA_WITH_NULL_SHA             = 0x02,
1035
    TLS_PSK_WITH_AES_256_CBC_SHA      = 0x8d,
1036
    TLS_PSK_WITH_AES_128_CBC_SHA256   = 0xae,
1037
    TLS_PSK_WITH_AES_256_CBC_SHA384   = 0xaf,
1038
    TLS_PSK_WITH_AES_128_CBC_SHA      = 0x8c,
1039
    TLS_PSK_WITH_NULL_SHA256          = 0xb0,
1040
    TLS_PSK_WITH_NULL_SHA384          = 0xb1,
1041
    TLS_PSK_WITH_NULL_SHA             = 0x2c,
1042
    SSL_RSA_WITH_RC4_128_SHA          = 0x05,
1043
    SSL_RSA_WITH_RC4_128_MD5          = 0x04,
1044
    SSL_RSA_WITH_3DES_EDE_CBC_SHA     = 0x0A,
1045
1046
    /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
1047
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA    = 0x14,
1048
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA    = 0x13,
1049
    TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA  = 0x0A,
1050
    TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA  = 0x09,
1051
    TLS_ECDHE_RSA_WITH_RC4_128_SHA        = 0x11,
1052
    TLS_ECDHE_ECDSA_WITH_RC4_128_SHA      = 0x07,
1053
    TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA   = 0x12,
1054
    TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x08,
1055
    TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256   = 0x27,
1056
    TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 = 0x23,
1057
    TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384   = 0x28,
1058
    TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 = 0x24,
1059
    TLS_ECDHE_ECDSA_WITH_NULL_SHA           = 0x06,
1060
    TLS_ECDHE_PSK_WITH_NULL_SHA256          = 0x3a,
1061
    TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256   = 0x37,
1062
1063
    /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
1064
    TLS_ECDH_RSA_WITH_AES_256_CBC_SHA    = 0x0F,
1065
    TLS_ECDH_RSA_WITH_AES_128_CBC_SHA    = 0x0E,
1066
    TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA  = 0x05,
1067
    TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA  = 0x04,
1068
    TLS_ECDH_RSA_WITH_RC4_128_SHA        = 0x0C,
1069
    TLS_ECDH_ECDSA_WITH_RC4_128_SHA      = 0x02,
1070
    TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA   = 0x0D,
1071
    TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA = 0x03,
1072
    TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256   = 0x29,
1073
    TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 = 0x25,
1074
    TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384   = 0x2A,
1075
    TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 = 0x26,
1076
1077
    WDM_WITH_NULL_SHA256          = 0xFE, /* wolfSSL DTLS Multicast */
1078
1079
    /* SHA256 */
1080
    TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x6b,
1081
    TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x67,
1082
    TLS_RSA_WITH_AES_256_CBC_SHA256     = 0x3d,
1083
    TLS_RSA_WITH_AES_128_CBC_SHA256     = 0x3c,
1084
    TLS_RSA_WITH_NULL_SHA256            = 0x3b,
1085
    TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 = 0xb2,
1086
    TLS_DHE_PSK_WITH_NULL_SHA256        = 0xb4,
1087
1088
    /* SHA384 */
1089
    TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 = 0xb3,
1090
    TLS_DHE_PSK_WITH_NULL_SHA384        = 0xb5,
1091
1092
    /* AES-GCM */
1093
    TLS_RSA_WITH_AES_128_GCM_SHA256          = 0x9c,
1094
    TLS_RSA_WITH_AES_256_GCM_SHA384          = 0x9d,
1095
    TLS_DHE_RSA_WITH_AES_128_GCM_SHA256      = 0x9e,
1096
    TLS_DHE_RSA_WITH_AES_256_GCM_SHA384      = 0x9f,
1097
    TLS_DH_anon_WITH_AES_256_GCM_SHA384      = 0xa7,
1098
    TLS_PSK_WITH_AES_128_GCM_SHA256          = 0xa8,
1099
    TLS_PSK_WITH_AES_256_GCM_SHA384          = 0xa9,
1100
    TLS_DHE_PSK_WITH_AES_128_GCM_SHA256      = 0xaa,
1101
    TLS_DHE_PSK_WITH_AES_256_GCM_SHA384      = 0xab,
1102
1103
    /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
1104
    TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256  = 0x2b,
1105
    TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384  = 0x2c,
1106
    TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256   = 0x2d,
1107
    TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384   = 0x2e,
1108
    TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256    = 0x2f,
1109
    TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384    = 0x30,
1110
    TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256     = 0x31,
1111
    TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384     = 0x32,
1112
1113
    /* AES-CCM, first byte is 0xC0 but isn't ECC,
1114
     * also, in some of the other AES-CCM suites
1115
     * there will be second byte number conflicts
1116
     * with non-ECC AES-GCM */
1117
    TLS_RSA_WITH_AES_128_CCM_8         = 0xa0,
1118
    TLS_RSA_WITH_AES_256_CCM_8         = 0xa1,
1119
    TLS_ECDHE_ECDSA_WITH_AES_128_CCM   = 0xac,
1120
    TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 = 0xae,
1121
    TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 = 0xaf,
1122
    TLS_PSK_WITH_AES_128_CCM           = 0xa4,
1123
    TLS_PSK_WITH_AES_256_CCM           = 0xa5,
1124
    TLS_PSK_WITH_AES_128_CCM_8         = 0xa8,
1125
    TLS_PSK_WITH_AES_256_CCM_8         = 0xa9,
1126
    TLS_DHE_PSK_WITH_AES_128_CCM       = 0xa6,
1127
    TLS_DHE_PSK_WITH_AES_256_CCM       = 0xa7,
1128
1129
    /* Camellia */
1130
    TLS_RSA_WITH_CAMELLIA_128_CBC_SHA        = 0x41,
1131
    TLS_RSA_WITH_CAMELLIA_256_CBC_SHA        = 0x84,
1132
    TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256     = 0xba,
1133
    TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256     = 0xc0,
1134
    TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA    = 0x45,
1135
    TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA    = 0x88,
1136
    TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 = 0xbe,
1137
    TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 = 0xc4,
1138
1139
    /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
1140
    TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   = 0xa8,
1141
    TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 = 0xa9,
1142
    TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256     = 0xaa,
1143
    TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256   = 0xac,
1144
    TLS_PSK_WITH_CHACHA20_POLY1305_SHA256         = 0xab,
1145
    TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256     = 0xad,
1146
1147
    /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
1148
    TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256   = 0x13,
1149
    TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 = 0x14,
1150
    TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256     = 0x15,
1151
1152
    /* ECDHE_PSK RFC8442, first byte is 0xD0 (EDHE_PSK_BYTE) */
1153
    TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256    = 0x01,
1154
1155
    /* TLS v1.3 cipher suites */
1156
    TLS_AES_128_GCM_SHA256       = 0x01,
1157
    TLS_AES_256_GCM_SHA384       = 0x02,
1158
    TLS_CHACHA20_POLY1305_SHA256 = 0x03,
1159
    TLS_AES_128_CCM_SHA256       = 0x04,
1160
    TLS_AES_128_CCM_8_SHA256     = 0x05,
1161
1162
    /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
1163
    TLS_SHA256_SHA256            = 0xB4,
1164
    TLS_SHA384_SHA384            = 0xB5,
1165
1166
    /* ARIA-GCM, first byte is 0xC0 (ECC_BYTE)
1167
    * See: https://www.rfc-editor.org/rfc/rfc6209.html#section-5
1168
    */
1169
    TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256   = 0x5c,
1170
    TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384   = 0x5d,
1171
1172
    /* TLS v1.3 SM cipher suites - 0x00 (CIPHER_BYTE) is first byte */
1173
    TLS_SM4_GCM_SM3              = 0xC6,
1174
    TLS_SM4_CCM_SM3              = 0xC7,
1175
1176
    /* TLS v1.2 SM cipher suites - 0xE0 (SM_BYTE) is first byte */
1177
    TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 = 0x11,
1178
    TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3 = 0x51,
1179
    TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3 = 0x52,
1180
1181
    /* Fallback SCSV (Signaling Cipher Suite Value) */
1182
    TLS_FALLBACK_SCSV                        = 0x56,
1183
    /* Renegotiation Indication Extension Special Suite */
1184
    TLS_EMPTY_RENEGOTIATION_INFO_SCSV        = 0xff
1185
};
1186
1187
1188
#ifndef WOLFSSL_SESSION_TIMEOUT
1189
5.30k
    #define WOLFSSL_SESSION_TIMEOUT 500
1190
    /* default session resumption cache timeout in seconds */
1191
#endif
1192
1193
1194
#ifndef WOLFSSL_DTLS_WINDOW_WORDS
1195
    #define WOLFSSL_DTLS_WINDOW_WORDS 2
1196
#endif /* WOLFSSL_DTLS_WINDOW_WORDS */
1197
#define DTLS_WORD_BITS (sizeof(word32) * CHAR_BIT)
1198
#define DTLS_SEQ_BITS  (WOLFSSL_DTLS_WINDOW_WORDS * DTLS_WORD_BITS)
1199
#define DTLS_SEQ_SZ    (sizeof(word32) * WOLFSSL_DTLS_WINDOW_WORDS)
1200
1201
#ifndef WOLFSSL_MULTICAST
1202
    #define WOLFSSL_DTLS_PEERSEQ_SZ 1
1203
#else
1204
    #ifndef WOLFSSL_MULTICAST_PEERS
1205
        /* max allowed multicast group peers */
1206
        #define WOLFSSL_MULTICAST_PEERS 100
1207
    #endif
1208
    #define WOLFSSL_DTLS_PEERSEQ_SZ WOLFSSL_MULTICAST_PEERS
1209
#endif /* WOLFSSL_MULTICAST */
1210
1211
#ifndef WOLFSSL_MAX_MTU
1212
    /* 1500 - 100 bytes to account for UDP and IP headers */
1213
    #define WOLFSSL_MAX_MTU 1400
1214
#endif /* WOLFSSL_MAX_MTU */
1215
1216
#ifndef WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER
1217
    #define WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER 500
1218
#endif /* WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER */
1219
1220
#ifndef WOLFSSL_DTLS_FRAG_POOL_SZ
1221
    #define WOLFSSL_DTLS_FRAG_POOL_SZ 10
1222
#endif
1223
1224
/* set minimum DH key size allowed */
1225
#ifndef WOLFSSL_MIN_DHKEY_BITS
1226
    #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
1227
        /* Using guidance from section 5.6.1
1228
         * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
1229
        #if WOLFSSL_HARDEN_TLS >= 128
1230
            #define WOLFSSL_MIN_DHKEY_BITS 3072
1231
        #elif WOLFSSL_HARDEN_TLS >= 112
1232
            #define WOLFSSL_MIN_DHKEY_BITS 2048
1233
        #endif
1234
    #elif defined(WOLFSSL_MAX_STRENGTH)
1235
        #define WOLFSSL_MIN_DHKEY_BITS 2048
1236
    #else
1237
5.30k
        #define WOLFSSL_MIN_DHKEY_BITS 1024
1238
    #endif
1239
#endif
1240
#if defined(WOLFSSL_HARDEN_TLS) && WOLFSSL_MIN_DHKEY_BITS < 2048 && \
1241
    !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
1242
    /* Implementations MUST NOT negotiate cipher suites offering less than
1243
     * 112 bits of security.
1244
     * https://www.rfc-editor.org/rfc/rfc9325#section-4.1
1245
     * Using guidance from section 5.6.1
1246
     * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
1247
    #error "For 112 bits of security DH needs at least 2048 bit keys"
1248
#endif
1249
#if (WOLFSSL_MIN_DHKEY_BITS % 8)
1250
    #error DH minimum bit size must be multiple of 8
1251
#endif
1252
#if (WOLFSSL_MIN_DHKEY_BITS > 16000)
1253
    #error DH minimum bit size must not be greater than 16000
1254
#endif
1255
5.30k
#define MIN_DHKEY_SZ (WOLFSSL_MIN_DHKEY_BITS / 8)
1256
/* set maximum DH key size allowed */
1257
#ifndef WOLFSSL_MAX_DHKEY_BITS
1258
    #if (defined(USE_FAST_MATH) && defined(FP_MAX_BITS) && FP_MAX_BITS >= 16384)
1259
        #define WOLFSSL_MAX_DHKEY_BITS  (FP_MAX_BITS / 2)
1260
    #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)) && \
1261
           defined(SP_INT_BITS)
1262
        /* SP implementation supports numbers of SP_INT_BITS bits. */
1263
5.30k
        #define WOLFSSL_MAX_DHKEY_BITS  WC_BITS_FULL_BYTES(SP_INT_BITS)
1264
    #else
1265
        #define WOLFSSL_MAX_DHKEY_BITS  4096
1266
    #endif
1267
#endif
1268
#if (WOLFSSL_MAX_DHKEY_BITS % 8)
1269
    #error DH maximum bit size must be multiple of 8
1270
#endif
1271
#if (WOLFSSL_MAX_DHKEY_BITS > 16384)
1272
    #error DH maximum bit size must not be greater than 16384
1273
#endif
1274
5.30k
#define MAX_DHKEY_SZ (WOLFSSL_MAX_DHKEY_BITS / 8)
1275
1276
#ifndef NO_DH
1277
#if WOLFSSL_MAX_DHKEY_BITS < WOLFSSL_MIN_DHKEY_BITS
1278
#error "WOLFSSL_MAX_DHKEY_BITS has to be greater than WOLFSSL_MIN_DHKEY_BITS"
1279
#endif
1280
#endif /* NO_DH */
1281
1282
#ifndef MAX_PSK_KEY_LEN
1283
    #define MAX_PSK_KEY_LEN 64
1284
#endif
1285
1286
#ifndef MAX_EARLY_DATA_SZ
1287
    /* maximum early data size */
1288
    #define MAX_EARLY_DATA_SZ  4096
1289
#endif
1290
1291
/* Anti-replay eviction keys off the ticket's session ID. */
1292
#if defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET) && \
1293
    !defined(WOLFSSL_TICKET_HAVE_ID)
1294
    #define WOLFSSL_TICKET_HAVE_ID
1295
#endif
1296
1297
1298
#if !defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC)
1299
    /* MySQL wants to be able to use 8192-bit numbers. */
1300
    #if defined(USE_FAST_MATH) && defined(FP_MAX_BITS)
1301
        /* Use the FP size up to 8192-bit and down to a min of 1024-bit. */
1302
        #if FP_MAX_BITS >= 16384
1303
            #define ENCRYPT_BASE_BITS  8192
1304
        #elif defined(HAVE_ECC)
1305
            #if FP_MAX_BITS > 2224
1306
                #define ENCRYPT_BASE_BITS  (FP_MAX_BITS / 2)
1307
            #else
1308
                /* 521-bit ASN.1 signature - 3 + 2 * (2 + 66) bytes. */
1309
                #define ENCRYPT_BASE_BITS  1112
1310
            #endif
1311
        #else
1312
            #if FP_MAX_BITS > 2048
1313
                #define ENCRYPT_BASE_BITS  (FP_MAX_BITS / 2)
1314
            #else
1315
                #define ENCRYPT_BASE_BITS  1024
1316
            #endif
1317
        #endif
1318
1319
        /* Check MySQL size requirements met. */
1320
        #if defined(WOLFSSL_MYSQL_COMPATIBLE) && ENCRYPT_BASE_BITS < 8192
1321
            #error "MySQL needs FP_MAX_BITS at least at 16384"
1322
        #endif
1323
1324
        #if !defined(NO_RSA) && defined(WC_MAX_RSA_BITS) && \
1325
            WC_MAX_RSA_BITS > ENCRYPT_BASE_BITS
1326
            #error "FP_MAX_BITS too small for WC_MAX_RSA_BITS"
1327
        #endif
1328
    #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
1329
        /* Use the SP size up to 8192-bit and down to a min of 1024-bit. */
1330
        #if SP_INT_BITS >= 8192
1331
            #define ENCRYPT_BASE_BITS  8192
1332
        #elif defined(HAVE_ECC)
1333
            #if SP_INT_BITS > 1112
1334
                #define ENCRYPT_BASE_BITS  SP_INT_BITS
1335
            #else
1336
                /* 521-bit ASN.1 signature - 3 + 2 * (2 + 66) bytes. */
1337
                #define ENCRYPT_BASE_BITS  1112
1338
            #endif
1339
        #else
1340
            #if SP_INT_BITS > 1024
1341
                #define ENCRYPT_BASE_BITS  SP_INT_BITS
1342
            #else
1343
                #define ENCRYPT_BASE_BITS  1024
1344
            #endif
1345
        #endif
1346
1347
        /* Check MySQL size requirements met. */
1348
        #if defined(WOLFSSL_MYSQL_COMPATIBLE) && ENCRYPT_BASE_BITS < 8192
1349
            #error "MySQL needs SP_INT_BITS at least at 8192"
1350
        #endif
1351
1352
        #if !defined(NO_RSA) && defined(WC_MAX_RSA_BITS) && \
1353
            WC_MAX_RSA_BITS > SP_INT_BITS
1354
            #error "SP_INT_BITS too small for WC_MAX_RSA_BITS"
1355
        #endif
1356
    #else
1357
        /* Integer/heap maths - support 4096-bit. */
1358
        #define ENCRYPT_BASE_BITS  4096
1359
    #endif
1360
#elif defined(HAVE_CURVE448)
1361
    #define ENCRYPT_BASE_BITS    (456 * 2)
1362
#elif defined(HAVE_CURVE25519)
1363
    #define ENCRYPT_BASE_BITS    (256 * 2)
1364
#else
1365
    /* No secret from public key operation but PSK key plus length used. */
1366
    #define ENCRYPT_BASE_BITS  ((MAX_PSK_KEY_LEN + 2) * 8)
1367
#endif
1368
1369
#ifdef WOLFSSL_DTLS_CID
1370
#ifndef DTLS_CID_MAX_SIZE
1371
/* DTLS parsing code copies the record header in a static buffer to decrypt
1372
 * the record. Increasing the CID max size does increase also this buffer,
1373
 * impacting on per-session runtime memory footprint. */
1374
#define DTLS_CID_MAX_SIZE 10
1375
#endif
1376
#else
1377
#undef DTLS_CID_MAX_SIZE
1378
#define DTLS_CID_MAX_SIZE 0
1379
#endif /* WOLFSSL_DTLS_CID */
1380
1381
#if DTLS_CID_MAX_SIZE > 255
1382
#error "Max size for DTLS CID is 255 bytes"
1383
#endif
1384
1385
/* Record Payload Protection Section 5
1386
 *   https://www.rfc-editor.org/rfc/rfc9146.html#section-5 */
1387
#define WOLFSSL_TLS_HMAC_CID_INNER_SZ                               \
1388
           (8 +                 /* seq_num_placeholder */           \
1389
            1 +                 /* tls12_cid */                     \
1390
            1 +                 /* cid_length */                    \
1391
            1 +                 /* tls12_cid */                     \
1392
            2 +                 /* DTLSCiphertext.version */        \
1393
            2 +                 /* epoch */                         \
1394
            6 +                 /* sequence_number */               \
1395
            DTLS_CID_MAX_SIZE + /* cid */                           \
1396
            2)                  /* length_of_DTLSInnerPlaintext */
1397
1398
#define WOLFSSL_TLS_AEAD_CID_AAD_SZ                                 \
1399
           (8 +                 /* seq_num_placeholder */           \
1400
            1 +                 /* tls12_cid */                     \
1401
            1 +                 /* cid_length */                    \
1402
            1 +                 /* tls12_cid */                     \
1403
            2 +                 /* DTLSCiphertext.version */        \
1404
            2 +                 /* epoch */                         \
1405
            6 +                 /* sequence_number */               \
1406
            DTLS_CID_MAX_SIZE + /* cid */                           \
1407
            2)                  /* length_of_DTLSInnerPlaintext */
1408
1409
#ifndef MAX_TICKET_AGE_DIFF
1410
/* maximum ticket age difference in seconds, 10 seconds */
1411
#define MAX_TICKET_AGE_DIFF     10
1412
#endif
1413
#ifndef TLS13_MAX_TICKET_AGE
1414
/* max ticket age in seconds, 7 days */
1415
#define TLS13_MAX_TICKET_AGE    (7*24*60*60)
1416
#endif
1417
1418
1419
/* Limit is 2^24.5
1420
 * https://www.rfc-editor.org/rfc/rfc8446#section-5.5
1421
 * Without the fraction is 23726566 (0x016A09E6) */
1422
#define AEAD_AES_LIMIT                           w64From32(0x016A, 0x09E6)
1423
/* Limit is 2^23
1424
 * https://www.rfc-editor.org/rfc/rfc9147.html#name-integrity-limits */
1425
#define DTLS_AEAD_AES_CCM_LIMIT                  w64From32(0, 1 << 22)
1426
1427
/* Limit is 2^36
1428
 * https://www.rfc-editor.org/rfc/rfc9147.html#name-aead-limits */
1429
#define DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT      w64From32(1 << 3, 0)
1430
#define DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT   w64From32(1 << 2, 0)
1431
/* Limit is 2^7
1432
 * https://www.rfc-editor.org/rfc/rfc9147.html#name-limits-for-aead_aes_128_ccm */
1433
#define DTLS_AEAD_AES_CCM_8_FAIL_LIMIT           w64From32(0, 1 << 6)
1434
#define DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT        w64From32(0, 1 << 5)
1435
/* Limit is 2^23.5.
1436
 * https://www.rfc-editor.org/rfc/rfc9147.html#name-integrity-limits
1437
 * Without the fraction is 11863283 (0x00B504F3)
1438
 * Half of this value is    5931641 (0x005A8279) */
1439
#define DTLS_AEAD_AES_CCM_FAIL_LIMIT             w64From32(0x00B5, 0x04F3)
1440
#define DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT          w64From32(0x005A, 0x8279)
1441
1442
/* Limit is (2^22 - 1) full messages [2^36 - 31 octets]
1443
 * https://www.rfc-editor.org/rfc/rfc8998.html#name-aead_sm4_gcm
1444
 */
1445
#define AEAD_SM4_GCM_LIMIT                       w64From32(0, (1 << 22) - 1)
1446
/* Limit is (2^10 - 1) full messages [2^24 - 1 octets]
1447
 * https://www.rfc-editor.org/rfc/rfc8998.html#name-aead_sm4_ccm
1448
 */
1449
#define AEAD_SM4_CCM_LIMIT                       w64From32(0, (1 << 10) - 1)
1450
1451
#ifndef WOLFSSL_COOKIE_LEN
1452
/* Maximum size for a DTLS cookie */
1453
#define WOLFSSL_COOKIE_LEN 32
1454
#endif
1455
1456
#if WOLFSSL_COOKIE_LEN > 255
1457
#error "WOLFSSL_COOKIE_LEN must be <= 255 per RFC 6347 (opaque<0..2^8-1>)"
1458
#endif
1459
1460
#if defined(WOLFSSL_TLS13) || !defined(NO_PSK)
1461
1462
#define TLS13_TICKET_NONCE_MAX_SZ 255
1463
1464
#if (defined(HAVE_FIPS) &&                                                     \
1465
    !(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))) &&                    \
1466
    defined(TLS13_TICKET_NONCE_STATIC_SZ)
1467
#error "TLS13_TICKET_NONCE_STATIC_SZ is not supported in this FIPS version"
1468
#endif
1469
1470
#ifndef TLS13_TICKET_NONCE_STATIC_SZ
1471
#define TLS13_TICKET_NONCE_STATIC_SZ 8
1472
#endif
1473
1474
#if TLS13_TICKET_NONCE_STATIC_SZ > TLS13_TICKET_NONCE_MAX_SZ
1475
#error "Max size for ticket nonce is 255 bytes"
1476
#endif
1477
1478
#endif /* WOLFSSL_TLS13 || !NO_PSK */
1479
1480
#ifdef WOLFSSL_TLS13
1481
/* The length of the certificate verification label - client and server. */
1482
113
#define CERT_VFY_LABEL_SZ    34
1483
/* The number of prefix bytes for signature data. */
1484
113
#define SIGNING_DATA_PREFIX_SZ     64
1485
/* Maximum length of the signature data. */
1486
#define MAX_SIG_DATA_SZ            (SIGNING_DATA_PREFIX_SZ + \
1487
                                    CERT_VFY_LABEL_SZ      + \
1488
                                    WC_MAX_DIGEST_SIZE)
1489
#endif /* WOLFSSL_TLS13 */
1490
1491
enum Misc {
1492
    CIPHER_BYTE    = 0x00,         /* Default ciphers */
1493
    ECC_BYTE       = 0xC0,         /* ECC first cipher suite byte */
1494
    CHACHA_BYTE    = 0xCC,         /* ChaCha first cipher suite */
1495
    TLS13_BYTE     = 0x13,         /* TLS v1.3 first byte of cipher suite */
1496
    ECDHE_PSK_BYTE = 0xD0,         /* RFC 8442 */
1497
    SM_BYTE        = 0xE0,         /* SM first byte - private range */
1498
1499
    SEND_CERT       = 1,
1500
    SEND_BLANK_CERT = 2,
1501
1502
    DTLS_MAJOR      = 0xfe,     /* DTLS major version number */
1503
    DTLS_MINOR      = 0xff,     /* DTLS minor version number */
1504
    DTLS_BOGUS_MINOR = 0xfe,    /* DTLS 0xfe was skipped, see RFC6347 Sec. 1 */
1505
    DTLSv1_2_MINOR  = 0xfd,     /* DTLS minor version number */
1506
    DTLSv1_3_MINOR  = 0xfc,     /* DTLS minor version number */
1507
    SSLv3_MAJOR     = 3,        /* SSLv3 and TLSv1+  major version number */
1508
    SSLv3_MINOR     = 0,        /* TLSv1   minor version number */
1509
    TLSv1_MINOR     = 1,        /* TLSv1   minor version number */
1510
    TLSv1_1_MINOR   = 2,        /* TLSv1_1 minor version number */
1511
    TLSv1_2_MINOR   = 3,        /* TLSv1_2 minor version number */
1512
    TLSv1_3_MINOR   = 4,        /* TLSv1_3 minor version number */
1513
    TLS_DRAFT_MAJOR = 0x7f,     /* Draft TLS major version number */
1514
    OLD_HELLO_ID    = 0x01,     /* SSLv2 Client Hello Indicator */
1515
    INVALID_BYTE    = 0xff,     /* Used to initialize cipher specs values */
1516
    NO_COMPRESSION  =  0,
1517
    ZLIB_COMPRESSION = 221,     /* wolfSSL zlib compression */
1518
    HELLO_EXT_SIG_ALGO = 13,    /* ID for the sig_algo hello extension */
1519
    HELLO_EXT_EXTMS = 0x0017,   /* ID for the extended master secret ext */
1520
    SECRET_LEN      = WOLFSSL_MAX_MASTER_KEY_LENGTH,
1521
                                /* pre RSA and all master */
1522
#if !defined(WOLFSSL_TLS13) || defined(WOLFSSL_32BIT_MILLI_TIME)
1523
    TIMESTAMP_LEN   = 4,        /* timestamp size in ticket */
1524
#else
1525
    TIMESTAMP_LEN   = 8,        /* timestamp size in ticket */
1526
#endif
1527
#ifdef WOLFSSL_TLS13
1528
    AGEADD_LEN      = 4,        /* ageAdd size in ticket */
1529
    NAMEDGROUP_LEN  = 2,        /* namedGroup size in ticket */
1530
#ifdef WOLFSSL_EARLY_DATA
1531
    MAXEARLYDATASZ_LEN = 4,     /* maxEarlyDataSz size in ticket */
1532
#endif
1533
#endif
1534
#ifndef NO_PSK
1535
    ENCRYPT_LEN     = (ENCRYPT_BASE_BITS / 8) + MAX_PSK_KEY_LEN + 2,
1536
#else
1537
    ENCRYPT_LEN     = (ENCRYPT_BASE_BITS / 8),
1538
#endif
1539
    SIZEOF_SENDER   =  4,       /* clnt or srvr           */
1540
    FINISHED_SZ     = 36,       /* WC_MD5_DIGEST_SIZE + WC_SHA_DIGEST_SIZE */
1541
    MAX_PLAINTEXT_SZ   = (1 << 14),        /* Max plaintext sz   */
1542
    MAX_TLS_CIPHER_SZ  = (1 << 14) + 2048, /* Max TLS encrypted data sz */
1543
#ifdef WOLFSSL_TLS13
1544
    MAX_TLS13_PLAIN_SZ = (1 << 14) + 1,    /* Max unencrypted data sz */
1545
    MAX_TLS13_ENC_SZ   = (1 << 14) + 256,  /* Max encrypted data sz   */
1546
#endif
1547
    MAX_MSG_EXTRA   = 38 + WC_MAX_DIGEST_SIZE,
1548
                                /* max added to msg, mac + pad  from */
1549
                                /* RECORD_HEADER_SZ + BLOCK_SZ (pad) + Max
1550
                                   digest sz + BLOC_SZ (iv) + pad byte (1) */
1551
    MAX_COMP_EXTRA  = 1024,     /* max compression extra */
1552
    MAX_MTU         = WOLFSSL_MAX_MTU,     /* max expected MTU */
1553
    MAX_UDP_SIZE    = 8192 - 100, /* was MAX_MTU - 100 */
1554
    MAX_DH_SZ       = (MAX_DHKEY_SZ * 3) + 12, /* DH_P, DH_G and DH_Pub */
1555
                                /* 4096 p, pub, g + 2 byte size for each */
1556
    MAX_STR_VERSION = 8,        /* string rep of protocol version */
1557
1558
    PAD_MD5        = 48,       /* pad length for finished */
1559
    PAD_SHA        = 40,       /* pad length for finished */
1560
    MAX_PAD_SIZE   = 256,      /* maximum length of padding */
1561
1562
    LENGTH_SZ      =  2,       /* length field for HMAC, data only */
1563
    VERSION_SZ     =  2,       /* length of proctocol version */
1564
    SEQ_SZ         =  8,       /* 64 bit sequence number  */
1565
    ALERT_SIZE     =  2,       /* level + description     */
1566
    VERIFY_HEADER  =  2,       /* always use 2 bytes      */
1567
    EXTS_SZ        =  2,       /* always use 2 bytes      */
1568
    EXT_ID_SZ      =  2,       /* always use 2 bytes      */
1569
    MAX_DH_SIZE    = MAX_DHKEY_SZ+1,
1570
                               /* Max size plus possible leading 0 */
1571
    MIN_FFHDE_GROUP = 0x100,   /* Named group minimum for FFDHE parameters  */
1572
    MAX_FFHDE_GROUP = 0x1FF,   /* Named group maximum for FFDHE parameters  */
1573
    SESSION_HINT_SZ = 4,       /* session timeout hint */
1574
    SESSION_ADD_SZ = 4,        /* session age add */
1575
    TICKET_NONCE_LEN_SZ = 1,   /* Ticket nonce length size */
1576
    DEF_TICKET_NONCE_SZ = 1,   /* Default ticket nonce size */
1577
#if defined(WOLFSSL_TLS13) || !defined(NO_PSK)
1578
    MAX_TICKET_NONCE_STATIC_SZ = TLS13_TICKET_NONCE_STATIC_SZ,
1579
                               /* maximum ticket nonce static size */
1580
#endif /* WOLFSSL_TLS13 || !NO_PSK */
1581
    MAX_LIFETIME   = 604800,   /* maximum ticket lifetime */
1582
1583
    RAN_LEN      = 32,         /* random length           */
1584
    SEED_LEN     = RAN_LEN * 2, /* tls prf seed length    */
1585
    ID_LEN       = 32,         /* session id length       */
1586
    COOKIE_SECRET_SZ = 14,     /* dtls cookie secret size */
1587
    MAX_COOKIE_LEN = WOLFSSL_COOKIE_LEN, /* max dtls cookie size */
1588
    COOKIE_SZ    = 20,         /* use a 20 byte cookie    */
1589
    SUITE_LEN    =  2,         /* cipher suite sz length  */
1590
    ENUM_LEN     =  1,         /* always a byte           */
1591
    OPAQUE8_LEN  =  1,         /* 1 byte                  */
1592
    OPAQUE16_LEN =  2,         /* 2 bytes                 */
1593
    OPAQUE24_LEN =  3,         /* 3 bytes                 */
1594
    OPAQUE32_LEN =  4,         /* 4 bytes                 */
1595
    OPAQUE64_LEN =  8,         /* 8 bytes                 */
1596
    COMP_LEN     =  1,         /* compression length      */
1597
    CURVE_LEN    =  2,         /* ecc named curve length  */
1598
    KE_GROUP_LEN =  2,         /* key exchange group length */
1599
#if defined(NO_SHA) && !defined(NO_SHA256)
1600
    SERVER_ID_LEN = WC_SHA256_DIGEST_SIZE,
1601
#else
1602
    SERVER_ID_LEN = WC_SHA_DIGEST_SIZE,
1603
#endif
1604
1605
    HANDSHAKE_HEADER_SZ   = 4,  /* type + length(3)        */
1606
    DTLS13_HANDSHAKE_HEADER_SZ   = 12, /* sizeof(Dtls13HandshakeHeader) */
1607
    RECORD_HEADER_SZ      = 5,  /* type + version + len(2) */
1608
    CERT_HEADER_SZ        = 3,  /* always 3 bytes          */
1609
    REQ_HEADER_SZ         = 2,  /* cert request header sz  */
1610
    HINT_LEN_SZ           = 2,  /* length of hint size field */
1611
    TRUNCATED_HMAC_SZ     = 10, /* length of hmac w/ truncated hmac extension */
1612
    HELLO_EXT_SZ          = 4,  /* base length of a hello extension */
1613
    HELLO_EXT_TYPE_SZ     = 2,  /* length of a hello extension type */
1614
    HELLO_EXT_SZ_SZ       = 2,  /* length of a hello extension size */
1615
    HELLO_EXT_SIGALGO_SZ  = 2,  /* length of number of items in sigalgo list */
1616
1617
    DTLS_HANDSHAKE_HEADER_SZ = 12, /* normal + seq(2) + offset(3) + length(3) */
1618
    DTLS_RECORD_HEADER_SZ    = 13, /* normal + epoch(2) + seq_num(6) */
1619
    DTLS12_CID_OFFSET        = 11,
1620
    DTLS_UNIFIED_HEADER_MIN_SZ = 2,
1621
    /* flags + seq_number(2) + length(2) + CID */
1622
    DTLS_RECVD_RL_HEADER_MAX_SZ = 5 + DTLS_CID_MAX_SIZE,
1623
    DTLS_RECORD_HEADER_MAX_SZ = 13,
1624
    DTLS_HANDSHAKE_EXTRA     = 8,  /* diff from normal */
1625
    DTLS_RECORD_EXTRA        = 8,  /* diff from normal */
1626
    DTLS_HANDSHAKE_SEQ_SZ    = 2,  /* handshake header sequence number */
1627
    DTLS_HANDSHAKE_FRAG_SZ   = 3,  /* fragment offset and length are 24 bit */
1628
    DTLS_POOL_SZ             = 20, /* allowed number of list items in TX and
1629
                                    * RX pool */
1630
    DTLS_FRAG_POOL_SZ        = WOLFSSL_DTLS_FRAG_POOL_SZ,
1631
                                   /* allowed number of fragments per msg */
1632
    DTLS_EXPORT_PRO          = 165,/* wolfSSL protocol for serialized session */
1633
    DTLS_EXPORT_STATE_PRO    = 166,/* wolfSSL protocol for serialized state */
1634
    TLS_EXPORT_PRO           = 167,/* wolfSSL protocol for serialized TLS */
1635
    DTLS_EXPORT_OPT_SZ       = 62, /* amount of bytes used from Options */
1636
    DTLS_EXPORT_OPT_SZ_4     = 61, /* amount of bytes used from Options */
1637
    TLS_EXPORT_OPT_SZ        = 66, /* amount of bytes used from Options */
1638
    TLS_EXPORT_OPT_SZ_4      = 65, /* amount of bytes used from Options */
1639
    DTLS_EXPORT_OPT_SZ_3     = 60, /* amount of bytes used from Options */
1640
    DTLS_EXPORT_KEY_SZ       = 325 + (DTLS_SEQ_SZ * 2),
1641
                                   /* max amount of bytes used from Keys */
1642
    DTLS_EXPORT_MIN_KEY_SZ   = 85 + (DTLS_SEQ_SZ * 2),
1643
                                   /* min amount of bytes used from Keys */
1644
    WOLFSSL_EXPORT_TLS       = 1,
1645
    WOLFSSL_EXPORT_DTLS      = 0,
1646
#ifndef WOLFSSL_EXPORT_SPC_SZ
1647
    WOLFSSL_EXPORT_SPC_SZ    = 16, /* amount of bytes used from CipherSpecs */
1648
#endif
1649
    WOLFSSL_EXPORT_LEN       = 2,  /* 2 bytes for length and protocol */
1650
    WOLFSSL_EXPORT_VERSION   = 5,  /* wolfSSL version for serialized session */
1651
1652
    WOLFSSL_EXPORT_VERSION_4 = 4,  /* 5.6.4 release and before */
1653
    /* older export versions supported */
1654
    WOLFSSL_EXPORT_VERSION_3 = 3,  /* wolfSSL version before TLS 1.3 addition */
1655
1656
    MAX_EXPORT_IP            = 46, /* max ip size IPv4 mapped IPv6 */
1657
    DTLS_MTU_ADDITIONAL_READ_BUFFER = WOLFSSL_DTLS_MTU_ADDITIONAL_READ_BUFFER,
1658
                                   /* Additional bytes to read so that
1659
                                    * we can work with a peer that has
1660
                                    * a slightly different MTU than us. */
1661
    MAX_EXPORT_BUFFER        = 514, /* max size of buffer for exporting */
1662
    MAX_EXPORT_STATE_BUFFER  = (DTLS_EXPORT_MIN_KEY_SZ) + (3 * WOLFSSL_EXPORT_LEN),
1663
                                    /* max size of buffer for exporting state */
1664
    FINISHED_LABEL_SZ   = 15,  /* TLS finished label size */
1665
    TLS_FINISHED_SZ     = 12,  /* TLS has a shorter size  */
1666
    TLS_FINISHED_SZ_MAX = WC_MAX_DIGEST_SIZE,
1667
                            /* longest message digest size is SHA512, 64 */
1668
    EXT_MASTER_LABEL_SZ = 22,  /* TLS extended master secret label sz */
1669
    MASTER_LABEL_SZ     = 13,  /* TLS master secret label sz */
1670
    KEY_LABEL_SZ        = 13,  /* TLS key block expansion sz */
1671
    PROTOCOL_LABEL_SZ   = 9,   /* Length of the protocol label */
1672
    MAX_LABEL_SZ        = 34,  /* Maximum length of a label */
1673
    MAX_REQUEST_SZ      = 256, /* Maximum cert req len (no auth yet */
1674
    SESSION_FLUSH_COUNT = 256, /* Flush session cache unless user turns off */
1675
    TLS_MAX_PAD_SZ      = 255, /* Max padding in TLS */
1676
    MAX_EXT_DATA_LEN    = 65535,
1677
                          /* Max extension data length <0..2^16-1> RFC 8446
1678
                           * Section 4.2 */
1679
    MAX_SV_EXT_LEN      = 255,
1680
                          /* Max supported_versions extension length
1681
                           * <2..254> RFC 8446 Section 4.2.1.*/
1682
1683
#if defined(HAVE_NULL_CIPHER) && defined(WOLFSSL_TLS13)
1684
    #if defined(WOLFSSL_SHA384) && WC_MAX_SYM_KEY_SIZE < 48
1685
        MAX_SYM_KEY_SIZE    = WC_SHA384_DIGEST_SIZE,
1686
    #elif !defined(NO_SHA256) && WC_MAX_SYM_KEY_SIZE < 32
1687
        MAX_SYM_KEY_SIZE    = WC_SHA256_DIGEST_SIZE,
1688
    #else
1689
        MAX_SYM_KEY_SIZE    = WC_MAX_SYM_KEY_SIZE,
1690
    #endif
1691
#else
1692
    MAX_SYM_KEY_SIZE    = WC_MAX_SYM_KEY_SIZE,
1693
#endif
1694
1695
#if defined(HAVE_SELFTEST) && \
1696
    (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
1697
    #ifndef WOLFSSL_AES_KEY_SIZE_ENUM
1698
    #define WOLFSSL_AES_KEY_SIZE_ENUM
1699
    AES_IV_SIZE         = 16,
1700
    AES_128_KEY_SIZE    = 16,
1701
    AES_192_KEY_SIZE    = 24,
1702
    AES_256_KEY_SIZE    = 32,
1703
    #endif
1704
#endif
1705
1706
    MAX_IV_SZ           = WC_AES_BLOCK_SIZE,
1707
1708
    AEAD_SEQ_OFFSET     = 4,   /* Auth Data: Sequence number */
1709
    AEAD_TYPE_OFFSET    = 8,   /* Auth Data: Type            */
1710
    AEAD_VMAJ_OFFSET    = 9,   /* Auth Data: Major Version   */
1711
    AEAD_VMIN_OFFSET    = 10,  /* Auth Data: Minor Version   */
1712
    AEAD_LEN_OFFSET     = 11,  /* Auth Data: Length          */
1713
    AEAD_AUTH_DATA_SZ   = 13,  /* Size of the data to authenticate */
1714
    AEAD_NONCE_SZ       = 12,
1715
    AESGCM_IMP_IV_SZ    = 4,   /* Size of GCM AEAD implicit IV */
1716
    AESCCM_IMP_IV_SZ    = 4,   /* Size of CCM AEAD implicit IV */
1717
    AESGCM_EXP_IV_SZ    = 8,   /* Size of GCM/CCM AEAD explicit IV */
1718
    AESGCM_NONCE_SZ     = AESGCM_EXP_IV_SZ + AESGCM_IMP_IV_SZ,
1719
    GCM_IMP_IV_SZ       = 4,   /* Size of GCM AEAD implicit IV */
1720
    CCM_IMP_IV_SZ       = 4,   /* Size of CCM AEAD implicit IV */
1721
    GCM_EXP_IV_SZ       = 8,   /* Size of GCM/CCM AEAD explicit IV */
1722
    GCM_NONCE_SZ        = GCM_EXP_IV_SZ + GCM_IMP_IV_SZ,
1723
1724
    CHACHA20_IMP_IV_SZ  = 12,  /* Size of ChaCha20 AEAD implicit IV */
1725
    CHACHA20_NONCE_SZ   = 12,  /* Size of ChacCha20 nonce           */
1726
    CHACHA20_OLD_OFFSET = 4,   /* Offset for seq # in old poly1305  */
1727
    CHACHA20_OFFSET     = 4,   /* Offset for seq # in poly1305  */
1728
1729
    /* For any new implicit/explicit IV size adjust AEAD_MAX_***_SZ */
1730
1731
    AES_GCM_AUTH_SZ     = 16, /* AES-GCM Auth Tag length    */
1732
    AES_CCM_16_AUTH_SZ  = 16, /* AES-CCM-16 Auth Tag length */
1733
    AES_CCM_8_AUTH_SZ   = 8,  /* AES-CCM-8 Auth Tag Length  */
1734
    AESCCM_NONCE_SZ     = 12,
1735
1736
    SM4_GCM_AUTH_SZ     = 16, /* SM4-GCM Auth Tag length    */
1737
    SM4_GCM_NONCE_SZ    = 12, /* SM4 GCM Nonce length       */
1738
    SM4_CCM_AUTH_SZ     = 16, /* SM4-CCM Auth Tag length    */
1739
    SM4_CCM_NONCE_SZ    = 12, /* SM4 CCM Nonce length       */
1740
1741
    CAMELLIA_128_KEY_SIZE = 16, /* for 128 bit */
1742
    CAMELLIA_192_KEY_SIZE = 24, /* for 192 bit */
1743
    CAMELLIA_256_KEY_SIZE = 32, /* for 256 bit */
1744
    CAMELLIA_IV_SIZE      = 16, /* always block size */
1745
1746
    CHACHA20_256_KEY_SIZE = 32,  /* for 256 bit             */
1747
    CHACHA20_128_KEY_SIZE = 16,  /* for 128 bit             */
1748
    CHACHA20_IV_SIZE      = 12,  /* 96 bits for iv          */
1749
1750
    POLY1305_AUTH_SZ    = 16,  /* 128 bits                */
1751
1752
    HMAC_NONCE_SZ       = 12,  /* Size of HMAC nonce */
1753
1754
    EVP_SALT_SIZE       =  8,  /* evp salt size 64 bits   */
1755
1756
#ifndef ECDHE_SIZE /* allow this to be overridden at compile-time */
1757
    ECDHE_SIZE          = 32,  /* ECDHE server size defaults to 256 bit */
1758
#endif
1759
    MAX_EXPORT_ECC_SZ   = 256, /* Export ANSI X9.62 max future size */
1760
    MAX_CURVE_NAME_SZ   = 20,  /* Maximum size of curve name string */
1761
1762
    NEW_SA_MAJOR        = 8,   /* Most significant byte used with new sig algos */
1763
    RSA_PSS_RSAE_SHA256_MINOR = 0x04,
1764
    RSA_PSS_RSAE_SHA384_MINOR = 0x05,
1765
    RSA_PSS_RSAE_SHA512_MINOR = 0x06,
1766
    RSA_PSS_PSS_SHA256_MINOR = 0x09,
1767
    RSA_PSS_PSS_SHA384_MINOR = 0x0A,
1768
    RSA_PSS_PSS_SHA512_MINOR = 0x0B,
1769
    ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR = 0x1A,
1770
    ECDSA_BRAINPOOLP384R1TLS13_SHA384_MINOR = 0x1B,
1771
    ECDSA_BRAINPOOLP512R1TLS13_SHA512_MINOR = 0x1C,
1772
1773
    ED25519_SA_MAJOR    = 8,   /* Most significant byte for ED25519 */
1774
    ED25519_SA_MINOR    = 7,   /* Least significant byte for ED25519 */
1775
    ED448_SA_MAJOR      = 8,   /* Most significant byte for ED448 */
1776
    ED448_SA_MINOR      = 8,   /* Least significant byte for ED448 */
1777
    SM2_SA_MAJOR        = 7,   /* Most significant byte for SM2 with SM3 */
1778
    SM2_SA_MINOR        = 8,   /* Least significant byte for SM2 with SM3 */
1779
1780
    FALCON_SA_MAJOR     = 0xFE,/* Most significant byte used with falcon sig algs */
1781
    DILITHIUM_SA_MAJOR  = 0x09,/* Most significant byte used with dilithium sig algs */
1782
1783
    /* These values for falcon match what OQS has defined. */
1784
    FALCON_LEVEL1_SA_MAJOR = 0xFE,
1785
    FALCON_LEVEL1_SA_MINOR = 0xD7,
1786
    FALCON_LEVEL5_SA_MAJOR = 0xFE,
1787
    FALCON_LEVEL5_SA_MINOR = 0xDA,
1788
1789
    /* these values for MLDSA (Dilithium) correspond to what is proposed in the
1790
     * IETF. */
1791
    DILITHIUM_LEVEL2_SA_MAJOR = 0x09,
1792
    DILITHIUM_LEVEL2_SA_MINOR = 0x04,
1793
    DILITHIUM_LEVEL3_SA_MAJOR = 0x09,
1794
    DILITHIUM_LEVEL3_SA_MINOR = 0x05,
1795
    DILITHIUM_LEVEL5_SA_MAJOR = 0x09,
1796
    DILITHIUM_LEVEL5_SA_MINOR = 0x06,
1797
1798
    MIN_RSA_SHA512_PSS_BITS = 512 * 2 + 8 * 8, /* Min key size */
1799
    MIN_RSA_SHA384_PSS_BITS = 384 * 2 + 8 * 8, /* Min key size */
1800
1801
    CLIENT_HELLO_FIRST =  35,  /* Protocol + RAN_LEN + sizeof(id_len) */
1802
    MAX_SUITE_NAME     =  48,  /* maximum length of cipher suite string */
1803
1804
    DTLS_TIMEOUT_INIT       =  1, /* default timeout init for DTLS receive  */
1805
    DTLS_TIMEOUT_MAX        = 64, /* default max timeout for DTLS receive */
1806
    DTLS_TIMEOUT_MULTIPLIER =  2, /* default timeout multiplier for DTLS recv */
1807
1808
    NULL_TERM_LEN        =   1,  /* length of null '\0' termination character */
1809
    MIN_PSK_ID_LEN       =   6,  /* min length of identities */
1810
    MIN_PSK_BINDERS_LEN  =  33,  /* min length of binders */
1811
1812
#ifndef MAX_WOLFSSL_FILE_SIZE
1813
    MAX_WOLFSSL_FILE_SIZE = 1024UL * 1024UL * 4,  /* 4 mb file size alloc limit */
1814
#endif
1815
#if defined(WOLFSSL_SYS_CRYPTO_POLICY)
1816
    MAX_WOLFSSL_CRYPTO_POLICY_SIZE = 1024UL, /* Crypto-policy file is one line.
1817
                                              * It should not be large. */
1818
    MIN_WOLFSSL_SEC_LEVEL = 0,
1819
    MAX_WOLFSSL_SEC_LEVEL = 5,
1820
#endif /* WOLFSSL_SYS_CRYPTO_POLICY */
1821
1822
    CERT_MIN_SIZE      =  256, /* min PEM cert size with header/footer */
1823
1824
    NO_SNIFF           =   0,  /* not sniffing */
1825
    SNIFF              =   1,  /* currently sniffing */
1826
1827
    HASH_SIG_SIZE      =   2,  /* default SHA1 RSA */
1828
1829
    NO_COPY            =   0,  /* should we copy static buffer for write */
1830
    COPY               =   1,  /* should we copy static buffer for write */
1831
1832
    INVALID_PEER_ID    = 0xFFFF, /* Initialize value for peer ID. */
1833
1834
    PREV_ORDER         = -1,   /* Sequence number is in previous epoch. */
1835
    PEER_ORDER         = 1,    /* Peer sequence number for verify. */
1836
    CUR_ORDER          = 0,    /* Current sequence number. */
1837
    WRITE_PROTO        = 1,    /* writing a protocol message */
1838
    READ_PROTO         = 0     /* reading a protocol message */
1839
};
1840
1841
1842
/* Size of the data to authenticate */
1843
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_DTLS_CID)
1844
#define AEAD_AUTH_DATA_SZ WOLFSSL_TLS_AEAD_CID_AAD_SZ
1845
#else
1846
0
#define AEAD_AUTH_DATA_SZ 13
1847
#endif
1848
1849
#define WOLFSSL_NAMED_GROUP_IS_FFDHE(group) \
1850
30.5k
    (WOLFSSL_FFDHE_START <= (group) && (group) <= WOLFSSL_FFDHE_END)
1851
#ifdef WOLFSSL_HAVE_MLKEM
1852
WOLFSSL_LOCAL int NamedGroupIsPqc(int group);
1853
WOLFSSL_LOCAL int NamedGroupIsPqcHybrid(int group);
1854
9.60k
#define WOLFSSL_NAMED_GROUP_IS_PQC(group) NamedGroupIsPqc(group)
1855
6.65k
#define WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group) NamedGroupIsPqcHybrid(group)
1856
#else
1857
#define WOLFSSL_NAMED_GROUP_IS_PQC(group)        ((void)(group), 0)
1858
#define WOLFSSL_NAMED_GROUP_IS_PQC_HYBRID(group) ((void)(group), 0)
1859
#endif /* WOLFSSL_HAVE_MLKEM */
1860
1861
/* minimum Downgrade Minor version */
1862
#ifndef WOLFSSL_MIN_DOWNGRADE
1863
    #ifndef NO_OLD_TLS
1864
        #define WOLFSSL_MIN_DOWNGRADE TLSv1_MINOR
1865
    #else
1866
5.30k
        #define WOLFSSL_MIN_DOWNGRADE TLSv1_2_MINOR
1867
    #endif
1868
#endif
1869
1870
/* minimum DTLS Downgrade Minor version */
1871
#ifndef WOLFSSL_MIN_DTLS_DOWNGRADE
1872
#define WOLFSSL_MIN_DTLS_DOWNGRADE DTLS_MINOR;
1873
#endif
1874
1875
/* Set max implicit IV size for AEAD cipher suites */
1876
#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && defined(WOLFSSL_SHA384)
1877
    /* Integrity-only cipher suites use IV size equal to hash output size */
1878
    #define AEAD_MAX_IMP_SZ 48
1879
#elif defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && !defined(NO_SHA256)
1880
    /* Integrity-only cipher suites use IV size equal to hash output size */
1881
    #define AEAD_MAX_IMP_SZ 32
1882
#else
1883
    #define AEAD_MAX_IMP_SZ 12
1884
#endif
1885
1886
/* Set max explicit IV size for AEAD cipher suites */
1887
209
#define AEAD_MAX_EXP_SZ 8
1888
1889
1890
#ifndef WOLFSSL_MAX_SUITE_SZ
1891
7.44k
    #define WOLFSSL_MAX_SUITE_SZ 300
1892
    /* 150 suites for now! */
1893
#endif
1894
1895
/* number of items in the signature algo list */
1896
#ifndef WOLFSSL_MAX_SIGALGO
1897
#if defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)
1898
    /* If we are building with post-quantum algorithms, we likely want to
1899
     * inter-op with OQS's OpenSSL and they send a lot more sigalgs.
1900
     */
1901
    #define WOLFSSL_MAX_SIGALGO 128
1902
#else
1903
939
    #define WOLFSSL_MAX_SIGALGO 44
1904
#endif
1905
#endif
1906
1907
1908
/* set minimum ECC key size allowed */
1909
#ifndef WOLFSSL_MIN_ECC_BITS
1910
    #ifdef WOLFSSL_MAX_STRENGTH
1911
        #define WOLFSSL_MIN_ECC_BITS  256
1912
    #else
1913
10.6k
        #define WOLFSSL_MIN_ECC_BITS 224
1914
    #endif
1915
#endif /* WOLFSSL_MIN_ECC_BITS */
1916
#if (WOLFSSL_MIN_ECC_BITS % 8)
1917
    /* Some ECC keys are not divisible by 8 such as prime239v1 or sect131r1.
1918
       In these cases round down to the nearest value divisible by 8. The
1919
       restriction of being divisible by 8 is in place to match wc_ecc_size
1920
       function from wolfSSL.
1921
     */
1922
    #error ECC minimum bit size must be a multiple of 8
1923
#endif
1924
10.6k
#define MIN_ECCKEY_SZ (WOLFSSL_MIN_ECC_BITS / 8)
1925
1926
#ifdef HAVE_FALCON
1927
#ifndef MIN_FALCONKEY_SZ
1928
    #define MIN_FALCONKEY_SZ    1281
1929
#endif
1930
#endif
1931
#ifdef HAVE_DILITHIUM
1932
#ifndef MIN_DILITHIUMKEY_SZ
1933
    #define MIN_DILITHIUMKEY_SZ    2528
1934
#endif
1935
#endif
1936
1937
/* set minimum RSA key size allowed */
1938
#ifndef WOLFSSL_MIN_RSA_BITS
1939
    #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
1940
        /* Using guidance from section 5.6.1
1941
         * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
1942
        #if WOLFSSL_HARDEN_TLS >= 128
1943
            #define WOLFSSL_MIN_RSA_BITS 3072
1944
        #elif WOLFSSL_HARDEN_TLS >= 112
1945
            #define WOLFSSL_MIN_RSA_BITS 2048
1946
        #endif
1947
    #elif defined(WOLFSSL_MAX_STRENGTH)
1948
        #define WOLFSSL_MIN_RSA_BITS 2048
1949
    #else
1950
10.6k
        #define WOLFSSL_MIN_RSA_BITS 1024
1951
    #endif
1952
#endif /* WOLFSSL_MIN_RSA_BITS */
1953
#if defined(WOLFSSL_HARDEN_TLS) && WOLFSSL_MIN_RSA_BITS < 2048 && \
1954
    !defined(WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK)
1955
    /* Implementations MUST NOT negotiate cipher suites offering less than
1956
     * 112 bits of security.
1957
     * https://www.rfc-editor.org/rfc/rfc9325#section-4.1
1958
     * Using guidance from section 5.6.1
1959
     * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf */
1960
    #error "For 112 bits of security RSA needs at least 2048 bit keys"
1961
#endif
1962
#if (WOLFSSL_MIN_RSA_BITS % 8)
1963
    /* This is to account for the example case of a min size of 2050 bits but
1964
       still allows 2049 bit key. So we need the measurement to be in bytes. */
1965
    #error RSA minimum bit size must be a multiple of 8
1966
#endif
1967
10.6k
#define MIN_RSAKEY_SZ (WOLFSSL_MIN_RSA_BITS / 8)
1968
1969
#ifdef SESSION_INDEX
1970
/* Shift values for making a session index */
1971
#define SESSIDX_ROW_SHIFT 4
1972
#define SESSIDX_IDX_MASK  0x0F
1973
#endif
1974
1975
#ifndef MAX_X509_SIZE
1976
    #if defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)
1977
        #define MAX_X509_SIZE   (8*1024) /* max static x509 buffer size; dilithium is big */
1978
    #elif defined(WOLFSSL_HAPROXY)
1979
        #define MAX_X509_SIZE   3072 /* max static x509 buffer size */
1980
    #else
1981
44.3k
        #define MAX_X509_SIZE   2048 /* max static x509 buffer size */
1982
    #endif
1983
#endif
1984
1985
/* max cert chain peer depth */
1986
#ifndef MAX_CHAIN_DEPTH
1987
49.6k
    #define MAX_CHAIN_DEPTH 9
1988
#endif
1989
1990
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
1991
                    defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
1992
    #if !defined(HAVE_OCSP)
1993
        #error OCSP Stapling and Stapling V2 needs OCSP. Please define HAVE_OCSP.
1994
    #endif
1995
#endif
1996
1997
/* Max certificate extensions in TLS1.3 */
1998
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST)
1999
    /* Number of extensions to set each OCSP response */
2000
    #define MAX_CERT_EXTENSIONS (1 + MAX_CHAIN_DEPTH)
2001
#else
2002
    /* Only empty extensions */
2003
    #define MAX_CERT_EXTENSIONS 1
2004
#endif
2005
2006
/* max size of a certificate message payload */
2007
/* assumes MAX_CHAIN_DEPTH number of certificates at 2kb per certificate */
2008
#ifndef MAX_CERTIFICATE_SZ
2009
    #define MAX_CERTIFICATE_SZ \
2010
44.3k
                (CERT_HEADER_SZ + \
2011
44.3k
                (MAX_X509_SIZE + CERT_HEADER_SZ) * MAX_CHAIN_DEPTH)
2012
#endif
2013
2014
/* max size of a handshake message, currently set to the certificate */
2015
#ifndef MAX_HANDSHAKE_SZ
2016
44.3k
    #define MAX_HANDSHAKE_SZ MAX_CERTIFICATE_SZ
2017
#endif
2018
2019
#ifndef PREALLOC_SESSION_TICKET_LEN
2020
    #define PREALLOC_SESSION_TICKET_LEN 512
2021
#endif
2022
2023
#ifndef PREALLOC_SESSION_TICKET_NONCE_LEN
2024
    #define PREALLOC_SESSION_TICKET_NONCE_LEN 32
2025
#endif
2026
2027
#ifndef SESSION_TICKET_HINT_DEFAULT
2028
    #define SESSION_TICKET_HINT_DEFAULT 300
2029
#endif
2030
2031
#if !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_WOLFSSL_SERVER)
2032
    /* Check chosen encryption is available. */
2033
    #if !(defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) && \
2034
        defined(WOLFSSL_TICKET_ENC_CHACHA20_POLY1305)
2035
        #error "ChaCha20-Poly1305 not available for default ticket encryption"
2036
    #endif
2037
    #if !defined(HAVE_AESGCM) && (defined(WOLFSSL_TICKET_ENC_AES128_GCM) || \
2038
        defined(WOLFSSL_TICKET_ENC_AES256_GCM))
2039
        #error "AES-GCM not available for default ticket encryption"
2040
    #endif
2041
2042
    #ifndef WOLFSSL_TICKET_KEY_LIFETIME
2043
        /* Default lifetime is 1 hour from issue of first ticket with key. */
2044
        #define WOLFSSL_TICKET_KEY_LIFETIME       (60 * 60)
2045
    #endif
2046
    #if WOLFSSL_TICKET_KEY_LIFETIME <= SESSION_TICKET_HINT_DEFAULT
2047
        #error "Ticket Key lifetime must be longer than ticket life hint."
2048
    #endif
2049
#endif
2050
2051
0
#define MAX_ENCRYPT_SZ ENCRYPT_LEN
2052
2053
#define WOLFSSL_ASSERT_EQ(x, y) wc_static_assert((x) == (y))
2054
#define WOLFSSL_ASSERT_GE(x, y) wc_static_assert((x) >= (y))
2055
2056
0
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) wc_static_assert(sizeof(x) >= sizeof(y))
2057
#define WOLFSSL_ASSERT_SIZEOF_EQ(x, y) wc_static_assert(sizeof(x) == sizeof(y))
2058
2059
/* states. Adding state before HANDSHAKE_DONE will break session importing */
2060
enum states {
2061
    NULL_STATE = 0,
2062
2063
    SERVER_HELLOVERIFYREQUEST_COMPLETE,
2064
    SERVER_HELLO_RETRY_REQUEST_COMPLETE,
2065
    SERVER_HELLO_COMPLETE,
2066
    SERVER_ENCRYPTED_EXTENSIONS_COMPLETE,
2067
    SERVER_CERT_COMPLETE,
2068
    SERVER_CERT_VERIFY_COMPLETE,
2069
    SERVER_KEYEXCHANGE_COMPLETE,
2070
    SERVER_HELLODONE_COMPLETE,
2071
    SERVER_CHANGECIPHERSPEC_COMPLETE,
2072
    SERVER_FINISHED_COMPLETE,
2073
2074
    CLIENT_HELLO_RETRY,
2075
    CLIENT_HELLO_COMPLETE,
2076
    CLIENT_KEYEXCHANGE_COMPLETE,
2077
    CLIENT_CHANGECIPHERSPEC_COMPLETE,
2078
    CLIENT_FINISHED_COMPLETE,
2079
2080
    HANDSHAKE_DONE,
2081
2082
#ifdef WOLFSSL_DTLS13
2083
    SERVER_FINISHED_ACKED,
2084
#endif /* WOLFSSL_DTLS13 */
2085
2086
};
2087
2088
/* SSL Version */
2089
typedef struct ProtocolVersion {
2090
    byte major;
2091
    byte minor;
2092
} WOLFSSL_PACK ProtocolVersion;
2093
2094
2095
WOLFSSL_LOCAL ProtocolVersion MakeSSLv3(void);
2096
WOLFSSL_LOCAL ProtocolVersion MakeTLSv1(void);
2097
WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_1(void);
2098
WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_2(void);
2099
WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_3(void);
2100
2101
#ifdef WOLFSSL_DTLS
2102
    WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1(void);
2103
    WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_2(void);
2104
2105
#ifdef WOLFSSL_DTLS13
2106
    WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_3(void);
2107
#endif /* WOLFSSL_DTLS13 */
2108
2109
#endif
2110
#ifdef WOLFSSL_SESSION_EXPORT
2111
WOLFSSL_LOCAL int wolfSSL_session_export_internal(WOLFSSL* ssl, byte* buf,
2112
        word32* sz, int type);
2113
WOLFSSL_LOCAL int wolfSSL_session_import_internal(WOLFSSL* ssl, const byte* buf,
2114
        word32 sz, int type);
2115
#ifdef WOLFSSL_DTLS
2116
    WOLFSSL_LOCAL int wolfSSL_dtls_export_state_internal(WOLFSSL* ssl,
2117
                                                          byte* buf, word32 sz);
2118
    WOLFSSL_LOCAL int wolfSSL_dtls_import_state_internal(WOLFSSL* ssl,
2119
                                                    const byte* buf, word32 sz);
2120
    WOLFSSL_LOCAL int wolfSSL_send_session(WOLFSSL* ssl);
2121
#endif
2122
#endif
2123
2124
struct WOLFSSL_BY_DIR_HASH {
2125
    unsigned long hash_value;
2126
    int last_suffix;
2127
};
2128
2129
struct WOLFSSL_BY_DIR_entry {
2130
    char*   dir_name;
2131
    int     dir_type;
2132
    WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *hashes;
2133
};
2134
2135
struct WOLFSSL_BY_DIR {
2136
    WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *dir_entry;
2137
    wolfSSL_Mutex    lock; /* dir list lock */
2138
};
2139
2140
/* wolfSSL method type */
2141
struct WOLFSSL_METHOD {
2142
    ProtocolVersion version;
2143
    byte            side;         /* connection side, server or client */
2144
    byte            downgrade;    /* whether to downgrade version, default no */
2145
};
2146
2147
/* wolfSSL buffer type - internal uses "buffer" type */
2148
typedef WOLFSSL_BUFFER_INFO buffer;
2149
2150
typedef struct Suites Suites;
2151
2152
/* Declare opaque struct for API to use */
2153
#ifndef WOLFSSL_CLIENT_SESSION_DEFINED
2154
    typedef struct ClientSession ClientSession;
2155
    #define WOLFSSL_CLIENT_SESSION_DEFINED
2156
#endif
2157
2158
/* defaults to client */
2159
WOLFSSL_LOCAL void InitSSL_Method(WOLFSSL_METHOD* method, ProtocolVersion pv);
2160
2161
WOLFSSL_LOCAL void InitSSL_CTX_Suites(WOLFSSL_CTX* ctx);
2162
WOLFSSL_LOCAL int InitSSL_Suites(WOLFSSL* ssl);
2163
WOLFSSL_LOCAL int InitSSL_Side(WOLFSSL* ssl, word16 side);
2164
2165
2166
WOLFSSL_LOCAL int DoHandShakeMsgType(WOLFSSL* ssl, byte* input,
2167
        word32* inOutIdx, byte type, word32 size, word32 totalSz);
2168
/* for sniffer */
2169
WOLFSSL_LOCAL int DoFinished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
2170
                            word32 size, word32 totalSz, int sniff);
2171
#ifdef WOLFSSL_TLS13
2172
WOLFSSL_LOCAL int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
2173
                           word32 size, word32 totalSz, int sniff);
2174
#endif
2175
#ifdef WOLFSSL_API_PREFIX_MAP
2176
    #define DoApplicationData wolfSSL_DoApplicationData
2177
#endif
2178
WOLFSSL_TEST_VIS int DoApplicationData(WOLFSSL* ssl, byte* input, word32* inOutIdx,
2179
                                    int sniff);
2180
/* TLS v1.3 needs these */
2181
WOLFSSL_LOCAL int  HandleTlsResumption(WOLFSSL* ssl, Suites* clSuites);
2182
#ifdef WOLFSSL_TLS13
2183
WOLFSSL_LOCAL byte SuiteMac(const byte* suite);
2184
#endif
2185
WOLFSSL_LOCAL int  DoClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
2186
                             word32 helloSz);
2187
#ifdef WOLFSSL_TLS13
2188
WOLFSSL_LOCAL int DoTls13ClientHello(WOLFSSL* ssl, const byte* input,
2189
                                     word32* inOutIdx, word32 helloSz);
2190
#endif
2191
WOLFSSL_LOCAL int  DoServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
2192
                      word32 helloSz);
2193
WOLFSSL_LOCAL int  CompleteServerHello(WOLFSSL *ssl);
2194
WOLFSSL_LOCAL int  CheckVersion(WOLFSSL *ssl, ProtocolVersion pv);
2195
WOLFSSL_LOCAL int  PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo,
2196
                                   word32 hashSigAlgoSz, int matchSuites);
2197
#if defined(WOLF_PRIVATE_KEY_ID) && !defined(NO_CHECK_PRIVATE_KEY)
2198
WOLFSSL_LOCAL int  CreateDevPrivateKey(void** pkey, byte* data, word32 length,
2199
                                       int hsType, int label, int id,
2200
                                       void* heap, int devId);
2201
#endif
2202
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
2203
WOLFSSL_LOCAL int wolfssl_priv_der_blind(WC_RNG* rng, DerBuffer* key,
2204
    DerBuffer** mask);
2205
WOLFSSL_LOCAL void wolfssl_priv_der_blind_toggle(DerBuffer* key,
2206
    const DerBuffer* mask);
2207
WOLFSSL_LOCAL WARN_UNUSED_RESULT DerBuffer *wolfssl_priv_der_unblind(
2208
    const DerBuffer* key, const DerBuffer* mask);
2209
WOLFSSL_LOCAL void wolfssl_priv_der_unblind_free(DerBuffer* key);
2210
#endif
2211
WOLFSSL_LOCAL int  DecodePrivateKey(WOLFSSL *ssl, word32* sigLen);
2212
#ifdef WOLFSSL_DUAL_ALG_CERTS
2213
WOLFSSL_LOCAL int  DecodeAltPrivateKey(WOLFSSL *ssl, word32* sigLen);
2214
#endif
2215
#if defined(WOLF_PRIVATE_KEY_ID) || defined(HAVE_PK_CALLBACKS)
2216
WOLFSSL_LOCAL int GetPrivateKeySigSize(WOLFSSL* ssl);
2217
#ifndef NO_ASN
2218
    WOLFSSL_LOCAL int  InitSigPkCb(WOLFSSL* ssl, SignatureCtx* sigCtx);
2219
#endif
2220
#endif
2221
WOLFSSL_LOCAL int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz,
2222
                                int check);
2223
WOLFSSL_LOCAL int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz,
2224
                                      int sigAlgo, int hashAlgo);
2225
#ifdef WOLFSSL_ASYNC_IO
2226
WOLFSSL_LOCAL void FreeAsyncCtx(WOLFSSL* ssl, byte freeAsync);
2227
#endif
2228
WOLFSSL_LOCAL void FreeKeyExchange(WOLFSSL* ssl);
2229
WOLFSSL_LOCAL void FreeSuites(WOLFSSL* ssl);
2230
WOLFSSL_LOCAL int  ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, word32 totalSz);
2231
#ifdef WOLFSSL_API_PREFIX_MAP
2232
    #define MatchDomainName wolfSSL_MatchDomainName
2233
#endif
2234
WOLFSSL_TEST_VIS int  MatchDomainName(const char* pattern, int len,
2235
                                      const char* str, word32 strLen,
2236
                                      unsigned int flags);
2237
#if !defined(NO_CERTS) && !defined(NO_ASN)
2238
WOLFSSL_LOCAL int  CheckForAltNames(DecodedCert* dCert, const char* domain,
2239
                                    word32 domainLen, int* checkCN,
2240
                                    unsigned int flags, byte isIP);
2241
WOLFSSL_LOCAL int  CheckIPAddr(DecodedCert* dCert, const char* ipasc);
2242
WOLFSSL_LOCAL void CopyDecodedName(WOLFSSL_X509_NAME* name, DecodedCert* dCert, int nameType);
2243
#endif
2244
WOLFSSL_LOCAL int  SetupTicket(WOLFSSL* ssl);
2245
WOLFSSL_LOCAL int  CreateTicket(WOLFSSL* ssl);
2246
WOLFSSL_LOCAL int  HashRaw(WOLFSSL* ssl, const byte* data, int sz);
2247
WOLFSSL_LOCAL int  HashOutput(WOLFSSL* ssl, const byte* output, int sz,
2248
                              int ivSz);
2249
WOLFSSL_LOCAL int  HashInput(WOLFSSL* ssl, const byte* input, int sz);
2250
2251
#ifdef HAVE_SNI
2252
#ifndef NO_WOLFSSL_SERVER
2253
WOLFSSL_LOCAL int SNI_Callback(WOLFSSL* ssl);
2254
#endif
2255
#endif
2256
2257
#ifdef HAVE_ALPN
2258
WOLFSSL_LOCAL int ALPN_Select(WOLFSSL* ssl);
2259
#endif
2260
2261
WOLFSSL_LOCAL int ChachaAEADEncrypt(WOLFSSL* ssl, byte* out, const byte* input,
2262
                              word16 sz, byte type); /* needed by sniffer */
2263
WOLFSSL_LOCAL int ChachaAEADDecrypt(WOLFSSL* ssl, byte* plain, const byte* input,
2264
                              word16 sz); /* needed by sniffer */
2265
2266
#ifdef WOLFSSL_TLS13
2267
WOLFSSL_LOCAL int  DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input,
2268
                                word16 sz, const byte* aad, word16 aadSz);
2269
WOLFSSL_LOCAL int  DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input,
2270
                                           word32* inOutIdx, byte type,
2271
                                           word32 size, word32 totalSz);
2272
WOLFSSL_LOCAL int  DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input,
2273
                                       word32* inOutIdx, word32 totalSz);
2274
WOLFSSL_LOCAL int DoTls13ServerHello(WOLFSSL* ssl, const byte* input,
2275
                                     word32* inOutIdx, word32 helloSz,
2276
                                     byte* extMsgType);
2277
WOLFSSL_LOCAL int RestartHandshakeHash(WOLFSSL* ssl);
2278
2279
WOLFSSL_LOCAL int Tls13DeriveKey(WOLFSSL *ssl, byte *output, int outputLen,
2280
    const byte *secret, const byte *label, word32 labelLen, int hashAlgo,
2281
    int includeMsgs, int side);
2282
#endif
2283
int TimingPadVerify(WOLFSSL* ssl, const byte* input, int padLen, int macSz,
2284
                    int pLen, int content);
2285
2286
2287
enum {
2288
    FORCED_FREE = 1,
2289
    NO_FORCED_FREE = 0
2290
};
2291
2292
2293
/* only use compression extra if using compression */
2294
#ifdef HAVE_LIBZ
2295
    #define COMP_EXTRA MAX_COMP_EXTRA
2296
#else
2297
0
    #define COMP_EXTRA 0
2298
#endif
2299
2300
/* only the sniffer needs space in the buffer for extra MTU record(s) */
2301
#ifdef WOLFSSL_SNIFFER
2302
    #define MTU_EXTRA MAX_MTU * 3
2303
#else
2304
    #define MTU_EXTRA 0
2305
#endif
2306
2307
2308
/* embedded callbacks require large static buffers, make sure on */
2309
#ifdef WOLFSSL_CALLBACKS
2310
    #undef  LARGE_STATIC_BUFFERS
2311
    #define LARGE_STATIC_BUFFERS
2312
#endif
2313
2314
2315
/* determine maximum record size */
2316
0
#define MAX_RECORD_SIZE 16384  /* 2^14, max size by standard */
2317
2318
#ifdef RECORD_SIZE
2319
    /* user supplied value */
2320
    #if RECORD_SIZE < 128 || RECORD_SIZE > MAX_RECORD_SIZE
2321
        #error Invalid record size
2322
    #endif
2323
#else
2324
    /* give user option to use 16K static buffers */
2325
    #if defined(LARGE_STATIC_BUFFERS)
2326
        #define RECORD_SIZE     MAX_RECORD_SIZE
2327
    #else
2328
        #ifdef WOLFSSL_DTLS
2329
            #define RECORD_SIZE MAX_MTU
2330
        #else
2331
            #define RECORD_SIZE 128
2332
        #endif
2333
    #endif
2334
#endif
2335
2336
2337
/* user option to turn off 16K output option */
2338
/* if using small static buffers (default) and SSL_write tries to write data
2339
   larger than the record we have, dynamically get it, unless user says only
2340
   write in static buffer chunks  */
2341
#ifndef STATIC_CHUNKS_ONLY
2342
0
    #define OUTPUT_RECORD_SIZE MAX_RECORD_SIZE
2343
#else
2344
    #define OUTPUT_RECORD_SIZE RECORD_SIZE
2345
#endif
2346
2347
/* wolfSSL input buffer
2348
2349
   RFC 2246:
2350
2351
   length
2352
       The length (in bytes) of the following TLSPlaintext.fragment.
2353
       The length should not exceed 2^14.
2354
*/
2355
#ifdef STATIC_BUFFER_LEN
2356
    /* user supplied option */
2357
    #if STATIC_BUFFER_LEN < 5 || STATIC_BUFFER_LEN > (RECORD_HEADER_SZ + \
2358
                          RECORD_SIZE + COMP_EXTRA + MTU_EXTRA + MAX_MSG_EXTRA))
2359
        #error Invalid static buffer length
2360
    #endif
2361
#elif defined(LARGE_STATIC_BUFFERS)
2362
    #define STATIC_BUFFER_LEN (RECORD_HEADER_SZ + RECORD_SIZE + COMP_EXTRA + \
2363
             MTU_EXTRA + MAX_MSG_EXTRA)
2364
#else
2365
    /* don't fragment memory from the record header */
2366
449k
    #define STATIC_BUFFER_LEN RECORD_HEADER_SZ
2367
#endif
2368
2369
typedef struct {
2370
    ALIGN16 byte staticBuffer[STATIC_BUFFER_LEN];
2371
    byte*  buffer;       /* place holder for static or dynamic buffer */
2372
    word32 length;       /* total buffer length used */
2373
    word32 idx;          /* idx to part of length already consumed */
2374
    word32 bufferSize;   /* current buffer size */
2375
    byte   dynamicFlag;  /* dynamic memory currently in use */
2376
    byte   offset;       /* alignment offset attempt */
2377
} bufferStatic;
2378
2379
/* Cipher Suites holder */
2380
struct Suites {
2381
    word16 suiteSz;                 /* suite length in bytes        */
2382
    word16 hashSigAlgoSz;           /* SigAlgo extension length in bytes */
2383
    byte   suites[WOLFSSL_MAX_SUITE_SZ];
2384
    byte   hashSigAlgo[WOLFSSL_MAX_SIGALGO]; /* sig/algo to offer */
2385
    byte   setSuites:1;             /* user set suites from default */
2386
};
2387
2388
typedef struct CipherSuite {
2389
    byte   cipherSuite0;
2390
    byte   cipherSuite;
2391
    word32 ecdhCurveOID;
2392
    struct KeyShareEntry* clientKSE;
2393
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
2394
    int    doHelloRetry;
2395
#endif
2396
} CipherSuite;
2397
2398
#ifdef WOLFSSL_API_PREFIX_MAP
2399
    #define InitSuitesHashSigAlgo wolfSSL_InitSuitesHashSigAlgo
2400
#endif
2401
WOLFSSL_TEST_VIS void InitSuitesHashSigAlgo(byte* hashSigAlgo, int have,
2402
                                       int tls1_2, int tls1_3, int keySz,
2403
                                       word16* len);
2404
WOLFSSL_LOCAL int AllocateCtxSuites(WOLFSSL_CTX* ctx);
2405
WOLFSSL_LOCAL int AllocateSuites(WOLFSSL* ssl);
2406
WOLFSSL_LOCAL void InitSuites(Suites* suites, ProtocolVersion pv, int keySz,
2407
                              word16 haveRSA, word16 havePSK, word16 haveDH,
2408
                              word16 haveECDSAsig, word16 haveECC,
2409
                              word16 haveStaticRSA, word16 haveStaticECC,
2410
                              word16 haveAnon, word16 haveNull,
2411
                              word16 haveAES128, word16 haveSHA1,
2412
                              word16 haveRC4, int side);
2413
2414
void refineSuites(const Suites* sslSuites, const Suites* peerSuites,
2415
        Suites* outSuites, byte useClientOrder);
2416
void sslRefineSuites(WOLFSSL* ssl, Suites* peerSuites);
2417
2418
typedef struct TLSX TLSX;
2419
WOLFSSL_LOCAL int MatchSuite_ex(const WOLFSSL* ssl, Suites* peerSuites,
2420
                                CipherSuite* cs, TLSX* extensions);
2421
WOLFSSL_LOCAL int  MatchSuite(WOLFSSL* ssl, Suites* peerSuites);
2422
WOLFSSL_LOCAL int  SetCipherList_ex(const WOLFSSL_CTX* ctx, const WOLFSSL* ssl,
2423
        Suites* suites, const char* list);
2424
WOLFSSL_LOCAL int  SetCipherList(const WOLFSSL_CTX* ctx, Suites* suites,
2425
                                 const char* list);
2426
WOLFSSL_LOCAL int  SetCipherListFromBytes(WOLFSSL_CTX* ctx, Suites* suites,
2427
                                          const byte* list, const int listSz);
2428
WOLFSSL_LOCAL int  SetSuitesHashSigAlgo(Suites* suites, const char* list);
2429
2430
#ifndef PSK_TYPES_DEFINED
2431
    typedef unsigned int (*wc_psk_client_callback)(WOLFSSL*, const char*, char*,
2432
                          unsigned int, unsigned char*, unsigned int);
2433
    typedef unsigned int (*wc_psk_server_callback)(WOLFSSL*, const char*,
2434
                          unsigned char*, unsigned int);
2435
#ifdef WOLFSSL_TLS13
2436
    typedef unsigned int (*wc_psk_client_cs_callback)(WOLFSSL*, const char*,
2437
                          char*, unsigned int, unsigned char*, unsigned int,
2438
                          const char* cipherName);
2439
    typedef unsigned int (*wc_psk_client_tls13_callback)(WOLFSSL*, const char*,
2440
                          char*, unsigned int, unsigned char*, unsigned int,
2441
                          const char** cipherName);
2442
    typedef unsigned int (*wc_psk_server_tls13_callback)(WOLFSSL*, const char*,
2443
                          unsigned char*, unsigned int,
2444
                          const char** cipherName);
2445
#endif
2446
#endif /* PSK_TYPES_DEFINED */
2447
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
2448
   !defined(WOLFSSL_DTLS_EXPORT_TYPES)
2449
    typedef int (*wc_dtls_export)(WOLFSSL* ssl,
2450
2451
#define WOLFSSL_DTLS_EXPORT_TYPES
2452
#endif /* WOLFSSL_DTLS_EXPORT_TYPES */
2453
2454
2455
#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
2456
#define MAX_DESCRIPTION_SZ 255
2457
#endif
2458
struct WOLFSSL_CIPHER {
2459
    byte cipherSuite0;
2460
    byte cipherSuite;
2461
    const WOLFSSL* ssl;
2462
#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
2463
    char description[MAX_DESCRIPTION_SZ];
2464
    unsigned long offset;
2465
    unsigned int in_stack; /* TRUE if added to stack in wolfSSL_get_ciphers_compat */
2466
    int bits;
2467
#endif
2468
};
2469
2470
2471
#ifdef NO_ASN
2472
    /* no_asn won't have */
2473
    typedef struct CertStatus CertStatus;
2474
#endif
2475
2476
#ifndef HAVE_OCSP
2477
    typedef struct WOLFSSL_OCSP WOLFSSL_OCSP;
2478
#endif
2479
2480
/* wolfSSL OCSP controller */
2481
#ifdef HAVE_OCSP
2482
struct WOLFSSL_OCSP {
2483
    WOLFSSL_CERT_MANAGER* cm;            /* pointer back to cert manager */
2484
    OcspEntry*            ocspList;      /* OCSP response list */
2485
    wolfSSL_Mutex         ocspLock;      /* OCSP list lock */
2486
    int                   error;
2487
    int(*statusCb)(WOLFSSL*, void*);
2488
    void*                 statusCbArg;
2489
};
2490
#endif
2491
2492
typedef struct CRL_Entry CRL_Entry;
2493
2494
#if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
2495
    #define CRL_DIGEST_SIZE WC_SM3_DIGEST_SIZE
2496
#elif defined(NO_SHA)
2497
    #define CRL_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
2498
#else
2499
    #define CRL_DIGEST_SIZE WC_SHA_DIGEST_SIZE
2500
#endif
2501
2502
#ifdef NO_ASN
2503
    typedef struct RevokedCert RevokedCert;
2504
#endif
2505
#ifdef CRL_STATIC_REVOKED_LIST
2506
    #ifndef CRL_MAX_REVOKED_CERTS
2507
        #define CRL_MAX_REVOKED_CERTS 4
2508
    #elif CRL_MAX_REVOKED_CERTS > 22000
2509
        #error CRL_MAX_REVOKED_CERTS too big, max is 22000
2510
    #endif
2511
#endif
2512
2513
#ifdef HAVE_CRL
2514
/* Complete CRL */
2515
struct CRL_Entry {
2516
    byte*   toBeSigned;
2517
    byte*   signature;
2518
#if defined(OPENSSL_EXTRA)
2519
    WOLFSSL_X509_NAME*    issuer;     /* X509_NAME type issuer */
2520
#endif
2521
    CRL_Entry* next;                      /* next entry */
2522
    wolfSSL_Mutex verifyMutex;
2523
    /* DupCRL_Entry copies data after the `verifyMutex` member. Using the mutex
2524
     * as the marker because clang-tidy doesn't like taking the sizeof a
2525
     * pointer. */
2526
    char    crlNumber[CRL_MAX_NUM_HEX_STR_SZ];    /* CRL number extension */
2527
    byte    issuerHash[CRL_DIGEST_SIZE];  /* issuer hash                 */
2528
    /* byte    crlHash[CRL_DIGEST_SIZE];      raw crl data hash           */
2529
    /* restore the hash here if needed for optimized comparisons */
2530
    byte    lastDate[MAX_DATE_SIZE]; /* last date updated  */
2531
    byte    nextDate[MAX_DATE_SIZE]; /* next update date   */
2532
    byte    lastDateFormat;          /* last date format */
2533
    byte    nextDateFormat;          /* next date format */
2534
#if defined(OPENSSL_EXTRA)
2535
    WOLFSSL_ASN1_TIME lastDateAsn1;  /* last date updated  */
2536
    WOLFSSL_ASN1_TIME nextDateAsn1;  /* next update date   */
2537
#endif
2538
#ifdef CRL_STATIC_REVOKED_LIST
2539
    RevokedCert certs[CRL_MAX_REVOKED_CERTS];
2540
#else
2541
    RevokedCert* certs;             /* revoked cert list  */
2542
#endif
2543
    int     totalCerts;             /* number on list     */
2544
    int     version;                /* version of certificate */
2545
    int     verified;
2546
    word32  tbsSz;
2547
    word32  signatureSz;
2548
    word32  signatureOID;
2549
#ifdef WC_RSA_PSS
2550
    word32  sigParamsSz; /* length of signature parameters   */
2551
    byte*   sigParams;   /* buffer with signature parameters */
2552
#endif
2553
#if !defined(NO_SKID) && !defined(NO_ASN)
2554
    byte    extAuthKeyId[KEYID_SIZE];
2555
    byte    extAuthKeyIdSet:1;  /* Auth key identifier set indicator */
2556
#endif
2557
    byte    crlNumberSet:1;     /* CRL number set indicator */
2558
};
2559
2560
2561
#ifdef HAVE_CRL_MONITOR
2562
typedef struct CRL_Monitor CRL_Monitor;
2563
2564
/* CRL directory monitor */
2565
struct CRL_Monitor {
2566
    char* path;      /* full dir path, if valid pointer we're using */
2567
    int   type;      /* PEM or ASN1 type */
2568
};
2569
2570
2571
#if defined(HAVE_CRL) && defined(NO_FILESYSTEM)
2572
    #undef HAVE_CRL_MONITOR
2573
#endif
2574
2575
/* PEM and DER possible */
2576
#define WOLFSSL_CRL_MONITORS_LEN (2)
2577
2578
#if defined(__MACH__) || defined(__FreeBSD__) || defined(__linux__)
2579
typedef int    wolfSSL_CRL_mfd_t; /* monitor fd, -1 if no init yet */
2580
/* mfd for bsd is kqueue fd, eventfd for linux */
2581
#define WOLFSSL_CRL_MFD_INIT_VAL (-1)
2582
#elif defined(_MSC_VER)
2583
typedef HANDLE wolfSSL_CRL_mfd_t; /* monitor fd, INVALID_HANDLE_VALUE if
2584
                                   * no init yet */
2585
#define WOLFSSL_CRL_MFD_INIT_VAL (INVALID_HANDLE_VALUE)
2586
#endif
2587
#endif
2588
2589
/* wolfSSL CRL controller */
2590
struct WOLFSSL_CRL {
2591
    WOLFSSL_CERT_MANAGER* cm;            /* pointer back to cert manager */
2592
    CRL_Entry*            currentEntry;  /* Current CRL entry being processed */
2593
    CRL_Entry*            crlList;       /* our CRL list */
2594
#ifdef HAVE_CRL_IO
2595
    CbCrlIO               crlIOCb;
2596
#endif
2597
    wolfSSL_RwLock        crlLock;       /* CRL list lock */
2598
#ifdef HAVE_CRL_MONITOR
2599
    CRL_Monitor           monitors[WOLFSSL_CRL_MONITORS_LEN];
2600
    COND_TYPE             cond;          /* condition to signal setup */
2601
    THREAD_TYPE           tid;           /* monitoring thread */
2602
    wolfSSL_CRL_mfd_t     mfd;
2603
    int                   setup;         /* thread is setup predicate */
2604
#endif
2605
#ifdef OPENSSL_ALL
2606
    wolfSSL_Ref           ref;
2607
#endif
2608
#if defined(OPENSSL_EXTRA)
2609
    WOLFSSL_STACK*        revokedStack;  /* cached STACK_OF(X509_REVOKED) */
2610
#endif
2611
    void*                 heap;          /* heap hint for dynamic memory */
2612
};
2613
#endif
2614
2615
2616
#ifdef NO_ASN
2617
    typedef struct Signer Signer;
2618
#ifdef WOLFSSL_TRUST_PEER_CERT
2619
    typedef struct TrustedPeerCert TrustedPeerCert;
2620
#endif
2621
#endif
2622
2623
2624
#ifndef CA_TABLE_SIZE
2625
91.1k
    #define CA_TABLE_SIZE 11
2626
#endif
2627
#ifdef WOLFSSL_TRUST_PEER_CERT
2628
    #define TP_TABLE_SIZE 11
2629
#endif
2630
2631
/* wolfSSL Certificate Manager */
2632
struct WOLFSSL_CERT_MANAGER {
2633
    Signer*         caTable[CA_TABLE_SIZE]; /* the CA signer table */
2634
    void*           heap;                /* heap helper */
2635
#ifdef WOLFSSL_TRUST_PEER_CERT
2636
    TrustedPeerCert* tpTable[TP_TABLE_SIZE]; /* table of trusted peer certs */
2637
    wolfSSL_Mutex   tpLock;                  /* trusted peer list lock */
2638
#endif
2639
    WOLFSSL_CRL*    crl;                 /* CRL checker */
2640
    WOLFSSL_OCSP*   ocsp;                /* OCSP checker */
2641
#if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
2642
                               ||  defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
2643
    WOLFSSL_OCSP*   ocsp_stapling;       /* OCSP checker for OCSP stapling */
2644
#endif
2645
    char*           ocspOverrideURL;     /* use this responder */
2646
    void*           ocspIOCtx;           /* I/O callback CTX */
2647
#ifndef NO_WOLFSSL_CM_VERIFY
2648
    VerifyCallback  verifyCallback;      /* Verify callback */
2649
#endif
2650
    CallbackCACache caCacheCallback;       /* CA cache addition callback */
2651
    CbMissingCRL    cbMissingCRL;          /* notify thru cb of missing crl */
2652
    crlErrorCb      crlCb;                 /* Allow user to override error */
2653
    void*           crlCbCtx;
2654
    CbOCSPIO        ocspIOCb;              /* I/O callback for OCSP lookup */
2655
    CbOCSPRespFree  ocspRespFreeCb;        /* Frees OCSP Response from IO Cb */
2656
    wolfSSL_Mutex   caLock;                /* CA list lock */
2657
    byte            crlEnabled:1;          /* is CRL on ? */
2658
    byte            crlCheckAll:1;         /* always leaf, but all ? */
2659
    byte            ocspEnabled:1;         /* is OCSP on ? */
2660
    byte            ocspCheckAll:1;        /* always leaf, but all ? */
2661
    byte            ocspSendNonce:1;       /* send the OCSP nonce ? */
2662
    byte            ocspUseOverrideURL:1;  /* ignore cert responder, override */
2663
    byte            ocspStaplingEnabled:1; /* is OCSP Stapling on ? */
2664
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
2665
||  defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
2666
    byte            ocspMustStaple:1;      /* server must respond with staple */
2667
#endif
2668
    /* Tracks which resources were successfully initialized so that
2669
     * DoCertManagerFree can dispose of them safely even when construction
2670
     * fails partway through. */
2671
    WC_BITFIELD     caLockInit:1;          /* caLock has been initialized */
2672
#ifdef WOLFSSL_TRUST_PEER_CERT
2673
    WC_BITFIELD     tpLockInit:1;          /* tpLock has been initialized */
2674
#endif
2675
    WC_BITFIELD     refInit:1;             /* ref has been initialized */
2676
2677
#ifndef NO_RSA
2678
    short           minRsaKeySz;         /* minimum allowed RSA key size */
2679
#endif
2680
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
2681
    short           minEccKeySz;         /* minimum allowed ECC key size */
2682
#endif
2683
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
2684
    WOLFSSL_X509_STORE  *x509_store_p;  /* a pointer back to CTX x509 store  */
2685
                                        /* CTX has ownership and free this   */
2686
                                        /* with CTX free.                    */
2687
#endif
2688
    wolfSSL_Ref     ref;
2689
#ifdef HAVE_FALCON
2690
    short           minFalconKeySz;     /* minimum allowed Falcon key size */
2691
#endif
2692
#ifdef HAVE_DILITHIUM
2693
    short           minDilithiumKeySz;  /* minimum allowed Dilithium key size */
2694
#endif
2695
#ifdef WC_ASN_UNKNOWN_EXT_CB
2696
    wc_UnknownExtCallback unknownExtCallback;
2697
#endif
2698
#ifdef HAVE_CRL_UPDATE_CB
2699
    CbUpdateCRL    cbUpdateCRL; /* notify thru cb that crl has updated */
2700
#endif
2701
};
2702
2703
WOLFSSL_LOCAL int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm,
2704
                                   const char* fname);
2705
WOLFSSL_LOCAL int CM_RestoreCertCache(WOLFSSL_CERT_MANAGER* cm,
2706
                                      const char* fname);
2707
WOLFSSL_LOCAL int CM_MemSaveCertCache(WOLFSSL_CERT_MANAGER* cm, void* mem,
2708
                                      int sz, int* used);
2709
WOLFSSL_LOCAL int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm,
2710
                                         const void* mem, int sz);
2711
WOLFSSL_LOCAL int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm);
2712
WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
2713
                                     long sz, int format, int prev_err);
2714
2715
2716
#ifndef NO_CERTS
2717
#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
2718
typedef struct ProcPeerCertArgs {
2719
    buffer*      certs;
2720
#ifdef WOLFSSL_TLS13
2721
    buffer*      exts; /* extensions */
2722
#endif
2723
#ifndef NO_ASN
2724
    DecodedCert* dCert;
2725
#endif
2726
    word32 idx;
2727
    word32 begin;
2728
    int    totalCerts; /* number of certs in certs buffer */
2729
    int    count;
2730
    int    certIdx;
2731
    int    lastErr;
2732
    int    leafVerifyErr;
2733
#ifdef WOLFSSL_TLS13
2734
    byte   ctxSz;
2735
#endif
2736
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
2737
    char   untrustedDepth;
2738
#endif
2739
    word16 fatal:1;
2740
    word16 verifyErr:1;
2741
    word16 dCertInit:1;
2742
#ifdef WOLFSSL_TRUST_PEER_CERT
2743
    word16 haveTrustPeer:1; /* was cert verified by loaded trusted peer cert */
2744
#endif
2745
} ProcPeerCertArgs;
2746
WOLFSSL_LOCAL int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl,
2747
        int cert_err, ProcPeerCertArgs* args);
2748
WOLFSSL_LOCAL void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl,
2749
        ProcPeerCertArgs* args, int* outRet);
2750
2751
WOLFSSL_LOCAL int SetupStoreCtxCallback(WOLFSSL_X509_STORE_CTX** store_pt,
2752
        WOLFSSL* ssl, WOLFSSL_CERT_MANAGER* cm, ProcPeerCertArgs* args,
2753
        int cert_err, void* heap, int* x509Free);
2754
WOLFSSL_LOCAL void CleanupStoreCtxCallback(WOLFSSL_X509_STORE_CTX* store,
2755
        WOLFSSL* ssl, void* heap, int x509Free);
2756
#endif /* !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) */
2757
WOLFSSL_LOCAL int X509StoreLoadCertBuffer(WOLFSSL_X509_STORE *str,
2758
                                        byte *buf, word32 bufLen, int type);
2759
WOLFSSL_LOCAL int X509StorePushCertsToCM(WOLFSSL_X509_STORE* store);
2760
#endif /* !defined NO_CERTS */
2761
2762
/* wolfSSL Sock Addr */
2763
struct WOLFSSL_SOCKADDR {
2764
    unsigned int sz; /* sockaddr size */
2765
    unsigned int bufSz; /* size of allocated buffer */
2766
    void*        sa; /* pointer to the sockaddr_in or sockaddr_in6 */
2767
};
2768
2769
#ifdef WOLFSSL_DTLS
2770
typedef struct WOLFSSL_DTLS_CTX {
2771
#ifdef WOLFSSL_RW_THREADED
2772
    /* Protect peer access after the handshake */
2773
    wolfSSL_RwLock peerLock;
2774
#endif
2775
    WOLFSSL_SOCKADDR peer;
2776
#ifdef WOLFSSL_DTLS_CID
2777
    WOLFSSL_SOCKADDR pendingPeer; /* When using CID's, we don't want to update
2778
                                   * the peer's address until we successfully
2779
                                   * de-protect the record. */
2780
#endif
2781
    int rfd;
2782
    int wfd;
2783
    WolfSSLRecvFrom recvfrom;
2784
    WolfSSLSento sendto;
2785
    byte userSet:1;
2786
    byte connected:1; /* When set indicates rfd and wfd sockets are
2787
                       * connected (connect() and bind() both called).
2788
                       * This means that sendto and recvfrom do not need to
2789
                       * specify and store the peer address. */
2790
#ifdef WOLFSSL_DTLS_CID
2791
    byte processingPendingRecord:1;
2792
#endif
2793
} WOLFSSL_DTLS_CTX;
2794
#endif
2795
2796
2797
typedef struct WOLFSSL_DTLS_PEERSEQ {
2798
    word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
2799
                        /* Sliding window for current epoch    */
2800
    word16 nextEpoch;   /* Expected epoch in next record       */
2801
    word16 nextSeq_hi;  /* Expected sequence in next record    */
2802
    word32 nextSeq_lo;
2803
2804
    word32 prevWindow[WOLFSSL_DTLS_WINDOW_WORDS];
2805
                        /* Sliding window for old epoch        */
2806
    word32 prevSeq_lo;
2807
    word16 prevSeq_hi;  /* Next sequence in allowed old epoch  */
2808
2809
#ifdef WOLFSSL_MULTICAST
2810
    word16 peerId;
2811
    word32 highwaterMark;
2812
#endif
2813
} WOLFSSL_DTLS_PEERSEQ;
2814
2815
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
2816
struct WOLFSSL_BIO {
2817
    WOLFSSL_BUF_MEM* mem_buf;
2818
    WOLFSSL_BIO_METHOD* method;
2819
    WOLFSSL_BIO* prev;          /* previous in chain */
2820
    WOLFSSL_BIO* next;          /* next in chain */
2821
    WOLFSSL_BIO* pair;          /* BIO paired with */
2822
    void*        heap;          /* user heap hint */
2823
    union {
2824
        byte*    mem_buf_data;
2825
#ifndef WOLFCRYPT_ONLY
2826
        WOLFSSL* ssl;
2827
        WOLFSSL_EVP_MD_CTX* md_ctx;
2828
#endif
2829
#ifndef NO_FILESYSTEM
2830
        XFILE    fh;
2831
#endif
2832
    } ptr;
2833
    void*        usrCtx;        /* user set pointer */
2834
    char*        ip;            /* IP address for wolfIO_TcpConnect */
2835
    word16       port;          /* Port for wolfIO_TcpConnect */
2836
    char*        infoArg;       /* BIO callback argument */
2837
    wolf_bio_info_cb infoCb;    /* BIO callback */
2838
    int          wrSz;          /* write buffer size (mem) */
2839
    int          wrSzReset;     /* First buffer size (mem) - read ONLY data */
2840
    int          wrIdx;         /* current index for write buffer */
2841
    int          rdIdx;         /* current read index */
2842
    int          readRq;        /* read request */
2843
    union {
2844
        SOCKET_T fd;
2845
        size_t   length;
2846
    } num;
2847
    int          eof;           /* eof flag */
2848
    int          flags;
2849
    int          type;          /* method type */
2850
    byte         init:1;        /* bio has been initialized */
2851
    byte         shutdown:1;    /* close flag */
2852
    byte         connected:1;   /* connected state, for datagram BIOs -- as for
2853
                                 * struct WOLFSSL_DTLS_CTX, when set, sendto and
2854
                                 * recvfrom leave the peer_addr unchanged. */
2855
#ifdef WOLFSSL_HAVE_BIO_ADDR
2856
    union WOLFSSL_BIO_ADDR peer_addr; /* for datagram BIOs, the socket address stored
2857
                                       * with BIO_CTRL_DGRAM_CONNECT,
2858
                                       * BIO_CTRL_DGRAM_SET_CONNECTED, or
2859
                                       * BIO_CTRL_DGRAM_SET_PEER, or stored when a
2860
                                       * packet was received on an unconnected BIO. */
2861
#endif
2862
2863
#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_BIO_NO_FLOW_STATS)
2864
    #define WOLFSSL_BIO_HAVE_FLOW_STATS
2865
    word64       bytes_read;
2866
    word64       bytes_written;
2867
#endif
2868
2869
#ifdef HAVE_EX_DATA
2870
    WOLFSSL_CRYPTO_EX_DATA ex_data;
2871
#endif
2872
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
2873
    wolfSSL_Ref  ref;
2874
#endif
2875
};
2876
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
2877
2878
#if defined(WOLFSSL_HAVE_BIO_ADDR) && defined(OPENSSL_EXTRA)
2879
WOLFSSL_LOCAL socklen_t wolfSSL_BIO_ADDR_size(const WOLFSSL_BIO_ADDR *addr);
2880
#endif
2881
2882
#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && defined(WOLFSSL_SHA384)
2883
    /* Integrity-only cipher suites use IV size equal to hash output size */
2884
    #define MAX_WRITE_IV_SZ 48
2885
#elif defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && !defined(NO_SHA256)
2886
    /* Integrity-only cipher suites use IV size equal to hash output size */
2887
    #define MAX_WRITE_IV_SZ 32
2888
#else
2889
    #define MAX_WRITE_IV_SZ 16 /* max size of client/server write_IV */
2890
#endif
2891
2892
/* keys and secrets
2893
 * keep as a constant size (no additional ifdefs) for session export */
2894
typedef struct Keys {
2895
#if !defined(WOLFSSL_AEAD_ONLY) || defined(WOLFSSL_TLS13)
2896
    byte client_write_MAC_secret[WC_MAX_DIGEST_SIZE];   /* max sizes */
2897
    byte server_write_MAC_secret[WC_MAX_DIGEST_SIZE];
2898
#endif
2899
    byte client_write_key[MAX_SYM_KEY_SIZE];         /* max sizes */
2900
    byte server_write_key[MAX_SYM_KEY_SIZE];
2901
    byte client_write_IV[MAX_WRITE_IV_SZ];               /* max sizes */
2902
    byte server_write_IV[MAX_WRITE_IV_SZ];
2903
#if defined(HAVE_AEAD) || defined(WOLFSSL_SESSION_EXPORT)
2904
    byte aead_exp_IV[AEAD_MAX_EXP_SZ];
2905
    byte aead_enc_imp_IV[AEAD_MAX_IMP_SZ];
2906
    byte aead_dec_imp_IV[AEAD_MAX_IMP_SZ];
2907
#endif
2908
2909
#ifdef WOLFSSL_DTLS13
2910
    byte client_sn_key[MAX_SYM_KEY_SIZE];
2911
    byte server_sn_key[MAX_SYM_KEY_SIZE];
2912
#endif /* WOLFSSL_DTLS13 */
2913
2914
    word32 peer_sequence_number_hi;
2915
    word32 peer_sequence_number_lo;
2916
    word32 sequence_number_hi;
2917
    word32 sequence_number_lo;
2918
2919
#ifdef WOLFSSL_DTLS
2920
    word16 curEpoch;    /* Received epoch in current record    */
2921
    word16 curSeq_hi;   /* Received sequence in current record */
2922
    word32 curSeq_lo;
2923
2924
#ifdef WOLFSSL_DTLS13
2925
    w64wrapper curEpoch64;    /* Received epoch in current record    */
2926
    w64wrapper curSeq;
2927
#endif /* WOLFSSL_DTLS13 */
2928
2929
#ifdef WOLFSSL_MULTICAST
2930
    byte   curPeerId;   /* Received peer group ID in current record */
2931
#endif
2932
    WOLFSSL_DTLS_PEERSEQ peerSeq[WOLFSSL_DTLS_PEERSEQ_SZ];
2933
2934
    word16 dtls_peer_handshake_number;
2935
    word16 dtls_expected_peer_handshake_number;
2936
2937
    word16 dtls_epoch;                          /* Current epoch    */
2938
    word16 dtls_sequence_number_hi;             /* Current epoch */
2939
    word32 dtls_sequence_number_lo;
2940
    word16 dtls_prev_sequence_number_hi;        /* Previous epoch */
2941
    word32 dtls_prev_sequence_number_lo;
2942
    word16 dtls_handshake_number;               /* Current tx handshake seq */
2943
#endif
2944
2945
    word32 encryptSz;             /* last size of encrypted data   */
2946
    word32 padSz;                 /* how much to advance after decrypt part */
2947
    byte   encryptionOn;          /* true after change cipher spec */
2948
    byte   decryptedCur;          /* only decrypt current record once */
2949
#ifdef WOLFSSL_TLS13
2950
    byte   updateResponseReq;     /* KeyUpdate response from peer required. */
2951
    byte   keyUpdateRespond;      /* KeyUpdate is to be responded to. */
2952
#endif
2953
#ifdef WOLFSSL_RENESAS_TSIP_TLS
2954
2955
    tsip_hmac_sha_key_index_t tsip_client_write_MAC_secret;
2956
    tsip_hmac_sha_key_index_t tsip_server_write_MAC_secret;
2957
2958
#endif
2959
#ifdef WOLFSSL_RENESAS_FSPSM_TLS
2960
    FSPSM_HMAC_WKEY fspsm_client_write_MAC_secret;
2961
    FSPSM_HMAC_WKEY fspsm_server_write_MAC_secret;
2962
#endif
2963
} Keys;
2964
2965
/* Forward declare opaque pointer to make available for func def */
2966
typedef struct Options Options;
2967
2968
2969
/** TLS Extensions - RFC 6066 */
2970
#ifdef HAVE_TLS_EXTENSIONS
2971
2972
771
#define TLSXT_SERVER_NAME                0x0000 /* a.k.a. SNI  */
2973
179
#define TLSXT_MAX_FRAGMENT_LENGTH        0x0001
2974
124
#define TLSXT_TRUSTED_CA_KEYS            0x0003
2975
262
#define TLSXT_TRUNCATED_HMAC             0x0004
2976
2.67k
#define TLSXT_STATUS_REQUEST             0x0005 /* a.k.a. OCSP stapling   */
2977
3.70k
#define TLSXT_SUPPORTED_GROUPS           0x000a /* a.k.a. Supported Curves */
2978
1.13k
#define TLSXT_EC_POINT_FORMATS           0x000b
2979
1.57k
#define TLSXT_SIGNATURE_ALGORITHMS       0x000d /* HELLO_EXT_SIG_ALGO */
2980
133
#define TLSXT_USE_SRTP                   0x000e /* 14 */
2981
168
#define TLSXT_APPLICATION_LAYER_PROTOCOL 0x0010 /* a.k.a. ALPN */
2982
77
#define TLSXT_STATUS_REQUEST_V2          0x0011 /* a.k.a. OCSP stapling v2 */
2983
101
#define TLSXT_CLIENT_CERTIFICATE         0x0013 /* RFC8446 */
2984
59
#define TLSXT_SERVER_CERTIFICATE         0x0014 /* RFC8446 */
2985
758
#define TLSXT_ENCRYPT_THEN_MAC           0x0016 /* RFC 7366 */
2986
#define TLSXT_EXTENDED_MASTER_SECRET     0x0017 /* HELLO_EXT_EXTMS */
2987
48
#define TLSXT_CERT_WITH_EXTERN_PSK       0x0021 /* RFC 8773bis */
2988
666
#define TLSXT_SESSION_TICKET             0x0023
2989
665
#define TLSXT_PRE_SHARED_KEY             0x0029
2990
355
#define TLSXT_EARLY_DATA                 0x002a
2991
#define TLSXT_SUPPORTED_VERSIONS         0x002b
2992
62
#define TLSXT_COOKIE                     0x002c
2993
947
#define TLSXT_PSK_KEY_EXCHANGE_MODES     0x002d
2994
111
#define TLSXT_CERTIFICATE_AUTHORITIES    0x002f
2995
109
#define TLSXT_POST_HANDSHAKE_AUTH        0x0031
2996
124
#define TLSXT_SIGNATURE_ALGORITHMS_CERT  0x0032
2997
1.12k
#define TLSXT_KEY_SHARE                  0x0033
2998
41
#define TLSXT_CONNECTION_ID              0x0036
2999
#define TLSXT_KEY_QUIC_TP_PARAMS         0x0039 /* RFC 9001, ch. 8.2 */
3000
12
#define TLSXT_ECH                        0xfe0d /* RFC 9849 */
3001
#define TLSXT_ECH_OUTER_EXTENSIONS       0xfd00 /* RFC 9849 */
3002
/* The 0xFF section is experimental/custom/personal use */
3003
#define TLSXT_CKS                        0xff92 /* X9.146 */
3004
243
#define TLSXT_RENEGOTIATION_INFO         0xff01
3005
7
#define TLSXT_KEY_QUIC_TP_PARAMS_DRAFT   0xffa5 /* from */
3006
                                                /* draft-ietf-quic-tls-27 */
3007
3008
typedef enum {
3009
#ifdef HAVE_SNI
3010
    TLSX_SERVER_NAME                = TLSXT_SERVER_NAME,
3011
#endif
3012
    TLSX_MAX_FRAGMENT_LENGTH        = TLSXT_MAX_FRAGMENT_LENGTH,
3013
    TLSX_TRUSTED_CA_KEYS            = TLSXT_TRUSTED_CA_KEYS,
3014
    TLSX_TRUNCATED_HMAC             = TLSXT_TRUNCATED_HMAC,
3015
    TLSX_STATUS_REQUEST             = TLSXT_STATUS_REQUEST,
3016
    TLSX_SUPPORTED_GROUPS           = TLSXT_SUPPORTED_GROUPS,
3017
    TLSX_EC_POINT_FORMATS           = TLSXT_EC_POINT_FORMATS,
3018
#if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
3019
    TLSX_SIGNATURE_ALGORITHMS       = TLSXT_SIGNATURE_ALGORITHMS,
3020
#endif
3021
#ifdef WOLFSSL_SRTP
3022
    TLSX_USE_SRTP                   = TLSXT_USE_SRTP,
3023
#endif
3024
    TLSX_APPLICATION_LAYER_PROTOCOL = TLSXT_APPLICATION_LAYER_PROTOCOL,
3025
    TLSX_STATUS_REQUEST_V2          = TLSXT_STATUS_REQUEST_V2,
3026
#ifdef HAVE_RPK
3027
    TLSX_CLIENT_CERTIFICATE_TYPE    = TLSXT_CLIENT_CERTIFICATE,
3028
    TLSX_SERVER_CERTIFICATE_TYPE    = TLSXT_SERVER_CERTIFICATE,
3029
#endif
3030
#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
3031
    TLSX_ENCRYPT_THEN_MAC           = TLSXT_ENCRYPT_THEN_MAC,
3032
#endif
3033
    TLSX_EXTENDED_MASTER_SECRET     = TLSXT_EXTENDED_MASTER_SECRET,
3034
    TLSX_SESSION_TICKET             = TLSXT_SESSION_TICKET,
3035
#ifdef WOLFSSL_TLS13
3036
    #ifdef WOLFSSL_EARLY_DATA
3037
    TLSX_EARLY_DATA                 = TLSXT_EARLY_DATA,
3038
    #endif
3039
    TLSX_SUPPORTED_VERSIONS         = TLSXT_SUPPORTED_VERSIONS,
3040
    TLSX_COOKIE                     = TLSXT_COOKIE,
3041
    #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
3042
    TLSX_PSK_KEY_EXCHANGE_MODES     = TLSXT_PSK_KEY_EXCHANGE_MODES,
3043
    #if defined(WOLFSSL_CERT_WITH_EXTERN_PSK)
3044
    TLSX_CERT_WITH_EXTERN_PSK       = TLSXT_CERT_WITH_EXTERN_PSK,
3045
    #endif
3046
    #endif
3047
    #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES)
3048
    TLSX_CERTIFICATE_AUTHORITIES    = TLSXT_CERTIFICATE_AUTHORITIES,
3049
    #endif
3050
    #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
3051
    TLSX_POST_HANDSHAKE_AUTH        = TLSXT_POST_HANDSHAKE_AUTH,
3052
    #endif
3053
    #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
3054
    TLSX_SIGNATURE_ALGORITHMS_CERT  = TLSXT_SIGNATURE_ALGORITHMS_CERT,
3055
    #endif
3056
    #ifdef WOLFSSL_QUIC
3057
    TLSX_KEY_QUIC_TP_PARAMS         = TLSXT_KEY_QUIC_TP_PARAMS,
3058
    #endif
3059
    #ifdef HAVE_ECH
3060
    TLSX_ECH                        = TLSXT_ECH,
3061
    #endif
3062
#endif
3063
#if defined(WOLFSSL_DTLS_CID)
3064
    TLSX_CONNECTION_ID              = TLSXT_CONNECTION_ID,
3065
#endif /* defined(WOLFSSL_DTLS_CID) */
3066
#if defined(WOLFSSL_TLS13) || !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
3067
    #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
3068
    TLSX_PRE_SHARED_KEY             = TLSXT_PRE_SHARED_KEY,
3069
    #endif
3070
    TLSX_KEY_SHARE                  = TLSXT_KEY_SHARE,
3071
#endif
3072
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_DUAL_ALG_CERTS)
3073
    TLSX_CKS                        = TLSXT_CKS,
3074
#endif
3075
    TLSX_RENEGOTIATION_INFO         = TLSXT_RENEGOTIATION_INFO,
3076
#ifdef WOLFSSL_QUIC
3077
    TLSX_KEY_QUIC_TP_PARAMS_DRAFT   = TLSXT_KEY_QUIC_TP_PARAMS_DRAFT,
3078
#endif
3079
} TLSX_Type;
3080
3081
/* TLS Certificate type defined RFC7250
3082
 * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#tls-extensiontype-values-3
3083
 */
3084
#if defined(HAVE_RPK)
3085
typedef struct RpkConfig {
3086
    /* user's preference */
3087
    byte preferred_ClientCertTypeCnt;
3088
    byte preferred_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
3089
    byte preferred_ServerCertTypeCnt;
3090
    byte preferred_ServerCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
3091
    /* reflect to client_certificate_type extension in xxxHello */
3092
} RpkConfig;
3093
3094
typedef struct RpkState {
3095
    byte sending_ClientCertTypeCnt;
3096
    byte sending_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
3097
    /* reflect to server_certificate_type extension in xxxHello */
3098
    byte sending_ServerCertTypeCnt;
3099
    byte sending_ServerCertTypes[MAX_SERVER_CERT_TYPE_CNT];
3100
    /* client_certificate_type extension in received yyyHello  */
3101
    byte received_ClientCertTypeCnt;
3102
    byte received_ClientCertTypes[MAX_CLIENT_CERT_TYPE_CNT];
3103
    /* server_certificate_type extension in received yyyHello  */
3104
    byte received_ServerCertTypeCnt;
3105
    byte received_ServerCertTypes[MAX_SERVER_CERT_TYPE_CNT];
3106
    /* set if Raw-public-key cert is loaded as own certificate */
3107
    int  isRPKLoaded;
3108
} RpkState;
3109
#endif /* HAVE_RPK */
3110
3111
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
3112
#define ECH_ACCEPT_CONFIRMATION_SZ 8
3113
3114
typedef enum {
3115
    ECH_TYPE_OUTER = 0,
3116
    ECH_TYPE_INNER = 1
3117
} EchType;
3118
3119
typedef enum {
3120
    ECH_WRITE_GREASE,
3121
    ECH_WRITE_REAL,
3122
    ECH_WRITE_RETRY_CONFIGS,
3123
    ECH_WRITE_NONE,
3124
    ECH_PARSED_INTERNAL,
3125
} EchState;
3126
3127
typedef enum {
3128
    ECH_OUTER_SNI,
3129
    ECH_INNER_SNI,
3130
    ECH_INNER_SNI_ATTEMPT,
3131
    ECH_SNI_DONE,
3132
} EchStateSNI;
3133
3134
typedef struct EchCipherSuite {
3135
    word16 kdfId;
3136
    word16 aeadId;
3137
} EchCipherSuite;
3138
3139
typedef struct WOLFSSL_EchConfig {
3140
    byte* raw;
3141
    char* publicName;
3142
    void* receiverPrivkey;
3143
    struct WOLFSSL_EchConfig* next;
3144
    EchCipherSuite* cipherSuites;
3145
    word32 rawLen;
3146
    word16 kemId;
3147
    byte configId;
3148
    byte numCipherSuites;
3149
    byte receiverPubkey[HPKE_Npk_MAX];
3150
    byte maxNameLen;
3151
} WOLFSSL_EchConfig;
3152
3153
typedef struct WOLFSSL_ECH {
3154
    Hpke* hpke;
3155
    HpkeBaseContext* hpkeContext;
3156
    const byte* aad;
3157
    const char* privateName;
3158
    void* ephemeralKey;
3159
    WOLFSSL_EchConfig* echConfig;
3160
    byte* innerClientHello;
3161
    byte* outerClientPayload;
3162
    byte* confBuf;
3163
    EchCipherSuite cipherSuite;
3164
    word32 aadLen;
3165
    word32 innerClientHelloLen;
3166
    word16 paddingLen;
3167
    word16 kemId;
3168
    word16 encLen;
3169
    EchState state;
3170
    EchStateSNI sniState;
3171
    byte type;
3172
    byte configId;
3173
    byte enc[HPKE_Npk_MAX];
3174
    byte innerCount;
3175
    byte writeEncoded;
3176
} WOLFSSL_ECH;
3177
3178
WOLFSSL_LOCAL int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config);
3179
3180
WOLFSSL_LOCAL int TLSX_FinalizeEch(WOLFSSL_ECH* ech, byte* aad, word32 aadLen);
3181
3182
3183
WOLFSSL_LOCAL int SetEchConfigsEx(WOLFSSL_EchConfig** outputConfigs, void* heap,
3184
    const byte* echConfigs, word32 echConfigsLen);
3185
3186
WOLFSSL_LOCAL int GetEchConfig(WOLFSSL_EchConfig* config, byte* output,
3187
    word32* outputLen);
3188
3189
WOLFSSL_LOCAL int GetEchConfigsEx(WOLFSSL_EchConfig* configs,
3190
    byte* output, word32* outputLen);
3191
3192
WOLFSSL_LOCAL void FreeEchConfigs(WOLFSSL_EchConfig* configs, void* heap);
3193
3194
WOLFSSL_LOCAL int SetRetryConfigs(WOLFSSL* ssl, const byte* echConfigs,
3195
    word32 echConfigsLen);
3196
#endif
3197
3198
struct TLSX {
3199
    TLSX_Type    type; /* Extension Type  */
3200
    void*        data; /* Extension Data  */
3201
    word32       val;  /* Extension Value */
3202
    byte         resp; /* IsResponse Flag */
3203
    struct TLSX* next; /* List Behavior   */
3204
};
3205
3206
#ifdef WOLFSSL_API_PREFIX_MAP
3207
    #define TLSX_Find wolfSSL_TLSX_Find
3208
#endif
3209
WOLFSSL_TEST_VIS TLSX* TLSX_Find(TLSX* list, TLSX_Type type);
3210
WOLFSSL_LOCAL void  TLSX_Remove(TLSX** list, TLSX_Type type, void* heap);
3211
WOLFSSL_LOCAL void  TLSX_FreeAll(TLSX* list, void* heap);
3212
WOLFSSL_LOCAL int   TLSX_SupportExtensions(WOLFSSL* ssl);
3213
WOLFSSL_LOCAL int   TLSX_PopulateExtensions(WOLFSSL* ssl, byte isRequest);
3214
3215
#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
3216
#ifdef WOLFSSL_API_PREFIX_MAP
3217
    #define TLSX_GetRequestSize wolfSSL_TLSX_GetRequestSize
3218
    #define TLSX_WriteRequest   wolfSSL_TLSX_WriteRequest
3219
#endif
3220
WOLFSSL_TEST_VIS int   TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType,
3221
                                         word32* pLength);
3222
WOLFSSL_TEST_VIS int   TLSX_WriteRequest(WOLFSSL* ssl, byte* output,
3223
                                       byte msgType, word32* pOffset);
3224
#endif
3225
3226
#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
3227
/* TLS 1.3 Certificate messages have extensions. */
3228
WOLFSSL_LOCAL int   TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType,
3229
                                          word16* pLength);
3230
WOLFSSL_LOCAL int   TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType,
3231
                                        word16* pOffset);
3232
#endif
3233
3234
WOLFSSL_LOCAL int   TLSX_ParseVersion(WOLFSSL* ssl, const byte* input,
3235
                                      word16 length, byte msgType, int* found);
3236
WOLFSSL_LOCAL int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl,
3237
        const byte* input, word16 length, byte msgType, ProtocolVersion* pv,
3238
        Options* opts, TLSX** exts);
3239
#ifdef WOLFSSL_API_PREFIX_MAP
3240
    #define TLSX_Parse wolfSSL_TLSX_Parse
3241
#endif
3242
WOLFSSL_TEST_VIS int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length,
3243
                               byte msgType, Suites *suites);
3244
WOLFSSL_LOCAL int TLSX_Push(TLSX** list, TLSX_Type type,
3245
                            const void* data, void* heap);
3246
WOLFSSL_LOCAL int TLSX_Append(TLSX** list, TLSX_Type type,
3247
                            const void* data, void* heap);
3248
3249
#elif defined(HAVE_SNI)                           \
3250
   || defined(HAVE_MAX_FRAGMENT)                  \
3251
   || defined(HAVE_TRUSTED_CA)                    \
3252
   || defined(HAVE_TRUNCATED_HMAC)                \
3253
   || defined(HAVE_CERTIFICATE_STATUS_REQUEST)    \
3254
   || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) \
3255
   || defined(HAVE_SUPPORTED_CURVES)              \
3256
   || defined(HAVE_ALPN)                          \
3257
   || defined(HAVE_SESSION_TICKET)                \
3258
   || defined(HAVE_SECURE_RENEGOTIATION)          \
3259
   || defined(HAVE_SERVER_RENEGOTIATION_INFO)
3260
3261
#ifndef NO_TLS
3262
#error Using TLS extensions requires HAVE_TLS_EXTENSIONS to be defined.
3263
#endif
3264
#endif /* HAVE_TLS_EXTENSIONS */
3265
3266
/** Server Name Indication - RFC 6066 (session 3) */
3267
#ifdef HAVE_SNI
3268
3269
typedef struct SNI {
3270
    byte                       type;    /* SNI Type         */
3271
    union { char* host_name; } data;    /* SNI Data         */
3272
    struct SNI*                next;    /* List Behavior    */
3273
    byte                       status;  /* Matching result  */
3274
#ifndef NO_WOLFSSL_SERVER
3275
    byte                       options; /* Behavior options */
3276
#endif
3277
} SNI;
3278
3279
WOLFSSL_LOCAL int TLSX_UseSNI(TLSX** extensions, byte type, const void* data,
3280
                                                       word16 size, void* heap);
3281
WOLFSSL_LOCAL byte TLSX_SNI_Status(TLSX* extensions, byte type);
3282
WOLFSSL_LOCAL word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type,
3283
                                                void** data, byte ignoreStatus);
3284
#ifdef WOLFSSL_API_PREFIX_MAP
3285
    #define TLSX_SNI_GetSize wolfSSL_TLSX_SNI_GetSize
3286
#endif
3287
WOLFSSL_TEST_VIS word16 TLSX_SNI_GetSize(SNI* list);
3288
3289
#ifndef NO_WOLFSSL_SERVER
3290
WOLFSSL_LOCAL void   TLSX_SNI_SetOptions(TLSX* extensions, byte type,
3291
                                                                  byte options);
3292
WOLFSSL_LOCAL int    TLSX_SNI_GetFromBuffer(const byte* clientHello,
3293
                         word32 helloSz, byte type, byte* sni, word32* inOutSz);
3294
#endif
3295
3296
#endif /* HAVE_SNI */
3297
3298
/* Trusted CA Key Indication - RFC 6066 (section 6) */
3299
#ifdef HAVE_TRUSTED_CA
3300
3301
typedef struct TCA {
3302
    byte                       type;    /* TCA Type            */
3303
    byte*                      id;      /* TCA identifier      */
3304
    word16                     idSz;    /* TCA identifier size */
3305
    struct TCA*                next;    /* List Behavior       */
3306
} TCA;
3307
3308
WOLFSSL_LOCAL int TLSX_UseTrustedCA(TLSX** extensions, byte type,
3309
                    const byte* id, word16 idSz, void* heap);
3310
3311
#endif /* HAVE_TRUSTED_CA */
3312
3313
/* Application-Layer Protocol Negotiation - RFC 7301 */
3314
#ifdef HAVE_ALPN
3315
typedef struct ALPN {
3316
    char*        protocol_name; /* ALPN protocol name */
3317
    struct ALPN* next;          /* List Behavior      */
3318
    byte         options;       /* Behavior options */
3319
    byte         negotiated;    /* ALPN protocol negotiated or not */
3320
} ALPN;
3321
3322
WOLFSSL_LOCAL int TLSX_ALPN_GetRequest(TLSX* extensions,
3323
                                       void** data, word16 *dataSz);
3324
3325
WOLFSSL_LOCAL int TLSX_UseALPN(TLSX** extensions, const void* data,
3326
                               word16 size, byte options, void* heap);
3327
3328
WOLFSSL_LOCAL int TLSX_ALPN_SetOptions(TLSX** extensions, byte option);
3329
3330
#endif /* HAVE_ALPN */
3331
3332
/** Maximum Fragment Length Negotiation - RFC 6066 (session 4) */
3333
#ifdef HAVE_MAX_FRAGMENT
3334
3335
WOLFSSL_LOCAL int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap);
3336
3337
#endif /* HAVE_MAX_FRAGMENT */
3338
3339
/** Truncated HMAC - RFC 6066 (session 7) */
3340
#ifdef HAVE_TRUNCATED_HMAC
3341
3342
WOLFSSL_LOCAL int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap);
3343
3344
#endif /* HAVE_TRUNCATED_HMAC */
3345
3346
/** Certificate Status Request - RFC 6066 (session 8) */
3347
#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
3348
3349
typedef struct {
3350
    byte status_type;
3351
    byte options;
3352
    WOLFSSL* ssl;
3353
    union {
3354
        OcspRequest ocsp[MAX_CERT_EXTENSIONS];
3355
    } request;
3356
    word16 requests;
3357
#ifdef WOLFSSL_TLS13
3358
    buffer responses[MAX_CERT_EXTENSIONS];
3359
#endif
3360
} CertificateStatusRequest;
3361
3362
WOLFSSL_LOCAL int   TLSX_UseCertificateStatusRequest(TLSX** extensions,
3363
           byte status_type, byte options, WOLFSSL* ssl, void* heap, int devId);
3364
#ifndef NO_CERTS
3365
WOLFSSL_LOCAL int   TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert,
3366
                                                                    void* heap);
3367
WOLFSSL_LOCAL int   TLSX_CSR_InitRequest_ex(TLSX* extensions, DecodedCert* cert,
3368
                                            void* heap, int idx);
3369
#endif
3370
WOLFSSL_LOCAL void* TLSX_CSR_GetRequest(TLSX* extensions);
3371
WOLFSSL_LOCAL int   TLSX_CSR_ForceRequest(WOLFSSL* ssl);
3372
WOLFSSL_LOCAL word16 TLSX_CSR_GetSize_ex(CertificateStatusRequest* csr,
3373
                                        byte isRequest,
3374
                                        int idx);
3375
WOLFSSL_LOCAL int TLSX_CSR_Write_ex(CertificateStatusRequest* csr, byte* output,
3376
                          byte isRequest, int idx);
3377
WOLFSSL_LOCAL void* TLSX_CSR_GetRequest_ex(TLSX* extensions, int idx);
3378
3379
WOLFSSL_LOCAL int TLSX_CSR_SetResponseWithStatusCB(WOLFSSL *ssl);
3380
WOLFSSL_LOCAL int ProcessChainOCSPRequest(WOLFSSL* ssl);
3381
3382
#endif
3383
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
3384
    defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
3385
WOLFSSL_LOCAL int CreateOcspRequest(WOLFSSL* ssl, OcspRequest* request,
3386
                             DecodedCert* cert, byte* certData, word32 length,
3387
                             byte *ctxOwnsRequest);
3388
#endif
3389
/** Certificate Status Request v2 - RFC 6961 */
3390
#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
3391
3392
typedef struct CSRIv2 {
3393
    byte status_type;
3394
    byte options;
3395
    word16 requests;
3396
    union {
3397
        OcspRequest ocsp[1 + MAX_CHAIN_DEPTH];
3398
    } request;
3399
    struct CSRIv2* next;
3400
    Signer *pendingSigners;
3401
} CertificateStatusRequestItemV2;
3402
3403
WOLFSSL_LOCAL int   TLSX_UseCertificateStatusRequestV2(TLSX** extensions,
3404
                         byte status_type, byte options, void* heap, int devId);
3405
#ifndef NO_CERTS
3406
WOLFSSL_LOCAL int TLSX_CSR2_IsMulti(TLSX *extensions);
3407
WOLFSSL_LOCAL int TLSX_CSR2_AddPendingSigner(TLSX *extensions, Signer *s);
3408
WOLFSSL_LOCAL Signer* TLSX_CSR2_GetPendingSigners(TLSX *extensions);
3409
WOLFSSL_LOCAL int TLSX_CSR2_ClearPendingCA(WOLFSSL *ssl);
3410
WOLFSSL_LOCAL int TLSX_CSR2_MergePendingCA(WOLFSSL* ssl);
3411
WOLFSSL_LOCAL int   TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert,
3412
                                                       byte isPeer, void* heap);
3413
#endif
3414
WOLFSSL_LOCAL void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type,
3415
                                                                    byte idx);
3416
WOLFSSL_LOCAL int   TLSX_CSR2_ForceRequest(WOLFSSL* ssl);
3417
3418
#endif
3419
3420
#if defined(WOLFSSL_PUBLIC_ASN) && defined(HAVE_PK_CALLBACKS)
3421
/* Internal callback guarded by WOLFSSL_TEST_VIS because of DecodedCert. */
3422
typedef int (*CallbackProcessPeerCert)(WOLFSSL* ssl, DecodedCert* p_cert);
3423
WOLFSSL_TEST_VIS void wolfSSL_CTX_SetProcessPeerCertCb(WOLFSSL_CTX* ctx,
3424
       CallbackProcessPeerCert cb);
3425
#endif /* DecodedCert && HAVE_PK_CALLBACKS */
3426
3427
#if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
3428
typedef struct SignatureAlgorithms {
3429
    /* Not const since it is modified in TLSX_SignatureAlgorithms_MapPss */
3430
    WOLFSSL*    ssl;
3431
    word16      hashSigAlgoSz; /* SigAlgo extension length in bytes */
3432
    /* Ignore "nonstandard extension used : zero-sized array in struct/union"
3433
     * MSVC warning */
3434
    #ifdef _MSC_VER
3435
    #pragma warning(disable: 4200)
3436
    #endif
3437
    byte        hashSigAlgo[]; /* sig/algo to offer */
3438
} SignatureAlgorithms;
3439
3440
WOLFSSL_LOCAL SignatureAlgorithms* TLSX_SignatureAlgorithms_New(
3441
        WOLFSSL* ssl, word16 hashSigAlgoSz, void* heap);
3442
WOLFSSL_LOCAL void TLSX_SignatureAlgorithms_FreeAll(SignatureAlgorithms* sa,
3443
                                                    void* heap);
3444
#endif
3445
3446
/** Supported Elliptic Curves - RFC 4492 (session 4) */
3447
#ifdef HAVE_SUPPORTED_CURVES
3448
3449
typedef struct SupportedCurve {
3450
    word16 name;                 /* Curve Names */
3451
    struct SupportedCurve* next; /* List Behavior */
3452
} SupportedCurve;
3453
3454
typedef struct PointFormat {
3455
    byte format;                /* PointFormat */
3456
    struct PointFormat* next;   /* List Behavior */
3457
} PointFormat;
3458
3459
WOLFSSL_LOCAL int TLSX_SupportedCurve_Copy(TLSX* src, TLSX** dst, void* heap);
3460
WOLFSSL_LOCAL int TLSX_UseSupportedCurve(TLSX** extensions, word16 name,
3461
                                                                    void* heap);
3462
3463
WOLFSSL_LOCAL int TLSX_UsePointFormat(TLSX** extensions, byte point,
3464
                                                                    void* heap);
3465
WOLFSSL_LOCAL int TLSX_IsGroupSupported(int namedGroup);
3466
3467
#ifndef NO_WOLFSSL_SERVER
3468
WOLFSSL_LOCAL int TLSX_ValidateSupportedCurves(const WOLFSSL* ssl, byte first,
3469
                                               byte second, word32* ecdhCurveOID);
3470
WOLFSSL_LOCAL int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl);
3471
WOLFSSL_LOCAL int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl);
3472
#endif
3473
WOLFSSL_LOCAL int TLSX_SupportedCurve_IsSupported(WOLFSSL* ssl, word16 name);
3474
WOLFSSL_LOCAL int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl,
3475
                                                            int checkSupported);
3476
WOLFSSL_LOCAL int TLSX_SupportedCurve_Parse(const WOLFSSL* ssl,
3477
        const byte* input, word16 length, byte isRequest, TLSX** extensions);
3478
3479
#endif /* HAVE_SUPPORTED_CURVES */
3480
3481
/** Renegotiation Indication - RFC 5746 */
3482
#if defined(HAVE_SECURE_RENEGOTIATION) \
3483
 || defined(HAVE_SERVER_RENEGOTIATION_INFO)
3484
3485
enum key_cache_state {
3486
    SCR_CACHE_NULL   = 0,       /* empty / begin state */
3487
    SCR_CACHE_NEEDED,           /* need to cache keys */
3488
    SCR_CACHE_COPY,             /* we have a cached copy */
3489
    SCR_CACHE_PARTIAL,          /* partial restore to real keys */
3490
    SCR_CACHE_COMPLETE          /* complete restore to real keys */
3491
};
3492
3493
/* Additional Connection State according to rfc5746 section 3.1 */
3494
typedef struct SecureRenegotiation {
3495
   byte                 enabled;  /* secure_renegotiation flag in rfc */
3496
   byte                 verifySet;
3497
   byte                 startScr; /* server requested client to start scr */
3498
   enum key_cache_state cache_status;  /* track key cache state */
3499
   byte                 client_verify_data[TLS_FINISHED_SZ];  /* cached */
3500
   byte                 server_verify_data[TLS_FINISHED_SZ];  /* cached */
3501
   byte                 subject_hash_set; /* if peer cert hash is set */
3502
   byte                 subject_hash[KEYID_SIZE];  /* peer cert hash */
3503
   Keys                 tmp_keys;  /* can't overwrite real keys yet */
3504
} SecureRenegotiation;
3505
3506
WOLFSSL_LOCAL int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap);
3507
3508
#ifdef HAVE_SERVER_RENEGOTIATION_INFO
3509
WOLFSSL_LOCAL int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap);
3510
#endif
3511
3512
#endif /* HAVE_SECURE_RENEGOTIATION */
3513
3514
#ifdef HAVE_SESSION_TICKET
3515
/* Max peer cert size for ticket: 2KB is reasonable for most RSA/ECC certs */
3516
#ifndef MAX_TICKET_PEER_CERT_SZ
3517
#define MAX_TICKET_PEER_CERT_SZ 2048
3518
#endif
3519
#if defined(HAVE_SNI) || defined(HAVE_ALPN)
3520
/* Hash algorithm used for SNI/ALPN binding in session tickets.
3521
 * Pick the best available at compile time. */
3522
#ifndef TICKET_BINDING_HASH_TYPE
3523
    #if !defined(NO_SHA256)
3524
        #define TICKET_BINDING_HASH_TYPE WC_HASH_TYPE_SHA256
3525
        #define TICKET_BINDING_HASH_SZ   WC_SHA256_DIGEST_SIZE
3526
    #elif defined(WOLFSSL_SHA384)
3527
        #define TICKET_BINDING_HASH_TYPE WC_HASH_TYPE_SHA384
3528
        #define TICKET_BINDING_HASH_SZ   WC_SHA384_DIGEST_SIZE
3529
    #elif !defined(NO_SHA)
3530
        #define TICKET_BINDING_HASH_TYPE WC_HASH_TYPE_SHA
3531
        #define TICKET_BINDING_HASH_SZ   WC_SHA_DIGEST_SIZE
3532
    #else
3533
        #error "No hash algorithm available for ticket binding"
3534
    #endif
3535
#endif
3536
#endif
3537
3538
/* Our ticket format. All members need to be a byte or array of byte to
3539
 * avoid alignment issues */
3540
typedef struct InternalTicket {
3541
    ProtocolVersion pv;                    /* version when ticket created */
3542
    byte            suite[SUITE_LEN];      /* cipher suite when created */
3543
    byte            msecret[SECRET_LEN];   /* master secret */
3544
    byte            timestamp[TIMESTAMP_LEN];          /* born on */
3545
    byte            haveEMS;               /* have extended master secret */
3546
#ifdef WOLFSSL_TLS13
3547
    byte            ageAdd[AGEADD_LEN];    /* Obfuscation of age */
3548
    byte            namedGroup[NAMEDGROUP_LEN]; /* Named group used */
3549
    byte            ticketNonceLen;
3550
    byte            ticketNonce[MAX_TICKET_NONCE_STATIC_SZ];
3551
#ifdef WOLFSSL_EARLY_DATA
3552
    byte            maxEarlyDataSz[MAXEARLYDATASZ_LEN]; /* Max size of
3553
                                                         * early data */
3554
#endif
3555
#endif
3556
#ifdef WOLFSSL_TICKET_HAVE_ID
3557
    byte            id[ID_LEN];
3558
#endif
3559
#ifdef HAVE_SNI
3560
    byte            sniHash[TICKET_BINDING_HASH_SZ]; /* digest of server name
3561
                                                      * at ticket issue */
3562
#endif
3563
#ifdef HAVE_ALPN
3564
    byte            alpnHash[TICKET_BINDING_HASH_SZ]; /* digest of negotiated
3565
                                                       * ALPN at issue */
3566
#endif
3567
#ifdef OPENSSL_EXTRA
3568
    byte            sessionCtxSz;          /* sessionCtx length        */
3569
    byte            sessionCtx[ID_LEN];    /* app specific context id */
3570
#endif /* OPENSSL_EXTRA */
3571
#if defined(OPENSSL_ALL) && defined(KEEP_PEER_CERT) && \
3572
    !defined(NO_CERT_IN_TICKET)
3573
    byte            peerCertLen[OPAQUE16_LEN]; /* peer cert length */
3574
    byte            peerCert[]; /* peer certificate DER - variable length */
3575
#endif
3576
} InternalTicket;
3577
3578
/* Base size of InternalTicket without the variable-length peerCert field */
3579
#define WOLFSSL_INTERNAL_TICKET_BASE_SZ  (sizeof(InternalTicket))
3580
3581
/* Minimum internal ticket length (no peer cert) */
3582
#ifndef WOLFSSL_TICKET_ENC_CBC_HMAC
3583
    #define WOLFSSL_INTERNAL_TICKET_LEN     WOLFSSL_INTERNAL_TICKET_BASE_SZ
3584
#else
3585
    #define WOLFSSL_INTERNAL_TICKET_LEN     \
3586
        (((WOLFSSL_INTERNAL_TICKET_BASE_SZ + 15) / 16) * 16)
3587
#endif
3588
3589
/* Maximum internal ticket length (with max peer cert) */
3590
#if defined(OPENSSL_ALL) && defined(KEEP_PEER_CERT) && \
3591
    !defined(NO_CERT_IN_TICKET)
3592
    #define WOLFSSL_INTERNAL_TICKET_MAX_SZ  \
3593
        (WOLFSSL_INTERNAL_TICKET_BASE_SZ + MAX_TICKET_PEER_CERT_SZ)
3594
#else
3595
    #define WOLFSSL_INTERNAL_TICKET_MAX_SZ  WOLFSSL_INTERNAL_TICKET_BASE_SZ
3596
#endif
3597
3598
#ifndef WOLFSSL_TICKET_EXTRA_PADDING_SZ
3599
#define WOLFSSL_TICKET_EXTRA_PADDING_SZ 32
3600
#endif
3601
3602
/* Maximum encrypted ticket size */
3603
#define WOLFSSL_TICKET_ENC_SZ \
3604
    (WOLFSSL_INTERNAL_TICKET_MAX_SZ + WOLFSSL_TICKET_EXTRA_PADDING_SZ)
3605
3606
/* RFC 5077 defines this for session tickets. All members need to be a byte or
3607
 * array of byte to avoid alignment issues */
3608
typedef struct ExternalTicket {
3609
    byte key_name[WOLFSSL_TICKET_NAME_SZ];  /* key context name - 16 */
3610
    byte iv[WOLFSSL_TICKET_IV_SZ];          /* this ticket's iv - 16 */
3611
    byte enc_len[OPAQUE16_LEN];             /* encrypted length - 2 */
3612
    byte enc_ticket[];                      /* encrypted ticket - var length
3613
                                             *   + total mac - 32 */
3614
} ExternalTicket;
3615
3616
/* Fixed portion of external ticket (key_name + iv + enc_len) */
3617
#define WOLFSSL_TICKET_FIXED_SZ  \
3618
    (WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + OPAQUE16_LEN + \
3619
        WOLFSSL_TICKET_MAC_SZ)
3620
3621
/* Maximum session ticket length */
3622
#define SESSION_TICKET_LEN  \
3623
    ((int)(WOLFSSL_TICKET_FIXED_SZ + WOLFSSL_TICKET_ENC_SZ))
3624
3625
typedef struct SessionTicket {
3626
    word32 lifetime;
3627
#ifdef WOLFSSL_TLS13
3628
    word64 seen;
3629
    word32 ageAdd;
3630
#endif
3631
    byte*  data;
3632
    word16 size;
3633
} SessionTicket;
3634
3635
#if !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_WOLFSSL_SERVER)
3636
3637
/* Data passed to default SessionTicket enc/dec callback. */
3638
typedef struct TicketEncCbCtx {
3639
    /* Name for this context. */
3640
    byte name[WOLFSSL_TICKET_NAME_SZ];
3641
    /* Current keys - current and next. */
3642
    byte key[2][WOLFSSL_TICKET_KEY_SZ];
3643
    /* Expirary date of keys. */
3644
    word32 expirary[2];
3645
    /* Random number generator to use for generating name, keys and IV. */
3646
    WC_RNG rng;
3647
#ifndef SINGLE_THREADED
3648
    /* Mutex for access to changing keys. */
3649
    wolfSSL_Mutex mutex;
3650
#endif
3651
    /* Pointer back to SSL_CTX. */
3652
    WOLFSSL_CTX* ctx;
3653
} TicketEncCbCtx;
3654
3655
#endif /* !WOLFSSL_NO_DEF_TICKET_ENC_CB && !NO_WOLFSSL_SERVER */
3656
3657
#ifdef WOLFSSL_API_PREFIX_MAP
3658
    #define TLSX_UseSessionTicket     wolfSSL_TLSX_UseSessionTicket
3659
    #define TLSX_SessionTicket_Create wolfSSL_TLSX_SessionTicket_Create
3660
    #define TLSX_SessionTicket_Free   wolfSSL_TLSX_SessionTicket_Free
3661
#endif
3662
WOLFSSL_TEST_VIS int  TLSX_UseSessionTicket(TLSX** extensions,
3663
                                             SessionTicket* ticket, void* heap);
3664
WOLFSSL_TEST_VIS SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
3665
                                           byte* data, word16 size, void* heap);
3666
WOLFSSL_TEST_VIS void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap);
3667
3668
#endif /* HAVE_SESSION_TICKET */
3669
3670
#ifndef MAX_PSK_ID_LEN
3671
    /* max psk identity/hint supported */
3672
    #if defined(WOLFSSL_TLS13)
3673
        #ifdef SESSION_TICKET_LEN
3674
            #define MAX_PSK_ID_LEN SESSION_TICKET_LEN
3675
        #else
3676
            /* Previous value. Use as fallback for when tickets are disabled. */
3677
            #define MAX_PSK_ID_LEN 1536
3678
        #endif
3679
    #else
3680
        #define MAX_PSK_ID_LEN 128
3681
    #endif
3682
#endif
3683
3684
#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
3685
int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl);
3686
#endif
3687
3688
#ifdef WOLFSSL_TLS13
3689
/* Cookie extension information - cookie data. */
3690
typedef struct Cookie {
3691
    word16 len;
3692
    /* Ignore "nonstandard extension used : zero-sized array in struct/union"
3693
     * MSVC warning */
3694
    #ifdef _MSC_VER
3695
    #pragma warning(disable: 4200)
3696
    #endif
3697
    byte   data[];
3698
} Cookie;
3699
3700
WOLFSSL_LOCAL int TLSX_Cookie_Use(const WOLFSSL* ssl, const byte* data,
3701
        word16 len, byte* mac, byte macSz, int resp, TLSX** exts);
3702
WOLFSSL_LOCAL int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie,
3703
                                 word16 cookieSz);
3704
3705
3706
/* Key Share - TLS v1.3 Specification */
3707
3708
/* The KeyShare extension information - entry in a linked list. */
3709
typedef struct KeyShareEntry {
3710
    word16                group;     /* NamedGroup                        */
3711
    byte*                 ke;        /* Key exchange data                 */
3712
    word32                keLen;     /* Key exchange data length          */
3713
    void*                 key;       /* Key struct                        */
3714
    word32                keyLen;    /* Key size (bytes)                  */
3715
    byte*                 pubKey;    /* Public key                        */
3716
    word32                pubKeyLen; /* Public key length                 */
3717
#if !defined(NO_DH) || defined(WOLFSSL_HAVE_MLKEM)
3718
    byte*                 privKey;   /* Private key                       */
3719
    word32                privKeyLen;/* Private key length - PQC only     */
3720
#endif
3721
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
3722
    word16                session;   /* NamedGroup that was in session    */
3723
    word16                derived;   /* preMaster has been derived        */
3724
#endif
3725
#ifdef WOLFSSL_ASYNC_CRYPT
3726
    int                   lastRet;
3727
#endif
3728
    struct KeyShareEntry* next;      /* List pointer             */
3729
} KeyShareEntry;
3730
3731
WOLFSSL_LOCAL int TLSX_KeyShare_Use(const WOLFSSL* ssl, word16 group,
3732
        word16 len, byte* data, KeyShareEntry **kse, TLSX** extensions);
3733
WOLFSSL_LOCAL int TLSX_KeyShare_Empty(WOLFSSL* ssl);
3734
WOLFSSL_LOCAL int TLSX_KeyShare_SetSupported(const WOLFSSL* ssl,
3735
        TLSX** extensions);
3736
WOLFSSL_LOCAL int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse);
3737
WOLFSSL_LOCAL int TLSX_KeyShare_Choose(const WOLFSSL *ssl, TLSX* extensions,
3738
        byte cipherSuite0, byte cipherSuite, KeyShareEntry** kse,
3739
        byte* searched);
3740
WOLFSSL_LOCAL int TLSX_KeyShare_Setup(WOLFSSL *ssl, KeyShareEntry* clientKSE);
3741
WOLFSSL_LOCAL int TLSX_KeyShare_Establish(WOLFSSL* ssl, int* doHelloRetry);
3742
WOLFSSL_LOCAL int TLSX_KeyShare_DeriveSecret(WOLFSSL* sclientKSEclientKSEsl);
3743
WOLFSSL_LOCAL int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input,
3744
        word16 length, byte msgType);
3745
WOLFSSL_LOCAL int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
3746
        const byte* input, word16 length, TLSX** extensions);
3747
WOLFSSL_LOCAL int TLSX_KeyShare_HandlePqcHybridKeyServer(WOLFSSL* ssl,
3748
        KeyShareEntry* keyShareEntry, byte* data, word16 len);
3749
#ifdef WOLFSSL_DUAL_ALG_CERTS
3750
WOLFSSL_LOCAL int TLSX_CKS_Parse(WOLFSSL* ssl, byte* input,
3751
                                 word16 length, TLSX** extensions);
3752
WOLFSSL_LOCAL int TLSX_CKS_Set(WOLFSSL* ssl, TLSX** extensions);
3753
#endif
3754
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
3755
3756
enum PskDecryptReturn {
3757
    PSK_DECRYPT_NONE = 0,
3758
    PSK_DECRYPT_OK,
3759
    PSK_DECRYPT_CREATE,
3760
    PSK_DECRYPT_FAIL,
3761
};
3762
3763
#ifdef HAVE_SESSION_TICKET
3764
typedef struct psk_sess_free_cb_ctx {
3765
    word32 row;
3766
#ifdef HAVE_EXT_CACHE
3767
    int extCache;
3768
    int freeSess;
3769
#endif
3770
} psk_sess_free_cb_ctx;
3771
typedef void (psk_sess_free_cb)(const WOLFSSL* ssl, const WOLFSSL_SESSION* sess,
3772
        psk_sess_free_cb_ctx* freeCtx);
3773
#endif
3774
3775
/* The PreSharedKey extension information - entry in a linked list. */
3776
typedef struct PreSharedKey {
3777
    word16               identityLen;             /* Length of identity */
3778
    byte*                identity;                /* PSK identity       */
3779
    word32               ticketAge;               /* Age of the ticket  */
3780
    byte                 cipherSuite0;            /* Cipher Suite       */
3781
    byte                 cipherSuite;             /* Cipher Suite       */
3782
    word32               binderLen;               /* Length of HMAC     */
3783
    byte                 binder[WC_MAX_DIGEST_SIZE]; /* HMAC of handshake */
3784
    byte                 hmac;                    /* HMAC algorithm     */
3785
#ifdef HAVE_SESSION_TICKET
3786
    InternalTicket*      it;                      /* ptr to ticket      */
3787
    const WOLFSSL_SESSION* sess; /* ptr to session either from external cache or
3788
                                  * into SessionCache. Work around so that we
3789
                                  * don't call into the cache more than once */
3790
    psk_sess_free_cb* sess_free_cb;               /* callback to free sess */
3791
    psk_sess_free_cb_ctx sess_free_cb_ctx;        /* info for sess_free_cb */
3792
#endif
3793
    byte                 resumption:1;            /* Resumption PSK     */
3794
    byte                 chosen:1;                /* Server's choice    */
3795
    byte                 decryptRet:3;            /* Ticket decrypt return */
3796
    struct PreSharedKey* next;                    /* List pointer       */
3797
} PreSharedKey;
3798
3799
WOLFSSL_LOCAL int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list,
3800
                                                 byte* output, byte msgType,
3801
                                                 word16* pSz);
3802
WOLFSSL_LOCAL int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list,
3803
                                                   byte msgType, word16* pSz);
3804
WOLFSSL_LOCAL int TLSX_PreSharedKey_Use(TLSX** extensions, const byte* identity,
3805
                                        word16 len, word32 age, byte hmac,
3806
                                        byte cipherSuite0, byte cipherSuite,
3807
                                        byte resumption,
3808
                                        PreSharedKey **preSharedKey,
3809
                                        void* heap);
3810
WOLFSSL_LOCAL int TLSX_PreSharedKey_Parse_ClientHello(TLSX** extensions,
3811
                                  const byte* input, word16 length, void* heap);
3812
#if defined(WOLFSSL_CERT_WITH_EXTERN_PSK) && defined(WOLFSSL_TLS13)
3813
WOLFSSL_LOCAL int TLSX_CertWithExternPsk_Use(WOLFSSL* ssl);
3814
#endif
3815
3816
/* The possible Pre-Shared Key key exchange modes. */
3817
enum PskKeyExchangeMode {
3818
    PSK_KE,
3819
    PSK_DHE_KE
3820
};
3821
3822
/* User can define this. */
3823
#ifndef WOLFSSL_DEF_PSK_CIPHER
3824
#define WOLFSSL_DEF_PSK_CIPHER    TLS_AES_128_GCM_SHA256
3825
#endif
3826
3827
WOLFSSL_LOCAL int TLSX_PskKeyModes_Use(WOLFSSL* ssl, byte modes);
3828
WOLFSSL_LOCAL int TLSX_PskKeyModes_Parse_Modes(const byte* input, word16 length,
3829
                                              byte msgType, byte* modes);
3830
3831
#ifdef WOLFSSL_EARLY_DATA
3832
WOLFSSL_LOCAL int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 max, int is_response);
3833
#endif
3834
#endif /* HAVE_SESSION_TICKET || !NO_PSK */
3835
3836
3837
/* The types of keys to derive for. */
3838
enum DeriveKeyType {
3839
    no_key,
3840
    early_data_key,
3841
    handshake_key,
3842
    traffic_key,
3843
    update_traffic_key
3844
};
3845
3846
WOLFSSL_LOCAL int DeriveEarlySecret(WOLFSSL* ssl);
3847
WOLFSSL_LOCAL int DeriveHandshakeSecret(WOLFSSL* ssl);
3848
#ifdef WOLFSSL_API_PREFIX_MAP
3849
    #define DeriveTls13Keys wolfSSL_DeriveTls13Keys
3850
#endif
3851
WOLFSSL_TEST_VIS int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store);
3852
WOLFSSL_LOCAL int DeriveMasterSecret(WOLFSSL* ssl);
3853
WOLFSSL_LOCAL int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret);
3854
WOLFSSL_LOCAL int DeriveResumptionSecret(WOLFSSL* ssl, byte* key);
3855
3856
WOLFSSL_LOCAL int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
3857
        const char *label, size_t labelLen,
3858
        const unsigned char *context, size_t contextLen);
3859
3860
/* The key update request values for KeyUpdate message. */
3861
enum KeyUpdateRequest {
3862
    update_not_requested,
3863
    update_requested
3864
};
3865
#endif /* WOLFSSL_TLS13 */
3866
3867
#ifdef WOLFSSL_DTLS_CID
3868
WOLFSSL_LOCAL void TLSX_ConnectionID_Free(byte* ext, void* heap);
3869
WOLFSSL_LOCAL word16 TLSX_ConnectionID_Write(byte* ext, byte* output);
3870
WOLFSSL_LOCAL word16 TLSX_ConnectionID_GetSize(byte* ext);
3871
WOLFSSL_LOCAL int TLSX_ConnectionID_Use(WOLFSSL* ssl);
3872
WOLFSSL_LOCAL int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input,
3873
    word16 length, byte isRequest);
3874
WOLFSSL_LOCAL void DtlsCIDOnExtensionsParsed(WOLFSSL* ssl);
3875
WOLFSSL_LOCAL byte DtlsCIDCheck(WOLFSSL* ssl, const byte* input,
3876
    word16 inputSize);
3877
WOLFSSL_LOCAL int Dtls13UnifiedHeaderCIDPresent(byte flags);
3878
#endif /* WOLFSSL_DTLS_CID */
3879
WOLFSSL_LOCAL byte DtlsGetCidTxSize(WOLFSSL* ssl);
3880
WOLFSSL_LOCAL byte DtlsGetCidRxSize(WOLFSSL* ssl);
3881
3882
#ifdef OPENSSL_EXTRA
3883
enum SetCBIO {
3884
    WOLFSSL_CBIO_NONE = 0,
3885
    WOLFSSL_CBIO_RECV = 0x1,
3886
    WOLFSSL_CBIO_SEND = 0x2,
3887
};
3888
#endif
3889
3890
#ifdef WOLFSSL_STATIC_EPHEMERAL
3891
/* contains static ephemeral keys */
3892
typedef struct {
3893
#ifndef NO_DH
3894
    DerBuffer* dhKey;
3895
#endif
3896
#ifdef HAVE_ECC
3897
    DerBuffer* ecKey;
3898
#endif
3899
#ifdef HAVE_CURVE25519
3900
    DerBuffer* x25519Key;
3901
#endif
3902
#ifdef HAVE_CURVE448
3903
    DerBuffer* x448Key;
3904
#endif
3905
} StaticKeyExchangeInfo_t;
3906
#endif /* WOLFSSL_STATIC_EPHEMERAL */
3907
3908
3909
/* wolfSSL context type */
3910
struct WOLFSSL_CTX {
3911
    WOLFSSL_METHOD* method;
3912
#ifdef SINGLE_THREADED
3913
    WC_RNG*         rng;          /* to be shared with WOLFSSL w/o locking */
3914
#endif
3915
    wolfSSL_RefWithMutex ref;
3916
    int         err;              /* error code in case of mutex not created */
3917
#ifndef NO_DH
3918
    buffer      serverDH_P;
3919
    buffer      serverDH_G;
3920
#endif
3921
#ifndef NO_CERTS
3922
    DerBuffer*  certificate;
3923
    DerBuffer*  certChain;
3924
    int         certChainCnt;
3925
                 /* chain after self, in DER, with leading size for each cert */
3926
    #ifndef WOLFSSL_NO_CA_NAMES
3927
    WOLF_STACK_OF(WOLFSSL_X509_NAME)* client_ca_names;
3928
    WOLF_STACK_OF(WOLFSSL_X509_NAME)* ca_names;
3929
    #endif
3930
    #ifdef OPENSSL_EXTRA
3931
    WOLF_STACK_OF(WOLFSSL_X509)* x509Chain;
3932
    #endif
3933
#ifdef WOLFSSL_CERT_SETUP_CB
3934
#ifdef OPENSSL_EXTRA
3935
    client_cert_cb CBClientCert;  /* client certificate callback */
3936
#endif
3937
    CertSetupCallback  certSetupCb;
3938
    void*              certSetupCbArg;
3939
#endif
3940
    DerBuffer*  privateKey;
3941
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
3942
    DerBuffer*  privateKeyMask;             /* Mask of private key DER. */
3943
#endif
3944
    byte        privateKeyType;
3945
    byte        privateKeyId:1;
3946
    byte        privateKeyLabel:1;
3947
    int         privateKeySz;
3948
    int         privateKeyDevId;
3949
3950
#ifdef WOLFSSL_DUAL_ALG_CERTS
3951
    DerBuffer*  altPrivateKey;
3952
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
3953
    DerBuffer*  altPrivateKeyMask;          /* Mask of alt private key DER. */
3954
#endif
3955
    byte        altPrivateKeyType;
3956
    byte        altPrivateKeyId:1;
3957
    byte        altPrivateKeyLabel:1;
3958
    int         altPrivateKeySz;
3959
    int         altPrivateKeyDevId;
3960
#endif /* WOLFSSL_DUAL_ALG_CERTS */
3961
#ifdef OPENSSL_ALL
3962
    /* note it is the privateKeyPKey pointer that is volatile, not the object it
3963
     * points to:
3964
     */
3965
    WOLFSSL_EVP_PKEY* volatile privateKeyPKey;
3966
#endif
3967
    WOLFSSL_CERT_MANAGER* cm;      /* our cert manager, ctx owns SSL will use */
3968
#endif
3969
#ifdef KEEP_OUR_CERT
3970
    WOLFSSL_X509*    ourCert;     /* keep alive a X509 struct of cert */
3971
    int              ownOurCert;  /* Dispose of certificate if we own */
3972
#endif
3973
    Suites*     suites;           /* make dynamic, user may not need/set */
3974
    void*       heap;             /* for user memory overrides */
3975
    byte        verifyDepth;
3976
    byte        verifyPeer:1;
3977
    byte        verifyNone:1;
3978
    byte        failNoCert:1;
3979
    byte        failNoCertxPSK:1; /* fail if no cert with the exception of PSK*/
3980
    byte        sessionCacheOff:1;
3981
    byte        sessionCacheFlushOff:1;
3982
#ifdef HAVE_EXT_CACHE
3983
    byte        internalCacheOff:1;
3984
    byte        internalCacheLookupOff:1;
3985
#endif
3986
    byte        sendVerify:2;     /* for client side (can not be single bit) */
3987
    byte        haveRSA:1;        /* RSA available */
3988
    byte        haveECC:1;        /* ECC available */
3989
    byte        haveDH:1;         /* server DH params set by user */
3990
    byte        haveECDSAsig:1;   /* server cert signed w/ ECDSA */
3991
    byte        haveFalconSig:1;  /* server cert signed w/ Falcon */
3992
    byte        haveDilithiumSig:1;/* server cert signed w/ Dilithium */
3993
    byte        haveStaticECC:1;  /* static server ECC private key */
3994
    byte        partialWrite:1;   /* only one msg per write call */
3995
    byte        autoRetry:1;      /* retry read/write on a WANT_{READ|WRITE} */
3996
    byte        quietShutdown:1;  /* don't send close notify */
3997
    byte        groupMessages:1;  /* group handshake messages before sending */
3998
    byte        minDowngrade;     /* minimum downgrade version */
3999
    byte        haveEMS:1;        /* have extended master secret extension */
4000
    byte        useClientOrder:1; /* Use client's cipher preference order */
4001
#if defined(HAVE_SESSION_TICKET)
4002
    byte        noTicketTls12:1;  /* TLS 1.2 server won't send ticket */
4003
#endif
4004
#ifdef WOLFSSL_TLS13
4005
    #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
4006
    unsigned int maxTicketTls13;  /* maximum number of tickets to send */
4007
    #endif
4008
    byte        noTicketTls13:1;  /* TLS 1.3 Server won't create new Ticket */
4009
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
4010
    byte        noPskDheKe:1;     /* Don't use (EC)DHE with PSK */
4011
#ifdef HAVE_SUPPORTED_CURVES
4012
    byte        onlyPskDheKe:1;   /* Only use (EC)DHE with PSK */
4013
#endif
4014
#if defined(WOLFSSL_CERT_WITH_EXTERN_PSK)
4015
    byte        certWithExternPsk:1; /* Use tls_cert_with_extern_psk extension */
4016
#endif
4017
#endif
4018
#endif /* WOLFSSL_TLS13 */
4019
    byte        mutualAuth:1;     /* Mutual authentication required */
4020
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
4021
    byte        postHandshakeAuth:1;  /* Post-handshake auth supported. */
4022
    byte        verifyPostHandshake:1; /* Only send client cert req post
4023
                                        * handshake, not also during */
4024
#endif
4025
#ifndef NO_DH
4026
    #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
4027
        !defined(HAVE_SELFTEST)
4028
    byte        dhKeyTested:1;   /* Set when key has been tested. */
4029
    #endif
4030
#endif
4031
#if defined(HAVE_SECURE_RENEGOTIATION) || defined(HAVE_SERVER_RENEGOTIATION_INFO)
4032
    byte        useSecureReneg:1; /* when set will set WOLFSSL objects generated to enable */
4033
#endif
4034
#ifdef HAVE_ENCRYPT_THEN_MAC
4035
    byte        disallowEncThenMac:1;  /* Don't do Encrypt-Then-MAC */
4036
#endif
4037
#ifdef WOLFSSL_STATIC_MEMORY
4038
    byte        onHeapHint:1; /* whether the ctx/method is put on heap hint */
4039
#endif
4040
#if defined(WOLFSSL_STATIC_EPHEMERAL) && !defined(SINGLE_THREADED)
4041
    byte        staticKELockInit:1;
4042
#endif
4043
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SCTP)
4044
    byte        dtlsSctp:1;         /* DTLS-over-SCTP mode */
4045
#endif
4046
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
4047
    byte        disableECH:1;
4048
#endif
4049
    word16      minProto:1; /* sets min to min available */
4050
    word16      maxProto:1; /* sets max to max available */
4051
#if defined(HAVE_RPK)
4052
    RpkConfig   rpkConfig;
4053
    RpkState    rpkState;
4054
#endif /* HAVE_RPK */
4055
#ifdef WOLFSSL_SRTP
4056
    word16      dtlsSrtpProfiles;  /* DTLS-with-SRTP mode
4057
                                    * (list of selected profiles - up to 16) */
4058
#endif
4059
#if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST)
4060
    byte        haveMcast;        /* multicast requested */
4061
    byte        mcastID;          /* multicast group ID */
4062
#endif
4063
#if defined(WOLFSSL_DTLS) && \
4064
    (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU))
4065
    word16      dtlsMtuSz;        /* DTLS MTU size */
4066
#endif
4067
#ifndef NO_DH
4068
    word16      minDhKeySz;       /* minimum DH key size */
4069
    word16      maxDhKeySz;       /* maximum DH key size */
4070
#endif
4071
#ifndef NO_RSA
4072
    short       minRsaKeySz;      /* minimum RSA key size */
4073
#ifdef WC_RSA_PSS
4074
    word8       useRsaPss;        /* cert supports RSA-PSS */
4075
#endif
4076
#endif
4077
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
4078
    short       minEccKeySz;      /* minimum ECC key size */
4079
#endif
4080
#ifdef HAVE_FALCON
4081
    short       minFalconKeySz;   /* minimum Falcon key size */
4082
#endif
4083
#ifdef HAVE_DILITHIUM
4084
    short       minDilithiumKeySz;/* minimum Dilithium key size */
4085
#endif
4086
    unsigned long     mask;             /* store SSL_OP_ flags */
4087
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
4088
    word32            disabledCurves;   /* curves disabled by user */
4089
#endif
4090
#ifdef WOLFSSL_SESSION_ID_CTX
4091
    byte              sessionCtx[ID_LEN]; /* app session context ID */
4092
    byte              sessionCtxSz;
4093
#endif
4094
#ifdef OPENSSL_EXTRA
4095
    const unsigned char *alpn_cli_protos;/* ALPN client protocol list */
4096
    unsigned int         alpn_cli_protos_len;
4097
    byte              cbioFlag;  /* WOLFSSL_CBIO_RECV/SEND: CBIORecv/Send is set */
4098
    CallbackInfoState* CBIS;      /* used to get info about SSL state */
4099
    WOLFSSL_X509_VERIFY_PARAM* param;    /* verification parameters*/
4100
#endif
4101
#ifdef WOLFSSL_WOLFSENTRY_HOOKS
4102
    NetworkFilterCallback_t AcceptFilter;
4103
    void *AcceptFilter_arg;
4104
    NetworkFilterCallback_t ConnectFilter;
4105
    void *ConnectFilter_arg;
4106
#endif /* WOLFSSL_WOLFSENTRY_HOOKS */
4107
    CallbackIORecv CBIORecv;
4108
    CallbackIOSend CBIOSend;
4109
#ifdef WOLFSSL_DTLS
4110
    CallbackGenCookie CBIOCookie;       /* gen cookie callback */
4111
#endif /* WOLFSSL_DTLS */
4112
#ifdef WOLFSSL_SESSION_EXPORT
4113
#ifdef WOLFSSL_DTLS
4114
    wc_dtls_export  dtls_export;        /* export function for DTLS session */
4115
#endif
4116
    CallbackGetPeer CBGetPeer;
4117
    CallbackSetPeer CBSetPeer;
4118
#endif
4119
    VerifyCallback  verifyCallback;     /* cert verification callback */
4120
    void*           verifyCbCtx;        /* cert verify callback user ctx*/
4121
#ifdef OPENSSL_ALL
4122
    CertVerifyCallback verifyCertCb;
4123
    void*              verifyCertCbArg;
4124
#endif /* OPENSSL_ALL */
4125
#ifdef OPENSSL_EXTRA
4126
    SSL_Msg_Cb      protoMsgCb;         /* inspect protocol message callback */
4127
    void*           protoMsgCtx;        /* user set context with msg callback */
4128
#endif
4129
    word32          timeout;            /* session timeout */
4130
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
4131
    defined(HAVE_ED448)
4132
    word32          ecdhCurveOID;       /* curve Ecc_Sum */
4133
#endif
4134
#ifdef HAVE_ECC
4135
    word16          eccTempKeySz;       /* in octets 20 - 66 */
4136
#endif
4137
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
4138
    word32          pkCurveOID;         /* curve Ecc_Sum */
4139
#endif
4140
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
4141
    byte        havePSK;                /* psk key set by user */
4142
    wc_psk_client_callback client_psk_cb;  /* client callback */
4143
    wc_psk_server_callback server_psk_cb;  /* server callback */
4144
#ifdef WOLFSSL_TLS13
4145
    wc_psk_client_cs_callback    client_psk_cs_cb;     /* client callback */
4146
    wc_psk_client_tls13_callback client_psk_tls13_cb;  /* client callback */
4147
    wc_psk_server_tls13_callback server_psk_tls13_cb;  /* server callback */
4148
#endif
4149
    void*       psk_ctx;
4150
    char        server_hint[MAX_PSK_ID_LEN + NULL_TERM_LEN];
4151
#endif /* HAVE_SESSION_TICKET || !NO_PSK */
4152
#ifdef WOLFSSL_TLS13
4153
    word16          group[WOLFSSL_MAX_GROUP_COUNT];
4154
    byte            numGroups;
4155
#endif
4156
#ifdef WOLFSSL_EARLY_DATA
4157
    word32          maxEarlyDataSz;
4158
#endif
4159
#ifdef HAVE_ANON
4160
    byte        useAnon;               /* User wants to allow Anon suites */
4161
#endif /* HAVE_ANON */
4162
#ifdef WOLFSSL_ENCRYPTED_KEYS
4163
    wc_pem_password_cb* passwd_cb;
4164
    void*               passwd_userdata;
4165
#endif
4166
#ifdef WOLFSSL_LOCAL_X509_STORE
4167
    WOLFSSL_X509_STORE x509_store; /* points to ctx->cm */
4168
    WOLFSSL_X509_STORE* x509_store_pt; /* take ownership of external store */
4169
#endif
4170
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL)
4171
    byte            readAhead;
4172
    void*           userPRFArg; /* passed to prf callback */
4173
#endif
4174
#ifdef HAVE_EX_DATA
4175
    WOLFSSL_CRYPTO_EX_DATA ex_data;
4176
#endif
4177
#if defined(HAVE_ALPN) && (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
4178
    defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY) || defined(WOLFSSL_QUIC))
4179
    CallbackALPNSelect alpnSelect;
4180
    void*              alpnSelectArg;
4181
#endif
4182
#ifdef HAVE_SNI
4183
    CallbackSniRecv sniRecvCb;
4184
    void*           sniRecvCbArg;
4185
#endif
4186
#if defined(WOLFSSL_MULTICAST) && defined(WOLFSSL_DTLS)
4187
    CallbackMcastHighwater mcastHwCb; /* Sequence number highwater callback */
4188
    word32      mcastFirstSeq;    /* first trigger level */
4189
    word32      mcastSecondSeq;   /* second trigger level */
4190
    word32      mcastMaxSeq;      /* max level */
4191
#endif
4192
#ifdef HAVE_OCSP
4193
    WOLFSSL_OCSP      ocsp;
4194
#endif
4195
    int             devId;              /* async device id to use */
4196
#ifdef HAVE_TLS_EXTENSIONS
4197
    TLSX* extensions;                  /* RFC 6066 TLS Extensions data */
4198
    #ifndef NO_WOLFSSL_SERVER
4199
        #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
4200
         || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
4201
            OcspRequest* certOcspRequest;
4202
            ocspVerifyStatusCb ocspStatusVerifyCb;
4203
            void* ocspStatusVerifyCbArg;
4204
        #endif
4205
        #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
4206
            OcspRequest* chainOcspRequest[MAX_CHAIN_DEPTH];
4207
        #endif
4208
    #endif
4209
    #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
4210
        SessionTicketEncCb ticketEncCb;   /* enc/dec session ticket Cb */
4211
        void*              ticketEncCtx;  /* session encrypt context */
4212
        #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
4213
          || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY)
4214
        ticketCompatCb     ticketEncWrapCb; /* callback for OpenSSL ticket key callback */
4215
        #endif
4216
        int                ticketHint;    /* ticket hint in seconds */
4217
        #ifndef WOLFSSL_NO_DEF_TICKET_ENC_CB
4218
            TicketEncCbCtx ticketKeyCtx;
4219
        #endif
4220
    #endif
4221
    #endif
4222
    #ifdef HAVE_SUPPORTED_CURVES
4223
        byte userCurves;                  /* indicates user called wolfSSL_CTX_UseSupportedCurve */
4224
    #endif
4225
#ifdef ATOMIC_USER
4226
    CallbackMacEncrypt    MacEncryptCb;    /* Atomic User Mac/Encrypt Cb */
4227
    CallbackDecryptVerify DecryptVerifyCb; /* Atomic User Decrypt/Verify Cb */
4228
    #ifdef HAVE_ENCRYPT_THEN_MAC
4229
        CallbackEncryptMac    EncryptMacCb;    /* Atomic User Mac/Enc Cb */
4230
        CallbackVerifyDecrypt VerifyDecryptCb; /* Atomic User Dec/Verify Cb */
4231
    #endif
4232
#endif
4233
#ifdef HAVE_PK_CALLBACKS
4234
    #ifdef HAVE_ECC
4235
        CallbackEccKeyGen EccKeyGenCb;  /* User EccKeyGen Callback Handler */
4236
        CallbackEccSign   EccSignCb;    /* User EccSign   Callback handler */
4237
        void*             EccSignCtx;   /* Ecc Sign       Callback Context */
4238
        CallbackEccVerify EccVerifyCb;  /* User EccVerify Callback handler */
4239
        CallbackEccSharedSecret EccSharedSecretCb; /* User EccVerify Callback handler */
4240
    #endif /* HAVE_ECC */
4241
    #ifdef HAVE_HKDF
4242
        CallbackHKDFExtract HkdfExtractCb; /* User hkdf Extract Callback handler */
4243
    #endif
4244
    #ifdef HAVE_ED25519
4245
        /* User Ed25519Sign   Callback handler */
4246
        CallbackEd25519Sign   Ed25519SignCb;
4247
        /* User Ed25519Verify Callback handler */
4248
        CallbackEd25519Verify Ed25519VerifyCb;
4249
    #endif
4250
    #ifdef HAVE_CURVE25519
4251
        /* User X25519 KeyGen Callback Handler */
4252
        CallbackX25519KeyGen X25519KeyGenCb;
4253
        /* User X25519 SharedSecret Callback handler */
4254
        CallbackX25519SharedSecret X25519SharedSecretCb;
4255
    #endif
4256
    #ifdef HAVE_ED448
4257
        /* User Ed448Sign   Callback handler */
4258
        CallbackEd448Sign   Ed448SignCb;
4259
        /* User Ed448Verify Callback handler */
4260
        CallbackEd448Verify Ed448VerifyCb;
4261
    #endif
4262
    #ifdef HAVE_CURVE448
4263
        /* User X448 KeyGen Callback Handler */
4264
        CallbackX448KeyGen X448KeyGenCb;
4265
        /* User X448 SharedSecret Callback handler */
4266
        CallbackX448SharedSecret X448SharedSecretCb;
4267
    #endif
4268
    #ifndef NO_DH
4269
        /* User DH KeyGen Callback handler*/
4270
        CallbackDhGenerateKeyPair DhGenerateKeyPairCb;
4271
        /* User DH Agree Callback handler */
4272
        CallbackDhAgree DhAgreeCb;
4273
    #endif
4274
    #ifndef NO_RSA
4275
        /* User RsaSign Callback handler (priv key) */
4276
        CallbackRsaSign   RsaSignCb;
4277
        /* User RsaVerify Callback handler (pub key) */
4278
        CallbackRsaVerify RsaVerifyCb;
4279
        /* User VerifyRsaSign Callback handler (priv key) */
4280
        CallbackRsaVerify RsaSignCheckCb;
4281
        #ifdef WC_RSA_PSS
4282
            /* User RsaSign (priv key) */
4283
            CallbackRsaPssSign   RsaPssSignCb;
4284
            /* User RsaVerify (pub key) */
4285
            CallbackRsaPssVerify RsaPssVerifyCb;
4286
            /* User VerifyRsaSign (priv key) */
4287
            CallbackRsaPssVerify RsaPssSignCheckCb;
4288
        #endif
4289
        CallbackRsaEnc    RsaEncCb;     /* User Rsa Public Encrypt  handler */
4290
        CallbackRsaDec    RsaDecCb;     /* User Rsa Private Decrypt handler */
4291
    #endif /* NO_RSA */
4292
4293
    /* User generate pre-master handler */
4294
    CallbackGenPreMaster        GenPreMasterCb;
4295
    /* User generate master secret handler */
4296
    CallbackGenMasterSecret     GenMasterCb;
4297
    /* User generate Extended master secret handler */
4298
    CallbackGenExtMasterSecret  GenExtMasterCb;
4299
    /* User generate session key handler */
4300
    CallbackGenSessionKey       GenSessionKeyCb;
4301
    /* User setting encrypt keys handler */
4302
    CallbackEncryptKeys         EncryptKeysCb;
4303
    /* User Tls finished handler */
4304
    CallbackTlsFinished         TlsFinishedCb;
4305
#if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
4306
    /* User Verify mac handler */
4307
    CallbackVerifyMac           VerifyMacCb;
4308
#endif
4309
#if defined(WOLFSSL_PUBLIC_ASN)
4310
    /* User handler to process a certificate */
4311
    CallbackProcessPeerCert ProcessPeerCertCb;
4312
#endif
4313
    /* User handler to process the server's key exchange public key */
4314
    CallbackProcessServerSigKex ProcessServerSigKexCb;
4315
    /* User handler to process the TLS record */
4316
    CallbackPerformTlsRecordProcessing PerformTlsRecordProcessingCb;
4317
    /* User handler to do HKDF expansions */
4318
    CallbackHKDFExpandLabel HKDFExpandLabelCb;
4319
4320
#endif /* HAVE_PK_CALLBACKS */
4321
#ifdef HAVE_WOLF_EVENT
4322
    WOLF_EVENT_QUEUE event_queue;
4323
#endif /* HAVE_WOLF_EVENT */
4324
#ifdef HAVE_EXT_CACHE
4325
    WOLFSSL_SESSION*(*get_sess_cb)(WOLFSSL*, const unsigned char*, int, int*);
4326
    int (*new_sess_cb)(WOLFSSL*, WOLFSSL_SESSION*);
4327
#endif
4328
#if defined(HAVE_EXT_CACHE) || defined(HAVE_EX_DATA)
4329
    Rem_Sess_Cb rem_sess_cb;
4330
#endif
4331
#if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256)
4332
    Srp*  srp;  /* TLS Secure Remote Password Protocol*/
4333
    byte* srp_password;
4334
#endif
4335
#if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
4336
    wolfSSL_CTX_keylog_cb_func keyLogCb;
4337
#endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
4338
#ifdef WOLFSSL_STATIC_EPHEMERAL
4339
    StaticKeyExchangeInfo_t staticKE;
4340
    #ifndef SINGLE_THREADED
4341
    wolfSSL_Mutex staticKELock;
4342
    #endif
4343
#endif
4344
#ifdef WOLFSSL_QUIC
4345
    struct {
4346
        const WOLFSSL_QUIC_METHOD *method;
4347
    } quic;
4348
#endif
4349
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
4350
    WOLFSSL_EchConfig* echConfigs;
4351
#endif
4352
#if defined(__APPLE__) && defined(WOLFSSL_SYS_CA_CERTS)
4353
    byte doAppleNativeCertValidationFlag:1;
4354
#endif /* defined(__APPLE__) && defined(WOLFSSL_SYS_CA_CERTS) */
4355
#ifdef WOLFSSL_DUAL_ALG_CERTS
4356
    byte *sigSpec;
4357
    word16 sigSpecSz;
4358
#endif
4359
#if defined(WOLFSSL_SYS_CRYPTO_POLICY)
4360
    int secLevel; /* The security level of system-wide crypto policy. */
4361
#endif /* WOLFSSL_SYS_CRYPTO_POLICY */
4362
4363
#ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION
4364
    CFMutableArrayRef testTrustedCAs;
4365
#endif /* WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION */
4366
};
4367
4368
WOLFSSL_LOCAL
4369
int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap);
4370
WOLFSSL_LOCAL
4371
void FreeSSL_Ctx(WOLFSSL_CTX* ctx);
4372
WOLFSSL_LOCAL
4373
void SSL_CtxResourceFree(WOLFSSL_CTX* ctx);
4374
4375
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
4376
    #ifndef HAVE_EX_DATA
4377
        #error "HAVE_EX_DATA_CLEANUP_HOOKS requires HAVE_EX_DATA to be defined"
4378
    #endif
4379
void wolfSSL_CRYPTO_cleanup_ex_data(WOLFSSL_CRYPTO_EX_DATA* ex_data);
4380
#endif
4381
4382
WOLFSSL_LOCAL
4383
int DeriveTlsKeys(WOLFSSL* ssl);
4384
WOLFSSL_LOCAL
4385
int ProcessOldClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
4386
                          word32 inSz, word16 sz);
4387
4388
#ifndef NO_CERTS
4389
    WOLFSSL_LOCAL int AddSigner(WOLFSSL_CERT_MANAGER* cm, Signer *s);
4390
    WOLFSSL_LOCAL
4391
    int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify);
4392
    WOLFSSL_LOCAL int RemoveCA(WOLFSSL_CERT_MANAGER* cm, byte* hash, int type);
4393
    WOLFSSL_LOCAL int SetCAType(WOLFSSL_CERT_MANAGER* cm, byte* hash, int type);
4394
    WOLFSSL_LOCAL
4395
    int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash);
4396
#ifdef WOLFSSL_TRUST_PEER_CERT
4397
    WOLFSSL_LOCAL
4398
    int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify);
4399
    WOLFSSL_LOCAL
4400
    int AlreadyTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DecodedCert* cert);
4401
#endif
4402
#endif
4403
4404
#ifdef WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION
4405
    WOLFSSL_API
4406
    int wolfSSL_TestAppleNativeCertValidation_AppendCA(WOLFSSL_CTX* ctx,
4407
                                                    const byte* derCert,
4408
                                                    int derLen);
4409
#endif /* WOLFSSL_TEST_APPLE_NATIVE_CERT_VALIDATION */
4410
4411
/* All cipher suite related info
4412
 * Keep as a constant size (no ifdefs) for session export */
4413
typedef struct CipherSpecs {
4414
    word16 key_size;
4415
    word16 iv_size;
4416
    word16 block_size;
4417
    word16 aead_mac_size;
4418
    byte bulk_cipher_algorithm;
4419
    byte cipher_type;               /* block, stream, or aead */
4420
    byte mac_algorithm;
4421
    byte kea;                       /* key exchange algo */
4422
    byte sig_algo;
4423
    byte hash_size;
4424
    byte pad_size;
4425
    byte static_ecdh;
4426
} CipherSpecs;
4427
4428
4429
void InitCipherSpecs(CipherSpecs* cs);
4430
4431
4432
/* Supported Key Exchange Protocols */
4433
enum KeyExchangeAlgorithm {
4434
    no_kea,
4435
    rsa_kea,
4436
    diffie_hellman_kea,
4437
    fortezza_kea,
4438
    psk_kea,
4439
    dhe_psk_kea,
4440
    ecdhe_psk_kea,
4441
    ecc_diffie_hellman_kea,
4442
    ecc_static_diffie_hellman_kea,      /* for verify suite only */
4443
    any_kea
4444
};
4445
4446
/* Used with InitSuitesHashSigAlgo */
4447
0
#define SIG_ECDSA       0x01
4448
0
#define SIG_RSA         0x02
4449
0
#define SIG_SM2         0x04
4450
0
#define SIG_FALCON      0x08
4451
0
#define SIG_DILITHIUM   0x10
4452
#define SIG_ANON        0x20
4453
/* SIG_ANON is omitted by default */
4454
0
#define SIG_ALL         (SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | \
4455
0
                         SIG_DILITHIUM)
4456
4457
/* Supported Authentication Schemes */
4458
enum SignatureAlgorithm {
4459
    anonymous_sa_algo            = 0,
4460
    rsa_sa_algo                  = 1,
4461
    dsa_sa_algo                  = 2,
4462
    ecc_dsa_sa_algo              = 3,
4463
    rsa_pss_sa_algo              = 8,
4464
    ed25519_sa_algo              = 9,
4465
    rsa_pss_pss_algo             = 10,
4466
    ed448_sa_algo                = 11,
4467
    falcon_level1_sa_algo        = 12,
4468
    falcon_level5_sa_algo        = 13,
4469
    dilithium_level2_sa_algo     = 14,
4470
    dilithium_level3_sa_algo     = 15,
4471
    dilithium_level5_sa_algo     = 16,
4472
    sm2_sa_algo                  = 17,
4473
    any_sa_algo                  = 18,
4474
    ecc_brainpool_sa_algo        = 19,
4475
    invalid_sa_algo              = 255
4476
};
4477
4478
#define PSS_RSAE_TO_PSS_PSS(macAlgo) \
4479
    ((macAlgo) + (pss_sha256 - sha256_mac))
4480
4481
#define PSS_PSS_HASH_TO_MAC(macAlgo) \
4482
    ((macAlgo) - (pss_sha256 - sha256_mac))
4483
4484
enum SigAlgRsaPss {
4485
    pss_sha256  = 0x09,
4486
    pss_sha384  = 0x0a,
4487
    pss_sha512  = 0x0b,
4488
};
4489
4490
#ifdef WOLFSSL_SM2
4491
    /* Default SM2 signature ID. */
4492
0
    #define TLS12_SM2_SIG_ID        ((byte*)"1234567812345678")
4493
    /* Length of default SM2 signature ID. */
4494
0
    #define TLS12_SM2_SIG_ID_SZ     16
4495
4496
    /* https://www.rfc-editor.org/rfc/rfc8998.html#name-sm2-signature-scheme */
4497
    /* ID to use when signing/verifying TLS v1.3 data. */
4498
0
    #define TLS13_SM2_SIG_ID        ((byte*)"TLSv1.3+GM+Cipher+Suite")
4499
    /* Length of ID to use when signing/verifying TLS v1.3 data. */
4500
0
    #define TLS13_SM2_SIG_ID_SZ     23
4501
#endif
4502
4503
/* Supported ECC Curve Types */
4504
enum EccCurves {
4505
    named_curve = 3
4506
};
4507
4508
4509
/* Valid client certificate request types from page 27 */
4510
enum ClientCertificateType {
4511
    rsa_sign            = 1,
4512
    dss_sign            = 2,
4513
    rsa_fixed_dh        = 3,
4514
    dss_fixed_dh        = 4,
4515
    rsa_ephemeral_dh    = 5,
4516
    dss_ephemeral_dh    = 6,
4517
    fortezza_kea_cert   = 20,
4518
    ecdsa_sign          = 64,
4519
    rsa_fixed_ecdh      = 65,
4520
    ecdsa_fixed_ecdh    = 66,
4521
    falcon_sign         = 67,
4522
    dilithium_sign      = 68,
4523
};
4524
4525
4526
#ifndef WOLFSSL_AEAD_ONLY
4527
enum CipherType { stream, block, aead };
4528
#else
4529
enum CipherType { aead };
4530
#endif
4531
4532
4533
#if defined(BUILD_AES) || defined(BUILD_AESGCM) || defined(HAVE_ARIA) || \
4534
        (defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(WOLFSSL_TLS13)
4535
    #define CIPHER_NONCE
4536
#endif
4537
4538
#if defined(WOLFSSL_DTLS) && defined(HAVE_SECURE_RENEGOTIATION)
4539
enum CipherSrc {
4540
    KEYS_NOT_SET = 0,
4541
    KEYS,     /* keys from ssl->keys are loaded */
4542
    SCR       /* keys from ssl->secure_renegotiation->tmp_keys are loaded */
4543
};
4544
#endif
4545
4546
#ifdef WOLFSSL_CIPHER_TEXT_CHECK
4547
    #ifndef WOLFSSL_CIPHER_CHECK_SZ
4548
        /* 64-bits to confirm encrypt operation worked */
4549
        #define WOLFSSL_CIPHER_CHECK_SZ 8
4550
    #endif
4551
#endif
4552
4553
/* cipher for now */
4554
typedef struct Ciphers {
4555
#ifdef BUILD_ARC4
4556
    Arc4*   arc4;
4557
#endif
4558
#ifdef BUILD_DES3
4559
    Des3*   des3;
4560
#endif
4561
#if defined(BUILD_AES) || defined(BUILD_AESGCM)
4562
    Aes*    aes;
4563
#endif
4564
#if (defined(BUILD_AESGCM) || defined(HAVE_AESCCM)) && !defined(WOLFSSL_NO_TLS12)
4565
    byte* additional;
4566
#endif
4567
#ifdef HAVE_ARIA
4568
    wc_Aria* aria;
4569
#endif
4570
#ifdef CIPHER_NONCE
4571
    byte* nonce;
4572
#endif
4573
#ifdef HAVE_CAMELLIA
4574
    wc_Camellia* cam;
4575
#endif
4576
#ifdef HAVE_CHACHA
4577
    ChaCha*   chacha;
4578
#endif
4579
#ifdef WOLFSSL_SM4
4580
    wc_Sm4*   sm4;
4581
#endif
4582
#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER) && !defined(NO_HMAC)
4583
    Hmac* hmac;
4584
#endif
4585
#ifdef WOLFSSL_CIPHER_TEXT_CHECK
4586
    word32 sanityCheck[WOLFSSL_CIPHER_CHECK_SZ/sizeof(word32)];
4587
#endif
4588
    byte    state;
4589
    byte    setup;       /* have we set it up flag for detection */
4590
#if defined(WOLFSSL_DTLS) && defined(HAVE_SECURE_RENEGOTIATION)
4591
    enum CipherSrc src;  /* DTLS uses this to determine which keys
4592
                          * are currently loaded */
4593
#endif
4594
} Ciphers;
4595
4596
#ifdef WOLFSSL_DTLS13
4597
typedef struct RecordNumberCiphers {
4598
#if defined(BUILD_AES) || defined(BUILD_AESGCM)
4599
        Aes *aes;
4600
#endif /*  BUILD_AES || BUILD_AESGCM */
4601
#ifdef HAVE_CHACHA
4602
        ChaCha *chacha;
4603
#endif
4604
} RecordNumberCiphers;
4605
#endif /* WOLFSSL_DTLS13 */
4606
4607
#ifdef HAVE_ONE_TIME_AUTH
4608
/* Ciphers for one time authentication such as poly1305 */
4609
typedef struct OneTimeAuth {
4610
#ifdef HAVE_POLY1305
4611
    Poly1305* poly1305;
4612
#endif
4613
    byte    setup;      /* flag for if a cipher has been set */
4614
4615
} OneTimeAuth;
4616
#endif
4617
4618
4619
WOLFSSL_LOCAL void InitCiphers(WOLFSSL* ssl);
4620
WOLFSSL_LOCAL void FreeCiphers(WOLFSSL* ssl);
4621
4622
4623
/* hashes type */
4624
typedef struct Hashes {
4625
    #if !defined(NO_MD5) && !defined(NO_OLD_TLS)
4626
        byte md5[WC_MD5_DIGEST_SIZE];
4627
    #endif
4628
    #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
4629
                              defined(WOLFSSL_ALLOW_TLS_SHA1))
4630
        byte sha[WC_SHA_DIGEST_SIZE];
4631
    #endif
4632
    #ifndef NO_SHA256
4633
        byte sha256[WC_SHA256_DIGEST_SIZE];
4634
    #endif
4635
    #ifdef WOLFSSL_SHA384
4636
        byte sha384[WC_SHA384_DIGEST_SIZE];
4637
    #endif
4638
    #ifdef WOLFSSL_SHA512
4639
        byte sha512[WC_SHA512_DIGEST_SIZE];
4640
    #endif
4641
    #ifdef WOLFSSL_SM3
4642
        byte sm3[WC_SM3_DIGEST_SIZE];
4643
    #endif
4644
} Hashes;
4645
4646
WOLFSSL_LOCAL int BuildCertHashes(const WOLFSSL* ssl, Hashes* hashes);
4647
4648
#ifdef WOLFSSL_TLS13
4649
typedef union Digest {
4650
#ifndef NO_SHA256
4651
    wc_Sha256 sha256;
4652
#endif
4653
#ifdef WOLFSSL_SHA384
4654
    wc_Sha384 sha384;
4655
#endif
4656
#ifdef WOLFSSL_SHA512
4657
    wc_Sha512 sha512;
4658
#endif
4659
#ifdef WOLFSSL_SM3
4660
    wc_Sm3    sm3;
4661
#endif
4662
} Digest;
4663
#endif
4664
4665
/* Static x509 buffer */
4666
typedef struct x509_buffer {
4667
    int  length;                  /* actual size */
4668
    byte buffer[MAX_X509_SIZE];   /* max static cert size */
4669
} x509_buffer;
4670
4671
4672
/* wolfSSL X509_CHAIN, for no dynamic memory SESSION_CACHE */
4673
struct WOLFSSL_X509_CHAIN {
4674
    int         count;                    /* total number in chain */
4675
    x509_buffer certs[MAX_CHAIN_DEPTH];   /* only allow max depth 4 for now */
4676
};
4677
4678
typedef enum WOLFSSL_SESSION_TYPE {
4679
    WOLFSSL_SESSION_TYPE_UNKNOWN,
4680
    WOLFSSL_SESSION_TYPE_SSL,    /* in ssl->session */
4681
    WOLFSSL_SESSION_TYPE_CACHE,  /* pointer to internal cache */
4682
    WOLFSSL_SESSION_TYPE_HEAP    /* allocated from heap SESSION_new */
4683
} WOLFSSL_SESSION_TYPE;
4684
4685
#ifdef WOLFSSL_QUIC
4686
typedef struct QuicRecord QuicRecord;
4687
typedef struct QuicRecord {
4688
    struct QuicRecord *next;
4689
    uint8_t *data;
4690
    word32 capacity;
4691
    word32 len;
4692
    word32 start;
4693
    word32 end;
4694
    WOLFSSL_ENCRYPTION_LEVEL level;
4695
    word32 rec_hdr_remain;
4696
} QuicEncData;
4697
4698
typedef struct QuicTransportParam QuicTransportParam;
4699
struct QuicTransportParam {
4700
    const uint8_t *data;
4701
    word16 len;
4702
};
4703
4704
WOLFSSL_LOCAL const QuicTransportParam *QuicTransportParam_new(const uint8_t *data, size_t len, void *heap);
4705
WOLFSSL_LOCAL const QuicTransportParam *QuicTransportParam_dup(const QuicTransportParam *tp, void *heap);
4706
WOLFSSL_LOCAL void QuicTransportParam_free(const QuicTransportParam *tp, void *heap);
4707
WOLFSSL_LOCAL int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response);
4708
WOLFSSL_LOCAL int wolfSSL_quic_add_transport_extensions(WOLFSSL *ssl, int msg_type);
4709
4710
#define QTP_FREE     QuicTransportParam_free
4711
4712
#endif /* WOLFSSL_QUIC */
4713
4714
/** Session Ticket - RFC 5077 (session 3.2) */
4715
#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
4716
/* Ticket nonce - for deriving PSK.
4717
   Length allowed to be: 1..255. Only support
4718
 * TLS13_TICKET_NONCE_STATIC_SZ length bytes.
4719
 */
4720
typedef struct TicketNonce {
4721
    byte len;
4722
#if defined(WOLFSSL_TICKET_NONCE_MALLOC) &&                                    \
4723
    (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
4724
    byte *data;
4725
    byte dataStatic[MAX_TICKET_NONCE_STATIC_SZ];
4726
#else
4727
    byte data[MAX_TICKET_NONCE_STATIC_SZ];
4728
#endif /* WOLFSSL_TICKET_NONCE_MALLOC  && FIPS_VERSION_GE(5,3) */
4729
} TicketNonce;
4730
#endif
4731
4732
/* wolfSSL session type */
4733
struct WOLFSSL_SESSION {
4734
    /* WARNING Do not add fields here. They will be ignored in
4735
     *         wolfSSL_DupSession. */
4736
    WOLFSSL_SESSION_TYPE type;
4737
#ifndef NO_SESSION_CACHE
4738
    int                cacheRow;          /* row in session cache     */
4739
#endif
4740
    wolfSSL_Ref        ref;
4741
    byte               altSessionID[ID_LEN];
4742
    byte               haveAltSessionID:1;
4743
#ifdef HAVE_EX_DATA
4744
    byte               ownExData:1;
4745
#endif
4746
#if defined(HAVE_EXT_CACHE) || defined(HAVE_EX_DATA)
4747
    Rem_Sess_Cb        rem_sess_cb;
4748
#endif
4749
    void*              heap;
4750
    /* WARNING The above fields (up to and including the heap) are not copied
4751
     *         in wolfSSL_DupSession. Place new fields after the heap
4752
     *         member */
4753
4754
    byte               side;              /* Either WOLFSSL_CLIENT_END or
4755
                                                    WOLFSSL_SERVER_END */
4756
4757
    word32             bornOn;            /* create time in seconds   */
4758
    word32             timeout;           /* timeout in seconds       */
4759
4760
    byte               sessionID[ID_LEN]; /* id for protocol or bogus
4761
                                           * ID for TLS 1.3           */
4762
    byte               sessionIDSz;
4763
4764
    byte               masterSecret[SECRET_LEN]; /* stored secret     */
4765
    word16             haveEMS;           /* ext master secret flag   */
4766
#if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
4767
    WOLFSSL_X509*      peer;              /* peer cert */
4768
#endif
4769
    ProtocolVersion    version;           /* which version was used   */
4770
#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
4771
                        (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
4772
    byte               cipherSuite0;      /* first byte, normally 0   */
4773
    byte               cipherSuite;       /* 2nd byte, actual suite   */
4774
#endif
4775
#ifndef NO_CLIENT_CACHE
4776
    word16             idLen;             /* serverID length          */
4777
    byte               serverID[SERVER_ID_LEN]; /* for easier client lookup */
4778
#endif
4779
#ifdef WOLFSSL_SESSION_ID_CTX
4780
    byte               sessionCtxSz;      /* sessionCtx length        */
4781
    byte               sessionCtx[ID_LEN]; /* app specific context id */
4782
#endif /* WOLFSSL_SESSION_ID_CTX */
4783
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
4784
    byte               peerVerifyRet;     /* cert verify error */
4785
#endif
4786
#ifdef WOLFSSL_TLS13
4787
    word16             namedGroup;
4788
#endif
4789
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
4790
#ifdef WOLFSSL_TLS13
4791
#ifdef WOLFSSL_32BIT_MILLI_TIME
4792
    word32             ticketSeen;        /* Time ticket seen (ms) */
4793
#else
4794
    sword64            ticketSeen;        /* Time ticket seen (ms) */
4795
#endif
4796
    word32             ticketAdd;         /* Added by client */
4797
    TicketNonce        ticketNonce;       /* Nonce used to derive PSK */
4798
#endif
4799
#ifdef WOLFSSL_EARLY_DATA
4800
    word32             maxEarlyDataSz;
4801
#endif
4802
#endif
4803
#ifdef HAVE_SESSION_TICKET
4804
    byte               staticTicket[SESSION_TICKET_LEN];
4805
    byte*              ticket;
4806
    word16             ticketLen;
4807
    word16             ticketLenAlloc;    /* is dynamic */
4808
#ifdef HAVE_SNI
4809
    byte               sniHash[TICKET_BINDING_HASH_SZ];  /* SNI at issue */
4810
#endif
4811
#ifdef HAVE_ALPN
4812
    byte               alpnHash[TICKET_BINDING_HASH_SZ]; /* ALPN at issue */
4813
#endif
4814
#endif
4815
4816
#ifdef SESSION_CERTS
4817
    WOLFSSL_X509_CHAIN chain;             /* peer cert chain, static  */
4818
    #ifdef WOLFSSL_ALT_CERT_CHAINS
4819
    WOLFSSL_X509_CHAIN altChain;          /* peer alt cert chain, static */
4820
    #endif
4821
#endif
4822
#ifdef HAVE_EX_DATA
4823
    WOLFSSL_CRYPTO_EX_DATA ex_data;
4824
#endif
4825
#ifdef HAVE_MAX_FRAGMENT
4826
    byte               mfl; /* max fragment length negotiated i.e.
4827
                             * WOLFSSL_MFL_2_8  (6) */
4828
#endif
4829
    byte               isSetup:1;
4830
};
4831
4832
#if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) &&                  \
4833
        defined(WOLFSSL_TICKET_NONCE_MALLOC) &&                                \
4834
    (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
4835
WOLFSSL_LOCAL int SessionTicketNoncePopulate(WOLFSSL_SESSION *session,
4836
    const byte* nonce, byte len);
4837
#endif /* WOLFSSL_TLS13 &&  */
4838
4839
WOLFSSL_LOCAL int wolfSSL_RAND_Init(void);
4840
4841
WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_NewSession(void* heap);
4842
WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_GetSession(
4843
    WOLFSSL* ssl, byte* masterSecret, byte restoreSessionCerts);
4844
WOLFSSL_LOCAL void SetupSession(WOLFSSL* ssl);
4845
WOLFSSL_LOCAL void AddSession(WOLFSSL* ssl);
4846
#ifdef WOLFSSL_API_PREFIX_MAP
4847
    #define AddSessionToCache wolfSSL_AddSessionToCache
4848
#endif
4849
WOLFSSL_TEST_VIS int AddSessionToCache(WOLFSSL_CTX* ctx,
4850
    WOLFSSL_SESSION* addSession, const byte* id, byte idSz, int* sessionIndex,
4851
    int side, word16 useTicket, ClientSession** clientCacheEntry);
4852
#ifndef NO_CLIENT_CACHE
4853
WOLFSSL_LOCAL ClientSession* AddSessionToClientCache(int side, int row, int idx,
4854
                      byte* serverID, word16 idLen, const byte* sessionID,
4855
                      word16 useTicket);
4856
#endif
4857
WOLFSSL_LOCAL
4858
WOLFSSL_SESSION* ClientSessionToSession(const WOLFSSL_SESSION* session);
4859
WOLFSSL_LOCAL void TlsSessionCacheUnlockRow(word32 row);
4860
WOLFSSL_LOCAL int TlsSessionCacheGetAndRdLock(const byte *id,
4861
    const WOLFSSL_SESSION **sess, word32 *lockedRow, byte side);
4862
WOLFSSL_LOCAL int TlsSessionCacheGetAndWrLock(const byte *id,
4863
    WOLFSSL_SESSION **sess, word32 *lockedRow, byte side);
4864
WOLFSSL_LOCAL void EvictSessionFromCache(WOLFSSL_SESSION* session);
4865
WOLFSSL_TEST_VIS int wolfSSL_GetSessionFromCache(WOLFSSL* ssl, WOLFSSL_SESSION* output);
4866
WOLFSSL_LOCAL int wolfSSL_SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session);
4867
WOLFSSL_LOCAL void wolfSSL_FreeSession(WOLFSSL_CTX* ctx,
4868
        WOLFSSL_SESSION* session);
4869
WOLFSSL_LOCAL int wolfSSL_DupSession(const WOLFSSL_SESSION* input,
4870
        WOLFSSL_SESSION* output, int avoidSysCalls);
4871
4872
4873
typedef int (*hmacfp) (WOLFSSL*, byte*, const byte*, word32, int, int, int, int);
4874
4875
#ifndef NO_CLIENT_CACHE
4876
    WOLFSSL_LOCAL WOLFSSL_SESSION* wolfSSL_GetSessionClient(
4877
        WOLFSSL* ssl, const byte* id, int len);
4878
#endif
4879
4880
/* client connect state for nonblocking restart */
4881
enum ConnectState {
4882
    CONNECT_BEGIN = 0,
4883
    CLIENT_HELLO_SENT,
4884
    HELLO_AGAIN,               /* HELLO_AGAIN s for DTLS case */
4885
    HELLO_AGAIN_REPLY,
4886
    FIRST_REPLY_DONE,
4887
    FIRST_REPLY_FIRST,
4888
    FIRST_REPLY_SECOND,
4889
    FIRST_REPLY_THIRD,
4890
    FIRST_REPLY_FOURTH,
4891
    FINISHED_DONE,
4892
    SECOND_REPLY_DONE,
4893
4894
#ifdef WOLFSSL_DTLS13
4895
    WAIT_FINISHED_ACK
4896
#endif /* WOLFSSL_DTLS13 */
4897
4898
};
4899
4900
4901
/* server accept state for nonblocking restart */
4902
enum AcceptState {
4903
    ACCEPT_BEGIN = 0,
4904
    ACCEPT_BEGIN_RENEG,
4905
    ACCEPT_CLIENT_HELLO_DONE,
4906
    ACCEPT_HELLO_RETRY_REQUEST_DONE,
4907
    ACCEPT_FIRST_REPLY_DONE,
4908
    SERVER_HELLO_SENT,
4909
    CERT_SENT,
4910
    CERT_VERIFY_SENT,
4911
    CERT_STATUS_SENT,
4912
    KEY_EXCHANGE_SENT,
4913
    CERT_REQ_SENT,
4914
    SERVER_HELLO_DONE,
4915
    ACCEPT_SECOND_REPLY_DONE,
4916
    TICKET_SENT,
4917
    CHANGE_CIPHER_SENT,
4918
    ACCEPT_FINISHED_DONE,
4919
    ACCEPT_THIRD_REPLY_DONE
4920
};
4921
4922
/* TLS 1.3 server accept state for nonblocking restart */
4923
enum AcceptStateTls13 {
4924
    TLS13_ACCEPT_BEGIN = 0,
4925
    TLS13_ACCEPT_BEGIN_RENEG,
4926
    TLS13_ACCEPT_CLIENT_HELLO_DONE,
4927
    TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE,
4928
    TLS13_ACCEPT_FIRST_REPLY_DONE,
4929
    TLS13_ACCEPT_SECOND_REPLY_DONE,
4930
    TLS13_SERVER_HELLO_SENT,
4931
    TLS13_ACCEPT_THIRD_REPLY_DONE,
4932
    TLS13_SERVER_EXTENSIONS_SENT,
4933
    TLS13_CERT_REQ_SENT,
4934
    TLS13_CERT_SENT,
4935
    TLS13_CERT_VERIFY_SENT,
4936
    TLS13_ACCEPT_FINISHED_SENT,
4937
    TLS13_PRE_TICKET_SENT,
4938
    TLS13_ACCEPT_FINISHED_DONE,
4939
    TLS13_TICKET_SENT
4940
};
4941
4942
#ifdef WOLFSSL_THREADED_CRYPT
4943
4944
#include <pthread.h>
4945
4946
typedef struct ThreadCrypt {
4947
    Ciphers encrypt;
4948
    bufferStatic buffer;
4949
    unsigned char nonce[AESGCM_NONCE_SZ];
4950
    unsigned char additional[AEAD_AUTH_DATA_SZ];
4951
    int init;
4952
    int offset;
4953
    int cryptLen;
4954
    int done;
4955
    int avail;
4956
    int stop;
4957
    WOLFSSL_THREAD_SIGNAL signal;
4958
    void*                 signalCtx;
4959
} ThreadCrypt;
4960
4961
#endif
4962
4963
/* buffers for struct WOLFSSL */
4964
typedef struct Buffers {
4965
    bufferStatic    inputBuffer;
4966
    bufferStatic    outputBuffer;
4967
#ifdef WOLFSSL_THREADED_CRYPT
4968
    ThreadCrypt     encrypt[WOLFSSL_THREADED_CRYPT_CNT];
4969
#endif
4970
    buffer          domainName;            /* for client check */
4971
    buffer          ipasc;                 /* for client IP SAN check */
4972
    buffer          clearOutputBuffer;
4973
    buffer          sig;                   /* signature data */
4974
    buffer          digest;                /* digest data */
4975
    word32          prevSent;              /* previous plain text bytes sent
4976
                                              when got WANT_WRITE            */
4977
    word32          plainSz;               /* plain text bytes in buffer to send
4978
                                              when got WANT_WRITE            */
4979
    byte            weOwnCert;             /* SSL own cert flag */
4980
    byte            weOwnCertChain;        /* SSL own cert chain flag */
4981
    byte            weOwnKey;              /* SSL own key flag */
4982
#ifdef WOLFSSL_DUAL_ALG_CERTS
4983
    byte            weOwnAltKey;           /* SSL own alt key flag */
4984
#endif
4985
    byte            weOwnDH;               /* SSL own dh (p,g)  flag */
4986
#ifndef NO_DH
4987
    buffer          serverDH_P;            /* WOLFSSL_CTX owns, unless we own */
4988
    buffer          serverDH_G;            /* WOLFSSL_CTX owns, unless we own */
4989
    buffer          serverDH_Pub;
4990
    buffer          serverDH_Priv;
4991
    DhKey*          serverDH_Key;
4992
#endif
4993
#ifndef NO_CERTS
4994
    DerBuffer*      certificate;           /* WOLFSSL_CTX owns, unless we own */
4995
    DerBuffer*      key;                   /* WOLFSSL_CTX owns, unless we own */
4996
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
4997
    DerBuffer*      keyMask;               /* Mask of private key DER. */
4998
#endif
4999
    byte            keyType;               /* Type of key */
5000
    byte            keyId:1;               /* Key data is an id not data */
5001
    byte            keyLabel:1;            /* Key data is a label not data */
5002
    int             keySz;                 /* Size of RSA key */
5003
    int             keyDevId;              /* Device Id for key */
5004
#ifdef WOLFSSL_DUAL_ALG_CERTS
5005
    DerBuffer*      altKey;                /* WOLFSSL_CTX owns, unless we own */
5006
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
5007
    DerBuffer*      altKeyMask;            /* Mask of alt private key DER. */
5008
#endif
5009
    byte            altKeyType;            /* Type of alt key */
5010
    byte            altKeyId:1;            /* Key data is an id not data */
5011
    byte            altKeyLabel:1;         /* Key data is a label not data */
5012
    int             altKeySz;              /* Size of alt key */
5013
    int             altKeyDevId;           /* Device Id for alt key */
5014
#endif
5015
    DerBuffer*      certChain;             /* WOLFSSL_CTX owns, unless we own */
5016
                 /* chain after self, in DER, with leading size for each cert */
5017
    int             certChainCnt;
5018
#ifdef WOLFSSL_TLS13
5019
    DerBuffer*      certExts[MAX_CERT_EXTENSIONS];
5020
#endif
5021
#endif
5022
#ifdef WOLFSSL_SEND_HRR_COOKIE
5023
    buffer          tls13CookieSecret;     /* HRR cookie secret */
5024
#endif
5025
#ifdef WOLFSSL_DTLS
5026
    WOLFSSL_DTLS_CTX dtlsCtx;              /* DTLS connection context */
5027
    #ifndef NO_WOLFSSL_SERVER
5028
        buffer       dtlsCookieSecret;     /* DTLS cookie secret */
5029
    #endif /* NO_WOLFSSL_SERVER */
5030
#endif
5031
#ifdef HAVE_PK_CALLBACKS
5032
    #ifdef HAVE_ECC
5033
        buffer peerEccDsaKey;              /* we own for Ecc Verify Callbacks */
5034
    #endif /* HAVE_ECC */
5035
    #ifdef HAVE_ED25519
5036
        buffer peerEd25519Key;             /* for Ed25519 Verify Callbacks */
5037
    #endif /* HAVE_ED25519 */
5038
    #ifdef HAVE_ED448
5039
        buffer peerEd448Key;             /* for Ed448 Verify Callbacks */
5040
    #endif /* HAVE_ED448 */
5041
    #ifndef NO_RSA
5042
        buffer peerRsaKey;                 /* we own for Rsa Verify Callbacks */
5043
    #endif /* NO_RSA */
5044
#endif /* HAVE_PK_CALLBACKS */
5045
} Buffers;
5046
5047
/* sub-states for send/do key share (key exchange) */
5048
enum asyncState {
5049
    TLS_ASYNC_BEGIN = 0,
5050
    TLS_ASYNC_BUILD,
5051
    TLS_ASYNC_DO,
5052
    TLS_ASYNC_VERIFY,
5053
    TLS_ASYNC_FINALIZE,
5054
    TLS_ASYNC_END
5055
};
5056
5057
/* sub-states for build message */
5058
enum buildMsgState {
5059
    BUILD_MSG_BEGIN = 0,
5060
    BUILD_MSG_SIZE,
5061
    BUILD_MSG_HASH,
5062
    BUILD_MSG_VERIFY_MAC,
5063
    BUILD_MSG_ENCRYPT,
5064
    BUILD_MSG_ENCRYPTED_VERIFY_MAC,
5065
};
5066
5067
/* sub-states for cipher operations */
5068
enum cipherState {
5069
    CIPHER_STATE_BEGIN = 0,
5070
    CIPHER_STATE_DO,
5071
    CIPHER_STATE_END,
5072
};
5073
5074
struct Options {
5075
#ifndef NO_PSK
5076
    wc_psk_client_callback client_psk_cb;
5077
    wc_psk_server_callback server_psk_cb;
5078
#ifdef OPENSSL_EXTRA
5079
    wc_psk_use_session_cb_func session_psk_cb;
5080
#endif
5081
#ifdef WOLFSSL_TLS13
5082
    wc_psk_client_cs_callback    client_psk_cs_cb;     /* client callback */
5083
    wc_psk_client_tls13_callback client_psk_tls13_cb;  /* client callback */
5084
    wc_psk_server_tls13_callback server_psk_tls13_cb;  /* server callback */
5085
#endif
5086
    void*             psk_ctx;
5087
#endif /* NO_PSK */
5088
    unsigned long     mask; /* store SSL_OP_ flags */
5089
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL)
5090
    word16            minProto:1; /* sets min to min available */
5091
    word16            maxProto:1; /* sets max to max available */
5092
#endif
5093
#if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TLS13)
5094
    unsigned int      maxTicketTls13;  /* maximum number of tickets to send */
5095
    unsigned int      ticketsSent;     /* keep track of the total sent */
5096
#endif
5097
5098
    /* on/off or small bit flags, optimize layout */
5099
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
5100
    word16            havePSK:1;            /* psk key set by user */
5101
#endif /* HAVE_SESSION_TICKET || !NO_PSK */
5102
    word16            sendVerify:2;     /* false = 0, true = 1, sendBlank = 2 */
5103
    word16            sessionCacheOff:1;
5104
    word16            sessionCacheFlushOff:1;
5105
#ifdef HAVE_EXT_CACHE
5106
    word16            internalCacheOff:1;
5107
    word16            internalCacheLookupOff:1;
5108
#endif
5109
    word16            side:2;             /* client, server or neither end */
5110
    word16            verifyPeer:1;
5111
    word16            verifyNone:1;
5112
    word16            failNoCert:1;
5113
    word16            failNoCertxPSK:1;   /* fail for no cert except with PSK */
5114
    word16            downgrade:1;        /* allow downgrade of versions */
5115
    word16            resuming:1;
5116
#ifdef HAVE_SECURE_RENEGOTIATION
5117
    word16            resumed:1;          /* resuming may be reset on SCR */
5118
#endif
5119
    word16            isPSK:1;
5120
    word16            haveSessionId:1;    /* server may not send */
5121
    word16            tls:1;              /* using TLS ? */
5122
    word16            tls1_1:1;           /* using TLSv1.1+ ? */
5123
    word16            tls1_3:1;           /* using TLSv1.3+ ? */
5124
    word16            dtls:1;             /* using datagrams ? */
5125
#ifdef WOLFSSL_DTLS
5126
    word16            dtlsStateful:1;     /* allow stateful processing ? */
5127
#endif
5128
    word16            connReset:1;        /* has the peer reset */
5129
    word16            isClosed:1;         /* if we consider conn closed */
5130
    word16            closeNotify:1;      /* we've received a close notify */
5131
    word16            sentNotify:1;       /* we've sent a close notify */
5132
    word16            usingCompression:1; /* are we using compression */
5133
    word16            haveRSA:1;          /* RSA available */
5134
    word16            haveECC:1;          /* ECC available */
5135
    word16            haveDH:1;           /* server DH params set by user */
5136
    word16            haveECDSAsig:1;     /* server ECDSA signed cert */
5137
    word16            haveStaticECC:1;    /* static server ECC private key */
5138
    word16            haveFalconSig:1;    /* server Falcon signed cert */
5139
    word16            haveDilithiumSig:1; /* server Dilithium signed cert */
5140
    word16            havePeerCert:1;     /* do we have peer's cert */
5141
    word16            havePeerVerify:1;   /* and peer's cert verify */
5142
    word16            usingPSK_cipher:1;  /* are using psk as cipher */
5143
    word16            usingAnon_cipher:1; /* are we using an anon cipher */
5144
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
5145
    word16            noPskDheKe:1;       /* Don't use (EC)DHE with PSK */
5146
#ifdef HAVE_SUPPORTED_CURVES
5147
    word16            onlyPskDheKe:1;     /* Only use (EC)DHE with PSK */
5148
#endif
5149
#if defined(WOLFSSL_CERT_WITH_EXTERN_PSK)
5150
    word16            certWithExternPsk:1; /* Cert auth with external PSK */
5151
#endif
5152
#endif
5153
    word16            partialWrite:1;     /* only one msg per write call */
5154
    word16            quietShutdown:1;    /* don't send close notify */
5155
    word16            certOnly:1;         /* stop once we get cert */
5156
    word16            groupMessages:1;    /* group handshake messages */
5157
    word16            saveArrays:1;       /* save array Memory for user get keys
5158
                                           or psk */
5159
    word16            weOwnRng:1;         /* will be true unless CTX owns */
5160
    word16            dontFreeDigest:1;   /* when true, we used SetDigest */
5161
    word16            haveEMS:1;          /* using extended master secret */
5162
#ifdef HAVE_POLY1305
5163
    word16            oldPoly:1;        /* set when to use old rfc way of poly*/
5164
#endif
5165
    word16            useAnon:1;       /* User wants to allow Anon suites */
5166
#ifdef HAVE_SESSION_TICKET
5167
    word16            createTicket:1;     /* Server to create new Ticket */
5168
    word16            useTicket:1;        /* Use Ticket not session cache */
5169
    word16            rejectTicket:1;     /* Callback rejected ticket */
5170
    word16            noTicketTls12:1;    /* TLS 1.2 server won't send ticket */
5171
#ifdef WOLFSSL_TLS13
5172
    word16            noTicketTls13:1;    /* Server won't create new Ticket */
5173
#endif
5174
#endif
5175
#ifdef WOLFSSL_DTLS
5176
#ifdef HAVE_SECURE_RENEGOTIATION
5177
    word16            dtlsDoSCR:1;        /* Enough packets were dropped. We
5178
                                           * need to re-key. */
5179
#endif
5180
    word16            dtlsUseNonblock:1;  /* are we using nonblocking socket */
5181
    word16            dtlsHsRetain:1;     /* DTLS retaining HS data */
5182
#ifdef WOLFSSL_SCTP
5183
    word16            dtlsSctp:1;         /* DTLS-over-SCTP mode */
5184
#endif
5185
#endif /* WOLFSSL_DTLS */
5186
#if defined(HAVE_TLS_EXTENSIONS) && defined(HAVE_SUPPORTED_CURVES)
5187
    word16            userCurves:1;       /* indicates user called wolfSSL_UseSupportedCurve */
5188
#endif
5189
    word16            keepResources:1;    /* Keep resources after handshake */
5190
    word16            useClientOrder:1;   /* Use client's cipher order */
5191
    word16            mutualAuth:1;       /* Mutual authentication is required */
5192
    word16            peerAuthGood:1;     /* Any required peer auth done */
5193
#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
5194
    word16            pskNegotiated:1;    /* Session Ticket/PSK negotiated. */
5195
#endif
5196
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
5197
    word16            postHandshakeAuth:1;/* Client send post_handshake_auth
5198
                                           * extension */
5199
    word16            verifyPostHandshake:1; /* Only send client cert req post
5200
                                              * handshake, not also during */
5201
#endif
5202
#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
5203
    word16            sendCookie:1;       /* Server creates a Cookie in HRR */
5204
#endif
5205
#ifdef WOLFSSL_ALT_CERT_CHAINS
5206
    word16            usingAltCertChain:1;/* Alternate cert chain was used */
5207
#endif
5208
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
5209
    word16            sentChangeCipher:1; /* Change Cipher Spec sent */
5210
#endif
5211
#if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
5212
               ((defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)) || \
5213
                (defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
5214
                (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
5215
    word16            cacheMessages:1;    /* Cache messages for sign/verify */
5216
#endif
5217
#ifndef NO_DH
5218
    #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
5219
        !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
5220
        word16        dhDoKeyTest:1;      /* Need to do the DH Key prime test */
5221
        word16        dhKeyTested:1;      /* Set when key has been tested. */
5222
    #endif
5223
#endif
5224
#ifdef HAVE_ENCRYPT_THEN_MAC
5225
    word16            disallowEncThenMac:1;   /* Don't do Encrypt-Then-MAC */
5226
    word16            encThenMac:1;           /* Doing Encrypt-Then-MAC */
5227
    word16            startedETMRead:1;       /* Doing Encrypt-Then-MAC read */
5228
    word16            startedETMWrite:1;      /* Doing Encrypt-Then-MAC write */
5229
#endif
5230
#ifdef WOLFSSL_ASYNC_CRYPT
5231
    word16            buildArgsSet:1;         /* buildArgs are set and need to
5232
                                               * be free'd */
5233
#endif
5234
#ifdef WOLFSSL_DTLS13
5235
    word16            dtls13SendMoreAcks:1;  /* Send more acks during the
5236
                                              * handshake process */
5237
#ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
5238
    word16            dtls13NoHrrOnResume:1;
5239
#endif
5240
#ifdef WOLFSSL_DTLS_CH_FRAG
5241
    word16            dtls13ChFrag:1;
5242
#endif
5243
#endif
5244
#ifdef WOLFSSL_TLS13
5245
    word16            tls13MiddleBoxCompat:1; /* TLSv1.3 middlebox compatibility */
5246
#endif
5247
#ifdef WOLFSSL_DTLS_CID
5248
    word16            useDtlsCID:1;
5249
#endif /* WOLFSSL_DTLS_CID */
5250
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
5251
    word16            echAccepted:1;
5252
    word16            disableECH:1;             /* Did the user disable ech */
5253
    word16            echProcessingInner:1;     /* Processing the inner hello */
5254
    word16            echRetryConfigsAccepted:1;
5255
#endif
5256
#ifdef WOLFSSL_SEND_HRR_COOKIE
5257
    word16            cookieGood:1;
5258
#endif
5259
#if defined(HAVE_DANE)
5260
    word16            useDANE:1;
5261
#endif /* HAVE_DANE */
5262
#ifdef WOLFSSL_TLS13
5263
#ifdef WOLFSSL_SEND_HRR_COOKIE
5264
    word16            hrrSentCookie:1;    /* HRR sent with cookie */
5265
#endif
5266
    word16            hrrSentKeyShare:1;  /* HRR sent with key share */
5267
    word16            shSentKeyShare:1;   /* SH sent with key share */
5268
#endif
5269
    word16            returnOnGoodCh:1;
5270
    word16            disableRead:1;
5271
5272
#ifdef WOLFSSL_EARLY_DATA
5273
    word16            clientInEarlyData:1; /* Client is in wolfSSL_read_early_data */
5274
#endif
5275
#ifdef WOLFSSL_DTLS
5276
    byte              haveMcast;          /* using multicast ? */
5277
#endif
5278
    byte              buildingMsg;        /* If set then we need to re-enter the
5279
                                           * handshake logic. */
5280
    byte              seenUnifiedHdr;     /* received msg with unified header */
5281
    byte              shutdownDone;       /* we've completed a shutdown */
5282
    byte              sendKeyUpdate;      /* Key Update to write */
5283
#if defined(HAVE_RPK)
5284
    RpkConfig         rpkConfig;
5285
    RpkState          rpkState;
5286
#endif /* HAVE_RPK */
5287
5288
    /* need full byte values for this section */
5289
    byte            processReply;           /* nonblocking resume */
5290
    byte            cipherSuite0;           /* first byte, normally 0 */
5291
    byte            cipherSuite;            /* second byte, actual suite */
5292
#ifdef WOLFSSL_TLS13
5293
    byte            hrrCipherSuite0;        /* first byte, normally 0 */
5294
    byte            hrrCipherSuite;         /* second byte, actual suite */
5295
#endif
5296
    byte            hashAlgo;               /* selected hash algorithm */
5297
    byte            sigAlgo;                /* selected sig algorithm */
5298
    byte            peerHashAlgo;           /* peer's chosen hash algo */
5299
    byte            peerSigAlgo;            /* peer's chosen sig algo */
5300
    byte            serverState;
5301
    byte            clientState;
5302
    byte            handShakeState;
5303
    byte            handShakeDone;      /* at least one handshake complete */
5304
    byte            minDowngrade;       /* minimum downgrade version */
5305
    byte            connectState;       /* nonblocking resume */
5306
    byte            acceptState;        /* nonblocking resume */
5307
    byte            asyncState;         /* sub-state for enum asyncState */
5308
    byte            buildMsgState;      /* sub-state for enum buildMsgState */
5309
    byte            alertCount;         /* detect warning dos attempt */
5310
    byte            emptyRecordCount;   /* detect empty record dos attempt */
5311
#ifdef WOLFSSL_MULTICAST
5312
    word16          mcastID;            /* Multicast group ID */
5313
#endif
5314
#ifndef NO_DH
5315
    word16          minDhKeySz;         /* minimum DH key size */
5316
    word16          maxDhKeySz;         /* minimum DH key size */
5317
    word16          dhKeySz;            /* actual DH key size */
5318
#endif
5319
#ifndef NO_RSA
5320
    short           minRsaKeySz;      /* minimum RSA key size */
5321
#endif
5322
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
5323
    short           minEccKeySz;      /* minimum ECC key size */
5324
#endif
5325
#if defined(HAVE_FALCON)
5326
    short           minFalconKeySz;   /* minimum Falcon key size */
5327
#endif
5328
#if defined(HAVE_DILITHIUM)
5329
    short           minDilithiumKeySz;/* minimum Dilithium key size */
5330
#endif
5331
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
5332
    byte            verifyDepth;      /* maximum verification depth */
5333
#endif
5334
#ifdef WOLFSSL_EARLY_DATA
5335
    word16          pskIdIndex;
5336
    word32          maxEarlyDataSz;
5337
#endif
5338
#ifdef WOLFSSL_TLS13
5339
    byte            oldMinor;          /* client preferred version < TLS 1.3 */
5340
#endif
5341
};
5342
5343
typedef struct Arrays {
5344
    byte*           pendingMsg;         /* defrag buffer */
5345
    byte*           preMasterSecret;
5346
    word32          preMasterSz;        /* differs for DH, actual size */
5347
    word32          pendingMsgSz;       /* defrag buffer size */
5348
    word32          pendingMsgOffset;   /* current offset into defrag buffer */
5349
#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
5350
    word32          psk_keySz;          /* actual size */
5351
    char            client_identity[MAX_PSK_ID_LEN + NULL_TERM_LEN];
5352
    char            server_hint[MAX_PSK_ID_LEN + NULL_TERM_LEN];
5353
    byte            psk_key[MAX_PSK_KEY_LEN];
5354
#endif
5355
    byte            clientRandom[RAN_LEN];
5356
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
5357
    byte            clientRandomInner[RAN_LEN];
5358
#endif
5359
    byte            serverRandom[RAN_LEN];
5360
    byte            sessionID[ID_LEN];
5361
    byte            sessionIDSz;
5362
#ifdef WOLFSSL_TLS13
5363
    byte            secret[SECRET_LEN];
5364
#endif
5365
#ifdef HAVE_KEYING_MATERIAL
5366
    byte            exporterSecret[WC_MAX_DIGEST_SIZE];
5367
#endif
5368
    byte            masterSecret[SECRET_LEN];
5369
#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
5370
   !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
5371
    byte            tsip_masterSecret[TSIP_TLS_MASTERSECRET_SIZE];
5372
#endif
5373
#if defined(WOLFSSL_RENESAS_FSPSM_TLS)
5374
    byte            fspsm_masterSecret[FSPSM_TLS_MASTERSECRET_SIZE];
5375
#endif
5376
#ifdef WOLFSSL_DTLS
5377
    byte            cookie[MAX_COOKIE_LEN];
5378
    byte            cookieSz;
5379
#endif
5380
    byte            pendingMsgType;    /* defrag buffer message type */
5381
} Arrays;
5382
5383
#ifndef ASN_NAME_MAX
5384
    #ifndef NO_ASN
5385
        /* use value from asn.h */
5386
0
        #define ASN_NAME_MAX WC_ASN_NAME_MAX
5387
    #else
5388
        /* calculate for WOLFSSL_X509 */
5389
        #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
5390
            defined(WOLFSSL_CERT_EXT)
5391
            #define ASN_NAME_MAX 330
5392
        #else
5393
            #define ASN_NAME_MAX 256
5394
        #endif
5395
    #endif
5396
#endif
5397
5398
typedef enum {
5399
    STACK_TYPE_X509               = 0,
5400
    STACK_TYPE_GEN_NAME           = 1,
5401
    STACK_TYPE_BIO                = 2,
5402
    STACK_TYPE_OBJ                = 3,
5403
    STACK_TYPE_STRING             = 4,
5404
    STACK_TYPE_CIPHER             = 5,
5405
    STACK_TYPE_ACCESS_DESCRIPTION = 6,
5406
    STACK_TYPE_X509_EXT           = 7,
5407
    STACK_TYPE_NULL               = 8,
5408
    STACK_TYPE_X509_NAME          = 9,
5409
    STACK_TYPE_CONF_VALUE         = 10,
5410
    STACK_TYPE_X509_INFO          = 11,
5411
    STACK_TYPE_BY_DIR_entry       = 12,
5412
    STACK_TYPE_BY_DIR_hash        = 13,
5413
    STACK_TYPE_X509_OBJ           = 14,
5414
    STACK_TYPE_DIST_POINT         = 15,
5415
    STACK_TYPE_X509_CRL           = 16,
5416
    STACK_TYPE_X509_NAME_ENTRY    = 17,
5417
    STACK_TYPE_X509_REQ_ATTR      = 18,
5418
    STACK_TYPE_GENERAL_SUBTREE    = 19,
5419
    STACK_TYPE_X509_REVOKED       = 20,
5420
} WOLF_STACK_TYPE;
5421
5422
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
5423
5424
struct WOLFSSL_STACK {
5425
    unsigned long num; /* number of nodes in stack
5426
                        * (safety measure for freeing and shortcut for count) */
5427
    #if defined(OPENSSL_ALL)
5428
    wolf_sk_hash_cb hash_fn;
5429
    unsigned long hash;
5430
    #endif
5431
5432
    union {
5433
        WOLFSSL_X509*          x509;
5434
        WOLFSSL_X509_NAME*     name;
5435
        WOLFSSL_X509_NAME_ENTRY* name_entry;
5436
        WOLFSSL_X509_INFO*     info;
5437
        WOLFSSL_BIO*           bio;
5438
        WOLFSSL_ASN1_OBJECT*   obj;
5439
        WOLFSSL_CIPHER         cipher;
5440
        WOLFSSL_ACCESS_DESCRIPTION* access;
5441
        WOLFSSL_X509_EXTENSION* ext;
5442
#ifdef OPENSSL_EXTRA
5443
        WOLFSSL_CONF_VALUE*    conf;
5444
#endif
5445
        void*                  generic;
5446
        char*                  string;
5447
        WOLFSSL_GENERAL_NAME*  gn;
5448
        WOLFSSL_GENERAL_SUBTREE* subtree;
5449
        WOLFSSL_BY_DIR_entry*  dir_entry;
5450
        WOLFSSL_BY_DIR_HASH*   dir_hash;
5451
        WOLFSSL_X509_OBJECT*   x509_obj;
5452
        WOLFSSL_DIST_POINT*    dp;
5453
        WOLFSSL_X509_CRL*      crl;
5454
        WOLFSSL_X509_REVOKED*  revoked;
5455
    } data;
5456
    void* heap; /* memory heap hint */
5457
    WOLFSSL_STACK* next;
5458
    WOLF_STACK_TYPE type;     /* Identifies type of stack. */
5459
};
5460
5461
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
5462
5463
struct WOLFSSL_X509_NAME {
5464
    char  *name;
5465
    int   dynamicName;
5466
    int   sz;
5467
    char  staticName[ASN_NAME_MAX];
5468
#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
5469
    !defined(NO_ASN)
5470
    DecodedName fullName;
5471
    int   entrySz; /* number of entries */
5472
    WOLFSSL_X509_NAME_ENTRY entry[MAX_NAME_ENTRIES]; /* all entries i.e. CN */
5473
    WOLFSSL_X509*           x509;   /* x509 that struct belongs to */
5474
#endif /* OPENSSL_EXTRA */
5475
#ifndef WOLFSSL_NO_CA_NAMES
5476
    byte  raw[ASN_NAME_MAX];
5477
    int   rawLen;
5478
5479
    WOLF_STACK_OF(WOLFSSL_X509_NAME_ENTRY)* entries;
5480
#endif
5481
    void* heap;
5482
};
5483
5484
#ifndef EXTERNAL_SERIAL_SIZE
5485
    #define EXTERNAL_SERIAL_SIZE 32
5486
#endif
5487
5488
#ifdef NO_ASN
5489
    typedef struct DNS_entry DNS_entry;
5490
    #ifndef IGNORE_NAME_CONSTRAINTS
5491
        typedef struct Base_entry Base_entry;
5492
    #endif
5493
#endif
5494
5495
#ifndef WOLFSSL_AIA_ENTRY_DEFINED
5496
#ifndef WOLFSSL_MAX_AIA_ENTRIES
5497
    #define WOLFSSL_MAX_AIA_ENTRIES 8
5498
#endif
5499
5500
#define WOLFSSL_AIA_ENTRY_DEFINED
5501
typedef struct WOLFSSL_AIA_ENTRY {
5502
    word32      method; /* AIA method OID sum (e.g., AIA_OCSP_OID). */
5503
    const byte* uri;    /* Pointer into cert DER for the URI. */
5504
    word32      uriSz;  /* Length of URI data. */
5505
} WOLFSSL_AIA_ENTRY;
5506
#endif /* WOLFSSL_AIA_ENTRY_DEFINED */
5507
5508
struct WOLFSSL_X509 {
5509
    int              version;
5510
    int              serialSz;
5511
#ifdef WOLFSSL_SEP
5512
    int              deviceTypeSz;
5513
    int              hwTypeSz;
5514
    byte             deviceType[EXTERNAL_SERIAL_SIZE];
5515
    byte             hwType[EXTERNAL_SERIAL_SIZE];
5516
    int              hwSerialNumSz;
5517
    byte             hwSerialNum[EXTERNAL_SERIAL_SIZE];
5518
    byte             certPolicySet;
5519
    byte             certPolicyCrit;
5520
#endif /* WOLFSSL_SEP */
5521
#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
5522
    WOLFSSL_STACK* ext_sk; /* Store X509_EXTENSIONS from wolfSSL_X509_get_ext */
5523
    WOLFSSL_STACK* ext_sk_full; /* Store X509_EXTENSIONS from wolfSSL_X509_get0_extensions */
5524
    WOLFSSL_STACK* ext_d2i;/* Store d2i extensions from wolfSSL_X509_get_ext_d2i */
5525
#endif /* WOLFSSL_QT || OPENSSL_ALL */
5526
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
5527
    WOLFSSL_ASN1_INTEGER* serialNumber; /* Stores SN from wolfSSL_X509_get_serialNumber */
5528
#endif
5529
    WOLFSSL_ASN1_TIME notBefore;
5530
    WOLFSSL_ASN1_TIME notAfter;
5531
    buffer           sig;
5532
    int              sigOID;
5533
    DNS_entry*       altNames;                       /* alt names list */
5534
#ifndef IGNORE_NAME_CONSTRAINTS
5535
    Base_entry*      permittedNames;                 /* name constraints */
5536
    Base_entry*      excludedNames;
5537
    byte             nameConstraintCrit:1;
5538
#endif
5539
    buffer           pubKey;
5540
    int              pubKeyOID;
5541
    DNS_entry*       altNamesNext;                   /* hint for retrieval */
5542
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
5543
    defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)
5544
    word32       pkCurveOID;
5545
#endif
5546
#ifndef NO_CERTS
5547
    DerBuffer*   derCert;                            /* may need  */
5548
#endif
5549
    void*            heap;                           /* heap hint */
5550
    byte             dynamicMemory;                  /* dynamic memory flag */
5551
    byte             isCa:1;
5552
#ifdef WOLFSSL_CERT_EXT
5553
    char             certPolicies[MAX_CERTPOL_NB][MAX_CERTPOL_SZ];
5554
    int              certPoliciesNb;
5555
#endif /* WOLFSSL_CERT_EXT */
5556
#if defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_EXTRA)
5557
    wolfSSL_Ref      ref;
5558
#endif
5559
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
5560
#ifdef HAVE_EX_DATA
5561
    WOLFSSL_CRYPTO_EX_DATA ex_data;
5562
#endif
5563
    byte*            authKeyId; /* Points into authKeyIdSrc */
5564
    byte*            authKeyIdSrc;
5565
    byte*            subjKeyId;
5566
    WOLFSSL_ASN1_STRING* subjKeyIdStr;
5567
    byte*            extKeyUsageSrc;
5568
#ifdef OPENSSL_ALL
5569
    byte*            subjAltNameSrc;
5570
#endif
5571
    byte*            rawCRLInfo;
5572
    byte*            CRLInfo;
5573
    byte*            authInfo;
5574
#ifdef WOLFSSL_ASN_CA_ISSUER
5575
    byte*            authInfoCaIssuer;
5576
    int              authInfoCaIssuerSz;
5577
#endif
5578
    WOLFSSL_AIA_ENTRY authInfoList[WOLFSSL_MAX_AIA_ENTRIES];
5579
    byte             authInfoListSz:7;
5580
    byte             authInfoListOverflow:1;
5581
    word32           pathLength;
5582
    word16           keyUsage;
5583
    int              rawCRLInfoSz;
5584
    int              CRLInfoSz;
5585
    int              authInfoSz;
5586
    word32           authKeyIdSz;
5587
    word32           authKeyIdSrcSz;
5588
    word32           subjKeyIdSz;
5589
    byte             extKeyUsage;
5590
    word32           extKeyUsageSz;
5591
    word32           extKeyUsageCount;
5592
#ifndef IGNORE_NETSCAPE_CERT_TYPE
5593
    byte             nsCertType;
5594
#endif
5595
#ifdef OPENSSL_ALL
5596
    word32           subjAltNameSz;
5597
#endif
5598
5599
    byte             CRLdistSet:1;
5600
    byte             CRLdistCrit:1;
5601
    byte             authInfoSet:1;
5602
    byte             authInfoCrit:1;
5603
    byte             keyUsageSet:1;
5604
    byte             keyUsageCrit:1;
5605
    byte             extKeyUsageCrit:1;
5606
    byte             subjKeyIdSet:1;
5607
    byte             pathLengthSet:1;
5608
5609
    byte             subjKeyIdCrit:1;
5610
    byte             basicConstSet:1;
5611
    byte             basicConstCrit:1;
5612
    byte             basicConstPlSet:1;
5613
    byte             subjAltNameSet:1;
5614
    byte             subjAltNameCrit:1;
5615
    byte             authKeyIdSet:1;
5616
    byte             authKeyIdCrit:1;
5617
    byte             issuerSet:1;
5618
#ifdef WOLFSSL_CUSTOM_OID
5619
    CertExtension    custom_exts[NUM_CUSTOM_EXT];
5620
    int              customExtCount;
5621
#endif /* WOLFSSL_CUSTOM_OID */
5622
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
5623
#ifdef WOLFSSL_CERT_REQ
5624
    byte             isCSR:1;
5625
#endif
5626
    byte             serial[EXTERNAL_SERIAL_SIZE];
5627
    char             subjectCN[ASN_NAME_MAX];        /* common name short cut */
5628
#if defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_GEN)
5629
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
5630
    /* stack of CSR attributes */
5631
    WOLF_STACK_OF(WOLFSSL_X509_ATRIBUTE)* reqAttributes;
5632
#endif
5633
    #if defined(WOLFSSL_CERT_REQ)
5634
    char             challengePw[CTC_NAME_SIZE]; /* for REQ certs */
5635
    char             contentType[CTC_NAME_SIZE];
5636
    #endif
5637
#endif /* WOLFSSL_CERT_REQ || WOLFSSL_CERT_GEN */
5638
    WOLFSSL_X509_NAME issuer;
5639
    WOLFSSL_X509_NAME subject;
5640
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
5641
    defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_APACHE_HTTPD) || \
5642
    defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
5643
    WOLFSSL_X509_ALGOR algor;
5644
    WOLFSSL_X509_PUBKEY key;
5645
#endif
5646
#if defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_EXTRA) || \
5647
    defined(OPENSSL_ALL) || defined(KEEP_OUR_CERT) || \
5648
    defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
5649
    byte            notBeforeData[CTC_DATE_SIZE];
5650
    byte            notAfterData[CTC_DATE_SIZE];
5651
#endif
5652
#ifdef WOLFSSL_DUAL_ALG_CERTS
5653
    /* Subject Alternative Public Key Info */
5654
    byte *sapkiDer;
5655
    int sapkiLen;
5656
    /* Alternative Signature Algorithm */
5657
    byte *altSigAlgDer;
5658
    int altSigAlgLen;
5659
    /* Alternative Signature Value */
5660
    byte *altSigValDer;
5661
    int altSigValLen;
5662
5663
    byte sapkiCrit:1;
5664
    byte altSigAlgCrit:1;
5665
    byte altSigValCrit:1;
5666
#endif /* WOLFSSL_DUAL_ALG_CERTS */
5667
};
5668
5669
#if defined(WOLFSSL_ACERT)
5670
struct WOLFSSL_X509_ACERT {
5671
    int               version;
5672
    int               serialSz;
5673
    byte              serial[EXTERNAL_SERIAL_SIZE];
5674
    WOLFSSL_ASN1_TIME notBefore;
5675
    WOLFSSL_ASN1_TIME notAfter;
5676
    buffer            sig;
5677
    int               sigOID;
5678
#ifndef NO_CERTS
5679
    DerBuffer *       derCert;
5680
#endif
5681
    void *            heap;
5682
    int               dynamic; /* whether struct was dynamically allocated */
5683
    /* copy of raw Attributes field from */
5684
    byte              holderSerial[EXTERNAL_SERIAL_SIZE];
5685
    int               holderSerialSz;
5686
    DNS_entry *       holderEntityName;  /* Holder entityName from ACERT */
5687
    DNS_entry *       holderIssuerName;  /* issuerName from ACERT */
5688
    DNS_entry *       AttCertIssuerName; /* AttCertIssuer name from ACERT */
5689
    byte *            rawAttr;
5690
    word32            rawAttrLen;
5691
};
5692
#endif /* WOLFSSL_ACERT */
5693
5694
/* record layer header for PlainText, Compressed, and CipherText */
5695
typedef struct RecordLayerHeader {
5696
    byte            type;
5697
    byte            pvMajor;
5698
    byte            pvMinor;
5699
    byte            length[2];
5700
} RecordLayerHeader;
5701
5702
5703
/* record layer header for DTLS PlainText, Compressed, and CipherText */
5704
typedef struct DtlsRecordLayerHeader {
5705
    byte            type;
5706
    byte            pvMajor;
5707
    byte            pvMinor;
5708
    byte            sequence_number[8];   /* per record */
5709
    byte            length[2];
5710
} DtlsRecordLayerHeader;
5711
5712
typedef struct DtlsFragBucket {
5713
    /* m stands for meta */
5714
    union {
5715
        struct {
5716
            struct DtlsFragBucket* next;
5717
            word32 offset;
5718
            word32 sz;
5719
        } m;
5720
        /* Make sure we have at least DTLS_HANDSHAKE_HEADER_SZ bytes before the
5721
         * buf so that we can reconstruct the header in the allocated
5722
         * DtlsFragBucket buffer. */
5723
        byte padding[DTLS_HANDSHAKE_HEADER_SZ];
5724
    } m;
5725
/* Ignore "nonstandard extension used : zero-sized array in struct/union"
5726
 * MSVC warning */
5727
#ifdef _MSC_VER
5728
#pragma warning(disable: 4200)
5729
#endif
5730
    byte buf[];
5731
} DtlsFragBucket;
5732
5733
typedef struct DtlsMsg {
5734
    struct DtlsMsg* next;
5735
    byte*           raw;
5736
    byte*           fullMsg;   /* for TX fullMsg == raw. For RX this points to
5737
                                * the start of the message after headers. */
5738
    DtlsFragBucket* fragBucketList;
5739
    word32          bytesReceived;
5740
    word16          epoch;     /* Epoch that this message belongs to */
5741
    word32          seq;       /* Handshake sequence number    */
5742
    word32          sz;        /* Length of whole message      */
5743
    byte            type;
5744
    byte            fragBucketListCount;
5745
    byte            ready:1;
5746
    byte            encrypted:1;
5747
} DtlsMsg;
5748
5749
5750
#ifdef HAVE_NETX
5751
5752
    /* NETX I/O Callback default */
5753
    typedef struct NetX_Ctx {
5754
        NX_TCP_SOCKET* nxSocket;    /* send/recv socket handle */
5755
        NX_PACKET*     nxPacket;    /* incoming packet handle for short reads */
5756
        ULONG          nxOffset;    /* offset already read from nxPacket */
5757
        ULONG          nxWait;      /* wait option flag */
5758
    } NetX_Ctx;
5759
5760
#endif
5761
5762
/* Handshake messages received from peer (plus change cipher */
5763
typedef struct MsgsReceived {
5764
    word16 got_hello_request:1;
5765
    word16 got_client_hello:2;
5766
    word16 got_server_hello:1;
5767
    word16 got_hello_verify_request:1;
5768
    word16 got_session_ticket:1;
5769
    word16 got_end_of_early_data:1;
5770
    word16 got_hello_retry_request:1;
5771
    word16 got_encrypted_extensions:1;
5772
    word16 got_certificate:1;
5773
    word16 got_certificate_status:1;
5774
    word16 got_server_key_exchange:1;
5775
    word16 got_certificate_request:1;
5776
    word16 got_server_hello_done:1;
5777
    word16 got_certificate_verify:1;
5778
    word16 got_client_key_exchange:1;
5779
    word16 got_finished:1;
5780
    word16 got_key_update:1;
5781
    word16 got_change_cipher:1;
5782
} MsgsReceived;
5783
5784
5785
/* Handshake hashes */
5786
typedef struct HS_Hashes {
5787
    Hashes          verifyHashes;
5788
    Hashes          certHashes;         /* for cert verify */
5789
#if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
5790
                          defined(WOLFSSL_ALLOW_TLS_SHA1))
5791
    wc_Sha          hashSha;            /* sha hash of handshake msgs */
5792
#endif
5793
#if !defined(NO_MD5) && !defined(NO_OLD_TLS)
5794
    wc_Md5          hashMd5;            /* md5 hash of handshake msgs */
5795
#endif
5796
#ifndef NO_SHA256
5797
    wc_Sha256       hashSha256;         /* sha256 hash of handshake msgs */
5798
#endif
5799
#ifdef WOLFSSL_SHA384
5800
    wc_Sha384       hashSha384;         /* sha384 hash of handshake msgs */
5801
#endif
5802
#ifdef WOLFSSL_SHA512
5803
    wc_Sha512       hashSha512;         /* sha512 hash of handshake msgs */
5804
#endif
5805
#ifdef WOLFSSL_SM3
5806
    wc_Sm3          hashSm3;            /* sm3 hash of handshake msgs */
5807
#endif
5808
#if (defined(HAVE_ED25519) || defined(HAVE_ED448) || \
5809
     (defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3))) && \
5810
    !defined(WOLFSSL_NO_CLIENT_AUTH)
5811
    byte*           messages;           /* handshake messages */
5812
    int             length;             /* length of handshake messages' data */
5813
    int             prevLen;            /* length of messages but last */
5814
#endif
5815
} HS_Hashes;
5816
5817
5818
#ifndef WOLFSSL_NO_TLS12
5819
/* Persistable BuildMessage arguments */
5820
typedef struct BuildMsgArgs {
5821
    word32 digestSz;
5822
    word32 sz;
5823
    word32 pad;
5824
    word32 idx;
5825
    word32 headerSz;
5826
    word16 size;
5827
    word32 ivSz;      /* TLSv1.1  IV */
5828
    byte   type;
5829
    byte*  iv;
5830
    ALIGN16 byte staticIvBuffer[MAX_IV_SZ];
5831
} BuildMsgArgs;
5832
#endif
5833
5834
#ifdef WOLFSSL_ASYNC_IO
5835
    #define MAX_ASYNC_ARGS 24
5836
    typedef void (*FreeArgsCb)(struct WOLFSSL* ssl, void* pArgs);
5837
5838
    struct WOLFSSL_ASYNC {
5839
#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WOLFSSL_NO_TLS12)
5840
        BuildMsgArgs  buildArgs; /* holder for current BuildMessage args */
5841
#endif
5842
        FreeArgsCb    freeArgs; /* function pointer to cleanup args */
5843
#ifdef WC_NO_PTR_INT_CAST
5844
        max_align_t args[MAX_ASYNC_ARGS * sizeof(word32) / sizeof(max_align_t)]; /* holder for current args */
5845
#else
5846
        word32        args[MAX_ASYNC_ARGS]; /* holder for current args */
5847
#endif
5848
    };
5849
#endif
5850
5851
#ifdef HAVE_WRITE_DUP
5852
5853
    #define WRITE_DUP_SIDE 1
5854
    #define READ_DUP_SIDE 2
5855
5856
    typedef struct WriteDup {
5857
        wolfSSL_Mutex   dupMutex;       /* field access mutex */
5858
        int             dupCount;       /* reference count */
5859
        int             dupErr;         /* under dupMutex, pass to other side */
5860
#ifdef WOLFSSL_DTLS13
5861
        struct Dtls13RecordNumber* sendAckList; /* ownership transferred */
5862
        /* Key update ACK tracking: write side stores the (epoch, seq) of its
5863
         * in-flight KeyUpdate; read side sets keyUpdateAcked when the ACK for
5864
         * that exact record arrives.  Both epoch and seq are checked to avoid
5865
         * false positives from data records in the same epoch. */
5866
        w64wrapper keyUpdateEpoch;     /* epoch of the KeyUpdate */
5867
        w64wrapper keyUpdateSeq;       /* seq num of the KeyUpdate */
5868
#endif /* WOLFSSL_DTLS13 */
5869
#ifdef WOLFSSL_TLS13
5870
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
5871
        /* Post-handshake certificate request delegation: the read side received
5872
         * a CertificateRequest but cannot write; it saves state here and the
5873
         * write side sends Certificate+CertificateVerify+Finished. */
5874
        struct HS_Hashes* postHandshakeHashState;    /* transcript at CR time */
5875
        struct CertReqCtx* postHandshakeCertReqCtx; /* context from CR */
5876
        byte postHandshakeSendVerify;    /* ssl->options.sendVerify */
5877
        byte postHandshakeSigAlgo;       /* ssl->options.sigAlgo */
5878
        byte postHandshakeHashAlgo;      /* ssl->options.hashAlgo */
5879
#endif /* WOLFSSL_POST_HANDSHAKE_AUTH */
5880
#endif /* WOLFSSL_TLS13 */
5881
5882
        /* Flags */
5883
#ifdef WOLFSSL_DTLS13
5884
        WC_BITFIELD keyUpdateWaiting:1; /* write side has an unACKed KeyUpdate */
5885
        WC_BITFIELD keyUpdateAcked:1;   /* read side confirmed the ACK arrived */
5886
        /* DTLS 1.3: read side cannot write, so it passes ACK work to the
5887
         * write side. */
5888
        WC_BITFIELD sendAcks:1;
5889
#endif /* WOLFSSL_DTLS13 */
5890
#ifdef WOLFSSL_TLS13
5891
        /* TLS 1.3 (and DTLS 1.3): read side received a KeyUpdate(update_requested)
5892
         * but cannot send the response; write side handles it. */
5893
        WC_BITFIELD keyUpdateRespond:1; /* write side must send a KeyUpdate response */
5894
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
5895
        WC_BITFIELD postHandshakeAuthPending:1; /* write side must respond */
5896
#endif /* WOLFSSL_POST_HANDSHAKE_AUTH */
5897
#endif /* WOLFSSL_TLS13 */
5898
    } WriteDup;
5899
5900
    WOLFSSL_LOCAL void FreeWriteDup(WOLFSSL* ssl);
5901
    WOLFSSL_LOCAL int  NotifyWriteSide(WOLFSSL* ssl, int err);
5902
#endif /* HAVE_WRITE_DUP */
5903
5904
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
5905
typedef struct CertReqCtx CertReqCtx;
5906
5907
struct CertReqCtx {
5908
    CertReqCtx* next;
5909
    byte        len;
5910
    byte        ctx;
5911
};
5912
#endif
5913
5914
#ifdef WOLFSSL_EARLY_DATA
5915
typedef enum EarlyDataState {
5916
    no_early_data,
5917
    early_data_ext,
5918
    expecting_early_data,
5919
    process_early_data,
5920
    done_early_data
5921
} EarlyDataState;
5922
#endif
5923
5924
#ifdef WOLFSSL_DTLS13
5925
5926
/* size of the mask used to encrypt/decrypt Record Number  */
5927
#define DTLS13_RN_MASK_SIZE 16
5928
5929
typedef struct Dtls13UnifiedHdrInfo {
5930
    word16 recordLength;
5931
    byte seqLo;
5932
    byte seqHi;
5933
    byte seqHiPresent:1;
5934
    byte epochBits;
5935
} Dtls13UnifiedHdrInfo;
5936
5937
enum  {
5938
    DTLS13_EPOCH_EARLYDATA = 1,
5939
    DTLS13_EPOCH_HANDSHAKE = 2,
5940
    DTLS13_EPOCH_TRAFFIC0 = 3
5941
};
5942
5943
/* 64-bit epoch + 64-bit sequence number */
5944
#define DTLS13_RN_SIZE (OPAQUE64_LEN + OPAQUE64_LEN)
5945
/* Maximum number of ACK records allowed in an ACK message */
5946
#ifndef DTLS13_ACK_MAX_RECORDS
5947
#define DTLS13_ACK_MAX_RECORDS 128
5948
#endif
5949
/* WOLFSSL_MAX_16BIT / DTLS13_RN_SIZE (0xffff / (OPAQUE64_LEN + OPAQUE64_LEN))
5950
 * Literals are used because OPAQUE64_LEN is an enum value, invisible to the
5951
 * preprocessor. */
5952
#if DTLS13_ACK_MAX_RECORDS > 0xffff / 16
5953
#error "DTLS13_ACK_MAX_RECORDS exceeds the maximum encodable in the word16 length field"
5954
#endif
5955
5956
5957
typedef struct Dtls13Epoch {
5958
    w64wrapper epochNumber;
5959
5960
    w64wrapper nextSeqNumber;
5961
    w64wrapper nextPeerSeqNumber;
5962
5963
#ifndef WOLFSSL_TLS13_IGNORE_AEAD_LIMITS
5964
    w64wrapper dropCount; /* Amount of records that failed decryption */
5965
#endif
5966
5967
    word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
5968
5969
    /* key material for the epoch */
5970
    byte client_write_key[MAX_SYM_KEY_SIZE];
5971
    byte server_write_key[MAX_SYM_KEY_SIZE];
5972
    byte client_write_IV[MAX_WRITE_IV_SZ];
5973
    byte server_write_IV[MAX_WRITE_IV_SZ];
5974
5975
    byte aead_exp_IV[AEAD_MAX_EXP_SZ];
5976
    byte aead_enc_imp_IV[AEAD_MAX_IMP_SZ];
5977
    byte aead_dec_imp_IV[AEAD_MAX_IMP_SZ];
5978
5979
    byte client_sn_key[MAX_SYM_KEY_SIZE];
5980
    byte server_sn_key[MAX_SYM_KEY_SIZE];
5981
5982
    byte isValid;
5983
    byte side;
5984
} Dtls13Epoch;
5985
5986
#ifndef DTLS13_EPOCH_SIZE
5987
#define DTLS13_EPOCH_SIZE 4
5988
#endif
5989
5990
#ifndef DTLS13_RETRANS_RN_SIZE
5991
#define DTLS13_RETRANS_RN_SIZE 3
5992
#endif
5993
5994
enum Dtls13RtxFsmState {
5995
    DTLS13_RTX_FSM_PREPARING = 0,
5996
    DTLS13_RTX_FSM_SENDING,
5997
    DTLS13_RTX_FSM_WAITING,
5998
    DTLS13_RTX_FSM_FINISHED
5999
};
6000
6001
typedef struct Dtls13RtxRecord {
6002
    struct Dtls13RtxRecord *next;
6003
    word16 length;
6004
    byte *data;
6005
    w64wrapper epoch;
6006
    w64wrapper seq[DTLS13_RETRANS_RN_SIZE];
6007
    byte rnIdx;
6008
    byte handshakeType;
6009
} Dtls13RtxRecord;
6010
6011
typedef struct Dtls13RecordNumber {
6012
    struct Dtls13RecordNumber *next;
6013
    w64wrapper epoch;
6014
    w64wrapper seq;
6015
} Dtls13RecordNumber;
6016
6017
typedef struct Dtls13Rtx {
6018
#ifdef WOLFSSL_RW_THREADED
6019
    wolfSSL_Mutex mutex;
6020
#endif
6021
    enum Dtls13RtxFsmState state; /* Unused? */
6022
    Dtls13RtxRecord *rtxRecords;
6023
    Dtls13RtxRecord **rtxRecordTailPtr;
6024
    Dtls13RecordNumber *seenRecords;
6025
    word16 seenRecordsCount;
6026
#ifdef WOLFSSL_32BIT_MILLI_TIME
6027
    word32 lastRtx;
6028
#else
6029
    sword64 lastRtx;
6030
#endif
6031
    byte triggeredRtxs; /* Unused? */
6032
    byte sendAcks;
6033
    byte retransmit;
6034
} Dtls13Rtx;
6035
6036
#endif /* WOLFSSL_DTLS13 */
6037
6038
#ifdef WOLFSSL_DTLS_CID
6039
typedef struct ConnectionID {
6040
    byte length;
6041
/* Ignore "nonstandard extension used : zero-sized array in struct/union"
6042
 * MSVC warning */
6043
#ifdef _MSC_VER
6044
#pragma warning(disable: 4200)
6045
#endif
6046
    byte id[];
6047
} ConnectionID;
6048
6049
typedef struct CIDInfo {
6050
    ConnectionID* tx;
6051
    ConnectionID* rx;
6052
    byte negotiated : 1;
6053
} CIDInfo;
6054
#endif /* WOLFSSL_DTLS_CID */
6055
6056
/* The idea is to reuse the context suites object whenever possible to save
6057
 * space. */
6058
#define WOLFSSL_SUITES(ssl) \
6059
41.3k
    ((const Suites*) ((ssl)->suites != NULL ? \
6060
41.3k
        (ssl)->suites : \
6061
41.3k
        (ssl)->ctx->suites))
6062
6063
/* wolfSSL ssl type */
6064
struct WOLFSSL {
6065
    WOLFSSL_CTX*    ctx;
6066
#if defined(WOLFSSL_HAPROXY)
6067
    WOLFSSL_CTX*    initial_ctx; /* preserve session key materials */
6068
#endif
6069
    Suites*         suites; /* Only need during handshake. Can be NULL when
6070
                             * reusing the context's object. When WOLFSSL
6071
                             * object needs separate instance of suites use
6072
                             * AllocateSuites(). */
6073
    Suites*         clSuites;
6074
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) || \
6075
    defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
6076
    WOLF_STACK_OF(WOLFSSL_CIPHER)* suitesStack; /* stack of available cipher
6077
                                                 * suites */
6078
    WOLF_STACK_OF(WOLFSSL_CIPHER)* clSuitesStack; /* stack of client cipher
6079
                                                   * suites */
6080
#endif
6081
    Arrays*         arrays;
6082
#ifdef WOLFSSL_TLS13
6083
    byte            clientSecret[SECRET_LEN];
6084
    byte            serverSecret[SECRET_LEN];
6085
#endif
6086
    HS_Hashes*      hsHashes;
6087
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
6088
    HS_Hashes*      hsHashesEch;
6089
#endif
6090
    void*           IOCB_ReadCtx;
6091
    void*           IOCB_WriteCtx;
6092
    WC_RNG*         rng;
6093
    void*           verifyCbCtx;        /* cert verify callback user ctx*/
6094
    VerifyCallback  verifyCallback;     /* cert verification callback */
6095
    void*           heap;               /* for user overrides */
6096
#ifdef HAVE_WRITE_DUP
6097
    WriteDup*       dupWrite;           /* valid pointer indicates ON */
6098
             /* side that decrements dupCount to zero frees overall structure */
6099
    byte            dupSide;            /* write side or read side */
6100
#endif
6101
#ifdef OPENSSL_EXTRA
6102
    byte              cbioFlag;         /* WOLFSSL_CBIO_RECV/SEND:
6103
                                         * CBIORecv/Send is set */
6104
#endif
6105
#ifdef WOLFSSL_WOLFSENTRY_HOOKS
6106
    NetworkFilterCallback_t AcceptFilter;
6107
    void *AcceptFilter_arg;
6108
    NetworkFilterCallback_t ConnectFilter;
6109
    void *ConnectFilter_arg;
6110
#endif /* WOLFSSL_WOLFSENTRY_HOOKS */
6111
    CallbackIORecv  CBIORecv;
6112
    CallbackIOSend  CBIOSend;
6113
#ifdef WOLFSSL_STATIC_MEMORY
6114
    WOLFSSL_HEAP_HINT heap_hint;
6115
#endif
6116
#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
6117
    ClientHelloGoodCb chGoodCb;        /* notify user we parsed a verified
6118
                                        * ClientHello that passed basic tests */
6119
    void*             chGoodCtx;       /* user ClientHello cb context  */
6120
#endif
6121
#ifndef NO_HANDSHAKE_DONE_CB
6122
    HandShakeDoneCb hsDoneCb;          /* notify user handshake done */
6123
    void*           hsDoneCtx;         /* user handshake cb context  */
6124
#endif
6125
#ifdef WOLFSSL_ASYNC_IO
6126
#ifdef WOLFSSL_ASYNC_CRYPT
6127
    WC_ASYNC_DEV* asyncDev;
6128
#endif
6129
    /* Message building context should be stored here for functions that expect
6130
     * to encounter encryption blocking or fragment the message. */
6131
    struct WOLFSSL_ASYNC* async;
6132
#endif
6133
    void*           hsKey;              /* Handshake key (RsaKey or ecc_key)
6134
                                         * allocated from heap */
6135
    word32          hsType;             /* Type of Handshake key (hsKey) */
6136
    WOLFSSL_CIPHER  cipher;
6137
#ifdef WOLFSSL_DUAL_ALG_CERTS
6138
    void*           hsAltKey;           /* Handshake key (dilithium, falcon)
6139
                                         * allocated from heap */
6140
    word32          hsAltType;          /* Type of Handshake key (hsAltKey) */
6141
#endif
6142
#ifndef WOLFSSL_AEAD_ONLY
6143
    hmacfp          hmac;
6144
#endif
6145
    Ciphers         encrypt;
6146
    Ciphers         decrypt;
6147
    Buffers         buffers;
6148
    WOLFSSL_SESSION* session;
6149
#ifndef NO_CLIENT_CACHE
6150
    ClientSession*  clientSession;
6151
#endif
6152
    WOLFSSL_ALERT_HISTORY alert_history;
6153
    WOLFSSL_ALERT   pendingAlert;
6154
    int             error;
6155
    int             rfd;                /* read  file descriptor */
6156
    int             wfd;                /* write file descriptor */
6157
    int             rflags;             /* user read  flags */
6158
    int             wflags;             /* user write flags */
6159
    word32          timeout;            /* session timeout */
6160
    word32          fragOffset;         /* fragment offset */
6161
    word16          curSize;
6162
    word32          curStartIdx;
6163
    byte            verifyDepth;
6164
    RecordLayerHeader curRL;
6165
    MsgsReceived    msgsReceived;       /* peer messages received */
6166
    ProtocolVersion version;            /* negotiated version */
6167
    ProtocolVersion chVersion;          /* client hello version */
6168
    CipherSpecs     specs;
6169
    Keys            keys;
6170
    Options         options;
6171
#ifdef WOLFSSL_SESSION_ID_CTX
6172
    byte             sessionCtx[ID_LEN]; /* app session context ID */
6173
    byte             sessionCtxSz;       /* size of sessionCtx stored */
6174
#endif
6175
#ifdef OPENSSL_EXTRA
6176
    CallbackInfoState* CBIS;             /* used to get info about SSL state */
6177
    int              cbmode;             /* read or write on info callback */
6178
    int              cbtype;             /* event type in info callback */
6179
    WOLFSSL_BIO*     biord;              /* socket bio read  to free/close */
6180
    WOLFSSL_BIO*     biowr;              /* socket bio write to free/close */
6181
    WOLFSSL_X509_VERIFY_PARAM* param;    /* verification parameters*/
6182
#endif
6183
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
6184
    word32            disabledCurves;   /* curves disabled by user */
6185
#endif
6186
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
6187
    defined(OPENSSL_ALL)
6188
    unsigned long    peerVerifyRet;
6189
#endif
6190
#ifdef OPENSSL_EXTRA
6191
    byte             readAhead;
6192
#ifdef HAVE_PK_CALLBACKS
6193
    void*            loggingCtx;         /* logging callback argument */
6194
#endif
6195
#endif /* OPENSSL_EXTRA */
6196
#ifndef NO_RSA
6197
    RsaKey*         peerRsaKey;
6198
#if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS)
6199
    void*           RenesasUserCtx;
6200
    byte*           peerSceTsipEncRsaKeyIndex;
6201
#endif
6202
    byte            peerRsaKeyPresent;
6203
#ifdef WC_RSA_PSS
6204
    word8           useRsaPss;           /* cert supports RSA-PSS */
6205
#endif
6206
#endif
6207
#if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
6208
    word16          namedGroup;
6209
#endif
6210
#ifdef WOLFSSL_TLS13
6211
    word16          group[WOLFSSL_MAX_GROUP_COUNT];
6212
    byte            numGroups;
6213
#endif
6214
    word16          pssAlgo;
6215
#ifdef WOLFSSL_TLS13
6216
    word16          certHashSigAlgoSz;  /* SigAlgoCert ext length in bytes */
6217
    byte            certHashSigAlgo[WOLFSSL_MAX_SIGALGO]; /* cert sig/algo to
6218
                                                           * offer */
6219
#endif
6220
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
6221
    int             eccVerifyRes;
6222
#endif
6223
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
6224
    defined(HAVE_ED448) || defined(HAVE_CURVE448)
6225
    word32          ecdhCurveOID;            /* curve Ecc_Sum     */
6226
    ecc_key*        eccTempKey;              /* private ECDHE key */
6227
    byte            eccTempKeyPresent;       /* also holds type */
6228
    byte            peerEccKeyPresent;
6229
#endif
6230
#ifdef HAVE_ECC
6231
    ecc_key*        peerEccKey;              /* peer's  ECDHE key */
6232
    ecc_key*        peerEccDsaKey;           /* peer's  ECDSA key */
6233
    word16          eccTempKeySz;            /* in octets 20 - 66 */
6234
    byte            peerEccDsaKeyPresent;
6235
#endif
6236
#if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
6237
    defined(HAVE_CURVE448) || defined(HAVE_ED448)
6238
    word32          pkCurveOID;              /* curve Ecc_Sum     */
6239
#endif
6240
#ifdef HAVE_ED25519
6241
    ed25519_key*    peerEd25519Key;
6242
    byte            peerEd25519KeyPresent;
6243
#endif
6244
#ifdef HAVE_CURVE25519
6245
    curve25519_key* peerX25519Key;
6246
    byte            peerX25519KeyPresent;
6247
#endif
6248
#ifdef HAVE_ED448
6249
    ed448_key*      peerEd448Key;
6250
    byte            peerEd448KeyPresent;
6251
#endif
6252
#ifdef HAVE_CURVE448
6253
    curve448_key*   peerX448Key;
6254
    byte            peerX448KeyPresent;
6255
#endif
6256
#ifdef HAVE_FALCON
6257
    falcon_key*     peerFalconKey;
6258
    byte            peerFalconKeyPresent;
6259
#endif
6260
#ifdef HAVE_DILITHIUM
6261
    dilithium_key*  peerDilithiumKey;
6262
    byte            peerDilithiumKeyPresent;
6263
#endif
6264
#ifdef HAVE_LIBZ
6265
    z_stream        c_stream;           /* compression   stream */
6266
    z_stream        d_stream;           /* decompression stream */
6267
    byte            didStreamInit;      /* for stream init and end */
6268
#endif
6269
#ifdef WOLFSSL_DTLS
6270
    int             dtls_timeout_init;  /* starting timeout value */
6271
    int             dtls_timeout_max;   /* maximum timeout value */
6272
    int             dtls_timeout;       /* current timeout value, changes */
6273
#ifndef NO_ASN_TIME
6274
    word32          dtls_start_timeout;
6275
#endif /* !NO_ASN_TIME */
6276
    word32          dtls_tx_msg_list_sz;
6277
    word32          dtls_rx_msg_list_sz;
6278
    DtlsMsg*        dtls_tx_msg_list;
6279
    DtlsMsg*        dtls_tx_msg;
6280
    DtlsMsg*        dtls_rx_msg_list;
6281
    void*           IOCB_CookieCtx;     /* gen cookie ctx */
6282
#ifdef WOLFSSL_SESSION_EXPORT
6283
    wc_dtls_export  dtls_export;        /* export function for session */
6284
#endif
6285
#if defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)
6286
    word16          dtlsMtuSz;
6287
#endif /* WOLFSSL_SCTP || WOLFSSL_DTLS_MTU */
6288
#ifdef WOLFSSL_MULTICAST
6289
    void*           mcastHwCbCtx;       /* Multicast highwater callback ctx */
6290
#endif /* WOLFSSL_MULTICAST */
6291
#ifdef WOLFSSL_DTLS_DROP_STATS
6292
    word32 macDropCount;
6293
    word32 replayDropCount;
6294
#endif /* WOLFSSL_DTLS_DROP_STATS */
6295
#ifdef WOLFSSL_SRTP
6296
    word16         dtlsSrtpProfiles;   /* DTLS-with-SRTP profiles list
6297
                                        * (selected profiles - up to 16) */
6298
    word16         dtlsSrtpId;         /* DTLS-with-SRTP profile ID selected */
6299
#endif
6300
#ifdef WOLFSSL_DTLS13
6301
    RecordNumberCiphers dtlsRecordNumberEncrypt;
6302
    RecordNumberCiphers dtlsRecordNumberDecrypt;
6303
    Dtls13Epoch dtls13Epochs[DTLS13_EPOCH_SIZE];
6304
    Dtls13Epoch *dtls13EncryptEpoch;
6305
    Dtls13Epoch *dtls13DecryptEpoch;
6306
    w64wrapper dtls13Epoch;
6307
    w64wrapper dtls13PeerEpoch;
6308
    w64wrapper dtls13InvalidateBefore;
6309
    byte dtls13CurRL[DTLS_RECVD_RL_HEADER_MAX_SZ];
6310
    word16 dtls13CurRlLength;
6311
6312
    /* used to store the message if it needs to be fragmented */
6313
    buffer dtls13FragmentsBuffer;
6314
    byte dtls13SendingFragments:1;
6315
    byte dtls13SendingAckOrRtx;
6316
    byte dtls13FastTimeout:1;
6317
#ifdef HAVE_WRITE_DUP
6318
    byte dtls13KeyUpdateAcked:1;
6319
#endif
6320
    byte dtls13WaitKeyUpdateAck;
6321
    byte dtls13DoKeyUpdate;
6322
    word32 dtls13MessageLength;
6323
    word32 dtls13FragOffset;
6324
    byte dtls13FragHandshakeType;
6325
    Dtls13Rtx dtls13Rtx;
6326
    byte *dtls13ClientHello;
6327
    word16 dtls13ClientHelloSz;
6328
6329
#endif /* WOLFSSL_DTLS13 */
6330
#ifdef WOLFSSL_DTLS_CID
6331
    CIDInfo *dtlsCidInfo;
6332
#endif /* WOLFSSL_DTLS_CID */
6333
6334
#endif /* WOLFSSL_DTLS */
6335
#ifdef WOLFSSL_CALLBACKS
6336
    TimeoutInfo     timeoutInfo;        /* info saved during handshake */
6337
    HandShakeInfo   handShakeInfo;      /* info saved during handshake */
6338
#endif
6339
#ifdef OPENSSL_EXTRA
6340
    SSL_Msg_Cb      protoMsgCb;         /* inspect protocol message callback */
6341
    void*           protoMsgCtx;        /* user set context with msg callback */
6342
#endif
6343
#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
6344
    byte            hsInfoOn;           /* track handshake info        */
6345
    byte            toInfoOn;           /* track timeout   info        */
6346
#endif
6347
#ifdef HAVE_FUZZER
6348
    CallbackFuzzer  fuzzerCb;           /* for testing with using fuzzer */
6349
    void*           fuzzerCtx;          /* user defined pointer */
6350
#endif
6351
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
6352
    CertReqCtx*     certReqCtx;
6353
#endif
6354
#ifdef WOLFSSL_LOCAL_X509_STORE
6355
    WOLFSSL_X509_STORE* x509_store_pt; /* take ownership of external store */
6356
#endif
6357
#ifdef KEEP_PEER_CERT
6358
    /* TODO put this on the heap so we can properly use the
6359
     * reference counter and not have to duplicate it. */
6360
    WOLFSSL_X509     peerCert;           /* X509 peer cert */
6361
#endif
6362
#ifdef KEEP_OUR_CERT
6363
    WOLFSSL_X509*    ourCert;            /* keep alive a X509 struct of cert.
6364
                                            points to ctx if not owned (owned
6365
                                            flag found in buffers.weOwnCert) */
6366
#endif
6367
    byte             keepCert;           /* keep certificate after handshake */
6368
#ifdef HAVE_EX_DATA
6369
    WOLFSSL_CRYPTO_EX_DATA ex_data; /* external data, for Fortress */
6370
#endif
6371
    int              devId;             /* async device id to use */
6372
#ifdef HAVE_ONE_TIME_AUTH
6373
    OneTimeAuth     auth;
6374
#endif
6375
#ifdef HAVE_TLS_EXTENSIONS
6376
    TLSX* extensions;                  /* RFC 6066 TLS Extensions data */
6377
    #ifdef HAVE_MAX_FRAGMENT
6378
        word16 max_fragment;
6379
    #endif
6380
    #ifdef HAVE_TRUNCATED_HMAC
6381
        byte truncated_hmac;
6382
    #endif
6383
    #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
6384
        byte status_request;
6385
    #endif
6386
    #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
6387
        byte status_request_v2;
6388
    #endif
6389
    #if defined(HAVE_SECURE_RENEGOTIATION) \
6390
        || defined(HAVE_SERVER_RENEGOTIATION_INFO)
6391
        int                  secure_rene_count;    /* how many times */
6392
        SecureRenegotiation* secure_renegotiation; /* valid pointer indicates */
6393
    #endif                                         /* user turned on */
6394
    #ifdef HAVE_ALPN
6395
        byte *alpn_peer_requested; /* the ALPN bytes requested by peer, sequence
6396
                                    * of length byte + chars */
6397
        word16 alpn_peer_requested_length; /* number of bytes total */
6398
        #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)  || \
6399
            defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_QUIC)
6400
            CallbackALPNSelect alpnSelect;
6401
            void*              alpnSelectArg;
6402
        #endif
6403
    #endif                         /* of accepted protocols */
6404
    #if !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET)
6405
        CallbackSessionTicket session_ticket_cb;
6406
        void*                 session_ticket_ctx;
6407
        byte                  expect_session_ticket;
6408
    #endif
6409
        word16 hrr_keyshare_group;
6410
#endif /* HAVE_TLS_EXTENSIONS */
6411
#ifdef HAVE_OCSP
6412
        void*       ocspIOCtx;
6413
        byte ocspProducedDate[MAX_DATE_SIZE];
6414
        int ocspProducedDateFormat;
6415
        buffer      ocspCsrResp[1 + MAX_CHAIN_DEPTH];
6416
    #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
6417
        char*   url;
6418
    #endif
6419
#if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
6420
            word32 response_idx;
6421
#endif
6422
#endif
6423
#ifdef HAVE_NETX
6424
    NetX_Ctx        nxCtx;             /* NetX IO Context */
6425
#endif
6426
#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
6427
    void*           mnCtx;             /* mynewt mn_socket IO Context */
6428
#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */
6429
#ifdef WOLFSSL_GNRC
6430
    struct gnrc_wolfssl_ctx *gnrcCtx;  /* Riot-OS GNRC UDP/IP context */
6431
#endif
6432
#ifdef SESSION_INDEX
6433
    int sessionIndex;                  /* Session's location in the cache. */
6434
#endif
6435
#ifdef ATOMIC_USER
6436
    void*    MacEncryptCtx;    /* Atomic User Mac/Encrypt Callback Context */
6437
    void*    DecryptVerifyCtx; /* Atomic User Decrypt/Verify Callback Context */
6438
    #ifdef HAVE_ENCRYPT_THEN_MAC
6439
        void*    EncryptMacCtx;    /* Atomic User Encrypt/Mac Callback Ctx */
6440
        void*    VerifyDecryptCtx; /* Atomic User Verify/Decrypt Callback Ctx */
6441
    #endif
6442
#endif
6443
#ifdef HAVE_PK_CALLBACKS
6444
    #ifdef HAVE_ECC
6445
        void* EccKeyGenCtx;          /* EccKeyGen  Callback Context */
6446
        void* EccSignCtx;            /* Ecc Sign   Callback Context */
6447
        void* EccVerifyCtx;          /* Ecc Verify Callback Context */
6448
        void* EccSharedSecretCtx;    /* Ecc Pms    Callback Context */
6449
    #endif /* HAVE_ECC */
6450
    #ifdef HAVE_HKDF
6451
        void* HkdfExtractCtx;       /* Hkdf extract callback context */
6452
    #endif
6453
    #ifdef HAVE_ED25519
6454
        void* Ed25519SignCtx;        /* ED25519 Sign   Callback Context */
6455
        void* Ed25519VerifyCtx;      /* ED25519 Verify Callback Context */
6456
    #endif
6457
    #ifdef HAVE_CURVE25519
6458
        void* X25519KeyGenCtx;       /* X25519 KeyGen Callback Context */
6459
        void* X25519SharedSecretCtx; /* X25519 Pms    Callback Context */
6460
    #endif
6461
    #ifdef HAVE_ED448
6462
        void* Ed448SignCtx;          /* ED448 Sign   Callback Context */
6463
        void* Ed448VerifyCtx;        /* ED448 Verify Callback Context */
6464
    #endif
6465
    #ifdef HAVE_CURVE448
6466
        void* X448KeyGenCtx;         /* X448 KeyGen Callback Context */
6467
        void* X448SharedSecretCtx;   /* X448 Pms    Callback Context */
6468
    #endif
6469
    #ifndef NO_DH
6470
        void* DhAgreeCtx; /* DH Pms Callback Context */
6471
    #endif /* !NO_DH */
6472
    #ifndef NO_RSA
6473
        void* RsaSignCtx;     /* Rsa Sign   Callback Context */
6474
        void* RsaVerifyCtx;   /* Rsa Verify Callback Context */
6475
        #ifdef WC_RSA_PSS
6476
            void* RsaPssSignCtx;     /* Rsa PSS Sign   Callback Context */
6477
            void* RsaPssVerifyCtx;   /* Rsa PSS Verify Callback Context */
6478
        #endif
6479
        void* RsaEncCtx;      /* Rsa Public  Encrypt   Callback Context */
6480
        void* RsaDecCtx;      /* Rsa Private Decrypt   Callback Context */
6481
    #endif /* NO_RSA */
6482
    void* GenPreMasterCtx;   /* Generate Premaster Callback Context */
6483
    void* GenMasterCtx;      /* Generate Master Callback Context */
6484
    void* GenExtMasterCtx;   /* Generate Extended Master Callback Context */
6485
    void* GenSessionKeyCtx;  /* Generate Session Key Callback Context */
6486
    void* EncryptKeysCtx;    /* Set Encrypt keys Callback Context */
6487
    void* TlsFinishedCtx;    /* Generate Tls Finished Callback Context */
6488
    void* VerifyMacCtx;      /* Verify mac Callback Context */
6489
#endif /* HAVE_PK_CALLBACKS */
6490
#ifdef HAVE_SECRET_CALLBACK
6491
        SessionSecretCb sessionSecretCb;
6492
        void*           sessionSecretCtx;
6493
        TicketParseCb   ticketParseCb;
6494
        void*           ticketParseCtx;
6495
        TlsSecretCb     tlsSecretCb;
6496
        void*           tlsSecretCtx;
6497
    #ifdef WOLFSSL_TLS13
6498
        Tls13SecretCb   tls13SecretCb;
6499
        void*           tls13SecretCtx;
6500
    #endif
6501
    #ifdef OPENSSL_EXTRA
6502
        SessionSecretCb keyLogCb;
6503
    #ifdef WOLFSSL_TLS13
6504
        Tls13SecretCb   tls13KeyLogCb;
6505
    #endif
6506
    #endif
6507
#endif /* HAVE_SECRET_CALLBACK */
6508
#ifdef WOLFSSL_JNI
6509
        void* jObjectRef;     /* reference to WolfSSLSession in JNI wrapper */
6510
#endif /* WOLFSSL_JNI */
6511
#ifdef WOLFSSL_EARLY_DATA
6512
    EarlyDataState earlyData;
6513
    word32 earlyDataSz;
6514
    byte earlyDataStatus;
6515
#endif
6516
#if defined(OPENSSL_EXTRA)
6517
    WOLFSSL_STACK* supportedCiphers; /* Used in wolfSSL_get_ciphers_compat */
6518
    WOLFSSL_STACK* peerCertChain;    /* Used in wolfSSL_get_peer_cert_chain */
6519
    WOLFSSL_STACK* verifiedChain;    /* peer cert chain to CA */
6520
#ifdef KEEP_OUR_CERT
6521
    WOLFSSL_STACK* ourCertChain;    /* Used in wolfSSL_add1_chain_cert */
6522
#endif
6523
#endif
6524
#ifdef WOLFSSL_STATIC_EPHEMERAL
6525
    StaticKeyExchangeInfo_t staticKE;
6526
#endif
6527
#ifdef WOLFSSL_MAXQ10XX_TLS
6528
    maxq_ssl_t maxq_ctx;
6529
#endif
6530
#ifdef WOLFSSL_HAVE_TLS_UNIQUE
6531
    /* Added in libest port: allow applications to get the 'tls-unique' Channel
6532
     * Binding Type (https://tools.ietf.org/html/rfc5929#section-3). This is
6533
     * used in the EST protocol to bind an enrollment to a TLS session through
6534
     * 'proof-of-possession' (https://tools.ietf.org/html/rfc7030#section-3.4
6535
     * and https://tools.ietf.org/html/rfc7030#section-3.5). */
6536
    byte clientFinished[TLS_FINISHED_SZ_MAX];
6537
    byte serverFinished[TLS_FINISHED_SZ_MAX];
6538
    byte clientFinished_len;
6539
    byte serverFinished_len;
6540
#endif
6541
#ifndef WOLFSSL_NO_CA_NAMES
6542
    WOLF_STACK_OF(WOLFSSL_X509_NAME)* client_ca_names; /* Used in *_set/get_client_CA_list
6543
                                                          (server only) */
6544
    WOLF_STACK_OF(WOLFSSL_X509_NAME)* ca_names;        /* Used in *_set0/get0_CA_list */
6545
    WOLF_STACK_OF(WOLFSSL_X509_NAME)* peer_ca_names;   /* Used in *_get0_peer_CA_list
6546
                                                          and (client only)
6547
                                                          wolfSSL_get_client_CA_list */
6548
#endif
6549
#if defined(WOLFSSL_IOTSAFE) && defined(HAVE_PK_CALLBACKS)
6550
    IOTSAFE iotsafe;
6551
#endif
6552
#ifdef WOLFSSL_LWIP_NATIVE
6553
    WOLFSSL_LWIP_NATIVE_STATE      lwipCtx; /* LwIP native socket IO Context */
6554
#endif
6555
#ifdef WOLFSSL_QUIC
6556
    struct {
6557
        const WOLFSSL_QUIC_METHOD* method;
6558
        WOLFSSL_ENCRYPTION_LEVEL enc_level_read;
6559
        WOLFSSL_ENCRYPTION_LEVEL enc_level_read_next;
6560
        WOLFSSL_ENCRYPTION_LEVEL enc_level_latest_recvd;
6561
        WOLFSSL_ENCRYPTION_LEVEL enc_level_write;
6562
        WOLFSSL_ENCRYPTION_LEVEL enc_level_write_next;
6563
        int transport_version;
6564
        const QuicTransportParam* transport_local;
6565
        const QuicTransportParam* transport_peer;
6566
        const QuicTransportParam* transport_peer_draft;
6567
        QuicRecord* input_head;          /* we own, data for handshake */
6568
        QuicRecord* input_tail;          /* points to last element for append */
6569
        QuicRecord* scratch;             /* we own, record construction */
6570
        enum wolfssl_encryption_level_t output_rec_level;
6571
                                         /* encryption level of current output record */
6572
        word32 output_rec_remain;        /* how many bytes of output TLS record
6573
                                          * content have not been handled yet by quic */
6574
    } quic;
6575
#endif /* WOLFSSL_QUIC */
6576
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
6577
    WOLFSSL_EchConfig* echConfigs;
6578
    WOLFSSL_EchConfig* echRetryConfigs;
6579
#endif
6580
#if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && defined(WOLFSSL_TEST_ECH)
6581
    /* Test-only hook: called on the client before ECH encryption, after the
6582
     * inner ClientHello body is fully constructed. The callback may modify
6583
     * innerCh in-place (length stays the same). */
6584
    int (*echInnerHelloCb)(byte* innerCh, word32 innerChLen);
6585
#endif
6586
6587
#if defined(WOLFSSL_SNIFFER) && defined(WOLFSSL_SNIFFER_KEYLOGFILE)
6588
    SSLSnifferSecretCb snifferSecretCb;
6589
#endif /* WOLFSSL_SNIFFER && WOLFSSL_SNIFFER_KEYLOGFILE */
6590
#ifdef WOLFSSL_DUAL_ALG_CERTS
6591
    byte *sigSpec;         /* This pointer never owns the memory. */
6592
    word16 sigSpecSz;
6593
    byte *peerSigSpec;     /* This pointer always owns the memory. */
6594
    word16 peerSigSpecSz;
6595
#endif
6596
#if defined(WOLFSSL_SYS_CRYPTO_POLICY)
6597
    int secLevel; /* The security level of system-wide crypto policy. */
6598
#endif /* WOLFSSL_SYS_CRYPTO_POLICY */
6599
#if !defined(NO_WOLFSSL_CLIENT) && !defined(WOLFSSL_NO_TLS12) && \
6600
    defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_HARDEN_TLS_NO_SCR_CHECK)
6601
    WC_BITFIELD          scr_check_enabled:1;  /* enable/disable SCR check */
6602
#endif
6603
#ifdef HAVE_WRITE_DUP
6604
#ifdef WOLFSSL_TLS13
6605
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
6606
    WC_BITFIELD postHandshakeAuthPending:1;
6607
#endif
6608
#endif
6609
#endif
6610
};
6611
6612
#if defined(WOLFSSL_SYS_CRYPTO_POLICY)
6613
#define WOLFSSL_SECLEVEL_STR "@SECLEVEL="
6614
struct SystemCryptoPolicy {
6615
    int    enabled;
6616
    int    secLevel;
6617
    char   str[MAX_WOLFSSL_CRYPTO_POLICY_SIZE + 1]; /* + 1 for null term */
6618
};
6619
#endif /* WOLFSSL_SYS_CRYPTO_POLICY */
6620
6621
/*
6622
 * wolfSSL_PEM_read_bio_X509 pushes an ASN_NO_PEM_HEADER error
6623
 * to the error queue on file end. This should not be left
6624
 * for the caller to find so we clear the last error.
6625
 */
6626
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_HAVE_ERROR_QUEUE)
6627
#define CLEAR_ASN_NO_PEM_HEADER_ERROR(err)                                     \
6628
do {                                                                           \
6629
    (err) = wolfSSL_ERR_peek_last_error();                                     \
6630
    if (wolfSSL_ERR_GET_LIB(err) == WOLFSSL_ERR_LIB_PEM &&                     \
6631
        wolfSSL_ERR_GET_REASON(err) == -WOLFSSL_PEM_R_NO_START_LINE_E) {       \
6632
        unsigned long peekErr;                                                 \
6633
        do {                                                                   \
6634
            wc_RemoveErrorNode(-1);                                            \
6635
            peekErr = wolfSSL_ERR_peek_last_error();                           \
6636
        } while (wolfSSL_ERR_GET_LIB(peekErr) == WOLFSSL_ERR_LIB_PEM &&        \
6637
                 wolfSSL_ERR_GET_REASON(peekErr) ==                            \
6638
                                              -WOLFSSL_PEM_R_NO_START_LINE_E); \
6639
    }                                                                          \
6640
} while(0)
6641
#else
6642
0
#define CLEAR_ASN_NO_PEM_HEADER_ERROR(err) (void)(err);
6643
#endif
6644
6645
/*
6646
 * The SSL object may have its own certificate store. The below macros simplify
6647
 * logic for choosing which WOLFSSL_CERT_MANAGER and WOLFSSL_X509_STORE to use.
6648
 * Always use SSL specific objects when available and revert to CTX otherwise.
6649
 */
6650
#ifdef WOLFSSL_LOCAL_X509_STORE
6651
#define SSL_CM(ssl) ((ssl)->x509_store_pt ? (ssl)->x509_store_pt->cm : \
6652
                     ((ssl)->ctx->x509_store_pt ? (ssl)->ctx->x509_store_pt->cm : \
6653
                                            (ssl)->ctx->cm))
6654
#define SSL_STORE(ssl) ((ssl)->x509_store_pt ? (ssl)->x509_store_pt : \
6655
                  ((ssl)->ctx->x509_store_pt ? (ssl)->ctx->x509_store_pt : \
6656
                                            &(ssl)->ctx->x509_store))
6657
#define CTX_STORE(ctx) ((ctx)->x509_store_pt ? (ctx)->x509_store_pt : \
6658
                                            &(ctx)->x509_store)
6659
#else
6660
0
#define SSL_CM(ssl) (ssl)->ctx->cm
6661
#endif
6662
/* Issue warning when we are modifying the overall context CM */
6663
#define SSL_CM_WARNING(ssl) \
6664
    do {                                                             \
6665
        if (SSL_CM( (ssl) ) == (ssl)->ctx->cm) {                     \
6666
            WOLFSSL_MSG("Modifying SSL_CTX CM not SSL specific CM"); \
6667
        }                                                            \
6668
    } while (0)
6669
6670
WOLFSSL_LOCAL int  SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
6671
WOLFSSL_LOCAL int  InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
6672
WOLFSSL_LOCAL int  ReinitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup);
6673
WOLFSSL_LOCAL void FreeSSL(WOLFSSL* ssl, void* heap);
6674
WOLFSSL_TEST_VIS   void wolfSSL_ResourceFree(WOLFSSL* ssl);   /* Micrium uses */
6675
#ifndef OPENSSL_COEXIST
6676
#define SSL_ResourceFree wolfSSL_ResourceFree
6677
#endif
6678
6679
6680
#ifndef NO_CERTS
6681
6682
    WOLFSSL_LOCAL int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
6683
                                    long sz, int format, int type, WOLFSSL* ssl,
6684
                                    long* used, int userChain, int verify,
6685
                                    const char *source_name);
6686
    WOLFSSL_LOCAL int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format,
6687
                                 int type, WOLFSSL* ssl, int userChain,
6688
                                WOLFSSL_CRL* crl, int verify);
6689
6690
    #ifndef NO_ASN
6691
    WOLFSSL_LOCAL int CheckHostName(DecodedCert* dCert, const char *domainName,
6692
                                    size_t domainNameLen, unsigned int flags,
6693
                                    byte isIP);
6694
    #endif
6695
#endif
6696
6697
6698
#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
6699
    WOLFSSL_LOCAL void InitHandShakeInfo(HandShakeInfo* info, WOLFSSL* ssl);
6700
    WOLFSSL_LOCAL void FinishHandShakeInfo(HandShakeInfo* info);
6701
    WOLFSSL_LOCAL void AddPacketName(WOLFSSL* ssl, const char* name);
6702
6703
    WOLFSSL_LOCAL void InitTimeoutInfo(TimeoutInfo* info);
6704
    WOLFSSL_LOCAL void FreeTimeoutInfo(TimeoutInfo* info, void* heap);
6705
    WOLFSSL_LOCAL int AddPacketInfo(WOLFSSL* ssl, const char* name, int type,
6706
                             const byte* data, int sz, int written, int lateRL,
6707
                             void* heap);
6708
    WOLFSSL_LOCAL void AddLateName(const char* name, TimeoutInfo* info);
6709
    WOLFSSL_LOCAL void AddLateRecordHeader(const RecordLayerHeader* rl,
6710
                                           TimeoutInfo* info);
6711
#endif
6712
6713
6714
/* Record Layer Header identifier from page 12 */
6715
enum ContentType {
6716
    no_type            = 0,
6717
    change_cipher_spec = 20,
6718
    alert              = 21,
6719
    handshake          = 22,
6720
    application_data   = 23,
6721
    dtls12_cid         = 25,
6722
#ifdef WOLFSSL_DTLS13
6723
    ack                = 26,
6724
#endif /* WOLFSSL_DTLS13 */
6725
};
6726
6727
6728
/* handshake header, same for each message type, pgs 20/21 */
6729
typedef struct HandShakeHeader {
6730
    byte            type;
6731
    word24          length;
6732
} HandShakeHeader;
6733
6734
6735
/* DTLS handshake header, same for each message type */
6736
typedef struct DtlsHandShakeHeader {
6737
    byte            type;
6738
    word24          length;
6739
    byte            message_seq[2];    /* start at 0, retransmit gets same # */
6740
    word24          fragment_offset;   /* bytes in previous fragments */
6741
    word24          fragment_length;   /* length of this fragment */
6742
} DtlsHandShakeHeader;
6743
6744
6745
enum HandShakeType {
6746
    hello_request        =   0,
6747
    client_hello         =   1,
6748
    server_hello         =   2,
6749
    hello_verify_request =   3,    /* DTLS addition */
6750
    session_ticket       =   4,
6751
    end_of_early_data    =   5,
6752
    hello_retry_request  =   6,
6753
    encrypted_extensions =   8,
6754
    certificate          =  11,
6755
    server_key_exchange  =  12,
6756
    certificate_request  =  13,
6757
    server_hello_done    =  14,
6758
    certificate_verify   =  15,
6759
    client_key_exchange  =  16,
6760
    finished             =  20,
6761
    certificate_status   =  22,
6762
    key_update           =  24,
6763
    change_cipher_hs     =  55,    /* simulate unique handshake type for sanity
6764
                                      checks.  record layer change_cipher
6765
                                      conflicts with handshake finished */
6766
    message_hash         = 254,    /* synthetic message type for TLS v1.3 */
6767
    no_shake             = 255     /* used to initialize the DtlsMsg record */
6768
};
6769
6770
enum ProvisionSide {
6771
    PROVISION_CLIENT = 1,
6772
    PROVISION_SERVER = 2,
6773
    PROVISION_CLIENT_SERVER = 3
6774
};
6775
6776
/* cipher requirements */
6777
enum {
6778
    REQUIRES_RSA,
6779
    REQUIRES_DHE,
6780
    REQUIRES_ECC,
6781
    REQUIRES_ECC_STATIC,
6782
    REQUIRES_PSK,
6783
    REQUIRES_RSA_SIG,
6784
    REQUIRES_AEAD
6785
};
6786
6787
static const byte kTlsClientStr[SIZEOF_SENDER+1] = { 0x43, 0x4C, 0x4E, 0x54, 0x00 }; /* CLNT */
6788
static const byte kTlsServerStr[SIZEOF_SENDER+1] = { 0x53, 0x52, 0x56, 0x52, 0x00 }; /* SRVR */
6789
6790
static const byte kTlsClientFinStr[FINISHED_LABEL_SZ + 1] = "client finished";
6791
static const byte kTlsServerFinStr[FINISHED_LABEL_SZ + 1] = "server finished";
6792
6793
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL) || defined(HAVE_CURL)
6794
typedef struct {
6795
    int name_len;
6796
    const char *name;
6797
    int nid;
6798
    word16 curve;
6799
} WOLF_EC_NIST_NAME;
6800
extern const WOLF_EC_NIST_NAME kNistCurves[];
6801
WOLFSSL_LOCAL int set_curves_list(WOLFSSL* ssl, WOLFSSL_CTX *ctx,
6802
        const char* names, byte curves_only);
6803
#endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL || HAVE_CURL */
6804
6805
/* internal functions */
6806
WOLFSSL_LOCAL int SendChangeCipher(WOLFSSL* ssl);
6807
WOLFSSL_LOCAL int SendTicket(WOLFSSL* ssl);
6808
#ifdef HAVE_SESSION_TICKET
6809
WOLFSSL_LOCAL int DoDecryptTicket(const WOLFSSL* ssl, const byte* input,
6810
        word32 len, InternalTicket **it);
6811
/* Return 0 when check successful. <0 on failure. */
6812
WOLFSSL_LOCAL void DoClientTicketFinalize(WOLFSSL* ssl, InternalTicket* it,
6813
                                          const WOLFSSL_SESSION* sess);
6814
6815
#ifdef WOLFSSL_TLS13
6816
WOLFSSL_LOCAL int DoClientTicketCheck(const WOLFSSL* ssl,
6817
        const PreSharedKey* psk, sword64 timeout, const byte* suite);
6818
WOLFSSL_LOCAL void CleanupClientTickets(PreSharedKey* psk);
6819
WOLFSSL_LOCAL int DoClientTicket_ex(const WOLFSSL* ssl, PreSharedKey* psk,
6820
                                    int retainSess);
6821
#endif
6822
6823
WOLFSSL_LOCAL int DoClientTicket(WOLFSSL* ssl, const byte* input, word32 len);
6824
#if defined(HAVE_SNI) || defined(HAVE_ALPN)
6825
WOLFSSL_LOCAL int VerifyTicketBinding(WOLFSSL* ssl);
6826
#endif
6827
#endif /* HAVE_SESSION_TICKET */
6828
WOLFSSL_LOCAL int SendData(WOLFSSL* ssl, const void* data, size_t sz);
6829
#ifdef WOLFSSL_THREADED_CRYPT
6830
WOLFSSL_LOCAL int SendAsyncData(WOLFSSL* ssl);
6831
#endif
6832
#ifdef WOLFSSL_TLS13
6833
WOLFSSL_LOCAL int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType);
6834
#endif
6835
WOLFSSL_LOCAL int SendCertificate(WOLFSSL* ssl);
6836
WOLFSSL_LOCAL int SendCertificateRequest(WOLFSSL* ssl);
6837
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
6838
 || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
6839
WOLFSSL_LOCAL int CreateOcspResponse(WOLFSSL* ssl, OcspRequest** ocspRequest,
6840
                       buffer* response);
6841
#endif
6842
#if defined(HAVE_SECURE_RENEGOTIATION) && \
6843
    !defined(NO_WOLFSSL_SERVER)
6844
WOLFSSL_LOCAL int SendHelloRequest(WOLFSSL* ssl);
6845
#endif
6846
WOLFSSL_LOCAL int SendCertificateStatus(WOLFSSL* ssl);
6847
WOLFSSL_LOCAL int SendServerKeyExchange(WOLFSSL* ssl);
6848
WOLFSSL_LOCAL int SendBuffered(WOLFSSL* ssl);
6849
WOLFSSL_LOCAL int ReceiveData(WOLFSSL* ssl, byte* output, size_t sz, int peek);
6850
WOLFSSL_LOCAL int SendFinished(WOLFSSL* ssl);
6851
WOLFSSL_LOCAL int RetrySendAlert(WOLFSSL* ssl);
6852
WOLFSSL_LOCAL int SendAlert(WOLFSSL* ssl, int severity, int type);
6853
WOLFSSL_LOCAL int SendFatalAlertOnly(WOLFSSL *ssl, int error);
6854
WOLFSSL_LOCAL int ProcessReply(WOLFSSL* ssl);
6855
WOLFSSL_LOCAL int ProcessReplyEx(WOLFSSL* ssl, int allowSocketErr);
6856
6857
WOLFSSL_LOCAL const char* AlertTypeToString(int type);
6858
6859
WOLFSSL_LOCAL int SetCipherSpecs(WOLFSSL* ssl);
6860
WOLFSSL_LOCAL int GetCipherSpec(word16 side, byte cipherSuite0,
6861
        byte cipherSuite, CipherSpecs* specs, Options* opts);
6862
WOLFSSL_LOCAL int MakeMasterSecret(WOLFSSL* ssl);
6863
6864
WOLFSSL_LOCAL int DeriveKeys(WOLFSSL* ssl);
6865
WOLFSSL_LOCAL int StoreKeys(WOLFSSL* ssl, const byte* keyData, int side);
6866
6867
WOLFSSL_LOCAL int IsTLS(const WOLFSSL* ssl);
6868
WOLFSSL_LOCAL int IsTLS_ex(const ProtocolVersion pv);
6869
WOLFSSL_LOCAL int IsAtLeastTLSv1_2(const WOLFSSL* ssl);
6870
WOLFSSL_LOCAL int IsAtLeastTLSv1_3(ProtocolVersion pv);
6871
WOLFSSL_LOCAL int IsEncryptionOn(const WOLFSSL* ssl, int isSend);
6872
WOLFSSL_LOCAL int TLSv1_3_Capable(WOLFSSL* ssl);
6873
6874
WOLFSSL_LOCAL void FreeHandshakeResources(WOLFSSL* ssl);
6875
WOLFSSL_LOCAL void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree);
6876
WOLFSSL_LOCAL void ShrinkOutputBuffer(WOLFSSL* ssl);
6877
WOLFSSL_LOCAL byte* GetOutputBuffer(WOLFSSL* ssl);
6878
6879
WOLFSSL_LOCAL int CipherRequires(byte first, byte second, int requirement);
6880
WOLFSSL_LOCAL int VerifyClientSuite(word16 havePSK, byte cipherSuite0,
6881
                                    byte cipherSuite);
6882
6883
WOLFSSL_LOCAL int SetTicket(WOLFSSL* ssl, const byte* ticket, word32 length);
6884
WOLFSSL_LOCAL int wolfssl_local_GetRecordSize(WOLFSSL *ssl, int payloadSz,
6885
        int isEncrypted);
6886
WOLFSSL_LOCAL int wolfssl_local_GetMaxPlaintextSize(WOLFSSL *ssl);
6887
WOLFSSL_LOCAL int wolfSSL_GetMaxFragSize(WOLFSSL* ssl);
6888
6889
#if defined(WOLFSSL_IOTSAFE) && defined(HAVE_PK_CALLBACKS)
6890
WOLFSSL_LOCAL IOTSAFE *wolfSSL_get_iotsafe_ctx(WOLFSSL *ssl);
6891
WOLFSSL_LOCAL int wolfSSL_set_iotsafe_ctx(WOLFSSL *ssl, IOTSAFE *iotsafe);
6892
#endif
6893
6894
#if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && defined(HAVE_ECC)
6895
WOLFSSL_LOCAL int SetECKeyInternal(WOLFSSL_EC_KEY* eckey);
6896
WOLFSSL_LOCAL int SetECKeyExternal(WOLFSSL_EC_KEY* eckey);
6897
#endif
6898
6899
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
6900
WOLFSSL_LOCAL int wolfSSL_curve_is_disabled(const WOLFSSL* ssl,
6901
                                            word16 curve_id);
6902
#else
6903
static WC_INLINE int wolfSSL_curve_is_disabled(const WOLFSSL* ssl,
6904
                                               word16 curve_id)
6905
0
{
6906
0
    (void)ssl;
6907
0
    (void)curve_id;
6908
0
    return 0;
6909
0
}
Unexecuted instantiation: asn.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: ssl.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: tls.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: tls13.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: internal.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: keys.c:wolfSSL_curve_is_disabled
Unexecuted instantiation: wolfio.c:wolfSSL_curve_is_disabled
6910
#endif
6911
6912
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
6913
WOLFSSL_LOCAL WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA *rsa, WC_RNG **tmpRNG,
6914
                                         int *initTmpRng);
6915
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
6916
6917
#ifndef NO_CERTS
6918
    #ifndef NO_RSA
6919
        #ifdef WC_RSA_PSS
6920
            WOLFSSL_LOCAL int CheckRsaPssPadding(const byte* plain, word32 plainSz,
6921
                byte* out, word32 sigSz, enum wc_HashType hashType);
6922
            WOLFSSL_LOCAL int ConvertHashPss(int hashAlgo,
6923
                enum wc_HashType* hashType, int* mgf);
6924
        #endif
6925
        WOLFSSL_LOCAL int VerifyRsaSign(WOLFSSL* ssl, byte* verifySig,
6926
            word32 sigSz, const byte* plain, word32 plainSz, int sigAlgo,
6927
            int hashAlgo, RsaKey* key, DerBuffer* keyBufInfo);
6928
        WOLFSSL_LOCAL int RsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
6929
            byte* out, word32* outSz, int sigAlgo, int hashAlgo, RsaKey* key,
6930
            DerBuffer* keyBufInfo);
6931
        WOLFSSL_LOCAL int RsaVerify(WOLFSSL* ssl, byte* in, word32 inSz,
6932
            byte** out, int sigAlgo, int hashAlgo, RsaKey* key,
6933
            buffer* keyBufInfo);
6934
        WOLFSSL_LOCAL int RsaDec(WOLFSSL* ssl, byte* in, word32 inSz, byte** out,
6935
            word32* outSz, RsaKey* key, DerBuffer* keyBufInfo);
6936
        WOLFSSL_LOCAL int RsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
6937
            word32* outSz, RsaKey* key, buffer* keyBufInfo);
6938
    #endif /* !NO_RSA */
6939
6940
    #ifdef HAVE_ECC
6941
        WOLFSSL_LOCAL int EccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
6942
            byte* out, word32* outSz, ecc_key* key, DerBuffer* keyBufInfo);
6943
        WOLFSSL_LOCAL int EccVerify(WOLFSSL* ssl, const byte* in, word32 inSz,
6944
            const byte* out, word32 outSz, ecc_key* key, buffer* keyBufInfo);
6945
        WOLFSSL_LOCAL int EccSharedSecret(WOLFSSL* ssl, ecc_key* priv_key,
6946
            ecc_key* pub_key, byte* pubKeyDer, word32* pubKeySz, byte* out,
6947
            word32* outlen, int side);
6948
    #endif /* HAVE_ECC */
6949
    #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
6950
        WOLFSSL_LOCAL int Sm2wSm3Sign(WOLFSSL* ssl, const byte* id, word32 idSz,
6951
            const byte* in, word32 inSz, byte* out, word32* outSz, ecc_key* key,
6952
            DerBuffer* keyBufInfo);
6953
        WOLFSSL_LOCAL int Sm2wSm3Verify(WOLFSSL* ssl, const byte* id,
6954
            word32 idSz, const byte* in, word32 inSz, const byte* out,
6955
            word32 outSz, ecc_key* key, buffer* keyBufInfo);
6956
    #endif /* WOLFSSL_SM2 && WOLFSSL_SM3 */
6957
    #ifdef HAVE_ED25519
6958
        WOLFSSL_LOCAL int Ed25519CheckPubKey(WOLFSSL* ssl);
6959
        WOLFSSL_LOCAL int Ed25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
6960
            byte* out, word32* outSz, ed25519_key* key, DerBuffer* keyBufInfo);
6961
        WOLFSSL_LOCAL int Ed25519Verify(WOLFSSL* ssl, const byte* in,
6962
            word32 inSz, const byte* msg, word32 msgSz, ed25519_key* key,
6963
            buffer* keyBufInfo);
6964
    #endif /* HAVE_ED25519 */
6965
    #ifdef HAVE_ED448
6966
        WOLFSSL_LOCAL int Ed448CheckPubKey(WOLFSSL* ssl);
6967
        WOLFSSL_LOCAL int Ed448Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
6968
            byte* out, word32* outSz, ed448_key* key, DerBuffer* keyBufInfo);
6969
        WOLFSSL_LOCAL int Ed448Verify(WOLFSSL* ssl, const byte* in,
6970
            word32 inSz, const byte* msg, word32 msgSz, ed448_key* key,
6971
            buffer* keyBufInfo);
6972
    #endif /* HAVE_ED448 */
6973
6974
6975
    #ifdef WOLFSSL_TRUST_PEER_CERT
6976
6977
        /* options for searching hash table for a matching trusted peer cert */
6978
        #define WC_MATCH_SKID 0
6979
        #define WC_MATCH_NAME 1
6980
6981
        WOLFSSL_LOCAL TrustedPeerCert* GetTrustedPeer(void* vp, DecodedCert* cert);
6982
        WOLFSSL_LOCAL int MatchTrustedPeer(TrustedPeerCert* tp,
6983
                                                             DecodedCert* cert);
6984
    #endif
6985
6986
6987
    #ifndef GetCA
6988
        WOLFSSL_LOCAL Signer* GetCA(void* vp, byte* hash);
6989
    #endif
6990
    #if defined(WOLFSSL_AKID_NAME) && !defined(WC_SYM_RELOC_TABLES)
6991
        /* note WOLFSSL_API_PREFIX_MAPping is in asn.h, and if
6992
         * WC_SYM_RELOC_TABLES, the prototype is in the port layer
6993
         * (e.g. linuxkm_wc_port.h), to allow shimming.
6994
         */
6995
        WOLFSSL_TEST_VIS Signer* GetCAByAKID(void* vp, const byte* issuer,
6996
                word32 issuerSz, const byte* serial, word32 serialSz);
6997
    #endif
6998
    #if defined(HAVE_OCSP) && !defined(GetCAByKeyHash)
6999
        WOLFSSL_LOCAL Signer* GetCAByKeyHash(void* vp, const byte* keyHash);
7000
    #endif
7001
    #if !defined(NO_SKID) && !defined(GetCAByName)
7002
        WOLFSSL_LOCAL Signer* GetCAByName(void* vp, byte* hash);
7003
    #endif
7004
#endif /* !NO_CERTS */
7005
WOLFSSL_LOCAL int  BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash,
7006
                                   word32* hashLen);
7007
WOLFSSL_LOCAL int  BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes,
7008
                                   const byte* sender);
7009
WOLFSSL_LOCAL void FreeArrays(WOLFSSL* ssl, int keep);
7010
WOLFSSL_LOCAL  int CheckAvailableSize(WOLFSSL *ssl, int size);
7011
WOLFSSL_LOCAL  int GrowInputBuffer(WOLFSSL* ssl, int size, int usedLength);
7012
WOLFSSL_LOCAL  int MsgCheckEncryption(WOLFSSL* ssl, byte type, byte encrypted);
7013
WOLFSSL_LOCAL  int EarlySanityCheckMsgReceived(WOLFSSL* ssl, byte type,
7014
        word32 msgSz);
7015
WOLFSSL_LOCAL int GetHandshakeHeader(WOLFSSL* ssl, const byte* input,
7016
        word32* inOutIdx, byte* type, word32* size, word32 totalSz);
7017
#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
7018
WOLFSSL_LOCAL void DoCertFatalAlert(WOLFSSL* ssl, int ret);
7019
#endif
7020
#ifndef NO_TLS
7021
    WOLFSSL_LOCAL int  MakeTlsMasterSecret(WOLFSSL* ssl);
7022
#ifndef WOLFSSL_AEAD_ONLY
7023
    WOLFSSL_LOCAL int  TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in,
7024
                                word32 sz, int padSz, int content, int verify, int epochOrder);
7025
#endif
7026
#endif
7027
7028
WOLFSSL_LOCAL int cipherExtraData(WOLFSSL* ssl);
7029
WOLFSSL_LOCAL word32 MacSize(const WOLFSSL* ssl);
7030
7031
#ifndef NO_WOLFSSL_CLIENT
7032
    WOLFSSL_LOCAL int HaveUniqueSessionObj(WOLFSSL* ssl);
7033
    WOLFSSL_LOCAL int SendClientHello(WOLFSSL* ssl);
7034
    WOLFSSL_LOCAL int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
7035
        word32 size);
7036
    #ifdef WOLFSSL_TLS13
7037
    WOLFSSL_LOCAL int SendTls13ClientHello(WOLFSSL* ssl);
7038
    #endif
7039
    WOLFSSL_LOCAL int SendClientKeyExchange(WOLFSSL* ssl);
7040
    WOLFSSL_LOCAL int SendCertificateVerify(WOLFSSL* ssl);
7041
#endif /* NO_WOLFSSL_CLIENT */
7042
7043
#ifndef NO_WOLFSSL_SERVER
7044
    WOLFSSL_LOCAL int SendServerHello(WOLFSSL* ssl);
7045
    WOLFSSL_LOCAL int SendServerHelloDone(WOLFSSL* ssl);
7046
#endif /* NO_WOLFSSL_SERVER */
7047
7048
#ifdef WOLFSSL_TLS13
7049
    WOLFSSL_LOCAL int SendTls13KeyUpdate(WOLFSSL* ssl);
7050
#endif
7051
7052
#ifdef WOLFSSL_DTLS
7053
    #ifdef WOLFSSL_API_PREFIX_MAP
7054
        #define DtlsMsgListDelete wolfSSL_DtlsMsgListDelete
7055
        #define DtlsMsgFind wolfSSL_DtlsMsgFind
7056
        #define DtlsMsgStore wolfSSL_DtlsMsgStore
7057
    #endif /* WOLFSSL_API_PREFIX_MAP */
7058
    WOLFSSL_LOCAL DtlsMsg* DtlsMsgNew(word32 sz, byte tx, void* heap);
7059
    WOLFSSL_LOCAL void DtlsMsgDelete(DtlsMsg* item, void* heap);
7060
    WOLFSSL_TEST_VIS void DtlsMsgListDelete(DtlsMsg* head, void* heap);
7061
    WOLFSSL_LOCAL void DtlsTxMsgListClean(WOLFSSL* ssl);
7062
    WOLFSSL_LOCAL int  DtlsMsgSet(DtlsMsg* msg, word32 seq, word16 epoch,
7063
                                  const byte* data, byte type,
7064
                                  word32 fragOffset, word32 fragSz, void* heap,
7065
                                  word32 totalLen, byte encrypted);
7066
    WOLFSSL_TEST_VIS DtlsMsg* DtlsMsgFind(DtlsMsg* head, word16 epoch, word32 seq);
7067
7068
    WOLFSSL_TEST_VIS void DtlsMsgStore(WOLFSSL* ssl, word16 epoch, word32 seq,
7069
                                    const byte* data, word32 dataSz, byte type,
7070
                                    word32 fragOffset, word32 fragSz,
7071
                                    void* heap);
7072
    WOLFSSL_LOCAL DtlsMsg* DtlsMsgInsert(DtlsMsg* head, DtlsMsg* item);
7073
7074
    WOLFSSL_LOCAL int  DtlsMsgPoolSave(WOLFSSL* ssl, const byte* data,
7075
                                       word32 dataSz, enum HandShakeType type);
7076
    WOLFSSL_LOCAL int  DtlsMsgPoolTimeout(WOLFSSL* ssl);
7077
    WOLFSSL_LOCAL int  VerifyForDtlsMsgPoolSend(WOLFSSL* ssl, byte type,
7078
                                                word32 fragOffset);
7079
    WOLFSSL_LOCAL int  VerifyForTxDtlsMsgDelete(WOLFSSL* ssl, DtlsMsg* item);
7080
    WOLFSSL_LOCAL void DtlsMsgPoolReset(WOLFSSL* ssl);
7081
    WOLFSSL_LOCAL int  DtlsMsgPoolSend(WOLFSSL* ssl, int sendOnlyFirstPacket);
7082
    WOLFSSL_LOCAL void DtlsMsgDestroyFragBucket(DtlsFragBucket* fragBucket, void* heap);
7083
    WOLFSSL_LOCAL int GetDtlsHandShakeHeader(WOLFSSL *ssl, const byte *input,
7084
        word32 *inOutIdx, byte *type, word32 *size, word32 *fragOffset,
7085
        word32 *fragSz, word32 totalSz);
7086
    WOLFSSL_LOCAL int DtlsMsgDrain(WOLFSSL *ssl);
7087
    WOLFSSL_LOCAL int SendHelloVerifyRequest(WOLFSSL* ssl,
7088
        const byte* cookie, byte cookieSz);
7089
7090
#if !defined(NO_WOLFSSL_SERVER)
7091
    WOLFSSL_LOCAL int DoClientHelloStateless(WOLFSSL* ssl,
7092
            const byte* input, word32 helloSz, byte isFirstCHFrag, byte* tls13);
7093
#endif /* !defined(NO_WOLFSSL_SERVER) */
7094
#if !defined(WOLFCRYPT_ONLY) && !defined(WOLFSSL_NO_SOCK) && \
7095
    (defined(USE_WOLFSSL_IO) || defined(WOLFSSL_USER_IO))
7096
    WOLFSSL_LOCAL int sockAddrEqual(SOCKADDR_S *a, XSOCKLENT aLen,
7097
                                    SOCKADDR_S *b, XSOCKLENT bLen);
7098
#endif
7099
#endif /* WOLFSSL_DTLS */
7100
7101
#if defined(HAVE_SECURE_RENEGOTIATION) && defined(WOLFSSL_DTLS)
7102
    WOLFSSL_LOCAL int DtlsSCRKeysSet(WOLFSSL* ssl);
7103
    WOLFSSL_LOCAL int IsDtlsMsgSCRKeys(WOLFSSL* ssl);
7104
    WOLFSSL_LOCAL int DtlsUseSCRKeys(WOLFSSL* ssl);
7105
    WOLFSSL_LOCAL int DtlsCheckOrder(WOLFSSL* ssl, int order);
7106
#endif
7107
    WOLFSSL_LOCAL int IsSCR(WOLFSSL* ssl);
7108
    WOLFSSL_LOCAL int IsDtlsNotSctpMode(WOLFSSL* ssl);
7109
    WOLFSSL_LOCAL int IsDtlsNotSrtpMode(WOLFSSL* ssl);
7110
7111
    WOLFSSL_LOCAL void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out);
7112
7113
#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || \
7114
        !defined(NO_PSK) || defined(WOLFSSL_DTLS13))
7115
#ifdef WOLFSSL_32BIT_MILLI_TIME
7116
    WOLFSSL_LOCAL word32 TimeNowInMilliseconds(void);
7117
#else
7118
    WOLFSSL_LOCAL sword64 TimeNowInMilliseconds(void);
7119
#endif
7120
7121
#endif
7122
WOLFSSL_LOCAL word32  LowResTimer(void);
7123
7124
WOLFSSL_LOCAL int FindSuiteSSL(const WOLFSSL* ssl, byte* suite);
7125
WOLFSSL_LOCAL int FindSuite(const Suites* suites, byte first, byte second);
7126
7127
WOLFSSL_LOCAL void DecodeSigAlg(const byte* input, byte* hashAlgo,
7128
        byte* hsType);
7129
WOLFSSL_LOCAL enum wc_HashType HashAlgoToType(int hashAlgo);
7130
7131
#ifndef NO_CERTS
7132
    WOLFSSL_LOCAL void InitX509Name(WOLFSSL_X509_NAME* name, int dynamicFlag,
7133
                                    void* heap);
7134
    WOLFSSL_LOCAL void FreeX509Name(WOLFSSL_X509_NAME* name);
7135
    WOLFSSL_LOCAL void InitX509(WOLFSSL_X509* x509, int dynamicFlag,
7136
                                void* heap);
7137
    WOLFSSL_LOCAL void FreeX509(WOLFSSL_X509* x509);
7138
    #ifndef NO_ASN
7139
    WOLFSSL_LOCAL int  CopyDecodedToX509(WOLFSSL_X509* x509,
7140
                                         DecodedCert* dCert);
7141
    #endif
7142
#endif
7143
7144
#if defined(WOLFSSL_ACERT)
7145
    WOLFSSL_LOCAL int  CopyDecodedAcertToX509(WOLFSSL_X509_ACERT* x509,
7146
                                              DecodedAcert* dAcert);
7147
#endif /* WOLFSSL_ACERT */
7148
7149
7150
#ifndef MAX_CIPHER_NAME
7151
#define MAX_CIPHER_NAME 50
7152
#endif
7153
7154
#ifdef WOLFSSL_NAMES_STATIC
7155
typedef char cipher_name[MAX_CIPHER_NAME];
7156
#else
7157
typedef const char* cipher_name;
7158
#endif
7159
7160
typedef struct CipherSuiteInfo {
7161
    cipher_name name;
7162
#ifndef NO_ERROR_STRINGS
7163
    cipher_name name_iana;
7164
#endif
7165
    byte cipherSuite0;
7166
    byte cipherSuite;
7167
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_QT) || \
7168
    defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
7169
    byte minor;
7170
    byte major;
7171
#endif
7172
    byte flags;
7173
} CipherSuiteInfo;
7174
7175
WOLFSSL_LOCAL const CipherSuiteInfo* GetCipherNames(void);
7176
WOLFSSL_LOCAL int GetCipherNamesSize(void);
7177
WOLFSSL_LOCAL const char* GetCipherNameInternal(byte cipherSuite0, byte cipherSuite);
7178
#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
7179
/* used in wolfSSL_sk_CIPHER_description */
7180
#define MAX_SEGMENTS    5
7181
#define MAX_SEGMENT_SZ 20
7182
WOLFSSL_LOCAL int wolfSSL_sk_CIPHER_description(WOLFSSL_CIPHER* cipher);
7183
WOLFSSL_LOCAL const char* GetCipherSegment(const WOLFSSL_CIPHER* cipher,
7184
                                           char n[][MAX_SEGMENT_SZ]);
7185
WOLFSSL_LOCAL const char* GetCipherProtocol(byte minor);
7186
WOLFSSL_LOCAL const char* GetCipherKeaStr(char n[][MAX_SEGMENT_SZ]);
7187
WOLFSSL_LOCAL const char* GetCipherAuthStr(char n[][MAX_SEGMENT_SZ]);
7188
WOLFSSL_LOCAL const char* GetCipherEncStr(char n[][MAX_SEGMENT_SZ]);
7189
WOLFSSL_LOCAL const char* GetCipherMacStr(char n[][MAX_SEGMENT_SZ]);
7190
WOLFSSL_LOCAL int SetCipherBits(const char* enc);
7191
WOLFSSL_LOCAL int IsCipherAEAD(char n[][MAX_SEGMENT_SZ]);
7192
#endif
7193
WOLFSSL_LOCAL const char* GetCipherNameIana(byte cipherSuite0, byte cipherSuite);
7194
WOLFSSL_LOCAL const char* wolfSSL_get_cipher_name_internal(WOLFSSL* ssl);
7195
WOLFSSL_LOCAL const char* wolfSSL_get_cipher_name_iana(WOLFSSL* ssl);
7196
WOLFSSL_LOCAL int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
7197
                       byte* cipherSuite, byte* major, byte* minor, int* flags);
7198
7199
7200
enum encrypt_side {
7201
    ENCRYPT_SIDE_ONLY = 1,
7202
    DECRYPT_SIDE_ONLY,
7203
    ENCRYPT_AND_DECRYPT_SIDE
7204
};
7205
7206
WOLFSSL_LOCAL int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys,
7207
    CipherSpecs* specs, int side, void* heap, int devId, WC_RNG* rng,
7208
    int tls13);
7209
WOLFSSL_LOCAL int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side);
7210
7211
/* Set*Internal and Set*External functions */
7212
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
7213
WOLFSSL_LOCAL int SetDsaInternal(WOLFSSL_DSA* dsa);
7214
WOLFSSL_LOCAL int SetDsaExternal(WOLFSSL_DSA* dsa);
7215
WOLFSSL_LOCAL int SetRsaExternal(WOLFSSL_RSA* rsa);
7216
WOLFSSL_LOCAL int SetRsaInternal(WOLFSSL_RSA* rsa);
7217
7218
typedef enum elem_set {
7219
    ELEMENT_P   = 0x01,
7220
    ELEMENT_Q   = 0x02,
7221
    ELEMENT_G   = 0x04,
7222
    ELEMENT_PUB = 0x08,
7223
    ELEMENT_PRV = 0x10,
7224
} Element_Set;
7225
WOLFSSL_LOCAL int SetDhExternal_ex(WOLFSSL_DH *dh, int elm );
7226
WOLFSSL_LOCAL int SetDhInternal(WOLFSSL_DH* dh);
7227
WOLFSSL_LOCAL int SetDhExternal(WOLFSSL_DH *dh);
7228
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
7229
7230
#if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
7231
    WOLFSSL_LOCAL int DhGenKeyPair(WOLFSSL* ssl, DhKey* dhKey,
7232
        byte* priv, word32* privSz,
7233
        byte* pub, word32* pubSz);
7234
    WOLFSSL_LOCAL int DhAgree(WOLFSSL* ssl, DhKey* dhKey,
7235
        const byte* priv, word32 privSz,
7236
        const byte* otherPub, word32 otherPubSz,
7237
        byte* agree, word32* agreeSz,
7238
        const byte* prime, word32 primeSz);
7239
#endif /* !NO_DH */
7240
7241
#ifdef HAVE_ECC
7242
    WOLFSSL_LOCAL int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer);
7243
    WOLFSSL_LOCAL word16 GetCurveByOID(int oidSum);
7244
#endif
7245
7246
WOLFSSL_LOCAL int InitHandshakeHashes(WOLFSSL* ssl);
7247
WOLFSSL_LOCAL void Free_HS_Hashes(HS_Hashes* hsHashes, void* heap);
7248
WOLFSSL_LOCAL void FreeHandshakeHashes(WOLFSSL* ssl);
7249
WOLFSSL_LOCAL int InitHandshakeHashesAndCopy(WOLFSSL* ssl, HS_Hashes* source,
7250
    HS_Hashes** destination);
7251
7252
7253
#ifndef WOLFSSL_NO_TLS12
7254
WOLFSSL_LOCAL void FreeBuildMsgArgs(WOLFSSL* ssl, BuildMsgArgs* args);
7255
#endif
7256
WOLFSSL_LOCAL int BuildMessage(WOLFSSL* ssl, byte* output, int outSz,
7257
                        const byte* input, int inSz, int type, int hashOutput,
7258
                        int sizeOnly, int asyncOkay, int epochOrder);
7259
7260
#ifdef WOLFSSL_TLS13
7261
#ifdef WOLFSSL_API_PREFIX_MAP
7262
    #define BuildTls13Message wolfSSL_BuildTls13Message
7263
#endif
7264
WOLFSSL_TEST_VIS int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
7265
               int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay);
7266
WOLFSSL_LOCAL int Tls13UpdateKeys(WOLFSSL* ssl);
7267
#endif
7268
7269
WOLFSSL_LOCAL int AllocKey(WOLFSSL* ssl, int type, void** pKey);
7270
WOLFSSL_LOCAL void FreeKey(WOLFSSL* ssl, int type, void** pKey);
7271
7272
#ifdef WOLFSSL_ASYNC_CRYPT
7273
    WOLFSSL_LOCAL int wolfSSL_AsyncInit(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags);
7274
    WOLFSSL_LOCAL int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state);
7275
    WOLFSSL_LOCAL int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev);
7276
#endif
7277
7278
#if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
7279
    (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
7280
    !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
7281
WOLFSSL_LOCAL int LoadCertByIssuer(WOLFSSL_X509_STORE* store,
7282
                                           X509_NAME* issuer, int Type);
7283
#endif
7284
#if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
7285
WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_BY_DIR_HASH_new(void);
7286
WOLFSSL_LOCAL void wolfSSL_BY_DIR_HASH_free(WOLFSSL_BY_DIR_HASH* dir_hash);
7287
WOLFSSL_LOCAL WOLFSSL_STACK* wolfSSL_sk_BY_DIR_HASH_new_null(void);
7288
WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_find(
7289
   WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk, const WOLFSSL_BY_DIR_HASH* toFind);
7290
WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_num(const WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk);
7291
WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_sk_BY_DIR_HASH_value(
7292
                        const WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk, int i);
7293
WOLFSSL_LOCAL WOLFSSL_BY_DIR_HASH* wolfSSL_sk_BY_DIR_HASH_pop(
7294
                                WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk);
7295
WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_HASH_pop_free(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk,
7296
    void (*f) (WOLFSSL_BY_DIR_HASH*));
7297
WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_HASH_free(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH) *sk);
7298
WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_HASH_push(WOLF_STACK_OF(WOLFSSL_BY_DIR_HASH)* sk,
7299
                                               WOLFSSL_BY_DIR_HASH* in);
7300
/* WOLFSSL_BY_DIR_entry stuff */
7301
WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_BY_DIR_entry_new(void);
7302
WOLFSSL_LOCAL void wolfSSL_BY_DIR_entry_free(WOLFSSL_BY_DIR_entry* entry);
7303
WOLFSSL_LOCAL WOLFSSL_STACK* wolfSSL_sk_BY_DIR_entry_new_null(void);
7304
WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_entry_num(const WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *sk);
7305
WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_sk_BY_DIR_entry_value(
7306
                        const WOLF_STACK_OF(WOLFSSL_BY_DIR_entry) *sk, int i);
7307
WOLFSSL_LOCAL WOLFSSL_BY_DIR_entry* wolfSSL_sk_BY_DIR_entry_pop(
7308
                                WOLF_STACK_OF(WOLFSSL_BY_DIR_entry)* sk);
7309
WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_entry_pop_free(WOLF_STACK_OF(wolfSSL_BY_DIR_entry)* sk,
7310
    void (*f) (WOLFSSL_BY_DIR_entry*));
7311
WOLFSSL_LOCAL void wolfSSL_sk_BY_DIR_entry_free(WOLF_STACK_OF(wolfSSL_BY_DIR_entry) *sk);
7312
WOLFSSL_LOCAL int wolfSSL_sk_BY_DIR_entry_push(WOLF_STACK_OF(wolfSSL_BY_DIR_entry)* sk,
7313
                                               WOLFSSL_BY_DIR_entry* in);
7314
#endif /* OPENSSL_ALL && !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
7315
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
7316
WOLFSSL_LOCAL int oid2nid(word32 oid, int grp);
7317
WOLFSSL_LOCAL word32 nid2oid(int nid, int grp);
7318
#endif
7319
7320
#ifdef WOLFSSL_DTLS
7321
WOLFSSL_TEST_VIS int wolfSSL_DtlsUpdateWindow(word16 cur_hi, word32 cur_lo,
7322
        word16* next_hi, word32* next_lo, word32 *window);
7323
WOLFSSL_LOCAL int DtlsUpdateWindow(WOLFSSL* ssl);
7324
WOLFSSL_LOCAL void DtlsResetState(WOLFSSL *ssl);
7325
WOLFSSL_LOCAL int DtlsIgnoreError(int err);
7326
WOLFSSL_LOCAL void DtlsSetSeqNumForReply(WOLFSSL* ssl);
7327
#endif
7328
7329
#ifdef WOLFSSL_DTLS13
7330
    #ifdef WOLFSSL_API_PREFIX_MAP
7331
        #define Dtls13GetEpoch wolfSSL_Dtls13GetEpoch
7332
        #define Dtls13CheckEpoch wolfSSL_Dtls13CheckEpoch
7333
        #define Dtls13WriteAckMessage wolfSSL_Dtls13WriteAckMessage
7334
        #define Dtls13RtxAddAck wolfSSL_Dtls13RtxAddAck
7335
        #define Dtls13DoScheduledWork wolfSSL_Dtls13DoScheduledWork
7336
    #endif
7337
7338
WOLFSSL_TEST_VIS struct Dtls13Epoch* Dtls13GetEpoch(WOLFSSL* ssl,
7339
    w64wrapper epochNumber);
7340
WOLFSSL_LOCAL void Dtls13SetOlderEpochSide(WOLFSSL* ssl, w64wrapper epochNumber,
7341
    int side);
7342
WOLFSSL_LOCAL int Dtls13NewEpoch(WOLFSSL* ssl, w64wrapper epochNumber,
7343
    int side);
7344
WOLFSSL_LOCAL int Dtls13SetEpochKeys(WOLFSSL* ssl, w64wrapper epochNumber,
7345
    enum encrypt_side side);
7346
WOLFSSL_LOCAL int Dtls13GetSeq(WOLFSSL* ssl, int order, word32* seq,
7347
    byte increment);
7348
WOLFSSL_LOCAL void Dtls13RtxRemoveRecord(WOLFSSL* ssl, w64wrapper epoch,
7349
    w64wrapper seq);
7350
WOLFSSL_TEST_VIS int Dtls13DoScheduledWork(WOLFSSL* ssl);
7351
WOLFSSL_LOCAL int Dtls13DeriveSnKeys(WOLFSSL* ssl, int provision);
7352
WOLFSSL_LOCAL int Dtls13SetRecordNumberKeys(WOLFSSL* ssl,
7353
    enum encrypt_side side);
7354
7355
WOLFSSL_LOCAL int Dtls13AddHeaders(byte* output, word32 length,
7356
    enum HandShakeType hs_type, WOLFSSL* ssl);
7357
WOLFSSL_LOCAL word16 Dtls13GetHeadersLength(WOLFSSL *ssl,
7358
    enum HandShakeType type);
7359
WOLFSSL_LOCAL word16 Dtls13GetRlHeaderLength(WOLFSSL *ssl, byte is_encrypted);
7360
WOLFSSL_LOCAL int Dtls13RlAddCiphertextHeader(WOLFSSL* ssl, byte* out,
7361
    word16 length);
7362
WOLFSSL_LOCAL int Dtls13RlAddPlaintextHeader(WOLFSSL* ssl, byte* out,
7363
    enum ContentType content_type, word16 length);
7364
WOLFSSL_LOCAL int Dtls13MinimumRecordLength(WOLFSSL* ssl);
7365
WOLFSSL_LOCAL int Dtls13EncryptRecordNumber(WOLFSSL* ssl, byte* hdr,
7366
    word16 recordLength);
7367
WOLFSSL_LOCAL int Dtls13IsUnifiedHeader(byte header_flags);
7368
WOLFSSL_LOCAL int Dtls13GetUnifiedHeaderSize(WOLFSSL* ssl, const byte input,
7369
    word16* size);
7370
WOLFSSL_LOCAL int Dtls13ParseUnifiedRecordLayer(WOLFSSL* ssl, const byte* input,
7371
    word16 input_size, Dtls13UnifiedHdrInfo* hdrInfo);
7372
WOLFSSL_LOCAL int Dtls13HandshakeSend(WOLFSSL* ssl, byte* output,
7373
    word16 output_size, word16 length, enum HandShakeType handshake_type,
7374
    int hash_output);
7375
WOLFSSL_LOCAL int Dtls13RecordRecvd(WOLFSSL* ssl);
7376
WOLFSSL_TEST_VIS int Dtls13CheckEpoch(WOLFSSL* ssl, enum HandShakeType type);
7377
WOLFSSL_LOCAL int Dtls13HandshakeRecv(WOLFSSL* ssl, byte* input,
7378
    word32* inOutIdx, word32 totalSz);
7379
WOLFSSL_LOCAL int Dtls13HandshakeAddHeader(WOLFSSL* ssl, byte* output,
7380
    enum HandShakeType msg_type, word32 length);
7381
#define EE_MASK (0x3)
7382
WOLFSSL_LOCAL int Dtls13FragmentsContinue(WOLFSSL* ssl);
7383
WOLFSSL_LOCAL int DoDtls13KeyUpdateAck(WOLFSSL* ssl);
7384
WOLFSSL_LOCAL int DoDtls13Ack(WOLFSSL* ssl, const byte* input, word32 inputSize,
7385
    word32* processedSize);
7386
WOLFSSL_LOCAL int Dtls13ReconstructEpochNumber(WOLFSSL* ssl, byte epochBits,
7387
    w64wrapper* epoch);
7388
WOLFSSL_LOCAL int Dtls13ReconstructSeqNumber(WOLFSSL* ssl,
7389
    Dtls13UnifiedHdrInfo* hdrInfo, w64wrapper* out);
7390
WOLFSSL_TEST_VIS int Dtls13WriteAckMessage(WOLFSSL* ssl,
7391
    Dtls13RecordNumber* recordNumberList, word16 recordsCount, word32* length);
7392
WOLFSSL_LOCAL int SendDtls13Ack(WOLFSSL* ssl);
7393
WOLFSSL_TEST_VIS int Dtls13RtxAddAck(WOLFSSL* ssl, w64wrapper epoch, w64wrapper seq);
7394
WOLFSSL_LOCAL int Dtls13RtxProcessingCertificate(WOLFSSL* ssl, byte* input,
7395
    word32 inputSize);
7396
WOLFSSL_LOCAL int Dtls13HashHandshake(WOLFSSL* ssl, const byte* input,
7397
    word16 length);
7398
WOLFSSL_LOCAL int Dtls13HashClientHello(const WOLFSSL* ssl, byte* hash,
7399
        int* hashSz, const byte* body, word32 length, CipherSpecs* specs);
7400
WOLFSSL_LOCAL void Dtls13FreeFsmResources(WOLFSSL* ssl);
7401
WOLFSSL_LOCAL void Dtls13RtxFlushBuffered(WOLFSSL* ssl,
7402
        byte keepNewSessionTicket);
7403
WOLFSSL_LOCAL int Dtls13RtxTimeout(WOLFSSL* ssl);
7404
WOLFSSL_LOCAL int Dtls13ProcessBufferedMessages(WOLFSSL* ssl);
7405
WOLFSSL_LOCAL int Dtls13CheckAEADFailLimit(WOLFSSL* ssl);
7406
WOLFSSL_LOCAL int Dtls13UpdateWindowRecordRecvd(WOLFSSL* ssl);
7407
#endif /* WOLFSSL_DTLS13 */
7408
7409
#ifdef WOLFSSL_STATIC_EPHEMERAL
7410
WOLFSSL_LOCAL int wolfSSL_StaticEphemeralKeyLoad(WOLFSSL* ssl, int keyAlgo, void* keyPtr);
7411
#endif
7412
7413
#ifndef NO_CERTS
7414
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
7415
    defined(OPENSSL_EXTRA_X509_SMALL)
7416
WOLFSSL_LOCAL int wolfSSL_ASN1_STRING_canon(WOLFSSL_ASN1_STRING* asn_out,
7417
    const WOLFSSL_ASN1_STRING* asn_in);
7418
#endif
7419
#ifdef OPENSSL_EXTRA
7420
WOLFSSL_LOCAL int GetX509Error(int e);
7421
#endif
7422
#endif
7423
7424
#ifdef HAVE_EX_DATA_CRYPTO
7425
typedef struct CRYPTO_EX_cb_ctx {
7426
    long ctx_l;
7427
    void *ctx_ptr;
7428
    WOLFSSL_CRYPTO_EX_new* new_func;
7429
    WOLFSSL_CRYPTO_EX_free* free_func;
7430
    WOLFSSL_CRYPTO_EX_dup* dup_func;
7431
    struct CRYPTO_EX_cb_ctx* next;
7432
} CRYPTO_EX_cb_ctx;
7433
7434
WOLFSSL_TEST_VIS extern CRYPTO_EX_cb_ctx* crypto_ex_cb_ctx_session;
7435
#ifdef WOLFSSL_API_PREFIX_MAP
7436
    #define crypto_ex_cb_free wolfSSL_crypto_ex_cb_free
7437
#endif
7438
WOLFSSL_TEST_VIS void crypto_ex_cb_free(CRYPTO_EX_cb_ctx* cb_ctx);
7439
WOLFSSL_LOCAL void crypto_ex_cb_setup_new_data(void *new_obj,
7440
        CRYPTO_EX_cb_ctx* cb_ctx, WOLFSSL_CRYPTO_EX_DATA* ex_data);
7441
WOLFSSL_LOCAL void crypto_ex_cb_free_data(void *obj, CRYPTO_EX_cb_ctx* cb_ctx,
7442
        WOLFSSL_CRYPTO_EX_DATA* ex_data);
7443
WOLFSSL_LOCAL int crypto_ex_cb_dup_data(const WOLFSSL_CRYPTO_EX_DATA *in,
7444
        WOLFSSL_CRYPTO_EX_DATA *out, CRYPTO_EX_cb_ctx* cb_ctx);
7445
WOLFSSL_LOCAL int wolfssl_local_get_ex_new_index(int class_index, long ctx_l,
7446
        void* ctx_ptr, WOLFSSL_CRYPTO_EX_new* new_func,
7447
        WOLFSSL_CRYPTO_EX_dup* dup_func, WOLFSSL_CRYPTO_EX_free* free_func);
7448
#endif /* HAVE_EX_DATA_CRYPTO */
7449
7450
WOLFSSL_LOCAL WC_RNG* wolfssl_get_global_rng(void);
7451
WOLFSSL_LOCAL WC_RNG* wolfssl_make_global_rng(void);
7452
7453
#if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
7454
#if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_PEM_TO_DER)
7455
WOLFSSL_LOCAL int EncryptDerKey(byte *der, int *derSz,
7456
    const WOLFSSL_EVP_CIPHER* cipher, unsigned char* passwd, int passwdSz,
7457
    byte **cipherInfo, int maxDerSz, int hashType);
7458
#endif
7459
#endif
7460
7461
#if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
7462
WOLFSSL_LOCAL int wolfSSL_RSA_To_Der(WOLFSSL_RSA* rsa, byte** outBuf,
7463
    int publicKey, void* heap);
7464
#endif
7465
7466
#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
7467
    || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || defined(HAVE_SECRET_CALLBACK)
7468
WOLFSSL_LOCAL int wolfSSL_SSL_do_handshake_internal(WOLFSSL *s);
7469
#endif
7470
7471
#ifdef WOLFSSL_QUIC
7472
#define WOLFSSL_IS_QUIC(s)  (((s) != NULL) && ((s)->quic.method != NULL))
7473
WOLFSSL_LOCAL int wolfSSL_quic_receive(WOLFSSL* ssl, byte* buf, word32 sz);
7474
WOLFSSL_LOCAL int wolfSSL_quic_send(WOLFSSL* ssl);
7475
WOLFSSL_LOCAL void wolfSSL_quic_clear(WOLFSSL* ssl);
7476
WOLFSSL_LOCAL void wolfSSL_quic_free(WOLFSSL* ssl);
7477
WOLFSSL_LOCAL int wolfSSL_quic_forward_secrets(WOLFSSL *ssl,
7478
                                               int ktype, int side);
7479
WOLFSSL_LOCAL int wolfSSL_quic_keys_active(WOLFSSL* ssl, enum encrypt_side side);
7480
7481
#else
7482
0
#define WOLFSSL_IS_QUIC(s) 0
7483
#endif /* WOLFSSL_QUIC (else) */
7484
7485
#if defined(SHOW_SECRETS) && defined(WOLFSSL_SSLKEYLOGFILE)
7486
WOLFSSL_LOCAL int tls13ShowSecrets(WOLFSSL* ssl, int id, const unsigned char* secret,
7487
    int secretSz, void* ctx);
7488
#endif
7489
7490
#if defined(SHOW_SECRETS)
7491
WOLFSSL_LOCAL int tlsShowSecrets(WOLFSSL* ssl, void* secret,
7492
        int secretSz, void* ctx);
7493
#endif
7494
7495
/* Optional Pre-Master-Secret logging for Wireshark */
7496
#if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
7497
#ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
7498
    #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
7499
#endif
7500
#endif
7501
7502
#if defined(WOLFSSL_TLS13) && !defined(NO_PSK)
7503
WOLFSSL_LOCAL int FindPskSuite(const WOLFSSL* ssl, PreSharedKey* psk,
7504
        byte* psk_key, word32* psk_keySz, const byte* suite, int* found,
7505
        byte* foundSuite);
7506
#endif
7507
7508
WOLFSSL_LOCAL int wolfSSL_GetHmacType_ex(CipherSpecs* specs);
7509
7510
#if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
7511
WOLFSSL_LOCAL int CreateCookieExt(const WOLFSSL* ssl, byte* hash,
7512
                                  word16 hashSz, TLSX** exts,
7513
                                  byte cipherSuite0, byte cipherSuite);
7514
#endif
7515
7516
WOLFSSL_LOCAL int TranslateErrorToAlert(int err);
7517
7518
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
7519
WOLFSSL_LOCAL void* wolfssl_sk_pop_type(WOLFSSL_STACK* sk,
7520
                                        WOLF_STACK_TYPE type);
7521
WOLFSSL_LOCAL void* wolfSSL_sk_pop_node(WOLFSSL_STACK* sk, int idx);
7522
WOLFSSL_LOCAL WOLFSSL_STACK* wolfssl_sk_new_type(WOLF_STACK_TYPE type);
7523
WOLFSSL_LOCAL WOLFSSL_STACK* wolfssl_sk_new_type_ex(WOLF_STACK_TYPE type,
7524
        void* heap);
7525
7526
WOLFSSL_LOCAL int wolfssl_asn1_obj_set(WOLFSSL_ASN1_OBJECT* obj,
7527
        const byte* der, word32 len, int addHdr);
7528
#endif
7529
7530
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
7531
WOLFSSL_LOCAL int pkcs8_encode(WOLFSSL_EVP_PKEY* pkey, byte* key,
7532
        word32* keySz);
7533
WOLFSSL_LOCAL int pkcs8_encrypt(WOLFSSL_EVP_PKEY* pkey,
7534
        const WOLFSSL_EVP_CIPHER* enc, char* passwd, int passwdSz, byte* key,
7535
        word32* keySz);
7536
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
7537
7538
#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && !defined(NO_BIO)
7539
WOLFSSL_LOCAL int wolfSSL_PEM_X509_X509_CRL_X509_PKEY_read_bio(
7540
        WOLFSSL_BIO* bio, wc_pem_password_cb* cb, WOLFSSL_X509** x509,
7541
        WOLFSSL_X509_CRL** crl, WOLFSSL_X509_PKEY** x_pkey);
7542
#endif
7543
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
7544
WOLFSSL_LOCAL void wolfSSL_X509_PKEY_free(WOLFSSL_X509_PKEY* xPkey);
7545
#endif
7546
7547
WOLFSSL_LOCAL void wolfssl_local_MaybeCheckAlertOnErr(WOLFSSL* ssl, int err);
7548
7549
#ifdef __cplusplus
7550
    }  /* extern "C" */
7551
#endif
7552
7553
#endif /* wolfSSL_INT_H */