Coverage Report

Created: 2025-07-23 06:59

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