1 /* api.c API unit tests
2  *
3  * Copyright (C) 2006-2021 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 2 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 /* For AES-CBC, input lengths can optionally be validated to be a
23  * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
24  * also available via the configure option --enable-aescbc-length-checks.
25  */
26 
27 
28 /*----------------------------------------------------------------------------*
29  | Includes
30  *----------------------------------------------------------------------------*/
31 
32 #ifdef HAVE_CONFIG_H
33     #include <config.h>
34 #endif
35 
36 #include <wolfssl/wolfcrypt/settings.h>
37 
38 #ifndef FOURK_BUF
39     #define FOURK_BUF 4096
40 #endif
41 #ifndef TWOK_BUF
42     #define TWOK_BUF 2048
43 #endif
44 #ifndef ONEK_BUF
45     #define ONEK_BUF 1024
46 #endif
47 #if defined(WOLFSSL_STATIC_MEMORY)
48     #include <wolfssl/wolfcrypt/memory.h>
49 #endif /* WOLFSSL_STATIC_MEMORY */
50 #ifndef HEAP_HINT
51     #define HEAP_HINT NULL
52 #endif /* WOLFSSL_STAIC_MEMORY */
53 #ifdef WOLFSSL_ASNC_CRYPT
54     #include <wolfssl/wolfcrypt/async.h>
55 #endif
56 #ifdef HAVE_ECC
57     #include <wolfssl/wolfcrypt/ecc.h>   /* wc_ecc_fp_free */
58     #ifndef ECC_ASN963_MAX_BUF_SZ
59         #define ECC_ASN963_MAX_BUF_SZ 133
60     #endif
61     #ifndef ECC_PRIV_KEY_BUF
62         #define ECC_PRIV_KEY_BUF 66  /* For non user defined curves. */
63     #endif
64     /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
65     /* logic to choose right key ECC size */
66     #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
67         #define KEY14 14
68     #else
69         #define KEY14 32
70     #endif
71     #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
72         #define KEY16 16
73     #else
74         #define KEY16 32
75     #endif
76     #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
77         #define KEY20 20
78     #else
79         #define KEY20 32
80     #endif
81     #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
82         #define KEY24 24
83     #else
84         #define KEY24 32
85     #endif
86     #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
87         #define KEY28 28
88     #else
89         #define KEY28 32
90     #endif
91     #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
92         #define KEY30 30
93     #else
94         #define KEY30 32
95     #endif
96     #define KEY32 32
97     #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
98         #define KEY40 40
99     #else
100         #define KEY40 32
101     #endif
102     #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
103         #define KEY48 48
104     #else
105         #define KEY48 32
106     #endif
107     #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
108         #define KEY64 64
109     #else
110         #define KEY64 32
111     #endif
112 
113     #if !defined(HAVE_COMP_KEY)
114         #if !defined(NOCOMP)
115             #define NOCOMP 0
116         #endif
117     #else
118         #if !defined(COMP)
119             #define COMP 1
120         #endif
121     #endif
122     #if !defined(DER_SZ)
123         #define DER_SZ(ks) (ks * 2 + 1)
124     #endif
125 #endif
126 #ifndef NO_ASN
127     #include <wolfssl/wolfcrypt/asn_public.h>
128 #endif
129 #include <wolfssl/error-ssl.h>
130 
131 #include <stdlib.h>
132 #include <wolfssl/ssl.h>  /* compatibility layer */
133 #include <wolfssl/test.h>
134 #include <tests/unit.h>
135 #include "examples/server/server.h"
136      /* for testing compatibility layer callbacks */
137 
138 #ifndef NO_MD5
139     #include <wolfssl/wolfcrypt/md5.h>
140 #endif
141 #ifndef NO_SHA
142     #include <wolfssl/wolfcrypt/sha.h>
143 #endif
144 #ifndef NO_SHA256
145     #include <wolfssl/wolfcrypt/sha256.h>
146 #endif
147 #ifdef WOLFSSL_SHA512
148     #include <wolfssl/wolfcrypt/sha512.h>
149 #endif
150 #ifdef WOLFSSL_SHA384
151     #include <wolfssl/wolfcrypt/sha512.h>
152 #endif
153 
154 #ifdef WOLFSSL_SHA3
155     #include <wolfssl/wolfcrypt/sha3.h>
156     #ifndef HEAP_HINT
157         #define HEAP_HINT   NULL
158     #endif
159 #endif
160 
161 #ifndef NO_AES
162     #include <wolfssl/wolfcrypt/aes.h>
163     #ifdef HAVE_AES_DECRYPT
164         #include <wolfssl/wolfcrypt/wc_encrypt.h>
165     #endif
166 #endif
167 #ifdef WOLFSSL_RIPEMD
168     #include <wolfssl/wolfcrypt/ripemd.h>
169 #endif
170 #ifdef HAVE_IDEA
171     #include <wolfssl/wolfcrypt/idea.h>
172 #endif
173 #ifndef NO_DES3
174     #include <wolfssl/wolfcrypt/des3.h>
175     #include <wolfssl/wolfcrypt/wc_encrypt.h>
176 #endif
177 #ifdef WC_RC2
178     #include <wolfssl/wolfcrypt/rc2.h>
179 #endif
180 
181 #ifndef NO_HMAC
182     #include <wolfssl/wolfcrypt/hmac.h>
183 #endif
184 
185 #ifdef HAVE_CHACHA
186     #include <wolfssl/wolfcrypt/chacha.h>
187 #endif
188 
189 #ifdef HAVE_POLY1305
190     #include <wolfssl/wolfcrypt/poly1305.h>
191 #endif
192 
193 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
194     #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
195 #endif
196 
197 #ifdef HAVE_CAMELLIA
198     #include <wolfssl/wolfcrypt/camellia.h>
199 #endif
200 
201 #ifndef NO_RABBIT
202     #include <wolfssl/wolfcrypt/rabbit.h>
203 #endif
204 
205 #ifndef NO_RC4
206     #include <wolfssl/wolfcrypt/arc4.h>
207 #endif
208 
209 #ifdef HAVE_BLAKE2
210     #include <wolfssl/wolfcrypt/blake2.h>
211 #endif
212 
213 #include <wolfssl/wolfcrypt/hash.h>
214 #ifndef NO_RSA
215     #include <wolfssl/wolfcrypt/rsa.h>
216 
217     #define FOURK_BUF 4096
218     #define GEN_BUF  294
219 
220     #ifndef USER_CRYPTO_ERROR
221         #define USER_CRYPTO_ERROR -101 /* error returned by IPP lib. */
222     #endif
223 #endif
224 
225 #ifndef NO_SIG_WRAPPER
226     #include <wolfssl/wolfcrypt/signature.h>
227 #endif
228 
229 
230 #ifdef HAVE_AESCCM
231     #include <wolfssl/wolfcrypt/aes.h>
232 #endif
233 
234 #ifdef HAVE_HC128
235     #include <wolfssl/wolfcrypt/hc128.h>
236 #endif
237 
238 #ifdef HAVE_PKCS7
239     #include <wolfssl/wolfcrypt/pkcs7.h>
240     #include <wolfssl/wolfcrypt/asn.h>
241     #ifdef HAVE_LIBZ
242     #include <wolfssl/wolfcrypt/compress.h>
243     #endif
244 #endif
245 
246 #ifdef WOLFSSL_SMALL_CERT_VERIFY
247     #include <wolfssl/wolfcrypt/asn.h>
248 #endif
249 
250 #ifndef NO_DSA
251     #include <wolfssl/wolfcrypt/dsa.h>
252     #ifndef ONEK_BUF
253         #define ONEK_BUF 1024
254     #endif
255     #ifndef TWOK_BUF
256         #define TWOK_BUF 2048
257     #endif
258     #ifndef FOURK_BUF
259         #define FOURK_BUF 4096
260     #endif
261     #ifndef DSA_SIG_SIZE
262         #define DSA_SIG_SIZE 40
263     #endif
264     #ifndef MAX_DSA_PARAM_SIZE
265         #define MAX_DSA_PARAM_SIZE 256
266     #endif
267 #endif
268 
269 #ifdef WOLFSSL_CMAC
270     #include <wolfssl/wolfcrypt/cmac.h>
271 #endif
272 
273 #ifdef HAVE_ED25519
274     #include <wolfssl/wolfcrypt/ed25519.h>
275 #endif
276 #ifdef HAVE_CURVE25519
277     #include <wolfssl/wolfcrypt/curve25519.h>
278 #endif
279 #ifdef HAVE_ED448
280     #include <wolfssl/wolfcrypt/ed448.h>
281 #endif
282 #ifdef HAVE_CURVE448
283     #include <wolfssl/wolfcrypt/curve448.h>
284 #endif
285 
286 #ifdef HAVE_PKCS12
287     #include <wolfssl/wolfcrypt/pkcs12.h>
288 #endif
289 
290 #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
291     #include <wolfssl/openssl/ssl.h>
292     #ifndef NO_ASN
293         /* for ASN_COMMON_NAME DN_tags enum */
294         #include <wolfssl/wolfcrypt/asn.h>
295     #endif
296     #ifdef HAVE_OCSP
297         #include <wolfssl/openssl/ocsp.h>
298     #endif
299 #endif
300 #ifdef OPENSSL_EXTRA
301     #include <wolfssl/openssl/cmac.h>
302     #include <wolfssl/openssl/x509v3.h>
303     #include <wolfssl/openssl/asn1.h>
304     #include <wolfssl/openssl/crypto.h>
305     #include <wolfssl/openssl/pkcs12.h>
306     #include <wolfssl/openssl/evp.h>
307     #include <wolfssl/openssl/dh.h>
308     #include <wolfssl/openssl/bn.h>
309     #include <wolfssl/openssl/buffer.h>
310     #include <wolfssl/openssl/pem.h>
311     #include <wolfssl/openssl/ec.h>
312     #include <wolfssl/openssl/engine.h>
313     #include <wolfssl/openssl/hmac.h>
314     #include <wolfssl/openssl/objects.h>
315     #include <wolfssl/openssl/rand.h>
316     #include <wolfssl/openssl/modes.h>
317 #ifdef OPENSSL_ALL
318     #include <wolfssl/openssl/txt_db.h>
319     #include <wolfssl/openssl/lhash.h>
320 #endif
321 #ifndef NO_AES
322     #include <wolfssl/openssl/aes.h>
323 #endif
324 #ifndef NO_DES3
325     #include <wolfssl/openssl/des.h>
326 #endif
327 #ifdef HAVE_ECC
328     #include <wolfssl/openssl/ecdsa.h>
329 #endif
330 #ifdef HAVE_PKCS7
331     #include <wolfssl/openssl/pkcs7.h>
332 #endif
333 #ifdef HAVE_ED25519
334     #include <wolfssl/openssl/ed25519.h>
335 #endif
336 #ifdef HAVE_ED448
337     #include <wolfssl/openssl/ed448.h>
338 #endif
339 #endif /* OPENSSL_EXTRA */
340 
341 #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
342     && !defined(NO_SHA256) && !defined(RC_NO_RNG)
343         #include <wolfssl/wolfcrypt/srp.h>
344 #endif
345 
346 #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
347     defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
348     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
349     /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
350      * or for setting authKeyIdSrc in WOLFSSL_X509 */
351 #include "wolfssl/internal.h"
352 #endif
353 
354 /* force enable test buffers */
355 #ifndef USE_CERT_BUFFERS_2048
356     #define USE_CERT_BUFFERS_2048
357 #endif
358 #ifndef USE_CERT_BUFFERS_256
359     #define USE_CERT_BUFFERS_256
360 #endif
361 #include <wolfssl/certs_test.h>
362 
363 
364 typedef struct testVector {
365     const char* input;
366     const char* output;
367     size_t inLen;
368     size_t outLen;
369 
370 } testVector;
371 
372 #if defined(HAVE_PKCS7)
373     typedef struct {
374         const byte* content;
375         word32      contentSz;
376         int         contentOID;
377         int         encryptOID;
378         int         keyWrapOID;
379         int         keyAgreeOID;
380         byte*       cert;
381         size_t      certSz;
382         byte*       privateKey;
383         word32      privateKeySz;
384     } pkcs7EnvelopedVector;
385 
386     #ifndef NO_PKCS7_ENCRYPTED_DATA
387         typedef struct {
388             const byte*     content;
389             word32          contentSz;
390             int             contentOID;
391             int             encryptOID;
392             byte*           encryptionKey;
393             word32          encryptionKeySz;
394         } pkcs7EncryptedVector;
395     #endif
396 #endif /* HAVE_PKCS7 */
397 
398 
399 /*----------------------------------------------------------------------------*
400  | Constants
401  *----------------------------------------------------------------------------*/
402 
403 #define TEST_SUCCESS    (1)
404 #define TEST_FAIL       (0)
405 
406 #define testingFmt "   %s:"
407 #define resultFmt  " %s\n"
408 static const char* passed = "passed";
409 static const char* failed = "failed";
410 
411 #define TEST_STRING    "Everyone gets Friday off."
412 #define TEST_STRING_SZ 25
413 
414 #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
415     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
416 #define TEST_RSA_BITS 1024
417 #else
418 #define TEST_RSA_BITS 2048
419 #endif
420 #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
421 
422 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
423     (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
424     static const char* bogusFile  =
425     #ifdef _WIN32
426         "NUL"
427     #else
428         "/dev/null"
429     #endif
430     ;
431 #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
432 
433 enum {
434     TESTING_RSA = 1,
435     TESTING_ECC = 2
436 };
437 
438 #ifdef WOLFSSL_QNX_CAAM
439 #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
440 static int devId = WOLFSSL_CAAM_DEVID;
441 #else
442 static int devId = INVALID_DEVID;
443 #endif
444 
445 
446 /*----------------------------------------------------------------------------*
447  | Setup
448  *----------------------------------------------------------------------------*/
449 
test_wolfSSL_Init(void)450 static int test_wolfSSL_Init(void)
451 {
452     int result;
453 
454     printf(testingFmt, "wolfSSL_Init()");
455     result = wolfSSL_Init();
456     printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
457 
458     return result;
459 }
460 
461 
test_wolfSSL_Cleanup(void)462 static int test_wolfSSL_Cleanup(void)
463 {
464     int result;
465 
466     printf(testingFmt, "wolfSSL_Cleanup()");
467     result = wolfSSL_Cleanup();
468     printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
469 
470     return result;
471 }
472 
473 
474 /*  Initialize the wolfCrypt state.
475  *  POST: 0 success.
476  */
test_wolfCrypt_Init(void)477 static int test_wolfCrypt_Init(void)
478 {
479     int result;
480 
481     printf(testingFmt, "wolfCrypt_Init()");
482     result = wolfCrypt_Init();
483     printf(resultFmt, result == 0 ? passed : failed);
484 
485     return result;
486 
487 } /* END test_wolfCrypt_Init */
488 
489 /*----------------------------------------------------------------------------*
490  | Platform dependent function test
491  *----------------------------------------------------------------------------*/
test_fileAccess(void)492  static int test_fileAccess(void)
493 {
494 #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
495     const char *fname[] = {
496     svrCertFile, svrKeyFile, caCertFile,
497     eccCertFile, eccKeyFile, eccRsaCertFile,
498     cliCertFile, cliCertDerFile, cliKeyFile,
499     dhParamFile,
500     cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
501     cliEdCertFile, cliEdKeyFile, caEdCertFile,
502     NULL
503     };
504      const char derfile[] = "./certs/server-cert.der";
505     XFILE f;
506     size_t sz;
507     byte *buff;
508     int i;
509 
510     printf(testingFmt, "test_fileAccess()");
511 
512     AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
513      for(i=0; fname[i] != NULL ; i++){
514         AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
515         XFCLOSE(f);
516     }
517 
518     AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
519     AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
520     sz = (size_t) XFTELL(f);
521     XREWIND(f);
522     AssertTrue(sz == sizeof_server_cert_der_2048);
523     AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
524     AssertTrue(XFREAD(buff, 1, sz, f) == sz);
525     XMEMCMP(server_cert_der_2048, buff, sz);
526     printf(resultFmt, passed);
527 #endif
528     return WOLFSSL_SUCCESS;
529 }
530 
531 /*----------------------------------------------------------------------------*
532  | Method Allocators
533  *----------------------------------------------------------------------------*/
534 
test_wolfSSL_Method_Allocators(void)535 static void test_wolfSSL_Method_Allocators(void)
536 {
537     #define TEST_METHOD_ALLOCATOR(allocator, condition) \
538         do {                                            \
539             WOLFSSL_METHOD *method;                      \
540             condition(method = allocator());            \
541             XFREE(method, 0, DYNAMIC_TYPE_METHOD);      \
542         } while(0)
543 
544     #define TEST_VALID_METHOD_ALLOCATOR(a) \
545             TEST_METHOD_ALLOCATOR(a, AssertNotNull)
546 
547     #define TEST_INVALID_METHOD_ALLOCATOR(a) \
548             TEST_METHOD_ALLOCATOR(a, AssertNull)
549 
550 #ifndef NO_OLD_TLS
551     #ifdef WOLFSSL_ALLOW_SSLV3
552         #ifndef NO_WOLFSSL_SERVER
553         TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
554         #endif
555         #ifndef NO_WOLFSSL_CLIENT
556         TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
557         #endif
558     #endif
559     #ifdef WOLFSSL_ALLOW_TLSV10
560         #ifndef NO_WOLFSSL_SERVER
561         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
562         #endif
563         #ifndef NO_WOLFSSL_CLIENT
564         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
565         #endif
566     #endif
567     #ifndef NO_WOLFSSL_SERVER
568         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
569     #endif
570     #ifndef NO_WOLFSSL_CLIENT
571         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
572     #endif
573 #endif /* !NO_OLD_TLS */
574 
575 #ifndef WOLFSSL_NO_TLS12
576     #ifndef NO_WOLFSSL_SERVER
577         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
578     #endif
579     #ifndef NO_WOLFSSL_CLIENT
580         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
581     #endif
582 #endif /* !WOLFSSL_NO_TLS12 */
583 
584 #ifdef WOLFSSL_TLS13
585     #ifndef NO_WOLFSSL_SERVER
586         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
587     #endif
588     #ifndef NO_WOLFSSL_CLIENT
589         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
590     #endif
591 #endif /* WOLFSSL_TLS13 */
592 
593 #ifndef NO_WOLFSSL_SERVER
594     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
595 #endif
596 #ifndef NO_WOLFSSL_CLIENT
597     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
598 #endif
599 
600 #ifdef WOLFSSL_DTLS
601     #ifndef NO_OLD_TLS
602         #ifndef NO_WOLFSSL_SERVER
603         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
604         #endif
605         #ifndef NO_WOLFSSL_CLIENT
606         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
607         #endif
608     #endif
609     #ifndef WOLFSSL_NO_TLS12
610         #ifndef NO_WOLFSSL_SERVER
611         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
612         #endif
613         #ifndef NO_WOLFSSL_CLIENT
614         TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
615         #endif
616     #endif
617 #endif /* WOLFSSL_DTLS */
618 
619 #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
620     /* Stubs */
621     #ifndef NO_WOLFSSL_SERVER
622         TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
623     #endif
624     #ifndef NO_WOLFSSL_CLIENT
625         TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
626     #endif
627 #endif
628 
629     /* Test Either Method (client or server) */
630 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
631     TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
632     #ifndef NO_OLD_TLS
633         #ifdef WOLFSSL_ALLOW_TLSV10
634             TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
635         #endif
636         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
637     #endif /* !NO_OLD_TLS */
638     #ifndef WOLFSSL_NO_TLS12
639         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
640     #endif /* !WOLFSSL_NO_TLS12 */
641     #ifdef WOLFSSL_TLS13
642         TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
643     #endif /* WOLFSSL_TLS13 */
644     #ifdef WOLFSSL_DTLS
645         TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
646         #ifndef NO_OLD_TLS
647             TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
648         #endif /* !NO_OLD_TLS */
649         #ifndef WOLFSSL_NO_TLS12
650             TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
651         #endif /* !WOLFSSL_NO_TLS12 */
652     #endif /* WOLFSSL_DTLS */
653 #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
654 }
655 
656 
657 /*----------------------------------------------------------------------------*
658  | Context
659  *----------------------------------------------------------------------------*/
660 #ifndef NO_WOLFSSL_SERVER
test_wolfSSL_CTX_new(WOLFSSL_METHOD * method)661 static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
662 {
663     WOLFSSL_CTX *ctx;
664 
665     AssertNull(ctx = wolfSSL_CTX_new(NULL));
666 
667     AssertNotNull(method);
668     AssertNotNull(ctx = wolfSSL_CTX_new(method));
669     wolfSSL_CTX_free(ctx);
670 }
671 #endif
672 
673 #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
674 (!defined(NO_RSA) || defined(HAVE_ECC))
test_for_double_Free(void)675 static void test_for_double_Free(void)
676 {
677     WOLFSSL_CTX* ctx;
678     WOLFSSL*     ssl;
679     int skipTest = 0;
680     const char* testCertFile;
681     const char* testKeyFile;
682     char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
683 ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
684 "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
685 "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
686 "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
687 "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
688 "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
689 "NULL-SHA:HC128-MD5:HC128-SHA:RABBIT-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
690 "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
691 "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
692 "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
693 ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
694 "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
695 ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
696 "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
697 "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
698 "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
699 "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
700 "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
701 "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
702 "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
703 "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
704 "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
705 "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
706 "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
707 "LY1305-OLD:IDEA-CBC-SHA:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
708 "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
709 "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
710 "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
711 "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
712 #ifndef NO_RSA
713         testCertFile = svrCertFile;
714         testKeyFile = svrKeyFile;
715 #elif defined(HAVE_ECC)
716         testCertFile = eccCertFile;
717         testKeyFile = eccKeyFile;
718 #else
719     skipTest = 1;
720 #endif
721 
722     if (skipTest != 1) {
723 #ifndef NO_WOLFSSL_SERVER
724         ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
725         AssertNotNull(ctx);
726 #else
727         ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
728         AssertNotNull(ctx);
729 #endif
730         AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
731         AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
732         ssl = wolfSSL_new(ctx);
733         AssertNotNull(ssl);
734 
735         /* First test freeing SSL, then CTX */
736         wolfSSL_free(ssl);
737         wolfSSL_CTX_free(ctx);
738 
739 #ifndef NO_WOLFSSL_CLIENT
740         ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
741         AssertNotNull(ctx);
742 #else
743         ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
744         AssertNotNull(ctx);
745 #endif
746         AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
747         AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
748         ssl = wolfSSL_new(ctx);
749         AssertNotNull(ssl);
750 
751         /* Next test freeing CTX then SSL */
752         wolfSSL_CTX_free(ctx);
753         wolfSSL_free(ssl);
754 
755 #ifndef NO_WOLFSSL_SERVER
756         ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
757         AssertNotNull(ctx);
758 #else
759         ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
760         AssertNotNull(ctx);
761 #endif
762         /* Test setting ciphers at ctx level */
763         AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
764         AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
765         AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
766 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
767         defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
768         /* only update TLSv13 suites */
769         AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
770 #endif
771 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
772     !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
773     defined(WOLFSSL_AES_128) && !defined(NO_RSA)
774         /* only update pre-TLSv13 suites */
775         AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
776 #endif
777         AssertNotNull(ssl = wolfSSL_new(ctx));
778         wolfSSL_CTX_free(ctx);
779         wolfSSL_free(ssl);
780 
781 #ifndef NO_WOLFSSL_CLIENT
782         ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
783         AssertNotNull(ctx);
784 #else
785         ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
786         AssertNotNull(ctx);
787 #endif
788         AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
789         AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
790         ssl = wolfSSL_new(ctx);
791         AssertNotNull(ssl);
792         /* test setting ciphers at SSL level */
793         AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
794 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
795         defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
796         /* only update TLSv13 suites */
797         AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
798 #endif
799 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
800     !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
801     defined(WOLFSSL_AES_128) && !defined(NO_RSA)
802         /* only update pre-TLSv13 suites */
803         AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
804 #endif
805         wolfSSL_CTX_free(ctx);
806         wolfSSL_free(ssl);
807     }
808 }
809 #endif
810 
811 
test_wolfSSL_CTX_use_certificate_file(void)812 static void test_wolfSSL_CTX_use_certificate_file(void)
813 {
814 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
815     WOLFSSL_CTX *ctx;
816 
817     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
818 
819     /* invalid context */
820     AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
821                                                              WOLFSSL_FILETYPE_PEM));
822     /* invalid cert file */
823     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
824                                                              WOLFSSL_FILETYPE_PEM));
825     /* invalid cert type */
826     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
827 
828 #ifdef NO_RSA
829     /* rsa needed */
830     AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
831 #else
832     /* success */
833     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
834 #endif
835 
836     wolfSSL_CTX_free(ctx);
837 #endif
838 }
839 
840 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
test_wolfSSL_CTX_use_certificate_ASN1(void)841 static int test_wolfSSL_CTX_use_certificate_ASN1(void)
842 {
843 #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
844     WOLFSSL_CTX*            ctx;
845     int                     ret;
846 
847     printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
848     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
849 
850     ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
851                 server_cert_der_2048);
852 
853     printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
854     wolfSSL_CTX_free(ctx);
855 
856     return ret;
857 #else
858     return WOLFSSL_SUCCESS;
859 #endif
860 }
861 #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
862 
863 /*  Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
864  *  context using buffer.
865  *  PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
866  *  --enable-testcert flag.
867  */
test_wolfSSL_CTX_use_certificate_buffer(void)868 static int test_wolfSSL_CTX_use_certificate_buffer(void)
869 {
870     #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
871             !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
872         WOLFSSL_CTX*            ctx;
873         int                     ret;
874 
875         printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
876         AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
877 
878         ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
879                     sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
880 
881         printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
882         wolfSSL_CTX_free(ctx);
883 
884         return ret;
885     #else
886         return WOLFSSL_SUCCESS;
887     #endif
888 
889 } /*END test_wolfSSL_CTX_use_certificate_buffer*/
890 
test_wolfSSL_CTX_use_PrivateKey_file(void)891 static void test_wolfSSL_CTX_use_PrivateKey_file(void)
892 {
893 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
894     WOLFSSL_CTX *ctx;
895 
896     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
897 
898     /* invalid context */
899     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
900                                                              WOLFSSL_FILETYPE_PEM));
901     /* invalid key file */
902     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
903                                                              WOLFSSL_FILETYPE_PEM));
904     /* invalid key type */
905     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
906 
907     /* success */
908 #ifdef NO_RSA
909     /* rsa needed */
910     AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
911 #else
912     /* success */
913     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
914 #endif
915 
916     wolfSSL_CTX_free(ctx);
917 #endif
918 }
919 
920 
921 /* test both file and buffer versions along with unloading trusted peer certs */
test_wolfSSL_CTX_trust_peer_cert(void)922 static void test_wolfSSL_CTX_trust_peer_cert(void)
923 {
924 #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
925     !defined(NO_WOLFSSL_CLIENT)
926     WOLFSSL_CTX *ctx;
927 
928     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
929 
930 #if !defined(NO_FILESYSTEM)
931     /* invalid file */
932     assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
933                                               WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
934     assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
935                                               WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
936     assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
937                                              WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
938 
939     /* success */
940     assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
941                                                                 == WOLFSSL_SUCCESS);
942 
943     /* unload cert */
944     assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
945     assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
946 #endif
947 
948     /* Test of loading certs from buffers */
949 
950     /* invalid buffer */
951     assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
952                                              WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
953 
954     /* success */
955 #ifdef USE_CERT_BUFFERS_1024
956     assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
957                 sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
958 #endif
959 #ifdef USE_CERT_BUFFERS_2048
960     assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
961                 sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
962 #endif
963 
964     /* unload cert */
965     assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
966     assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
967 
968     wolfSSL_CTX_free(ctx);
969 #endif
970 }
971 
972 
test_wolfSSL_CTX_load_verify_locations(void)973 static void test_wolfSSL_CTX_load_verify_locations(void)
974 {
975 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
976     WOLFSSL_CTX *ctx;
977 #ifndef NO_RSA
978     WOLFSSL_CERT_MANAGER* cm;
979 #ifdef PERSIST_CERT_CACHE
980     int cacheSz;
981 #endif
982 #endif
983 #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
984     const char* load_certs_path = "./certs/external";
985     const char* load_no_certs_path = "./examples";
986     const char* load_expired_path = "./certs/test/expired";
987 #endif
988 
989     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
990 
991     /* invalid arguments */
992     AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
993     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
994 
995     /* invalid ca file */
996     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
997      WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
998 
999 
1000 #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
1001   (defined(WOLFSSL_QT) && \
1002   !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
1003     /* invalid path */
1004     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
1005      WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
1006 #endif
1007 
1008     /* load ca cert */
1009 #ifdef NO_RSA
1010     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
1011      WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
1012 #else /* Skip the following test without RSA certs. */
1013     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
1014 
1015 #ifdef PERSIST_CERT_CACHE
1016     /* Get cert cache size */
1017     cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
1018 #endif
1019     /* Test unloading CA's */
1020     AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
1021 
1022 #ifdef PERSIST_CERT_CACHE
1023     /* Verify no certs (result is less than cacheSz) */
1024     AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
1025 #endif
1026 
1027     /* load ca cert again */
1028     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
1029 
1030     /* Test getting CERT_MANAGER */
1031     AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
1032 
1033     /* Test unloading CA's using CM */
1034     AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
1035 
1036 #ifdef PERSIST_CERT_CACHE
1037     /* Verify no certs (result is less than cacheSz) */
1038     AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
1039 #endif
1040 #endif
1041 
1042 #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
1043     /* Test loading CA certificates using a path */
1044     #ifdef NO_RSA
1045     /* failure here okay since certs in external directory are RSA */
1046     AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
1047         WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
1048     #else
1049     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
1050         WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
1051     #endif
1052 
1053     /* Test loading path with no files */
1054     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
1055         WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
1056 
1057     /* Test loading expired CA certificates */
1058     #ifdef NO_RSA
1059     AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
1060         WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
1061         WOLFSSL_SUCCESS);
1062     #else
1063     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
1064         WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
1065         WOLFSSL_SUCCESS);
1066     #endif
1067 
1068     /* Test loading CA certificates and ignoring all errors */
1069     #ifdef NO_RSA
1070     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
1071         WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
1072     #else
1073     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
1074         WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
1075     #endif
1076 #endif
1077 
1078     wolfSSL_CTX_free(ctx);
1079 #endif
1080 }
1081 
1082 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
test_cm_load_ca_buffer(const byte * cert_buf,size_t cert_sz,int file_type)1083 static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
1084 {
1085     int ret;
1086     WOLFSSL_CERT_MANAGER* cm;
1087 
1088     cm = wolfSSL_CertManagerNew();
1089     if (cm == NULL) {
1090         printf("test_cm_load_ca failed\n");
1091         return -1;
1092     }
1093 
1094     ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
1095 
1096     wolfSSL_CertManagerFree(cm);
1097 
1098     return ret;
1099 }
1100 
test_cm_load_ca_file(const char * ca_cert_file)1101 static int test_cm_load_ca_file(const char* ca_cert_file)
1102 {
1103     int ret = 0;
1104     byte* cert_buf = NULL;
1105     size_t cert_sz = 0;
1106 #if defined(WOLFSSL_PEM_TO_DER)
1107     DerBuffer* pDer = NULL;
1108 #endif
1109 
1110     ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
1111     if (ret == 0) {
1112         /* normal test */
1113         ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
1114 
1115         if (ret == WOLFSSL_SUCCESS) {
1116             /* test including null terminator in length */
1117             byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
1118             if (tmp == NULL) {
1119                 ret = MEMORY_E;
1120             }
1121             else {
1122                 cert_buf = tmp;
1123                 cert_buf[cert_sz] = '\0';
1124                 ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
1125                         WOLFSSL_FILETYPE_PEM);
1126             }
1127 
1128         }
1129 
1130     #if defined(WOLFSSL_PEM_TO_DER)
1131         if (ret == WOLFSSL_SUCCESS) {
1132             /* test loading DER */
1133             ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
1134             if (ret == 0 && pDer != NULL) {
1135                 ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
1136                     WOLFSSL_FILETYPE_ASN1);
1137 
1138                 wc_FreeDer(&pDer);
1139             }
1140         }
1141     #endif
1142 
1143     }
1144     free(cert_buf);
1145 
1146     return ret;
1147 }
1148 #endif /* !NO_FILESYSTEM && !NO_CERTS */
1149 
test_wolfSSL_CertManagerCheckOCSPResponse(void)1150 static void test_wolfSSL_CertManagerCheckOCSPResponse(void)
1151 {
1152 #if defined(HAVE_OCSP) && !defined(NO_RSA)
1153 /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
1154 #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
1155     defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
1156     defined(HAVE_LIGHTY)
1157     WOLFSSL_CERT_MANAGER* cm = NULL;
1158     /* Raw OCSP response bytes captured using the following setup:
1159      * - Run responder with
1160      *      openssl ocsp -port 9999 -ndays 9999
1161      *      -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
1162      *      -rsigner certs/ocsp/ocsp-responder-cert.pem
1163      *      -rkey certs/ocsp/ocsp-responder-key.pem
1164      *      -CA certs/ocsp/intermediate1-ca-cert.pem
1165      * - Run client with
1166      *      openssl ocsp -host 127.0.0.1:9999 -respout resp.out
1167      *      -issuer certs/ocsp/intermediate1-ca-cert.pem
1168      *      -cert certs/ocsp/server1-cert.pem
1169      *      -CAfile certs/ocsp/root-ca-cert.pem -noverify
1170      * - Copy raw response from Wireshark.
1171      */
1172     byte response[] = {
1173         0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
1174         0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
1175         0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
1176         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
1177         0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
1178         0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
1179         0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
1180         0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
1181         0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
1182         0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
1183         0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
1184         0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
1185         0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
1186         0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
1187         0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
1188         0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
1189         0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
1190         0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
1191         0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
1192         0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
1193         0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
1194         0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
1195         0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
1196         0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
1197         0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
1198         0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
1199         0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
1200         0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
1201         0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
1202         0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
1203         0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
1204         0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
1205         0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
1206         0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
1207         0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
1208         0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
1209         0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
1210         0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
1211         0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
1212         0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
1213         0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
1214         0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
1215         0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
1216         0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
1217         0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
1218         0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
1219         0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
1220         0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
1221         0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
1222         0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
1223         0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
1224         0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
1225         0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
1226         0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
1227         0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
1228         0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
1229         0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
1230         0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
1231         0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
1232         0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
1233         0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
1234         0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
1235         0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
1236         0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
1237         0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
1238         0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
1239         0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
1240         0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
1241         0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
1242         0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
1243         0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
1244         0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
1245         0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
1246         0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
1247         0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
1248         0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
1249         0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
1250         0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
1251         0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
1252         0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
1253         0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
1254         0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
1255         0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
1256         0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
1257         0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
1258         0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
1259         0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
1260         0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
1261         0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
1262         0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
1263         0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
1264         0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
1265         0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
1266         0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
1267         0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
1268         0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
1269         0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
1270         0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
1271         0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
1272         0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
1273         0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
1274         0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
1275         0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
1276         0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
1277         0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
1278         0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
1279         0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
1280         0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
1281         0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
1282         0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
1283         0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
1284         0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
1285         0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
1286         0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
1287         0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
1288         0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
1289         0x59, 0x59, 0xa0, 0x07
1290     };
1291     OcspEntry entry[1];
1292     CertStatus status[1];
1293     OcspRequest* request;
1294 
1295     byte serial[] = {0x05};
1296     byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
1297     byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
1298 
1299     printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
1300 
1301     XMEMSET(entry, 0, sizeof(OcspEntry));
1302     XMEMSET(status, 0, sizeof(CertStatus));
1303 
1304     AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
1305     request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
1306                                      DYNAMIC_TYPE_OCSP_REQUEST);
1307     AssertNotNull(request->serial);
1308 
1309     request->serialSz = sizeof(serial);
1310     XMEMCPY(request->serial, serial, sizeof(serial));
1311     XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
1312     XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
1313 
1314     AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
1315     AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
1316     AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
1317         "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
1318 
1319     /* Response should be valid. */
1320     AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
1321         sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
1322 
1323     /* Flip a byte in the request serial number, response should be invalid
1324      * now. */
1325     request->serial[0] ^= request->serial[0];
1326     AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
1327         sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
1328 
1329 
1330     wolfSSL_OCSP_REQUEST_free(request);
1331     wolfSSL_CertManagerFree(cm);
1332 
1333     printf(resultFmt, passed);
1334 #endif /* OPENSSL_ALL || WOLFSSL_NGINX ||  WOLFSSL_HAPROXY ||
1335         * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
1336 #endif /* HAVE_OCSP */
1337 }
1338 
test_wolfSSL_CertManagerLoadCABuffer(void)1339 static int test_wolfSSL_CertManagerLoadCABuffer(void)
1340 {
1341     int ret = 0;
1342 
1343 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
1344     const char* ca_cert = "./certs/ca-cert.pem";
1345     const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
1346 
1347     ret = test_cm_load_ca_file(ca_cert);
1348     #ifdef NO_RSA
1349     AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
1350     #else
1351     AssertIntEQ(ret, WOLFSSL_SUCCESS);
1352     #endif
1353 
1354     ret = test_cm_load_ca_file(ca_expired_cert);
1355     #ifdef NO_RSA
1356     AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
1357     #else
1358     #if !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
1359             !defined(OPENSSL_COMPATIBLE_DEFAULTS)
1360     AssertIntEQ(ret, ASN_AFTER_DATE_E);
1361     #else
1362     AssertIntEQ(ret, WOLFSSL_SUCCESS);
1363     #endif
1364     #endif
1365 #endif
1366 
1367     return ret;
1368 }
1369 
test_wolfSSL_CertManagerGetCerts(void)1370 static void test_wolfSSL_CertManagerGetCerts(void)
1371 {
1372 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
1373     !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
1374      defined(WOLFSSL_SIGNER_DER_CERT)
1375 
1376     WOLFSSL_CERT_MANAGER* cm = NULL;
1377     WOLFSSL_STACK* sk = NULL;
1378     X509* x509 = NULL;
1379     X509* cert1 = NULL;
1380     FILE* file1 = NULL;
1381 #ifdef DEBUG_WOLFSSL_VERBOSE
1382     WOLFSSL_BIO* bio = NULL;
1383 #endif
1384     int i = 0;
1385 
1386     printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
1387     AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
1388 
1389     AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
1390     fclose(file1);
1391 
1392     AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
1393     AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
1394     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
1395                 "./certs/ca-cert.pem", NULL));
1396 
1397     AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
1398 
1399     for (i = 0; i < sk_X509_num(sk); i++) {
1400         x509 = sk_X509_value(sk, i);
1401         AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
1402 
1403 #ifdef DEBUG_WOLFSSL_VERBOSE
1404         bio = BIO_new(wolfSSL_BIO_s_file());
1405         if (bio != NULL) {
1406             BIO_set_fp(bio, stdout, BIO_NOCLOSE);
1407             X509_print(bio, x509);
1408             BIO_free(bio);
1409         }
1410 #endif /* DEBUG_WOLFSSL_VERBOSE */
1411     }
1412     wolfSSL_X509_free(cert1);
1413     sk_X509_pop_free(sk, NULL);
1414     wolfSSL_CertManagerFree(cm);
1415     printf(resultFmt, passed);
1416 #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
1417           !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
1418           defined(WOLFSSL_SIGNER_DER_CERT) */
1419 }
1420 
test_wolfSSL_CertManagerSetVerify(void)1421 static int test_wolfSSL_CertManagerSetVerify(void)
1422 {
1423     int ret = 0;
1424 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
1425     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
1426     (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
1427     WOLFSSL_CERT_MANAGER* cm;
1428     int tmp = myVerifyAction;
1429     const char* ca_cert = "./certs/ca-cert.pem";
1430     const char* expiredCert = "./certs/test/expired/expired-cert.pem";
1431 
1432     cm = wolfSSL_CertManagerNew();
1433     AssertNotNull(cm);
1434 
1435     wolfSSL_CertManagerSetVerify(cm, myVerify);
1436 
1437     ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
1438     AssertIntEQ(ret, WOLFSSL_SUCCESS);
1439 
1440     /* Use the test CB that always accepts certs */
1441     myVerifyAction = VERIFY_OVERRIDE_ERROR;
1442 
1443     ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
1444     AssertIntEQ(ret, WOLFSSL_SUCCESS);
1445 
1446 #ifdef WOLFSSL_ALWAYS_VERIFY_CB
1447     {
1448         const char* verifyCert = "./certs/server-cert.pem";
1449         /* Use the test CB that always fails certs */
1450         myVerifyAction = VERIFY_FORCE_FAIL;
1451 
1452         ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
1453         AssertIntEQ(ret, VERIFY_CERT_ERROR);
1454     }
1455 #endif
1456 
1457     wolfSSL_CertManagerFree(cm);
1458     myVerifyAction = tmp;
1459 #endif
1460 
1461     return ret;
1462 }
1463 
1464 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
1465     defined(DEBUG_UNIT_TEST_CERTS)
1466 /* Used when debugging name constraint tests. Not static to allow use in
1467  * multiple locations with complex define guards. */
DEBUG_WRITE_CERT_X509(WOLFSSL_X509 * x509,const char * fileName)1468 void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
1469 {
1470     BIO* out = BIO_new(BIO_s_file());
1471     if (out != NULL) {
1472         FILE* f = fopen(fileName, "wb");
1473         BIO_set_fp(out, f, BIO_CLOSE);
1474         PEM_write_bio_X509(out, x509);
1475         BIO_free(out);
1476     }
1477 }
DEBUG_WRITE_DER(const byte * der,int derSz,const char * fileName)1478 void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
1479 {
1480     BIO* out = BIO_new(BIO_s_file());
1481     if (out != NULL) {
1482         FILE* f = fopen(fileName, "wb");
1483         BIO_set_fp(out, f, BIO_CLOSE);
1484         BIO_write(out, der, derSz);
1485         BIO_free(out);
1486     }
1487 }
1488 #else
1489 #define DEBUG_WRITE_CERT_X509(x509, fileName)
1490 #define DEBUG_WRITE_DER(der, derSz, fileName)
1491 #endif
1492 
1493 
test_wolfSSL_CertManagerNameConstraint(void)1494 static void test_wolfSSL_CertManagerNameConstraint(void)
1495 {
1496 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
1497     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
1498     defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
1499     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
1500     !defined(NO_SHA256)
1501     WOLFSSL_CERT_MANAGER* cm;
1502     WOLFSSL_EVP_PKEY *priv;
1503     WOLFSSL_X509_NAME* name;
1504     const char* ca_cert = "./certs/test/cert-ext-nc.der";
1505     const char* server_cert = "./certs/test/server-goodcn.pem";
1506     int i = 0;
1507     static const byte extNameConsOid[] = {85, 29, 30};
1508 
1509     RsaKey  key;
1510     WC_RNG  rng;
1511     byte    *der;
1512     int     derSz;
1513     word32  idx = 0;
1514     byte    *pt;
1515     WOLFSSL_X509 *x509, *ca;
1516 
1517     wc_InitRng(&rng);
1518 
1519     /* load in CA private key for signing */
1520     AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
1521     AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
1522                 sizeof_server_key_der_2048), 0);
1523 
1524     /* get ca certificate then alter it */
1525     AssertNotNull(der =
1526             (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
1527     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
1528                 WOLFSSL_FILETYPE_ASN1));
1529     AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
1530     XMEMCPY(der, pt, derSz);
1531 
1532     /* find the name constraint extension and alter it */
1533     pt = der;
1534     for (i = 0; i < derSz - 3; i++) {
1535         if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
1536             pt += 3;
1537             break;
1538         }
1539         pt++;
1540     }
1541     AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
1542 
1543     /* go to the length value and set it to 0 */
1544     while (i < derSz && *pt != 0x81) {
1545         pt++;
1546         i++;
1547     }
1548     AssertIntNE(i, derSz); /* did not place to alter */
1549     pt++;
1550     *pt = 0x00;
1551 
1552     /* resign the altered certificate */
1553     AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
1554                              FOURK_BUF, &key, NULL, &rng)), 0);
1555 
1556     AssertNotNull(cm = wolfSSL_CertManagerNew());
1557     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1558                 WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
1559     wolfSSL_CertManagerFree(cm);
1560 
1561     XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
1562     wolfSSL_X509_free(x509);
1563     wc_FreeRsaKey(&key);
1564     wc_FreeRng(&rng);
1565 
1566     /* add email alt name to satisfy constraint */
1567     pt = (byte*)server_key_der_2048;
1568     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
1569                 (const unsigned char**)&pt, sizeof_server_key_der_2048));
1570 
1571     AssertNotNull(cm = wolfSSL_CertManagerNew());
1572     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
1573                 WOLFSSL_FILETYPE_ASN1));
1574 
1575     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
1576     DEBUG_WRITE_DER(der, derSz, "ca.der");
1577 
1578     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1579                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1580 
1581     /* Good cert test with proper alt email name */
1582     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1583                 WOLFSSL_FILETYPE_PEM));
1584     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1585     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1586 
1587     AssertNotNull(name = X509_NAME_new());
1588     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1589                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1590     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1591                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1592     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
1593                      (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
1594     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1595     X509_NAME_free(name);
1596 
1597     wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
1598 
1599     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1600     DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
1601 
1602     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1603     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1604                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1605     wolfSSL_X509_free(x509);
1606 
1607 
1608     /* Cert with bad alt name list */
1609     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1610                 WOLFSSL_FILETYPE_PEM));
1611     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1612     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1613 
1614     AssertNotNull(name = X509_NAME_new());
1615     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1616                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1617     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1618                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1619     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
1620                      (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
1621     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1622     X509_NAME_free(name);
1623 
1624     wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
1625     wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
1626 
1627     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1628     DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
1629 
1630     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1631     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1632                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1633 
1634     wolfSSL_CertManagerFree(cm);
1635     wolfSSL_X509_free(x509);
1636     wolfSSL_X509_free(ca);
1637     wolfSSL_EVP_PKEY_free(priv);
1638 #endif
1639 }
1640 
1641 
test_wolfSSL_CertManagerNameConstraint2(void)1642 static void test_wolfSSL_CertManagerNameConstraint2(void)
1643 {
1644 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
1645     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
1646     defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
1647     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
1648     const char* ca_cert  = "./certs/test/cert-ext-ndir.der";
1649     const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
1650     const char* server_cert = "./certs/server-cert.pem";
1651     WOLFSSL_CERT_MANAGER* cm;
1652     WOLFSSL_X509 *x509, *ca;
1653 
1654     const unsigned char *der;
1655     const unsigned char *pt;
1656     WOLFSSL_EVP_PKEY *priv;
1657     WOLFSSL_X509_NAME* name;
1658     int   derSz;
1659 
1660     /* C=US*/
1661     char altName[] = {
1662         0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
1663         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
1664     };
1665 
1666     /* C=ID */
1667     char altNameFail[] = {
1668         0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
1669         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
1670     };
1671 
1672     /* C=US ST=California*/
1673     char altNameExc[] = {
1674         0x30, 0x22,
1675         0x31, 0x0B,
1676         0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
1677         0x31, 0x13,
1678         0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
1679         0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
1680     };
1681     /* load in CA private key for signing */
1682     pt = ca_key_der_2048;
1683     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
1684                                                 sizeof_ca_key_der_2048));
1685 
1686     AssertNotNull(cm = wolfSSL_CertManagerNew());
1687     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
1688                 WOLFSSL_FILETYPE_ASN1));
1689     AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
1690     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1691                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1692 
1693     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1694                 WOLFSSL_FILETYPE_PEM));
1695     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1696     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1697 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1698     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1699 #else
1700     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1701 #endif
1702     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1703     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1704                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1705 
1706     /* add in matching DIR alt name and resign */
1707     wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
1708 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1709     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1710 #else
1711     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1712 #endif
1713 
1714     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1715     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1716                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1717     wolfSSL_X509_free(x509);
1718 
1719     /* check verify fail */
1720     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1721                 WOLFSSL_FILETYPE_PEM));
1722     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1723     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1724 
1725     /* add in miss matching DIR alt name and resign */
1726     wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
1727             ASN_DIR_TYPE);
1728 
1729 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1730     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1731 #else
1732     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1733 #endif
1734     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1735 #ifndef WOLFSSL_NO_ASN_STRICT
1736     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1737                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1738 #else
1739     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1740                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1741 #endif
1742 
1743     /* check that it still fails if one bad altname and one good altname is in
1744      * the certificate */
1745     wolfSSL_X509_free(x509);
1746     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1747                 WOLFSSL_FILETYPE_PEM));
1748     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1749     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1750     wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
1751     wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
1752             ASN_DIR_TYPE);
1753 
1754 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1755     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1756 #else
1757     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1758 #endif
1759     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1760 #ifndef WOLFSSL_NO_ASN_STRICT
1761     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1762                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1763 #else
1764     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1765                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1766 #endif
1767 
1768     /* check it fails with switching position of bad altname */
1769     wolfSSL_X509_free(x509);
1770     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1771                 WOLFSSL_FILETYPE_PEM));
1772     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1773     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1774     wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
1775             ASN_DIR_TYPE);
1776     wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
1777 
1778 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1779     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1780 #else
1781     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1782 #endif
1783     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1784 #ifndef WOLFSSL_NO_ASN_STRICT
1785     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1786                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1787 #else
1788     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1789                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1790 #endif
1791     wolfSSL_CertManagerFree(cm);
1792 
1793     wolfSSL_X509_free(x509);
1794     wolfSSL_X509_free(ca);
1795 
1796     /* now test with excluded name constraint */
1797     AssertNotNull(cm = wolfSSL_CertManagerNew());
1798     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
1799                 WOLFSSL_FILETYPE_ASN1));
1800     AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
1801     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1802                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1803 
1804     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1805                 WOLFSSL_FILETYPE_PEM));
1806     wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
1807             ASN_DIR_TYPE);
1808     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1809     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1810 
1811 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
1812     wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
1813 #else
1814     wolfSSL_X509_sign(x509, priv, EVP_sha256());
1815 #endif
1816     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
1817 #ifndef WOLFSSL_NO_ASN_STRICT
1818     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1819                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1820 #else
1821     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1822                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1823 #endif
1824     wolfSSL_CertManagerFree(cm);
1825     wolfSSL_X509_free(x509);
1826     wolfSSL_X509_free(ca);
1827     wolfSSL_EVP_PKEY_free(priv);
1828 #endif
1829 }
1830 
test_wolfSSL_CertManagerNameConstraint3(void)1831 static void test_wolfSSL_CertManagerNameConstraint3(void)
1832 {
1833 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
1834     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
1835     defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
1836     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
1837     !defined(NO_SHA256)
1838     WOLFSSL_CERT_MANAGER* cm;
1839     WOLFSSL_EVP_PKEY *priv;
1840     WOLFSSL_X509_NAME* name;
1841     const char* ca_cert = "./certs/test/cert-ext-mnc.der";
1842     const char* server_cert = "./certs/test/server-goodcn.pem";
1843 
1844     byte    *der;
1845     int     derSz;
1846     byte    *pt;
1847     WOLFSSL_X509 *x509, *ca;
1848 
1849     pt = (byte*)server_key_der_2048;
1850     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
1851                 (const unsigned char**)&pt, sizeof_server_key_der_2048));
1852 
1853     AssertNotNull(cm = wolfSSL_CertManagerNew());
1854     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
1855                 WOLFSSL_FILETYPE_ASN1));
1856     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
1857     DEBUG_WRITE_DER(der, derSz, "ca.der");
1858 
1859     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1860                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1861 
1862     /* check satisfying .wolfssl.com constraint passes */
1863     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1864                 WOLFSSL_FILETYPE_PEM));
1865     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1866     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1867 
1868     AssertNotNull(name = X509_NAME_new());
1869     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1870                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1871     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1872                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1873     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
1874                      (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
1875     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1876     X509_NAME_free(name);
1877 
1878     wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
1879 
1880     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1881     DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
1882 
1883     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1884     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1885                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1886     wolfSSL_X509_free(x509);
1887 
1888     /* check satisfying .random.com constraint passes */
1889     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1890                 WOLFSSL_FILETYPE_PEM));
1891     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1892     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1893 
1894     AssertNotNull(name = X509_NAME_new());
1895     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1896                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1897     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1898                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1899     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
1900                      (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
1901     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1902     X509_NAME_free(name);
1903 
1904     wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
1905 
1906     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1907     DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
1908 
1909     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1910     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1911                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1912     wolfSSL_X509_free(x509);
1913 
1914     /* check fail case when neither constraint is matched */
1915     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1916                 WOLFSSL_FILETYPE_PEM));
1917     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1918     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1919 
1920     AssertNotNull(name = X509_NAME_new());
1921     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1922                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1923     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1924                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1925     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
1926                      (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
1927     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1928     X509_NAME_free(name);
1929 
1930     wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
1931 
1932     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1933     DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
1934 
1935     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1936     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1937                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
1938 
1939     wolfSSL_CertManagerFree(cm);
1940     wolfSSL_X509_free(x509);
1941     wolfSSL_X509_free(ca);
1942     wolfSSL_EVP_PKEY_free(priv);
1943 #endif
1944 }
1945 
test_wolfSSL_CertManagerNameConstraint4(void)1946 static void test_wolfSSL_CertManagerNameConstraint4(void)
1947 {
1948 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
1949     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
1950     defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
1951     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
1952     !defined(NO_SHA256)
1953     WOLFSSL_CERT_MANAGER* cm;
1954     WOLFSSL_EVP_PKEY *priv;
1955     WOLFSSL_X509_NAME* name;
1956     const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
1957     const char* server_cert = "./certs/test/server-goodcn.pem";
1958 
1959     byte    *der;
1960     int     derSz;
1961     byte    *pt;
1962     WOLFSSL_X509 *x509, *ca;
1963 
1964     pt = (byte*)server_key_der_2048;
1965     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
1966                 (const unsigned char**)&pt, sizeof_server_key_der_2048));
1967 
1968     AssertNotNull(cm = wolfSSL_CertManagerNew());
1969     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
1970                 WOLFSSL_FILETYPE_ASN1));
1971     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
1972     DEBUG_WRITE_DER(der, derSz, "ca.der");
1973 
1974     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
1975                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1976 
1977     /* check satisfying wolfssl.com constraint passes */
1978     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
1979                 WOLFSSL_FILETYPE_PEM));
1980     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
1981     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
1982 
1983     AssertNotNull(name = X509_NAME_new());
1984     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
1985                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
1986     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
1987                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
1988     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
1989     X509_NAME_free(name);
1990 
1991     wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
1992     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
1993     DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
1994 
1995     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
1996     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
1997                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
1998     wolfSSL_X509_free(x509);
1999 
2000     /* check satisfying example.com constraint passes */
2001     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2002                 WOLFSSL_FILETYPE_PEM));
2003     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2004     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2005 
2006     AssertNotNull(name = X509_NAME_new());
2007     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2008                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2009     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2010                              (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
2011     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2012     X509_NAME_free(name);
2013 
2014     wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
2015     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2016     DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
2017 
2018     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2019     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2020                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2021     wolfSSL_X509_free(x509);
2022 
2023     /* check satisfying wolfssl.com constraint passes with list of DNS's */
2024     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2025                 WOLFSSL_FILETYPE_PEM));
2026     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2027     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2028 
2029     AssertNotNull(name = X509_NAME_new());
2030     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2031                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2032     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2033                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
2034     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2035     X509_NAME_free(name);
2036 
2037     wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
2038     wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
2039     wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
2040     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2041     DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
2042 
2043     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2044     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2045                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2046     wolfSSL_X509_free(x509);
2047 
2048     /* check fail when one DNS in the list is bad */
2049     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2050                 WOLFSSL_FILETYPE_PEM));
2051     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2052     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2053 
2054     AssertNotNull(name = X509_NAME_new());
2055     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2056                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2057     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2058                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
2059     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2060     X509_NAME_free(name);
2061 
2062     wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
2063     wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
2064     wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
2065     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2066     DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
2067 
2068     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2069     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2070                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
2071     wolfSSL_X509_free(x509);
2072 
2073     /* check fail case when neither constraint is matched */
2074     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2075                 WOLFSSL_FILETYPE_PEM));
2076     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2077     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2078 
2079     AssertNotNull(name = X509_NAME_new());
2080     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2081                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2082     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2083                              (byte*)"common", 6, -1, 0), SSL_SUCCESS);
2084     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2085     X509_NAME_free(name);
2086 
2087     wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
2088     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2089     DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
2090 
2091     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2092     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2093                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
2094 
2095     wolfSSL_CertManagerFree(cm);
2096     wolfSSL_X509_free(x509);
2097     wolfSSL_X509_free(ca);
2098     wolfSSL_EVP_PKEY_free(priv);
2099 #endif
2100 }
2101 
test_wolfSSL_CertManagerNameConstraint5(void)2102 static void test_wolfSSL_CertManagerNameConstraint5(void)
2103 {
2104 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
2105     !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
2106     defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
2107     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
2108     !defined(NO_SHA256)
2109     WOLFSSL_CERT_MANAGER* cm;
2110     WOLFSSL_EVP_PKEY *priv;
2111     WOLFSSL_X509_NAME* name;
2112     const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
2113     const char* server_cert = "./certs/test/server-goodcn.pem";
2114 
2115     byte    *der;
2116     int     derSz;
2117     byte    *pt;
2118     WOLFSSL_X509 *x509, *ca;
2119 
2120     pt = (byte*)server_key_der_2048;
2121     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
2122                 (const unsigned char**)&pt, sizeof_server_key_der_2048));
2123 
2124     AssertNotNull(cm = wolfSSL_CertManagerNew());
2125     AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
2126                 WOLFSSL_FILETYPE_ASN1));
2127     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
2128     DEBUG_WRITE_DER(der, derSz, "ca.der");
2129 
2130     AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
2131                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2132 
2133     /* check satisfying wolfssl.com constraint passes */
2134     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2135                 WOLFSSL_FILETYPE_PEM));
2136     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2137     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2138 
2139     AssertNotNull(name = X509_NAME_new());
2140     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2141                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2142     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2143                              (byte*)"example", 7, -1, 0), SSL_SUCCESS);
2144     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2145     X509_NAME_free(name);
2146 
2147     wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
2148     wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
2149     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2150     DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
2151 
2152     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2153     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2154                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2155     wolfSSL_X509_free(x509);
2156 
2157     /* fail with DNS check because of common name */
2158     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2159                 WOLFSSL_FILETYPE_PEM));
2160     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2161     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2162 
2163     AssertNotNull(name = X509_NAME_new());
2164     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2165                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2166     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
2167                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
2168     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2169     X509_NAME_free(name);
2170 
2171     wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
2172     wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
2173     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2174     DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
2175 
2176     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2177     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2178                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
2179     wolfSSL_X509_free(x509);
2180 
2181     /* fail on permitted DNS name constraint */
2182     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2183                 WOLFSSL_FILETYPE_PEM));
2184     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2185     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2186 
2187     AssertNotNull(name = X509_NAME_new());
2188     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2189                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2190     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2191     X509_NAME_free(name);
2192 
2193     wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
2194     wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
2195     wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
2196     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2197     DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
2198 
2199     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2200     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2201                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
2202     wolfSSL_X509_free(x509);
2203 
2204     /* fail on permitted email name constraint */
2205     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2206                 WOLFSSL_FILETYPE_PEM));
2207     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2208     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2209 
2210     AssertNotNull(name = X509_NAME_new());
2211     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2212                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2213     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2214     X509_NAME_free(name);
2215 
2216     wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
2217     wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
2218     wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
2219     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2220     DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
2221 
2222     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2223     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2224                 WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
2225     wolfSSL_X509_free(x509);
2226 
2227     /* success with empty email name */
2228     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
2229                 WOLFSSL_FILETYPE_PEM));
2230     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
2231     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
2232 
2233     AssertNotNull(name = X509_NAME_new());
2234     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
2235                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
2236     AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
2237     X509_NAME_free(name);
2238 
2239     wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
2240     AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
2241     DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
2242 
2243     AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
2244     AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
2245                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2246     wolfSSL_X509_free(x509);
2247 
2248     wolfSSL_CertManagerFree(cm);
2249     wolfSSL_X509_free(ca);
2250     wolfSSL_EVP_PKEY_free(priv);
2251 #endif
2252 }
2253 
test_wolfSSL_CertManagerCRL(void)2254 static void test_wolfSSL_CertManagerCRL(void)
2255 {
2256 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
2257     !defined(NO_RSA)
2258 
2259     const char* ca_cert = "./certs/ca-cert.pem";
2260     const char* crl1     = "./certs/crl/crl.pem";
2261     const char* crl2     = "./certs/crl/crl2.pem";
2262 
2263     WOLFSSL_CERT_MANAGER* cm = NULL;
2264 
2265     AssertNotNull(cm = wolfSSL_CertManagerNew());
2266     AssertIntEQ(WOLFSSL_SUCCESS,
2267         wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
2268     AssertIntEQ(WOLFSSL_SUCCESS,
2269         wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
2270     AssertIntEQ(WOLFSSL_SUCCESS,
2271         wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
2272     wolfSSL_CertManagerFreeCRL(cm);
2273 
2274     AssertIntEQ(WOLFSSL_SUCCESS,
2275         wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
2276     AssertIntEQ(WOLFSSL_SUCCESS,
2277         wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
2278     wolfSSL_CertManagerFree(cm);
2279 
2280 #endif
2281 }
2282 
test_wolfSSL_CTX_load_verify_locations_ex(void)2283 static void test_wolfSSL_CTX_load_verify_locations_ex(void)
2284 {
2285 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
2286     !defined(NO_WOLFSSL_CLIENT)
2287     WOLFSSL_CTX* ctx;
2288     const char* ca_cert = "./certs/ca-cert.pem";
2289     const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
2290     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2291     AssertNotNull(ctx);
2292 
2293     /* test good CA */
2294     AssertTrue(WOLFSSL_SUCCESS ==
2295         wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
2296             WOLFSSL_LOAD_FLAG_NONE));
2297 
2298     /* test expired CA */
2299 #ifndef OPENSSL_COMPATIBLE_DEFAULTS
2300     AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
2301             WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
2302 #else
2303     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
2304             WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
2305 #endif
2306     AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
2307             WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
2308 
2309     wolfSSL_CTX_free(ctx);
2310 
2311 #endif
2312 }
2313 
test_wolfSSL_CTX_load_verify_buffer_ex(void)2314 static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
2315 {
2316 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
2317     defined(USE_CERT_BUFFERS_2048)
2318     WOLFSSL_CTX* ctx;
2319     const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
2320     byte ca_expired_cert[TWOK_BUF];
2321     word32 sizeof_ca_expired_cert;
2322     XFILE fp;
2323 
2324 #ifndef NO_WOLFSSL_CLIENT
2325     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2326 #else
2327     ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
2328 #endif
2329     AssertNotNull(ctx);
2330 
2331     /* test good CA */
2332     AssertTrue(WOLFSSL_SUCCESS ==
2333         wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
2334             sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
2335             WOLFSSL_LOAD_FLAG_NONE));
2336 
2337     /* load expired CA */
2338     XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
2339     fp = XFOPEN(ca_expired_cert_file, "rb");
2340     AssertTrue(fp != XBADFILE);
2341     sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
2342         sizeof(ca_expired_cert), fp);
2343     XFCLOSE(fp);
2344 
2345     /* test expired CA failure */
2346 
2347 
2348 #ifndef OPENSSL_COMPATIBLE_DEFAULTS
2349     AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
2350             sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
2351             WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
2352 #else
2353     AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
2354             sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
2355             WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
2356 #endif
2357     /* test expired CA success */
2358     AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
2359             sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
2360             WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
2361 
2362     wolfSSL_CTX_free(ctx);
2363 
2364 #endif
2365 }
2366 
test_wolfSSL_CTX_load_verify_chain_buffer_format(void)2367 static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
2368 {
2369 #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
2370     defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
2371     WOLFSSL_CTX* ctx;
2372 
2373   #ifndef NO_WOLFSSL_CLIENT
2374     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2375   #else
2376     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2377   #endif
2378 
2379     AssertTrue(WOLFSSL_SUCCESS ==
2380                wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
2381                                                            sizeof_ca_cert_chain_der,
2382                                                            WOLFSSL_FILETYPE_ASN1));
2383 
2384     wolfSSL_CTX_free(ctx);
2385 #endif
2386 }
2387 
test_wolfSSL_CTX_add1_chain_cert(void)2388 static void test_wolfSSL_CTX_add1_chain_cert(void)
2389 {
2390 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
2391     defined(KEEP_OUR_CERT)
2392     WOLFSSL_CTX*        ctx;
2393     WOLFSSL*            ssl;
2394     const char *certChain[] = {
2395             "./certs/intermediate/client-int-cert.pem",
2396             "./certs/intermediate/ca-int2-cert.pem",
2397             "./certs/intermediate/ca-int-cert.pem",
2398             "./certs/ca-cert.pem",
2399             NULL
2400     };
2401     const char** cert;
2402     WOLFSSL_X509* x509;
2403     WOLF_STACK_OF(X509)* chain = NULL;
2404 
2405     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2406     AssertNotNull(ssl = wolfSSL_new(ctx));
2407 
2408     for (cert = certChain; *cert != NULL; cert++) {
2409         x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
2410         AssertNotNull(x509);
2411         AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
2412         X509_free(x509);
2413     }
2414     for (cert = certChain; *cert != NULL; cert++) {
2415         x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
2416         AssertNotNull(x509);
2417         AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
2418         X509_free(x509);
2419     }
2420 
2421     AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
2422     AssertIntEQ(sk_X509_num(chain), 3);
2423     AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
2424     AssertIntEQ(sk_X509_num(chain), 3);
2425 
2426     SSL_free(ssl);
2427     SSL_CTX_free(ctx);
2428 #endif
2429 }
2430 
test_wolfSSL_CTX_use_certificate_chain_file_format(void)2431 static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
2432 {
2433     int ret = 0;
2434 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
2435     const char* server_chain_der = "./certs/server-cert-chain.der";
2436     WOLFSSL_CTX* ctx;
2437 
2438   #ifndef NO_WOLFSSL_CLIENT
2439     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2440     AssertNotNull(ctx);
2441   #else
2442     ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
2443     AssertNotNull(ctx);
2444   #endif
2445 
2446     AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
2447         server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2448 
2449     wolfSSL_CTX_free(ctx);
2450 #endif
2451     return ret;
2452 }
2453 
test_wolfSSL_CTX_SetTmpDH_file(void)2454 static void test_wolfSSL_CTX_SetTmpDH_file(void)
2455 {
2456 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
2457     WOLFSSL_CTX *ctx;
2458 
2459   #ifndef NO_WOLFSSL_CLIENT
2460     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2461   #else
2462     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2463   #endif
2464 
2465     /* invalid context */
2466     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
2467                 dhParamFile, WOLFSSL_FILETYPE_PEM));
2468 
2469     /* invalid dhParamFile file */
2470     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
2471                 NULL, WOLFSSL_FILETYPE_PEM));
2472     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
2473                 bogusFile, WOLFSSL_FILETYPE_PEM));
2474 
2475     /* success */
2476     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
2477                 WOLFSSL_FILETYPE_PEM));
2478 
2479     wolfSSL_CTX_free(ctx);
2480 #endif
2481 }
2482 
test_wolfSSL_CTX_SetTmpDH_buffer(void)2483 static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
2484 {
2485 #if !defined(NO_CERTS) && !defined(NO_DH)
2486     WOLFSSL_CTX *ctx;
2487 
2488   #ifndef NO_WOLFSSL_CLIENT
2489     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2490   #else
2491     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2492   #endif
2493 
2494     /* invalid context */
2495     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
2496                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2497 
2498     /* invalid dhParamFile file */
2499     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
2500                 0, WOLFSSL_FILETYPE_ASN1));
2501     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
2502                 sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2503 
2504     /* success */
2505     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
2506                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2507 
2508     wolfSSL_CTX_free(ctx);
2509 #endif
2510 }
2511 
test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)2512 static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
2513 {
2514 #if !defined(NO_CERTS) && !defined(NO_DH)
2515     WOLFSSL_CTX *ctx;
2516 
2517   #ifndef NO_WOLFSSL_CLIENT
2518     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2519     AssertNotNull(ctx);
2520   #else
2521     ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
2522     AssertNotNull(ctx);
2523   #endif
2524 
2525     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
2526 
2527     AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
2528                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2529 
2530     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
2531 
2532     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
2533                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2534 
2535     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
2536 
2537     AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
2538                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2539 
2540     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
2541 
2542     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
2543                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2544 
2545     wolfSSL_CTX_free(ctx);
2546 #endif
2547 }
2548 
test_wolfSSL_CTX_der_load_verify_locations(void)2549 static void test_wolfSSL_CTX_der_load_verify_locations(void)
2550 {
2551 #ifdef WOLFSSL_DER_LOAD
2552     WOLFSSL_CTX* ctx = NULL;
2553     const char* derCert = "./certs/server-cert.der";
2554     const char* nullPath = NULL;
2555     const char* invalidPath = "./certs/this-cert-does-not-exist.der";
2556     const char* emptyPath = "";
2557 
2558     /* der load Case 1 ctx NULL */
2559     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
2560                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
2561 
2562   #ifndef NO_WOLFSSL_CLIENT
2563     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2564   #else
2565     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2566   #endif
2567 
2568     /* Case 2 filePath NULL */
2569     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
2570                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
2571     /* Case 3 invalid format */
2572     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
2573                 WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
2574     /* Case 4 filePath not valid */
2575     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
2576                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
2577     /* Case 5 filePath empty */
2578     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
2579                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
2580 #ifndef NO_RSA
2581     /* Case 6 success case */
2582     AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
2583                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
2584 #endif
2585 
2586     wolfSSL_CTX_free(ctx);
2587 #endif
2588 }
2589 
test_wolfSSL_CTX_enable_disable(void)2590 static void test_wolfSSL_CTX_enable_disable(void)
2591 {
2592 #ifndef NO_CERTS
2593     WOLFSSL_CTX* ctx = NULL;
2594 
2595   #ifdef HAVE_CRL
2596     AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
2597     AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
2598   #endif
2599 
2600   #ifdef HAVE_OCSP
2601     AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
2602     AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
2603   #endif
2604 
2605   #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
2606       defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
2607     AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
2608     AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
2609     AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
2610     AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
2611   #endif
2612 
2613   #ifndef NO_WOLFSSL_CLIENT
2614 
2615     #ifdef HAVE_EXTENDED_MASTER
2616     AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
2617     #endif
2618     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2619     AssertNotNull(ctx);
2620 
2621     #ifdef HAVE_EXTENDED_MASTER
2622     AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
2623     #endif
2624 
2625   #elif !defined(NO_WOLFSSL_SERVER)
2626     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2627   #else
2628     return;
2629   #endif
2630 
2631   #ifdef HAVE_CRL
2632     AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
2633     AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
2634   #endif
2635 
2636   #ifdef HAVE_OCSP
2637     AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
2638     AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
2639                 WOLFSSL_SUCCESS);
2640     AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
2641                 WOLFSSL_SUCCESS);
2642     AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
2643                 WOLFSSL_SUCCESS);
2644   #endif
2645 
2646   #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
2647       defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
2648     AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
2649     AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
2650     AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
2651     AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
2652   #endif
2653     wolfSSL_CTX_free(ctx);
2654 #endif /* NO_CERTS */
2655 }
2656 
test_wolfSSL_CTX_ticket_API(void)2657 static void test_wolfSSL_CTX_ticket_API(void)
2658 {
2659 #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
2660     WOLFSSL_CTX* ctx = NULL;
2661     void *userCtx = (void*)"this is my ctx";
2662 
2663     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2664 
2665     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
2666     AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
2667 
2668     wolfSSL_CTX_free(ctx);
2669 
2670     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
2671     AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
2672 #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
2673 }
2674 
test_wolfSSL_set_minmax_proto_version(void)2675 static void test_wolfSSL_set_minmax_proto_version(void)
2676 {
2677 #ifdef OPENSSL_EXTRA
2678 WOLFSSL_CTX *ctx;
2679 WOLFSSL *ssl;
2680 int ret;
2681 (void)ret;
2682 (void)ssl;
2683 printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
2684 
2685 #ifndef NO_WOLFSSL_CLIENT
2686     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2687     AssertNotNull(ssl = wolfSSL_new(ctx));
2688 
2689     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
2690     AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
2691     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
2692     AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
2693 
2694     AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
2695     AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
2696     AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
2697     AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
2698 
2699     wolfSSL_free(ssl);
2700     wolfSSL_CTX_free(ctx);
2701 
2702 #else
2703     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2704 
2705     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
2706     AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
2707     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
2708     AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
2709 
2710     wolfSSL_CTX_free(ctx);
2711 #endif
2712 
2713     printf(resultFmt, passed);
2714 #endif
2715 }
2716 
2717 /*----------------------------------------------------------------------------*
2718  | SSL
2719  *----------------------------------------------------------------------------*/
2720 
test_server_wolfSSL_new(void)2721 static void test_server_wolfSSL_new(void)
2722 {
2723 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
2724         !defined(NO_WOLFSSL_SERVER)
2725     WOLFSSL_CTX *ctx;
2726     WOLFSSL_CTX *ctx_nocert;
2727     WOLFSSL *ssl;
2728 
2729     AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2730     AssertNotNull(ctx        = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2731 
2732     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
2733     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
2734 
2735     /* invalid context */
2736     AssertNull(ssl = wolfSSL_new(NULL));
2737 #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
2738     AssertNull(ssl = wolfSSL_new(ctx_nocert));
2739 #endif
2740 
2741     /* success */
2742     AssertNotNull(ssl = wolfSSL_new(ctx));
2743 
2744     wolfSSL_free(ssl);
2745     wolfSSL_CTX_free(ctx);
2746     wolfSSL_CTX_free(ctx_nocert);
2747 #endif
2748 }
2749 
2750 
test_client_wolfSSL_new(void)2751 static void test_client_wolfSSL_new(void)
2752 {
2753 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
2754         !defined(NO_WOLFSSL_CLIENT)
2755     WOLFSSL_CTX *ctx;
2756     WOLFSSL_CTX *ctx_nocert;
2757     WOLFSSL *ssl;
2758 
2759     AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2760     AssertNotNull(ctx        = wolfSSL_CTX_new(wolfSSLv23_client_method()));
2761 
2762     AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
2763 
2764     /* invalid context */
2765     AssertNull(ssl = wolfSSL_new(NULL));
2766 
2767     /* success */
2768     AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
2769     wolfSSL_free(ssl);
2770 
2771     /* success */
2772     AssertNotNull(ssl = wolfSSL_new(ctx));
2773     wolfSSL_free(ssl);
2774 
2775     wolfSSL_CTX_free(ctx);
2776     wolfSSL_CTX_free(ctx_nocert);
2777 #endif
2778 }
2779 
test_wolfSSL_SetTmpDH_file(void)2780 static void test_wolfSSL_SetTmpDH_file(void)
2781 {
2782 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
2783         !defined(NO_WOLFSSL_SERVER)
2784     WOLFSSL_CTX *ctx;
2785     WOLFSSL *ssl;
2786 
2787     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2788 #ifndef NO_RSA
2789     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
2790                WOLFSSL_FILETYPE_PEM));
2791     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
2792                WOLFSSL_FILETYPE_PEM));
2793 #elif defined(HAVE_ECC)
2794     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
2795                WOLFSSL_FILETYPE_PEM));
2796     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
2797                WOLFSSL_FILETYPE_PEM));
2798 #elif defined(HAVE_ED25519)
2799     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
2800                WOLFSSL_FILETYPE_PEM));
2801     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
2802                WOLFSSL_FILETYPE_PEM));
2803 #elif defined(HAVE_ED448)
2804     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
2805                WOLFSSL_FILETYPE_PEM));
2806     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
2807                WOLFSSL_FILETYPE_PEM));
2808 #endif
2809     AssertNotNull(ssl = wolfSSL_new(ctx));
2810 
2811     /* invalid ssl */
2812     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
2813                 dhParamFile, WOLFSSL_FILETYPE_PEM));
2814 
2815     /* invalid dhParamFile file */
2816     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
2817                 NULL, WOLFSSL_FILETYPE_PEM));
2818     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
2819                 bogusFile, WOLFSSL_FILETYPE_PEM));
2820 
2821     /* success */
2822     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
2823                 WOLFSSL_FILETYPE_PEM));
2824 
2825     wolfSSL_free(ssl);
2826     wolfSSL_CTX_free(ctx);
2827 #endif
2828 }
2829 
test_wolfSSL_SetTmpDH_buffer(void)2830 static void test_wolfSSL_SetTmpDH_buffer(void)
2831 {
2832 #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
2833     WOLFSSL_CTX *ctx;
2834     WOLFSSL *ssl;
2835 
2836     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
2837     AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
2838                 sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
2839     AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
2840                 sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2841     AssertNotNull(ssl = wolfSSL_new(ctx));
2842 
2843     /* invalid ssl */
2844     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
2845                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2846 
2847     /* invalid dhParamFile file */
2848     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
2849                 0, WOLFSSL_FILETYPE_ASN1));
2850     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
2851                 sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2852 
2853     /* success */
2854     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
2855                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2856 
2857     wolfSSL_free(ssl);
2858     wolfSSL_CTX_free(ctx);
2859 #endif
2860 }
2861 
test_wolfSSL_SetMinMaxDhKey_Sz(void)2862 static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
2863 {
2864 #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
2865     WOLFSSL_CTX *ctx, *ctx2;
2866     WOLFSSL *ssl, *ssl2;
2867     ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
2868     AssertNotNull(ctx);
2869     AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
2870                 sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
2871     AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
2872                 sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2873     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
2874     ssl = wolfSSL_new(ctx);
2875     AssertNotNull(ssl);
2876     ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
2877     AssertNotNull(ctx2);
2878     AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
2879                 sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
2880     AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
2881                 sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2882     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
2883     ssl2 = wolfSSL_new(ctx2);
2884     AssertNotNull(ssl2);
2885 
2886     AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
2887                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2888 
2889     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
2890     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
2891                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2892 
2893     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
2894     AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
2895                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2896 
2897     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
2898                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2899 
2900     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
2901     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
2902                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2903 
2904     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
2905     AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
2906                 sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
2907 
2908     wolfSSL_free(ssl2);
2909     wolfSSL_CTX_free(ctx2);
2910     wolfSSL_free(ssl);
2911     wolfSSL_CTX_free(ctx);
2912 #endif
2913 }
2914 
2915 
2916 /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
2917  * allowed.
2918  * POST: return 1 on success.
2919  */
test_wolfSSL_SetMinVersion(void)2920 static int test_wolfSSL_SetMinVersion(void)
2921 {
2922     int                 failFlag = WOLFSSL_SUCCESS;
2923 #ifndef NO_WOLFSSL_CLIENT
2924     WOLFSSL_CTX*        ctx;
2925     WOLFSSL*            ssl;
2926     int                 itr;
2927 
2928     #ifndef NO_OLD_TLS
2929         const int versions[]  =  {
2930                             #ifdef WOLFSSL_ALLOW_TLSV10
2931                                    WOLFSSL_TLSV1,
2932                             #endif
2933                                    WOLFSSL_TLSV1_1,
2934                                    WOLFSSL_TLSV1_2};
2935     #elif !defined(WOLFSSL_NO_TLS12)
2936         const int versions[]  =  { WOLFSSL_TLSV1_2 };
2937     #else
2938         const int versions[]  =  { WOLFSSL_TLSV1_3 };
2939     #endif
2940 
2941     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
2942     ssl = wolfSSL_new(ctx);
2943 
2944     printf(testingFmt, "wolfSSL_SetMinVersion()");
2945 
2946     for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
2947        if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
2948             failFlag = WOLFSSL_FAILURE;
2949         }
2950     }
2951 
2952     printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
2953 
2954     wolfSSL_free(ssl);
2955     wolfSSL_CTX_free(ctx);
2956 #endif
2957     return failFlag;
2958 
2959 } /* END test_wolfSSL_SetMinVersion */
2960 
2961 
2962 /*----------------------------------------------------------------------------*
2963  | EC
2964  *----------------------------------------------------------------------------*/
2965 
2966 /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
2967     EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
2968  */
2969 
2970 # if defined(OPENSSL_EXTRA) && \
2971   (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
test_wolfSSL_EC(void)2972 static void test_wolfSSL_EC(void)
2973 {
2974 #if defined(HAVE_ECC)
2975     BN_CTX *ctx;
2976     EC_GROUP *group;
2977     EC_GROUP *group2;
2978     EC_POINT *Gxy, *new_point, *set_point;
2979     BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
2980     BIGNUM *X, *Y;
2981     BIGNUM *set_point_bn;
2982     char* hexStr;
2983     int group_bits;
2984 
2985     const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
2986     /* NISTP256R1 Gx/Gy */
2987     const char* kGx   = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
2988     const char* kGy   = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
2989 
2990 #ifndef HAVE_SELFTEST
2991     EC_POINT *tmp;
2992     size_t bin_len;
2993     unsigned char* buf = NULL;
2994 
2995     const char* uncompG   = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
2996     const unsigned char binUncompG[] = {
2997         0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
2998         0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
2999         0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
3000         0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
3001         0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
3002         0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
3003     };
3004 
3005 #ifdef HAVE_COMP_KEY
3006     const char* compG   = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
3007     const unsigned char binCompG[] = {
3008         0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
3009         0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
3010         0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
3011     };
3012 #endif
3013 #endif
3014 
3015     AssertNotNull(ctx = BN_CTX_new());
3016     AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
3017     AssertNotNull(group2 = EC_GROUP_dup(group));
3018     AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
3019     AssertNotNull(Gxy = EC_POINT_new(group));
3020     AssertNotNull(new_point = EC_POINT_new(group));
3021     AssertNotNull(set_point = EC_POINT_new(group));
3022     AssertNotNull(X = BN_new());
3023     AssertNotNull(Y = BN_new());
3024     AssertNotNull(set_point_bn = BN_new());
3025 
3026     /* load test values */
3027     AssertIntEQ(BN_hex2bn(&k,  kTest), WOLFSSL_SUCCESS);
3028     AssertIntEQ(BN_hex2bn(&Gx, kGx),   WOLFSSL_SUCCESS);
3029     AssertIntEQ(BN_hex2bn(&Gy, kGy),   WOLFSSL_SUCCESS);
3030     AssertIntEQ(BN_hex2bn(&Gz, "1"),   WOLFSSL_SUCCESS);
3031 
3032     /* populate coordinates for input point */
3033     Gxy->X = Gx;
3034     Gxy->Y = Gy;
3035     Gxy->Z = Gz;
3036 
3037 #ifndef HAVE_SELFTEST
3038     /* perform point multiplication */
3039     AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
3040     AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
3041     AssertIntEQ(BN_is_zero(new_point->X), 0);
3042     AssertIntEQ(BN_is_zero(new_point->Y), 0);
3043     AssertIntEQ(BN_is_zero(new_point->Z), 0);
3044     AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
3045     AssertIntEQ(BN_is_zero(new_point->X), 0);
3046     AssertIntEQ(BN_is_zero(new_point->Y), 0);
3047     AssertIntEQ(BN_is_zero(new_point->Z), 0);
3048     AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
3049     AssertIntEQ(BN_is_zero(new_point->X), 0);
3050     AssertIntEQ(BN_is_zero(new_point->Y), 0);
3051     AssertIntEQ(BN_is_zero(new_point->Z), 0);
3052 #else
3053     AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
3054     AssertIntEQ(BN_is_zero(new_point->X), 0);
3055     AssertIntEQ(BN_is_zero(new_point->Y), 0);
3056     AssertIntEQ(BN_is_zero(new_point->Z), 0);
3057 #endif
3058 
3059     /* check if point X coordinate is zero */
3060     AssertIntEQ(BN_is_zero(new_point->X), 0);
3061 
3062 #ifdef USE_ECC_B_PARAM
3063     AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
3064 #endif /* USE_ECC_B_PARAM */
3065 
3066     /* Force non-affine coordinates */
3067     AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
3068                                      (WOLFSSL_BIGNUM*)BN_value_one()), 1);
3069     new_point->inSet = 0;
3070 
3071     /* extract the coordinates from point */
3072     AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
3073 
3074     /* check if point X coordinate is zero */
3075     AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
3076 
3077     /* set the same X and Y points in another object */
3078     AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
3079 
3080     /* compare points as they should be the same */
3081     AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
3082 
3083     /* Test copying */
3084     AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
3085 
3086     /* Test inverting */
3087     AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
3088 
3089     AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
3090                                   set_point_bn, ctx), set_point_bn);
3091 
3092     /* check bn2hex */
3093     hexStr = BN_bn2hex(k);
3094     AssertStrEQ(hexStr, kTest);
3095 #ifndef NO_FILESYSTEM
3096     BN_print_fp(stdout, k);
3097     printf("\n");
3098 #endif
3099     XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
3100 
3101     hexStr = BN_bn2hex(Gx);
3102     AssertStrEQ(hexStr, kGx);
3103 #ifndef NO_FILESYSTEM
3104     BN_print_fp(stdout, Gx);
3105     printf("\n");
3106 #endif
3107     XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
3108 
3109     hexStr = BN_bn2hex(Gy);
3110     AssertStrEQ(hexStr, kGy);
3111 #ifndef NO_FILESYSTEM
3112     BN_print_fp(stdout, Gy);
3113     printf("\n");
3114 #endif
3115     XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
3116 
3117 #ifndef HAVE_SELFTEST
3118     hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
3119     AssertStrEQ(hexStr, uncompG);
3120     XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
3121 
3122 #ifdef HAVE_COMP_KEY
3123     hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
3124     AssertStrEQ(hexStr, compG);
3125     XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
3126 #endif
3127 
3128     bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
3129     AssertIntEQ(bin_len, sizeof(binUncompG));
3130     AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
3131     AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
3132                                    bin_len, ctx), bin_len);
3133     AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
3134     XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
3135 
3136 #ifdef HAVE_COMP_KEY
3137     bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
3138     AssertIntEQ(bin_len, sizeof(binCompG));
3139     AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
3140     AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
3141                                    bin_len, ctx), bin_len);
3142     AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
3143     XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
3144 #endif
3145 
3146     AssertNotNull(tmp = EC_POINT_new(group));
3147     AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
3148     AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
3149     EC_POINT_free(tmp);
3150 
3151 #ifdef HAVE_COMP_KEY
3152     AssertNotNull(tmp = EC_POINT_new(group));
3153     AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
3154     AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
3155     EC_POINT_free(tmp);
3156 #endif
3157 #endif
3158 
3159     /* test BN_mod_add */
3160     AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
3161                                          (WOLFSSL_BIGNUM*)BN_value_one(),
3162                                          (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
3163     AssertIntEQ(BN_is_zero(new_point->Z), 1);
3164     /* cleanup */
3165     BN_free(X);
3166     BN_free(Y);
3167     BN_free(k);
3168     BN_free(set_point_bn);
3169     EC_POINT_free(new_point);
3170     EC_POINT_free(set_point);
3171     EC_POINT_free(Gxy);
3172     EC_GROUP_free(group);
3173     EC_GROUP_free(group2);
3174     BN_CTX_free(ctx);
3175 #endif /* HAVE_ECC */
3176 }
3177 #endif /* OPENSSL_EXTRA && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
3178 
3179 #ifndef NO_BIO
test_wolfSSL_PEM_read_bio_ECPKParameters(void)3180 static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
3181 {
3182 #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
3183     EC_GROUP *group;
3184     BIO* bio;
3185 
3186     AssertNotNull(bio = BIO_new(BIO_s_file()));
3187     AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
3188     AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
3189     AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
3190     EC_GROUP_free(group);
3191     BIO_free(bio);
3192 #endif /* HAVE_ECC */
3193 }
3194 #endif /* !NO_BIO */
3195 
3196 # if defined(OPENSSL_EXTRA)
test_wolfSSL_ECDSA_SIG(void)3197 static void test_wolfSSL_ECDSA_SIG(void)
3198 {
3199 #ifdef HAVE_ECC
3200     WOLFSSL_ECDSA_SIG* sig = NULL;
3201     WOLFSSL_ECDSA_SIG* sig2 = NULL;
3202     const unsigned char* cp;
3203     unsigned char* p;
3204     unsigned char outSig[8];
3205     unsigned char sigData[8] =
3206                              { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
3207     sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
3208     AssertNull(sig);
3209     cp = sigData;
3210     AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
3211     AssertIntEQ((cp == sigData + 8), 1);
3212     cp = sigData;
3213     AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
3214     AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
3215     AssertIntEQ((sig == sig2), 1);
3216     cp = outSig;
3217 
3218     p = outSig;
3219     AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
3220     AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
3221     AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
3222     AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
3223     AssertIntEQ((p == outSig + 8), 1);
3224     AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
3225 
3226     wolfSSL_ECDSA_SIG_free(sig);
3227 #endif /* HAVE_ECC */
3228 }
3229 
test_EC_i2d(void)3230 static void test_EC_i2d(void)
3231 {
3232 #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
3233     EC_KEY *key;
3234     EC_KEY *copy;
3235     int len;
3236     unsigned char *buf = NULL;
3237     const unsigned char *tmp = NULL;
3238 
3239     AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
3240     AssertIntEQ(EC_KEY_generate_key(key), 1);
3241 
3242     AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
3243     AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
3244 
3245     XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3246     buf = NULL;
3247 
3248     AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
3249     AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
3250 
3251     tmp = buf;
3252     AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
3253     XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3254     buf = NULL;
3255 
3256     AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
3257     tmp = buf;
3258     AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
3259     AssertIntEQ(EC_KEY_check_key(key), 1);
3260     XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
3261 
3262     EC_KEY_free(key);
3263     EC_KEY_free(copy);
3264 #endif /* HAVE_ECC */
3265 }
3266 
test_ECDSA_size_sign(void)3267 static void test_ECDSA_size_sign(void)
3268 {
3269 #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
3270     EC_KEY *key;
3271     int id;
3272     byte hash[WC_MAX_DIGEST_SIZE];
3273     byte sig[ECC_MAX_SIG_SIZE];
3274     unsigned int sigSz = sizeof(sig);
3275 
3276     XMEMSET(hash, 123, sizeof(hash));
3277 
3278     id = wc_ecc_get_curve_id_from_name("SECP256R1");
3279     AssertIntEQ(id, ECC_SECP256R1);
3280 
3281     AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
3282     AssertIntEQ(EC_KEY_generate_key(key), 1);
3283     AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
3284     AssertIntGE(ECDSA_size(key), sigSz);
3285     AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
3286 
3287     EC_KEY_free(key);
3288 
3289 #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
3290 }
3291 
test_ED25519(void)3292 static void test_ED25519(void)
3293 {
3294 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
3295     defined(WOLFSSL_KEY_GEN)
3296     byte         priv[ED25519_PRV_KEY_SIZE];
3297     unsigned int privSz = (unsigned int)sizeof(priv);
3298     byte         pub[ED25519_PUB_KEY_SIZE];
3299     unsigned int pubSz = (unsigned int)sizeof(pub);
3300 #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
3301     const char*  msg = TEST_STRING;
3302     unsigned int msglen = (unsigned int)TEST_STRING_SZ;
3303     byte         sig[ED25519_SIG_SIZE];
3304     unsigned int sigSz = (unsigned int)sizeof(sig);
3305 #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
3306 
3307     AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
3308                 WOLFSSL_SUCCESS);
3309     AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
3310     AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
3311 
3312 #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
3313     AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
3314                                       &sigSz), WOLFSSL_SUCCESS);
3315     AssertIntEQ(sigSz, ED25519_SIG_SIZE);
3316 
3317 #ifdef HAVE_ED25519_VERIFY
3318     AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
3319                                        sigSz), WOLFSSL_SUCCESS);
3320 #endif /* HAVE_ED25519_VERIFY */
3321 #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
3322 #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
3323 }
3324 
test_ED448(void)3325 static void test_ED448(void)
3326 {
3327 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
3328     defined(WOLFSSL_KEY_GEN)
3329     byte         priv[ED448_PRV_KEY_SIZE];
3330     unsigned int privSz = (unsigned int)sizeof(priv);
3331     byte         pub[ED448_PUB_KEY_SIZE];
3332     unsigned int pubSz = (unsigned int)sizeof(pub);
3333 #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
3334     const char*  msg = TEST_STRING;
3335     unsigned int msglen = (unsigned int)TEST_STRING_SZ;
3336     byte         sig[ED448_SIG_SIZE];
3337     unsigned int sigSz = (unsigned int)sizeof(sig);
3338 #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
3339 
3340     AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
3341                 WOLFSSL_SUCCESS);
3342     AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
3343     AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
3344 
3345 #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
3346     AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
3347                                    &sigSz), WOLFSSL_SUCCESS);
3348     AssertIntEQ(sigSz, ED448_SIG_SIZE);
3349 
3350 #ifdef HAVE_ED448_VERIFY
3351     AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
3352                                      sigSz), WOLFSSL_SUCCESS);
3353 #endif /* HAVE_ED448_VERIFY */
3354 #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
3355 #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
3356 }
3357 #endif /* OPENSSL_EXTRA */
3358 
3359 #include <wolfssl/openssl/pem.h>
3360 /*----------------------------------------------------------------------------*
3361  | EVP
3362  *----------------------------------------------------------------------------*/
3363 
test_wolfSSL_EVP_PKEY_print_public(void)3364 static void test_wolfSSL_EVP_PKEY_print_public(void)
3365 {
3366 #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
3367 
3368     WOLFSSL_BIO* rbio = NULL;
3369     WOLFSSL_BIO* wbio = NULL;
3370     WOLFSSL_EVP_PKEY* pkey = NULL;
3371     char line[256] = { 0 };
3372     char line1[256] = { 0 };
3373     int i;
3374 
3375     printf(testingFmt, "EVP_PKEY_print_public()");
3376     /* test error cases */
3377     AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
3378 
3379     /*
3380      *  test RSA public key print
3381      *  in this test, pass '3' for indent
3382      */
3383 #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
3384 
3385     rbio = BIO_new_mem_buf( client_keypub_der_1024,
3386                             sizeof_client_keypub_der_1024);
3387     AssertNotNull(rbio);
3388 
3389     wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
3390     AssertNotNull(pkey);
3391 
3392     wbio = BIO_new(BIO_s_mem());
3393     AssertNotNull(wbio);
3394 
3395     AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
3396 
3397     BIO_gets(wbio, line, sizeof(line));
3398     strcpy(line1, "   RSA Public-Key: (1024 bit)\n");
3399     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3400 
3401     BIO_gets(wbio, line, sizeof(line));
3402     strcpy(line1, "   Modulus:\n");
3403     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3404 
3405     BIO_gets(wbio, line, sizeof(line));
3406     strcpy(line1, "       00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
3407     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3408 
3409 
3410     /* skip to the end of modulus element*/
3411     for( i = 0; i < 8 ;i++) {
3412         BIO_gets(wbio, line, sizeof(line));
3413     }
3414 
3415     BIO_gets(wbio, line, sizeof(line));
3416     strcpy(line1, "   Exponent: 65537 (0x010001)\n");
3417     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3418 
3419 
3420     /* should reach EOF */
3421     AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
3422 
3423     EVP_PKEY_free(pkey);
3424     pkey = NULL;
3425     BIO_free(rbio);
3426     BIO_free(wbio);
3427     rbio = NULL;
3428     wbio = NULL;
3429 
3430 #endif  /* !NO_RSA && USE_CERT_BUFFERS_1024*/
3431 
3432     /*
3433      *  test DSA public key print
3434      */
3435 #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
3436     rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
3437                             sizeof_dsa_pub_key_der_2048);
3438     AssertNotNull(rbio);
3439 
3440     wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
3441     AssertNotNull(pkey);
3442 
3443     wbio = BIO_new(BIO_s_mem());
3444     AssertNotNull(wbio);
3445 
3446     AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
3447 
3448     BIO_gets(wbio, line, sizeof(line));
3449     strcpy(line1, "DSA Public-Key: (2048 bit)\n");
3450     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3451 
3452     BIO_gets(wbio, line, sizeof(line));
3453     strcpy(line1, "pub:\n");
3454     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3455 
3456     BIO_gets(wbio, line, sizeof(line));
3457     strcpy(line1,
3458         "    00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
3459     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3460 
3461     /* skip to the end of pub element*/
3462     for( i = 0; i < 17 ;i++) {
3463         BIO_gets(wbio, line, sizeof(line));
3464     }
3465 
3466     BIO_gets(wbio, line, sizeof(line));
3467     strcpy(line1, "P:\n");
3468     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3469 
3470     /* skip to the end of P element*/
3471     for( i = 0; i < 18 ;i++) {
3472         BIO_gets(wbio, line, sizeof(line));
3473     }
3474 
3475     BIO_gets(wbio, line, sizeof(line));
3476     strcpy(line1, "Q:\n");
3477     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3478 
3479     /* skip to the end of Q element*/
3480     for( i = 0; i < 3 ;i++) {
3481         BIO_gets(wbio, line, sizeof(line));
3482     }
3483     BIO_gets(wbio, line, sizeof(line));
3484     strcpy(line1, "G:\n");
3485     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3486 
3487     /* skip to the end of G element*/
3488     for( i = 0; i < 18 ;i++) {
3489         BIO_gets(wbio, line, sizeof(line));
3490     }
3491     /* should reach EOF */
3492     AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
3493 
3494     EVP_PKEY_free(pkey);
3495     pkey = NULL;
3496     BIO_free(rbio);
3497     BIO_free(wbio);
3498     rbio = NULL;
3499     wbio = NULL;
3500 
3501 #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
3502 
3503     /*
3504      *  test ECC public key print
3505      */
3506 #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
3507 
3508     rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
3509                             sizeof_ecc_clikeypub_der_256);
3510     AssertNotNull(rbio);
3511 
3512     wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
3513     AssertNotNull(pkey);
3514 
3515     wbio = BIO_new(BIO_s_mem());
3516     AssertNotNull(wbio);
3517 
3518     AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
3519 
3520     BIO_gets(wbio, line, sizeof(line));
3521     strcpy(line1, "Public-Key: (256 bit)\n");
3522     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3523 
3524     BIO_gets(wbio, line, sizeof(line));
3525     strcpy(line1, "pub:\n");
3526     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3527 
3528     BIO_gets(wbio, line, sizeof(line));
3529     strcpy(line1,
3530             "    04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
3531     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3532 
3533     /* skip to the end of pub element*/
3534     for( i = 0; i < 4 ;i++) {
3535         BIO_gets(wbio, line, sizeof(line));
3536     }
3537 
3538     BIO_gets(wbio, line, sizeof(line));
3539     strcpy(line1, "ASN1 OID: prime256v1\n");
3540     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3541 
3542     BIO_gets(wbio, line, sizeof(line));
3543     strcpy(line1, "NIST CURVE: P-256\n");
3544     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3545 
3546 
3547     /* should reach EOF */
3548     AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
3549 
3550     EVP_PKEY_free(pkey);
3551     pkey = NULL;
3552     BIO_free(rbio);
3553     BIO_free(wbio);
3554     rbio = NULL;
3555     wbio = NULL;
3556 
3557 #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
3558 
3559     /*
3560      *  test DH public key print
3561      */
3562 #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
3563 
3564     rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
3565                             sizeof_dh_pub_key_der_2048);
3566     AssertNotNull(rbio);
3567 
3568     wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
3569     AssertNotNull(pkey);
3570 
3571     wbio = BIO_new(BIO_s_mem());
3572     AssertNotNull(wbio);
3573 
3574     AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
3575 
3576     BIO_gets(wbio, line, sizeof(line));
3577     strcpy(line1, "DH Public-Key: (2048 bit)\n");
3578     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3579 
3580     BIO_gets(wbio, line, sizeof(line));
3581     strcpy(line1, "public-key:\n");
3582     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3583 
3584     BIO_gets(wbio, line, sizeof(line));
3585     strcpy(line1,
3586         "    34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
3587     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3588 
3589     /* skip to the end of public-key element*/
3590     for( i = 0; i < 17 ;i++) {
3591         BIO_gets(wbio, line, sizeof(line));
3592     }
3593 
3594     BIO_gets(wbio, line, sizeof(line));
3595     strcpy(line1, "prime:\n");
3596     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3597 
3598     BIO_gets(wbio, line, sizeof(line));
3599     strcpy(line1,
3600         "    00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
3601     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3602 
3603     /* skip to the end of prime element*/
3604     for( i = 0; i < 17 ;i++) {
3605         BIO_gets(wbio, line, sizeof(line));
3606     }
3607 
3608     BIO_gets(wbio, line, sizeof(line));
3609     strcpy(line1, "generator: 2 (0x02)\n");
3610     AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
3611 
3612     /* should reach EOF */
3613     AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
3614 
3615     EVP_PKEY_free(pkey);
3616     pkey = NULL;
3617     BIO_free(rbio);
3618     BIO_free(wbio);
3619     rbio = NULL;
3620     wbio = NULL;
3621 
3622 #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
3623 
3624     /* to prevent "unused variable" warning */
3625     (void)pkey;
3626     (void)wbio;
3627     (void)rbio;
3628     (void)line;
3629     (void)line1;
3630     (void)i;
3631     printf(resultFmt, passed);
3632 #endif /* OPENSSL_EXTRA */
3633 }
3634 /* Test functions for base64 encode/decode */
test_wolfSSL_EVP_ENCODE_CTX_new(void)3635 static void test_wolfSSL_EVP_ENCODE_CTX_new(void)
3636 {
3637 #if defined(OPENSSL_EXTRA) && \
3638 ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
3639     printf(testingFmt, "EVP_ENCODE_CTX_new()");
3640 
3641     EVP_ENCODE_CTX* ctx = NULL;
3642 
3643     AssertNotNull( ctx = EVP_ENCODE_CTX_new());
3644     AssertIntEQ( ctx->remaining,0);
3645     AssertIntEQ( ctx->data[0],0);
3646     AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
3647     EVP_ENCODE_CTX_free(ctx);
3648 
3649     printf(resultFmt, passed);
3650 #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
3651 }
test_wolfSSL_EVP_ENCODE_CTX_free(void)3652 static void test_wolfSSL_EVP_ENCODE_CTX_free(void)
3653 {
3654 #if defined(OPENSSL_EXTRA) && \
3655 ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
3656     printf(testingFmt, "EVP_ENCODE_CTX_free()");
3657     EVP_ENCODE_CTX* ctx = NULL;
3658 
3659     AssertNotNull( ctx = EVP_ENCODE_CTX_new());
3660     EVP_ENCODE_CTX_free(ctx);
3661     printf(resultFmt, passed);
3662 #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
3663 }
3664 
test_wolfSSL_EVP_EncodeInit(void)3665 static void test_wolfSSL_EVP_EncodeInit(void)
3666 {
3667 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
3668     printf(testingFmt, "EVP_EncodeInit()");
3669     EVP_ENCODE_CTX* ctx = NULL;
3670 
3671     AssertNotNull( ctx = EVP_ENCODE_CTX_new());
3672     AssertIntEQ( ctx->remaining,0);
3673     AssertIntEQ( ctx->data[0],0);
3674     AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
3675 
3676     /* make ctx dirty */
3677     ctx->remaining = 10;
3678     XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
3679 
3680     EVP_EncodeInit(ctx);
3681 
3682     AssertIntEQ( ctx->remaining,0);
3683     AssertIntEQ( ctx->data[0],0);
3684     AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
3685 
3686     EVP_ENCODE_CTX_free(ctx);
3687     printf(resultFmt, passed);
3688 #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
3689 }
test_wolfSSL_EVP_EncodeUpdate(void)3690 static void test_wolfSSL_EVP_EncodeUpdate(void)
3691 {
3692 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
3693     printf(testingFmt, "EVP_EncodeUpdate()");
3694     int outl;
3695     int total;
3696 
3697     const unsigned char plain0[] = {"Th"};
3698     const unsigned char plain1[] = {"This is a base64 encodeing test."};
3699     const unsigned char plain2[] = {"This is additional data."};
3700 
3701     const unsigned char enc0[]   = {"VGg=\n"};
3702     /* expected encoded result for the first output 64 chars plus trailing LF*/
3703     const unsigned char enc1[]   = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
3704 
3705     const unsigned char enc2[]   =
3706     {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
3707 
3708     unsigned char encOutBuff[300];
3709 
3710     EVP_ENCODE_CTX* ctx = NULL;
3711     AssertNotNull( ctx = EVP_ENCODE_CTX_new());
3712 
3713     EVP_EncodeInit(ctx);
3714 
3715     /* illegal parameter test */
3716     AssertIntEQ(
3717         EVP_EncodeUpdate(
3718             NULL,            /* pass NULL as ctx */
3719             encOutBuff,
3720             &outl,
3721             plain1,
3722             sizeof(plain1)-1),
3723         0                    /* expected result code 0: fail */
3724     );
3725 
3726     AssertIntEQ(
3727         EVP_EncodeUpdate(
3728             ctx,
3729             NULL,           /* pass NULL as out buff */
3730             &outl,
3731             plain1,
3732             sizeof(plain1)-1),
3733         0                    /* expected result code 0: fail */
3734     );
3735 
3736     AssertIntEQ(
3737         EVP_EncodeUpdate(
3738             ctx,
3739             encOutBuff,
3740             NULL,            /* pass NULL as outl */
3741             plain1,
3742             sizeof(plain1)-1),
3743         0                    /* expected result code 0: fail */
3744     );
3745 
3746     AssertIntEQ(
3747         EVP_EncodeUpdate(
3748             ctx,
3749             encOutBuff,
3750             &outl,
3751             NULL,            /* pass NULL as in */
3752             sizeof(plain1)-1),
3753         0                    /* expected result code 0: fail */
3754     );
3755 
3756     /* meaningless parameter test */
3757 
3758     AssertIntEQ(
3759         EVP_EncodeUpdate(
3760             ctx,
3761             encOutBuff,
3762             &outl,
3763             plain1,
3764             0),              /* pass zero input */
3765         1                    /* expected result code 1: success */
3766     );
3767 
3768     /* very small data encoding test */
3769 
3770     EVP_EncodeInit(ctx);
3771 
3772     AssertIntEQ(
3773         EVP_EncodeUpdate(
3774             ctx,
3775             encOutBuff,
3776             &outl,
3777             plain0,
3778             sizeof(plain0)-1),
3779         1                    /* expected result code 1: success */
3780     );
3781     AssertIntEQ(outl,0);
3782 
3783         EVP_EncodeFinal(
3784             ctx,
3785             encOutBuff + outl,
3786             &outl);
3787 
3788     AssertIntEQ( outl, sizeof(enc0)-1);
3789     AssertIntEQ(
3790         XSTRNCMP(
3791             (const char*)encOutBuff,
3792             (const char*)enc0,sizeof(enc0) ),
3793     0);
3794 
3795     /* pass small size( < 48bytes ) input, then make sure they are not
3796      * encoded  and just stored in ctx
3797      */
3798 
3799     EVP_EncodeInit(ctx);
3800 
3801     total = 0;
3802     outl = 0;
3803     XMEMSET( encOutBuff,0, sizeof(encOutBuff));
3804 
3805     AssertIntEQ(
3806     EVP_EncodeUpdate(
3807         ctx,
3808         encOutBuff,         /* buffer for output */
3809         &outl,              /* size of output */
3810         plain1,             /* input */
3811         sizeof(plain1)-1),  /* size of input */
3812         1);                 /* expected result code 1:success */
3813 
3814     total += outl;
3815 
3816     AssertIntEQ(outl, 0);  /* no output expected */
3817     AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
3818     AssertTrue(
3819         XSTRNCMP((const char*)(ctx->data),
3820                  (const char*)plain1,
3821                  ctx->remaining) ==0 );
3822     AssertTrue(encOutBuff[0] == 0);
3823 
3824     /* call wolfSSL_EVP_EncodeUpdate again to make it encode
3825      * the stored data and the new input together
3826      */
3827     AssertIntEQ(
3828     EVP_EncodeUpdate(
3829         ctx,
3830         encOutBuff + outl,  /* buffer for output */
3831         &outl,              /* size of output */
3832         plain2,             /* additional input */
3833         sizeof(plain2) -1), /* size of additional input */
3834         1);                 /* expected result code 1:success */
3835 
3836     total += outl;
3837 
3838     AssertIntNE(outl, 0);   /* some output is expected this time*/
3839     AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
3840     AssertIntEQ(
3841         XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
3842 
3843     /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
3844     EVP_EncodeFinal(
3845         ctx,
3846         encOutBuff + outl,
3847         &outl);
3848 
3849     total += outl;
3850 
3851     AssertIntNE(total,0);
3852     AssertIntNE(outl,0);
3853     AssertIntEQ(XSTRNCMP(
3854         (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
3855 
3856     /* test with illeagal parameters */
3857     outl = 1;
3858     EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
3859     AssertIntEQ(outl, 0);
3860     outl = 1;
3861     EVP_EncodeFinal(ctx, NULL, &outl);
3862     AssertIntEQ(outl, 0);
3863     EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
3864     EVP_EncodeFinal(NULL, NULL, NULL);
3865 
3866     EVP_ENCODE_CTX_free(ctx);
3867     printf(resultFmt, passed);
3868 #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
3869 }
test_wolfSSL_EVP_EncodeFinal(void)3870 static void test_wolfSSL_EVP_EncodeFinal(void)
3871 {
3872 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
3873     printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
3874 
3875     /* tests for wolfSSL_EVP_EncodeFinal are included in
3876      * test_wolfSSL_EVP_EncodeUpdate
3877      */
3878     printf(resultFmt, passed);
3879 #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
3880 }
3881 
3882 
test_wolfSSL_EVP_DecodeInit(void)3883 static void test_wolfSSL_EVP_DecodeInit(void)
3884 {
3885 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
3886     printf(testingFmt, "EVP_DecodeInit()");
3887 
3888     EVP_ENCODE_CTX* ctx = NULL;
3889 
3890     AssertNotNull( ctx = EVP_ENCODE_CTX_new());
3891     AssertIntEQ( ctx->remaining,0);
3892     AssertIntEQ( ctx->data[0],0);
3893     AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
3894 
3895     /* make ctx dirty */
3896     ctx->remaining = 10;
3897     XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
3898 
3899     EVP_DecodeInit(ctx);
3900 
3901     AssertIntEQ( ctx->remaining,0);
3902     AssertIntEQ( ctx->data[0],0);
3903     AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
3904 
3905     EVP_ENCODE_CTX_free(ctx);
3906     printf(resultFmt, passed);
3907 #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
3908 }
test_wolfSSL_EVP_DecodeUpdate(void)3909 static void test_wolfSSL_EVP_DecodeUpdate(void)
3910 {
3911 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
3912     printf(testingFmt, "EVP_DecodeUpdate()");
3913 
3914     int outl;
3915     unsigned char decOutBuff[300];
3916 
3917     EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
3918     EVP_DecodeInit(ctx);
3919 
3920     const unsigned char enc1[]   =
3921             {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
3922 /*    const unsigned char plain1[] =
3923     {"This is a base64 decoding test."} */
3924 
3925     /* illegal parameter tests */
3926 
3927     /* pass NULL as ctx */
3928     AssertIntEQ(
3929         EVP_DecodeUpdate(
3930             NULL,            /* pass NULL as ctx */
3931             decOutBuff,
3932             &outl,
3933             enc1,
3934             sizeof(enc1)-1),
3935         -1                    /* expected result code -1: fail */
3936     );
3937     AssertIntEQ( outl, 0);
3938 
3939     /* pass NULL as output */
3940     AssertIntEQ(
3941         EVP_DecodeUpdate(
3942             ctx,
3943             NULL,           /* pass NULL as out buff */
3944             &outl,
3945             enc1,
3946             sizeof(enc1)-1),
3947         -1                    /* expected result code -1: fail */
3948     );
3949     AssertIntEQ( outl, 0);
3950 
3951     /* pass NULL as outl */
3952     AssertIntEQ(
3953         EVP_DecodeUpdate(
3954             ctx,
3955             decOutBuff,
3956             NULL,            /* pass NULL as outl */
3957             enc1,
3958             sizeof(enc1)-1),
3959         -1                   /* expected result code -1: fail */
3960     );
3961 
3962     /* pass NULL as input */
3963     AssertIntEQ(
3964         EVP_DecodeUpdate(
3965             ctx,
3966             decOutBuff,
3967             &outl,
3968             NULL,            /* pass NULL as in */
3969             sizeof(enc1)-1),
3970         -1                    /* expected result code -1: fail */
3971     );
3972     AssertIntEQ( outl, 0);
3973 
3974     /* pass zero length input */
3975 
3976     AssertIntEQ(
3977         EVP_DecodeUpdate(
3978             ctx,
3979             decOutBuff,
3980             &outl,
3981             enc1,
3982             0),              /* pass zero as input len */
3983         1                    /* expected result code 1: success */
3984     );
3985 
3986     /* decode correct base64 string */
3987 
3988         const unsigned char enc2[]   =
3989         {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
3990         const unsigned char plain2[] =
3991         {"This is a base64 decoding test."};
3992 
3993     EVP_EncodeInit(ctx);
3994 
3995     AssertIntEQ(
3996         EVP_DecodeUpdate(
3997             ctx,
3998             decOutBuff,
3999             &outl,
4000             enc2,
4001             sizeof(enc2)-1),
4002         0                    /* expected result code 0: success */
4003     );
4004 
4005     AssertIntEQ(outl,sizeof(plain2) -1);
4006 
4007     AssertIntEQ(
4008         EVP_DecodeFinal(
4009             ctx,
4010             decOutBuff + outl,
4011             &outl),
4012         1                    /* expected result code 1: success */
4013     );
4014     AssertIntEQ(outl, 0);   /* expected DecodeFinal outout no data */
4015 
4016     AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
4017                 sizeof(plain2) -1 ),0);
4018 
4019     /* decode correct base64 string which does not have '\n' in its last*/
4020 
4021         const unsigned char enc3[]   =
4022         {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
4023         const unsigned char plain3[] =
4024         {"This is a base64 decoding test."}; /* 31 chars */
4025 
4026     EVP_EncodeInit(ctx);
4027 
4028     AssertIntEQ(
4029         EVP_DecodeUpdate(
4030             ctx,
4031             decOutBuff,
4032             &outl,
4033             enc3,
4034             sizeof(enc3)-1),
4035         0                    /* expected result code 0: success */
4036     );
4037 
4038     AssertIntEQ(outl,sizeof(plain3)-1);   /* 31 chars should be output */
4039 
4040     AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
4041                 sizeof(plain3) -1 ),0);
4042 
4043     AssertIntEQ(
4044         EVP_DecodeFinal(
4045             ctx,
4046             decOutBuff + outl,
4047             &outl),
4048         1                    /* expected result code 1: success */
4049     );
4050 
4051     AssertIntEQ(outl,0 );
4052 
4053     /* decode string which has a padding char ('=') in the illegal position*/
4054 
4055         const unsigned char enc4[]   =
4056             {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
4057 
4058 
4059     EVP_EncodeInit(ctx);
4060 
4061     AssertIntEQ(
4062         EVP_DecodeUpdate(
4063             ctx,
4064             decOutBuff,
4065             &outl,
4066             enc4,
4067             sizeof(enc4)-1),
4068         -1                    /* expected result code -1: error */
4069     );
4070     AssertIntEQ(outl,0);
4071 
4072     /* small data decode test */
4073 
4074     const unsigned char enc00[]   = {"VG"};
4075     const unsigned char enc01[]   = {"g=\n"};
4076     const unsigned char plain4[]  = {"Th"};
4077 
4078     EVP_EncodeInit(ctx);
4079 
4080     AssertIntEQ(
4081         EVP_DecodeUpdate(
4082             ctx,
4083             decOutBuff,
4084             &outl,
4085             enc00,
4086             sizeof(enc00)-1),
4087         1                    /* expected result code 1: success */
4088     );
4089     AssertIntEQ(outl,0);
4090 
4091     AssertIntEQ(
4092         EVP_DecodeUpdate(
4093             ctx,
4094             decOutBuff + outl,
4095             &outl,
4096             enc01,
4097             sizeof(enc01)-1),
4098         0                    /* expected result code 0: success */
4099     );
4100 
4101     AssertIntEQ(outl,sizeof(plain4)-1);
4102 
4103     /* test with illegal parameters */
4104     AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
4105     AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
4106     AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
4107     AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
4108 
4109     EVP_DecodeFinal(
4110             ctx,
4111             decOutBuff + outl,
4112             &outl);
4113 
4114     AssertIntEQ( outl, 0);
4115     AssertIntEQ(
4116         XSTRNCMP(
4117             (const char*)decOutBuff,
4118             (const char*)plain4,sizeof(plain4)-1 ),
4119         0);
4120 
4121     EVP_ENCODE_CTX_free(ctx);
4122 
4123     printf(resultFmt, passed);
4124 #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
4125 }
test_wolfSSL_EVP_DecodeFinal(void)4126 static void test_wolfSSL_EVP_DecodeFinal(void)
4127 {
4128 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
4129     printf(testingFmt, "EVP_DecodeFinal()");
4130     /* tests for wolfSSL_EVP_DecodeFinal are included in
4131      * test_wolfSSL_EVP_DecodeUpdate
4132      */
4133     printf(resultFmt, passed);
4134 #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
4135 }
4136 
4137 /* Test function for wolfSSL_EVP_get_cipherbynid.
4138  */
4139 
4140 #ifdef OPENSSL_EXTRA
test_wolfSSL_EVP_get_cipherbynid(void)4141 static void test_wolfSSL_EVP_get_cipherbynid(void)
4142 {
4143 #ifndef NO_AES
4144     const WOLFSSL_EVP_CIPHER* c;
4145 
4146     c = wolfSSL_EVP_get_cipherbynid(419);
4147     #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
4148          defined(WOLFSSL_AES_128)
4149         AssertNotNull(c);
4150         AssertNotNull(strcmp("EVP_AES_128_CBC", c));
4151     #else
4152         AssertNull(c);
4153     #endif
4154 
4155     c = wolfSSL_EVP_get_cipherbynid(423);
4156     #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
4157          defined(WOLFSSL_AES_192)
4158         AssertNotNull(c);
4159         AssertNotNull(strcmp("EVP_AES_192_CBC", c));
4160     #else
4161         AssertNull(c);
4162     #endif
4163 
4164     c = wolfSSL_EVP_get_cipherbynid(427);
4165     #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
4166          defined(WOLFSSL_AES_256)
4167         AssertNotNull(c);
4168         AssertNotNull(strcmp("EVP_AES_256_CBC", c));
4169     #else
4170         AssertNull(c);
4171     #endif
4172 
4173     c = wolfSSL_EVP_get_cipherbynid(904);
4174     #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
4175         AssertNotNull(c);
4176         AssertNotNull(strcmp("EVP_AES_128_CTR", c));
4177     #else
4178         AssertNull(c);
4179     #endif
4180 
4181     c = wolfSSL_EVP_get_cipherbynid(905);
4182     #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
4183         AssertNotNull(c);
4184         AssertNotNull(strcmp("EVP_AES_192_CTR", c));
4185     #else
4186         AssertNull(c);
4187     #endif
4188 
4189     c = wolfSSL_EVP_get_cipherbynid(906);
4190     #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
4191         AssertNotNull(c);
4192         AssertNotNull(strcmp("EVP_AES_256_CTR", c));
4193     #else
4194         AssertNull(c);
4195     #endif
4196 
4197     c = wolfSSL_EVP_get_cipherbynid(418);
4198     #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
4199         AssertNotNull(c);
4200         AssertNotNull(strcmp("EVP_AES_128_ECB", c));
4201     #else
4202         AssertNull(c);
4203     #endif
4204 
4205     c = wolfSSL_EVP_get_cipherbynid(422);
4206     #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
4207         AssertNotNull(c);
4208         AssertNotNull(strcmp("EVP_AES_192_ECB", c));
4209     #else
4210         AssertNull(c);
4211     #endif
4212 
4213     c = wolfSSL_EVP_get_cipherbynid(426);
4214     #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
4215         AssertNotNull(c);
4216         AssertNotNull(strcmp("EVP_AES_256_ECB", c));
4217     #else
4218         AssertNull(c);
4219     #endif
4220 #endif /* !NO_AES */
4221 
4222 #ifndef NO_DES3
4223     AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
4224 #ifdef WOLFSSL_DES_ECB
4225     AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
4226 #endif
4227     AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
4228 #ifdef WOLFSSL_DES_ECB
4229     AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
4230 #endif
4231 #endif /* !NO_DES3 */
4232 
4233 #ifdef HAVE_IDEA
4234     AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
4235 #endif
4236 
4237   /* test for nid is out of range */
4238   AssertNull(wolfSSL_EVP_get_cipherbynid(1));
4239 
4240 }
4241 
test_wolfSSL_EVP_CIPHER_CTX(void)4242 static void test_wolfSSL_EVP_CIPHER_CTX(void)
4243 {
4244 #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
4245     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
4246     const EVP_CIPHER *init = EVP_aes_128_cbc();
4247     const EVP_CIPHER *test;
4248     byte key[AES_BLOCK_SIZE] = {0};
4249     byte iv[AES_BLOCK_SIZE] = {0};
4250 
4251     AssertNotNull(ctx);
4252     wolfSSL_EVP_CIPHER_CTX_init(ctx);
4253     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
4254     test = EVP_CIPHER_CTX_cipher(ctx);
4255     AssertTrue(init == test);
4256     AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
4257 
4258     AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
4259     AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
4260 
4261     EVP_CIPHER_CTX_free(ctx);
4262     /* test EVP_CIPHER_CTX_cleanup with NULL */
4263     AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
4264 #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
4265 }
4266 #endif /* OPENSSL_EXTRA */
4267 
4268 /*----------------------------------------------------------------------------*
4269  | IO
4270  *----------------------------------------------------------------------------*/
4271 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
4272     !defined(NO_RSA)        && !defined(SINGLE_THREADED) && \
4273     !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
4274 #define HAVE_IO_TESTS_DEPENDENCIES
4275 #endif
4276 
4277 /* helper functions */
4278 #ifdef HAVE_IO_TESTS_DEPENDENCIES
4279 
4280 #ifdef WOLFSSL_SESSION_EXPORT
4281 #ifdef WOLFSSL_DTLS
4282 /* set up function for sending session information */
test_export(WOLFSSL * inSsl,byte * buf,word32 sz,void * userCtx)4283 static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
4284 {
4285     WOLFSSL_CTX* ctx = NULL;
4286     WOLFSSL*     ssl = NULL;
4287 
4288     AssertNotNull(inSsl);
4289     AssertNotNull(buf);
4290     AssertIntNE(0, sz);
4291 
4292     /* Set ctx to DTLS 1.2 */
4293     ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
4294     AssertNotNull(ctx);
4295 
4296     ssl = wolfSSL_new(ctx);
4297     AssertNotNull(ssl);
4298 
4299     AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
4300 
4301     wolfSSL_free(ssl);
4302     wolfSSL_CTX_free(ctx);
4303     (void)userCtx;
4304     return WOLFSSL_SUCCESS;
4305 }
4306 #endif
4307 
4308 /* returns negative value on fail and positive (including 0) on success */
nonblocking_accept_read(void * args,WOLFSSL * ssl,SOCKET_T * sockfd)4309 static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
4310 {
4311     int ret, err, loop_count, count, timeout = 10;
4312     char msg[] = "I hear you fa shizzle!";
4313     char input[1024];
4314 
4315     loop_count = ((func_args*)args)->argc;
4316 
4317     err = 0; /* Reset error */
4318     do {
4319     #ifdef WOLFSSL_ASYNC_CRYPT
4320         if (err == WC_PENDING_E) {
4321             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
4322             if (ret < 0) { break; } else if (ret == 0) { continue; }
4323         }
4324     #endif
4325         ret = wolfSSL_accept(ssl);
4326         err = wolfSSL_get_error(ssl, 0);
4327 
4328         if (err == WOLFSSL_ERROR_WANT_READ ||
4329             err == WOLFSSL_ERROR_WANT_WRITE) {
4330             int select_ret;
4331 
4332             err = WC_PENDING_E;
4333             select_ret = tcp_select(*sockfd, timeout);
4334             if (select_ret == TEST_TIMEOUT) {
4335                 return WOLFSSL_FATAL_ERROR;
4336             }
4337         }
4338     } while (err == WC_PENDING_E);
4339     if (ret != WOLFSSL_SUCCESS) {
4340         char buff[WOLFSSL_MAX_ERROR_SZ];
4341         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
4342         return ret;
4343     }
4344 
4345     for (count = 0; count < loop_count; count++) {
4346         int select_ret;
4347 
4348         select_ret = tcp_select(*sockfd, timeout);
4349         if (select_ret == TEST_TIMEOUT) {
4350             ret = WOLFSSL_FATAL_ERROR;
4351             break;
4352         }
4353 
4354         do {
4355             ret = wolfSSL_read(ssl, input, sizeof(input)-1);
4356             if (ret > 0) {
4357                 input[ret] = '\0';
4358                 printf("Client message: %s\n", input);
4359             }
4360         } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
4361 
4362         do {
4363             if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
4364                 return WOLFSSL_FATAL_ERROR;
4365             }
4366             err = wolfSSL_get_error(ssl, ret);
4367         } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
4368     }
4369     return ret;
4370 }
4371 #endif /* WOLFSSL_SESSION_EXPORT */
4372 
4373 /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
4374 #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
4375     defined(HAVE_AES_CBC)
4376 
4377     typedef struct openssl_key_ctx {
4378         byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
4379         byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
4380         byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
4381         byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
4382     } openssl_key_ctx;
4383 
4384     static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
4385     static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
4386 
OpenSSLTicketInit(void)4387     static WC_INLINE int OpenSSLTicketInit(void)
4388     {
4389         int ret = wc_InitRng(&myOpenSSLKey_rng);
4390         if (ret != 0) return ret;
4391 
4392         ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
4393                 sizeof(myOpenSSLKey_ctx.name));
4394         if (ret != 0) return ret;
4395 
4396         ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
4397                 sizeof(myOpenSSLKey_ctx.key));
4398         if (ret != 0) return ret;
4399 
4400         ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
4401                 sizeof(myOpenSSLKey_ctx.hmacKey));
4402         if (ret != 0) return ret;
4403 
4404         ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
4405                 sizeof(myOpenSSLKey_ctx.iv));
4406         if (ret != 0) return ret;
4407 
4408         return 0;
4409     }
4410 
myTicketEncCbOpenSSL(WOLFSSL * ssl,byte name[WOLFSSL_TICKET_NAME_SZ],byte iv[WOLFSSL_TICKET_IV_SZ],WOLFSSL_EVP_CIPHER_CTX * ectx,WOLFSSL_HMAC_CTX * hctx,int enc)4411     static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
4412                              byte name[WOLFSSL_TICKET_NAME_SZ],
4413                              byte iv[WOLFSSL_TICKET_IV_SZ],
4414                              WOLFSSL_EVP_CIPHER_CTX *ectx,
4415                              WOLFSSL_HMAC_CTX *hctx, int enc) {
4416         (void)ssl;
4417         if (enc) {
4418             XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
4419             XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
4420         }
4421         else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
4422                             sizeof(myOpenSSLKey_ctx.name)) != 0 ||
4423                  XMEMCMP(iv, myOpenSSLKey_ctx.iv,
4424                             sizeof(myOpenSSLKey_ctx.iv)) != 0) {
4425             return 0;
4426         }
4427         HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
4428         if (enc)
4429             EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
4430         else
4431             EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
4432         return 1;
4433     }
4434 
OpenSSLTicketCleanup(void)4435     static WC_INLINE void OpenSSLTicketCleanup(void)
4436     {
4437         wc_FreeRng(&myOpenSSLKey_rng);
4438     }
4439 #endif
4440 
4441 #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
4442     #ifdef WC_SHA512_DIGEST_SIZE
4443         #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
4444     #else
4445         #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
4446     #endif
4447     byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
4448     byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
4449     byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
4450     byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
4451 #endif
test_server_nofail(void * args)4452 static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
4453 {
4454     SOCKET_T sockfd = 0;
4455     SOCKET_T clientfd = 0;
4456     word16 port;
4457 
4458     callback_functions* cbf;
4459     WOLFSSL_CTX* ctx = 0;
4460     WOLFSSL* ssl = 0;
4461 
4462     char msg[] = "I hear you fa shizzle!";
4463     char input[1024];
4464     int idx;
4465     int ret, err = 0;
4466     int sharedCtx = 0;
4467 #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
4468     size_t msg_len = 0;
4469 #endif
4470 
4471 #ifdef WOLFSSL_TIRTOS
4472     fdOpenSession(Task_self());
4473 #endif
4474 
4475     ((func_args*)args)->return_code = TEST_FAIL;
4476     cbf = ((func_args*)args)->callbacks;
4477 
4478 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4479     if (cbf != NULL && cbf->ctx) {
4480         ctx = cbf->ctx;
4481         sharedCtx = 1;
4482     }
4483     else
4484 #endif
4485     {
4486         WOLFSSL_METHOD* method = NULL;
4487         if (cbf != NULL && cbf->method != NULL) {
4488             method = cbf->method();
4489         }
4490         else {
4491             method = wolfSSLv23_server_method();
4492         }
4493         ctx = wolfSSL_CTX_new(method);
4494     }
4495     if (ctx == NULL) {
4496         goto done;
4497     }
4498 
4499 #if defined(HAVE_SESSION_TICKET) && \
4500     ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
4501 #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
4502     OpenSSLTicketInit();
4503     wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
4504 #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
4505     TicketInit();
4506     wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
4507 #endif
4508 #endif
4509 
4510 #if defined(USE_WINDOWS_API)
4511     port = ((func_args*)args)->signal->port;
4512 #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
4513      !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
4514     /* Let tcp_listen assign port */
4515     port = 0;
4516 #else
4517     /* Use default port */
4518     port = wolfSSLPort;
4519 #endif
4520 
4521     /* do it here to detect failure */
4522     tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
4523     CloseSocket(sockfd);
4524 
4525     wolfSSL_CTX_set_verify(ctx,
4526                   WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
4527 
4528 #ifdef WOLFSSL_ENCRYPTED_KEYS
4529     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
4530 #endif
4531 
4532     if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
4533                                                            != WOLFSSL_SUCCESS) {
4534         /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
4535         goto done;
4536     }
4537 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4538     if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
4539                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4540 #else
4541     if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
4542                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4543 #endif
4544         /*err_sys("can't load server cert chain file, "
4545                 "Please run from wolfSSL home dir");*/
4546         goto done;
4547     }
4548 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4549     if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
4550                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4551 #else
4552     if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
4553                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4554 #endif
4555         /*err_sys("can't load server key file, "
4556                 "Please run from wolfSSL home dir");*/
4557         goto done;
4558     }
4559 
4560     /* call ctx setup callback */
4561     if (cbf != NULL && cbf->ctx_ready != NULL) {
4562         cbf->ctx_ready(ctx);
4563     }
4564 
4565     ssl = wolfSSL_new(ctx);
4566     if (ssl == NULL) {
4567         goto done;
4568     }
4569 
4570 #ifdef WOLFSSL_SESSION_EXPORT
4571     /* only add in more complex nonblocking case with session export tests */
4572     if (args && ((func_args*)args)->argc > 0) {
4573         /* set as nonblock and time out for waiting on read/write */
4574         tcp_set_nonblocking(&clientfd);
4575         wolfSSL_dtls_set_using_nonblock(ssl, 1);
4576     }
4577 #endif
4578 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4579     if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
4580                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4581 #else
4582     if (wolfSSL_use_certificate_file(ssl, svrCertFile,
4583                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4584 #endif
4585         /*err_sys("can't load server cert chain file, "
4586                 "Please run from wolfSSL home dir");*/
4587         goto done;
4588     }
4589 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4590     if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
4591                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4592 #else
4593     if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
4594                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4595 #endif
4596         /*err_sys("can't load server key file, "
4597                 "Please run from wolfSSL home dir");*/
4598         goto done;
4599     }
4600 
4601     if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
4602         /*err_sys("SSL_set_fd failed");*/
4603         goto done;
4604     }
4605 
4606 #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
4607     wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
4608 #elif !defined(NO_DH)
4609     SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
4610 #endif
4611 
4612     /* call ssl setup callback */
4613     if (cbf != NULL && cbf->ssl_ready != NULL) {
4614         cbf->ssl_ready(ssl);
4615     }
4616 
4617 #ifdef WOLFSSL_SESSION_EXPORT
4618     /* only add in more complex nonblocking case with session export tests */
4619     if (((func_args*)args)->argc > 0) {
4620         ret = nonblocking_accept_read(args, ssl, &clientfd);
4621         if (ret >= 0) {
4622             ((func_args*)args)->return_code = TEST_SUCCESS;
4623         }
4624     #ifdef WOLFSSL_TIRTOS
4625         Task_yield();
4626     #endif
4627         goto done;
4628     }
4629 #endif
4630 
4631     #ifdef WOLFSSL_ASYNC_CRYPT
4632     err = 0; /* Reset error */
4633     #endif
4634     do {
4635     #ifdef WOLFSSL_ASYNC_CRYPT
4636         if (err == WC_PENDING_E) {
4637             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
4638             if (ret < 0) { break; } else if (ret == 0) { continue; }
4639         }
4640     #endif
4641         ret = wolfSSL_accept(ssl);
4642         err = wolfSSL_get_error(ssl, 0);
4643     } while (err == WC_PENDING_E);
4644     if (ret != WOLFSSL_SUCCESS) {
4645         char buff[WOLFSSL_MAX_ERROR_SZ];
4646         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
4647         /*err_sys("SSL_accept failed");*/
4648         goto done;
4649     }
4650 
4651 #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
4652     XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
4653     msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
4654     AssertIntGE(msg_len, 0);
4655 
4656     XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
4657     msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
4658     AssertIntGE(msg_len, 0);
4659 #endif
4660 
4661     idx = wolfSSL_read(ssl, input, sizeof(input)-1);
4662     if (idx > 0) {
4663         input[idx] = '\0';
4664         printf("Client message: %s\n", input);
4665     }
4666 
4667     if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
4668         /*err_sys("SSL_write failed");*/
4669 #ifdef WOLFSSL_TIRTOS
4670         return;
4671 #else
4672         return 0;
4673 #endif
4674     }
4675 
4676     if (cbf != NULL && cbf->on_result != NULL)
4677         cbf->on_result(ssl);
4678 
4679 #ifdef WOLFSSL_TIRTOS
4680     Task_yield();
4681 #endif
4682 
4683     ((func_args*)args)->return_code = TEST_SUCCESS;
4684 
4685 done:
4686     wolfSSL_shutdown(ssl);
4687     wolfSSL_free(ssl);
4688     if (!sharedCtx)
4689         wolfSSL_CTX_free(ctx);
4690 
4691     CloseSocket(clientfd);
4692 
4693 #ifdef WOLFSSL_TIRTOS
4694     fdCloseSession(Task_self());
4695 #endif
4696 
4697 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
4698                             && defined(HAVE_THREAD_LS)
4699     wc_ecc_fp_free();  /* free per thread cache */
4700 #endif
4701 
4702 #if defined(HAVE_SESSION_TICKET) && \
4703     ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
4704 #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
4705     OpenSSLTicketCleanup();
4706 #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
4707     TicketCleanup();
4708 #endif
4709 #endif
4710 
4711 #ifndef WOLFSSL_TIRTOS
4712     return 0;
4713 #endif
4714 }
4715 
4716 #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
4717 static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
4718 {
4719     SOCKET_T sockfd = 0;
4720     SOCKET_T clientfd = 0;
4721     word16 port;
4722 
4723     callback_functions* cbf;
4724     WOLFSSL_CTX* ctx = 0;
4725     WOLFSSL* ssl = 0;
4726 
4727     char msg[] = "I hear you fa shizzle!";
4728     char input[1024];
4729     int idx;
4730     int ret, err = 0;
4731     int sharedCtx = 0;
4732     int loop_count = ((func_args*)args)->argc;
4733     int count = 0;
4734 
4735 #ifdef WOLFSSL_TIRTOS
4736     fdOpenSession(Task_self());
4737 #endif
4738 
4739     ((func_args*)args)->return_code = TEST_FAIL;
4740     cbf = ((func_args*)args)->callbacks;
4741 
4742 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4743     if (cbf != NULL && cbf->ctx) {
4744         ctx = cbf->ctx;
4745         sharedCtx = 1;
4746     }
4747     else
4748 #endif
4749     {
4750         WOLFSSL_METHOD* method = NULL;
4751         if (cbf != NULL && cbf->method != NULL) {
4752             method = cbf->method();
4753         }
4754         else {
4755             method = wolfSSLv23_server_method();
4756         }
4757         ctx = wolfSSL_CTX_new(method);
4758     }
4759 
4760 #if defined(USE_WINDOWS_API)
4761     port = ((func_args*)args)->signal->port;
4762 #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
4763      !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
4764     /* Let tcp_listen assign port */
4765     port = 0;
4766 #else
4767     /* Use default port */
4768     port = wolfSSLPort;
4769 #endif
4770 
4771     wolfSSL_CTX_set_verify(ctx,
4772                   WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
4773 
4774 #ifdef WOLFSSL_ENCRYPTED_KEYS
4775     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
4776 #endif
4777 
4778     if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
4779                                                            != WOLFSSL_SUCCESS) {
4780         /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
4781         goto done;
4782     }
4783     if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
4784                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4785         /*err_sys("can't load server cert chain file, "
4786                 "Please run from wolfSSL home dir");*/
4787         goto done;
4788     }
4789     if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
4790                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4791         /*err_sys("can't load server key file, "
4792                 "Please run from wolfSSL home dir");*/
4793         goto done;
4794     }
4795     /* call ctx setup callback */
4796     if (cbf != NULL && cbf->ctx_ready != NULL) {
4797         cbf->ctx_ready(ctx);
4798     }
4799 
4800     while(count != loop_count) {
4801         ssl = wolfSSL_new(ctx);
4802         if (ssl == NULL) {
4803             goto done;
4804         }
4805         if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
4806                                         WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4807             /*err_sys("can't load server cert chain file, "
4808                     "Please run from wolfSSL home dir");*/
4809             goto done;
4810         }
4811         if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
4812                                         WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4813             /*err_sys("can't load server key file, "
4814                     "Please run from wolfSSL home dir");*/
4815             goto done;
4816         }
4817 
4818 #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
4819         wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
4820 #elif !defined(NO_DH)
4821         SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
4822 #endif
4823         /* call ssl setup callback */
4824         if (cbf != NULL && cbf->ssl_ready != NULL) {
4825             cbf->ssl_ready(ssl);
4826         }
4827         /* do it here to detect failure */
4828         tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
4829         CloseSocket(sockfd);
4830         if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
4831             /*err_sys("SSL_set_fd failed");*/
4832             goto done;
4833         }
4834 
4835         #ifdef WOLFSSL_ASYNC_CRYPT
4836         err = 0; /* Reset error */
4837         #endif
4838         do {
4839         #ifdef WOLFSSL_ASYNC_CRYPT
4840             if (err == WC_PENDING_E) {
4841                 ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
4842                 if (ret < 0) { break; } else if (ret == 0) { continue; }
4843             }
4844         #endif
4845             ret = wolfSSL_accept(ssl);
4846             err = wolfSSL_get_error(ssl, 0);
4847         } while (err == WC_PENDING_E);
4848         if (ret != WOLFSSL_SUCCESS) {
4849             char buff[WOLFSSL_MAX_ERROR_SZ];
4850             printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
4851             /*err_sys("SSL_accept failed");*/
4852             goto done;
4853         }
4854 
4855         idx = wolfSSL_read(ssl, input, sizeof(input)-1);
4856         if (idx > 0) {
4857             input[idx] = '\0';
4858             printf("Client message: %s\n", input);
4859         }
4860 
4861         if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
4862             /*err_sys("SSL_write failed");*/
4863     #ifdef WOLFSSL_TIRTOS
4864             return;
4865     #else
4866             return 0;
4867     #endif
4868         }
4869         /* free ssl for this connection */
4870         wolfSSL_shutdown(ssl);
4871         wolfSSL_free(ssl); ssl = NULL;
4872         CloseSocket(clientfd);
4873 
4874         count++;
4875     }
4876 #ifdef WOLFSSL_TIRTOS
4877     Task_yield();
4878 #endif
4879 
4880     ((func_args*)args)->return_code = TEST_SUCCESS;
4881 
4882 done:
4883     if(ssl != NULL) {
4884         wolfSSL_shutdown(ssl);
4885         wolfSSL_free(ssl);
4886     }
4887     if (!sharedCtx)
4888         wolfSSL_CTX_free(ctx);
4889 
4890     CloseSocket(clientfd);
4891 
4892 #ifdef WOLFSSL_TIRTOS
4893     fdCloseSession(Task_self());
4894 #endif
4895 
4896 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
4897                             && defined(HAVE_THREAD_LS)
4898     wc_ecc_fp_free();  /* free per thread cache */
4899 #endif
4900 
4901 #ifndef WOLFSSL_TIRTOS
4902     return 0;
4903 #endif
4904 }
4905 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
4906 
4907 typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
4908 
4909 static void test_client_nofail(void* args, cbType cb)
4910 {
4911     SOCKET_T sockfd = 0;
4912     callback_functions* cbf;
4913 
4914     WOLFSSL_CTX*     ctx     = 0;
4915     WOLFSSL*         ssl     = 0;
4916     WOLFSSL_CIPHER*  cipher;
4917 
4918     char msg[64] = "hello wolfssl!";
4919     char reply[1024];
4920     int  input;
4921     int  msgSz = (int)XSTRLEN(msg);
4922     int  ret, err = 0;
4923     int  cipherSuite;
4924     int  sharedCtx = 0;
4925     const char* cipherName1, *cipherName2;
4926 
4927 #ifdef WOLFSSL_TIRTOS
4928     fdOpenSession(Task_self());
4929 #endif
4930 
4931     ((func_args*)args)->return_code = TEST_FAIL;
4932     cbf = ((func_args*)args)->callbacks;
4933 
4934 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4935     if (cbf != NULL && cbf->ctx) {
4936         ctx = cbf->ctx;
4937         sharedCtx = cbf->isSharedCtx;
4938     }
4939     else
4940 #endif
4941     {
4942         WOLFSSL_METHOD* method  = NULL;
4943         if (cbf != NULL && cbf->method != NULL) {
4944             method = cbf->method();
4945         }
4946         else {
4947             method = wolfSSLv23_client_method();
4948         }
4949         ctx = wolfSSL_CTX_new(method);
4950     }
4951 
4952 #ifdef WOLFSSL_ENCRYPTED_KEYS
4953     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
4954 #endif
4955 
4956     /* Do connect here so server detects failures */
4957     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
4958                 0, 0, NULL);
4959 
4960     if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
4961     {
4962         /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
4963         goto done;
4964     }
4965 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4966     if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
4967                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4968 #else
4969     if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
4970                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4971 #endif
4972         /*err_sys("can't load client cert file, "
4973                 "Please run from wolfSSL home dir");*/
4974         goto done;
4975     }
4976 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4977     if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
4978                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4979 #else
4980     if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
4981                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
4982 #endif
4983 
4984         /*err_sys("can't load client key file, "
4985                 "Please run from wolfSSL home dir");*/
4986         goto done;
4987     }
4988 
4989     /* call ctx setup callback */
4990     if (cbf != NULL && cbf->ctx_ready != NULL) {
4991         cbf->ctx_ready(ctx);
4992     }
4993 
4994     ssl = wolfSSL_new(ctx);
4995     if (ssl == NULL) {
4996         goto done;
4997     }
4998 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
4999     if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
5000                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5001 #else
5002     if (wolfSSL_use_certificate_file(ssl, cliCertFile,
5003                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5004 #endif
5005         /*err_sys("can't load client cert file, "
5006                 "Please run from wolfSSL home dir");*/
5007         goto done;
5008     }
5009 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
5010     if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
5011                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5012 #else
5013     if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
5014                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5015 #endif
5016         /*err_sys("can't load client key file, "
5017                 "Please run from wolfSSL home dir");*/
5018         goto done;
5019     }
5020 
5021     if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
5022         /*err_sys("SSL_set_fd failed");*/
5023         goto done;
5024     }
5025 
5026     /* call ssl setup callback */
5027     if (cbf != NULL && cbf->ssl_ready != NULL) {
5028         cbf->ssl_ready(ssl);
5029     }
5030 
5031     #ifdef WOLFSSL_ASYNC_CRYPT
5032     err = 0; /* Reset error */
5033     #endif
5034     do {
5035     #ifdef WOLFSSL_ASYNC_CRYPT
5036         if (err == WC_PENDING_E) {
5037             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5038             if (ret < 0) { break; } else if (ret == 0) { continue; }
5039         }
5040     #endif
5041         ret = wolfSSL_connect(ssl);
5042         err = wolfSSL_get_error(ssl, 0);
5043     } while (err == WC_PENDING_E);
5044     if (ret != WOLFSSL_SUCCESS) {
5045         char buff[WOLFSSL_MAX_ERROR_SZ];
5046         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5047         /*err_sys("SSL_connect failed");*/
5048         goto done;
5049     }
5050 
5051     /* test the various get cipher methods */
5052     /* Internal cipher suite names */
5053     cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
5054     cipherName1 = wolfSSL_get_cipher_name(ssl);
5055     cipherName2 = wolfSSL_get_cipher_name_from_suite(
5056         (cipherSuite >> 8), cipherSuite & 0xFF);
5057     AssertStrEQ(cipherName1, cipherName2);
5058 
5059     /* IANA Cipher Suites Names */
5060     /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
5061         then it's the internal cipher suite name */
5062     cipher = wolfSSL_get_current_cipher(ssl);
5063     cipherName1 = wolfSSL_CIPHER_get_name(cipher);
5064     cipherName2 = wolfSSL_get_cipher(ssl);
5065     AssertStrEQ(cipherName1, cipherName2);
5066 #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
5067     !defined(WOLFSSL_QT)
5068     cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
5069             (cipherSuite >> 8), cipherSuite & 0xFF);
5070     AssertStrEQ(cipherName1, cipherName2);
5071 #endif
5072 
5073     if (cb != NULL)
5074         (cb)(ctx, ssl);
5075 
5076     if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
5077         /*err_sys("SSL_write failed");*/
5078         goto done;
5079     }
5080 
5081     input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
5082     if (input > 0) {
5083         reply[input] = '\0';
5084         printf("Server response: %s\n", reply);
5085     }
5086 
5087     ((func_args*)args)->return_code = TEST_SUCCESS;
5088 
5089 done:
5090     wolfSSL_free(ssl);
5091     if (!sharedCtx)
5092         wolfSSL_CTX_free(ctx);
5093 
5094     CloseSocket(sockfd);
5095 
5096 #ifdef WOLFSSL_TIRTOS
5097     fdCloseSession(Task_self());
5098 #endif
5099 
5100 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
5101                             && defined(HAVE_THREAD_LS)
5102     wc_ecc_fp_free();  /* free per thread cache */
5103 #endif
5104 
5105     return;
5106 }
5107 
5108 #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
5109 static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
5110 {
5111     SOCKET_T sockfd = 0;
5112     callback_functions* cbf;
5113 
5114     WOLFSSL_CTX*     ctx     = 0;
5115     WOLFSSL*         ssl     = 0;
5116     WOLFSSL_SESSION* session = NULL;
5117 
5118     char msg[64] = "hello wolfssl!";
5119     char reply[1024];
5120     int  input;
5121     int  msgSz = (int)XSTRLEN(msg);
5122     int  ret, err = 0;
5123     int  sharedCtx = 0;
5124 
5125 #ifdef WOLFSSL_TIRTOS
5126     fdOpenSession(Task_self());
5127 #endif
5128 
5129     ((func_args*)args)->return_code = TEST_FAIL;
5130     cbf = ((func_args*)args)->callbacks;
5131 
5132 #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
5133     if (cbf != NULL && cbf->ctx) {
5134         ctx = cbf->ctx;
5135         sharedCtx = 1;
5136     }
5137     else
5138 #endif
5139     {
5140         WOLFSSL_METHOD* method  = NULL;
5141         if (cbf != NULL && cbf->method != NULL) {
5142             method = cbf->method();
5143         }
5144         else {
5145             method = wolfSSLv23_client_method();
5146         }
5147         ctx = wolfSSL_CTX_new(method);
5148     }
5149 
5150 #ifdef WOLFSSL_ENCRYPTED_KEYS
5151     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
5152 #endif
5153 
5154     /* Do connect here so server detects failures */
5155     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
5156                 0, 0, NULL);
5157 
5158     if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
5159     {
5160         /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
5161         goto done;
5162     }
5163     if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
5164                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5165         /*err_sys("can't load client cert file, "
5166                 "Please run from wolfSSL home dir");*/
5167         goto done;
5168     }
5169     if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
5170                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5171         /*err_sys("can't load client key file, "
5172                 "Please run from wolfSSL home dir");*/
5173         goto done;
5174     }
5175 
5176     /* call ctx setup callback */
5177     if (cbf != NULL && cbf->ctx_ready != NULL) {
5178         cbf->ctx_ready(ctx);
5179     }
5180 
5181     ssl = wolfSSL_new(ctx);
5182     if (ssl == NULL) {
5183         goto done;
5184     }
5185     /* keep handshakre resources for re-using WOLFSSL obj */
5186     wolfSSL_KeepArrays(ssl);
5187     if(wolfSSL_KeepHandshakeResources(ssl)) {
5188         /* err_sys("SSL_KeepHandshakeResources failed"); */
5189         goto done;
5190     }
5191     if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
5192                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5193         /*err_sys("can't load client cert file, "
5194                 "Please run from wolfSSL home dir");*/
5195         goto done;
5196     }
5197     if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
5198                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5199         /*err_sys("can't load client key file, "
5200                 "Please run from wolfSSL home dir");*/
5201         goto done;
5202     }
5203 
5204     if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
5205         /*err_sys("SSL_set_fd failed");*/
5206         goto done;
5207     }
5208 
5209     /* call ssl setup callback */
5210     if (cbf != NULL && cbf->ssl_ready != NULL) {
5211         cbf->ssl_ready(ssl);
5212     }
5213 
5214     #ifdef WOLFSSL_ASYNC_CRYPT
5215     err = 0; /* Reset error */
5216     #endif
5217     do {
5218     #ifdef WOLFSSL_ASYNC_CRYPT
5219         if (err == WC_PENDING_E) {
5220             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5221             if (ret < 0) { break; } else if (ret == 0) { continue; }
5222         }
5223     #endif
5224         ret = wolfSSL_connect(ssl);
5225         err = wolfSSL_get_error(ssl, 0);
5226     } while (err == WC_PENDING_E);
5227     if (ret != WOLFSSL_SUCCESS) {
5228         char buff[WOLFSSL_MAX_ERROR_SZ];
5229         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5230         /*err_sys("SSL_connect failed");*/
5231         goto done;
5232     }
5233     /* Build first session */
5234     if (cb != NULL)
5235         ((cbType)cb)(ctx, ssl);
5236 
5237     if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
5238         /*err_sys("SSL_write failed");*/
5239         goto done;
5240     }
5241 
5242     input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
5243     if (input > 0) {
5244         reply[input] = '\0';
5245         printf("Server response: %s\n", reply);
5246     }
5247 
5248     /* Session Resumption by re-using WOLFSSL object */
5249     wolfSSL_set_quiet_shutdown(ssl, 1);
5250     if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
5251         /* err_sys ("SSL shutdown failed"); */
5252         goto done;
5253     }
5254     session = wolfSSL_get_session(ssl);
5255     if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
5256         /* err_sys ("SSL_clear failed"); */
5257         goto done;
5258     }
5259     wolfSSL_set_session(ssl, session);
5260     /* close socket once */
5261     CloseSocket(sockfd);
5262     sockfd = 0;
5263     /* wait until server ready */
5264     wait_tcp_ready((func_args*)server_args);
5265     printf("session resumption\n");
5266     /* Do re-connect  */
5267     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
5268                 0, 0, NULL);
5269     if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
5270         /*err_sys("SSL_set_fd failed");*/
5271         goto done;
5272     }
5273 
5274     #ifdef WOLFSSL_ASYNC_CRYPT
5275     err = 0; /* Reset error */
5276     #endif
5277     do {
5278     #ifdef WOLFSSL_ASYNC_CRYPT
5279         if (err == WC_PENDING_E) {
5280             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5281             if (ret < 0) { break; } else if (ret == 0) { continue; }
5282         }
5283     #endif
5284         ret = wolfSSL_connect(ssl);
5285         err = wolfSSL_get_error(ssl, 0);
5286     } while (err == WC_PENDING_E);
5287     if (ret != WOLFSSL_SUCCESS) {
5288         char buff[WOLFSSL_MAX_ERROR_SZ];
5289         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5290         /*err_sys("SSL_connect failed");*/
5291         goto done;
5292     }
5293     /* Build first session */
5294     if (cb != NULL)
5295         ((cbType)cb)(ctx, ssl);
5296 
5297     if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
5298         /*err_sys("SSL_write failed");*/
5299         goto done;
5300     }
5301 
5302     input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
5303     if (input > 0) {
5304         reply[input] = '\0';
5305         printf("Server response: %s\n", reply);
5306     }
5307 
5308     ((func_args*)args)->return_code = TEST_SUCCESS;
5309 
5310 done:
5311     wolfSSL_free(ssl);
5312     if (!sharedCtx)
5313         wolfSSL_CTX_free(ctx);
5314 
5315     CloseSocket(sockfd);
5316 
5317 #ifdef WOLFSSL_TIRTOS
5318     fdCloseSession(Task_self());
5319 #endif
5320 
5321     return;
5322 }
5323 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
5324 
5325 
5326 static void test_client_verifyDepth(void* args)
5327 {
5328 #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
5329     SOCKET_T sockfd = 0;
5330     callback_functions* cbf;
5331 
5332     WOLFSSL_CTX*     ctx     = 0;
5333     WOLFSSL*         ssl     = 0;
5334 
5335     char msg[64] = "hello wolfssl!";
5336     char reply[1024];
5337     int  input;
5338     int  msgSz = (int)XSTRLEN(msg);
5339     int  ret, err = 0;
5340     int  verify_depth = ((func_args*)args)->argc;
5341 
5342     ((func_args*)args)->return_code = TEST_FAIL;
5343     cbf = ((func_args*)args)->callbacks;
5344 
5345     {
5346         WOLFSSL_METHOD* method = NULL;
5347         if (cbf != NULL && cbf->method != NULL) {
5348             method = cbf->method();
5349         }
5350         else {
5351             method = wolfSSLv23_client_method();
5352         }
5353         ctx = wolfSSL_CTX_new(method);
5354     }
5355 
5356     /* Do connect here so server detects failures */
5357     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
5358                 0, 0, NULL);
5359 
5360     if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
5361                                                         != WOLFSSL_SUCCESS)
5362     {
5363         /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
5364         goto done;
5365     }
5366     if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
5367                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5368         /*err_sys("can't load client cert file, "
5369                 "Please run from wolfSSL home dir");*/
5370         goto done;
5371     }
5372     if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
5373                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
5374         /*err_sys("can't load client key file, "
5375                 "Please run from wolfSSL home dir");*/
5376         goto done;
5377     }
5378 
5379     SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
5380 
5381     /* set verify depth */
5382     if (verify_depth == 0) {
5383         myVerifyAction = VERIFY_OVERRIDE_ERROR;
5384         SSL_CTX_set_verify_depth(ctx, verify_depth);
5385     } else if (verify_depth == -1) {
5386         myVerifyAction = VERIFY_USE_PREVERFIY;
5387         SSL_CTX_set_verify_depth(ctx, 0);
5388     } else if (verify_depth > 0) {
5389         myVerifyAction = VERIFY_USE_PREVERFIY;
5390         SSL_CTX_set_verify_depth(ctx, verify_depth);
5391     }
5392 
5393     ssl = wolfSSL_new(ctx);
5394     if (ssl == NULL) {
5395         goto done;
5396     }
5397 
5398     if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
5399         /*err_sys("SSL_set_fd failed");*/
5400         goto done;
5401     }
5402 
5403     #ifdef WOLFSSL_ASYNC_CRYPT
5404     err = 0; /* Reset error */
5405     #endif
5406     do {
5407     #ifdef WOLFSSL_ASYNC_CRYPT
5408         if (err == WC_PENDING_E) {
5409             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5410             if (ret < 0) { break; } else if (ret == 0) { continue; }
5411         }
5412     #endif
5413         ret = wolfSSL_connect(ssl);
5414         err = wolfSSL_get_error(ssl, 0);
5415     } while (err == WC_PENDING_E);
5416     if (ret != WOLFSSL_SUCCESS) {
5417         char buff[WOLFSSL_MAX_ERROR_SZ];
5418         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5419         goto done;
5420     }
5421 
5422     if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
5423         goto done;
5424     }
5425 
5426     input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
5427     if (input > 0) {
5428         reply[input] = '\0';
5429         printf("Server response: %s\n", reply);
5430     }
5431 
5432     ((func_args*)args)->return_code = TEST_SUCCESS;
5433 
5434 done:
5435     wolfSSL_free(ssl);
5436     wolfSSL_CTX_free(ctx);
5437     CloseSocket(sockfd);
5438 #else
5439     (void)args;
5440 #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
5441 }
5442 
5443 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
5444      defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
5445     defined(HAVE_ALPN) && defined(HAVE_SNI) && \
5446     defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
5447     #define HAVE_ALPN_PROTOS_SUPPORT
5448 #endif
5449 
5450 /* Generic TLS client / server with callbacks for API unit tests
5451  * Used by SNI / ALPN / crypto callback helper functions */
5452 #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
5453     (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
5454      defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
5455     #define ENABLE_TLS_CALLBACK_TEST
5456 #endif
5457 
5458 #if defined(ENABLE_TLS_CALLBACK_TEST) || \
5459     (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
5460 /* TLS server for API unit testing - generic */
5461 static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
5462 {
5463     callback_functions* callbacks = ((func_args*)args)->callbacks;
5464 
5465     WOLFSSL_CTX* ctx = NULL;
5466     WOLFSSL*     ssl = NULL;
5467     SOCKET_T     sfd = 0;
5468     SOCKET_T     cfd = 0;
5469     word16       port;
5470 
5471     char msg[] = "I hear you fa shizzle!";
5472     int  len   = (int) XSTRLEN(msg);
5473     char input[1024];
5474     int  idx;
5475     int  ret, err = 0;
5476 
5477     ((func_args*)args)->return_code = TEST_FAIL;
5478 
5479 #ifdef WOLFSSL_STATIC_MEMORY
5480     if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
5481         callbacks->memSz > 0) {
5482         ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
5483             callbacks->mem, callbacks->memSz, 0, 1);
5484         if (ret != WOLFSSL_SUCCESS) {
5485             printf("CTX static new failed %d\n", ret);
5486             return 0;
5487         }
5488     }
5489 #endif
5490     if (ctx == NULL) {
5491         ctx = wolfSSL_CTX_new(callbacks->method());
5492     }
5493     if (ctx == NULL) {
5494         printf("CTX new failed\n");
5495         return 0;
5496     }
5497 
5498     /* set defaults */
5499     if (callbacks->caPemFile == NULL)
5500         callbacks->caPemFile = cliCertFile;
5501     if (callbacks->certPemFile == NULL)
5502         callbacks->certPemFile = svrCertFile;
5503     if (callbacks->keyPemFile == NULL)
5504         callbacks->keyPemFile = svrKeyFile;
5505 
5506 #ifdef WOLFSSL_TIRTOS
5507     fdOpenSession(Task_self());
5508 #endif
5509 
5510     wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
5511 
5512 #if defined(USE_WINDOWS_API)
5513     port = ((func_args*)args)->signal->port;
5514 #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
5515      !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
5516     /* Let tcp_listen assign port */
5517     port = 0;
5518 #else
5519     /* Use default port */
5520     port = wolfSSLPort;
5521 #endif
5522 
5523     wolfSSL_CTX_set_verify(ctx,
5524                   WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
5525 
5526 #ifdef WOLFSSL_ENCRYPTED_KEYS
5527     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
5528 #endif
5529 #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
5530     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
5531 #endif
5532 
5533 
5534     AssertIntEQ(WOLFSSL_SUCCESS,
5535         wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
5536 
5537     AssertIntEQ(WOLFSSL_SUCCESS,
5538         wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
5539             WOLFSSL_FILETYPE_PEM));
5540 
5541     AssertIntEQ(WOLFSSL_SUCCESS,
5542         wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
5543             WOLFSSL_FILETYPE_PEM));
5544 
5545     if (callbacks->ctx_ready)
5546         callbacks->ctx_ready(ctx);
5547 
5548     ssl = wolfSSL_new(ctx);
5549     if (ssl == NULL) {
5550         printf("SSL new failed\n");
5551         wolfSSL_CTX_free(ctx);
5552         return 0;
5553     }
5554     if (wolfSSL_dtls(ssl)) {
5555         SOCKADDR_IN_T cliAddr;
5556         socklen_t     cliLen;
5557 
5558         cliLen = sizeof(cliAddr);
5559         tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
5560         idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
5561                 (struct sockaddr*)&cliAddr, &cliLen);
5562         AssertIntGT(idx, 0);
5563         wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
5564     }
5565     else {
5566         tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
5567         CloseSocket(sfd);
5568     }
5569 
5570     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
5571 
5572     if (callbacks->loadToSSL) {
5573         wolfSSL_SetDevId(ssl, callbacks->devId);
5574 
5575         AssertIntEQ(WOLFSSL_SUCCESS,
5576             wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
5577                 WOLFSSL_FILETYPE_PEM));
5578 
5579         AssertIntEQ(WOLFSSL_SUCCESS,
5580             wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
5581                 WOLFSSL_FILETYPE_PEM));
5582     }
5583 
5584 #ifdef NO_PSK
5585     #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
5586         wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
5587     #elif !defined(NO_DH)
5588         SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
5589     #endif
5590 #endif
5591 
5592     if (callbacks->ssl_ready)
5593         callbacks->ssl_ready(ssl);
5594 
5595 
5596     #ifdef WOLFSSL_ASYNC_CRYPT
5597     err = 0; /* Reset error */
5598     #endif
5599     do {
5600     #ifdef WOLFSSL_ASYNC_CRYPT
5601         if (err == WC_PENDING_E) {
5602             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5603             if (ret < 0) { break; } else if (ret == 0) { continue; }
5604         }
5605     #endif
5606         ret = wolfSSL_accept(ssl);
5607         err = wolfSSL_get_error(ssl, 0);
5608     } while (err == WC_PENDING_E);
5609     if (ret != WOLFSSL_SUCCESS) {
5610         char buff[WOLFSSL_MAX_ERROR_SZ];
5611         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5612         /*err_sys("SSL_accept failed");*/
5613     }
5614     else {
5615         if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
5616             input[idx] = 0;
5617             printf("Client message: %s\n", input);
5618         }
5619 
5620         AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
5621 #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
5622         defined(WOLFSSL_DTLS)
5623         if (wolfSSL_dtls(ssl)) {
5624             byte*  import;
5625             word32 sz;
5626 
5627             wolfSSL_dtls_export(ssl, NULL, &sz);
5628             import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
5629             AssertNotNull(import);
5630             idx = wolfSSL_dtls_export(ssl, import, &sz);
5631             AssertIntGE(idx, 0);
5632             AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
5633             XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
5634         }
5635 #endif
5636 #ifdef WOLFSSL_TIRTOS
5637         Task_yield();
5638 #endif
5639         ((func_args*)args)->return_code = TEST_SUCCESS;
5640     }
5641 
5642     if (callbacks->on_result)
5643         callbacks->on_result(ssl);
5644 
5645     wolfSSL_shutdown(ssl);
5646     wolfSSL_free(ssl);
5647     wolfSSL_CTX_free(ctx);
5648     CloseSocket(cfd);
5649 
5650 
5651 #ifdef WOLFSSL_TIRTOS
5652     fdCloseSession(Task_self());
5653 #endif
5654 
5655 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
5656                             && defined(HAVE_THREAD_LS)
5657     wc_ecc_fp_free();  /* free per thread cache */
5658 #endif
5659 
5660 #ifndef WOLFSSL_TIRTOS
5661     return 0;
5662 #endif
5663 }
5664 
5665 /* TLS Client for API unit testing - generic */
5666 static void run_wolfssl_client(void* args)
5667 {
5668     callback_functions* callbacks = ((func_args*)args)->callbacks;
5669 
5670     WOLFSSL_CTX* ctx = NULL;
5671     WOLFSSL*     ssl = NULL;
5672     SOCKET_T    sfd = 0;
5673 
5674     char msg[] = "hello wolfssl server!";
5675     int  len   = (int) XSTRLEN(msg);
5676     char input[1024];
5677     int  ret, err = 0;
5678 
5679     ((func_args*)args)->return_code = TEST_FAIL;
5680 
5681     /* set defaults */
5682     if (callbacks->caPemFile == NULL)
5683         callbacks->caPemFile = caCertFile;
5684     if (callbacks->certPemFile == NULL)
5685         callbacks->certPemFile = cliCertFile;
5686     if (callbacks->keyPemFile == NULL)
5687         callbacks->keyPemFile = cliKeyFile;
5688 
5689 #ifdef WOLFSSL_STATIC_MEMORY
5690     if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
5691         callbacks->memSz > 0) {
5692         ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
5693             callbacks->mem, callbacks->memSz, 0, 1);
5694         if (ret != WOLFSSL_SUCCESS) {
5695             printf("CTX static new failed %d\n", ret);
5696             return;
5697         }
5698     }
5699 #endif
5700     if (ctx == NULL) {
5701         ctx = wolfSSL_CTX_new(callbacks->method());
5702     }
5703     if (ctx == NULL) {
5704         printf("CTX new failed\n");
5705         return;
5706     }
5707 
5708 #ifdef WOLFSSL_TIRTOS
5709     fdOpenSession(Task_self());
5710 #endif
5711 
5712     if (!callbacks->loadToSSL) {
5713         wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
5714     }
5715 
5716 #ifdef WOLFSSL_ENCRYPTED_KEYS
5717     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
5718 #endif
5719 
5720     AssertIntEQ(WOLFSSL_SUCCESS,
5721         wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
5722 
5723     if (!callbacks->loadToSSL) {
5724         AssertIntEQ(WOLFSSL_SUCCESS,
5725             wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
5726                 WOLFSSL_FILETYPE_PEM));
5727 
5728         AssertIntEQ(WOLFSSL_SUCCESS,
5729             wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
5730                 WOLFSSL_FILETYPE_PEM));
5731     }
5732 
5733     if (callbacks->ctx_ready)
5734         callbacks->ctx_ready(ctx);
5735 
5736     ssl = wolfSSL_new(ctx);
5737     if (wolfSSL_dtls(ssl)) {
5738         tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
5739                     1, 0, ssl);
5740     }
5741     else {
5742         tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
5743                     0, 0, ssl);
5744     }
5745     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
5746 
5747     if (callbacks->loadToSSL) {
5748         wolfSSL_SetDevId(ssl, callbacks->devId);
5749 
5750         AssertIntEQ(WOLFSSL_SUCCESS,
5751             wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
5752                 WOLFSSL_FILETYPE_PEM));
5753 
5754         AssertIntEQ(WOLFSSL_SUCCESS,
5755             wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
5756                 WOLFSSL_FILETYPE_PEM));
5757     }
5758 
5759     if (callbacks->ssl_ready)
5760         callbacks->ssl_ready(ssl);
5761 
5762     #ifdef WOLFSSL_ASYNC_CRYPT
5763     err = 0; /* Reset error */
5764     #endif
5765     do {
5766     #ifdef WOLFSSL_ASYNC_CRYPT
5767         if (err == WC_PENDING_E) {
5768             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5769             if (ret < 0) { break; } else if (ret == 0) { continue; }
5770         }
5771     #endif
5772         ret = wolfSSL_connect(ssl);
5773         err = wolfSSL_get_error(ssl, 0);
5774     } while (err == WC_PENDING_E);
5775     if (ret != WOLFSSL_SUCCESS) {
5776         char buff[WOLFSSL_MAX_ERROR_SZ];
5777         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
5778         /*err_sys("SSL_connect failed");*/
5779     }
5780     else {
5781         #ifdef WOLFSSL_ASYNC_CRYPT
5782         err = 0; /* Reset error */
5783         #endif
5784         do {
5785         #ifdef WOLFSSL_ASYNC_CRYPT
5786             if (err == WC_PENDING_E) {
5787                 ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5788                 if (ret < 0) { break; } else if (ret == 0) { continue; }
5789             }
5790         #endif
5791             ret = wolfSSL_write(ssl, msg, len);
5792             err = wolfSSL_get_error(ssl, 0);
5793         } while (err == WC_PENDING_E);
5794         AssertIntEQ(len, ret);
5795 
5796         #ifdef WOLFSSL_ASYNC_CRYPT
5797         err = 0; /* Reset error */
5798         #endif
5799         do {
5800         #ifdef WOLFSSL_ASYNC_CRYPT
5801             if (err == WC_PENDING_E) {
5802                 ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
5803                 if (ret < 0) { break; } else if (ret == 0) { continue; }
5804             }
5805         #endif
5806             ret = wolfSSL_read(ssl, input, sizeof(input)-1);
5807             err = wolfSSL_get_error(ssl, 0);
5808         } while (err == WC_PENDING_E);
5809         if (ret > 0) {
5810             input[ret] = '\0'; /* null term */
5811             printf("Server response: %s\n", input);
5812         }
5813         ((func_args*)args)->return_code = TEST_SUCCESS;
5814     }
5815 
5816     if (callbacks->on_result)
5817         callbacks->on_result(ssl);
5818 
5819     wolfSSL_free(ssl);
5820     wolfSSL_CTX_free(ctx);
5821     CloseSocket(sfd);
5822 
5823 #ifdef WOLFSSL_TIRTOS
5824     fdCloseSession(Task_self());
5825 #endif
5826 }
5827 
5828 #endif /* ENABLE_TLS_CALLBACK_TEST */
5829 
5830 
5831 static void test_wolfSSL_read_write(void)
5832 {
5833     /* The unit testing for read and write shall happen simultaneously, since
5834      * one can't do anything with one without the other. (Except for a failure
5835      * test case.) This function will call all the others that will set up,
5836      * execute, and report their test findings.
5837      *
5838      * Set up the success case first. This function will become the template
5839      * for the other tests. This should eventually be renamed
5840      *
5841      * The success case isn't interesting, how can this fail?
5842      * - Do not give the client context a CA certificate. The connect should
5843      *   fail. Do not need server for this?
5844      * - Using NULL for the ssl object on server. Do not need client for this.
5845      * - Using NULL for the ssl object on client. Do not need server for this.
5846      * - Good ssl objects for client and server. Client write() without server
5847      *   read().
5848      * - Good ssl objects for client and server. Server write() without client
5849      *   read().
5850      * - Forgetting the password callback?
5851     */
5852     tcp_ready ready;
5853     func_args client_args;
5854     func_args server_args;
5855     THREAD_TYPE serverThread;
5856 
5857     XMEMSET(&client_args, 0, sizeof(func_args));
5858     XMEMSET(&server_args, 0, sizeof(func_args));
5859 #ifdef WOLFSSL_TIRTOS
5860     fdOpenSession(Task_self());
5861 #endif
5862 
5863     StartTCP();
5864     InitTcpReady(&ready);
5865 
5866 #if defined(USE_WINDOWS_API)
5867     /* use RNG to get random port if using windows */
5868     ready.port = GetRandomPort();
5869 #endif
5870 
5871     server_args.signal = &ready;
5872     client_args.signal = &ready;
5873 
5874     start_thread(test_server_nofail, &server_args, &serverThread);
5875     wait_tcp_ready(&server_args);
5876     test_client_nofail(&client_args, NULL);
5877     join_thread(serverThread);
5878 
5879     AssertTrue(client_args.return_code);
5880     AssertTrue(server_args.return_code);
5881 
5882     FreeTcpReady(&ready);
5883 
5884 #ifdef WOLFSSL_TIRTOS
5885     fdOpenSession(Task_self());
5886 #endif
5887 }
5888 
5889 #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
5890 static void test_wolfSSL_reuse_WOLFSSLobj(void)
5891 {
5892     /* The unit test for session resumption by re-using WOLFSSL object.
5893      * WOLFSSL object is not cleared after first session. It re-use the obeject
5894      * for second connection.
5895     */
5896     tcp_ready ready;
5897     func_args client_args;
5898     func_args server_args;
5899     THREAD_TYPE serverThread;
5900 
5901     XMEMSET(&client_args, 0, sizeof(func_args));
5902     XMEMSET(&server_args, 0, sizeof(func_args));
5903 #ifdef WOLFSSL_TIRTOS
5904     fdOpenSession(Task_self());
5905 #endif
5906 
5907     StartTCP();
5908     InitTcpReady(&ready);
5909 
5910 #if defined(USE_WINDOWS_API)
5911     /* use RNG to get random port if using windows */
5912     ready.port = GetRandomPort();
5913 #endif
5914 
5915     server_args.signal = &ready;
5916     client_args.signal = &ready;
5917     /* the var is used for loop number */
5918     server_args.argc = 2;
5919 
5920     start_thread(test_server_loop, &server_args, &serverThread);
5921     wait_tcp_ready(&server_args);
5922     test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
5923     join_thread(serverThread);
5924 
5925     AssertTrue(client_args.return_code);
5926     AssertTrue(server_args.return_code);
5927 
5928     FreeTcpReady(&ready);
5929 
5930 #ifdef WOLFSSL_TIRTOS
5931     fdOpenSession(Task_self());
5932 #endif
5933 }
5934 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
5935 
5936 static void test_wolfSSL_CTX_verifyDepth_ServerClient(void)
5937 {
5938 #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
5939 
5940     /* This unit test is to check set verify Depth */
5941     tcp_ready ready;
5942     func_args client_args;
5943     func_args server_args;
5944     THREAD_TYPE serverThread;
5945     callback_functions client_cbf;
5946 
5947     XMEMSET(&client_args, 0, sizeof(func_args));
5948     XMEMSET(&server_args, 0, sizeof(func_args));
5949     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
5950 
5951     printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
5952 
5953 #ifdef WOLFSSL_TLS13
5954     client_cbf.method = wolfTLSv1_3_client_method;
5955 #endif /* WOLFSSL_TLS13 */
5956 
5957     client_args.callbacks = &client_cbf;
5958 
5959     StartTCP();
5960     InitTcpReady(&ready);
5961 
5962 #if defined(USE_WINDOWS_API)
5963     /* use RNG to get random port if using windows */
5964     ready.port = GetRandomPort();
5965 #endif
5966 
5967     server_args.signal = &ready;
5968     client_args.signal = &ready;
5969     /* the var is used for loop number */
5970     server_args.argc = 1;
5971 
5972     /* test case 1 verify depth is equal to peer chain */
5973     {
5974         start_thread(test_server_nofail, &server_args, &serverThread);
5975         wait_tcp_ready(&server_args);
5976 
5977         /* the var is used for verify depth */
5978         client_args.argc = 2;
5979 
5980         test_client_verifyDepth(&client_args);
5981         join_thread(serverThread);
5982         AssertIntEQ(client_args.return_code, TEST_SUCCESS);
5983         AssertIntEQ(server_args.return_code, TEST_SUCCESS);
5984     }
5985 
5986     /* test case 2
5987      * verify depth is zero, number of peer's chain is 2.
5988      * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
5989      * callback.
5990      */
5991 
5992     /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR   */
5993     {
5994         start_thread(test_server_nofail, &server_args, &serverThread);
5995         wait_tcp_ready(&server_args);
5996 
5997         client_args.argc = 0;
5998         test_client_verifyDepth(&client_args);
5999         join_thread(serverThread);
6000         AssertIntEQ(client_args.return_code, TEST_SUCCESS);
6001         AssertIntEQ(server_args.return_code, TEST_SUCCESS);
6002     }
6003     /* test case 3
6004      * verify depth is zero, number of peer's chain is 2
6005      * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
6006      * therefore, handshake becomes failure.
6007      */
6008     /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY   */
6009     {
6010         start_thread(test_server_nofail, &server_args, &serverThread);
6011         wait_tcp_ready(&server_args);
6012 
6013         client_args.argc = -1;
6014         test_client_verifyDepth(&client_args);
6015         join_thread(serverThread);
6016         AssertIntEQ(client_args.return_code, TEST_SUCCESS);
6017         AssertIntEQ(server_args.return_code, TEST_SUCCESS);
6018     }
6019 
6020     FreeTcpReady(&ready);
6021     printf(resultFmt, passed);
6022 #else
6023     (void)test_client_verifyDepth;
6024 #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
6025 }
6026 
6027 static void test_client_get_finished(void* args, cbType cb)
6028 {
6029     (void) args;
6030     (void) cb;
6031 #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
6032     SOCKET_T sockfd = 0;
6033     callback_functions* cbf;
6034 
6035     WOLFSSL_CTX*     ctx     = 0;
6036     WOLFSSL*         ssl     = 0;
6037 
6038     char msg[64] = "hello wolfssl!";
6039     char reply[1024];
6040     int  msgSz = (int)XSTRLEN(msg);
6041     int  ret, err = 0;
6042     WOLFSSL_METHOD* method  = NULL;
6043     size_t msg_len = 0;
6044 
6045     ((func_args*)args)->return_code = TEST_FAIL;
6046     cbf = ((func_args*)args)->callbacks;
6047 
6048     if (cbf != NULL && cbf->method != NULL) {
6049         method = cbf->method();
6050     }
6051     else {
6052         method = wolfSSLv23_client_method();
6053     }
6054     ctx = wolfSSL_CTX_new(method);
6055 
6056     /* Do connect here so server detects failures */
6057     tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
6058                 0, 0, NULL);
6059 
6060     if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
6061     {
6062         /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
6063         goto done;
6064     }
6065 
6066     if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
6067                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
6068         goto done;
6069     }
6070     if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
6071                                      WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
6072         goto done;
6073     }
6074 
6075     /* call ctx setup callback */
6076     if (cbf != NULL && cbf->ctx_ready != NULL) {
6077         cbf->ctx_ready(ctx);
6078     }
6079 
6080     ssl = wolfSSL_new(ctx);
6081     if (ssl == NULL) {
6082         goto done;
6083     }
6084 
6085     if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
6086         goto done;
6087     }
6088 
6089     /* call ssl setup callback */
6090     if (cbf != NULL && cbf->ssl_ready != NULL) {
6091         cbf->ssl_ready(ssl);
6092     }
6093 
6094     #ifdef WOLFSSL_ASYNC_CRYPT
6095     err = 0; /* Reset error */
6096     #endif
6097     do {
6098     #ifdef WOLFSSL_ASYNC_CRYPT
6099         if (err == WC_PENDING_E) {
6100             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
6101             if (ret < 0) { break; } else if (ret == 0) { continue; }
6102         }
6103     #endif
6104         ret = wolfSSL_connect(ssl);
6105         err = wolfSSL_get_error(ssl, 0);
6106     } while (err == WC_PENDING_E);
6107     if (ret != WOLFSSL_SUCCESS) {
6108         char buff[WOLFSSL_MAX_ERROR_SZ];
6109         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
6110         goto done;
6111     }
6112 
6113     /* get_finished test */
6114     /* 1. get own sent message */
6115     XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
6116     msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
6117     AssertIntGE(msg_len, 0);
6118     /* 2. get peer message */
6119     XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
6120     msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
6121     AssertIntGE(msg_len, 0);
6122 
6123     if (cb != NULL)
6124         (cb)(ctx, ssl);
6125 
6126     #ifdef WOLFSSL_ASYNC_CRYPT
6127     err = 0; /* Reset error */
6128     #endif
6129     do {
6130     #ifdef WOLFSSL_ASYNC_CRYPT
6131         if (err == WC_PENDING_E) {
6132             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
6133             if (ret < 0) { break; } else if (ret == 0) { continue; }
6134         }
6135     #endif
6136         ret = wolfSSL_write(ssl, msg, msgSz);
6137         err = wolfSSL_get_error(ssl, 0);
6138     } while (err == WC_PENDING_E);
6139     if (ret != msgSz) {
6140         /*err_sys("SSL_write failed");*/
6141         goto done;
6142     }
6143 
6144     #ifdef WOLFSSL_ASYNC_CRYPT
6145     err = 0; /* Reset error */
6146     #endif
6147     do {
6148     #ifdef WOLFSSL_ASYNC_CRYPT
6149         if (err == WC_PENDING_E) {
6150             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
6151             if (ret < 0) { break; } else if (ret == 0) { continue; }
6152         }
6153     #endif
6154         ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
6155         err = wolfSSL_get_error(ssl, 0);
6156     } while (err == WC_PENDING_E);
6157     if (ret > 0) {
6158         reply[ret] = '\0';
6159         printf("Server response: %s\n", reply);
6160     }
6161 
6162     ((func_args*)args)->return_code = TEST_SUCCESS;
6163 
6164 done:
6165     wolfSSL_free(ssl);
6166     wolfSSL_CTX_free(ctx);
6167     CloseSocket(sockfd);
6168 
6169     return;
6170 #endif
6171 }
6172 
6173 static void test_wolfSSL_get_finished(void)
6174 {
6175 #if !defined(NO_RSA) && defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
6176 
6177     tcp_ready ready;
6178     func_args client_args;
6179     func_args server_args;
6180     THREAD_TYPE serverThread;
6181 
6182     XMEMSET(&client_args, 0, sizeof(func_args));
6183     XMEMSET(&server_args, 0, sizeof(func_args));
6184 
6185     StartTCP();
6186     InitTcpReady(&ready);
6187 
6188 #if defined(USE_WINDOWS_API)
6189     /* use RNG to get random port if using windows */
6190     ready.port = GetRandomPort();
6191 #endif
6192 
6193     server_args.signal = &ready;
6194     client_args.signal = &ready;
6195 
6196     start_thread(test_server_nofail, &server_args, &serverThread);
6197     wait_tcp_ready(&server_args);
6198     test_client_get_finished(&client_args, NULL);
6199     join_thread(serverThread);
6200 
6201     AssertTrue(client_args.return_code);
6202     AssertTrue(server_args.return_code);
6203 
6204     /* test received msg vs sent msg */
6205     AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
6206     AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
6207 
6208     FreeTcpReady(&ready);
6209 #else
6210     (void)test_client_get_finished;
6211 #endif
6212 }
6213 
6214 #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
6215 /* canned export of a session using older version 3 */
6216 static unsigned char version_3[] = {
6217     0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
6218     0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
6219     0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
6220     0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
6221     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
6222     0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6223     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
6224     0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
6225     0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
6226     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6227     0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
6228     0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
6229     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6230     0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
6231     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
6232     0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
6233     0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
6234     0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
6235     0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
6236     0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
6237     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6238     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6239     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6240     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6241     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6242     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6243     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6244     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6245     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6246     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6247     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6248     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
6249     0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
6250     0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
6251     0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
6252     0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
6253     0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
6254     0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
6255     0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
6256     0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
6257     0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
6258     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
6259     0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
6260     0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
6261     0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6262     0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
6263     0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
6264     0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
6265     0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
6266     0x2E, 0x31, 0xED, 0x4F
6267 };
6268 #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
6269 
6270 static void test_wolfSSL_dtls_export(void)
6271 {
6272 #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
6273     tcp_ready ready;
6274     func_args client_args;
6275     func_args server_args;
6276     THREAD_TYPE serverThread;
6277     callback_functions server_cbf;
6278     callback_functions client_cbf;
6279 #ifdef WOLFSSL_TIRTOS
6280     fdOpenSession(Task_self());
6281 #endif
6282 
6283     InitTcpReady(&ready);
6284 
6285 #if defined(USE_WINDOWS_API)
6286     /* use RNG to get random port if using windows */
6287     ready.port = GetRandomPort();
6288 #endif
6289 
6290     /* set using dtls */
6291     XMEMSET(&client_args, 0, sizeof(func_args));
6292     XMEMSET(&server_args, 0, sizeof(func_args));
6293     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
6294     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
6295     server_cbf.method = wolfDTLSv1_2_server_method;
6296     client_cbf.method = wolfDTLSv1_2_client_method;
6297     server_args.callbacks = &server_cbf;
6298     client_args.callbacks = &client_cbf;
6299 
6300     server_args.signal = &ready;
6301     client_args.signal = &ready;
6302 
6303     start_thread(run_wolfssl_server, &server_args, &serverThread);
6304     wait_tcp_ready(&server_args);
6305     run_wolfssl_client(&client_args);
6306     join_thread(serverThread);
6307 
6308     AssertTrue(client_args.return_code);
6309     AssertTrue(server_args.return_code);
6310 
6311     FreeTcpReady(&ready);
6312 
6313 #ifdef WOLFSSL_TIRTOS
6314     fdOpenSession(Task_self());
6315 #endif
6316 
6317     {
6318         SOCKET_T sockfd = 0;
6319         WOLFSSL_CTX* ctx;
6320         WOLFSSL*     ssl;
6321         char msg[64] = "hello wolfssl!";
6322         char reply[1024];
6323         int  msgSz = (int)XSTRLEN(msg);
6324         byte *session, *window;
6325         unsigned int sessionSz, windowSz;
6326         struct sockaddr_in peerAddr;
6327         int i;
6328 
6329 
6330         /* Set ctx to DTLS 1.2 */
6331         AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
6332         AssertNotNull(ssl = wolfSSL_new(ctx));
6333 
6334         /* test importing version 3 */
6335         AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
6336 
6337         /* test importing bad length and bad version */
6338         version_3[2] += 1;
6339         AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
6340         version_3[2] -= 1; version_3[1] = 0XA0;
6341         AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
6342         wolfSSL_free(ssl);
6343         wolfSSL_CTX_free(ctx);
6344 
6345 
6346     /* check storing client state after connection and storing window only */
6347 #ifdef WOLFSSL_TIRTOS
6348     fdOpenSession(Task_self());
6349 #endif
6350 
6351     InitTcpReady(&ready);
6352 
6353 #if defined(USE_WINDOWS_API)
6354     /* use RNG to get random port if using windows */
6355     ready.port = GetRandomPort();
6356 #endif
6357 
6358     /* set using dtls */
6359     XMEMSET(&server_args, 0, sizeof(func_args));
6360     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
6361     server_cbf.method = wolfDTLSv1_2_server_method;
6362     server_args.callbacks = &server_cbf;
6363     server_args.argc = 3; /* set loop_count to 3 */
6364 
6365 
6366     server_args.signal = &ready;
6367     start_thread(test_server_nofail, &server_args, &serverThread);
6368     wait_tcp_ready(&server_args);
6369 
6370     /* create and connect with client */
6371     AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
6372     AssertIntEQ(WOLFSSL_SUCCESS,
6373             wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
6374     AssertIntEQ(WOLFSSL_SUCCESS,
6375           wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
6376     AssertIntEQ(WOLFSSL_SUCCESS,
6377             wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
6378     tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
6379     AssertNotNull(ssl = wolfSSL_new(ctx));
6380     AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
6381 
6382     /* store server information connected too */
6383     XMEMSET(&peerAddr, 0, sizeof(peerAddr));
6384     peerAddr.sin_family = AF_INET;
6385     peerAddr.sin_port = XHTONS(server_args.signal->port);
6386     wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
6387 
6388     AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
6389     AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
6390     session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6391     AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
6392     AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
6393     AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
6394     AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
6395     window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6396     AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
6397     wolfSSL_free(ssl);
6398 
6399     for (i = 1; i < server_args.argc; i++) {
6400         /* restore state */
6401         AssertNotNull(ssl = wolfSSL_new(ctx));
6402         AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
6403         AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
6404         AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
6405         AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
6406         AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
6407         AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
6408         wolfSSL_free(ssl);
6409     }
6410     XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6411     XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6412     wolfSSL_CTX_free(ctx);
6413 
6414     printf("done and waiting for server\n");
6415     join_thread(serverThread);
6416     AssertIntEQ(server_args.return_code, TEST_SUCCESS);
6417 
6418     FreeTcpReady(&ready);
6419 
6420 #ifdef WOLFSSL_TIRTOS
6421     fdOpenSession(Task_self());
6422 #endif
6423     }
6424 
6425 
6426     printf(testingFmt, "wolfSSL_dtls_export()");
6427     printf(resultFmt, passed);
6428 #endif
6429 }
6430 
6431 
6432 #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
6433 #ifdef WOLFSSL_TLS13
6434 static const byte canned_client_tls13_session[] = {
6435     0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
6436     0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
6437     0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
6438     0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
6439     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
6440     0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
6441     0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
6442     0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
6443     0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
6444     0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6445     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6446     0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
6447     0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
6448     0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
6449     0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
6450     0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
6451     0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
6452     0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
6453     0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
6454     0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
6455     0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
6456     0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
6457     0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
6458     0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
6459     0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
6460     0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
6461     0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
6462     0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
6463     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
6464     0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
6465     0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
6466     0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
6467     0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
6468     0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
6469     0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
6470     0x00, 0x03
6471 };
6472 
6473 static const byte canned_server_tls13_session[] = {
6474     0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
6475     0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
6476     0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
6477     0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
6478     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6479     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
6480     0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
6481     0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
6482     0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
6483     0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6484     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6485     0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
6486     0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
6487     0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
6488     0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
6489     0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
6490     0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
6491     0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
6492     0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
6493     0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
6494     0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
6495     0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
6496     0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
6497     0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
6498     0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
6499     0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
6500     0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
6501     0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
6502     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
6503     0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
6504     0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
6505     0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
6506     0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
6507     0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
6508     0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
6509     0x00, 0x04
6510 };
6511 #endif /* WOLFSSL_TLS13 */
6512 
6513 static const byte canned_client_session[] = {
6514     0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
6515     0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
6516     0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
6517     0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
6518     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
6519     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6520     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
6521     0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
6522     0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
6523     0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6524     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6525     0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
6526     0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
6527     0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
6528     0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
6529     0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
6530     0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
6531     0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
6532     0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
6533     0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
6534     0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
6535     0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
6536     0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
6537     0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
6538     0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
6539     0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
6540     0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
6541     0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
6542     0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
6543     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
6544     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6545     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6546     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6547     0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
6548     0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
6549     0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
6550     0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
6551     0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
6552     0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
6553     0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
6554     0x00, 0x03
6555 };
6556 
6557 
6558 static const byte canned_server_session[] = {
6559     0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
6560     0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
6561     0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
6562     0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
6563     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
6564     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6565     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
6566     0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
6567     0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
6568     0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6569     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6570     0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
6571     0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
6572     0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
6573     0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
6574     0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
6575     0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
6576     0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
6577     0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
6578     0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
6579     0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
6580     0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
6581     0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
6582     0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
6583     0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
6584     0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
6585     0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
6586     0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
6587     0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
6588     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
6589     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6590     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6591     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6592     0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
6593     0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
6594     0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
6595     0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
6596     0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
6597     0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
6598     0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
6599     0x00, 0x04
6600 };
6601 
6602 
6603 static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
6604 {
6605     SOCKET_T sockfd = 0;
6606     SOCKET_T clientfd = 0;
6607     word16 port;
6608 
6609     callback_functions* cbf;
6610     WOLFSSL_CTX* ctx = 0;
6611     WOLFSSL* ssl = 0;
6612 
6613     char msg[] = "I hear you fa shizzle!";
6614     char input[1024];
6615     int idx;
6616 
6617 #ifdef WOLFSSL_TIRTOS
6618     fdOpenSession(Task_self());
6619 #endif
6620 
6621     ((func_args*)args)->return_code = TEST_FAIL;
6622     cbf = ((func_args*)args)->callbacks;
6623 
6624     {
6625         WOLFSSL_METHOD* method = NULL;
6626         if (cbf != NULL && cbf->method != NULL) {
6627             method = cbf->method();
6628         }
6629         else {
6630             method = wolfTLSv1_2_server_method();
6631         }
6632         ctx = wolfSSL_CTX_new(method);
6633     }
6634     if (ctx == NULL) {
6635         goto done;
6636     }
6637     wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
6638 
6639 #if defined(USE_WINDOWS_API)
6640     port = ((func_args*)args)->signal->port;
6641 #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
6642      !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
6643     /* Let tcp_listen assign port */
6644     port = 0;
6645 #else
6646     /* Use default port */
6647     port = wolfSSLPort;
6648 #endif
6649 
6650     /* do it here to detect failure */
6651     tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
6652     CloseSocket(sockfd);
6653 
6654     /* call ctx setup callback */
6655     if (cbf != NULL && cbf->ctx_ready != NULL) {
6656         cbf->ctx_ready(ctx);
6657     }
6658 
6659     ssl = wolfSSL_new(ctx);
6660     if (ssl == NULL) {
6661         goto done;
6662     }
6663     wolfSSL_set_fd(ssl, clientfd);
6664 
6665     /* call ssl setup callback */
6666     if (cbf != NULL && cbf->ssl_ready != NULL) {
6667         cbf->ssl_ready(ssl);
6668     }
6669     idx = wolfSSL_read(ssl, input, sizeof(input)-1);
6670     if (idx > 0) {
6671         input[idx] = '\0';
6672         printf("Client message export/import: %s\n", input);
6673     }
6674     else {
6675         printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
6676         goto done;
6677     }
6678 
6679     if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
6680         /*err_sys("SSL_write failed");*/
6681 #ifdef WOLFSSL_TIRTOS
6682         return;
6683 #else
6684         return 0;
6685 #endif
6686     }
6687 
6688 #ifdef WOLFSSL_TIRTOS
6689     Task_yield();
6690 #endif
6691 
6692     ((func_args*)args)->return_code = TEST_SUCCESS;
6693 
6694 done:
6695     wolfSSL_shutdown(ssl);
6696     wolfSSL_free(ssl);
6697     wolfSSL_CTX_free(ctx);
6698 
6699     CloseSocket(clientfd);
6700 
6701 #ifdef WOLFSSL_TIRTOS
6702     fdCloseSession(Task_self());
6703 #endif
6704 
6705 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
6706                             && defined(HAVE_THREAD_LS)
6707     wc_ecc_fp_free();  /* free per thread cache */
6708 #endif
6709 
6710 #if defined(HAVE_SESSION_TICKET) && \
6711     ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
6712 #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
6713     OpenSSLTicketCleanup();
6714 #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
6715     TicketCleanup();
6716 #endif
6717 #endif
6718 
6719 #ifndef WOLFSSL_TIRTOS
6720     return 0;
6721 #endif
6722 }
6723 
6724 
6725 static void load_tls12_canned_server(WOLFSSL* ssl)
6726 {
6727     int clientfd = wolfSSL_get_fd(ssl);
6728     AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
6729                 sizeof(canned_server_session)), sizeof(canned_server_session));
6730     wolfSSL_set_fd(ssl, clientfd);
6731 }
6732 
6733 
6734 #ifdef WOLFSSL_TLS13
6735 static void load_tls13_canned_server(WOLFSSL* ssl)
6736 {
6737     int clientfd = wolfSSL_get_fd(ssl);
6738     AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
6739             sizeof(canned_server_tls13_session)),
6740             sizeof(canned_server_tls13_session));
6741     wolfSSL_set_fd(ssl, clientfd);
6742 }
6743 #endif
6744 
6745 
6746 /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
6747 static void test_wolfSSL_tls_export_run(int v)
6748 {
6749     SOCKET_T sockfd = 0;
6750     WOLFSSL_CTX*     ctx     = 0;
6751     WOLFSSL*         ssl     = 0;
6752     char msg[64] = "hello wolfssl!";
6753     char reply[1024];
6754     word32 replySz;
6755     int  msgSz = (int)XSTRLEN(msg);
6756     const byte* clientSession = NULL;
6757     int   clientSessionSz = 0;
6758 
6759     tcp_ready ready;
6760     func_args server_args;
6761     THREAD_TYPE serverThread;
6762     callback_functions server_cbf;
6763 
6764 #ifdef WOLFSSL_TIRTOS
6765     fdOpenSession(Task_self());
6766 #endif
6767 
6768     InitTcpReady(&ready);
6769 
6770 #if defined(USE_WINDOWS_API)
6771     /* use RNG to get random port if using windows */
6772     ready.port = GetRandomPort();
6773 #endif
6774 
6775     XMEMSET(&server_args, 0, sizeof(func_args));
6776     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
6777     switch (v) {
6778         case WOLFSSL_TLSV1_2:
6779             server_cbf.method     = wolfTLSv1_2_server_method;
6780             server_cbf.ssl_ready  = load_tls12_canned_server;
6781 
6782             /* setup the client side */
6783             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
6784             wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
6785             clientSession   = canned_client_session;
6786             clientSessionSz = sizeof(canned_client_session);
6787             break;
6788     #ifdef WOLFSSL_TLS13
6789         case WOLFSSL_TLSV1_3:
6790             server_cbf.method     = wolfTLSv1_3_server_method;
6791             server_cbf.ssl_ready  = load_tls13_canned_server;
6792 
6793             /* setup the client side */
6794             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
6795             clientSession   = canned_client_tls13_session;
6796             clientSessionSz = sizeof(canned_client_tls13_session);
6797             break;
6798     #endif
6799     }
6800     server_args.callbacks = &server_cbf;
6801     server_args.signal    = &ready;
6802 
6803     start_thread(tls_export_server, &server_args, &serverThread);
6804     wait_tcp_ready(&server_args);
6805 
6806 
6807 #ifdef WOLFSSL_TIRTOS
6808     fdOpenSession(Task_self());
6809 #endif
6810 
6811     AssertNotNull(ssl = wolfSSL_new(ctx));
6812     tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
6813     AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
6814                 clientSessionSz);
6815     replySz = sizeof(reply);
6816     AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
6817 #if !defined(NO_PSK) && defined(HAVE_ANON)
6818     /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
6819     AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
6820 #endif
6821     wolfSSL_set_fd(ssl, sockfd);
6822 
6823     AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
6824     AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
6825 
6826     wolfSSL_free(ssl);
6827     wolfSSL_CTX_free(ctx);
6828 
6829     CloseSocket(sockfd);
6830 
6831 #ifdef WOLFSSL_TIRTOS
6832     fdCloseSession(Task_self());
6833 #endif
6834 
6835 #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
6836                             && defined(HAVE_THREAD_LS)
6837     wc_ecc_fp_free();  /* free per thread cache */
6838 #endif
6839 
6840     join_thread(serverThread);
6841 
6842     AssertIntEQ(server_args.return_code, TEST_SUCCESS);
6843     FreeTcpReady(&ready);
6844 
6845 #ifdef WOLFSSL_TIRTOS
6846     fdOpenSession(Task_self());
6847 #endif
6848 
6849 }
6850 #endif
6851 
6852 static void test_wolfSSL_tls_export(void)
6853 {
6854 #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
6855     printf(testingFmt, "wolfSSL_tls_export()");
6856     test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
6857     #ifdef WOLFSSL_TLS13
6858     test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
6859     #endif
6860     printf(resultFmt, passed);
6861 #endif
6862 }
6863 
6864 /*----------------------------------------------------------------------------*
6865  | TLS extensions tests
6866  *----------------------------------------------------------------------------*/
6867 
6868 #ifdef ENABLE_TLS_CALLBACK_TEST
6869 /* Connection test runner - generic */
6870 static void test_wolfSSL_client_server(callback_functions* client_callbacks,
6871                                        callback_functions* server_callbacks)
6872 {
6873     tcp_ready ready;
6874     func_args client_args;
6875     func_args server_args;
6876     THREAD_TYPE serverThread;
6877 
6878     XMEMSET(&client_args, 0, sizeof(func_args));
6879     XMEMSET(&server_args, 0, sizeof(func_args));
6880 
6881     StartTCP();
6882 
6883     client_args.callbacks = client_callbacks;
6884     server_args.callbacks = server_callbacks;
6885 
6886 #ifdef WOLFSSL_TIRTOS
6887     fdOpenSession(Task_self());
6888 #endif
6889 
6890     /* RUN Server side */
6891     InitTcpReady(&ready);
6892 
6893 #if defined(USE_WINDOWS_API)
6894     /* use RNG to get random port if using windows */
6895     ready.port = GetRandomPort();
6896 #endif
6897 
6898     server_args.signal = &ready;
6899     client_args.signal = &ready;
6900     start_thread(run_wolfssl_server, &server_args, &serverThread);
6901     wait_tcp_ready(&server_args);
6902 
6903     /* RUN Client side */
6904     run_wolfssl_client(&client_args);
6905     join_thread(serverThread);
6906 
6907     FreeTcpReady(&ready);
6908 #ifdef WOLFSSL_TIRTOS
6909     fdCloseSession(Task_self());
6910 #endif
6911 
6912     client_callbacks->return_code = client_args.return_code;
6913     server_callbacks->return_code = server_args.return_code;
6914 }
6915 #endif /* ENABLE_TLS_CALLBACK_TEST */
6916 
6917 
6918 #ifdef HAVE_SNI
6919 static void test_wolfSSL_UseSNI_params(void)
6920 {
6921     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
6922     WOLFSSL     *ssl = wolfSSL_new(ctx);
6923 
6924     AssertNotNull(ctx);
6925     AssertNotNull(ssl);
6926 
6927     /* invalid [ctx|ssl] */
6928     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
6929     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI(    NULL, 0, "ssl", 3));
6930     /* invalid type */
6931     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
6932     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI(    ssl, -1, "ssl", 3));
6933     /* invalid data */
6934     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx,  0, NULL,  3));
6935     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI(    ssl,  0, NULL,  3));
6936     /* success case */
6937     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx,  0, "ctx", 3));
6938     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(    ssl,  0, "ssl", 3));
6939 
6940     wolfSSL_free(ssl);
6941     wolfSSL_CTX_free(ctx);
6942 }
6943 
6944 /* BEGIN of connection tests callbacks */
6945 static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
6946 {
6947     AssertIntEQ(WOLFSSL_SUCCESS,
6948         wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
6949 }
6950 
6951 static void use_SNI_at_ssl(WOLFSSL* ssl)
6952 {
6953     AssertIntEQ(WOLFSSL_SUCCESS,
6954              wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
6955 }
6956 
6957 static void different_SNI_at_ssl(WOLFSSL* ssl)
6958 {
6959     AssertIntEQ(WOLFSSL_SUCCESS,
6960              wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
6961 }
6962 
6963 static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
6964 {
6965     use_SNI_at_ssl(ssl);
6966     wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
6967                                               WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
6968 }
6969 
6970 static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
6971 {
6972     use_SNI_at_ssl(ssl);
6973     wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
6974                                                 WOLFSSL_SNI_ANSWER_ON_MISMATCH);
6975 }
6976 
6977 static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
6978 {
6979     use_SNI_at_ctx(ctx);
6980     wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
6981                                                   WOLFSSL_SNI_ABORT_ON_ABSENCE);
6982 }
6983 
6984 static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
6985 {
6986     use_SNI_at_ssl(ssl);
6987     wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
6988                                                   WOLFSSL_SNI_ABORT_ON_ABSENCE);
6989 }
6990 
6991 static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
6992 {
6993     use_SNI_at_ctx(ctx);
6994     wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
6995                  WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
6996 }
6997 
6998 static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
6999 {
7000     AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
7001 }
7002 
7003 static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
7004 {
7005     AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
7006 }
7007 
7008 static void verify_SNI_no_matching(WOLFSSL* ssl)
7009 {
7010     byte type = WOLFSSL_SNI_HOST_NAME;
7011     char* request = (char*) &type; /* to be overwritten */
7012 
7013     AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
7014     AssertNotNull(request);
7015     AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
7016     AssertNull(request);
7017 }
7018 
7019 static void verify_SNI_real_matching(WOLFSSL* ssl)
7020 {
7021     byte type = WOLFSSL_SNI_HOST_NAME;
7022     char* request = NULL;
7023 
7024     AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
7025     AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
7026     AssertNotNull(request);
7027     AssertStrEQ("www.wolfssl.com", request);
7028 }
7029 
7030 static void verify_SNI_fake_matching(WOLFSSL* ssl)
7031 {
7032     byte type = WOLFSSL_SNI_HOST_NAME;
7033     char* request = NULL;
7034 
7035     AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
7036     AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
7037     AssertNotNull(request);
7038     AssertStrEQ("ww2.wolfssl.com", request);
7039 }
7040 
7041 static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
7042 {
7043     AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
7044 }
7045 /* END of connection tests callbacks */
7046 
7047 static void test_wolfSSL_UseSNI_connection(void)
7048 {
7049     callback_functions client_cb;
7050     callback_functions server_cb;
7051 
7052     XMEMSET(&client_cb, 0, sizeof(callback_functions));
7053     XMEMSET(&server_cb, 0, sizeof(callback_functions));
7054     client_cb.method = wolfSSLv23_client_method;
7055     server_cb.method = wolfSSLv23_server_method;
7056     server_cb.devId = devId;
7057     server_cb.devId = devId;
7058 
7059     /* success case at ctx */
7060     client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
7061     server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
7062     test_wolfSSL_client_server(&client_cb, &server_cb);
7063 
7064     /* success case at ssl */
7065     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
7066     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
7067     test_wolfSSL_client_server(&client_cb, &server_cb);
7068 
7069     /* default mismatch behavior */
7070     client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
7071     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl;       server_cb.on_result = verify_UNKNOWN_SNI_on_server;
7072     test_wolfSSL_client_server(&client_cb, &server_cb);
7073 
7074     /* continue on mismatch */
7075     client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl;         client_cb.on_result = NULL;
7076     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
7077     test_wolfSSL_client_server(&client_cb, &server_cb);
7078 
7079     /* fake answer on mismatch */
7080     client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl;            client_cb.on_result = NULL;
7081     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
7082     test_wolfSSL_client_server(&client_cb, &server_cb);
7083 
7084     /* sni abort - success */
7085     client_cb.ctx_ready = use_SNI_at_ctx;           client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
7086     server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
7087     test_wolfSSL_client_server(&client_cb, &server_cb);
7088 
7089     /* sni abort - abort when absent (ctx) */
7090     client_cb.ctx_ready = NULL;                     client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
7091     server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
7092 
7093     /* sni abort - abort when absent (ssl) */
7094     client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL;                     client_cb.on_result = verify_FATAL_ERROR_on_client;
7095     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
7096     test_wolfSSL_client_server(&client_cb, &server_cb);
7097 
7098     /* sni abort - success when overwritten */
7099     client_cb.ctx_ready = NULL;                     client_cb.ssl_ready = NULL;           client_cb.on_result = NULL;
7100     server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_no_matching;
7101     test_wolfSSL_client_server(&client_cb, &server_cb);
7102 
7103     /* sni abort - success when allowing mismatches */
7104     client_cb.ctx_ready = NULL;                            client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
7105     server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL;                 server_cb.on_result = verify_SNI_fake_matching;
7106     test_wolfSSL_client_server(&client_cb, &server_cb);
7107 }
7108 
7109 static void test_wolfSSL_SNI_GetFromBuffer(void)
7110 {
7111     byte buff[] = { /* www.paypal.com */
7112         0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
7113         0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
7114         0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
7115         0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
7116         0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
7117         0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
7118         0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
7119         0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
7120         0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
7121     };
7122 
7123     byte buff2[] = { /* api.textmate.org */
7124         0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
7125         0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
7126         0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
7127         0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
7128         0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
7129         0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
7130         0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
7131         0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
7132         0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
7133         0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
7134         0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
7135         0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
7136         0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
7137         0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
7138         0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
7139         0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
7140         0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
7141     };
7142 
7143     byte buff3[] = { /* no sni extension */
7144         0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
7145         0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
7146         0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
7147         0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
7148         0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
7149         0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
7150         0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
7151     };
7152 
7153     byte buff4[] = { /* last extension has zero size */
7154         0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
7155         0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
7156         0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
7157         0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
7158         0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
7159         0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
7160         0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
7161         0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
7162         0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
7163         0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
7164         0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
7165         0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
7166         0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
7167         0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
7168         0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
7169         0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
7170         0x12, 0x00, 0x00
7171     };
7172 
7173     byte buff5[] = { /* SSL v2.0 client hello */
7174         0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
7175         /* dummy bytes bellow, just to pass size check */
7176         0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
7177         0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
7178         0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
7179     };
7180 
7181     byte result[32] = {0};
7182     word32 length   = 32;
7183 
7184     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
7185                                                            0, result, &length));
7186 
7187     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
7188                                                            0, result, &length));
7189 
7190     AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
7191                                                            1, result, &length));
7192 
7193     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
7194                                                            0, result, &length));
7195     buff[0] = 0x16;
7196 
7197     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
7198                                                            0, result, &length));
7199     buff[1] = 0x03;
7200 
7201     AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
7202                                            sizeof(buff), 0, result, &length));
7203     buff[2] = 0x03;
7204 
7205     AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
7206                                            sizeof(buff), 0, result, &length));
7207     buff[4] = 0x64;
7208 
7209     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
7210                                                            0, result, &length));
7211     result[length] = 0;
7212     AssertStrEQ("www.paypal.com", (const char*) result);
7213 
7214     length = 32;
7215 
7216     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
7217                                                            0, result, &length));
7218     result[length] = 0;
7219     AssertStrEQ("api.textmate.org", (const char*) result);
7220 
7221     /* SSL v2.0 tests */
7222     AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
7223                                           sizeof(buff5), 0, result, &length));
7224 
7225     buff5[2] = 0x02;
7226     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
7227                                           sizeof(buff5), 0, result, &length));
7228 
7229     buff5[2] = 0x01; buff5[6] = 0x08;
7230     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
7231                                           sizeof(buff5), 0, result, &length));
7232 
7233     buff5[6] = 0x09; buff5[8] = 0x01;
7234     AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
7235                                           sizeof(buff5), 0, result, &length));
7236 }
7237 
7238 #endif /* HAVE_SNI */
7239 
7240 static void test_wolfSSL_UseSNI(void)
7241 {
7242 #ifdef HAVE_SNI
7243     test_wolfSSL_UseSNI_params();
7244     test_wolfSSL_UseSNI_connection();
7245 
7246     test_wolfSSL_SNI_GetFromBuffer();
7247 #endif
7248 }
7249 
7250 #endif /* HAVE_IO_TESTS_DEPENDENCIES */
7251 
7252 static void test_wolfSSL_UseTrustedCA(void)
7253 {
7254 #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
7255     && !defined(NO_RSA)
7256     WOLFSSL_CTX *ctx;
7257     WOLFSSL     *ssl;
7258     byte        id[20];
7259 
7260 #ifndef NO_WOLFSSL_SERVER
7261     AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
7262     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
7263     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
7264 #else
7265     AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
7266 #endif
7267     AssertNotNull((ssl = wolfSSL_new(ctx)));
7268     XMEMSET(id, 0, sizeof(id));
7269 
7270     /* error cases */
7271     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
7272     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7273                 WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
7274     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7275                 WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
7276     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7277                 WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
7278 #ifdef NO_SHA
7279     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7280                 WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
7281 #endif
7282     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7283                 WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
7284 
7285     /* success cases */
7286     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7287                 WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
7288 #ifndef NO_SHA
7289     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7290                 WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
7291 #endif
7292     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
7293                 WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
7294 
7295     wolfSSL_free(ssl);
7296     wolfSSL_CTX_free(ctx);
7297 #endif /* HAVE_TRUSTED_CA */
7298 }
7299 
7300 static void test_wolfSSL_UseMaxFragment(void)
7301 {
7302 #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
7303     !defined(NO_FILESYSTEM) && !defined(NO_RSA)
7304   #ifndef NO_WOLFSSL_SERVER
7305     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
7306     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
7307     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
7308   #else
7309     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7310   #endif
7311     WOLFSSL     *ssl = wolfSSL_new(ctx);
7312 
7313 #ifdef OPENSSL_EXTRA
7314     int (*UseMaxFragment)(SSL *s, uint8_t mode);
7315     int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
7316 
7317     CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
7318     UseMaxFragment = SSL_set_tlsext_max_fragment_length;
7319 #else
7320     int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
7321     int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
7322 
7323     UseMaxFragment = wolfSSL_UseMaxFragment;
7324     CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
7325 #endif
7326 
7327     AssertNotNull(ctx);
7328     AssertNotNull(ssl);
7329 
7330     /* error cases */
7331     AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
7332     AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(    NULL, WOLFSSL_MFL_2_9));
7333     AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
7334     AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
7335     AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
7336     AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
7337 
7338     /* success case */
7339 #ifdef OPENSSL_EXTRA
7340     AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_8));
7341 #else
7342     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_8));
7343 #endif
7344     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_9));
7345     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_10));
7346     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_11));
7347     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_12));
7348 #ifdef OPENSSL_EXTRA
7349     AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
7350 
7351     AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_8));
7352 #else
7353     AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx,  WOLFSSL_MFL_2_13));
7354 
7355     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_8));
7356 #endif
7357     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_9));
7358     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_10));
7359     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_11));
7360     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_12));
7361 
7362 #ifdef OPENSSL_EXTRA
7363     AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_13));
7364 #else
7365     AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment(    ssl,  WOLFSSL_MFL_2_13));
7366 #endif
7367 
7368     wolfSSL_free(ssl);
7369     wolfSSL_CTX_free(ctx);
7370 #endif
7371 }
7372 
7373 static void test_wolfSSL_UseTruncatedHMAC(void)
7374 {
7375 #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
7376     !defined(NO_FILESYSTEM) && !defined(NO_RSA)
7377   #ifndef NO_WOLFSSL_SERVER
7378     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
7379     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
7380     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
7381   #else
7382     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7383   #endif
7384     WOLFSSL     *ssl = wolfSSL_new(ctx);
7385 
7386     AssertNotNull(ctx);
7387     AssertNotNull(ssl);
7388 
7389     /* error cases */
7390     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
7391     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
7392 
7393     /* success case */
7394     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
7395     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
7396 
7397     wolfSSL_free(ssl);
7398     wolfSSL_CTX_free(ctx);
7399 #endif
7400 }
7401 
7402 static void test_wolfSSL_UseSupportedCurve(void)
7403 {
7404 #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
7405     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7406     WOLFSSL     *ssl = wolfSSL_new(ctx);
7407 
7408     AssertNotNull(ctx);
7409     AssertNotNull(ssl);
7410 
7411     /* error cases */
7412     AssertIntNE(WOLFSSL_SUCCESS,
7413                       wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
7414     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx,  0));
7415 
7416     AssertIntNE(WOLFSSL_SUCCESS,
7417                           wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
7418     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl,  0));
7419 
7420     /* success case */
7421     AssertIntEQ(WOLFSSL_SUCCESS,
7422                        wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
7423     AssertIntEQ(WOLFSSL_SUCCESS,
7424                            wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
7425 
7426     wolfSSL_free(ssl);
7427     wolfSSL_CTX_free(ctx);
7428 #endif
7429 }
7430 
7431 #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
7432 
7433 static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
7434 {
7435     AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
7436 }
7437 
7438 static void use_ALPN_all(WOLFSSL* ssl)
7439 {
7440     /* http/1.1,spdy/1,spdy/2,spdy/3 */
7441     char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
7442                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
7443                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
7444                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7445     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
7446                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7447 }
7448 
7449 static void use_ALPN_all_continue(WOLFSSL* ssl)
7450 {
7451     /* http/1.1,spdy/1,spdy/2,spdy/3 */
7452     char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
7453         0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
7454         0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
7455         0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7456     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
7457                                              WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
7458 }
7459 
7460 static void use_ALPN_one(WOLFSSL* ssl)
7461 {
7462     /* spdy/2 */
7463     char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
7464 
7465     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
7466                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7467 }
7468 
7469 static void use_ALPN_unknown(WOLFSSL* ssl)
7470 {
7471     /* http/2.0 */
7472     char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
7473 
7474     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
7475                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7476 }
7477 
7478 static void use_ALPN_unknown_continue(WOLFSSL* ssl)
7479 {
7480     /* http/2.0 */
7481     char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
7482 
7483     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
7484                                              WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
7485 }
7486 
7487 static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
7488 {
7489     /* spdy/3 */
7490     char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7491 
7492     char *proto = NULL;
7493     word16 protoSz = 0;
7494 
7495     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
7496 
7497     /* check value */
7498     AssertIntNE(1, sizeof(nego_proto) == protoSz);
7499     if (proto) {
7500         AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
7501     }
7502 }
7503 
7504 static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
7505 {
7506     char *proto = NULL;
7507     word16 protoSz = 0;
7508 
7509     AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
7510                 wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
7511 
7512     /* check value */
7513     AssertIntEQ(1, (0 == protoSz));
7514     AssertIntEQ(1, (NULL == proto));
7515 }
7516 
7517 static void verify_ALPN_matching_http1(WOLFSSL* ssl)
7518 {
7519     /* http/1.1 */
7520     char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
7521     char *proto;
7522     word16 protoSz = 0;
7523 
7524     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
7525 
7526     /* check value */
7527     AssertIntEQ(1, sizeof(nego_proto) == protoSz);
7528     AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
7529 }
7530 
7531 static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
7532 {
7533     /* spdy/2 */
7534     char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
7535     char *proto;
7536     word16 protoSz = 0;
7537 
7538     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
7539 
7540     /* check value */
7541     AssertIntEQ(1, sizeof(nego_proto) == protoSz);
7542     AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
7543 }
7544 
7545 static void verify_ALPN_client_list(WOLFSSL* ssl)
7546 {
7547     /* http/1.1,spdy/1,spdy/2,spdy/3 */
7548     char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
7549                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
7550                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
7551                         0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7552     char    *clist = NULL;
7553     word16  clistSz = 0;
7554 
7555     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
7556                                                           &clistSz));
7557 
7558     /* check value */
7559     AssertIntEQ(1, sizeof(alpn_list) == clistSz);
7560     AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
7561 
7562     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
7563 }
7564 
7565 static void test_wolfSSL_UseALPN_connection(void)
7566 {
7567     callback_functions client_cb;
7568     callback_functions server_cb;
7569 
7570     XMEMSET(&client_cb, 0, sizeof(callback_functions));
7571     XMEMSET(&server_cb, 0, sizeof(callback_functions));
7572     client_cb.method = wolfSSLv23_client_method;
7573     server_cb.method = wolfSSLv23_server_method;
7574     server_cb.devId = devId;
7575     server_cb.devId = devId;
7576 
7577     /* success case same list */
7578     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
7579     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
7580     test_wolfSSL_client_server(&client_cb, &server_cb);
7581 
7582     /* success case only one for server */
7583     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
7584     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
7585     test_wolfSSL_client_server(&client_cb, &server_cb);
7586 
7587     /* success case only one for client */
7588     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
7589     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
7590     test_wolfSSL_client_server(&client_cb, &server_cb);
7591 
7592     /* success case none for client */
7593     client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL;         client_cb.on_result = NULL;
7594     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
7595     test_wolfSSL_client_server(&client_cb, &server_cb);
7596 
7597     /* success case mismatch behavior but option 'continue' set */
7598     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue;     client_cb.on_result = verify_ALPN_not_matching_continue;
7599     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
7600     test_wolfSSL_client_server(&client_cb, &server_cb);
7601 
7602     /* success case read protocol send by client */
7603     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
7604     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
7605     test_wolfSSL_client_server(&client_cb, &server_cb);
7606 
7607     /* mismatch behavior with same list
7608         * the first and only this one must be taken */
7609     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
7610     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
7611     test_wolfSSL_client_server(&client_cb, &server_cb);
7612 
7613     /* default mismatch behavior */
7614     client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all;     client_cb.on_result = NULL;
7615     server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
7616     test_wolfSSL_client_server(&client_cb, &server_cb);
7617 }
7618 
7619 static void test_wolfSSL_UseALPN_params(void)
7620 {
7621 #ifndef NO_WOLFSSL_CLIENT
7622     /* "http/1.1" */
7623     char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
7624     /* "spdy/1" */
7625     char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
7626     /* "spdy/2" */
7627     char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
7628     /* "spdy/3" */
7629     char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7630     char buff[256];
7631     word32 idx;
7632 
7633     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7634     WOLFSSL     *ssl = wolfSSL_new(ctx);
7635 
7636     AssertNotNull(ctx);
7637     AssertNotNull(ssl);
7638 
7639     /* error cases */
7640     AssertIntNE(WOLFSSL_SUCCESS,
7641                 wolfSSL_UseALPN(NULL, http1, sizeof(http1),
7642                                 WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7643     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
7644                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7645 
7646     /* success case */
7647     /* http1 only */
7648     AssertIntEQ(WOLFSSL_SUCCESS,
7649                 wolfSSL_UseALPN(ssl, http1, sizeof(http1),
7650                                 WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7651 
7652     /* http1, spdy1 */
7653     XMEMCPY(buff, http1, sizeof(http1));
7654     idx = sizeof(http1);
7655     buff[idx++] = ',';
7656     XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
7657     idx += sizeof(spdy1);
7658     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
7659                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7660 
7661     /* http1, spdy2, spdy1 */
7662     XMEMCPY(buff, http1, sizeof(http1));
7663     idx = sizeof(http1);
7664     buff[idx++] = ',';
7665     XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
7666     idx += sizeof(spdy2);
7667     buff[idx++] = ',';
7668     XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
7669     idx += sizeof(spdy1);
7670     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
7671                                              WOLFSSL_ALPN_FAILED_ON_MISMATCH));
7672 
7673     /* spdy3, http1, spdy2, spdy1 */
7674     XMEMCPY(buff, spdy3, sizeof(spdy3));
7675     idx = sizeof(spdy3);
7676     buff[idx++] = ',';
7677     XMEMCPY(buff+idx, http1, sizeof(http1));
7678     idx += sizeof(http1);
7679     buff[idx++] = ',';
7680     XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
7681     idx += sizeof(spdy2);
7682     buff[idx++] = ',';
7683     XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
7684     idx += sizeof(spdy1);
7685     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
7686                                              WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
7687 
7688     wolfSSL_free(ssl);
7689     wolfSSL_CTX_free(ctx);
7690 #endif
7691 }
7692 #endif /* HAVE_ALPN  */
7693 
7694 #ifdef HAVE_ALPN_PROTOS_SUPPORT
7695 static void CTX_set_alpn_protos(SSL_CTX *ctx)
7696 {
7697     unsigned char p[] = {
7698        8, 'h', 't', 't', 'p', '/', '1', '.', '1',
7699        6, 's', 'p', 'd', 'y', '/', '2',
7700        6, 's', 'p', 'd', 'y', '/', '1',
7701     };
7702 
7703     unsigned char p_len = sizeof(p);
7704     int ret;
7705 
7706     ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
7707 
7708 #ifdef WOLFSSL_ERROR_CODE_OPENSSL
7709     AssertIntEQ(ret, 0);
7710 #else
7711     AssertIntEQ(ret, SSL_SUCCESS);
7712 #endif
7713 }
7714 
7715 static void set_alpn_protos(SSL* ssl)
7716 {
7717     unsigned char p[] = {
7718        6, 's', 'p', 'd', 'y', '/', '3',
7719        8, 'h', 't', 't', 'p', '/', '1', '.', '1',
7720        6, 's', 'p', 'd', 'y', '/', '2',
7721        6, 's', 'p', 'd', 'y', '/', '1',
7722     };
7723 
7724     unsigned char p_len = sizeof(p);
7725     int ret;
7726 
7727     ret = SSL_set_alpn_protos(ssl, p, p_len);
7728 
7729 #ifdef WOLFSSL_ERROR_CODE_OPENSSL
7730     AssertIntEQ(ret, 0);
7731 #else
7732     AssertIntEQ(ret, SSL_SUCCESS);
7733 #endif
7734 
7735 }
7736 
7737 static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
7738 {
7739     /* "spdy/3" */
7740     char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
7741     const unsigned char *proto;
7742     unsigned int protoSz = 0;
7743 
7744     SSL_get0_alpn_selected(ssl, &proto, &protoSz);
7745 
7746     /* check value */
7747     AssertIntEQ(1, sizeof(nego_proto) == protoSz);
7748     AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
7749 }
7750 
7751 static void verify_alpn_matching_http1(WOLFSSL* ssl)
7752 {
7753     /* "http/1.1" */
7754     char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
7755     const unsigned char *proto;
7756     unsigned int protoSz = 0;
7757 
7758     SSL_get0_alpn_selected(ssl, &proto, &protoSz);
7759 
7760     /* check value */
7761     AssertIntEQ(1, sizeof(nego_proto) == protoSz);
7762     AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
7763 }
7764 
7765 static void test_wolfSSL_set_alpn_protos(void)
7766 {
7767     callback_functions client_cb;
7768     callback_functions server_cb;
7769 
7770     XMEMSET(&client_cb, 0, sizeof(callback_functions));
7771     XMEMSET(&server_cb, 0, sizeof(callback_functions));
7772     client_cb.method = wolfSSLv23_client_method;
7773     server_cb.method = wolfSSLv23_server_method;
7774     server_cb.devId = devId;
7775     server_cb.devId = devId;
7776 
7777     /* use CTX_alpn_protos */
7778     client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
7779     server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
7780     test_wolfSSL_client_server(&client_cb, &server_cb);
7781 
7782     /* use set_alpn_protos */
7783     client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
7784     server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
7785     test_wolfSSL_client_server(&client_cb, &server_cb);
7786 }
7787 
7788 #endif /* HAVE_ALPN_PROTOS_SUPPORT */
7789 
7790 static void test_wolfSSL_UseALPN(void)
7791 {
7792 #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
7793     defined(HAVE_IO_TESTS_DEPENDENCIES)
7794     test_wolfSSL_UseALPN_connection();
7795     test_wolfSSL_UseALPN_params();
7796 #endif
7797 
7798 #ifdef HAVE_ALPN_PROTOS_SUPPORT
7799     test_wolfSSL_set_alpn_protos();
7800 #endif
7801 }
7802 
7803 static void test_wolfSSL_DisableExtendedMasterSecret(void)
7804 {
7805 #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
7806     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7807     WOLFSSL     *ssl = wolfSSL_new(ctx);
7808 
7809     AssertNotNull(ctx);
7810     AssertNotNull(ssl);
7811 
7812     /* error cases */
7813     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
7814     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
7815 
7816     /* success cases */
7817     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
7818     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
7819 
7820     wolfSSL_free(ssl);
7821     wolfSSL_CTX_free(ctx);
7822 #endif
7823 }
7824 
7825 static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
7826 {
7827 #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
7828     WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
7829     WOLFSSL     *ssl = wolfSSL_new(ctx);
7830 
7831     AssertNotNull(ctx);
7832     AssertNotNull(ssl);
7833 
7834     /* error cases */
7835     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
7836     AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
7837 
7838     /* success cases */
7839     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
7840     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
7841 
7842     wolfSSL_free(ssl);
7843     wolfSSL_CTX_free(ctx);
7844 #endif
7845 }
7846 
7847 
7848 /*----------------------------------------------------------------------------*
7849  | X509 Tests
7850  *----------------------------------------------------------------------------*/
7851 static void test_wolfSSL_X509_NAME_get_entry(void)
7852 {
7853 #if !defined(NO_CERTS) && !defined(NO_RSA)
7854 #if defined(OPENSSL_ALL) || \
7855         (defined(OPENSSL_EXTRA) && \
7856             (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
7857     printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
7858 
7859     {
7860         /* use openssl like name to test mapping */
7861         X509_NAME_ENTRY* ne;
7862         X509_NAME* name;
7863         X509* x509;
7864     #ifndef NO_FILESYSTEM
7865         ASN1_STRING* asn;
7866         char* subCN = NULL;
7867     #endif
7868         int idx;
7869         ASN1_OBJECT *object = NULL;
7870 #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
7871 #ifndef NO_BIO
7872         BIO* bio;
7873 #endif
7874 #endif
7875 
7876     #ifndef NO_FILESYSTEM
7877         x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
7878         AssertNotNull(x509);
7879         name = X509_get_subject_name(x509);
7880         idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
7881         AssertIntGE(idx, 0);
7882         ne = X509_NAME_get_entry(name, idx);
7883         AssertNotNull(ne);
7884         asn = X509_NAME_ENTRY_get_data(ne);
7885         AssertNotNull(asn);
7886         subCN = (char*)ASN1_STRING_data(asn);
7887         AssertNotNull(subCN);
7888         wolfSSL_FreeX509(x509);
7889     #endif
7890 
7891         x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
7892         AssertNotNull(x509);
7893         name = X509_get_subject_name(x509);
7894         idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
7895         AssertIntGE(idx, 0);
7896 
7897 #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
7898 #ifndef NO_BIO
7899         AssertNotNull(bio = BIO_new(BIO_s_mem()));
7900         AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
7901                         (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
7902         AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
7903                         (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
7904         BIO_free(bio);
7905 #endif
7906 #endif
7907 
7908         ne = X509_NAME_get_entry(name, idx);
7909         AssertNotNull(ne);
7910         AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
7911         wolfSSL_FreeX509(x509);
7912     }
7913 
7914     printf(resultFmt, passed);
7915 #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
7916 #endif /* !NO_CERTS && !NO_RSA */
7917 }
7918 
7919 /* Testing functions dealing with PKCS12 parsing out X509 certs */
7920 static void test_wolfSSL_PKCS12(void)
7921 {
7922     /* .p12 file is encrypted with DES3 */
7923 #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
7924                    * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
7925                    * Password Key
7926                    */
7927 #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
7928     !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
7929     !defined(NO_SHA) && defined(HAVE_PKCS12)
7930     byte buf[6000];
7931     char file[] = "./certs/test-servercert.p12";
7932     char order[] = "./certs/ecc-rsa-server.p12";
7933 #ifdef WC_RC2
7934     char rc2p12[] = "./certs/test-servercert-rc2.p12";
7935 #endif
7936     char pass[] = "a password";
7937     const char goodPsw[] = "wolfSSL test";
7938     const char badPsw[] = "bad";
7939 #ifdef HAVE_ECC
7940     WOLFSSL_X509_NAME* subject;
7941     WOLFSSL_X509     *x509;
7942 #endif
7943     XFILE f;
7944     int  bytes, ret, goodPswLen, badPswLen;
7945     WOLFSSL_BIO      *bio;
7946     WOLFSSL_EVP_PKEY *pkey;
7947     WC_PKCS12        *pkcs12;
7948     WC_PKCS12        *pkcs12_2;
7949     WOLFSSL_X509     *cert;
7950     WOLFSSL_X509     *tmp;
7951     WOLF_STACK_OF(WOLFSSL_X509) *ca;
7952 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
7953     || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
7954     WOLFSSL_CTX      *ctx;
7955     WOLFSSL          *ssl;
7956     WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
7957 #endif
7958 
7959     printf(testingFmt, "wolfSSL_PKCS12()");
7960 
7961     f = XFOPEN(file, "rb");
7962     AssertTrue((f != XBADFILE));
7963     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
7964     XFCLOSE(f);
7965 
7966     goodPswLen = (int)XSTRLEN(goodPsw);
7967     badPswLen = (int)XSTRLEN(badPsw);
7968 
7969     bio = BIO_new_mem_buf((void*)buf, bytes);
7970     AssertNotNull(bio);
7971 
7972     pkcs12 = d2i_PKCS12_bio(bio, NULL);
7973     AssertNotNull(pkcs12);
7974     PKCS12_free(pkcs12);
7975 
7976     d2i_PKCS12_bio(bio, &pkcs12);
7977     AssertNotNull(pkcs12);
7978     BIO_free(bio);
7979 
7980     /* check verify MAC directly */
7981     ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
7982     AssertIntEQ(ret, 1);
7983 
7984     /* check verify MAC fail case directly */
7985     ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
7986     AssertIntEQ(ret, 0);
7987 
7988     /* check verify MAC fail case */
7989     ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
7990     AssertIntEQ(ret, 0);
7991     AssertNull(pkey);
7992     AssertNull(cert);
7993 
7994     /* check parse with no extra certs kept */
7995     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
7996     AssertIntEQ(ret, 1);
7997     AssertNotNull(pkey);
7998     AssertNotNull(cert);
7999 
8000     wolfSSL_EVP_PKEY_free(pkey);
8001     wolfSSL_X509_free(cert);
8002 
8003     /* check parse with extra certs kept */
8004     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
8005     AssertIntEQ(ret, 1);
8006     AssertNotNull(pkey);
8007     AssertNotNull(cert);
8008     AssertNotNull(ca);
8009 
8010 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
8011     || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
8012 
8013     /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
8014 #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
8015     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
8016 #else
8017     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
8018 #endif
8019     /* Copy stack structure */
8020     AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
8021     AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
8022     /* CTX now owns the tmp_ca stack structure */
8023     tmp_ca = NULL;
8024     AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
8025     AssertNotNull(tmp_ca);
8026     AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
8027     /* Check that the main cert is also set */
8028     AssertNotNull(SSL_CTX_get0_certificate(ctx));
8029     AssertNotNull(ssl = SSL_new(ctx));
8030     AssertNotNull(SSL_get_certificate(ssl));
8031     SSL_free(ssl);
8032     SSL_CTX_free(ctx);
8033 #endif
8034 
8035     /* should be 2 other certs on stack */
8036     tmp = sk_X509_pop(ca);
8037     AssertNotNull(tmp);
8038     X509_free(tmp);
8039     tmp = sk_X509_pop(ca);
8040     AssertNotNull(tmp);
8041     X509_free(tmp);
8042     AssertNull(sk_X509_pop(ca));
8043 
8044     EVP_PKEY_free(pkey);
8045     X509_free(cert);
8046     sk_X509_pop_free(ca, X509_free);
8047 
8048     /* check PKCS12_create */
8049     AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
8050     AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
8051             SSL_SUCCESS);
8052     AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
8053                     -1, -1, 100, -1, 0)));
8054     EVP_PKEY_free(pkey);
8055     X509_free(cert);
8056     sk_X509_pop_free(ca, NULL);
8057 
8058     AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
8059             SSL_SUCCESS);
8060     PKCS12_free(pkcs12_2);
8061     AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
8062              NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
8063              NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
8064              2000, 1, 0)));
8065     EVP_PKEY_free(pkey);
8066     X509_free(cert);
8067     sk_X509_pop_free(ca, NULL);
8068 
8069     /* convert to DER then back and parse */
8070     AssertNotNull(bio = BIO_new(BIO_s_mem()));
8071     AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
8072     PKCS12_free(pkcs12_2);
8073 
8074     AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
8075     BIO_free(bio);
8076     AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
8077             SSL_SUCCESS);
8078 
8079     /* should be 2 other certs on stack */
8080     tmp = sk_X509_pop(ca);
8081     AssertNotNull(tmp);
8082     X509_free(tmp);
8083     tmp = sk_X509_pop(ca);
8084     AssertNotNull(tmp);
8085     X509_free(tmp);
8086     AssertNull(sk_X509_pop(ca));
8087 
8088 
8089 #ifndef NO_RC4
8090     PKCS12_free(pkcs12_2);
8091     AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
8092              NID_pbe_WithSHA1And128BitRC4,
8093              NID_pbe_WithSHA1And128BitRC4,
8094              2000, 1, 0)));
8095     EVP_PKEY_free(pkey);
8096     X509_free(cert);
8097     sk_X509_pop_free(ca, NULL);
8098 
8099     AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
8100             SSL_SUCCESS);
8101 
8102 #endif /* NO_RC4 */
8103 
8104     EVP_PKEY_free(pkey);
8105     X509_free(cert);
8106     PKCS12_free(pkcs12);
8107     PKCS12_free(pkcs12_2);
8108     sk_X509_pop_free(ca, NULL);
8109 
8110 #ifdef HAVE_ECC
8111     /* test order of parsing */
8112     f = XFOPEN(order, "rb");
8113     AssertTrue(f != XBADFILE);
8114     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
8115     XFCLOSE(f);
8116 
8117     AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
8118     AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
8119     AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
8120             WOLFSSL_SUCCESS);
8121     AssertNotNull(pkey);
8122     AssertNotNull(cert);
8123     AssertNotNull(ca);
8124 
8125     /* compare subject lines of certificates */
8126     AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
8127     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
8128                 SSL_FILETYPE_PEM));
8129     AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
8130             (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
8131     X509_free(x509);
8132 
8133     /* test expected fail case */
8134     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
8135                 SSL_FILETYPE_PEM));
8136     AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
8137             (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
8138     X509_free(x509);
8139     X509_free(cert);
8140 
8141     /* get subject line from ca stack */
8142     AssertNotNull(cert = sk_X509_pop(ca));
8143     AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
8144 
8145     /* compare subject from certificate in ca to expected */
8146     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
8147                 SSL_FILETYPE_PEM));
8148     AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
8149             (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
8150 
8151     EVP_PKEY_free(pkey);
8152     X509_free(x509);
8153     X509_free(cert);
8154     BIO_free(bio);
8155     PKCS12_free(pkcs12);
8156     sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
8157 
8158     /* test order of parsing */
8159     f = XFOPEN(file, "rb");
8160     AssertTrue(f != XBADFILE);
8161     AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
8162     XFCLOSE(f);
8163 
8164     /* check verify MAC fail case */
8165     ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
8166     AssertIntEQ(ret, 0);
8167     AssertNull(pkey);
8168     AssertNull(cert);
8169 
8170     /* check parse with no extra certs kept */
8171     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
8172     AssertIntEQ(ret, 1);
8173     AssertNotNull(pkey);
8174     AssertNotNull(cert);
8175 
8176     wolfSSL_EVP_PKEY_free(pkey);
8177     wolfSSL_X509_free(cert);
8178 
8179     /* check parse with extra certs kept */
8180     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
8181     AssertIntEQ(ret, 1);
8182     AssertNotNull(pkey);
8183     AssertNotNull(cert);
8184     AssertNotNull(ca);
8185 
8186     wolfSSL_EVP_PKEY_free(pkey);
8187     wolfSSL_X509_free(cert);
8188     sk_X509_pop_free(ca, NULL);
8189 
8190     PKCS12_free(pkcs12);
8191 #endif /* HAVE_ECC */
8192 
8193 #ifdef WC_RC2
8194     /* test PKCS#12 with RC2 encryption */
8195     f = XFOPEN(rc2p12, "rb");
8196     AssertTrue(f != XBADFILE);
8197     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
8198     XFCLOSE(f);
8199 
8200     AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
8201     AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
8202 
8203     /* check verify MAC fail case */
8204     ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
8205     AssertIntEQ(ret, 0);
8206     AssertNull(pkey);
8207     AssertNull(cert);
8208 
8209     /* check parse iwth not extra certs kept */
8210     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
8211     AssertIntEQ(ret, WOLFSSL_SUCCESS);
8212     AssertNotNull(pkey);
8213     AssertNotNull(cert);
8214 
8215     /* check parse with extra certs kept */
8216     ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
8217     AssertIntEQ(ret, WOLFSSL_SUCCESS);
8218     AssertNotNull(pkey);
8219     AssertNotNull(cert);
8220     AssertNotNull(ca);
8221 
8222     wolfSSL_EVP_PKEY_free(pkey);
8223     wolfSSL_X509_free(cert);
8224     sk_X509_pop_free(ca, NULL);
8225 
8226     BIO_free(bio);
8227     PKCS12_free(pkcs12);
8228 #endif /* WC_RC2 */
8229 
8230     /* Test i2d_PKCS12_bio */
8231     f = XFOPEN(file, "rb");
8232     AssertTrue((f != XBADFILE));
8233     AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
8234     XFCLOSE(f);
8235 
8236     bio = BIO_new(BIO_s_mem());
8237     AssertNotNull(bio);
8238 
8239     ret = i2d_PKCS12_bio(bio, pkcs12);
8240     AssertIntEQ(ret, 1);
8241 
8242     ret = i2d_PKCS12_bio(NULL, pkcs12);
8243     AssertIntEQ(ret, 0);
8244 
8245     ret = i2d_PKCS12_bio(bio, NULL);
8246     AssertIntEQ(ret, 0);
8247 
8248     PKCS12_free(pkcs12);
8249     BIO_free(bio);
8250 
8251     (void)order;
8252 
8253     printf(resultFmt, passed);
8254 #endif /* OPENSSL_EXTRA */
8255 #endif /* HAVE_FIPS */
8256 }
8257 
8258 
8259 #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
8260     defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
8261     (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
8262     #define TEST_PKCS8_ENC
8263 #endif
8264 
8265 #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
8266     && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
8267 
8268 /* used to keep track if FailTestCallback was called */
8269 static int failTestCallbackCalled = 0;
8270 
8271 static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
8272 {
8273     (void)passwd;
8274     (void)sz;
8275     (void)rw;
8276     (void)userdata;
8277 
8278     /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
8279     failTestCallbackCalled = 1;
8280 
8281     return -1;
8282 }
8283 #endif
8284 
8285 static void test_wolfSSL_no_password_cb(void)
8286 {
8287 #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
8288     && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
8289     WOLFSSL_CTX* ctx;
8290     byte buff[FOURK_BUF];
8291     const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
8292     const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
8293     XFILE f;
8294     int bytes;
8295 
8296     printf(testingFmt, "test_wolfSSL_no_password_cb()");
8297 
8298 #ifndef NO_WOLFSSL_CLIENT
8299     AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
8300 #else
8301     AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
8302 #endif
8303     wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
8304 
8305     AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
8306     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8307     XFCLOSE(f);
8308     AssertIntLE(bytes, sizeof(buff));
8309     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8310                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8311 
8312     AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
8313     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8314     XFCLOSE(f);
8315     AssertIntLE(bytes, sizeof(buff));
8316     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8317                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8318 
8319     wolfSSL_CTX_free(ctx);
8320 
8321     if (failTestCallbackCalled != 0) {
8322         Fail(("Password callback should not be called by default"),
8323             ("Password callback was called without attempting "
8324              "to first decipher private key without password."));
8325 }
8326 
8327     printf(resultFmt, passed);
8328 #endif
8329 }
8330 
8331 #ifdef TEST_PKCS8_ENC
8332 /* for PKCS8 test case */
8333 static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
8334 {
8335     int flag = 0;
8336 
8337     (void)rw;
8338     if (userdata != NULL) {
8339         flag = *((int*)userdata); /* user set data */
8340     }
8341 
8342     switch (flag) {
8343         case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
8344                  * can be anything the user wishes to be passed to the callback
8345                  * associated with the WOLFSSL_CTX */
8346             XSTRNCPY(passwd, "yassl123", sz);
8347             return 8;
8348 
8349         default:
8350             return BAD_FUNC_ARG;
8351     }
8352 }
8353 #endif /* TEST_PKCS8_ENC */
8354 
8355 /* Testing functions dealing with PKCS8 */
8356 static void test_wolfSSL_PKCS8(void)
8357 {
8358 #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
8359     byte buff[FOURK_BUF];
8360     byte der[FOURK_BUF];
8361     #ifndef NO_RSA
8362         const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
8363         const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
8364     #endif
8365     const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
8366     #ifdef HAVE_ECC
8367         const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
8368     #endif
8369     XFILE f;
8370     int bytes;
8371     WOLFSSL_CTX* ctx;
8372 #if defined(HAVE_ECC) && !defined(NO_CODING)
8373     int ret;
8374     ecc_key key;
8375     word32 x = 0;
8376 #endif
8377 #ifdef TEST_PKCS8_ENC
8378     #if !defined(NO_RSA) && !defined(NO_SHA)
8379         const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
8380         const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
8381     #endif
8382     #if defined(HAVE_ECC) && !defined(NO_SHA)
8383         const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
8384         const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
8385     #endif
8386     int flag;
8387 #endif
8388 
8389     (void)der;
8390 
8391     printf(testingFmt, "wolfSSL_PKCS8()");
8392 
8393 #ifndef NO_WOLFSSL_CLIENT
8394     #ifndef WOLFSSL_NO_TLS12
8395         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
8396     #else
8397         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
8398     #endif
8399 #else
8400     #ifndef WOLFSSL_NO_TLS12
8401         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
8402     #else
8403         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
8404     #endif
8405 #endif
8406 
8407 #ifdef TEST_PKCS8_ENC
8408     wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
8409     wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
8410     flag = 1; /* used by password callback as return code */
8411 
8412     #if !defined(NO_RSA) && !defined(NO_SHA)
8413     /* test loading PEM PKCS8 encrypted file */
8414     f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
8415     AssertTrue((f != XBADFILE));
8416     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8417     XFCLOSE(f);
8418     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8419                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8420 
8421     /* this next case should fail because of password callback return code */
8422     flag = 0; /* used by password callback as return code */
8423     AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8424                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8425 
8426     /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
8427     AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
8428         "yassl123"), 0);
8429 
8430     /* test that error value is returned with a bad password */
8431     AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
8432         "bad"), 0);
8433 
8434     /* test loading PEM PKCS8 encrypted file */
8435     f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
8436     AssertTrue((f != XBADFILE));
8437     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8438     XFCLOSE(f);
8439     flag = 1; /* used by password callback as return code */
8440     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8441                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8442 
8443     /* this next case should fail because of password callback return code */
8444     flag = 0; /* used by password callback as return code */
8445     AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8446                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8447     #endif /* !NO_RSA && !NO_SHA */
8448 
8449     #if defined(HAVE_ECC) && !defined(NO_SHA)
8450     /* test loading PEM PKCS8 encrypted ECC Key file */
8451     f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
8452     AssertTrue((f != XBADFILE));
8453     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8454     XFCLOSE(f);
8455     flag = 1; /* used by password callback as return code */
8456     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8457                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8458 
8459     /* this next case should fail because of password callback return code */
8460     flag = 0; /* used by password callback as return code */
8461     AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8462                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8463 
8464     /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
8465     AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
8466         "yassl123"), 0);
8467 
8468     /* test that error value is returned with a bad password */
8469     AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
8470         "bad"), 0);
8471 
8472     /* test loading DER PKCS8 encrypted ECC Key file */
8473     f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
8474     AssertTrue((f != XBADFILE));
8475     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8476     XFCLOSE(f);
8477     flag = 1; /* used by password callback as return code */
8478     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8479                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8480 
8481     /* this next case should fail because of password callback return code */
8482     flag = 0; /* used by password callback as return code */
8483     AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8484                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8485 
8486     /* leave flag as "okay" */
8487     flag = 1;
8488     #endif /* HAVE_ECC && !NO_SHA */
8489 #endif /* TEST_PKCS8_ENC */
8490 
8491 
8492 #ifndef NO_RSA
8493     /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
8494     f = XFOPEN(serverKeyPkcs8DerFile, "rb");
8495     AssertTrue((f != XBADFILE));
8496     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8497     XFCLOSE(f);
8498     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8499                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8500 
8501     /* test loading PEM PKCS8 private key file (not encrypted) */
8502     f = XFOPEN(serverKeyPkcs8PemFile, "rb");
8503     AssertTrue((f != XBADFILE));
8504     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8505     XFCLOSE(f);
8506     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8507                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8508 #endif /* !NO_RSA */
8509 
8510     /* Test PKCS8 PEM ECC key no crypt */
8511     f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
8512     AssertTrue((f != XBADFILE));
8513     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8514     XFCLOSE(f);
8515 #ifdef HAVE_ECC
8516     /* Test PKCS8 PEM ECC key no crypt */
8517     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8518                 WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
8519 
8520 #ifndef NO_CODING
8521     /* decrypt PKCS8 PEM to key in DER format */
8522     AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
8523         (word32)sizeof(der), NULL)), 0);
8524     ret = wc_ecc_init(&key);
8525     if (ret == 0) {
8526         ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
8527         wc_ecc_free(&key);
8528     }
8529     AssertIntEQ(ret, 0);
8530 #endif
8531 
8532     /* Test PKCS8 DER ECC key no crypt */
8533     f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
8534     AssertTrue((f != XBADFILE));
8535     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
8536     XFCLOSE(f);
8537 
8538     /* Test using a PKCS8 ECC PEM */
8539     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
8540                 WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8541 #else
8542     /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
8543     AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
8544         (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
8545 #endif /* HAVE_ECC */
8546 
8547     wolfSSL_CTX_free(ctx);
8548 
8549     printf(resultFmt, passed);
8550 #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
8551 }
8552 
8553 static void test_wolfSSL_PKCS8_ED25519(void)
8554 {
8555 #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
8556     defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
8557     defined(HAVE_ED25519_KEY_IMPORT)
8558     const byte encPrivKey[] = \
8559     "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
8560     "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
8561     "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
8562     "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
8563     "2L9W4v7swXkV+X+a1ww=\n"
8564     "-----END ENCRYPTED PRIVATE KEY-----\n";
8565     const char password[] = "abcdefghijklmnopqrstuvwxyz";
8566     byte der[FOURK_BUF];
8567     WOLFSSL_CTX* ctx;
8568     int bytes;
8569 
8570     XMEMSET(der, 0, sizeof(der));
8571     AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
8572         (word32)sizeof(der), password)), 0);
8573 #ifndef NO_WOLFSSL_SERVER
8574     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
8575 #else
8576     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
8577 #endif
8578     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
8579         WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8580 
8581     wolfSSL_CTX_free(ctx);
8582 #endif
8583 }
8584 
8585 static void test_wolfSSL_PKCS8_ED448(void)
8586 {
8587 #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
8588     defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
8589     defined(HAVE_ED448_KEY_IMPORT)
8590     const byte encPrivKey[] = \
8591     "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
8592     "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
8593     "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
8594     "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
8595     "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
8596     "-----END ENCRYPTED PRIVATE KEY-----\n";
8597     const char password[] = "abcdefghijklmnopqrstuvwxyz";
8598     byte der[FOURK_BUF];
8599     WOLFSSL_CTX* ctx;
8600     int bytes;
8601 
8602     XMEMSET(der, 0, sizeof(der));
8603     AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
8604         (word32)sizeof(der), password)), 0);
8605 #ifndef NO_WOLFSSL_SERVER
8606     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
8607 #else
8608     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
8609 #endif
8610     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
8611         WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8612 
8613     wolfSSL_CTX_free(ctx);
8614 #endif
8615 }
8616 
8617 /* Testing functions dealing with PKCS5 */
8618 static void test_wolfSSL_PKCS5(void)
8619 {
8620 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
8621 #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
8622     const char* passwd = "myfipsPa$$W0rd";
8623 #else
8624     const char *passwd = "pass1234";
8625 #endif
8626     const unsigned char *salt = (unsigned char *)"salt1234";
8627     unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
8628                                                   DYNAMIC_TYPE_TMP_BUFFER);
8629     int ret = 0;
8630 
8631     AssertNotNull(out);
8632     ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
8633                                  (int)XSTRLEN((const char *) salt), 10,
8634                                  WC_SHA_DIGEST_SIZE,out);
8635     AssertIntEQ(ret, SSL_SUCCESS);
8636 
8637 #ifdef WOLFSSL_SHA512
8638     ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
8639                              (int)XSTRLEN((const char *) salt), 10,
8640                              wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
8641     AssertIntEQ(ret, SSL_SUCCESS);
8642 #endif
8643 
8644     XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
8645 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
8646 }
8647 
8648 /* test parsing URI from certificate */
8649 static void test_wolfSSL_URI(void)
8650 {
8651 #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
8652     && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
8653     defined(OPENSSL_EXTRA))
8654     WOLFSSL_X509* x509;
8655     const char uri[] = "./certs/client-uri-cert.pem";
8656     const char badUri[] = "./certs/client-relative-uri.pem";
8657 
8658     printf(testingFmt, "wolfSSL URI parse");
8659 
8660     x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
8661     AssertNotNull(x509);
8662 
8663     wolfSSL_FreeX509(x509);
8664 
8665     x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
8666 #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT)
8667     AssertNull(x509);
8668 #else
8669     AssertNotNull(x509);
8670 #endif
8671 
8672     printf(resultFmt, passed);
8673 #endif
8674 }
8675 
8676 
8677 static void test_wolfSSL_TBS(void)
8678 {
8679 #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
8680     && defined(OPENSSL_EXTRA)
8681     WOLFSSL_X509* x509;
8682     const unsigned char* tbs;
8683     int tbsSz;
8684 
8685     printf(testingFmt, "wolfSSL TBS");
8686 
8687     AssertNotNull(x509 =
8688           wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
8689 
8690     AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
8691     AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
8692     AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
8693     AssertIntEQ(tbsSz, 1003);
8694 
8695     wolfSSL_FreeX509(x509);
8696 
8697     printf(resultFmt, passed);
8698 #endif
8699 }
8700 
8701 static void test_wolfSSL_X509_verify(void)
8702 {
8703 #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
8704     && defined(OPENSSL_EXTRA)
8705     WOLFSSL_X509* ca;
8706     WOLFSSL_X509* serv;
8707     WOLFSSL_EVP_PKEY* pkey;
8708     unsigned char buf[2048];
8709     const unsigned char* pt = NULL;
8710     int bufSz;
8711 
8712     printf(testingFmt, "wolfSSL X509 verify");
8713 
8714     AssertNotNull(ca =
8715           wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
8716 
8717     AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
8718             WOLFSSL_SUCCESS);
8719     AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
8720             WOLFSSL_SUCCESS);
8721     AssertIntEQ(bufSz, 294);
8722 
8723     bufSz = 2048;
8724     AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
8725             WOLFSSL_SUCCESS);
8726     AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
8727     AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
8728 
8729 
8730     AssertNotNull(serv =
8731           wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
8732 
8733     /* success case */
8734     pt = buf;
8735     AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
8736 
8737     AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
8738 
8739     AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
8740     wolfSSL_EVP_PKEY_free(pkey);
8741 
8742     /* fail case */
8743     bufSz = 2048;
8744     AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
8745             WOLFSSL_SUCCESS);
8746     pt = buf;
8747     AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
8748     AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
8749 
8750     AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
8751     AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
8752     wolfSSL_EVP_PKEY_free(pkey);
8753 
8754     wolfSSL_FreeX509(ca);
8755     wolfSSL_FreeX509(serv);
8756 
8757     printf(resultFmt, passed);
8758 #endif
8759 }
8760 
8761 
8762 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
8763         !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
8764          defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
8765         defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
8766 /* create certificate with version 2 */
8767 static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
8768 {
8769     WOLFSSL_X509 *x509, *x509v2;
8770     WOLFSSL_EVP_PKEY *priv, *pub;
8771     unsigned char *der = NULL, *key = NULL, *pt;
8772     char *header, *name;
8773     int derSz;
8774     long keySz;
8775     XFILE fp;
8776     WOLFSSL_ASN1_TIME *notBefore, *notAfter;
8777     time_t t;
8778 
8779     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
8780                 WOLFSSL_FILETYPE_PEM));
8781 
8782     fp = XFOPEN(cliKeyFile, "rb");
8783     AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
8784             WOLFSSL_SUCCESS);
8785     XFCLOSE(fp);
8786     pt = key;
8787     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
8788                 (const unsigned char**)&pt, keySz));
8789 
8790 
8791     /* create the version 2 certificate */
8792     AssertNotNull(x509v2 = X509_new());
8793     AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
8794 
8795     AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
8796                 wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
8797     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
8798                 wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
8799     AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
8800     AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
8801 
8802     t = time(NULL);
8803     AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
8804     AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
8805     AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
8806     AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
8807 
8808     AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
8809     derSz = wolfSSL_i2d_X509(x509v2, &der);
8810     AssertIntGT(derSz, 0);
8811     AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
8812                                      WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
8813     XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
8814     XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
8815     XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
8816     XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
8817     wolfSSL_X509_free(x509);
8818     wolfSSL_X509_free(x509v2);
8819     wolfSSL_EVP_PKEY_free(priv);
8820     wolfSSL_EVP_PKEY_free(pub);
8821     wolfSSL_ASN1_TIME_free(notBefore);
8822     wolfSSL_ASN1_TIME_free(notAfter);
8823 }
8824 
8825 
8826 /* override certificate version error */
8827 static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
8828 {
8829 #ifndef OPENSSL_COMPATIBLE_DEFAULTS
8830     AssertIntEQ(store->error, ASN_VERSION_E);
8831 #else
8832     AssertIntEQ(store->error, 0);
8833 #endif
8834     AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
8835     (void)preverify;
8836     return 1;
8837 }
8838 
8839 
8840 /* set verify callback that will override bad certificate version */
8841 static void test_set_override_x509(WOLFSSL_CTX* ctx)
8842 {
8843     wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
8844 }
8845 #endif
8846 
8847 
8848 static void test_wolfSSL_X509_TLS_version(void)
8849 {
8850 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
8851         !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
8852          defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
8853         defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
8854     tcp_ready   ready;
8855     func_args   server_args;
8856     func_args   client_args;
8857     THREAD_TYPE serverThread;
8858     callback_functions func_cb_client;
8859     callback_functions func_cb_server;
8860 
8861     printf(testingFmt, "test_wolfSSL_X509_TLS_version");
8862 
8863     /* test server rejects a client certificate that is not version 3 */
8864 #ifdef WOLFSSL_TIRTOS
8865     fdOpenSession(Task_self());
8866 #endif
8867     XMEMSET(&server_args, 0, sizeof(func_args));
8868     XMEMSET(&client_args, 0, sizeof(func_args));
8869     XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
8870     XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
8871 
8872     StartTCP();
8873     InitTcpReady(&ready);
8874 
8875 #if defined(USE_WINDOWS_API)
8876     /* use RNG to get random port if using windows */
8877     ready.port = GetRandomPort();
8878 #endif
8879 
8880     server_args.signal = &ready;
8881     client_args.signal = &ready;
8882     server_args.return_code = TEST_FAIL;
8883     client_args.return_code = TEST_FAIL;
8884 
8885     func_cb_client.ctx_ready = &test_set_x509_badversion;
8886 #ifndef WOLFSSL_NO_TLS12
8887     func_cb_client.method = wolfTLSv1_2_client_method;
8888 #else
8889     func_cb_client.method = wolfTLSv1_3_client_method;
8890 #endif
8891     client_args.callbacks = &func_cb_client;
8892 
8893 #ifndef WOLFSSL_NO_TLS12
8894     func_cb_server.method = wolfTLSv1_2_server_method;
8895 #else
8896     func_cb_server.method = wolfTLSv1_3_server_method;
8897 #endif
8898     server_args.callbacks = &func_cb_server;
8899 
8900     start_thread(test_server_nofail, &server_args, &serverThread);
8901     wait_tcp_ready(&server_args);
8902     test_client_nofail(&client_args, NULL);
8903     join_thread(serverThread);
8904 
8905 #ifndef OPENSSL_COMPATIBLE_DEFAULTS
8906     AssertIntEQ(client_args.return_code, TEST_FAIL);
8907     AssertIntEQ(server_args.return_code, TEST_FAIL);
8908 #else
8909     AssertIntEQ(client_args.return_code, TEST_SUCCESS);
8910     AssertIntEQ(server_args.return_code, TEST_SUCCESS);
8911 #endif
8912 
8913     FreeTcpReady(&ready);
8914 
8915 #ifdef WOLFSSL_TIRTOS
8916     fdCloseSession(Task_self());
8917 #endif
8918 
8919     /* Now re run but override the bad X509 version */
8920 #ifdef WOLFSSL_TIRTOS
8921     fdOpenSession(Task_self());
8922 #endif
8923     XMEMSET(&server_args, 0, sizeof(func_args));
8924     XMEMSET(&client_args, 0, sizeof(func_args));
8925     XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
8926     XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
8927 
8928     StartTCP();
8929     InitTcpReady(&ready);
8930 
8931 #if defined(USE_WINDOWS_API)
8932     /* use RNG to get random port if using windows */
8933     ready.port = GetRandomPort();
8934 #endif
8935 
8936     server_args.signal = &ready;
8937     client_args.signal = &ready;
8938     server_args.return_code = TEST_FAIL;
8939     client_args.return_code = TEST_FAIL;
8940 
8941     func_cb_client.ctx_ready = &test_set_x509_badversion;
8942     func_cb_server.ctx_ready = &test_set_override_x509;
8943 #ifndef WOLFSSL_NO_TLS12
8944     func_cb_client.method = wolfTLSv1_2_client_method;
8945 #else
8946     func_cb_client.method = wolfTLSv1_3_client_method;
8947 #endif
8948     client_args.callbacks = &func_cb_client;
8949 
8950 #ifndef WOLFSSL_NO_TLS12
8951     func_cb_server.method = wolfTLSv1_2_server_method;
8952 #else
8953     func_cb_server.method = wolfTLSv1_3_server_method;
8954 #endif
8955     server_args.callbacks = &func_cb_server;
8956 
8957     start_thread(test_server_nofail, &server_args, &serverThread);
8958     wait_tcp_ready(&server_args);
8959     test_client_nofail(&client_args, NULL);
8960     join_thread(serverThread);
8961 
8962     AssertIntEQ(client_args.return_code, TEST_SUCCESS);
8963     AssertIntEQ(server_args.return_code, TEST_SUCCESS);
8964 
8965     FreeTcpReady(&ready);
8966 
8967 #ifdef WOLFSSL_TIRTOS
8968     fdCloseSession(Task_self());
8969 #endif
8970 
8971     printf(resultFmt, passed);
8972 #endif
8973 }
8974 
8975 /* Testing function  wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
8976  * version allowed.
8977  * POST: 1 on success.
8978  */
8979 static int test_wolfSSL_CTX_SetMinVersion(void)
8980 {
8981     int                     failFlag = WOLFSSL_SUCCESS;
8982 #ifndef NO_WOLFSSL_CLIENT
8983     WOLFSSL_CTX*            ctx;
8984     int                     itr;
8985 
8986     #ifndef NO_OLD_TLS
8987         const int versions[]  = {
8988                             #ifdef WOLFSSL_ALLOW_TLSV10
8989                                   WOLFSSL_TLSV1,
8990                             #endif
8991                                   WOLFSSL_TLSV1_1,
8992                                   WOLFSSL_TLSV1_2 };
8993     #elif !defined(WOLFSSL_NO_TLS12)
8994         const int versions[]  = { WOLFSSL_TLSV1_2 };
8995     #elif defined(WOLFSSL_TLS13)
8996         const int versions[]  = { WOLFSSL_TLSV1_3 };
8997     #else
8998         const int versions[0];
8999     #endif
9000 
9001     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
9002 
9003     printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
9004 
9005     for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
9006         if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
9007             failFlag = WOLFSSL_FAILURE;
9008         }
9009     }
9010 
9011     printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
9012 
9013     wolfSSL_CTX_free(ctx);
9014 #endif
9015     return failFlag;
9016 
9017 } /* END test_wolfSSL_CTX_SetMinVersion */
9018 
9019 
9020 /*----------------------------------------------------------------------------*
9021  | OCSP Stapling
9022  *----------------------------------------------------------------------------*/
9023 
9024 
9025 /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
9026  * need to contact the CA, lowering the cost of cert revocation checking.
9027  * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
9028  * POST: 1 returned for success.
9029  */
9030 static int test_wolfSSL_UseOCSPStapling(void)
9031 {
9032     #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
9033             !defined(NO_WOLFSSL_CLIENT)
9034         int             ret;
9035         WOLFSSL_CTX*    ctx;
9036         WOLFSSL*        ssl;
9037 
9038 #ifndef NO_WOLFSSL_CLIENT
9039     #ifndef WOLFSSL_NO_TLS12
9040         ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
9041     #else
9042         ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
9043     #endif
9044 #else
9045     #ifndef WOLFSSL_NO_TLS12
9046         ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
9047     #else
9048         ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
9049     #endif
9050 #endif
9051         ssl = wolfSSL_new(ctx);
9052         printf(testingFmt, "wolfSSL_UseOCSPStapling()");
9053 
9054         ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
9055                                     WOLFSSL_CSR2_OCSP_USE_NONCE);
9056 
9057         printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
9058 
9059 
9060         wolfSSL_free(ssl);
9061         wolfSSL_CTX_free(ctx);
9062 
9063         return ret;
9064     #else
9065         return WOLFSSL_SUCCESS;
9066     #endif
9067 
9068 } /*END test_wolfSSL_UseOCSPStapling */
9069 
9070 
9071 /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
9072  * stapling eliminates the need to contact the CA and lowers cert revocation
9073  * check.
9074  * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
9075  */
9076 static int test_wolfSSL_UseOCSPStaplingV2 (void)
9077 {
9078     #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
9079             !defined(NO_WOLFSSL_CLIENT)
9080         int                 ret;
9081         WOLFSSL_CTX*        ctx;
9082         WOLFSSL*            ssl;
9083 
9084 #ifndef NO_WOLFSSL_CLIENT
9085     #ifndef WOLFSSL_NO_TLS12
9086         ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
9087     #else
9088         ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
9089     #endif
9090 #else
9091     #ifndef WOLFSSL_NO_TLS12
9092         ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
9093     #else
9094         ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
9095     #endif
9096 #endif
9097         ssl = wolfSSL_new(ctx);
9098         printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
9099 
9100         ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
9101                                         WOLFSSL_CSR2_OCSP_USE_NONCE );
9102 
9103         printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
9104 
9105         wolfSSL_free(ssl);
9106         wolfSSL_CTX_free(ctx);
9107 
9108         return ret;
9109     #else
9110         return WOLFSSL_SUCCESS;
9111     #endif
9112 
9113 } /*END test_wolfSSL_UseOCSPStaplingV2*/
9114 
9115 /*----------------------------------------------------------------------------*
9116  | Multicast Tests
9117  *----------------------------------------------------------------------------*/
9118 static void test_wolfSSL_mcast(void)
9119 {
9120 #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
9121     (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
9122     WOLFSSL_CTX* ctx;
9123     WOLFSSL* ssl;
9124     int result;
9125     byte preMasterSecret[512];
9126     byte clientRandom[32];
9127     byte serverRandom[32];
9128     byte suite[2] = {0, 0xfe};  /* WDM_WITH_NULL_SHA256 */
9129     byte buf[256];
9130     word16 newId;
9131 
9132     ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
9133     AssertNotNull(ctx);
9134 
9135     result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
9136     AssertIntEQ(result, WOLFSSL_SUCCESS);
9137 
9138     ssl = wolfSSL_new(ctx);
9139     AssertNotNull(ssl);
9140 
9141     XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
9142     XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
9143     XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
9144     result = wolfSSL_set_secret(ssl, 23,
9145                 preMasterSecret, sizeof(preMasterSecret),
9146                 clientRandom, serverRandom, suite);
9147     AssertIntEQ(result, WOLFSSL_SUCCESS);
9148 
9149     result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
9150     AssertIntLE(result, 0);
9151     AssertIntLE(newId, 100);
9152 
9153     wolfSSL_free(ssl);
9154     wolfSSL_CTX_free(ctx);
9155 #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
9156 }
9157 
9158 
9159 /*----------------------------------------------------------------------------*
9160  |  Wolfcrypt
9161  *----------------------------------------------------------------------------*/
9162 
9163 /*
9164  * Unit test for the wc_InitBlake2b()
9165  */
9166 static int test_wc_InitBlake2b (void)
9167 {
9168     int ret = 0;
9169 #ifdef HAVE_BLAKE2
9170 
9171     Blake2b blake2b;
9172 
9173     printf(testingFmt, "wc_InitBlake2B()");
9174 
9175     /* Test good arg. */
9176     ret = wc_InitBlake2b(&blake2b, 64);
9177     if (ret != 0) {
9178         ret = WOLFSSL_FATAL_ERROR;
9179     }
9180 
9181     /* Test bad arg. */
9182     if (!ret) {
9183         ret = wc_InitBlake2b(NULL, 64);
9184         if (ret == 0) {
9185             ret = WOLFSSL_FATAL_ERROR;
9186         } else {
9187             ret = 0;
9188         }
9189     }
9190 
9191     if (!ret) {
9192         ret = wc_InitBlake2b(NULL, 128);
9193         if (ret == 0) {
9194             ret = WOLFSSL_FATAL_ERROR;
9195         } else {
9196             ret = 0;
9197         }
9198     }
9199 
9200     if (!ret) {
9201         ret = wc_InitBlake2b(&blake2b, 128);
9202         if (ret == 0) {
9203             ret = WOLFSSL_FATAL_ERROR;
9204         } else {
9205             ret = 0;
9206         }
9207     }
9208 
9209     if (!ret) {
9210         ret = wc_InitBlake2b(NULL, 0);
9211         if (ret == 0) {
9212             ret = WOLFSSL_FATAL_ERROR;
9213         } else {
9214             ret = 0;
9215         }
9216     }
9217 
9218     if (!ret) {
9219         ret = wc_InitBlake2b(&blake2b, 0);
9220         if (ret == 0) {
9221             ret = WOLFSSL_FATAL_ERROR;
9222         } else {
9223             ret = 0;
9224         }
9225     }
9226 
9227     printf(resultFmt, ret == 0 ? passed : failed);
9228 
9229 #endif
9230     return ret;
9231 }     /*END test_wc_InitBlake2b*/
9232 
9233 /*
9234  * Unit test for the wc_InitBlake2b_WithKey()
9235  */
9236 static int test_wc_InitBlake2b_WithKey (void)
9237 {
9238     int ret = 0;
9239 #ifdef HAVE_BLAKE2
9240     Blake2b     blake2b;
9241     word32      digestSz = BLAKE2B_KEYBYTES;
9242     byte        key[BLAKE2B_KEYBYTES];
9243     word32      keylen = BLAKE2B_KEYBYTES;
9244 
9245 
9246 
9247     printf(testingFmt, "wc_InitBlake2b_WithKey()");
9248 
9249     /* Test good arg. */
9250     ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, keylen);
9251     if (ret != 0) {
9252         ret = WOLFSSL_FATAL_ERROR;
9253     }
9254     /* Test bad args. */
9255     if (ret == 0) {
9256         ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
9257         if (ret == BAD_FUNC_ARG) {
9258             ret = 0;
9259         }
9260     }
9261     if (ret == 0) {
9262         ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, 256);
9263         if (ret == BAD_FUNC_ARG) {
9264             ret = 0;
9265         }
9266     }
9267     if (ret == 0) {
9268         ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, NULL, keylen);
9269     }
9270 
9271     printf(resultFmt, ret == 0 ? passed : failed);
9272 
9273 #endif
9274     return ret;
9275 }     /*END wc_InitBlake2b_WithKey*/
9276 
9277 /*
9278  * Unit test for the wc_InitBlake2s_WithKey()
9279  */
9280 static int test_wc_InitBlake2s_WithKey (void)
9281 {
9282     int ret = 0;
9283 #ifdef HAVE_BLAKE2S
9284     Blake2s     blake2s;
9285     word32      digestSz = BLAKE2S_KEYBYTES;
9286     byte        *key = (byte*)"01234567890123456789012345678901";
9287     word32      keylen = BLAKE2S_KEYBYTES;
9288 
9289     printf(testingFmt, "wc_InitBlake2s_WithKey()");
9290 
9291     /* Test good arg. */
9292     ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, keylen);
9293     if (ret != 0) {
9294         ret = WOLFSSL_FATAL_ERROR;
9295     }
9296     /* Test bad args. */
9297     if (ret == 0) {
9298         ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
9299         if (ret == BAD_FUNC_ARG) {
9300             ret = 0;
9301         }
9302     }
9303     if (ret == 0) {
9304         ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, 256);
9305         if (ret == BAD_FUNC_ARG) {
9306             ret = 0;
9307         }
9308     }
9309     if (ret == 0) {
9310         ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, NULL, keylen);
9311     }
9312 
9313     printf(resultFmt, ret == 0 ? passed : failed);
9314 
9315 #endif
9316     return ret;
9317 }     /*END wc_InitBlake2s_WithKey*/
9318 
9319 /*
9320  * Unit test for the wc_InitMd5()
9321  */
9322 static int test_wc_InitMd5 (void)
9323 {
9324     int flag = 0;
9325 #ifndef NO_MD5
9326 
9327     wc_Md5 md5;
9328     int ret;
9329 
9330     printf(testingFmt, "wc_InitMd5()");
9331 
9332     /* Test good arg. */
9333     ret = wc_InitMd5(&md5);
9334     if (ret != 0) {
9335         flag = WOLFSSL_FATAL_ERROR;
9336     }
9337 
9338     /* Test bad arg. */
9339     if (!flag) {
9340         ret = wc_InitMd5(NULL);
9341         if (ret != BAD_FUNC_ARG) {
9342             flag = WOLFSSL_FATAL_ERROR;
9343         }
9344     }
9345 
9346     wc_Md5Free(&md5);
9347 
9348     printf(resultFmt, flag == 0 ? passed : failed);
9349 
9350 #endif
9351     return flag;
9352 }     /* END test_wc_InitMd5 */
9353 
9354 
9355 /*
9356  * Testing wc_UpdateMd5()
9357  */
9358 static int test_wc_Md5Update (void)
9359 {
9360 
9361     int flag = 0;
9362 #ifndef NO_MD5
9363     wc_Md5 md5;
9364     byte hash[WC_MD5_DIGEST_SIZE];
9365     testVector a, b, c;
9366     int ret;
9367 
9368     ret = wc_InitMd5(&md5);
9369     if (ret != 0) {
9370         flag = ret;
9371     }
9372 
9373     printf(testingFmt, "wc_Md5Update()");
9374 
9375     /* Input */
9376     if (!flag) {
9377         a.input = "a";
9378         a.inLen = XSTRLEN(a.input);
9379 
9380         ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
9381         if (ret != 0) {
9382             flag = ret;
9383         }
9384     }
9385 
9386     if (!flag) {
9387         ret = wc_Md5Final(&md5, hash);
9388         if (ret != 0) {
9389             flag = ret;
9390         }
9391     }
9392 
9393     /* Update input. */
9394     if (!flag) {
9395         a.input = "abc";
9396         a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
9397                     "\x72";
9398         a.inLen = XSTRLEN(a.input);
9399         a.outLen = XSTRLEN(a.output);
9400 
9401         ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
9402         if (ret != 0) {
9403             flag = ret;
9404         }
9405     }
9406 
9407     if (!flag) {
9408         ret = wc_Md5Final(&md5, hash);
9409         if (ret != 0) {
9410             flag = ret;
9411         }
9412     }
9413 
9414     if (!flag) {
9415         if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
9416             flag = WOLFSSL_FATAL_ERROR;
9417         }
9418     }
9419 
9420     /*Pass in bad values. */
9421     if (!flag) {
9422         b.input = NULL;
9423         b.inLen = 0;
9424 
9425         ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
9426         if (ret != 0) {
9427             flag = ret;
9428         }
9429     }
9430 
9431     if (!flag) {
9432         c.input = NULL;
9433         c.inLen = WC_MD5_DIGEST_SIZE;
9434 
9435         ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
9436         if (ret != BAD_FUNC_ARG) {
9437             flag = WOLFSSL_FATAL_ERROR;
9438         }
9439     }
9440 
9441     if (!flag) {
9442         ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
9443         if (ret != BAD_FUNC_ARG) {
9444             flag = WOLFSSL_FATAL_ERROR;
9445         }
9446     }
9447 
9448     wc_Md5Free(&md5);
9449 
9450     printf(resultFmt, flag == 0 ? passed : failed);
9451 
9452 #endif
9453     return flag;
9454 } /* END test_wc_Md5Update()  */
9455 
9456 
9457 /*
9458  *  Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
9459  */
9460 static int test_wc_Md5Final (void)
9461 {
9462 
9463     int flag = 0;
9464 #ifndef NO_MD5
9465     /* Instantiate */
9466     wc_Md5 md5;
9467     byte* hash_test[3];
9468     byte hash1[WC_MD5_DIGEST_SIZE];
9469     byte hash2[2*WC_MD5_DIGEST_SIZE];
9470     byte hash3[5*WC_MD5_DIGEST_SIZE];
9471     int times, i, ret;
9472 
9473     /* Initialize */
9474     ret = wc_InitMd5(&md5);
9475     if (ret != 0)  {
9476         flag = ret;
9477     }
9478 
9479     if (!flag) {
9480         hash_test[0] = hash1;
9481         hash_test[1] = hash2;
9482         hash_test[2] = hash3;
9483     }
9484 
9485     times = sizeof(hash_test)/sizeof(byte*);
9486 
9487     /* Test good args. */
9488     printf(testingFmt, "wc_Md5Final()");
9489 
9490     for (i = 0; i < times; i++) {
9491         if (!flag) {
9492             ret = wc_Md5Final(&md5, hash_test[i]);
9493             if (ret != 0) {
9494                 flag = WOLFSSL_FATAL_ERROR;
9495             }
9496         }
9497     }
9498 
9499     /* Test bad args. */
9500     if (!flag) {
9501         ret = wc_Md5Final(NULL, NULL);
9502         if (ret != BAD_FUNC_ARG) {
9503             flag = WOLFSSL_FATAL_ERROR;
9504         }
9505     }
9506 
9507     if (!flag) {
9508         ret = wc_Md5Final(NULL, hash1);
9509         if (ret != BAD_FUNC_ARG) {
9510             flag = WOLFSSL_FATAL_ERROR;
9511         }
9512     }
9513 
9514     if (!flag) {
9515         ret = wc_Md5Final(&md5, NULL);
9516         if (ret != BAD_FUNC_ARG) {
9517             flag = WOLFSSL_FATAL_ERROR;
9518         }
9519     }
9520 
9521     wc_Md5Free(&md5);
9522 
9523     printf(resultFmt, flag == 0 ? passed : failed);
9524 
9525 #endif
9526     return flag;
9527 }
9528 
9529 /*
9530  * Unit test for the wc_InitSha()
9531  */
9532 static int test_wc_InitSha(void)
9533 {
9534     int flag = 0;
9535 #ifndef NO_SHA
9536     wc_Sha sha;
9537     int ret;
9538 
9539     printf(testingFmt, "wc_InitSha()");
9540 
9541     /* Test good arg. */
9542     ret = wc_InitSha(&sha);
9543     if (ret != 0) {
9544         flag = WOLFSSL_FATAL_ERROR;
9545     }
9546 
9547     /* Test bad arg. */
9548     if (!flag) {
9549         ret = wc_InitSha(NULL);
9550         if (ret != BAD_FUNC_ARG) {
9551             flag = WOLFSSL_FATAL_ERROR;
9552         }
9553     }
9554 
9555     wc_ShaFree(&sha);
9556 
9557     printf(resultFmt, flag == 0 ? passed : failed);
9558 #endif
9559     return flag;
9560 
9561 } /* END test_wc_InitSha */
9562 
9563 /*
9564  *  Tesing wc_ShaUpdate()
9565  */
9566 static int test_wc_ShaUpdate (void)
9567 {
9568     int flag = 0;
9569 #ifndef NO_SHA
9570     wc_Sha sha;
9571     byte hash[WC_SHA_DIGEST_SIZE];
9572     testVector a, b, c;
9573     int ret;
9574 
9575     ret = wc_InitSha(&sha);
9576     if (ret != 0) {
9577         flag = ret;
9578     }
9579 
9580     printf(testingFmt, "wc_ShaUpdate()");
9581 
9582     /* Input. */
9583     if (!flag) {
9584         a.input = "a";
9585         a.inLen = XSTRLEN(a.input);
9586 
9587         ret = wc_ShaUpdate(&sha, NULL, 0);
9588         if (ret != 0) {
9589             flag = ret;
9590         }
9591 
9592         ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
9593         if (ret != 0) {
9594             flag = ret;
9595         }
9596 
9597         ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
9598         if (ret != 0) {
9599             flag = ret;
9600         }
9601     }
9602 
9603     if (!flag) {
9604         ret = wc_ShaFinal(&sha, hash);
9605         if (ret != 0) {
9606             flag = ret;
9607         }
9608     }
9609 
9610     /* Update input. */
9611     if (!flag) {
9612         a.input = "abc";
9613         a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
9614                     "\x6C\x9C\xD0\xD8\x9D";
9615         a.inLen = XSTRLEN(a.input);
9616         a.outLen = XSTRLEN(a.output);
9617 
9618         ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
9619         if (ret != 0) {
9620             flag = ret;
9621         }
9622     }
9623 
9624     if (!flag) {
9625         ret = wc_ShaFinal(&sha, hash);
9626         if (ret !=0) {
9627             flag = ret;
9628         }
9629     }
9630 
9631     if (!flag) {
9632         if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
9633             flag = WOLFSSL_FATAL_ERROR;
9634         }
9635     }
9636 
9637     /* Try passing in bad values. */
9638     if (!flag) {
9639         b.input = NULL;
9640         b.inLen = 0;
9641 
9642         ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
9643         if (ret != 0) {
9644             flag = ret;
9645         }
9646     }
9647 
9648     if (!flag) {
9649         c.input = NULL;
9650         c.inLen = WC_SHA_DIGEST_SIZE;
9651 
9652         ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
9653         if (ret != BAD_FUNC_ARG) {
9654             flag = WOLFSSL_FATAL_ERROR;
9655         }
9656     }
9657 
9658     if (!flag) {
9659         ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
9660         if (ret != BAD_FUNC_ARG) {
9661             flag = WOLFSSL_FATAL_ERROR;
9662         }
9663     }
9664 
9665     wc_ShaFree(&sha);
9666     /* If not returned then the unit test passed test vectors. */
9667     printf(resultFmt, flag == 0 ? passed : failed);
9668 
9669 #endif
9670     return flag;
9671 
9672 } /* END test_wc_ShaUpdate() */
9673 
9674 
9675 /*
9676  * Unit test on wc_ShaFinal
9677  */
9678 static int test_wc_ShaFinal (void)
9679 {
9680     int flag = 0;
9681 #ifndef NO_SHA
9682     wc_Sha sha;
9683     byte* hash_test[3];
9684     byte hash1[WC_SHA_DIGEST_SIZE];
9685     byte hash2[2*WC_SHA_DIGEST_SIZE];
9686     byte hash3[5*WC_SHA_DIGEST_SIZE];
9687     int times, i, ret;
9688 
9689     /*Initialize*/
9690     ret = wc_InitSha(&sha);
9691     if (ret) {
9692         flag =  ret;
9693     }
9694 
9695     if (!flag) {
9696         hash_test[0] = hash1;
9697         hash_test[1] = hash2;
9698         hash_test[2] = hash3;
9699     }
9700 
9701     times = sizeof(hash_test)/sizeof(byte*);
9702 
9703     /* Good test args. */
9704     printf(testingFmt, "wc_ShaFinal()");
9705 
9706     for (i = 0; i < times; i++) {
9707         if (!flag) {
9708             ret = wc_ShaFinal(&sha, hash_test[i]);
9709             if (ret != 0) {
9710                 flag = WOLFSSL_FATAL_ERROR;
9711             }
9712         }
9713     }
9714 
9715     /* Test bad args. */
9716     if (!flag) {
9717         ret = wc_ShaFinal(NULL, NULL);
9718         if (ret != BAD_FUNC_ARG) {
9719             flag = WOLFSSL_FATAL_ERROR;
9720         }
9721     }
9722 
9723     if (!flag) {
9724         ret = wc_ShaFinal(NULL, hash1);
9725         if (ret != BAD_FUNC_ARG) {
9726             flag = WOLFSSL_FATAL_ERROR;
9727         }
9728     }
9729 
9730     if (!flag) {
9731         ret = wc_ShaFinal(&sha, NULL);
9732         if (ret != BAD_FUNC_ARG) {
9733             flag = WOLFSSL_FATAL_ERROR;
9734         }
9735     }
9736 
9737     wc_ShaFree(&sha);
9738 
9739     printf(resultFmt, flag == 0 ? passed : failed);
9740 
9741 #endif
9742     return flag;
9743 } /* END test_wc_ShaFinal */
9744 
9745 
9746 /*
9747  * Unit test for wc_InitSha256()
9748  */
9749 static int test_wc_InitSha256 (void)
9750 {
9751     int flag = 0;
9752 #ifndef NO_SHA256
9753     wc_Sha256 sha256;
9754     int ret;
9755 
9756     printf(testingFmt, "wc_InitSha256()");
9757 
9758     /* Test good arg. */
9759     ret = wc_InitSha256(&sha256);
9760     if (ret != 0) {
9761         flag = WOLFSSL_FATAL_ERROR;
9762     }
9763 
9764     /* Test bad arg. */
9765     if (!flag) {
9766         ret = wc_InitSha256(NULL);
9767         if (ret != BAD_FUNC_ARG) {
9768             flag = WOLFSSL_FATAL_ERROR;
9769         }
9770     }
9771 
9772     wc_Sha256Free(&sha256);
9773 
9774     printf(resultFmt, flag == 0 ? passed : failed);
9775 
9776 #endif
9777     return flag;
9778 } /* END test_wc_InitSha256 */
9779 
9780 
9781 /*
9782  * Unit test for wc_Sha256Update()
9783  */
9784 static int test_wc_Sha256Update (void)
9785 {
9786     int flag = 0;
9787 #ifndef NO_SHA256
9788     wc_Sha256 sha256;
9789     byte hash[WC_SHA256_DIGEST_SIZE];
9790     testVector a, b, c;
9791     int ret;
9792 
9793     ret = wc_InitSha256(&sha256);
9794     if (ret != 0) {
9795         flag = ret;
9796     }
9797 
9798     printf(testingFmt, "wc_Sha256Update()");
9799 
9800     /*  Input. */
9801     if (!flag) {
9802         a.input = "a";
9803         a.inLen = XSTRLEN(a.input);
9804 
9805         ret = wc_Sha256Update(&sha256, NULL, 0);
9806         if (ret != 0) {
9807             flag = ret;
9808         }
9809 
9810         ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
9811         if (ret != 0) {
9812             flag = ret;
9813         }
9814 
9815         ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
9816         if (ret != 0) {
9817             flag = ret;
9818         }
9819     }
9820 
9821     if (!flag) {
9822         ret = wc_Sha256Final(&sha256, hash);
9823         if (ret != 0) {
9824             flag = ret;
9825         }
9826     }
9827 
9828     /* Update input. */
9829     if (!flag) {
9830         a.input = "abc";
9831         a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
9832                     "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
9833                     "\x15\xAD";
9834         a.inLen = XSTRLEN(a.input);
9835         a.outLen = XSTRLEN(a.output);
9836 
9837         ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
9838         if (ret != 0) {
9839             flag = ret;
9840         }
9841     }
9842 
9843     if (!flag) {
9844         ret = wc_Sha256Final(&sha256, hash);
9845         if (ret != 0) {
9846             flag = ret;
9847         }
9848     }
9849 
9850     if (!flag) {
9851         if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
9852             flag = WOLFSSL_FATAL_ERROR;
9853         }
9854     }
9855 
9856     /* Try passing in bad values */
9857     if (!flag) {
9858         b.input = NULL;
9859         b.inLen = 0;
9860 
9861         ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
9862         if (ret != 0) {
9863             flag = ret;
9864         }
9865     }
9866 
9867     if (!flag) {
9868         c.input = NULL;
9869         c.inLen = WC_SHA256_DIGEST_SIZE;
9870 
9871         ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
9872         if (ret != BAD_FUNC_ARG) {
9873             flag = WOLFSSL_FATAL_ERROR;
9874         }
9875     }
9876 
9877     if (!flag) {
9878         ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
9879         if (ret != BAD_FUNC_ARG) {
9880             flag = WOLFSSL_FATAL_ERROR;
9881         }
9882     }
9883 
9884     wc_Sha256Free(&sha256);
9885 
9886     /* If not returned then the unit test passed. */
9887     printf(resultFmt, flag == 0 ? passed : failed);
9888 #endif
9889     return flag;
9890 
9891 } /* END test_wc_Sha256Update */
9892 
9893 
9894 /*
9895  * Unit test function for wc_Sha256Final()
9896  */
9897 static int test_wc_Sha256Final (void)
9898 {
9899     int flag = 0;
9900 #ifndef NO_SHA256
9901     wc_Sha256 sha256;
9902     byte* hash_test[3];
9903     byte hash1[WC_SHA256_DIGEST_SIZE];
9904     byte hash2[2*WC_SHA256_DIGEST_SIZE];
9905     byte hash3[5*WC_SHA256_DIGEST_SIZE];
9906     int times, i, ret;
9907 
9908     /* Initialize */
9909     ret = wc_InitSha256(&sha256);
9910     if (ret != 0) {
9911         flag =  ret;
9912     }
9913 
9914     if (!flag) {
9915         hash_test[0] = hash1;
9916         hash_test[1] = hash2;
9917         hash_test[2] = hash3;
9918     }
9919 
9920     times = sizeof(hash_test) / sizeof(byte*);
9921 
9922     /* Good test args. */
9923     printf(testingFmt, "wc_Sha256Final()");
9924 
9925     for (i = 0; i < times; i++) {
9926         if (!flag) {
9927             ret = wc_Sha256Final(&sha256, hash_test[i]);
9928             if (ret != 0) {
9929                 flag = WOLFSSL_FATAL_ERROR;
9930             }
9931         }
9932     }
9933 
9934     /* Test bad args. */
9935     if (!flag ) {
9936         ret = wc_Sha256Final(NULL, NULL);
9937         if (ret != BAD_FUNC_ARG) {
9938             flag = WOLFSSL_FATAL_ERROR;
9939         }
9940     }
9941 
9942     if (!flag) {
9943         ret = wc_Sha256Final(NULL, hash1);
9944         if (ret != BAD_FUNC_ARG) {
9945             flag = WOLFSSL_FATAL_ERROR;
9946         }
9947     }
9948 
9949     if (!flag) {
9950         ret = wc_Sha256Final(&sha256, NULL);
9951         if (ret != BAD_FUNC_ARG) {
9952             flag = WOLFSSL_FATAL_ERROR;
9953         }
9954     }
9955 
9956     wc_Sha256Free(&sha256);
9957 
9958     printf(resultFmt, flag == 0 ? passed : failed);
9959 
9960 #endif
9961     return flag;
9962 
9963 } /* END test_wc_Sha256Final */
9964 /*
9965  * Unit test function for wc_Sha256FinalRaw()
9966  */
9967 static int test_wc_Sha256FinalRaw (void)
9968 {
9969     int flag = 0;
9970 #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
9971     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
9972     !defined(WOLFSSL_NO_HASH_RAW)
9973     wc_Sha256 sha256;
9974     byte* hash_test[3];
9975     byte hash1[WC_SHA256_DIGEST_SIZE];
9976     byte hash2[2*WC_SHA256_DIGEST_SIZE];
9977     byte hash3[5*WC_SHA256_DIGEST_SIZE];
9978     int times, i, ret;
9979 
9980     /* Initialize */
9981     ret = wc_InitSha256(&sha256);
9982     if (ret != 0) {
9983         flag =  ret;
9984     }
9985 
9986     if (!flag) {
9987         hash_test[0] = hash1;
9988         hash_test[1] = hash2;
9989         hash_test[2] = hash3;
9990     }
9991 
9992     times = sizeof(hash_test) / sizeof(byte*);
9993 
9994     /* Good test args. */
9995     printf(testingFmt, "wc_Sha256FinalRaw()");
9996 
9997     for (i = 0; i < times; i++) {
9998         if (!flag) {
9999             ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
10000             if (ret != 0) {
10001                 flag = WOLFSSL_FATAL_ERROR;
10002             }
10003         }
10004     }
10005 
10006     /* Test bad args. */
10007     if (!flag ) {
10008         ret = wc_Sha256FinalRaw(NULL, NULL);
10009         if (ret != BAD_FUNC_ARG) {
10010             flag = WOLFSSL_FATAL_ERROR;
10011         }
10012     }
10013 
10014     if (!flag) {
10015         ret = wc_Sha256FinalRaw(NULL, hash1);
10016         if (ret != BAD_FUNC_ARG) {
10017             flag = WOLFSSL_FATAL_ERROR;
10018         }
10019     }
10020 
10021     if (!flag) {
10022         ret = wc_Sha256FinalRaw(&sha256, NULL);
10023         if (ret != BAD_FUNC_ARG) {
10024             flag = WOLFSSL_FATAL_ERROR;
10025         }
10026     }
10027 
10028     wc_Sha256Free(&sha256);
10029 
10030     printf(resultFmt, flag == 0 ? passed : failed);
10031 #endif
10032     return flag;
10033 
10034 } /* END test_wc_Sha256FinalRaw */
10035 /*
10036  * Unit test function for wc_Sha256GetFlags()
10037  */
10038 static int test_wc_Sha256GetFlags (void)
10039 {
10040     int flag = 0;
10041 #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
10042     wc_Sha256 sha256;
10043     word32 flags = 0;
10044 
10045     printf(testingFmt, "wc_Sha256GetFlags()");
10046 
10047     /* Initialize */
10048     flag = wc_InitSha256(&sha256);
10049     if (flag == 0) {
10050         flag = wc_Sha256GetFlags(&sha256, &flags);
10051     }
10052     if (flag == 0) {
10053         if (flags & WC_HASH_FLAG_ISCOPY) {
10054             flag = 0;
10055         }
10056     }
10057 
10058     wc_Sha256Free(&sha256);
10059 
10060     printf(resultFmt, flag == 0 ? passed : failed);
10061 #endif
10062     return flag;
10063 
10064 } /* END test_wc_Sha256GetFlags */
10065 /*
10066  * Unit test function for wc_Sha256Free()
10067  */
10068 static int test_wc_Sha256Free (void)
10069 {
10070     int flag = 0;
10071 #ifndef NO_SHA256
10072     printf(testingFmt, "wc_Sha256Free()");
10073     wc_Sha256Free(NULL);
10074 
10075     printf(resultFmt, flag == 0 ? passed : failed);
10076 #endif
10077     return flag;
10078 
10079 } /* END test_wc_Sha256Free */
10080 /*
10081  * Unit test function for wc_Sha256GetHash()
10082  */
10083 static int test_wc_Sha256GetHash (void)
10084 {
10085     int flag = 0;
10086 #ifndef NO_SHA256
10087     wc_Sha256 sha256;
10088     byte hash1[WC_SHA256_DIGEST_SIZE];
10089 
10090     printf(testingFmt, "wc_Sha256GetHash()");
10091 
10092     /* Initialize */
10093     flag = wc_InitSha256(&sha256);
10094 
10095     if (flag == 0) {
10096         flag = wc_Sha256GetHash(&sha256, hash1);
10097     }
10098     /*test bad arguements*/
10099     if (flag == 0) {
10100         flag = wc_Sha256GetHash(NULL, NULL);
10101         if (flag == BAD_FUNC_ARG) {
10102             flag = 0;
10103         }
10104     }
10105     if (flag == 0) {
10106         flag = wc_Sha256GetHash(NULL, hash1);
10107         if (flag == BAD_FUNC_ARG) {
10108             flag = 0;
10109         }
10110     }
10111     if (flag == 0) {
10112         flag = wc_Sha256GetHash(&sha256, NULL);
10113         if (flag == BAD_FUNC_ARG) {
10114             flag = 0;
10115         }
10116     }
10117 
10118     wc_Sha256Free(&sha256);
10119 
10120     printf(resultFmt, flag == 0 ? passed : failed);
10121 #endif
10122     return flag;
10123 
10124 } /* END test_wc_Sha256GetHash */
10125 /*
10126  * Unit test function for wc_Sha256Copy()
10127  */
10128 static int test_wc_Sha256Copy (void)
10129 {
10130     int flag = 0;
10131 #ifndef NO_SHA256
10132     wc_Sha256 sha256;
10133     wc_Sha256 temp;
10134 
10135     printf(testingFmt, "wc_Sha256Copy()");
10136 
10137     /* Initialize */
10138     flag = wc_InitSha256(&sha256);
10139     if (flag == 0) {
10140         flag = wc_InitSha256(&temp);
10141     }
10142     if (flag == 0) {
10143         flag = wc_Sha256Copy(&sha256, &temp);
10144     }
10145     /*test bad arguements*/
10146      if (flag == 0) {
10147         flag = wc_Sha256Copy(NULL, NULL);
10148         if (flag == BAD_FUNC_ARG) {
10149             flag = 0;
10150         }
10151      }
10152      if (flag == 0) {
10153         flag = wc_Sha256Copy(NULL, &temp);
10154         if (flag == BAD_FUNC_ARG) {
10155             flag = 0;
10156         }
10157      }
10158      if (flag == 0) {
10159         flag = wc_Sha256Copy(&sha256, NULL);
10160         if (flag == BAD_FUNC_ARG) {
10161             flag = 0;
10162         }
10163     }
10164 
10165     wc_Sha256Free(&sha256);
10166     wc_Sha256Free(&temp);
10167 
10168     printf(resultFmt, flag == 0 ? passed : failed);
10169 #endif
10170     return flag;
10171 
10172 } /* END test_wc_Sha256Copy */
10173 /*
10174  * Testing wc_InitSha512()
10175  */
10176 static int test_wc_InitSha512 (void)
10177 {
10178     int flag = 0;
10179 #ifdef WOLFSSL_SHA512
10180     wc_Sha512 sha512;
10181     int ret;
10182 
10183     printf(testingFmt, "wc_InitSha512()");
10184 
10185     /* Test good arg. */
10186     ret = wc_InitSha512(&sha512);
10187     if (ret != 0) {
10188         flag  = WOLFSSL_FATAL_ERROR;
10189     }
10190 
10191     /* Test bad arg. */
10192     if (!flag) {
10193         ret = wc_InitSha512(NULL);
10194         if (ret != BAD_FUNC_ARG) {
10195             flag = WOLFSSL_FATAL_ERROR;
10196         }
10197     }
10198 
10199     wc_Sha512Free(&sha512);
10200 
10201     printf(resultFmt, flag == 0 ? passed : failed);
10202 
10203 #endif
10204     return flag;
10205 
10206 } /* END test_wc_InitSha512 */
10207 
10208 
10209 /*
10210  *  wc_Sha512Update() test.
10211  */
10212 static int test_wc_Sha512Update (void)
10213 {
10214 
10215     int flag = 0;
10216 #ifdef WOLFSSL_SHA512
10217     wc_Sha512 sha512;
10218     byte hash[WC_SHA512_DIGEST_SIZE];
10219     testVector a, b, c;
10220     int ret;
10221 
10222     ret = wc_InitSha512(&sha512);
10223     if (ret != 0) {
10224         flag = ret;
10225     }
10226 
10227     printf(testingFmt, "wc_Sha512Update()");
10228 
10229     /* Input. */
10230     if (!flag) {
10231         a.input = "a";
10232         a.inLen = XSTRLEN(a.input);
10233 
10234         ret = wc_Sha512Update(&sha512, NULL, 0);
10235         if (ret != 0) {
10236             flag = ret;
10237         }
10238 
10239         ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
10240         if (ret != 0) {
10241             flag = ret;
10242         }
10243 
10244         ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
10245         if (ret != 0) {
10246             flag = ret;
10247         }
10248 
10249         ret = wc_Sha512Final(&sha512, hash);
10250         if (ret != 0) {
10251             flag = ret;
10252         }
10253     }
10254 
10255     /* Update input. */
10256     if (!flag) {
10257         a.input = "abc";
10258         a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
10259                     "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
10260                     "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
10261                     "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
10262                     "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
10263         a.inLen = XSTRLEN(a.input);
10264         a.outLen = XSTRLEN(a.output);
10265 
10266         ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
10267         if (ret != 0) {
10268             flag = ret;
10269         }
10270     }
10271 
10272     if (!flag) {
10273         ret = wc_Sha512Final(&sha512, hash);
10274         if (ret != 0) {
10275             flag = ret;
10276         }
10277     }
10278 
10279     if (!flag) {
10280         if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
10281             flag = WOLFSSL_FATAL_ERROR;
10282         }
10283     }
10284 
10285     /* Try passing in bad values */
10286     if (!flag) {
10287         b.input = NULL;
10288         b.inLen = 0;
10289 
10290         ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
10291         if (ret != 0) {
10292             flag = ret;
10293         }
10294     }
10295 
10296     if (!flag) {
10297         c.input = NULL;
10298         c.inLen = WC_SHA512_DIGEST_SIZE;
10299 
10300         ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
10301         if (ret != BAD_FUNC_ARG) {
10302             flag = WOLFSSL_FATAL_ERROR;
10303         }
10304     }
10305 
10306     if (!flag) {
10307         ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
10308         if (ret != BAD_FUNC_ARG) {
10309             flag = WOLFSSL_FATAL_ERROR;
10310         }
10311     }
10312 
10313     wc_Sha512Free(&sha512);
10314 
10315     /* If not returned then the unit test passed test vectors. */
10316     printf(resultFmt, flag == 0 ? passed : failed);
10317 #endif
10318     return flag;
10319 
10320 } /* END test_wc_Sha512Update  */
10321 
10322 #ifdef WOLFSSL_SHA512
10323 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
10324         (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
10325 /* Perfoms test for
10326  * - wc_Sha512Final/wc_Sha512FinalRaw
10327  * - wc_Sha512_224Final/wc_Sha512_224Final
10328  * - wc_Sha512_256Final/wc_Sha512_256Final
10329  * parameter:
10330  * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
10331  *          WC_HASH_TYPE_SHA512_256
10332  * - isRaw: if is non-zero, xxxFinalRaw function will be tested
10333  *return 0 on success
10334  */
10335 static int test_Sha512_Family_Final(int type, int isRaw)
10336 {
10337     wc_Sha512 sha512;
10338     byte* hash_test[3];
10339     byte hash1[WC_SHA512_DIGEST_SIZE];
10340     byte hash2[2*WC_SHA512_DIGEST_SIZE];
10341     byte hash3[5*WC_SHA512_DIGEST_SIZE];
10342     int times, i, ret;
10343 
10344     int(*initFp)(wc_Sha512*);
10345     int(*finalFp)(wc_Sha512*, byte*);
10346     void(*freeFp)(wc_Sha512*);
10347 
10348     if (type == WC_HASH_TYPE_SHA512) {
10349         initFp  = wc_InitSha512;
10350 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
10351     !defined(WOLFSSL_NO_HASH_RAW)
10352         finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
10353 #else
10354         finalFp = (isRaw)? NULL : wc_Sha512Final;
10355 #endif
10356         freeFp  = wc_Sha512Free;
10357     }
10358 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10359 #if !defined(WOLFSSL_NOSHA512_224)
10360     else if (type == WC_HASH_TYPE_SHA512_224) {
10361         initFp  = wc_InitSha512_224;
10362     #if !defined(WOLFSSL_NO_HASH_RAW)
10363         finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
10364     #else
10365         finalFp = (isRaw)? NULL : wc_Sha512_224Final;
10366     #endif
10367         freeFp  = wc_Sha512_224Free;
10368     }
10369 #endif
10370 #if !defined(WOLFSSL_NOSHA512_256)
10371     else if (type == WC_HASH_TYPE_SHA512_256) {
10372         initFp  = wc_InitSha512_256;
10373     #if !defined(WOLFSSL_NO_HASH_RAW)
10374         finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
10375     #else
10376         finalFp = (isRaw)? NULL : wc_Sha512_256Final;
10377     #endif
10378         freeFp  = wc_Sha512_256Free;
10379     }
10380 #endif
10381 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10382     else
10383         return BAD_FUNC_ARG;
10384 
10385     /* Initialize  */
10386     ret = initFp(&sha512);
10387 
10388     if (!ret) {
10389         hash_test[0] = hash1;
10390         hash_test[1] = hash2;
10391         hash_test[2] = hash3;
10392     }
10393 
10394     times = sizeof(hash_test) / sizeof(byte *);
10395 
10396     /* Good test args. */
10397     for (i = 0; i < times && ret == 0; i++) {
10398         ret = finalFp(&sha512, hash_test[i]);
10399     }
10400     /* Test bad args. */
10401     if (!ret) {
10402         if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
10403             ret = WOLFSSL_FATAL_ERROR;
10404         }
10405     }
10406     if (!ret) {
10407         if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
10408             ret = WOLFSSL_FATAL_ERROR;
10409         }
10410     }
10411 
10412     if (!ret) {
10413         if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
10414             ret = WOLFSSL_FATAL_ERROR;
10415         }
10416     }
10417 
10418     freeFp(&sha512);
10419     return ret;
10420 }
10421 #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
10422                         (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
10423 #endif /* WOLFSSL_SHA512 */
10424 /*
10425  * Unit test function for wc_Sha512Final()
10426  */
10427 static int test_wc_Sha512Final (void)
10428 {
10429     int flag = 0;
10430 #ifdef WOLFSSL_SHA512
10431     wc_Sha512 sha512;
10432     byte* hash_test[3];
10433     byte hash1[WC_SHA512_DIGEST_SIZE];
10434     byte hash2[2*WC_SHA512_DIGEST_SIZE];
10435     byte hash3[5*WC_SHA512_DIGEST_SIZE];
10436     int times, i, ret;
10437 
10438     /* Initialize  */
10439     ret = wc_InitSha512(&sha512);
10440     if (ret != 0) {
10441         flag =  ret;
10442     }
10443 
10444     if (!flag) {
10445         hash_test[0] = hash1;
10446         hash_test[1] = hash2;
10447         hash_test[2] = hash3;
10448     }
10449 
10450     times = sizeof(hash_test) / sizeof(byte *);
10451 
10452     /* Good test args. */
10453     printf(testingFmt, "wc_Sha512Final()");
10454 
10455     for (i = 0; i < times; i++) {
10456         if (!flag) {
10457             ret = wc_Sha512Final(&sha512, hash_test[i]);
10458             if (ret != 0) {
10459                 flag = WOLFSSL_FATAL_ERROR;
10460             }
10461         }
10462     }
10463     /* Test bad args. */
10464     if (!flag) {
10465         ret = wc_Sha512Final(NULL, NULL);
10466         if (ret != BAD_FUNC_ARG) {
10467             flag = WOLFSSL_FATAL_ERROR;
10468         }
10469 
10470     if (!flag) {}
10471         ret = wc_Sha512Final(NULL, hash1);
10472         if (ret != BAD_FUNC_ARG) {
10473             flag = WOLFSSL_FATAL_ERROR;
10474         }
10475     }
10476 
10477     if (!flag) {
10478         ret = wc_Sha512Final(&sha512, NULL);
10479         if (ret != BAD_FUNC_ARG) {
10480             flag = WOLFSSL_FATAL_ERROR;
10481         }
10482     }
10483 
10484     wc_Sha512Free(&sha512);
10485 
10486     printf(resultFmt, flag == 0 ? passed : failed);
10487 #endif
10488     return flag;
10489 } /* END test_wc_Sha512Final */
10490 /*
10491  * Unit test function for wc_Sha512GetFlags()
10492  */
10493 static int test_wc_Sha512GetFlags (void)
10494 {
10495     int flag = 0;
10496 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
10497     wc_Sha512 sha512;
10498     word32 flags = 0;
10499 
10500     printf(testingFmt, "wc_Sha512GetFlags()");
10501 
10502     /* Initialize */
10503     flag = wc_InitSha512(&sha512);
10504     if (flag == 0) {
10505         flag = wc_Sha512GetFlags(&sha512, &flags);
10506     }
10507     if (flag == 0) {
10508         if (flags & WC_HASH_FLAG_ISCOPY) {
10509             flag = 0;
10510         }
10511     }
10512 
10513 
10514     wc_Sha512Free(&sha512);
10515 
10516     printf(resultFmt, flag == 0 ? passed : failed);
10517 #endif
10518     return flag;
10519 
10520 } /* END test_wc_Sha512GetFlags */
10521 /*
10522  * Unit test function for wc_Sha512FinalRaw()
10523  */
10524 static int test_wc_Sha512FinalRaw (void)
10525 {
10526     int flag = 0;
10527 #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
10528     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
10529     !defined(WOLFSSL_NO_HASH_RAW)
10530     wc_Sha512 sha512;
10531     byte* hash_test[3];
10532     byte hash1[WC_SHA512_DIGEST_SIZE];
10533     byte hash2[2*WC_SHA512_DIGEST_SIZE];
10534     byte hash3[5*WC_SHA512_DIGEST_SIZE];
10535     int times, i, ret;
10536 
10537     /* Initialize */
10538     ret = wc_InitSha512(&sha512);
10539     if (ret != 0) {
10540         flag =  ret;
10541     }
10542 
10543     if (!flag) {
10544         hash_test[0] = hash1;
10545         hash_test[1] = hash2;
10546         hash_test[2] = hash3;
10547     }
10548 
10549     times = sizeof(hash_test) / sizeof(byte*);
10550 
10551     /* Good test args. */
10552     printf(testingFmt, "wc_Sha512FinalRaw()");
10553 
10554     for (i = 0; i < times; i++) {
10555         if (!flag) {
10556             ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
10557             if (ret != 0) {
10558                 flag = WOLFSSL_FATAL_ERROR;
10559             }
10560         }
10561     }
10562 
10563     /* Test bad args. */
10564     if (!flag ) {
10565         ret = wc_Sha512FinalRaw(NULL, NULL);
10566         if (ret != BAD_FUNC_ARG) {
10567             flag = WOLFSSL_FATAL_ERROR;
10568         }
10569     }
10570 
10571     if (!flag) {
10572         ret = wc_Sha512FinalRaw(NULL, hash1);
10573         if (ret != BAD_FUNC_ARG) {
10574             flag = WOLFSSL_FATAL_ERROR;
10575         }
10576     }
10577 
10578     if (!flag) {
10579         ret = wc_Sha512FinalRaw(&sha512, NULL);
10580         if (ret != BAD_FUNC_ARG) {
10581             flag = WOLFSSL_FATAL_ERROR;
10582         }
10583     }
10584 
10585     wc_Sha512Free(&sha512);
10586 
10587     printf(resultFmt, flag == 0 ? passed : failed);
10588 #endif
10589     return flag;
10590 
10591 } /* END test_wc_Sha512FinalRaw */
10592 
10593 /*
10594  * Unit test function for wc_Sha512Free()
10595  */
10596 static int test_wc_Sha512Free (void)
10597 {
10598     int flag = 0;
10599 #ifdef WOLFSSL_SHA512
10600     printf(testingFmt, "wc_Sha512Free()");
10601     wc_Sha512Free(NULL);
10602 
10603     printf(resultFmt, flag == 0 ? passed : failed);
10604 #endif
10605     return flag;
10606 
10607 } /* END test_wc_Sha512Free */
10608 #ifdef WOLFSSL_SHA512
10609 
10610 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
10611         (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
10612 static int test_Sha512_Family_GetHash(int type )
10613 {
10614     int flag = 0;
10615     int(*initFp)(wc_Sha512*);
10616     int(*ghashFp)(wc_Sha512*, byte*);
10617     wc_Sha512 sha512;
10618     byte hash1[WC_SHA512_DIGEST_SIZE];
10619 
10620     if (type == WC_HASH_TYPE_SHA512) {
10621         initFp  = wc_InitSha512;
10622         ghashFp = wc_Sha512GetHash;
10623     }
10624 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10625 #if !defined(WOLFSSL_NOSHA512_224)
10626     else if (type == WC_HASH_TYPE_SHA512_224) {
10627         initFp  = wc_InitSha512_224;
10628         ghashFp = wc_Sha512_224GetHash;
10629     }
10630 #endif
10631 #if !defined(WOLFSSL_NOSHA512_256)
10632     else if (type == WC_HASH_TYPE_SHA512_256) {
10633         initFp  = wc_InitSha512_256;
10634         ghashFp = wc_Sha512_256GetHash;
10635     }
10636 #endif
10637 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10638     else {
10639         initFp  = NULL;
10640         ghashFp = NULL;
10641     }
10642 
10643     if (initFp == NULL || ghashFp == NULL)
10644         return WOLFSSL_FATAL_ERROR;
10645 
10646     if (!flag) {
10647         flag = initFp(&sha512);
10648     }
10649 
10650     if (!flag) {
10651         flag = ghashFp(&sha512, hash1);
10652     }
10653 
10654     /*test bad arguements*/
10655      if (!flag) {
10656         if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
10657             flag = WOLFSSL_FATAL_ERROR;
10658      }
10659 
10660      if (!flag) {
10661         if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
10662             flag = WOLFSSL_FATAL_ERROR;
10663      }
10664 
10665      if (!flag) {
10666         if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
10667             flag = WOLFSSL_FATAL_ERROR;
10668     }
10669 
10670     wc_Sha512Free(&sha512);
10671     return flag;
10672 }
10673 #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
10674                         (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
10675 #endif /* WOLFSSL_SHA512 */
10676 /*
10677  * Unit test function for wc_Sha512GetHash()
10678  */
10679 static int test_wc_Sha512GetHash (void)
10680 {
10681     int flag = 0;
10682 #ifdef WOLFSSL_SHA512
10683     wc_Sha512 sha512;
10684     byte hash1[WC_SHA512_DIGEST_SIZE];
10685 
10686     printf(testingFmt, "wc_Sha512GetHash()");
10687 
10688     /* Initialize */
10689     flag = wc_InitSha512(&sha512);
10690 
10691     if (flag == 0) {
10692         flag = wc_Sha512GetHash(&sha512, hash1);
10693     }
10694     /*test bad arguements*/
10695      if (flag == 0) {
10696         flag = wc_Sha512GetHash(NULL, NULL);
10697         if (flag == BAD_FUNC_ARG) {
10698             flag = 0;
10699         }
10700      }
10701      if (flag == 0) {
10702         flag = wc_Sha512GetHash(NULL, hash1);
10703         if (flag == BAD_FUNC_ARG) {
10704             flag = 0;
10705         }
10706      }
10707      if (flag == 0) {
10708         flag = wc_Sha512GetHash(&sha512, NULL);
10709         if (flag == BAD_FUNC_ARG) {
10710             flag = 0;
10711         }
10712     }
10713 
10714     wc_Sha512Free(&sha512);
10715 
10716     printf(resultFmt, flag == 0 ? passed : failed);
10717 #endif
10718     return flag;
10719 
10720 } /* END test_wc_Sha512GetHash */
10721 
10722 /*
10723  * Unit test function for wc_Sha512Copy()
10724  */
10725 static int test_wc_Sha512Copy (void)
10726 {
10727     int flag = 0;
10728 #ifdef WOLFSSL_SHA512
10729     wc_Sha512 sha512;
10730     wc_Sha512 temp;
10731 
10732     printf(testingFmt, "wc_Sha512Copy()");
10733 
10734     /* Initialize */
10735     flag = wc_InitSha512(&sha512);
10736     if (flag == 0) {
10737         flag = wc_InitSha512(&temp);
10738     }
10739     if (flag == 0) {
10740         flag = wc_Sha512Copy(&sha512, &temp);
10741     }
10742     /*test bad arguements*/
10743      if (flag == 0) {
10744         flag = wc_Sha512Copy(NULL, NULL);
10745         if (flag == BAD_FUNC_ARG) {
10746             flag = 0;
10747         }
10748      }
10749      if (flag == 0) {
10750         flag = wc_Sha512Copy(NULL, &temp);
10751         if (flag == BAD_FUNC_ARG) {
10752             flag = 0;
10753         }
10754      }
10755      if (flag == 0) {
10756         flag = wc_Sha512Copy(&sha512, NULL);
10757         if (flag == BAD_FUNC_ARG) {
10758             flag = 0;
10759         }
10760     }
10761 
10762     wc_Sha512Free(&sha512);
10763     wc_Sha512Free(&temp);
10764 
10765     printf(resultFmt, flag == 0 ? passed : failed);
10766 #endif
10767     return flag;
10768 
10769 } /* END test_wc_Sha512Copy */
10770 
10771 static int test_wc_InitSha512_224 (void)
10772 {
10773     int flag = 0;
10774 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10775 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
10776     wc_Sha512 sha512;
10777     int ret;
10778 
10779     printf(testingFmt, "wc_InitSha512_224()");
10780 
10781     /* Test good arg. */
10782     ret = wc_InitSha512_224(&sha512);
10783     if (ret != 0) {
10784         flag  = WOLFSSL_FATAL_ERROR;
10785     }
10786 
10787     /* Test bad arg. */
10788     if (!flag) {
10789         ret = wc_InitSha512_224(NULL);
10790         if (ret != BAD_FUNC_ARG) {
10791             flag = WOLFSSL_FATAL_ERROR;
10792         }
10793     }
10794 
10795     wc_Sha512_224Free(&sha512);
10796 
10797     printf(resultFmt, flag == 0 ? passed : failed);
10798 
10799 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
10800 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10801     return flag;
10802 
10803 }
10804 
10805 static int test_wc_Sha512_224Update (void)
10806 {
10807     int flag = 0;
10808 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10809 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
10810     wc_Sha512 sha512;
10811     byte hash[WC_SHA512_DIGEST_SIZE];
10812     testVector a, c;
10813     int ret;
10814 
10815     ret = wc_InitSha512_224(&sha512);
10816     if (ret != 0) {
10817         flag = ret;
10818     }
10819 
10820     printf(testingFmt, "wc_Sha512_224Update()");
10821 
10822     /* Input. */
10823     if (!flag) {
10824         a.input = "a";
10825         a.inLen = XSTRLEN(a.input);
10826 
10827         ret = wc_Sha512_224Update(&sha512, NULL, 0);
10828         if (ret != 0) {
10829             flag = ret;
10830         }
10831 
10832         ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
10833         if (ret != 0) {
10834             flag = ret;
10835         }
10836 
10837         ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
10838         if (ret != 0) {
10839             flag = ret;
10840         }
10841 
10842         ret = wc_Sha512_224Final(&sha512, hash);
10843         if (ret != 0) {
10844             flag = ret;
10845         }
10846     }
10847 
10848     /* Update input. */
10849     if (!flag) {
10850         a.input = "abc";
10851         a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
10852                    "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
10853 
10854         a.inLen = XSTRLEN(a.input);
10855         a.outLen = XSTRLEN(a.output);
10856 
10857         ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
10858         if (ret != 0) {
10859             flag = ret;
10860         }
10861     }
10862 
10863     if (!flag) {
10864         ret = wc_Sha512_224Final(&sha512, hash);
10865         if (ret != 0) {
10866             flag = ret;
10867         }
10868     }
10869 
10870     if (!flag) {
10871         if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
10872             flag = WOLFSSL_FATAL_ERROR;
10873         }
10874     }
10875 
10876     if (!flag) {
10877         c.input = NULL;
10878         c.inLen = WC_SHA512_224_DIGEST_SIZE;
10879 
10880         ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
10881         if (ret != BAD_FUNC_ARG) {
10882             flag = WOLFSSL_FATAL_ERROR;
10883         }
10884     }
10885 
10886     if (!flag) {
10887         ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
10888         if (ret != BAD_FUNC_ARG) {
10889             flag = WOLFSSL_FATAL_ERROR;
10890         }
10891     }
10892 
10893     wc_Sha512_224Free(&sha512);
10894 
10895     /* If not returned then the unit test passed test vectors. */
10896     printf(resultFmt, flag == 0 ? passed : failed);
10897 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
10898 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10899     return flag;
10900 
10901 }
10902 
10903 static int test_wc_Sha512_224Final (void)
10904 {
10905     int flag = 0;
10906 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10907 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
10908     printf(testingFmt, "wc_Sha512_224Final()");
10909     flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
10910     printf(resultFmt, flag == 0 ? passed : failed);
10911 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
10912 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10913     return flag;
10914 }
10915 
10916 static int test_wc_Sha512_224GetFlags (void)
10917 {
10918     int flag = 0;
10919 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10920 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
10921     wc_Sha512 sha512, copy;
10922     word32 flags = 0;
10923 
10924     printf(testingFmt, "wc_Sha512_224GetFlags()");
10925 
10926     /* Initialize */
10927     flag = wc_InitSha512_224(&sha512);
10928     if (!flag) {
10929         flag = wc_InitSha512_224(&copy);
10930     }
10931     if (!flag) {
10932         flag = wc_Sha512_224Copy(&sha512, &copy);
10933     }
10934     if (!flag) {
10935         flag = wc_Sha512_224GetFlags(&copy, &flags);
10936     }
10937     if (!flag) {
10938         if (flags & WC_HASH_FLAG_ISCOPY)
10939             flag = 0;
10940         else
10941             flag = WOLFSSL_FATAL_ERROR;
10942     }
10943     wc_Sha512_224Free(&copy);
10944     wc_Sha512_224Free(&sha512);
10945 
10946     printf(resultFmt, flag == 0 ? passed : failed);
10947 #endif
10948 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10949     return flag;
10950 
10951 }
10952 
10953 static int test_wc_Sha512_224FinalRaw (void)
10954 {
10955     int flag = 0;
10956 
10957 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
10958     defined(WOLFSSL_SHA512) &&  !defined(WOLFSSL_NOSHA512_224) && \
10959     !defined(WOLFSSL_NO_HASH_RAW)
10960     printf(testingFmt, "wc_Sha512_224FinalRaw()");
10961     flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
10962     printf(resultFmt, flag == 0 ? passed : failed);
10963 #endif
10964     return flag;
10965 
10966 }
10967 
10968 static int test_wc_Sha512_224Free (void)
10969 {
10970     int flag = 0;
10971 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10972 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
10973     printf(testingFmt, "wc_Sha512_224Free()");
10974     wc_Sha512_224Free(NULL);
10975 
10976     printf(resultFmt, passed);
10977 #endif
10978 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10979     return flag;
10980 
10981 }
10982 
10983 static int test_wc_Sha512_224GetHash (void)
10984 {
10985     int flag = 0;
10986 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
10987 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
10988     printf(testingFmt, "wc_Sha512_224GetHash()");
10989     flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
10990     printf(resultFmt, flag == 0 ? passed : failed);
10991 #endif
10992 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
10993     return flag;
10994 
10995 }
10996 static int test_wc_Sha512_224Copy (void)
10997 {
10998     int flag = 0;
10999 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11000 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
11001     wc_Sha512 sha512;
11002     wc_Sha512 temp;
11003 
11004     printf(testingFmt, "wc_Sha512_224Copy()");
11005 
11006     /* Initialize */
11007     flag = wc_InitSha512_224(&sha512);
11008     if (flag == 0) {
11009         flag = wc_InitSha512_224(&temp);
11010     }
11011     if (flag == 0) {
11012         flag = wc_Sha512_224Copy(&sha512, &temp);
11013     }
11014     /*test bad arguements*/
11015     if (flag == 0) {
11016         if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
11017             flag = WOLFSSL_FATAL_ERROR;
11018     }
11019     if (flag == 0) {
11020         if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
11021             flag = WOLFSSL_FATAL_ERROR;
11022     }
11023     if (flag == 0) {
11024         if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
11025             flag = WOLFSSL_FATAL_ERROR;
11026     }
11027 
11028     wc_Sha512_224Free(&sha512);
11029     wc_Sha512_224Free(&temp);
11030 
11031     printf(resultFmt, flag == 0 ? passed : failed);
11032 #endif
11033 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11034     return flag;
11035 
11036 }
11037 
11038 static int test_wc_InitSha512_256 (void)
11039 {
11040     int flag = 0;
11041 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11042 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11043     wc_Sha512 sha512;
11044     int ret;
11045 
11046     printf(testingFmt, "wc_InitSha512_256()");
11047 
11048     /* Test good arg. */
11049     ret = wc_InitSha512_256(&sha512);
11050     if (ret != 0) {
11051         flag  = WOLFSSL_FATAL_ERROR;
11052     }
11053 
11054     /* Test bad arg. */
11055     if (!flag) {
11056         ret = wc_InitSha512_256(NULL);
11057         if (ret != BAD_FUNC_ARG) {
11058             flag = WOLFSSL_FATAL_ERROR;
11059         }
11060     }
11061 
11062     wc_Sha512_256Free(&sha512);
11063 
11064     printf(resultFmt, flag == 0 ? passed : failed);
11065 
11066 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
11067 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11068     return flag;
11069 
11070 }
11071 
11072 static int test_wc_Sha512_256Update (void)
11073 {
11074     int flag = 0;
11075 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11076 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11077     wc_Sha512 sha512;
11078     byte hash[WC_SHA512_DIGEST_SIZE];
11079     testVector a, c;
11080     int ret;
11081 
11082     ret = wc_InitSha512_256(&sha512);
11083     if (ret != 0) {
11084         flag = ret;
11085     }
11086 
11087     printf(testingFmt, "wc_Sha512_256Update()");
11088 
11089     /* Input. */
11090     if (!flag) {
11091         a.input = "a";
11092         a.inLen = XSTRLEN(a.input);
11093 
11094         ret = wc_Sha512_256Update(&sha512, NULL, 0);
11095         if (ret != 0) {
11096             flag = ret;
11097         }
11098 
11099         ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
11100         if (ret != 0) {
11101             flag = ret;
11102         }
11103 
11104         ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
11105         if (ret != 0) {
11106             flag = ret;
11107         }
11108 
11109         ret = wc_Sha512_256Final(&sha512, hash);
11110         if (ret != 0) {
11111             flag = ret;
11112         }
11113     }
11114 
11115     /* Update input. */
11116     if (!flag) {
11117         a.input = "abc";
11118         a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
11119                    "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
11120                    "\x07\xe7\xaf\x23";
11121 
11122         a.inLen = XSTRLEN(a.input);
11123         a.outLen = XSTRLEN(a.output);
11124 
11125         ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
11126         if (ret != 0) {
11127             flag = ret;
11128         }
11129     }
11130 
11131     if (!flag) {
11132         ret = wc_Sha512_256Final(&sha512, hash);
11133         if (ret != 0) {
11134             flag = ret;
11135         }
11136     }
11137 
11138     if (!flag) {
11139         if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
11140             flag = WOLFSSL_FATAL_ERROR;
11141         }
11142     }
11143 
11144     if (!flag) {
11145         c.input = NULL;
11146         c.inLen = WC_SHA512_256_DIGEST_SIZE;
11147 
11148         ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
11149         if (ret != BAD_FUNC_ARG) {
11150             flag = WOLFSSL_FATAL_ERROR;
11151         }
11152     }
11153 
11154     if (!flag) {
11155         ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
11156         if (ret != BAD_FUNC_ARG) {
11157             flag = WOLFSSL_FATAL_ERROR;
11158         }
11159     }
11160 
11161     wc_Sha512_256Free(&sha512);
11162 
11163     /* If not returned then the unit test passed test vectors. */
11164     printf(resultFmt, flag == 0 ? passed : failed);
11165 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
11166 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11167     return flag;
11168 
11169 }
11170 
11171 static int test_wc_Sha512_256Final (void)
11172 {
11173     int flag = 0;
11174 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11175 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11176     printf(testingFmt, "wc_Sha512_256Final()");
11177     flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
11178     printf(resultFmt, flag == 0 ? passed : failed);
11179 #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
11180 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11181     return flag;
11182 }
11183 
11184 static int test_wc_Sha512_256GetFlags (void)
11185 {
11186     int flag = 0;
11187 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11188 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
11189     wc_Sha512 sha512, copy;
11190     word32 flags = 0;
11191 
11192     printf(testingFmt, "wc_Sha512_256GetFlags()");
11193 
11194     /* Initialize */
11195     flag = wc_InitSha512_256(&sha512);
11196     if (!flag ) {
11197         flag = wc_InitSha512_256(&copy);
11198     }
11199     if (!flag ) {
11200         flag = wc_Sha512_256Copy(&sha512, &copy);
11201     }
11202     if (!flag ) {
11203         flag = wc_Sha512_256GetFlags(&copy, &flags);
11204     }
11205     if (!flag) {
11206         if (flags & WC_HASH_FLAG_ISCOPY)
11207             flag = 0;
11208         else
11209             flag = WOLFSSL_FATAL_ERROR;
11210     }
11211 
11212     wc_Sha512_256Free(&sha512);
11213 
11214     printf(resultFmt, flag == 0 ? passed : failed);
11215 #endif
11216 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11217     return flag;
11218 
11219 }
11220 
11221 static int test_wc_Sha512_256FinalRaw (void)
11222 {
11223     int flag = 0;
11224 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
11225     defined(WOLFSSL_SHA512) &&  !defined(WOLFSSL_NOSHA512_256) && \
11226     !defined(WOLFSSL_NO_HASH_RAW)
11227     printf(testingFmt, "wc_Sha512_256FinalRaw()");
11228     flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
11229     printf(resultFmt, flag == 0 ? passed : failed);
11230 #endif
11231     return flag;
11232 
11233 }
11234 
11235 static int test_wc_Sha512_256Free (void)
11236 {
11237     int flag = 0;
11238 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11239 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11240     printf(testingFmt, "wc_Sha512_256Free()");
11241     wc_Sha512_256Free(NULL);
11242 
11243     printf(resultFmt, passed);
11244 #endif
11245 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11246     return flag;
11247 
11248 }
11249 
11250 static int test_wc_Sha512_256GetHash (void)
11251 {
11252     int flag = 0;
11253 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11254 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11255     printf(testingFmt, "wc_Sha512_256GetHash()");
11256     flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
11257     printf(resultFmt, flag == 0 ? passed : failed);
11258 #endif
11259 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11260     return flag;
11261 
11262 }
11263 static int test_wc_Sha512_256Copy (void)
11264 {
11265     int flag = 0;
11266 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
11267 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
11268     wc_Sha512 sha512;
11269     wc_Sha512 temp;
11270 
11271     printf(testingFmt, "wc_Sha512_256Copy()");
11272 
11273     /* Initialize */
11274     flag = wc_InitSha512_256(&sha512);
11275     if (flag == 0) {
11276         flag = wc_InitSha512_256(&temp);
11277     }
11278     if (flag == 0) {
11279         flag = wc_Sha512_256Copy(&sha512, &temp);
11280     }
11281     /*test bad arguements*/
11282      if (flag == 0) {
11283         if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
11284             flag = WOLFSSL_FATAL_ERROR;
11285      }
11286      if (flag == 0) {
11287         if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
11288             flag = WOLFSSL_FATAL_ERROR;
11289      }
11290      if (flag == 0) {
11291         if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
11292             flag = WOLFSSL_FATAL_ERROR;
11293     }
11294 
11295     wc_Sha512_256Free(&sha512);
11296     wc_Sha512_256Free(&temp);
11297 
11298     printf(resultFmt, flag == 0 ? passed : failed);
11299 #endif
11300 #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
11301     return flag;
11302 
11303 }
11304 
11305 
11306 
11307 /*
11308  * Testing wc_InitSha384()
11309  */
11310 static int test_wc_InitSha384 (void)
11311 {
11312     int flag = 0;
11313 #ifdef WOLFSSL_SHA384
11314     wc_Sha384 sha384;
11315     int ret;
11316 
11317     printf(testingFmt, "wc_InitSha384()");
11318 
11319     /* Test good arg. */
11320     ret = wc_InitSha384(&sha384);
11321     if (ret != 0) {
11322         flag = WOLFSSL_FATAL_ERROR;
11323     }
11324 
11325     /* Test bad arg. */
11326     if (!flag) {
11327         ret = wc_InitSha384(NULL);
11328         if (ret != BAD_FUNC_ARG) {
11329             flag = WOLFSSL_FATAL_ERROR;
11330         }
11331     }
11332 
11333     wc_Sha384Free(&sha384);
11334 
11335     printf(resultFmt, flag == 0 ? passed : failed);
11336 #endif
11337     return flag;
11338 } /* END test_wc_InitSha384 */
11339 
11340 
11341 /*
11342  * test wc_Sha384Update()
11343  */
11344 static int test_wc_Sha384Update (void)
11345 {
11346 
11347     int flag = 0;
11348 #ifdef WOLFSSL_SHA384
11349     wc_Sha384 sha384;
11350     byte hash[WC_SHA384_DIGEST_SIZE];
11351     testVector a, b, c;
11352     int ret;
11353 
11354     ret = wc_InitSha384(&sha384);
11355     if (ret != 0) {
11356         flag = ret;
11357     }
11358 
11359     printf(testingFmt, "wc_Sha384Update()");
11360 
11361     /* Input */
11362     if (!flag) {
11363         a.input = "a";
11364         a.inLen = XSTRLEN(a.input);
11365 
11366         ret = wc_Sha384Update(&sha384, NULL, 0);
11367         if (ret != 0) {
11368             flag = ret;
11369         }
11370 
11371         ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
11372         if (ret != 0) {
11373             flag = ret;
11374         }
11375 
11376         ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
11377         if (ret != 0) {
11378             flag = ret;
11379         }
11380     }
11381 
11382     if (!flag) {
11383         ret = wc_Sha384Final(&sha384, hash);
11384         if (ret != 0) {
11385             flag = ret;
11386         }
11387     }
11388 
11389     /* Update input. */
11390     if (!flag) {
11391         a.input = "abc";
11392         a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
11393                    "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
11394                    "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
11395                    "\xc8\x25\xa7";
11396         a.inLen = XSTRLEN(a.input);
11397         a.outLen = XSTRLEN(a.output);
11398 
11399         ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
11400         if (ret != 0) {
11401             flag = ret;
11402         }
11403     }
11404 
11405     if (!flag) {
11406         ret = wc_Sha384Final(&sha384, hash);
11407         if (ret != 0) {
11408             flag = ret;
11409         }
11410     }
11411 
11412     if (!flag) {
11413         if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
11414             flag = WOLFSSL_FATAL_ERROR;
11415         }
11416     }
11417 
11418     /* Pass in bad values. */
11419     if (!flag) {
11420         b.input = NULL;
11421         b.inLen = 0;
11422 
11423         ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
11424         if (ret != 0) {
11425             flag = ret;
11426         }
11427     }
11428 
11429     if (!flag) {
11430         c.input = NULL;
11431         c.inLen = WC_SHA384_DIGEST_SIZE;
11432 
11433         ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
11434         if (ret != BAD_FUNC_ARG) {
11435             flag = WOLFSSL_FATAL_ERROR;
11436         }
11437     }
11438 
11439     if (!flag) {
11440         ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
11441         if (ret != BAD_FUNC_ARG) {
11442             flag = WOLFSSL_FATAL_ERROR;
11443         }
11444     }
11445 
11446     wc_Sha384Free(&sha384);
11447 
11448     /* If not returned then the unit test passed test vectors. */
11449     printf(resultFmt, flag == 0 ? passed : failed);
11450 
11451 #endif
11452     return flag;
11453 } /* END test_wc_Sha384Update */
11454 
11455 /*
11456  * Unit test function for wc_Sha384Final();
11457  */
11458 static int test_wc_Sha384Final (void)
11459 {
11460     int flag = 0;
11461 #ifdef WOLFSSL_SHA384
11462     wc_Sha384 sha384;
11463     byte* hash_test[3];
11464     byte hash1[WC_SHA384_DIGEST_SIZE];
11465     byte hash2[2*WC_SHA384_DIGEST_SIZE];
11466     byte hash3[5*WC_SHA384_DIGEST_SIZE];
11467     int times, i, ret;
11468 
11469     /* Initialize */
11470     ret = wc_InitSha384(&sha384);
11471     if (ret) {
11472         flag = ret;
11473     }
11474 
11475     if (!flag) {
11476         hash_test[0] = hash1;
11477         hash_test[1] = hash2;
11478         hash_test[2] = hash3;
11479     }
11480 
11481     times = sizeof(hash_test) / sizeof(byte*);
11482 
11483     /* Good test args. */
11484     printf(testingFmt, "wc_Sha384Final()");
11485 
11486     for (i = 0; i < times; i++) {
11487         if (!flag) {
11488             ret = wc_Sha384Final(&sha384, hash_test[i]);
11489             if (ret != 0) {
11490                 flag = WOLFSSL_FATAL_ERROR;
11491             }
11492         }
11493     }
11494 
11495     /* Test bad args. */
11496     if (!flag) {
11497         ret = wc_Sha384Final(NULL, NULL);
11498         if (ret != BAD_FUNC_ARG) {
11499             flag = WOLFSSL_FATAL_ERROR;
11500         }
11501     }
11502 
11503     if (!flag) {
11504         ret = wc_Sha384Final(NULL, hash1);
11505         if (ret != BAD_FUNC_ARG) {
11506             flag = WOLFSSL_FATAL_ERROR;
11507         }
11508     }
11509 
11510     if (!flag) {
11511         ret = wc_Sha384Final(&sha384, NULL);
11512         if (ret != BAD_FUNC_ARG) {
11513             flag = WOLFSSL_FATAL_ERROR;
11514         }
11515     }
11516 
11517     wc_Sha384Free(&sha384);
11518 
11519     printf(resultFmt, flag == 0 ? passed : failed);
11520 
11521 #endif
11522     return flag;
11523 
11524 } /* END test_wc_Sha384Final */
11525 /*
11526  * Unit test function for wc_Sha384GetFlags()
11527  */
11528 static int test_wc_Sha384GetFlags (void)
11529 {
11530     int flag = 0;
11531 #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
11532     wc_Sha384 sha384;
11533     word32 flags = 0;
11534 
11535     printf(testingFmt, "wc_Sha384GetFlags()");
11536 
11537     /* Initialize */
11538     flag = wc_InitSha384(&sha384);
11539     if (flag == 0) {
11540         flag = wc_Sha384GetFlags(&sha384, &flags);
11541     }
11542     if (flag == 0) {
11543         if (flags & WC_HASH_FLAG_ISCOPY) {
11544             flag = 0;
11545         }
11546     }
11547 
11548 
11549     wc_Sha384Free(&sha384);
11550 
11551     printf(resultFmt, flag == 0 ? passed : failed);
11552 
11553 #endif
11554     return flag;
11555 
11556 } /* END test_wc_Sha384GetFlags */
11557 /*
11558  * Unit test function for wc_Sha384FinalRaw()
11559  */
11560 static int test_wc_Sha384FinalRaw (void)
11561 {
11562     int flag = 0;
11563 #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
11564     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
11565     !defined(WOLFSSL_NO_HASH_RAW)
11566     wc_Sha384 sha384;
11567     byte* hash_test[3];
11568     byte hash1[WC_SHA384_DIGEST_SIZE];
11569     byte hash2[2*WC_SHA384_DIGEST_SIZE];
11570     byte hash3[5*WC_SHA384_DIGEST_SIZE];
11571     int times, i, ret;
11572 
11573     /* Initialize */
11574     ret = wc_InitSha384(&sha384);
11575     if (ret != 0) {
11576         flag =  ret;
11577     }
11578 
11579     if (!flag) {
11580         hash_test[0] = hash1;
11581         hash_test[1] = hash2;
11582         hash_test[2] = hash3;
11583     }
11584 
11585     times = sizeof(hash_test) / sizeof(byte*);
11586 
11587     /* Good test args. */
11588     printf(testingFmt, "wc_Sha384FinalRaw()");
11589 
11590     for (i = 0; i < times; i++) {
11591         if (!flag) {
11592             ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
11593             if (ret != 0) {
11594                 flag = WOLFSSL_FATAL_ERROR;
11595             }
11596         }
11597     }
11598 
11599     /* Test bad args. */
11600     if (!flag ) {
11601         ret = wc_Sha384FinalRaw(NULL, NULL);
11602         if (ret != BAD_FUNC_ARG) {
11603             flag = WOLFSSL_FATAL_ERROR;
11604         }
11605     }
11606 
11607     if (!flag) {
11608         ret = wc_Sha384FinalRaw(NULL, hash1);
11609         if (ret != BAD_FUNC_ARG) {
11610             flag = WOLFSSL_FATAL_ERROR;
11611         }
11612     }
11613 
11614     if (!flag) {
11615         ret = wc_Sha384FinalRaw(&sha384, NULL);
11616         if (ret != BAD_FUNC_ARG) {
11617             flag = WOLFSSL_FATAL_ERROR;
11618         }
11619     }
11620 
11621     wc_Sha384Free(&sha384);
11622 
11623     printf(resultFmt, flag == 0 ? passed : failed);
11624 #endif
11625     return flag;
11626 
11627 } /* END test_wc_Sha384FinalRaw */
11628 /*
11629  * Unit test function for wc_Sha384Free()
11630  */
11631 static int test_wc_Sha384Free (void)
11632 {
11633     int flag = 0;
11634 #ifdef WOLFSSL_SHA384
11635     printf(testingFmt, "wc_Sha384Free()");
11636     wc_Sha384Free(NULL);
11637 
11638 
11639     printf(resultFmt, flag == 0 ? passed : failed);
11640 #endif
11641     return flag;
11642 
11643 } /* END test_wc_Sha384Free */
11644 /*
11645  * Unit test function for wc_Sha384GetHash()
11646  */
11647 static int test_wc_Sha384GetHash (void)
11648 {
11649     int flag = 0;
11650 #ifdef WOLFSSL_SHA384
11651     wc_Sha384 sha384;
11652     byte hash1[WC_SHA384_DIGEST_SIZE];
11653 
11654     printf(testingFmt, "wc_Sha384GetHash()");
11655 
11656     /* Initialize */
11657     flag = wc_InitSha384(&sha384);
11658 
11659     if (flag == 0) {
11660         flag = wc_Sha384GetHash(&sha384, hash1);
11661     }
11662     /*test bad arguements*/
11663      if (flag == 0) {
11664         flag = wc_Sha384GetHash(NULL, NULL);
11665         if (flag == BAD_FUNC_ARG) {
11666             flag = 0;
11667         }
11668      }
11669      if (flag == 0) {
11670         flag = wc_Sha384GetHash(NULL, hash1);
11671         if (flag == BAD_FUNC_ARG) {
11672             flag = 0;
11673         }
11674      }
11675      if (flag == 0) {
11676         flag = wc_Sha384GetHash(&sha384, NULL);
11677         if (flag == BAD_FUNC_ARG) {
11678             flag = 0;
11679         }
11680     }
11681 
11682     wc_Sha384Free(&sha384);
11683 
11684     printf(resultFmt, flag == 0 ? passed : failed);
11685 #endif
11686     return flag;
11687 
11688 } /* END test_wc_Sha384GetHash */
11689 /*
11690  * Unit test function for wc_Sha384Copy()
11691  */
11692 static int test_wc_Sha384Copy (void)
11693 {
11694     int flag = 0;
11695 #ifdef WOLFSSL_SHA384
11696     wc_Sha384 sha384;
11697     wc_Sha384 temp;
11698 
11699     printf(testingFmt, "wc_Sha384Copy()");
11700 
11701     /* Initialize */
11702     flag = wc_InitSha384(&sha384);
11703     if (flag == 0) {
11704         flag = wc_InitSha384(&temp);
11705     }
11706     if (flag == 0) {
11707         flag = wc_Sha384Copy(&sha384, &temp);
11708     }
11709     /*test bad arguements*/
11710      if (flag == 0) {
11711         flag = wc_Sha384Copy(NULL, NULL);
11712         if (flag == BAD_FUNC_ARG) {
11713             flag = 0;
11714         }
11715      }
11716      if (flag == 0) {
11717         flag = wc_Sha384Copy(NULL, &temp);
11718         if (flag == BAD_FUNC_ARG) {
11719             flag = 0;
11720         }
11721      }
11722      if (flag == 0) {
11723         flag = wc_Sha384Copy(&sha384, NULL);
11724         if (flag == BAD_FUNC_ARG) {
11725             flag = 0;
11726         }
11727     }
11728 
11729     wc_Sha384Free(&sha384);
11730     wc_Sha384Free(&temp);
11731 
11732     printf(resultFmt, flag == 0 ? passed : failed);
11733 #endif
11734     return flag;
11735 
11736 } /* END test_wc_Sha384Copy */
11737 
11738 /*
11739  * Testing wc_InitSha224();
11740  */
11741 static int test_wc_InitSha224 (void)
11742 {
11743     int flag = 0;
11744 #ifdef WOLFSSL_SHA224
11745     wc_Sha224 sha224;
11746     int ret;
11747 
11748     printf(testingFmt, "wc_InitSha224()");
11749 
11750     /* Test good arg. */
11751     ret = wc_InitSha224(&sha224);
11752     if (ret != 0) {
11753         flag = WOLFSSL_FATAL_ERROR;
11754     }
11755 
11756     /* Test bad arg. */
11757     if (!flag) {
11758         ret = wc_InitSha224(NULL);
11759         if (ret != BAD_FUNC_ARG) {
11760             flag = WOLFSSL_FATAL_ERROR;
11761         }
11762     }
11763 
11764     wc_Sha224Free(&sha224);
11765     printf(resultFmt, flag == 0 ? passed : failed);
11766 
11767 #endif
11768     return flag;
11769 } /* END test_wc_InitSha224 */
11770 
11771 /*
11772  * Unit test on wc_Sha224Update
11773  */
11774 static int test_wc_Sha224Update (void)
11775 {
11776     int flag = 0;
11777 #ifdef WOLFSSL_SHA224
11778     wc_Sha224 sha224;
11779     byte hash[WC_SHA224_DIGEST_SIZE];
11780     testVector a, b, c;
11781     int ret;
11782 
11783     ret = wc_InitSha224(&sha224);
11784     if (ret != 0) {
11785         flag = ret;
11786     }
11787 
11788     printf(testingFmt, "wc_Sha224Update()");
11789 
11790     /* Input. */
11791     if (!flag) {
11792         a.input = "a";
11793         a.inLen = XSTRLEN(a.input);
11794 
11795         ret = wc_Sha224Update(&sha224, NULL, 0);
11796         if (ret != 0) {
11797             flag = ret;
11798         }
11799 
11800         ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
11801         if (ret != 0) {
11802             flag = ret;
11803         }
11804 
11805         ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
11806         if (ret != 0) {
11807             flag = ret;
11808         }
11809     }
11810 
11811     if (!flag) {
11812         ret = wc_Sha224Final(&sha224, hash);
11813         if (ret != 0) {
11814             flag = ret;
11815         }
11816     }
11817 
11818     /* Update input. */
11819     if (!flag) {
11820         a.input = "abc";
11821         a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
11822                     "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
11823         a.inLen = XSTRLEN(a.input);
11824         a.outLen = XSTRLEN(a.output);
11825 
11826         ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
11827         if (ret != 0) {
11828             flag = ret;
11829         }
11830     }
11831 
11832     if (!flag) {
11833         ret = wc_Sha224Final(&sha224, hash);
11834         if (ret != 0) {
11835             flag = ret;
11836         }
11837     }
11838 
11839     if (!flag) {
11840         if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
11841             flag = WOLFSSL_FATAL_ERROR;
11842         }
11843     }
11844 
11845     /* Pass  in bad values. */
11846     if (!flag) {
11847         b.input = NULL;
11848         b.inLen = 0;
11849 
11850         ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
11851         if (ret != 0) {
11852             flag = ret;
11853         }
11854     }
11855 
11856     if (!flag) {
11857         c.input = NULL;
11858         c.inLen = WC_SHA224_DIGEST_SIZE;
11859 
11860         ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
11861         if (ret != BAD_FUNC_ARG) {
11862             flag = WOLFSSL_FATAL_ERROR;
11863         }
11864     }
11865 
11866     if (!flag) {
11867         ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
11868         if (ret != BAD_FUNC_ARG) {
11869             flag = WOLFSSL_FATAL_ERROR;
11870         }
11871     }
11872 
11873     wc_Sha224Free(&sha224);
11874 
11875     /* If not returned then the unit test passed test vectors. */
11876     printf(resultFmt, flag == 0 ? passed : failed);
11877 #endif
11878     return flag;
11879 
11880 } /* END test_wc_Sha224Update */
11881 
11882 /*
11883  * Unit test for wc_Sha224Final();
11884  */
11885 static int test_wc_Sha224Final (void)
11886 {
11887     int flag = 0;
11888 #ifdef WOLFSSL_SHA224
11889     wc_Sha224 sha224;
11890     byte* hash_test[3];
11891     byte hash1[WC_SHA224_DIGEST_SIZE];
11892     byte hash2[2*WC_SHA224_DIGEST_SIZE];
11893     byte hash3[5*WC_SHA224_DIGEST_SIZE];
11894     int times, i, ret;
11895 
11896     /* Initialize */
11897     ret = wc_InitSha224(&sha224);
11898     if (ret) {
11899         flag = ret;
11900     }
11901 
11902     if (!flag) {
11903         hash_test[0] = hash1;
11904         hash_test[1] = hash2;
11905         hash_test[2] = hash3;
11906     }
11907 
11908     times = sizeof(hash_test) / sizeof(byte*);
11909 
11910     /* Good test args. */
11911     printf(testingFmt, "wc_sha224Final()");
11912     /* Testing oversized buffers. */
11913     for (i = 0; i < times; i++) {
11914         if (!flag) {
11915             ret = wc_Sha224Final(&sha224, hash_test[i]);
11916             if (ret != 0) {
11917                 flag = WOLFSSL_FATAL_ERROR;
11918             }
11919         }
11920     }
11921 
11922     /* Test bad args. */
11923     if (!flag) {
11924         ret = wc_Sha224Final(NULL, NULL);
11925         if (ret != BAD_FUNC_ARG) {
11926             flag = WOLFSSL_FATAL_ERROR;
11927         }
11928     }
11929 
11930     if (!flag) {
11931         ret = wc_Sha224Final(NULL, hash1);
11932         if (ret != BAD_FUNC_ARG) {
11933             flag = WOLFSSL_FATAL_ERROR;
11934         }
11935     }
11936 
11937     if (!flag) {
11938         ret = wc_Sha224Final(&sha224, NULL);
11939         if (ret != BAD_FUNC_ARG) {
11940             flag = WOLFSSL_FATAL_ERROR;
11941         }
11942     }
11943 
11944     wc_Sha224Free(&sha224);
11945 
11946     printf(resultFmt, flag == 0 ? passed : failed);
11947 #endif
11948     return flag;
11949 } /* END test_wc_Sha224Final */
11950 
11951 /*
11952  * Unit test function for wc_Sha224SetFlags()
11953  */
11954 static int test_wc_Sha224SetFlags (void)
11955 {
11956     int flag = 0;
11957 #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
11958     wc_Sha224 sha224;
11959     word32 flags = 0;
11960 
11961     printf(testingFmt, "wc_Sha224SetFlags()");
11962 
11963     /* Initialize */
11964     flag = wc_InitSha224(&sha224);
11965     if (flag == 0) {
11966         flag = wc_Sha224SetFlags(&sha224, flags);
11967     }
11968     if (flag == 0) {
11969         if (flags & WC_HASH_FLAG_ISCOPY) {
11970             flag = 0;
11971         }
11972     }
11973 
11974     wc_Sha224Free(&sha224);
11975 
11976     printf(resultFmt, flag == 0 ? passed : failed);
11977 
11978 #endif
11979     return flag;
11980 
11981 } /* END test_wc_Sha224SetFlags */
11982 
11983 /*
11984  * Unit test function for wc_Sha224GetFlags()
11985  */
11986 static int test_wc_Sha224GetFlags (void)
11987 {
11988     int flag = 0;
11989 #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
11990     wc_Sha224 sha224;
11991     word32 flags = 0;
11992 
11993     printf(testingFmt, "wc_Sha224GetFlags()");
11994 
11995     /* Initialize */
11996     flag = wc_InitSha224(&sha224);
11997     if (flag == 0) {
11998         flag = wc_Sha224GetFlags(&sha224, &flags);
11999     }
12000     if (flag == 0) {
12001         if (flags & WC_HASH_FLAG_ISCOPY) {
12002             flag = 0;
12003         }
12004     }
12005 
12006 
12007     wc_Sha224Free(&sha224);
12008 
12009     printf(resultFmt, flag == 0 ? passed : failed);
12010 
12011 #endif
12012     return flag;
12013 
12014 } /* END test_wc_Sha224GetFlags */
12015 /*
12016  * Unit test function for wc_Sha224Free()
12017  */
12018 static int test_wc_Sha224Free (void)
12019 {
12020     int flag = 0;
12021 #ifdef WOLFSSL_SHA224
12022     printf(testingFmt, "wc_Sha224Free()");
12023     wc_Sha224Free(NULL);
12024 
12025 
12026     printf(resultFmt, flag == 0 ? passed : failed);
12027 #endif
12028     return flag;
12029 
12030 } /* END test_wc_Sha224Free */
12031 
12032 /*
12033  * Unit test function for wc_Sha224GetHash()
12034  */
12035 static int test_wc_Sha224GetHash (void)
12036 {
12037     int flag = 0;
12038 #ifdef WOLFSSL_SHA224
12039     wc_Sha224 sha224;
12040     byte hash1[WC_SHA224_DIGEST_SIZE];
12041 
12042     printf(testingFmt, "wc_Sha224GetHash()");
12043 
12044     /* Initialize */
12045     flag = wc_InitSha224(&sha224);
12046 
12047     if (flag == 0) {
12048         flag = wc_Sha224GetHash(&sha224, hash1);
12049     }
12050     /*test bad arguements*/
12051      if (flag == 0) {
12052         flag = wc_Sha224GetHash(NULL, NULL);
12053         if (flag == BAD_FUNC_ARG) {
12054             flag = 0;
12055         }
12056      }
12057      if (flag == 0) {
12058         flag = wc_Sha224GetHash(NULL, hash1);
12059         if (flag == BAD_FUNC_ARG) {
12060             flag = 0;
12061         }
12062      }
12063      if (flag == 0) {
12064         flag = wc_Sha224GetHash(&sha224, NULL);
12065         if (flag == BAD_FUNC_ARG) {
12066             flag = 0;
12067         }
12068     }
12069 
12070     wc_Sha224Free(&sha224);
12071 
12072     printf(resultFmt, flag == 0 ? passed : failed);
12073 
12074 #endif
12075     return flag;
12076 
12077 } /* END test_wc_Sha224GetHash */
12078 
12079 /*
12080  * Unit test function for wc_Sha224Copy()
12081  */
12082 static int test_wc_Sha224Copy (void)
12083 {
12084     int flag = 0;
12085 #ifdef WOLFSSL_SHA224
12086     wc_Sha224 sha224;
12087     wc_Sha224 temp;
12088 
12089     printf(testingFmt, "wc_Sha224Copy()");
12090 
12091     /* Initialize */
12092     flag = wc_InitSha224(&sha224);
12093     if (flag == 0) {
12094         flag = wc_InitSha224(&temp);
12095     }
12096     if (flag == 0) {
12097         flag = wc_Sha224Copy(&sha224, &temp);
12098     }
12099     /*test bad arguements*/
12100      if (flag == 0) {
12101         flag = wc_Sha224Copy(NULL, NULL);
12102         if (flag == BAD_FUNC_ARG) {
12103             flag = 0;
12104         }
12105      }
12106      if (flag == 0) {
12107         flag = wc_Sha224Copy(NULL, &temp);
12108         if (flag == BAD_FUNC_ARG) {
12109             flag = 0;
12110         }
12111      }
12112      if (flag == 0) {
12113         flag = wc_Sha224Copy(&sha224, NULL);
12114         if (flag == BAD_FUNC_ARG) {
12115             flag = 0;
12116         }
12117     }
12118 
12119 
12120     wc_Sha224Free(&sha224);
12121     wc_Sha224Free(&temp);
12122 
12123     printf(resultFmt, flag == 0 ? passed : failed);
12124 
12125 #endif
12126     return flag;
12127 
12128 } /* END test_wc_Sha224Copy */
12129 
12130 
12131 /*
12132  * Testing wc_InitRipeMd()
12133  */
12134 static int test_wc_InitRipeMd (void)
12135 {
12136     int flag = 0;
12137 #ifdef WOLFSSL_RIPEMD
12138     RipeMd ripemd;
12139     int ret;
12140 
12141     printf(testingFmt, "wc_InitRipeMd()");
12142 
12143     /* Test good arg. */
12144     ret = wc_InitRipeMd(&ripemd);
12145     if (ret != 0) {
12146         flag = WOLFSSL_FATAL_ERROR;
12147     }
12148 
12149     /* Test bad arg. */
12150     if (!flag) {
12151         ret = wc_InitRipeMd(NULL);
12152         if (ret != BAD_FUNC_ARG) {
12153             flag = WOLFSSL_FATAL_ERROR;
12154         }
12155     }
12156 
12157     printf(resultFmt, flag == 0 ? passed : failed);
12158 
12159 #endif
12160     return flag;
12161 
12162 } /* END test_wc_InitRipeMd */
12163 
12164 /*
12165  * Testing wc_RipeMdUpdate()
12166  */
12167 static int test_wc_RipeMdUpdate (void)
12168 {
12169 
12170     int flag = 0;
12171 #ifdef WOLFSSL_RIPEMD
12172     RipeMd ripemd;
12173     byte hash[RIPEMD_DIGEST_SIZE];
12174     testVector a, b, c;
12175     int ret;
12176 
12177     ret = wc_InitRipeMd(&ripemd);
12178     if (ret != 0) {
12179         flag = ret;
12180     }
12181 
12182     printf(testingFmt, "wc_RipeMdUpdate()");
12183 
12184     /* Input */
12185     if (!flag) {
12186         a.input = "a";
12187         a.inLen = XSTRLEN(a.input);
12188 
12189         ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
12190         if (ret != 0) {
12191             flag = ret;
12192         }
12193     }
12194 
12195     if (!flag) {
12196         ret = wc_RipeMdFinal(&ripemd, hash);
12197         if (ret != 0) {
12198             flag = ret;
12199         }
12200     }
12201 
12202     /* Update input. */
12203     if (!flag) {
12204         a.input = "abc";
12205         a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
12206                    "\xb0\x87\xf1\x5a\x0b\xfc";
12207         a.inLen = XSTRLEN(a.input);
12208         a.outLen = XSTRLEN(a.output);
12209 
12210         ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
12211         if (ret != 0) {
12212             flag = ret;
12213         }
12214     }
12215 
12216     if (!flag) {
12217         ret = wc_RipeMdFinal(&ripemd, hash);
12218         if (ret != 0) {
12219             flag = ret;
12220         }
12221     }
12222 
12223     if (!flag) {
12224         if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
12225             flag = WOLFSSL_FATAL_ERROR;
12226         }
12227     }
12228 
12229     /* Pass in bad values. */
12230     if (!flag) {
12231         b.input = NULL;
12232         b.inLen = 0;
12233 
12234         ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
12235         if (ret != 0) {
12236             flag = ret;
12237         }
12238     }
12239 
12240     if (!flag) {
12241         c.input = NULL;
12242         c.inLen = RIPEMD_DIGEST_SIZE;
12243 
12244         ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
12245         if (ret != BAD_FUNC_ARG) {
12246             flag = WOLFSSL_FATAL_ERROR;
12247         }
12248     }
12249 
12250     if (!flag) {
12251         ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
12252         if (ret != BAD_FUNC_ARG) {
12253             flag = WOLFSSL_FATAL_ERROR;
12254         }
12255     }
12256 
12257     printf(resultFmt, flag == 0 ? passed : failed);
12258 
12259 #endif
12260     return flag;
12261 
12262 } /* END test_wc_RipeMdUdpate */
12263 
12264 /*
12265  * Unit test function for wc_RipeMdFinal()
12266  */
12267 static int test_wc_RipeMdFinal (void)
12268 {
12269     int flag = 0;
12270 #ifdef WOLFSSL_RIPEMD
12271     RipeMd ripemd;
12272     byte* hash_test[3];
12273     byte hash1[RIPEMD_DIGEST_SIZE];
12274     byte hash2[2*RIPEMD_DIGEST_SIZE];
12275     byte hash3[5*RIPEMD_DIGEST_SIZE];
12276     int times, i, ret;
12277 
12278     /* Initialize */
12279     ret = wc_InitRipeMd(&ripemd);
12280     if (ret != 0) {
12281         flag = ret;
12282     }
12283 
12284     if (!flag) {
12285         hash_test[0] = hash1;
12286         hash_test[1] = hash2;
12287         hash_test[2] = hash3;
12288     }
12289 
12290     times = sizeof(hash_test) / sizeof(byte*);
12291 
12292     /* Good test args. */
12293     printf(testingFmt, "wc_RipeMdFinal()");
12294     /* Testing oversized buffers. */
12295     for (i = 0; i < times; i++) {
12296         if (!flag) {
12297             ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
12298             if (ret != 0) {
12299                 flag = WOLFSSL_FATAL_ERROR;
12300             }
12301         }
12302     }
12303 
12304     /* Test bad args. */
12305     if (!flag) {
12306         ret = wc_RipeMdFinal(NULL, NULL);
12307         if (ret != BAD_FUNC_ARG) {
12308             flag = WOLFSSL_FATAL_ERROR;
12309         }
12310     }
12311 
12312     if (!flag) {
12313         ret = wc_RipeMdFinal(NULL, hash1);
12314         if (ret != BAD_FUNC_ARG) {
12315             flag = WOLFSSL_FATAL_ERROR;
12316         }
12317     }
12318 
12319     if (!flag) {
12320         ret = wc_RipeMdFinal(&ripemd, NULL);
12321         if (ret != BAD_FUNC_ARG) {
12322             flag = WOLFSSL_FATAL_ERROR;
12323         }
12324     }
12325 
12326     printf(resultFmt, flag == 0 ? passed : failed);
12327 #endif
12328     return flag;
12329 } /* END test_wc_RipeMdFinal */
12330 
12331 
12332 /*
12333  * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
12334  * wc_InitSha3_512
12335  */
12336 static int test_wc_InitSha3 (void)
12337 {
12338     int             ret = 0;
12339 #if defined(WOLFSSL_SHA3)
12340     wc_Sha3            sha3;
12341 
12342     (void)sha3;
12343 
12344     #if !defined(WOLFSSL_NOSHA3_224)
12345         printf(testingFmt, "wc_InitSha3_224()");
12346 
12347         ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
12348 
12349         /* Test bad args. */
12350         if (ret == 0) {
12351             ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
12352             if (ret == BAD_FUNC_ARG) {
12353                 ret = 0;
12354             } else if (ret == 0) {
12355                 ret = WOLFSSL_FATAL_ERROR;
12356             }
12357         }
12358         wc_Sha3_224_Free(&sha3);
12359         printf(resultFmt, ret == 0 ? passed : failed);
12360     #endif /* NOSHA3_224 */
12361     #if !defined(WOLFSSL_NOSHA3_256)
12362         if (ret == 0) {
12363             printf(testingFmt, "wc_InitSha3_256()");
12364 
12365             ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
12366             /* Test bad args. */
12367             if (ret == 0) {
12368                 ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
12369                 if (ret == BAD_FUNC_ARG) {
12370                     ret = 0;
12371                 } else if (ret == 0) {
12372                     ret = WOLFSSL_FATAL_ERROR;
12373                 }
12374             }
12375             wc_Sha3_256_Free(&sha3);
12376             printf(resultFmt, ret == 0 ? passed : failed);
12377         } /* END sha3_256 */
12378     #endif /* NOSHA3_256 */
12379     #if !defined(WOLFSSL_NOSHA3_384)
12380         if (ret == 0) {
12381             printf(testingFmt, "wc_InitSha3_384()");
12382 
12383             ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
12384             /* Test bad args. */
12385             if (ret == 0) {
12386                 ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
12387                 if (ret == BAD_FUNC_ARG) {
12388                     ret = 0;
12389                 } else if (ret == 0) {
12390                     ret = WOLFSSL_FATAL_ERROR;
12391                 }
12392             }
12393             wc_Sha3_384_Free(&sha3);
12394             printf(resultFmt, ret == 0 ? passed : failed);
12395         } /* END sha3_384 */
12396     #endif /* NOSHA3_384 */
12397     #if !defined(WOLFSSL_NOSHA3_512)
12398         if (ret == 0) {
12399             printf(testingFmt, "wc_InitSha3_512()");
12400 
12401             ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
12402             /* Test bad args. */
12403             if (ret == 0) {
12404                 ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
12405                 if (ret == BAD_FUNC_ARG) {
12406                     ret = 0;
12407                 } else if (ret == 0) {
12408                     ret = WOLFSSL_FATAL_ERROR;
12409                 }
12410             }
12411             wc_Sha3_512_Free(&sha3);
12412             printf(resultFmt, ret == 0 ? passed : failed);
12413         } /* END sha3_512 */
12414     #endif /* NOSHA3_512 */
12415 #endif
12416     return ret;
12417 
12418 } /* END test_wc_InitSha3 */
12419 
12420 
12421 /*
12422  * Testing wc_Sha3_Update()
12423  */
12424 static int testing_wc_Sha3_Update (void)
12425 {
12426     int         ret = 0;
12427 
12428 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
12429    !defined(WOLFSSL_AFALG_XILINX)
12430     wc_Sha3        sha3;
12431     byte        msg[] = "Everybody's working for the weekend.";
12432     byte        msg2[] = "Everybody gets Friday off.";
12433     byte        msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
12434                         "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
12435                         "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
12436                         "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
12437                         "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
12438     word32      msglen = sizeof(msg) - 1;
12439     word32      msg2len = sizeof(msg2);
12440     word32      msgCmplen = sizeof(msgCmp);
12441 
12442     #if !defined(WOLFSSL_NOSHA3_224)
12443         printf(testingFmt, "wc_Sha3_224_Update()");
12444 
12445         ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
12446         if (ret != 0) {
12447             return ret;
12448         }
12449 
12450         ret = wc_Sha3_224_Update(&sha3, msg, msglen);
12451         if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
12452             ret = WOLFSSL_FATAL_ERROR;
12453         }
12454         if (ret == 0) {
12455             ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
12456             if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
12457                 ret = WOLFSSL_FATAL_ERROR;
12458             }
12459         }
12460         /* Pass bad args. */
12461         if (ret == 0) {
12462             ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
12463             if (ret == BAD_FUNC_ARG) {
12464                 ret = wc_Sha3_224_Update(&sha3, NULL, 5);
12465             }
12466             if (ret == BAD_FUNC_ARG) {
12467                 wc_Sha3_224_Free(&sha3);
12468                 if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
12469                     return ret;
12470                 }
12471                 ret = wc_Sha3_224_Update(&sha3, NULL, 0);
12472                 if (ret == 0) {
12473                     ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
12474                 }
12475                 if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
12476                     ret = WOLFSSL_FATAL_ERROR;
12477                 }
12478             }
12479         }
12480         wc_Sha3_224_Free(&sha3);
12481 
12482         printf(resultFmt, ret == 0 ? passed : failed);
12483     #endif /* SHA3_224 */
12484 
12485     #if !defined(WOLFSSL_NOSHA3_256)
12486         if (ret == 0) {
12487             printf(testingFmt, "wc_Sha3_256_Update()");
12488 
12489             ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
12490             if (ret != 0) {
12491                 return ret;
12492             }
12493             ret = wc_Sha3_256_Update(&sha3, msg, msglen);
12494             if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
12495                 ret = WOLFSSL_FATAL_ERROR;
12496             }
12497             if (ret == 0) {
12498                 ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
12499                 if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
12500                     ret = WOLFSSL_FATAL_ERROR;
12501                 }
12502             }
12503             /* Pass bad args. */
12504             if (ret == 0) {
12505                 ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
12506                 if (ret == BAD_FUNC_ARG) {
12507                     ret = wc_Sha3_256_Update(&sha3, NULL, 5);
12508                 }
12509                 if (ret == BAD_FUNC_ARG) {
12510                     wc_Sha3_256_Free(&sha3);
12511                     if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
12512                         return ret;
12513                     }
12514                     ret = wc_Sha3_256_Update(&sha3, NULL, 0);
12515                     if (ret == 0) {
12516                         ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
12517                     }
12518                     if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
12519                         ret = WOLFSSL_FATAL_ERROR;
12520                     }
12521                 }
12522             }
12523             wc_Sha3_256_Free(&sha3);
12524 
12525             printf(resultFmt, ret == 0 ? passed : failed);
12526         }
12527     #endif /* SHA3_256 */
12528 
12529     #if !defined(WOLFSSL_NOSHA3_384)
12530         if (ret == 0) {
12531             printf(testingFmt, "wc_Sha3_384_Update()");
12532 
12533             ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
12534             if (ret != 0) {
12535                 return ret;
12536             }
12537             ret = wc_Sha3_384_Update(&sha3, msg, msglen);
12538             if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
12539                 ret = WOLFSSL_FATAL_ERROR;
12540             }
12541             if (ret == 0) {
12542                 ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
12543                 if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
12544                     ret = WOLFSSL_FATAL_ERROR;
12545                 }
12546             }
12547             /* Pass bad args. */
12548             if (ret == 0) {
12549                 ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
12550                 if (ret == BAD_FUNC_ARG) {
12551                     ret = wc_Sha3_384_Update(&sha3, NULL, 5);
12552                 }
12553                 if (ret == BAD_FUNC_ARG) {
12554                     wc_Sha3_384_Free(&sha3);
12555                     if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
12556                         return ret;
12557                     }
12558                     ret = wc_Sha3_384_Update(&sha3, NULL, 0);
12559                     if (ret == 0) {
12560                         ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
12561                     }
12562                     if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
12563                         ret = WOLFSSL_FATAL_ERROR;
12564                     }
12565                 }
12566             }
12567             wc_Sha3_384_Free(&sha3);
12568 
12569             printf(resultFmt, ret == 0 ? passed : failed);
12570         }
12571     #endif /* SHA3_384 */
12572 
12573     #if !defined(WOLFSSL_NOSHA3_512)
12574         if (ret == 0) {
12575             printf(testingFmt, "wc_Sha3_512_Update()");
12576 
12577             ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
12578             if (ret != 0) {
12579                 return ret;
12580             }
12581             ret = wc_Sha3_512_Update(&sha3, msg, msglen);
12582             if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
12583                 ret = WOLFSSL_FATAL_ERROR;
12584             }
12585             if (ret == 0) {
12586                 ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
12587                 if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
12588                     ret = WOLFSSL_FATAL_ERROR;
12589                 }
12590             }
12591             /* Pass bad args. */
12592             if (ret == 0) {
12593                 ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
12594                 if (ret == BAD_FUNC_ARG) {
12595                     ret = wc_Sha3_512_Update(&sha3, NULL, 5);
12596                 }
12597                 if (ret == BAD_FUNC_ARG) {
12598                     wc_Sha3_512_Free(&sha3);
12599                     if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
12600                         return ret;
12601                     }
12602                     ret = wc_Sha3_512_Update(&sha3, NULL, 0);
12603                     if (ret == 0) {
12604                         ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
12605                     }
12606                     if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
12607                         ret = WOLFSSL_FATAL_ERROR;
12608                     }
12609                 }
12610             }
12611             wc_Sha3_512_Free(&sha3);
12612             printf(resultFmt, ret == 0 ? passed : failed);
12613         }
12614     #endif /* SHA3_512 */
12615 #endif /* WOLFSSL_SHA3 */
12616     return ret;
12617 
12618 } /* END testing_wc_Sha3_Update */
12619 
12620 /*
12621  *  Testing wc_Sha3_224_Final()
12622  */
12623 static int test_wc_Sha3_224_Final (void)
12624 {
12625     int         ret = 0;
12626 
12627 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
12628     wc_Sha3        sha3;
12629     const char* msg    = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
12630                          "nopnopq";
12631     const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
12632                          "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
12633                          "\x64\xea\xd0\xfc\xce\x33";
12634     byte        hash[WC_SHA3_224_DIGEST_SIZE];
12635     byte        hashRet[WC_SHA3_224_DIGEST_SIZE];
12636 
12637     /* Init stack variables. */
12638     XMEMSET(hash, 0, sizeof(hash));
12639 
12640     printf(testingFmt, "wc_Sha3_224_Final()");
12641 
12642     ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
12643     if (ret != 0) {
12644         return ret;
12645     }
12646 
12647     ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12648     if (ret == 0) {
12649         ret = wc_Sha3_224_Final(&sha3, hash);
12650         if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
12651             ret = WOLFSSL_FATAL_ERROR;
12652         }
12653     }
12654     /* Test bad args. */
12655     if (ret == 0) {
12656         ret = wc_Sha3_224_Final(NULL, hash);
12657         if (ret == 0) {
12658             ret = wc_Sha3_224_Final(&sha3, NULL);
12659         }
12660         if (ret == BAD_FUNC_ARG) {
12661             ret = 0;
12662         } else if (ret == 0) {
12663             ret = WOLFSSL_FATAL_ERROR;
12664         }
12665     }
12666     wc_Sha3_224_Free(&sha3);
12667     printf(resultFmt, ret == 0 ? passed : failed);
12668 
12669     if (ret == 0) {
12670         printf(testingFmt, "wc_Sha3_224_GetHash()");
12671 
12672         ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
12673         if (ret != 0) {
12674             return ret;
12675         }
12676 
12677         /* Init stack variables. */
12678         XMEMSET(hash, 0, sizeof(hash));
12679         XMEMSET(hashRet, 0, sizeof(hashRet));
12680 
12681         ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12682         if (ret == 0) {
12683             ret = wc_Sha3_224_GetHash(&sha3, hashRet);
12684         }
12685 
12686         if (ret == 0) {
12687             ret = wc_Sha3_224_Final(&sha3, hash);
12688             if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
12689                 ret = WOLFSSL_FATAL_ERROR;
12690             }
12691         }
12692         if (ret == 0) {
12693             /* Test bad args. */
12694             ret = wc_Sha3_224_GetHash(NULL, hashRet);
12695             if (ret == BAD_FUNC_ARG) {
12696                 ret = wc_Sha3_224_GetHash(&sha3, NULL);
12697             }
12698             if (ret == BAD_FUNC_ARG) {
12699                 ret = 0;
12700             } else if (ret == 0) {
12701                 ret = WOLFSSL_FATAL_ERROR;
12702             }
12703         }
12704 
12705         printf(resultFmt, ret == 0 ? passed : failed);
12706     }
12707 
12708     wc_Sha3_224_Free(&sha3);
12709 #endif
12710     return ret;
12711 } /* END test_wc_Sha3_224_Final */
12712 
12713 
12714 /*
12715  *  Testing wc_Sha3_256_Final()
12716  */
12717 static int test_wc_Sha3_256_Final (void)
12718 {
12719     int         ret = 0;
12720 
12721 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
12722     wc_Sha3        sha3;
12723     const char* msg    = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
12724                          "nopnopq";
12725     const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
12726                         "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
12727                         "\xdd\x97\x49\x6d\x33\x76";
12728     byte        hash[WC_SHA3_256_DIGEST_SIZE];
12729     byte        hashRet[WC_SHA3_256_DIGEST_SIZE];
12730 
12731     /* Init stack variables. */
12732     XMEMSET(hash, 0, sizeof(hash));
12733 
12734     printf(testingFmt, "wc_Sha3_256_Final()");
12735 
12736     ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
12737     if (ret != 0) {
12738         return ret;
12739     }
12740 
12741     ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12742     if (ret == 0) {
12743         ret = wc_Sha3_256_Final(&sha3, hash);
12744         if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
12745             ret = WOLFSSL_FATAL_ERROR;
12746         }
12747     }
12748     /* Test bad args. */
12749     if (ret == 0) {
12750         ret = wc_Sha3_256_Final(NULL, hash);
12751         if (ret == 0) {
12752             ret = wc_Sha3_256_Final(&sha3, NULL);
12753         }
12754         if (ret == BAD_FUNC_ARG) {
12755             ret = 0;
12756         } else if (ret == 0) {
12757             ret = WOLFSSL_FATAL_ERROR;
12758         }
12759     }
12760     wc_Sha3_256_Free(&sha3);
12761     printf(resultFmt, ret == 0 ? passed : failed);
12762 
12763     if (ret == 0) {
12764         printf(testingFmt, "wc_Sha3_256_GetHash()");
12765 
12766         ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
12767         if (ret != 0) {
12768             return ret;
12769         }
12770         /* Init stack variables. */
12771         XMEMSET(hash, 0, sizeof(hash));
12772         XMEMSET(hashRet, 0, sizeof(hashRet));
12773 
12774         ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12775         if (ret == 0) {
12776             ret = wc_Sha3_256_GetHash(&sha3, hashRet);
12777         }
12778         if (ret == 0) {
12779             ret = wc_Sha3_256_Final(&sha3, hash);
12780             if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
12781                 ret = WOLFSSL_FATAL_ERROR;
12782             }
12783         }
12784         if (ret == 0) {
12785             /* Test bad args. */
12786             ret = wc_Sha3_256_GetHash(NULL, hashRet);
12787             if (ret == BAD_FUNC_ARG) {
12788                 ret = wc_Sha3_256_GetHash(&sha3, NULL);
12789             }
12790             if (ret == BAD_FUNC_ARG) {
12791                 ret = 0;
12792             } else if (ret == 0) {
12793                 ret = WOLFSSL_FATAL_ERROR;
12794             }
12795         }
12796 
12797         printf(resultFmt, ret == 0 ? passed : failed);
12798     }
12799 
12800     wc_Sha3_256_Free(&sha3);
12801 #endif
12802     return ret;
12803 } /* END test_wc_Sha3_256_Final */
12804 
12805 
12806 /*
12807  *  Testing wc_Sha3_384_Final()
12808  */
12809 static int test_wc_Sha3_384_Final (void)
12810 {
12811     int         ret = 0;
12812 
12813 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
12814     wc_Sha3        sha3;
12815     const char* msg    = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
12816                          "nopnopq";
12817     const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
12818                          "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
12819                          "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
12820                          "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
12821     byte        hash[WC_SHA3_384_DIGEST_SIZE];
12822     byte        hashRet[WC_SHA3_384_DIGEST_SIZE];
12823 
12824     /* Init stack variables. */
12825     XMEMSET(hash, 0, sizeof(hash));
12826 
12827     printf(testingFmt, "wc_Sha3_384_Final()");
12828 
12829     ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
12830     if (ret != 0) {
12831         return ret;
12832     }
12833 
12834     ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12835     if (ret == 0) {
12836         ret = wc_Sha3_384_Final(&sha3, hash);
12837         if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
12838             ret = WOLFSSL_FATAL_ERROR;
12839         }
12840     }
12841     /* Test bad args. */
12842     if (ret == 0) {
12843         ret = wc_Sha3_384_Final(NULL, hash);
12844         if (ret == 0) {
12845             ret = wc_Sha3_384_Final(&sha3, NULL);
12846         }
12847         if (ret == BAD_FUNC_ARG) {
12848             ret = 0;
12849         } else if (ret == 0) {
12850             ret = WOLFSSL_FATAL_ERROR;
12851         }
12852     }
12853     wc_Sha3_384_Free(&sha3);
12854     printf(resultFmt, ret == 0 ? passed : failed);
12855 
12856     if (ret == 0) {
12857         printf(testingFmt, "wc_Sha3_384_GetHash()");
12858 
12859         ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
12860         if (ret != 0) {
12861             return ret;
12862         }
12863         /* Init stack variables. */
12864         XMEMSET(hash, 0, sizeof(hash));
12865         XMEMSET(hashRet, 0, sizeof(hashRet));
12866 
12867         ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12868         if (ret == 0) {
12869             ret = wc_Sha3_384_GetHash(&sha3, hashRet);
12870         }
12871         if (ret == 0) {
12872             ret = wc_Sha3_384_Final(&sha3, hash);
12873             if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
12874                 ret = WOLFSSL_FATAL_ERROR;
12875             }
12876         }
12877         if (ret == 0) {
12878             /* Test bad args. */
12879             ret = wc_Sha3_384_GetHash(NULL, hashRet);
12880             if (ret == BAD_FUNC_ARG) {
12881                 ret = wc_Sha3_384_GetHash(&sha3, NULL);
12882             }
12883             if (ret == BAD_FUNC_ARG) {
12884                 ret = 0;
12885             } else if (ret == 0) {
12886                 ret = WOLFSSL_FATAL_ERROR;
12887             }
12888         }
12889 
12890         printf(resultFmt, ret == 0 ? passed : failed);
12891     }
12892 
12893     wc_Sha3_384_Free(&sha3);
12894 #endif
12895     return ret;
12896 } /* END test_wc_Sha3_384_Final */
12897 
12898 
12899 
12900 /*
12901  *  Testing wc_Sha3_512_Final()
12902  */
12903 static int test_wc_Sha3_512_Final (void)
12904 {
12905     int         ret = 0;
12906 
12907 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
12908    !defined(WOLFSSL_NOSHA3_384)
12909     wc_Sha3        sha3;
12910     const char* msg    = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
12911                          "nopnopq";
12912     const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
12913                          "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
12914                          "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
12915                          "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
12916                          "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
12917     byte        hash[WC_SHA3_512_DIGEST_SIZE];
12918     byte        hashRet[WC_SHA3_512_DIGEST_SIZE];
12919 
12920     /* Init stack variables. */
12921     XMEMSET(hash, 0, sizeof(hash));
12922 
12923     printf(testingFmt, "wc_Sha3_512_Final()");
12924 
12925     ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
12926     if (ret != 0) {
12927         return ret;
12928     }
12929 
12930     ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12931     if (ret == 0) {
12932         ret = wc_Sha3_512_Final(&sha3, hash);
12933         if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
12934             ret = WOLFSSL_FATAL_ERROR;
12935         }
12936     }
12937     /* Test bad args. */
12938     if (ret == 0) {
12939         ret = wc_Sha3_512_Final(NULL, hash);
12940         if (ret == 0) {
12941             ret = wc_Sha3_384_Final(&sha3, NULL);
12942         }
12943         if (ret == BAD_FUNC_ARG) {
12944             ret = 0;
12945         } else if (ret == 0) {
12946             ret = WOLFSSL_FATAL_ERROR;
12947         }
12948     }
12949     wc_Sha3_512_Free(&sha3);
12950     printf(resultFmt, ret == 0 ? passed : failed);
12951 
12952     if (ret == 0) {
12953         printf(testingFmt, "wc_Sha3_512_GetHash()");
12954 
12955         ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
12956         if (ret != 0) {
12957             return ret;
12958         }
12959         /* Init stack variables. */
12960         XMEMSET(hash, 0, sizeof(hash));
12961         XMEMSET(hashRet, 0, sizeof(hashRet));
12962 
12963         ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
12964         if (ret == 0) {
12965             ret = wc_Sha3_512_GetHash(&sha3, hashRet);
12966         }
12967         if (ret == 0) {
12968             ret = wc_Sha3_512_Final(&sha3, hash);
12969             if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
12970                 ret = WOLFSSL_FATAL_ERROR;
12971             }
12972         }
12973         if (ret == 0) {
12974             /* Test bad args. */
12975             ret = wc_Sha3_512_GetHash(NULL, hashRet);
12976             if (ret == BAD_FUNC_ARG) {
12977                 ret = wc_Sha3_512_GetHash(&sha3, NULL);
12978             }
12979             if (ret == BAD_FUNC_ARG) {
12980                 ret = 0;
12981             } else if (ret == 0) {
12982                 ret = WOLFSSL_FATAL_ERROR;
12983             }
12984         }
12985 
12986         printf(resultFmt, ret == 0 ? passed : failed);
12987     }
12988 
12989     wc_Sha3_512_Free(&sha3);
12990 #endif
12991     return ret;
12992 } /* END test_wc_Sha3_512_Final */
12993 
12994 
12995 /*
12996  *  Testing wc_Sha3_224_Copy()
12997  */
12998 static int test_wc_Sha3_224_Copy (void)
12999 {
13000     int         ret = 0;
13001 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
13002     wc_Sha3        sha3, sha3Cpy;
13003     const char* msg = TEST_STRING;
13004     word32      msglen = (word32)TEST_STRING_SZ;
13005     byte        hash[WC_SHA3_224_DIGEST_SIZE];
13006     byte        hashCpy[WC_SHA3_224_DIGEST_SIZE];
13007 
13008     XMEMSET(hash, 0, sizeof(hash));
13009     XMEMSET(hashCpy, 0, sizeof(hashCpy));
13010 
13011     printf(testingFmt, "wc_Sha3_224_Copy()");
13012 
13013     ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
13014     if (ret != 0) {
13015         return ret;
13016     }
13017 
13018     ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
13019     if (ret != 0) {
13020     wc_Sha3_224_Free(&sha3);
13021     return ret;
13022     }
13023 
13024     ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
13025 
13026     if (ret == 0) {
13027         ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
13028         if (ret == 0) {
13029             ret = wc_Sha3_224_Final(&sha3, hash);
13030             if (ret == 0) {
13031                 ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
13032             }
13033         }
13034         if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
13035             ret = WOLFSSL_FATAL_ERROR;
13036         }
13037     }
13038     /* Test bad args. */
13039     if (ret == 0) {
13040         ret = wc_Sha3_224_Copy(NULL, &sha3);
13041         if (ret == BAD_FUNC_ARG) {
13042             ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
13043         }
13044         if (ret == BAD_FUNC_ARG) {
13045             ret = 0;
13046         } else if (ret == 0) {
13047             ret = WOLFSSL_FATAL_ERROR;
13048         }
13049     }
13050 
13051     printf(resultFmt, ret == 0 ? passed : failed);
13052 
13053 #endif
13054     return ret;
13055 
13056 } /* END test_wc_Sha3_224_Copy */
13057 
13058 
13059 
13060 /*
13061  *  Testing wc_Sha3_256_Copy()
13062  */
13063 static int test_wc_Sha3_256_Copy (void)
13064 {
13065     int         ret = 0;
13066 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
13067     wc_Sha3        sha3, sha3Cpy;
13068     const char* msg = TEST_STRING;
13069     word32      msglen = (word32)TEST_STRING_SZ;
13070     byte        hash[WC_SHA3_256_DIGEST_SIZE];
13071     byte        hashCpy[WC_SHA3_256_DIGEST_SIZE];
13072 
13073     XMEMSET(hash, 0, sizeof(hash));
13074     XMEMSET(hashCpy, 0, sizeof(hashCpy));
13075 
13076     printf(testingFmt, "wc_Sha3_256_Copy()");
13077 
13078     ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
13079     if (ret != 0) {
13080         return ret;
13081     }
13082 
13083     ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
13084     if (ret != 0) {
13085     wc_Sha3_256_Free(&sha3);
13086     return ret;
13087     }
13088 
13089     ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
13090 
13091     if (ret == 0) {
13092         ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
13093         if (ret == 0) {
13094             ret = wc_Sha3_256_Final(&sha3, hash);
13095             if (ret == 0) {
13096                 ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
13097             }
13098         }
13099         if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
13100             ret = WOLFSSL_FATAL_ERROR;
13101         }
13102     }
13103     /* Test bad args. */
13104     if (ret == 0) {
13105         ret = wc_Sha3_256_Copy(NULL, &sha3);
13106         if (ret == BAD_FUNC_ARG) {
13107             ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
13108         }
13109         if (ret == BAD_FUNC_ARG) {
13110             ret = 0;
13111         } else if (ret == 0) {
13112             ret = WOLFSSL_FATAL_ERROR;
13113         }
13114     }
13115 
13116     printf(resultFmt, ret == 0 ? passed : failed);
13117 
13118 #endif
13119     return ret;
13120 
13121 } /* END test_wc_Sha3_256_Copy */
13122 
13123 
13124 
13125 /*
13126  *  Testing wc_Sha3_384_Copy()
13127  */
13128 static int test_wc_Sha3_384_Copy (void)
13129 {
13130     int         ret = 0;
13131 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
13132     wc_Sha3        sha3, sha3Cpy;
13133     const char* msg = TEST_STRING;
13134     word32      msglen = (word32)TEST_STRING_SZ;
13135     byte        hash[WC_SHA3_384_DIGEST_SIZE];
13136     byte        hashCpy[WC_SHA3_384_DIGEST_SIZE];
13137 
13138     XMEMSET(hash, 0, sizeof(hash));
13139     XMEMSET(hashCpy, 0, sizeof(hashCpy));
13140 
13141     printf(testingFmt, "wc_Sha3_384_Copy()");
13142 
13143     ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
13144     if (ret != 0) {
13145         return ret;
13146     }
13147 
13148     ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
13149     if (ret != 0) {
13150     wc_Sha3_384_Free(&sha3);
13151     return ret;
13152     }
13153 
13154     ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
13155 
13156     if (ret == 0) {
13157         ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
13158         if (ret == 0) {
13159             ret = wc_Sha3_384_Final(&sha3, hash);
13160             if (ret == 0) {
13161                 ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
13162             }
13163         }
13164         if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
13165             ret = WOLFSSL_FATAL_ERROR;
13166         }
13167     }
13168     /* Test bad args. */
13169     if (ret == 0) {
13170         ret = wc_Sha3_384_Copy(NULL, &sha3);
13171         if (ret == BAD_FUNC_ARG) {
13172             ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
13173         }
13174         if (ret == BAD_FUNC_ARG) {
13175             ret = 0;
13176         } else if (ret == 0) {
13177             ret = WOLFSSL_FATAL_ERROR;
13178         }
13179     }
13180 
13181     printf(resultFmt, ret == 0 ? passed : failed);
13182 
13183 #endif
13184     return ret;
13185 
13186 } /* END test_wc_Sha3_384_Copy */
13187 
13188 
13189 /*
13190  *  Testing wc_Sha3_512_Copy()
13191  */
13192 static int test_wc_Sha3_512_Copy (void)
13193 {
13194     int         ret = 0;
13195 
13196 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
13197     wc_Sha3        sha3, sha3Cpy;
13198     const char* msg = TEST_STRING;
13199     word32      msglen = (word32)TEST_STRING_SZ;
13200     byte        hash[WC_SHA3_512_DIGEST_SIZE];
13201     byte        hashCpy[WC_SHA3_512_DIGEST_SIZE];
13202 
13203     XMEMSET(hash, 0, sizeof(hash));
13204     XMEMSET(hashCpy, 0, sizeof(hashCpy));
13205 
13206 
13207     printf(testingFmt, "wc_Sha3_512_Copy()");
13208 
13209     ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
13210     if (ret != 0) {
13211         return ret;
13212     }
13213 
13214     ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
13215     if (ret != 0) {
13216     wc_Sha3_512_Free(&sha3);
13217     return ret;
13218     }
13219 
13220     ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
13221 
13222     if (ret == 0) {
13223         ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
13224         if (ret == 0) {
13225             ret = wc_Sha3_512_Final(&sha3, hash);
13226             if (ret == 0) {
13227                 ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
13228             }
13229         }
13230         if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
13231             ret = WOLFSSL_FATAL_ERROR;
13232         }
13233     }
13234     /* Test bad args. */
13235     if (ret == 0) {
13236         ret = wc_Sha3_512_Copy(NULL, &sha3);
13237         if (ret == BAD_FUNC_ARG) {
13238             ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
13239         }
13240         if (ret == BAD_FUNC_ARG) {
13241             ret = 0;
13242         } else if (ret == 0) {
13243             ret = WOLFSSL_FATAL_ERROR;
13244         }
13245     }
13246 
13247     printf(resultFmt, ret == 0 ? passed : failed);
13248 
13249 #endif
13250     return ret;
13251 
13252 } /* END test_wc_Sha3_512_Copy */
13253 /*
13254  * Unit test function for wc_Sha3_GetFlags()
13255  */
13256 static int test_wc_Sha3_GetFlags (void)
13257 {
13258     int ret = 0;
13259 #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
13260     wc_Sha3            sha3;
13261     word32             flags = 0;
13262 
13263 
13264     printf(testingFmt, "wc_Sha3_GetFlags()");
13265 
13266     /* Initialize */
13267     ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
13268     if (ret != 0) {
13269         return ret;
13270     }
13271     if (ret == 0) {
13272         ret = wc_Sha3_GetFlags(&sha3, &flags);
13273     }
13274     if (ret == 0) {
13275         if (flags & WC_HASH_FLAG_ISCOPY) {
13276             ret = 0;
13277         }
13278     }
13279     wc_Sha3_224_Free(&sha3);
13280     printf(resultFmt, ret == 0 ? passed : failed);
13281 
13282 #endif
13283     return ret;
13284 
13285 } /* END test_wc_Sha3_GetFlags */
13286 
13287 
13288 
13289 
13290 static int test_wc_InitShake256 (void)
13291 {
13292     int             ret = 0;
13293 #ifdef WOLFSSL_SHAKE256
13294     wc_Shake        shake;
13295 
13296     printf(testingFmt, "wc_InitShake256()");
13297 
13298     ret = wc_InitShake256(&shake, HEAP_HINT, devId);
13299     /* Test bad args. */
13300     if (ret == 0) {
13301         ret = wc_InitShake256(NULL, HEAP_HINT, devId);
13302         if (ret == BAD_FUNC_ARG) {
13303             ret = 0;
13304         } else if (ret == 0) {
13305             ret = WOLFSSL_FATAL_ERROR;
13306         }
13307     }
13308     wc_Shake256_Free(&shake);
13309     printf(resultFmt, ret == 0 ? passed : failed);
13310 #endif
13311     return ret;
13312 
13313 } /* END test_wc_InitSha3 */
13314 
13315 
13316 static int testing_wc_Shake256_Update (void)
13317 {
13318     int         ret = 0;
13319 
13320 #ifdef WOLFSSL_SHAKE256
13321     wc_Shake    shake;
13322     byte        msg[] = "Everybody's working for the weekend.";
13323     byte        msg2[] = "Everybody gets Friday off.";
13324     byte        msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
13325                         "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
13326                         "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
13327                         "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
13328                         "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
13329     word32      msglen = sizeof(msg) - 1;
13330     word32      msg2len = sizeof(msg2);
13331     word32      msgCmplen = sizeof(msgCmp);
13332 
13333     printf(testingFmt, "wc_Shake256_Update()");
13334 
13335     ret = wc_InitShake256(&shake, HEAP_HINT, devId);
13336     if (ret != 0) {
13337         return ret;
13338     }
13339     ret = wc_Shake256_Update(&shake, msg, msglen);
13340     if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
13341         ret = WOLFSSL_FATAL_ERROR;
13342     }
13343     if (ret == 0) {
13344         ret = wc_Shake256_Update(&shake, msg2, msg2len);
13345         if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
13346             ret = WOLFSSL_FATAL_ERROR;
13347         }
13348     }
13349     /* Pass bad args. */
13350     if (ret == 0) {
13351         ret = wc_Shake256_Update(NULL, msg2, msg2len);
13352         if (ret == BAD_FUNC_ARG) {
13353             ret = wc_Shake256_Update(&shake, NULL, 5);
13354         }
13355         if (ret == BAD_FUNC_ARG) {
13356             wc_Shake256_Free(&shake);
13357             if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
13358                 return ret;
13359             }
13360             ret = wc_Shake256_Update(&shake, NULL, 0);
13361             if (ret == 0) {
13362                 ret = wc_Shake256_Update(&shake, msg2, msg2len);
13363             }
13364             if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
13365                 ret = WOLFSSL_FATAL_ERROR;
13366             }
13367         }
13368     }
13369     wc_Shake256_Free(&shake);
13370 
13371     printf(resultFmt, ret == 0 ? passed : failed);
13372 #endif /* WOLFSSL_SHAKE256 */
13373 
13374     return ret;
13375 
13376 }
13377 
13378 static int test_wc_Shake256_Final (void)
13379 {
13380     int         ret = 0;
13381 
13382 #ifdef WOLFSSL_SHAKE256
13383     wc_Shake    shake;
13384     const char* msg    = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
13385                          "nopnopq";
13386     const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
13387                          "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
13388                          "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
13389                          "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
13390                          "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
13391                          "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
13392                          "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
13393                          "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
13394                          "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
13395     byte        hash[114];
13396 
13397     /* Init stack variables. */
13398     XMEMSET(hash, 0, sizeof(hash));
13399 
13400     printf(testingFmt, "wc_Shake256_Final()");
13401 
13402     ret = wc_InitShake256(&shake, HEAP_HINT, devId);
13403     if (ret != 0) {
13404         return ret;
13405     }
13406 
13407     ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
13408     if (ret == 0) {
13409         ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
13410         if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
13411             ret = WOLFSSL_FATAL_ERROR;
13412         }
13413     }
13414     /* Test bad args. */
13415     if (ret == 0) {
13416         ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
13417         if (ret == 0) {
13418             ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
13419         }
13420         if (ret == BAD_FUNC_ARG) {
13421             ret = 0;
13422         } else if (ret == 0) {
13423             ret = WOLFSSL_FATAL_ERROR;
13424         }
13425     }
13426     wc_Shake256_Free(&shake);
13427     printf(resultFmt, ret == 0 ? passed : failed);
13428 #endif
13429     return ret;
13430 }
13431 /*
13432  *  Testing wc_Shake256_Copy()
13433  */
13434 static int test_wc_Shake256_Copy (void)
13435 {
13436     int         ret = 0;
13437 #ifdef WOLFSSL_SHAKE256
13438     wc_Shake    shake, shakeCpy;
13439     const char* msg = TEST_STRING;
13440     word32      msglen = (word32)TEST_STRING_SZ;
13441     byte        hash[144];
13442     byte        hashCpy[144];
13443     word32      hashLen = sizeof(hash);
13444     word32      hashLenCpy = sizeof(hashCpy);
13445 
13446     XMEMSET(hash, 0, sizeof(hash));
13447     XMEMSET(hashCpy, 0, sizeof(hashCpy));
13448 
13449     printf(testingFmt, "wc_Shake256_Copy()");
13450 
13451     ret = wc_InitShake256(&shake, HEAP_HINT, devId);
13452     if (ret != 0) {
13453         return ret;
13454     }
13455 
13456     ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
13457     if (ret != 0) {
13458         wc_Shake256_Free(&shake);
13459         return ret;
13460     }
13461 
13462     ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
13463 
13464     if (ret == 0) {
13465         ret = wc_Shake256_Copy(&shakeCpy, &shake);
13466         if (ret == 0) {
13467             ret = wc_Shake256_Final(&shake, hash, hashLen);
13468             if (ret == 0) {
13469                 ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
13470             }
13471         }
13472         if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
13473             ret = WOLFSSL_FATAL_ERROR;
13474         }
13475     }
13476     /* Test bad args. */
13477     if (ret == 0) {
13478         ret = wc_Shake256_Copy(NULL, &shake);
13479         if (ret == BAD_FUNC_ARG) {
13480             ret = wc_Shake256_Copy(&shakeCpy, NULL);
13481         }
13482         if (ret == BAD_FUNC_ARG) {
13483             ret = 0;
13484         } else if (ret == 0) {
13485             ret = WOLFSSL_FATAL_ERROR;
13486         }
13487     }
13488     wc_Shake256_Free(&shake);
13489     printf(resultFmt, ret == 0 ? passed : failed);
13490 
13491 #endif
13492     return ret;
13493 } /* END test_wc_Shake256_Copy */
13494 /*
13495  * Unit test function for wc_Shake256Hash()
13496  */
13497 static int test_wc_Shake256Hash(void)
13498 {
13499     int ret = 0;
13500 #ifdef WOLFSSL_SHAKE256
13501 
13502     const byte data[] = { /* Hello World */
13503         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
13504         0x72,0x6c,0x64
13505     };
13506     word32      len = sizeof(data);
13507     byte        hash[144];
13508     word32      hashLen = sizeof(hash);
13509 
13510     printf(testingFmt, "wc_Shake256Hash()");
13511 
13512     ret = wc_Shake256Hash(data, len, hash, hashLen);
13513 
13514     printf(resultFmt, ret == 0 ? passed : failed);
13515 
13516 #endif
13517     return ret;
13518 }  /* END test_wc_Shake256Hash */
13519 /*
13520  * unit test for wc_IdeaSetKey()
13521  */
13522 static int test_wc_IdeaSetKey (void)
13523 {
13524     int ret = 0;
13525 #ifdef HAVE_IDEA
13526 
13527     Idea        idea;
13528     const byte  key[] =
13529     {
13530         0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
13531         0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
13532     };
13533     int         flag = 0;
13534 
13535     printf(testingFmt, "wc_IdeaSetKey()");
13536     /*IV can be NULL, default value is 0*/
13537     ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
13538     if (ret == 0) {
13539         ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
13540     }
13541     /* Bad args. */
13542     if (ret == 0) {
13543         ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
13544         if (ret != BAD_FUNC_ARG) {
13545             flag = 1;
13546         }
13547         ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
13548         if (ret != BAD_FUNC_ARG) {
13549             flag = 1;
13550         }
13551         ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
13552                                     NULL, IDEA_ENCRYPTION);
13553         if (ret != BAD_FUNC_ARG) {
13554             flag = 1;
13555         }
13556         ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
13557         if (ret != BAD_FUNC_ARG) {
13558             flag = 1;
13559         }
13560         if (flag == 1) {
13561             ret = WOLFSSL_FATAL_ERROR;
13562         } else {
13563             ret = 0;
13564         }
13565     } /* END Test Bad Args. */
13566 
13567     printf(resultFmt, ret == 0 ? passed : failed);
13568 
13569 #endif
13570     return ret;
13571 
13572 } /* END test_wc_IdeaSetKey */
13573 
13574 /*
13575  * Unit test for wc_IdeaSetIV()
13576  */
13577 static int test_wc_IdeaSetIV (void)
13578 {
13579     int     ret = 0;
13580 #ifdef HAVE_IDEA
13581     Idea    idea;
13582 
13583     printf(testingFmt, "wc_IdeaSetIV()");
13584 
13585     ret = wc_IdeaSetIV(&idea, NULL);
13586     /* Test bad args. */
13587     if (ret == 0) {
13588         ret = wc_IdeaSetIV(NULL, NULL);
13589         if (ret == BAD_FUNC_ARG) {
13590             ret = 0;
13591         } else {
13592             ret = WOLFSSL_FATAL_ERROR;
13593         }
13594     }
13595 
13596     printf(resultFmt, ret == 0 ? passed : failed);
13597 #endif
13598     return ret;
13599 
13600 } /* END test_wc_IdeaSetIV */
13601 
13602 /*
13603  * Unit test for wc_IdeaCipher()
13604  */
13605 static int test_wc_IdeaCipher (void)
13606 {
13607     int     ret = 0;
13608 #ifdef HAVE_IDEA
13609     Idea        idea;
13610     const byte  key[] =
13611     {
13612         0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
13613         0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
13614     };
13615     const byte  plain[] =
13616     {
13617         0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
13618     };
13619     byte    enc[sizeof(plain)];
13620     byte    dec[sizeof(enc)];
13621 
13622     printf(testingFmt, "wc_IdeaCipher()");
13623 
13624     ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
13625     if (ret == 0) {
13626         ret = wc_IdeaCipher(&idea, enc, plain);
13627         if (ret != 0) {
13628             ret = WOLFSSL_FATAL_ERROR;
13629         }
13630     }
13631     if (ret == 0) {
13632         ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
13633         if (ret == 0) {
13634             ret = wc_IdeaCipher(&idea, dec, enc);
13635         }
13636         if (ret == 0) {
13637             ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
13638         }
13639         if (ret != 0) {
13640             ret = WOLFSSL_FATAL_ERROR;
13641         }
13642     }
13643     /* Pass Bad Args. */
13644     if (ret == 0) {
13645         ret = wc_IdeaCipher(NULL, enc, dec);
13646         if (ret == BAD_FUNC_ARG) {
13647             ret = wc_IdeaCipher(&idea, NULL, dec);
13648         }
13649         if (ret == BAD_FUNC_ARG) {
13650             ret = wc_IdeaCipher(&idea, enc, NULL);
13651         }
13652         if (ret == BAD_FUNC_ARG) {
13653             ret = 0;
13654         } else {
13655             ret = WOLFSSL_FATAL_ERROR;
13656         }
13657     }
13658 
13659     printf(resultFmt, ret == 0 ? passed : failed);
13660 
13661 #endif
13662     return ret;
13663 } /* END test_wc_IdeaCipher */
13664 
13665 /*
13666  * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
13667  */
13668 static int test_wc_IdeaCbcEncyptDecrypt (void)
13669 {
13670     int         ret = 0;
13671 #ifdef HAVE_IDEA
13672     Idea        idea;
13673     const byte  key[] =
13674     {
13675         0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
13676         0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
13677     };
13678     const char* message = "International Data Encryption Algorithm";
13679     byte        msg_enc[40];
13680     byte        msg_dec[40];
13681 
13682     printf(testingFmt, "wc_IdeaCbcEncrypt()");
13683 
13684     ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
13685     if (ret == 0) {
13686         ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
13687                                         (word32)XSTRLEN(message) + 1);
13688     }
13689     if (ret == 0) {
13690         ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
13691     }
13692     if (ret == 0) {
13693         ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
13694                                             (word32)XSTRLEN(message) + 1);
13695         if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
13696             ret = WOLFSSL_FATAL_ERROR;
13697         }
13698     }
13699 
13700     /* Test bad args. Enc */
13701     if (ret == 0) {
13702         ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
13703                                     (word32)XSTRLEN(message) + 1);
13704         if (ret == BAD_FUNC_ARG) {
13705             ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
13706                                     (word32)XSTRLEN(message) + 1);
13707         }
13708         if (ret == BAD_FUNC_ARG) {
13709             ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
13710                                     (word32)XSTRLEN(message) + 1);
13711         }
13712         if (ret != BAD_FUNC_ARG) {
13713             ret = WOLFSSL_FATAL_ERROR;
13714         } else {
13715             ret = 0;
13716         }
13717     } /* END test bad args ENC  */
13718 
13719     /* Test bad args DEC */
13720     if (ret == 0) {
13721         ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
13722                                     (word32)XSTRLEN(message) + 1);
13723         if (ret == BAD_FUNC_ARG) {
13724             ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
13725                                     (word32)XSTRLEN(message) + 1);
13726         }
13727         if (ret == BAD_FUNC_ARG) {
13728             ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
13729                                     (word32)XSTRLEN(message) + 1);
13730         }
13731         if (ret != BAD_FUNC_ARG) {
13732             ret = WOLFSSL_FATAL_ERROR;
13733         } else {
13734             ret = 0;
13735         }
13736     }
13737 
13738     printf(resultFmt, ret == 0 ? passed : failed);
13739 
13740 #endif
13741     return ret;
13742 
13743 } /* END test_wc_IdeaCbcEncryptDecrypt */
13744 
13745 
13746 /*
13747  * Test function for wc_HmacSetKey
13748  */
13749 static int test_wc_Md5HmacSetKey (void)
13750 {
13751     int flag = 0;
13752 #if !defined(NO_HMAC) && !defined(NO_MD5)
13753     Hmac hmac;
13754     int ret,  times, itr;
13755 
13756     const char* keys[]=
13757     {
13758         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
13759 #ifndef HAVE_FIPS
13760         "Jefe", /* smaller than minimum FIPS key size */
13761 #endif
13762         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
13763     };
13764 
13765     times = sizeof(keys) / sizeof(char*);
13766     flag = 0;
13767 
13768     printf(testingFmt, "wc_HmacSetKey() with MD5");
13769 
13770     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
13771     if (ret != 0)
13772         return ret;
13773 
13774     for (itr = 0; itr < times; itr++) {
13775         ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
13776                             (word32)XSTRLEN(keys[itr]));
13777         if (ret != 0) {
13778             flag = ret;
13779         }
13780     }
13781 
13782     /* Bad args. */
13783     if (!flag) {
13784         ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
13785                                         (word32)XSTRLEN(keys[0]));
13786         if (ret != BAD_FUNC_ARG) {
13787             flag = WOLFSSL_FATAL_ERROR;
13788         }
13789     }
13790 
13791     if (!flag) {
13792         ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
13793         if (ret != BAD_FUNC_ARG) {
13794             flag = WOLFSSL_FATAL_ERROR;
13795         }
13796     }
13797 
13798     if (!flag) {
13799         ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
13800                                         (word32)XSTRLEN(keys[0]));
13801         if (ret != BAD_FUNC_ARG) {
13802             flag = WOLFSSL_FATAL_ERROR;
13803         }
13804     }
13805 
13806     if (!flag) {
13807         ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
13808 #ifdef HAVE_FIPS
13809         if (ret != HMAC_MIN_KEYLEN_E) {
13810             flag = WOLFSSL_FATAL_ERROR;
13811         }
13812 #else
13813         if (ret != 0) {
13814             flag = WOLFSSL_FATAL_ERROR;
13815         }
13816 #endif
13817     }
13818 
13819     wc_HmacFree(&hmac);
13820 
13821     printf(resultFmt, flag == 0 ? passed : failed);
13822 
13823 #endif
13824     return flag;
13825 } /* END test_wc_Md5HmacSetKey */
13826 
13827 
13828 /*
13829  * testing wc_HmacSetKey() on wc_Sha hash.
13830  */
13831 static int test_wc_ShaHmacSetKey (void)
13832 {
13833 
13834     int flag = 0;
13835 #if !defined(NO_HMAC) && !defined(NO_SHA)
13836     Hmac hmac;
13837     int ret, times, itr;
13838 
13839     const char* keys[]=
13840     {
13841         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
13842                                                                 "\x0b\x0b\x0b",
13843 #ifndef HAVE_FIPS
13844         "Jefe", /* smaller than minimum FIPS key size */
13845 #endif
13846         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
13847                                                                 "\xAA\xAA\xAA"
13848     };
13849 
13850     times = sizeof(keys) / sizeof(char*);
13851     flag = 0;
13852 
13853     printf(testingFmt, "wc_HmacSetKey() with SHA");
13854 
13855     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
13856     if (ret != 0)
13857         return ret;
13858 
13859     for (itr = 0; itr < times; itr++) {
13860         ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
13861                                         (word32)XSTRLEN(keys[itr]));
13862         if (ret != 0) {
13863             flag = ret;
13864         }
13865     }
13866 
13867     /* Bad args. */
13868     if (!flag) {
13869         ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
13870                                         (word32)XSTRLEN(keys[0]));
13871         if (ret != BAD_FUNC_ARG) {
13872             flag = WOLFSSL_FATAL_ERROR;
13873         }
13874     }
13875 
13876     if (!flag) {
13877         ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
13878         if (ret != BAD_FUNC_ARG) {
13879             flag = WOLFSSL_FATAL_ERROR;
13880         }
13881     }
13882 
13883     if (!flag) {
13884         ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
13885                                         (word32)XSTRLEN(keys[0]));
13886         if (ret != BAD_FUNC_ARG) {
13887             flag = WOLFSSL_FATAL_ERROR;
13888         }
13889     }
13890 
13891     if (!flag) {
13892         ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
13893 #ifdef HAVE_FIPS
13894         if (ret != HMAC_MIN_KEYLEN_E) {
13895             flag = WOLFSSL_FATAL_ERROR;
13896         }
13897 #else
13898         if (ret != 0) {
13899             flag = WOLFSSL_FATAL_ERROR;
13900         }
13901 #endif
13902     }
13903 
13904     wc_HmacFree(&hmac);
13905 
13906     printf(resultFmt, flag == 0 ? passed : failed);
13907 
13908 #endif
13909     return flag;
13910 } /* END test_wc_ShaHmacSetKey() */
13911 
13912 /*
13913  * testing wc_HmacSetKey() on Sha224 hash.
13914  */
13915 static int test_wc_Sha224HmacSetKey (void)
13916 {
13917 
13918     int flag = 0;
13919 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
13920     Hmac hmac;
13921     int ret, times, itr;
13922 
13923     const char* keys[]=
13924     {
13925         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
13926                                                                 "\x0b\x0b\x0b",
13927 #ifndef HAVE_FIPS
13928         "Jefe", /* smaller than minimum FIPS key size */
13929 #endif
13930         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
13931                                                                 "\xAA\xAA\xAA"
13932     };
13933 
13934     times = sizeof(keys) / sizeof(char*);
13935     flag = 0;
13936 
13937     printf(testingFmt, "wc_HmacSetKey() with SHA 224");
13938 
13939     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
13940     if (ret != 0)
13941         return ret;
13942 
13943     for (itr = 0; itr < times; itr++) {
13944         ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
13945                                             (word32)XSTRLEN(keys[itr]));
13946         if (ret != 0) {
13947             flag = ret;
13948         }
13949     }
13950 
13951     /* Bad args. */
13952     if (!flag) {
13953         ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
13954                                             (word32)XSTRLEN(keys[0]));
13955         if (ret != BAD_FUNC_ARG) {
13956             flag = WOLFSSL_FATAL_ERROR;
13957         }
13958     }
13959 
13960     if (!flag) {
13961         ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
13962         if (ret != BAD_FUNC_ARG) {
13963             flag = WOLFSSL_FATAL_ERROR;
13964         }
13965     }
13966 
13967     if (!flag) {
13968         ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
13969                                             (word32)XSTRLEN(keys[0]));
13970         if (ret != BAD_FUNC_ARG) {
13971             flag = WOLFSSL_FATAL_ERROR;
13972         }
13973     }
13974 
13975     if (!flag) {
13976         ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
13977 #ifdef HAVE_FIPS
13978         if (ret != HMAC_MIN_KEYLEN_E) {
13979             flag = WOLFSSL_FATAL_ERROR;
13980         }
13981 #else
13982         if (ret != 0) {
13983             flag = WOLFSSL_FATAL_ERROR;
13984         }
13985 #endif
13986     }
13987 
13988     wc_HmacFree(&hmac);
13989 
13990     printf(resultFmt, flag == 0 ? passed : failed);
13991 
13992 #endif
13993     return flag;
13994 } /* END test_wc_Sha224HmacSetKey() */
13995 
13996  /*
13997   * testing wc_HmacSetKey() on Sha256 hash
13998   */
13999 static int test_wc_Sha256HmacSetKey (void)
14000 {
14001 
14002     int flag = 0;
14003 #if !defined(NO_HMAC) && !defined(NO_SHA256)
14004     Hmac hmac;
14005     int ret, times, itr;
14006 
14007     const char* keys[]=
14008     {
14009         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14010                                                                 "\x0b\x0b\x0b",
14011 #ifndef HAVE_FIPS
14012         "Jefe", /* smaller than minimum FIPS key size */
14013 #endif
14014         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
14015                                                                 "\xAA\xAA\xAA"
14016     };
14017 
14018     times = sizeof(keys) / sizeof(char*);
14019     flag = 0;
14020 
14021     printf(testingFmt, "wc_HmacSetKey() with SHA256");
14022 
14023     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14024     if (ret != 0)
14025         return ret;
14026 
14027     for (itr = 0; itr < times; itr++) {
14028         ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
14029                                             (word32)XSTRLEN(keys[itr]));
14030         if (ret != 0) {
14031             flag = ret;
14032         }
14033     }
14034 
14035     /* Bad args. */
14036     if (!flag) {
14037         ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
14038                                             (word32)XSTRLEN(keys[0]));
14039         if (ret != BAD_FUNC_ARG) {
14040             flag = WOLFSSL_FATAL_ERROR;
14041         }
14042     }
14043 
14044     if (!flag) {
14045         ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
14046         if (ret != BAD_FUNC_ARG) {
14047             flag = WOLFSSL_FATAL_ERROR;
14048         }
14049     }
14050 
14051     if (!flag) {
14052         ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
14053                                             (word32)XSTRLEN(keys[0]));
14054         if (ret != BAD_FUNC_ARG) {
14055             flag = WOLFSSL_FATAL_ERROR;
14056         }
14057     }
14058 
14059     if (!flag) {
14060         ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
14061 #ifdef HAVE_FIPS
14062         if (ret != HMAC_MIN_KEYLEN_E) {
14063             flag = WOLFSSL_FATAL_ERROR;
14064         }
14065 #else
14066         if (ret != 0) {
14067             flag = WOLFSSL_FATAL_ERROR;
14068         }
14069 #endif
14070     }
14071 
14072     wc_HmacFree(&hmac);
14073 
14074     printf(resultFmt, flag == 0 ? passed : failed);
14075 
14076 #endif
14077     return flag;
14078 } /* END test_wc_Sha256HmacSetKey() */
14079 
14080 
14081 /*
14082  * testing wc_HmacSetKey on Sha384 hash.
14083  */
14084 static int test_wc_Sha384HmacSetKey (void)
14085 {
14086     int flag = 0;
14087 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
14088     Hmac hmac;
14089     int ret, times, itr;
14090 
14091     const char* keys[]=
14092     {
14093         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14094                                                                 "\x0b\x0b\x0b",
14095 #ifndef HAVE_FIPS
14096         "Jefe", /* smaller than minimum FIPS key size */
14097 #endif
14098         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
14099                                                                 "\xAA\xAA\xAA"
14100     };
14101 
14102     times = sizeof(keys) / sizeof(char*);
14103     flag = 0;
14104 
14105     printf(testingFmt, "wc_HmacSetKey() with SHA384");
14106 
14107     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14108     if (ret != 0)
14109         return ret;
14110 
14111     for (itr = 0; itr < times; itr++) {
14112         ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
14113                                             (word32)XSTRLEN(keys[itr]));
14114         if (ret != 0) {
14115             flag = ret;
14116         }
14117     }
14118 
14119     /* Bad args. */
14120     if (!flag) {
14121         ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
14122                                             (word32)XSTRLEN(keys[0]));
14123         if (ret != BAD_FUNC_ARG) {
14124             flag = WOLFSSL_FATAL_ERROR;
14125         }
14126     }
14127 
14128     if (!flag) {
14129         ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
14130         if (ret != BAD_FUNC_ARG) {
14131             flag = WOLFSSL_FATAL_ERROR;
14132         }
14133     }
14134 
14135     if (!flag) {
14136         ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
14137                                             (word32)XSTRLEN(keys[0]));
14138         if (ret != BAD_FUNC_ARG) {
14139             flag = WOLFSSL_FATAL_ERROR;
14140         }
14141     }
14142 
14143     if (!flag) {
14144         ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
14145 #ifdef HAVE_FIPS
14146         if (ret != HMAC_MIN_KEYLEN_E) {
14147             flag = WOLFSSL_FATAL_ERROR;
14148         }
14149 #else
14150         if (ret != 0) {
14151             flag = WOLFSSL_FATAL_ERROR;
14152         }
14153 #endif
14154     }
14155 
14156     wc_HmacFree(&hmac);
14157 
14158     printf(resultFmt, flag == 0 ? passed : failed);
14159 
14160 #endif
14161     return flag;
14162 } /* END test_wc_Sha384HmacSetKey() */
14163 
14164 
14165 /*
14166  * testing wc_HmacUpdate on wc_Md5 hash.
14167  */
14168 static int test_wc_Md5HmacUpdate (void)
14169 {
14170     int flag = 0;
14171 #if !defined(NO_HMAC) && !defined(NO_MD5)
14172     Hmac hmac;
14173     testVector a, b;
14174     int ret;
14175 #ifdef HAVE_FIPS
14176     const char* keys =
14177         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14178 #else
14179     const char* keys = "Jefe";
14180 #endif
14181 
14182     a.input = "what do ya want for nothing?";
14183     a.inLen  = XSTRLEN(a.input);
14184 
14185     b.input = "Hi There";
14186     b.inLen = XSTRLEN(b.input);
14187 
14188     flag = 0;
14189 
14190     printf(testingFmt, "wc_HmacUpdate() with MD5");
14191 
14192     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14193     if (ret != 0)
14194         return ret;
14195 
14196     ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
14197     if (ret != 0) {
14198         flag = ret;
14199     }
14200 
14201     if (!flag) {
14202         ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
14203         if (ret != 0) {
14204             flag = ret;
14205         }
14206     }
14207     /* Update Hmac. */
14208     if (!flag) {
14209         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14210         if (ret != 0) {
14211             flag = ret;
14212         }
14213     }
14214 
14215     /* Test bad args. */
14216     if (!flag) {
14217         ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
14218         if (ret != BAD_FUNC_ARG) {
14219             flag = WOLFSSL_FATAL_ERROR;
14220         }
14221     }
14222 
14223     if (!flag) {
14224         ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
14225         if (ret != BAD_FUNC_ARG) {
14226             flag = WOLFSSL_FATAL_ERROR;
14227         }
14228     }
14229 
14230     if (!flag) {
14231         ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
14232         if (ret != 0) {
14233             flag = ret;
14234         }
14235     }
14236 
14237     wc_HmacFree(&hmac);
14238 
14239     printf(resultFmt, flag == 0 ? passed : failed);
14240 
14241 #endif
14242     return flag;
14243 } /* END test_wc_Md5HmacUpdate */
14244 
14245 /*
14246  * testing wc_HmacUpdate on SHA hash.
14247  */
14248 static int test_wc_ShaHmacUpdate (void)
14249 {
14250     int flag = 0;
14251 #if !defined(NO_HMAC) && !defined(NO_SHA)
14252     Hmac hmac;
14253     testVector a, b;
14254     int ret;
14255 #ifdef HAVE_FIPS
14256     const char* keys =
14257         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14258 #else
14259     const char* keys = "Jefe";
14260 #endif
14261 
14262     a.input = "what do ya want for nothing?";
14263     a.inLen  = XSTRLEN(a.input);
14264 
14265     b.input = "Hi There";
14266     b.inLen = XSTRLEN(b.input);
14267 
14268     flag = 0;
14269 
14270     printf(testingFmt, "wc_HmacUpdate() with SHA");
14271 
14272     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14273     if (ret != 0)
14274         return ret;
14275 
14276     ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
14277     if (ret != 0) {
14278         flag = ret;
14279     }
14280 
14281     if (!flag) {
14282         ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
14283         if (ret != 0) {
14284             flag = ret;
14285         }
14286     }
14287     /* Update Hmac. */
14288     if (!flag) {
14289         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14290         if (ret != 0) {
14291             flag = ret;
14292         }
14293     }
14294 
14295     /* Test bad args. */
14296     if (!flag) {
14297         ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
14298         if (ret != BAD_FUNC_ARG) {
14299             flag = WOLFSSL_FATAL_ERROR;
14300         }
14301     }
14302 
14303     if (!flag) {
14304         ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
14305         if (ret != BAD_FUNC_ARG) {
14306             flag = WOLFSSL_FATAL_ERROR;
14307         }
14308     }
14309 
14310     if (!flag) {
14311         ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
14312         if (ret != 0) {
14313             flag = ret;
14314         }
14315     }
14316 
14317     wc_HmacFree(&hmac);
14318 
14319     printf(resultFmt, flag == 0 ? passed : failed);
14320 
14321 #endif
14322     return flag;
14323 } /* END test_wc_ShaHmacUpdate */
14324 
14325 /*
14326  * testing wc_HmacUpdate on SHA224 hash.
14327  */
14328 static int test_wc_Sha224HmacUpdate (void)
14329 {
14330     int flag = 0;
14331 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
14332     Hmac hmac;
14333     testVector a, b;
14334     int ret;
14335 #ifdef HAVE_FIPS
14336     const char* keys =
14337         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14338 #else
14339     const char* keys = "Jefe";
14340 #endif
14341 
14342     a.input = "what do ya want for nothing?";
14343     a.inLen  = XSTRLEN(a.input);
14344 
14345     b.input = "Hi There";
14346     b.inLen = XSTRLEN(b.input);
14347 
14348     flag = 0;
14349 
14350     printf(testingFmt, "wc_HmacUpdate() with SHA224");
14351 
14352     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14353     if (ret != 0)
14354         return ret;
14355 
14356     ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
14357     if (ret != 0) {
14358         flag = ret;
14359     }
14360 
14361     if (!flag) {
14362         ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
14363         if (ret != 0) {
14364             flag = ret;
14365         }
14366     }
14367     /* Update Hmac. */
14368     if (!flag) {
14369         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14370         if (ret != 0) {
14371             flag = ret;
14372         }
14373     }
14374 
14375     /* Test bad args. */
14376     if (!flag) {
14377         ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
14378         if (ret != BAD_FUNC_ARG) {
14379             flag = WOLFSSL_FATAL_ERROR;
14380         }
14381     }
14382 
14383     if (!flag) {
14384         ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
14385         if (ret != BAD_FUNC_ARG) {
14386             flag = WOLFSSL_FATAL_ERROR;
14387         }
14388     }
14389 
14390     if (!flag) {
14391         ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
14392         if (ret != 0) {
14393             flag = ret;
14394         }
14395     }
14396 
14397     wc_HmacFree(&hmac);
14398 
14399     printf(resultFmt, flag == 0 ? passed : failed);
14400 
14401 #endif
14402     return flag;
14403 } /* END test_wc_Sha224HmacUpdate */
14404 
14405 /*
14406  * testing wc_HmacUpdate on SHA256 hash.
14407  */
14408 static int test_wc_Sha256HmacUpdate (void)
14409 {
14410     int flag = 0;
14411 #if !defined(NO_HMAC) && !defined(NO_SHA256)
14412     Hmac hmac;
14413     testVector a, b;
14414     int ret;
14415 #ifdef HAVE_FIPS
14416     const char* keys =
14417         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14418 #else
14419     const char* keys = "Jefe";
14420 #endif
14421 
14422     a.input = "what do ya want for nothing?";
14423     a.inLen  = XSTRLEN(a.input);
14424 
14425     b.input = "Hi There";
14426     b.inLen = XSTRLEN(b.input);
14427 
14428     flag = 0;
14429 
14430     printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
14431 
14432     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14433     if (ret != 0)
14434         return ret;
14435 
14436     ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
14437     if (ret != 0) {
14438         flag = ret;
14439     }
14440 
14441     if (!flag) {
14442         ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
14443         if (ret != 0) {
14444             flag = ret;
14445         }
14446     }
14447     /* Update Hmac. */
14448     if (!flag) {
14449         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14450         if (ret != 0) {
14451             flag = ret;
14452         }
14453     }
14454 
14455     /* Test bad args. */
14456     if (!flag) {
14457         ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
14458         if (ret != BAD_FUNC_ARG) {
14459             flag = WOLFSSL_FATAL_ERROR;
14460         }
14461     }
14462 
14463     if (!flag) {
14464         ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
14465         if (ret != BAD_FUNC_ARG) {
14466             flag = WOLFSSL_FATAL_ERROR;
14467         }
14468     }
14469 
14470     if (!flag) {
14471         ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
14472         if (ret != 0) {
14473             flag = ret;
14474         }
14475     }
14476 
14477     wc_HmacFree(&hmac);
14478 
14479     printf(resultFmt, flag == 0 ? passed : failed);
14480 
14481 #endif
14482     return flag;
14483 } /* END test_wc_Sha256HmacUpdate */
14484 
14485 /*
14486  * testing wc_HmacUpdate on SHA384  hash.
14487  */
14488 static int test_wc_Sha384HmacUpdate (void)
14489 {
14490     int flag = 0;
14491 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
14492     Hmac hmac;
14493     testVector a, b;
14494     int ret;
14495 #ifdef HAVE_FIPS
14496     const char* keys =
14497         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14498 #else
14499     const char* keys = "Jefe";
14500 #endif
14501 
14502     a.input = "what do ya want for nothing?";
14503     a.inLen  = XSTRLEN(a.input);
14504 
14505     b.input = "Hi There";
14506     b.inLen = XSTRLEN(b.input);
14507 
14508     flag = 0;
14509 
14510     printf(testingFmt, "wc_HmacUpdate() with SHA384");
14511 
14512     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14513     if (ret != 0)
14514         return ret;
14515 
14516     ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
14517     if (ret != 0) {
14518         flag = ret;
14519     }
14520 
14521     if (!flag) {
14522         ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
14523         if (ret != 0) {
14524             flag = ret;
14525         }
14526     }
14527     /* Update Hmac. */
14528     if (!flag) {
14529         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14530         if (ret != 0) {
14531             flag = ret;
14532         }
14533     }
14534 
14535     /* Test bad args. */
14536     if (!flag) {
14537         ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
14538         if (ret != BAD_FUNC_ARG) {
14539             flag = WOLFSSL_FATAL_ERROR;
14540         }
14541     }
14542 
14543     if (!flag) {
14544         ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
14545         if (ret != BAD_FUNC_ARG) {
14546             flag = WOLFSSL_FATAL_ERROR;
14547         }
14548     }
14549 
14550     if (!flag) {
14551         ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
14552         if (ret != 0) {
14553             flag = ret;
14554         }
14555     }
14556 
14557     wc_HmacFree(&hmac);
14558 
14559     printf(resultFmt, flag == 0 ? passed : failed);
14560 
14561 #endif
14562     return flag;
14563 } /* END test_wc_Sha384HmacUpdate */
14564 
14565 /*
14566  * Testing wc_HmacFinal() with MD5
14567  */
14568 
14569 static int test_wc_Md5HmacFinal (void)
14570 {
14571     int flag = 0;
14572 #if !defined(NO_HMAC) && !defined(NO_MD5)
14573     Hmac hmac;
14574     byte hash[WC_MD5_DIGEST_SIZE];
14575     testVector a;
14576     int ret;
14577     const char* key;
14578 
14579     key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
14580     a.input = "Hi There";
14581     a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
14582                "\x9d";
14583     a.inLen  = XSTRLEN(a.input);
14584     a.outLen = XSTRLEN(a.output);
14585 
14586     flag = 0;
14587 
14588     printf(testingFmt, "wc_HmacFinal() with MD5");
14589 
14590     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14591     if (ret != 0)
14592         return ret;
14593 
14594     ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
14595     if (ret != 0) {
14596         flag = ret;
14597     }
14598 
14599     if (!flag) {
14600         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14601         if (ret != 0) {
14602             flag = ret;
14603         }
14604     }
14605 
14606     if (!flag) {
14607         ret = wc_HmacFinal(&hmac, hash);
14608         if (ret != 0) {
14609             flag = ret;
14610         }
14611     }
14612 
14613     if (!flag) {
14614         if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
14615             flag = WOLFSSL_FATAL_ERROR;
14616         }
14617     }
14618 
14619     /* Try bad parameters. */
14620     if (!flag) {
14621         ret = wc_HmacFinal(NULL, hash);
14622         if (ret != BAD_FUNC_ARG) {
14623             flag = WOLFSSL_FATAL_ERROR;
14624         }
14625     }
14626 
14627 #ifndef HAVE_FIPS
14628     if (!flag) {
14629         ret = wc_HmacFinal(&hmac, NULL);
14630         if (ret != BAD_FUNC_ARG) {
14631             flag = WOLFSSL_FATAL_ERROR;
14632         }
14633     }
14634 #endif
14635 
14636     wc_HmacFree(&hmac);
14637 
14638     printf(resultFmt, flag == 0 ? passed : failed);
14639 
14640 #endif
14641     return flag;
14642 
14643 } /* END test_wc_Md5HmacFinal */
14644 
14645 /*
14646  * Testing wc_HmacFinal() with SHA
14647  */
14648 static int test_wc_ShaHmacFinal (void)
14649 {
14650     int flag = 0;
14651 #if !defined(NO_HMAC) && !defined(NO_SHA)
14652     Hmac hmac;
14653     byte hash[WC_SHA_DIGEST_SIZE];
14654     testVector a;
14655     int ret;
14656     const char* key;
14657 
14658     key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14659                                                                 "\x0b\x0b\x0b";
14660     a.input = "Hi There";
14661     a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
14662                "\x8e\xf1\x46\xbe\x00";
14663     a.inLen  = XSTRLEN(a.input);
14664     a.outLen = XSTRLEN(a.output);
14665 
14666     flag = 0;
14667 
14668     printf(testingFmt, "wc_HmacFinal() with SHA");
14669 
14670     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14671     if (ret != 0)
14672         return ret;
14673 
14674     ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
14675     if (ret != 0) {
14676         flag = ret;
14677     }
14678 
14679     if (!flag) {
14680         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14681         if (ret != 0) {
14682             flag = ret;
14683         }
14684     }
14685 
14686     if (!flag) {
14687         ret = wc_HmacFinal(&hmac, hash);
14688         if (ret != 0) {
14689             flag = ret;
14690         }
14691     }
14692 
14693     if (!flag) {
14694         if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
14695             flag = WOLFSSL_FATAL_ERROR;
14696         }
14697     }
14698 
14699     /* Try bad parameters. */
14700     if (!flag) {
14701         ret = wc_HmacFinal(NULL, hash);
14702         if (ret != BAD_FUNC_ARG) {
14703             flag = WOLFSSL_FATAL_ERROR;
14704         }
14705     }
14706 
14707 #ifndef HAVE_FIPS
14708     if (!flag) {
14709         ret = wc_HmacFinal(&hmac, NULL);
14710         if (ret != BAD_FUNC_ARG) {
14711             flag = WOLFSSL_FATAL_ERROR;
14712         }
14713     }
14714 #endif
14715 
14716     wc_HmacFree(&hmac);
14717 
14718     printf(resultFmt, flag == 0 ? passed : failed);
14719 
14720 #endif
14721     return flag;
14722 
14723 } /* END test_wc_ShaHmacFinal */
14724 
14725 
14726 /*
14727  * Testing wc_HmacFinal() with SHA224
14728  */
14729 static int test_wc_Sha224HmacFinal (void)
14730 {
14731     int flag = 0;
14732 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
14733     Hmac hmac;
14734     byte hash[WC_SHA224_DIGEST_SIZE];
14735     testVector a;
14736     int ret;
14737     const char* key;
14738 
14739     key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14740                                                                 "\x0b\x0b\x0b";
14741     a.input = "Hi There";
14742     a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
14743                "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
14744     a.inLen  = XSTRLEN(a.input);
14745     a.outLen = XSTRLEN(a.output);
14746 
14747     flag = 0;
14748 
14749     printf(testingFmt, "wc_HmacFinal() with SHA224");
14750 
14751     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14752     if (ret != 0)
14753         return ret;
14754 
14755     ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
14756     if (ret != 0) {
14757         flag = ret;
14758     }
14759 
14760     if (!flag) {
14761         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14762         if (ret != 0) {
14763             flag = ret;
14764         }
14765     }
14766 
14767     if (!flag) {
14768         ret = wc_HmacFinal(&hmac, hash);
14769         if (ret != 0) {
14770             flag = ret;
14771         }
14772     }
14773 
14774     if (!flag) {
14775         if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
14776             flag = WOLFSSL_FATAL_ERROR;
14777         }
14778     }
14779 
14780     /* Try bad parameters. */
14781     if (!flag) {
14782         ret = wc_HmacFinal(NULL, hash);
14783         if (ret != BAD_FUNC_ARG) {
14784             flag = WOLFSSL_FATAL_ERROR;
14785         }
14786     }
14787 
14788 #ifndef HAVE_FIPS
14789     if (!flag) {
14790         ret = wc_HmacFinal(&hmac, NULL);
14791         if (ret != BAD_FUNC_ARG) {
14792             flag = WOLFSSL_FATAL_ERROR;
14793         }
14794     }
14795 #endif
14796 
14797     wc_HmacFree(&hmac);
14798 
14799     printf(resultFmt, flag == 0 ? passed : failed);
14800 
14801 #endif
14802     return flag;
14803 } /* END test_wc_Sha224HmacFinal */
14804 
14805 /*
14806  * Testing wc_HmacFinal() with SHA256
14807  */
14808 static int test_wc_Sha256HmacFinal (void)
14809 {
14810     int flag = 0;
14811 #if !defined(NO_HMAC) && !defined(NO_SHA256)
14812     Hmac hmac;
14813     byte hash[WC_SHA256_DIGEST_SIZE];
14814     testVector a;
14815     int ret;
14816     const char* key;
14817 
14818     key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14819                                                                 "\x0b\x0b\x0b";
14820     a.input = "Hi There";
14821     a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
14822                "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
14823                "\xcf\xf7";
14824     a.inLen  = XSTRLEN(a.input);
14825     a.outLen = XSTRLEN(a.output);
14826 
14827     flag = 0;
14828 
14829     printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
14830 
14831     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14832     if (ret != 0)
14833         return ret;
14834 
14835     ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
14836     if (ret != 0) {
14837         flag = ret;
14838     }
14839 
14840     if (!flag) {
14841         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14842         if (ret != 0) {
14843             flag = ret;
14844         }
14845     }
14846 
14847     if (!flag) {
14848         ret = wc_HmacFinal(&hmac, hash);
14849         if (ret != 0) {
14850             flag = ret;
14851         }
14852     }
14853 
14854     if (!flag) {
14855         if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
14856             flag = WOLFSSL_FATAL_ERROR;
14857         }
14858     }
14859 
14860     /* Try bad parameters. */
14861     if (!flag) {
14862         ret = wc_HmacFinal(NULL, hash);
14863         if (ret != BAD_FUNC_ARG) {
14864             flag = WOLFSSL_FATAL_ERROR;
14865         }
14866     }
14867 
14868 #ifndef HAVE_FIPS
14869     if (!flag) {
14870         ret = wc_HmacFinal(&hmac, NULL);
14871         if (ret != BAD_FUNC_ARG) {
14872             flag = WOLFSSL_FATAL_ERROR;
14873         }
14874     }
14875 #endif
14876 
14877     wc_HmacFree(&hmac);
14878 
14879     printf(resultFmt, flag == 0 ? passed : failed);
14880 
14881 #endif
14882     return flag;
14883 } /* END test_wc_Sha256HmacFinal */
14884 
14885 /*
14886  * Testing wc_HmacFinal() with SHA384
14887  */
14888 static int test_wc_Sha384HmacFinal (void)
14889 {
14890     int flag = 0;
14891 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
14892     Hmac hmac;
14893     byte hash[WC_SHA384_DIGEST_SIZE];
14894     testVector a;
14895     int ret;
14896     const char* key;
14897 
14898     key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
14899                                                                 "\x0b\x0b\x0b";
14900     a.input = "Hi There";
14901     a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
14902                "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
14903                "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
14904                "\xfa\x9c\xb6";
14905     a.inLen  = XSTRLEN(a.input);
14906     a.outLen = XSTRLEN(a.output);
14907 
14908     flag = 0;
14909 
14910     printf(testingFmt, "wc_HmacFinal() with SHA384");
14911 
14912     ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
14913     if (ret != 0)
14914         return ret;
14915 
14916     ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
14917     if (ret != 0) {
14918         flag = ret;
14919     }
14920 
14921     if (!flag) {
14922         ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
14923         if (ret != 0) {
14924             flag = ret;
14925         }
14926     }
14927 
14928     if (!flag) {
14929         ret = wc_HmacFinal(&hmac, hash);
14930         if (ret != 0) {
14931             flag = ret;
14932         }
14933     }
14934 
14935     if (!flag) {
14936         if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
14937             flag = WOLFSSL_FATAL_ERROR;
14938         }
14939     }
14940 
14941     /* Try bad parameters. */
14942     if (!flag) {
14943         ret = wc_HmacFinal(NULL, hash);
14944         if (ret != BAD_FUNC_ARG) {
14945             flag = WOLFSSL_FATAL_ERROR;
14946         }
14947     }
14948 #ifndef HAVE_FIPS
14949     if (!flag) {
14950         ret = wc_HmacFinal(&hmac, NULL);
14951         if (ret != BAD_FUNC_ARG) {
14952             flag = WOLFSSL_FATAL_ERROR;
14953         }
14954     }
14955 #endif
14956 
14957     wc_HmacFree(&hmac);
14958 
14959     printf(resultFmt, flag == 0 ? passed : failed);
14960 
14961 #endif
14962     return flag;
14963 } /* END test_wc_Sha384HmacFinal */
14964 
14965 
14966 
14967 /*
14968  * Testing wc_InitCmac()
14969  */
14970 static int test_wc_InitCmac (void)
14971 {
14972     int         ret = 0;
14973 
14974 #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
14975     Cmac        cmac1, cmac2, cmac3;
14976     /* AES 128 key. */
14977     byte        key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
14978                          "\x09\x10\x11\x12\x13\x14\x15\x16";
14979     /* AES 192 key. */
14980     byte        key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
14981                          "\x09\x01\x11\x12\x13\x14\x15\x16"
14982                          "\x01\x02\x03\x04\x05\x06\x07\x08";
14983 
14984     /* AES 256 key. */
14985     byte        key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
14986                          "\x09\x01\x11\x12\x13\x14\x15\x16"
14987                          "\x01\x02\x03\x04\x05\x06\x07\x08"
14988                          "\x09\x01\x11\x12\x13\x14\x15\x16";
14989 
14990     word32      key1Sz = (word32)sizeof(key1) - 1;
14991     word32      key2Sz = (word32)sizeof(key2) - 1;
14992     word32      key3Sz = (word32)sizeof(key3) - 1;
14993     int         type   = WC_CMAC_AES;
14994 
14995     printf(testingFmt, "wc_InitCmac()");
14996 
14997 #ifdef WOLFSSL_AES_128
14998     ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
14999 #endif
15000 #ifdef WOLFSSL_AES_192
15001     if (ret == 0)
15002         ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
15003 #endif
15004 #ifdef WOLFSSL_AES_256
15005     if (ret == 0)
15006         ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
15007 #endif
15008 
15009     /* Test bad args. */
15010     if (ret == 0) {
15011         ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
15012         if (ret == BAD_FUNC_ARG) {
15013             ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
15014         }
15015         if (ret == BAD_FUNC_ARG) {
15016             ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
15017         }
15018         if (ret == BAD_FUNC_ARG) {
15019             ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
15020         }
15021         if (ret == BAD_FUNC_ARG) {
15022             ret = 0;
15023         } else {
15024             ret = WOLFSSL_FATAL_ERROR;
15025         }
15026     }
15027 
15028     (void)key1;
15029     (void)key1Sz;
15030     (void)key2;
15031     (void)key2Sz;
15032     (void)cmac1;
15033     (void)cmac2;
15034 
15035     printf(resultFmt, ret == 0 ? passed : failed);
15036 
15037 #endif
15038     return ret;
15039 
15040 } /* END test_wc_InitCmac */
15041 
15042 
15043 /*
15044  * Testing wc_CmacUpdate()
15045  */
15046 static int test_wc_CmacUpdate (void)
15047 {
15048     int         ret = 0;
15049 
15050 #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
15051     Cmac        cmac;
15052     byte        key[] =
15053     {
15054         0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
15055         0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
15056     };
15057     byte        in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
15058                        "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
15059                        "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
15060                        "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
15061                        "\xf4";
15062 
15063     word32      inSz  = (word32)sizeof(in) - 1;
15064     word32      keySz = (word32)sizeof(key);
15065     int         type  = WC_CMAC_AES;
15066 
15067     ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
15068     if (ret != 0) {
15069         return ret;
15070     }
15071 
15072     printf(testingFmt, "wc_CmacUpdate()");
15073 
15074     ret = wc_CmacUpdate(&cmac, in, inSz);
15075 
15076     /* Test bad args. */
15077     if (ret == 0) {
15078         ret = wc_CmacUpdate(NULL, in, inSz);
15079         if (ret == BAD_FUNC_ARG) {
15080             ret = wc_CmacUpdate(&cmac, NULL, 30);
15081         }
15082         if (ret == BAD_FUNC_ARG) {
15083             ret = 0;
15084         } else if (ret == 0) {
15085             ret = WOLFSSL_FATAL_ERROR;
15086         }
15087     }
15088 
15089     printf(resultFmt, ret == 0 ? passed : failed);
15090 
15091 #endif
15092     return ret;
15093 
15094 } /* END test_wc_CmacUpdate */
15095 
15096 
15097 /*
15098  * Testing wc_CmacFinal()
15099  */
15100 static int test_wc_CmacFinal (void)
15101 {
15102     int         ret = 0;
15103 
15104 #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
15105     Cmac        cmac;
15106     byte        key[] =
15107     {
15108         0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
15109         0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
15110     };
15111     byte        msg[] =
15112     {
15113         0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
15114         0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
15115         0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
15116         0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
15117         0xf4
15118     };
15119     /* Test vectors from CMACGenAES128.rsp from
15120      * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
15121      * Per RFC4493 truncation of lsb is possible.
15122      */
15123     byte        expMac[] =
15124     {
15125         0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
15126         0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
15127     };
15128     byte        mac[AES_BLOCK_SIZE];
15129     word32      msgSz    = (word32)sizeof(msg);
15130     word32      keySz    = (word32)sizeof(key);
15131     word32      macSz    = sizeof(mac);
15132     word32      badMacSz = 17;
15133     int         expMacSz = sizeof(expMac);
15134     int         type     = WC_CMAC_AES;
15135 
15136     XMEMSET(mac, 0, macSz);
15137 
15138     ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
15139     if (ret != 0) {
15140         return ret;
15141     }
15142     ret = wc_CmacUpdate(&cmac, msg, msgSz);
15143 
15144     printf(testingFmt, "wc_CmacFinal()");
15145     if (ret == 0) {
15146         ret = wc_CmacFinal(&cmac, mac, &macSz);
15147         if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
15148             ret = WOLFSSL_FATAL_ERROR;
15149         }
15150         /* Pass in bad args. */
15151         if (ret == 0) {
15152             ret = wc_CmacFinal(NULL, mac, &macSz);
15153             if (ret == BAD_FUNC_ARG) {
15154                 ret = wc_CmacFinal(&cmac, NULL, &macSz);
15155             }
15156             if (ret == BAD_FUNC_ARG) {
15157                 ret = wc_CmacFinal(&cmac, mac, &badMacSz);
15158                 if (ret == BUFFER_E) {
15159                     ret = 0;
15160                 }
15161             } else if (ret == 0) {
15162                 ret = WOLFSSL_FATAL_ERROR;
15163             }
15164         }
15165     }
15166     printf(resultFmt, ret == 0 ? passed : failed);
15167 
15168 #endif
15169     return ret;
15170 
15171 } /* END test_wc_CmacFinal */
15172 
15173 
15174 /*
15175  * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
15176  */
15177 static int test_wc_AesCmacGenerate (void)
15178 {
15179     int         ret = 0;
15180 #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
15181     Cmac        cmac;
15182     byte        key[] =
15183     {
15184         0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
15185         0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
15186     };
15187     byte        msg[]    = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
15188                            "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
15189     byte        expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
15190                            "\x3d\x32\x65\x4c\x66\x23\xc5";
15191     byte        mac[AES_BLOCK_SIZE];
15192     word32      keySz    = sizeof(key);
15193     word32      macSz    = sizeof(mac);
15194     word32      msgSz    = sizeof(msg) - 1;
15195     word32      expMacSz = sizeof(expMac) - 1;
15196     int         type     = WC_CMAC_AES;
15197 
15198     XMEMSET(mac, 0, macSz);
15199 
15200     ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
15201     if (ret != 0) {
15202         return ret;
15203     }
15204 
15205     ret = wc_CmacUpdate(&cmac, msg, msgSz);
15206     if (ret != 0) {
15207         return ret;
15208     }
15209 
15210     printf(testingFmt, "wc_AesCmacGenerate()");
15211 
15212     ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
15213     if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
15214         ret = WOLFSSL_FATAL_ERROR;
15215     }
15216     /* Pass in bad args. */
15217     if (ret == 0) {
15218         ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
15219         if (ret == BAD_FUNC_ARG) {
15220             ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
15221         }
15222         if (ret == BAD_FUNC_ARG) {
15223             ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
15224         }
15225         if (ret == BAD_FUNC_ARG) {
15226             ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
15227         }
15228         if (ret == BAD_FUNC_ARG) {
15229             ret = 0;
15230         } else if (ret == 0) {
15231             ret = WOLFSSL_FATAL_ERROR;
15232         }
15233     }
15234     printf(resultFmt, ret == 0 ? passed : failed);
15235 
15236     if (ret == 0) {
15237         printf(testingFmt, "wc_AesCmacVerify()");
15238 
15239         ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
15240         /* Test bad args. */
15241         if (ret == 0) {
15242             ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
15243             if (ret == BAD_FUNC_ARG) {
15244                 ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
15245             }
15246             if (ret == BAD_FUNC_ARG) {
15247                 ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
15248             }
15249             if (ret == BAD_FUNC_ARG) {
15250                 ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
15251             }
15252             if (ret == BAD_FUNC_ARG) {
15253                 ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
15254             }
15255             if (ret == BAD_FUNC_ARG) {
15256                 ret = 0;
15257             } else if (ret == 0) {
15258                 ret = WOLFSSL_FATAL_ERROR;
15259             }
15260         }
15261 
15262         printf(resultFmt, ret == 0 ? passed : failed);
15263     }
15264 
15265 #endif
15266     return ret;
15267 
15268 } /* END test_wc_AesCmacGenerate */
15269 
15270 
15271 /*
15272  * Testing streaming AES-GCM API.
15273  */
15274 static int test_wc_AesGcmStream (void)
15275 {
15276     int ret = 0;
15277 #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
15278     defined(WOLFSSL_AESGCM_STREAM)
15279 
15280     int i;
15281     WC_RNG rng[1];
15282     Aes aesEnc[1];
15283     Aes aesDec[1];
15284     byte tag[AES_BLOCK_SIZE];
15285     byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
15286     byte out[AES_BLOCK_SIZE * 3 + 2];
15287     byte plain[AES_BLOCK_SIZE * 3 + 2];
15288     byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
15289     byte key[AES_128_KEY_SIZE] = { 0, };
15290     byte iv[AES_IV_SIZE] = { 1, };
15291     byte ivOut[AES_IV_SIZE];
15292     static const byte expTagAAD1[AES_BLOCK_SIZE] = {
15293         0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
15294         0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
15295     };
15296     static const byte expTagPlain1[AES_BLOCK_SIZE] = {
15297         0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
15298         0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
15299     };
15300     static const byte expTag[AES_BLOCK_SIZE] = {
15301         0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
15302         0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
15303     };
15304 
15305     /* Create a random for generating IV/nonce. */
15306     AssertIntEQ(wc_InitRng(rng), 0);
15307 
15308     /* Initialize data structures. */
15309     AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
15310     AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
15311 
15312     /* BadParameters to streaming init. */
15313     AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
15314     AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
15315     AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
15316                 BAD_FUNC_ARG);
15317     AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
15318                 BAD_FUNC_ARG);
15319 
15320     /* Bad parameters to encrypt update. */
15321     AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
15322                 BAD_FUNC_ARG);
15323     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
15324                 BAD_FUNC_ARG);
15325     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
15326                 BAD_FUNC_ARG);
15327     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
15328                 BAD_FUNC_ARG);
15329     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
15330                 BAD_FUNC_ARG);
15331     /* Bad parameters to decrypt update. */
15332     AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
15333                 BAD_FUNC_ARG);
15334     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
15335                 BAD_FUNC_ARG);
15336     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
15337                 BAD_FUNC_ARG);
15338     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
15339                 BAD_FUNC_ARG);
15340     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
15341                 BAD_FUNC_ARG);
15342 
15343     /* Bad parameters to encrypt final. */
15344     AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
15345     AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
15346     AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
15347                 BAD_FUNC_ARG);
15348     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
15349     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
15350                 BAD_FUNC_ARG);
15351     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
15352                 BAD_FUNC_ARG);
15353     /* Bad parameters to decrypt final. */
15354     AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
15355     AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
15356     AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
15357                 BAD_FUNC_ARG);
15358     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
15359     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
15360                 BAD_FUNC_ARG);
15361     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
15362                 BAD_FUNC_ARG);
15363 
15364     /* Check calling final before setting key fails. */
15365     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
15366     AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
15367     /* Check calling update before setting key else fails. */
15368     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
15369                 MISSING_KEY);
15370     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
15371                 MISSING_KEY);
15372 
15373     /* Set key but not IV. */
15374     AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
15375     AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
15376     /* Check calling final before setting IV fails. */
15377     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
15378     AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
15379     /* Check calling update before setting IV else fails. */
15380     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
15381                 MISSING_IV);
15382     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
15383                 MISSING_IV);
15384 
15385     /* Set IV using fixed part IV and external IV APIs. */
15386     AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
15387                                rng), 0);
15388     AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
15389                                         GCM_NONCE_MID_SZ), 0);
15390     AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
15391     AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
15392     /* Encrypt and decrypt data. */
15393     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
15394     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
15395     AssertIntEQ(XMEMCMP(plain, in, 1), 0);
15396     /* Finalize and check tag matches. */
15397     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
15398     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
15399 
15400     /* Set key and IV through streaming init API. */
15401     AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
15402     AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
15403     /* Encrypt/decrypt one block and AAD of one block. */
15404     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
15405                                        AES_BLOCK_SIZE), 0);
15406     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
15407                                        AES_BLOCK_SIZE), 0);
15408     AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
15409     /* Finalize and check tag matches. */
15410     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
15411     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
15412 
15413     /* Set key and IV through streaming init API. */
15414     AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
15415     AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
15416     /* No data to encrypt/decrypt one byte of AAD. */
15417     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
15418     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
15419     /* Finalize and check tag matches. */
15420     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
15421     AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
15422     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
15423 
15424     /* Set key and IV through streaming init API. */
15425     AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
15426     AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
15427     /* Encrypt/decrypt one byte and no AAD. */
15428     AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
15429     AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
15430     AssertIntEQ(XMEMCMP(plain, in, 1), 0);
15431     /* Finalize and check tag matches. */
15432     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
15433     AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
15434     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
15435 
15436     /* Set key and IV through streaming init API. */
15437     AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
15438     AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
15439     /* Encryption AES is one byte at a time */
15440     for (i = 0; i < (int)sizeof(aad); i++) {
15441         AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
15442             0);
15443     }
15444     for (i = 0; i < (int)sizeof(in); i++) {
15445         AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
15446                     0);
15447     }
15448     /* Decryption AES is two bytes at a time */
15449     for (i = 0; i < (int)sizeof(aad); i += 2) {
15450         AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
15451             0);
15452     }
15453     for (i = 0; i < (int)sizeof(aad); i += 2) {
15454         AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
15455                                            0), 0);
15456     }
15457     AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
15458     /* Finalize and check tag matches. */
15459     AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
15460     AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
15461     AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
15462 
15463     /* Check streaming encryption can be decrypted with one shot. */
15464     AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
15465     AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
15466         AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
15467     AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
15468 
15469     wc_AesFree(aesEnc);
15470     wc_AesFree(aesDec);
15471     wc_FreeRng(rng);
15472 
15473 #endif
15474     return ret;
15475 
15476 } /* END test_wc_AesGcmStream */
15477 
15478 
15479 /*
15480  * unit test for wc_Des3_SetIV()
15481  */
15482 static int test_wc_Des3_SetIV (void)
15483 {
15484     int  ret = 0;
15485 #ifndef NO_DES3
15486     Des3 des;
15487     const byte key[] =
15488     {
15489         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
15490         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
15491         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
15492     };
15493 
15494     const byte iv[] =
15495     {
15496         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
15497         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
15498         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
15499     };
15500 
15501     printf(testingFmt, "wc_Des3_SetIV()");
15502 
15503     ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
15504     if (ret != 0)
15505         return ret;
15506 
15507     /* DES_ENCRYPTION or DES_DECRYPTION */
15508     ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
15509 
15510     if (ret == 0) {
15511         if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
15512             ret = WOLFSSL_FATAL_ERROR;
15513         }
15514     }
15515 
15516 #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
15517     /* Test explicitly wc_Des3_SetIV()  */
15518     if (ret == 0) {
15519         ret = wc_Des3_SetIV(NULL, iv);
15520         if (ret == BAD_FUNC_ARG) {
15521             ret = wc_Des3_SetIV(&des, NULL);
15522         } else if (ret == 0) {
15523             ret = WOLFSSL_FATAL_ERROR;
15524         }
15525     }
15526 #endif
15527 
15528     wc_Des3Free(&des);
15529 
15530     printf(resultFmt, ret == 0 ? passed : failed);
15531 
15532 #endif
15533     return ret;
15534 
15535 } /* END test_wc_Des3_SetIV */
15536 
15537 /*
15538  * unit test for wc_Des3_SetKey()
15539  */
15540 static int test_wc_Des3_SetKey (void)
15541 {
15542     int  ret = 0;
15543 #ifndef NO_DES3
15544     Des3 des;
15545     const byte key[] =
15546     {
15547         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
15548         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
15549         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
15550     };
15551 
15552     const byte iv[] =
15553     {
15554         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
15555         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
15556         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
15557     };
15558 
15559     printf(testingFmt, "wc_Des3_SetKey()");
15560 
15561     ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
15562     if (ret != 0)
15563         return ret;
15564 
15565     /* DES_ENCRYPTION or DES_DECRYPTION */
15566     ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
15567     if (ret == 0) {
15568         if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
15569             ret = WOLFSSL_FATAL_ERROR;
15570         }
15571     }
15572 
15573     /* Test bad args. */
15574     if (ret == 0) {
15575         ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
15576         if (ret == BAD_FUNC_ARG) {
15577             ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
15578         }
15579         if (ret == BAD_FUNC_ARG) {
15580             ret = wc_Des3_SetKey(&des, key, iv, -1);
15581         }
15582         if (ret == BAD_FUNC_ARG) {
15583             /* Default case. Should return 0. */
15584             ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
15585         }
15586     } /* END if ret != 0 */
15587 
15588     wc_Des3Free(&des);
15589 
15590     printf(resultFmt, ret == 0 ? passed : failed);
15591 
15592 #endif
15593     return ret;
15594 
15595 } /* END test_wc_Des3_SetKey */
15596 
15597 
15598 /*
15599  * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
15600  */
15601 static int test_wc_Des3_CbcEncryptDecrypt (void)
15602 {
15603     int ret = 0;
15604 #ifndef NO_DES3
15605     Des3 des;
15606     byte cipher[24];
15607     byte plain[24];
15608 
15609     const byte key[] =
15610     {
15611         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
15612         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
15613         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
15614     };
15615 
15616     const byte iv[] =
15617     {
15618         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
15619         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
15620         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
15621     };
15622 
15623     const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
15624         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
15625         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
15626         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
15627     };
15628 
15629     printf(testingFmt, "wc_Des3_CbcEncrypt()");
15630 
15631     ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
15632     if (ret != 0)
15633         return ret;
15634 
15635     ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
15636 
15637     if (ret == 0) {
15638         ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
15639 
15640         if (ret == 0) {
15641             ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
15642         }
15643         if (ret == 0) {
15644             ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
15645         }
15646     }
15647 
15648     if (ret == 0) {
15649         if (XMEMCMP(plain, vector, 24) != 0) {
15650             ret = WOLFSSL_FATAL_ERROR;
15651         }
15652     }
15653 
15654     /* Pass in bad args. */
15655     if (ret == 0) {
15656         ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
15657         if (ret == BAD_FUNC_ARG) {
15658             ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
15659         }
15660         if (ret == BAD_FUNC_ARG) {
15661             ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
15662         }
15663         if (ret != BAD_FUNC_ARG) {
15664             ret = WOLFSSL_FATAL_ERROR;
15665         } else {
15666             ret = 0;
15667         }
15668     }
15669 
15670     if (ret == 0) {
15671         ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
15672         if (ret == BAD_FUNC_ARG) {
15673             ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
15674         }
15675         if (ret == BAD_FUNC_ARG) {
15676             ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
15677         }
15678         if (ret != BAD_FUNC_ARG) {
15679             ret = WOLFSSL_FATAL_ERROR;
15680         } else {
15681             ret = 0;
15682         }
15683     }
15684 
15685     wc_Des3Free(&des);
15686 
15687     printf(resultFmt, ret == 0 ? passed : failed);
15688 
15689 #endif
15690     return ret;
15691 
15692 } /* END wc_Des3_CbcEncrypt */
15693 
15694 /*
15695  *  Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
15696  */
15697 static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
15698 {
15699     int ret = 0;
15700 #ifndef NO_DES3
15701 
15702     word32 vectorSz, cipherSz;
15703     byte cipher[24];
15704     byte plain[24];
15705 
15706     byte vector[] = /* Now is the time for all w/o trailing 0 */
15707     {
15708         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
15709         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
15710         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
15711     };
15712 
15713     byte key[] =
15714     {
15715         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
15716         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
15717         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
15718     };
15719 
15720     byte iv[] =
15721     {
15722         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
15723         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
15724         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
15725     };
15726 
15727 
15728     vectorSz = sizeof(byte) * 24;
15729     cipherSz = sizeof(byte) * 24;
15730 
15731     printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
15732 
15733     ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
15734     if (ret == 0) {
15735         ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
15736         if (ret == 0) {
15737             if (XMEMCMP(plain, vector, 24) !=  0) {
15738                 ret = WOLFSSL_FATAL_ERROR;
15739             }
15740         }
15741     }
15742 
15743     /* pass in bad args. */
15744     if (ret == 0) {
15745         ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
15746         if (ret == BAD_FUNC_ARG) {
15747             ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
15748         }
15749         if (ret == BAD_FUNC_ARG) {
15750             ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
15751         }
15752         if (ret == BAD_FUNC_ARG) {
15753             ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
15754                                                                     key, NULL);
15755         } else {
15756             /* Return code catch. */
15757             ret = WOLFSSL_FAILURE;
15758         }
15759     }
15760 
15761     if (ret == 0) {
15762         ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
15763         if (ret == BAD_FUNC_ARG) {
15764             ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
15765         }
15766         if (ret == BAD_FUNC_ARG) {
15767             ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
15768         }
15769         if (ret == BAD_FUNC_ARG) {
15770             ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
15771         } else {
15772             ret = WOLFSSL_FAILURE;
15773         }
15774     }
15775 
15776     printf(resultFmt, ret == 0 ? passed : failed);
15777 
15778 #endif
15779     return ret;
15780 } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
15781 /*
15782  *  Unit test for wc_Des3_EcbEncrypt
15783  */
15784 static int test_wc_Des3_EcbEncrypt (void)
15785 {
15786     int ret = 0;
15787 #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
15788 
15789     Des3    des;
15790     byte    cipher[24];
15791     word32  cipherSz = sizeof(cipher);
15792 
15793     const byte key[] =
15794     {
15795         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
15796         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
15797         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
15798     };
15799 
15800     const byte iv[] =
15801     {
15802         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
15803         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
15804         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
15805     };
15806 
15807     const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
15808         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
15809         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
15810         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
15811     };
15812 
15813     printf(testingFmt, "wc_Des3_EcbEncrypt()");
15814 
15815     ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
15816     if (ret != 0) {
15817         return ret;
15818     }
15819     if (ret == 0 ) {
15820         ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
15821     }
15822     /* Bad Cases */
15823     if (ret == 0) {
15824         ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
15825         if (ret == BAD_FUNC_ARG) {
15826             ret = 0;
15827         }
15828     }
15829     if (ret == 0) {
15830         ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
15831         if (ret == BAD_FUNC_ARG) {
15832             ret = 0;
15833         }
15834     }
15835     if (ret == 0) {
15836         ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
15837         if (ret == BAD_FUNC_ARG) {
15838             ret = 0;
15839         }
15840     }
15841     if (ret == 0) {
15842         ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
15843         if (ret == BAD_FUNC_ARG) {
15844             ret = 0;
15845         }
15846     }
15847     if (ret == 0) {
15848         ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
15849         if (ret == BAD_FUNC_ARG) {
15850             ret = 0;
15851         }
15852     }
15853     /* Good Cases */
15854     if (ret == 0) {
15855         ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
15856     }
15857     wc_Des3Free(&des);
15858 
15859     printf(resultFmt, ret == 0 ? passed : failed);
15860 
15861 #endif
15862     return ret;
15863 } /* END test_wc_Des3_EcbEncrypt */
15864 
15865 /*
15866  * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
15867  */
15868 static int test_wc_Chacha_SetKey (void)
15869 {
15870     int         ret = 0;
15871 #ifdef HAVE_CHACHA
15872     ChaCha      ctx;
15873     const byte  key[] =
15874     {
15875          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15876          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15877          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15878          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
15879     };
15880     byte        cipher[128];
15881 
15882     printf(testingFmt, "wc_Chacha_SetKey()");
15883 
15884     ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
15885     /* Test bad args. */
15886     if (ret == 0) {
15887         ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
15888         if (ret == BAD_FUNC_ARG) {
15889             ret = wc_Chacha_SetKey(&ctx, key, 18);
15890         }
15891         if (ret == BAD_FUNC_ARG) {
15892             ret = 0;
15893         } else {
15894             ret = WOLFSSL_FATAL_ERROR;
15895         }
15896     }
15897     printf(resultFmt, ret == 0 ? passed : failed);
15898     if (ret != 0) {
15899         return ret;
15900     }
15901 
15902     printf(testingFmt, "wc_Chacha_SetIV");
15903     ret = wc_Chacha_SetIV(&ctx, cipher, 0);
15904     if (ret == 0) {
15905     /* Test bad args. */
15906         ret = wc_Chacha_SetIV(NULL, cipher, 0);
15907         if (ret == BAD_FUNC_ARG) {
15908             ret = 0;
15909         } else {
15910             ret = WOLFSSL_FAILURE;
15911         }
15912     }
15913     printf(resultFmt, ret == 0 ? passed : failed);
15914 
15915 #endif
15916     return ret;
15917 } /* END test_wc_Chacha_SetKey */
15918 
15919 /*
15920  * unit test for wc_Poly1305SetKey()
15921  */
15922 static int test_wc_Poly1305SetKey(void)
15923 {
15924     int ret = 0;
15925 
15926 #ifdef HAVE_POLY1305
15927     Poly1305      ctx;
15928     const byte  key[] =
15929     {
15930          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15931          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15932          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15933          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
15934     };
15935 
15936     printf(testingFmt, "wc_Poly1305_SetKey()");
15937 
15938     ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
15939     /* Test bad args. */
15940     if (ret == 0) {
15941         ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
15942         if(ret == BAD_FUNC_ARG) {
15943             ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
15944         }
15945         if (ret == BAD_FUNC_ARG) {
15946             ret = wc_Poly1305SetKey(&ctx, key, 18);
15947         }
15948         if (ret == BAD_FUNC_ARG) {
15949             ret = 0;
15950         } else {
15951             ret = WOLFSSL_FATAL_ERROR;
15952         }
15953     }
15954 
15955     printf(resultFmt, ret == 0 ? passed : failed);
15956 
15957 #endif
15958     return ret;
15959 } /* END test_wc_Poly1305_SetKey() */
15960 
15961 /*
15962  * Testing wc_Chacha_Process()
15963  */
15964 static int test_wc_Chacha_Process (void)
15965 {
15966     int         ret = 0;
15967 #ifdef HAVE_CHACHA
15968     ChaCha      enc, dec;
15969     byte        cipher[128];
15970     byte        plain[128];
15971     const byte  key[] =
15972     {
15973          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15974          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15975          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15976          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
15977     };
15978     const char* input = "Everybody gets Friday off.";
15979     word32      keySz = sizeof(key)/sizeof(byte);
15980     unsigned long int inlen = XSTRLEN(input);
15981 
15982     /*Initialize stack varialbes.*/
15983     XMEMSET(cipher, 0, 128);
15984     XMEMSET(plain, 0, 128);
15985 
15986     printf(testingFmt, "wc_Chacha_Process()");
15987 
15988     ret = wc_Chacha_SetKey(&enc, key, keySz);
15989     AssertIntEQ(ret, 0);
15990     ret = wc_Chacha_SetKey(&dec, key, keySz);
15991     AssertIntEQ(ret, 0);
15992     ret = wc_Chacha_SetIV(&enc, cipher, 0);
15993     AssertIntEQ(ret, 0);
15994     ret = wc_Chacha_SetIV(&dec, cipher, 0);
15995     AssertIntEQ(ret, 0);
15996 
15997     ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
15998     AssertIntEQ(ret, 0);
15999     ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
16000     AssertIntEQ(ret, 0);
16001     ret = XMEMCMP(input, plain, (int)inlen);
16002     AssertIntEQ(ret, 0);
16003 
16004 #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
16005     /* test checking and using leftovers, currently just in C code */
16006     ret = wc_Chacha_SetIV(&enc, cipher, 0);
16007     AssertIntEQ(ret, 0);
16008     ret = wc_Chacha_SetIV(&dec, cipher, 0);
16009     AssertIntEQ(ret, 0);
16010 
16011     ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
16012     AssertIntEQ(ret, 0);
16013     ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
16014             (byte*)input + (inlen - 2), 2);
16015     AssertIntEQ(ret, 0);
16016     ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
16017     AssertIntEQ(ret, 0);
16018     ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
16019             (byte*)input + (inlen - 2), 2);
16020     AssertIntEQ(ret, 0);
16021     ret = XMEMCMP(input, plain, (int)inlen);
16022     AssertIntEQ(ret, 0);
16023 
16024     /* check edge cases with counter increment */
16025     {
16026         /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
16027         const byte expected[] = {
16028             0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
16029             0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
16030             0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
16031             0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
16032             0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
16033             0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
16034             0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
16035             0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
16036             0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
16037             0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
16038             0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
16039             0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
16040             0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
16041             0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
16042             0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
16043             0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
16044             0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
16045             0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
16046             0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
16047             0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
16048             0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
16049             0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
16050             0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
16051             0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
16052             0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
16053             0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
16054             0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
16055             0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
16056             0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
16057             0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
16058             0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
16059             0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
16060         };
16061         const byte iv2[] = {
16062             0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
16063             0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
16064         };
16065         byte input2[256];
16066         int i;
16067 
16068         for (i = 0; i < 256; i++)
16069             input2[i] = i;
16070 
16071         ret = wc_Chacha_SetIV(&enc, iv2, 0);
16072         AssertIntEQ(ret, 0);
16073 
16074         ret = wc_Chacha_Process(&enc, cipher, input2, 64);
16075         AssertIntEQ(ret, 0);
16076         AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
16077 
16078         ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
16079         AssertIntEQ(ret, 0);
16080         AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
16081 
16082         /* partial */
16083         ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
16084         AssertIntEQ(ret, 0);
16085         AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
16086 
16087         ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
16088         AssertIntEQ(ret, 0);
16089         AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
16090     }
16091 #endif
16092 
16093     /* Test bad args. */
16094     ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
16095     AssertIntEQ(ret, BAD_FUNC_ARG);
16096     if (ret == BAD_FUNC_ARG) {
16097         ret = 0;
16098     }
16099 
16100     printf(resultFmt, ret == 0 ? passed : failed);
16101 
16102 #endif
16103     return ret;
16104 } /* END test_wc_Chacha_Process */
16105 
16106 /*
16107  * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
16108  */
16109 static int test_wc_ChaCha20Poly1305_aead (void)
16110 {
16111     int   ret = 0;
16112 #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
16113     const byte  key[] = {
16114         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
16115         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
16116         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
16117         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
16118     };
16119 
16120     const byte  plaintext[] = {
16121         0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
16122         0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
16123         0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
16124         0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
16125         0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
16126         0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
16127         0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
16128         0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
16129         0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
16130         0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
16131         0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
16132         0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
16133         0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
16134         0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
16135         0x74, 0x2e
16136     };
16137 
16138     const byte  iv[] = {
16139         0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
16140         0x44, 0x45, 0x46, 0x47
16141     };
16142 
16143     const byte  aad[] = { /* additional data */
16144         0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
16145         0xc4, 0xc5, 0xc6, 0xc7
16146     };
16147     const byte  cipher[] = { /* expected output from operation */
16148         0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
16149         0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
16150         0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
16151         0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
16152         0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
16153         0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
16154         0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
16155         0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
16156         0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
16157         0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
16158         0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
16159         0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
16160         0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
16161         0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
16162         0x61, 0x16
16163     };
16164     const byte  authTag[] = { /* expected output from operation */
16165         0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
16166         0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
16167     };
16168     byte        generatedCiphertext[272];
16169     byte        generatedPlaintext[272];
16170     byte        generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
16171 
16172     /* Initialize stack variables. */
16173     XMEMSET(generatedCiphertext, 0, 272);
16174     XMEMSET(generatedPlaintext, 0, 272);
16175 
16176     /* Test Encrypt */
16177     printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
16178 
16179     ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
16180                 sizeof(plaintext), generatedCiphertext, generatedAuthTag);
16181     AssertIntEQ(ret, 0);
16182     ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
16183     AssertIntEQ(ret, 0);
16184 
16185     /* Test bad args. */
16186     ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
16187                     sizeof(plaintext), generatedCiphertext, generatedAuthTag);
16188     AssertIntEQ(ret,  BAD_FUNC_ARG);
16189     ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
16190                                         plaintext, sizeof(plaintext),
16191                                         generatedCiphertext, generatedAuthTag);
16192     AssertIntEQ(ret,  BAD_FUNC_ARG);
16193     ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
16194                     sizeof(plaintext), generatedCiphertext, generatedAuthTag);
16195     AssertIntEQ(ret,  BAD_FUNC_ARG);
16196     ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
16197                     plaintext, 0, generatedCiphertext, generatedAuthTag);
16198     AssertIntEQ(ret,  BAD_FUNC_ARG);
16199     ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
16200                     plaintext, sizeof(plaintext), NULL, generatedAuthTag);
16201     AssertIntEQ(ret,  BAD_FUNC_ARG);
16202     ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
16203                     plaintext, sizeof(plaintext), generatedCiphertext, NULL);
16204     if (ret == BAD_FUNC_ARG) {
16205         ret = 0;
16206     }
16207     printf(resultFmt, ret == 0 ? passed : failed);
16208     if (ret != 0) {
16209         return ret;
16210     }
16211 
16212     printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
16213     ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
16214                             sizeof(cipher), authTag, generatedPlaintext);
16215     AssertIntEQ(ret, 0);
16216     ret = XMEMCMP(generatedPlaintext, plaintext,
16217                                         sizeof(plaintext)/sizeof(byte));
16218     AssertIntEQ(ret, 0);
16219 
16220     /* Test bad args. */
16221     ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
16222                                 sizeof(cipher), authTag, generatedPlaintext);
16223     AssertIntEQ(ret,  BAD_FUNC_ARG);
16224     ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
16225                         cipher, sizeof(cipher), authTag, generatedPlaintext);
16226     AssertIntEQ(ret,  BAD_FUNC_ARG);
16227     ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
16228                                 sizeof(cipher), authTag, generatedPlaintext);
16229     AssertIntEQ(ret,  BAD_FUNC_ARG);
16230     ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
16231                                     sizeof(cipher), NULL, generatedPlaintext);
16232     AssertIntEQ(ret,  BAD_FUNC_ARG);
16233     ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
16234                                                 sizeof(cipher), authTag, NULL);
16235     AssertIntEQ(ret,  BAD_FUNC_ARG);
16236     ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
16237                                                 0, authTag, generatedPlaintext);
16238     AssertIntEQ(ret,  BAD_FUNC_ARG);
16239     if (ret == BAD_FUNC_ARG) {
16240         ret = 0;
16241     }
16242 
16243     printf(resultFmt, ret == 0 ? passed : failed);
16244 
16245 #endif
16246     return ret;
16247 
16248 } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
16249 
16250 
16251 /*
16252  * Testing function for wc_Rc2SetKey().
16253  */
16254 static int test_wc_Rc2SetKey(void)
16255 {
16256     int     ret = 0;
16257 #ifdef WC_RC2
16258     Rc2     rc2;
16259     byte    key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
16260     byte    iv[]    = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
16261 
16262     printf(testingFmt, "wc_Rc2SetKey()");
16263 
16264     /* valid key and IV */
16265     ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
16266                        iv, 40);
16267     if (ret == 0) {
16268         /* valid key, no IV */
16269         ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
16270                            NULL, 40);
16271     }
16272 
16273     /* bad arguments  */
16274     if (ret == 0) {
16275         /* null Rc2 struct */
16276         ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
16277                            iv, 40);
16278         if (ret == BAD_FUNC_ARG) {
16279             ret = 0;
16280         }
16281     }
16282 
16283     if (ret == 0) {
16284         /* null key */
16285         ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
16286                            iv, 40);
16287         if (ret == BAD_FUNC_ARG) {
16288             ret = 0;
16289         }
16290     }
16291 
16292     if (ret == 0) {
16293         /* key size == 0 */
16294         ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
16295         if (ret == WC_KEY_SIZE_E) {
16296             ret = 0;
16297         }
16298     }
16299 
16300     if (ret == 0) {
16301         /* key size > 128 */
16302         ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
16303         if (ret == WC_KEY_SIZE_E) {
16304             ret = 0;
16305         }
16306     }
16307 
16308     if (ret == 0) {
16309         /* effective bits == 0 */
16310         ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
16311                            iv, 0);
16312         if (ret == WC_KEY_SIZE_E) {
16313             ret = 0;
16314         }
16315     }
16316 
16317     if (ret == 0) {
16318         /* effective bits > 1024 */
16319         ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
16320                            iv, 1025);
16321         if (ret == WC_KEY_SIZE_E) {
16322             ret = 0;
16323         }
16324     }
16325 
16326     printf(resultFmt, ret == 0 ? passed : failed);
16327 
16328 #endif
16329     return ret;
16330 } /* END test_wc_Rc2SetKey */
16331 
16332 /*
16333  * Testing function for wc_Rc2SetIV().
16334  */
16335 static int test_wc_Rc2SetIV(void)
16336 {
16337     int     ret = 0;
16338 #ifdef WC_RC2
16339     Rc2     rc2;
16340     byte    iv[]    = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
16341 
16342     printf(testingFmt, "wc_Rc2SetIV()");
16343 
16344     /* valid IV */
16345     ret = wc_Rc2SetIV(&rc2, iv);
16346     if (ret == 0) {
16347         /* valid NULL IV */
16348         ret = wc_Rc2SetIV(&rc2, NULL);
16349     }
16350 
16351     /* bad arguments */
16352     if (ret == 0) {
16353         ret = wc_Rc2SetIV(NULL, iv);
16354         if (ret == BAD_FUNC_ARG) {
16355             ret = 0;
16356         }
16357     }
16358 
16359     printf(resultFmt, ret == 0 ? passed : failed);
16360 
16361 #endif
16362     return ret;
16363 } /* END test_wc_Rc2SetKey */
16364 
16365 /*
16366  * Testing function for wc_Rc2EcbEncrypt().
16367  */
16368 static int test_wc_Rc2EcbEncryptDecrypt(void)
16369 {
16370     int     ret = 0;
16371 #ifdef WC_RC2
16372     Rc2     rc2;
16373     int effectiveKeyBits = 63;
16374 
16375     byte cipher[RC2_BLOCK_SIZE];
16376     byte plain[RC2_BLOCK_SIZE];
16377 
16378     byte key[]    = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16379     byte input[]  = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16380     byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
16381 
16382     printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
16383 
16384     XMEMSET(cipher, 0, sizeof(cipher));
16385     XMEMSET(plain, 0, sizeof(plain));
16386 
16387     ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
16388                        NULL, effectiveKeyBits);
16389     if (ret == 0) {
16390         ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
16391         if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
16392             ret = WOLFSSL_FATAL_ERROR;
16393         }
16394 
16395         if (ret == 0) {
16396             ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
16397             if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
16398                 ret = WOLFSSL_FATAL_ERROR;
16399             }
16400         }
16401     }
16402 
16403     /* Rc2EcbEncrypt bad arguments */
16404     if (ret == 0) {
16405         /* null Rc2 struct */
16406         ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
16407         if (ret == BAD_FUNC_ARG) {
16408             ret = 0;
16409         }
16410     }
16411 
16412     if (ret == 0) {
16413         /* null out buffer */
16414         ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
16415         if (ret == BAD_FUNC_ARG) {
16416             ret = 0;
16417         }
16418     }
16419 
16420     if (ret == 0) {
16421         /* null input buffer */
16422         ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
16423         if (ret == BAD_FUNC_ARG) {
16424             ret = 0;
16425         }
16426     }
16427 
16428     if (ret == 0) {
16429         /* output buffer sz != RC2_BLOCK_SIZE (8) */
16430         ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
16431         if (ret == BUFFER_E) {
16432             ret = 0;
16433         }
16434     }
16435 
16436     /* Rc2EcbDecrypt bad arguments */
16437     if (ret == 0) {
16438         /* null Rc2 struct */
16439         ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
16440         if (ret == BAD_FUNC_ARG) {
16441             ret = 0;
16442         }
16443     }
16444 
16445     if (ret == 0) {
16446         /* null out buffer */
16447         ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
16448         if (ret == BAD_FUNC_ARG) {
16449             ret = 0;
16450         }
16451     }
16452 
16453     if (ret == 0) {
16454         /* null input buffer */
16455         ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
16456         if (ret == BAD_FUNC_ARG) {
16457             ret = 0;
16458         }
16459     }
16460 
16461     if (ret == 0) {
16462         /* output buffer sz != RC2_BLOCK_SIZE (8) */
16463         ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
16464         if (ret == BUFFER_E) {
16465             ret = 0;
16466         }
16467     }
16468 
16469     printf(resultFmt, ret == 0 ? passed : failed);
16470 
16471 #endif
16472     return ret;
16473 } /* END test_wc_Rc2SetKey */
16474 
16475 /*
16476  * Testing function for wc_Rc2CbcEncrypt().
16477  */
16478 static int test_wc_Rc2CbcEncryptDecrypt(void)
16479 {
16480     int     ret = 0;
16481 #ifdef WC_RC2
16482     Rc2     rc2;
16483     int effectiveKeyBits = 63;
16484 
16485     byte cipher[RC2_BLOCK_SIZE*2];
16486     byte plain[RC2_BLOCK_SIZE*2];
16487 
16488     /* vector taken from test.c */
16489     byte key[]    = {
16490         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
16491     };
16492     byte iv[] = {
16493         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
16494     };
16495     byte input[]  = {
16496         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16497         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
16498     };
16499     byte output[] = {
16500         0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
16501         0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
16502     };
16503 
16504     printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
16505 
16506     XMEMSET(cipher, 0, sizeof(cipher));
16507     XMEMSET(plain, 0, sizeof(plain));
16508 
16509     ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
16510                        iv, effectiveKeyBits);
16511     if (ret == 0) {
16512         ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
16513         if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
16514             ret = WOLFSSL_FATAL_ERROR;
16515         } else {
16516             /* reset IV for decrypt */
16517             ret = wc_Rc2SetIV(&rc2, iv);
16518         }
16519 
16520         if (ret == 0) {
16521             ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
16522             if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
16523                 ret = WOLFSSL_FATAL_ERROR;
16524             }
16525         }
16526     }
16527 
16528     /* Rc2CbcEncrypt bad arguments */
16529     if (ret == 0) {
16530         /* null Rc2 struct */
16531         ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
16532         if (ret == BAD_FUNC_ARG) {
16533             ret = 0;
16534         }
16535     }
16536 
16537     if (ret == 0) {
16538         /* null out buffer */
16539         ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
16540         if (ret == BAD_FUNC_ARG) {
16541             ret = 0;
16542         }
16543     }
16544 
16545     if (ret == 0) {
16546         /* null input buffer */
16547         ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
16548         if (ret == BAD_FUNC_ARG) {
16549             ret = 0;
16550         }
16551     }
16552 
16553     /* Rc2CbcDecrypt bad arguments */
16554     if (ret == 0) {
16555         /* in size is 0 */
16556         ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
16557         if (ret != 0) {
16558             ret = WOLFSSL_FATAL_ERROR;
16559         }
16560     }
16561 
16562     if (ret == 0) {
16563         /* null Rc2 struct */
16564         ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
16565         if (ret == BAD_FUNC_ARG) {
16566             ret = 0;
16567         }
16568     }
16569 
16570     if (ret == 0) {
16571         /* null out buffer */
16572         ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
16573         if (ret == BAD_FUNC_ARG) {
16574             ret = 0;
16575         }
16576     }
16577 
16578     if (ret == 0) {
16579         /* null input buffer */
16580         ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
16581         if (ret == BAD_FUNC_ARG) {
16582             ret = 0;
16583         }
16584     }
16585 
16586     printf(resultFmt, ret == 0 ? passed : failed);
16587 
16588 #endif
16589     return ret;
16590 } /* END test_wc_Rc2SetKey */
16591 
16592 
16593 /*
16594  * Testing function for wc_AesSetIV
16595  */
16596 static int test_wc_AesSetIV (void)
16597 {
16598     int     ret = 0;
16599 #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
16600     Aes     aes;
16601     byte    key16[] =
16602     {
16603         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16604         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
16605     };
16606     byte    iv1[]    = "1234567890abcdef";
16607     byte    iv2[]    = "0987654321fedcba";
16608 
16609     printf(testingFmt, "wc_AesSetIV()");
16610 
16611     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
16612     if (ret != 0)
16613         return ret;
16614 
16615     ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
16616                                                      iv1, AES_ENCRYPTION);
16617     if(ret == 0) {
16618         ret = wc_AesSetIV(&aes, iv2);
16619     }
16620     /* Test bad args. */
16621     if(ret == 0) {
16622         ret = wc_AesSetIV(NULL, iv1);
16623         if(ret == BAD_FUNC_ARG) {
16624             /* NULL iv should return 0. */
16625             ret = wc_AesSetIV(&aes, NULL);
16626         } else {
16627             ret = WOLFSSL_FATAL_ERROR;
16628         }
16629     }
16630 
16631     wc_AesFree(&aes);
16632 
16633     printf(resultFmt, ret == 0 ? passed : failed);
16634 
16635 #endif
16636     return ret;
16637 } /* test_wc_AesSetIV */
16638 
16639 
16640 /*
16641  * Testing function for wc_AesSetKey().
16642  */
16643 static int test_wc_AesSetKey (void)
16644 {
16645     int     ret = 0;
16646 #ifndef NO_AES
16647     Aes     aes;
16648     byte    key16[] =
16649     {
16650         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16651         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
16652     };
16653 #ifdef WOLFSSL_AES_192
16654     byte    key24[] =
16655     {
16656         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16657         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
16658         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
16659     };
16660 #endif
16661 #ifdef WOLFSSL_AES_256
16662     byte    key32[] =
16663     {
16664         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16665         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
16666         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16667         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
16668     };
16669 #endif
16670     byte    badKey16[] =
16671     {
16672         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16673         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
16674     };
16675     byte    iv[]    = "1234567890abcdef";
16676 
16677     printf(testingFmt, "wc_AesSetKey()");
16678 
16679     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
16680     if (ret != 0)
16681         return ret;
16682 
16683 #ifdef WOLFSSL_AES_128
16684     ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
16685                                                         iv, AES_ENCRYPTION);
16686 #endif
16687 #ifdef WOLFSSL_AES_192
16688     if (ret == 0) {
16689         ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
16690                                                            iv, AES_ENCRYPTION);
16691     }
16692 #endif
16693 #ifdef WOLFSSL_AES_256
16694     if (ret == 0) {
16695         ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
16696                                                            iv, AES_ENCRYPTION);
16697     }
16698 #endif
16699 
16700     /* Pass in bad args. */
16701     if (ret == 0) {
16702         ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
16703                                                            iv, AES_ENCRYPTION);
16704             if (ret == BAD_FUNC_ARG) {
16705                 ret = wc_AesSetKey(&aes, badKey16,
16706                                     (word32) sizeof(badKey16) / sizeof(byte),
16707                                                          iv, AES_ENCRYPTION);
16708         }
16709         if (ret == BAD_FUNC_ARG) {
16710             ret = 0;
16711         } else {
16712             ret = WOLFSSL_FATAL_ERROR;
16713         }
16714     }
16715 
16716     wc_AesFree(&aes);
16717 
16718     printf(resultFmt, ret == 0 ? passed : failed);
16719 
16720 #endif
16721     return ret;
16722 } /* END test_wc_AesSetKey */
16723 
16724 
16725 
16726 /*
16727  * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
16728  * and wc_AesCbcDecryptWithKey()
16729  */
16730 static int test_wc_AesCbcEncryptDecrypt (void)
16731 {
16732     int     ret = 0;
16733 #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
16734     defined(WOLFSSL_AES_256)
16735     Aes     aes;
16736     byte    key32[] =
16737     {
16738         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16739         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
16740         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16741         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
16742     };
16743     byte    vector[] = /* Now is the time for all good men w/o trailing 0 */
16744     {
16745         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
16746         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
16747         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
16748         0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
16749     };
16750     byte    iv[]    = "1234567890abcdef";
16751     byte    enc[sizeof(vector)];
16752     byte    dec[sizeof(vector)];
16753     int     cbcE    =   WOLFSSL_FATAL_ERROR;
16754     int     cbcD    =   WOLFSSL_FATAL_ERROR;
16755     int     cbcDWK  =   WOLFSSL_FATAL_ERROR;
16756     byte    dec2[sizeof(vector)];
16757 
16758     /* Init stack variables. */
16759     XMEMSET(enc, 0, sizeof(enc));
16760     XMEMSET(dec, 0, sizeof(vector));
16761     XMEMSET(dec2, 0, sizeof(vector));
16762 
16763     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
16764     if (ret != 0)
16765         return ret;
16766 
16767     ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
16768     if (ret == 0) {
16769         ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
16770         if (ret == 0) {
16771             /* Re init for decrypt and set flag. */
16772             cbcE = 0;
16773             wc_AesFree(&aes);
16774             ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
16775                                                     iv, AES_DECRYPTION);
16776         }
16777         if (ret == 0) {
16778             ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
16779             if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
16780                 ret = WOLFSSL_FATAL_ERROR;
16781             } else {
16782                 /* Set flag. */
16783                 cbcD = 0;
16784             }
16785         }
16786     }
16787     /* If encrypt succeeds but cbc decrypt fails, we can still test. */
16788     if (ret == 0 || cbcE == 0) {
16789         ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
16790                                      key32, sizeof(key32)/sizeof(byte), iv);
16791         if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
16792             cbcDWK = 0;
16793         }
16794     }
16795 
16796     printf(testingFmt, "wc_AesCbcEncrypt()");
16797     /* Pass in bad args */
16798     if (cbcE == 0) {
16799         cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
16800         if (cbcE == BAD_FUNC_ARG) {
16801             cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
16802         }
16803         if (cbcE == BAD_FUNC_ARG) {
16804             cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
16805         }
16806         if (cbcE == BAD_FUNC_ARG) {
16807             cbcE = 0;
16808         } else {
16809             cbcE = WOLFSSL_FATAL_ERROR;
16810         }
16811 #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
16812         if (cbcE == 0) {
16813             cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
16814         }
16815         if (cbcE == BAD_LENGTH_E) {
16816             cbcE = 0;
16817         } else {
16818             cbcE = WOLFSSL_FATAL_ERROR;
16819         }
16820 #endif
16821     }
16822     if (cbcE == 0) {
16823     #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
16824         (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
16825         printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
16826                " skip test");
16827     #else
16828         /* Test passing in size of 0  */
16829         XMEMSET(enc, 0, sizeof(enc));
16830         cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
16831         if (cbcE == 0) {
16832             /* Check enc was not modified */
16833             int i;
16834             for (i = 0; i < (int)sizeof(enc); i++)
16835                 cbcE |= enc[i];
16836         }
16837     #endif
16838     }
16839     printf(resultFmt, cbcE == 0 ? passed : failed);
16840     if (cbcE != 0) {
16841         wc_AesFree(&aes);
16842         return cbcE;
16843     }
16844 
16845     printf(testingFmt, "wc_AesCbcDecrypt()");
16846     if (cbcD == 0) {
16847         cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
16848         if (cbcD == BAD_FUNC_ARG) {
16849             cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
16850         }
16851         if (cbcD == BAD_FUNC_ARG) {
16852             cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
16853         }
16854         if (cbcD == BAD_FUNC_ARG) {
16855             cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
16856         }
16857 #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
16858         if (cbcD == BAD_LENGTH_E) {
16859             cbcD = 0;
16860         } else {
16861             cbcD = WOLFSSL_FATAL_ERROR;
16862         }
16863 #else
16864         if (cbcD == BAD_FUNC_ARG) {
16865             cbcD = 0;
16866         } else {
16867             cbcD = WOLFSSL_FATAL_ERROR;
16868         }
16869 #endif
16870     }
16871     if (cbcD == 0) {
16872         /* Test passing in size of 0  */
16873         XMEMSET(dec, 0, sizeof(dec));
16874         cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
16875         if (cbcD == 0) {
16876             /* Check dec was not modified */
16877             int i;
16878             for (i = 0; i < (int)sizeof(dec); i++)
16879                 cbcD |= dec[i];
16880         }
16881     }
16882     printf(resultFmt, cbcD == 0 ? passed : failed);
16883     if (cbcD != 0) {
16884         wc_AesFree(&aes);
16885         return cbcD;
16886     }
16887 
16888     printf(testingFmt, "wc_AesCbcDecryptWithKey()");
16889     if (cbcDWK == 0) {
16890         cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
16891                                      key32, sizeof(key32)/sizeof(byte), iv);
16892         if (cbcDWK == BAD_FUNC_ARG) {
16893             cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
16894                                      key32, sizeof(key32)/sizeof(byte), iv);
16895         }
16896         if (cbcDWK == BAD_FUNC_ARG) {
16897             cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
16898                                      NULL, sizeof(key32)/sizeof(byte), iv);
16899         }
16900         if (cbcDWK == BAD_FUNC_ARG) {
16901             cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
16902                                      key32, sizeof(key32)/sizeof(byte), NULL);
16903         }
16904         if (cbcDWK == BAD_FUNC_ARG) {
16905             cbcDWK = 0;
16906         } else {
16907             cbcDWK = WOLFSSL_FATAL_ERROR;
16908         }
16909     }
16910 
16911     wc_AesFree(&aes);
16912 
16913     printf(resultFmt, cbcDWK == 0 ? passed : failed);
16914 
16915     if (cbcDWK != 0) {
16916         return cbcDWK;
16917     }
16918 #endif
16919     return ret;
16920 } /* END test_wc_AesCbcEncryptDecrypt */
16921 
16922 /*
16923  * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
16924  */
16925 static int test_wc_AesCtrEncryptDecrypt (void)
16926 {
16927     int     ret = 0;
16928 #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
16929     Aes     aesEnc, aesDec;
16930     byte    key32[] =
16931     {
16932         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16933         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
16934         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
16935         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
16936     };
16937     byte    vector[] = /* Now is the time for all w/o trailing 0 */
16938     {
16939         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
16940         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
16941         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
16942     };
16943     byte    iv[]    = "1234567890abcdef";
16944     byte    enc[AES_BLOCK_SIZE * 2];
16945     byte    dec[AES_BLOCK_SIZE * 2];
16946 
16947     /* Init stack variables. */
16948     XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
16949     XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
16950 
16951     printf(testingFmt, "wc_AesCtrEncrypt()");
16952 
16953     ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
16954     if (ret != 0)
16955         return ret;
16956     ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
16957     if (ret != 0) {
16958         wc_AesFree(&aesEnc);
16959         return ret;
16960     }
16961 
16962     ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
16963                                                     iv, AES_ENCRYPTION);
16964     if (ret == 0) {
16965         ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
16966                                             sizeof(vector)/sizeof(byte));
16967         if (ret == 0) {
16968             /* Decrypt with wc_AesCtrEncrypt() */
16969             ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
16970                                                     iv, AES_ENCRYPTION);
16971         }
16972         if (ret == 0) {
16973             ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
16974             if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
16975                 ret = WOLFSSL_FATAL_ERROR;
16976             }
16977         }
16978     }
16979 
16980     /* Test bad args. */
16981     if (ret == 0) {
16982         ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
16983         if (ret == BAD_FUNC_ARG) {
16984             ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
16985         }
16986         if (ret == BAD_FUNC_ARG) {
16987             ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
16988         }
16989         if (ret == BAD_FUNC_ARG) {
16990             ret = 0;
16991         } else {
16992             ret = WOLFSSL_FATAL_ERROR;
16993         }
16994     }
16995 
16996     wc_AesFree(&aesEnc);
16997     wc_AesFree(&aesDec);
16998 
16999     printf(resultFmt, ret == 0 ? passed : failed);
17000 
17001 #endif
17002     return ret;
17003 
17004 } /* END test_wc_AesCtrEncryptDecrypt */
17005 
17006 /*
17007  * test function for wc_AesGcmSetKey()
17008  */
17009 static int test_wc_AesGcmSetKey (void)
17010 {
17011     int     ret = 0;
17012 #if  !defined(NO_AES) && defined(HAVE_AESGCM)
17013 
17014     Aes     aes;
17015 #ifdef WOLFSSL_AES_128
17016     byte    key16[] =
17017     {
17018         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17019         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17020     };
17021 #endif
17022 #ifdef WOLFSSL_AES_192
17023     byte    key24[] =
17024     {
17025         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17026         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17027         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
17028     };
17029 #endif
17030 #ifdef WOLFSSL_AES_256
17031     byte    key32[] =
17032     {
17033         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17034         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17035         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17036         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17037     };
17038 #endif
17039     byte    badKey16[] =
17040     {
17041         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17042         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
17043     };
17044     byte    badKey24[] =
17045     {
17046         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17047         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17048         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
17049     };
17050     byte   badKey32[] =
17051     {
17052         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
17053         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17054         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17055         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
17056     };
17057 
17058     printf(testingFmt, "wc_AesGcmSetKey()");
17059 
17060     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
17061     if (ret != 0)
17062         return ret;
17063 
17064 #ifdef WOLFSSL_AES_128
17065     ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
17066 #endif
17067 #ifdef WOLFSSL_AES_192
17068     if (ret == 0) {
17069         ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
17070     }
17071 #endif
17072 #ifdef WOLFSSL_AES_256
17073     if (ret == 0) {
17074         ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
17075     }
17076 #endif
17077 
17078     /* Pass in bad args. */
17079     if (ret == 0) {
17080         ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
17081         if (ret == BAD_FUNC_ARG) {
17082             ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
17083         }
17084         if (ret == BAD_FUNC_ARG) {
17085             ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
17086         }
17087         if (ret == BAD_FUNC_ARG) {
17088             ret = 0;
17089         } else {
17090             ret = WOLFSSL_FATAL_ERROR;
17091         }
17092     }
17093 
17094     wc_AesFree(&aes);
17095 
17096     printf(resultFmt, ret == 0 ? passed : failed);
17097 
17098 #endif
17099     return ret;
17100 } /* END test_wc_AesGcmSetKey */
17101 
17102 /*
17103  * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
17104  */
17105 static int test_wc_AesGcmEncryptDecrypt (void)
17106 {
17107     int     ret = 0;
17108     /* WOLFSSL_AFALG requires 12 byte IV */
17109 #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
17110     !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
17111 
17112     Aes     aes;
17113     byte    key32[] =
17114     {
17115         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17116         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17117         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17118         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17119     };
17120     byte    vector[] = /* Now is the time for all w/o trailing 0 */
17121     {
17122         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
17123         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
17124         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
17125     };
17126     const byte a[] =
17127     {
17128         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
17129         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
17130         0xab, 0xad, 0xda, 0xd2
17131     };
17132     byte    iv[]   = "1234567890a";
17133     byte    longIV[]  = "1234567890abcdefghij";
17134     byte    enc[sizeof(vector)];
17135     byte    resultT[AES_BLOCK_SIZE];
17136     byte    dec[sizeof(vector)];
17137     int     gcmD     =   WOLFSSL_FATAL_ERROR;
17138     int     gcmE     =   WOLFSSL_FATAL_ERROR;
17139 
17140     /* Init stack variables. */
17141     XMEMSET(enc, 0, sizeof(vector));
17142     XMEMSET(dec, 0, sizeof(vector));
17143     XMEMSET(resultT, 0, AES_BLOCK_SIZE);
17144 
17145     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
17146     if (ret != 0)
17147         return ret;
17148 
17149     ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
17150     if (ret == 0) {
17151         gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
17152                                         iv, sizeof(iv)/sizeof(byte), resultT,
17153                                         sizeof(resultT), a, sizeof(a));
17154     }
17155     if (gcmE == 0) { /* If encrypt fails, no decrypt. */
17156         gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
17157                                         iv, sizeof(iv)/sizeof(byte), resultT,
17158                                         sizeof(resultT), a, sizeof(a));
17159         if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) !=  0)) {
17160             gcmD = WOLFSSL_FATAL_ERROR;
17161         }
17162     }
17163     printf(testingFmt, "wc_AesGcmEncrypt()");
17164     /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
17165     if (gcmE == 0) {
17166         gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
17167                         iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
17168                         a, sizeof(a));
17169         if (gcmE == BAD_FUNC_ARG) {
17170             gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
17171                     sizeof(vector), iv, sizeof(iv)/sizeof(byte),
17172                     resultT, sizeof(resultT) + 1, a, sizeof(a));
17173         }
17174         if (gcmE == BAD_FUNC_ARG) {
17175             gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
17176                     sizeof(vector), iv, sizeof(iv)/sizeof(byte),
17177                     resultT, sizeof(resultT) - 5, a, sizeof(a));
17178         }
17179 
17180 #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
17181         (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
17182         defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
17183         /* FIPS does not check the lower bound of ivSz */
17184 #else
17185         if (gcmE == BAD_FUNC_ARG) {
17186             gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
17187                     sizeof(vector), iv, 0,
17188                     resultT, sizeof(resultT), a, sizeof(a));
17189         }
17190 #endif
17191         if (gcmE == BAD_FUNC_ARG) {
17192             gcmE = 0;
17193         } else {
17194             gcmE = WOLFSSL_FATAL_ERROR;
17195         }
17196     }
17197 
17198     /* This case is now considered good. Long IVs are now allowed.
17199      * Except for the original FIPS release, it still has an upper
17200      * bound on the IV length. */
17201 #if (!defined(HAVE_FIPS) || \
17202     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
17203     !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
17204     if (gcmE == 0) {
17205         gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
17206                         sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
17207                         a, sizeof(a));
17208     }
17209 #else
17210     (void)longIV;
17211 #endif /* Old FIPS */
17212     /* END wc_AesGcmEncrypt */
17213 
17214     printf(resultFmt, gcmE == 0 ? passed : failed);
17215     if (gcmE != 0) {
17216         wc_AesFree(&aes);
17217         return gcmE;
17218     }
17219 
17220     #ifdef HAVE_AES_DECRYPT
17221         printf(testingFmt, "wc_AesGcmDecrypt()");
17222 
17223         if (gcmD == 0) {
17224             gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
17225                                    iv, sizeof(iv)/sizeof(byte), resultT,
17226                                    sizeof(resultT), a, sizeof(a));
17227             if (gcmD == BAD_FUNC_ARG) {
17228                 gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
17229                                    iv, sizeof(iv)/sizeof(byte), resultT,
17230                                    sizeof(resultT), a, sizeof(a));
17231             }
17232             if (gcmD == BAD_FUNC_ARG) {
17233                 gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
17234                                    iv, sizeof(iv)/sizeof(byte), resultT,
17235                                    sizeof(resultT), a, sizeof(a));
17236             }
17237             if (gcmD == BAD_FUNC_ARG) {
17238                 gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
17239                                    NULL, sizeof(iv)/sizeof(byte), resultT,
17240                                    sizeof(resultT), a, sizeof(a));
17241             }
17242             if (gcmD == BAD_FUNC_ARG) {
17243                 gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
17244                                    iv, sizeof(iv)/sizeof(byte), NULL,
17245                                    sizeof(resultT), a, sizeof(a));
17246             }
17247             if (gcmD == BAD_FUNC_ARG) {
17248                 gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
17249                                    iv, sizeof(iv)/sizeof(byte), resultT,
17250                                    sizeof(resultT) + 1, a, sizeof(a));
17251             }
17252         #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
17253                 (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
17254                 !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
17255                 /* FIPS does not check the lower bound of ivSz */
17256         #else
17257             if (gcmD == BAD_FUNC_ARG) {
17258                 gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
17259                                    iv, 0, resultT,
17260                                    sizeof(resultT), a, sizeof(a));
17261             }
17262         #endif
17263             if (gcmD == BAD_FUNC_ARG) {
17264                 gcmD = 0;
17265             } else {
17266                 gcmD = WOLFSSL_FATAL_ERROR;
17267             }
17268         } /* END wc_AesGcmDecrypt */
17269 
17270         printf(resultFmt, gcmD == 0 ? passed : failed);
17271     #endif /* HAVE_AES_DECRYPT */
17272 
17273     wc_AesFree(&aes);
17274 #endif
17275 
17276     return ret;
17277 
17278 } /* END test_wc_AesGcmEncryptDecrypt */
17279 
17280 /*
17281  * unit test for wc_GmacSetKey()
17282  */
17283 static int test_wc_GmacSetKey (void)
17284 {
17285     int     ret = 0;
17286 #if !defined(NO_AES) && defined(HAVE_AESGCM)
17287     Gmac    gmac;
17288     byte    key16[] =
17289     {
17290         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17291         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17292     };
17293 #ifdef WOLFSSL_AES_192
17294     byte    key24[] =
17295     {
17296         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17297         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17298         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
17299     };
17300 #endif
17301 #ifdef WOLFSSL_AES_256
17302     byte    key32[] =
17303     {
17304         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17305         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17306         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17307         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17308     };
17309 #endif
17310     byte    badKey16[] =
17311     {
17312         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17313         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
17314     };
17315     byte    badKey24[] =
17316     {
17317         0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
17318         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
17319         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
17320     };
17321     byte    badKey32[] =
17322     {
17323         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17324         0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
17325         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
17326         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
17327     };
17328 
17329     printf(testingFmt, "wc_GmacSetKey()");
17330 
17331     ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
17332     if (ret != 0)
17333         return ret;
17334 
17335 #ifdef WOLFSSL_AES_128
17336     ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
17337 #endif
17338 #ifdef WOLFSSL_AES_192
17339     if (ret == 0) {
17340         ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
17341     }
17342 #endif
17343 #ifdef WOLFSSL_AES_256
17344     if (ret == 0) {
17345         ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
17346     }
17347 #endif
17348 
17349     /* Pass in bad args. */
17350     if (ret == 0) {
17351         ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
17352         if (ret == BAD_FUNC_ARG) {
17353             ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
17354         }
17355         if (ret == BAD_FUNC_ARG) {
17356             ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
17357         }
17358         if (ret == BAD_FUNC_ARG) {
17359             ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
17360         }
17361         if (ret == BAD_FUNC_ARG) {
17362             ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
17363         }
17364         if (ret == BAD_FUNC_ARG) {
17365             ret = 0;
17366         } else {
17367             ret = WOLFSSL_FATAL_ERROR;
17368         }
17369     }
17370 
17371     wc_AesFree(&gmac.aes);
17372 
17373     printf(resultFmt, ret == 0 ? passed : failed);
17374 
17375 #endif
17376     return ret;
17377 
17378 } /* END test_wc_GmacSetKey */
17379 
17380 /*
17381  * unit test for wc_GmacUpdate
17382  */
17383 static int test_wc_GmacUpdate (void)
17384 {
17385     int     ret = 0;
17386 #if !defined(NO_AES) && defined(HAVE_AESGCM)
17387     Gmac    gmac;
17388 #ifdef WOLFSSL_AES_128
17389     const byte key16[] =
17390     {
17391         0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
17392         0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
17393     };
17394 #endif
17395 #ifdef WOLFSSL_AES_192
17396     byte    key24[] =
17397     {
17398         0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
17399         0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
17400         0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
17401     };
17402 #endif
17403 #ifdef WOLFSSL_AES_256
17404    byte    key32[] =
17405     {
17406         0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
17407         0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
17408         0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
17409         0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
17410     };
17411 #endif
17412 #ifdef WOLFSSL_AES_128
17413     const byte authIn[] =
17414     {
17415         0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
17416         0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
17417     };
17418 #endif
17419 #ifdef WOLFSSL_AES_192
17420     const byte authIn2[] =
17421     {
17422        0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
17423        0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
17424     };
17425 #endif
17426     const byte authIn3[] =
17427     {
17428         0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
17429         0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
17430     };
17431 #ifdef WOLFSSL_AES_128
17432     const byte tag1[] = /* Known. */
17433     {
17434         0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
17435         0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
17436     };
17437 #endif
17438 #ifdef WOLFSSL_AES_192
17439     const byte tag2[] = /* Known */
17440     {
17441         0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
17442         0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
17443     };
17444 #endif
17445     const byte tag3[] = /* Known */
17446     {
17447         0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
17448         0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
17449     };
17450 #ifdef WOLFSSL_AES_128
17451     const byte iv[] =
17452     {
17453         0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
17454         0xe2, 0x8c, 0x8f, 0x16
17455     };
17456 #endif
17457 #ifdef WOLFSSL_AES_192
17458     const byte iv2[] =
17459     {
17460         0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
17461         0x7e, 0x1a, 0x6f, 0xbc
17462     };
17463 #endif
17464     const byte iv3[] =
17465     {
17466         0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
17467         0xc3, 0xfb, 0x6c, 0x8a
17468     };
17469     byte    tagOut[16];
17470     byte    tagOut2[24];
17471     byte    tagOut3[32];
17472 
17473     /* Init stack variables. */
17474     XMEMSET(tagOut, 0, sizeof(tagOut));
17475     XMEMSET(tagOut2, 0, sizeof(tagOut2));
17476     XMEMSET(tagOut3, 0, sizeof(tagOut3));
17477 
17478     printf(testingFmt, "wc_GmacUpdate()");
17479 
17480     ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
17481     if (ret != 0)
17482         return ret;
17483 
17484 #ifdef WOLFSSL_AES_128
17485     ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
17486     if (ret == 0) {
17487         ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
17488                                                     tagOut, sizeof(tag1));
17489         if (ret == 0) {
17490             ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
17491         }
17492     }
17493 #endif
17494 
17495 #ifdef WOLFSSL_AES_192
17496     if (ret == 0) {
17497         XMEMSET(&gmac, 0, sizeof(Gmac));
17498         ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
17499     }
17500     if (ret == 0) {
17501         ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
17502                             sizeof(authIn2), tagOut2, sizeof(tag2));
17503     }
17504     if (ret == 0) {
17505         ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
17506     }
17507 #endif
17508 
17509 #ifdef WOLFSSL_AES_256
17510     if (ret == 0) {
17511         XMEMSET(&gmac, 0, sizeof(Gmac));
17512         ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
17513     }
17514     if (ret == 0) {
17515         ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
17516                             sizeof(authIn3), tagOut3, sizeof(tag3));
17517     }
17518     if (ret == 0) {
17519         ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
17520     }
17521 #endif
17522 
17523     /*Pass bad args. */
17524     if (ret == 0) {
17525         ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
17526                                 sizeof(authIn3), tagOut3, sizeof(tag3));
17527         if (ret == BAD_FUNC_ARG) {
17528             ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
17529                                 sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
17530         }
17531         if (ret == BAD_FUNC_ARG) {
17532             ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
17533                                 sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
17534         }
17535         if (ret == BAD_FUNC_ARG) {
17536             ret = 0;
17537         } else {
17538             ret = WOLFSSL_FATAL_ERROR;
17539         }
17540     }
17541 
17542     wc_AesFree(&gmac.aes);
17543 
17544     printf(resultFmt, ret == 0 ? passed : failed);
17545 
17546 #endif
17547     return ret;
17548 
17549 } /* END test_wc_GmacUpdate */
17550 
17551 
17552 /*
17553  * testing wc_CamelliaSetKey
17554  */
17555 static int test_wc_CamelliaSetKey (void)
17556 {
17557     int ret = 0;
17558 #ifdef HAVE_CAMELLIA
17559     Camellia camellia;
17560     /*128-bit key*/
17561     static const byte key16[] =
17562     {
17563         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
17564         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
17565     };
17566     /* 192-bit key */
17567     static const byte key24[] =
17568     {
17569         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
17570         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
17571         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
17572     };
17573     /* 256-bit key */
17574     static const byte key32[] =
17575     {
17576         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
17577         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
17578         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
17579         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
17580     };
17581     static const byte iv[] =
17582     {
17583         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
17584         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
17585     };
17586 
17587     printf(testingFmt, "wc_CamelliaSetKey()");
17588 
17589     ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
17590     if (ret == 0) {
17591         ret = wc_CamelliaSetKey(&camellia, key16,
17592                                         (word32)sizeof(key16), NULL);
17593         if (ret == 0) {
17594             ret = wc_CamelliaSetKey(&camellia, key24,
17595                                         (word32)sizeof(key24), iv);
17596         }
17597         if (ret == 0) {
17598             ret = wc_CamelliaSetKey(&camellia, key24,
17599                                         (word32)sizeof(key24), NULL);
17600         }
17601         if (ret == 0) {
17602             ret = wc_CamelliaSetKey(&camellia, key32,
17603                                         (word32)sizeof(key32), iv);
17604         }
17605         if (ret == 0) {
17606             ret = wc_CamelliaSetKey(&camellia, key32,
17607                                         (word32)sizeof(key32), NULL);
17608         }
17609     }
17610     /* Bad args. */
17611     if (ret == 0) {
17612         ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
17613         if (ret != BAD_FUNC_ARG) {
17614             ret = WOLFSSL_FATAL_ERROR;
17615         } else {
17616             ret = 0;
17617         }
17618     } /* END bad args. */
17619 
17620 
17621 #endif
17622     return ret;
17623 
17624 } /* END test_wc_CammeliaSetKey */
17625 
17626 /*
17627  * Testing wc_CamelliaSetIV()
17628  */
17629 static int test_wc_CamelliaSetIV (void)
17630 {
17631     int ret = 0;
17632 #ifdef HAVE_CAMELLIA
17633     Camellia    camellia;
17634     static const byte iv[] =
17635     {
17636         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
17637         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
17638     };
17639 
17640     printf(testingFmt, "wc_CamelliaSetIV()");
17641 
17642     ret = wc_CamelliaSetIV(&camellia, iv);
17643     if (ret == 0) {
17644         ret = wc_CamelliaSetIV(&camellia, NULL);
17645     }
17646     /* Bad args. */
17647     if (ret == 0) {
17648         ret = wc_CamelliaSetIV(NULL, NULL);
17649         if (ret != BAD_FUNC_ARG) {
17650             ret = WOLFSSL_FATAL_ERROR;
17651         } else {
17652             ret = 0;
17653         }
17654     }
17655 
17656     printf(resultFmt, ret == 0 ? passed : failed);
17657 
17658 #endif
17659     return ret;
17660 } /*END test_wc_CamelliaSetIV*/
17661 
17662 /*
17663  * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
17664  */
17665 static int test_wc_CamelliaEncryptDecryptDirect (void)
17666 {
17667     int     ret = 0;
17668 #ifdef HAVE_CAMELLIA
17669     Camellia camellia;
17670     static const byte key24[] =
17671     {
17672         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
17673         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
17674         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
17675     };
17676     static const byte iv[] =
17677     {
17678         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
17679         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
17680     };
17681     static const byte plainT[] =
17682     {
17683         0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
17684         0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
17685     };
17686     byte    enc[sizeof(plainT)];
17687     byte    dec[sizeof(enc)];
17688     int     camE = WOLFSSL_FATAL_ERROR;
17689     int     camD = WOLFSSL_FATAL_ERROR;
17690 
17691     /*Init stack variables.*/
17692     XMEMSET(enc, 0, 16);
17693     XMEMSET(enc, 0, 16);
17694 
17695     ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
17696     if (ret == 0) {
17697         ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
17698         if (ret == 0) {
17699             ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
17700             if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
17701                 ret = WOLFSSL_FATAL_ERROR;
17702             }
17703         }
17704     }
17705     printf(testingFmt, "wc_CamelliaEncryptDirect()");
17706     /* Pass bad args. */
17707     if (ret == 0) {
17708         camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
17709         if (camE == BAD_FUNC_ARG) {
17710             camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
17711         }
17712         if (camE == BAD_FUNC_ARG) {
17713             camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
17714         }
17715         if (camE == BAD_FUNC_ARG) {
17716             camE = 0;
17717         } else {
17718             camE = WOLFSSL_FATAL_ERROR;
17719         }
17720     }
17721 
17722     printf(resultFmt, camE == 0 ? passed : failed);
17723     if (camE != 0) {
17724         return camE;
17725     }
17726 
17727     printf(testingFmt, "wc_CamelliaDecryptDirect()");
17728 
17729     if (ret == 0) {
17730         camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
17731         if (camD == BAD_FUNC_ARG) {
17732             camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
17733         }
17734         if (camD == BAD_FUNC_ARG) {
17735             camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
17736         }
17737         if (camD == BAD_FUNC_ARG) {
17738             camD = 0;
17739         } else {
17740             camD = WOLFSSL_FATAL_ERROR;
17741         }
17742     }
17743 
17744     printf(resultFmt, camD == 0 ? passed : failed);
17745     if (camD != 0) {
17746         return camD;
17747     }
17748 
17749 #endif
17750     return ret;
17751 
17752 } /* END test-wc_CamelliaEncryptDecryptDirect */
17753 
17754 /*
17755  * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
17756  */
17757 static int test_wc_CamelliaCbcEncryptDecrypt (void)
17758 {
17759     int     ret = 0;
17760 #ifdef HAVE_CAMELLIA
17761     Camellia camellia;
17762     static const byte key24[] =
17763     {
17764         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
17765         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
17766         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
17767     };
17768     static const byte plainT[] =
17769     {
17770         0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
17771         0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
17772     };
17773     byte    enc[CAMELLIA_BLOCK_SIZE];
17774     byte    dec[CAMELLIA_BLOCK_SIZE];
17775     int     camCbcE = WOLFSSL_FATAL_ERROR;
17776     int     camCbcD = WOLFSSL_FATAL_ERROR;
17777 
17778     /* Init stack variables. */
17779     XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
17780     XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
17781 
17782     ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
17783     if (ret == 0) {
17784         ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
17785         if (ret != 0) {
17786             ret = WOLFSSL_FATAL_ERROR;
17787         }
17788     }
17789     if (ret == 0) {
17790         ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
17791         if (ret == 0) {
17792             ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
17793             if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
17794                 ret = WOLFSSL_FATAL_ERROR;
17795             }
17796         }
17797     }
17798 
17799     printf(testingFmt, "wc_CamelliaCbcEncrypt");
17800     /* Pass in bad args. */
17801     if (ret == 0) {
17802         camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
17803         if (camCbcE == BAD_FUNC_ARG) {
17804             camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
17805                                                     CAMELLIA_BLOCK_SIZE);
17806         }
17807         if (camCbcE == BAD_FUNC_ARG) {
17808             camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
17809                                                     CAMELLIA_BLOCK_SIZE);
17810         }
17811         if (camCbcE == BAD_FUNC_ARG) {
17812             camCbcE = 0;
17813         } else {
17814             camCbcE = WOLFSSL_FATAL_ERROR;
17815         }
17816     }
17817 
17818     printf(resultFmt, camCbcE == 0 ? passed : failed);
17819     if (camCbcE != 0) {
17820         return camCbcE;
17821     }
17822 
17823     printf(testingFmt, "wc_CamelliaCbcDecrypt()");
17824 
17825     if (ret == 0) {
17826         camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
17827         if (camCbcD == BAD_FUNC_ARG) {
17828             camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
17829                                                     CAMELLIA_BLOCK_SIZE);
17830         }
17831         if (camCbcD == BAD_FUNC_ARG) {
17832             camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
17833                                                     CAMELLIA_BLOCK_SIZE);
17834         }
17835         if (camCbcD == BAD_FUNC_ARG) {
17836             camCbcD = 0;
17837         } else {
17838             camCbcD = WOLFSSL_FATAL_ERROR;
17839         }
17840     } /* END bad args. */
17841 
17842     printf(resultFmt, camCbcD == 0 ? passed : failed);
17843     if (camCbcD != 0) {
17844         return camCbcD;
17845     }
17846 
17847 #endif
17848     return ret;
17849 
17850 } /* END test_wc_CamelliaCbcEncryptDecrypt */
17851 
17852 /*
17853  * Testing wc_RabbitSetKey()
17854  */
17855 static int test_wc_RabbitSetKey (void)
17856 {
17857     int     ret = 0;
17858 #ifndef NO_RABBIT
17859     Rabbit  rabbit;
17860     const char* key =  "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
17861                         "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
17862     const char* iv =   "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
17863 
17864     printf(testingFmt, "wc_RabbitSetKey()");
17865 
17866     ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
17867 
17868     /* Test bad args. */
17869     if (ret == 0) {
17870         ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
17871         if (ret == BAD_FUNC_ARG) {
17872             ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
17873         }
17874         if (ret == BAD_FUNC_ARG) {
17875             ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
17876         }
17877     }
17878 
17879     printf(resultFmt, ret == 0 ? passed : failed);
17880 
17881 #endif
17882     return ret;
17883 
17884 } /* END test_wc_RabbitSetKey */
17885 
17886 /*
17887  * Test wc_RabbitProcess()
17888  */
17889 static int test_wc_RabbitProcess (void)
17890 {
17891     int     ret = 0;
17892 #if !defined(NO_RABBIT) && !defined(BIG_ENDIAN_ORDER)
17893     Rabbit  enc, dec;
17894     byte    cipher[25];
17895     byte    plain[25];
17896     const char* key     =  "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
17897                             "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
17898     const char* iv      =   "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
17899     const char* input   =   TEST_STRING;
17900     unsigned long int inlen = (unsigned long int)TEST_STRING_SZ;
17901 
17902     /* Initialize stack variables. */
17903     XMEMSET(cipher, 0, sizeof(cipher));
17904     XMEMSET(plain, 0, sizeof(plain));
17905 
17906     printf(testingFmt, "wc_RabbitProcess()");
17907 
17908     ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
17909     if (ret == 0) {
17910         ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
17911     }
17912     if (ret == 0) {
17913        ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
17914     }
17915     if (ret == 0) {
17916         ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
17917         if (ret != 0 || XMEMCMP(input, plain, inlen)) {
17918             ret = WOLFSSL_FATAL_ERROR;
17919         } else {
17920             ret = 0;
17921         }
17922     }
17923     /* Test bad args. */
17924     if (ret == 0) {
17925         ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
17926         if (ret == BAD_FUNC_ARG) {
17927             ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
17928         }
17929         if (ret == BAD_FUNC_ARG) {
17930             ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
17931         }
17932         if (ret == BAD_FUNC_ARG) {
17933             ret = 0;
17934         } else {
17935             ret = WOLFSSL_FATAL_ERROR;
17936         }
17937     }
17938 
17939     printf(resultFmt, ret == 0 ? passed : failed);
17940 
17941 #endif
17942     return ret;
17943 
17944 } /* END test_wc_RabbitProcess */
17945 
17946 
17947 
17948 
17949 
17950 /*
17951  * Testing wc_Arc4SetKey()
17952  */
17953 static int test_wc_Arc4SetKey (void)
17954 {
17955     int ret = 0;
17956 #ifndef NO_RC4
17957     Arc4 arc;
17958     const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
17959     int keyLen = 8;
17960 
17961     printf(testingFmt, "wc_Arch4SetKey()");
17962 
17963     ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
17964     /* Test bad args. */
17965     if (ret == 0) {
17966         ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
17967         if (ret == BAD_FUNC_ARG)
17968             ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
17969         if (ret == BAD_FUNC_ARG)
17970             ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
17971         if (ret == BAD_FUNC_ARG)
17972             ret = WOLFSSL_ERROR_NONE;
17973         else
17974             ret = WOLFSSL_FATAL_ERROR;
17975     } /* END test bad args. */
17976 
17977     printf(resultFmt, ret == 0 ? passed : failed);
17978 
17979 #endif
17980     return ret;
17981 
17982 } /* END test_wc_Arc4SetKey */
17983 
17984 /*
17985  * Testing wc_Arc4Process for ENC/DEC.
17986  */
17987 static int test_wc_Arc4Process (void)
17988 {
17989     int ret = 0;
17990 #ifndef NO_RC4
17991     Arc4 enc, dec;
17992     const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
17993     int keyLen = 8;
17994     const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
17995     byte cipher[8];
17996     byte plain[8];
17997 
17998     /* Init stack variables */
17999     XMEMSET(cipher, 0, sizeof(cipher));
18000     XMEMSET(plain, 0, sizeof(plain));
18001 
18002     /* Use for async. */
18003     ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
18004     if (ret == 0) {
18005         ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
18006     }
18007 
18008     printf(testingFmt, "wc_Arc4Process()");
18009 
18010     if (ret == 0) {
18011         ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
18012     }
18013     if (ret == 0) {
18014         ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
18015     }
18016     if (ret == 0) {
18017         ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
18018     }
18019     if (ret == 0) {
18020         ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
18021         if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
18022             ret = WOLFSSL_FATAL_ERROR;
18023         } else {
18024             ret = 0;
18025         }
18026     }
18027 
18028     /* Bad args. */
18029     if (ret == 0) {
18030         ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
18031         if (ret == BAD_FUNC_ARG) {
18032             ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
18033         }
18034         if (ret == BAD_FUNC_ARG) {
18035             ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
18036         }
18037         if (ret == BAD_FUNC_ARG) {
18038             ret = 0;
18039         } else {
18040             ret = WOLFSSL_FATAL_ERROR;
18041         }
18042     }
18043 
18044     printf(resultFmt, ret == 0 ? passed : failed);
18045 
18046     wc_Arc4Free(&enc);
18047     wc_Arc4Free(&dec);
18048 
18049 #endif
18050     return ret;
18051 
18052 }/* END test_wc_Arc4Process */
18053 
18054 
18055 /*
18056  * Testing wc_Init RsaKey()
18057  */
18058 static int test_wc_InitRsaKey (void)
18059 {
18060     int     ret = 0;
18061 #ifndef NO_RSA
18062     RsaKey  key;
18063 
18064     printf(testingFmt, "wc_InitRsaKey()");
18065 
18066     ret = wc_InitRsaKey(&key, NULL);
18067 
18068     /* Test bad args. */
18069     if (ret == 0) {
18070         ret = wc_InitRsaKey(NULL, NULL);
18071         #ifndef HAVE_USER_RSA
18072             if (ret == BAD_FUNC_ARG) {
18073                 ret = 0;
18074             } else {
18075         #else
18076             if (ret == USER_CRYPTO_ERROR) {
18077                 ret = 0;
18078             } else {
18079         #endif
18080             ret = WOLFSSL_FATAL_ERROR;
18081         }
18082     } /* end if */
18083 
18084     if (wc_FreeRsaKey(&key) || ret != 0) {
18085         ret = WOLFSSL_FATAL_ERROR;
18086     }
18087 
18088     printf(resultFmt, ret == 0 ? passed : failed);
18089 
18090 #endif
18091     return ret;
18092 } /* END test_wc_InitRsaKey */
18093 
18094 
18095 /*
18096  * Testing wc_RsaPrivateKeyDecode()
18097  */
18098 static int test_wc_RsaPrivateKeyDecode (void)
18099 {
18100     int     ret = 0;
18101 #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
18102         || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
18103     RsaKey  key;
18104     byte*   tmp;
18105     word32  idx = 0;
18106     int     bytes = 0;
18107 
18108     printf(testingFmt, "wc_RsaPrivateKeyDecode()");
18109 
18110     tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
18111     if (tmp == NULL) {
18112         ret = WOLFSSL_FATAL_ERROR;
18113     }
18114     if (ret == 0) {
18115         ret = wc_InitRsaKey(&key, NULL);
18116     }
18117     if (ret == 0) {
18118         #ifdef USE_CERT_BUFFERS_1024
18119             XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
18120             bytes = sizeof_client_key_der_1024;
18121         #else
18122             XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
18123             bytes = sizeof_client_key_der_2048;
18124         #endif /* Use cert buffers. */
18125 
18126         ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
18127     }
18128     #ifndef HAVE_USER_RSA
18129         /* Test bad args. */
18130         if (ret == 0) {
18131             ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
18132             if (ret == BAD_FUNC_ARG) {
18133                 ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
18134             }
18135             if (ret == BAD_FUNC_ARG) {
18136                 ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
18137             }
18138             if (ret == BAD_FUNC_ARG) {
18139                 ret = 0;
18140             } else {
18141                 ret = WOLFSSL_FATAL_ERROR;
18142             }
18143         }
18144     #else
18145         /* Test bad args. User RSA. */
18146         if (ret == 0) {
18147             ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
18148             if (ret == USER_CRYPTO_ERROR) {
18149                 ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
18150             }
18151             if (ret == USER_CRYPTO_ERROR) {
18152                 ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
18153             }
18154             if (ret == USER_CRYPTO_ERROR) {
18155                 ret = 0;
18156             } else {
18157                 ret = WOLFSSL_FATAL_ERROR;
18158             }
18159         }
18160     #endif
18161 
18162     if (tmp != NULL) {
18163         XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
18164     }
18165     if (wc_FreeRsaKey(&key) || ret != 0) {
18166         ret = WOLFSSL_FATAL_ERROR;
18167     }
18168 
18169     printf(resultFmt, ret == 0 ? passed : failed);
18170 
18171 #endif
18172     return ret;
18173 
18174 } /* END test_wc_RsaPrivateKeyDecode */
18175 
18176 /*
18177  * Testing wc_RsaPublicKeyDecode()
18178  */
18179 static int test_wc_RsaPublicKeyDecode (void)
18180 {
18181     int     ret = 0;
18182 #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
18183         || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
18184     RsaKey  keyPub;
18185     byte*   tmp;
18186     word32  idx = 0;
18187     int     bytes = 0;
18188     word32  keySz = 0;
18189     word32  tstKeySz = 0;
18190 
18191     tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
18192     if (tmp == NULL) {
18193         ret = WOLFSSL_FATAL_ERROR;
18194     }
18195     if (ret == 0) {
18196         ret = wc_InitRsaKey(&keyPub, NULL);
18197     }
18198     if (ret == 0) {
18199         #ifdef USE_CERT_BUFFERS_1024
18200             XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
18201             bytes = sizeof_client_keypub_der_1024;
18202             keySz = 1024;
18203         #else
18204             XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
18205             bytes = sizeof_client_keypub_der_2048;
18206             keySz = 2048;
18207         #endif
18208 
18209         printf(testingFmt, "wc_RsaPublicKeyDecode()");
18210 
18211         ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
18212     }
18213     #ifndef HAVE_USER_RSA
18214         /* Pass in bad args. */
18215         if (ret == 0) {
18216             ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
18217             if (ret == BAD_FUNC_ARG) {
18218                 ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
18219             }
18220             if (ret == BAD_FUNC_ARG) {
18221                 ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
18222             }
18223             if (ret == BAD_FUNC_ARG) {
18224                 ret = 0;
18225             } else {
18226                 ret = WOLFSSL_FATAL_ERROR;
18227             }
18228         }
18229     #else
18230         /* Pass in bad args. */
18231         if (ret == 0) {
18232             ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
18233             if (ret == USER_CRYPTO_ERROR) {
18234                 ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
18235             }
18236             if (ret == USER_CRYPTO_ERROR) {
18237                 ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
18238             }
18239             if (ret == USER_CRYPTO_ERROR) {
18240                 ret = 0;
18241             } else {
18242                 ret = WOLFSSL_FATAL_ERROR;
18243             }
18244         }
18245     #endif
18246 
18247     if (wc_FreeRsaKey(&keyPub) || ret != 0) {
18248         ret = WOLFSSL_FATAL_ERROR;
18249     }
18250 
18251     if (ret == 0) {
18252         /* Test for getting modulus key size */
18253         idx = 0;
18254         ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
18255             &tstKeySz, NULL, NULL);
18256         ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
18257     }
18258 
18259     if (tmp != NULL) {
18260         XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
18261     }
18262 
18263     printf(resultFmt, ret == 0 ? passed : failed);
18264 
18265 
18266 #endif
18267     return ret;
18268 
18269 }  /* END test_wc_RsaPublicKeyDecode */
18270 
18271 /*
18272  * Testing wc_RsaPublicKeyDecodeRaw()
18273  */
18274 static int test_wc_RsaPublicKeyDecodeRaw (void)
18275 {
18276     int         ret = 0;
18277 #if !defined(NO_RSA)
18278     RsaKey      key;
18279     const byte  n = 0x23;
18280     const byte  e = 0x03;
18281     int         nSz = sizeof(n);
18282     int         eSz = sizeof(e);
18283 
18284     printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
18285 
18286     ret = wc_InitRsaKey(&key, NULL);
18287     if (ret == 0) {
18288         ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
18289     }
18290 #ifndef HAVE_USER_RSA
18291     /* Pass in bad args. */
18292     if (ret == 0) {
18293         ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
18294         if (ret == BAD_FUNC_ARG) {
18295             ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
18296         }
18297         if (ret == BAD_FUNC_ARG) {
18298             ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
18299         }
18300         if (ret == BAD_FUNC_ARG) {
18301             ret = 0;
18302         } else {
18303             ret = WOLFSSL_FATAL_ERROR;
18304         }
18305     }
18306 #else
18307     /* Pass in bad args. User RSA. */
18308     if (ret == 0) {
18309         ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
18310         if (ret == USER_CRYPTO_ERROR) {
18311             ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
18312         }
18313         if (ret == USER_CRYPTO_ERROR) {
18314             ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
18315         }
18316         if (ret == USER_CRYPTO_ERROR) {
18317             ret = 0;
18318         } else {
18319             ret = WOLFSSL_FATAL_ERROR;
18320         }
18321     }
18322 #endif
18323 
18324     if (wc_FreeRsaKey(&key) || ret != 0) {
18325         ret = WOLFSSL_FATAL_ERROR;
18326     }
18327 
18328     printf(resultFmt, ret == 0 ? passed : failed);
18329 
18330 #endif
18331     return ret;
18332 
18333 } /* END test_wc_RsaPublicKeyDecodeRaw */
18334 
18335 
18336 #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
18337     /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
18338      * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
18339      * trying until it gets a probable prime. */
18340     #ifdef HAVE_FIPS
18341         static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
18342         {
18343             int ret;
18344 
18345             for (;;) {
18346                 ret = wc_MakeRsaKey(key, size, e, rng);
18347                 if (ret != PRIME_GEN_E) break;
18348                 printf("MakeRsaKey couldn't find prime; trying again.\n");
18349             }
18350 
18351             return ret;
18352         }
18353         #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
18354     #else
18355         #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
18356     #endif
18357 #endif
18358 
18359 
18360 /*
18361  * Testing wc_MakeRsaKey()
18362  */
18363 static int test_wc_MakeRsaKey (void)
18364 {
18365     int     ret = 0;
18366 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
18367 
18368     RsaKey  genKey;
18369     WC_RNG  rng;
18370     #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
18371         (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
18372     int     bits = 1024;
18373     #else
18374     int     bits = 2048;
18375     #endif
18376 
18377     printf(testingFmt, "wc_MakeRsaKey()");
18378 
18379     ret = wc_InitRsaKey(&genKey, NULL);
18380     if (ret == 0) {
18381         ret = wc_InitRng(&rng);
18382         if (ret == 0) {
18383             ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
18384             if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
18385                 ret = WOLFSSL_FATAL_ERROR;
18386             }
18387         }
18388     }
18389     #ifndef HAVE_USER_RSA
18390         /* Test bad args. */
18391         if (ret == 0) {
18392             ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
18393             if (ret == BAD_FUNC_ARG) {
18394                 ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
18395             }
18396             if (ret == BAD_FUNC_ARG) {
18397                 /* e < 3 */
18398                 ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
18399             }
18400             if (ret == BAD_FUNC_ARG) {
18401                 /* e & 1 == 0 */
18402                 ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
18403             }
18404             if (ret == BAD_FUNC_ARG) {
18405                 ret = 0;
18406             } else {
18407                 ret = WOLFSSL_FATAL_ERROR;
18408             }
18409         }
18410     #else
18411         /* Test bad args. */
18412         if (ret == 0) {
18413             ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
18414             if (ret == USER_CRYPTO_ERROR) {
18415                 ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
18416             }
18417             if (ret == USER_CRYPTO_ERROR) {
18418                 /* e < 3 */
18419                 ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
18420             }
18421             if (ret == USER_CRYPTO_ERROR) {
18422                 /* e & 1 == 0 */
18423                 ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
18424             }
18425             if (ret == USER_CRYPTO_ERROR) {
18426                 ret = 0;
18427             } else {
18428                 ret = WOLFSSL_FATAL_ERROR;
18429             }
18430         }
18431     #endif
18432 
18433     if (wc_FreeRng(&rng) || ret != 0) {
18434         ret = WOLFSSL_FATAL_ERROR;
18435     }
18436 
18437     printf(resultFmt, ret == 0 ? passed : failed);
18438 
18439 #endif
18440     return ret;
18441 
18442 } /* END test_wc_MakeRsaKey */
18443 
18444 /*
18445  * Test the bounds checking on the cipher text versus the key modulus.
18446  * 1. Make a new RSA key.
18447  * 2. Set c to 1.
18448  * 3. Decrypt c into k. (error)
18449  * 4. Copy the key modulus to c and sub 1 from the copy.
18450  * 5. Decrypt c into k. (error)
18451  * Valid bounds test cases are covered by all the other RSA tests.
18452  */
18453 static int test_RsaDecryptBoundsCheck(void)
18454 {
18455     int ret = 0;
18456 #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
18457     (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
18458     defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
18459     RsaKey key;
18460     byte flatC[256];
18461     word32 flatCSz;
18462     byte out[256];
18463     word32 outSz = sizeof(out);
18464     WC_RNG rng;
18465 
18466     printf(testingFmt, "RSA decrypt bounds check");
18467     XMEMSET(&rng, 0, sizeof(rng));
18468 
18469     ret = wc_InitRng(&rng);
18470 
18471     if (ret == 0)
18472         ret = wc_InitRsaKey(&key, NULL);
18473 
18474     if (ret == 0) {
18475         const byte* derKey;
18476         word32 derKeySz;
18477         word32 idx = 0;
18478 
18479         #ifdef USE_CERT_BUFFERS_1024
18480             derKey = server_key_der_1024;
18481             derKeySz = (word32)sizeof_server_key_der_1024;
18482             flatCSz = 128;
18483         #else
18484             derKey = server_key_der_2048;
18485             derKeySz = (word32)sizeof_server_key_der_2048;
18486             flatCSz = 256;
18487         #endif
18488 
18489         ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
18490     }
18491 
18492     if (ret == 0) {
18493         XMEMSET(flatC, 0, flatCSz);
18494         flatC[flatCSz-1] = 1;
18495 
18496         ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
18497                            RSA_PRIVATE_DECRYPT, &rng);
18498 
18499         if (ret == RSA_OUT_OF_RANGE_E) {
18500             mp_int c;
18501             mp_init_copy(&c, &key.n);
18502             mp_sub_d(&c, 1, &c);
18503             mp_to_unsigned_bin(&c, flatC);
18504             ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
18505                                RSA_PRIVATE_DECRYPT, NULL);
18506             mp_clear(&c);
18507         }
18508         if (ret == RSA_OUT_OF_RANGE_E)
18509             ret = 0;
18510         else
18511             ret = WOLFSSL_FATAL_ERROR;
18512     }
18513 
18514     if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
18515         ret = WOLFSSL_FATAL_ERROR;
18516 
18517     printf(resultFmt, ret == 0 ? passed : failed);
18518 
18519 
18520 #endif
18521     return ret;
18522 
18523 } /* END test_wc_RsaDecryptBoundsCheck */
18524 
18525 /*
18526  * Testing wc_SetKeyUsage()
18527  */
18528 static int test_wc_SetKeyUsage (void)
18529 {
18530     int     ret = 0;
18531 #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
18532     Cert    myCert;
18533 
18534     ret = wc_InitCert(&myCert);
18535 
18536     printf(testingFmt, "wc_SetKeyUsage()");
18537     if (ret == 0) {
18538         ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
18539         if (ret == 0) {
18540             ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
18541         }
18542         if (ret == 0) {
18543             ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
18544         }
18545         if (ret == 0) {
18546             ret = wc_SetKeyUsage(&myCert, "decipherOnly");
18547         }
18548         if (ret == 0) {
18549             ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
18550         }
18551     }
18552     /* Test bad args. */
18553     if (ret == 0) {
18554         ret = wc_SetKeyUsage(NULL, "decipherOnly");
18555         if (ret == BAD_FUNC_ARG) {
18556             ret = wc_SetKeyUsage(&myCert, NULL);
18557         }
18558         if (ret == BAD_FUNC_ARG) {
18559             ret = wc_SetKeyUsage(&myCert, "");
18560         }
18561         if (ret == KEYUSAGE_E) {
18562             ret = wc_SetKeyUsage(&myCert, ",");
18563         }
18564         if (ret == KEYUSAGE_E) {
18565             ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
18566         }
18567         if (ret == KEYUSAGE_E) {
18568             ret = 0;
18569         } else {
18570             ret = WOLFSSL_FATAL_ERROR;
18571         }
18572     }
18573 
18574     printf(resultFmt, ret == 0 ? passed : failed);
18575 
18576 #endif
18577     return ret;
18578 
18579 } /* END  test_wc_SetKeyUsage */
18580 /*
18581  * Testing wc_CheckProbablePrime()
18582  */
18583 static int test_wc_CheckProbablePrime (void)
18584 {
18585     int ret = 0;
18586 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
18587  !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
18588 
18589 #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
18590 
18591     RsaKey              key;
18592     WC_RNG              rng;
18593     byte                e[3];
18594     word32              eSz = (word32)sizeof(e);
18595     byte                n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
18596     word32              nSz = (word32)sizeof(n);
18597     byte                d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
18598     word32              dSz = (word32)sizeof(d);
18599     byte                p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
18600     word32              pSz = (word32)sizeof(p);
18601     byte                q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
18602     word32              qSz = (word32)sizeof(q);
18603     int                 nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
18604     int*                isPrime;
18605     int                 test[5];
18606     isPrime = test;
18607 
18608 
18609     printf(testingFmt, "wc_CheckProbablePrime()");
18610 
18611 
18612     ret = wc_InitRsaKey(&key, NULL);
18613     if (ret == 0) {
18614         ret = wc_InitRng(&rng);
18615     }
18616     if (ret == 0) {
18617         ret = wc_RsaSetRNG(&key, &rng);
18618     }
18619     if (ret == 0) {
18620         ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
18621     }
18622     if (ret == 0) {
18623         PRIVATE_KEY_UNLOCK();
18624         ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
18625                                 p, &pSz, q, &qSz);
18626         PRIVATE_KEY_LOCK();
18627     }
18628     /* Bad cases */
18629     if (ret == 0) {
18630         ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
18631                                      nlen, isPrime);
18632         if (ret == BAD_FUNC_ARG) {
18633             ret = 0;
18634         }
18635     }
18636     if (ret == 0) {
18637         ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
18638                                      nlen, isPrime);
18639         if (ret == BAD_FUNC_ARG) {
18640             ret = 0;
18641         }
18642     }
18643     if (ret == 0) {
18644         ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
18645                                      nlen, isPrime);
18646         if (ret == BAD_FUNC_ARG) {
18647             ret = 0;
18648         }
18649     }
18650     if (ret == 0) {
18651         ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
18652                                      nlen, isPrime);
18653         if (ret == BAD_FUNC_ARG) {
18654             ret = 0;
18655         }
18656     }
18657     if (ret == 0) {
18658         ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
18659                                      nlen, isPrime);
18660         if (ret == BAD_FUNC_ARG) {
18661             ret = 0;
18662         }
18663     }
18664     if (ret == 0) {
18665         ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
18666                                      nlen, isPrime);
18667         if (ret == BAD_FUNC_ARG) {
18668             ret = 0;
18669         }
18670     }
18671     if (ret == 0) {
18672         ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
18673                                      nlen, isPrime);
18674         if (ret == BAD_FUNC_ARG) {
18675             ret = 0;
18676         }
18677     }
18678 
18679     /* Good case */
18680     if (ret == 0) {
18681         ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
18682                                      nlen, isPrime);
18683     }
18684     wc_FreeRsaKey(&key);
18685     wc_FreeRng(&rng);
18686 
18687     printf(resultFmt, ret == 0 ? passed : failed);
18688 
18689 #undef CHECK_PROBABLE_PRIME_KEY_BITS
18690 
18691 #endif
18692 
18693     return ret;
18694 } /* END  test_wc_CheckProbablePrime */
18695 /*
18696  * Testing wc_RsaPSS_Verify()
18697  */
18698 static int test_wc_RsaPSS_Verify (void)
18699 {
18700     int ret = 0;
18701 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
18702  !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
18703     RsaKey              key;
18704     WC_RNG              rng;
18705     int                 sz = 256;
18706     byte*               pt;
18707     const char*         szMessage = "This is the string to be signed";
18708     unsigned char       pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
18709     unsigned char       pDecrypted[2048/8];
18710     word32              outLen = sizeof(pDecrypted);
18711     pt = pDecrypted;
18712 
18713     printf(testingFmt, "wc_RsaPSS_Verify()");
18714 
18715     ret = wc_InitRsaKey(&key, NULL);
18716 
18717     if (ret == 0) {
18718         ret = wc_InitRng(&rng);
18719     }
18720     if (ret == 0) {
18721         ret = wc_RsaSetRNG(&key, &rng);
18722     }
18723     if (ret == 0) {
18724             ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
18725     }
18726 
18727     if (ret == 0) {
18728         ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
18729                 pSignature, sizeof(pSignature),
18730                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
18731         if (ret > 0 ){
18732             sz = ret;
18733             ret = 0;
18734         }
18735     }
18736     /* Bad cases */
18737     if (ret == 0) {
18738         ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
18739                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18740             if (ret == BAD_FUNC_ARG) {
18741                 ret = 0;
18742             }
18743     }
18744     if (ret == 0) {
18745         ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
18746                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18747             if (ret == BAD_FUNC_ARG) {
18748                 ret = 0;
18749             }
18750     }
18751     if (ret == 0) {
18752         ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
18753                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18754             if (ret == BAD_FUNC_ARG) {
18755                 ret = 0;
18756             }
18757     }
18758     if (ret == 0) {
18759         ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
18760                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18761             if (ret == BAD_FUNC_ARG) {
18762                 ret = 0;
18763             }
18764     }
18765     /* Good case */
18766     if (ret == 0) {
18767         ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
18768                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18769             if (ret > 0) {
18770                 ret = 0;
18771             }
18772     }
18773     wc_FreeRsaKey(&key);
18774     wc_FreeRng(&rng);
18775     printf(resultFmt, ret == 0 ? passed : failed);
18776 #endif
18777 
18778     return ret;
18779 } /* END  test_wc_RsaPSS_Verify */
18780 /*
18781  * Testing wc_RsaPSS_VerifyCheck()
18782  */
18783 static int test_wc_RsaPSS_VerifyCheck (void)
18784 {
18785     int ret = 0;
18786 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
18787  !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
18788     RsaKey              key;
18789     WC_RNG              rng;
18790     int                 sz = 256; /* 2048/8 */
18791     byte*               pt;
18792     byte                digest[32];
18793     word32              digestSz;
18794     unsigned char       pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
18795     word32              pSignatureSz = sizeof(pSignature);
18796     unsigned char       pDecrypted[2048/8];
18797     word32              outLen = sizeof(pDecrypted);
18798     pt = pDecrypted;
18799 
18800     printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
18801 
18802     XMEMSET(digest, 0, sizeof(digest));
18803     XMEMSET(pSignature, 0, sizeof(pSignature));
18804 
18805     ret = wc_InitRsaKey(&key, NULL);
18806 
18807     if (ret == 0) {
18808         ret = wc_InitRng(&rng);
18809     }
18810     if (ret == 0) {
18811         ret = wc_RsaSetRNG(&key, &rng);
18812     }
18813     if (ret == 0) {
18814             ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
18815     }
18816     if (ret == 0) {
18817         digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
18818         ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
18819 
18820     }
18821 
18822     if (ret == 0) {
18823         ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
18824                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
18825         if (ret > 0 ){
18826             sz = ret;
18827             ret = 0;
18828         }
18829     }
18830     /* Bad cases */
18831     if (ret == 0) {
18832         ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
18833                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18834             if (ret == BAD_FUNC_ARG) {
18835                 ret = 0;
18836             }
18837     }
18838     if (ret == 0) {
18839         ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
18840                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18841             if (ret == BAD_FUNC_ARG) {
18842                 ret = 0;
18843             }
18844     }
18845     if (ret == 0) {
18846         ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
18847                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18848             if (ret == BAD_FUNC_ARG) {
18849                 ret = 0;
18850             }
18851     }
18852     if (ret == 0) {
18853         ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
18854                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18855             if (ret == BAD_FUNC_ARG) {
18856                 ret = 0;
18857             }
18858     }
18859 
18860     /* Good case */
18861     if (ret == 0) {
18862         ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
18863                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18864             if (ret > 0) {
18865                 ret = 0;
18866             }
18867     }
18868     wc_FreeRsaKey(&key);
18869     wc_FreeRng(&rng);
18870     printf(resultFmt, ret == 0 ? passed : failed);
18871 #endif
18872 
18873     return ret;
18874 } /* END  test_wc_RsaPSS_VerifyCheck */
18875 /*
18876  * Testing wc_RsaPSS_VerifyCheckInline()
18877  */
18878 static int test_wc_RsaPSS_VerifyCheckInline (void)
18879 {
18880     int ret = 0;
18881 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
18882  !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
18883     RsaKey              key;
18884     WC_RNG              rng;
18885     int                 sz = 256;
18886     byte*               pt;
18887     byte                digest[32];
18888     word32              digestSz;
18889     unsigned char       pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
18890     unsigned char       pDecrypted[2048/8];
18891     pt = pDecrypted;
18892 
18893 
18894     printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
18895 
18896     ret = wc_InitRsaKey(&key, NULL);
18897 
18898     XMEMSET(digest, 0, sizeof(digest));
18899     XMEMSET(pSignature, 0, sizeof(pSignature));
18900 
18901     if (ret == 0) {
18902         ret = wc_InitRng(&rng);
18903     }
18904     if (ret == 0) {
18905         ret = wc_RsaSetRNG(&key, &rng);
18906     }
18907     if (ret == 0) {
18908             ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
18909     }
18910     if (ret == 0) {
18911         digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
18912         ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
18913 
18914     }
18915 
18916     if (ret == 0) {
18917         ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
18918                 WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
18919         if (ret > 0 ){
18920             sz = ret;
18921             ret = 0;
18922         }
18923     }
18924     /* Bad Cases */
18925     if (ret == 0) {
18926         ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
18927                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18928             if (ret == BAD_FUNC_ARG) {
18929                 ret = 0;
18930             }
18931     }
18932     if (ret == 0) {
18933         ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
18934                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18935             if (ret == BAD_FUNC_ARG) {
18936                 ret = 0;
18937             }
18938     }
18939     if (ret == 0) {
18940         ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
18941                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18942             if (ret == BAD_FUNC_ARG) {
18943                 ret = 0;
18944             }
18945     }
18946     if (ret == 0) {
18947         ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
18948                 digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
18949             if (ret == BAD_FUNC_ARG) {
18950                 ret = 0;
18951             }
18952     }
18953     /* Good case */
18954     if (ret == 0) {
18955         ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
18956                 digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
18957             if (ret > 0) {
18958                 ret = 0;
18959             }
18960     }
18961     wc_FreeRsaKey(&key);
18962     wc_FreeRng(&rng);
18963     printf(resultFmt, ret == 0 ? passed : failed);
18964 #endif
18965 
18966     return ret;
18967 } /* END  test_wc_RsaPSS_VerifyCheckInline */
18968 
18969 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
18970 static void sample_mutex_cb (int flag, int type, const char* file, int line)
18971 {
18972     (void)flag;
18973     (void)type;
18974     (void)file;
18975     (void)line;
18976 }
18977 #endif
18978 /*
18979  * Testing wc_LockMutex_ex
18980  */
18981 static int test_wc_LockMutex_ex (void)
18982 {
18983     int ret = 0;
18984 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
18985     int flag = CRYPTO_LOCK;
18986     int type = 0;
18987     const char* file = "./test-LockMutex_ex.txt";
18988     int line = 0;
18989 
18990     printf(testingFmt, "wc_LockMutex_ex()");
18991 
18992     /*without SetMutexCb*/
18993     ret = wc_LockMutex_ex(flag, type, file, line);
18994     if (ret ==  BAD_STATE_E) {
18995         ret = 0;
18996     }
18997     /*with SetMutexCb*/
18998     if (ret == 0) {
18999         ret = wc_SetMutexCb(sample_mutex_cb);
19000         if (ret == 0) {
19001              ret = wc_LockMutex_ex(flag, type, file, line);
19002         }
19003     }
19004 
19005     printf(resultFmt, ret == 0 ? passed : failed);
19006 
19007 #endif
19008     return ret;
19009 }/*End test_wc_LockMutex_ex*/
19010 /*
19011  * Testing wc_SetMutexCb
19012  */
19013 static int test_wc_SetMutexCb (void)
19014 {
19015     int ret = 0;
19016 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
19017 
19018     printf(testingFmt, "wc_SetMutexCb()");
19019 
19020     ret = wc_SetMutexCb(sample_mutex_cb);
19021 
19022     printf(resultFmt, ret == 0 ? passed : failed);
19023 #endif
19024     return ret;
19025 }/*End test_wc_SetMutexCb*/
19026 
19027 /*
19028  * Testing wc_RsaKeyToDer()
19029  */
19030 static int test_wc_RsaKeyToDer (void)
19031 {
19032     int     ret = 0;
19033 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19034     RsaKey  genKey;
19035     WC_RNG  rng;
19036     byte*   der;
19037     #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
19038         (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
19039     int     bits = 1024;
19040     word32  derSz = 611;
19041     /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
19042        + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
19043     #else
19044     int     bits = 2048;
19045     word32  derSz = 1196;
19046     /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
19047        + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
19048     #endif
19049     XMEMSET(&rng, 0, sizeof(rng));
19050     XMEMSET(&genKey, 0, sizeof(genKey));
19051 
19052     der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
19053     if (der == NULL) {
19054         ret = WOLFSSL_FATAL_ERROR;
19055     }
19056     /* Init structures. */
19057     if (ret == 0) {
19058         ret = wc_InitRsaKey(&genKey, NULL);
19059     }
19060         if (ret == 0) {
19061         ret = wc_InitRng(&rng);
19062     }
19063     /* Make key. */
19064     if (ret == 0) {
19065         ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
19066         if (ret != 0) {
19067             ret = WOLFSSL_FATAL_ERROR;
19068         }
19069     }
19070 
19071     printf(testingFmt, "wc_RsaKeyToDer()");
19072 
19073     if (ret == 0) {
19074         ret = wc_RsaKeyToDer(&genKey, der, derSz);
19075         if (ret > 0) {
19076             ret = 0;
19077         } else {
19078             ret = WOLFSSL_FATAL_ERROR;
19079         }
19080     }
19081     #ifndef HAVE_USER_RSA
19082         /* Pass good/bad args. */
19083         if (ret == 0) {
19084             ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
19085             if (ret == BAD_FUNC_ARG) {
19086                 /* Get just the output length */
19087                 ret = wc_RsaKeyToDer(&genKey, NULL, 0);
19088             }
19089             if (ret > 0) {
19090                 /* Try Public Key. */
19091                 genKey.type = 0;
19092                 ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
19093             }
19094             if (ret == BAD_FUNC_ARG) {
19095                 ret = 0;
19096             } else {
19097                 ret = WOLFSSL_FATAL_ERROR;
19098             }
19099         }
19100     #else
19101         /* Pass good/bad args. */
19102         if (ret == 0) {
19103             ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
19104             if (ret == USER_CRYPTO_ERROR) {
19105                 /* Get just the output length */
19106                 ret = wc_RsaKeyToDer(&genKey, NULL, 0);
19107             }
19108             if (ret > 0) {
19109                 /* Try Public Key. */
19110                 genKey.type = 0;
19111                 ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
19112             }
19113             if (ret == USER_CRYPTO_ERROR) {
19114                 ret = 0;
19115             } else {
19116                 ret = WOLFSSL_FATAL_ERROR;
19117             }
19118         }
19119     #endif
19120 
19121     if (der != NULL) {
19122         XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
19123     }
19124     if (wc_FreeRsaKey(&genKey) || ret != 0) {
19125         ret = WOLFSSL_FATAL_ERROR;
19126     }
19127     if (wc_FreeRng(&rng) || ret != 0) {
19128         ret = WOLFSSL_FATAL_ERROR;
19129     }
19130 
19131     printf(resultFmt, ret == 0 ? passed : failed);
19132 
19133 #endif
19134     return ret;
19135 } /* END test_wc_RsaKeyToDer */
19136 
19137 /*
19138  *  Testing wc_RsaKeyToPublicDer()
19139  */
19140 static int test_wc_RsaKeyToPublicDer (void)
19141 {
19142     int         ret = 0;
19143 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19144     RsaKey      key;
19145     WC_RNG      rng;
19146     byte*       der;
19147     #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
19148         (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
19149     int         bits = 1024;
19150     word32      derLen = 162;
19151     #else
19152     int         bits = 2048;
19153     word32      derLen = 294;
19154     #endif
19155 
19156     XMEMSET(&rng, 0, sizeof(rng));
19157     XMEMSET(&key, 0, sizeof(key));
19158 
19159     der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
19160     if (der == NULL) {
19161         ret = WOLFSSL_FATAL_ERROR;
19162     }
19163     if (ret == 0) {
19164         ret = wc_InitRsaKey(&key, NULL);
19165     }
19166     if (ret == 0) {
19167         ret = wc_InitRng(&rng);
19168     }
19169     if (ret == 0) {
19170         ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
19171     }
19172 
19173     printf(testingFmt, "wc_RsaKeyToPublicDer()");
19174 
19175     if (ret == 0) {
19176         /* test getting size only */
19177         ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
19178         if (ret >= 0)
19179             ret = 0;
19180     }
19181     if (ret == 0) {
19182         ret = wc_RsaKeyToPublicDer(&key, der, derLen);
19183         if (ret >= 0) {
19184             ret = 0;
19185         } else {
19186             ret = WOLFSSL_FATAL_ERROR;
19187         }
19188     }
19189 
19190     if (ret == 0) {
19191         /* test getting size only */
19192         ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
19193         if (ret >= 0)
19194             ret = 0;
19195     }
19196     if (ret == 0) {
19197         ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
19198         if (ret >= 0) {
19199             ret = 0;
19200         } else {
19201             ret = WOLFSSL_FATAL_ERROR;
19202         }
19203     }
19204 
19205     #ifndef HAVE_USER_RSA
19206         /* Pass in bad args. */
19207         if (ret == 0) {
19208             ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
19209             if (ret == BAD_FUNC_ARG) {
19210                 ret = wc_RsaKeyToPublicDer(&key, der, -1);
19211             }
19212             if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
19213                 ret = 0;
19214             } else {
19215                 ret = WOLFSSL_FATAL_ERROR;
19216             }
19217         }
19218     #else
19219         /* Pass in bad args. */
19220         if (ret == 0) {
19221             ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
19222             if (ret == USER_CRYPTO_ERROR) {
19223                 ret = wc_RsaKeyToPublicDer(&key, der, -1);
19224             }
19225             if (ret == USER_CRYPTO_ERROR) {
19226                 ret = 0;
19227             } else {
19228                 ret = WOLFSSL_FATAL_ERROR;
19229             }
19230         }
19231     #endif
19232 
19233     if (der != NULL) {
19234         XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
19235     }
19236     if (wc_FreeRsaKey(&key) || ret != 0) {
19237         ret = WOLFSSL_FATAL_ERROR;
19238     }
19239     if (wc_FreeRng(&rng) || ret != 0) {
19240         ret = WOLFSSL_FATAL_ERROR;
19241     }
19242 
19243     printf(resultFmt, ret == 0 ? passed : failed);
19244 
19245 #endif
19246     return ret;
19247 
19248 } /* END test_wc_RsaKeyToPublicDer */
19249 
19250 /*
19251  *  Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
19252  */
19253 static int test_wc_RsaPublicEncryptDecrypt (void)
19254 {
19255     int     ret = 0;
19256 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19257     RsaKey  key;
19258     WC_RNG  rng;
19259     const char inStr[] = TEST_STRING;
19260     const word32 plainLen = (word32)TEST_STRING_SZ;
19261     const word32 inLen = (word32)TEST_STRING_SZ;
19262     int          bits = TEST_RSA_BITS;
19263     const word32 cipherLen = TEST_RSA_BYTES;
19264     word32 cipherLenResult = cipherLen;
19265 
19266     WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
19267     WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
19268     WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
19269 
19270 #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
19271     if (in == NULL || plain == NULL || cipher == NULL) {
19272         printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
19273         return MEMORY_E;
19274 }
19275 #endif
19276     XMEMCPY(in, inStr, inLen);
19277 
19278     ret = wc_InitRsaKey(&key, NULL);
19279     if (ret == 0) {
19280         ret = wc_InitRng(&rng);
19281     }
19282     if (ret == 0) {
19283         ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
19284     }
19285     /* Encrypt. */
19286     printf(testingFmt, "wc_RsaPublicEncrypt()");
19287 
19288     if (ret == 0) {
19289         ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
19290         if (ret >= 0) {
19291             cipherLenResult = ret;
19292             ret = 0;
19293         } else {
19294             ret = WOLFSSL_FATAL_ERROR;
19295         }
19296     }
19297 
19298     /* Pass bad args. */
19299    /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
19300     printf(resultFmt, ret == 0 ? passed : failed);
19301     if (ret != 0) {
19302         return ret;
19303     }
19304 
19305     /* Decrypt */
19306     printf(testingFmt, "wc_RsaPrivateDecrypt()");
19307     #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
19308         /* Bind rng */
19309         if (ret == 0) {
19310             ret = wc_RsaSetRNG(&key, &rng);
19311         }
19312     #endif
19313     if (ret == 0) {
19314         ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
19315     }
19316     if (ret >= 0) {
19317         ret = XMEMCMP(plain, inStr, plainLen);
19318     }
19319 
19320     /* Pass in bad args. */
19321    /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
19322 
19323     WC_FREE_VAR(in, NULL);
19324     WC_FREE_VAR(plain, NULL);
19325     WC_FREE_VAR(cipher, NULL);
19326     if (wc_FreeRsaKey(&key) || ret != 0) {
19327         ret = WOLFSSL_FATAL_ERROR;
19328     }
19329     if (wc_FreeRng(&rng) || ret != 0) {
19330         ret = WOLFSSL_FATAL_ERROR;
19331     }
19332 
19333     printf(resultFmt, ret == 0 ? passed : failed);
19334 
19335 #endif
19336     return ret;
19337 
19338 } /* END test_wc_RsaPublicEncryptDecrypt */
19339 
19340 /*
19341  * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
19342  */
19343 static int test_wc_RsaPublicEncryptDecrypt_ex (void)
19344 {
19345     int     ret = 0;
19346 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
19347         && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
19348         && !defined(NO_SHA)
19349     RsaKey  key;
19350     WC_RNG  rng;
19351     const char inStr[] = TEST_STRING;
19352     const word32 inLen = (word32)TEST_STRING_SZ;
19353     const word32 plainSz = (word32)TEST_STRING_SZ;
19354     byte*   res = NULL;
19355     int     idx = 0;
19356     int          bits = TEST_RSA_BITS;
19357     const word32 cipherSz = TEST_RSA_BYTES;
19358 
19359     WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
19360     WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
19361     WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
19362 
19363 #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
19364     if (in == NULL || plain == NULL || cipher == NULL) {
19365         printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
19366         return MEMORY_E;
19367     }
19368 #endif
19369     XMEMCPY(in, inStr, inLen);
19370 
19371     /* Initialize stack structures. */
19372     XMEMSET(&rng, 0, sizeof(rng));
19373     XMEMSET(&key, 0, sizeof(key));
19374 
19375     ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
19376     if (ret == 0) {
19377         ret = wc_InitRng(&rng);
19378     }
19379     if (ret == 0) {
19380         ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
19381     }
19382     /* Encrypt */
19383     printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
19384     if (ret == 0) {
19385         ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
19386                 WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
19387         if (ret >= 0) {
19388             idx = ret;
19389             ret = 0;
19390         } else {
19391             ret = WOLFSSL_FATAL_ERROR;
19392         }
19393     }
19394 
19395     /*Pass bad args.*/
19396    /* Tests RsaPublicEncryptEx again. No need duplicate. */
19397     printf(resultFmt, ret == 0 ? passed : failed);
19398     if (ret != 0) {
19399         return ret;
19400     }
19401 
19402 #ifndef WOLFSSL_RSA_PUBLIC_ONLY
19403     /* Decrypt */
19404     printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
19405     #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
19406         if (ret == 0) {
19407             ret = wc_RsaSetRNG(&key, &rng);
19408         }
19409     #endif
19410     if (ret == 0) {
19411         ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
19412                 plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
19413                 WC_MGF1SHA1, NULL, 0);
19414     }
19415     if (ret >= 0) {
19416         if (!XMEMCMP(plain, inStr, plainSz)) {
19417             ret = 0;
19418         } else {
19419             ret = WOLFSSL_FATAL_ERROR;
19420         }
19421     }
19422 
19423     /*Pass bad args.*/
19424    /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
19425     printf(resultFmt, ret == 0 ? passed : failed);
19426     if (ret != 0) {
19427         return ret;
19428     }
19429 
19430     printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
19431     if (ret == 0) {
19432         ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
19433                 &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
19434                 WC_MGF1SHA1, NULL, 0);
19435 
19436         if (ret >= 0) {
19437             if (!XMEMCMP(inStr, res, plainSz)) {
19438                 ret = 0;
19439             } else {
19440                 ret = WOLFSSL_FATAL_ERROR;
19441             }
19442         }
19443     }
19444 #endif
19445 
19446     WC_FREE_VAR(in, NULL);
19447     WC_FREE_VAR(plain, NULL);
19448     WC_FREE_VAR(cipher, NULL);
19449     if (wc_FreeRsaKey(&key) || ret != 0) {
19450         ret = WOLFSSL_FATAL_ERROR;
19451     }
19452     if (wc_FreeRng(&rng) || ret != 0) {
19453         ret = WOLFSSL_FATAL_ERROR;
19454     }
19455 
19456     printf(resultFmt, ret == 0 ? passed : failed);
19457 
19458 #endif
19459     return ret;
19460 
19461 } /* END test_wc_RsaPublicEncryptDecrypt_ex */
19462 
19463 /*
19464  * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
19465  */
19466 static int test_wc_RsaSSL_SignVerify (void)
19467 {
19468     int     ret = 0;
19469 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19470     RsaKey  key;
19471     WC_RNG  rng;
19472     const char inStr[] = TEST_STRING;
19473     const word32 plainSz = (word32)TEST_STRING_SZ;
19474     const word32 inLen = (word32)TEST_STRING_SZ;
19475     word32  idx = 0;
19476     int          bits = TEST_RSA_BITS;
19477     const word32 outSz = TEST_RSA_BYTES;
19478 
19479     WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
19480     WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
19481     WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
19482 
19483 #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
19484     if (in == NULL || out == NULL || plain == NULL) {
19485         printf("test_wc_RsaSSL_SignVerify failed\n");
19486         return MEMORY_E;
19487     }
19488 #endif
19489     XMEMCPY(in, inStr, inLen);
19490 
19491     ret = wc_InitRsaKey(&key, NULL);
19492 
19493     if (ret == 0) {
19494         ret = wc_InitRng(&rng);
19495     }
19496 
19497     if (ret == 0) {
19498         ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
19499     }
19500     /* Sign. */
19501     printf(testingFmt, "wc_RsaSSL_Sign()");
19502 
19503     if (ret == 0) {
19504         ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
19505         if (ret == (int)outSz) {
19506             idx = ret;
19507             ret = 0;
19508         } else {
19509             ret = WOLFSSL_FATAL_ERROR;
19510         }
19511     }
19512 #ifndef HAVE_USER_RSA
19513     /* Test bad args. */
19514     if (ret == 0) {
19515         ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
19516         if (ret == BAD_FUNC_ARG) {
19517             ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
19518         }
19519         if (ret == BAD_FUNC_ARG) {
19520             ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
19521         }
19522         if (ret == BAD_FUNC_ARG) {
19523             ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
19524         }
19525         if (ret == BAD_FUNC_ARG) {
19526             ret = 0;
19527         } else {
19528             ret = WOLFSSL_FATAL_ERROR;
19529         }
19530     }
19531 #else
19532     /* Test bad args. */
19533     if (ret == 0) {
19534         ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
19535         if (ret == USER_CRYPTO_ERROR) {
19536             ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
19537         }
19538         if (ret == USER_CRYPTO_ERROR) {
19539             ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
19540         }
19541         if (ret == USER_CRYPTO_ERROR) {
19542            ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
19543         }
19544         if (ret == USER_CRYPTO_ERROR) {
19545             ret = 0;
19546         } else {
19547             ret = WOLFSSL_FATAL_ERROR;
19548         }
19549     }
19550 #endif
19551     printf(resultFmt, ret == 0 ? passed : failed);
19552     if (ret != 0) {
19553         return ret;
19554     }
19555 
19556     /* Verify. */
19557     printf(testingFmt, "wc_RsaSSL_Verify()");
19558 
19559     if (ret == 0) {
19560         ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
19561         if (ret == (int)inLen) {
19562             ret = 0;
19563         } else {
19564             ret = WOLFSSL_FATAL_ERROR;
19565         }
19566     }
19567     #ifndef HAVE_USER_RSA
19568         /* Pass bad args. */
19569          if (ret == 0) {
19570                 ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
19571             if (ret == BAD_FUNC_ARG) {
19572                 ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
19573             }
19574             if (ret == BAD_FUNC_ARG) {
19575                 ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
19576             }
19577             if (ret == BAD_FUNC_ARG) {
19578                ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
19579             }
19580             if (ret == BAD_FUNC_ARG) {
19581                 ret = 0;
19582             } else {
19583                 ret = WOLFSSL_FATAL_ERROR;
19584             }
19585         }
19586     #else
19587         /* Pass bad args. */
19588          if (ret == 0) {
19589                 ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
19590             if (ret == USER_CRYPTO_ERROR) {
19591                 ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
19592             }
19593             if (ret == USER_CRYPTO_ERROR) {
19594                 ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
19595             }
19596             if (ret == USER_CRYPTO_ERROR) {
19597                ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
19598             }
19599             if (ret == USER_CRYPTO_ERROR) {
19600                 ret = 0;
19601             } else {
19602                 ret = WOLFSSL_FATAL_ERROR;
19603             }
19604         }
19605     #endif
19606 
19607     WC_FREE_VAR(in, NULL);
19608     WC_FREE_VAR(out, NULL);
19609     WC_FREE_VAR(plain, NULL);
19610     if (wc_FreeRsaKey(&key) || ret != 0) {
19611         ret = WOLFSSL_FATAL_ERROR;
19612     }
19613     if (wc_FreeRng(&rng) || ret != 0) {
19614         ret = WOLFSSL_FATAL_ERROR;
19615     }
19616 
19617     printf(resultFmt, ret == 0 ? passed : failed);
19618 
19619 #endif
19620     return ret;
19621 
19622 } /* END test_wc_RsaSSL_SignVerify */
19623 
19624 /*
19625  * Testing wc_RsaEncryptSize()
19626  */
19627 static int test_wc_RsaEncryptSize (void)
19628 {
19629     int     ret = 0;
19630 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19631     RsaKey  key;
19632     WC_RNG  rng;
19633 
19634     ret = wc_InitRsaKey(&key, NULL);
19635 
19636     if (ret == 0) {
19637         ret = wc_InitRng(&rng);
19638     }
19639 
19640     printf(testingFmt, "wc_RsaEncryptSize()");
19641 #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
19642     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
19643     if (ret == 0) {
19644         ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
19645         if (ret == 0) {
19646             ret = wc_RsaEncryptSize(&key);
19647         }
19648         if (ret == 128) {
19649             ret = 0;
19650         } else {
19651             ret = WOLFSSL_FATAL_ERROR;
19652         }
19653     }
19654     if (wc_FreeRsaKey(&key) || ret != 0) {
19655         ret = WOLFSSL_FATAL_ERROR;
19656     } else {
19657         ret = 0;
19658     }
19659 #endif
19660 
19661     if (ret == 0) {
19662         ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
19663         if (ret == 0) {
19664             ret = wc_RsaEncryptSize(&key);
19665         }
19666         if (ret == 256) {
19667             ret = 0;
19668         } else {
19669             ret = WOLFSSL_FATAL_ERROR;
19670         }
19671     }
19672 
19673     /* Pass in bad arg. */
19674     if (ret == 0) {
19675         ret = wc_RsaEncryptSize(NULL);
19676         #ifndef HAVE_USER_RSA
19677             if (ret == BAD_FUNC_ARG) {
19678                 ret = 0;
19679             } else {
19680                 ret = WOLFSSL_FATAL_ERROR;
19681             }
19682         #endif
19683     }
19684 
19685     if (wc_FreeRsaKey(&key) || ret != 0) {
19686         ret = WOLFSSL_FATAL_ERROR;
19687     }
19688     if (wc_FreeRng(&rng) || ret != 0) {
19689         ret = WOLFSSL_FATAL_ERROR;
19690     }
19691 
19692     printf(resultFmt, ret == 0 ? passed : failed);
19693 
19694 #endif
19695     return ret;
19696 
19697 } /* END test_wc_RsaEncryptSize*/
19698 
19699 /*
19700  * Testing wc_RsaFlattenPublicKey()
19701  */
19702 static int test_wc_RsaFlattenPublicKey (void)
19703 {
19704     int     ret = 0;
19705 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19706     RsaKey  key;
19707     WC_RNG  rng;
19708     byte    e[256];
19709     byte    n[256];
19710     word32  eSz = sizeof(e);
19711     word32  nSz = sizeof(n);
19712     #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
19713         (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
19714     int         bits = 1024;
19715     #else
19716     int         bits = 2048;
19717     #endif
19718 
19719     ret = wc_InitRsaKey(&key, NULL);
19720     if (ret == 0) {
19721         ret = wc_InitRng(&rng);
19722     }
19723 
19724     if (ret == 0) {
19725         ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
19726         if (ret >= 0) {
19727             ret = 0;
19728         } else {
19729             ret = WOLFSSL_FATAL_ERROR;
19730         }
19731     }
19732 
19733     printf(testingFmt, "wc_RsaFlattenPublicKey()");
19734 
19735     if (ret == 0) {
19736         ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
19737     }
19738     #ifndef HAVE_USER_RSA
19739         /* Pass bad args. */
19740         if (ret == 0) {
19741             ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
19742             if (ret == BAD_FUNC_ARG) {
19743                 ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
19744             }
19745             if (ret == BAD_FUNC_ARG) {
19746                 ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
19747             }
19748             if (ret == BAD_FUNC_ARG) {
19749                 ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
19750             }
19751             if (ret == BAD_FUNC_ARG) {
19752                 ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
19753             }
19754             if (ret == BAD_FUNC_ARG) {
19755                 ret = 0;
19756             } else {
19757                 ret = WOLFSSL_FATAL_ERROR;
19758             }
19759         }
19760     #else
19761         /* Pass bad args. */
19762         if (ret == 0) {
19763             ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
19764             if (ret == USER_CRYPTO_ERROR) {
19765                 ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
19766             }
19767             if (ret == USER_CRYPTO_ERROR) {
19768                 ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
19769             }
19770             if (ret == USER_CRYPTO_ERROR) {
19771                 ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
19772             }
19773             if (ret == USER_CRYPTO_ERROR) {
19774                 ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
19775             }
19776             if (ret == USER_CRYPTO_ERROR) {
19777                 ret = 0;
19778             } else {
19779                 ret = WOLFSSL_FATAL_ERROR;
19780             }
19781         }
19782     #endif
19783     if (wc_FreeRsaKey(&key) || ret != 0) {
19784         ret = WOLFSSL_FATAL_ERROR;
19785     }
19786     if (wc_FreeRng(&rng) || ret != 0) {
19787         ret = WOLFSSL_FATAL_ERROR;
19788     }
19789 
19790     printf(resultFmt, ret == 0 ? passed : failed);
19791 
19792 #endif
19793     return ret;
19794 
19795 } /* END test_wc_RsaFlattenPublicKey */
19796 
19797 
19798 
19799 /*
19800  * unit test for wc_AesCcmSetKey
19801  */
19802 static int test_wc_AesCcmSetKey (void)
19803 {
19804     int ret = 0;
19805 #ifdef HAVE_AESCCM
19806     Aes aes;
19807     const byte  key16[] =
19808     {
19809         0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
19810         0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
19811     };
19812     const byte  key24[] =
19813     {
19814         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
19815         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
19816         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
19817     };
19818     const byte  key32[] =
19819     {
19820         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
19821         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
19822         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
19823         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
19824     };
19825 
19826     printf(testingFmt, "wc_AesCcmSetKey()");
19827 
19828     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
19829     if (ret != 0)
19830         return ret;
19831 
19832 #ifdef WOLFSSL_AES_128
19833     ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
19834 #endif
19835 #ifdef WOLFSSL_AES_192
19836     if (ret == 0) {
19837         ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
19838     }
19839 #endif
19840 #ifdef WOLFSSL_AES_256
19841     if (ret == 0) {
19842         ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
19843     }
19844 #endif
19845 
19846     /* Test bad args. */
19847     if (ret == 0) {
19848         ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
19849         if (ret == BAD_FUNC_ARG) {
19850             ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
19851         }
19852         if (ret == BAD_FUNC_ARG) {
19853             ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
19854         }
19855         if (ret != BAD_FUNC_ARG) {
19856             ret = WOLFSSL_FATAL_ERROR;
19857         } else {
19858             ret = 0;
19859         }
19860     }
19861 
19862     wc_AesFree(&aes);
19863 
19864     printf(resultFmt, ret == 0 ? passed : failed);
19865 
19866 #endif
19867     return ret;
19868 
19869 } /* END test_wc_AesCcmSetKey */
19870 
19871 /*
19872  * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
19873  */
19874 static int test_wc_AesCcmEncryptDecrypt (void)
19875 {
19876     int ret = 0;
19877 #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
19878     Aes aes;
19879     const byte  key16[] =
19880     {
19881         0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
19882         0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
19883     };
19884     /* plaintext */
19885     const byte plainT[] =
19886     {
19887         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
19888         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
19889         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
19890     };
19891     /* nonce */
19892     const byte iv[] =
19893     {
19894         0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
19895         0xa1, 0xa2, 0xa3, 0xa4, 0xa5
19896     };
19897     const byte c[] =  /* cipher text. */
19898     {
19899         0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
19900         0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
19901         0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
19902     };
19903     const byte t[] =  /* Auth tag */
19904     {
19905         0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
19906     };
19907     const byte authIn[] =
19908     {
19909         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
19910     };
19911     byte cipherOut[sizeof(plainT)];
19912     byte authTag[sizeof(t)];
19913     int ccmE = WOLFSSL_FATAL_ERROR;
19914     #ifdef HAVE_AES_DECRYPT
19915         int ccmD = WOLFSSL_FATAL_ERROR;
19916         byte plainOut[sizeof(cipherOut)];
19917     #endif
19918 
19919     ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
19920     if (ret != 0)
19921         return ret;
19922 
19923     ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
19924     if (ret == 0) {
19925         ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
19926                                     iv, sizeof(iv), authTag, sizeof(authTag),
19927                                     authIn , sizeof(authIn));
19928         if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
19929                 XMEMCMP(t, authTag, sizeof(t))) {
19930             ccmE = WOLFSSL_FATAL_ERROR;
19931             ret = WOLFSSL_FATAL_ERROR;
19932         }
19933         #ifdef HAVE_AES_DECRYPT
19934             if (ret == 0) {
19935                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
19936                                         sizeof(plainOut), iv, sizeof(iv),
19937                                         authTag, sizeof(authTag),
19938                                         authIn, sizeof(authIn));
19939                 if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
19940                     ccmD = WOLFSSL_FATAL_ERROR;
19941                 }
19942             }
19943         #endif
19944     }
19945 
19946     printf(testingFmt, "wc_AesCcmEncrypt()");
19947 
19948     /* Pass in bad args. Encrypt*/
19949     if (ret == 0 && ccmE == 0) {
19950         ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
19951                                     iv, sizeof(iv), authTag, sizeof(authTag),
19952                                     authIn , sizeof(authIn));
19953         if (ccmE == BAD_FUNC_ARG) {
19954             ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
19955                                     iv, sizeof(iv), authTag, sizeof(authTag),
19956                                     authIn , sizeof(authIn));
19957         }
19958         if (ccmE == BAD_FUNC_ARG) {
19959             ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
19960                                     iv, sizeof(iv), authTag, sizeof(authTag),
19961                                     authIn , sizeof(authIn));
19962         }
19963         if (ccmE == BAD_FUNC_ARG) {
19964             ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
19965                                     NULL, sizeof(iv), authTag, sizeof(authTag),
19966                                     authIn , sizeof(authIn));
19967         }
19968         if (ccmE == BAD_FUNC_ARG) {
19969             ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
19970                                     iv, sizeof(iv), NULL, sizeof(authTag),
19971                                     authIn , sizeof(authIn));
19972         }
19973         if (ccmE == BAD_FUNC_ARG) {
19974             ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
19975                                     iv, sizeof(iv) + 1, authTag, sizeof(authTag),
19976                                     authIn , sizeof(authIn));
19977         }
19978         if (ccmE == BAD_FUNC_ARG) {
19979             ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
19980                                     iv, sizeof(iv) - 7, authTag, sizeof(authTag),
19981                                     authIn , sizeof(authIn));
19982         }
19983 
19984         if (ccmE != BAD_FUNC_ARG) {
19985             ccmE = WOLFSSL_FATAL_ERROR;
19986         } else {
19987             ccmE = 0;
19988         }
19989     } /* End Encrypt */
19990 
19991     printf(resultFmt, ccmE == 0 ? passed : failed);
19992     if (ccmE != 0) {
19993         wc_AesFree(&aes);
19994         return ccmE;
19995     }
19996     #ifdef HAVE_AES_DECRYPT
19997         printf(testingFmt, "wc_AesCcmDecrypt()");
19998 
19999         /* Pass in bad args. Decrypt*/
20000         if (ret == 0 && ccmD == 0) {
20001             ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
20002                                         iv, sizeof(iv), authTag, sizeof(authTag),
20003                                         authIn, sizeof(authIn));
20004             if (ccmD == BAD_FUNC_ARG) {
20005                 ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
20006                                         iv, sizeof(iv), authTag, sizeof(authTag),
20007                                         authIn, sizeof(authIn));
20008             }
20009             if (ccmD == BAD_FUNC_ARG) {
20010                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
20011                                         iv, sizeof(iv), authTag, sizeof(authTag),
20012                                         authIn, sizeof(authIn));
20013             }
20014             if (ccmD == BAD_FUNC_ARG) {
20015                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
20016                                         sizeof(plainOut), NULL, sizeof(iv),
20017                                         authTag, sizeof(authTag),
20018                                         authIn, sizeof(authIn));
20019             }
20020             if (ccmD == BAD_FUNC_ARG) {
20021                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
20022                                         sizeof(plainOut), iv, sizeof(iv), NULL,
20023                                         sizeof(authTag), authIn, sizeof(authIn));
20024             }
20025             if (ccmD == BAD_FUNC_ARG) {
20026                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
20027                                         sizeof(plainOut), iv, sizeof(iv) + 1,
20028                                         authTag, sizeof(authTag),
20029                                         authIn, sizeof(authIn));
20030             }
20031             if (ccmD == BAD_FUNC_ARG) {
20032                 ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
20033                                         sizeof(plainOut), iv, sizeof(iv) - 7,
20034                                         authTag, sizeof(authTag),
20035                                         authIn, sizeof(authIn));
20036             }
20037             if (ccmD != BAD_FUNC_ARG) {
20038                 ccmD = WOLFSSL_FATAL_ERROR;
20039             } else {
20040                 ccmD = 0;
20041             }
20042         } /* END Decrypt */
20043 
20044         printf(resultFmt, ccmD == 0 ? passed : failed);
20045         if (ccmD != 0) {
20046             return ccmD;
20047         }
20048     #endif
20049 
20050     wc_AesFree(&aes);
20051 
20052 #endif  /* HAVE_AESCCM */
20053 
20054     return ret;
20055 
20056 } /* END test_wc_AesCcmEncryptDecrypt */
20057 
20058 
20059 
20060 /*
20061  * Test wc_Hc128_SetKey()
20062  */
20063 static int test_wc_Hc128_SetKey (void)
20064 {
20065     int ret = 0;
20066 #ifdef HAVE_HC128
20067     HC128 ctx;
20068     const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
20069                       "\x00\x00\x00\x00\x00\x00\x00\x00";
20070     const char* iv =  "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
20071                       "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
20072 
20073     printf(testingFmt, "wc_Hc128_SetKey()");
20074         ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
20075         /* Test bad args. */
20076         if (ret == 0) {
20077             ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
20078             if (ret == BAD_FUNC_ARG) {
20079                 ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
20080             }
20081             if (ret == BAD_FUNC_ARG) {
20082                 ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
20083             }
20084         }
20085 
20086     printf(resultFmt, ret == 0 ? passed : failed);
20087 
20088 
20089 #endif
20090     return ret;
20091 
20092 } /* END test_wc_Hc128_SetKey */
20093 
20094 /*
20095  * Testing wc_Hc128_Process()
20096  */
20097 static int test_wc_Hc128_Process (void)
20098 {
20099     int ret = 0;
20100 #ifdef HAVE_HC128
20101     HC128 enc;
20102     HC128 dec;
20103     const char* key =  "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
20104                        "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
20105     const char* input = "Encrypt Hc128, and then Decrypt.";
20106     size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
20107     byte cipher[inlen];
20108     byte plain[inlen];
20109 
20110     printf(testingFmt, "wc_Hc128_Process()");
20111     ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
20112     if (ret == 0) {
20113         ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
20114     }
20115     if (ret == 0) {
20116         ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
20117         if (ret == 0) {
20118             ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
20119         }
20120     }
20121 
20122     /* Bad args. */
20123     if (ret == 0) {
20124         ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
20125         if (ret == BAD_FUNC_ARG) {
20126             ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
20127         }
20128         if (ret == BAD_FUNC_ARG) {
20129             ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
20130         }
20131         if (ret == BAD_FUNC_ARG) {
20132             ret = 0;
20133         } else {
20134             ret = WOLFSSL_FATAL_ERROR;
20135         }
20136     }
20137 
20138     printf(resultFmt, ret == 0 ? passed : failed);
20139 
20140    #endif
20141     return ret;
20142 
20143 } /* END test_wc_Hc128_Process */
20144 
20145 
20146 /*
20147  * Testing wc_InitDsaKey()
20148  */
20149 static int test_wc_InitDsaKey (void)
20150 {
20151     int     ret = 0;
20152 
20153 #ifndef NO_DSA
20154     DsaKey  key;
20155 
20156     printf(testingFmt, "wc_InitDsaKey()");
20157 
20158     ret = wc_InitDsaKey(&key);
20159 
20160     /* Pass in bad args. */
20161     if (ret == 0) {
20162         ret = wc_InitDsaKey(NULL);
20163         if (ret == BAD_FUNC_ARG) {
20164             ret = 0;
20165         } else {
20166             ret = WOLFSSL_FATAL_ERROR;
20167         }
20168     }
20169 
20170     printf(resultFmt, ret == 0 ? passed : failed);
20171 
20172     wc_FreeDsaKey(&key);
20173 
20174 #endif
20175     return ret;
20176 
20177 } /* END test_wc_InitDsaKey */
20178 
20179 /*
20180  * Testing wc_DsaSign() and wc_DsaVerify()
20181  */
20182 static int test_wc_DsaSignVerify (void)
20183 {
20184     int     ret = 0;
20185 #if !defined(NO_DSA)
20186     DsaKey  key;
20187     WC_RNG  rng;
20188     wc_Sha  sha;
20189     byte    signature[DSA_SIG_SIZE];
20190     byte    hash[WC_SHA_DIGEST_SIZE];
20191     word32  idx = 0;
20192     word32  bytes;
20193     int      answer;
20194 #ifdef USE_CERT_BUFFERS_1024
20195     byte    tmp[ONEK_BUF];
20196     XMEMSET(tmp, 0, sizeof(tmp));
20197     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
20198     bytes = sizeof_dsa_key_der_1024;
20199 #elif defined(USE_CERT_BUFFERS_2048)
20200     byte    tmp[TWOK_BUF];
20201     XMEMSET(tmp, 0, sizeof(tmp));
20202     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
20203     bytes = sizeof_dsa_key_der_2048;
20204 #else
20205     byte    tmp[TWOK_BUF];
20206     XMEMSET(tmp, 0, sizeof(tmp));
20207     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
20208     if (fp == XBADFILE) {
20209         return WOLFSSL_BAD_FILE;
20210     }
20211     bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
20212     XFCLOSE(fp);
20213 #endif /* END USE_CERT_BUFFERS_1024 */
20214 
20215     ret = wc_InitSha(&sha);
20216     if (ret == 0) {
20217         ret = wc_ShaUpdate(&sha, tmp, bytes);
20218         if (ret == 0) {
20219             ret = wc_ShaFinal(&sha, hash);
20220         }
20221         if (ret == 0) {
20222             ret = wc_InitDsaKey(&key);
20223         }
20224         if (ret == 0) {
20225             ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
20226         }
20227         if (ret == 0) {
20228             ret = wc_InitRng(&rng);
20229         }
20230     }
20231 
20232     printf(testingFmt, "wc_DsaSign()");
20233     /* Sign. */
20234     if (ret == 0) {
20235         ret = wc_DsaSign(hash, signature, &key, &rng);
20236     }
20237 
20238     /* Test bad args. */
20239     if (ret == 0) {
20240         ret = wc_DsaSign(NULL, signature, &key, &rng);
20241         if (ret == BAD_FUNC_ARG) {
20242             ret = wc_DsaSign(hash, NULL, &key, &rng);
20243         }
20244         if (ret == BAD_FUNC_ARG) {
20245             ret = wc_DsaSign(hash, signature, NULL, &rng);
20246         }
20247         if (ret == BAD_FUNC_ARG) {
20248             ret = wc_DsaSign(hash, signature, &key, NULL);
20249         }
20250         if (ret == BAD_FUNC_ARG) {
20251             ret = 0;
20252         } else {
20253             ret = WOLFSSL_FATAL_ERROR;
20254         }
20255     }
20256 
20257     printf(resultFmt, ret == 0 ? passed : failed);
20258 
20259     if (ret != 0) {
20260         return ret;
20261     }
20262 
20263     /* Verify. */
20264     printf(testingFmt, "wc_DsaVerify()");
20265 
20266     ret = wc_DsaVerify(hash, signature, &key, &answer);
20267     if (ret != 0 || answer != 1) {
20268         ret = WOLFSSL_FATAL_ERROR;
20269     } else {
20270         ret = 0;
20271     }
20272 
20273     /* Pass in bad args. */
20274     if (ret == 0) {
20275         ret = wc_DsaVerify(NULL, signature, &key, &answer);
20276         if (ret == BAD_FUNC_ARG) {
20277             ret = wc_DsaVerify(hash, NULL, &key, &answer);
20278         }
20279         if (ret == BAD_FUNC_ARG) {
20280             ret = wc_DsaVerify(hash, signature, NULL, &answer);
20281         }
20282         if (ret == BAD_FUNC_ARG) {
20283             ret = wc_DsaVerify(hash, signature, &key, NULL);
20284         }
20285         if (ret == BAD_FUNC_ARG) {
20286             ret = 0;
20287         } else {
20288             ret = WOLFSSL_FATAL_ERROR;
20289         }
20290     }
20291 
20292 #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
20293     /* hard set q to 0 and test fail case */
20294     mp_free(&key.q);
20295     mp_init(&key.q);
20296     AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
20297 
20298     mp_set(&key.q, 1);
20299     AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
20300 #endif
20301 
20302     if (wc_FreeRng(&rng) && ret == 0) {
20303         ret = WOLFSSL_FATAL_ERROR;
20304     }
20305 
20306     printf(resultFmt, ret == 0 ? passed : failed);
20307 
20308     wc_FreeDsaKey(&key);
20309     wc_ShaFree(&sha);
20310 
20311 #endif
20312     return ret;
20313 
20314 } /* END test_wc_DsaSign */
20315 
20316 /*
20317  * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
20318  */
20319 static int test_wc_DsaPublicPrivateKeyDecode (void)
20320 {
20321     int     ret = 0;
20322 
20323 #if !defined(NO_DSA)
20324     DsaKey  key;
20325     word32  bytes;
20326     word32  idx  = 0;
20327     int     priv = WOLFSSL_FATAL_ERROR;
20328     int     pub  = WOLFSSL_FATAL_ERROR;
20329 
20330 #ifdef USE_CERT_BUFFERS_1024
20331     byte    tmp[ONEK_BUF];
20332     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
20333     bytes = sizeof_dsa_key_der_1024;
20334 #elif defined(USE_CERT_BUFFERS_2048)
20335     byte    tmp[TWOK_BUF];
20336     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
20337     bytes = sizeof_dsa_key_der_2048;
20338 #else
20339     byte    tmp[TWOK_BUF];
20340     XMEMSET(tmp, 0, sizeof(tmp));
20341     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
20342     if (fp == XBADFILE)
20343     {
20344         return WOLFSSL_BAD_FILE;
20345     }
20346     bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
20347     XFCLOSE(fp);
20348 #endif /* END USE_CERT_BUFFERS_1024 */
20349 
20350     ret = wc_InitDsaKey(&key);
20351 
20352     printf(testingFmt, "wc_DsaPrivateKeyDecode()");
20353     if (ret == 0) {
20354         priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
20355 
20356         /* Test bad args. */
20357         if (priv == 0) {
20358             priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
20359             if (priv == BAD_FUNC_ARG) {
20360                 priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
20361             }
20362             if (priv == BAD_FUNC_ARG) {
20363                 priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
20364             }
20365             if (priv == BAD_FUNC_ARG) {
20366                 priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
20367             }
20368             if (priv == ASN_PARSE_E) {
20369                 priv = 0;
20370             } else {
20371                 priv = WOLFSSL_FATAL_ERROR;
20372             }
20373         }
20374 
20375         wc_FreeDsaKey(&key);
20376         ret = wc_InitDsaKey(&key);
20377     }
20378 
20379     printf(resultFmt, priv == 0 ? passed : failed);
20380 
20381     printf(testingFmt, "wc_DsaPublicKeyDecode()");
20382     if (ret == 0) {
20383         idx = 0; /* Reset */
20384         pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
20385         /* Test bad args. */
20386         if (pub == 0) {
20387             pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
20388             if (pub == BAD_FUNC_ARG) {
20389                 pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
20390             }
20391             if (pub == BAD_FUNC_ARG) {
20392                 pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
20393             }
20394             if (pub == BAD_FUNC_ARG) {
20395                 pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
20396             }
20397             if (pub == ASN_PARSE_E) {
20398                 pub = 0;
20399             } else {
20400                 pub = WOLFSSL_FATAL_ERROR;
20401             }
20402         }
20403 
20404     } /* END Public Key */
20405 
20406     printf(resultFmt, pub == 0 ? passed : failed);
20407 
20408     wc_FreeDsaKey(&key);
20409 
20410 #endif
20411     return ret;
20412 
20413 } /* END test_wc_DsaPublicPrivateKeyDecode */
20414 
20415 
20416 /*
20417  * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
20418  */
20419 static int test_wc_MakeDsaKey (void)
20420 {
20421     int     ret = 0;
20422 
20423 #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
20424     DsaKey  genKey;
20425     WC_RNG  rng;
20426     XMEMSET(&rng, 0, sizeof(rng));
20427     XMEMSET(&genKey, 0, sizeof(genKey));
20428 
20429     ret = wc_InitRng(&rng);
20430     if (ret == 0) {
20431         ret = wc_InitDsaKey(&genKey);
20432     }
20433 
20434     printf(testingFmt, "wc_MakeDsaParameters()");
20435     if (ret == 0) {
20436         ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
20437     }
20438     /* Test bad args. */
20439     if (ret == 0) {
20440         ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
20441         if (ret == BAD_FUNC_ARG) {
20442             ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
20443         }
20444         if (ret == BAD_FUNC_ARG) {
20445             ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
20446         }
20447         if (ret == BAD_FUNC_ARG) {
20448             ret = 0;
20449         } else {
20450             ret = WOLFSSL_FATAL_ERROR;
20451         }
20452     }
20453 
20454     printf(resultFmt, ret == 0 ? passed : failed);
20455 
20456     printf(testingFmt, "wc_MakeDsaKey()");
20457 
20458     if (ret == 0) {
20459         ret = wc_MakeDsaKey(&rng, &genKey);
20460     }
20461 
20462     /* Test bad args. */
20463     if (ret == 0) {
20464         ret = wc_MakeDsaKey(NULL, &genKey);
20465         if (ret == BAD_FUNC_ARG) {
20466             ret = wc_MakeDsaKey(&rng, NULL);
20467         }
20468         if (ret == BAD_FUNC_ARG) {
20469             ret = 0;
20470         } else {
20471             ret = WOLFSSL_FATAL_ERROR;
20472         }
20473     }
20474 
20475     if (wc_FreeRng(&rng) && ret == 0) {
20476         ret = WOLFSSL_FAILURE;
20477     }
20478 
20479     printf(resultFmt, ret == 0 ? passed : failed);
20480 
20481     wc_FreeDsaKey(&genKey);
20482 #endif
20483     return ret;
20484 } /* END test_wc_MakeDsaKey */
20485 
20486 /*
20487  * Testing wc_DsaKeyToDer()
20488  */
20489 static int test_wc_DsaKeyToDer (void)
20490 {
20491     int     ret = 0;
20492 
20493 #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
20494     DsaKey  genKey;
20495     WC_RNG  rng;
20496     word32  bytes;
20497     word32  idx = 0;
20498 
20499 #ifdef USE_CERT_BUFFERS_1024
20500     byte    tmp[ONEK_BUF];
20501     byte    der[ONEK_BUF];
20502     XMEMSET(tmp, 0, sizeof(tmp));
20503     XMEMSET(der, 0, sizeof(der));
20504     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
20505     bytes = sizeof_dsa_key_der_1024;
20506 #elif defined(USE_CERT_BUFFERS_2048)
20507     byte    tmp[TWOK_BUF];
20508     byte    der[TWOK_BUF];
20509     XMEMSET(tmp, 0, sizeof(tmp));
20510     XMEMSET(der, 0, sizeof(der));
20511     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
20512     bytes = sizeof_dsa_key_der_2048;
20513 #else
20514     byte    tmp[TWOK_BUF];
20515     byte    der[TWOK_BUF];
20516     XMEMSET(tmp, 0, sizeof(tmp));
20517     XMEMSET(der, 0, sizeof(der));
20518     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
20519     if (fp == XBADFILE) {
20520         return WOLFSSL_BAD_FILE;
20521     }
20522     bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
20523     XFCLOSE(fp);
20524 #endif /* END USE_CERT_BUFFERS_1024 */
20525 #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
20526     XMEMSET(&rng, 0, sizeof(rng));
20527     XMEMSET(&genKey, 0, sizeof(genKey));
20528 #endif
20529     ret = wc_InitRng(&rng);
20530     if (ret == 0) {
20531         ret = wc_InitDsaKey(&genKey);
20532     }
20533     if (ret == 0) {
20534         ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
20535         if (ret == 0) {
20536             wc_FreeDsaKey(&genKey);
20537             ret = wc_InitDsaKey(&genKey);
20538         }
20539     }
20540     if (ret == 0) {
20541         ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
20542     }
20543 
20544     printf(testingFmt, "wc_DsaKeyToDer()");
20545 
20546     if (ret == 0) {
20547         ret = wc_DsaKeyToDer(&genKey, der, bytes);
20548         if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
20549             ret = 0;
20550         }
20551     }
20552 
20553     /* Test bad args. */
20554     if (ret == 0) {
20555         ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
20556         if (ret == BAD_FUNC_ARG) {
20557             ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
20558         }
20559         if (ret == BAD_FUNC_ARG) {
20560             ret = 0;
20561         } else {
20562             ret = WOLFSSL_FATAL_ERROR;
20563         }
20564     }
20565 
20566     if (wc_FreeRng(&rng) && ret == 0) {
20567         ret = WOLFSSL_FATAL_ERROR;
20568     }
20569 
20570     printf(resultFmt, ret == 0 ? passed : failed);
20571 
20572     wc_FreeDsaKey(&genKey);
20573 
20574 #endif
20575 
20576     return ret;
20577 
20578 } /* END test_wc_DsaKeyToDer */
20579 
20580 /*
20581  *  Testing wc_DsaKeyToPublicDer()
20582  *  (indirectly testing setDsaPublicKey())
20583  */
20584 static int test_wc_DsaKeyToPublicDer(void)
20585 {
20586     int         ret = 0;
20587 #ifndef HAVE_SELFTEST
20588 #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
20589     DsaKey  genKey;
20590     WC_RNG  rng;
20591     byte*   der;
20592     word32  sz;
20593 
20594     printf(testingFmt, "wc_DsaKeyToPublicDer()");
20595 
20596     der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
20597     if (der == NULL) {
20598         ret = WOLFSSL_FATAL_ERROR;
20599     }
20600     if (ret == 0) {
20601         ret = wc_InitDsaKey(&genKey);
20602     }
20603     if (ret == 0) {
20604         ret = wc_InitRng(&rng);
20605     }
20606     if (ret == 0) {
20607         ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
20608     }
20609     if (ret == 0) {
20610         ret = wc_MakeDsaKey(&rng, &genKey);
20611     }
20612 
20613     if (ret == 0) {
20614         ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
20615         if (ret >= 0) {
20616             sz = ret;
20617             ret = 0;
20618         } else {
20619             ret = WOLFSSL_FATAL_ERROR;
20620         }
20621     }
20622     if (ret == 0) {
20623         word32 idx = 0;
20624         wc_FreeDsaKey(&genKey);
20625         ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
20626     }
20627     /* Test without the SubjectPublicKeyInfo header */
20628     if (ret == 0) {
20629         ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
20630         if (ret >= 0) {
20631             sz = ret;
20632             ret = 0;
20633         } else {
20634             ret = WOLFSSL_FATAL_ERROR;
20635         }
20636     }
20637     if (ret == 0) {
20638         word32 idx = 0;
20639         wc_FreeDsaKey(&genKey);
20640         ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
20641     }
20642 
20643     /* Test bad args. */
20644     if (ret == 0) {
20645         ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
20646         if (ret == BAD_FUNC_ARG) {
20647             ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
20648         }
20649         if (ret == BAD_FUNC_ARG) {
20650             ret = 0;
20651         } else {
20652             ret = WOLFSSL_FATAL_ERROR;
20653         }
20654     }
20655 
20656     if (wc_FreeRng(&rng) && ret == 0) {
20657         ret = WOLFSSL_FATAL_ERROR;
20658     }
20659 
20660     printf(resultFmt, ret == 0 ? passed : failed);
20661 
20662     XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
20663     wc_FreeDsaKey(&genKey);
20664 #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
20665 #endif /* HAVE_SELFTEST */
20666     return ret;
20667 
20668 } /* END test_wc_DsaKeyToPublicDer */
20669 
20670 /*
20671  * Testing wc_DsaImportParamsRaw()
20672  */
20673 static int test_wc_DsaImportParamsRaw (void)
20674 {
20675     int     ret = 0;
20676 
20677 #if !defined(NO_DSA)
20678     DsaKey  key;
20679 
20680     /* [mod = L=1024, N=160], from CAVP KeyPair */
20681     const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
20682                     "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
20683                     "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
20684                     "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
20685                     "47123188f8dc551054ee162b634d60f097f719076640e209"
20686                     "80a0093113a8bd73";
20687     const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
20688     const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
20689                     "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
20690                     "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
20691                     "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
20692                     "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
20693                     "76341a7e7d9";
20694 
20695     /* invalid p and q parameters */
20696     const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
20697     const char* invalidQ = "96c5390a";
20698 
20699     printf(testingFmt, "wc_DsaImportParamsRaw()");
20700 
20701     ret = wc_InitDsaKey(&key);
20702     if (ret == 0) {
20703         ret = wc_DsaImportParamsRaw(&key, p, q, g);
20704     }
20705 
20706     /* test bad args */
20707     if (ret == 0) {
20708         /* null key struct */
20709         ret = wc_DsaImportParamsRaw(NULL, p, q, g);
20710         if (ret == BAD_FUNC_ARG) {
20711             /* null param pointers */
20712             ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
20713         }
20714 
20715         if (ret == BAD_FUNC_ARG) {
20716             /* illegal p length */
20717             ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
20718         }
20719 
20720         if (ret == BAD_FUNC_ARG) {
20721             /* illegal q length */
20722             ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
20723             if (ret == BAD_FUNC_ARG)
20724                 ret = 0;
20725         }
20726 
20727     }
20728 
20729     printf(resultFmt, ret == 0 ? passed : failed);
20730 
20731     wc_FreeDsaKey(&key);
20732 
20733 #endif
20734 
20735     return ret;
20736 
20737 } /* END test_wc_DsaImportParamsRaw */
20738 
20739 /*
20740  * Testing wc_DsaImportParamsRawCheck()
20741  */
20742 static int test_wc_DsaImportParamsRawCheck (void)
20743 {
20744     int     ret = 0;
20745 
20746 #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
20747     DsaKey  key;
20748     int     trusted = 0;
20749     /* [mod = L=1024, N=160], from CAVP KeyPair */
20750     const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
20751                     "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
20752                     "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
20753                     "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
20754                     "47123188f8dc551054ee162b634d60f097f719076640e209"
20755                     "80a0093113a8bd73";
20756     const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
20757     const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
20758                     "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
20759                     "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
20760                     "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
20761                     "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
20762                     "76341a7e7d9";
20763 
20764     /* invalid p and q parameters */
20765     const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
20766     const char* invalidQ = "96c5390a";
20767 
20768     printf(testingFmt, "wc_DsaImportParamsRawCheck()");
20769 
20770     ret = wc_InitDsaKey(&key);
20771     if (ret == 0) {
20772         ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
20773     }
20774 
20775     /* test bad args */
20776     if (ret == 0) {
20777         /* null key struct */
20778         ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
20779         if (ret == BAD_FUNC_ARG) {
20780             /* null param pointers */
20781             ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
20782         }
20783 
20784         if (ret == BAD_FUNC_ARG) {
20785             /* illegal p length */
20786             ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
20787         }
20788 
20789         if (ret == BAD_FUNC_ARG) {
20790             /* illegal q length */
20791             ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
20792             if (ret == BAD_FUNC_ARG)
20793                 ret = 0;
20794         }
20795 
20796     }
20797 
20798     printf(resultFmt, ret == 0 ? passed : failed);
20799 
20800     wc_FreeDsaKey(&key);
20801 
20802 #endif
20803 
20804     return ret;
20805 
20806 } /* END test_wc_DsaImportParamsRawCheck */
20807 
20808 /*
20809  * Testing wc_DsaExportParamsRaw()
20810  */
20811 static int test_wc_DsaExportParamsRaw (void)
20812 {
20813     int     ret = 0;
20814 
20815 #if !defined(NO_DSA)
20816     DsaKey  key;
20817 
20818     /* [mod = L=1024, N=160], from CAVP KeyPair */
20819     const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
20820                     "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
20821                     "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
20822                     "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
20823                     "47123188f8dc551054ee162b634d60f097f719076640e209"
20824                     "80a0093113a8bd73";
20825     const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
20826     const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
20827                     "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
20828                     "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
20829                     "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
20830                     "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
20831                     "76341a7e7d9";
20832 
20833     const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
20834                            "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
20835                            "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
20836                            "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
20837                            "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
20838                            "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
20839                            "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
20840                            "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
20841                            "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
20842                            "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
20843                            "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
20844     const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
20845                            "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
20846     const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
20847                            "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
20848                            "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
20849                            "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
20850                            "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
20851                            "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
20852                            "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
20853                            "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
20854                            "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
20855                            "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
20856                            "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
20857 
20858     byte pOut[MAX_DSA_PARAM_SIZE];
20859     byte qOut[MAX_DSA_PARAM_SIZE];
20860     byte gOut[MAX_DSA_PARAM_SIZE];
20861     word32 pOutSz, qOutSz, gOutSz;
20862 
20863     printf(testingFmt, "wc_DsaExportParamsRaw()");
20864 
20865     ret = wc_InitDsaKey(&key);
20866     if (ret == 0) {
20867         /* first test using imported raw parameters, for expected */
20868         ret = wc_DsaImportParamsRaw(&key, p, q, g);
20869     }
20870 
20871     if (ret == 0) {
20872         pOutSz = sizeof(pOut);
20873         qOutSz = sizeof(qOut);
20874         gOutSz = sizeof(gOut);
20875         ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
20876                                     gOut, &gOutSz);
20877     }
20878 
20879     if (ret == 0) {
20880         /* validate exported parameters are correct */
20881         if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
20882             (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
20883             (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
20884             ret = -1;
20885         }
20886     }
20887 
20888     /* test bad args */
20889     if (ret == 0) {
20890         /* null key struct */
20891         ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
20892                                     gOut, &gOutSz);
20893 
20894         if (ret == BAD_FUNC_ARG) {
20895             /* null output pointers */
20896             ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
20897                                         NULL, &gOutSz);
20898         }
20899 
20900         if (ret == LENGTH_ONLY_E) {
20901             /* null output size pointers */
20902             ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
20903                                         gOut, NULL);
20904         }
20905 
20906         if (ret == BAD_FUNC_ARG) {
20907             /* p output buffer size too small */
20908             pOutSz = 1;
20909             ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
20910                                         gOut, &gOutSz);
20911             pOutSz = sizeof(pOut);
20912         }
20913 
20914         if (ret == BUFFER_E) {
20915             /* q output buffer size too small */
20916             qOutSz = 1;
20917             ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
20918                                         gOut, &gOutSz);
20919             qOutSz = sizeof(qOut);
20920         }
20921 
20922         if (ret == BUFFER_E) {
20923             /* g output buffer size too small */
20924             gOutSz = 1;
20925             ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
20926                                         gOut, &gOutSz);
20927             if (ret == BUFFER_E)
20928                 ret = 0;
20929         }
20930     }
20931 
20932     printf(resultFmt, ret == 0 ? passed : failed);
20933 
20934     wc_FreeDsaKey(&key);
20935 
20936 #endif
20937 
20938     return ret;
20939 
20940 } /* END test_wc_DsaExportParamsRaw */
20941 
20942 /*
20943  * Testing wc_DsaExportKeyRaw()
20944  */
20945 static int test_wc_DsaExportKeyRaw (void)
20946 {
20947     int     ret = 0;
20948 
20949 #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
20950     DsaKey  key;
20951     WC_RNG  rng;
20952 
20953     byte xOut[MAX_DSA_PARAM_SIZE];
20954     byte yOut[MAX_DSA_PARAM_SIZE];
20955     word32 xOutSz, yOutSz;
20956 
20957     printf(testingFmt, "wc_DsaExportKeyRaw()");
20958     XMEMSET(&rng, 0, sizeof(rng));
20959     XMEMSET(&key, 0, sizeof(key));
20960 
20961     ret = wc_InitRng(&rng);
20962     if (ret == 0) {
20963         ret = wc_InitDsaKey(&key);
20964     }
20965 
20966     if (ret == 0) {
20967         ret = wc_MakeDsaParameters(&rng, 1024, &key);
20968 
20969         if (ret == 0)  {
20970             ret = wc_MakeDsaKey(&rng, &key);
20971         }
20972     }
20973 
20974     /* try successful export */
20975     if (ret == 0) {
20976         xOutSz = sizeof(xOut);
20977         yOutSz = sizeof(yOut);
20978         ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
20979     }
20980 
20981     /* test bad args */
20982     if (ret == 0) {
20983         /* null key struct */
20984         ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
20985 
20986         if (ret == BAD_FUNC_ARG) {
20987             /* null output pointers */
20988             ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
20989         }
20990 
20991         if (ret == LENGTH_ONLY_E) {
20992             /* null output size pointers */
20993             ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
20994         }
20995 
20996         if (ret == BAD_FUNC_ARG) {
20997             /* x output buffer size too small */
20998             xOutSz = 1;
20999             ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
21000             xOutSz = sizeof(xOut);
21001         }
21002 
21003         if (ret == BUFFER_E) {
21004             /* y output buffer size too small */
21005             yOutSz = 1;
21006             ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
21007 
21008             if (ret == BUFFER_E)
21009                 ret = 0;
21010         }
21011     }
21012 
21013     printf(resultFmt, ret == 0 ? passed : failed);
21014 
21015     wc_FreeDsaKey(&key);
21016     wc_FreeRng(&rng);
21017 
21018 #endif
21019 
21020     return ret;
21021 
21022 } /* END test_wc_DsaExportParamsRaw */
21023 
21024 
21025 /*
21026  * Testing wc_ed25519_make_key().
21027  */
21028 static int test_wc_ed25519_make_key (void)
21029 {
21030     int ret = 0;
21031 
21032 #if defined(HAVE_ED25519)
21033     ed25519_key     key;
21034     WC_RNG          rng;
21035 
21036     ret = wc_InitRng(&rng);
21037     if (ret == 0) {
21038         ret = wc_ed25519_init(&key);
21039     }
21040     printf(testingFmt, "wc_ed25519_make_key()");
21041     if (ret == 0) {
21042         ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21043     }
21044     /* Test bad args. */
21045     if (ret == 0) {
21046         ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
21047         if (ret == BAD_FUNC_ARG) {
21048             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
21049         }
21050         if (ret == BAD_FUNC_ARG) {
21051             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
21052         }
21053         if (ret == BAD_FUNC_ARG) {
21054             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
21055         }
21056         if (ret == BAD_FUNC_ARG) {
21057             ret = 0;
21058         } else if (ret == 0) {
21059             ret = WOLFSSL_FATAL_ERROR;
21060         }
21061     }
21062 
21063 
21064     printf(resultFmt, ret == 0 ? passed : failed);
21065 
21066     if (wc_FreeRng(&rng) && ret == 0) {
21067         ret = WOLFSSL_FATAL_ERROR;
21068     }
21069     wc_ed25519_free(&key);
21070 
21071 #endif
21072     return ret;
21073 
21074 } /* END test_wc_ed25519_make_key */
21075 
21076 
21077 /*
21078  * Testing wc_ed25519_init()
21079  */
21080 static int test_wc_ed25519_init (void)
21081 {
21082     int             ret = 0;
21083 
21084 #if defined(HAVE_ED25519)
21085 
21086     ed25519_key    key;
21087 
21088     printf(testingFmt, "wc_ed25519_init()");
21089 
21090     ret = wc_ed25519_init(&key);
21091 
21092     /* Test bad args. */
21093     if (ret == 0) {
21094         ret = wc_ed25519_init(NULL);
21095         if (ret == BAD_FUNC_ARG) {
21096             ret = 0;
21097         } else if (ret == 0) {
21098             ret = WOLFSSL_FATAL_ERROR;
21099         }
21100     }
21101 
21102     printf(resultFmt, ret == 0 ? passed : failed);
21103 
21104     wc_ed25519_free(&key);
21105 
21106 #endif
21107     return ret;
21108 
21109 } /* END test_wc_ed25519_init */
21110 
21111 /*
21112  * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
21113  */
21114 static int test_wc_ed25519_sign_msg (void)
21115 {
21116     int             ret = 0;
21117 
21118 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
21119     WC_RNG          rng;
21120     ed25519_key     key;
21121     byte            msg[] = "Everybody gets Friday off.\n";
21122     byte            sig[ED25519_SIG_SIZE];
21123     word32          msglen = sizeof(msg);
21124     word32          siglen = sizeof(sig);
21125     word32          badSigLen = sizeof(sig) - 1;
21126 #ifdef HAVE_ED25519_VERIFY
21127     int             verify_ok = 0; /*1 = Verify success.*/
21128 #endif
21129 
21130     /* Initialize stack variables. */
21131     XMEMSET(sig, 0, siglen);
21132 
21133     /* Initialize key. */
21134     ret = wc_InitRng(&rng);
21135     if (ret == 0) {
21136         ret = wc_ed25519_init(&key);
21137         if (ret == 0) {
21138             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21139         }
21140     }
21141 
21142     printf(testingFmt, "wc_ed25519_sign_msg()");
21143 
21144     if (ret == 0) {
21145         ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
21146     }
21147     /* Test bad args. */
21148     if (ret == 0 && siglen == ED25519_SIG_SIZE) {
21149         ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
21150         if (ret == BAD_FUNC_ARG) {
21151             ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
21152         }
21153         if (ret == BAD_FUNC_ARG) {
21154             ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
21155         }
21156         if (ret == BAD_FUNC_ARG) {
21157             ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
21158         }
21159         if (ret == BAD_FUNC_ARG) {
21160             ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
21161         }
21162         if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
21163             badSigLen -= 1;
21164             ret = 0;
21165         } else if (ret == 0) {
21166             ret = WOLFSSL_FATAL_ERROR;
21167         }
21168     } /* END sign */
21169 
21170     printf(resultFmt, ret == 0 ? passed : failed);
21171     #ifdef HAVE_ED25519_VERIFY
21172         printf(testingFmt, "wc_ed25519_verify_msg()");
21173 
21174         if (ret == 0) {
21175 
21176             ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
21177             if (ret == 0  && verify_ok == 1) {
21178                 ret = 0;
21179             } else if (ret == 0) {
21180                 ret = WOLFSSL_FATAL_ERROR;
21181             }
21182 
21183             /* Test bad args. */
21184             if (ret == 0) {
21185                 AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
21186                                                   msglen, &verify_ok, &key),
21187                                                   BAD_FUNC_ARG);
21188                 AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
21189                                                   msglen, &verify_ok, &key),
21190                                                   BAD_FUNC_ARG);
21191 
21192                 ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
21193                                                                         &key);
21194                 if (ret == BAD_FUNC_ARG) {
21195                     ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
21196                                                                 &verify_ok, &key);
21197                 }
21198                 if (ret == BAD_FUNC_ARG) {
21199                     ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
21200                                                                   NULL, &key);
21201                 }
21202                 if (ret == BAD_FUNC_ARG) {
21203                     ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
21204                                                                 &verify_ok, NULL);
21205                 }
21206                 if (ret == BAD_FUNC_ARG) {
21207                     ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
21208                                                                 &verify_ok, &key);
21209                 }
21210                 if (ret == BAD_FUNC_ARG) {
21211                     ret = 0;
21212                 } else if (ret == 0) {
21213                     ret = WOLFSSL_FATAL_ERROR;
21214                 }
21215             }
21216 
21217         } /* END verify. */
21218 
21219         printf(resultFmt, ret == 0 ? passed : failed);
21220     #endif /* Verify. */
21221 
21222     if (wc_FreeRng(&rng) && ret == 0) {
21223         ret = WOLFSSL_FATAL_ERROR;
21224     }
21225     wc_ed25519_free(&key);
21226 
21227 #endif
21228     return ret;
21229 
21230 } /* END test_wc_ed25519_sign_msg */
21231 
21232 /*
21233  * Testing wc_ed25519_import_public()
21234  */
21235 static int test_wc_ed25519_import_public (void)
21236 {
21237     int             ret = 0;
21238 
21239 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
21240     WC_RNG          rng;
21241     ed25519_key     pubKey;
21242     const byte      in[] = "Ed25519PublicKeyUnitTest......\n";
21243     word32          inlen = sizeof(in);
21244 
21245 
21246     ret = wc_InitRng(&rng);
21247     if (ret == 0) {
21248         ret = wc_ed25519_init(&pubKey);
21249         if (ret == 0) {
21250             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
21251         }
21252     }
21253     printf(testingFmt, "wc_ed25519_import_public()");
21254 
21255     if (ret == 0) {
21256         ret = wc_ed25519_import_public(in, inlen, &pubKey);
21257 
21258         if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
21259             ret = 0;
21260         } else {
21261             ret = WOLFSSL_FATAL_ERROR;
21262         }
21263 
21264         /* Test bad args. */
21265         if (ret == 0) {
21266             ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
21267             if (ret == BAD_FUNC_ARG) {
21268                 ret = wc_ed25519_import_public(in, inlen, NULL);
21269             }
21270             if (ret == BAD_FUNC_ARG) {
21271                 ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
21272             }
21273             if (ret == BAD_FUNC_ARG) {
21274                 ret = 0;
21275             } else if (ret == 0) {
21276                 ret = WOLFSSL_FATAL_ERROR;
21277             }
21278         }
21279     }
21280 
21281     printf(resultFmt, ret == 0 ? passed : failed);
21282 
21283     if (wc_FreeRng(&rng) && ret == 0) {
21284         ret = WOLFSSL_FATAL_ERROR;
21285     }
21286     wc_ed25519_free(&pubKey);
21287 
21288 #endif
21289     return ret;
21290 
21291 } /* END wc_ed25519_import_public */
21292 
21293 /*
21294  * Testing wc_ed25519_import_private_key()
21295  */
21296 static int test_wc_ed25519_import_private_key (void)
21297 {
21298     int         ret = 0;
21299 
21300 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
21301     WC_RNG      rng;
21302     ed25519_key key;
21303     const byte  privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
21304     const byte  pubKey[] = "Ed25519PublicKeyUnitTest......\n";
21305     word32      privKeySz = sizeof(privKey);
21306     word32      pubKeySz = sizeof(pubKey);
21307 #ifdef HAVE_ED25519_KEY_EXPORT
21308     byte        bothKeys[sizeof(privKey) + sizeof(pubKey)];
21309     word32      bothKeysSz = sizeof(bothKeys);
21310 #endif
21311 
21312     ret = wc_InitRng(&rng);
21313     if (ret != 0) {
21314         return ret;
21315     }
21316     ret = wc_ed25519_init(&key);
21317     if (ret != 0) {
21318         wc_FreeRng(&rng);
21319         return ret;
21320     }
21321     ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21322 
21323     printf(testingFmt, "wc_ed25519_import_private_key()");
21324 
21325     if (ret == 0) {
21326         ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
21327                                                             pubKeySz, &key);
21328         if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
21329                                 || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
21330             ret = WOLFSSL_FATAL_ERROR;
21331         }
21332     }
21333 
21334 #ifdef HAVE_ED25519_KEY_EXPORT
21335     if (ret == 0)
21336         ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
21337 
21338     if (ret == 0) {
21339         ret = wc_ed25519_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
21340         if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
21341                                 || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
21342             ret = WOLFSSL_FATAL_ERROR;
21343         }
21344     }
21345 #endif
21346 
21347     /* Test bad args. */
21348     if (ret == 0) {
21349         ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
21350                                                                         &key);
21351         if (ret == BAD_FUNC_ARG) {
21352             ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
21353                                                                 pubKeySz, &key);
21354         }
21355         if (ret == BAD_FUNC_ARG) {
21356             ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
21357                                                                 pubKeySz, NULL);
21358         }
21359         if (ret == BAD_FUNC_ARG) {
21360             ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
21361                                                                 pubKeySz, &key);
21362         }
21363         if (ret == BAD_FUNC_ARG) {
21364             ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
21365                                                             pubKeySz - 1, &key);
21366         }
21367         if (ret == BAD_FUNC_ARG) {
21368             ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
21369                                                             0, &key);
21370         }
21371         if (ret == BAD_FUNC_ARG) {
21372             ret = 0;
21373         } else if (ret == 0) {
21374             ret = WOLFSSL_FATAL_ERROR;
21375         }
21376     }
21377 
21378     printf(resultFmt, ret == 0 ? passed : failed);
21379 
21380     if (wc_FreeRng(&rng) && ret == 0) {
21381         ret = WOLFSSL_FATAL_ERROR;
21382     }
21383     wc_ed25519_free(&key);
21384 
21385 #endif
21386     return ret;
21387 
21388 } /* END test_wc_ed25519_import_private_key */
21389 
21390 /*
21391  * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
21392  */
21393 static int test_wc_ed25519_export (void)
21394 {
21395     int             ret = 0;
21396 
21397 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
21398     WC_RNG          rng;
21399     ed25519_key     key;
21400     byte            priv[ED25519_PRV_KEY_SIZE];
21401     byte            pub[ED25519_PUB_KEY_SIZE];
21402     word32          privSz = sizeof(priv);
21403     word32          pubSz = sizeof(pub);
21404 
21405     ret = wc_InitRng(&rng);
21406     if (ret != 0) {
21407         return ret;
21408     }
21409 
21410     ret = wc_ed25519_init(&key);
21411     if (ret != 0) {
21412         wc_FreeRng(&rng);
21413         return ret;
21414     }
21415 
21416     if (ret == 0) {
21417         ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21418     }
21419 
21420     printf(testingFmt, "wc_ed25519_export_public()");
21421 
21422     if (ret == 0) {
21423         ret = wc_ed25519_export_public(&key, pub, &pubSz);
21424         if (ret == 0 && (pubSz != ED25519_KEY_SIZE
21425                                         || XMEMCMP(key.p, pub, pubSz) != 0)) {
21426             ret = WOLFSSL_FATAL_ERROR;
21427         }
21428         if (ret == 0) {
21429             ret = wc_ed25519_export_public(NULL, pub, &pubSz);
21430             if (ret == BAD_FUNC_ARG) {
21431                 ret = wc_ed25519_export_public(&key, NULL, &pubSz);
21432             }
21433             if (ret == BAD_FUNC_ARG) {
21434                 ret = wc_ed25519_export_public(&key, pub, NULL);
21435             }
21436             if (ret == BAD_FUNC_ARG) {
21437                 ret = 0;
21438             } else if (ret == 0) {
21439                 ret = WOLFSSL_FATAL_ERROR;
21440             }
21441         }
21442     }
21443 
21444     printf(resultFmt, ret == 0 ? passed : failed);
21445     printf(testingFmt, "wc_ed25519_export_private_only()");
21446 
21447     if (ret == 0) {
21448         ret = wc_ed25519_export_private_only(&key, priv, &privSz);
21449         if (ret == 0 && (privSz != ED25519_KEY_SIZE
21450                                         || XMEMCMP(key.k, priv, privSz) != 0)) {
21451             ret = WOLFSSL_FATAL_ERROR;
21452         }
21453         if (ret == 0) {
21454             ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
21455             if (ret == BAD_FUNC_ARG) {
21456                 ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
21457             }
21458             if (ret == BAD_FUNC_ARG) {
21459                 ret = wc_ed25519_export_private_only(&key, priv, NULL);
21460             }
21461             if (ret == BAD_FUNC_ARG) {
21462                 ret = 0;
21463             } else if (ret == 0) {
21464                 ret = WOLFSSL_FATAL_ERROR;
21465             }
21466         }
21467     }
21468 
21469     printf(resultFmt, ret == 0 ? passed : failed);
21470 
21471     if (wc_FreeRng(&rng) && ret == 0) {
21472         ret = WOLFSSL_FATAL_ERROR;
21473     }
21474     wc_ed25519_free(&key);
21475 
21476 #endif
21477     return ret;
21478 
21479 } /* END test_wc_ed25519_export */
21480 
21481 /*
21482  *  Testing wc_ed25519_size()
21483  */
21484 static int test_wc_ed25519_size (void)
21485 {
21486     int             ret = 0;
21487 #if defined(HAVE_ED25519)
21488     WC_RNG          rng;
21489     ed25519_key     key;
21490 
21491     ret = wc_InitRng(&rng);
21492     if (ret != 0) {
21493         return ret;
21494     }
21495     ret = wc_ed25519_init(&key);
21496     if (ret != 0) {
21497         wc_FreeRng(&rng);
21498         return ret;
21499     }
21500 
21501     ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21502     if (ret != 0) {
21503         wc_FreeRng(&rng);
21504         wc_ed25519_free(&key);
21505         return ret;
21506     }
21507 
21508     printf(testingFmt, "wc_ed25519_size()");
21509     ret = wc_ed25519_size(&key);
21510     /* Test bad args. */
21511     if (ret == ED25519_KEY_SIZE) {
21512         ret = wc_ed25519_size(NULL);
21513         if (ret == BAD_FUNC_ARG) {
21514             ret = 0;
21515         }
21516     }
21517     printf(resultFmt, ret == 0 ? passed : failed);
21518 
21519     if (ret == 0) {
21520         printf(testingFmt, "wc_ed25519_sig_size()");
21521 
21522         ret = wc_ed25519_sig_size(&key);
21523         if (ret == ED25519_SIG_SIZE) {
21524             ret = 0;
21525         }
21526         /* Test bad args. */
21527         if (ret == 0) {
21528             ret = wc_ed25519_sig_size(NULL);
21529             if (ret == BAD_FUNC_ARG) {
21530                 ret = 0;
21531             }
21532         }
21533 
21534         printf(resultFmt, ret == 0 ? passed : failed);
21535     } /* END wc_ed25519_sig_size() */
21536 
21537     if (ret == 0) {
21538         printf(testingFmt, "wc_ed25519_pub_size");
21539         ret = wc_ed25519_pub_size(&key);
21540         if (ret == ED25519_PUB_KEY_SIZE) {
21541             ret = 0;
21542         }
21543         if (ret == 0) {
21544             ret = wc_ed25519_pub_size(NULL);
21545             if (ret == BAD_FUNC_ARG) {
21546                 ret = 0;
21547             }
21548         }
21549         printf(resultFmt, ret == 0 ? passed : failed);
21550     } /* END wc_ed25519_pub_size */
21551 
21552     if (ret == 0) {
21553         printf(testingFmt, "wc_ed25519_priv_size");
21554         ret = wc_ed25519_priv_size(&key);
21555         if (ret == ED25519_PRV_KEY_SIZE) {
21556             ret = 0;
21557         }
21558         if (ret == 0) {
21559             ret = wc_ed25519_priv_size(NULL);
21560             if (ret == BAD_FUNC_ARG) {
21561                 ret = 0;
21562             }
21563         }
21564         printf(resultFmt, ret == 0 ? passed : failed);
21565     } /* END wc_ed25519_pub_size */
21566 
21567     if (wc_FreeRng(&rng) && ret == 0) {
21568         ret = WOLFSSL_FATAL_ERROR;
21569     }
21570     wc_ed25519_free(&key);
21571 
21572 #endif
21573     return ret;
21574 
21575 } /* END test_wc_ed25519_size */
21576 
21577 /*
21578  * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
21579  */
21580 static int test_wc_ed25519_exportKey (void)
21581 {
21582     int             ret = 0;
21583 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
21584     WC_RNG          rng;
21585     ed25519_key     key;
21586     byte            priv[ED25519_PRV_KEY_SIZE];
21587     byte            pub[ED25519_PUB_KEY_SIZE];
21588     byte            privOnly[ED25519_PRV_KEY_SIZE];
21589     word32          privSz      = sizeof(priv);
21590     word32          pubSz       = sizeof(pub);
21591     word32          privOnlySz  = sizeof(privOnly);
21592 
21593     ret = wc_InitRng(&rng);
21594     if (ret != 0) {
21595         return ret;
21596     }
21597     ret = wc_ed25519_init(&key);
21598     if (ret != 0) {
21599         wc_FreeRng(&rng);
21600         return ret;
21601     }
21602 
21603     ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21604     if (ret != 0) {
21605         wc_FreeRng(&rng);
21606         wc_ed25519_free(&key);
21607         return ret;
21608     }
21609 
21610     printf(testingFmt, "wc_ed25519_export_private()");
21611 
21612     ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
21613     if (ret == 0) {
21614         ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
21615         if (ret == BAD_FUNC_ARG) {
21616             ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
21617         }
21618         if (ret == BAD_FUNC_ARG) {
21619             ret = wc_ed25519_export_private(&key, privOnly, NULL);
21620         }
21621         if (ret == BAD_FUNC_ARG) {
21622             ret = 0;
21623         } else if (ret == 0) {
21624             ret = WOLFSSL_FATAL_ERROR;
21625         }
21626     }
21627 
21628     printf(resultFmt, ret == 0 ? passed : failed);
21629 
21630     if (ret == 0) {
21631         printf(testingFmt, "wc_ed25519_export_key()");
21632 
21633         ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
21634         if (ret == 0) {
21635             ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
21636             if (ret == BAD_FUNC_ARG) {
21637                 ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
21638             }
21639             if (ret == BAD_FUNC_ARG) {
21640                 ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
21641             }
21642             if (ret == BAD_FUNC_ARG) {
21643                 ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
21644             }
21645             if (ret == BAD_FUNC_ARG) {
21646                 ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
21647             }
21648             if (ret == BAD_FUNC_ARG) {
21649                 ret = 0;
21650             } else if (ret == 0) {
21651                 ret = WOLFSSL_FATAL_ERROR;
21652             }
21653         }
21654         printf(resultFmt, ret == 0 ? passed : failed);
21655     } /* END wc_ed25519_export_key() */
21656 
21657     /* Cross check output. */
21658     if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
21659         ret = WOLFSSL_FATAL_ERROR;
21660     }
21661 
21662     if (wc_FreeRng(&rng) && ret == 0) {
21663         ret = WOLFSSL_FATAL_ERROR;
21664     }
21665     wc_ed25519_free(&key);
21666 
21667 #endif
21668     return ret;
21669 
21670 } /* END test_wc_ed25519_exportKey */
21671 
21672 /*
21673  * Testing wc_Ed25519PublicKeyToDer
21674  */
21675 static int test_wc_Ed25519PublicKeyToDer (void)
21676 {
21677     int ret = 0;
21678 
21679 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
21680     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
21681     int tmp;
21682     ed25519_key key;
21683     byte derBuf[1024];
21684 
21685     printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
21686 
21687     /* Test bad args */
21688     tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
21689     if (tmp != BAD_FUNC_ARG) {
21690         ret = WOLFSSL_FATAL_ERROR;
21691     }
21692 
21693     if (ret == 0) {
21694         wc_ed25519_init(&key);
21695         tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
21696         if (tmp != BUFFER_E) {
21697             ret = WOLFSSL_FATAL_ERROR;
21698         }
21699         wc_ed25519_free(&key);
21700     }
21701 
21702     /*  Test good args */
21703     if (ret == 0) {
21704         WC_RNG          rng;
21705         ret = wc_InitRng(&rng);
21706         if (ret != 0) {
21707             return ret;
21708         }
21709         ret = wc_ed25519_init(&key);
21710         if (ret != 0) {
21711             wc_FreeRng(&rng);
21712             return ret;
21713         }
21714 
21715         ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
21716         if (ret != 0) {
21717             wc_FreeRng(&rng);
21718             wc_ed25519_free(&key);
21719             return ret;
21720         }
21721 
21722         tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
21723         if (tmp <= 0) {
21724             ret = WOLFSSL_FATAL_ERROR;
21725         }
21726 
21727         wc_FreeRng(&rng);
21728         wc_ed25519_free(&key);
21729     }
21730     printf(resultFmt, ret == 0 ? passed : failed);
21731 #endif
21732     return ret;
21733 
21734 } /* END testing wc_Ed25519PublicKeyToDer */
21735 
21736 /*
21737  * Testing wc_curve25519_init and wc_curve25519_free.
21738  */
21739 static int test_wc_curve25519_init (void)
21740 {
21741     int ret = 0;
21742 
21743 #if defined(HAVE_CURVE25519)
21744 
21745     curve25519_key  key;
21746 
21747     printf(testingFmt, "wc_curve25519_init()");
21748     ret = wc_curve25519_init(&key);
21749 
21750     /* Test bad args for wc_curve25519_init */
21751     if (ret == 0) {
21752         ret = wc_curve25519_init(NULL);
21753         if (ret == BAD_FUNC_ARG) {
21754             ret = 0;
21755         } else if (ret == 0) {
21756             ret = WOLFSSL_FATAL_ERROR;
21757         }
21758     }
21759 
21760     printf(resultFmt, ret == 0 ? passed : failed);
21761     /*  Test good args for wc_curve_25519_free */
21762     wc_curve25519_free(&key);
21763 
21764     wc_curve25519_free(NULL);
21765 
21766 #endif
21767     return ret;
21768 
21769 } /* END test_wc_curve25519_init and wc_curve_25519_free*/
21770 /*
21771  * Testing test_wc_curve25519_size.
21772  */
21773 static int test_wc_curve25519_size (void)
21774 {
21775     int ret = 0;
21776 
21777 #if defined(HAVE_CURVE25519)
21778 
21779     curve25519_key  key;
21780 
21781     printf(testingFmt, "wc_curve25519_size()");
21782 
21783     ret = wc_curve25519_init(&key);
21784 
21785     /*  Test good args for wc_curve25519_size */
21786     if (ret == 0) {
21787         ret = wc_curve25519_size(&key);
21788     }
21789 
21790     /* Test bad args for wc_curve25519_size */
21791     if (ret != 0) {
21792         ret = wc_curve25519_size(NULL);
21793     }
21794 
21795     printf(resultFmt, ret == 0 ? passed : failed);
21796     wc_curve25519_free(&key);
21797 #endif
21798     return ret;
21799 
21800 } /* END test_wc_curve25519_size*/
21801 
21802 /*
21803  * Testing test_wc_curve25519_export_key_raw().
21804  */
21805 static int test_wc_curve25519_export_key_raw (void)
21806 {
21807 
21808 #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
21809 
21810     curve25519_key  key;
21811     WC_RNG          rng;
21812 
21813     byte            privateKey[CURVE25519_KEYSIZE];
21814     byte            publicKey[CURVE25519_KEYSIZE];
21815     word32          prvkSz;
21816     word32          pubkSz;
21817 
21818     byte            prik[CURVE25519_KEYSIZE];
21819     byte            pubk[CURVE25519_KEYSIZE];
21820     word32          prksz;
21821     word32          pbksz;
21822 
21823     printf(testingFmt, "wc_curve25519_export_key_raw()");
21824 
21825 
21826     if(0 != wc_InitRng(&rng)){
21827         printf(testingFmt, "failed due to wc_InitRng");
21828         fflush( stdout );
21829         return  1;
21830     }
21831 
21832     if(0 != wc_curve25519_init(&key)){
21833         printf(testingFmt, "failed due to wc_curve25519_init");
21834         fflush( stdout );
21835         wc_FreeRng(&rng);
21836         return  1;
21837     }
21838 
21839     if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
21840         printf(testingFmt, "failed due to wc_curve25519_make_key");
21841         fflush( stdout );
21842         wc_curve25519_free(&key);
21843         wc_FreeRng(&rng);
21844         return  1;
21845     }
21846 
21847     /*
21848         bad-argument-test cases
21849         target function sould return BAD_FUNC_ARG
21850     */
21851     prvkSz = CURVE25519_KEYSIZE;
21852     pubkSz = CURVE25519_KEYSIZE;
21853 
21854     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
21855             NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
21856 
21857         printf(testingFmt,"failed at bad-arg-case-1.");
21858         fflush( stdout );
21859         wc_curve25519_free(&key);
21860         wc_FreeRng(&rng);
21861         return  1;
21862 
21863     }
21864 
21865     prvkSz = CURVE25519_KEYSIZE;
21866     pubkSz = CURVE25519_KEYSIZE;
21867 
21868     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
21869             &key , NULL, &prvkSz, publicKey, &pubkSz)){
21870 
21871         printf(testingFmt,"failed at bad-arg-case-2.");
21872         fflush( stdout );
21873         wc_curve25519_free(&key);
21874         wc_FreeRng(&rng);
21875         return  1;
21876 
21877     }
21878 
21879     prvkSz = CURVE25519_KEYSIZE;
21880     pubkSz = CURVE25519_KEYSIZE;
21881 
21882     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
21883             &key , privateKey, NULL, publicKey, &pubkSz)){
21884 
21885         printf(testingFmt,"failed at bad-arg-case-3.");
21886         fflush( stdout );
21887         wc_curve25519_free(&key);
21888         wc_FreeRng(&rng);
21889         return  1;
21890 
21891     }
21892 
21893     /* prvkSz = CURVE25519_KEYSIZE; */
21894     pubkSz = CURVE25519_KEYSIZE;
21895 
21896     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
21897             &key , privateKey, &prvkSz, NULL, &pubkSz)){
21898 
21899         printf(testingFmt,"failed at bad-arg-case-4.");
21900         fflush( stdout );
21901         wc_curve25519_free(&key);
21902         wc_FreeRng(&rng);
21903         return  1;
21904 
21905     }
21906 
21907     prvkSz = CURVE25519_KEYSIZE;
21908     pubkSz = CURVE25519_KEYSIZE;
21909 
21910     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
21911             &key , privateKey, &prvkSz, publicKey, NULL )){
21912 
21913         printf(testingFmt,"failed at bad-arg-case-5.");
21914         fflush( stdout );
21915         wc_curve25519_free(&key);
21916         wc_FreeRng(&rng);
21917         return  1;
21918 
21919     }
21920 
21921     /*
21922         cross-testing
21923     */
21924     prksz = CURVE25519_KEYSIZE;
21925 
21926     if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
21927 
21928         printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
21929         fflush( stdout );
21930         wc_curve25519_free(&key);
21931         wc_FreeRng(&rng);
21932         return  1;
21933 
21934     }
21935 
21936     pbksz = CURVE25519_KEYSIZE;
21937 
21938     if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
21939 
21940         printf(testingFmt,"failed due to wc_curve25519_export_public");
21941         fflush( stdout );
21942         wc_curve25519_free(&key);
21943         wc_FreeRng(&rng);
21944         return  1;
21945 
21946     }
21947 
21948     prvkSz = CURVE25519_KEYSIZE;
21949     /* pubkSz = CURVE25519_KEYSIZE; */
21950 
21951     if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
21952                                                      publicKey,  &pubkSz)){
21953 
21954         printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
21955         fflush( stdout );
21956         wc_curve25519_free(&key);
21957         wc_FreeRng(&rng);
21958         return  1;
21959 
21960     }
21961 
21962     if((prksz  == CURVE25519_KEYSIZE) &&
21963        (pbksz  == CURVE25519_KEYSIZE) &&
21964        (prvkSz == CURVE25519_KEYSIZE) &&
21965        (pubkSz == CURVE25519_KEYSIZE)){
21966 
21967         if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
21968             0 == XMEMCMP(publicKey,  pubk, CURVE25519_KEYSIZE)){
21969 
21970             printf(resultFmt,passed);
21971             fflush( stdout );
21972             wc_curve25519_free(&key);
21973             wc_FreeRng(&rng);
21974             return  0;
21975 
21976         }
21977         else{
21978 
21979             printf(testingFmt,"failed due to key-contents-inconsistency.");
21980             fflush( stdout );
21981             wc_curve25519_free(&key);
21982             wc_FreeRng(&rng);
21983             return  1;
21984         }
21985     }
21986     else{
21987 
21988         printf(testingFmt,"failed due to bad-key-size.");
21989         fflush( stdout );
21990         wc_curve25519_free(&key);
21991         wc_FreeRng(&rng);
21992         return  1;
21993     }
21994 
21995 #endif
21996     fflush( stdout );
21997 
21998     return 0;
21999 } /* end of test_wc_curve25519_export_key_raw */
22000 
22001 /*
22002  * Testing test_wc_curve25519_export_key_raw_ex().
22003  */
22004 static int test_wc_curve25519_export_key_raw_ex (void)
22005 {
22006 
22007 #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
22008 
22009     curve25519_key  key;
22010     WC_RNG          rng;
22011 
22012     byte            privateKey[CURVE25519_KEYSIZE];
22013     byte            publicKey[CURVE25519_KEYSIZE];
22014     word32          prvkSz;
22015     word32          pubkSz;
22016 
22017     byte            prik[CURVE25519_KEYSIZE];
22018     byte            pubk[CURVE25519_KEYSIZE];
22019     word32          prksz;
22020     word32          pbksz;
22021 
22022     printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
22023 
22024     if(0 != wc_InitRng(&rng)){
22025         printf(testingFmt, "failed due to wc_InitRng");
22026         fflush( stdout );
22027         return  1;
22028     }
22029 
22030     if(0 != wc_curve25519_init(&key)){
22031         printf(testingFmt, "failed due to wc_curve25519_init");
22032         fflush( stdout );
22033         wc_FreeRng(&rng);
22034         return  1;
22035     }
22036 
22037     if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
22038         printf(testingFmt, "failed due to wc_curve25519_make_key");
22039         fflush( stdout );
22040         wc_curve25519_free(&key);
22041         wc_FreeRng(&rng);
22042         return  1;
22043     }
22044 
22045     /*
22046         bad-argument-test cases
22047         target function sould return BAD_FUNC_ARG
22048     */
22049     prvkSz = CURVE25519_KEYSIZE;
22050     pubkSz = CURVE25519_KEYSIZE;
22051 
22052     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
22053                      &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
22054 
22055         printf(testingFmt,"failed at bad-arg-case-1.");
22056         fflush( stdout );
22057         wc_curve25519_free(&key);
22058         wc_FreeRng(&rng);
22059         return  1;
22060 
22061     }
22062 
22063     prvkSz = CURVE25519_KEYSIZE;
22064     pubkSz = CURVE25519_KEYSIZE;
22065 
22066     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
22067                     &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
22068 
22069         printf(testingFmt,"failed at bad-arg-case-2.");
22070         fflush( stdout );
22071         wc_curve25519_free(&key);
22072         wc_FreeRng(&rng);
22073         return  1;
22074     }
22075 
22076     prvkSz = CURVE25519_KEYSIZE;
22077     pubkSz = CURVE25519_KEYSIZE;
22078 
22079     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
22080                             NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
22081 
22082         printf(testingFmt,"failed at bad-arg-case-3.");
22083         fflush( stdout );
22084         wc_curve25519_free(&key);
22085         wc_FreeRng(&rng);
22086         return  1;
22087     }
22088 
22089     /* prvkSz = CURVE25519_KEYSIZE; */
22090     pubkSz = CURVE25519_KEYSIZE;
22091 
22092     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22093      &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
22094 
22095         printf(testingFmt,"failed at bad-arg-case-4.");
22096         fflush( stdout );
22097         wc_curve25519_free(&key);
22098         wc_FreeRng(&rng);
22099         return  1;
22100     }
22101 
22102     prvkSz = CURVE25519_KEYSIZE;
22103     pubkSz = CURVE25519_KEYSIZE;
22104 
22105     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22106      &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
22107 
22108         printf(testingFmt,"failed at bad-arg-case-5.");
22109         fflush( stdout );
22110         wc_curve25519_free(&key);
22111         wc_FreeRng(&rng);
22112         return  1;
22113     }
22114 
22115     prvkSz = CURVE25519_KEYSIZE;
22116     /* pubkSz = CURVE25519_KEYSIZE; */
22117 
22118     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
22119      &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
22120 
22121         printf(testingFmt,"failed at bad-arg-case-6.");
22122         fflush( stdout );
22123         wc_curve25519_free(&key);
22124         wc_FreeRng(&rng);
22125         return  1;
22126     }
22127 
22128     prvkSz = CURVE25519_KEYSIZE;
22129     pubkSz = CURVE25519_KEYSIZE;
22130 
22131     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
22132             publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
22133 
22134         printf(testingFmt,"failed at bad-arg-case-7.");
22135         fflush( stdout );
22136         wc_curve25519_free(&key);
22137         wc_FreeRng(&rng);
22138         return  1;
22139     }
22140 
22141     prvkSz = CURVE25519_KEYSIZE;
22142     pubkSz = CURVE25519_KEYSIZE;
22143 
22144     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22145                               NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
22146 
22147         printf(testingFmt,"failed at bad-arg-case-8.");
22148         fflush( stdout );
22149         wc_curve25519_free(&key);
22150         wc_FreeRng(&rng);
22151         return  1;
22152     }
22153 
22154     /* prvkSz = CURVE25519_KEYSIZE; */
22155     pubkSz = CURVE25519_KEYSIZE;
22156 
22157     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22158     &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
22159 
22160         printf(testingFmt,"failed at bad-arg-case-9.");
22161         fflush( stdout );
22162         wc_curve25519_free(&key);
22163         wc_FreeRng(&rng);
22164         return  1;
22165     }
22166 
22167     prvkSz = CURVE25519_KEYSIZE;
22168     pubkSz = CURVE25519_KEYSIZE;
22169 
22170     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22171      &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
22172 
22173         printf(testingFmt,"failed at bad-arg-case-10.");
22174         fflush( stdout );
22175         wc_curve25519_free(&key);
22176         wc_FreeRng(&rng);
22177         return  1;
22178     }
22179 
22180     /* illegal value for endien */
22181 
22182     prvkSz = CURVE25519_KEYSIZE;
22183     /* pubkSz = CURVE25519_KEYSIZE; */
22184 
22185     if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
22186      &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
22187 
22188         printf(testingFmt,"failed at bad-arg-case-11.");
22189         fflush( stdout );
22190         wc_curve25519_free(&key);
22191         wc_FreeRng(&rng);
22192         return  1;
22193     }
22194 
22195     /*
22196         cross-testing
22197     */
22198     prksz = CURVE25519_KEYSIZE;
22199 
22200     if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
22201 
22202         printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
22203         fflush( stdout );
22204         wc_curve25519_free(&key);
22205         wc_FreeRng(&rng);
22206         return  1;
22207     }
22208 
22209     pbksz = CURVE25519_KEYSIZE;
22210 
22211     if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
22212 
22213         printf(testingFmt,"failed due to wc_curve25519_export_public");
22214         fflush( stdout );
22215         wc_curve25519_free(&key);
22216         wc_FreeRng(&rng);
22217         return  1;
22218 
22219     }
22220 
22221     prvkSz = CURVE25519_KEYSIZE;
22222     /* pubkSz = CURVE25519_KEYSIZE; */
22223 
22224     if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
22225                     publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
22226 
22227         printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
22228         fflush( stdout );
22229         wc_curve25519_free(&key);
22230         wc_FreeRng(&rng);
22231         return  1;
22232     }
22233 
22234     if( prksz  == CURVE25519_KEYSIZE &&
22235         pbksz  == CURVE25519_KEYSIZE &&
22236         prvkSz == CURVE25519_KEYSIZE &&
22237         pubkSz == CURVE25519_KEYSIZE ){
22238 
22239         if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
22240             0 == XMEMCMP( publicKey,  pubk, CURVE25519_KEYSIZE )){
22241 
22242             if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
22243                 &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
22244 
22245                 if( prvkSz == CURVE25519_KEYSIZE &&
22246                     pubkSz == CURVE25519_KEYSIZE ){
22247 
22248                     ;   /* proceed to the next test */
22249                 }
22250                 else{
22251 
22252                     printf(testingFmt,"failed due to key-size-inconsistency");
22253                     fflush( stdout );
22254                     wc_curve25519_free(&key);
22255                     wc_FreeRng(&rng);
22256                     return  1;
22257                 }
22258             }
22259             else{
22260 
22261                 printf(testingFmt,
22262                             "failed due to wc_curve25519_export_key_raw_ex");
22263                 fflush( stdout );
22264                 wc_curve25519_free(&key);
22265                 wc_FreeRng(&rng);
22266                 return  1;
22267 
22268             }
22269         }
22270         else{
22271 
22272             printf(testingFmt,"failed due to key-contents-inconsistency");
22273             fflush( stdout );
22274             wc_curve25519_free(&key);
22275             wc_FreeRng(&rng);
22276             return  1;
22277 
22278         }
22279     }
22280     else{
22281 
22282         printf(testingFmt,"failed due to bad-key-size");
22283         fflush( stdout );
22284         wc_curve25519_free(&key);
22285         wc_FreeRng(&rng);
22286         return  1;
22287     }
22288 
22289     /*
22290         try once with another endian
22291     */
22292 
22293     prvkSz = CURVE25519_KEYSIZE;
22294     pubkSz = CURVE25519_KEYSIZE;
22295 
22296     if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
22297     &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
22298 
22299         if( prvkSz == CURVE25519_KEYSIZE &&
22300             pubkSz == CURVE25519_KEYSIZE ){
22301 
22302                 /* no more test*/
22303                 printf(resultFmt, passed );
22304                 fflush( stdout );
22305                 wc_curve25519_free(&key);
22306                 wc_FreeRng(&rng);
22307                 return 0;
22308         }
22309         else{
22310 
22311             printf(testingFmt,"failed due to key-size-inconsistency");
22312             fflush( stdout );
22313             wc_curve25519_free(&key);
22314             wc_FreeRng(&rng);
22315             return  1;
22316         }
22317     }
22318     else{
22319 
22320         printf(testingFmt,
22321                    "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
22322         fflush( stdout );
22323         wc_curve25519_free(&key);
22324         wc_FreeRng(&rng);
22325         return  1;
22326     }
22327 
22328 #else
22329     return 0;
22330 #endif
22331 } /* end of test_wc_curve25519_export_key_raw_ex */
22332 /*
22333  * Testing wc_curve25519_make_key
22334  */
22335 static int test_wc_curve25519_make_key (void)
22336 {
22337     int ret = 0;
22338 #if defined(HAVE_CURVE25519)
22339     WC_RNG          rng;
22340     curve25519_key  key;
22341     int             keysize;
22342 
22343 
22344     printf(testingFmt, "wc_curve25519_make_key()");
22345 
22346     ret = wc_curve25519_init(&key);
22347     if (ret == 0) {
22348         ret = wc_InitRng(&rng);
22349     }
22350     if (ret == 0) {
22351 
22352         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
22353         if (ret == 0) {
22354             keysize = wc_curve25519_size(&key);
22355             if (keysize != CURVE25519_KEYSIZE) {
22356                 ret = WOLFSSL_FATAL_ERROR;
22357             }
22358         }
22359         if (ret == 0) {
22360             ret = wc_curve25519_make_key(&rng, keysize, &key);
22361         }
22362     }
22363     /*test bad cases*/
22364     if (ret == 0) {
22365         ret = wc_curve25519_make_key(NULL, 0, NULL);
22366         if (ret == BAD_FUNC_ARG) {
22367             ret = 0;
22368         }
22369     }
22370     if (ret == 0) {
22371         ret = wc_curve25519_make_key(&rng, keysize, NULL);
22372         if (ret == BAD_FUNC_ARG) {
22373             ret = 0;
22374         }
22375     }
22376     if (ret == 0) {
22377         ret = wc_curve25519_make_key(NULL, keysize, &key);
22378         if (ret == BAD_FUNC_ARG) {
22379             ret = 0;
22380         }
22381     }
22382     if (ret == 0) {
22383         ret = wc_curve25519_make_key(&rng, 0, &key);
22384         if (ret == ECC_BAD_ARG_E) {
22385             ret = 0;
22386         }
22387     }
22388     printf(resultFmt, ret == 0 ? passed : failed);
22389     wc_curve25519_free(&key);
22390     wc_FreeRng(&rng);
22391 #endif
22392     return ret;
22393 } /*END test_wc_curve25519_make_key*/
22394 /*
22395  * Testing wc_curve25519_shared_secret_ex
22396  */
22397 static int test_wc_curve25519_shared_secret_ex(void)
22398 {
22399     int ret = 0;
22400 #if defined(HAVE_CURVE25519)
22401     WC_RNG          rng;
22402     curve25519_key  private_key, public_key;
22403     byte            out[CURVE25519_KEYSIZE];
22404     word32          outLen = sizeof(out);
22405     int             endian = EC25519_BIG_ENDIAN;
22406 
22407     printf(testingFmt, "wc_curve25519_shared_secret_ex()");
22408 
22409     ret = wc_curve25519_init(&private_key);
22410     if (ret == 0) {
22411         ret = wc_curve25519_init(&public_key);
22412     }
22413     if (ret == 0) {
22414         ret = wc_InitRng(&rng);
22415     }
22416     if (ret == 0) {
22417         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
22418     }
22419     if (ret == 0) {
22420         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
22421     }
22422     if (ret == 0) {
22423         ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
22424                                               &outLen, endian);
22425     }
22426     /*test bad cases*/
22427     if (ret == 0) {
22428         ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
22429                                               0, endian);
22430         if (ret == 0) {
22431             ret = -1;
22432         }
22433         if (ret == BAD_FUNC_ARG) {
22434             ret = 0;
22435         }
22436     }
22437     if (ret == 0) {
22438         ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
22439                                              &outLen, endian);
22440         if (ret == 0) {
22441             ret = -1;
22442         }
22443         else if (ret == BAD_FUNC_ARG) {
22444             ret = 0;
22445         }
22446     }
22447     if (ret == 0) {
22448         ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
22449                                               &outLen, endian);
22450         if (ret == 0) {
22451             ret = -1;
22452         }
22453         else if (ret == BAD_FUNC_ARG) {
22454             ret = 0;
22455         }
22456     }
22457     if (ret == 0) {
22458         ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
22459                                               &outLen, endian);
22460         if (ret == 0) {
22461             ret = -1;
22462         }
22463         else if (ret == BAD_FUNC_ARG) {
22464             ret = 0;
22465         }
22466     }
22467     if (ret == 0) {
22468         ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
22469                                               NULL, endian);
22470         if (ret == 0) {
22471             ret = -1;
22472         }
22473         else if (ret == BAD_FUNC_ARG) {
22474             ret = 0;
22475         }
22476     }
22477 
22478     if (ret == 0) {
22479     /*curve25519.c is checking for public_key size less than or equal to 0x7f,
22480      *increasing to 0x8f checks for error being returned*/
22481         public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
22482         ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
22483                                               &outLen, endian);
22484         if (ret == 0) {
22485             ret = -1;
22486         }
22487         else if (ret == ECC_BAD_ARG_E) {
22488            ret = 0;
22489         }
22490     }
22491 
22492     outLen = outLen - 2;
22493     if (ret == 0) {
22494         ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
22495                                               &outLen, endian);
22496         if (ret == 0) {
22497             ret = -1;
22498         }
22499         else if (ret == BAD_FUNC_ARG) {
22500             ret = 0;
22501         }
22502     }
22503 
22504 
22505     printf(resultFmt, ret == 0 ? passed : failed);
22506     wc_curve25519_free(&private_key);
22507     wc_curve25519_free(&public_key);
22508     wc_FreeRng(&rng);
22509 #endif
22510     return ret;
22511 } /*END test_wc_curve25519_shared_secret_ex*/
22512 /*
22513  * Testing wc_curve25519_make_pub
22514  */
22515 static int test_wc_curve25519_make_pub(void)
22516 {
22517     int ret = 0;
22518 #ifdef HAVE_CURVE25519
22519     WC_RNG          rng;
22520     curve25519_key  key;
22521     byte            out[CURVE25519_KEYSIZE];
22522 
22523     printf(testingFmt, "wc_curve25519_make_pub()");
22524 
22525     ret = wc_curve25519_init(&key);
22526     if (ret == 0) {
22527         ret = wc_InitRng(&rng);
22528         if (ret == 0) {
22529             ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
22530         }
22531     }
22532     if (ret == 0) {
22533       ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
22534     }
22535     /*test bad cases*/
22536     if (ret == 0) {
22537         ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
22538         if (ret == ECC_BAD_ARG_E) {
22539             ret = 0;
22540         }
22541     }
22542     if (ret == 0) {
22543         ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
22544         if (ret == ECC_BAD_ARG_E) {
22545             ret = 0;
22546         }
22547     }
22548     if (ret == 0) {
22549         ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
22550         if (ret == ECC_BAD_ARG_E) {
22551             ret = 0;
22552         }
22553     }
22554     if (ret == 0) {
22555         ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
22556         if (ret == ECC_BAD_ARG_E) {
22557             ret = 0;
22558         }
22559     }
22560     if (ret == 0) {
22561         /* verify clamping test */
22562         key.k[0] |= ~248;
22563         ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
22564         if (ret == ECC_BAD_ARG_E) {
22565             ret = 0;
22566         }
22567         key.k[0] &= 248;
22568     }
22569     /* repeat the expected-to-succeed test. */
22570     if (ret == 0) {
22571       ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
22572     }
22573 
22574     printf(resultFmt, ret == 0 ? passed : failed);
22575     wc_curve25519_free(&key);
22576     wc_FreeRng(&rng);
22577 #endif
22578     return ret;
22579 } /*END test_wc_curve25519_make_pub */
22580 /*
22581  * Testing test_wc_curve25519_export_public_ex
22582  */
22583 static int test_wc_curve25519_export_public_ex (void)
22584 {
22585     int ret = 0;
22586 #if defined(HAVE_CURVE25519)
22587 
22588     WC_RNG          rng;
22589     curve25519_key  key;
22590     byte            out[CURVE25519_KEYSIZE];
22591     word32          outLen = sizeof(out);
22592     int             endian = EC25519_BIG_ENDIAN;
22593 
22594     printf(testingFmt, "wc_curve25519_export_public_ex()");
22595 
22596     ret = wc_curve25519_init(&key);
22597     if (ret == 0) {
22598         ret = wc_InitRng(&rng);
22599     }
22600     if (ret == 0) {
22601 
22602         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
22603         if (ret == 0) {
22604             ret = wc_curve25519_export_public(&key, out, &outLen);
22605             }
22606         if (ret == 0) {
22607             ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
22608         }
22609     }
22610     /*test bad cases*/
22611     if (ret == 0) {
22612         ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
22613         if (ret == BAD_FUNC_ARG) {
22614             ret = 0;
22615         }
22616     }
22617     if (ret == 0) {
22618         ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
22619         if (ret == BAD_FUNC_ARG) {
22620             ret = 0;
22621         }
22622     }
22623     if (ret == 0) {
22624         ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
22625         if (ret == BAD_FUNC_ARG) {
22626             ret = 0;
22627         }
22628     }
22629     if (ret == 0) {
22630         ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
22631         if (ret == BAD_FUNC_ARG) {
22632             ret = 0;
22633         }
22634     }
22635     outLen = outLen - 2;
22636     if (ret == 0) {
22637         ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
22638         if (ret == ECC_BAD_ARG_E) {
22639             ret = 0;
22640         }
22641     }
22642 
22643     printf(resultFmt, ret == 0 ? passed : failed);
22644     wc_curve25519_free(&key);
22645     wc_FreeRng(&rng);
22646 #endif
22647     return ret;
22648 
22649 } /*END test_wc_curve25519_export_public_ex*/
22650 /*
22651  * Testing test_wc_curve25519_import_private_raw_ex
22652  */
22653 static int test_wc_curve25519_import_private_raw_ex (void)
22654 {
22655     int ret = 0;
22656 #if defined(HAVE_CURVE25519)
22657     WC_RNG          rng;
22658     curve25519_key  key;
22659     byte            priv[CURVE25519_KEYSIZE];
22660     byte            pub[CURVE25519_KEYSIZE];
22661     word32          privSz = sizeof(priv);
22662     word32          pubSz = sizeof(pub);
22663     int             endian = EC25519_BIG_ENDIAN;
22664 
22665 
22666     printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
22667 
22668     ret = wc_curve25519_init(&key);
22669     if (ret == 0) {
22670         ret = wc_InitRng(&rng);
22671     }
22672     if (ret == 0) {
22673 
22674         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
22675         if (ret == 0) {
22676             ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
22677         }
22678         if (ret == 0) {
22679             ret = wc_curve25519_export_public(&key, pub, &pubSz);
22680         }
22681         if (ret == 0) {
22682             ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
22683                                                      &key, endian);
22684         }
22685     }
22686     /*test bad cases*/
22687     if (ret == 0) {
22688         ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
22689                                                    endian);
22690         if (ret == BAD_FUNC_ARG) {
22691             ret = 0;
22692         }
22693     }
22694     if (ret == 0) {
22695         ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
22696                                                  &key, endian);
22697         if (ret == BAD_FUNC_ARG) {
22698             ret = 0;
22699         }
22700     }
22701     if (ret == 0) {
22702         ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
22703                                                  &key, endian);
22704         if (ret == BAD_FUNC_ARG) {
22705             ret = 0;
22706         }
22707     }
22708     if (ret == 0) {
22709         ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
22710                                                  NULL, endian);
22711         if (ret == BAD_FUNC_ARG) {
22712             ret = 0;
22713         }
22714     }
22715     if (ret == 0) {
22716         ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
22717                                                  &key, endian);
22718         if (ret == ECC_BAD_ARG_E) {
22719             ret = 0;
22720         }
22721     }
22722     if (ret == 0) {
22723         ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
22724                                                  &key, endian);
22725         if (ret == ECC_BAD_ARG_E) {
22726             ret = 0;
22727         }
22728     }
22729     if (ret == 0) {
22730         ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
22731                                                  &key, EC25519_LITTLE_ENDIAN);
22732 
22733     }
22734 
22735     printf(resultFmt, ret == 0 ? passed : failed);
22736     wc_curve25519_free(&key);
22737     wc_FreeRng(&rng);
22738 
22739 #endif
22740     return ret;
22741 } /*END test_wc_curve25519_import_private_raw_ex*/
22742 /*
22743  * Testing test_wc_curve25519_import_private
22744  */
22745 static int test_wc_curve25519_import_private (void)
22746 {
22747     int ret = 0;
22748 #if defined(HAVE_CURVE25519)
22749 
22750     curve25519_key     key;
22751     WC_RNG             rng;
22752     byte               priv[CURVE25519_KEYSIZE];
22753     word32             privSz = sizeof(priv);
22754 
22755     printf(testingFmt, "wc_curve25519_import_private()");
22756 
22757     ret = wc_curve25519_init(&key);
22758     if (ret == 0) {
22759         ret = wc_InitRng(&rng);
22760     }
22761     if (ret == 0) {
22762 
22763         ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
22764         if (ret == 0) {
22765             ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
22766         }
22767     }
22768     if (ret == 0) {
22769         ret = wc_curve25519_import_private(priv, privSz, &key);
22770     }
22771     printf(resultFmt, ret == 0 ? passed : failed);
22772     wc_curve25519_free(&key);
22773     wc_FreeRng(&rng);
22774 #endif
22775     return ret;
22776 
22777 } /*END test_wc_curve25519_import*/
22778 /*
22779  * Testing test_wc_curve25519_export_private_raw_ex
22780  */
22781 static int test_wc_curve25519_export_private_raw_ex (void)
22782 {
22783 
22784     int ret = 0;
22785 #if defined(HAVE_CURVE25519)
22786     curve25519_key  key;
22787     byte            out[CURVE25519_KEYSIZE];
22788     word32          outLen = sizeof(out);
22789     int             endian = EC25519_BIG_ENDIAN;
22790 
22791     printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
22792 
22793     ret = wc_curve25519_init(&key);
22794     if (ret == 0) {
22795         ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
22796     }
22797     /*test bad cases*/
22798     if (ret == 0) {
22799         ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
22800         if (ret == BAD_FUNC_ARG) {
22801             ret = 0;
22802         }
22803     }
22804     if (ret == 0) {
22805         ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
22806         if (ret == BAD_FUNC_ARG) {
22807             ret = 0;
22808         }
22809     }
22810     if (ret == 0) {
22811         ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
22812         if (ret == BAD_FUNC_ARG) {
22813             ret = 0;
22814         }
22815     }
22816     if (ret == 0) {
22817         ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
22818         if (ret == BAD_FUNC_ARG) {
22819             ret = 0;
22820         }
22821     }
22822     if (ret == 0) {
22823         ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
22824                                                  EC25519_LITTLE_ENDIAN);
22825     }
22826     outLen = outLen - 2;
22827     if (ret == 0) {
22828         ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
22829         if (ret == ECC_BAD_ARG_E) {
22830             ret = 0;
22831         }
22832     }
22833 
22834     printf(resultFmt, ret == 0 ? passed : failed);
22835     wc_curve25519_free(&key);
22836 #endif
22837     return ret;
22838 
22839 }/*END test_wc_curve25519_export_private_raw_ex*/
22840 
22841 /*
22842  * Testing wc_ed448_make_key().
22843  */
22844 static int test_wc_ed448_make_key (void)
22845 {
22846     int ret = 0;
22847 
22848 #if defined(HAVE_ED448)
22849     ed448_key     key;
22850     WC_RNG        rng;
22851 
22852     ret = wc_InitRng(&rng);
22853     if (ret == 0) {
22854         ret = wc_ed448_init(&key);
22855     }
22856     printf(testingFmt, "wc_ed448_make_key()");
22857     if (ret == 0) {
22858         ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
22859     }
22860     /* Test bad args. */
22861     if (ret == 0) {
22862         ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
22863         if (ret == BAD_FUNC_ARG) {
22864             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
22865         }
22866         if (ret == BAD_FUNC_ARG) {
22867             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
22868         }
22869         if (ret == BAD_FUNC_ARG) {
22870             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
22871         }
22872         if (ret == BAD_FUNC_ARG) {
22873             ret = 0;
22874         } else if (ret == 0) {
22875             ret = WOLFSSL_FATAL_ERROR;
22876         }
22877     }
22878 
22879 
22880     printf(resultFmt, ret == 0 ? passed : failed);
22881 
22882     if (wc_FreeRng(&rng) && ret == 0) {
22883         ret = WOLFSSL_FATAL_ERROR;
22884     }
22885     wc_ed448_free(&key);
22886 
22887 #endif
22888     return ret;
22889 
22890 } /* END test_wc_ed448_make_key */
22891 
22892 
22893 /*
22894  * Testing wc_ed448_init()
22895  */
22896 static int test_wc_ed448_init (void)
22897 {
22898     int             ret = 0;
22899 
22900 #if defined(HAVE_ED448)
22901 
22902     ed448_key    key;
22903 
22904     printf(testingFmt, "wc_ed448_init()");
22905 
22906     ret = wc_ed448_init(&key);
22907 
22908     /* Test bad args. */
22909     if (ret == 0) {
22910         ret = wc_ed448_init(NULL);
22911         if (ret == BAD_FUNC_ARG) {
22912             ret = 0;
22913         } else if (ret == 0) {
22914             ret = WOLFSSL_FATAL_ERROR;
22915         }
22916     }
22917 
22918     printf(resultFmt, ret == 0 ? passed : failed);
22919 
22920     wc_ed448_free(&key);
22921 
22922 #endif
22923     return ret;
22924 
22925 } /* END test_wc_ed448_init */
22926 
22927 /*
22928  * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
22929  */
22930 static int test_wc_ed448_sign_msg (void)
22931 {
22932     int           ret = 0;
22933 
22934 #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
22935     WC_RNG        rng;
22936     ed448_key     key;
22937     byte          msg[] = "Everybody gets Friday off.\n";
22938     byte          sig[ED448_SIG_SIZE];
22939     word32        msglen = sizeof(msg);
22940     word32        siglen = sizeof(sig);
22941     word32        badSigLen = sizeof(sig) - 1;
22942 #ifdef HAVE_ED448_VERIFY
22943     int           verify_ok = 0; /*1 = Verify success.*/
22944 #endif
22945 
22946     /* Initialize stack variables. */
22947     XMEMSET(sig, 0, siglen);
22948 
22949     /* Initialize key. */
22950     ret = wc_InitRng(&rng);
22951     if (ret == 0) {
22952         ret = wc_ed448_init(&key);
22953         if (ret == 0) {
22954             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
22955         }
22956     }
22957 
22958     printf(testingFmt, "wc_ed448_sign_msg()");
22959 
22960     if (ret == 0) {
22961         ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
22962     }
22963     /* Test bad args. */
22964     if (ret == 0 && siglen == ED448_SIG_SIZE) {
22965         ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
22966         if (ret == BAD_FUNC_ARG) {
22967             ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
22968         }
22969         if (ret == BAD_FUNC_ARG) {
22970             ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
22971         }
22972         if (ret == BAD_FUNC_ARG) {
22973             ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
22974         }
22975         if (ret == BAD_FUNC_ARG) {
22976             ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
22977                                     NULL, 0);
22978         }
22979         if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
22980             badSigLen -= 1;
22981             ret = 0;
22982         } else if (ret == 0) {
22983             ret = WOLFSSL_FATAL_ERROR;
22984         }
22985     } /* END sign */
22986 
22987     printf(resultFmt, ret == 0 ? passed : failed);
22988     #ifdef HAVE_ED448_VERIFY
22989         printf(testingFmt, "wc_ed448_verify_msg()");
22990 
22991         if (ret == 0) {
22992 
22993             ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
22994                                       &key, NULL, 0);
22995             if (ret == 0  && verify_ok == 1) {
22996                 ret = 0;
22997             } else if (ret == 0) {
22998                 ret = WOLFSSL_FATAL_ERROR;
22999             }
23000 
23001             /* Test bad args. */
23002             if (ret == 0) {
23003                 AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
23004                                                   msglen, &verify_ok, &key,
23005                                                   NULL, 0),
23006                                                   BAD_FUNC_ARG);
23007                 AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
23008                                                   msglen, &verify_ok, &key,
23009                                                   NULL, 0),
23010                                                   BAD_FUNC_ARG);
23011 
23012                 ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
23013                                           &key, NULL, 0);
23014                 if (ret == BAD_FUNC_ARG) {
23015                     ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
23016                                               &verify_ok, &key, NULL, 0);
23017                 }
23018                 if (ret == BAD_FUNC_ARG) {
23019                     ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
23020                                               NULL, &key, NULL, 0);
23021                 }
23022                 if (ret == BAD_FUNC_ARG) {
23023                     ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
23024                                               &verify_ok, NULL, NULL, 0);
23025                 }
23026                 if (ret == BAD_FUNC_ARG) {
23027                     ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
23028                                               &verify_ok, &key, NULL, 0);
23029                 }
23030                 if (ret == BAD_FUNC_ARG) {
23031                     ret = 0;
23032                 } else if (ret == 0) {
23033                     ret = WOLFSSL_FATAL_ERROR;
23034                 }
23035             }
23036 
23037         } /* END verify. */
23038 
23039         printf(resultFmt, ret == 0 ? passed : failed);
23040     #endif /* Verify. */
23041 
23042     if (wc_FreeRng(&rng) && ret == 0) {
23043         ret = WOLFSSL_FATAL_ERROR;
23044     }
23045     wc_ed448_free(&key);
23046 
23047 #endif
23048     return ret;
23049 
23050 } /* END test_wc_ed448_sign_msg */
23051 
23052 /*
23053  * Testing wc_ed448_import_public()
23054  */
23055 static int test_wc_ed448_import_public (void)
23056 {
23057     int             ret = 0;
23058 
23059 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
23060     WC_RNG          rng;
23061     ed448_key     pubKey;
23062     const byte      in[] =
23063                     "Ed448PublicKeyUnitTest.................................\n";
23064     word32          inlen = sizeof(in);
23065 
23066 
23067     ret = wc_InitRng(&rng);
23068     if (ret == 0) {
23069         ret = wc_ed448_init(&pubKey);
23070         if (ret == 0) {
23071             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
23072         }
23073     }
23074     printf(testingFmt, "wc_ed448_import_public()");
23075 
23076     if (ret == 0) {
23077         ret = wc_ed448_import_public(in, inlen, &pubKey);
23078 
23079         if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
23080             ret = 0;
23081         } else {
23082             ret = WOLFSSL_FATAL_ERROR;
23083         }
23084 
23085         /* Test bad args. */
23086         if (ret == 0) {
23087             ret = wc_ed448_import_public(NULL, inlen, &pubKey);
23088             if (ret == BAD_FUNC_ARG) {
23089                 ret = wc_ed448_import_public(in, inlen, NULL);
23090             }
23091             if (ret == BAD_FUNC_ARG) {
23092                 ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
23093             }
23094             if (ret == BAD_FUNC_ARG) {
23095                 ret = 0;
23096             } else if (ret == 0) {
23097                 ret = WOLFSSL_FATAL_ERROR;
23098             }
23099         }
23100     }
23101 
23102     printf(resultFmt, ret == 0 ? passed : failed);
23103 
23104     if (wc_FreeRng(&rng) && ret == 0) {
23105         ret = WOLFSSL_FATAL_ERROR;
23106     }
23107     wc_ed448_free(&pubKey);
23108 
23109 #endif
23110     return ret;
23111 
23112 } /* END wc_ed448_import_public */
23113 
23114 /*
23115  * Testing wc_ed448_import_private_key()
23116  */
23117 static int test_wc_ed448_import_private_key (void)
23118 {
23119     int         ret = 0;
23120 
23121 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
23122     WC_RNG      rng;
23123     ed448_key   key;
23124     const byte  privKey[] =
23125                     "Ed448PrivateKeyUnitTest................................\n";
23126     const byte  pubKey[] =
23127                     "Ed448PublicKeyUnitTest.................................\n";
23128     word32      privKeySz = sizeof(privKey);
23129     word32      pubKeySz = sizeof(pubKey);
23130 #ifdef HAVE_ED448_KEY_EXPORT
23131     byte        bothKeys[sizeof(privKey) + sizeof(pubKey)];
23132     word32      bothKeysSz = sizeof(bothKeys);
23133 #endif
23134 
23135     ret = wc_InitRng(&rng);
23136     if (ret != 0) {
23137         return ret;
23138     }
23139     ret = wc_ed448_init(&key);
23140     if (ret != 0) {
23141         wc_FreeRng(&rng);
23142         return ret;
23143     }
23144     ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
23145 
23146     printf(testingFmt, "wc_ed448_import_private_key()");
23147 
23148     if (ret == 0) {
23149         ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
23150                                                                           &key);
23151         if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
23152                                       XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
23153             ret = WOLFSSL_FATAL_ERROR;
23154         }
23155     }
23156 
23157 #ifdef HAVE_ED448_KEY_EXPORT
23158     if (ret == 0)
23159         ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
23160 
23161     if (ret == 0) {
23162         ret = wc_ed448_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
23163         if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
23164                                       XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
23165             ret = WOLFSSL_FATAL_ERROR;
23166         }
23167     }
23168 #endif
23169 
23170     /* Test bad args. */
23171     if (ret == 0) {
23172         ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
23173                                                                           &key);
23174         if (ret == BAD_FUNC_ARG) {
23175             ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
23176                                                                 pubKeySz, &key);
23177         }
23178         if (ret == BAD_FUNC_ARG) {
23179             ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
23180                                                                 pubKeySz, NULL);
23181         }
23182         if (ret == BAD_FUNC_ARG) {
23183             ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
23184                                                                 pubKeySz, &key);
23185         }
23186         if (ret == BAD_FUNC_ARG) {
23187             ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
23188                                                             pubKeySz - 1, &key);
23189         }
23190         if (ret == BAD_FUNC_ARG) {
23191             ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
23192                                                             0, &key);
23193         }
23194 
23195         if (ret == BAD_FUNC_ARG) {
23196             ret = 0;
23197         } else if (ret == 0) {
23198             ret = WOLFSSL_FATAL_ERROR;
23199         }
23200     }
23201 
23202     printf(resultFmt, ret == 0 ? passed : failed);
23203 
23204     if (wc_FreeRng(&rng) && ret == 0) {
23205         ret = WOLFSSL_FATAL_ERROR;
23206     }
23207     wc_ed448_free(&key);
23208 
23209 #endif
23210     return ret;
23211 
23212 } /* END test_wc_ed448_import_private_key */
23213 
23214 /*
23215  * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
23216  */
23217 static int test_wc_ed448_export (void)
23218 {
23219     int             ret = 0;
23220 
23221 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
23222     WC_RNG          rng;
23223     ed448_key       key;
23224     byte            priv[ED448_PRV_KEY_SIZE];
23225     byte            pub[ED448_PUB_KEY_SIZE];
23226     word32          privSz = sizeof(priv);
23227     word32          pubSz = sizeof(pub);
23228 
23229     ret = wc_InitRng(&rng);
23230     if (ret != 0) {
23231         return ret;
23232     }
23233 
23234     ret = wc_ed448_init(&key);
23235     if (ret != 0) {
23236         wc_FreeRng(&rng);
23237         return ret;
23238     }
23239 
23240     if (ret == 0) {
23241         ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
23242     }
23243 
23244     printf(testingFmt, "wc_ed448_export_public()");
23245 
23246     if (ret == 0) {
23247         ret = wc_ed448_export_public(&key, pub, &pubSz);
23248         if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
23249                                              XMEMCMP(key.p, pub, pubSz) != 0)) {
23250             ret = WOLFSSL_FATAL_ERROR;
23251         }
23252         if (ret == 0) {
23253             ret = wc_ed448_export_public(NULL, pub, &pubSz);
23254             if (ret == BAD_FUNC_ARG) {
23255                 ret = wc_ed448_export_public(&key, NULL, &pubSz);
23256             }
23257             if (ret == BAD_FUNC_ARG) {
23258                 ret = wc_ed448_export_public(&key, pub, NULL);
23259             }
23260             if (ret == BAD_FUNC_ARG) {
23261                 ret = 0;
23262             } else if (ret == 0) {
23263                 ret = WOLFSSL_FATAL_ERROR;
23264             }
23265         }
23266     }
23267 
23268     printf(resultFmt, ret == 0 ? passed : failed);
23269     printf(testingFmt, "wc_ed448_export_private_only()");
23270 
23271     if (ret == 0) {
23272         ret = wc_ed448_export_private_only(&key, priv, &privSz);
23273         if (ret == 0 && (privSz != ED448_KEY_SIZE ||
23274                                            XMEMCMP(key.k, priv, privSz) != 0)) {
23275             ret = WOLFSSL_FATAL_ERROR;
23276         }
23277         if (ret == 0) {
23278             ret = wc_ed448_export_private_only(NULL, priv, &privSz);
23279             if (ret == BAD_FUNC_ARG) {
23280                 ret = wc_ed448_export_private_only(&key, NULL, &privSz);
23281             }
23282             if (ret == BAD_FUNC_ARG) {
23283                 ret = wc_ed448_export_private_only(&key, priv, NULL);
23284             }
23285             if (ret == BAD_FUNC_ARG) {
23286                 ret = 0;
23287             } else if (ret == 0) {
23288                 ret = WOLFSSL_FATAL_ERROR;
23289             }
23290         }
23291     }
23292 
23293     printf(resultFmt, ret == 0 ? passed : failed);
23294 
23295     if (wc_FreeRng(&rng) && ret == 0) {
23296         ret = WOLFSSL_FATAL_ERROR;
23297     }
23298     wc_ed448_free(&key);
23299 
23300 #endif
23301     return ret;
23302 
23303 } /* END test_wc_ed448_export */
23304 
23305 /*
23306  *  Testing wc_ed448_size()
23307  */
23308 static int test_wc_ed448_size (void)
23309 {
23310     int             ret = 0;
23311 #if defined(HAVE_ED448)
23312     WC_RNG          rng;
23313     ed448_key       key;
23314 
23315     ret = wc_InitRng(&rng);
23316     if (ret != 0) {
23317         return ret;
23318     }
23319     ret = wc_ed448_init(&key);
23320     if (ret != 0) {
23321         wc_FreeRng(&rng);
23322         return ret;
23323     }
23324 
23325     ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
23326     if (ret != 0) {
23327         wc_FreeRng(&rng);
23328         wc_ed448_free(&key);
23329         return ret;
23330     }
23331 
23332     printf(testingFmt, "wc_ed448_size()");
23333     ret = wc_ed448_size(&key);
23334     /* Test bad args. */
23335     if (ret == ED448_KEY_SIZE) {
23336         ret = wc_ed448_size(NULL);
23337         if (ret == BAD_FUNC_ARG) {
23338             ret = 0;
23339         }
23340     }
23341     printf(resultFmt, ret == 0 ? passed : failed);
23342 
23343     if (ret == 0) {
23344         printf(testingFmt, "wc_ed448_sig_size()");
23345 
23346         ret = wc_ed448_sig_size(&key);
23347         if (ret == ED448_SIG_SIZE) {
23348             ret = 0;
23349         }
23350         /* Test bad args. */
23351         if (ret == 0) {
23352             ret = wc_ed448_sig_size(NULL);
23353             if (ret == BAD_FUNC_ARG) {
23354                 ret = 0;
23355             }
23356         }
23357 
23358         printf(resultFmt, ret == 0 ? passed : failed);
23359     } /* END wc_ed448_sig_size() */
23360 
23361     if (ret == 0) {
23362         printf(testingFmt, "wc_ed448_pub_size");
23363         ret = wc_ed448_pub_size(&key);
23364         if (ret == ED448_PUB_KEY_SIZE) {
23365             ret = 0;
23366         }
23367         if (ret == 0) {
23368             ret = wc_ed448_pub_size(NULL);
23369             if (ret == BAD_FUNC_ARG) {
23370                 ret = 0;
23371             }
23372         }
23373         printf(resultFmt, ret == 0 ? passed : failed);
23374     } /* END wc_ed448_pub_size */
23375 
23376     if (ret == 0) {
23377         printf(testingFmt, "wc_ed448_priv_size");
23378         ret = wc_ed448_priv_size(&key);
23379         if (ret == ED448_PRV_KEY_SIZE) {
23380             ret = 0;
23381         }
23382         if (ret == 0) {
23383             ret = wc_ed448_priv_size(NULL);
23384             if (ret == BAD_FUNC_ARG) {
23385                 ret = 0;
23386             }
23387         }
23388         printf(resultFmt, ret == 0 ? passed : failed);
23389     } /* END wc_ed448_pub_size */
23390 
23391     if (wc_FreeRng(&rng) && ret == 0) {
23392         ret = WOLFSSL_FATAL_ERROR;
23393     }
23394     wc_ed448_free(&key);
23395 
23396 #endif
23397     return ret;
23398 
23399 } /* END test_wc_ed448_size */
23400 
23401 /*
23402  * Testing wc_ed448_export_private() and wc_ed448_export_key()
23403  */
23404 static int test_wc_ed448_exportKey (void)
23405 {
23406     int             ret = 0;
23407 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
23408     WC_RNG          rng;
23409     ed448_key       key;
23410     byte            priv[ED448_PRV_KEY_SIZE];
23411     byte            pub[ED448_PUB_KEY_SIZE];
23412     byte            privOnly[ED448_PRV_KEY_SIZE];
23413     word32          privSz      = sizeof(priv);
23414     word32          pubSz       = sizeof(pub);
23415     word32          privOnlySz  = sizeof(privOnly);
23416 
23417     ret = wc_InitRng(&rng);
23418     if (ret != 0) {
23419         return ret;
23420     }
23421     ret = wc_ed448_init(&key);
23422     if (ret != 0) {
23423         wc_FreeRng(&rng);
23424         return ret;
23425     }
23426 
23427     ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
23428     if (ret != 0) {
23429         wc_FreeRng(&rng);
23430         wc_ed448_free(&key);
23431         return ret;
23432     }
23433 
23434     printf(testingFmt, "wc_ed448_export_private()");
23435 
23436     ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
23437     if (ret == 0) {
23438         ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
23439         if (ret == BAD_FUNC_ARG) {
23440             ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
23441         }
23442         if (ret == BAD_FUNC_ARG) {
23443             ret = wc_ed448_export_private(&key, privOnly, NULL);
23444         }
23445         if (ret == BAD_FUNC_ARG) {
23446             ret = 0;
23447         } else if (ret == 0) {
23448             ret = WOLFSSL_FATAL_ERROR;
23449         }
23450     }
23451 
23452     printf(resultFmt, ret == 0 ? passed : failed);
23453 
23454     if (ret == 0) {
23455         printf(testingFmt, "wc_ed448_export_key()");
23456 
23457         ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
23458         if (ret == 0) {
23459             ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
23460             if (ret == BAD_FUNC_ARG) {
23461                 ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
23462             }
23463             if (ret == BAD_FUNC_ARG) {
23464                 ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
23465             }
23466             if (ret == BAD_FUNC_ARG) {
23467                 ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
23468             }
23469             if (ret == BAD_FUNC_ARG) {
23470                 ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
23471             }
23472             if (ret == BAD_FUNC_ARG) {
23473                 ret = 0;
23474             } else if (ret == 0) {
23475                 ret = WOLFSSL_FATAL_ERROR;
23476             }
23477         }
23478         printf(resultFmt, ret == 0 ? passed : failed);
23479     } /* END wc_ed448_export_key() */
23480 
23481     /* Cross check output. */
23482     if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
23483         ret = WOLFSSL_FATAL_ERROR;
23484     }
23485 
23486     if (wc_FreeRng(&rng) && ret == 0) {
23487         ret = WOLFSSL_FATAL_ERROR;
23488     }
23489     wc_ed448_free(&key);
23490 
23491 #endif
23492     return ret;
23493 
23494 } /* END test_wc_ed448_exportKey */
23495 
23496 /*
23497  * Testing wc_Ed448PublicKeyToDer
23498  */
23499 static int test_wc_Ed448PublicKeyToDer (void)
23500 {
23501     int ret = 0;
23502 
23503 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
23504     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
23505     int       tmp;
23506     ed448_key key;
23507     byte      derBuf[1024];
23508 
23509     printf(testingFmt, "wc_Ed448PublicKeyToDer()");
23510 
23511     /* Test bad args */
23512     tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
23513     if (tmp != BAD_FUNC_ARG) {
23514         ret = WOLFSSL_FATAL_ERROR;
23515     }
23516 
23517     if (ret == 0) {
23518         wc_ed448_init(&key);
23519         tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
23520         if (tmp != BUFFER_E) {
23521             ret = WOLFSSL_FATAL_ERROR;
23522         }
23523         wc_ed448_free(&key);
23524     }
23525 
23526     /*  Test good args */
23527     if (ret == 0) {
23528         WC_RNG          rng;
23529         ret = wc_InitRng(&rng);
23530         if (ret != 0) {
23531             return ret;
23532         }
23533         ret = wc_ed448_init(&key);
23534         if (ret != 0) {
23535             wc_FreeRng(&rng);
23536             return ret;
23537         }
23538 
23539         ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
23540         if (ret != 0) {
23541             wc_FreeRng(&rng);
23542             wc_ed448_free(&key);
23543             return ret;
23544         }
23545 
23546         tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
23547         if (tmp <= 0) {
23548             ret = WOLFSSL_FATAL_ERROR;
23549         }
23550 
23551         wc_FreeRng(&rng);
23552         wc_ed448_free(&key);
23553     }
23554     printf(resultFmt, ret == 0 ? passed : failed);
23555 #endif
23556     return ret;
23557 
23558 } /* END testing wc_Ed448PublicKeyToDer */
23559 
23560 /*
23561  * Testing wc_curve448_init and wc_curve448_free.
23562  */
23563 static int test_wc_curve448_init (void)
23564 {
23565     int ret = 0;
23566 
23567 #if defined(HAVE_CURVE448)
23568 
23569     curve448_key  key;
23570 
23571     printf(testingFmt, "wc_curve448_init()");
23572     ret = wc_curve448_init(&key);
23573 
23574     /* Test bad args for wc_curve448_init */
23575     if (ret == 0) {
23576         ret = wc_curve448_init(NULL);
23577         if (ret == BAD_FUNC_ARG) {
23578             ret = 0;
23579         } else if (ret == 0) {
23580             ret = WOLFSSL_FATAL_ERROR;
23581         }
23582     }
23583 
23584     printf(resultFmt, ret == 0 ? passed : failed);
23585     /*  Test good args for wc_curve_448_free */
23586     wc_curve448_free(&key);
23587 
23588     wc_curve448_free(NULL);
23589 
23590 #endif
23591     return ret;
23592 
23593 } /* END test_wc_curve448_init and wc_curve_448_free*/
23594 
23595 /*
23596  * Testing wc_curve448_make_key
23597  */
23598 static int test_wc_curve448_make_key (void)
23599 {
23600     int ret = 0;
23601 #if defined(HAVE_CURVE448)
23602     WC_RNG           rng;
23603     curve448_key  key;
23604     int           keysize;
23605 
23606 
23607     printf(testingFmt, "wc_curve448_make_key()");
23608 
23609     ret = wc_curve448_init(&key);
23610     if (ret == 0) {
23611         ret = wc_InitRng(&rng);
23612     }
23613     if (ret == 0) {
23614 
23615         ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
23616         if (ret == 0) {
23617             keysize = wc_curve448_size(&key);
23618             if (keysize != CURVE448_KEY_SIZE) {
23619                 ret = WOLFSSL_FATAL_ERROR;
23620             }
23621         }
23622         if (ret == 0) {
23623             ret = wc_curve448_make_key(&rng, keysize, &key);
23624         }
23625     }
23626     /*test bad cases*/
23627     if (ret == 0) {
23628         ret = wc_curve448_make_key(NULL, 0, NULL);
23629         if (ret == BAD_FUNC_ARG) {
23630             ret = 0;
23631         }
23632     }
23633     if (ret == 0) {
23634         ret = wc_curve448_make_key(&rng, keysize, NULL);
23635         if (ret == BAD_FUNC_ARG) {
23636             ret = 0;
23637         }
23638     }
23639     if (ret == 0) {
23640         ret = wc_curve448_make_key(NULL, keysize, &key);
23641         if (ret == BAD_FUNC_ARG) {
23642             ret = 0;
23643         }
23644     }
23645     if (ret == 0) {
23646         ret = wc_curve448_make_key(&rng, 0, &key);
23647         if (ret == ECC_BAD_ARG_E) {
23648             ret = 0;
23649         }
23650     }
23651 
23652     if (wc_FreeRng(&rng) != 0 && ret == 0) {
23653         ret = WOLFSSL_FATAL_ERROR;
23654     }
23655 
23656     printf(resultFmt, ret == 0 ? passed : failed);
23657     wc_curve448_free(&key);
23658 #endif
23659     return ret;
23660 } /*END test_wc_curve448_make_key*/
23661 /*
23662  * Testing test_wc_curve448_shared_secret_ex
23663  */
23664 static int test_wc_curve448_shared_secret_ex (void)
23665 {
23666     int ret = 0;
23667 #if defined(HAVE_CURVE448)
23668     WC_RNG        rng;
23669     curve448_key  private_key, public_key;
23670     byte          out[CURVE448_KEY_SIZE];
23671     word32        outLen = sizeof(out);
23672     int           endian = EC448_BIG_ENDIAN;
23673 
23674     printf(testingFmt, "wc_curve448_shared_secret_ex()");
23675 
23676     ret = wc_curve448_init(&private_key);
23677     if (ret == 0) {
23678         ret = wc_InitRng(&rng);
23679         if (ret == 0) {
23680             ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
23681         }
23682     }
23683     if (ret == 0) {
23684         ret = wc_curve448_init(&public_key);
23685     }
23686     if (ret == 0) {
23687         if (ret == 0) {
23688             ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
23689         }
23690     }
23691 
23692     if (ret == 0) {
23693         ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
23694                                             &outLen, endian);
23695     }
23696     /*test bad cases*/
23697     if (ret == 0) {
23698         ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
23699                                             0, endian);
23700         if (ret == BAD_FUNC_ARG) {
23701             ret = 0;
23702         }
23703     }
23704     if (ret == 0) {
23705         ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
23706                                             &outLen, endian);
23707         if (ret == BAD_FUNC_ARG) {
23708             ret = 0;
23709         }
23710     }
23711     if (ret == 0) {
23712         ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
23713                                             &outLen, endian);
23714         if (ret == BAD_FUNC_ARG) {
23715             ret = 0;
23716         }
23717     }
23718     if (ret == 0) {
23719         ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
23720                                             &outLen, endian);
23721         if (ret == BAD_FUNC_ARG) {
23722             ret = 0;
23723         }
23724     }
23725         if (ret == 0) {
23726         ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
23727                                             NULL, endian);
23728         if (ret == BAD_FUNC_ARG) {
23729             ret = 0;
23730         }
23731     }
23732     outLen = outLen - 2;
23733     if (ret == 0) {
23734         ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
23735                                             &outLen, endian);
23736         if (ret == BAD_FUNC_ARG) {
23737             ret = 0;
23738         }
23739     }
23740 
23741     printf(resultFmt, ret == 0 ? passed : failed);
23742     wc_curve448_free(&private_key);
23743     wc_curve448_free(&public_key);
23744     wc_FreeRng(&rng);
23745 #endif
23746     return ret;
23747 } /*END test_wc_curve448_shared_secret_ex*/
23748 /*
23749  * Testing test_wc_curve448_export_public_ex
23750  */
23751 static int test_wc_curve448_export_public_ex (void)
23752 {
23753     int ret = 0;
23754 #if defined(HAVE_CURVE448)
23755 
23756     WC_RNG        rng;
23757     curve448_key  key;
23758     byte          out[CURVE448_KEY_SIZE];
23759     word32        outLen = sizeof(out);
23760     int           endian = EC448_BIG_ENDIAN;
23761 
23762     printf(testingFmt, "wc_curve448_export_public_ex()");
23763 
23764     ret = wc_curve448_init(&key);
23765     if (ret == 0) {
23766         ret = wc_InitRng(&rng);
23767     }
23768     if (ret == 0) {
23769 
23770         ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
23771         if (ret == 0){
23772             ret = wc_curve448_export_public(&key, out, &outLen);
23773             }
23774         if (ret == 0) {
23775             ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
23776         }
23777     }
23778     /*test bad cases*/
23779     if (ret == 0) {
23780         ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
23781         if (ret == BAD_FUNC_ARG) {
23782             ret = 0;
23783         }
23784     }
23785     if (ret == 0) {
23786         ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
23787         if (ret == BAD_FUNC_ARG) {
23788             ret = 0;
23789         }
23790     }
23791     if (ret == 0) {
23792         ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
23793         if (ret == BAD_FUNC_ARG) {
23794             ret = 0;
23795         }
23796     }
23797     if (ret == 0) {
23798         ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
23799         if (ret == BAD_FUNC_ARG) {
23800             ret = 0;
23801         }
23802     }
23803     outLen = outLen - 2;
23804     if (ret == 0) {
23805         ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
23806         if (ret == ECC_BAD_ARG_E) {
23807             ret = 0;
23808         }
23809     }
23810 
23811     printf(resultFmt, ret == 0 ? passed : failed);
23812     wc_curve448_free(&key);
23813     wc_FreeRng(&rng);
23814 #endif
23815     return ret;
23816 
23817 } /*END test_wc_curve448_export_public_ex*/
23818 /*
23819  * Testing test_wc_curve448_export_private_raw_ex
23820  */
23821 static int test_wc_curve448_export_private_raw_ex (void)
23822 {
23823 
23824     int ret = 0;
23825 #if defined(HAVE_CURVE448)
23826     curve448_key  key;
23827     byte          out[CURVE448_KEY_SIZE];
23828     word32        outLen = sizeof(out);
23829     int           endian = EC448_BIG_ENDIAN;
23830 
23831     printf(testingFmt, "wc_curve448_export_private_raw_ex()");
23832 
23833     ret = wc_curve448_init(&key);
23834     if (ret == 0) {
23835         ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
23836     }
23837     /*test bad cases*/
23838     if (ret == 0) {
23839         ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
23840         if (ret == BAD_FUNC_ARG) {
23841             ret = 0;
23842         }
23843     }
23844     if (ret == 0) {
23845         ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
23846         if (ret == BAD_FUNC_ARG) {
23847             ret = 0;
23848         }
23849     }
23850     if (ret == 0) {
23851         ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
23852         if (ret == BAD_FUNC_ARG) {
23853             ret = 0;
23854         }
23855     }
23856     if (ret == 0) {
23857         ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
23858         if (ret == BAD_FUNC_ARG) {
23859             ret = 0;
23860         }
23861     }
23862     if (ret == 0) {
23863         ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
23864                                                  EC448_LITTLE_ENDIAN);
23865     }
23866     outLen = outLen - 2;
23867     if (ret == 0) {
23868         ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
23869         if (ret == ECC_BAD_ARG_E) {
23870             ret = 0;
23871         }
23872     }
23873 
23874     printf(resultFmt, ret == 0 ? passed : failed);
23875     wc_curve448_free(&key);
23876 #endif
23877     return ret;
23878 
23879 }/*END test_wc_curve448_export_private_raw_ex*/
23880 /*
23881  * Testing test_wc_curve448_import_private_raw_ex
23882  */
23883 static int test_wc_curve448_import_private_raw_ex (void)
23884 {
23885     int ret = 0;
23886 #if defined(HAVE_CURVE448)
23887     WC_RNG             rng;
23888     curve448_key    key;
23889     byte            priv[CURVE448_KEY_SIZE];
23890     byte            pub[CURVE448_KEY_SIZE];
23891     word32          privSz = sizeof(priv);
23892     word32          pubSz = sizeof(pub);
23893     int             endian = EC448_BIG_ENDIAN;
23894 
23895     printf(testingFmt, "wc_curve448_import_private_raw_ex()");
23896 
23897     ret = wc_curve448_init(&key);
23898     if (ret == 0) {
23899         ret = wc_InitRng(&rng);
23900     }
23901     if (ret == 0) {
23902 
23903         ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
23904         if (ret == 0){
23905             ret = wc_curve448_export_private_raw(&key, priv, &privSz);
23906         }
23907         if (ret == 0){
23908             ret = wc_curve448_export_public(&key, pub, &pubSz);
23909             }
23910         if (ret == 0) {
23911             ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
23912                                                      &key, endian);
23913         }
23914     }
23915     /*test bad cases*/
23916     if (ret == 0) {
23917         ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
23918         if (ret == BAD_FUNC_ARG) {
23919             ret = 0;
23920         }
23921     }
23922     if (ret == 0) {
23923         ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
23924                                                  &key, endian);
23925         if (ret == BAD_FUNC_ARG) {
23926             ret = 0;
23927         }
23928     }
23929     if (ret == 0) {
23930         ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
23931                                                  &key, endian);
23932         if (ret == BAD_FUNC_ARG) {
23933             ret = 0;
23934         }
23935     }
23936     if (ret == 0) {
23937         ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
23938                                                  NULL, endian);
23939         if (ret == BAD_FUNC_ARG) {
23940             ret = 0;
23941         }
23942     }
23943     if (ret == 0) {
23944         ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
23945                                                  &key, endian);
23946         if (ret == ECC_BAD_ARG_E) {
23947             ret = 0;
23948         }
23949     }
23950     if (ret == 0) {
23951         ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
23952                                                  &key, endian);
23953         if (ret == ECC_BAD_ARG_E) {
23954             ret = 0;
23955         }
23956     }
23957     if (ret == 0) {
23958         ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
23959                                                  &key, EC448_LITTLE_ENDIAN);
23960 
23961     }
23962 
23963     if (wc_FreeRng(&rng) != 0 && ret == 0) {
23964         ret = WOLFSSL_FATAL_ERROR;
23965     }
23966 
23967     printf(resultFmt, ret == 0 ? passed : failed);
23968     wc_curve448_free(&key);
23969 
23970 #endif
23971     return ret;
23972 } /*END test_wc_curve448_import_private_raw_ex*/
23973 /*
23974  * Testing test_curve448_export_key_raw
23975  */
23976 static int test_wc_curve448_export_key_raw (void)
23977 {
23978     int ret = 0;
23979 #if defined(HAVE_CURVE448)
23980     WC_RNG          rng;
23981     curve448_key    key;
23982     byte            priv[CURVE448_KEY_SIZE];
23983     byte            pub[CURVE448_KEY_SIZE];
23984     word32          privSz = sizeof(priv);
23985     word32          pubSz = sizeof(pub);
23986 
23987     printf(testingFmt, "wc_curve448_export_key_raw()");
23988 
23989     ret = wc_curve448_init(&key);
23990     if (ret == 0) {
23991         ret = wc_InitRng(&rng);
23992     }
23993     if (ret == 0) {
23994 
23995         ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
23996         if (ret == 0) {
23997             ret = wc_curve448_export_private_raw(&key, priv, &privSz);
23998         }
23999         if (ret == 0) {
24000             ret = wc_curve448_export_public(&key, pub, &pubSz);
24001             }
24002         if (ret == 0) {
24003             ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
24004         }
24005     }
24006 
24007     printf(resultFmt, ret == 0 ? passed : failed);
24008     wc_curve448_free(&key);
24009     wc_FreeRng(&rng);
24010 #endif
24011     return ret;
24012 
24013 }/*END test_wc_curve448_import_private_raw_ex*/
24014 
24015 
24016 /*
24017  * Testing test_wc_curve448_import_private
24018  */
24019 static int test_wc_curve448_import_private (void)
24020 {
24021     int ret = 0;
24022 #if defined(HAVE_CURVE448)
24023 
24024     curve448_key       key;
24025     WC_RNG             rng;
24026     byte               priv[CURVE448_KEY_SIZE];
24027     word32             privSz = sizeof(priv);
24028 
24029     printf(testingFmt, "wc_curve448_import_private()");
24030 
24031     ret = wc_curve448_init(&key);
24032     if (ret == 0) {
24033         ret = wc_InitRng(&rng);
24034     }
24035     if (ret == 0) {
24036 
24037         ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
24038         if (ret == 0) {
24039             ret = wc_curve448_export_private_raw(&key, priv, &privSz);
24040         }
24041     }
24042     if (ret == 0) {
24043         ret = wc_curve448_import_private(priv, privSz, &key);
24044     }
24045     printf(resultFmt, ret == 0 ? passed : failed);
24046     wc_curve448_free(&key);
24047     wc_FreeRng(&rng);
24048 #endif
24049     return ret;
24050 
24051 } /*END test_wc_curve448_import*/
24052 /*
24053  * Testing test_wc_curve448_size.
24054  */
24055 static int test_wc_curve448_size (void)
24056 {
24057     int ret = 0;
24058 
24059 #if defined(HAVE_CURVE448)
24060 
24061     curve448_key  key;
24062 
24063     printf(testingFmt, "wc_curve448_size()");
24064 
24065     ret = wc_curve448_init(&key);
24066 
24067     /*  Test good args for wc_curve448_size */
24068     if (ret == 0) {
24069         ret = wc_curve448_size(&key);
24070     }
24071 
24072     /* Test bad args for wc_curve448_size */
24073     if (ret != 0) {
24074         ret = wc_curve448_size(NULL);
24075     }
24076 
24077     printf(resultFmt, ret == 0 ? passed : failed);
24078     wc_curve448_free(&key);
24079 #endif
24080     return ret;
24081 
24082 } /* END test_wc_curve448_size*/
24083 
24084 /*
24085  * Testing wc_ecc_make_key.
24086  */
24087 static int test_wc_ecc_make_key (void)
24088 {
24089     int     ret = 0;
24090 
24091 #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
24092     WC_RNG rng;
24093     ecc_key key;
24094 
24095     printf(testingFmt, "wc_ecc_make_key()");
24096 
24097     ret = wc_InitRng(&rng);
24098     if (ret != 0)
24099         return ret;
24100 
24101     ret = wc_ecc_init(&key);
24102     if (ret == 0) {
24103         ret = wc_ecc_make_key(&rng, KEY14, &key);
24104 
24105         /* Pass in bad args. */
24106         if (ret == 0) {
24107             ret = wc_ecc_make_key(NULL, KEY14, &key);
24108             if (ret == BAD_FUNC_ARG) {
24109                 ret = wc_ecc_make_key(&rng, KEY14, NULL);
24110             }
24111             if (ret == BAD_FUNC_ARG) {
24112                 ret = 0;
24113             } else if (ret == 0) {
24114                 ret = WOLFSSL_FATAL_ERROR;
24115             }
24116         }
24117         wc_ecc_free(&key);
24118     }
24119 
24120     if (wc_FreeRng(&rng) != 0 && ret == 0) {
24121         ret = WOLFSSL_FATAL_ERROR;
24122     }
24123 
24124 #ifdef FP_ECC
24125     wc_ecc_fp_free();
24126 #endif
24127 
24128     printf(resultFmt, ret == 0 ? passed : failed);
24129 #endif
24130 
24131     return ret;
24132 
24133 } /* END test_wc_ecc_make_key */
24134 
24135 
24136 /*
24137  * Testing wc_ecc_init()
24138  */
24139 static int test_wc_ecc_init (void)
24140 {
24141     int         ret = 0;
24142 
24143 #ifdef HAVE_ECC
24144     ecc_key     key;
24145 
24146     printf(testingFmt, "wc_ecc_init()");
24147 
24148     ret = wc_ecc_init(&key);
24149     /* Pass in bad args. */
24150     if (ret == 0) {
24151         ret = wc_ecc_init(NULL);
24152         if (ret == BAD_FUNC_ARG) {
24153             ret = 0;
24154         } else if (ret == 0) {
24155             ret = WOLFSSL_FATAL_ERROR;
24156         }
24157     }
24158 
24159     printf(resultFmt, ret == 0 ? passed : failed);
24160 
24161     wc_ecc_free(&key);
24162 
24163 #endif
24164     return ret;
24165 
24166 } /* END test_wc_ecc_init */
24167 
24168 /*
24169  * Testing wc_ecc_check_key()
24170  */
24171 static int test_wc_ecc_check_key (void)
24172 {
24173     int         ret = 0;
24174 
24175 #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
24176     WC_RNG      rng;
24177     ecc_key     key;
24178 
24179     XMEMSET(&rng, 0, sizeof(rng));
24180     XMEMSET(&key, 0, sizeof(key));
24181 
24182     ret = wc_InitRng(&rng);
24183     if (ret == 0) {
24184         ret = wc_ecc_init(&key);
24185         if (ret == 0) {
24186             ret = wc_ecc_make_key(&rng, KEY14, &key);
24187         }
24188     }
24189 
24190     printf(testingFmt, "wc_ecc_check_key()");
24191 
24192     if (ret == 0) {
24193         ret = wc_ecc_check_key(&key);
24194     }
24195 
24196     /* Pass in bad args. */
24197     if (ret == 0) {
24198         ret = wc_ecc_check_key(NULL);
24199         if (ret == BAD_FUNC_ARG) {
24200             ret = 0;
24201         } else if (ret == 0) {
24202             ret = WOLFSSL_FATAL_ERROR;
24203         }
24204     }
24205 
24206     printf(resultFmt, ret == 0 ? passed : failed);
24207 
24208     if (wc_FreeRng(&rng) && ret == 0) {
24209         ret = WOLFSSL_FATAL_ERROR;
24210     }
24211     wc_ecc_free(&key);
24212 
24213 #ifdef FP_ECC
24214     wc_ecc_fp_free();
24215 #endif
24216 
24217 #endif
24218     return ret;
24219 
24220 } /* END test_wc_ecc_check_key */
24221 
24222 /*
24223  * Testing wc_ecc_get_generator()
24224  */
24225 static int test_wc_ecc_get_generator(void)
24226 {
24227     int         ret = 0;
24228 #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
24229     !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
24230     ecc_point* pt;
24231 
24232     printf(testingFmt, "wc_ecc_new_point()");
24233 
24234     pt = wc_ecc_new_point();
24235     if (!pt) {
24236         ret = WOLFSSL_FATAL_ERROR;
24237     }
24238 
24239     printf(testingFmt, "wc_ecc_get_generator()");
24240 
24241     if (ret == 0) {
24242         ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
24243     }
24244 
24245     /* Test bad args. */
24246     if (ret == MP_OKAY) {
24247         /* Returns Zero for bad arg. */
24248         ret = wc_ecc_get_generator(pt, -1);
24249         if (ret != MP_OKAY)
24250             wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
24251         if (ret != MP_OKAY)
24252             wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
24253                                              * increase this number */
24254         if (ret != MP_OKAY)
24255             wc_ecc_get_generator(NULL, -1);
24256         ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
24257     }
24258 
24259     printf(resultFmt, ret == 0 ? passed : failed);
24260 
24261     wc_ecc_del_point(pt);
24262 #endif
24263     return ret;
24264 } /* END test_wc_ecc_get_generator */
24265 
24266 /*
24267  * Testing wc_ecc_size()
24268  */
24269 static int test_wc_ecc_size (void)
24270 {
24271     int         ret = 0;
24272 
24273 #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
24274     WC_RNG      rng;
24275     ecc_key     key;
24276 
24277     XMEMSET(&rng, 0, sizeof(rng));
24278     XMEMSET(&key, 0, sizeof(key));
24279 
24280     ret = wc_InitRng(&rng);
24281     if (ret == 0) {
24282         ret = wc_ecc_init(&key);
24283         if (ret == 0) {
24284             ret = wc_ecc_make_key(&rng, KEY14, &key);
24285         }
24286     }
24287 
24288     printf(testingFmt, "wc_ecc_size()");
24289 
24290     if (ret == 0) {
24291         ret = wc_ecc_size(&key);
24292         if (ret == KEY14) {
24293             ret = 0;
24294         } else if (ret == 0){
24295             ret = WOLFSSL_FATAL_ERROR;
24296         }
24297     }
24298     /* Test bad args. */
24299     if (ret == 0) {
24300         /* Returns Zero for bad arg. */
24301         ret = wc_ecc_size(NULL);
24302     }
24303 
24304     printf(resultFmt, ret == 0 ? passed : failed);
24305 
24306     if (wc_FreeRng(&rng) && ret == 0) {
24307         ret = WOLFSSL_FATAL_ERROR;
24308     }
24309     wc_ecc_free(&key);
24310 
24311 #endif
24312     return ret;
24313 } /* END test_wc_ecc_size */
24314 
24315 static void test_wc_ecc_params(void)
24316 {
24317     /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
24318         It was added after certifications */
24319 #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
24320     const ecc_set_type* ecc_set;
24321 #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
24322     /* Test for SECP256R1 curve */
24323     int curve_id = ECC_SECP256R1;
24324     int curve_idx = wc_ecc_get_curve_idx(curve_id);
24325     AssertIntNE(curve_idx, ECC_CURVE_INVALID);
24326     ecc_set = wc_ecc_get_curve_params(curve_idx);
24327     AssertNotNull(ecc_set);
24328     AssertIntEQ(ecc_set->id, curve_id);
24329 #endif
24330     /* Test case when SECP256R1 is not enabled */
24331     /* Test that we get curve params for index 0 */
24332     ecc_set = wc_ecc_get_curve_params(0);
24333     AssertNotNull(ecc_set);
24334 #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
24335 }
24336 
24337 /*
24338  * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
24339  */
24340 static int test_wc_ecc_signVerify_hash (void)
24341 {
24342     int         ret = 0;
24343 
24344 #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
24345     WC_RNG      rng;
24346     ecc_key     key;
24347     int         signH = WOLFSSL_FATAL_ERROR;
24348     #ifdef HAVE_ECC_VERIFY
24349         int     verifyH = WOLFSSL_FATAL_ERROR;
24350         int     verify  = 0;
24351     #endif
24352     word32      siglen = ECC_BUFSIZE;
24353     byte        sig[ECC_BUFSIZE];
24354     byte        adjustedSig[ECC_BUFSIZE+1];
24355     byte        digest[] = TEST_STRING;
24356     word32      digestlen = (word32)TEST_STRING_SZ;
24357 
24358     /* Init stack var */
24359     XMEMSET(sig, 0, siglen);
24360     XMEMSET(&key, 0, sizeof(key));
24361     XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
24362 
24363     /* Init structs. */
24364     ret = wc_InitRng(&rng);
24365     if (ret == 0) {
24366         ret = wc_ecc_init(&key);
24367         if (ret == 0) {
24368             ret = wc_ecc_make_key(&rng, KEY14, &key);
24369         }
24370     }
24371 
24372     printf(testingFmt, "wc_ecc_sign_hash()");
24373 
24374     if (ret == 0) {
24375         ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
24376     }
24377 
24378     /* Check bad args. */
24379     if (ret == 0) {
24380         signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
24381         if (signH == ECC_BAD_ARG_E) {
24382             signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
24383                                                                 &rng, &key);
24384         }
24385         if (signH == ECC_BAD_ARG_E) {
24386             signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
24387                                                                 &rng, &key);
24388         }
24389         if (signH == ECC_BAD_ARG_E) {
24390             signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
24391                                                                 NULL, &key);
24392         }
24393         if (signH == ECC_BAD_ARG_E) {
24394             signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
24395                                                                 &rng, NULL);
24396         }
24397         if (signH == ECC_BAD_ARG_E) {
24398             signH = 0;
24399         } else if (ret == 0) {
24400             signH = WOLFSSL_FATAL_ERROR;
24401         }
24402     }
24403 
24404     printf(resultFmt, signH == 0 ? passed : failed);
24405 
24406     #ifdef HAVE_ECC_VERIFY
24407         printf(testingFmt, "wc_ecc_verify_hash()");
24408 
24409         ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
24410         if (verify != 1 && ret == 0) {
24411             ret = WOLFSSL_FATAL_ERROR;
24412         }
24413 
24414         /* test check on length of signature passed in */
24415         XMEMCPY(adjustedSig, sig, siglen);
24416         adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
24417 #ifndef NO_STRICT_ECDSA_LEN
24418         AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
24419                     &verify, &key), 0);
24420 #else
24421         /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
24422          * is allowed */
24423         AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
24424                     &verify, &key), 0);
24425 #endif
24426 
24427         /* Test bad args. */
24428         if (ret == 0) {
24429             verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
24430                                                             &verify, &key);
24431             if (verifyH == ECC_BAD_ARG_E) {
24432                 verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
24433                                                             &verify, &key);
24434             }
24435             if (verifyH == ECC_BAD_ARG_E) {
24436                 verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
24437                                                                 NULL, &key);
24438             }
24439             if (verifyH == ECC_BAD_ARG_E) {
24440                 verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
24441                                                             &verify, NULL);
24442             }
24443             if (verifyH == ECC_BAD_ARG_E) {
24444                 verifyH = 0;
24445             } else if (ret == 0) {
24446                 verifyH = WOLFSSL_FATAL_ERROR;
24447             }
24448         }
24449 
24450         printf(resultFmt, verifyH == 0 ? passed : failed);
24451 
24452     #endif /* HAVE_ECC_VERIFY */
24453 
24454         if (wc_FreeRng(&rng) && ret == 0) {
24455             ret = WOLFSSL_FATAL_ERROR;
24456         }
24457         wc_ecc_free(&key);
24458 
24459     #ifdef FP_ECC
24460         wc_ecc_fp_free();
24461     #endif
24462 
24463 #endif
24464     return ret;
24465 
24466 } /*  END test_wc_ecc_sign_hash */
24467 
24468 
24469 /*
24470  * Testing wc_ecc_shared_secret()
24471  */
24472 static int test_wc_ecc_shared_secret (void)
24473 {
24474     int         ret = 0;
24475 
24476 #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
24477     ecc_key     key, pubKey;
24478     WC_RNG      rng;
24479     byte        out[KEY32];
24480     int         keySz = sizeof(out);
24481     word32      outlen = (word32)sizeof(out);
24482 
24483 #if defined(HAVE_ECC) && !defined(NO_ECC256)
24484     const char* qx =
24485         "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
24486     const char* qy =
24487         "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
24488     const char* d  =
24489         "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
24490     const char* curveName = "SECP256R1";
24491     const byte expected_shared_secret[] =
24492         {
24493             0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
24494             0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
24495             0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
24496             0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
24497         };
24498 #endif
24499 
24500     /* Initialize variables. */
24501     XMEMSET(out, 0, keySz);
24502     XMEMSET(&rng, 0, sizeof(rng));
24503     XMEMSET(&key, 0, sizeof(key));
24504     XMEMSET(&pubKey, 0, sizeof(pubKey));
24505 
24506     ret = wc_InitRng(&rng);
24507     if (ret == 0) {
24508         ret = wc_ecc_init(&key);
24509         if (ret == 0) {
24510             ret = wc_ecc_init(&pubKey);
24511         }
24512     }
24513 
24514 #if defined(HAVE_ECC) && !defined(NO_ECC256)
24515     if (ret == 0) {
24516         ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
24517     }
24518     if (ret == 0) {
24519         ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
24520     }
24521 #else
24522     if (ret == 0) {
24523         ret = wc_ecc_make_key(&rng, keySz, &key);
24524     }
24525     if (ret == 0) {
24526         ret = wc_ecc_make_key(&rng, keySz, &pubKey);
24527     }
24528 #endif
24529 
24530 #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
24531     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
24532     !defined(HAVE_SELFTEST)
24533     if (ret == 0) {
24534         ret = wc_ecc_set_rng(&key, &rng);
24535     }
24536 #endif
24537 
24538     printf(testingFmt, "wc_ecc_shared_secret()");
24539     if (ret == 0) {
24540         ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
24541 
24542 #if defined(HAVE_ECC) && !defined(NO_ECC256)
24543         if (ret == 0) {
24544             if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
24545                 ret = WOLFSSL_FATAL_ERROR;
24546             }
24547         }
24548 #endif
24549 
24550         /* Test bad args. */
24551         if (ret == 0) {
24552             ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
24553             if (ret == BAD_FUNC_ARG) {
24554                 ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
24555             }
24556             if (ret == BAD_FUNC_ARG) {
24557                 ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
24558             }
24559             if (ret == BAD_FUNC_ARG) {
24560                 ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
24561             }
24562             if (ret == BAD_FUNC_ARG) {
24563                 /* Invalid length */
24564                 outlen = 1;
24565                 ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
24566             }
24567 
24568             if (ret == BUFFER_E) {
24569                 ret = 0;
24570             } else if (ret == 0) {
24571                 ret = WOLFSSL_FATAL_ERROR;
24572             }
24573         }
24574     }
24575 
24576     printf(resultFmt, ret == 0 ? passed : failed);
24577 
24578     if (wc_FreeRng(&rng) && ret == 0) {
24579         ret = WOLFSSL_FATAL_ERROR;
24580     }
24581     wc_ecc_free(&key);
24582     wc_ecc_free(&pubKey);
24583 
24584 #ifdef FP_ECC
24585     wc_ecc_fp_free();
24586 #endif
24587 
24588 #endif
24589     return ret;
24590 
24591 } /* END tests_wc_ecc_shared_secret */
24592 
24593 /*
24594  * testint wc_ecc_export_x963()
24595  */
24596 static int test_wc_ecc_export_x963 (void)
24597 {
24598     int     ret = 0;
24599 
24600 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
24601     ecc_key key;
24602     WC_RNG  rng;
24603     byte    out[ECC_ASN963_MAX_BUF_SZ];
24604     word32  outlen = sizeof(out);
24605 
24606     /* Initialize variables. */
24607     XMEMSET(out, 0, outlen);
24608     XMEMSET(&rng, 0, sizeof(rng));
24609     XMEMSET(&key, 0, sizeof(key));
24610 
24611     ret = wc_InitRng(&rng);
24612     if (ret == 0) {
24613         ret = wc_ecc_init(&key);
24614         if (ret == 0) {
24615             ret = wc_ecc_make_key(&rng, KEY20, &key);
24616         }
24617     }
24618     printf(testingFmt, "wc_ecc_export_x963()");
24619 
24620     if (ret == 0) {
24621         ret = wc_ecc_export_x963(&key, out, &outlen);
24622     }
24623 
24624     /* Test bad args. */
24625     if (ret == 0) {
24626         ret = wc_ecc_export_x963(NULL, out, &outlen);
24627         if (ret == ECC_BAD_ARG_E) {
24628             ret = wc_ecc_export_x963(&key, NULL, &outlen);
24629         }
24630         if (ret == LENGTH_ONLY_E) {
24631             ret = wc_ecc_export_x963(&key, out, NULL);
24632         }
24633         if (ret == ECC_BAD_ARG_E) {
24634             key.idx = -4;
24635             ret = wc_ecc_export_x963(&key, out, &outlen);
24636         }
24637         if (ret == ECC_BAD_ARG_E) {
24638             ret = 0;
24639         } else {
24640             ret = WOLFSSL_FATAL_ERROR;
24641         }
24642     }
24643 
24644     printf(resultFmt, ret == 0 ? passed : failed);
24645 
24646     if (wc_FreeRng(&rng) && ret == 0) {
24647         ret = WOLFSSL_FATAL_ERROR;
24648     }
24649     wc_ecc_free(&key);
24650 
24651 #ifdef FP_ECC
24652     wc_ecc_fp_free();
24653 #endif
24654 
24655 #endif
24656     return ret;
24657 
24658 } /* END test_wc_ecc_export_x963 */
24659 
24660 /*
24661  * Testing wc_ecc_export_x963_ex()
24662  * compile with --enable-compkey will use compression.
24663  */
24664 static int test_wc_ecc_export_x963_ex (void)
24665 {
24666     int     ret = 0;
24667 
24668 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
24669     ecc_key key;
24670     WC_RNG  rng;
24671     byte    out[ECC_ASN963_MAX_BUF_SZ];
24672     word32  outlen = sizeof(out);
24673     #ifdef HAVE_COMP_KEY
24674         word32  badOutLen = 5;
24675     #endif
24676 
24677     /* Init stack variables. */
24678     XMEMSET(out, 0, outlen);
24679     XMEMSET(&rng, 0, sizeof(rng));
24680     XMEMSET(&key, 0, sizeof(key));
24681 
24682     ret = wc_InitRng(&rng);
24683     if (ret == 0) {
24684         ret = wc_ecc_init(&key);
24685         if (ret == 0) {
24686             ret = wc_ecc_make_key(&rng, KEY64, &key);
24687         }
24688     }
24689 
24690     printf(testingFmt, "wc_ecc_export_x963_ex()");
24691 
24692     #ifdef HAVE_COMP_KEY
24693         if (ret == 0) {
24694             ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
24695         }
24696     #else
24697         if (ret == 0) {
24698             ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
24699         }
24700     #endif
24701 
24702     /* Test bad args. */
24703     #ifdef HAVE_COMP_KEY
24704     if (ret == 0) {
24705         ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
24706         if (ret == BAD_FUNC_ARG) {
24707             ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
24708         }
24709         if (ret == BAD_FUNC_ARG) {
24710             ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
24711         }
24712         if (ret == BAD_FUNC_ARG) {
24713             ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
24714         }
24715         if (ret == BUFFER_E) {
24716             key.idx = -4;
24717             ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
24718         }
24719         if (ret == ECC_BAD_ARG_E) {
24720             ret = 0;
24721         } else {
24722             ret = WOLFSSL_FATAL_ERROR;
24723         }
24724     }
24725     #else
24726         if (ret == 0) {
24727             ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
24728             if (ret == BAD_FUNC_ARG) {
24729                 ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
24730             }
24731             if (ret == BAD_FUNC_ARG) {
24732                 ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
24733             }
24734             if (ret == NOT_COMPILED_IN) {
24735                 ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
24736             }
24737             if (ret == BAD_FUNC_ARG) {
24738                 key.idx = -4;
24739                 ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
24740             }
24741             if (ret == ECC_BAD_ARG_E) {
24742                 ret = 0;
24743             } else if (ret == 0) {
24744                 ret = WOLFSSL_FATAL_ERROR;
24745             }
24746         }
24747     #endif
24748 
24749     printf(resultFmt, ret == 0 ? passed : failed);
24750 
24751     if (wc_FreeRng(&rng) && ret == 0) {
24752         ret = WOLFSSL_FATAL_ERROR;
24753     }
24754     wc_ecc_free(&key);
24755 
24756 #ifdef FP_ECC
24757     wc_ecc_fp_free();
24758 #endif
24759 
24760 #endif
24761     return ret;
24762 
24763 } /* END test_wc_ecc_export_x963_ex */
24764 
24765 /*
24766  * testing wc_ecc_import_x963()
24767  */
24768 static int test_wc_ecc_import_x963 (void)
24769 {
24770     int     ret = 0;
24771 
24772 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
24773     defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
24774     ecc_key pubKey, key;
24775     WC_RNG  rng;
24776     byte    x963[ECC_ASN963_MAX_BUF_SZ];
24777     word32  x963Len = (word32)sizeof(x963);
24778 
24779     /* Init stack variables. */
24780     XMEMSET(x963, 0, x963Len);
24781     XMEMSET(&rng, 0, sizeof(rng));
24782     XMEMSET(&key, 0, sizeof(key));
24783     XMEMSET(&pubKey, 0, sizeof(pubKey));
24784 
24785     ret = wc_InitRng(&rng);
24786     if (ret == 0) {
24787         ret = wc_ecc_init(&pubKey);
24788         if (ret == 0) {
24789             ret = wc_ecc_init(&key);
24790         }
24791         if (ret == 0) {
24792             ret = wc_ecc_make_key(&rng, KEY24, &key);
24793         }
24794         if (ret == 0) {
24795             PRIVATE_KEY_UNLOCK();
24796             ret = wc_ecc_export_x963(&key, x963, &x963Len);
24797             PRIVATE_KEY_LOCK();
24798         }
24799     }
24800 
24801     printf(testingFmt, "wc_ecc_import_x963()");
24802     if (ret == 0) {
24803         ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
24804     }
24805 
24806     /* Test bad args. */
24807     if (ret == 0) {
24808         ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
24809         if (ret == BAD_FUNC_ARG) {
24810             ret = wc_ecc_import_x963(x963, x963Len, NULL);
24811         }
24812         if (ret == BAD_FUNC_ARG) {
24813             ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
24814         }
24815         if (ret == ECC_BAD_ARG_E) {
24816             ret = 0;
24817         } else if (ret == 0) {
24818             ret = WOLFSSL_FATAL_ERROR;
24819         }
24820     }
24821 
24822     printf(resultFmt, ret == 0 ? passed : failed);
24823 
24824     if (wc_FreeRng(&rng) && ret == 0) {
24825         ret = WOLFSSL_FATAL_ERROR;
24826     }
24827     wc_ecc_free(&key);
24828     wc_ecc_free(&pubKey);
24829 
24830 #ifdef FP_ECC
24831     wc_ecc_fp_free();
24832 #endif
24833 
24834 #endif
24835     return ret;
24836 
24837 } /* END wc_ecc_import_x963 */
24838 
24839 /*
24840  * testing wc_ecc_import_private_key()
24841  */
24842 static int ecc_import_private_key (void)
24843 {
24844     int     ret = 0;
24845 
24846 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
24847     defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
24848     ecc_key key, keyImp;
24849     WC_RNG  rng;
24850     byte    privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
24851     byte    x963Key[ECC_ASN963_MAX_BUF_SZ];
24852     word32  privKeySz = (word32)sizeof(privKey);
24853     word32  x963KeySz = (word32)sizeof(x963Key);
24854 
24855     /* Init stack variables. */
24856     XMEMSET(privKey, 0, privKeySz);
24857     XMEMSET(x963Key, 0, x963KeySz);
24858     XMEMSET(&rng, 0, sizeof(rng));
24859     XMEMSET(&key, 0, sizeof(key));
24860     XMEMSET(&keyImp, 0, sizeof(keyImp));
24861 
24862     ret = wc_InitRng(&rng);
24863     if (ret == 0) {
24864         ret = wc_ecc_init(&key);
24865         if (ret == 0) {
24866             ret = wc_ecc_init(&keyImp);
24867         }
24868         if (ret == 0) {
24869             ret = wc_ecc_make_key(&rng, KEY48, &key);
24870         }
24871         if (ret == 0) {
24872             PRIVATE_KEY_UNLOCK();
24873             ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
24874             PRIVATE_KEY_LOCK();
24875         }
24876         if (ret == 0) {
24877             ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
24878         }
24879     }
24880 
24881     printf(testingFmt, "wc_ecc_import_private_key()");
24882 
24883     if (ret == 0) {
24884         ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
24885                                                 x963KeySz, &keyImp);
24886     }
24887     /* Pass in bad args. */
24888     if (ret == 0) {
24889         ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
24890                                                 x963KeySz, NULL);
24891         if (ret == BAD_FUNC_ARG) {
24892             ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
24893                                                 x963KeySz, &keyImp);
24894         }
24895         if (ret == BAD_FUNC_ARG) {
24896             ret = 0;
24897         } else if (ret == 0) {
24898             ret = WOLFSSL_FATAL_ERROR;
24899         }
24900     }
24901 
24902     printf(resultFmt, ret == 0 ? passed : failed);
24903 
24904     if (wc_FreeRng(&rng) && ret == 0) {
24905         ret = WOLFSSL_FATAL_ERROR;
24906     }
24907     wc_ecc_free(&key);
24908     wc_ecc_free(&keyImp);
24909 
24910 #ifdef FP_ECC
24911     wc_ecc_fp_free();
24912 #endif
24913 
24914 #endif
24915     return ret;
24916 
24917 } /* END wc_ecc_import_private_key */
24918 
24919 
24920 /*
24921  * Testing wc_ecc_export_private_only()
24922  */
24923 static int test_wc_ecc_export_private_only (void)
24924 {
24925     int     ret = 0;
24926 
24927 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
24928     ecc_key key;
24929     WC_RNG  rng;
24930     byte    out[ECC_PRIV_KEY_BUF];
24931     word32  outlen = sizeof(out);
24932 
24933     /* Init stack variables. */
24934     XMEMSET(out, 0, outlen);
24935     XMEMSET(&rng, 0, sizeof(rng));
24936     XMEMSET(&key, 0, sizeof(key));
24937 
24938     ret = wc_InitRng(&rng);
24939     if (ret == 0) {
24940         ret = wc_ecc_init(&key);
24941         if (ret == 0) {
24942             ret = wc_ecc_make_key(&rng, KEY32, &key);
24943         }
24944     }
24945     printf(testingFmt, "wc_ecc_export_private_only()");
24946 
24947     if (ret == 0) {
24948         ret = wc_ecc_export_private_only(&key, out, &outlen);
24949     }
24950     /* Pass in bad args. */
24951     if (ret == 0) {
24952         ret = wc_ecc_export_private_only(NULL, out, &outlen);
24953         if (ret == BAD_FUNC_ARG) {
24954             ret = wc_ecc_export_private_only(&key, NULL, &outlen);
24955         }
24956         if (ret == BAD_FUNC_ARG) {
24957             ret = wc_ecc_export_private_only(&key, out, NULL);
24958         }
24959         if (ret == BAD_FUNC_ARG) {
24960             ret = 0;
24961         } else if (ret == 0) {
24962             ret = WOLFSSL_FATAL_ERROR;
24963         }
24964     }
24965 
24966     printf(resultFmt, ret == 0 ? passed : failed);
24967 
24968     if (wc_FreeRng(&rng) && ret == 0) {
24969         ret = WOLFSSL_FATAL_ERROR;
24970     }
24971     wc_ecc_free(&key);
24972 
24973 #ifdef FP_ECC
24974     wc_ecc_fp_free();
24975 #endif
24976 
24977 #endif
24978     return ret;
24979 
24980 } /* END test_wc_ecc_export_private_only */
24981 
24982 
24983 /*
24984  * Testing wc_ecc_rs_to_sig()
24985  */
24986 static int test_wc_ecc_rs_to_sig (void)
24987 {
24988 
24989     int           ret = 0;
24990 
24991 #if defined(HAVE_ECC) && !defined(NO_ASN)
24992     /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
24993     const char*   R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
24994     const char*   S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
24995     const char*   zeroStr = "0";
24996     byte          sig[ECC_MAX_SIG_SIZE];
24997     word32        siglen = (word32)sizeof(sig);
24998     /*R and S max size is the order of curve. 2^192.*/
24999     int           keySz = KEY24;
25000     byte          r[KEY24];
25001     byte          s[KEY24];
25002     word32        rlen = (word32)sizeof(r);
25003     word32        slen = (word32)sizeof(s);
25004 
25005     /* Init stack variables. */
25006     XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
25007     XMEMSET(r, 0, keySz);
25008     XMEMSET(s, 0, keySz);
25009 
25010     printf(testingFmt, "wc_ecc_rs_to_sig()");
25011 
25012     ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
25013     /* Test bad args. */
25014     if (ret == 0) {
25015         ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
25016         if (ret == ECC_BAD_ARG_E) {
25017             ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
25018         }
25019         if (ret == ECC_BAD_ARG_E) {
25020             ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
25021         }
25022         if (ret == ECC_BAD_ARG_E) {
25023             ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
25024         }
25025         if (ret == ECC_BAD_ARG_E) {
25026             ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
25027         }
25028         if (ret == MP_ZERO_E) {
25029             ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
25030         }
25031         if (ret == MP_ZERO_E) {
25032             ret = 0;
25033         } else {
25034             ret = WOLFSSL_FATAL_ERROR;
25035         }
25036     }
25037 
25038     printf(resultFmt, ret == 0 ? passed : failed);
25039     printf(testingFmt, "wc_ecc_sig_to_rs()");
25040     if (ret == 0) {
25041         ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
25042     }
25043     /* Test bad args. */
25044     if (ret == 0) {
25045         ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
25046         if (ret == ECC_BAD_ARG_E) {
25047             ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
25048         }
25049         if (ret == ECC_BAD_ARG_E) {
25050             ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
25051         }
25052         if (ret == ECC_BAD_ARG_E) {
25053             ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
25054         }
25055         if (ret == ECC_BAD_ARG_E) {
25056             ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
25057         }
25058         if (ret == ECC_BAD_ARG_E) {
25059             ret = 0;
25060         } else if (ret == 0) {
25061             ret = WOLFSSL_FATAL_ERROR;
25062         }
25063     }
25064 
25065     printf(resultFmt, ret == 0 ? passed : failed);
25066 
25067 #endif
25068     return ret;
25069 
25070 } /* END test_wc_ecc_rs_to_sig */
25071 
25072 static int test_wc_ecc_import_raw(void)
25073 {
25074     int         ret = 0;
25075 
25076 #if defined(HAVE_ECC) && !defined(NO_ECC256)
25077     ecc_key     key;
25078     const char* qx =
25079               "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
25080     const char* qy =
25081               "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
25082     const char* d  =
25083              "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
25084     const char* curveName = "SECP256R1";
25085 #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
25086     const char* kNullStr = "";
25087 #endif
25088 
25089     ret = wc_ecc_init(&key);
25090 
25091     printf(testingFmt, "wc_ecc_import_raw()");
25092 
25093     if (ret == 0) {
25094         ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
25095     }
25096     /* Test bad args. */
25097     if (ret == 0) {
25098         ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
25099         if (ret == BAD_FUNC_ARG) {
25100             ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
25101         }
25102         if (ret == BAD_FUNC_ARG) {
25103             ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
25104         }
25105         if (ret == BAD_FUNC_ARG) {
25106             ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
25107         }
25108     #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
25109         if (ret == BAD_FUNC_ARG) {
25110         #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
25111             wc_ecc_free(&key);
25112         #endif
25113             ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
25114             if (ret == ECC_INF_E)
25115                 ret = BAD_FUNC_ARG; /* This is expected by other tests */
25116         }
25117     #endif
25118     #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
25119         if (ret == BAD_FUNC_ARG) {
25120         #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
25121             wc_ecc_free(&key);
25122         #endif
25123             ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
25124         }
25125         if (ret == BAD_FUNC_ARG) {
25126         #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
25127             wc_ecc_free(&key);
25128         #endif
25129             ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
25130         }
25131     #endif
25132         if (ret == BAD_FUNC_ARG) {
25133             ret = 0;
25134         }
25135     }
25136 
25137     printf(resultFmt, ret == 0 ? passed : failed);
25138 
25139     wc_ecc_free(&key);
25140 
25141 #endif
25142 
25143     return ret;
25144 
25145 } /* END test_wc_ecc_import_raw */
25146 
25147 static int test_wc_ecc_import_unsigned(void)
25148 {
25149     int         ret = 0;
25150 #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
25151     (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
25152     ecc_key     key;
25153     const byte  qx[] = {
25154         0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
25155         0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
25156         0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
25157         0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
25158     };
25159     const byte  qy[] = {
25160         0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
25161         0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
25162         0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
25163         0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
25164     };
25165     const byte  d[] = {
25166         0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
25167         0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
25168         0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
25169         0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
25170     };
25171 #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
25172     const byte nullBytes[32] = {0};
25173 #endif
25174     int curveId = ECC_SECP256R1;
25175 
25176     ret = wc_ecc_init(&key);
25177 
25178     printf(testingFmt, "wc_ecc_import_unsigned()");
25179 
25180     if (ret == 0) {
25181         ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
25182             curveId);
25183     }
25184     /* Test bad args. */
25185     if (ret == 0) {
25186         ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
25187             curveId);
25188         if (ret == BAD_FUNC_ARG) {
25189             ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
25190                 curveId);
25191         }
25192         if (ret == BAD_FUNC_ARG) {
25193             ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
25194                 curveId);
25195         }
25196         if (ret == BAD_FUNC_ARG) {
25197             ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
25198                 ECC_CURVE_INVALID);
25199         }
25200     #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
25201         if (ret == BAD_FUNC_ARG) {
25202             ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
25203                 (byte*)nullBytes, (byte*)nullBytes, curveId);
25204         }
25205     #endif
25206         if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
25207             ret = 0;
25208         }
25209     }
25210 
25211     printf(resultFmt, ret == 0 ? passed : failed);
25212 
25213     wc_ecc_free(&key);
25214 
25215 #endif
25216 
25217     return ret;
25218 
25219 } /* END test_wc_ecc_import_unsigned */
25220 
25221 
25222 /*
25223  * Testing wc_ecc_sig_size()
25224  */
25225 static int test_wc_ecc_sig_size (void)
25226 {
25227    int         ret = 0;
25228 
25229 #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
25230     ecc_key     key;
25231     WC_RNG      rng;
25232     int         keySz = KEY16;
25233 
25234     XMEMSET(&rng, 0, sizeof(rng));
25235     XMEMSET(&key, 0, sizeof(key));
25236     ret = wc_InitRng(&rng);
25237     if (ret == 0) {
25238         ret = wc_ecc_init(&key);
25239         if (ret == 0) {
25240             ret = wc_ecc_make_key(&rng, keySz, &key);
25241         }
25242     }
25243 
25244     printf(testingFmt, "wc_ecc_sig_size()");
25245 
25246     if (ret == 0) {
25247         ret = wc_ecc_sig_size(&key);
25248         if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
25249             ret = 0;
25250         }
25251     }
25252 
25253     printf(resultFmt, ret == 0 ? passed : failed);
25254 
25255     if (wc_FreeRng(&rng) && ret == 0) {
25256         ret = WOLFSSL_FATAL_ERROR;
25257     }
25258     wc_ecc_free(&key);
25259 
25260 #endif
25261     return ret;
25262 
25263 } /* END test_wc_ecc_sig_size */
25264 
25265 /*
25266  * Testing wc_ecc_ctx_new()
25267  */
25268 static int test_wc_ecc_ctx_new (void)
25269 {
25270     int         ret = 0;
25271 
25272 #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
25273     WC_RNG      rng;
25274     ecEncCtx*   cli = NULL;
25275     ecEncCtx*   srv = NULL;
25276 
25277     ret = wc_InitRng(&rng);
25278 
25279     printf(testingFmt, "wc_ecc_ctx_new()");
25280     if (ret == 0) {
25281         cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
25282         srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
25283     }
25284     if (ret == 0 && (cli == NULL || srv == NULL)) {
25285         ret = WOLFSSL_FATAL_ERROR;
25286     }
25287 
25288     wc_ecc_ctx_free(cli);
25289     wc_ecc_ctx_free(srv);
25290 
25291     /* Test bad args. */
25292     if (ret == 0) {
25293         /* wc_ecc_ctx_new_ex() will free if returned NULL. */
25294         cli = wc_ecc_ctx_new(0, &rng);
25295         if (cli != NULL) {
25296             ret = WOLFSSL_FATAL_ERROR;
25297         }
25298         cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
25299         if (cli != NULL) {
25300             ret = WOLFSSL_FATAL_ERROR;
25301         }
25302     }
25303 
25304     printf(resultFmt, ret == 0 ? passed : failed);
25305 
25306     if (wc_FreeRng(&rng) && ret == 0) {
25307         ret = WOLFSSL_FATAL_ERROR;
25308     }
25309     wc_ecc_ctx_free(cli);
25310 
25311 #endif
25312     return ret;
25313 
25314 } /* END test_wc_ecc_ctx_new */
25315 
25316 /*
25317  * Tesing wc_ecc_reset()
25318  */
25319 static int test_wc_ecc_ctx_reset (void)
25320 {
25321     int         ret = 0;
25322 
25323 #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
25324     ecEncCtx*   ctx = NULL;
25325     WC_RNG      rng;
25326 
25327     ret = wc_InitRng(&rng);
25328     if (ret == 0) {
25329         if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
25330             ret = WOLFSSL_FATAL_ERROR;
25331         }
25332     }
25333 
25334     printf(testingFmt, "wc_ecc_ctx_reset()");
25335 
25336     if (ret == 0) {
25337         ret = wc_ecc_ctx_reset(ctx, &rng);
25338     }
25339 
25340     /* Pass in bad args. */
25341     if (ret == 0) {
25342         ret = wc_ecc_ctx_reset(NULL, &rng);
25343         if (ret == BAD_FUNC_ARG) {
25344             ret = wc_ecc_ctx_reset(ctx, NULL);
25345         }
25346         if (ret == BAD_FUNC_ARG) {
25347             ret = 0;
25348         } else if (ret == 0) {
25349             ret = WOLFSSL_FATAL_ERROR;
25350         }
25351     }
25352 
25353     printf(resultFmt, ret == 0 ? passed : failed);
25354 
25355     if (wc_FreeRng(&rng) && ret == 0) {
25356         ret = WOLFSSL_FATAL_ERROR;
25357     }
25358     wc_ecc_ctx_free(ctx);
25359 
25360 #endif
25361     return ret;
25362 
25363 } /* END test_wc_ecc_ctx_reset */
25364 
25365 /*
25366  * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
25367  */
25368 static int test_wc_ecc_ctx_set_peer_salt (void)
25369 {
25370     int         ret = 0;
25371 
25372 #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
25373     WC_RNG          rng;
25374     ecEncCtx*       cliCtx      = NULL;
25375     ecEncCtx*       servCtx     = NULL;
25376     const byte*     cliSalt     = NULL;
25377     const byte*     servSalt    = NULL;
25378 
25379     ret = wc_InitRng(&rng);
25380     if (ret == 0) {
25381         if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
25382            ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
25383             ret = WOLFSSL_FATAL_ERROR;
25384         }
25385     }
25386 
25387     printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
25388 
25389     /* Test bad args. */
25390     if (ret == 0) {
25391         cliSalt  = wc_ecc_ctx_get_own_salt(NULL);
25392         if (cliSalt != NULL) {
25393             ret = WOLFSSL_FATAL_ERROR;
25394         }
25395     }
25396 
25397     if (ret == 0) {
25398         cliSalt  = wc_ecc_ctx_get_own_salt(cliCtx);
25399         servSalt = wc_ecc_ctx_get_own_salt(servCtx);
25400         if (cliSalt == NULL || servSalt == NULL) {
25401             ret = WOLFSSL_FATAL_ERROR;
25402         }
25403     }
25404 
25405     printf(resultFmt, ret == 0 ? passed : failed);
25406     printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
25407 
25408     if (ret == 0) {
25409         ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
25410     }
25411     /* Test bad args. */
25412     if (ret == 0) {
25413         ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
25414         if (ret == BAD_FUNC_ARG) {
25415             ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
25416         }
25417         if (ret == BAD_FUNC_ARG) {
25418             ret = 0;
25419         } else if (ret == 0) {
25420             ret = WOLFSSL_FATAL_ERROR;
25421         }
25422     }
25423 
25424     printf(resultFmt, ret == 0 ? passed : failed);
25425 
25426     if (wc_FreeRng(&rng) && ret == 0) {
25427         ret = WOLFSSL_FATAL_ERROR;
25428     }
25429     wc_ecc_ctx_free(cliCtx);
25430     wc_ecc_ctx_free(servCtx);
25431 
25432 #endif
25433     return ret;
25434 
25435 } /* END test_wc_ecc_ctx_set_peer_salt */
25436 
25437 /*
25438  * Testing wc_ecc_ctx_set_info()
25439  */
25440 static int test_wc_ecc_ctx_set_info (void)
25441 {
25442     int         ret = 0;
25443 
25444 #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
25445     ecEncCtx*   ctx = NULL;
25446     WC_RNG      rng;
25447     const char* optInfo = "Optional Test Info.";
25448     int         optInfoSz = (int)XSTRLEN(optInfo);
25449     const char* badOptInfo = NULL;
25450 
25451     ret = wc_InitRng(&rng);
25452     if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
25453         ret = WOLFSSL_FATAL_ERROR;
25454     }
25455 
25456     printf(testingFmt, "wc_ecc_ctx_set_info()");
25457 
25458     if (ret == 0) {
25459         ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
25460     }
25461     /* Test bad args. */
25462     if (ret == 0) {
25463         ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
25464         if (ret == BAD_FUNC_ARG) {
25465             ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
25466         }
25467         if (ret == BAD_FUNC_ARG) {
25468             ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
25469         }
25470         if (ret == BAD_FUNC_ARG) {
25471             ret = 0;
25472         } else if (ret == 0) {
25473             ret = WOLFSSL_FATAL_ERROR;
25474         }
25475     }
25476 
25477     printf(resultFmt, ret == 0 ? passed : failed);
25478 
25479     if (wc_FreeRng(&rng) && ret == 0) {
25480         ret = WOLFSSL_FATAL_ERROR;
25481     }
25482     wc_ecc_ctx_free(ctx);
25483 
25484 #endif
25485     return ret;
25486 
25487 } /* END test_wc_ecc_ctx_set_info */
25488 
25489 /*
25490  * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
25491  */
25492 static int test_wc_ecc_encryptDecrypt (void)
25493 {
25494     int         ret = 0;
25495 
25496 #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
25497     defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
25498     ecc_key     srvKey, cliKey, tmpKey;
25499     WC_RNG      rng;
25500     const char* msg   = "EccBlock Size 16";
25501     word32      msgSz = (word32)XSTRLEN(msg);
25502 #ifdef WOLFSSL_ECIES_OLD
25503     byte        out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
25504 #else
25505     byte        out[KEY20 * 2 + 1 + XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
25506 #endif
25507     word32      outSz = (word32)sizeof(out);
25508     byte        plain[XSTRLEN(msg) + 1];
25509     word32      plainSz = (word32)sizeof(plain);
25510     int         keySz = KEY20;
25511 
25512     /* Init stack variables. */
25513     XMEMSET(out, 0, outSz);
25514     XMEMSET(plain, 0, plainSz);
25515     XMEMSET(&rng, 0, sizeof(rng));
25516     XMEMSET(&srvKey, 0, sizeof(srvKey));
25517     XMEMSET(&cliKey, 0, sizeof(cliKey));
25518 
25519     ret = wc_InitRng(&rng);
25520     if (ret == 0) {
25521         ret = wc_ecc_init(&cliKey);
25522         if (ret == 0) {
25523             ret = wc_ecc_make_key(&rng, keySz, &cliKey);
25524         }
25525         if (ret == 0) {
25526             ret = wc_ecc_init(&srvKey);
25527         }
25528         if (ret == 0) {
25529             ret = wc_ecc_make_key(&rng, keySz, &srvKey);
25530         }
25531         if (ret == 0) {
25532             ret = wc_ecc_init(&tmpKey);
25533         }
25534     }
25535 
25536 #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
25537     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
25538     !defined(HAVE_SELFTEST)
25539     if (ret == 0) {
25540         ret = wc_ecc_set_rng(&srvKey, &rng);
25541     }
25542     if (ret == 0) {
25543         ret = wc_ecc_set_rng(&cliKey, &rng);
25544     }
25545 #endif
25546 
25547     printf(testingFmt, "wc_ecc_encrypt()");
25548 
25549     if (ret == 0) {
25550         ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
25551                                                             &outSz, NULL);
25552     }
25553     if (ret == 0) {
25554         ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
25555                                                             &outSz, NULL);
25556         if (ret == BAD_FUNC_ARG) {
25557             ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
25558                                                             &outSz, NULL);
25559         }
25560         if (ret == BAD_FUNC_ARG) {
25561             ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
25562                                                             &outSz, NULL);
25563         }
25564         if (ret == BAD_FUNC_ARG) {
25565             ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
25566                                                             &outSz, NULL);
25567         }
25568         if (ret == BAD_FUNC_ARG) {
25569             ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
25570                                                             NULL, NULL);
25571         }
25572         if (ret == BAD_FUNC_ARG) {
25573             ret = 0;
25574         } else if (ret == 0) {
25575             ret = WOLFSSL_FATAL_ERROR;
25576         }
25577     }
25578 
25579     printf(resultFmt, ret == 0 ? passed : failed);
25580     printf(testingFmt, "wc_ecc_decrypt()");
25581 
25582 #ifdef WOLFSSL_ECIES_OLD
25583     if (ret == 0) {
25584         tmpKey.dp = cliKey.dp;
25585         ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
25586     }
25587 #endif
25588 
25589     if (ret == 0) {
25590         ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
25591                                                         &plainSz, NULL);
25592     }
25593     if (ret == 0) {
25594         ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
25595                                                         &plainSz, NULL);
25596     #ifdef WOLFSSL_ECIES_OLD
25597         /* NULL parameter allowed in new implementations - public key comes from
25598          * the message. */
25599         if (ret == BAD_FUNC_ARG) {
25600             ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
25601                                                         &plainSz, NULL);
25602         }
25603     #endif
25604         if (ret == BAD_FUNC_ARG) {
25605             ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
25606                                                         &plainSz, NULL);
25607         }
25608         if (ret == BAD_FUNC_ARG) {
25609             ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
25610                                                         &plainSz, NULL);
25611         }
25612         if (ret == BAD_FUNC_ARG) {
25613             ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
25614                                                         plain, NULL, NULL);
25615         }
25616         if (ret == BAD_FUNC_ARG) {
25617             ret = 0;
25618         } else if (ret == 0) {
25619             ret = WOLFSSL_FATAL_ERROR;
25620         }
25621     }
25622 
25623     if (XMEMCMP(msg, plain, msgSz) != 0) {
25624         ret = WOLFSSL_FATAL_ERROR;
25625     }
25626 
25627     printf(resultFmt, ret == 0 ? passed : failed);
25628 
25629     if (wc_FreeRng(&rng) && ret == 0) {
25630         ret = WOLFSSL_FATAL_ERROR;
25631     }
25632     wc_ecc_free(&tmpKey);
25633     wc_ecc_free(&cliKey);
25634     wc_ecc_free(&srvKey);
25635 
25636 #endif
25637     return ret;
25638 
25639 } /* END test_wc_ecc_encryptDecrypt */
25640 
25641 /*
25642  * Testing wc_ecc_del_point() and wc_ecc_new_point()
25643  */
25644 static int test_wc_ecc_del_point (void)
25645 {
25646     int         ret = 0;
25647 
25648 #if defined(HAVE_ECC)
25649     ecc_point*   pt;
25650 
25651     printf(testingFmt, "wc_ecc_new_point()");
25652 
25653     pt = wc_ecc_new_point();
25654     if (!pt) {
25655         ret = WOLFSSL_FATAL_ERROR;
25656     }
25657 
25658     printf(resultFmt, ret == 0 ? passed : failed);
25659 
25660     wc_ecc_del_point(pt);
25661 
25662 #endif
25663     return ret;
25664 
25665 } /* END test_wc_ecc_del_point */
25666 
25667 /*
25668  * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
25669  * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
25670  * and wc_ecc_cmp_point()
25671  */
25672 static int test_wc_ecc_pointFns (void)
25673 {
25674     int         ret = 0;
25675 
25676 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
25677     !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
25678     !defined(WOLFSSL_ATECC608A)
25679     ecc_key     key;
25680     WC_RNG      rng;
25681     ecc_point*  point = NULL;
25682     ecc_point*  cpypt = NULL;
25683     int         idx = 0;
25684     int         keySz = KEY32;
25685     byte        der[DER_SZ(KEY32)];
25686     word32      derlenChk = 0;
25687     word32      derSz = DER_SZ(KEY32);
25688 
25689     /* Init stack variables. */
25690     XMEMSET(der, 0, derSz);
25691     XMEMSET(&rng, 0, sizeof(rng));
25692     XMEMSET(&key, 0, sizeof(key));
25693 
25694     ret = wc_InitRng(&rng);
25695     if (ret == 0) {
25696         ret = wc_ecc_init(&key);
25697         if (ret == 0) {
25698             ret = wc_ecc_make_key(&rng, keySz, &key);
25699         }
25700     }
25701 
25702     if (ret == 0) {
25703         point = wc_ecc_new_point();
25704         if (!point) {
25705             ret = WOLFSSL_FATAL_ERROR;
25706         }
25707     }
25708 
25709     if (ret == 0) {
25710         cpypt = wc_ecc_new_point();
25711         if (!cpypt) {
25712             ret = WOLFSSL_FATAL_ERROR;
25713         }
25714     }
25715 
25716     /* Export */
25717     printf(testingFmt, "wc_ecc_export_point_der()");
25718     if (ret == 0) {
25719         ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
25720                                                        NULL, &derlenChk);
25721         /* Check length value. */
25722         if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
25723             ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
25724                                                            der, &derSz);
25725         }
25726     }
25727     /* Test bad args. */
25728     if (ret == 0) {
25729         ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
25730         if (ret == ECC_BAD_ARG_E) {
25731             ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
25732         }
25733         if (ret == ECC_BAD_ARG_E) {
25734             ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
25735                                                                 der, NULL);
25736         }
25737         if (ret == ECC_BAD_ARG_E) {
25738             ret = 0;
25739         } else if (ret == 0) {
25740             ret = WOLFSSL_FATAL_ERROR;
25741         }
25742     }
25743 
25744     printf(resultFmt, ret == 0 ? passed : failed);
25745 
25746     /* Import */
25747     printf(testingFmt, "wc_ecc_import_point_der()");
25748 
25749     if (ret == 0) {
25750         ret = wc_ecc_import_point_der(der, derSz, idx, point);
25751         /* Condition double checks wc_ecc_cmp_point().  */
25752         if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
25753             ret = wc_ecc_cmp_point(&key.pubkey, point);
25754         }
25755     }
25756     /* Test bad args. */
25757     if (ret == 0) {
25758         ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
25759         if (ret == ECC_BAD_ARG_E) {
25760             ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
25761         }
25762         if (ret == ECC_BAD_ARG_E) {
25763             ret = wc_ecc_import_point_der(der, derSz, -1, point);
25764         }
25765         if (ret == ECC_BAD_ARG_E) {
25766             ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
25767         }
25768         if (ret == ECC_BAD_ARG_E) {
25769             ret = 0;
25770         } else if (ret == 0) {
25771             ret = WOLFSSL_FATAL_ERROR;
25772         }
25773     }
25774     printf(resultFmt, ret == 0 ? passed : failed);
25775 
25776     /* Copy */
25777     printf(testingFmt, "wc_ecc_copy_point()");
25778 
25779     if (ret == 0) {
25780         ret = wc_ecc_copy_point(point, cpypt);
25781     }
25782     /* Test bad args. */
25783     if (ret == 0) {
25784         ret = wc_ecc_copy_point(NULL, cpypt);
25785         if (ret == ECC_BAD_ARG_E) {
25786             ret = wc_ecc_copy_point(point, NULL);
25787         }
25788         if (ret == ECC_BAD_ARG_E) {
25789             ret = 0;
25790         } else if (ret == 0) {
25791             ret = WOLFSSL_FATAL_ERROR;
25792         }
25793     }
25794 
25795     printf(resultFmt, ret == 0 ? passed : failed);
25796 
25797     printf(testingFmt, "wc_ecc_cmp_point()");
25798     /* Compare point */
25799     if (ret == 0) {
25800         ret = wc_ecc_cmp_point(point, cpypt);
25801     }
25802     /* Test bad args. */
25803     if (ret == 0) {
25804         ret = wc_ecc_cmp_point(NULL, cpypt);
25805         if (ret == BAD_FUNC_ARG) {
25806             ret = wc_ecc_cmp_point(point, NULL);
25807         }
25808         if (ret == BAD_FUNC_ARG) {
25809             ret = 0;
25810         } else if (ret == 0) {
25811             ret = WOLFSSL_FATAL_ERROR;
25812         }
25813     }
25814     printf(resultFmt, ret == 0 ? passed : failed);
25815 
25816     printf(testingFmt, "wc_ecc_point_is_at_infinity()");
25817     /* At infinity if return == 1, otherwise return == 0. */
25818     if (ret == 0) {
25819         ret = wc_ecc_point_is_at_infinity(point);
25820     }
25821     /* Test bad args. */
25822     if (ret == 0) {
25823         ret = wc_ecc_point_is_at_infinity(NULL);
25824         if (ret == BAD_FUNC_ARG) {
25825             ret = 0;
25826         } else if (ret == 0) {
25827             ret = WOLFSSL_FATAL_ERROR;
25828         }
25829     }
25830 
25831     printf(resultFmt, ret == 0 ? passed : failed);
25832 
25833 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
25834 #ifdef USE_ECC_B_PARAM
25835     printf(testingFmt, "wc_ecc_point_is_on_curve()");
25836     /* On curve if ret == 0 */
25837     if (ret == 0) {
25838         ret = wc_ecc_point_is_on_curve(point, idx);
25839     }
25840     /* Test bad args. */
25841     if (ret == 0) {
25842         ret = wc_ecc_point_is_on_curve(NULL, idx);
25843         if (ret == BAD_FUNC_ARG) {
25844             ret = wc_ecc_point_is_on_curve(point, 1000);
25845         }
25846         if (ret == ECC_BAD_ARG_E) {
25847             ret = 0;
25848         } else if (ret == 0) {
25849             ret = WOLFSSL_FATAL_ERROR;
25850         }
25851     }
25852     printf(resultFmt, ret == 0 ? passed : failed);
25853 #endif /* USE_ECC_B_PARAM */
25854 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
25855 
25856     /* Free */
25857     wc_ecc_del_point(point);
25858     wc_ecc_del_point(cpypt);
25859     wc_ecc_free(&key);
25860     if (wc_FreeRng(&rng) && ret == 0) {
25861         ret = WOLFSSL_FATAL_ERROR;
25862     }
25863 
25864 #endif
25865     return ret;
25866 
25867 } /* END test_wc_ecc_pointFns */
25868 
25869 
25870 /*
25871  * Testing wc_ecc_sahred_secret_ssh()
25872  */
25873 static int test_wc_ecc_shared_secret_ssh (void)
25874 {
25875     int         ret = 0;
25876 
25877 #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
25878     !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
25879     !defined(WOLFSSL_ATECC608A)
25880     ecc_key     key, key2;
25881     WC_RNG      rng;
25882     int         keySz = KEY32;
25883     int         key2Sz = KEY24;
25884     byte        secret[KEY32];
25885     word32      secretLen = keySz;
25886 
25887     /* Init stack variables. */
25888     XMEMSET(secret, 0, secretLen);
25889     XMEMSET(&rng, 0, sizeof(rng));
25890     XMEMSET(&key, 0, sizeof(key));
25891     XMEMSET(&key2, 0, sizeof(key2));
25892     /* Make keys */
25893     ret = wc_InitRng(&rng);
25894     if (ret == 0) {
25895         ret = wc_ecc_init(&key);
25896         if (ret == 0) {
25897             ret = wc_ecc_make_key(&rng, keySz, &key);
25898         }
25899         if (wc_FreeRng(&rng) && ret == 0) {
25900             ret = WOLFSSL_FATAL_ERROR;
25901         }
25902     }
25903     if (ret == 0) {
25904         ret = wc_InitRng(&rng);
25905         if (ret == 0) {
25906             ret = wc_ecc_init(&key2);
25907         }
25908         if (ret == 0) {
25909             ret = wc_ecc_make_key(&rng, key2Sz, &key2);
25910         }
25911     }
25912 
25913     printf(testingFmt, "ecc_shared_secret_ssh()");
25914 
25915 #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
25916     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
25917     !defined(HAVE_SELFTEST)
25918     if (ret == 0) {
25919         ret = wc_ecc_set_rng(&key, &rng);
25920     }
25921 #endif
25922 
25923     if (ret == 0) {
25924         ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
25925     }
25926     /* Pass in bad args. */
25927     if (ret == 0) {
25928         ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
25929         if (ret == BAD_FUNC_ARG) {
25930             ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
25931         }
25932         if (ret == BAD_FUNC_ARG) {
25933             ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
25934         }
25935         if (ret == BAD_FUNC_ARG) {
25936             ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
25937         }
25938         if (ret == BAD_FUNC_ARG) {
25939             key.type = ECC_PUBLICKEY;
25940             ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
25941             if (ret == ECC_BAD_ARG_E) {
25942                 ret = 0;
25943             } else if (ret == 0) {
25944                 ret = WOLFSSL_FATAL_ERROR;
25945             }
25946         } else if (ret == 0) {
25947             ret = WOLFSSL_FATAL_ERROR;
25948         }
25949     }
25950 
25951     printf(resultFmt, ret == 0 ? passed : failed);
25952 
25953     if (wc_FreeRng(&rng) && ret == 0) {
25954         ret = WOLFSSL_FATAL_ERROR;
25955     }
25956     wc_ecc_free(&key);
25957     wc_ecc_free(&key2);
25958 
25959 #ifdef FP_ECC
25960     wc_ecc_fp_free();
25961 #endif
25962 
25963 #endif
25964     return ret;
25965 
25966 } /* END test_wc_ecc_shared_secret_ssh */
25967 
25968 /*
25969  * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
25970  */
25971 static int test_wc_ecc_verify_hash_ex (void)
25972 {
25973     int             ret = 0;
25974 
25975 #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
25976      && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
25977     !defined(WOLFSSL_ATECC608A)
25978     ecc_key         key;
25979     WC_RNG          rng;
25980     mp_int          r;
25981     mp_int          s;
25982     unsigned char   hash[] = "Everyone gets Friday off.EccSig";
25983     unsigned char   iHash[] = "Everyone gets Friday off.......";
25984     unsigned char   shortHash[] = TEST_STRING;
25985     word32          hashlen = sizeof(hash);
25986     word32          iHashLen = sizeof(iHash);
25987     word32          shortHashLen = sizeof(shortHash);
25988     int             keySz = KEY32;
25989     int             sig = WOLFSSL_FATAL_ERROR;
25990     int             ver = WOLFSSL_FATAL_ERROR;
25991     int             verify_ok = 0;
25992 
25993     /* Initialize r and s. */
25994     ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
25995     if (ret != MP_OKAY) {
25996         return MP_INIT_E;
25997     }
25998 
25999     ret = wc_InitRng(&rng);
26000     if (ret == 0) {
26001         ret = wc_ecc_init(&key);
26002         if (ret == 0) {
26003             ret = wc_ecc_make_key(&rng, keySz, &key);
26004         }
26005     }
26006     if (ret == 0) {
26007         ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
26008         if (ret == 0) {
26009             /* verify_ok should be 1. */
26010             ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
26011             if (verify_ok != 1 && ret == 0) {
26012                 ret = WOLFSSL_FATAL_ERROR;
26013             }
26014         }
26015         if (ret == 0) {
26016             /* verify_ok should be 0 */
26017             ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
26018                                                     &verify_ok, &key);
26019             if (verify_ok != 0 && ret == 0) {
26020                 ret = WOLFSSL_FATAL_ERROR;
26021             }
26022         }
26023         if (ret == 0) {
26024             /* verify_ok should be 0. */
26025             ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
26026                                                             &verify_ok, &key);
26027             if (verify_ok != 0 && ret == 0) {
26028                 ret = WOLFSSL_FATAL_ERROR;
26029             }
26030         }
26031     }
26032 
26033     printf(testingFmt, "wc_ecc_sign_hash_ex()");
26034     /* Test bad args. */
26035     if (ret == 0) {
26036         if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
26037                                                 == ECC_BAD_ARG_E) {
26038             sig = 0;
26039         }
26040         if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
26041                                                             != ECC_BAD_ARG_E) {
26042             sig = WOLFSSL_FATAL_ERROR;
26043         }
26044         if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
26045                                                             != ECC_BAD_ARG_E) {
26046             sig = WOLFSSL_FATAL_ERROR;
26047         }
26048         if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
26049                                                             != ECC_BAD_ARG_E) {
26050             sig = WOLFSSL_FATAL_ERROR;
26051         }
26052         if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
26053                                                             != ECC_BAD_ARG_E) {
26054             sig = WOLFSSL_FATAL_ERROR;
26055         }
26056     }
26057     printf(resultFmt, sig == 0 ? passed : failed);
26058     printf(testingFmt, "wc_ecc_verify_hash_ex()");
26059     /* Test bad args. */
26060     if (ret == 0) {
26061         if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
26062                                                             == ECC_BAD_ARG_E) {
26063             ver = 0;
26064         }
26065         if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
26066                                                 &verify_ok, &key) != ECC_BAD_ARG_E) {
26067             ver = WOLFSSL_FATAL_ERROR;
26068         }
26069         if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
26070                                                        &key) != ECC_BAD_ARG_E) {
26071             ver = WOLFSSL_FATAL_ERROR;
26072         }
26073         if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
26074                                                 NULL, &key) != ECC_BAD_ARG_E) {
26075             ver = WOLFSSL_FATAL_ERROR;
26076         }
26077         if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
26078                                                 &verify_ok, NULL) != ECC_BAD_ARG_E) {
26079             ver = WOLFSSL_FATAL_ERROR;
26080         }
26081     }
26082     printf(resultFmt, ver == 0 ? passed : failed);
26083 
26084     wc_ecc_free(&key);
26085     mp_free(&r);
26086     mp_free(&s);
26087     if (wc_FreeRng(&rng)) {
26088         return WOLFSSL_FATAL_ERROR;
26089     }
26090     if (ret == 0 && (sig != 0 || ver != 0)) {
26091         ret = WOLFSSL_FATAL_ERROR;
26092     }
26093 #endif
26094     return ret;
26095 
26096 } /* END test_wc_ecc_verify_hash_ex */
26097 
26098 /*
26099  * Testing wc_ecc_mulmod()
26100  */
26101 
26102 static int test_wc_ecc_mulmod (void)
26103 {
26104     int         ret = 0;
26105 
26106 #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
26107     !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
26108       defined(WOLFSSL_VALIDATE_ECC_IMPORT))
26109     ecc_key     key1, key2, key3;
26110     WC_RNG      rng;
26111 
26112     ret = wc_InitRng(&rng);
26113     if (ret == 0) {
26114         ret = wc_ecc_init(&key1);
26115         if (ret == 0) {
26116             ret = wc_ecc_init(&key2);
26117         }
26118         if (ret == 0) {
26119             ret = wc_ecc_init(&key3);
26120         }
26121         if (ret == 0) {
26122             ret = wc_ecc_make_key(&rng, KEY32, &key1);
26123         }
26124         wc_FreeRng(&rng);
26125     }
26126     if (ret == 0) {
26127         ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
26128                                                                  ECC_SECP256R1);
26129         if (ret == 0) {
26130             ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
26131                                         key1.dp->prime, ECC_SECP256R1);
26132         }
26133     }
26134 
26135     printf(testingFmt, "wc_ecc_mulmod()");
26136     if (ret == 0) {
26137         ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
26138                                                             &key3.k, 1);
26139     }
26140 
26141     /* Test bad args. */
26142     if (ret == 0) {
26143         ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
26144                                                             &key3.k, 1);
26145         if (ret == ECC_BAD_ARG_E) {
26146             ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
26147                                                             &key3.k, 1);
26148         }
26149         if (ret == ECC_BAD_ARG_E) {
26150             ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
26151                                                             &key3.k, 1);
26152         }
26153         if (ret == ECC_BAD_ARG_E) {
26154             ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
26155                                                             &key2.k, NULL, 1);
26156         }
26157         if (ret == ECC_BAD_ARG_E) {
26158             ret = 0;
26159         } else if (ret == 0) {
26160             ret = WOLFSSL_FATAL_ERROR;
26161         }
26162     }
26163 
26164     printf(resultFmt, ret == 0 ? passed : failed);
26165 
26166     wc_ecc_free(&key1);
26167     wc_ecc_free(&key2);
26168     wc_ecc_free(&key3);
26169 
26170 #ifdef FP_ECC
26171     wc_ecc_fp_free();
26172 #endif
26173 
26174 #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
26175     return ret;
26176 
26177 
26178 } /* END test_wc_ecc_mulmod */
26179 
26180 /*
26181  * Testing wc_ecc_is_valid_idx()
26182  */
26183 static int test_wc_ecc_is_valid_idx (void)
26184 {
26185     int         ret = 0;
26186 
26187 #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
26188     ecc_key     key;
26189     WC_RNG      rng;
26190     int         iVal = -2;
26191     int         iVal2 = 3000;
26192 
26193     XMEMSET(&rng, 0, sizeof(rng));
26194     XMEMSET(&key, 0, sizeof(key));
26195 
26196     ret = wc_InitRng(&rng);
26197     if (ret == 0) {
26198         ret = wc_ecc_init(&key);
26199         if (ret == 0) {
26200             ret = wc_ecc_make_key(&rng, 32, &key);
26201         }
26202     }
26203 
26204     printf(testingFmt, "wc_ecc_is_valid_idx()");
26205     if (ret == 0) {
26206         ret = wc_ecc_is_valid_idx(key.idx);
26207         if (ret == 1) {
26208             ret = 0;
26209         } else {
26210             ret = WOLFSSL_FATAL_ERROR;
26211         }
26212     }
26213     /* Test bad args. */
26214     if (ret == 0) {
26215         ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
26216         if (ret == 0) {
26217             ret = wc_ecc_is_valid_idx(iVal2);
26218         }
26219         if (ret != 0) {
26220             ret = WOLFSSL_FATAL_ERROR;
26221         }
26222     }
26223 
26224     printf(resultFmt, ret == 0 ? passed : failed);
26225 
26226     if (wc_FreeRng(&rng) && ret == 0) {
26227         ret = WOLFSSL_FATAL_ERROR;
26228     }
26229     wc_ecc_free(&key);
26230 #ifdef FP_ECC
26231     wc_ecc_fp_free();
26232 #endif
26233 
26234 #endif
26235     return ret;
26236 
26237 
26238 } /* END test_wc_ecc_is_valid_idx */
26239 /*
26240  * Testing wc_ecc_get_curve_id_from_oid()
26241  */
26242 static int test_wc_ecc_get_curve_id_from_oid (void)
26243 {
26244     int ret = 0;
26245 #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
26246     !defined(HAVE_FIPS)
26247     const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
26248     word32 len = sizeof(oid);
26249 
26250     printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
26251 
26252     /* Bad Cases */
26253     ret = wc_ecc_get_curve_id_from_oid(NULL, len);
26254     if (ret == BAD_FUNC_ARG) {
26255         ret = 0;
26256     }
26257     if (ret == 0) {
26258         ret = wc_ecc_get_curve_id_from_oid(oid, 0);
26259         if (ret == ECC_CURVE_INVALID) {
26260             ret = 0;
26261         }
26262     }
26263     /* Good Case */
26264     if (ret == 0) {
26265         ret = wc_ecc_get_curve_id_from_oid(oid, len);
26266         if (ret == 7) {
26267             ret = 0;
26268         }
26269     }
26270 
26271     printf(resultFmt, ret == 0 ? passed : failed);
26272 
26273 #endif
26274     return ret;
26275 }/* END test_wc_ecc_get_curve_id_from_oid */
26276 /*
26277  * Testing wc_ecc_sig_size_calc()
26278  */
26279 static int test_wc_ecc_sig_size_calc (void)
26280 {
26281     int ret = 0;
26282 #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
26283     ecc_key     key;
26284     WC_RNG      rng;
26285     int         sz = 0;
26286 
26287     printf(testingFmt, "wc_ecc_sig_size_calc()");
26288 
26289     ret = wc_InitRng(&rng);
26290     if (ret == 0) {
26291         ret = wc_ecc_init(&key);
26292         if (ret == 0) {
26293             ret = wc_ecc_make_key(&rng, 16, &key);
26294         }
26295         sz = key.dp->size;
26296     }
26297     if (ret == 0) {
26298         ret = wc_ecc_sig_size_calc(sz);
26299         if (ret > 0) {
26300             ret = 0;
26301         }
26302     }
26303      printf(resultFmt, ret == 0 ? passed : failed);
26304     wc_ecc_free(&key);
26305     wc_FreeRng(&rng);
26306 #endif
26307     return ret;
26308 } /* END test_wc_ecc_sig_size_calc */
26309 /*
26310  * Testing ToTraditional
26311  */
26312 static int test_ToTraditional (void)
26313 {
26314     int ret = 0;
26315 #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
26316     (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
26317      defined(OPENSSL_EXTRA_X509_SMALL))
26318 
26319     XFILE   f;
26320     byte    input[TWOK_BUF];
26321     word32  sz;
26322 
26323     printf(testingFmt, "ToTraditional()");
26324 
26325     f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
26326     AssertTrue((f != XBADFILE));
26327     sz = (word32)XFREAD(input, 1, sizeof(input), f);
26328     XFCLOSE(f);
26329 
26330     /* Good case */
26331     ret = ToTraditional(input, sz);
26332     if (ret > 0) {
26333         ret = 0;
26334     }
26335     /* Bad cases */
26336     if (ret == 0) {
26337         ret = ToTraditional(NULL, 0);
26338         if (ret == BAD_FUNC_ARG) {
26339             ret = 0;
26340         }
26341     }
26342     if (ret == 0) {
26343         ret = ToTraditional(NULL, sz);
26344         if (ret == BAD_FUNC_ARG) {
26345             ret = 0;
26346         }
26347     }
26348     if (ret == 0) {
26349         ret = ToTraditional(input, 0);
26350         if (ret == ASN_PARSE_E || ret == BUFFER_E) {
26351             ret = 0;
26352         }
26353     }
26354 
26355     printf(resultFmt, ret == 0 ? passed : failed);
26356 #endif
26357     return ret;
26358 }/* End test_ToTraditional*/
26359 
26360 /*
26361  * Testing wc_EccPrivateKeyToDer
26362  */
26363 static int test_wc_EccPrivateKeyToDer (void)
26364 {
26365     int ret = 0;
26366 #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
26367 
26368     byte            output[ONEK_BUF];
26369     ecc_key         eccKey;
26370     WC_RNG          rng;
26371     word32          inLen;
26372     printf(testingFmt, "wc_EccPrivateKeyToDer()");
26373 
26374     ret = wc_InitRng(&rng);
26375 
26376     if (ret == 0) {
26377         ret = wc_ecc_init(&eccKey);
26378         if (ret == 0) {
26379             ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
26380         }
26381         inLen = (word32)sizeof(output);
26382         /* Bad Cases */
26383         if (ret == 0) {
26384             ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
26385             if (ret == BAD_FUNC_ARG) {
26386                 ret = 0;
26387             }
26388         }
26389         if (ret == 0) {
26390             ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
26391             if (ret == BAD_FUNC_ARG) {
26392                 ret = 0;
26393             }
26394         }
26395         if (ret == 0) {
26396             ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
26397             if (ret == LENGTH_ONLY_E) {
26398                 ret = 0;
26399             }
26400         }
26401         if (ret == 0) {
26402             ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
26403             if (ret == BAD_FUNC_ARG) {
26404                 ret = 0;
26405             }
26406         }
26407         /*Good Case */
26408         if (ret == 0) {
26409             ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
26410             if (ret > 0) {
26411                 ret = 0;
26412             }
26413         }
26414         wc_ecc_free(&eccKey);
26415     }
26416     wc_FreeRng(&rng);
26417 
26418     printf(resultFmt, ret == 0 ? passed : failed);
26419 #endif
26420     return ret;
26421 }/* End test_wc_EccPrivateKeyToDer*/
26422 
26423 /*
26424  * Testing wc_DhPublicKeyDecode
26425  */
26426 static int test_wc_DhPublicKeyDecode(void)
26427 {
26428     int ret = 0;
26429 #ifndef NO_DH
26430     word32 inOutIdx;
26431 
26432 #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
26433     DhKey  key;
26434     AssertIntEQ(wc_InitDhKey(&key), 0);
26435     printf(testingFmt, "wc_DhPublicKeyDecode()");
26436 
26437     AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
26438                                                         BAD_FUNC_ARG);
26439     AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
26440                                                         BAD_FUNC_ARG);
26441     AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
26442                                                         BAD_FUNC_ARG);
26443     inOutIdx = 0;
26444     AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
26445                                                         BAD_FUNC_ARG);
26446     inOutIdx = 0;
26447     AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
26448                                                         BAD_FUNC_ARG);
26449     inOutIdx = 0;
26450     AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
26451                                             sizeof_dh_pub_key_der_2048), 0);
26452     AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
26453               key.pub.used != 0 && key.priv.used == 0);
26454 
26455     wc_FreeDhKey(&key);
26456     printf(resultFmt, passed);
26457 
26458 #endif
26459     (void)inOutIdx;
26460 #endif /* !NO_DH */
26461     return ret;
26462 }
26463 
26464 /*
26465  * Testing wc_Ed25519KeyToDer
26466  */
26467 static int test_wc_Ed25519KeyToDer (void)
26468 {
26469     int ret = 0;
26470 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
26471     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
26472 
26473     byte            output[ONEK_BUF];
26474     ed25519_key     ed25519Key;
26475     WC_RNG          rng;
26476     word32          inLen;
26477 
26478     printf(testingFmt, "wc_Ed25519KeyToDer()");
26479 
26480     ret = wc_InitRng(&rng);
26481 
26482     if (ret == 0) {
26483         ret = wc_ed25519_init(&ed25519Key);
26484         if (ret == 0) {
26485             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
26486         }
26487         inLen = (word32)sizeof(output);
26488 
26489         /* Bad Cases */
26490         if (ret == 0) {
26491             ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
26492             if (ret == BAD_FUNC_ARG) {
26493                 ret = 0;
26494             }
26495         }
26496         if (ret == 0) {
26497             ret = wc_Ed25519KeyToDer(NULL, output, inLen);
26498             if (ret == BAD_FUNC_ARG) {
26499                 ret = 0;
26500             }
26501         }
26502         if (ret == 0) {
26503             ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
26504             if (ret == BAD_FUNC_ARG) {
26505                 ret = 0;
26506             }
26507         }
26508         if (ret == 0) {
26509             ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
26510             if (ret == BAD_FUNC_ARG) {
26511                 ret = 0;
26512             }
26513         }
26514         /* Good Case */
26515         if (ret == 0) {
26516             ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
26517             if (ret > 0) {
26518                 ret = 0;
26519             }
26520         }
26521         wc_ed25519_free(&ed25519Key);
26522     }
26523     wc_FreeRng(&rng);
26524 
26525     printf(resultFmt, ret == 0 ? passed : failed);
26526 #endif
26527     return ret;
26528 }/* End test_wc_Ed25519KeyToDer*/
26529 /*
26530  * Testing wc_Ed25519PrivateKeyToDer
26531  */
26532 static int test_wc_Ed25519PrivateKeyToDer (void)
26533 {
26534     int ret = 0;
26535 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
26536     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
26537 
26538     byte            output[ONEK_BUF];
26539     ed25519_key     ed25519PrivKey;
26540     WC_RNG          rng;
26541     word32          inLen;
26542 
26543     printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
26544 
26545     ret = wc_InitRng(&rng);
26546 
26547 
26548     if (ret == 0) {
26549         ret = wc_ed25519_init(&ed25519PrivKey);
26550         if (ret == 0) {
26551             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
26552         }
26553         inLen = (word32)sizeof(output);
26554 
26555         /* Bad Cases */
26556         if (ret == 0) {
26557             ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
26558             if (ret == BAD_FUNC_ARG) {
26559                 ret = 0;
26560             }
26561         }
26562         if (ret == 0) {
26563             ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
26564             if (ret == BAD_FUNC_ARG) {
26565                 ret = 0;
26566             }
26567         }
26568         if (ret == 0) {
26569             ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
26570             if (ret == BAD_FUNC_ARG) {
26571                 ret = 0;
26572             }
26573         }
26574         if (ret == 0) {
26575             ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
26576             if (ret == BAD_FUNC_ARG) {
26577                 ret = 0;
26578             }
26579         }
26580         /* Good Case */
26581         if (ret == 0) {
26582             ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
26583             if (ret > 0) {
26584                 ret = 0;
26585             }
26586         }
26587         wc_ed25519_free(&ed25519PrivKey);
26588     }
26589     wc_FreeRng(&rng);
26590 
26591     printf(resultFmt, ret == 0 ? passed : failed);
26592 #endif
26593     return ret;
26594 }/* End test_wc_Ed25519PrivateKeyToDer*/
26595 /*
26596  * Testing wc_Ed448KeyToDer
26597  */
26598 static int test_wc_Ed448KeyToDer (void)
26599 {
26600     int ret = 0;
26601 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
26602     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
26603 
26604     byte            output[ONEK_BUF];
26605     ed448_key       ed448Key;
26606     WC_RNG          rng;
26607     word32          inLen;
26608 
26609     printf(testingFmt, "wc_Ed448KeyToDer()");
26610 
26611     ret = wc_InitRng(&rng);
26612 
26613     if (ret == 0) {
26614         ret = wc_ed448_init(&ed448Key);
26615         if (ret == 0) {
26616             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
26617         }
26618         inLen = sizeof(output);
26619 
26620         /* Bad Cases */
26621         if (ret == 0) {
26622             ret = wc_Ed448KeyToDer(NULL, NULL, 0);
26623             if (ret == BAD_FUNC_ARG) {
26624                 ret = 0;
26625             }
26626         }
26627         if (ret == 0) {
26628             ret = wc_Ed448KeyToDer(NULL, output, inLen);
26629             if (ret == BAD_FUNC_ARG) {
26630                 ret = 0;
26631             }
26632         }
26633         if (ret == 0) {
26634             ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
26635             if (ret == BAD_FUNC_ARG) {
26636                 ret = 0;
26637             }
26638         }
26639         if (ret == 0) {
26640             ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
26641             if (ret == BAD_FUNC_ARG) {
26642                 ret = 0;
26643             }
26644         }
26645         /* Good Case */
26646         if (ret == 0) {
26647             ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
26648             if (ret > 0) {
26649                 ret = 0;
26650             }
26651         }
26652         wc_ed448_free(&ed448Key);
26653     }
26654     wc_FreeRng(&rng);
26655 
26656     printf(resultFmt, ret == 0 ? passed : failed);
26657 #endif
26658     return ret;
26659 }/* End test_wc_Ed448KeyToDer*/
26660 /*
26661  * Testing wc_Ed448PrivateKeyToDer
26662  */
26663 static int test_wc_Ed448PrivateKeyToDer (void)
26664 {
26665     int ret = 0;
26666 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
26667     (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
26668 
26669     byte            output[ONEK_BUF];
26670     ed448_key       ed448PrivKey;
26671     WC_RNG          rng;
26672     word32          inLen;
26673 
26674     printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
26675 
26676     ret = wc_InitRng(&rng);
26677 
26678     if (ret == 0) {
26679         ret = wc_ed448_init(&ed448PrivKey);
26680         if (ret == 0) {
26681             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
26682         }
26683         inLen = sizeof(output);
26684 
26685         /* Bad Cases */
26686         if (ret == 0) {
26687             ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
26688             if (ret == BAD_FUNC_ARG) {
26689                 ret = 0;
26690             }
26691         }
26692         if (ret == 0) {
26693             ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
26694             if (ret == BAD_FUNC_ARG) {
26695                 ret = 0;
26696             }
26697         }
26698         if (ret == 0) {
26699             ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
26700             if (ret == BAD_FUNC_ARG) {
26701                 ret = 0;
26702             }
26703         }
26704         if (ret == 0) {
26705             ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
26706             if (ret == BAD_FUNC_ARG) {
26707                 ret = 0;
26708             }
26709         }
26710         /* Good case */
26711         if (ret == 0) {
26712             ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
26713             if (ret > 0) {
26714                 ret = 0;
26715             }
26716         }
26717         wc_ed448_free(&ed448PrivKey);
26718     }
26719     wc_FreeRng(&rng);
26720 
26721     printf(resultFmt, ret == 0 ? passed : failed);
26722 #endif
26723     return ret;
26724 }/* End test_wc_Ed448PrivateKeyToDer*/
26725 /*
26726  * Testing wc_SetSubjectBuffer
26727  */
26728 static int test_wc_SetSubjectBuffer (void)
26729 {
26730     int ret = 0;
26731 #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
26732     Cert    cert;
26733     FILE*   file;
26734     byte*   der;
26735     word32  derSz;
26736 
26737     printf(testingFmt, "wc_SetSubjectBuffer()");
26738 
26739     derSz = FOURK_BUF;
26740     der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
26741     if (der == NULL) {
26742         ret = -1;
26743     }
26744     if (ret == 0) {
26745         file = XFOPEN("./certs/ca-cert.der", "rb");
26746         if (file != NULL) {
26747             derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
26748             XFCLOSE(file);
26749         }
26750         else {
26751             ret = -1;
26752         }
26753     }
26754     if (ret == 0) {
26755         ret = wc_InitCert(&cert);
26756     }
26757 
26758     if (ret == 0) {
26759         ret = wc_SetSubjectBuffer(&cert, der, derSz);
26760     }
26761 
26762     if (ret == 0) {
26763         ret = wc_SetSubjectBuffer(NULL, der, derSz);
26764         if (ret == BAD_FUNC_ARG) {
26765             ret = 0;
26766         }
26767     }
26768     XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
26769     printf(resultFmt, ret == 0 ? passed : failed);
26770 #endif
26771     return ret;
26772 }/* End test_wc_SetSubjectBuffer*/
26773 
26774 /*
26775  * Testing wc_SetSubjectKeyIdFromPublicKey_ex
26776  */
26777 static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
26778 {
26779     int ret = 0;
26780 #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
26781     WC_RNG          rng;
26782     Cert            cert;
26783 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
26784     ed25519_key     ed25519Key;
26785 #endif
26786 #if !defined(NO_RSA) && defined(HAVE_RSA)
26787     RsaKey          rsaKey;
26788     int             bits = 2048;
26789 #endif
26790 #if defined(HAVE_ECC)
26791     ecc_key         eccKey;
26792 #endif
26793 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
26794     ed448_key       ed448Key;
26795 #endif
26796 
26797     printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
26798 
26799 #ifndef HAVE_FIPS
26800     ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
26801 #else
26802     ret = wc_InitRng(&rng);
26803 #endif
26804 
26805     wc_InitCert(&cert);
26806 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
26807     if (ret == 0) { /*ED25519*/
26808         ret = wc_ed25519_init(&ed25519Key);
26809         if (ret == 0) {
26810             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
26811         }
26812         if (ret == 0) {
26813             ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
26814                                                   &ed25519Key);
26815         }
26816         wc_ed25519_free(&ed25519Key);
26817     }
26818 #endif
26819 #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
26820     if (ret == 0) { /*RSA*/
26821         ret = wc_InitRsaKey(&rsaKey, NULL);
26822         if (ret == 0) {
26823             MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
26824         }
26825         if (ret == 0) {
26826             ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
26827         }
26828         wc_FreeRsaKey(&rsaKey);
26829     }
26830 #endif
26831 #if defined(HAVE_ECC)
26832     if (ret == 0) { /*ECC*/
26833         ret = wc_ecc_init(&eccKey);
26834         if (ret == 0) {
26835             ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
26836         }
26837         if (ret == 0) {
26838             ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
26839         }
26840         wc_ecc_free(&eccKey);
26841     }
26842 #endif
26843 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
26844     if (ret == 0) { /*ED448*/
26845         ret = wc_ed448_init(&ed448Key);
26846         if (ret == 0) {
26847             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
26848         }
26849         if (ret == 0) {
26850             ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
26851                                                   &ed448Key);
26852         }
26853         wc_ed448_free(&ed448Key);
26854     }
26855 #endif
26856 
26857     printf(resultFmt, ret == 0 ? passed : failed);
26858 
26859     wc_FreeRng(&rng);
26860 #endif
26861     return ret;
26862 }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
26863 /*
26864  * Testing wc_SetAuthKeyIdFromPublicKey_ex
26865  */
26866 static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
26867 {
26868     int ret = 0;
26869 #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
26870     WC_RNG          rng;
26871     Cert            cert;
26872 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
26873     ed25519_key     ed25519Key;
26874 #endif
26875 #if !defined(NO_RSA) && defined(HAVE_RSA)
26876     RsaKey          rsaKey;
26877     int             bits = 2048;
26878 #endif
26879 #if defined(HAVE_ECC)
26880     ecc_key         eccKey;
26881 #endif
26882 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
26883     ed448_key       ed448Key;
26884 #endif
26885 
26886     printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
26887 
26888 #ifndef HAVE_FIPS
26889     ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
26890 #else
26891     ret = wc_InitRng(&rng);
26892 #endif
26893 
26894     wc_InitCert(&cert);
26895 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
26896     if (ret == 0) { /*ED25519*/
26897         ret = wc_ed25519_init(&ed25519Key);
26898         if (ret == 0) {
26899             ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
26900         }
26901         if (ret == 0) {
26902             ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
26903                                                   &ed25519Key);
26904         }
26905         wc_ed25519_free(&ed25519Key);
26906     }
26907 #endif
26908 #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
26909     if (ret == 0) { /*RSA*/
26910         ret = wc_InitRsaKey(&rsaKey, NULL);
26911         if (ret == 0) {
26912             MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
26913         }
26914         if (ret == 0) {
26915             ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
26916         }
26917         wc_FreeRsaKey(&rsaKey);
26918     }
26919 #endif
26920 #if defined(HAVE_ECC)
26921     if (ret == 0) { /*ECC*/
26922         ret = wc_ecc_init(&eccKey);
26923         if (ret == 0) {
26924             ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
26925         }
26926         if (ret == 0) {
26927             ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
26928         }
26929         wc_ecc_free(&eccKey);
26930     }
26931 #endif
26932 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
26933     if (ret == 0) { /*ED448*/
26934         ret = wc_ed448_init(&ed448Key);
26935         if (ret == 0) {
26936             ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
26937         }
26938         if (ret == 0) {
26939             ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
26940                                                   &ed448Key);
26941         }
26942         wc_ed448_free(&ed448Key);
26943     }
26944 #endif
26945 
26946     printf(resultFmt, ret == 0 ? passed : failed);
26947 
26948     wc_FreeRng(&rng);
26949 #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
26950     return ret;
26951 }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
26952 /*
26953  * Testing wc_PKCS7_New()
26954  */
26955 static void test_wc_PKCS7_New (void)
26956 {
26957 #if defined(HAVE_PKCS7)
26958     PKCS7*      pkcs7;
26959     void*       heap = NULL;
26960 
26961     printf(testingFmt, "wc_PKCS7_New()");
26962 
26963     pkcs7 = wc_PKCS7_New(heap, devId);
26964     AssertNotNull(pkcs7);
26965 
26966     printf(resultFmt, passed);
26967     wc_PKCS7_Free(pkcs7);
26968 #endif
26969 } /* END test-wc_PKCS7_New */
26970 
26971 /*
26972  * Testing wc_PKCS7_Init()
26973  */
26974 static void test_wc_PKCS7_Init (void)
26975 {
26976 #if defined(HAVE_PKCS7)
26977     PKCS7*      pkcs7;
26978     void*       heap = NULL;
26979 
26980     printf(testingFmt, "wc_PKCS7_Init()");
26981 
26982     pkcs7 = wc_PKCS7_New(heap, devId);
26983     AssertNotNull(pkcs7);
26984 
26985     AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
26986 
26987     /* Pass in bad args. */
26988     AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
26989 
26990     printf(resultFmt, passed);
26991     wc_PKCS7_Free(pkcs7);
26992 #endif
26993 } /* END test-wc_PKCS7_Init */
26994 
26995 
26996 /*
26997  * Testing wc_PKCS7_InitWithCert()
26998  */
26999 static void test_wc_PKCS7_InitWithCert (void)
27000 {
27001 #if defined(HAVE_PKCS7)
27002     PKCS7*       pkcs7;
27003 
27004 #ifndef NO_RSA
27005     #if defined(USE_CERT_BUFFERS_2048)
27006         unsigned char    cert[sizeof(client_cert_der_2048)];
27007         int              certSz = (int)sizeof(cert);
27008         XMEMSET(cert, 0, certSz);
27009         XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
27010     #elif defined(USE_CERT_BUFFERS_1024)
27011         unsigned char    cert[sizeof(client_cert_der_1024)];
27012         int              certSz = (int)sizeof(cert);
27013         XMEMSET(cert, 0, certSz);
27014         XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
27015     #else
27016         unsigned char   cert[ONEK_BUF];
27017         XFILE           fp;
27018         int             certSz;
27019         fp = XFOPEN("./certs/1024/client-cert.der", "rb");
27020         AssertTrue(fp != XBADFILE);
27021 
27022         certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
27023         XFCLOSE(fp);
27024     #endif
27025 #elif defined(HAVE_ECC)
27026     #if defined(USE_CERT_BUFFERS_256)
27027         unsigned char    cert[sizeof(cliecc_cert_der_256)];
27028         int              certSz = (int)sizeof(cert);
27029         XMEMSET(cert, 0, certSz);
27030         XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
27031     #else
27032         unsigned char   cert[ONEK_BUF];
27033         XFILE           fp;
27034         int             certSz;
27035         fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
27036 
27037         AssertTrue(fp != XBADFILE);
27038 
27039         certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
27040         XFCLOSE(fp);
27041     #endif
27042 #else
27043         #error PKCS7 requires ECC or RSA
27044 #endif
27045 
27046 #ifdef HAVE_ECC
27047     /* bad test case from ZD 11011, malformed cert gives bad ECC key */
27048     unsigned char certWithInvalidEccKey[] = {
27049         0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
27050         0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
27051         0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
27052         0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
27053         0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
27054         0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
27055         0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
27056         0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
27057         0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
27058         0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
27059         0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
27060         0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
27061         0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
27062         0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
27063         0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
27064         0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
27065         0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
27066         0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
27067         0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
27068         0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
27069         0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
27070         0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
27071         0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
27072         0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
27073         0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
27074         0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
27075         0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
27076         0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
27077         0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
27078         0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
27079         0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
27080         0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
27081         0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
27082         0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
27083         0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
27084         0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
27085         0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
27086         0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
27087         0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
27088         0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
27089         0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
27090         0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
27091         0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
27092         0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
27093         0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
27094         0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
27095         0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
27096         0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
27097         0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
27098         0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
27099         0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
27100         0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
27101         0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
27102         0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
27103         0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
27104         0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
27105         0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
27106         0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
27107         0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
27108         0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
27109         0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
27110         0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
27111         0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
27112         0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
27113         0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
27114         0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
27115         0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
27116         0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
27117         0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
27118         0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
27119         0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
27120         0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
27121         0x08, 0xA8, 0xB4};
27122 #endif
27123     printf(testingFmt, "wc_PKCS7_InitWithCert()");
27124 
27125     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27126     /* If initialization is not successful, it's free'd in init func. */
27127     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
27128 
27129     wc_PKCS7_Free(pkcs7);
27130     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27131 
27132     /* Valid initialization usage. */
27133     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27134 
27135     /* Pass in bad args. No need free for null checks, free at end.*/
27136     AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
27137                                                            BAD_FUNC_ARG);
27138     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
27139                                                       BAD_FUNC_ARG);
27140 
27141 #ifdef HAVE_ECC
27142     AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
27143                                      sizeof(certWithInvalidEccKey)), 0);
27144 #endif
27145 
27146     printf(resultFmt, passed);
27147 
27148     wc_PKCS7_Free(pkcs7);
27149 #endif
27150 } /* END test_wc_PKCS7_InitWithCert */
27151 
27152 
27153 /*
27154  * Testing wc_PKCS7_EncodeData()
27155  */
27156 static void test_wc_PKCS7_EncodeData (void)
27157 {
27158 #if defined(HAVE_PKCS7)
27159     PKCS7*      pkcs7;
27160     byte        output[FOURK_BUF];
27161     byte        data[] = "My encoded DER cert.";
27162 
27163 #ifndef NO_RSA
27164     #if defined(USE_CERT_BUFFERS_2048)
27165         unsigned char cert[sizeof(client_cert_der_2048)];
27166         unsigned char key[sizeof(client_key_der_2048)];
27167         int certSz = (int)sizeof(cert);
27168         int keySz = (int)sizeof(key);
27169         XMEMSET(cert, 0, certSz);
27170         XMEMSET(key, 0, keySz);
27171         XMEMCPY(cert, client_cert_der_2048, certSz);
27172         XMEMCPY(key, client_key_der_2048, keySz);
27173 
27174     #elif defined(USE_CERT_BUFFERS_1024)
27175         unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
27176         unsigned char key[sizeof_client_key_der_1024];
27177         int certSz = (int)sizeof(cert);
27178         int keySz = (int)sizeof(key);
27179         XMEMSET(cert, 0, certSz);
27180         XMEMSET(key, 0, keySz);
27181         XMEMCPY(cert, client_cert_der_1024, certSz);
27182         XMEMCPY(key, client_key_der_1024, keySz);
27183     #else
27184         unsigned char   cert[ONEK_BUF];
27185         unsigned char   key[ONEK_BUF];
27186         XFILE           fp;
27187         int             certSz;
27188         int             keySz;
27189 
27190         fp = XFOPEN("./certs/1024/client-cert.der", "rb");
27191         AssertTrue(fp != XBADFILE);
27192         certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
27193         XFCLOSE(fp);
27194 
27195         fp = XFOPEN("./certs/1024/client-key.der", "rb");
27196         AssertTrue(fp != XBADFILE);
27197         keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
27198         XFCLOSE(fp);
27199     #endif
27200 #elif defined(HAVE_ECC)
27201     #if defined(USE_CERT_BUFFERS_256)
27202         unsigned char    cert[sizeof(cliecc_cert_der_256)];
27203         unsigned char    key[sizeof(ecc_clikey_der_256)];
27204         int              certSz = (int)sizeof(cert);
27205         int              keySz = (int)sizeof(key);
27206         XMEMSET(cert, 0, certSz);
27207         XMEMSET(key, 0, keySz);
27208         XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
27209         XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
27210     #else
27211         unsigned char   cert[ONEK_BUF];
27212         unsigned char   key[ONEK_BUF];
27213         XFILE           fp;
27214         int             certSz, keySz;
27215 
27216         fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
27217         AssertTrue(fp != XBADFILE);
27218         certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
27219         XFCLOSE(fp);
27220 
27221         fp = XFOPEN("./certs/client-ecc-key.der", "rb");
27222         AssertTrue(fp != XBADFILE);
27223         keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
27224         XFCLOSE(fp);
27225     #endif
27226 #endif
27227 
27228     XMEMSET(output, 0, sizeof(output));
27229 
27230     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27231     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27232 
27233     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
27234 
27235     printf(testingFmt, "wc_PKCS7_EncodeData()");
27236 
27237     pkcs7->content = data;
27238     pkcs7->contentSz = sizeof(data);
27239     pkcs7->privateKey = key;
27240     pkcs7->privateKeySz = keySz;
27241     AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
27242 
27243     /* Test bad args. */
27244     AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
27245                                                             BAD_FUNC_ARG);
27246     AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
27247                                                             BAD_FUNC_ARG);
27248     AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
27249 
27250     printf(resultFmt, passed);
27251 
27252     wc_PKCS7_Free(pkcs7);
27253 #endif
27254 }  /* END test_wc_PKCS7_EncodeData */
27255 
27256 
27257 #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
27258     !defined(NO_RSA) && !defined(NO_SHA256)
27259 /* RSA sign raw digest callback */
27260 static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
27261                               byte* out, word32 outSz, byte* privateKey,
27262                               word32 privateKeySz, int devid, int hashOID)
27263 {
27264     /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
27265     byte digInfoEncoding[] = {
27266         0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
27267         0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
27268         0x00, 0x04, 0x20
27269     };
27270 
27271     int ret;
27272     byte digestInfo[ONEK_BUF];
27273     byte sig[FOURK_BUF];
27274     word32 digestInfoSz = 0;
27275     word32 idx = 0;
27276     RsaKey rsa;
27277 
27278     /* SHA-256 required only for this example callback due to above
27279      * digInfoEncoding[] */
27280     if (pkcs7 == NULL || digest == NULL || out == NULL ||
27281         (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
27282         (hashOID != SHA256h)) {
27283         return -1;
27284     }
27285 
27286     /* build DigestInfo */
27287     XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
27288     digestInfoSz += sizeof(digInfoEncoding);
27289     XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
27290     digestInfoSz += digestSz;
27291 
27292     /* set up RSA key */
27293     ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
27294     if (ret != 0) {
27295         return ret;
27296     }
27297 
27298     ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
27299 
27300     /* sign DigestInfo */
27301     if (ret == 0) {
27302         ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
27303                              &rsa, pkcs7->rng);
27304         if (ret > 0) {
27305             if (ret > (int)outSz) {
27306                 /* output buffer too small */
27307                 ret = -1;
27308             } else {
27309                 /* success, ret holds sig size */
27310                 XMEMCPY(out, sig, ret);
27311             }
27312         }
27313     }
27314 
27315     wc_FreeRsaKey(&rsa);
27316 
27317     return ret;
27318 }
27319 #endif
27320 
27321 
27322 /*
27323  * Testing wc_PKCS7_EncodeSignedData()
27324  */
27325 static void test_wc_PKCS7_EncodeSignedData(void)
27326 {
27327 #if defined(HAVE_PKCS7)
27328     PKCS7*      pkcs7;
27329     WC_RNG      rng;
27330     byte        output[FOURK_BUF];
27331     byte        badOut[1];
27332     word32      outputSz = (word32)sizeof(output);
27333     word32      badOutSz = 0;
27334     byte        data[] = "Test data to encode.";
27335 
27336 #ifndef NO_RSA
27337     #if defined(USE_CERT_BUFFERS_2048)
27338         byte        key[sizeof(client_key_der_2048)];
27339         byte        cert[sizeof(client_cert_der_2048)];
27340         word32      keySz = (word32)sizeof(key);
27341         word32      certSz = (word32)sizeof(cert);
27342         XMEMSET(key, 0, keySz);
27343         XMEMSET(cert, 0, certSz);
27344         XMEMCPY(key, client_key_der_2048, keySz);
27345         XMEMCPY(cert, client_cert_der_2048, certSz);
27346     #elif defined(USE_CERT_BUFFERS_1024)
27347         byte        key[sizeof_client_key_der_1024];
27348         byte        cert[sizeof(sizeof_client_cert_der_1024)];
27349         word32      keySz = (word32)sizeof(key);
27350         word32      certSz = (word32)sizeof(cert);
27351         XMEMSET(key, 0, keySz);
27352         XMEMSET(cert, 0, certSz);
27353         XMEMCPY(key, client_key_der_1024, keySz);
27354         XMEMCPY(cert, client_cert_der_1024, certSz);
27355     #else
27356         unsigned char   cert[ONEK_BUF];
27357         unsigned char   key[ONEK_BUF];
27358         XFILE           fp;
27359         int             certSz;
27360         int             keySz;
27361 
27362         fp = XFOPEN("./certs/1024/client-cert.der", "rb");
27363         AssertTrue(fp != XBADFILE);
27364         certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
27365         XFCLOSE(fp);
27366 
27367         fp = XFOPEN("./certs/1024/client-key.der", "rb");
27368         AssertTrue(fp != XBADFILE);
27369         keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
27370         XFCLOSE(fp);
27371     #endif
27372 #elif defined(HAVE_ECC)
27373     #if defined(USE_CERT_BUFFERS_256)
27374         unsigned char    cert[sizeof(cliecc_cert_der_256)];
27375         unsigned char    key[sizeof(ecc_clikey_der_256)];
27376         int              certSz = (int)sizeof(cert);
27377         int              keySz = (int)sizeof(key);
27378         XMEMSET(cert, 0, certSz);
27379         XMEMSET(key, 0, keySz);
27380         XMEMCPY(cert, cliecc_cert_der_256, certSz);
27381         XMEMCPY(key, ecc_clikey_der_256, keySz);
27382     #else
27383         unsigned char   cert[ONEK_BUF];
27384         unsigned char   key[ONEK_BUF];
27385         XFILE           fp;
27386         int             certSz, keySz;
27387 
27388         fp = XOPEN("./certs/client-ecc-cert.der", "rb");
27389         AssertTrue(fp != XBADFILE);
27390         certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
27391         XFCLOSE(fp);
27392 
27393         fp = XFOPEN("./certs/client-ecc-key.der", "rb");
27394         AssertTrue(fp != XBADFILE);
27395         keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
27396         XFCLOSE(fp);
27397     #endif
27398 #endif
27399 
27400     XMEMSET(output, 0, outputSz);
27401     AssertIntEQ(wc_InitRng(&rng), 0);
27402 
27403     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27404     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27405 
27406     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
27407 
27408     printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
27409 
27410     pkcs7->content = data;
27411     pkcs7->contentSz = (word32)sizeof(data);
27412     pkcs7->privateKey = key;
27413     pkcs7->privateKeySz = (word32)sizeof(key);
27414     pkcs7->encryptOID = RSAk;
27415     pkcs7->hashOID = SHAh;
27416     pkcs7->rng = &rng;
27417 
27418     AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
27419 
27420     wc_PKCS7_Free(pkcs7);
27421     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27422     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27423     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
27424 
27425     /* Pass in bad args. */
27426     AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
27427     AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
27428     AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
27429                                 badOutSz), BAD_FUNC_ARG);
27430     pkcs7->hashOID = 0; /* bad hashOID */
27431     AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
27432 
27433 #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
27434     !defined(NO_RSA) && !defined(NO_SHA256)
27435     /* test RSA sign raw digest callback, if using RSA and compiled in.
27436      * Example callback assumes SHA-256, so only run test if compiled in. */
27437     wc_PKCS7_Free(pkcs7);
27438     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27439     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
27440 
27441     pkcs7->content = data;
27442     pkcs7->contentSz = (word32)sizeof(data);
27443     pkcs7->privateKey = key;
27444     pkcs7->privateKeySz = (word32)sizeof(key);
27445     pkcs7->encryptOID = RSAk;
27446     pkcs7->hashOID = SHA256h;
27447     pkcs7->rng = &rng;
27448 
27449     AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
27450 
27451     AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
27452 #endif
27453 
27454     printf(resultFmt, passed);
27455 
27456     wc_PKCS7_Free(pkcs7);
27457     wc_FreeRng(&rng);
27458 
27459 #endif
27460 } /* END test_wc_PKCS7_EncodeSignedData */
27461 
27462 
27463 /*
27464  * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
27465  */
27466 static void test_wc_PKCS7_EncodeSignedData_ex(void)
27467 {
27468 #if defined(HAVE_PKCS7)
27469     int         ret, i;
27470     PKCS7*      pkcs7;
27471     WC_RNG      rng;
27472     byte        outputHead[FOURK_BUF/2];
27473     byte        outputFoot[FOURK_BUF/2];
27474     word32      outputHeadSz = (word32)sizeof(outputHead);
27475     word32      outputFootSz = (word32)sizeof(outputFoot);
27476     byte        data[FOURK_BUF];
27477     wc_HashAlg  hash;
27478     enum wc_HashType hashType = WC_HASH_TYPE_SHA;
27479     byte        hashBuf[WC_MAX_DIGEST_SIZE];
27480     word32      hashSz = wc_HashGetDigestSize(hashType);
27481 
27482 #ifndef NO_RSA
27483     #if defined(USE_CERT_BUFFERS_2048)
27484         byte        key[sizeof(client_key_der_2048)];
27485         byte        cert[sizeof(client_cert_der_2048)];
27486         word32      keySz = (word32)sizeof(key);
27487         word32      certSz = (word32)sizeof(cert);
27488         XMEMSET(key, 0, keySz);
27489         XMEMSET(cert, 0, certSz);
27490         XMEMCPY(key, client_key_der_2048, keySz);
27491         XMEMCPY(cert, client_cert_der_2048, certSz);
27492     #elif defined(USE_CERT_BUFFERS_1024)
27493         byte        key[sizeof_client_key_der_1024];
27494         byte        cert[sizeof(sizeof_client_cert_der_1024)];
27495         word32      keySz = (word32)sizeof(key);
27496         word32      certSz = (word32)sizeof(cert);
27497         XMEMSET(key, 0, keySz);
27498         XMEMSET(cert, 0, certSz);
27499         XMEMCPY(key, client_key_der_1024, keySz);
27500         XMEMCPY(cert, client_cert_der_1024, certSz);
27501     #else
27502         unsigned char   cert[ONEK_BUF];
27503         unsigned char   key[ONEK_BUF];
27504         XFILE           fp;
27505         int             certSz;
27506         int             keySz;
27507 
27508         fp = XFOPEN("./certs/1024/client-cert.der", "rb");
27509         AssertTrue((fp != XBADFILE));
27510         certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
27511         XFCLOSE(fp);
27512 
27513         fp = XFOPEN("./certs/1024/client-key.der", "rb");
27514         AssertTrue(fp != XBADFILE);
27515         keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
27516         XFCLOSE(fp);
27517     #endif
27518 #elif defined(HAVE_ECC)
27519     #if defined(USE_CERT_BUFFERS_256)
27520         unsigned char    cert[sizeof(cliecc_cert_der_256)];
27521         unsigned char    key[sizeof(ecc_clikey_der_256)];
27522         int              certSz = (int)sizeof(cert);
27523         int              keySz = (int)sizeof(key);
27524         XMEMSET(cert, 0, certSz);
27525         XMEMSET(key, 0, keySz);
27526         XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
27527         XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
27528     #else
27529         unsigned char   cert[ONEK_BUF];
27530         unsigned char   key[ONEK_BUF];
27531         XFILE           fp;
27532         int             certSz, keySz;
27533 
27534         fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
27535         AssertTrue(fp != XBADFILE);
27536         certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
27537         XFCLOSE(fp);
27538 
27539         fp = XFOPEN("./certs/client-ecc-key.der", "rb");
27540         AssertTrue(fp != XBADFILE);
27541         keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
27542         XFCLOSE(fp);
27543     #endif
27544 #endif
27545 
27546     /* initialize large data with sequence */
27547     for (i=0; i<(int)sizeof(data); i++)
27548         data[i] = i & 0xff;
27549 
27550     XMEMSET(outputHead, 0, outputHeadSz);
27551     XMEMSET(outputFoot, 0, outputFootSz);
27552     AssertIntEQ(wc_InitRng(&rng), 0);
27553 
27554     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27555     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27556 
27557     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
27558 
27559     printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
27560 
27561     pkcs7->content = NULL; /* not used for ex */
27562     pkcs7->contentSz = (word32)sizeof(data);
27563     pkcs7->privateKey = key;
27564     pkcs7->privateKeySz = (word32)sizeof(key);
27565     pkcs7->encryptOID = RSAk;
27566     pkcs7->hashOID = SHAh;
27567     pkcs7->rng = &rng;
27568 
27569     /* calculate hash for content */
27570     ret = wc_HashInit(&hash, hashType);
27571     if (ret == 0) {
27572         ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
27573         if (ret == 0) {
27574             ret = wc_HashFinal(&hash, hashType, hashBuf);
27575         }
27576         wc_HashFree(&hash, hashType);
27577     }
27578     AssertIntEQ(ret, 0);
27579 
27580     /* Perform PKCS7 sign using hash directly */
27581     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
27582         outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
27583     AssertIntGT(outputHeadSz, 0);
27584     AssertIntGT(outputFootSz, 0);
27585 
27586     wc_PKCS7_Free(pkcs7);
27587     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27588     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27589 
27590     /* required parameter even on verify when using _ex, if using outputHead
27591      * and outputFoot */
27592     pkcs7->contentSz = (word32)sizeof(data);
27593     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27594         outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
27595 
27596     wc_PKCS7_Free(pkcs7);
27597 
27598     /* assembly complete PKCS7 sign and use normal verify */
27599     {
27600         byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
27601         word32 outputSz = 0;
27602         AssertNotNull(output);
27603         XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
27604         outputSz += outputHeadSz;
27605         XMEMCPY(&output[outputSz], data, sizeof(data));
27606         outputSz += sizeof(data);
27607         XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
27608         outputSz += outputFootSz;
27609 
27610         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27611         AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27612         AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
27613         XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
27614     }
27615 
27616     /* Pass in bad args. */
27617     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
27618         &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27619     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
27620         &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27621     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
27622         &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27623     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
27624         &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27625     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
27626         outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27627     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
27628         outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
27629     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
27630         outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
27631     pkcs7->hashOID = 0; /* bad hashOID */
27632     AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
27633         outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
27634 
27635     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
27636         outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
27637 
27638     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
27639         outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
27640 #ifndef NO_PKCS7_STREAM
27641     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
27642         outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
27643 #else
27644     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
27645         outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
27646 #endif
27647     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
27648         outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
27649 #ifndef NO_PKCS7_STREAM
27650     /* can pass in 0 buffer length with streaming API */
27651     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27652         outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
27653 #else
27654     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27655         outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
27656 #endif
27657     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27658         outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
27659 #ifndef NO_PKCS7_STREAM
27660     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27661         outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
27662 #else
27663     AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27664         outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
27665 #endif
27666 
27667     printf(resultFmt, passed);
27668 
27669     wc_PKCS7_Free(pkcs7);
27670     wc_FreeRng(&rng);
27671 
27672 #endif
27673 } /* END test_wc_PKCS7_EncodeSignedData_ex */
27674 
27675 
27676 #if defined(HAVE_PKCS7)
27677 static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
27678                                  byte* data, word32 dataSz,
27679                                  int withAttribs, int detachedSig)
27680 {
27681     PKCS7*      pkcs7;
27682     WC_RNG      rng;
27683 
27684     static byte messageTypeOid[] =
27685                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
27686                  0x09, 0x02 };
27687     static byte messageType[] = { 0x13, 2, '1', '9' };
27688 
27689     PKCS7Attrib attribs[] =
27690     {
27691         { messageTypeOid, sizeof(messageTypeOid), messageType,
27692                                        sizeof(messageType) }
27693     };
27694 
27695 #ifndef NO_RSA
27696     #if defined(USE_CERT_BUFFERS_2048)
27697         byte        key[sizeof(client_key_der_2048)];
27698         byte        cert[sizeof(client_cert_der_2048)];
27699         word32      keySz = (word32)sizeof(key);
27700         word32      certSz = (word32)sizeof(cert);
27701         XMEMSET(key, 0, keySz);
27702         XMEMSET(cert, 0, certSz);
27703         XMEMCPY(key, client_key_der_2048, keySz);
27704         XMEMCPY(cert, client_cert_der_2048, certSz);
27705     #elif defined(USE_CERT_BUFFERS_1024)
27706         byte        key[sizeof_client_key_der_1024];
27707         byte        cert[sizeof(sizeof_client_cert_der_1024)];
27708         word32      keySz = (word32)sizeof(key);
27709         word32      certSz = (word32)sizeof(cert);
27710         XMEMSET(key, 0, keySz);
27711         XMEMSET(cert, 0, certSz);
27712         XMEMCPY(key, client_key_der_1024, keySz);
27713         XMEMCPY(cert, client_cert_der_1024, certSz);
27714     #else
27715         unsigned char   cert[ONEK_BUF];
27716         unsigned char   key[ONEK_BUF];
27717         FILE*           fp;
27718         int             certSz;
27719         int             keySz;
27720 
27721         fp = fopen("./certs/1024/client-cert.der", "rb");
27722         AssertNotNull(fp);
27723         certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
27724         fclose(fp);
27725 
27726         fp = fopen("./certs/1024/client-key.der", "rb");
27727         AssertNotNull(fp);
27728         keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
27729         fclose(fp);
27730     #endif
27731 #elif defined(HAVE_ECC)
27732     #if defined(USE_CERT_BUFFERS_256)
27733         unsigned char    cert[sizeof(cliecc_cert_der_256)];
27734         unsigned char    key[sizeof(ecc_clikey_der_256)];
27735         int              certSz = (int)sizeof(cert);
27736         int              keySz = (int)sizeof(key);
27737         XMEMSET(cert, 0, certSz);
27738         XMEMSET(key, 0, keySz);
27739         XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
27740         XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
27741     #else
27742         unsigned char   cert[ONEK_BUF];
27743         unsigned char   key[ONEK_BUF];
27744         FILE*           fp;
27745         int             certSz, keySz;
27746 
27747         fp = fopen("./certs/client-ecc-cert.der", "rb");
27748         AssertNotNull(fp);
27749         certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
27750         fclose(fp);
27751 
27752         fp = fopen("./certs/client-ecc-key.der", "rb");
27753         AssertNotNull(fp);
27754         keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
27755         fclose(fp);
27756     #endif
27757 #endif
27758 
27759     XMEMSET(output, 0, outputSz);
27760     AssertIntEQ(wc_InitRng(&rng), 0);
27761 
27762     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27763     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27764 
27765     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
27766 
27767     printf(testingFmt, "wc_PKCS7_VerifySignedData()");
27768 
27769     pkcs7->content = data;
27770     pkcs7->contentSz = dataSz;
27771     pkcs7->privateKey = key;
27772     pkcs7->privateKeySz = (word32)sizeof(key);
27773     pkcs7->encryptOID = RSAk;
27774     pkcs7->hashOID = SHAh;
27775     pkcs7->rng = &rng;
27776     if (withAttribs) {
27777         /* include a signed attribute */
27778         pkcs7->signedAttribs   = attribs;
27779         pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
27780     }
27781 
27782     if (detachedSig) {
27783         AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
27784     }
27785 
27786     AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
27787     wc_PKCS7_Free(pkcs7);
27788     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27789     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27790     if (detachedSig) {
27791         pkcs7->content = data;
27792         pkcs7->contentSz = dataSz;
27793     }
27794     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
27795 
27796     wc_PKCS7_Free(pkcs7);
27797     wc_FreeRng(&rng);
27798 
27799     return outputSz;
27800 }
27801 #endif
27802 
27803 /*
27804  * Testing wc_PKCS_VerifySignedData()
27805  */
27806 static void test_wc_PKCS7_VerifySignedData(void)
27807 {
27808 #if defined(HAVE_PKCS7)
27809     PKCS7* pkcs7;
27810     byte   output[FOURK_BUF];
27811     word32 outputSz = sizeof(output);
27812     byte   data[] = "Test data to encode.";
27813     byte   badOut[1];
27814     word32 badOutSz = 0;
27815     byte   badContent[] = "This is different content than was signed";
27816 
27817     int ret;
27818     wc_HashAlg  hash;
27819     enum wc_HashType hashType = WC_HASH_TYPE_SHA;
27820     byte        hashBuf[WC_MAX_DIGEST_SIZE];
27821     word32      hashSz = wc_HashGetDigestSize(hashType);
27822 
27823     AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
27824                                                   (word32)sizeof(data),
27825                                                   0, 0)), 0);
27826 
27827     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27828     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
27829     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27830     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
27831 
27832     /* Test bad args. */
27833     AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
27834     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
27835 #ifndef NO_PKCS7_STREAM
27836     /* can pass in 0 buffer length with streaming API */
27837     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
27838                                 badOutSz), WC_PKCS7_WANT_READ_E);
27839 #else
27840     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
27841                                 badOutSz), BAD_FUNC_ARG);
27842 #endif
27843 
27844     wc_PKCS7_Free(pkcs7);
27845 
27846     /* Invalid content should error, use detached signature so we can
27847      * easily change content */
27848     AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
27849                                                   (word32)sizeof(data),
27850                                                   1, 1)), 0);
27851     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27852     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27853     pkcs7->content = badContent;
27854     pkcs7->contentSz = sizeof(badContent);
27855     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
27856     wc_PKCS7_Free(pkcs7);
27857 
27858     /* Test success case with detached signature and valid content */
27859     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27860     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
27861     pkcs7->content = data;
27862     pkcs7->contentSz = sizeof(data);
27863     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
27864     wc_PKCS7_Free(pkcs7);
27865 
27866     /* verify using pre-computed content digest only (no content) */
27867     {
27868         /* calculate hash for content */
27869         ret = wc_HashInit(&hash, hashType);
27870         if (ret == 0) {
27871             ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
27872             if (ret == 0) {
27873                 ret = wc_HashFinal(&hash, hashType, hashBuf);
27874             }
27875             wc_HashFree(&hash, hashType);
27876         }
27877         AssertIntEQ(ret, 0);
27878 
27879         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
27880         AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
27881         AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
27882                                                  output, outputSz,
27883                                                  NULL, 0), 0);
27884         wc_PKCS7_Free(pkcs7);
27885     }
27886 
27887     printf(resultFmt, passed);
27888 #endif
27889 } /* END test_wc_PKCS7_VerifySignedData() */
27890 
27891 
27892 #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
27893     !defined(NO_AES_256)
27894 static const byte defKey[] = {
27895     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
27896     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
27897     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
27898     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
27899 };
27900 static byte aesHandle[32]; /* simulated hardware key handle */
27901 
27902 /* return 0 on success */
27903 static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
27904         byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
27905         byte* in, int inSz, byte* out, void* usrCtx)
27906 {
27907     int ret;
27908     Aes aes;
27909 
27910     if (usrCtx == NULL) {
27911         /* no simulated handle passed in */
27912         return -1;
27913     }
27914 
27915     switch (encryptOID) {
27916         case AES256CBCb:
27917             if (ivSz  != AES_BLOCK_SIZE)
27918                 return BAD_FUNC_ARG;
27919             break;
27920 
27921         default:
27922             WOLFSSL_MSG("Unsupported content cipher type for test");
27923             return ALGO_ID_E;
27924     };
27925 
27926     /* simulate using handle to get key */
27927     ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
27928     if (ret == 0) {
27929         ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
27930         if (ret == 0)
27931             ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
27932         wc_AesFree(&aes);
27933     }
27934 
27935     (void)aad;
27936     (void)aadSz;
27937     (void)authTag;
27938     (void)authTagSz;
27939     (void)pkcs7;
27940     return ret;
27941 }
27942 
27943 
27944 /* returns key size on success */
27945 static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
27946         word32 keyIdSz, byte* orginKey, word32 orginKeySz,
27947         byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
27948 {
27949     int ret = -1;
27950 
27951     if (out == NULL)
27952         return BAD_FUNC_ARG;
27953 
27954     if (keyId[0] != 0x00) {
27955         return -1;
27956     }
27957 
27958     if (type != (int)PKCS7_KEKRI) {
27959         return -1;
27960     }
27961 
27962     switch (keyWrapAlgo) {
27963         case AES256_WRAP:
27964             /* simulate setting a handle for later decryption but use key
27965              * as handle in the test case here */
27966             ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
27967                                       aesHandle, sizeof(aesHandle), NULL);
27968             if (ret < 0)
27969                 return ret;
27970 
27971             ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
27972             if (ret < 0)
27973                 return ret;
27974 
27975             /* return key size on success */
27976             return sizeof(defKey);
27977 
27978         default:
27979             WOLFSSL_MSG("Unsupported key wrap algorithm in example");
27980             return BAD_KEYWRAP_ALG_E;
27981     };
27982 
27983     (void)cekSz;
27984     (void)cek;
27985     (void)outSz;
27986     (void)keyIdSz;
27987     (void)direction;
27988     (void)orginKey; /* used with KAKRI */
27989     (void)orginKeySz;
27990     return ret;
27991 }
27992 #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
27993 
27994 
27995 /*
27996  * Testing wc_PKCS7_EncodeEnvelopedData()
27997  */
27998 static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
27999 {
28000 #if defined(HAVE_PKCS7)
28001     PKCS7*      pkcs7;
28002 #ifdef ECC_TIMING_RESISTANT
28003     WC_RNG      rng;
28004 #endif
28005     word32      tempWrd32   = 0;
28006     byte*       tmpBytePtr = NULL;
28007     const char  input[] = "Test data to encode.";
28008     int         i;
28009     int         testSz = 0;
28010     #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
28011         !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
28012 
28013         byte*   rsaCert     = NULL;
28014         byte*   rsaPrivKey  = NULL;
28015         word32  rsaCertSz;
28016         word32  rsaPrivKeySz;
28017         #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
28018                                            !defined(USE_CERT_BUFFERS_2048) )
28019             static const char* rsaClientCert = "./certs/client-cert.der";
28020             static const char* rsaClientKey = "./certs/client-key.der";
28021             rsaCertSz = (word32)sizeof(rsaClientCert);
28022             rsaPrivKeySz = (word32)sizeof(rsaClientKey);
28023         #endif
28024     #endif
28025     #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
28026         !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
28027 
28028         byte*   eccCert     = NULL;
28029         byte*   eccPrivKey  = NULL;
28030         word32  eccCertSz;
28031         word32  eccPrivKeySz;
28032         #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
28033             static const char* eccClientCert = "./certs/client-ecc-cert.der";
28034             static const char* eccClientKey = "./certs/ecc-client-key.der";
28035         #endif
28036     #endif
28037     /* Generic buffer size. */
28038     byte    output[ONEK_BUF];
28039     byte    decoded[sizeof(input)/sizeof(char)];
28040     int     decodedSz = 0;
28041 #ifndef NO_FILESYSTEM
28042     XFILE certFile;
28043     XFILE keyFile;
28044 #endif
28045 
28046 #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
28047     !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
28048     /* RSA certs and keys. */
28049     #if defined(USE_CERT_BUFFERS_1024)
28050         /* Allocate buffer space. */
28051         AssertNotNull(rsaCert =
28052                 (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28053         /* Init buffer. */
28054         rsaCertSz = (word32)sizeof_client_cert_der_1024;
28055         XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
28056         AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
28057                                 DYNAMIC_TYPE_TMP_BUFFER));
28058         rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
28059         XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
28060 
28061     #elif defined(USE_CERT_BUFFERS_2048)
28062         /* Allocate buffer */
28063         AssertNotNull(rsaCert =
28064                 (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28065         /* Init buffer. */
28066         rsaCertSz = (word32)sizeof_client_cert_der_2048;
28067         XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
28068         AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
28069                                 DYNAMIC_TYPE_TMP_BUFFER));
28070         rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
28071         XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
28072 
28073     #else
28074         /* File system. */
28075         certFile = XFOPEN(rsaClientCert, "rb");
28076         AssertTrue(certFile != XBADFILE);
28077         rsaCertSz = (word32)FOURK_BUF;
28078         AssertNotNull(rsaCert =
28079                 (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28080         rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
28081         XFCLOSE(certFile);
28082         keyFile = XFOPEN(rsaClientKey, "rb");
28083         AssertTrue(keyFile != XBADFILE);
28084         AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
28085                                 DYNAMIC_TYPE_TMP_BUFFER));
28086         rsaPrivKeySz = (word32)FOURK_BUF;
28087         rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
28088         XFCLOSE(keyFile);
28089     #endif /* USE_CERT_BUFFERS */
28090 #endif /* NO_RSA */
28091 
28092 /* ECC */
28093 #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
28094     !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
28095 
28096     #ifdef USE_CERT_BUFFERS_256
28097         AssertNotNull(eccCert =
28098                 (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28099         /* Init buffer. */
28100         eccCertSz = (word32)sizeof_cliecc_cert_der_256;
28101         XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
28102         AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
28103                                 DYNAMIC_TYPE_TMP_BUFFER));
28104         eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
28105         XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
28106     #else /* File system. */
28107         certFile = XFOPEN(eccClientCert, "rb");
28108         AssertTrue(certFile != XBADFILE);
28109         eccCertSz = (word32)FOURK_BUF;
28110         AssertNotNull(eccCert =
28111                 (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28112         eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
28113         XFCLOSE(certFile);
28114         keyFile = XFOPEN(eccClientKey, "rb");
28115         AssertTrue(keyFile != XBADFILE);
28116         eccPrivKeySz = (word32)FOURK_BUF;
28117         AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
28118                                 DYNAMIC_TYPE_TMP_BUFFER));
28119         eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
28120         XFCLOSE(keyFile);
28121     #endif /* USE_CERT_BUFFERS_256 */
28122 #endif /* END HAVE_ECC */
28123 
28124     /* Silence. */
28125     (void)keyFile;
28126     (void)certFile;
28127 
28128     const pkcs7EnvelopedVector testVectors[] = {
28129     /* DATA is a global variable defined in the makefile. */
28130 #if !defined(NO_RSA)
28131     #ifndef NO_DES3
28132         {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
28133             rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
28134     #endif /* NO_DES3 */
28135     #if !defined(NO_AES) && defined(HAVE_AES_CBC)
28136         #ifndef NO_AES_128
28137         {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
28138             0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
28139         #endif
28140         #ifndef NO_AES_192
28141         {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
28142             0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
28143         #endif
28144         #ifndef NO_AES_256
28145         {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
28146             0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
28147         #endif
28148     #endif /* NO_AES && HAVE_AES_CBC */
28149 
28150 #endif /* NO_RSA */
28151 #if defined(HAVE_ECC)
28152     #if !defined(NO_AES) && defined(HAVE_AES_CBC)
28153         #if !defined(NO_SHA) && !defined(NO_AES_128)
28154             {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
28155                 AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
28156                 eccCertSz, eccPrivKey, eccPrivKeySz},
28157         #endif
28158         #if !defined(NO_SHA256) && !defined(NO_AES_256)
28159             {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
28160                 AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
28161                 eccCertSz, eccPrivKey, eccPrivKeySz},
28162         #endif
28163         #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
28164             {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
28165                 AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
28166                 eccCertSz, eccPrivKey, eccPrivKeySz},
28167         #endif
28168     #endif /* NO_AES && HAVE_AES_CBC*/
28169 #endif /* END HAVE_ECC */
28170     }; /* END pkcs7EnvelopedVector */
28171 
28172 #ifdef ECC_TIMING_RESISTANT
28173     AssertIntEQ(wc_InitRng(&rng), 0);
28174 #endif
28175 
28176     printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
28177 
28178     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28179     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
28180 
28181     testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
28182     for (i = 0; i < testSz; i++) {
28183         AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
28184                                     (word32)(testVectors + i)->certSz), 0);
28185 #ifdef ECC_TIMING_RESISTANT
28186         pkcs7->rng = &rng;
28187 #endif
28188 
28189         pkcs7->content       = (byte*)(testVectors + i)->content;
28190         pkcs7->contentSz     = (testVectors + i)->contentSz;
28191         pkcs7->contentOID    = (testVectors + i)->contentOID;
28192         pkcs7->encryptOID    = (testVectors + i)->encryptOID;
28193         pkcs7->keyWrapOID    = (testVectors + i)->keyWrapOID;
28194         pkcs7->keyAgreeOID   = (testVectors + i)->keyAgreeOID;
28195         pkcs7->privateKey    = (testVectors + i)->privateKey;
28196         pkcs7->privateKeySz  = (testVectors + i)->privateKeySz;
28197 
28198         AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
28199                             (word32)sizeof(output)), 0);
28200 
28201         decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28202                 (word32)sizeof(output), decoded, (word32)sizeof(decoded));
28203         AssertIntGE(decodedSz, 0);
28204         /* Verify the size of each buffer. */
28205         AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
28206         /* Don't free the last time through the loop. */
28207         if (i < testSz - 1 ){
28208             wc_PKCS7_Free(pkcs7);
28209             AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28210         }
28211     }  /* END test loop. */
28212 
28213     /* Test bad args. */
28214     AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
28215                     (word32)sizeof(output)), BAD_FUNC_ARG);
28216     AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
28217                     (word32)sizeof(output)), BAD_FUNC_ARG);
28218     AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
28219     printf(resultFmt, passed);
28220 
28221     /* Decode.  */
28222     printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
28223 
28224     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
28225         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28226     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28227         (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28228     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28229         (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
28230     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
28231         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28232     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
28233         (word32)sizeof(decoded)), BAD_FUNC_ARG);
28234     /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
28235 #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
28236     /* only a failure for KARI test cases */
28237     tempWrd32 = pkcs7->singleCertSz;
28238     pkcs7->singleCertSz = 0;
28239     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28240         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28241     pkcs7->singleCertSz = tempWrd32;
28242 
28243     tmpBytePtr = pkcs7->singleCert;
28244     pkcs7->singleCert = NULL;
28245     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28246         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28247     pkcs7->singleCert = tmpBytePtr;
28248 #endif
28249     tempWrd32 = pkcs7->privateKeySz;
28250     pkcs7->privateKeySz = 0;
28251 
28252     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28253         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28254     pkcs7->privateKeySz = tempWrd32;
28255 
28256     tmpBytePtr = pkcs7->privateKey;
28257     pkcs7->privateKey = NULL;
28258     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28259         (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
28260     pkcs7->privateKey = tmpBytePtr;
28261 
28262     wc_PKCS7_Free(pkcs7);
28263 
28264 #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
28265     /* test of decrypt callback with KEKRI enveloped data */
28266     {
28267         int envelopedSz;
28268         const byte keyId[] = { 0x00 };
28269 
28270         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28271         pkcs7->content      = (byte*)input;
28272         pkcs7->contentSz    = (word32)(sizeof(input)/sizeof(char));
28273         pkcs7->contentOID   = DATA;
28274         pkcs7->encryptOID   = AES256CBCb;
28275         AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
28276                     (byte*)defKey, sizeof(defKey), (byte*)keyId,
28277                     sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
28278         AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
28279         AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
28280                         (word32)sizeof(output))), 0);
28281         wc_PKCS7_Free(pkcs7);
28282 
28283         /* decode envelopedData */
28284         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28285         AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
28286         AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
28287         AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
28288                         envelopedSz, decoded, sizeof(decoded))), 0);
28289         wc_PKCS7_Free(pkcs7);
28290     }
28291 #endif /* !NO_AES && !NO_AES_256 */
28292 
28293 
28294     printf(resultFmt, passed);
28295 #ifndef NO_RSA
28296     if (rsaCert) {
28297         XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28298     }
28299     if (rsaPrivKey) {
28300         XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28301     }
28302 #endif /*NO_RSA */
28303 #ifdef HAVE_ECC
28304     if (eccCert) {
28305         XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28306     }
28307     if (eccPrivKey) {
28308         XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28309     }
28310 #endif /* HAVE_ECC */
28311 
28312 #ifdef ECC_TIMING_RESISTANT
28313     wc_FreeRng(&rng);
28314 #endif
28315 
28316 #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3)
28317     {
28318         byte   out[7];
28319         byte   *cms;
28320         word32 cmsSz;
28321         XFILE  cmsFile;
28322 
28323         XMEMSET(out, 0, sizeof(out));
28324         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28325         cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
28326         AssertTrue(cmsFile != XBADFILE);
28327         cmsSz = (word32)FOURK_BUF;
28328         AssertNotNull(cms =
28329                 (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
28330         cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
28331         XFCLOSE(cmsFile);
28332 
28333         AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
28334                     sizeof_client_cert_der_2048), 0);
28335         pkcs7->privateKey   = (byte*)client_key_der_2048;
28336         pkcs7->privateKeySz = sizeof_client_key_der_2048;
28337         AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
28338                     sizeof(out)), 0);
28339         XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28340         AssertIntEQ(XMEMCMP(out, "test", 4), 0);
28341         wc_PKCS7_Free(pkcs7);
28342     }
28343 #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
28344 #endif /* HAVE_PKCS7 */
28345 } /* END test_wc_PKCS7_EncodeEnvelopedData() */
28346 
28347 
28348 /*
28349  * Testing wc_PKCS7_EncodeEncryptedData()
28350  */
28351 static void test_wc_PKCS7_EncodeEncryptedData (void)
28352 {
28353 #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
28354     PKCS7*      pkcs7 = NULL;
28355     byte*       tmpBytePtr = NULL;
28356     byte        encrypted[TWOK_BUF];
28357     byte        decoded[TWOK_BUF];
28358     word32      tmpWrd32 = 0;
28359     int         tmpInt = 0;
28360     int         decodedSz;
28361     int         encryptedSz;
28362     int         testSz;
28363     int         i;
28364 
28365     const byte data[] = { /* Hello World */
28366         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
28367         0x72,0x6c,0x64
28368     };
28369 
28370     #ifndef NO_DES3
28371         byte desKey[] = {
28372             0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
28373         };
28374         byte des3Key[] = {
28375             0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
28376             0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
28377             0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
28378         };
28379     #endif
28380 
28381     #if !defined(NO_AES) && defined(HAVE_AES_CBC)
28382         #ifndef NO_AES_128
28383         byte aes128Key[] = {
28384             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28385             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
28386         };
28387         #endif
28388         #ifndef NO_AES_192
28389         byte aes192Key[] = {
28390             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28391             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28392             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
28393         };
28394         #endif
28395         #ifndef NO_AES_256
28396         byte aes256Key[] = {
28397             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28398             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28399             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
28400             0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
28401         };
28402         #endif
28403     #endif /* !NO_AES && HAVE_AES_CBC */
28404     const pkcs7EncryptedVector testVectors[] =
28405     {
28406     #ifndef NO_DES3
28407         {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
28408 
28409         {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
28410     #endif /* !NO_DES3 */
28411     #if !defined(NO_AES) && defined(HAVE_AES_CBC)
28412         #ifndef NO_AES_128
28413         {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
28414          sizeof(aes128Key)},
28415         #endif
28416 
28417         #ifndef NO_AES_192
28418         {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
28419          sizeof(aes192Key)},
28420         #endif
28421 
28422         #ifndef NO_AES_256
28423         {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
28424          sizeof(aes256Key)},
28425         #endif
28426 
28427     #endif /* !NO_AES && HAVE_AES_CBC */
28428     };
28429 
28430     testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
28431 
28432     for (i = 0; i < testSz; i++) {
28433         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28434         AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
28435         pkcs7->content              = (byte*)testVectors[i].content;
28436         pkcs7->contentSz            = testVectors[i].contentSz;
28437         pkcs7->contentOID           = testVectors[i].contentOID;
28438         pkcs7->encryptOID           = testVectors[i].encryptOID;
28439         pkcs7->encryptionKey        = testVectors[i].encryptionKey;
28440         pkcs7->encryptionKeySz      = testVectors[i].encryptionKeySz;
28441         pkcs7->heap                 = HEAP_HINT;
28442 
28443         /* encode encryptedData */
28444         encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28445                                                    sizeof(encrypted));
28446         AssertIntGT(encryptedSz, 0);
28447 
28448         /* Decode encryptedData */
28449         decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
28450                                                     decoded, sizeof(decoded));
28451 
28452         AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
28453         /* Keep values for last itr. */
28454         if (i < testSz - 1) {
28455             wc_PKCS7_Free(pkcs7);
28456         }
28457     }
28458     if (pkcs7 == NULL || testSz == 0) {
28459         AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28460         AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
28461     }
28462 
28463     printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
28464     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
28465                      sizeof(encrypted)),BAD_FUNC_ARG);
28466     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
28467                      sizeof(encrypted)), BAD_FUNC_ARG);
28468     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28469                      0), BAD_FUNC_ARG);
28470     /* Testing the struct. */
28471     tmpBytePtr = pkcs7->content;
28472     pkcs7->content = NULL;
28473     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28474                              sizeof(encrypted)), BAD_FUNC_ARG);
28475     pkcs7->content = tmpBytePtr;
28476     tmpWrd32 = pkcs7->contentSz;
28477     pkcs7->contentSz = 0;
28478     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28479                              sizeof(encrypted)), BAD_FUNC_ARG);
28480     pkcs7->contentSz = tmpWrd32;
28481     tmpInt = pkcs7->encryptOID;
28482     pkcs7->encryptOID = 0;
28483     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28484                              sizeof(encrypted)), BAD_FUNC_ARG);
28485     pkcs7->encryptOID = tmpInt;
28486     tmpBytePtr = pkcs7->encryptionKey;
28487     pkcs7->encryptionKey = NULL;
28488     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28489                              sizeof(encrypted)), BAD_FUNC_ARG);
28490     pkcs7->encryptionKey = tmpBytePtr;
28491     tmpWrd32 = pkcs7->encryptionKeySz;
28492     pkcs7->encryptionKeySz = 0;
28493     AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
28494                              sizeof(encrypted)), BAD_FUNC_ARG);
28495     pkcs7->encryptionKeySz = tmpWrd32;
28496 
28497     printf(resultFmt, passed);
28498 
28499     printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
28500 
28501     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
28502                 decoded, sizeof(decoded)), BAD_FUNC_ARG);
28503     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
28504                 decoded, sizeof(decoded)), BAD_FUNC_ARG);
28505     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
28506                 decoded, sizeof(decoded)), BAD_FUNC_ARG);
28507     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
28508                 NULL, sizeof(decoded)), BAD_FUNC_ARG);
28509     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
28510                 decoded, 0), BAD_FUNC_ARG);
28511     /* Test struct fields */
28512 
28513     tmpBytePtr = pkcs7->encryptionKey;
28514     pkcs7->encryptionKey = NULL;
28515     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
28516                                    decoded, sizeof(decoded)), BAD_FUNC_ARG);
28517     pkcs7->encryptionKey = tmpBytePtr;
28518     pkcs7->encryptionKeySz = 0;
28519     AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
28520                                    decoded, sizeof(decoded)), BAD_FUNC_ARG);
28521 
28522     printf(resultFmt, passed);
28523     wc_PKCS7_Free(pkcs7);
28524 #endif
28525 } /* END test_wc_PKCS7_EncodeEncryptedData() */
28526 
28527 /*
28528  * Testing wc_PKCS7_Degenerate()
28529  */
28530 static void test_wc_PKCS7_Degenerate(void)
28531 {
28532 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
28533     PKCS7* pkcs7;
28534     char   fName[] = "./certs/test-degenerate.p7b";
28535     XFILE  f;
28536     byte   der[4096];
28537     word32 derSz;
28538     int    ret;
28539 
28540     printf(testingFmt, "wc_PKCS7_Degenerate()");
28541 
28542     AssertNotNull(f = XFOPEN(fName, "rb"));
28543     AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
28544     derSz = (word32)ret;
28545     XFCLOSE(f);
28546 
28547     /* test degenerate success */
28548     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28549     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
28550     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28551 #ifndef NO_RSA
28552     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
28553 #else
28554     AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
28555 #endif
28556     wc_PKCS7_Free(pkcs7);
28557 
28558     /* test with turning off degenerate cases */
28559     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28560     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
28561     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28562     wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
28563     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
28564     wc_PKCS7_Free(pkcs7);
28565 
28566     printf(resultFmt, passed);
28567 #endif
28568 } /* END test_wc_PKCS7_Degenerate() */
28569 
28570 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
28571     defined(ASN_BER_TO_DER) && !defined(NO_DES3)
28572 static byte berContent[] = {
28573     0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
28574     0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
28575     0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
28576     0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
28577     0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
28578     0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
28579     0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
28580     0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
28581     0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
28582     0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
28583     0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
28584     0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
28585     0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
28586     0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
28587     0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
28588     0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
28589     0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
28590     0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
28591     0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
28592     0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
28593     0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
28594     0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
28595     0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
28596     0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
28597     0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
28598     0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
28599     0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
28600     0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
28601     0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
28602     0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
28603     0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
28604     0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
28605     0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
28606     0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
28607     0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
28608     0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
28609     0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
28610     0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
28611     0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
28612     0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
28613     0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
28614     0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
28615     0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
28616     0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
28617     0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
28618     0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
28619     0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
28620     0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
28621     0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
28622     0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
28623     0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
28624     0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
28625     0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
28626     0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
28627     0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
28628     0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
28629     0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
28630     0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
28631     0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
28632     0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
28633     0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
28634     0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
28635     0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
28636     0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
28637     0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
28638     0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
28639     0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
28640     0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
28641     0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
28642     0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
28643     0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
28644     0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
28645     0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
28646     0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
28647     0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
28648     0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
28649     0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
28650     0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
28651     0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
28652     0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
28653     0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
28654     0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
28655     0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
28656     0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
28657     0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
28658     0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
28659     0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
28660     0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
28661     0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
28662     0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
28663     0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
28664     0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
28665     0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
28666     0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
28667     0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
28668     0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
28669     0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
28670     0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
28671     0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
28672     0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
28673     0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
28674     0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
28675     0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
28676     0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
28677     0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
28678     0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
28679     0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
28680     0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
28681     0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
28682     0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
28683     0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
28684     0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
28685     0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
28686     0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
28687     0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
28688     0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
28689     0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
28690     0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
28691     0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
28692     0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
28693     0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
28694     0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
28695     0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
28696     0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
28697     0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
28698     0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
28699     0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
28700     0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
28701     0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
28702     0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
28703     0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
28704     0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
28705     0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
28706     0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
28707     0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
28708     0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
28709     0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
28710     0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
28711     0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
28712     0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
28713     0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
28714     0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
28715     0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
28716     0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
28717     0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
28718     0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
28719     0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
28720     0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
28721     0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
28722     0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
28723     0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
28724     0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
28725     0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
28726     0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
28727     0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
28728     0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
28729     0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
28730     0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
28731     0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
28732     0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
28733     0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
28734     0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
28735     0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
28736     0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
28737     0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
28738     0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
28739     0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
28740     0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
28741     0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
28742     0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
28743     0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
28744     0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
28745     0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
28746     0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
28747     0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
28748     0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
28749     0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
28750     0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
28751     0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
28752     0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
28753     0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
28754     0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
28755     0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
28756     0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
28757     0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
28758     0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
28759     0x00, 0x00, 0x00, 0x00, 0x00
28760 };
28761 #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
28762 
28763 /*
28764  * Testing wc_PKCS7_BER()
28765  */
28766 static void test_wc_PKCS7_BER(void)
28767 {
28768 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
28769     defined(ASN_BER_TO_DER)
28770     PKCS7* pkcs7;
28771     char   fName[] = "./certs/test-ber-exp02-05-2022.p7b";
28772     XFILE  f;
28773     byte   der[4096];
28774 #ifndef NO_DES3
28775     byte   decoded[2048];
28776 #endif
28777     word32 derSz;
28778     int    ret;
28779 
28780     printf(testingFmt, "wc_PKCS7_BER()");
28781 
28782     AssertNotNull(f = XFOPEN(fName, "rb"));
28783     AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
28784     derSz = (word32)ret;
28785     XFCLOSE(f);
28786 
28787     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28788     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
28789     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28790 #ifndef NO_RSA
28791     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
28792 #else
28793     AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
28794 #endif
28795     wc_PKCS7_Free(pkcs7);
28796 
28797 #ifndef NO_DES3
28798     /* decode BER content */
28799     AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
28800     AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
28801     derSz = (word32)ret;
28802     XFCLOSE(f);
28803     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
28804 #ifndef NO_RSA
28805     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
28806 #else
28807     AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
28808 #endif
28809 
28810     AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
28811     AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
28812     derSz = (word32)ret;
28813     XFCLOSE(f);
28814     pkcs7->privateKey   = der;
28815     pkcs7->privateKeySz = derSz;
28816 #ifndef NO_RSA
28817 #ifdef WOLFSSL_SP_MATH
28818     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
28819         sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
28820 #else
28821     AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
28822         sizeof(berContent), decoded, sizeof(decoded)), 0);
28823 #endif
28824 #else
28825     AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
28826         sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
28827 #endif
28828     wc_PKCS7_Free(pkcs7);
28829 #endif /* !NO_DES3 */
28830 
28831     printf(resultFmt, passed);
28832 #endif
28833 } /* END test_wc_PKCS7_BER() */
28834 
28835 static void test_PKCS7_signed_enveloped(void)
28836 {
28837 #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
28838     !defined(NO_FILESYSTEM)
28839     XFILE  f;
28840     PKCS7* pkcs7;
28841 #ifdef HAVE_AES_CBC
28842     PKCS7* inner;
28843 #endif
28844     void*  pt;
28845     WC_RNG rng;
28846     unsigned char key[FOURK_BUF/2];
28847     unsigned char cert[FOURK_BUF/2];
28848     unsigned char env[FOURK_BUF];
28849     int envSz  = FOURK_BUF;
28850     int keySz;
28851     int certSz;
28852 
28853     unsigned char sig[FOURK_BUF * 2];
28854     int sigSz = FOURK_BUF * 2;
28855 #ifdef HAVE_AES_CBC
28856     unsigned char decoded[FOURK_BUF];
28857     int decodedSz = FOURK_BUF;
28858 #endif
28859 
28860     printf(testingFmt, "PKCS7_signed_enveloped");
28861 
28862     /* load cert */
28863     AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
28864     AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
28865     XFCLOSE(f);
28866 
28867     /* load key */
28868     AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
28869     AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
28870     XFCLOSE(f);
28871     keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
28872 
28873     /* sign cert for envelope */
28874     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28875     AssertIntEQ(wc_InitRng(&rng), 0);
28876     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
28877     pkcs7->content    = cert;
28878     pkcs7->contentSz  = certSz;
28879     pkcs7->contentOID = DATA;
28880     pkcs7->privateKey   = key;
28881     pkcs7->privateKeySz = keySz;
28882     pkcs7->encryptOID   = RSAk;
28883     pkcs7->hashOID      = SHA256h;
28884     pkcs7->rng          = &rng;
28885     AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
28886     wc_PKCS7_Free(pkcs7);
28887     wc_FreeRng(&rng);
28888 
28889 #ifdef HAVE_AES_CBC
28890     /* create envelope */
28891     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28892     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
28893     pkcs7->content   = sig;
28894     pkcs7->contentSz = sigSz;
28895     pkcs7->contentOID = DATA;
28896     pkcs7->encryptOID = AES256CBCb;
28897     pkcs7->privateKey   = key;
28898     pkcs7->privateKeySz = keySz;
28899     AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
28900     wc_PKCS7_Free(pkcs7);
28901 #endif
28902 
28903     /* create bad signed enveloped data */
28904     sigSz = FOURK_BUF * 2;
28905     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28906     AssertIntEQ(wc_InitRng(&rng), 0);
28907     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
28908     pkcs7->content    = env;
28909     pkcs7->contentSz  = envSz;
28910     pkcs7->contentOID = DATA;
28911     pkcs7->privateKey   = key;
28912     pkcs7->privateKeySz = keySz;
28913     pkcs7->encryptOID   = RSAk;
28914     pkcs7->hashOID      = SHA256h;
28915     pkcs7->rng = &rng;
28916 
28917     /* Set no certs in bundle for this test. Hang on to the pointer though to
28918      * free it later. */
28919     pt = (void*)pkcs7->certList;
28920     pkcs7->certList = NULL; /* no certs in bundle */
28921     AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
28922     pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
28923     wc_PKCS7_Free(pkcs7);
28924 
28925     /* check verify fails */
28926     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28927     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28928     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
28929             PKCS7_SIGNEEDS_CHECK);
28930 
28931     /* try verifying the signature manually */
28932     {
28933         RsaKey rKey;
28934         word32 idx = 0;
28935         byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
28936             WC_MAX_DIGEST_SIZE];
28937         int  digestSz;
28938 
28939         AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
28940         AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
28941         digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
28942                     digest, sizeof(digest), &rKey);
28943         AssertIntGT(digestSz, 0);
28944         AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
28945         AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
28946         AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
28947         /* verify was success */
28948     }
28949 
28950     wc_PKCS7_Free(pkcs7);
28951 
28952     /* initializing the PKCS7 struct with the signing certificate should pass */
28953     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28954     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
28955     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
28956     wc_PKCS7_Free(pkcs7);
28957 
28958     /* create valid degenerate bundle */
28959     sigSz = FOURK_BUF * 2;
28960     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28961     pkcs7->content    = env;
28962     pkcs7->contentSz  = envSz;
28963     pkcs7->contentOID = DATA;
28964     pkcs7->privateKey   = key;
28965     pkcs7->privateKeySz = keySz;
28966     pkcs7->encryptOID   = RSAk;
28967     pkcs7->hashOID      = SHA256h;
28968     pkcs7->rng = &rng;
28969     AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
28970     AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
28971     wc_PKCS7_Free(pkcs7);
28972     wc_FreeRng(&rng);
28973 
28974     /* check verify */
28975     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28976     AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
28977     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
28978     AssertNotNull(pkcs7->content);
28979 
28980 #ifdef HAVE_AES_CBC
28981     /* check decode */
28982     AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
28983     AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
28984     inner->privateKey   = key;
28985     inner->privateKeySz = keySz;
28986     AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
28987                    pkcs7->contentSz, decoded, decodedSz)), 0);
28988     wc_PKCS7_Free(inner);
28989 #endif
28990     wc_PKCS7_Free(pkcs7);
28991 
28992 #ifdef HAVE_AES_CBC
28993     /* check cert set */
28994     AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
28995     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28996     AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
28997     AssertNotNull(pkcs7->singleCert);
28998     AssertIntNE(pkcs7->singleCertSz, 0);
28999     wc_PKCS7_Free(pkcs7);
29000 #endif
29001 
29002     printf(resultFmt, passed);
29003 
29004 #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
29005 }
29006 static void test_wc_PKCS7_NoDefaultSignedAttribs (void)
29007 {
29008 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
29009     && !defined(NO_AES)
29010     PKCS7*      pkcs7;
29011     void*       heap = NULL;
29012 
29013     printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
29014 
29015     pkcs7 = wc_PKCS7_New(heap, devId);
29016     AssertNotNull(pkcs7);
29017     AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
29018 
29019     AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
29020 
29021     AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
29022 
29023     wc_PKCS7_Free(pkcs7);
29024 
29025     printf(resultFmt, passed);
29026 #endif
29027 }
29028 static void test_wc_PKCS7_SetOriEncryptCtx (void)
29029 {
29030 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
29031     && !defined(NO_AES)
29032     PKCS7*      pkcs7;
29033     void*       heap = NULL;
29034     WOLFSSL_CTX* ctx;
29035     ctx = NULL;
29036 
29037     printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
29038 
29039     pkcs7 = wc_PKCS7_New(heap, devId);
29040     AssertNotNull(pkcs7);
29041     AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
29042 
29043     AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
29044 
29045     AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
29046 
29047     wc_PKCS7_Free(pkcs7);
29048 
29049     printf(resultFmt, passed);
29050 #endif
29051 }
29052 static void test_wc_PKCS7_SetOriDecryptCtx (void)
29053 {
29054 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
29055     && !defined(NO_AES)
29056     PKCS7*      pkcs7;
29057     void*       heap = NULL;
29058     WOLFSSL_CTX* ctx;
29059     ctx = NULL;
29060 
29061     printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
29062 
29063     pkcs7 = wc_PKCS7_New(heap, devId);
29064     AssertNotNull(pkcs7);
29065     AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
29066 
29067     AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
29068 
29069     AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
29070 
29071     wc_PKCS7_Free(pkcs7);
29072     printf(resultFmt, passed);
29073 #endif
29074 }
29075 
29076 static void test_wc_PKCS7_DecodeCompressedData(void)
29077 {
29078 #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
29079     && !defined(NO_AES) && defined(HAVE_LIBZ)
29080     PKCS7* pkcs7;
29081     void*  heap = NULL;
29082     byte   out[4096];
29083     byte   *decompressed;
29084     int    outSz, decompressedSz;
29085 
29086     const char* cert = "./certs/client-cert.pem";
29087     byte*  cert_buf = NULL;
29088     size_t cert_sz = 0;
29089 
29090     printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
29091 
29092     AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
29093     AssertNotNull((decompressed =
29094                 (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
29095     decompressedSz = (int)cert_sz;
29096     AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
29097 
29098     pkcs7->content    = (byte*)cert_buf;
29099     pkcs7->contentSz  = (word32)cert_sz;
29100     pkcs7->contentOID = DATA;
29101 
29102     AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
29103                     sizeof(out))), 0);
29104     wc_PKCS7_Free(pkcs7);
29105 
29106     /* compressed key should be smaller than when started */
29107     AssertIntLT(outSz, cert_sz);
29108 
29109     /* test decompression */
29110     AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
29111     AssertIntEQ(pkcs7->contentOID, 0);
29112 
29113     /* fail case with out buffer too small */
29114     AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
29115                 decompressed, outSz), 0);
29116 
29117     /* success case */
29118     AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
29119                 decompressed, decompressedSz), cert_sz);
29120     AssertIntEQ(pkcs7->contentOID, DATA);
29121     AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
29122     XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
29123     decompressed = NULL;
29124 
29125     /* test decompression function with different 'max' inputs */
29126     outSz = sizeof(out);
29127     AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
29128             0);
29129     AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
29130             out, outSz, 0, heap), 0);
29131     AssertNull(decompressed);
29132     AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
29133             out, outSz, 0, heap), 0);
29134     AssertNotNull(decompressed);
29135     AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
29136     XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
29137     decompressed = NULL;
29138 
29139     AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
29140             out, outSz, 0, heap), 0);
29141     AssertNotNull(decompressed);
29142     AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
29143     XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
29144 
29145     if (cert_buf)
29146         free(cert_buf);
29147     wc_PKCS7_Free(pkcs7);
29148 
29149     printf(resultFmt, passed);
29150 #endif
29151 }
29152 
29153 static void test_wc_i2d_PKCS12(void)
29154 {
29155 #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
29156     && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
29157     && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
29158     WC_PKCS12* pkcs12 = NULL;
29159     unsigned char der[FOURK_BUF * 2];
29160     unsigned char* pt;
29161     int derSz;
29162     unsigned char out[FOURK_BUF * 2];
29163     int outSz = FOURK_BUF * 2;
29164 
29165     const char p12_f[] = "./certs/test-servercert.p12";
29166     XFILE f;
29167 
29168     printf(testingFmt, "wc_i2d_PKCS12");
29169 
29170     f =  XFOPEN(p12_f, "rb");
29171     AssertNotNull(f);
29172     derSz = (int)XFREAD(der, 1, sizeof(der), f);
29173     AssertIntGT(derSz, 0);
29174     XFCLOSE(f);
29175 
29176     AssertNotNull(pkcs12 = wc_PKCS12_new());
29177     AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
29178     AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
29179     AssertIntEQ(outSz, derSz);
29180 
29181     outSz = derSz - 1;
29182     pt = out;
29183     AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
29184 
29185     outSz = derSz;
29186     AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
29187     AssertIntEQ((pt == out), 0);
29188 
29189     pt = NULL;
29190     AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
29191     XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
29192     wc_PKCS12_free(pkcs12);
29193 
29194     /* Run the same test but use wc_d2i_PKCS12_fp. */
29195     AssertNotNull(pkcs12 = wc_PKCS12_new());
29196     AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
29197     AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
29198     AssertIntEQ(outSz, derSz);
29199     wc_PKCS12_free(pkcs12);
29200 
29201     /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
29202     pkcs12 = NULL;
29203     AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
29204     AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
29205     AssertIntEQ(outSz, derSz);
29206     wc_PKCS12_free(pkcs12);
29207 
29208     printf(resultFmt, passed);
29209 
29210 #endif
29211 }
29212 
29213 
29214 /* Testing wc_SignatureGetSize() for signature type ECC */
29215 static int test_wc_SignatureGetSize_ecc(void)
29216 {
29217     int ret = 0;
29218     #ifndef NO_SIG_WRAPPER
29219     #if defined(HAVE_ECC) && !defined(NO_ECC256)
29220         enum wc_SignatureType sig_type;
29221         word32 key_len;
29222 
29223         /* Initialize ECC Key */
29224         ecc_key ecc;
29225         const char* qx =
29226             "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
29227         const char* qy =
29228             "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
29229         const char* d =
29230             "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
29231 
29232         ret = wc_ecc_init(&ecc);
29233         if (ret == 0) {
29234             ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
29235         }
29236         printf(testingFmt, "wc_SigntureGetSize_ecc()");
29237         if (ret == 0) {
29238             /* Input for signature type ECC */
29239             sig_type = WC_SIGNATURE_TYPE_ECC;
29240             key_len = sizeof(ecc_key);
29241             ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
29242 
29243             /* Test bad args */
29244             if (ret > 0) {
29245                 sig_type = (enum wc_SignatureType) 100;
29246                 ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
29247                 if (ret == BAD_FUNC_ARG) {
29248                     sig_type = WC_SIGNATURE_TYPE_ECC;
29249                     ret = wc_SignatureGetSize(sig_type, NULL, key_len);
29250                 }
29251                 if (ret >= 0) {
29252                     key_len = (word32) 0;
29253                     ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
29254                 }
29255                 if (ret == BAD_FUNC_ARG) {
29256                     ret = SIG_TYPE_E;
29257                 }
29258             }
29259         } else {
29260             ret = WOLFSSL_FATAL_ERROR;
29261         }
29262         wc_ecc_free(&ecc);
29263     #else
29264         ret = SIG_TYPE_E;
29265     #endif
29266 
29267     if (ret == SIG_TYPE_E) {
29268         ret = 0;
29269     }
29270     else {
29271         ret = WOLFSSL_FATAL_ERROR;
29272     }
29273 
29274     printf(resultFmt, ret == 0 ? passed : failed);
29275     #endif /* NO_SIG_WRAPPER */
29276     return ret;
29277 }/* END test_wc_SignatureGetSize_ecc() */
29278 
29279 /* Testing wc_SignatureGetSize() for signature type rsa */
29280 static int test_wc_SignatureGetSize_rsa(void)
29281 {
29282     int ret = 0;
29283     #ifndef NO_SIG_WRAPPER
29284     #ifndef NO_RSA
29285         enum wc_SignatureType sig_type;
29286         word32 key_len;
29287         word32 idx = 0;
29288 
29289         /* Initialize RSA Key */
29290         RsaKey rsa_key;
29291         byte* tmp = NULL;
29292         size_t bytes;
29293 
29294         #ifdef USE_CERT_BUFFERS_1024
29295             bytes = (size_t)sizeof_client_key_der_1024;
29296             if (bytes < (size_t)sizeof_client_key_der_1024)
29297                 bytes = (size_t)sizeof_client_cert_der_1024;
29298         #elif defined(USE_CERT_BUFFERS_2048)
29299             bytes = (size_t)sizeof_client_key_der_2048;
29300             if (bytes < (size_t)sizeof_client_cert_der_2048)
29301                 bytes = (size_t)sizeof_client_cert_der_2048;
29302         #else
29303             bytes = FOURK_BUF;
29304         #endif
29305 
29306         tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
29307         if (tmp != NULL) {
29308             #ifdef USE_CERT_BUFFERS_1024
29309                 XMEMCPY(tmp, client_key_der_1024,
29310                     (size_t)sizeof_client_key_der_1024);
29311             #elif defined(USE_CERT_BUFFERS_2048)
29312                 XMEMCPY(tmp, client_key_der_2048,
29313                     (size_t)sizeof_client_key_der_2048);
29314             #elif !defined(NO_FILESYSTEM)
29315                 file = XFOPEN(clientKey, "rb");
29316                 if (file != XBADFILE) {
29317                     bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
29318                     XFCLOSE(file);
29319                 }
29320                 else {
29321                     ret = WOLFSSL_FATAL_ERROR;
29322                 }
29323             #else
29324                 ret = WOLFSSL_FATAL_ERROR;
29325             #endif
29326         } else {
29327             ret = WOLFSSL_FATAL_ERROR;
29328         }
29329 
29330         if (ret == 0) {
29331             ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
29332         }
29333         if (ret == 0) {
29334             ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
29335         }
29336 
29337         printf(testingFmt, "wc_SigntureGetSize_rsa()");
29338         if (ret == 0) {
29339             /* Input for signature type RSA */
29340             sig_type = WC_SIGNATURE_TYPE_RSA;
29341             key_len = sizeof(RsaKey);
29342             ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
29343 
29344             /* Test bad args */
29345             if (ret > 0) {
29346                 sig_type = (enum wc_SignatureType) 100;
29347                 ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
29348                 if (ret == BAD_FUNC_ARG) {
29349                     sig_type = WC_SIGNATURE_TYPE_RSA;
29350                     ret = wc_SignatureGetSize(sig_type, NULL, key_len);
29351                 }
29352             #ifndef HAVE_USER_RSA
29353                 if (ret == BAD_FUNC_ARG) {
29354             #else
29355                 if (ret == 0) {
29356             #endif
29357                     key_len = (word32)0;
29358                     ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
29359                 }
29360                 if (ret == BAD_FUNC_ARG) {
29361                     ret = SIG_TYPE_E;
29362                 }
29363             }
29364         } else {
29365             ret = WOLFSSL_FATAL_ERROR;
29366         }
29367         wc_FreeRsaKey(&rsa_key);
29368         XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
29369     #else
29370         ret = SIG_TYPE_E;
29371     #endif
29372 
29373     if (ret == SIG_TYPE_E) {
29374         ret = 0;
29375     }else {
29376         ret = WOLFSSL_FATAL_ERROR;
29377     }
29378 
29379    printf(resultFmt, ret == 0 ? passed : failed);
29380    #endif /* NO_SIG_WRAPPER */
29381    return ret;
29382 }/* END test_wc_SignatureGetSize_rsa(void) */
29383 
29384 /*----------------------------------------------------------------------------*
29385  | hash.h Tests
29386  *----------------------------------------------------------------------------*/
29387 
29388 static int test_wc_HashInit(void)
29389 {
29390     int ret = 0, i;  /* 0 indicates tests passed, 1 indicates failure */
29391 
29392     wc_HashAlg hash;
29393 
29394     /* enum for holding supported algorithms, #ifndef's restrict if disabled */
29395     enum wc_HashType enumArray[] = {
29396     #ifndef NO_MD5
29397             WC_HASH_TYPE_MD5,
29398     #endif
29399     #ifndef NO_SHA
29400             WC_HASH_TYPE_SHA,
29401     #endif
29402     #ifndef WOLFSSL_SHA224
29403             WC_HASH_TYPE_SHA224,
29404     #endif
29405     #ifndef NO_SHA256
29406             WC_HASH_TYPE_SHA256,
29407     #endif
29408     #ifndef WOLFSSL_SHA384
29409             WC_HASH_TYPE_SHA384,
29410     #endif
29411     #ifndef WOLFSSL_SHA512
29412             WC_HASH_TYPE_SHA512,
29413     #endif
29414     };
29415     /* dynamically finds the length */
29416     int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
29417 
29418     /* For loop to test various arguments... */
29419     for (i = 0; i < enumlen; i++) {
29420         /* check for bad args */
29421         if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
29422             ret = 1;
29423             break;
29424         }
29425         wc_HashFree(&hash, enumArray[i]);
29426 
29427         /* check for null ptr */
29428         if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
29429             ret = 1;
29430             break;
29431         }
29432 
29433     }  /* end of for loop */
29434 
29435     printf(testingFmt, "wc_HashInit()");
29436     if (ret==0) {  /* all tests have passed */
29437         printf(resultFmt, passed);
29438     }
29439     else {  /* a test has failed */
29440         printf(resultFmt, failed);
29441     }
29442     return ret;
29443 }  /* end of test_wc_HashInit */
29444 /*
29445  * Unit test function for wc_HashSetFlags()
29446  */
29447 static int test_wc_HashSetFlags(void)
29448 {
29449     int ret = 0;
29450 #ifdef WOLFSSL_HASH_FLAGS
29451     wc_HashAlg hash;
29452     word32 flags = 0;
29453     int i, j;
29454     printf(testingFmt, "wc_HashSetFlags()");
29455 
29456 
29457     /* enum for holding supported algorithms, #ifndef's restrict if disabled */
29458     enum wc_HashType enumArray[] = {
29459     #ifndef NO_MD5
29460             WC_HASH_TYPE_MD5,
29461     #endif
29462     #ifndef NO_SHA
29463             WC_HASH_TYPE_SHA,
29464     #endif
29465     #ifdef WOLFSSL_SHA224
29466             WC_HASH_TYPE_SHA224,
29467     #endif
29468     #ifndef NO_SHA256
29469             WC_HASH_TYPE_SHA256,
29470     #endif
29471     #ifdef WOLFSSL_SHA384
29472             WC_HASH_TYPE_SHA384,
29473     #endif
29474     #ifdef WOLFSSL_SHA512
29475             WC_HASH_TYPE_SHA512,
29476     #endif
29477     #ifdef WOLFSSL_SHA3
29478             WC_HASH_TYPE_SHA3_224,
29479     #endif
29480     };
29481     enum wc_HashType notSupported[] = {
29482               WC_HASH_TYPE_MD5_SHA,
29483               WC_HASH_TYPE_MD2,
29484               WC_HASH_TYPE_MD4,
29485               WC_HASH_TYPE_BLAKE2B,
29486               WC_HASH_TYPE_BLAKE2S,
29487               WC_HASH_TYPE_NONE,
29488      };
29489 
29490     /* dynamically finds the length */
29491     int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
29492 
29493     /* For loop to test various arguments... */
29494     for (i = 0; i < enumlen; i++) {
29495         ret = wc_HashInit(&hash, enumArray[i]);
29496         if (ret == 0) {
29497             ret = wc_HashSetFlags(&hash, enumArray[i], flags);
29498         }
29499         if (ret == 0) {
29500             if (flags & WC_HASH_FLAG_ISCOPY) {
29501                 ret = 0;
29502             }
29503         }
29504         if (ret == 0) {
29505             ret = wc_HashSetFlags(NULL, enumArray[i], flags);
29506             if (ret == BAD_FUNC_ARG) {
29507                 ret = 0;
29508             }
29509         }
29510 
29511         wc_HashFree(&hash, enumArray[i]);
29512 
29513     }
29514     /* For loop to test not supported cases */
29515     int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
29516     for (j = 0; ret == 0 && j < notSupportedLen; j++){
29517         ret = wc_HashInit(&hash, notSupported[j]);
29518         if (ret == 0) {
29519             ret = -1;
29520         }
29521         else if (ret == BAD_FUNC_ARG){
29522             ret = wc_HashSetFlags(&hash, notSupported[j], flags);
29523             if (ret == 0) {
29524                 ret = -1;
29525             }
29526             else if (ret == BAD_FUNC_ARG) {
29527                 ret = 0;
29528             }
29529         }
29530         if (ret == 0) {
29531             ret = wc_HashFree(&hash, notSupported[j]);
29532             if (ret == 0) {
29533                 ret = -1;
29534             }
29535             else if (ret == BAD_FUNC_ARG) {
29536                 ret = 0;
29537             }
29538         }
29539     }
29540 
29541     printf(resultFmt, ret == 0 ? passed : failed);
29542 
29543 #endif
29544     return ret;
29545 }  /* END test_wc_HashSetFlags */
29546 /*
29547  * Unit test function for wc_HashGetFlags()
29548  */
29549 static int test_wc_HashGetFlags(void)
29550 {
29551     int ret = 0;
29552 #ifdef WOLFSSL_HASH_FLAGS
29553     wc_HashAlg hash;
29554     word32 flags = 0;
29555     int i, j;
29556     printf(testingFmt, "wc_HashGetFlags()");
29557 
29558 
29559     /* enum for holding supported algorithms, #ifndef's restrict if disabled */
29560     enum wc_HashType enumArray[] = {
29561     #ifndef NO_MD5
29562             WC_HASH_TYPE_MD5,
29563     #endif
29564     #ifndef NO_SHA
29565             WC_HASH_TYPE_SHA,
29566     #endif
29567     #ifdef WOLFSSL_SHA224
29568             WC_HASH_TYPE_SHA224,
29569     #endif
29570     #ifndef NO_SHA256
29571             WC_HASH_TYPE_SHA256,
29572     #endif
29573     #ifdef WOLFSSL_SHA384
29574             WC_HASH_TYPE_SHA384,
29575     #endif
29576     #ifdef WOLFSSL_SHA512
29577             WC_HASH_TYPE_SHA512,
29578     #endif
29579     #ifdef WOLFSSL_SHA3
29580             WC_HASH_TYPE_SHA3_224,
29581     #endif
29582     };
29583     enum wc_HashType notSupported[] = {
29584               WC_HASH_TYPE_MD5_SHA,
29585               WC_HASH_TYPE_MD2,
29586               WC_HASH_TYPE_MD4,
29587               WC_HASH_TYPE_BLAKE2B,
29588               WC_HASH_TYPE_BLAKE2S,
29589               WC_HASH_TYPE_NONE,
29590     };
29591     int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
29592 
29593     /* For loop to test various arguments... */
29594     for (i = 0; i < enumlen; i++) {
29595         ret = wc_HashInit(&hash, enumArray[i]);
29596         if (ret == 0) {
29597             ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
29598         }
29599         if (ret == 0) {
29600             if (flags & WC_HASH_FLAG_ISCOPY) {
29601                 ret = 0;
29602             }
29603         }
29604         if (ret == 0) {
29605             ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
29606             if (ret == BAD_FUNC_ARG) {
29607                 ret = 0;
29608             }
29609         }
29610         wc_HashFree(&hash, enumArray[i]);
29611         if (ret != 0) {
29612             break;
29613         }
29614     }
29615     /* For loop to test not supported cases */
29616     int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
29617     for (j = 0; ret == 0 && j < notSupportedLen; j++){
29618         ret = wc_HashInit(&hash, notSupported[j]);
29619         if (ret == 0) {
29620             ret = -1;
29621         }
29622         else if (ret == BAD_FUNC_ARG){
29623             ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
29624             if (ret == 0) {
29625                 ret = -1;
29626             }
29627             else if (ret == BAD_FUNC_ARG) {
29628                 ret = 0;
29629             }
29630         }
29631         if (ret == 0) {
29632             ret = wc_HashFree(&hash, notSupported[j]);
29633             if (ret == 0) {
29634                 ret = -1;
29635             }
29636             if (ret == BAD_FUNC_ARG) {
29637                 ret = 0;
29638             }
29639         }
29640     }
29641 
29642     printf(resultFmt, ret == 0 ? passed : failed);
29643 
29644 #endif
29645     return ret;
29646 }  /* END test_wc_HashGetFlags */
29647 
29648 /*----------------------------------------------------------------------------*
29649  | Compatibility Tests
29650  *----------------------------------------------------------------------------*/
29651 
29652 static void test_wolfSSL_lhash(void)
29653 {
29654 #ifdef OPENSSL_ALL
29655     const char testStr[] = "Like a true nature's child\n"
29656                            "We were born\n"
29657                            "Born to be wild";
29658 
29659     printf(testingFmt, "wolfSSL_LH_strhash()");
29660 
29661     AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
29662 
29663     printf(resultFmt, passed);
29664 #endif
29665 }
29666 
29667 static void test_wolfSSL_X509_NAME(void)
29668 {
29669     #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
29670         !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
29671         && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
29672         (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
29673          defined(OPENSSL_EXTRA))
29674     X509* x509;
29675     const unsigned char* c;
29676     unsigned char buf[4096];
29677     int bytes;
29678     XFILE f;
29679     const X509_NAME* a;
29680     const X509_NAME* b;
29681     X509_NAME* d2i_name = NULL;
29682     int sz;
29683     unsigned char* tmp;
29684     char file[] = "./certs/ca-cert.der";
29685 #ifndef OPENSSL_EXTRA_X509_SMALL
29686     byte empty[] = { /* CN=empty emailAddress= */
29687         0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
29688         0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
29689         0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
29690         0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
29691         0x01, 0x16, 0x00
29692     };
29693 #endif
29694 
29695     printf(testingFmt, "wolfSSL_X509_NAME()");
29696 
29697 #ifndef OPENSSL_EXTRA_X509_SMALL
29698     /* test compile of deprecated function, returns 0 */
29699     AssertIntEQ(CRYPTO_thread_id(), 0);
29700 #endif
29701 
29702     AssertNotNull(a = X509_NAME_new());
29703     X509_NAME_free((X509_NAME*)a);
29704 
29705     f = XFOPEN(file, "rb");
29706     AssertTrue(f != XBADFILE);
29707     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
29708     XFCLOSE(f);
29709 
29710     c = buf;
29711     AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
29712 
29713     /* test cmp function */
29714     AssertNotNull(a = X509_get_issuer_name(x509));
29715     AssertNotNull(b = X509_get_subject_name(x509));
29716 
29717 #ifndef OPENSSL_EXTRA_X509_SMALL
29718     AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
29719 #endif
29720 
29721     tmp = buf;
29722     AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
29723     if (sz > 0 && tmp == buf) {
29724         printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);           \
29725         printf(" Expected pointer to be incremented\n");
29726         abort();
29727     }
29728 
29729 #ifndef OPENSSL_EXTRA_X509_SMALL
29730     tmp = buf;
29731     AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
29732 #endif
29733 
29734     /* retry but with the function creating a buffer */
29735     tmp = NULL;
29736     AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
29737     XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
29738 
29739 
29740     AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
29741 #ifndef OPENSSL_EXTRA_X509_SMALL
29742     AssertIntEQ(X509_NAME_cmp(a, b), 0);
29743 #endif
29744     X509_NAME_free((X509_NAME*)b);
29745     X509_NAME_free(d2i_name);
29746     X509_free(x509);
29747 
29748 #ifndef OPENSSL_EXTRA_X509_SMALL
29749     /* test with an empty domain component */
29750     tmp = empty;
29751     sz  = sizeof(empty);
29752     AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
29753     AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
29754 
29755     /* size of empty emailAddress will be 0 */
29756     tmp = buf;
29757     AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
29758                 (char*)tmp, sizeof(buf)), 0);
29759 
29760     /* should contain no organization name */
29761     tmp = buf;
29762     AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
29763                 (char*)tmp, sizeof(buf)), -1);
29764     X509_NAME_free(d2i_name);
29765 #endif
29766 
29767     printf(resultFmt, passed);
29768     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
29769 }
29770 
29771 static void test_wolfSSL_X509_NAME_hash(void)
29772 {
29773 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
29774     && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
29775     BIO* bio;
29776     X509* x509 = NULL;
29777 
29778     printf(testingFmt, "wolfSSL_X509_NAME_hash");
29779 
29780     AssertNotNull(bio = BIO_new(BIO_s_file()));
29781     AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
29782     AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
29783     AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0xF6CF410E);
29784     AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0x677DD39A);
29785 
29786     X509_free(x509);
29787     BIO_free(bio);
29788     printf(resultFmt, passed);
29789 #endif
29790 }
29791 
29792 #ifndef NO_BIO
29793 static void test_wolfSSL_X509_INFO_multiple_info(void)
29794 {
29795 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
29796     STACK_OF(X509_INFO) *info_stack;
29797     X509_INFO *info;
29798     int len;
29799     int i;
29800     const char* files[] = {
29801         cliCertFile,
29802         cliKeyFile,
29803         /* This needs to be the order as svrCertFile contains the
29804          * intermediate cert as well. */
29805         svrKeyFile,
29806         svrCertFile,
29807         NULL,
29808     };
29809     const char** curFile;
29810     BIO *fileBIO;
29811     BIO *concatBIO = NULL;
29812     byte tmp[FOURK_BUF];
29813 
29814     /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
29815      * to group objects together. */
29816     AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
29817     for (curFile = files; *curFile != NULL; curFile++) {
29818         int fileLen;
29819         AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
29820         fileLen = wolfSSL_BIO_get_len(fileBIO);
29821         while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
29822             AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
29823             fileLen -= len;
29824         }
29825         /* Make sure we read the entire file */
29826         AssertIntEQ(fileLen, 0);
29827         BIO_free(fileBIO);
29828     }
29829 
29830     AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
29831                                                       NULL));
29832     AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
29833     for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
29834         AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
29835         AssertNotNull(info->x509);
29836         AssertNull(info->crl);
29837         if (i != 0) {
29838             AssertNotNull(info->x_pkey);
29839             AssertIntEQ(X509_check_private_key(info->x509,
29840                                                info->x_pkey->dec_pkey), 1);
29841         }
29842         else {
29843             AssertNull(info->x_pkey);
29844         }
29845     }
29846 
29847     sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
29848     BIO_free(concatBIO);
29849 
29850 #endif
29851 }
29852 #endif
29853 
29854 #ifndef NO_BIO
29855 static void test_wolfSSL_X509_INFO(void)
29856 {
29857 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
29858     STACK_OF(X509_INFO) *info_stack;
29859     X509_INFO *info;
29860     BIO *cert;
29861     int i;
29862     /* PEM in hex format to avoid null terminator */
29863     byte data[] = {
29864         0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
29865         0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
29866         0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
29867         0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
29868         0x2d, 0x2d, 0x2d
29869     };
29870     /* PEM in hex format to avoid null terminator */
29871     byte data2[] = {
29872         0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
29873         0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
29874         0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
29875         0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
29876         0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
29877         0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
29878         0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
29879     };
29880 
29881     printf(testingFmt, "wolfSSL_X509_INFO");
29882 
29883     AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
29884     AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
29885     for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
29886         AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
29887         AssertNotNull(info->x509);
29888         AssertNull(info->crl);
29889         AssertNull(info->x_pkey);
29890     }
29891     sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
29892     BIO_free(cert);
29893 
29894     AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
29895     AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
29896     sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
29897     BIO_free(cert);
29898 
29899     /* This case should fail due to invalid input. */
29900     AssertNotNull(cert = BIO_new(BIO_s_mem()));
29901     AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
29902     AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
29903     sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
29904     BIO_free(cert);
29905     AssertNotNull(cert = BIO_new(BIO_s_mem()));
29906     AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
29907     AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
29908     sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
29909     BIO_free(cert);
29910 
29911     printf(resultFmt, passed);
29912 #endif
29913 }
29914 #endif
29915 
29916 static void test_wolfSSL_X509_subject_name_hash(void)
29917 {
29918 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
29919     && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
29920 
29921     X509* x509;
29922     X509_NAME* subjectName = NULL;
29923     unsigned long ret = 0;
29924 
29925     printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
29926 
29927     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
29928                 SSL_FILETYPE_PEM));
29929 
29930     AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
29931     ret = X509_subject_name_hash(x509);
29932     AssertIntNE(ret, 0);
29933 
29934     X509_free(x509);
29935     printf(resultFmt, passed);
29936 
29937 #endif
29938 }
29939 
29940 static void test_wolfSSL_X509_issuer_name_hash(void)
29941 {
29942 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
29943     && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
29944 
29945     X509* x509;
29946     X509_NAME* issuertName = NULL;
29947     unsigned long ret = 0;
29948 
29949     printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
29950 
29951     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
29952                 SSL_FILETYPE_PEM));
29953 
29954     AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
29955     ret = X509_issuer_name_hash(x509);
29956     AssertIntNE(ret, 0);
29957 
29958     X509_free(x509);
29959     printf(resultFmt, passed);
29960 
29961 #endif
29962 }
29963 
29964 static void test_wolfSSL_X509_check_host(void)
29965 {
29966 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
29967     && !defined(NO_SHA) && !defined(NO_RSA)
29968 
29969     X509* x509;
29970     const char altName[] = "example.com";
29971 
29972     printf(testingFmt, "wolfSSL_X509_check_host()");
29973 
29974     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
29975                 SSL_FILETYPE_PEM));
29976 
29977     AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
29978             WOLFSSL_SUCCESS);
29979 
29980     AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
29981             WOLFSSL_FAILURE);
29982 
29983     X509_free(x509);
29984 
29985     AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
29986             WOLFSSL_FAILURE);
29987 
29988     printf(resultFmt, passed);
29989 
29990 #endif
29991 }
29992 
29993 static void test_wolfSSL_X509_check_email(void)
29994 {
29995 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
29996     X509* x509;
29997     const char goodEmail[] = "info@wolfssl.com";
29998     const char badEmail[] = "disinfo@wolfssl.com";
29999 
30000     printf(testingFmt, "wolfSSL_X509_check_email()");
30001 
30002     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
30003                 SSL_FILETYPE_PEM));
30004 
30005     /* Should fail on non-matching email address */
30006     AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
30007             WOLFSSL_FAILURE);
30008     /* Should succeed on matching email address */
30009     AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
30010             WOLFSSL_SUCCESS);
30011     /* Should compute length internally when not provided */
30012     AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
30013             WOLFSSL_SUCCESS);
30014     /* Should fail when email address is NULL */
30015     AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
30016             WOLFSSL_FAILURE);
30017 
30018     X509_free(x509);
30019 
30020     /* Should fail when x509 is NULL */
30021     AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
30022             WOLFSSL_FAILURE);
30023 
30024     printf(resultFmt, passed);
30025 #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
30026 }
30027 
30028 static void test_wolfSSL_DES(void)
30029 {
30030     #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
30031     const_DES_cblock myDes;
30032     DES_cblock iv;
30033     DES_key_schedule key;
30034     word32 i;
30035     DES_LONG dl;
30036     unsigned char msg[] = "hello wolfssl";
30037 
30038     printf(testingFmt, "wolfSSL_DES()");
30039 
30040     DES_check_key(1);
30041     DES_set_key(&myDes, &key);
30042 
30043     /* check, check of odd parity */
30044     XMEMSET(myDes, 4, sizeof(const_DES_cblock));  myDes[0] = 6; /*set even parity*/
30045     XMEMSET(key, 5, sizeof(DES_key_schedule));
30046     AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
30047     AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
30048 
30049     /* set odd parity for success case */
30050     DES_set_odd_parity(&myDes);
30051     AssertIntEQ(DES_check_key_parity(&myDes), 1);
30052     printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
30053     AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
30054     for (i = 0; i < sizeof(DES_key_schedule); i++) {
30055         AssertIntEQ(key[i], myDes[i]);
30056     }
30057     AssertIntEQ(DES_is_weak_key(&myDes), 0);
30058 
30059     /* check weak key */
30060     XMEMSET(myDes, 1, sizeof(const_DES_cblock));
30061     XMEMSET(key, 5, sizeof(DES_key_schedule));
30062     AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
30063     AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
30064 
30065     /* now do unchecked copy of a weak key over */
30066     DES_set_key_unchecked(&myDes, &key);
30067     /* compare arrays, should be the same */
30068     for (i = 0; i < sizeof(DES_key_schedule); i++) {
30069         AssertIntEQ(key[i], myDes[i]);
30070     }
30071     AssertIntEQ(DES_is_weak_key(&myDes), 1);
30072 
30073     /* check DES_key_sched API */
30074     XMEMSET(key, 1, sizeof(DES_key_schedule));
30075     AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
30076     AssertIntEQ(DES_key_sched(NULL, &key),   0);
30077     AssertIntEQ(DES_key_sched(&myDes, &key), 0);
30078     /* compare arrays, should be the same */
30079     for (i = 0; i < sizeof(DES_key_schedule); i++) {
30080         AssertIntEQ(key[i], myDes[i]);
30081     }
30082 
30083     /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
30084      * DES_cbc_encrypt on the input */
30085     XMEMSET(iv, 0, sizeof(DES_cblock));
30086     XMEMSET(myDes, 5, sizeof(DES_key_schedule));
30087     AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
30088     AssertIntEQ(dl, 480052723);
30089 
30090     printf(resultFmt, passed);
30091     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
30092 }
30093 
30094 static void test_wc_PemToDer(void)
30095 {
30096 #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
30097     int ret;
30098     DerBuffer* pDer = NULL;
30099     const char* ca_cert = "./certs/server-cert.pem";
30100     byte* cert_buf = NULL;
30101     size_t cert_sz = 0;
30102     int eccKey = 0;
30103     EncryptedInfo info;
30104 
30105     printf(testingFmt, "wc_PemToDer()");
30106 
30107     XMEMSET(&info, 0, sizeof(info));
30108 
30109     ret = load_file(ca_cert, &cert_buf, &cert_sz);
30110     if (ret == 0) {
30111         ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
30112             &pDer, NULL, &info, &eccKey);
30113         AssertIntEQ(ret, 0);
30114 
30115         wc_FreeDer(&pDer);
30116     }
30117 
30118     if (cert_buf)
30119         free(cert_buf);
30120 
30121 #ifdef HAVE_ECC
30122     {
30123         const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
30124         byte key_buf[256] = {0};
30125 
30126         /* Test fail of loading a key with cert type */
30127         AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
30128         key_buf[0] = '\n';
30129         XMEMCPY(key_buf + 1, cert_buf, cert_sz);
30130         AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
30131             &pDer, NULL, &info, &eccKey)), 0);
30132 
30133     #ifdef OPENSSL_EXTRA
30134         AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
30135             &pDer, NULL, &info, &eccKey)), 0);
30136     #endif
30137         wc_FreeDer(&pDer);
30138         if (cert_buf)
30139             free(cert_buf);
30140     }
30141 #endif
30142     printf(resultFmt, passed);
30143 #endif
30144 }
30145 
30146 static void test_wc_AllocDer(void)
30147 {
30148 #if !defined(NO_CERTS)
30149     int ret;
30150     DerBuffer* pDer = NULL;
30151     word32 testSize = 1024;
30152 
30153     printf(testingFmt, "wc_AllocDer()");
30154 
30155     ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
30156     AssertIntEQ(ret, 0);
30157     AssertNotNull(pDer);
30158     wc_FreeDer(&pDer);
30159     printf(resultFmt, passed);
30160 #endif
30161 }
30162 
30163 static void test_wc_CertPemToDer(void)
30164 {
30165 #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
30166     int ret;
30167     const char* ca_cert = "./certs/ca-cert.pem";
30168     byte* cert_buf = NULL;
30169     size_t cert_sz = 0, cert_dersz = 0;
30170     byte* cert_der = NULL;
30171 
30172     printf(testingFmt, "wc_CertPemToDer()");
30173 
30174     ret = load_file(ca_cert, &cert_buf, &cert_sz);
30175     if (ret == 0) {
30176         cert_dersz = cert_sz; /* DER will be smaller than PEM */
30177         cert_der = (byte*)malloc(cert_dersz);
30178         if (cert_der) {
30179             ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
30180                 cert_der, (int)cert_dersz, CERT_TYPE);
30181             AssertIntGE(ret, 0);
30182         }
30183     }
30184 
30185     if (cert_der)
30186         free(cert_der);
30187     if (cert_buf)
30188         free(cert_buf);
30189 
30190     printf(resultFmt, passed);
30191 #endif
30192 }
30193 
30194 static void test_wc_PubKeyPemToDer(void)
30195 {
30196 #ifdef WOLFSSL_PEM_TO_DER
30197 #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
30198     int ret;
30199     const char* key = "./certs/ecc-client-keyPub.pem";
30200     byte* cert_buf = NULL;
30201     size_t cert_sz = 0, cert_dersz = 0;
30202     byte* cert_der = NULL;
30203 
30204     printf(testingFmt, "wc_PubKeyPemToDer()");
30205 
30206 
30207     ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
30208         cert_der, (int)cert_dersz);
30209     AssertIntGE(ret, BAD_FUNC_ARG);
30210 
30211     ret = load_file(key, &cert_buf, &cert_sz);
30212     if (ret == 0) {
30213         cert_dersz = cert_sz; /* DER will be smaller than PEM */
30214         cert_der = (byte*)malloc(cert_dersz);
30215         if (cert_der) {
30216             ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
30217                 cert_der, (int)cert_dersz);
30218             AssertIntGE(ret, 0);
30219         }
30220     }
30221 
30222     if (cert_der)
30223         free(cert_der);
30224     if (cert_buf)
30225         free(cert_buf);
30226 
30227     printf(resultFmt, passed);
30228 #endif
30229 #endif
30230 }
30231 
30232 static void test_wc_PemPubKeyToDer(void)
30233 {
30234 #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
30235     int ret;
30236     const char* key = "./certs/ecc-client-keyPub.pem";
30237     size_t cert_dersz = 1024;
30238     byte* cert_der = (byte*)malloc(cert_dersz);
30239 
30240     printf(testingFmt, "wc_PemPubKeyToDer()");
30241 
30242     ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
30243     AssertIntGE(ret, BAD_FUNC_ARG);
30244 
30245     if (cert_der) {
30246         ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
30247         AssertIntGE(ret, 0);
30248 
30249         free(cert_der);
30250     }
30251 
30252     printf(resultFmt, passed);
30253 #endif
30254 }
30255 
30256 static void test_wc_GetPubKeyDerFromCert(void)
30257 {
30258 #if !defined(NO_RSA) || defined(HAVE_ECC)
30259     int ret;
30260     word32 idx = 0;
30261     byte keyDer[TWOK_BUF];  /* large enough for up to RSA 2048 */
30262     word32 keyDerSz = (word32)sizeof(keyDer);
30263     DecodedCert decoded;
30264 #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
30265     byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
30266     word32 certBufSz = sizeof(certBuf);
30267 #endif
30268 #if (!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
30269     defined(WOLFSSL_CERT_REQ)
30270     XFILE fp;
30271 #endif
30272 #ifndef NO_RSA
30273     RsaKey rsaKey;
30274     #if defined(USE_CERT_BUFFERS_2048)
30275         byte* rsaCertDer = (byte*)client_cert_der_2048;
30276         word32 rsaCertDerSz = sizeof_client_cert_der_2048;
30277     #elif defined(USE_CERT_BUFFERS_1024)
30278         byte* rsaCertDer = (byte*)client_cert_der_1024;
30279         word32 rsaCertDerSz = sizeof_client_cert_der_1024;
30280     #else
30281         unsigned char rsaCertDer[TWOK_BUF];
30282         word32 rsaCertDerSz;
30283     #endif
30284 #endif
30285 #ifdef HAVE_ECC
30286     ecc_key eccKey;
30287     #if defined(USE_CERT_BUFFERS_256)
30288         byte* eccCert = (byte*)cliecc_cert_der_256;
30289         word32 eccCertSz = sizeof_cliecc_cert_der_256;
30290     #else
30291         unsigned char eccCert[ONEK_BUF];
30292         word32 eccCertSz;
30293         XFILE fp2;
30294     #endif
30295 #endif
30296 
30297     printf(testingFmt, "wc_GetPubKeyDerFromCert()");
30298 
30299 #ifndef NO_RSA
30300 
30301 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
30302     fp = XFOPEN("./certs/1024/client-cert.der", "rb");
30303     AssertTrue((fp != XBADFILE));
30304     rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
30305     XFCLOSE(fp);
30306 #endif
30307 
30308     /* good test case - RSA DER cert */
30309     wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
30310     ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
30311     AssertIntEQ(ret, 0);
30312 
30313     ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
30314     AssertIntEQ(ret, 0);
30315     AssertIntGT(keyDerSz, 0);
30316 
30317     /* sanity check, verify we can import DER public key */
30318     ret = wc_InitRsaKey(&rsaKey, NULL);
30319     AssertIntEQ(ret, 0);
30320     ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
30321     AssertIntEQ(ret, 0);
30322     wc_FreeRsaKey(&rsaKey);
30323 
30324     /* test LENGTH_ONLY_E case */
30325     keyDerSz = 0;
30326     ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
30327     AssertIntEQ(ret, LENGTH_ONLY_E);
30328     AssertIntGT(keyDerSz, 0);
30329 
30330     /* bad args: DecodedCert NULL */
30331     ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
30332     AssertIntEQ(ret, BAD_FUNC_ARG);
30333 
30334     /* bad args: output key buff size */
30335     ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
30336     AssertIntEQ(ret, BAD_FUNC_ARG);
30337 
30338     /* bad args: zero size output key buffer */
30339     keyDerSz = 0;
30340     ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
30341     AssertIntEQ(ret, BAD_FUNC_ARG);
30342 
30343     wc_FreeDecodedCert(&decoded);
30344 
30345     /* Certificate Request Tests */
30346     #ifdef WOLFSSL_CERT_REQ
30347     {
30348         XMEMSET(certBuf, 0, sizeof(certBuf));
30349         fp = XFOPEN("./certs/csr.signed.der", "rb");
30350         AssertTrue((fp != XBADFILE));
30351         certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
30352         XFCLOSE(fp);
30353 
30354         wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
30355         ret = wc_ParseCert(&decoded, CERTREQ_TYPE, NO_VERIFY, NULL);
30356         AssertIntEQ(ret, 0);
30357 
30358         /* good test case - RSA DER certificate request */
30359         keyDerSz = sizeof(keyDer);
30360         ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
30361         AssertIntEQ(ret, 0);
30362         AssertIntGT(keyDerSz, 0);
30363 
30364         /* sanity check, verify we can import DER public key */
30365         ret = wc_InitRsaKey(&rsaKey, NULL);
30366         AssertIntEQ(ret, 0);
30367         idx = 0;
30368         ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
30369         AssertIntEQ(ret, 0);
30370         wc_FreeRsaKey(&rsaKey);
30371 
30372         wc_FreeDecodedCert(&decoded);
30373     }
30374     #endif /* WOLFSSL_CERT_REQ */
30375 #endif /* NO_RSA */
30376 
30377 #ifdef HAVE_ECC
30378     #ifndef USE_CERT_BUFFERS_256
30379         fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
30380         AssertTrue((fp2 != XBADFILE));
30381         eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
30382         XFCLOSE(fp2);
30383     #endif
30384 
30385     wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
30386     ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
30387     AssertIntEQ(ret, 0);
30388 
30389     /* good test case - ECC */
30390     XMEMSET(keyDer, 0, sizeof(keyDer));
30391     keyDerSz = sizeof(keyDer);
30392     ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
30393     AssertIntEQ(ret, 0);
30394     AssertIntGT(keyDerSz, 0);
30395 
30396     /* sanity check, verify we can import DER public key */
30397     ret = wc_ecc_init(&eccKey);
30398     AssertIntEQ(ret, 0);
30399     idx = 0; /* reset idx to 0, used above in RSA case */
30400     ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
30401     AssertIntEQ(ret, 0);
30402     wc_ecc_free(&eccKey);
30403 
30404     /* test LENGTH_ONLY_E case */
30405     keyDerSz = 0;
30406     ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
30407     AssertIntEQ(ret, LENGTH_ONLY_E);
30408     AssertIntGT(keyDerSz, 0);
30409 
30410     wc_FreeDecodedCert(&decoded);
30411 #endif
30412 
30413     printf(resultFmt, passed);
30414 
30415 #endif /* !NO_RSA || HAVE_ECC */
30416 }
30417 
30418 static void test_wolfSSL_certs(void)
30419 {
30420 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
30421     !defined(NO_RSA)
30422     X509*  x509ext;
30423 #ifdef OPENSSL_ALL
30424     X509*  x509;
30425     WOLFSSL_X509_EXTENSION* ext;
30426     ASN1_OBJECT* obj;
30427 #endif
30428     WOLFSSL*     ssl;
30429     WOLFSSL_CTX* ctx;
30430     STACK_OF(ASN1_OBJECT)* sk;
30431     ASN1_STRING* asn1_str;
30432     AUTHORITY_KEYID* akey;
30433     BASIC_CONSTRAINTS* bc;
30434     int crit;
30435 
30436     printf(testingFmt, "wolfSSL_certs()");
30437 
30438 #ifndef NO_WOLFSSL_SERVER
30439     AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
30440 #else
30441     AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
30442 #endif
30443     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
30444     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
30445     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
30446     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30447     AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
30448     #endif
30449     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
30450     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30451     AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
30452     #endif
30453     AssertNotNull(ssl = SSL_new(ctx));
30454 
30455     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30456     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30457     #endif
30458 
30459     #ifdef HAVE_PK_CALLBACKS
30460     AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
30461     #endif /* HAVE_PK_CALLBACKS */
30462 
30463     /* create and use x509 */
30464 #ifdef OPENSSL_ALL
30465     x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
30466     AssertNotNull(x509);
30467 #endif
30468     x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
30469     AssertNotNull(x509ext);
30470     AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
30471 
30472     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30473     /* with loading in a new cert the check on private key should now fail */
30474     AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30475     #endif
30476 
30477 
30478     #if defined(USE_CERT_BUFFERS_2048)
30479         AssertIntEQ(SSL_use_certificate_ASN1(ssl,
30480                                   (unsigned char*)server_cert_der_2048,
30481                                   sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
30482     #endif
30483 
30484     #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
30485     /************* Get Digest of Certificate ******************/
30486     {
30487         byte   digest[64]; /* max digest size */
30488         word32 digestSz;
30489 
30490         XMEMSET(digest, 0, sizeof(digest));
30491         AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
30492                     WOLFSSL_SUCCESS);
30493         AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
30494                     WOLFSSL_SUCCESS);
30495 
30496         AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
30497                     WOLFSSL_FAILURE);
30498     }
30499     #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
30500 
30501     /* test and checkout X509 extensions */
30502     bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
30503             &crit, NULL);
30504     AssertNotNull(bc);
30505     AssertIntEQ(crit, 0);
30506 
30507 #ifdef OPENSSL_ALL
30508     ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
30509     AssertNotNull(ext);
30510     X509_EXTENSION_free(ext);
30511 
30512     AssertNotNull(ext = X509_EXTENSION_new());
30513     X509_EXTENSION_set_critical(ext, 1);
30514     AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
30515     AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
30516     ASN1_OBJECT_free(obj);
30517     X509_EXTENSION_free(ext);
30518 
30519     AssertNotNull(ext = X509_EXTENSION_new());
30520     X509_EXTENSION_set_critical(ext, 0);
30521     AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
30522     asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
30523             NULL);
30524     AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
30525     ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
30526                                  * and X509_get_ext_d2i has created new */
30527     X509_EXTENSION_free(ext);
30528 
30529 #endif
30530     BASIC_CONSTRAINTS_free(bc);
30531 
30532     asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
30533     AssertNotNull(asn1_str);
30534     AssertIntEQ(crit, 1);
30535     AssertIntEQ(asn1_str->type, NID_key_usage);
30536 #ifdef OPENSSL_ALL
30537     ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
30538     AssertNotNull(ext);
30539     X509_EXTENSION_free(ext);
30540 #endif
30541     ASN1_STRING_free(asn1_str);
30542 
30543 #ifdef OPENSSL_ALL
30544     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
30545             &crit, NULL);
30546     AssertNotNull(sk);
30547     ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
30548     AssertNotNull(ext);
30549     X509_EXTENSION_free(ext);
30550     sk_ASN1_OBJECT_pop_free(sk, NULL);
30551 #else
30552     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
30553             &crit, NULL);
30554     AssertNull(sk);
30555 #endif
30556 
30557     akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
30558             NID_authority_key_identifier, &crit, NULL);
30559     AssertNotNull(akey);
30560 #ifdef OPENSSL_ALL
30561     ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
30562     AssertNotNull(ext);
30563     X509_EXTENSION_free(ext);
30564 #endif
30565     wolfSSL_AUTHORITY_KEYID_free(akey);
30566 
30567     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
30568             NID_private_key_usage_period, &crit, NULL);
30569     /* AssertNotNull(sk); NID not yet supported */
30570     AssertIntEQ(crit, -1);
30571     sk_ASN1_OBJECT_free(sk);
30572 
30573     sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
30574             &crit, NULL);
30575     /* AssertNotNull(sk); no alt names set */
30576     sk_GENERAL_NAME_free(sk);
30577 
30578     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
30579             &crit, NULL);
30580     /* AssertNotNull(sk); NID not yet supported */
30581     AssertIntEQ(crit, -1);
30582     sk_ASN1_OBJECT_free(sk);
30583 
30584     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
30585             NULL);
30586     /* AssertNotNull(sk); no auth info set */
30587     sk_ASN1_OBJECT_free(sk);
30588 
30589     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
30590             &crit, NULL);
30591     /* AssertNotNull(sk); NID not yet supported */
30592     AssertIntEQ(crit, -1);
30593     sk_ASN1_OBJECT_free(sk);
30594 
30595     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
30596             &crit, NULL);
30597     /* AssertNotNull(sk); NID not yet supported */
30598     AssertIntEQ(crit, -1);
30599     sk_ASN1_OBJECT_free(sk);
30600 
30601     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
30602             NID_certificate_policies, &crit, NULL);
30603     #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
30604         AssertNull(sk);
30605     #else
30606         /* AssertNotNull(sk); no cert policy set */
30607     #endif
30608     sk_ASN1_OBJECT_free(sk);
30609 
30610     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
30611             &crit, NULL);
30612     /* AssertNotNull(sk); NID not yet supported */
30613     AssertIntEQ(crit, -1);
30614     sk_ASN1_OBJECT_free(sk);
30615 
30616     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
30617             &crit, NULL);
30618     /* AssertNotNull(sk); NID not yet supported */
30619     AssertIntEQ(crit, -1);
30620     sk_ASN1_OBJECT_free(sk);
30621 
30622     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
30623             &crit, NULL);
30624     /* AssertNotNull(sk); NID not yet supported */
30625     AssertIntEQ(crit, -1);
30626     sk_ASN1_OBJECT_free(sk);
30627 
30628     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
30629             NULL);
30630     /* AssertNotNull(sk); NID not yet supported */
30631     AssertIntEQ(crit, -1);
30632     sk_ASN1_OBJECT_free(sk);
30633 
30634     /* test invalid cases */
30635     crit = 0;
30636     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
30637     AssertNull(sk);
30638     AssertIntEQ(crit, -1);
30639     sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
30640             NULL, NULL);
30641     AssertNull(sk);
30642 
30643     AssertIntEQ(SSL_get_hit(ssl), 0);
30644 #ifdef OPENSSL_ALL
30645     X509_free(x509);
30646 #endif
30647     X509_free(x509ext);
30648     SSL_free(ssl);
30649     SSL_CTX_free(ctx);
30650 
30651     printf(resultFmt, passed);
30652 #endif /* OPENSSL_EXTRA && !NO_CERTS */
30653 }
30654 
30655 static void test_wolfSSL_X509_check_private_key(void)
30656 {
30657 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
30658         defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
30659     X509*  x509;
30660     EVP_PKEY* pkey = NULL;
30661     const byte* key;
30662 
30663     printf(testingFmt, "wolfSSL_X509_check_private_key()");
30664 
30665     /* Check with correct key */
30666     AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
30667                                                      SSL_FILETYPE_PEM)));
30668     key = client_key_der_2048;
30669     AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
30670                 &key, (long)sizeof_client_key_der_2048));
30671     AssertIntEQ(X509_check_private_key(x509, pkey), 1);
30672     EVP_PKEY_free(pkey);
30673     pkey = NULL;
30674 
30675     /* Check with wrong key */
30676     key = server_key_der_2048;
30677     AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
30678                 &key, (long)sizeof_server_key_der_2048));
30679     AssertIntEQ(X509_check_private_key(x509, pkey), 0);
30680 
30681     /* test for incorrect parameter */
30682     AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
30683     AssertIntEQ(X509_check_private_key(x509, NULL), 0);
30684     AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
30685 
30686     EVP_PKEY_free(pkey);
30687     X509_free(x509);
30688     printf(resultFmt, passed);
30689 #endif
30690 }
30691 
30692 
30693 static void test_wolfSSL_ASN1_TIME_print(void)
30694 {
30695     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
30696         && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
30697             defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
30698     BIO*  bio;
30699     X509*  x509;
30700     const unsigned char* der = client_cert_der_2048;
30701     ASN1_TIME* t;
30702     unsigned char buf[25];
30703 
30704     printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
30705 
30706     AssertNotNull(bio = BIO_new(BIO_s_mem()));
30707     AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
30708                 sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
30709     AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
30710     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
30711     AssertIntEQ(XMEMCMP(buf, "Dec 20 23:07:24 2021 GMT", sizeof(buf) - 1), 0);
30712 
30713     /* create a bad time and test results */
30714     AssertNotNull(t = X509_get_notAfter(x509));
30715     AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
30716     t->data[8] = 0;
30717     t->data[3]  = 0;
30718     AssertIntNE(ASN1_TIME_print(bio, t), 1);
30719     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
30720     AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
30721     AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
30722 
30723     BIO_free(bio);
30724     X509_free(x509);
30725 
30726     printf(resultFmt, passed);
30727     #endif
30728 }
30729 
30730 static void test_wolfSSL_ASN1_UTCTIME_print(void)
30731 {
30732 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
30733     BIO*  bio;
30734     ASN1_UTCTIME* utc = NULL;
30735     unsigned char buf[25];
30736     const char* validDate   = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
30737     const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
30738 
30739     printf(testingFmt, "ASN1_UTCTIME_print()");
30740 
30741     /* NULL parameter check */
30742     AssertNotNull(bio = BIO_new(BIO_s_mem()));
30743     AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
30744     BIO_free(bio);
30745 
30746     /* Valid date */
30747     AssertNotNull(bio = BIO_new(BIO_s_mem()));
30748     AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
30749                                                            DYNAMIC_TYPE_ASN1));
30750     utc->type = ASN_UTC_TIME;
30751     utc->length = ASN_UTC_TIME_SIZE;
30752     XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
30753     AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
30754     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
30755     AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
30756 
30757     XMEMSET(buf, 0, sizeof(buf));
30758     BIO_free(bio);
30759 
30760     /* Invalid format */
30761     AssertNotNull(bio = BIO_new(BIO_s_mem()));
30762     utc->type = ASN_UTC_TIME;
30763     utc->length = ASN_UTC_TIME_SIZE;
30764     XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
30765     AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
30766     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
30767     AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
30768 
30769     XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
30770     BIO_free(bio);
30771 
30772     printf(resultFmt, passed);
30773 #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
30774 }
30775 
30776 static void test_wolfSSL_ASN1_TIME_diff(void)
30777 {
30778 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
30779     ASN1_TIME* fromTime;
30780     ASN1_TIME* toTime;
30781     int daysDiff;
30782     int secsDiff;
30783 
30784     printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
30785 
30786     AssertNotNull((fromTime = ASN1_TIME_new()));
30787     /* Feb 22, 2003, 21:15:15 */
30788     AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
30789     AssertNotNull((toTime = ASN1_TIME_new()));
30790     /* Dec 19, 2010, 18:10:11 */
30791     AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
30792     AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
30793 
30794     AssertIntEQ(daysDiff, 2856);
30795     AssertIntEQ(secsDiff, 75296);
30796 
30797     ASN1_TIME_free(fromTime);
30798     ASN1_TIME_free(toTime);
30799 
30800     printf(resultFmt, passed);
30801 #endif
30802 }
30803 
30804 static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
30805 {
30806     #if defined(OPENSSL_EXTRA)
30807     WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
30808     unsigned char nullstr[32];
30809 
30810     printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
30811 
30812     XMEMSET(nullstr, 0, 32);
30813     asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
30814                     sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
30815                     DYNAMIC_TYPE_TMP_BUFFER);
30816     if (asn1_gtime) {
30817         XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
30818 
30819         wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
30820         AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
30821 
30822         XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
30823     }
30824 
30825     printf(resultFmt, passed);
30826     #endif /* OPENSSL_EXTRA */
30827 }
30828 
30829 
30830 static void test_wolfSSL_private_keys(void)
30831 {
30832     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
30833        !defined(NO_FILESYSTEM)
30834     WOLFSSL*     ssl;
30835     WOLFSSL_CTX* ctx;
30836     EVP_PKEY* pkey = NULL;
30837 
30838     printf(testingFmt, "wolfSSL_private_keys()");
30839 
30840     OpenSSL_add_all_digests();
30841     OpenSSL_add_all_algorithms();
30842 
30843 #ifndef NO_RSA
30844     #ifndef NO_WOLFSSL_SERVER
30845     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
30846     #else
30847     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
30848     #endif
30849     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
30850     /* Have to load a cert before you can check the private key against that
30851      * certificates public key! */
30852     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30853     AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
30854     #endif
30855     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
30856     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30857     AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
30858     #endif
30859     AssertNotNull(ssl = SSL_new(ctx));
30860 
30861     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30862     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30863     #endif
30864 
30865 #ifdef USE_CERT_BUFFERS_2048
30866     {
30867     const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
30868     unsigned char buf[FOURK_BUF];
30869     word32 bufSz;
30870 
30871     AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
30872                 (unsigned char*)client_key_der_2048,
30873                 sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
30874     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30875     /* Should mismatch now that a different private key loaded */
30876     AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30877     #endif
30878 
30879     AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
30880                 (unsigned char*)server_key,
30881                 sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
30882     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30883     /* After loading back in DER format of original key, should match */
30884     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30885     #endif
30886 
30887     /* test loading private key to the WOLFSSL_CTX */
30888     AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
30889                 (unsigned char*)client_key_der_2048,
30890                 sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
30891 
30892     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30893     /* Should mismatch now that a different private key loaded */
30894     AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
30895     #endif
30896 
30897     AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
30898                 (unsigned char*)server_key,
30899                 sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
30900     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30901     /* After loading back in DER format of original key, should match */
30902     AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
30903     #endif
30904 
30905     /* pkey not set yet, expecting to fail */
30906     AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
30907 
30908     /* set PKEY and test again */
30909     AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
30910                 &server_key, (long)sizeof_server_key_der_2048));
30911     AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
30912 
30913     /* reuse PKEY structure and test
30914      * this should be checked with a memory management sanity checker */
30915     AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
30916     server_key = (const unsigned char*)server_key_der_2048;
30917     AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
30918                 &server_key, (long)sizeof_server_key_der_2048));
30919     AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
30920 
30921     /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
30922     bufSz = FOURK_BUF;
30923     AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
30924                     (byte*)server_key_der_2048, sizeof_server_key_der_2048,
30925                     RSAk, NULL, 0)), 0);
30926     server_key = (const unsigned char*)buf;
30927     AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
30928                 (long)bufSz));
30929     }
30930 #endif
30931 
30932 
30933     EVP_PKEY_free(pkey);
30934     SSL_free(ssl); /* frees x509 also since loaded into ssl */
30935     SSL_CTX_free(ctx);
30936 #endif /* end of RSA private key match tests */
30937 
30938 
30939 #ifdef HAVE_ECC
30940     #ifndef NO_WOLFSSL_SERVER
30941     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
30942     #else
30943     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
30944     #endif
30945     AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
30946                                                          WOLFSSL_FILETYPE_PEM));
30947     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
30948                                                          WOLFSSL_FILETYPE_PEM));
30949     AssertNotNull(ssl = SSL_new(ctx));
30950 
30951     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30952     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30953     #endif
30954     SSL_free(ssl);
30955 
30956 
30957     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
30958                                                          WOLFSSL_FILETYPE_PEM));
30959     AssertNotNull(ssl = SSL_new(ctx));
30960 
30961     #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
30962     AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30963     #endif
30964 
30965     SSL_free(ssl);
30966     SSL_CTX_free(ctx);
30967 #endif /* end of ECC private key match tests */
30968 
30969 #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
30970     #ifndef NO_WOLFSSL_SERVER
30971     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
30972     #else
30973     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
30974     #endif
30975     AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
30976                                                          WOLFSSL_FILETYPE_PEM));
30977     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
30978                                                          WOLFSSL_FILETYPE_PEM));
30979     AssertNotNull(ssl = SSL_new(ctx));
30980 
30981     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30982     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30983     #endif
30984     SSL_free(ssl);
30985 
30986 
30987     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
30988                                                          WOLFSSL_FILETYPE_PEM));
30989     AssertNotNull(ssl = SSL_new(ctx));
30990 
30991     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
30992     AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
30993     #endif
30994 
30995     SSL_free(ssl);
30996     SSL_CTX_free(ctx);
30997 #endif /* end of Ed25519 private key match tests */
30998 
30999 #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
31000     #ifndef NO_WOLFSSL_SERVER
31001     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
31002     #else
31003     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
31004     #endif
31005     AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
31006                                                          WOLFSSL_FILETYPE_PEM));
31007     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
31008                                                          WOLFSSL_FILETYPE_PEM));
31009     AssertNotNull(ssl = SSL_new(ctx));
31010 
31011     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
31012     AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
31013     #endif
31014     SSL_free(ssl);
31015 
31016 
31017     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
31018                                                          WOLFSSL_FILETYPE_PEM));
31019     AssertNotNull(ssl = SSL_new(ctx));
31020 
31021     #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
31022     AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
31023     #endif
31024 
31025     SSL_free(ssl);
31026     SSL_CTX_free(ctx);
31027 #endif /* end of Ed448 private key match tests */
31028 
31029     EVP_cleanup();
31030 
31031     /* test existence of no-op macros in wolfssl/openssl/ssl.h */
31032     CONF_modules_free();
31033     ENGINE_cleanup();
31034     CONF_modules_unload();
31035 
31036     (void)ssl;
31037     (void)ctx;
31038     (void)pkey;
31039 
31040     printf(resultFmt, passed);
31041     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
31042 }
31043 
31044 static void test_wolfSSL_PEM_read_PrivateKey(void)
31045 {
31046 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
31047     && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
31048     XFILE file;
31049     const char* fname = "./certs/server-key.pem";
31050     EVP_PKEY* pkey;
31051     RSA* rsa;
31052     WOLFSSL_EVP_PKEY_CTX* ctx;
31053     unsigned char* sig;
31054     size_t sigLen;
31055     const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
31056     size_t tbsLen = sizeof(tbs);
31057 
31058     printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
31059 
31060     /* Check error case. */
31061     AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
31062 
31063     /* Read in an RSA key. */
31064     file = XFOPEN(fname, "rb");
31065     AssertTrue(file != XBADFILE);
31066     AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
31067     XFCLOSE(file);
31068 
31069     /* Make sure the key is usable by signing some data with it. */
31070     AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
31071     AssertIntGT((sigLen = RSA_size(rsa)), 0);
31072     AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
31073         DYNAMIC_TYPE_TMP_BUFFER));
31074     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
31075     AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
31076     AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
31077         WOLFSSL_SUCCESS);
31078 
31079     XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
31080     EVP_PKEY_CTX_free(ctx);
31081     EVP_PKEY_free(pkey);
31082 
31083     printf(resultFmt, passed);
31084 #endif
31085 }
31086 
31087 static void test_wolfSSL_PEM_PrivateKey(void)
31088 {
31089 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
31090     (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
31091 
31092 #ifndef NO_BIO
31093     BIO*      bio = NULL;
31094 #endif
31095     EVP_PKEY* pkey  = NULL;
31096     const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
31097 
31098 #ifndef NO_BIO
31099 
31100     /* test creating new EVP_PKEY with bad arg */
31101     AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
31102 
31103     /* test loading RSA key using BIO */
31104 #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
31105     {
31106         XFILE file;
31107         const char* fname = "./certs/server-key.pem";
31108         const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
31109 
31110         size_t sz;
31111         byte* buf;
31112         EVP_PKEY* pkey2;
31113         EVP_PKEY* pkey3;
31114         RSA* rsa_key = NULL;
31115 
31116         file = XFOPEN(fname, "rb");
31117         AssertTrue((file != XBADFILE));
31118         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
31119         sz = XFTELL(file);
31120         XREWIND(file);
31121         AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
31122         if (buf) {
31123             AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
31124         }
31125         XFCLOSE(file);
31126 
31127         /* Test using BIO new mem and loading PEM private key */
31128         bio = BIO_new_mem_buf(buf, (int)sz);
31129         AssertNotNull(bio);
31130         AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
31131         XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
31132         BIO_free(bio);
31133         bio = NULL;
31134         AssertNotNull(pkey2 = EVP_PKEY_new());
31135         pkey2->type = EVP_PKEY_RSA;
31136         /* Test parameter copy */
31137         AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
31138         EVP_PKEY_free(pkey2);
31139         EVP_PKEY_free(pkey);
31140         pkey  = NULL;
31141 
31142         /* Qt unit test case : rsa pkcs8 key */
31143         file = XFOPEN(fname_rsa_p8, "rb");
31144         AssertTrue((file != XBADFILE));
31145         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
31146         sz = XFTELL(file);
31147         XREWIND(file);
31148         AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
31149         if (buf)
31150             AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
31151         XFCLOSE(file);
31152 
31153         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
31154         AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
31155         XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
31156         BIO_free(bio);
31157         bio = NULL;
31158         AssertNotNull(pkey3 = EVP_PKEY_new());
31159 
31160         AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
31161         AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
31162 
31163         #ifdef WOLFSSL_ERROR_CODE_OPENSSL
31164         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
31165         #else
31166         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
31167         #endif
31168 
31169         RSA_free(rsa_key);
31170         EVP_PKEY_free(pkey3);
31171         EVP_PKEY_free(pkey);
31172         pkey  = NULL;
31173     }
31174 #endif
31175 
31176     /* test loading ECC key using BIO */
31177 #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
31178     {
31179         XFILE file;
31180         const char* fname = "./certs/ecc-key.pem";
31181         const char* fname_ecc_p8  = "./certs/ecc-keyPkcs8.pem";
31182 
31183         size_t sz;
31184         byte* buf;
31185         EVP_PKEY* pkey2;
31186         EVP_PKEY* pkey3;
31187         EC_KEY*   ec_key;
31188         int nid = 0;
31189 
31190         file = XFOPEN(fname, "rb");
31191         AssertTrue((file != XBADFILE));
31192         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
31193         sz = XFTELL(file);
31194         XREWIND(file);
31195         AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
31196         if (buf)
31197             AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
31198         XFCLOSE(file);
31199 
31200         /* Test using BIO new mem and loading PEM private key */
31201         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
31202         AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
31203         XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
31204         BIO_free(bio);
31205         bio = NULL;
31206         AssertNotNull(pkey2 = EVP_PKEY_new());
31207         AssertNotNull(pkey3 = EVP_PKEY_new());
31208         pkey2->type = EVP_PKEY_EC;
31209         /* Test parameter copy */
31210         AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
31211         /* Qt unit test case 1*/
31212         AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
31213         AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
31214         #ifdef WOLFSSL_ERROR_CODE_OPENSSL
31215         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
31216         #else
31217         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
31218         #endif
31219         /* Test default digest */
31220         AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
31221         AssertIntEQ(nid, NID_sha256);
31222         EC_KEY_free(ec_key);
31223         EVP_PKEY_free(pkey3);
31224         EVP_PKEY_free(pkey2);
31225         EVP_PKEY_free(pkey);
31226         pkey  = NULL;
31227 
31228         /* Qt unit test case ec pkcs8 key */
31229         file = XFOPEN(fname_ecc_p8, "rb");
31230         AssertTrue((file != XBADFILE));
31231         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
31232         sz = XFTELL(file);
31233         XREWIND(file);
31234         AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
31235         if (buf)
31236             AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
31237         XFCLOSE(file);
31238 
31239         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
31240         AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
31241         XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
31242         BIO_free(bio);
31243         bio = NULL;
31244         AssertNotNull(pkey3 = EVP_PKEY_new());
31245         /* Qt unit test case */
31246         AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
31247         AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
31248         #ifdef WOLFSSL_ERROR_CODE_OPENSSL
31249         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
31250         #else
31251         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
31252         #endif
31253         EC_KEY_free(ec_key);
31254         EVP_PKEY_free(pkey3);
31255         EVP_PKEY_free(pkey);
31256         pkey  = NULL;
31257     }
31258 #endif
31259 
31260 #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
31261     {
31262         #define BIO_PEM_TEST_CHAR 'a'
31263         EVP_PKEY* pkey2 = NULL;
31264         unsigned char extra[10];
31265         int i;
31266         BIO* pub_bio = NULL;
31267 
31268         printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
31269 
31270         XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
31271 
31272         AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31273         AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
31274         AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31275         AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
31276 
31277         AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
31278                 &server_key, (long)sizeof_server_key_der_2048));
31279         AssertNull(pkey);
31280 
31281         AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
31282                 &server_key, (long)sizeof_server_key_der_2048));
31283         AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
31284                 WOLFSSL_SUCCESS);
31285         AssertIntGT(BIO_pending(bio), 0);
31286         AssertIntEQ(BIO_pending(bio), 1679);
31287         /* Check if the pubkey API writes only the public key */
31288 #ifdef WOLFSSL_KEY_GEN
31289         AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
31290         AssertIntGT(BIO_pending(pub_bio), 0);
31291         /* Previously both the private key and the pubkey calls would write
31292          * out the private key and the PEM header was the only difference.
31293          * The public PEM should be significantly shorter than the
31294          * private key versison. */
31295         AssertIntEQ(BIO_pending(pub_bio), 451);
31296 #endif
31297 
31298 
31299         /* test creating new EVP_PKEY with good args */
31300         AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
31301         if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
31302             AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
31303 
31304         /* test of reuse of EVP_PKEY */
31305         AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
31306         AssertIntEQ(BIO_pending(bio), 0);
31307         AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
31308                 SSL_SUCCESS);
31309         AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
31310         AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
31311         AssertNotNull(pkey);
31312         if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
31313             AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
31314         }
31315         AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
31316         AssertIntEQ(BIO_read(bio, extra, 10), 10);
31317         for (i = 0; i < 10; i++) {
31318             AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
31319         }
31320 
31321         BIO_free(pub_bio);
31322         BIO_free(bio);
31323         bio = NULL;
31324         EVP_PKEY_free(pkey);
31325         pkey  = NULL;
31326         EVP_PKEY_free(pkey2);
31327     }
31328     #endif
31329 
31330     /* key is DES encrypted */
31331     #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
31332     !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_MD5) && \
31333     defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && !defined(NO_RSA)
31334     {
31335         XFILE f;
31336         wc_pem_password_cb* passwd_cb;
31337         void* passwd_cb_userdata;
31338         SSL_CTX* ctx;
31339         char passwd[] = "bad password";
31340 
31341     #ifndef WOLFSSL_NO_TLS12
31342         #ifndef NO_WOLFSSL_SERVER
31343         AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
31344         #else
31345         AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
31346         #endif
31347     #else
31348         #ifndef NO_WOLFSSL_SERVER
31349         AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
31350         #else
31351         AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
31352         #endif
31353     #endif
31354 
31355         AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
31356         SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
31357         AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
31358         AssertNull(passwd_cb_userdata =
31359             SSL_CTX_get_default_passwd_cb_userdata(ctx));
31360 
31361         /* fail case with password call back */
31362         AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
31363                     (void*)passwd));
31364         BIO_free(bio);
31365         AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
31366         AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
31367                     (void*)passwd));
31368         BIO_free(bio);
31369 
31370         f = XFOPEN("./certs/server-keyEnc.pem", "rb");
31371         AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
31372 
31373         /* use callback that works */
31374         AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
31375                 (void*)"yassl123"));
31376 
31377         AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
31378 
31379         EVP_PKEY_free(pkey);
31380         pkey  = NULL;
31381         BIO_free(bio);
31382         bio = NULL;
31383         SSL_CTX_free(ctx);
31384     }
31385     #endif /* !defined(NO_DES3) */
31386 
31387 #endif /* !NO_BIO */
31388 
31389     #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
31390     {
31391         unsigned char buf[2048];
31392         size_t bytes;
31393         XFILE f;
31394         SSL_CTX* ctx;
31395 
31396     #ifndef WOLFSSL_NO_TLS12
31397         #ifndef NO_WOLFSSL_SERVER
31398         AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
31399         #else
31400         AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
31401         #endif
31402     #else
31403         #ifndef NO_WOLFSSL_SERVER
31404         AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
31405         #else
31406         AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
31407         #endif
31408     #endif
31409 
31410         f = XFOPEN("./certs/ecc-key.der", "rb");
31411         AssertTrue((f != XBADFILE));
31412         bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
31413         XFCLOSE(f);
31414 
31415         server_key = buf;
31416         pkey = NULL;
31417         AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
31418         AssertNull(pkey);
31419         AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
31420         AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
31421 
31422         EVP_PKEY_free(pkey);
31423         pkey = NULL;
31424         SSL_CTX_free(ctx);
31425     }
31426     #endif
31427 
31428     printf(resultFmt, passed);
31429 
31430 #ifndef NO_BIO
31431     (void)bio;
31432 #endif
31433     (void)pkey;
31434     (void)server_key;
31435 
31436 #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
31437 }
31438 
31439 #ifndef NO_BIO
31440 static void test_wolfSSL_PEM_bio_RSAKey(void)
31441 {
31442 #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
31443     defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
31444     !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
31445     RSA* rsa = NULL;
31446     BIO* bio = NULL;
31447 
31448     printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
31449 
31450     /* PrivateKey */
31451     AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
31452     AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
31453     AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
31454     AssertIntEQ(RSA_size(rsa), 256);
31455     AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
31456                                             NULL), WOLFSSL_FAILURE);
31457     BIO_free(bio);
31458     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31459     AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
31460                                             NULL), WOLFSSL_SUCCESS);
31461     BIO_free(bio);
31462     RSA_free(rsa);
31463 
31464     /* PUBKEY */
31465     AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
31466     AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
31467     AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
31468     AssertIntEQ(RSA_size(rsa), 256);
31469     AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
31470     BIO_free(bio);
31471     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31472     AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
31473     BIO_free(bio);
31474     RSA_free(rsa);
31475 
31476     /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
31477     AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
31478     AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
31479     BIO_free(bio);
31480     RSA_free(rsa);
31481 
31482     #ifdef HAVE_ECC
31483     /* ensure that non-rsa keys do not work */
31484     AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
31485     AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
31486     AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
31487     BIO_free(bio);
31488     RSA_free(rsa);
31489     #endif /* HAVE_ECC */
31490 
31491     printf(resultFmt, passed);
31492 #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
31493          (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
31494          !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
31495 }
31496 
31497 static void test_wolfSSL_PEM_RSAPrivateKey(void)
31498 {
31499     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
31500        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
31501     RSA* rsa = NULL;
31502     RSA* rsa_dup = NULL;
31503     BIO* bio = NULL;
31504 
31505     printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
31506 
31507     AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
31508     AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
31509     AssertIntEQ(RSA_size(rsa), 256);
31510 
31511 #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
31512     AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
31513     AssertPtrNE(rsa_dup, rsa);
31514 #endif
31515 
31516     /* test if valgrind complains about unreleased memory */
31517     RSA_up_ref(rsa);
31518     RSA_free(rsa);
31519 
31520     BIO_free(bio);
31521     RSA_free(rsa);
31522     RSA_free(rsa_dup);
31523 
31524 #ifdef HAVE_ECC
31525     AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
31526     AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
31527 
31528     BIO_free(bio);
31529 #endif /* HAVE_ECC */
31530 
31531     printf(resultFmt, passed);
31532     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
31533 }
31534 
31535 static void test_wolfSSL_PEM_bio_DSAKey(void)
31536 {
31537 #ifndef HAVE_SELFTEST
31538 #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
31539     defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
31540     DSA* dsa = NULL;
31541     BIO* bio = NULL;
31542 
31543     printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
31544 
31545     /* PrivateKey */
31546     AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
31547     AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
31548     AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
31549     AssertIntEQ(BN_num_bytes(dsa->g), 128);
31550     AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
31551             WOLFSSL_FAILURE);
31552     BIO_free(bio);
31553     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31554     AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
31555             WOLFSSL_SUCCESS);
31556     BIO_free(bio);
31557     DSA_free(dsa);
31558 
31559     /* PUBKEY */
31560     AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
31561     AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
31562     AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
31563     AssertIntEQ(BN_num_bytes(dsa->g), 128);
31564     AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
31565     BIO_free(bio);
31566     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31567     AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
31568     BIO_free(bio);
31569     DSA_free(dsa);
31570 
31571     #ifdef HAVE_ECC
31572     /* ensure that non-dsa keys do not work */
31573     AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
31574     AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
31575     AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
31576     BIO_free(bio);
31577     DSA_free(dsa);
31578     #endif /* HAVE_ECC */
31579 
31580     printf(resultFmt, passed);
31581 #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
31582          !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
31583          !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
31584 #endif /* HAVE_SELFTEST */
31585 }
31586 
31587 static void test_wolfSSL_PEM_bio_ECKey(void)
31588 {
31589 #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
31590     defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
31591     EC_KEY* ec = NULL;
31592     BIO* bio = NULL;
31593 
31594     printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
31595 
31596     /* PrivateKey */
31597     AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
31598     AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
31599     AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
31600     AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
31601     AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
31602                                            NULL),WOLFSSL_FAILURE);
31603     BIO_free(bio);
31604     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31605     AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
31606                                            NULL), WOLFSSL_SUCCESS);
31607     BIO_free(bio);
31608     EC_KEY_free(ec);
31609 
31610     /* PUBKEY */
31611     AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
31612     AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
31613     AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
31614     AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
31615     AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
31616     BIO_free(bio);
31617     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
31618     AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
31619     BIO_free(bio);
31620     EC_KEY_free(ec);
31621 
31622     #ifndef NO_RSA
31623     /* ensure that non-ec keys do not work */
31624     AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
31625     AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
31626     AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
31627     BIO_free(bio);
31628     EC_KEY_free(ec);
31629     #endif /* HAVE_ECC */
31630 
31631     printf(resultFmt, passed);
31632 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
31633 }
31634 
31635 static void test_wolfSSL_PEM_PUBKEY(void)
31636 {
31637 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
31638     BIO*      bio = NULL;
31639     EVP_PKEY* pkey  = NULL;
31640 
31641     /* test creating new EVP_PKEY with bad arg */
31642     AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
31643 
31644     /* test loading ECC key using BIO */
31645 #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
31646     {
31647         XFILE file;
31648         const char* fname = "./certs/ecc-client-keyPub.pem";
31649         size_t sz;
31650         byte* buf;
31651 
31652         EVP_PKEY* pkey2;
31653         EC_KEY*   ec_key;
31654 
31655         file = XFOPEN(fname, "rb");
31656         AssertTrue((file != XBADFILE));
31657         XFSEEK(file, 0, XSEEK_END);
31658         sz = XFTELL(file);
31659         XREWIND(file);
31660         AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
31661         if (buf)
31662             AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
31663         XFCLOSE(file);
31664 
31665         /* Test using BIO new mem and loading PEM private key */
31666         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
31667         AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
31668         XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
31669         BIO_free(bio);
31670         bio = NULL;
31671 
31672         /* Qt unit test case*/
31673         AssertNotNull(pkey2 = EVP_PKEY_new());
31674         AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
31675         AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
31676         #ifdef WOLFSSL_ERROR_CODE_OPENSSL
31677         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
31678         #else
31679         AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
31680         #endif
31681 
31682         EC_KEY_free(ec_key);
31683         EVP_PKEY_free(pkey2);
31684         EVP_PKEY_free(pkey);
31685         pkey  = NULL;
31686     }
31687 #endif
31688 
31689     (void)bio;
31690     (void)pkey;
31691 #endif
31692 }
31693 
31694 #endif /* !NO_BIO */
31695 
31696 static void test_DSA_do_sign_verify(void)
31697 {
31698 #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
31699 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
31700     !defined(NO_DSA)
31701     unsigned char digest[WC_SHA_DIGEST_SIZE];
31702     DSA_SIG* sig;
31703     DSA* dsa;
31704     word32  bytes;
31705     byte sigBin[DSA_SIG_SIZE];
31706     int dsacheck;
31707 
31708 #ifdef USE_CERT_BUFFERS_1024
31709     byte    tmp[ONEK_BUF];
31710     XMEMSET(tmp, 0, sizeof(tmp));
31711     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
31712     bytes = sizeof_dsa_key_der_1024;
31713 #elif defined(USE_CERT_BUFFERS_2048)
31714     byte    tmp[TWOK_BUF];
31715     XMEMSET(tmp, 0, sizeof(tmp));
31716     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
31717     bytes = sizeof_dsa_key_der_2048;
31718 #else
31719     byte    tmp[TWOK_BUF];
31720     XMEMSET(tmp, 0, sizeof(tmp));
31721     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
31722     if (fp == XBADFILE) {
31723         return WOLFSSL_BAD_FILE;
31724     }
31725     bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
31726     XFCLOSE(fp);
31727 #endif /* END USE_CERT_BUFFERS_1024 */
31728 
31729     printf(testingFmt, "DSA_do_sign_verify()");
31730     XMEMSET(digest, 202, sizeof(digest));
31731 
31732     AssertNotNull(dsa = DSA_new());
31733     AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
31734 
31735     AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
31736     AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
31737 
31738     AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
31739     AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
31740 
31741     DSA_SIG_free(sig);
31742     DSA_free(dsa);
31743 #endif
31744 #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
31745 }
31746 
31747 static void test_wolfSSL_tmp_dh(void)
31748 {
31749 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
31750     !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
31751     byte buff[6000];
31752     char file[] = "./certs/dsaparams.pem";
31753     XFILE f;
31754     int  bytes;
31755     DSA* dsa;
31756     DH*  dh;
31757 #if defined(WOLFSSL_DH_EXTRA) && \
31758     (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
31759     DH*  dh2;
31760 #endif
31761     BIO*     bio;
31762     SSL*     ssl;
31763     SSL_CTX* ctx;
31764 
31765     printf(testingFmt, "wolfSSL_tmp_dh()");
31766 
31767     #ifndef NO_WOLFSSL_SERVER
31768     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
31769     #else
31770     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
31771     #endif
31772     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
31773     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
31774     AssertNotNull(ssl = SSL_new(ctx));
31775 
31776     f = XFOPEN(file, "rb");
31777     AssertTrue((f != XBADFILE));
31778     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
31779     XFCLOSE(f);
31780 
31781     bio = BIO_new_mem_buf((void*)buff, bytes);
31782     AssertNotNull(bio);
31783 
31784     dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
31785     AssertNotNull(dsa);
31786 
31787     dh = wolfSSL_DSA_dup_DH(dsa);
31788     AssertNotNull(dh);
31789 #if defined(WOLFSSL_DH_EXTRA) && \
31790     (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
31791     AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
31792 #endif
31793 
31794     AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
31795     #ifndef NO_WOLFSSL_SERVER
31796     AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
31797     #else
31798     AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
31799     #endif
31800 
31801     BIO_free(bio);
31802     DSA_free(dsa);
31803     DH_free(dh);
31804 #if defined(WOLFSSL_DH_EXTRA) && \
31805     (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
31806     DH_free(dh2);
31807 #endif
31808     SSL_free(ssl);
31809     SSL_CTX_free(ctx);
31810 
31811     printf(resultFmt, passed);
31812 #endif
31813 }
31814 
31815 static void test_wolfSSL_ctrl(void)
31816 {
31817 #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
31818     byte buff[6000];
31819     BIO* bio;
31820     int  bytes;
31821     BUF_MEM* ptr = NULL;
31822 
31823     printf(testingFmt, "wolfSSL_crtl()");
31824 
31825     bytes = sizeof(buff);
31826     bio = BIO_new_mem_buf((void*)buff, bytes);
31827     AssertNotNull(bio);
31828     AssertNotNull(BIO_s_socket());
31829 
31830     AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
31831 
31832     /* needs tested after stubs filled out @TODO
31833         SSL_ctrl
31834         SSL_CTX_ctrl
31835     */
31836 
31837     BIO_free(bio);
31838     printf(resultFmt, passed);
31839 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
31840 }
31841 
31842 
31843 static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
31844 {
31845 #ifdef OPENSSL_EXTRA
31846     static const unsigned char pw[] = "password";
31847     static const int pwSz = sizeof(pw) - 1;
31848     size_t checkPwSz = 0;
31849     const unsigned char* checkPw = NULL;
31850     WOLFSSL_EVP_PKEY* key = NULL;
31851 
31852     printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
31853 
31854     AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
31855     AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
31856 
31857     AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
31858     if (key) {
31859         AssertIntEQ(key->type, EVP_PKEY_HMAC);
31860         AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
31861         AssertIntEQ(key->pkey_sz, pwSz);
31862         AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
31863     }
31864     AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
31865     AssertIntEQ((int)checkPwSz, pwSz);
31866     if (checkPw) {
31867         AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
31868     }
31869     wolfSSL_EVP_PKEY_free(key);
31870 
31871     AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
31872     if (key) {
31873         AssertIntEQ(key->pkey_sz, 0);
31874     }
31875     checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
31876     (void)checkPw;
31877     AssertIntEQ((int)checkPwSz, 0);
31878     wolfSSL_EVP_PKEY_free(key);
31879 
31880     AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
31881     if (key) {
31882         AssertIntEQ(key->pkey_sz, 0);
31883     }
31884     checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
31885     (void)checkPw;
31886     AssertIntEQ((int)checkPwSz, 0);
31887     wolfSSL_EVP_PKEY_free(key);
31888 
31889     printf(resultFmt, passed);
31890 #endif /* OPENSSL_EXTRA */
31891 }
31892 static void test_wolfSSL_EVP_Digest(void)
31893 {
31894 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
31895 
31896 
31897     const char* in = "abc";
31898     int   inLen = (int)XSTRLEN(in);
31899     byte  out[WC_SHA256_DIGEST_SIZE];
31900     unsigned int outLen;
31901     const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
31902                "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
31903                "\x15\xAD";
31904     printf(testingFmt, "wolfSSL_EVP_Digest()");
31905 
31906     AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
31907     AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
31908     AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
31909 
31910     printf(resultFmt, passed);
31911 
31912 #endif /* OPEN_EXTRA && ! NO_SHA256 */
31913 }
31914 
31915 static void test_wolfSSL_EVP_Digest_all(void)
31916 {
31917 #ifdef OPENSSL_EXTRA
31918     const char* digests[] = {
31919 #ifndef NO_MD5
31920         "MD5",
31921 #endif
31922 #ifndef NO_SHA
31923         "SHA",
31924 #endif
31925 #ifdef WOLFSSL_SHA224
31926         "SHA224",
31927 #endif
31928 #ifndef NO_SHA256
31929         "SHA256",
31930 #endif
31931 #ifdef WOLFSSL_SHA384
31932         "SHA384",
31933 #endif
31934 #ifdef WOLFSSL_SHA512
31935         "SHA512",
31936 #endif
31937 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
31938         "SHA512_224",
31939 #endif
31940 #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
31941         "SHA512_256",
31942 #endif
31943 #ifdef WOLFSSL_SHA3
31944 #ifndef WOLFSSL_NOSHA3_224
31945         "SHA3_224",
31946 #endif
31947 #ifndef WOLFSSL_NOSHA3_256
31948         "SHA3_256",
31949 #endif
31950         "SHA3_384",
31951 #ifndef WOLFSSL_NOSHA3_512
31952         "SHA3_512",
31953 #endif
31954 #endif /* WOLFSSL_SHA3 */
31955         NULL
31956     };
31957     const char** d;
31958     const unsigned char in[] = "abc";
31959     int   inLen = XSTR_SIZEOF(in);
31960     byte  out[WC_MAX_DIGEST_SIZE];
31961     unsigned int outLen;
31962 
31963     printf(testingFmt, "wolfSSL_EVP_Digest_all");
31964 
31965     for (d = digests; *d != NULL; d++) {
31966         AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
31967         AssertIntGT(outLen, 0);
31968         AssertIntEQ(EVP_MD_size(*d), outLen);
31969     }
31970 
31971     printf(resultFmt, passed);
31972 #endif
31973 }
31974 
31975 static void test_wolfSSL_EVP_MD_size(void)
31976 {
31977 #ifdef OPENSSL_EXTRA
31978 
31979     WOLFSSL_EVP_MD_CTX mdCtx;
31980 
31981     printf(testingFmt, "wolfSSL_EVP_MD_size()");
31982 
31983 #ifdef WOLFSSL_SHA3
31984 #ifndef WOLFSSL_NOSHA3_224
31985     wolfSSL_EVP_MD_CTX_init(&mdCtx);
31986 
31987     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
31988     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
31989     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
31990     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
31991 #endif
31992 #ifndef WOLFSSL_NOSHA3_256
31993     wolfSSL_EVP_MD_CTX_init(&mdCtx);
31994 
31995     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
31996     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
31997     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
31998     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
31999 #endif
32000     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32001 
32002     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
32003     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
32004     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
32005     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32006 #ifndef WOLFSSL_NOSHA3_512
32007     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32008 
32009     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
32010     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
32011     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
32012     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32013 #endif
32014 #endif /* WOLFSSL_SHA3 */
32015 
32016 #ifndef NO_SHA256
32017     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32018 
32019     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
32020     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
32021     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
32022     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
32023     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
32024     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32025 
32026 #endif
32027 
32028 #ifndef NO_MD5
32029     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32030 
32031     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
32032     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
32033     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
32034     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
32035     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
32036     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32037 
32038 #endif
32039 
32040 #ifdef WOLFSSL_SHA224
32041     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32042 
32043     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
32044     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
32045     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
32046     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
32047     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
32048     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32049 
32050 #endif
32051 
32052 #ifdef WOLFSSL_SHA384
32053     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32054 
32055     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
32056     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
32057     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
32058     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
32059     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
32060     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32061 
32062 #endif
32063 
32064 #ifdef WOLFSSL_SHA512
32065     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32066 
32067     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
32068     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
32069     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
32070     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
32071     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
32072     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32073 
32074 #endif
32075 
32076 #ifndef NO_SHA
32077     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32078 
32079     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
32080     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
32081     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
32082     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
32083     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
32084     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32085 
32086     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32087 
32088     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
32089     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
32090     AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
32091     AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
32092     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
32093     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32094 #endif
32095     /* error case */
32096     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32097 
32098     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
32099     AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
32100     AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
32101     /* Cleanup is valid on uninit'ed struct */
32102     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32103 
32104     printf(resultFmt, passed);
32105 
32106 #endif /* OPENSSL_EXTRA */
32107 }
32108 
32109 static void test_wolfSSL_EVP_MD_pkey_type(void)
32110 {
32111 #ifdef OPENSSL_EXTRA
32112     const WOLFSSL_EVP_MD* md;
32113 
32114     printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
32115 
32116 #ifndef NO_MD5
32117     AssertNotNull(md = EVP_md5());
32118     AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
32119 #endif
32120 #ifndef NO_SHA
32121     AssertNotNull(md = EVP_sha1());
32122     AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
32123 #endif
32124 #ifdef WOLFSSL_SHA224
32125     AssertNotNull(md = EVP_sha224());
32126     AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
32127 #endif
32128     AssertNotNull(md = EVP_sha256());
32129     AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
32130 #ifdef WOLFSSL_SHA384
32131     AssertNotNull(md = EVP_sha384());
32132     AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
32133 #endif
32134 #ifdef WOLFSSL_SHA512
32135     AssertNotNull(md = EVP_sha512());
32136     AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
32137 #endif
32138 
32139     printf(resultFmt, passed);
32140 #endif
32141 }
32142 
32143 #ifdef OPENSSL_EXTRA
32144 static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
32145         size_t testKeySz, const char* testData, size_t testDataSz,
32146         const byte* testResult, size_t testResultSz)
32147 {
32148     unsigned char check[WC_MAX_DIGEST_SIZE];
32149     size_t checkSz = -1;
32150     WOLFSSL_EVP_PKEY* key;
32151     WOLFSSL_EVP_MD_CTX mdCtx;
32152 
32153     printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
32154     AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
32155                                                       testKey, (int)testKeySz));
32156     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32157     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
32158     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
32159                                                   (unsigned int)testDataSz), 1);
32160     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32161     AssertIntEQ((int)checkSz, (int)testResultSz);
32162     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32163     AssertIntEQ((int)checkSz,(int)testResultSz);
32164     AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
32165     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32166 
32167     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
32168     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
32169                                                   (unsigned int)testDataSz), 1);
32170     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
32171 
32172     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32173     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32174     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
32175     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
32176     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32177     AssertIntEQ((int)checkSz, (int)testResultSz);
32178     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32179     AssertIntEQ((int)checkSz,(int)testResultSz);
32180     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
32181                                               (unsigned int)testDataSz - 4), 1);
32182     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32183     AssertIntEQ((int)checkSz,(int)testResultSz);
32184     AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
32185 
32186     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32187     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
32188     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
32189     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
32190                                               (unsigned int)testDataSz - 4), 1);
32191     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
32192 
32193     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32194 
32195     wolfSSL_EVP_PKEY_free(key);
32196 }
32197 #endif
32198 
32199 static void test_wolfSSL_EVP_MD_hmac_signing(void)
32200 {
32201 #ifdef OPENSSL_EXTRA
32202     static const unsigned char testKey[] =
32203     {
32204         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
32205         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
32206         0x0b, 0x0b, 0x0b, 0x0b
32207     };
32208     static const char testData[] = "Hi There";
32209 #ifdef WOLFSSL_SHA224
32210     static const unsigned char testResultSha224[] =
32211     {
32212         0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
32213         0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
32214         0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
32215         0x53, 0x68, 0x4b, 0x22
32216     };
32217 #endif
32218 #ifndef NO_SHA256
32219     static const unsigned char testResultSha256[] =
32220     {
32221         0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
32222         0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
32223         0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
32224         0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
32225     };
32226 #endif
32227 #ifdef WOLFSSL_SHA384
32228     static const unsigned char testResultSha384[] =
32229     {
32230         0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
32231         0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
32232         0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
32233         0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
32234         0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
32235         0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
32236     };
32237 #endif
32238 #ifdef WOLFSSL_SHA512
32239     static const unsigned char testResultSha512[] =
32240     {
32241         0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
32242         0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
32243         0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
32244         0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
32245         0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
32246         0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
32247         0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
32248         0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
32249     };
32250 #endif
32251 #ifdef WOLFSSL_SHA3
32252     #ifndef WOLFSSL_NOSHA3_224
32253     static const unsigned char testResultSha3_224[] =
32254     {
32255         0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
32256         0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
32257         0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
32258         0xf3, 0xc8, 0x60, 0xf7
32259     };
32260     #endif
32261     #ifndef WOLFSSL_NOSHA3_256
32262     static const unsigned char testResultSha3_256[] =
32263     {
32264         0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
32265         0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
32266         0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
32267         0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
32268     };
32269     #endif
32270     #ifndef WOLFSSL_NOSHA3_384
32271     static const unsigned char testResultSha3_384[] =
32272     {
32273         0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
32274         0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
32275         0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
32276         0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
32277         0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
32278         0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
32279     };
32280     #endif
32281     #ifndef WOLFSSL_NOSHA3_512
32282     static const unsigned char testResultSha3_512[] =
32283     {
32284         0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
32285         0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
32286         0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
32287         0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
32288         0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
32289         0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
32290         0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
32291         0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
32292     };
32293     #endif
32294 #endif
32295 
32296 #ifndef NO_SHA256
32297     test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
32298         XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
32299 #endif
32300 #ifdef WOLFSSL_SHA224
32301     test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
32302         XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
32303 #endif
32304 #ifdef WOLFSSL_SHA384
32305     test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
32306         XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
32307 #endif
32308 #ifdef WOLFSSL_SHA512
32309     test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
32310         XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
32311 #endif
32312 #ifdef WOLFSSL_SHA3
32313     #ifndef WOLFSSL_NOSHA3_224
32314     test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
32315         testData, XSTRLEN(testData), testResultSha3_224,
32316         sizeof(testResultSha3_224));
32317     #endif
32318     #ifndef WOLFSSL_NOSHA3_256
32319     test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
32320         testData, XSTRLEN(testData), testResultSha3_256,
32321         sizeof(testResultSha3_256));
32322     #endif
32323     #ifndef WOLFSSL_NOSHA3_384
32324     test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
32325         testData, XSTRLEN(testData), testResultSha3_384,
32326         sizeof(testResultSha3_384));
32327     #endif
32328     #ifndef WOLFSSL_NOSHA3_512
32329     test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
32330         testData, XSTRLEN(testData), testResultSha3_512,
32331         sizeof(testResultSha3_512));
32332     #endif
32333 #endif
32334 
32335     printf(resultFmt, passed);
32336 #endif /* OPENSSL_EXTRA */
32337 }
32338 
32339 
32340 static void test_wolfSSL_EVP_MD_rsa_signing(void)
32341 {
32342 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
32343                                                   defined(USE_CERT_BUFFERS_2048)
32344     WOLFSSL_EVP_PKEY* privKey;
32345     WOLFSSL_EVP_PKEY* pubKey;
32346     WOLFSSL_EVP_PKEY_CTX* keyCtx;
32347     const char testData[] = "Hi There";
32348     WOLFSSL_EVP_MD_CTX mdCtx;
32349     size_t checkSz = -1;
32350     int sz = 2048 / 8;
32351     const unsigned char* cp;
32352     const unsigned char* p;
32353     unsigned char check[2048/8];
32354     size_t i;
32355     int paddings[] = {
32356             RSA_PKCS1_PADDING,
32357 #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
32358             RSA_PKCS1_PSS_PADDING,
32359 #endif
32360     };
32361 
32362 
32363     printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
32364 
32365     cp = client_key_der_2048;
32366     AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
32367                                                   sizeof_client_key_der_2048)));
32368     p = client_keypub_der_2048;
32369     AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
32370                                                sizeof_client_keypub_der_2048)));
32371 
32372     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32373     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32374                                                              NULL, privKey), 1);
32375     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
32376                                           (unsigned int)XSTRLEN(testData)), 1);
32377     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32378     AssertIntEQ((int)checkSz, sz);
32379     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32380     AssertIntEQ((int)checkSz,sz);
32381     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32382 
32383     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32384     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32385                                                               NULL, pubKey), 1);
32386     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
32387                                                (unsigned int)XSTRLEN(testData)),
32388                 1);
32389     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
32390     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32391 
32392     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32393     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32394                                                              NULL, privKey), 1);
32395     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
32396     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32397     AssertIntEQ((int)checkSz, sz);
32398     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32399     AssertIntEQ((int)checkSz, sz);
32400     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
32401                                       (unsigned int)XSTRLEN(testData) - 4), 1);
32402     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32403     AssertIntEQ((int)checkSz, sz);
32404     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32405 
32406     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32407     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32408                                                               NULL, pubKey), 1);
32409     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
32410     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
32411                                            (unsigned int)XSTRLEN(testData) - 4),
32412                 1);
32413     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
32414     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32415 
32416     /* Check all signing padding types */
32417     for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
32418         wolfSSL_EVP_MD_CTX_init(&mdCtx);
32419         AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
32420                 wolfSSL_EVP_sha256(), NULL, privKey), 1);
32421         AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
32422                 paddings[i]), 1);
32423         AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
32424                 (unsigned int)XSTRLEN(testData)), 1);
32425         AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32426         AssertIntEQ((int)checkSz, sz);
32427         AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32428         AssertIntEQ((int)checkSz,sz);
32429         AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32430 
32431         wolfSSL_EVP_MD_CTX_init(&mdCtx);
32432         AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
32433                 wolfSSL_EVP_sha256(), NULL, pubKey), 1);
32434         AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
32435                 paddings[i]), 1);
32436         AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
32437                 (unsigned int)XSTRLEN(testData)), 1);
32438         AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
32439         AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32440     }
32441 
32442     wolfSSL_EVP_PKEY_free(pubKey);
32443     wolfSSL_EVP_PKEY_free(privKey);
32444 
32445     printf(resultFmt, passed);
32446 #endif
32447 }
32448 
32449 
32450 static void test_wolfSSL_EVP_MD_ecc_signing(void)
32451 {
32452 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
32453     WOLFSSL_EVP_PKEY* privKey;
32454     WOLFSSL_EVP_PKEY* pubKey;
32455     const char testData[] = "Hi There";
32456     WOLFSSL_EVP_MD_CTX mdCtx;
32457     size_t checkSz = -1;
32458     const unsigned char* cp;
32459     const unsigned char* p;
32460     unsigned char check[2048/8];
32461 
32462     printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
32463 
32464     cp = ecc_clikey_der_256;
32465     privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
32466                                                    sizeof_ecc_clikey_der_256);
32467     AssertNotNull(privKey);
32468     p = ecc_clikeypub_der_256;
32469     AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
32470                                                 sizeof_ecc_clikeypub_der_256)));
32471 
32472     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32473     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32474                                                              NULL, privKey), 1);
32475     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
32476                                           (unsigned int)XSTRLEN(testData)), 1);
32477     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32478     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32479     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32480 
32481     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32482     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32483                                                               NULL, pubKey), 1);
32484     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
32485                                                (unsigned int)XSTRLEN(testData)),
32486                 1);
32487     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
32488     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32489 
32490     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32491     AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32492                                                              NULL, privKey), 1);
32493     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
32494     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
32495     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32496     AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
32497                                       (unsigned int)XSTRLEN(testData) - 4), 1);
32498     AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
32499     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32500 
32501     wolfSSL_EVP_MD_CTX_init(&mdCtx);
32502     AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
32503                                                               NULL, pubKey), 1);
32504     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
32505     AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
32506                                            (unsigned int)XSTRLEN(testData) - 4),
32507                 1);
32508     AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
32509     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
32510 
32511     wolfSSL_EVP_PKEY_free(pubKey);
32512     wolfSSL_EVP_PKEY_free(privKey);
32513 
32514     printf(resultFmt, passed);
32515 #endif
32516 }
32517 
32518 
32519 static void test_wolfSSL_CTX_add_extra_chain_cert(void)
32520 {
32521 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
32522    !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
32523     char caFile[] = "./certs/client-ca.pem";
32524     char clientFile[] = "./certs/client-cert.pem";
32525     SSL_CTX* ctx;
32526     X509* x509;
32527     BIO *bio = NULL;
32528     X509 *cert = NULL;
32529     X509 *ca;
32530     STACK_OF(X509) *chain = NULL;
32531     STACK_OF(X509) *chain2 = NULL;
32532 
32533     printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
32534 
32535     #ifndef NO_WOLFSSL_SERVER
32536     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
32537     #else
32538     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
32539     #endif
32540 
32541     x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
32542     AssertNotNull(x509);
32543     AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
32544 
32545     x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
32546     AssertNotNull(x509);
32547 
32548     #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
32549     /* additional test of getting EVP_PKEY key size from X509
32550      * Do not run with user RSA because wolfSSL_RSA_size is not currently
32551      * allowed with user RSA */
32552     {
32553         EVP_PKEY* pkey;
32554         #if defined(HAVE_ECC)
32555         X509* ecX509;
32556         #endif /* HAVE_ECC */
32557 
32558         AssertNotNull(pkey = X509_get_pubkey(x509));
32559         /* current RSA key is 2048 bit (256 bytes) */
32560         AssertIntEQ(EVP_PKEY_size(pkey), 256);
32561 
32562         EVP_PKEY_free(pkey);
32563 
32564         #if defined(HAVE_ECC)
32565         #if defined(USE_CERT_BUFFERS_256)
32566         AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
32567                     cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
32568                     SSL_FILETYPE_ASN1));
32569         #else
32570         AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
32571                     SSL_FILETYPE_PEM));
32572         #endif
32573         pkey = X509_get_pubkey(ecX509);
32574         AssertNotNull(pkey);
32575         /* current ECC key is 256 bit (32 bytes) */
32576         AssertIntEQ(EVP_PKEY_size(pkey), 32);
32577 
32578         X509_free(ecX509);
32579         EVP_PKEY_free(pkey);
32580         #endif /* HAVE_ECC */
32581     }
32582 #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
32583 
32584     AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
32585 
32586 #ifdef WOLFSSL_ENCRYPTED_KEYS
32587     AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
32588     AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
32589 #endif
32590     SSL_CTX_free(ctx);
32591 
32592 #ifndef NO_WOLFSSL_SERVER
32593     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
32594 #else
32595     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
32596 #endif
32597     /* Test haproxy use case */
32598     AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
32599     /* Read Certificate */
32600     AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
32601     AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
32602     AssertNotNull(chain = sk_X509_new_null());
32603     AssertIntEQ(sk_X509_push(chain, ca), 1);
32604     AssertNotNull(chain2 = X509_chain_up_ref(chain));
32605     AssertNotNull(ca = sk_X509_shift(chain2));
32606     AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
32607     AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
32608 
32609     BIO_free(bio);
32610     X509_free(cert);
32611     sk_X509_pop_free(chain, X509_free);
32612     sk_X509_pop_free(chain2, X509_free);
32613     SSL_CTX_free(ctx);
32614     printf(resultFmt, passed);
32615     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
32616              !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
32617 }
32618 
32619 
32620 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
32621 static void test_wolfSSL_ERR_peek_last_error_line(void)
32622 {
32623     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
32624        !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
32625        !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
32626        defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
32627     tcp_ready ready;
32628     func_args client_args;
32629     func_args server_args;
32630 #ifndef SINGLE_THREADED
32631     THREAD_TYPE serverThread;
32632 #endif
32633     callback_functions client_cb;
32634     callback_functions server_cb;
32635     int         line = 0;
32636     int         flag = ERR_TXT_STRING;
32637     const char* file = NULL;
32638     const char* data = NULL;
32639 
32640     printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
32641 
32642     /* create a failed connection and inspect the error */
32643 #ifdef WOLFSSL_TIRTOS
32644     fdOpenSession(Task_self());
32645 #endif
32646     XMEMSET(&client_args, 0, sizeof(func_args));
32647     XMEMSET(&server_args, 0, sizeof(func_args));
32648 
32649     StartTCP();
32650     InitTcpReady(&ready);
32651 
32652     XMEMSET(&client_cb, 0, sizeof(callback_functions));
32653     XMEMSET(&server_cb, 0, sizeof(callback_functions));
32654     client_cb.method  = wolfTLSv1_1_client_method;
32655     server_cb.method  = wolfTLSv1_2_server_method;
32656 
32657     server_args.signal    = &ready;
32658     server_args.callbacks = &server_cb;
32659     client_args.signal    = &ready;
32660     client_args.callbacks = &client_cb;
32661 
32662 #ifndef SINGLE_THREADED
32663     start_thread(test_server_nofail, &server_args, &serverThread);
32664     wait_tcp_ready(&server_args);
32665     test_client_nofail(&client_args, NULL);
32666     join_thread(serverThread);
32667 #endif
32668 
32669     FreeTcpReady(&ready);
32670 
32671     AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
32672     AssertNotNull(data);
32673 
32674     /* check clearing error state */
32675     ERR_remove_state(0);
32676     AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
32677     ERR_peek_last_error_line(NULL, &line);
32678     AssertIntEQ(line, 0);
32679     ERR_peek_last_error_line(&file, NULL);
32680     AssertNull(file);
32681 
32682     /* retry connection to fill error queue */
32683     XMEMSET(&client_args, 0, sizeof(func_args));
32684     XMEMSET(&server_args, 0, sizeof(func_args));
32685 
32686     StartTCP();
32687     InitTcpReady(&ready);
32688 
32689     client_cb.method  = wolfTLSv1_1_client_method;
32690     server_cb.method  = wolfTLSv1_2_server_method;
32691 
32692     server_args.signal    = &ready;
32693     server_args.callbacks = &server_cb;
32694     client_args.signal    = &ready;
32695     client_args.callbacks = &client_cb;
32696 
32697     start_thread(test_server_nofail, &server_args, &serverThread);
32698     wait_tcp_ready(&server_args);
32699     test_client_nofail(&client_args, NULL);
32700     join_thread(serverThread);
32701 
32702     FreeTcpReady(&ready);
32703 
32704     /* check that error code was stored */
32705     AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
32706     ERR_peek_last_error_line(NULL, &line);
32707     AssertIntNE(line, 0);
32708     ERR_peek_last_error_line(&file, NULL);
32709     AssertNotNull(file);
32710 
32711 #ifdef WOLFSSL_TIRTOS
32712     fdOpenSession(Task_self());
32713 #endif
32714 
32715     printf(resultFmt, passed);
32716 
32717     printf("\nTesting error print out\n");
32718     ERR_print_errors_fp(stdout);
32719     printf("Done testing print out\n\n");
32720     fflush(stdout);
32721     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
32722              !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
32723 }
32724 #endif
32725 
32726 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
32727        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
32728 static int verify_cb(int ok, X509_STORE_CTX *ctx)
32729 {
32730     (void) ok;
32731     (void) ctx;
32732     printf("ENTER verify_cb\n");
32733     return SSL_SUCCESS;
32734 }
32735 #endif
32736 
32737 static void test_wolfSSL_X509_Name_canon(void)
32738 {
32739 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
32740     !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
32741      defined(WOLFSSL_CERT_GEN) && \
32742     (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
32743 
32744     const long ex_hash1 = 0x0fdb2da4;
32745     const long ex_hash2 = 0x9f3e8c9e;
32746     X509_NAME *name = NULL;
32747     X509 *x509 = NULL;
32748     FILE* file = NULL;
32749     unsigned long hash = 0;
32750     byte digest[WC_MAX_DIGEST_SIZE] = {0};
32751     byte  *pbuf = NULL;
32752     word32 len = 0;
32753     (void) ex_hash2;
32754     printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
32755 
32756     file = XFOPEN(caCertFile, "rb");
32757     AssertNotNull(file);
32758     AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
32759     AssertNotNull(name = X509_get_issuer_name(x509));
32760 
32761     AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
32762     AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
32763 
32764     hash = (((unsigned long)digest[3] << 24) |
32765             ((unsigned long)digest[2] << 16) |
32766             ((unsigned long)digest[1] <<  8) |
32767             ((unsigned long)digest[0]));
32768     AssertIntEQ(hash, ex_hash1);
32769 
32770     XFCLOSE(file);
32771     X509_free(x509);
32772     XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
32773     pbuf = NULL;
32774 
32775     file = XFOPEN(cliCertFile, "rb");
32776     AssertNotNull(file);
32777     AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
32778     AssertNotNull(name = X509_get_issuer_name(x509));
32779 
32780     AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
32781     AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
32782 
32783     hash = (((unsigned long)digest[3] << 24) |
32784             ((unsigned long)digest[2] << 16) |
32785             ((unsigned long)digest[1] <<  8) |
32786             ((unsigned long)digest[0]));
32787 
32788     AssertIntEQ(hash, ex_hash2);
32789 
32790     XFCLOSE(file);
32791     X509_free(x509);
32792     XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
32793 
32794     printf(resultFmt, passed);
32795 
32796 #endif
32797 
32798 }
32799 
32800 static void test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
32801 {
32802 #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
32803     const int  MAX_DIR = 4;
32804     const char paths[][32] = {
32805                              "./certs/ed25519",
32806                              "./certs/ecc",
32807                              "./certs/crl",
32808                              "./certs/",
32809                             };
32810 
32811     char CertCrl_path[MAX_FILENAME_SZ];
32812     char *p;
32813     X509_STORE* str;
32814     X509_LOOKUP* lookup;
32815     WOLFSSL_STACK* sk = NULL;
32816     int len, total_len, i;
32817 
32818     (void) sk;
32819 
32820     printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
32821 
32822     XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
32823 
32824     /* illegal string */
32825     AssertNotNull((str = wolfSSL_X509_STORE_new()));
32826     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32827     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
32828                                     SSL_FILETYPE_PEM,NULL), 0);
32829 
32830     /* free store */
32831     X509_STORE_free(str);
32832 
32833     /* short folder string */
32834     AssertNotNull((str = wolfSSL_X509_STORE_new()));
32835     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32836     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
32837                                     SSL_FILETYPE_PEM,NULL), 1);
32838     #if defined(WOLFSSL_INT_H)
32839     /* only available when including internal.h */
32840     AssertNotNull(sk = lookup->dirs->dir_entry);
32841     #endif
32842     /* free store */
32843     X509_STORE_free(str);
32844 
32845     /* typical function check */
32846     p = &CertCrl_path[0];
32847     total_len = 0;
32848 
32849     for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
32850         len = (int)XSTRLEN((const char*)&paths[i]);
32851         total_len += len;
32852         XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
32853         p += len;
32854         if (i != 0) *(p++) = SEPARATOR_CHAR;
32855     }
32856 
32857     AssertNotNull((str = wolfSSL_X509_STORE_new()));
32858     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32859     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
32860                                     SSL_FILETYPE_PEM,NULL), 1);
32861     #if defined(WOLFSSL_INT_H)
32862     /* only available when including internal.h */
32863     AssertNotNull(sk = lookup->dirs->dir_entry);
32864     #endif
32865 
32866     X509_STORE_free(str);
32867 
32868     printf(resultFmt, passed);
32869 #endif
32870 
32871 }
32872 
32873 static void test_wolfSSL_X509_LOOKUP_ctrl_file(void)
32874 {
32875 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
32876     !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
32877     defined(WOLFSSL_SIGNER_DER_CERT)
32878 
32879     X509_STORE_CTX* ctx;
32880     X509_STORE* str;
32881     X509_LOOKUP* lookup;
32882 
32883     X509* cert1;
32884     X509* x509Ca;
32885     X509* x509Svr;
32886     X509* issuer;
32887 
32888     WOLFSSL_STACK* sk = NULL;
32889     X509_NAME* caName;
32890     X509_NAME* issuerName;
32891 
32892     FILE* file1 = NULL;
32893     int i, cert_count, cmp;
32894 
32895     char der[] = "certs/ca-cert.der";
32896 
32897 #ifdef HAVE_CRL
32898     char pem[][100] = {
32899         "./certs/crl/crl.pem",
32900         "./certs/crl/crl2.pem",
32901         "./certs/crl/caEccCrl.pem",
32902         "./certs/crl/eccCliCRL.pem",
32903         "./certs/crl/eccSrvCRL.pem",
32904         ""
32905     };
32906 #endif
32907     printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
32908     AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
32909 
32910     AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
32911     fclose(file1);
32912 
32913     AssertNotNull(ctx = X509_STORE_CTX_new());
32914     AssertNotNull((str = wolfSSL_X509_STORE_new()));
32915     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32916     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
32917                                     SSL_FILETYPE_PEM,NULL), 1);
32918     AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
32919     AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
32920 
32921     /* check if CA cert is loaded into the store */
32922     for (i = 0; i < cert_count; i++) {
32923         x509Ca = sk_X509_value(sk, i);
32924         AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
32925     }
32926 
32927     AssertNotNull((x509Svr =
32928             wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
32929 
32930     AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
32931 
32932     AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
32933     issuer = X509_STORE_CTX_get0_current_issuer(ctx);
32934     AssertNotNull(issuer);
32935 
32936     caName = X509_get_subject_name(x509Ca);
32937     AssertNotNull(caName);
32938     issuerName = X509_get_subject_name(issuer);
32939     AssertNotNull(issuerName);
32940     cmp = X509_NAME_cmp(caName, issuerName);
32941     AssertIntEQ(cmp, 0);
32942 
32943     /* load der format */
32944     X509_free(issuer);
32945     X509_STORE_CTX_free(ctx);
32946     X509_STORE_free(str);
32947     sk_X509_pop_free(sk, NULL);
32948     X509_free(x509Svr);
32949 
32950     AssertNotNull((str = wolfSSL_X509_STORE_new()));
32951     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32952     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
32953                                     SSL_FILETYPE_ASN1,NULL), 1);
32954     AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
32955     AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
32956     /* check if CA cert is loaded into the store */
32957     for (i = 0; i < cert_count; i++) {
32958         x509Ca = sk_X509_value(sk, i);
32959         AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
32960     }
32961 
32962     X509_STORE_free(str);
32963     sk_X509_pop_free(sk, NULL);
32964     X509_free(cert1);
32965 
32966 #ifdef HAVE_CRL
32967     AssertNotNull(str = wolfSSL_X509_STORE_new());
32968     AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
32969     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
32970                                                     SSL_FILETYPE_PEM,NULL), 1);
32971     AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
32972                                 "certs/server-revoked-cert.pem",
32973                                  SSL_FILETYPE_PEM,NULL), 1);
32974     if (str) {
32975         AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
32976                     WOLFSSL_FILETYPE_PEM), 1);
32977         /* since store hasn't yet known the revoked cert*/
32978         AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
32979                     "certs/server-revoked-cert.pem",
32980                     WOLFSSL_FILETYPE_PEM), 1);
32981     }
32982     for (i = 0; pem[i][0] != '\0'; i++)
32983     {
32984         AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
32985                                         SSL_FILETYPE_PEM, NULL), 1);
32986     }
32987 
32988     if (str) {
32989         /* since store knows crl list */
32990         AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
32991                     "certs/server-revoked-cert.pem",
32992                     WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
32993     }
32994 
32995     AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
32996     X509_STORE_free(str);
32997 
32998 #endif
32999 
33000 
33001     printf(resultFmt, passed);
33002 #endif
33003 }
33004 static void test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
33005 {
33006 #if defined(OPENSSL_EXTRA)
33007     printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
33008 
33009     X509_STORE_CTX_cleanup(NULL);
33010     X509_STORE_CTX_trusted_stack(NULL, NULL);
33011     AssertTrue(1);   /* to confirm previous call gives no harm */
33012     printf(resultFmt, passed);
33013 #endif
33014 }
33015 static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
33016 {
33017 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
33018     #ifdef WOLFSSL_SIGNER_DER_CERT
33019     int cmp;
33020     #endif
33021     X509_STORE_CTX* ctx;
33022     X509_STORE* str;
33023     X509* x509Ca;
33024     X509* x509Svr;
33025     X509* issuer;
33026     X509_NAME* caName;
33027     X509_NAME* issuerName;
33028 
33029     printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
33030 
33031     AssertNotNull(ctx = X509_STORE_CTX_new());
33032     AssertNotNull((str = wolfSSL_X509_STORE_new()));
33033     AssertNotNull((x509Ca =
33034             wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
33035     AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
33036     AssertNotNull((x509Svr =
33037             wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
33038 
33039     AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
33040 
33041     AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
33042     issuer = X509_STORE_CTX_get0_current_issuer(ctx);
33043     AssertNotNull(issuer);
33044 
33045     caName = X509_get_subject_name(x509Ca);
33046     AssertNotNull(caName);
33047     issuerName = X509_get_subject_name(issuer);
33048     #ifdef WOLFSSL_SIGNER_DER_CERT
33049         AssertNotNull(issuerName);
33050         cmp = X509_NAME_cmp(caName, issuerName);
33051         AssertIntEQ(cmp, 0);
33052     #else
33053         AssertNull(issuerName);
33054     #endif
33055 
33056     X509_free(issuer);
33057     X509_STORE_CTX_free(ctx);
33058     X509_free(x509Svr);
33059     X509_STORE_free(str);
33060     X509_free(x509Ca);
33061 
33062     printf(resultFmt, passed);
33063 #endif
33064 }
33065 
33066 static void test_wolfSSL_PKCS7_certs(void)
33067 {
33068 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
33069    !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
33070     STACK_OF(X509)* sk = NULL;
33071     STACK_OF(X509_INFO)* info_sk = NULL;
33072     PKCS7 *p7 = NULL;
33073     BIO* bio;
33074     const byte* p = NULL;
33075     int buflen = 0;
33076     int i;
33077 
33078     printf(testingFmt, "wolfSSL_PKCS7_certs()");
33079 
33080     /* Test twice. Once with d2i and once without to test
33081      * that everything is free'd correctly. */
33082     for (i = 0; i < 2; i++) {
33083         AssertNotNull(p7 = PKCS7_new());
33084         p7->version = 1;
33085         p7->hashOID = SHAh;
33086         AssertNotNull(bio = BIO_new(BIO_s_file()));
33087         AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
33088         AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
33089         AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
33090         AssertNotNull(sk = sk_X509_new_null());
33091         while (sk_X509_INFO_num(info_sk)) {
33092             X509_INFO* info;
33093             AssertNotNull(info = sk_X509_INFO_shift(info_sk));
33094             AssertIntEQ(sk_X509_push(sk, info->x509), 1);
33095             info->x509 = NULL;
33096             X509_INFO_free(info);
33097         }
33098         sk_X509_INFO_free(info_sk);
33099         BIO_free(bio);
33100         bio = BIO_new(BIO_s_mem());
33101         AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
33102         AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
33103 
33104         if (i == 0) {
33105             PKCS7_free(p7);
33106             AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
33107             /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
33108             ((WOLFSSL_PKCS7*)p7)->certs = NULL;
33109             /* PKCS7_free free's the certs */
33110             AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
33111         }
33112 
33113         BIO_free(bio);
33114         PKCS7_free(p7);
33115     }
33116 
33117     printf(resultFmt, passed);
33118 #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
33119          !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
33120 }
33121 
33122 static void test_wolfSSL_X509_STORE_CTX(void)
33123 {
33124     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
33125        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
33126 
33127     X509_STORE_CTX* ctx;
33128     X509_STORE* str;
33129     X509* x509;
33130 #ifdef OPENSSL_ALL
33131     X509* x5092;
33132     STACK_OF(X509) *sk, *sk2, *sk3;
33133 #endif
33134 
33135     printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
33136     AssertNotNull(ctx = X509_STORE_CTX_new());
33137     AssertNotNull((str = wolfSSL_X509_STORE_new()));
33138     AssertNotNull((x509 =
33139                 wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
33140     AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
33141 #ifdef OPENSSL_ALL
33142     /* sk_X509_new only in OPENSSL_ALL */
33143     sk = sk_X509_new();
33144     AssertNotNull(sk);
33145     AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
33146 #else
33147     AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
33148 #endif
33149     AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
33150     X509_STORE_CTX_set_error(ctx, -5);
33151     X509_STORE_CTX_set_error(NULL, -5);
33152 
33153     X509_STORE_CTX_free(ctx);
33154 #ifdef OPENSSL_ALL
33155     sk_X509_pop_free(sk, NULL);
33156 #endif
33157     X509_STORE_free(str);
33158     X509_free(x509);
33159 
33160     AssertNotNull(ctx = X509_STORE_CTX_new());
33161     X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
33162     X509_STORE_CTX_free(ctx);
33163 
33164 #ifdef OPENSSL_ALL
33165     /* test X509_STORE_CTX_get(1)_chain */
33166     AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
33167                                                      SSL_FILETYPE_PEM)));
33168     AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
33169                                                      SSL_FILETYPE_PEM)));
33170     AssertNotNull((sk = sk_X509_new()));
33171     AssertIntEQ(sk_X509_push(sk, x509), 1);
33172     AssertNotNull((str = X509_STORE_new()));
33173     AssertNotNull((ctx = X509_STORE_CTX_new()));
33174     AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
33175     AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
33176     AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
33177     AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
33178     AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
33179     AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
33180     AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
33181     X509_STORE_CTX_free(ctx);
33182     X509_STORE_free(str);
33183     /* CTX certs not freed yet */
33184     X509_free(x5092);
33185     sk_X509_pop_free(sk, NULL);
33186     /* sk3 is dup so free here */
33187     sk_X509_pop_free(sk3, NULL);
33188 #endif
33189 
33190     /* test X509_STORE_CTX_get/set_ex_data */
33191     {
33192         int i = 0, tmpData = 5;
33193         void* tmpDataRet;
33194         AssertNotNull(ctx = X509_STORE_CTX_new());
33195     #ifdef HAVE_EX_DATA
33196         for (i = 0; i < MAX_EX_DATA; i++) {
33197             AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
33198                         WOLFSSL_SUCCESS);
33199             tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
33200             AssertNotNull(tmpDataRet);
33201             AssertIntEQ(tmpData, *(int*)tmpDataRet);
33202         }
33203     #else
33204         AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
33205                     WOLFSSL_FAILURE);
33206         tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
33207         AssertNull(tmpDataRet);
33208     #endif
33209         X509_STORE_CTX_free(ctx);
33210     }
33211 
33212     /* test X509_STORE_get/set_ex_data */
33213     {
33214         int i = 0, tmpData = 99;
33215         void* tmpDataRet;
33216         AssertNotNull(str = X509_STORE_new());
33217     #ifdef HAVE_EX_DATA
33218         for (i = 0; i < MAX_EX_DATA; i++) {
33219             AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
33220                         WOLFSSL_SUCCESS);
33221             tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
33222             AssertNotNull(tmpDataRet);
33223             AssertIntEQ(tmpData, *(int*)tmpDataRet);
33224         }
33225     #else
33226         AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
33227                     WOLFSSL_FAILURE);
33228         tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
33229         AssertNull(tmpDataRet);
33230     #endif
33231         X509_STORE_free(str);
33232     }
33233 
33234     printf(resultFmt, passed);
33235     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
33236              !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
33237 }
33238 
33239 static void test_wolfSSL_X509_STORE_set_flags(void)
33240 {
33241     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
33242        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
33243 
33244     X509_STORE* store;
33245     X509* x509;
33246 
33247     printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
33248     AssertNotNull((store = wolfSSL_X509_STORE_new()));
33249     AssertNotNull((x509 =
33250                 wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
33251     AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
33252 
33253 #ifdef HAVE_CRL
33254     AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
33255 #else
33256     AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
33257         NOT_COMPILED_IN);
33258 #endif
33259 
33260     wolfSSL_X509_free(x509);
33261     wolfSSL_X509_STORE_free(store);
33262 
33263     printf(resultFmt, passed);
33264     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
33265              !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
33266 }
33267 
33268 static void test_wolfSSL_X509_LOOKUP_load_file(void)
33269 {
33270     #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
33271        !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
33272        (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
33273     WOLFSSL_X509_STORE*  store;
33274     WOLFSSL_X509_LOOKUP* lookup;
33275 
33276     printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
33277 
33278     AssertNotNull(store = wolfSSL_X509_STORE_new());
33279     AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
33280     AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
33281                                               X509_FILETYPE_PEM), 1);
33282     AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
33283                                                          X509_FILETYPE_PEM), 1);
33284 
33285     if (store) {
33286         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
33287                     WOLFSSL_FILETYPE_PEM), 1);
33288         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
33289                     WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
33290     }
33291     AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
33292                                               X509_FILETYPE_PEM), 1);
33293     if (store) {
33294         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
33295                     WOLFSSL_FILETYPE_PEM), 1);
33296     }
33297 
33298     wolfSSL_X509_STORE_free(store);
33299 
33300     printf(resultFmt, passed);
33301     #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
33302              !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
33303 }
33304 
33305 static void test_wolfSSL_X509_STORE_CTX_set_time(void)
33306 {
33307     #if defined(OPENSSL_EXTRA)
33308     WOLFSSL_X509_STORE_CTX*  ctx;
33309     time_t c_time;
33310 
33311     printf(testingFmt, "wolfSSL_X509_set_time()");
33312     AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
33313     c_time = 365*24*60*60;
33314     wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
33315     AssertTrue(
33316       (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
33317     AssertTrue(ctx->param->check_time == c_time);
33318     wolfSSL_X509_STORE_CTX_free(ctx);
33319 
33320     printf(resultFmt, passed);
33321     #endif /* OPENSSL_EXTRA */
33322 }
33323 
33324 static void test_wolfSSL_CTX_get0_set1_param(void)
33325 {
33326 #if defined(OPENSSL_EXTRA)
33327     int ret;
33328     SSL_CTX* ctx;
33329     WOLFSSL_X509_VERIFY_PARAM* pParam;
33330     WOLFSSL_X509_VERIFY_PARAM* pvpm;
33331     char testIPv4[] = "127.0.0.1";
33332     char testhostName[] = "foo.hoge.com";
33333 
33334     printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
33335 
33336     #ifndef NO_WOLFSSL_SERVER
33337     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
33338     #else
33339     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
33340     #endif
33341 
33342     AssertNull(SSL_CTX_get0_param(NULL));
33343     AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
33344 
33345     pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
33346                 sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
33347     AssertNotNull(pvpm);
33348     XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
33349 
33350     wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
33351                                         (int)XSTRLEN(testhostName));
33352     wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
33353     wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
33354 
33355     ret = SSL_CTX_set1_param(ctx, pvpm);
33356     AssertIntEQ(1, ret);
33357     AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
33358                                          (int)XSTRLEN(testhostName)));
33359     AssertIntEQ(0x01, pParam->hostFlags);
33360     AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
33361 
33362     /* test for incorrect patameter */
33363     AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
33364     AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
33365     AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
33366 
33367     SSL_CTX_free(ctx);
33368 
33369     XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
33370 
33371     printf(resultFmt, passed);
33372 #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
33373 }
33374 
33375 static void test_wolfSSL_get0_param(void)
33376 {
33377 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
33378     SSL_CTX* ctx;
33379     SSL*     ssl;
33380     WOLFSSL_X509_VERIFY_PARAM* pParam;
33381 
33382     printf(testingFmt, "wolfSSL_get0_param()");
33383 
33384     #ifndef NO_WOLFSSL_SERVER
33385     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
33386     #else
33387     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
33388     #endif
33389     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
33390     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
33391     AssertNotNull(ssl = SSL_new(ctx));
33392 
33393     pParam = SSL_get0_param(ssl);
33394 
33395     (void)pParam;
33396 
33397     SSL_free(ssl);
33398     SSL_CTX_free(ctx);
33399     printf(resultFmt, passed);
33400 #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
33401 }
33402 
33403 static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
33404 {
33405 #if defined(OPENSSL_EXTRA)
33406     const char host[] = "www.example.com";
33407     WOLFSSL_X509_VERIFY_PARAM* pParam;
33408 
33409     printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
33410 
33411     AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
33412                            sizeof(WOLFSSL_X509_VERIFY_PARAM),
33413                            HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
33414 
33415     XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
33416 
33417     X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
33418 
33419     AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
33420 
33421     XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
33422 
33423     AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
33424 
33425     XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
33426 
33427     printf(resultFmt, passed);
33428 #endif /* OPENSSL_EXTRA */
33429 }
33430 
33431 static void test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
33432 {
33433 #if defined(OPENSSL_EXTRA)
33434     unsigned char buf[16] = {0};
33435     WOLFSSL_X509_VERIFY_PARAM* param;
33436 
33437     printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
33438 
33439     AssertNotNull(param = X509_VERIFY_PARAM_new());
33440 
33441     /* test 127.0.0.1 */
33442     buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
33443     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
33444     AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
33445 
33446     /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
33447     buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
33448     buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
33449     buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
33450     buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
33451     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
33452     AssertIntEQ(XSTRNCMP(param->ipasc,
33453         "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
33454 
33455     /* test 2001:db8:: */
33456     buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
33457     buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
33458     buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
33459     buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
33460     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
33461     AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
33462 
33463     /* test ::1234:5678 */
33464     buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
33465     buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
33466     buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
33467     buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
33468     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
33469     AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
33470 
33471 
33472     /* test 2001:db8::1234:5678 */
33473     buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
33474     buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
33475     buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
33476     buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
33477     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
33478     AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
33479                                                 sizeof(param->ipasc)), 0);
33480 
33481     /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
33482     /*      2001:db8:1::ab9:c0a8:102 */
33483     buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
33484     buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
33485     buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
33486     buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
33487     AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
33488     AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
33489                                                 sizeof(param->ipasc)), 0);
33490 
33491     XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
33492     printf(resultFmt, passed);
33493 #endif /* OPENSSL_EXTRA */
33494 }
33495 
33496 static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
33497 {
33498     #if defined(OPENSSL_EXTRA)
33499     X509_STORE* store;
33500     X509_STORE_CTX* ctx;
33501     X509_STORE_CTX* ctx_no_init;
33502 
33503     printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
33504     AssertNotNull((store = X509_STORE_new()));
33505     AssertNotNull(ctx = X509_STORE_CTX_new());
33506     AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
33507     AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
33508 
33509     AssertNull(X509_STORE_CTX_get0_store(NULL));
33510     /* should return NULL if ctx has not bee initialized */
33511     AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
33512     AssertNotNull(X509_STORE_CTX_get0_store(ctx));
33513 
33514     wolfSSL_X509_STORE_CTX_free(ctx);
33515     wolfSSL_X509_STORE_CTX_free(ctx_no_init);
33516     X509_STORE_free(store);
33517 
33518     printf(resultFmt, passed);
33519     #endif /* OPENSSL_EXTRA */
33520 }
33521 
33522 static void test_wolfSSL_CTX_set_client_CA_list(void)
33523 {
33524 #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
33525     !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
33526     WOLFSSL_CTX* ctx;
33527     WOLFSSL* ssl;
33528     X509_NAME* name = NULL;
33529     STACK_OF(X509_NAME)* names = NULL;
33530     STACK_OF(X509_NAME)* ca_list = NULL;
33531     int i, names_len;
33532 
33533     printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
33534     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
33535     /* Send two X501 names in cert request */
33536     names = SSL_load_client_CA_file(cliCertFile);
33537     AssertNotNull(names);
33538     ca_list = SSL_load_client_CA_file(caCertFile);
33539     AssertNotNull(ca_list);
33540     AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
33541     SSL_CTX_set_client_CA_list(ctx, names);
33542     /* This should only free the stack structure */
33543     sk_X509_NAME_free(ca_list);
33544     AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
33545     AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
33546 
33547     AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
33548     for (i=0; i<names_len; i++) {
33549         AssertNotNull(name = sk_X509_NAME_value(names, i));
33550         AssertIntEQ(sk_X509_NAME_find(names, name), i);
33551     }
33552 
33553     /* Needed to be able to create ssl object */
33554     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
33555     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
33556     AssertNotNull(ssl = wolfSSL_new(ctx));
33557     /* load again as old names are responsibility of ctx to free*/
33558     names = SSL_load_client_CA_file(cliCertFile);
33559     AssertNotNull(names);
33560     SSL_set_client_CA_list(ssl, names);
33561     AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
33562     AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
33563 
33564     AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
33565     for (i=0; i<names_len; i++) {
33566         AssertNotNull(name = sk_X509_NAME_value(names, i));
33567         AssertIntEQ(sk_X509_NAME_find(names, name), i);
33568     }
33569 
33570     printf(resultFmt, passed);
33571 
33572 #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
33573     {
33574         tcp_ready ready;
33575         func_args server_args;
33576         callback_functions server_cb;
33577         THREAD_TYPE serverThread;
33578         WOLFSSL* ssl_client;
33579         WOLFSSL_CTX* ctx_client;
33580         SOCKET_T sockfd = 0;
33581 
33582         printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
33583 
33584         StartTCP();
33585         InitTcpReady(&ready);
33586 
33587         XMEMSET(&server_args, 0, sizeof(func_args));
33588         XMEMSET(&server_cb, 0, sizeof(callback_functions));
33589 
33590         server_args.signal    = &ready;
33591         server_args.callbacks = &server_cb;
33592 
33593         /* we are responsible for free'ing WOLFSSL_CTX */
33594         server_cb.ctx = ctx;
33595         server_cb.isSharedCtx = 1;
33596 
33597         AssertIntEQ(WOLFSSL_SUCCESS,
33598                 wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
33599 
33600         start_thread(test_server_nofail, &server_args, &serverThread);
33601         wait_tcp_ready(&server_args);
33602 
33603         tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
33604         AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
33605         AssertIntEQ(WOLFSSL_SUCCESS,
33606                 wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
33607         AssertIntEQ(WOLFSSL_SUCCESS,
33608               wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
33609         AssertIntEQ(WOLFSSL_SUCCESS,
33610                 wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
33611 
33612         AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
33613         AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
33614         AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
33615 
33616         AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
33617         /* We are expecting two cert names to be sent */
33618         AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
33619 
33620         AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
33621         for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
33622             AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
33623             AssertIntGE(sk_X509_NAME_find(names, name), 0);
33624         }
33625 
33626         wolfSSL_shutdown(ssl_client);
33627         wolfSSL_free(ssl_client);
33628         wolfSSL_CTX_free(ctx_client);
33629 
33630         join_thread(serverThread);
33631         FreeTcpReady(&ready);
33632 
33633         printf(resultFmt, passed);
33634     }
33635 #endif
33636 
33637     wolfSSL_free(ssl);
33638     wolfSSL_CTX_free(ctx);
33639 #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
33640 }
33641 
33642 static void test_wolfSSL_CTX_add_client_CA(void)
33643 {
33644 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
33645     !defined(NO_WOLFSSL_CLIENT)
33646     WOLFSSL_CTX* ctx;
33647     WOLFSSL_X509* x509;
33648     WOLFSSL_X509* x509_a;
33649     STACK_OF(X509_NAME)* ca_list;
33650     int ret = 0;
33651 
33652     printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
33653     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
33654     /* Add client cert */
33655     x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
33656     AssertNotNull(x509);
33657     ret = SSL_CTX_add_client_CA(ctx, x509);
33658     AssertIntEQ(ret, SSL_SUCCESS);
33659     AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
33660     /* Add another client cert */
33661     AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
33662                                                         SSL_FILETYPE_PEM));
33663     AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
33664 
33665    /* test for incorrect parameter */
33666     AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
33667     AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
33668     AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
33669 
33670     X509_free(x509);
33671     X509_free(x509_a);
33672     SSL_CTX_free(ctx);
33673 
33674     printf(resultFmt, passed);
33675 #endif /* OPENSSL_EXTRA  && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
33676 }
33677 #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
33678 static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
33679 {
33680     callback_functions* callbacks = ((func_args*)args)->callbacks;
33681     WOLFSSL_CTX* ctx       = wolfSSL_CTX_new(callbacks->method());
33682     WOLFSSL*  ssl   = NULL;
33683     SOCKET_T  sfd   = 0;
33684     SOCKET_T  cfd   = 0;
33685     word16    port;
33686     char      msg[] = "I hear you fa shizzle!";
33687     int       len   = (int) XSTRLEN(msg);
33688     char      input[1024];
33689     int       idx;
33690     int       ret, err = 0;
33691 
33692 #ifdef WOLFSSL_TIRTOS
33693     fdOpenSession(Task_self());
33694 #endif
33695     ((func_args*)args)->return_code = TEST_FAIL;
33696     port = ((func_args*)args)->signal->port;
33697 
33698     AssertIntEQ(WOLFSSL_SUCCESS,
33699         wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
33700 
33701     AssertIntEQ(WOLFSSL_SUCCESS,
33702         wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
33703             WOLFSSL_FILETYPE_PEM));
33704 
33705     AssertIntEQ(WOLFSSL_SUCCESS,
33706         wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
33707                  WOLFSSL_FILETYPE_PEM));
33708 
33709     if (callbacks->ctx_ready)
33710         callbacks->ctx_ready(ctx);
33711 
33712     ssl = wolfSSL_new(ctx);
33713     tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
33714     CloseSocket(sfd);
33715     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
33716 
33717     if (callbacks->ssl_ready)
33718         callbacks->ssl_ready(ssl);
33719 
33720     do {
33721         err = 0; /* Reset error */
33722         ret = wolfSSL_accept(ssl);
33723         if (ret != WOLFSSL_SUCCESS) {
33724             err = wolfSSL_get_error(ssl, 0);
33725         }
33726     } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
33727 
33728     if (ret != WOLFSSL_SUCCESS) {
33729         char buff[WOLFSSL_MAX_ERROR_SZ];
33730         printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
33731     }
33732     else {
33733         if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
33734             input[idx] = 0;
33735             printf("Client message: %s\n", input);
33736         }
33737 
33738         AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
33739 #ifdef WOLFSSL_TIRTOS
33740         Task_yield();
33741 #endif
33742         ((func_args*)args)->return_code = TEST_SUCCESS;
33743     }
33744 
33745     if (callbacks->on_result)
33746         callbacks->on_result(ssl);
33747 
33748     wolfSSL_shutdown(ssl);
33749     wolfSSL_free(ssl);
33750     wolfSSL_CTX_free(ctx);
33751     CloseSocket(cfd);
33752 
33753 #ifdef WOLFSSL_TIRTOS
33754     fdCloseSession(Task_self());
33755 #endif
33756 #ifndef WOLFSSL_TIRTOS
33757     return 0;
33758 #endif
33759 }
33760 
33761 static void keyLog_callback(const WOLFSSL* ssl, const char* line )
33762 {
33763 
33764     AssertNotNull(ssl);
33765     AssertNotNull(line);
33766 
33767     XFILE fp;
33768     const byte  lf = '\n';
33769     fp = XFOPEN("./MyKeyLog.txt", "a");
33770     XFWRITE( line, 1, strlen(line),fp);
33771     XFWRITE( (void*)&lf,1,1,fp);
33772     XFCLOSE(fp);
33773 
33774 }
33775 #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
33776 static void test_wolfSSL_CTX_set_keylog_callback(void)
33777 {
33778 #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
33779     SSL_CTX* ctx;
33780 
33781     printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
33782     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
33783     SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
33784     SSL_CTX_free(ctx);
33785     SSL_CTX_set_keylog_callback(NULL, NULL);
33786     printf(resultFmt, passed);
33787 
33788 #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
33789 }
33790 static void test_wolfSSL_CTX_get_keylog_callback(void)
33791 {
33792 #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
33793     SSL_CTX* ctx;
33794 
33795     printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
33796     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
33797     AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
33798     SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
33799     AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
33800     SSL_CTX_set_keylog_callback(ctx, NULL );
33801     AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
33802     SSL_CTX_free(ctx);
33803     printf(resultFmt, passed);
33804 
33805 #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
33806 }
33807 static void test_wolfSSL_Tls12_Key_Logging_test(void)
33808 {
33809  #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
33810  /* This test is intended for checking whether keylog callback is called
33811   * in client during TLS handshake between the client and a server.
33812   */
33813 
33814     tcp_ready ready;
33815     func_args client_args;
33816     func_args server_args;
33817     THREAD_TYPE serverThread;
33818     callback_functions server_cbf;
33819     callback_functions client_cbf;
33820     SOCKET_T sockfd = 0;
33821     WOLFSSL_CTX* ctx;
33822     WOLFSSL*     ssl;
33823     XFILE fp;
33824     char msg[64] = "hello wolfssl!";
33825     char reply[1024];
33826     int  msgSz = (int)XSTRLEN(msg);
33827 
33828     printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
33829 
33830 #ifdef WOLFSSL_TIRTOS
33831     fdOpenSession(Task_self());
33832 #endif
33833 
33834     InitTcpReady(&ready);
33835     ready.port = 22222;
33836 
33837     XMEMSET(&client_args, 0, sizeof(func_args));
33838     XMEMSET(&server_args, 0, sizeof(func_args));
33839     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
33840     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
33841     server_cbf.method     = wolfTLSv1_2_server_method;
33842 
33843     server_args.callbacks = &server_cbf;
33844     server_args.signal    = &ready;
33845 
33846     /* clean up keylog file */
33847     fp = XFOPEN("./MyKeyLog.txt", "w");
33848     XFCLOSE(fp);
33849 
33850     /* start server task */
33851     start_thread(server_task, &server_args, &serverThread);
33852     wait_tcp_ready(&server_args);
33853 
33854 
33855     /* run as a TLS1.2 client */
33856     AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
33857     AssertIntEQ(WOLFSSL_SUCCESS,
33858             wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
33859     AssertIntEQ(WOLFSSL_SUCCESS,
33860         wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
33861     AssertIntEQ(WOLFSSL_SUCCESS,
33862         wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
33863 
33864     tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
33865 
33866     /* set keylog callback */
33867     wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
33868 
33869     /* get connected the server task */
33870     AssertNotNull(ssl = wolfSSL_new(ctx));
33871     AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
33872 
33873 
33874     AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
33875 
33876     AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
33877     AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
33878     wolfSSL_shutdown(ssl);
33879     wolfSSL_free(ssl);
33880     wolfSSL_CTX_free(ctx);
33881     CloseSocket(sockfd);
33882     join_thread(serverThread);
33883 
33884     FreeTcpReady(&ready);
33885 
33886 #ifdef WOLFSSL_TIRTOS
33887     fdOpenSession(Task_self());
33888 #endif
33889 
33890     /* check if the keylog file exists */
33891 
33892     char  buff[300] = {0};
33893     int  found = 0;
33894 
33895     fp = XFOPEN("./MyKeyLog.txt", "r");
33896 
33897     AssertNotNull(fp);
33898 
33899     while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
33900         if(0 == strncmp(buff,"CLIENT_RANDOM ",
33901                     sizeof("CLIENT_RANDOM ")-1)) {
33902             found = 1;
33903             break;
33904         }
33905     }
33906     XFCLOSE(fp);
33907     /* a log starting with "CLIENT_RANDOM " should exit in the file */
33908     AssertNotNull( found );
33909     printf(resultFmt, passed);
33910 
33911 #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
33912 }
33913 static void test_wolfSSL_Tls13_Key_Logging_test(void)
33914 {
33915  #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
33916     defined(HAVE_SECRET_CALLBACK)
33917  /* This test is intended for checking whether keylog callback is called
33918   * in client during TLS handshake between the client and a server.
33919   */
33920 
33921     tcp_ready ready;
33922     func_args client_args;
33923     func_args server_args;
33924     THREAD_TYPE serverThread;
33925     callback_functions server_cbf;
33926     callback_functions client_cbf;
33927     SOCKET_T sockfd = 0;
33928     WOLFSSL_CTX* ctx;
33929     WOLFSSL*     ssl;
33930     XFILE fp;
33931     char msg[64] = "hello wolfssl!";
33932     char reply[1024];
33933     int  msgSz = (int)XSTRLEN(msg);
33934 
33935     printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
33936 
33937 #ifdef WOLFSSL_TIRTOS
33938     fdOpenSession(Task_self());
33939 #endif
33940 
33941     InitTcpReady(&ready);
33942     ready.port = 22222;
33943 
33944     XMEMSET(&client_args, 0, sizeof(func_args));
33945     XMEMSET(&server_args, 0, sizeof(func_args));
33946     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
33947     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
33948     server_cbf.method     = wolfTLSv1_3_server_method;  /* TLS1.3 */
33949 
33950     server_args.callbacks = &server_cbf;
33951     server_args.signal    = &ready;
33952 
33953     /* clean up keylog file */
33954     fp = XFOPEN("./MyKeyLog.txt", "w");
33955     XFCLOSE(fp);
33956 
33957     /* start server task */
33958     start_thread(server_task, &server_args, &serverThread);
33959     wait_tcp_ready(&server_args);
33960 
33961 
33962     /* run as a TLS1.2 client */
33963     AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
33964     AssertIntEQ(WOLFSSL_SUCCESS,
33965             wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
33966     AssertIntEQ(WOLFSSL_SUCCESS,
33967         wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
33968     AssertIntEQ(WOLFSSL_SUCCESS,
33969         wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
33970 
33971     tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
33972 
33973     /* set keylog callback */
33974     wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
33975 
33976     /* get connected the server task */
33977     AssertNotNull(ssl = wolfSSL_new(ctx));
33978     AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
33979     AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
33980     AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
33981     AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
33982     wolfSSL_free(ssl);
33983     wolfSSL_CTX_free(ctx);
33984 
33985     join_thread(serverThread);
33986 
33987     FreeTcpReady(&ready);
33988 
33989 #ifdef WOLFSSL_TIRTOS
33990     fdOpenSession(Task_self());
33991 #endif
33992 
33993     /* check if the keylog file exists */
33994 
33995     char  buff[300] = {0};
33996     int  found[4]   = {0};
33997 
33998     fp = XFOPEN("./MyKeyLog.txt", "r");
33999 
34000     AssertNotNull(fp);
34001 
34002     while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
34003         if(0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
34004                     sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
34005             found[0] = 1;
34006             continue;
34007         }
34008         else if(0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
34009                     sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
34010             found[1] = 1;
34011             continue;
34012         }
34013         else if(0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
34014                     sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
34015             found[2] = 1;
34016             continue;
34017         }
34018         else if(0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
34019                     sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
34020             found[3] = 1;
34021             continue;
34022         }
34023     }
34024     XFCLOSE(fp);
34025     int numfnd = 0;
34026     for( uint i = 0; i < 4; i++) {
34027         if( found[i] != 0)
34028             numfnd++;
34029     }
34030     AssertIntEQ( numfnd,4 );
34031 
34032     printf(resultFmt, passed);
34033 
34034 #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
34035 }
34036 
34037 #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
34038 defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
34039     defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
34040 static void post_auth_version_cb(WOLFSSL* ssl)
34041 {
34042     /* do handshake and then test version error */
34043     AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
34044     AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
34045     AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
34046 #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
34047     /* check was added to error queue */
34048     AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
34049 
34050     /* check the string matches expected string */
34051     AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
34052             "WRONG_SSL_VERSION");
34053 #endif
34054 }
34055 
34056 static void post_auth_cb(WOLFSSL* ssl)
34057 {
34058     /* do handshake and then test version error */
34059     AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
34060     AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
34061     AssertNull(wolfSSL_get_peer_certificate(ssl));
34062     AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
34063 }
34064 
34065 static void set_post_auth_cb(WOLFSSL* ssl)
34066 {
34067     if (!wolfSSL_is_server(ssl)) {
34068         AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
34069     }
34070     else {
34071         wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
34072     }
34073 }
34074 #endif
34075 
34076 static void test_wolfSSL_Tls13_postauth(void)
34077 {
34078 #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
34079     defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
34080     defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
34081     tcp_ready ready;
34082     func_args client_args;
34083     func_args server_args;
34084     callback_functions server_cbf;
34085     callback_functions client_cbf;
34086     THREAD_TYPE serverThread;
34087 
34088     printf(testingFmt, "wolfSSL_Tls13_postauth()");
34089     XMEMSET(&client_args, 0, sizeof(func_args));
34090     XMEMSET(&server_args, 0, sizeof(func_args));
34091 
34092     StartTCP();
34093     InitTcpReady(&ready);
34094 
34095 #if defined(USE_WINDOWS_API)
34096     /* use RNG to get random port if using windows */
34097     ready.port = GetRandomPort();
34098 #endif
34099 
34100     server_args.signal = &ready;
34101     client_args.signal = &ready;
34102 
34103     /* test version failure doing post auth with TLS 1.2 connection */
34104     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
34105     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
34106     server_cbf.method = wolfTLSv1_2_server_method;
34107     server_cbf.ssl_ready = set_post_auth_cb;
34108     client_cbf.ssl_ready = set_post_auth_cb;
34109     server_cbf.on_result = post_auth_version_cb;
34110     server_args.callbacks = &server_cbf;
34111     client_args.callbacks = &client_cbf;
34112 
34113     start_thread(test_server_nofail, &server_args, &serverThread);
34114     wait_tcp_ready(&server_args);
34115     test_client_nofail(&client_args, NULL);
34116     join_thread(serverThread);
34117 
34118     /* tests on post auth with TLS 1.3 */
34119     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
34120     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
34121     server_cbf.method = wolfTLSv1_3_server_method;
34122     server_cbf.ssl_ready = set_post_auth_cb;
34123     client_cbf.ssl_ready = set_post_auth_cb;
34124     server_cbf.on_result = post_auth_cb;
34125     server_args.callbacks = &server_cbf;
34126     client_args.callbacks = &client_cbf;
34127 
34128     start_thread(test_server_nofail, &server_args, &serverThread);
34129     wait_tcp_ready(&server_args);
34130     test_client_nofail(&client_args, NULL);
34131     join_thread(serverThread);
34132 
34133     FreeTcpReady(&ready);
34134     printf(resultFmt, passed);
34135 #endif
34136 }
34137 
34138 
34139 static void test_wolfSSL_X509_NID(void)
34140 {
34141     #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
34142     !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
34143     int   sigType;
34144     int   nameSz;
34145 
34146     X509*  cert;
34147     EVP_PKEY*  pubKeyTmp;
34148     X509_NAME* name;
34149 
34150     char commonName[80];
34151     char countryName[80];
34152     char localityName[80];
34153     char stateName[80];
34154     char orgName[80];
34155     char orgUnit[80];
34156 
34157     printf(testingFmt, "wolfSSL_X509_NID()");
34158     /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
34159 
34160     /* convert cert from DER to internal WOLFSSL_X509 struct */
34161     AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
34162             sizeof_client_cert_der_2048));
34163 
34164     /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
34165 
34166     /* extract PUBLIC KEY from cert */
34167     AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
34168 
34169     /* extract signatureType */
34170     AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
34171 
34172     /* extract subjectName info */
34173     AssertNotNull(name = X509_get_subject_name(cert));
34174     AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
34175     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
34176                                            NULL, 0)), 0);
34177     AssertIntEQ(nameSz, 15);
34178     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
34179                                            commonName, sizeof(commonName))), 0);
34180     AssertIntEQ(nameSz, 15);
34181     AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
34182     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
34183                                             commonName, 9)), 0);
34184     AssertIntEQ(nameSz, 8);
34185     AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
34186 
34187     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
34188                                          countryName, sizeof(countryName))), 0);
34189     AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
34190 
34191     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
34192                                        localityName, sizeof(localityName))), 0);
34193     AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
34194 
34195     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
34196                                             stateName, sizeof(stateName))), 0);
34197     AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
34198 
34199     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
34200                                             orgName, sizeof(orgName))), 0);
34201     AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
34202 
34203     AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
34204                                             orgUnit, sizeof(orgUnit))), 0);
34205     AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
34206 
34207     EVP_PKEY_free(pubKeyTmp);
34208     X509_free(cert);
34209 
34210     printf(resultFmt, passed);
34211     #endif
34212 }
34213 
34214 static void test_wolfSSL_CTX_set_srp_username(void)
34215 {
34216 #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
34217     && !defined(NO_SHA256) && !defined(WC_NO_RNG)
34218     WOLFSSL_CTX* ctx;
34219     WOLFSSL* ssl;
34220     const char *username = "TESTUSER";
34221     const char *password = "TESTPASSWORD";
34222     int r;
34223 
34224     printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
34225 
34226     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
34227     AssertNotNull(ctx);
34228     r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
34229     AssertIntEQ(r,SSL_SUCCESS);
34230     wolfSSL_CTX_free(ctx);
34231 
34232     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
34233     AssertNotNull(ctx);
34234     r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
34235     AssertIntEQ(r,SSL_SUCCESS);
34236     r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
34237     AssertIntEQ(r,SSL_SUCCESS);
34238 
34239     AssertNotNull(ssl = SSL_new(ctx));
34240     AssertNotNull(SSL_get_srp_username(ssl));
34241     AssertStrEQ(SSL_get_srp_username(ssl), username);
34242 
34243     wolfSSL_free(ssl);
34244     wolfSSL_CTX_free(ctx);
34245 
34246     printf(resultFmt, passed);
34247 #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
34248        /* && !NO_SHA256 && !WC_NO_RNG */
34249 }
34250 
34251 static void test_wolfSSL_CTX_set_srp_password(void)
34252 {
34253 #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
34254     && !defined(NO_SHA256) && !defined(WC_NO_RNG)
34255     WOLFSSL_CTX* ctx;
34256     const char *username = "TESTUSER";
34257     const char *password = "TESTPASSWORD";
34258     int r;
34259 
34260     printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
34261     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
34262     AssertNotNull(ctx);
34263     r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
34264     AssertIntEQ(r,SSL_SUCCESS);
34265     wolfSSL_CTX_free(ctx);
34266 
34267     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
34268     AssertNotNull(ctx);
34269     r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
34270     AssertIntEQ(r,SSL_SUCCESS);
34271     r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
34272     AssertIntEQ(r,SSL_SUCCESS);
34273     wolfSSL_CTX_free(ctx);
34274 
34275     printf(resultFmt, passed);
34276 #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
34277        /* && !NO_SHA256 && !WC_NO_RNG */
34278 }
34279 
34280 static void test_wolfSSL_X509_STORE(void)
34281 {
34282 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
34283     X509_STORE *store;
34284 
34285     #ifdef HAVE_CRL
34286     X509_STORE_CTX *storeCtx;
34287     X509_CRL *crl;
34288     X509 *ca, *cert;
34289     const char crlPem[] = "./certs/crl/crl.revoked";
34290     const char srvCert[] = "./certs/server-revoked-cert.pem";
34291     const char caCert[] = "./certs/ca-cert.pem";
34292     XFILE fp;
34293 
34294     printf(testingFmt, "test_wolfSSL_X509_STORE");
34295     AssertNotNull(store = (X509_STORE *)X509_STORE_new());
34296     AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
34297                            SSL_FILETYPE_PEM)));
34298     AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
34299     AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
34300                     SSL_FILETYPE_PEM)));
34301     AssertNotNull((storeCtx = X509_STORE_CTX_new()));
34302     AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
34303     AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
34304     X509_STORE_free(store);
34305     X509_STORE_CTX_free(storeCtx);
34306     X509_free(cert);
34307     X509_free(ca);
34308 
34309     /* should fail to verify now after adding in CRL */
34310     AssertNotNull(store = (X509_STORE *)X509_STORE_new());
34311     AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
34312                            SSL_FILETYPE_PEM)));
34313     AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
34314     fp = XFOPEN(crlPem, "rb");
34315     AssertTrue((fp != XBADFILE));
34316     AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
34317                 NULL, NULL));
34318     XFCLOSE(fp);
34319     AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
34320     AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
34321     AssertNotNull((storeCtx = X509_STORE_CTX_new()));
34322     AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
34323                     SSL_FILETYPE_PEM)));
34324     AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
34325     AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
34326     AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
34327     X509_CRL_free(crl);
34328     X509_STORE_free(store);
34329     X509_STORE_CTX_free(storeCtx);
34330     X509_free(cert);
34331     X509_free(ca);
34332     #endif /* HAVE_CRL */
34333 
34334 
34335 
34336     #ifndef WOLFCRYPT_ONLY
34337     {
34338         SSL_CTX* ctx;
34339         SSL* ssl;
34340         int i;
34341         for (i = 0; i < 2; i++) {
34342         #ifndef NO_WOLFSSL_SERVER
34343             AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
34344         #else
34345             AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
34346         #endif
34347             AssertNotNull(store = (X509_STORE *)X509_STORE_new());
34348             SSL_CTX_set_cert_store(ctx, store);
34349             AssertNotNull(store = (X509_STORE *)X509_STORE_new());
34350             SSL_CTX_set_cert_store(ctx, store);
34351             AssertNotNull(store = (X509_STORE *)X509_STORE_new());
34352             AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
34353                     SSL_FILETYPE_PEM), SSL_SUCCESS);
34354             AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
34355                     SSL_FILETYPE_PEM), SSL_SUCCESS);
34356             AssertNotNull(ssl = SSL_new(ctx));
34357             if (i == 0) {
34358                 AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
34359             }
34360             else {
34361                 AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
34362                 X509_STORE_free(store);
34363             }
34364             SSL_free(ssl);
34365             SSL_CTX_free(ctx);
34366         }
34367     }
34368     #endif
34369     printf(resultFmt, passed);
34370 #endif
34371     return;
34372 }
34373 
34374 static void test_wolfSSL_X509_STORE_load_locations(void)
34375 {
34376 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
34377     !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
34378     SSL_CTX *ctx;
34379     X509_STORE *store;
34380 
34381     const char ca_file[] = "./certs/ca-cert.pem";
34382     const char client_pem_file[] = "./certs/client-cert.pem";
34383     const char client_der_file[] = "./certs/client-cert.der";
34384     const char ecc_file[] = "./certs/ecc-key.pem";
34385     const char certs_path[] = "./certs/";
34386     const char bad_path[] = "./bad-path/";
34387 #ifdef HAVE_CRL
34388     const char crl_path[] = "./certs/crl/";
34389     const char crl_file[] = "./certs/crl/crl.pem";
34390 #endif
34391 
34392     printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
34393 
34394 #ifndef NO_WOLFSSL_SERVER
34395     AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
34396 #else
34397     AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
34398 #endif
34399     AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
34400     AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
34401 
34402     /* Test bad arguments */
34403     AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
34404     AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
34405     AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
34406     AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
34407     AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
34408 
34409 #ifdef HAVE_CRL
34410     /* Test with CRL */
34411     AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
34412     AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
34413 #endif
34414 
34415     /* Test with CA */
34416     AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
34417 
34418     /* Test with client_cert and certs path */
34419     AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
34420     AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
34421 
34422 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
34423     /* Clear nodes */
34424     ERR_clear_error();
34425 #endif
34426 
34427     SSL_CTX_free(ctx);
34428     printf(resultFmt, passed);
34429 #endif
34430 }
34431 
34432 static void test_X509_STORE_get0_objects(void)
34433 {
34434 #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
34435     X509_STORE *store;
34436     X509_STORE *store_cpy;
34437     SSL_CTX *ctx;
34438     X509_OBJECT *obj;
34439     STACK_OF(X509_OBJECT) *objs;
34440     int i;
34441 
34442     printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
34443 
34444     /* Setup store */
34445 #ifndef NO_WOLFSSL_SERVER
34446     AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
34447 #else
34448     AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
34449 #endif
34450     AssertNotNull(store_cpy = X509_STORE_new());
34451     AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
34452     AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
34453     AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
34454     AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
34455 #ifdef HAVE_CRL
34456     AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
34457 #endif
34458     /* Store ready */
34459 
34460     /* Similar to HaProxy ssl_set_cert_crl_file use case */
34461     AssertNotNull(objs = X509_STORE_get0_objects(store));
34462 #ifdef HAVE_CRL
34463 #ifdef WOLFSSL_SIGNER_DER_CERT
34464     AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
34465 #else
34466     AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
34467 #endif
34468 #else
34469 #ifdef WOLFSSL_SIGNER_DER_CERT
34470     AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
34471 #else
34472     AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
34473 #endif
34474 #endif
34475     for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
34476         obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
34477         switch (X509_OBJECT_get_type(obj)) {
34478         case X509_LU_X509:
34479             AssertNotNull(X509_OBJECT_get0_X509(obj));
34480             AssertIntEQ(X509_STORE_add_cert(store_cpy,
34481                     X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
34482             break;
34483         case X509_LU_CRL:
34484 #ifdef HAVE_CRL
34485             AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
34486             AssertIntEQ(X509_STORE_add_crl(store_cpy,
34487                     X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
34488             break;
34489 #endif
34490         case X509_LU_NONE:
34491         default:
34492             Fail(("X509_OBJECT_get_type should return x509 or crl "
34493                     "(when built with crl support)"),
34494                     ("Unrecognized X509_OBJECT type or none"));
34495         }
34496     }
34497 
34498     X509_STORE_free(store_cpy);
34499     SSL_CTX_free(ctx);
34500     printf(resultFmt, passed);
34501 #endif
34502 }
34503 
34504 static void test_wolfSSL_BN(void)
34505 {
34506 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
34507     BIGNUM* a;
34508     BIGNUM* b;
34509     BIGNUM* c;
34510     BIGNUM* d;
34511     ASN1_INTEGER* ai;
34512 
34513     printf(testingFmt, "wolfSSL_BN()");
34514 
34515     AssertNotNull(b = BN_new());
34516     AssertNotNull(c = BN_new());
34517     AssertNotNull(d = BN_new());
34518 
34519     ai = ASN1_INTEGER_new();
34520     AssertNotNull(ai);
34521     /* at the moment hard setting since no set function */
34522     ai->data[0] = 0x02; /* tag for ASN_INTEGER */
34523     ai->data[1] = 0x01; /* length of integer */
34524     ai->data[2] = 0x03;
34525 
34526     AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
34527     ASN1_INTEGER_free(ai);
34528 
34529     AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
34530     AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
34531 
34532     /* a + 3 = */
34533     AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
34534     AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
34535 
34536     /* check result 3 + 3*/
34537     AssertIntEQ(BN_get_word(a), 6);
34538     /* set a back to 3 */
34539     AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
34540 
34541     /* a - 3 = */
34542     AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
34543     AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
34544 
34545     /* check result 3 - 3*/
34546     AssertIntEQ(BN_get_word(a), 0);
34547     /* set a back to 3 */
34548     AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
34549 
34550     /* a^b mod c = */
34551     AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
34552     AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
34553 
34554     /* check result 3^2 mod 5 */
34555     AssertIntEQ(BN_get_word(d), 4);
34556 
34557     /* a*b = */
34558     AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
34559     AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
34560 
34561     /* check result 3*2 */
34562     AssertIntEQ(BN_get_word(d), 6);
34563 
34564     /* c/b => db + a */
34565     AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
34566     AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
34567 
34568     /* check result 5/2 */
34569     AssertIntEQ(BN_get_word(d), 2); /* check quotient */
34570     AssertIntEQ(BN_get_word(a), 1); /* check remainder */
34571     /* set a back to 3 */
34572     AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
34573 
34574     /* a*b mod c = */
34575     AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
34576     AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
34577 
34578     /* check result 3*2 mod 5 */
34579     AssertIntEQ(BN_get_word(d), 1);
34580 
34581     AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
34582     AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
34583 
34584 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
34585     /* gcd of a and b */
34586     AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
34587     AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
34588 
34589     /* check result gcd(16, 24) */
34590     AssertIntEQ(BN_get_word(d), 8);
34591 #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
34592 
34593     AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
34594     AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
34595     AssertIntEQ(BN_is_zero(b), 0);
34596     AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
34597     AssertIntEQ(BN_is_zero(b), 1);
34598     AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
34599     AssertIntEQ(BN_is_zero(b), 0);
34600 
34601     /* set b back to 2 */
34602     AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
34603 
34604     /* BN_mod_inverse test */
34605     BIGNUM *r = BN_new();
34606     BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
34607     AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
34608     BN_free(val);
34609 
34610 #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
34611                                                defined(WOLFSSL_SP_INT_NEGATIVE))
34612     AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
34613     AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
34614     AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
34615     AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
34616     AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
34617 #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
34618     {
34619     char* ret;
34620     AssertNotNull(ret = BN_bn2dec(c));
34621     AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
34622     XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
34623     }
34624 #endif
34625     AssertIntEQ(BN_get_word(c), 4);
34626 #endif
34627 
34628     BN_free(a);
34629     BN_free(b);
34630     BN_free(c);
34631     BN_clear_free(d);
34632 
34633     /* check that converting NULL and the null string returns an error */
34634     a = NULL;
34635     AssertIntLE(BN_hex2bn(&a, NULL), 0);
34636     AssertIntLE(BN_hex2bn(&a, ""), 0);
34637     AssertNull(a);
34638 
34639     /* check that getting a string and a bin of the same number are equal,
34640      * and that the comparison works EQ, LT and GT */
34641     AssertIntGT(BN_hex2bn(&a, "03"), 0);
34642     AssertNotNull(b = BN_new());
34643     AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
34644     AssertNotNull(c = BN_new());
34645     AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
34646     AssertIntEQ(BN_cmp(a, b), 0);
34647     AssertIntLT(BN_cmp(a, c), 0);
34648     AssertIntGT(BN_cmp(c, b), 0);
34649 
34650     AssertIntEQ(BN_set_word(a, 0), 1);
34651     AssertIntEQ(BN_is_zero(a), 1);
34652     AssertIntEQ(BN_set_bit(a, 0x45), 1);
34653     AssertIntEQ(BN_is_zero(a), 0);
34654     AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
34655     AssertIntEQ(BN_clear_bit(a, 0x45), 1);
34656     AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
34657     AssertIntEQ(BN_is_zero(a), 1);
34658 
34659     BN_free(a);
34660     BN_free(b);
34661     BN_free(c);
34662 
34663     #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
34664     {
34665         BIGNUM *ap;
34666         BIGNUM bv;
34667         BIGNUM cv;
34668         BIGNUM dv;
34669 
34670         AssertNotNull(ap = BN_new());
34671         BN_init(&bv);
34672         BN_init(&cv);
34673         BN_init(&dv);
34674 
34675         AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
34676         AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
34677         AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
34678 
34679         /* a^b mod c = */
34680         AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
34681         AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
34682 
34683         /* check result  3^2 mod 5 */
34684         AssertIntEQ(BN_get_word(&dv), 4);
34685 
34686         /* a*b mod c = */
34687         AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
34688         AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
34689 
34690         /* check result  3*2 mod 5 */
34691         AssertIntEQ(BN_get_word(&dv), 1);
34692 
34693         BN_free(ap);
34694     }
34695     #endif
34696 
34697 #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
34698     AssertNotNull(a = BN_new());
34699     AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
34700         SSL_SUCCESS);
34701     AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
34702     BN_free(a);
34703 #endif
34704 
34705     printf(resultFmt, passed);
34706     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
34707 }
34708 
34709 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
34710    !defined(NO_FILESYSTEM) && !defined(NO_RSA)
34711 #define TEST_ARG 0x1234
34712 static void msg_cb(int write_p, int version, int content_type,
34713                    const void *buf, size_t len, SSL *ssl, void *arg)
34714 {
34715     (void)write_p;
34716     (void)version;
34717     (void)content_type;
34718     (void)buf;
34719     (void)len;
34720     (void)ssl;
34721 
34722     AssertTrue(arg == (void*)TEST_ARG);
34723 }
34724 #endif
34725 
34726 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
34727      !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
34728      defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
34729      !defined(NO_WOLFSSL_SERVER)
34730 #ifndef SINGLE_THREADED
34731 #if defined(SESSION_CERTS)
34732 #include "wolfssl/internal.h"
34733 #endif
34734 static int msgCb(SSL_CTX *ctx, SSL *ssl)
34735 {
34736     (void) ctx;
34737     (void) ssl;
34738     #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
34739         STACK_OF(X509)* sk;
34740         X509* x509;
34741         int i, num;
34742         BIO* bio;
34743     #endif
34744     printf("\n===== msgcb called ====\n");
34745     #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
34746     AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
34747     AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
34748     AssertNotNull(SSL_get0_verified_chain(ssl));
34749     #endif
34750 
34751     #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
34752     bio = BIO_new(BIO_s_file());
34753     BIO_set_fp(bio, stdout, BIO_NOCLOSE);
34754     sk = SSL_get_peer_cert_chain(ssl);
34755     AssertNotNull(sk);
34756     if (!sk) {
34757         BIO_free(bio);
34758         return SSL_FAILURE;
34759     }
34760     num = sk_X509_num(sk);
34761     AssertTrue(num > 0);
34762     for (i = 0; i < num; i++) {
34763         x509 = sk_X509_value(sk,i);
34764         AssertNotNull(x509);
34765         if (!x509)
34766             break;
34767         printf("Certificate at index [%d] = :\n",i);
34768         X509_print(bio,x509);
34769         printf("\n\n");
34770     }
34771     BIO_free(bio);
34772     #endif
34773     return SSL_SUCCESS;
34774 }
34775 #endif
34776 #endif
34777 
34778 static void test_wolfSSL_msgCb(void)
34779 {
34780   #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
34781      !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
34782      defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
34783      !defined(NO_WOLFSSL_SERVER)
34784 
34785     tcp_ready ready;
34786     func_args client_args;
34787     func_args server_args;
34788     #ifndef SINGLE_THREADED
34789     THREAD_TYPE serverThread;
34790     #endif
34791     callback_functions client_cb;
34792     callback_functions server_cb;
34793 
34794     printf(testingFmt, "test_wolfSSL_msgCb");
34795 
34796 /* create a failed connection and inspect the error */
34797 #ifdef WOLFSSL_TIRTOS
34798     fdOpenSession(Task_self());
34799 #endif
34800     XMEMSET(&client_args, 0, sizeof(func_args));
34801     XMEMSET(&server_args, 0, sizeof(func_args));
34802 
34803     StartTCP();
34804     InitTcpReady(&ready);
34805 
34806     XMEMSET(&client_cb, 0, sizeof(callback_functions));
34807     XMEMSET(&server_cb, 0, sizeof(callback_functions));
34808 #ifndef WOLFSSL_NO_TLS12
34809     client_cb.method  = wolfTLSv1_2_client_method;
34810     server_cb.method  = wolfTLSv1_2_server_method;
34811 #else
34812     client_cb.method  = wolfTLSv1_3_client_method;
34813     server_cb.method  = wolfTLSv1_3_server_method;
34814 #endif
34815 
34816     server_args.signal    = &ready;
34817     server_args.callbacks = &server_cb;
34818     client_args.signal    = &ready;
34819     client_args.callbacks = &client_cb;
34820     client_args.return_code = TEST_FAIL;
34821 
34822 #ifndef SINGLE_THREADED
34823     start_thread(test_server_nofail, &server_args, &serverThread);
34824     wait_tcp_ready(&server_args);
34825     test_client_nofail(&client_args, msgCb);
34826     join_thread(serverThread);
34827 #endif
34828 
34829     FreeTcpReady(&ready);
34830 
34831 #ifndef SINGLE_THREADED
34832     AssertTrue(client_args.return_code);
34833     AssertTrue(server_args.return_code);
34834 #endif
34835 
34836 #ifdef WOLFSSL_TIRTOS
34837     fdOpenSession(Task_self());
34838 #endif
34839 
34840     printf(resultFmt, passed);
34841 
34842 #endif
34843 }
34844 
34845 static void test_wolfSSL_either_side(void)
34846 {
34847 #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
34848     !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
34849     !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
34850 
34851     tcp_ready ready;
34852     func_args client_args;
34853     func_args server_args;
34854     #ifndef SINGLE_THREADED
34855     THREAD_TYPE serverThread;
34856     #endif
34857     callback_functions client_cb;
34858     callback_functions server_cb;
34859 
34860     printf(testingFmt, "test_wolfSSL_either_side");
34861 
34862 /* create a failed connection and inspect the error */
34863 #ifdef WOLFSSL_TIRTOS
34864     fdOpenSession(Task_self());
34865 #endif
34866     XMEMSET(&client_args, 0, sizeof(func_args));
34867     XMEMSET(&server_args, 0, sizeof(func_args));
34868 
34869     StartTCP();
34870     InitTcpReady(&ready);
34871 
34872     XMEMSET(&client_cb, 0, sizeof(callback_functions));
34873     XMEMSET(&server_cb, 0, sizeof(callback_functions));
34874 
34875     /* Use different CTX for client and server */
34876     client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
34877     AssertNotNull(client_cb.ctx);
34878     server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
34879     AssertNotNull(server_cb.ctx);
34880     /* we are responsible for free'ing WOLFSSL_CTX */
34881     server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
34882 
34883     server_args.signal    = &ready;
34884     server_args.callbacks = &server_cb;
34885     client_args.signal    = &ready;
34886     client_args.callbacks = &client_cb;
34887     client_args.return_code = TEST_FAIL;
34888 
34889 #ifndef SINGLE_THREADED
34890     start_thread(test_server_nofail, &server_args, &serverThread);
34891     wait_tcp_ready(&server_args);
34892     test_client_nofail(&client_args, NULL);
34893     join_thread(serverThread);
34894 #endif
34895 
34896     wolfSSL_CTX_free(client_cb.ctx);
34897     wolfSSL_CTX_free(server_cb.ctx);
34898     FreeTcpReady(&ready);
34899 
34900 #ifndef SINGLE_THREADED
34901     AssertTrue(client_args.return_code);
34902     AssertTrue(server_args.return_code);
34903 #endif
34904 
34905 #ifdef WOLFSSL_TIRTOS
34906     fdOpenSession(Task_self());
34907 #endif
34908 
34909     printf(resultFmt, passed);
34910 
34911 #endif
34912 }
34913 
34914 static void test_wolfSSL_DTLS_either_side(void)
34915 {
34916 #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
34917     !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
34918     !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
34919     defined(WOLFSSL_DTLS)
34920 
34921     tcp_ready ready;
34922     func_args client_args;
34923     func_args server_args;
34924     #ifndef SINGLE_THREADED
34925     THREAD_TYPE serverThread;
34926     #endif
34927     callback_functions client_cb;
34928     callback_functions server_cb;
34929 
34930     printf(testingFmt, "test_wolfSSL_DTLS_either_side");
34931 
34932 /* create a failed connection and inspect the error */
34933 #ifdef WOLFSSL_TIRTOS
34934     fdOpenSession(Task_self());
34935 #endif
34936     XMEMSET(&client_args, 0, sizeof(func_args));
34937     XMEMSET(&server_args, 0, sizeof(func_args));
34938 
34939     StartTCP();
34940     InitTcpReady(&ready);
34941 
34942     XMEMSET(&client_cb, 0, sizeof(callback_functions));
34943     XMEMSET(&server_cb, 0, sizeof(callback_functions));
34944 
34945     /* Use different CTX for client and server */
34946     client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
34947     AssertNotNull(client_cb.ctx);
34948     server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
34949     AssertNotNull(server_cb.ctx);
34950     /* we are responsible for free'ing WOLFSSL_CTX */
34951     server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
34952 
34953     server_args.signal    = &ready;
34954     server_args.callbacks = &server_cb;
34955     client_args.signal    = &ready;
34956     client_args.callbacks = &client_cb;
34957     client_args.return_code = TEST_FAIL;
34958 
34959 #ifndef SINGLE_THREADED
34960     start_thread(test_server_nofail, &server_args, &serverThread);
34961     wait_tcp_ready(&server_args);
34962     test_client_nofail(&client_args, NULL);
34963     join_thread(serverThread);
34964 #endif
34965 
34966     wolfSSL_CTX_free(client_cb.ctx);
34967     wolfSSL_CTX_free(server_cb.ctx);
34968     FreeTcpReady(&ready);
34969 
34970 #ifndef SINGLE_THREADED
34971     AssertTrue(client_args.return_code);
34972     AssertTrue(server_args.return_code);
34973 #endif
34974 
34975 #ifdef WOLFSSL_TIRTOS
34976     fdOpenSession(Task_self());
34977 #endif
34978 
34979     printf(resultFmt, passed);
34980 #endif
34981 }
34982 
34983 static void test_generate_cookie(void)
34984 {
34985 #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
34986     SSL_CTX* ctx;
34987     SSL* ssl;
34988     byte    buf[FOURK_BUF] = {0};
34989 
34990     printf(testingFmt, "test_generate_cookie");
34991 
34992     AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
34993     AssertNotNull(ssl = SSL_new(ctx));
34994 
34995     /* Test unconnected */
34996     AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
34997 
34998     wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
34999 
35000     wolfSSL_SetCookieCtx(ssl, ctx);
35001 
35002     AssertNotNull(wolfSSL_GetCookieCtx(ssl));
35003 
35004     AssertNull(wolfSSL_GetCookieCtx(NULL));
35005 
35006     SSL_free(ssl);
35007     SSL_CTX_free(ctx);
35008 
35009     printf(resultFmt, passed);
35010 #endif
35011 }
35012 
35013 static void test_wolfSSL_set_options(void)
35014 {
35015 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
35016    !defined(NO_FILESYSTEM) && !defined(NO_RSA)
35017     SSL*     ssl;
35018     SSL_CTX* ctx;
35019     char appData[] = "extra msg";
35020 
35021     unsigned char protos[] = {
35022         7, 't', 'l', 's', '/', '1', '.', '2',
35023         8, 'h', 't', 't', 'p', '/', '1', '.', '1'
35024     };
35025     unsigned int len = sizeof(protos);
35026 
35027     void *arg = (void *)TEST_ARG;
35028 
35029     printf(testingFmt, "wolfSSL_set_options()");
35030 
35031 #ifndef NO_WOLFSSL_SERVER
35032     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
35033 #else
35034     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
35035 #endif
35036     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
35037     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35038 
35039     AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
35040     AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
35041 
35042     AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
35043                                                               SSL_OP_NO_SSLv2)), 0);
35044     AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
35045                                  SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
35046     AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
35047                                            SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
35048     AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
35049                                    SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
35050     AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
35051                                                SSL_OP_NO_COMPRESSION));
35052 
35053     SSL_CTX_free(ctx);
35054 
35055 #ifndef NO_WOLFSSL_SERVER
35056     ctx = SSL_CTX_new(wolfSSLv23_server_method());
35057     AssertNotNull(ctx);
35058 #else
35059     ctx = SSL_CTX_new(wolfSSLv23_client_method());
35060     AssertNotNull(ctx);
35061 #endif
35062     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
35063     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35064     AssertTrue(SSL_CTX_set_msg_callback(ctx, msg_cb) == SSL_SUCCESS);
35065 
35066     AssertNotNull(ssl = SSL_new(ctx));
35067 #ifdef HAVE_EX_DATA
35068     AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
35069     AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
35070     if (ssl) {
35071         AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
35072                     appData, sizeof(appData)), 0);
35073     }
35074 #else
35075     AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
35076     AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
35077 #endif
35078 
35079     AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
35080     AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
35081 
35082     AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
35083                                                           WOLFSSL_OP_NO_SSLv2)), 0);
35084     AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
35085                              SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
35086     AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
35087                                        SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
35088     AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
35089                                SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
35090     AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
35091                                        SSL_OP_NO_COMPRESSION));
35092 
35093     AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
35094     SSL_set_msg_callback_arg(ssl, arg);
35095 #ifdef WOLFSSL_ERROR_CODE_OPENSSL
35096     AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
35097 #else
35098     AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
35099 #endif
35100 
35101 #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
35102     defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
35103     defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
35104 
35105 #if defined(HAVE_ALPN) && !defined(NO_BIO)
35106 
35107 #ifdef WOLFSSL_ERROR_CODE_OPENSSL
35108     AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == 0);
35109 #else
35110     AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == SSL_SUCCESS);
35111 #endif
35112 
35113 #endif /* HAVE_ALPN && !NO_BIO */
35114 #endif
35115 
35116     SSL_free(ssl);
35117     SSL_CTX_free(ctx);
35118 
35119     printf(resultFmt, passed);
35120 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
35121          !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
35122 }
35123 
35124 static void test_wolfSSL_sk_SSL_CIPHER(void)
35125 {
35126     #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
35127        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
35128     SSL*     ssl;
35129     SSL_CTX* ctx;
35130     STACK_OF(SSL_CIPHER) *sk, *dup;
35131 
35132     printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
35133 
35134 #ifndef NO_WOLFSSL_SERVER
35135     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
35136 #else
35137     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
35138 #endif
35139     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
35140     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35141     AssertNotNull(ssl = SSL_new(ctx));
35142     AssertNotNull(sk = SSL_get_ciphers(ssl));
35143     AssertNotNull(dup = sk_SSL_CIPHER_dup(sk));
35144     AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
35145     AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
35146 
35147     /* error case because connection has not been established yet */
35148     AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
35149     sk_SSL_CIPHER_free(dup);
35150 
35151     /* sk is pointer to internal struct that should be free'd in SSL_free */
35152     SSL_free(ssl);
35153     SSL_CTX_free(ctx);
35154 
35155     printf(resultFmt, passed);
35156     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
35157              !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
35158 }
35159 
35160 static void test_wolfSSL_set1_curves_list(void)
35161 {
35162 #if defined(OPENSSL_EXTRA) && defined(HAVE_RSA)
35163     SSL*     ssl = NULL;
35164     SSL_CTX* ctx = NULL;
35165 
35166 #ifndef NO_WOLFSSL_SERVER
35167     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
35168 #else
35169     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
35170 #endif
35171     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
35172                SSL_FILETYPE_PEM));
35173     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35174     AssertNotNull(ssl = SSL_new(ctx));
35175 
35176     AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
35177     AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
35178     AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
35179 
35180     AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
35181     AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
35182     AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
35183 
35184     SSL_free(ssl);
35185     SSL_CTX_free(ctx);
35186 
35187     printf(resultFmt, passed);
35188 #endif
35189 }
35190 
35191 static void test_wolfSSL_set1_sigalgs_list(void)
35192 {
35193     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
35194     SSL*     ssl;
35195     SSL_CTX* ctx;
35196 
35197 #ifndef NO_WOLFSSL_SERVER
35198     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
35199 #else
35200     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
35201 #endif
35202     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
35203                SSL_FILETYPE_PEM));
35204     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35205     AssertNotNull(ssl = SSL_new(ctx));
35206 
35207     AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
35208     AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
35209     AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
35210     AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
35211 
35212     AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
35213     AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
35214 
35215 #ifndef NO_RSA
35216     #ifndef NO_SHA256
35217         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
35218                     WOLFSSL_FAILURE);
35219         AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
35220                     WOLFSSL_FAILURE);
35221 
35222         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
35223                     WOLFSSL_SUCCESS);
35224         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
35225                     WOLFSSL_SUCCESS);
35226         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
35227                     WOLFSSL_FAILURE);
35228         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
35229                     WOLFSSL_FAILURE);
35230         #ifdef WC_RSA_PSS
35231             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
35232                         WOLFSSL_SUCCESS);
35233             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
35234                         WOLFSSL_SUCCESS);
35235             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
35236                         WOLFSSL_SUCCESS);
35237             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
35238                         WOLFSSL_SUCCESS);
35239         #endif
35240         #ifdef WOLFSSL_SHA512
35241             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
35242                        "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
35243             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
35244                        "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
35245         #elif defined(WOLFSSL_SHA384)
35246             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
35247                        "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
35248             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
35249                        "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
35250         #endif
35251         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
35252         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
35253         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
35254                     WOLFSSL_FAILURE);
35255         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
35256                     WOLFSSL_FAILURE);
35257 
35258         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
35259                     WOLFSSL_FAILURE);
35260         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
35261                     WOLFSSL_FAILURE);
35262     #endif
35263 #endif
35264 #ifdef HAVE_ECC
35265     #ifndef NO_SHA256
35266         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
35267                     WOLFSSL_SUCCESS);
35268         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
35269         #ifdef WOLFSSL_SHA512
35270             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
35271                        "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
35272             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
35273                        "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
35274         #elif defined(WOLFSSL_SHA384)
35275             AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
35276                        "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
35277             AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
35278                        "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
35279         #endif
35280     #endif
35281 #endif
35282 #ifdef HAVE_ED25519
35283     AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
35284     AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
35285 #endif
35286 #ifdef HAVE_ED448
35287     AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
35288     AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
35289 #endif
35290 #ifndef NO_DSA
35291     #ifndef NO_SHA256
35292         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
35293                     WOLFSSL_SUCCESS);
35294         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
35295                     WOLFSSL_SUCCESS);
35296     #endif
35297     #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
35298                                                 defined(WOLFSSL_ALLOW_TLS_SHA1))
35299         AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
35300                     WOLFSSL_SUCCESS);
35301         AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
35302                     WOLFSSL_SUCCESS);
35303     #endif
35304 #endif
35305 
35306     SSL_free(ssl);
35307     SSL_CTX_free(ctx);
35308 
35309     printf(resultFmt, passed);
35310     #endif
35311 }
35312 
35313 /* Testing  wolfSSL_set_tlsext_status_type function.
35314  * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
35315  */
35316 static void test_wolfSSL_set_tlsext_status_type(void){
35317     #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
35318     !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
35319     SSL*     ssl;
35320     SSL_CTX* ctx;
35321 
35322     printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
35323 
35324     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
35325     AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
35326     AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
35327     AssertNotNull(ssl = SSL_new(ctx));
35328     AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
35329             SSL_SUCCESS);
35330     AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
35331     SSL_free(ssl);
35332     SSL_CTX_free(ctx);
35333     #endif  /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
35334 }
35335 
35336 #ifndef NO_BIO
35337 
35338 static void test_wolfSSL_PEM_read_bio(void)
35339 {
35340     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
35341        !defined(NO_FILESYSTEM) && !defined(NO_RSA)
35342     byte buff[6000];
35343     XFILE f;
35344     int  bytes;
35345     X509* x509;
35346     BIO*  bio = NULL;
35347     BUF_MEM* buf;
35348 
35349     printf(testingFmt, "wolfSSL_PEM_read_bio()");
35350 
35351     f = XFOPEN(cliCertFile, "rb");
35352     AssertTrue((f != XBADFILE));
35353     bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
35354     XFCLOSE(f);
35355 
35356     AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
35357     AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
35358     AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
35359     AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
35360     AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
35361     AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
35362     AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
35363 
35364     BIO_free(bio);
35365     BUF_MEM_free(buf);
35366     X509_free(x509);
35367 
35368     printf(resultFmt, passed);
35369     #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
35370              !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
35371 }
35372 
35373 
35374 #if defined(OPENSSL_EXTRA)
35375 static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
35376                  long argl, long ret)
35377 {
35378     (void)bio;
35379     (void)cmd;
35380     (void)argp;
35381     (void)argi;
35382     (void)argl;
35383     return ret;
35384 }
35385 #endif
35386 
35387 
35388 static void test_wolfSSL_BIO(void)
35389 {
35390     #if defined(OPENSSL_EXTRA)
35391     const unsigned char* p;
35392     byte buff[20];
35393     BIO* bio1;
35394     BIO* bio2;
35395     BIO* bio3;
35396     char* bufPt;
35397     int i;
35398 
35399     printf(testingFmt, "wolfSSL_BIO()");
35400 
35401     for (i = 0; i < 20; i++) {
35402         buff[i] = i;
35403     }
35404     /* test BIO_free with NULL */
35405     AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
35406 
35407     /* Creating and testing type BIO_s_bio */
35408     AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
35409     AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
35410     AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
35411 
35412     /* read/write before set up */
35413     AssertIntEQ(BIO_read(bio1, buff, 2),  WOLFSSL_BIO_UNSET);
35414     AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
35415 
35416     AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
35417     AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
35418     AssertIntEQ(BIO_set_write_buf_size(bio2, 8),  WOLFSSL_SUCCESS);
35419     AssertIntEQ(BIO_make_bio_pair(bio1, bio2),    WOLFSSL_SUCCESS);
35420 
35421     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
35422     XMEMCPY(bufPt, buff, 10);
35423     AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
35424     /* write buffer full */
35425     AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
35426     AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
35427     AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
35428 
35429     /* write the other direction with pair */
35430     AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
35431     XMEMCPY(bufPt, buff, 8);
35432     AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
35433 
35434     /* try read */
35435     AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
35436     AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
35437 
35438     /* try read using ctrl function */
35439     AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
35440     AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
35441     AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
35442     AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
35443 
35444     AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
35445     for (i = 0; i < 20; i++) {
35446         AssertIntEQ((int)bufPt[i], i);
35447     }
35448     AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
35449     AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
35450     for (i = 0; i < 8; i++) {
35451         AssertIntEQ((int)bufPt[i], i);
35452     }
35453     AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
35454     AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
35455 
35456     /* new pair */
35457     AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
35458     BIO_free(bio2); /* free bio2 and automatically remove from pair */
35459     AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
35460     AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
35461     AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
35462 
35463     /* test wrap around... */
35464     AssertIntEQ(BIO_reset(bio1), 0);
35465     AssertIntEQ(BIO_reset(bio3), 0);
35466 
35467     /* fill write buffer, read only small amount then write again */
35468     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
35469     XMEMCPY(bufPt, buff, 20);
35470     AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
35471     for (i = 0; i < 4; i++) {
35472         AssertIntEQ(bufPt[i], i);
35473     }
35474 
35475     /* try writing over read index */
35476     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
35477     XMEMSET(bufPt, 0, 4);
35478     AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
35479 
35480     /* read and write 0 bytes */
35481     AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
35482     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
35483 
35484     /* should read only to end of write buffer then need to read again */
35485     AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
35486     for (i = 0; i < 16; i++) {
35487         AssertIntEQ(bufPt[i], buff[4 + i]);
35488     }
35489 
35490     AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
35491     AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
35492     for (i = 0; i < 4; i++) {
35493         AssertIntEQ(bufPt[i], 0);
35494     }
35495 
35496     /* read index should not have advanced with nread0 */
35497     AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
35498     for (i = 0; i < 4; i++) {
35499         AssertIntEQ(bufPt[i], 0);
35500     }
35501 
35502     /* write and fill up buffer checking reset of index state */
35503     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
35504     XMEMCPY(bufPt, buff, 20);
35505 
35506     /* test reset on data in bio1 write buffer */
35507     AssertIntEQ(BIO_reset(bio1), 0);
35508     AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
35509     AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
35510     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
35511     AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
35512     AssertNotNull(p);
35513     XMEMCPY(bufPt, buff, 20);
35514     AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
35515     for (i = 0; i < 6; i++) {
35516         AssertIntEQ(bufPt[i], i);
35517     }
35518 
35519     /* test case of writing twice with offset read index */
35520     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
35521     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
35522     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
35523     AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
35524     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
35525     AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
35526     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
35527     AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
35528 
35529     BIO_free(bio1);
35530     BIO_free(bio3);
35531 
35532     #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
35533     {
35534         BIO* bioA = NULL;
35535         BIO* bioB = NULL;
35536         AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
35537         AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
35538         BIO_free(bioA);
35539         bioA = NULL;
35540         BIO_free(bioB);
35541         bioB = NULL;
35542     }
35543     #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
35544 
35545     /* BIOs with file pointers */
35546     #if !defined(NO_FILESYSTEM)
35547     {
35548         XFILE f1;
35549         XFILE f2;
35550         BIO*  f_bio1;
35551         BIO*  f_bio2;
35552         unsigned char cert[300];
35553         char testFile[] = "tests/bio_write_test.txt";
35554         char msg[]      = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
35555 
35556         AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
35557         AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
35558 
35559         AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
35560         AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1),   0);
35561 
35562         f1 = XFOPEN(svrCertFile, "rwb");
35563         AssertTrue((f1 != XBADFILE));
35564         AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
35565         AssertIntEQ(BIO_write_filename(f_bio2, testFile),
35566                 WOLFSSL_SUCCESS);
35567 
35568         AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
35569         AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
35570         AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
35571         AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
35572         AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
35573         AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
35574 
35575         AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
35576         AssertIntEQ(BIO_reset(f_bio2), 0);
35577         AssertIntEQ(BIO_tell(NULL),-1);
35578         AssertIntEQ(BIO_tell(f_bio2),0);
35579         AssertIntEQ(BIO_seek(f_bio2, 4), 0);
35580         AssertIntEQ(BIO_tell(f_bio2),4);
35581 
35582         BIO_free(f_bio1);
35583         BIO_free(f_bio2);
35584 
35585         AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
35586         AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
35587         AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
35588         BIO_free(f_bio1);
35589 
35590     }
35591     #endif /* !defined(NO_FILESYSTEM) */
35592 
35593     /* BIO info callback */
35594     {
35595         const char* testArg = "test";
35596         BIO* cb_bio;
35597         AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
35598 
35599         BIO_set_callback(cb_bio, bioCallback);
35600         AssertNotNull(BIO_get_callback(cb_bio));
35601         BIO_set_callback(cb_bio, NULL);
35602         AssertNull(BIO_get_callback(cb_bio));
35603 
35604         BIO_set_callback_arg(cb_bio, (char*)testArg);
35605         AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
35606         AssertNull(BIO_get_callback_arg(NULL));
35607 
35608         BIO_free(cb_bio);
35609     }
35610 
35611     /* BIO_vfree */
35612     AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
35613     BIO_vfree(NULL);
35614     BIO_vfree(bio1);
35615 
35616     printf(resultFmt, passed);
35617     #endif
35618 }
35619 
35620 #endif /* !NO_BIO */
35621 
35622 
35623 static void test_wolfSSL_ASN1_STRING(void)
35624 {
35625     #if defined(OPENSSL_EXTRA)
35626     ASN1_STRING* str = NULL;
35627     const char data[] = "hello wolfSSL";
35628 
35629     printf(testingFmt, "wolfSSL_ASN1_STRING()");
35630 
35631     AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
35632     AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
35633     AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
35634     AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
35635     AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
35636 
35637     ASN1_STRING_free(str);
35638 
35639     printf(resultFmt, passed);
35640     #endif
35641 }
35642 
35643 static void test_wolfSSL_ASN1_BIT_STRING(void)
35644 {
35645 #ifdef OPENSSL_ALL
35646     ASN1_BIT_STRING* str;
35647 
35648     printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
35649     AssertNotNull(str = ASN1_BIT_STRING_new());
35650 
35651     AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
35652     AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
35653     AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
35654     AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
35655     AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
35656     AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
35657 
35658     ASN1_BIT_STRING_free(str);
35659     printf(resultFmt, passed);
35660 #endif
35661 }
35662 
35663 static void test_wolfSSL_a2i_ASN1_INTEGER(void)
35664 {
35665 #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
35666     BIO *bio, *out;
35667     ASN1_INTEGER* ai;
35668     char buf[] = "123456\n12345\n112345678912345678901234567890\n";
35669     char tmp[1024];
35670     int  tmpSz;
35671 
35672     const char expected1[] = "123456";
35673     const char expected2[] = "112345678912345678901234567890";
35674 
35675     printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
35676 
35677     AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
35678     AssertNotNull(out = BIO_new(BIO_s_mem()));
35679     AssertNotNull(ai = ASN1_INTEGER_new());
35680 
35681     /* read first line */
35682     AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
35683     AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
35684     XMEMSET(tmp, 0, 1024);
35685     tmpSz = BIO_read(out, tmp, 1024);
35686     AssertIntEQ(tmpSz, 6);
35687     AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
35688 
35689     /* fail on second line (not % 2) */
35690     AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
35691 
35692     /* read 3rd long line */
35693     AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
35694     AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
35695     XMEMSET(tmp, 0, 1024);
35696     tmpSz = BIO_read(out, tmp, 1024);
35697     AssertIntEQ(tmpSz, 30);
35698     AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
35699 
35700     BIO_free(out);
35701     BIO_free(bio);
35702     ASN1_INTEGER_free(ai);
35703 
35704     printf(resultFmt, passed);
35705 
35706 #endif
35707 }
35708 
35709 static void test_wolfSSL_a2i_IPADDRESS(void)
35710 {
35711 #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
35712     const unsigned char* data;
35713     int dataSz = 0;
35714     ASN1_OCTET_STRING *st;
35715 
35716     const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
35717     const unsigned char ipv6_exp[] = {
35718         0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
35719         0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
35720     };
35721     const unsigned char ipv6_home[] = {
35722         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35723         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
35724     };
35725     printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
35726 
35727     AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
35728     AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
35729     data   = ASN1_STRING_get0_data(st);
35730     dataSz = ASN1_STRING_length(st);
35731     AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
35732     AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
35733     ASN1_STRING_free(st);
35734 
35735     AssertNotNull(st = a2i_IPADDRESS("::1"));
35736     data   = ASN1_STRING_get0_data(st);
35737     dataSz = ASN1_STRING_length(st);
35738     AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
35739     AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
35740     ASN1_STRING_free(st);
35741 
35742     AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
35743     data   = ASN1_STRING_get0_data(st);
35744     dataSz = ASN1_STRING_length(st);
35745     AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
35746     AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
35747     ASN1_STRING_free(st);
35748 
35749     printf(resultFmt, passed);
35750 
35751 #endif
35752 }
35753 
35754 static void test_wolfSSL_DES_ecb_encrypt(void)
35755 {
35756     #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
35757     WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
35758     WOLFSSL_DES_key_schedule key;
35759 
35760     printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
35761 
35762     XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
35763     XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
35764     XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
35765     XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
35766     XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
35767     XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
35768     XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
35769 
35770     /* Encrypt messages */
35771     wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
35772     wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
35773 
35774     /* Decrypt messages */
35775     int ret1 = 0;
35776     int ret2 = 0;
35777     wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
35778     ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
35779     AssertIntEQ(ret1,0);
35780     wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
35781     ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
35782     AssertIntEQ(ret2,0);
35783 
35784     printf(resultFmt, passed);
35785     #endif
35786 }
35787 
35788 static void test_wolfSSL_ASN1_TIME_adj(void)
35789 {
35790 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
35791 && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
35792 
35793     const int year = 365*24*60*60;
35794     const int day  = 24*60*60;
35795     const int hour = 60*60;
35796     const int mini = 60;
35797     const byte asn_utc_time = ASN_UTC_TIME;
35798 #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
35799     const byte asn_gen_time = ASN_GENERALIZED_TIME;
35800 #endif
35801     WOLFSSL_ASN1_TIME *asn_time, *s;
35802     int offset_day;
35803     long offset_sec;
35804     char date_str[CTC_DATE_SIZE + 1];
35805     time_t t;
35806 
35807     printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
35808 
35809     AssertNotNull(s = wolfSSL_ASN1_TIME_new());
35810     /* UTC notation test */
35811     /* 2000/2/15 20:30:00 */
35812     t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
35813     offset_day = 7;
35814     offset_sec = 45 * mini;
35815     /* offset_sec = -45 * min;*/
35816     AssertNotNull(asn_time =
35817             wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
35818     AssertTrue(asn_time->type == asn_utc_time);
35819     XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
35820     date_str[CTC_DATE_SIZE] = '\0';
35821     AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
35822 
35823     /* negative offset */
35824     offset_sec = -45 * mini;
35825     asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
35826     AssertTrue(asn_time->type == asn_utc_time);
35827     XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
35828     date_str[CTC_DATE_SIZE] = '\0';
35829     AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
35830 
35831     XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
35832     XMEMSET(date_str, 0, sizeof(date_str));
35833 
35834     /* Generalized time will overflow time_t if not long */
35835 #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
35836     s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
35837                                     DYNAMIC_TYPE_OPENSSL);
35838     /* GeneralizedTime notation test */
35839     /* 2055/03/01 09:00:00 */
35840     t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
35841         offset_day = 12;
35842         offset_sec = 10 * mini;
35843     asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
35844     AssertTrue(asn_time->type == asn_gen_time);
35845     XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
35846     date_str[CTC_DATE_SIZE] = '\0';
35847     AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
35848 
35849     XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
35850     XMEMSET(date_str, 0, sizeof(date_str));
35851 #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
35852 
35853     /* if WOLFSSL_ASN1_TIME struct is not allocated */
35854     s = NULL;
35855 
35856     t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
35857     offset_day = 7;
35858     offset_sec = 45 * mini;
35859     asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
35860     AssertTrue(asn_time->type == asn_utc_time);
35861     XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
35862     date_str[CTC_DATE_SIZE] = '\0';
35863     AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
35864     XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
35865 
35866     asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
35867     AssertTrue(asn_time->type == asn_utc_time);
35868     XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
35869     date_str[CTC_DATE_SIZE] = '\0';
35870     AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
35871     XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
35872 
35873     printf(resultFmt, passed);
35874 #endif
35875 }
35876 
35877 
35878 static void test_wolfSSL_ASN1_TIME_to_tm(void)
35879 {
35880 #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
35881     defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
35882     && !defined(NO_ASN_TIME)
35883     ASN1_TIME asnTime;
35884     struct tm tm;
35885 
35886     printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
35887 
35888     XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
35889     AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
35890     AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
35891 
35892     AssertIntEQ(tm.tm_sec, 15);
35893     AssertIntEQ(tm.tm_min, 15);
35894     AssertIntEQ(tm.tm_hour, 21);
35895     AssertIntEQ(tm.tm_mday, 22);
35896     AssertIntEQ(tm.tm_mon, 1);
35897     AssertIntEQ(tm.tm_year, 100);
35898     AssertIntEQ(tm.tm_isdst, 0);
35899 #ifdef XMKTIME
35900     AssertIntEQ(tm.tm_wday, 2);
35901     AssertIntEQ(tm.tm_yday, 52);
35902 #endif
35903 
35904     printf(resultFmt, passed);
35905 #endif
35906 }
35907 
35908 static void test_wolfSSL_X509_cmp_time(void)
35909 {
35910 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
35911 && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
35912     WOLFSSL_ASN1_TIME asn_time;
35913     time_t t;
35914 
35915     printf(testingFmt, "wolfSSL_X509_cmp_time()");
35916 
35917     AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
35918     XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
35919     AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
35920 
35921     AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
35922     AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
35923 
35924     printf(resultFmt, passed);
35925 #endif
35926 }
35927 
35928 static void test_wolfSSL_X509_time_adj(void)
35929 {
35930 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
35931     !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
35932     defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
35933     !defined(NO_ASN_TIME)
35934     X509*  x509;
35935     time_t t, not_before, not_after;
35936 
35937     printf(testingFmt, "wolfSSL_X509_time_adj()");
35938 
35939     AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
35940         client_cert_der_2048, sizeof_client_cert_der_2048,
35941         WOLFSSL_FILETYPE_ASN1));
35942 
35943     t = 0;
35944     not_before = XTIME(0);
35945     not_after = XTIME(0) + (60 * 24 * 30); /* 30 days after */
35946     AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
35947     AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
35948     /* Check X509_gmtime_adj, too. */
35949     AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
35950 
35951     X509_free(x509);
35952 
35953     printf(resultFmt, passed);
35954 #endif
35955 }
35956 
35957 
35958 static void test_wolfSSL_X509(void)
35959 {
35960     #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
35961     && !defined(NO_RSA)
35962     X509* x509;
35963 #ifndef NO_BIO
35964     BIO*  bio;
35965     X509_STORE_CTX* ctx;
35966     X509_STORE* store;
35967 #endif
35968 
35969     char der[] = "certs/ca-cert.der";
35970     XFILE fp;
35971 
35972     printf(testingFmt, "wolfSSL_X509()");
35973 
35974     AssertNotNull(x509 = X509_new());
35975     X509_free(x509);
35976 
35977 #ifndef NO_BIO
35978     x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
35979 
35980     AssertNotNull(bio = BIO_new(BIO_s_mem()));
35981 
35982 #ifdef WOLFSSL_CERT_GEN
35983     AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
35984 #endif
35985 
35986     AssertNotNull(ctx = X509_STORE_CTX_new());
35987 
35988     AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
35989 
35990     AssertNotNull(store = X509_STORE_new());
35991     AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
35992     AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
35993     AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
35994 
35995 
35996     X509_STORE_CTX_free(ctx);
35997     X509_STORE_free(store);
35998     X509_free(x509);
35999     BIO_free(bio);
36000 #endif
36001 
36002     /** d2i_X509_fp test **/
36003     fp = XFOPEN(der, "rb");
36004     AssertTrue((fp != XBADFILE));
36005     AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
36006     AssertNotNull(x509);
36007     X509_free(x509);
36008     XFCLOSE(fp);
36009     fp = XFOPEN(der, "rb");
36010     AssertTrue((fp != XBADFILE));
36011     AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
36012     AssertNotNull(x509);
36013     X509_free(x509);
36014     XFCLOSE(fp);
36015 
36016     /* X509_up_ref test */
36017     AssertIntEQ(X509_up_ref(NULL), 0);
36018     AssertNotNull(x509 = X509_new());   /* refCount = 1 */
36019     AssertIntEQ(X509_up_ref(x509), 1);  /* refCount = 2 */
36020     AssertIntEQ(X509_up_ref(x509), 1);  /* refCount = 3 */
36021     X509_free(x509); /* refCount = 2 */
36022     X509_free(x509); /* refCount = 1 */
36023     X509_free(x509); /* refCount = 0, free */
36024 
36025     printf(resultFmt, passed);
36026     #endif
36027 }
36028 
36029 static void test_wolfSSL_X509_get_ext_count(void)
36030 {
36031 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
36032     !defined(NO_RSA)
36033     int ret = 0;
36034     WOLFSSL_X509* x509;
36035     const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
36036     FILE* f;
36037 
36038     printf(testingFmt, "wolfSSL_X509_get_ext_count()");
36039 
36040     /* NULL parameter check */
36041     AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
36042 
36043     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
36044                                                              SSL_FILETYPE_PEM));
36045     AssertIntEQ(X509_get_ext_count(x509), 5);
36046     wolfSSL_X509_free(x509);
36047 
36048     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
36049                                                              SSL_FILETYPE_PEM));
36050     AssertIntEQ(X509_get_ext_count(x509), 5);
36051     wolfSSL_X509_free(x509);
36052 
36053     AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
36054     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
36055     fclose(f);
36056 
36057     printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
36058     AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
36059     printf(resultFmt, ret == 4 ? passed : failed);
36060 
36061     printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
36062     AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
36063     printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
36064 
36065     wolfSSL_X509_free(x509);
36066 
36067     printf(resultFmt, passed);
36068 #endif
36069 }
36070 
36071 static void test_wolfSSL_X509_sign2(void)
36072 {
36073 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
36074     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
36075     defined(WOLFSSL_CERT_EXT) && \
36076     (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
36077 
36078     WOLFSSL_X509 *x509, *ca;
36079     const unsigned char *der;
36080     const unsigned char *pt;
36081     WOLFSSL_EVP_PKEY  *priv;
36082     WOLFSSL_X509_NAME *name;
36083     WOLFSSL_ASN1_TIME *notBefore, *notAfter;
36084     int derSz;
36085 
36086     const int year = 365*24*60*60;
36087     const int day  = 24*60*60;
36088     const int hour = 60*60;
36089     const int mini = 60;
36090     time_t t;
36091 
36092     const unsigned char expected[] = {
36093         0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
36094         0x02, 0x02, 0x14, 0x53, 0x16, 0x7c, 0xa0, 0x56, 0x50, 0x46, 0x27, 0x82,
36095         0xed, 0x60, 0xb4, 0xda, 0x33, 0xd8, 0x6a, 0xc0, 0xea, 0xdc, 0x31, 0x30,
36096         0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
36097         0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
36098         0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
36099         0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
36100         0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
36101         0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
36102         0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
36103         0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
36104         0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
36105         0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
36106         0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
36107         0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
36108         0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
36109         0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
36110         0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
36111         0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
36112         0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
36113         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
36114         0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
36115         0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
36116         0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
36117         0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
36118         0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
36119         0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
36120         0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
36121         0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
36122         0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
36123         0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
36124         0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
36125         0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
36126         0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
36127         0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
36128         0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
36129         0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
36130         0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
36131         0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
36132         0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
36133         0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
36134         0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
36135         0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
36136         0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
36137         0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
36138         0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
36139         0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
36140         0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
36141         0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
36142         0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
36143         0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
36144         0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
36145         0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
36146         0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
36147         0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
36148         0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
36149         0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
36150         0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
36151         0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
36152         0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
36153         0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
36154         0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
36155         0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
36156         0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
36157         0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
36158         0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
36159         0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
36160         0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
36161         0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
36162         0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
36163         0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
36164         0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
36165         0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
36166         0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
36167         0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
36168         0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
36169         0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
36170         0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
36171         0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
36172         0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
36173         0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
36174         0x6f, 0x6d, 0x82, 0x14, 0x53, 0x16, 0x7c, 0xa0, 0x56, 0x50, 0x46, 0x27,
36175         0x82, 0xed, 0x60, 0xb4, 0xda, 0x33, 0xd8, 0x6a, 0xc0, 0xea, 0xdc, 0x31,
36176         0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
36177         0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
36178         0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
36179         0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
36180         0x01, 0x01, 0x00, 0x53, 0xba, 0xa1, 0xe4, 0x1b, 0x63, 0xea, 0x3d, 0x7a,
36181         0x6c, 0x21, 0xfb, 0x08, 0xb4, 0x42, 0x1d, 0xbc, 0xe6, 0x16, 0xd5, 0x3a,
36182         0x66, 0x1f, 0x8b, 0x7f, 0x9d, 0x89, 0x6b, 0xcc, 0x7e, 0xa6, 0x13, 0x95,
36183         0x94, 0x3f, 0xff, 0x0a, 0x0c, 0xca, 0xde, 0xa1, 0xf3, 0x97, 0xb4, 0xf9,
36184         0xf8, 0x2b, 0x5f, 0x02, 0x6a, 0xbb, 0x65, 0xd1, 0x49, 0x6c, 0xaf, 0x99,
36185         0xeb, 0x24, 0x7a, 0xd5, 0x4d, 0x8e, 0x7f, 0x12, 0xbc, 0x9b, 0x45, 0x38,
36186         0x76, 0x5e, 0xe5, 0x3e, 0x84, 0x49, 0x8d, 0xa4, 0xdb, 0xa5, 0x70, 0x15,
36187         0xc7, 0xc5, 0x9d, 0x5a, 0xac, 0xfb, 0x9e, 0x00, 0xf5, 0xde, 0xa2, 0x3d,
36188         0x10, 0x64, 0x60, 0xeb, 0x15, 0x9e, 0x70, 0x7e, 0xf9, 0x05, 0xe2, 0x71,
36189         0xaf, 0xe8, 0xf0, 0x98, 0xa6, 0x57, 0x0b, 0xfd, 0x63, 0x58, 0xa2, 0xf7,
36190         0x71, 0xdd, 0xb1, 0xc6, 0x76, 0x85, 0x12, 0x2b, 0x38, 0x18, 0xc9, 0x90,
36191         0x77, 0x78, 0x2a, 0xc1, 0x22, 0x88, 0x5e, 0xab, 0xbb, 0xcf, 0xf5, 0xe0,
36192         0x67, 0x1c, 0x2f, 0x62, 0x18, 0x1d, 0x9d, 0x22, 0x08, 0x87, 0x31, 0x77,
36193         0x47, 0x8b, 0x5e, 0x94, 0x3a, 0xb6, 0x99, 0xb4, 0x9d, 0x52, 0x8d, 0xb1,
36194         0xdc, 0xbe, 0x9c, 0x46, 0xde, 0xbe, 0xb1, 0xd8, 0xef, 0x65, 0x9c, 0xe3,
36195         0xcb, 0xea, 0x0b, 0xec, 0x36, 0xf6, 0xbb, 0x9c, 0x5f, 0x64, 0x9f, 0xfc,
36196         0x55, 0xc3, 0xf5, 0xab, 0x44, 0xd1, 0x89, 0x2f, 0x92, 0x9b, 0xa1, 0x93,
36197         0x46, 0x8c, 0xbe, 0xcf, 0x03, 0xff, 0x24, 0x74, 0x37, 0xdd, 0x30, 0x82,
36198         0xf6, 0x9f, 0xba, 0x15, 0xfe, 0xb5, 0x62, 0x83, 0x20, 0x9d, 0x3a, 0x26,
36199         0x11, 0x1b, 0xa0, 0xcd, 0xa1, 0x43, 0x28, 0xc7, 0x06, 0x55, 0x69, 0x26,
36200         0x90, 0x57, 0xb7, 0xd0, 0x5b, 0x8d, 0xee, 0x2e, 0x82, 0xee, 0x3f, 0xe7,
36201         0xe2, 0x47, 0x25, 0x98, 0x9c, 0x83, 0x10
36202     };
36203 
36204     printf(testingFmt, "wolfSSL_X509_sign2");
36205 
36206     pt = ca_key_der_2048;
36207     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
36208                                                 sizeof_ca_key_der_2048));
36209 
36210     pt = client_cert_der_2048;
36211     AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
36212                 sizeof_client_cert_der_2048));
36213 
36214     pt = ca_cert_der_2048;
36215     AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
36216     AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
36217     AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
36218 
36219     t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
36220     AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
36221     AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
36222     AssertIntEQ(notAfter->length, 13);
36223 
36224     AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
36225     AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
36226 
36227     wolfSSL_X509_sign(x509, priv, EVP_sha256());
36228     AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
36229 
36230     AssertIntEQ(derSz, sizeof(expected));
36231     AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
36232 
36233     wolfSSL_X509_free(ca);
36234     wolfSSL_X509_free(x509);
36235     wolfSSL_EVP_PKEY_free(priv);
36236     wolfSSL_ASN1_TIME_free(notBefore);
36237     wolfSSL_ASN1_TIME_free(notAfter);
36238     printf(resultFmt, passed);
36239 #endif
36240 }
36241 
36242 
36243 static void test_wolfSSL_X509_sign(void)
36244 {
36245 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
36246     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
36247     int ret;
36248     char *cn;
36249     word32 cnSz;
36250     X509_NAME *name;
36251     X509 *x509, *ca;
36252     DecodedCert dCert;
36253     EVP_PKEY *pub;
36254     EVP_PKEY *priv;
36255     EVP_MD_CTX *mctx;
36256 #if defined(USE_CERT_BUFFERS_1024)
36257     const unsigned char* rsaPriv = client_key_der_1024;
36258     const unsigned char* rsaPub = client_keypub_der_1024;
36259     const unsigned char* certIssuer = client_cert_der_1024;
36260     long clientKeySz = (long)sizeof_client_key_der_1024;
36261     long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
36262     long certIssuerSz = (long)sizeof_client_cert_der_1024;
36263 #elif defined(USE_CERT_BUFFERS_2048)
36264     const unsigned char* rsaPriv = client_key_der_2048;
36265     const unsigned char* rsaPub = client_keypub_der_2048;
36266     const unsigned char* certIssuer = client_cert_der_2048;
36267     long clientKeySz = (long)sizeof_client_key_der_2048;
36268     long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
36269     long certIssuerSz = (long)sizeof_client_cert_der_2048;
36270 #endif
36271     byte sn[16];
36272     int snSz = sizeof(sn);
36273 
36274     printf(testingFmt, "wolfSSL_X509_sign");
36275 
36276     /* Set X509_NAME fields */
36277     AssertNotNull(name = X509_NAME_new());
36278     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
36279                                        (byte*)"US", 2, -1, 0), SSL_SUCCESS);
36280     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
36281                              (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
36282     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
36283                      (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
36284 
36285     /* Get private and public keys */
36286     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
36287                                                                   clientKeySz));
36288     AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
36289     AssertNotNull(x509 = X509_new());
36290     /* Set version 3 */
36291     AssertIntNE(X509_set_version(x509, 2L), 0);
36292     /* Set subject name, add pubkey, and sign certificate */
36293     AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
36294     X509_NAME_free(name);
36295     AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
36296 #ifdef WOLFSSL_ALT_NAMES
36297     /* Add some subject alt names */
36298     AssertIntNE(wolfSSL_X509_add_altname(NULL,
36299                 "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
36300     AssertIntEQ(wolfSSL_X509_add_altname(x509,
36301                 NULL, ASN_DNS_TYPE), SSL_SUCCESS);
36302     AssertIntEQ(wolfSSL_X509_add_altname(x509,
36303                 "sphygmomanometer",
36304                 ASN_DNS_TYPE), SSL_SUCCESS);
36305     AssertIntEQ(wolfSSL_X509_add_altname(x509,
36306                 "supercalifragilisticexpialidocious",
36307                 ASN_DNS_TYPE), SSL_SUCCESS);
36308     AssertIntEQ(wolfSSL_X509_add_altname(x509,
36309                 "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
36310                 ASN_DNS_TYPE), SSL_SUCCESS);
36311 #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
36312     {
36313         unsigned char ip4_type[] = {127,128,0,255};
36314         unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
36315                                     0xff, 0xee, 0x99, 0x88,
36316                                     0x77, 0x66, 0x55, 0x44,
36317                                     0x00, 0x33, 0x22, 0x11};
36318         AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
36319                 sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
36320         AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
36321                 sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
36322     }
36323 #endif
36324 #endif /* WOLFSSL_ALT_NAMES */
36325 
36326     /* test valid sign case */
36327     ret = X509_sign(x509, priv, EVP_sha256());
36328 
36329     /* test valid X509_sign_ctx case */
36330     AssertNotNull(mctx = EVP_MD_CTX_new());
36331     AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
36332     AssertIntGT(X509_sign_ctx(x509, mctx), 0);
36333 
36334 #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
36335     AssertIntEQ(X509_get_ext_count(x509), 1);
36336 #endif
36337 #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
36338     AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
36339     AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
36340 #endif
36341 
36342     AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
36343                 WOLFSSL_SUCCESS);
36344     DEBUG_WRITE_CERT_X509(x509, "signed.pem");
36345 
36346     /* Variation in size depends on ASN.1 encoding when MSB is set.
36347      * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
36348      * with the MSB set. See GenerateInteger in asn.c */
36349 #ifndef USE_CERT_BUFFERS_1024
36350 #ifndef WOLFSSL_ALT_NAMES
36351     /* Valid case - size should be 798-797 with 16 byte serial number */
36352     AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
36353 #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
36354     /* Valid case - size should be 955-956 with 16 byte serial number */
36355     AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
36356 #else
36357     /* Valid case - size should be 926-927 with 16 byte serial number */
36358     AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
36359 #endif
36360 #else
36361 #ifndef WOLFSSL_ALT_NAMES
36362     /* Valid case - size should be 537-538 with 16 byte serial number */
36363     AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
36364 #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
36365     /* Valid case - size should be 695-696 with 16 byte serial number */
36366     AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
36367 #else
36368     /* Valid case - size should be 666-667 with 16 byte serial number */
36369     AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
36370 #endif
36371 #endif
36372     /* check that issuer name is as expected after signature */
36373     InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
36374     AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
36375 
36376     AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
36377     AssertNotNull(name = X509_get_subject_name(ca));
36378     cnSz = X509_NAME_get_sz(name);
36379     AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
36380     AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
36381     AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
36382     XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
36383 
36384 #ifdef WOLFSSL_MULTI_ATTRIB
36385     /* test adding multiple OU's to the signer */
36386     AssertNotNull(name = X509_get_subject_name(ca));
36387     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
36388                                        (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
36389     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
36390                                        (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
36391     AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
36392 #endif
36393 
36394     AssertNotNull(name = X509_get_subject_name(ca));
36395     AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
36396 
36397     AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
36398     AssertNotNull(name = X509_get_issuer_name(x509));
36399     cnSz = X509_NAME_get_sz(name);
36400     AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
36401     AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
36402     /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
36403     AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
36404     XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
36405 
36406     FreeDecodedCert(&dCert);
36407 
36408     /* Test invalid parameters */
36409     AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
36410     AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
36411     AssertIntEQ(X509_sign(x509, priv, NULL), 0);
36412 
36413     AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
36414     EVP_MD_CTX_free(mctx);
36415     AssertNotNull(mctx = EVP_MD_CTX_new());
36416     AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
36417     AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
36418 
36419     /* test invalid version number */
36420 #if defined(OPENSSL_ALL)
36421     AssertIntNE(X509_set_version(x509, 6L), 0);
36422     AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
36423 
36424     /* uses ParseCert which fails on bad version number */
36425     AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
36426 #endif
36427 
36428     EVP_MD_CTX_free(mctx);
36429     EVP_PKEY_free(priv);
36430     EVP_PKEY_free(pub);
36431     X509_free(x509);
36432     X509_free(ca);
36433 
36434     printf(resultFmt, passed);
36435 #endif
36436 }
36437 
36438 static void test_wolfSSL_X509_get0_tbs_sigalg(void)
36439 {
36440 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
36441     X509* x509 = NULL;
36442     const X509_ALGOR* alg;
36443     printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
36444 
36445     AssertNotNull(x509 = X509_new());
36446 
36447     AssertNull(alg = X509_get0_tbs_sigalg(NULL));
36448     AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
36449 
36450     X509_free(x509);
36451 
36452     printf(resultFmt, passed);
36453 #endif
36454 }
36455 
36456 static void test_wolfSSL_X509_ALGOR_get0(void)
36457 {
36458 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
36459     !defined(NO_SHA256) && !defined(NO_RSA)
36460     X509* x509 = NULL;
36461     const ASN1_OBJECT* obj = NULL;
36462     const X509_ALGOR* alg;
36463     int pptype = 0;
36464     const void *ppval = NULL;
36465     printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
36466 
36467     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
36468                                                              SSL_FILETYPE_PEM));
36469     AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
36470 
36471     /* Invalid case */
36472     X509_ALGOR_get0(&obj, NULL, NULL, NULL);
36473     AssertNull(obj);
36474 
36475     /* Valid case */
36476     X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
36477     AssertNotNull(obj);
36478     AssertNull(ppval);
36479     AssertIntNE(pptype, 0);
36480     /* Make sure NID of X509_ALGOR is Sha256 with RSA */
36481     AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
36482 
36483     X509_free(x509);
36484 
36485     printf(resultFmt, passed);
36486 #endif
36487 }
36488 
36489 
36490 static void test_wolfSSL_X509_VERIFY_PARAM(void)
36491 {
36492 #if defined(OPENSSL_EXTRA)
36493     X509_VERIFY_PARAM *paramTo;
36494     X509_VERIFY_PARAM *paramFrom;
36495     int ret;
36496     char testIPv4[] = "127.0.0.1";
36497     char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
36498     char testhostName1[] = "foo.hoge.com";
36499     char testhostName2[] = "foobar.hoge.com";
36500 
36501     printf(testingFmt, "wolfSSL_X509()");
36502 
36503     paramTo = X509_VERIFY_PARAM_new();
36504     AssertNotNull(paramTo);
36505     XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
36506 
36507     paramFrom = X509_VERIFY_PARAM_new();
36508     AssertNotNull(paramFrom);
36509     XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
36510 
36511     ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
36512                                          (int)XSTRLEN(testhostName1));
36513     AssertIntEQ(1, ret);
36514     AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
36515                                         (int)XSTRLEN(testhostName1)));
36516 
36517     X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
36518 
36519     X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
36520     AssertIntEQ(0x01, paramFrom->hostFlags);
36521 
36522     ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
36523     AssertIntEQ(0, ret);
36524 
36525     ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
36526     AssertIntEQ(1, ret);
36527     AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
36528 
36529     ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
36530     AssertIntEQ(1, ret);
36531 
36532     ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
36533     AssertIntEQ(1, ret);
36534     AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
36535 
36536     /* null pointer */
36537     ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
36538     AssertIntEQ(WOLFSSL_FAILURE, ret);
36539     /* in the case of "from" null, returns success */
36540     ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
36541     AssertIntEQ(WOLFSSL_SUCCESS, ret);
36542 
36543     ret = X509_VERIFY_PARAM_set1(NULL, NULL);
36544     AssertIntEQ(WOLFSSL_FAILURE, ret);
36545 
36546     /* inherit flags test : VPARAM_DEFAULT */
36547     ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
36548     AssertIntEQ(1, ret);
36549     AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
36550                                     (int)XSTRLEN(testhostName1)));
36551     AssertIntEQ(0x01, paramTo->hostFlags);
36552     AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
36553 
36554     /* inherit flags test : VPARAM OVERWRITE */
36555     X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
36556                                     (int)XSTRLEN(testhostName2));
36557     X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
36558     X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
36559 
36560     paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
36561 
36562     ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
36563     AssertIntEQ(1, ret);
36564     AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
36565                                         (int)XSTRLEN(testhostName1)));
36566     AssertIntEQ(0x01, paramTo->hostFlags);
36567     AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
36568 
36569     /* inherit flags test : VPARAM_RESET_FLAGS */
36570     X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
36571                                       (int)XSTRLEN(testhostName2));
36572     X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
36573     X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
36574 
36575     paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
36576 
36577     ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
36578     AssertIntEQ(1, ret);
36579     AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
36580                                         (int)XSTRLEN(testhostName1)));
36581     AssertIntEQ(0x01, paramTo->hostFlags);
36582     AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
36583 
36584     /* inherit flags test : VPARAM_LOCKED */
36585     X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
36586                                     (int)XSTRLEN(testhostName2));
36587     X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
36588     X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
36589 
36590     paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
36591 
36592     ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
36593     AssertIntEQ(1, ret);
36594     AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
36595                                     (int)XSTRLEN(testhostName2)));
36596     AssertIntEQ(0x00, paramTo->hostFlags);
36597     AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
36598 
36599     /* test for incorrect parameters */
36600     ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
36601     AssertIntEQ(0, ret);
36602 
36603     ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
36604     AssertIntEQ(0, ret);
36605 
36606     /* inherit flags test : VPARAM_ONCE, not testable yet */
36607 
36608     ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
36609     AssertIntEQ(1, ret);
36610 
36611     ret = X509_VERIFY_PARAM_get_flags(paramTo);
36612     AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
36613 
36614     ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
36615     AssertIntEQ(1, ret);
36616 
36617     ret = X509_VERIFY_PARAM_get_flags(paramTo);
36618     AssertIntEQ(0, ret);
36619 
36620     X509_VERIFY_PARAM_free(paramTo);
36621     X509_VERIFY_PARAM_free(paramFrom);
36622     X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
36623     printf(resultFmt, passed);
36624 
36625 #endif
36626 }
36627 
36628 static void test_wolfSSL_X509_get_X509_PUBKEY(void)
36629 {
36630 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
36631     X509* x509 = NULL;
36632     X509_PUBKEY* pubKey;
36633     printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
36634 
36635     AssertNotNull(x509 = X509_new());
36636 
36637     AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
36638     AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
36639 
36640     X509_free(x509);
36641 
36642     printf(resultFmt, passed);
36643 #endif
36644 }
36645 
36646 static void test_wolfSSL_X509_PUBKEY_RSA(void)
36647 {
36648 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
36649     !defined(NO_SHA256) && !defined(NO_RSA)
36650     X509* x509 = NULL;
36651     ASN1_OBJECT* obj = NULL;
36652     const ASN1_OBJECT* pa_oid = NULL;
36653     X509_PUBKEY* pubKey;
36654     X509_PUBKEY* pubKey2;
36655     EVP_PKEY* evpKey;
36656 
36657     const unsigned char *pk;
36658     int ppklen, pptype;
36659     X509_ALGOR *pa;
36660     const void *pval;
36661 
36662     printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
36663 
36664     AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
36665                                                     SSL_FILETYPE_PEM));
36666 
36667     AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
36668     AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
36669     AssertNotNull(pk);
36670     AssertNotNull(pa);
36671     AssertNotNull(pubKey);
36672     AssertIntGT(ppklen, 0);
36673 
36674     AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
36675 
36676     AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
36677     AssertNotNull(pubKey2 = X509_PUBKEY_new());
36678     AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
36679     AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
36680     AssertNotNull(pk);
36681     AssertNotNull(pa);
36682     AssertIntGT(ppklen, 0);
36683     X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
36684     AssertNotNull(pa_oid);
36685     AssertNull(pval);
36686     AssertIntEQ(pptype, V_ASN1_NULL);
36687     AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
36688 
36689     X509_PUBKEY_free(pubKey2);
36690     X509_free(x509);
36691     EVP_PKEY_free(evpKey);
36692 
36693     printf(resultFmt, passed);
36694 #endif
36695 }
36696 
36697 static void test_wolfSSL_X509_PUBKEY_EC(void)
36698 {
36699 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
36700     X509* x509 = NULL;
36701     ASN1_OBJECT* obj = NULL;
36702     ASN1_OBJECT* poid;
36703     const ASN1_OBJECT* pa_oid = NULL;
36704     X509_PUBKEY* pubKey;
36705     X509_PUBKEY* pubKey2;
36706     EVP_PKEY* evpKey;
36707 
36708     const unsigned char *pk;
36709     int ppklen, pptype;
36710     X509_ALGOR *pa;
36711     const void *pval;
36712     char buf[50];
36713 
36714     printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
36715 
36716     AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
36717                                                     SSL_FILETYPE_PEM));
36718     AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
36719     AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
36720     AssertNotNull(pubKey2 = X509_PUBKEY_new());
36721     AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
36722     AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
36723     AssertNotNull(pk);
36724     AssertNotNull(pa);
36725     AssertIntGT(ppklen, 0);
36726     X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
36727     AssertNotNull(pa_oid);
36728     AssertNotNull(pval);
36729     AssertIntEQ(pptype, V_ASN1_OBJECT);
36730     AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
36731     poid = (ASN1_OBJECT *)pval;
36732     AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
36733     AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
36734 
36735     X509_PUBKEY_free(pubKey2);
36736     X509_free(x509);
36737     EVP_PKEY_free(evpKey);
36738 
36739     printf(resultFmt, passed);
36740 #endif
36741 }
36742 
36743 static void test_wolfSSL_X509_PUBKEY_DSA(void)
36744 {
36745 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
36746     word32  bytes;
36747 #ifdef USE_CERT_BUFFERS_1024
36748     byte    tmp[ONEK_BUF];
36749     XMEMSET(tmp, 0, sizeof(tmp));
36750     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
36751     bytes = sizeof_dsa_key_der_1024;
36752 #elif defined(USE_CERT_BUFFERS_2048)
36753     byte    tmp[TWOK_BUF];
36754     XMEMSET(tmp, 0, sizeof(tmp));
36755     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
36756     bytes = sizeof_dsa_key_der_2048;
36757 #else
36758     byte    tmp[TWOK_BUF];
36759     XMEMSET(tmp, 0, sizeof(tmp));
36760     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
36761     if (fp == XBADFILE) {
36762         return WOLFSSL_BAD_FILE;
36763     }
36764     bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
36765     XFCLOSE(fp);
36766 #endif /* END USE_CERT_BUFFERS_1024 */
36767     const unsigned char* dsaKeyDer = tmp;
36768 
36769     ASN1_OBJECT* obj = NULL;
36770     ASN1_STRING* str;
36771     const ASN1_OBJECT* pa_oid = NULL;
36772     X509_PUBKEY* pubKey = NULL;
36773     EVP_PKEY* evpKey = NULL;
36774 
36775     const unsigned char *pk;
36776     int ppklen, pptype;
36777     X509_ALGOR *pa;
36778     const void *pval;
36779 
36780     printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
36781 
36782     /* Initialize pkey with der format dsa key */
36783     AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
36784 
36785     AssertNotNull(pubKey = X509_PUBKEY_new());
36786     AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
36787     AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
36788     AssertNotNull(pk);
36789     AssertNotNull(pa);
36790     AssertIntGT(ppklen, 0);
36791     X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
36792     AssertNotNull(pa_oid);
36793     AssertNotNull(pval);
36794     AssertIntEQ(pptype, V_ASN1_SEQUENCE);
36795     AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
36796     str = (ASN1_STRING *)pval;
36797     DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
36798 #ifdef USE_CERT_BUFFERS_1024
36799     AssertIntEQ(ASN1_STRING_length(str), 291);
36800 #else
36801     AssertIntEQ(ASN1_STRING_length(str), 549);
36802 #endif /* END USE_CERT_BUFFERS_1024 */
36803 
36804     X509_PUBKEY_free(pubKey);
36805     EVP_PKEY_free(evpKey);
36806 
36807     printf(resultFmt, passed);
36808 #endif
36809 }
36810 
36811 static void test_wolfSSL_RAND(void)
36812 {
36813     #if defined(OPENSSL_EXTRA)
36814     byte seed[16];
36815 
36816     printf(testingFmt, "wolfSSL_RAND()");
36817 
36818     RAND_seed(seed, sizeof(seed));
36819     AssertIntEQ(RAND_poll(), 1);
36820     RAND_cleanup();
36821 
36822     AssertIntEQ(RAND_egd(NULL), -1);
36823 #ifndef NO_FILESYSTEM
36824     {
36825         char fname[100];
36826 
36827         AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
36828         AssertIntEQ(RAND_write_file(NULL), 0);
36829     }
36830 #endif
36831 
36832     printf(resultFmt, passed);
36833     #endif
36834 }
36835 
36836 
36837 static void test_wolfSSL_BUF(void)
36838 {
36839     #if defined(OPENSSL_EXTRA)
36840     BUF_MEM* buf;
36841     AssertNotNull(buf = BUF_MEM_new());
36842     AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
36843     AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
36844     BUF_MEM_free(buf);
36845     #endif /* OPENSSL_EXTRA */
36846 }
36847 
36848 #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
36849 static int stub_rand_seed(const void *buf, int num)
36850 {
36851     (void)buf;
36852     (void)num;
36853 
36854     return 123;
36855 }
36856 
36857 static int stub_rand_bytes(unsigned char *buf, int num)
36858 {
36859     (void)buf;
36860     (void)num;
36861 
36862     return 456;
36863 }
36864 
36865 static byte* was_stub_rand_cleanup_called(void)
36866 {
36867     static byte was_called = 0;
36868 
36869     return &was_called;
36870 }
36871 
36872 static void stub_rand_cleanup(void)
36873 {
36874     byte* was_called = was_stub_rand_cleanup_called();
36875 
36876     *was_called = 1;
36877 
36878     return;
36879 }
36880 
36881 static byte* was_stub_rand_add_called(void)
36882 {
36883     static byte was_called = 0;
36884 
36885     return &was_called;
36886 }
36887 
36888 static int stub_rand_add(const void *buf, int num, double entropy)
36889 {
36890     byte* was_called = was_stub_rand_add_called();
36891 
36892     (void)buf;
36893     (void)num;
36894     (void)entropy;
36895 
36896     *was_called = 1;
36897 
36898     return 0;
36899 }
36900 
36901 static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
36902 {
36903     (void)buf;
36904     (void)num;
36905 
36906     return 9876;
36907 }
36908 
36909 static int stub_rand_status(void)
36910 {
36911     return 5432;
36912 }
36913 #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
36914 
36915 static void test_wolfSSL_RAND_set_rand_method(void)
36916 {
36917 #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
36918     RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
36919     unsigned char* buf = NULL;
36920     int num = 0;
36921     double entropy = 0;
36922     byte* was_cleanup_called = was_stub_rand_cleanup_called();
36923     byte* was_add_called = was_stub_rand_add_called();
36924 
36925     printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
36926 
36927     buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
36928                                                DYNAMIC_TYPE_TMP_BUFFER);
36929 
36930     AssertIntNE(wolfSSL_RAND_status(), 5432);
36931     AssertIntEQ(*was_cleanup_called, 0);
36932     RAND_cleanup();
36933     AssertIntEQ(*was_cleanup_called, 0);
36934 
36935 
36936     rand_methods.seed = &stub_rand_seed;
36937     rand_methods.bytes = &stub_rand_bytes;
36938     rand_methods.cleanup = &stub_rand_cleanup;
36939     rand_methods.add = &stub_rand_add;
36940     rand_methods.pseudorand = &stub_rand_pseudo_bytes;
36941     rand_methods.status = &stub_rand_status;
36942 
36943     AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
36944     AssertIntEQ(RAND_seed(buf, num), 123);
36945     AssertIntEQ(RAND_bytes(buf, num), 456);
36946     AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
36947     AssertIntEQ(RAND_status(), 5432);
36948 
36949     AssertIntEQ(*was_add_called, 0);
36950     /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
36951     RAND_add(buf, num, entropy);
36952     AssertIntEQ(*was_add_called, 1);
36953     was_add_called = 0;
36954     AssertIntEQ(*was_cleanup_called, 0);
36955     RAND_cleanup();
36956     AssertIntEQ(*was_cleanup_called, 1);
36957     *was_cleanup_called = 0;
36958 
36959 
36960     AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
36961     AssertIntNE(RAND_status(), 5432);
36962     AssertIntEQ(*was_cleanup_called, 0);
36963     RAND_cleanup();
36964     AssertIntEQ(*was_cleanup_called, 0);
36965 
36966     XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
36967 
36968     printf(resultFmt, passed);
36969 #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
36970 }
36971 
36972 static void test_wolfSSL_RAND_bytes(void)
36973 {
36974     #if defined(OPENSSL_EXTRA)
36975     const int size1 = RNG_MAX_BLOCK_LEN;        /* in bytes */
36976     const int size2 = RNG_MAX_BLOCK_LEN + 1;    /* in bytes */
36977     const int size3 = RNG_MAX_BLOCK_LEN * 2;    /* in bytes */
36978     const int size4 = RNG_MAX_BLOCK_LEN * 4;    /* in bytes */
36979     int  max_bufsize;
36980     byte *my_buf;
36981 
36982     printf(testingFmt, "test_wolfSSL_RAND_bytes()");
36983     /* sanity check */
36984     AssertIntEQ(RAND_bytes(NULL, 16), 0);
36985     AssertIntEQ(RAND_bytes(NULL, 0), 0);
36986 
36987     max_bufsize = size4;
36988 
36989     my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
36990                                                      DYNAMIC_TYPE_TMP_BUFFER);
36991 
36992     AssertIntEQ(RAND_bytes(my_buf, 0), 1);
36993     AssertIntEQ(RAND_bytes(my_buf, -1), 0);
36994 
36995     AssertNotNull(my_buf);
36996     XMEMSET(my_buf, 0, max_bufsize);
36997     AssertIntEQ(RAND_bytes(my_buf, size1), 1);
36998     AssertIntEQ(RAND_bytes(my_buf, size2), 1);
36999     AssertIntEQ(RAND_bytes(my_buf, size3), 1);
37000     AssertIntEQ(RAND_bytes(my_buf, size4), 1);
37001 
37002     XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
37003 
37004     printf(resultFmt, passed);
37005 
37006     #endif
37007 }
37008 
37009 static void test_wolfSSL_BN_rand(void)
37010 {
37011     #if defined(OPENSSL_EXTRA)
37012     BIGNUM* bn;
37013     BIGNUM* range;
37014 
37015     printf(testingFmt, "wolfSSL_BN_rand()");
37016 
37017     AssertNotNull(bn = BN_new());
37018     AssertIntNE(BN_rand(bn, 0, 0, 0), SSL_SUCCESS);
37019     BN_free(bn);
37020 
37021     AssertNotNull(bn = BN_new());
37022     AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
37023     BN_free(bn);
37024 
37025     AssertNotNull(bn = BN_new());
37026     AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
37027     BN_free(bn);
37028 
37029     AssertNotNull(bn = BN_new());
37030     AssertNotNull(range = BN_new());
37031     AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
37032     AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
37033     BN_free(bn);
37034     BN_free(range);
37035 
37036     printf(resultFmt, passed);
37037     #endif
37038 }
37039 
37040 static void test_wolfSSL_pseudo_rand(void)
37041 {
37042     #if defined(OPENSSL_EXTRA)
37043     BIGNUM* bn;
37044     unsigned char bin[8];
37045     int i;
37046 
37047     printf(testingFmt, "wolfSSL_pseudo_rand()");
37048 
37049     /* BN_pseudo_rand returns 1 on success 0 on failure
37050      * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
37051     for (i = 0; i < 10; i++) {
37052         AssertNotNull(bn = BN_new());
37053         AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
37054         AssertIntGT(BN_bn2bin(bn, bin),0);
37055         AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
37056         BN_free(bn);
37057     }
37058 
37059     for (i = 0; i < 10; i++) {
37060         AssertNotNull(bn = BN_new());
37061         AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
37062         AssertIntGT(BN_bn2bin(bn, bin),0);
37063         AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
37064         BN_free(bn);
37065     }
37066 
37067     printf(resultFmt, passed);
37068     #endif
37069 }
37070 
37071 static void test_wolfSSL_PKCS8_Compat(void)
37072 {
37073     #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
37074     #ifndef NO_BIO
37075     PKCS8_PRIV_KEY_INFO* pt;
37076     BIO* bio;
37077     XFILE f;
37078     int bytes;
37079     char pkcs8_buffer[512];
37080 #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
37081     EVP_PKEY *pkey = NULL;
37082 #endif
37083 
37084     printf(testingFmt, "wolfSSL_pkcs8()");
37085 
37086     /* file from wolfssl/certs/ directory */
37087     f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
37088     AssertTrue(f != XBADFILE);
37089     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
37090     XFCLOSE(f);
37091     AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
37092     AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
37093 
37094 #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
37095     AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
37096     AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
37097 
37098     /* gets PKCS8 pointer to pkey */
37099     AssertNotNull(EVP_PKEY2PKCS8(pkey));
37100 
37101     EVP_PKEY_free(pkey);
37102 #endif
37103 
37104     BIO_free(bio);
37105     PKCS8_PRIV_KEY_INFO_free(pt);
37106 
37107     printf(resultFmt, passed);
37108     #endif
37109     #endif
37110 }
37111 
37112 static void test_wolfSSL_PKCS8_d2i(void)
37113 {
37114 #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
37115     /* This test ends up using HMAC as a part of PBKDF2, and HMAC
37116      * requires a 12 byte password in FIPS mode. This test ends up
37117      * trying to use an 8 byte password. */
37118 
37119 #ifndef NO_FILESYSTEM
37120     unsigned char pkcs8_buffer[2048];
37121     const unsigned char* p;
37122     int bytes;
37123     XFILE file;
37124     WOLFSSL_EVP_PKEY* pkey = NULL;
37125 #ifndef NO_BIO
37126     BIO* bio;
37127     #if defined(OPENSSL_ALL) && \
37128             ((!defined(NO_RSA) && !defined(NO_DES3)) || \
37129                defined(HAVE_ECC)) && \
37130         !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
37131     WOLFSSL_EVP_PKEY* evpPkey = NULL;
37132     #endif
37133 #endif
37134 #ifndef NO_RSA
37135     const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
37136     const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
37137     #ifndef NO_DES3
37138     const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
37139     #endif
37140 #endif /* NO_RSA */
37141 #ifdef HAVE_ECC
37142     const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
37143     const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
37144     #ifndef NO_DES3
37145     const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
37146     #endif
37147 #endif /* HAVE_ECC */
37148 #endif /* !NO_FILESYSTEM */
37149 
37150 #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
37151 #ifndef NO_RSA
37152     #ifdef USE_CERT_BUFFERS_1024
37153     const unsigned char* rsa = (unsigned char*)server_key_der_1024;
37154     int rsaSz = sizeof_server_key_der_1024;
37155     #else
37156     const unsigned char* rsa = (unsigned char*)server_key_der_2048;
37157     int rsaSz = sizeof_server_key_der_2048;
37158     #endif
37159 #endif
37160 #ifdef HAVE_ECC
37161     const unsigned char* ec = (unsigned char*)ecc_key_der_256;
37162     int ecSz = sizeof_ecc_key_der_256;
37163 #endif
37164 #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
37165 
37166 
37167 #ifndef NO_FILESYSTEM
37168    (void)pkcs8_buffer;
37169    (void)p;
37170    (void)bytes;
37171    (void)file;
37172 #ifndef NO_BIO
37173    (void)bio;
37174 #endif
37175 #endif
37176 
37177 #ifdef OPENSSL_ALL
37178 #ifndef NO_RSA
37179     /* Try to auto-detect normal RSA private key */
37180     AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
37181     EVP_PKEY_free(pkey);
37182 #endif
37183 #ifdef HAVE_ECC
37184     /* Try to auto-detect normal EC private key */
37185     AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
37186     EVP_PKEY_free(pkey);
37187 #endif
37188 #endif /* OPENSSL_ALL */
37189 
37190 #ifndef NO_FILESYSTEM
37191 #ifndef NO_RSA
37192     /* Get DER encoded RSA PKCS#8 data. */
37193     file = XFOPEN(rsaDerPkcs8File, "rb");
37194     AssertTrue(file != XBADFILE);
37195     XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
37196     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37197                                                                      file)), 0);
37198     XFCLOSE(file);
37199 
37200     p = pkcs8_buffer;
37201 #ifdef OPENSSL_ALL
37202     /* Try to decode - auto-detect key type. */
37203     AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
37204 #else
37205     AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
37206 #endif
37207 
37208     /* Get PEM encoded RSA PKCS#8 data. */
37209     file = XFOPEN(rsaPemPkcs8File, "rb");
37210     AssertTrue(file != XBADFILE);
37211     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37212                                                                      file)), 0);
37213     XFCLOSE(file);
37214 #if defined(OPENSSL_ALL) && \
37215     !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
37216     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37217     /* Write PKCS#8 PEM to BIO. */
37218     AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
37219                                                                   NULL), bytes);
37220     /* Compare file and written data */
37221     AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
37222     AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
37223     BIO_free(bio);
37224 #ifndef NO_DES3
37225     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37226     /* Write Encrypted PKCS#8 PEM to BIO. */
37227     bytes = 1834;
37228     AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
37229                           NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
37230     AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
37231                                                             (void*)"yassl123"));
37232     EVP_PKEY_free(evpPkey);
37233     BIO_free(bio);
37234 #endif /* !NO_DES3 */
37235 #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
37236     EVP_PKEY_free(pkey);
37237 
37238     /* PKCS#8 encrypted RSA key */
37239 #ifndef NO_DES3
37240     file = XFOPEN(rsaDerPkcs8EncFile, "rb");
37241     AssertTrue(file != XBADFILE);
37242     XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
37243     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37244                                                                      file)), 0);
37245     XFCLOSE(file);
37246 #if defined(OPENSSL_ALL) && \
37247     !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
37248     AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
37249     AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
37250                                                             (void*)"yassl123"));
37251     EVP_PKEY_free(pkey);
37252     BIO_free(bio);
37253 #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
37254 #endif /* !NO_DES3 */
37255 #endif /* NO_RSA */
37256 
37257 #ifdef HAVE_ECC
37258     /* PKCS#8 encode EC key */
37259     file = XFOPEN(ecDerPkcs8File, "rb");
37260     AssertTrue(file != XBADFILE);
37261     XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
37262     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37263                                                                      file)), 0);
37264     XFCLOSE(file);
37265 
37266     p = pkcs8_buffer;
37267 #ifdef OPENSSL_ALL
37268     /* Try to decode - auto-detect key type. */
37269     AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
37270 #else
37271     AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
37272 #endif
37273 
37274     /* Get PEM encoded RSA PKCS#8 data. */
37275     file = XFOPEN(ecPemPkcs8File, "rb");
37276     AssertTrue(file != XBADFILE);
37277     XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
37278     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37279                                                                      file)), 0);
37280     XFCLOSE(file);
37281 #if defined(OPENSSL_ALL) && \
37282     !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
37283     defined(HAVE_AES_CBC)
37284     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37285     /* Write PKCS#8 PEM to BIO. */
37286     AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
37287                                                                   NULL), bytes);
37288     /* Compare file and written data */
37289     AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
37290     AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
37291     BIO_free(bio);
37292     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37293     /* Write Encrypted PKCS#8 PEM to BIO. */
37294     bytes = 379;
37295     AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
37296                           NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
37297     AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
37298                                                             (void*)"yassl123"));
37299     EVP_PKEY_free(evpPkey);
37300     BIO_free(bio);
37301 #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
37302     EVP_PKEY_free(pkey);
37303 
37304     /* PKCS#8 encrypted EC key */
37305 #ifndef NO_DES3
37306     file = XFOPEN(ecDerPkcs8EncFile, "rb");
37307     AssertTrue(file != XBADFILE);
37308     XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
37309     AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
37310                                                                      file)), 0);
37311     XFCLOSE(file);
37312 #if defined(OPENSSL_ALL) && \
37313     !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
37314     AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
37315     AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
37316                                                             (void*)"yassl123"));
37317     EVP_PKEY_free(pkey);
37318     BIO_free(bio);
37319 #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
37320 #endif /* !NO_DES3 */
37321 #endif /* HAVE_ECC */
37322 
37323 #endif /* !NO_FILESYSTEM */
37324 
37325     printf(resultFmt, passed);
37326 #endif /* HAVE_FIPS && OPENSSL_EXTRA */
37327 }
37328 
37329 #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
37330     defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
37331 #define LOGGING_THREADS 5
37332 #define ERROR_COUNT 10
37333 static volatile int loggingThreadsReady;
37334 static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
37335 {
37336     const char* file;
37337     int line;
37338     int err;
37339     int errorCount = 0;
37340     int i;
37341 
37342     (void)args;
37343 
37344     while (!loggingThreadsReady);
37345     for (i = 0; i < ERROR_COUNT; i++)
37346         ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
37347 
37348     while ((err = ERR_get_error_line(&file, &line))) {
37349         AssertIntEQ(err, 990 + errorCount);
37350         errorCount++;
37351     }
37352     AssertIntEQ(errorCount, ERROR_COUNT);
37353 
37354     /* test max queue behavior, trying to add an arbitrary 3 errors over */
37355     errorCount = 0;
37356     for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
37357         ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
37358 
37359     while ((err = ERR_get_error_line(&file, &line))) {
37360         AssertIntEQ(err, 990 + errorCount);
37361         errorCount++;
37362     }
37363 
37364     /* test that the 3 errors over the max were dropped */
37365     AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
37366 
37367     return 0;
37368 }
37369 #endif
37370 
37371 static void test_error_queue_per_thread(void)
37372 {
37373 #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
37374     defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
37375     THREAD_TYPE loggingThreads[LOGGING_THREADS];
37376     int i;
37377 
37378     printf(testingFmt, "error_queue_per_thread()");
37379 
37380     ERR_clear_error(); /* clear out any error nodes */
37381 
37382     loggingThreadsReady = 0;
37383     for (i = 0; i < LOGGING_THREADS; i++)
37384         start_thread(test_logging, NULL, &loggingThreads[i]);
37385     loggingThreadsReady = 1;
37386     for (i = 0; i < LOGGING_THREADS; i++)
37387         join_thread(loggingThreads[i]);
37388 
37389     printf(resultFmt, passed);
37390 #endif
37391 }
37392 
37393 static void test_wolfSSL_ERR_put_error(void)
37394 {
37395     #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
37396         defined(DEBUG_WOLFSSL)
37397     const char* file;
37398     int line;
37399 
37400     printf(testingFmt, "wolfSSL_ERR_put_error()");
37401 
37402 
37403     ERR_clear_error(); /* clear out any error nodes */
37404     ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
37405     AssertIntEQ(ERR_get_error_line(&file, &line), 0);
37406     ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
37407     AssertIntEQ(ERR_get_error_line(&file, &line), 1);
37408     ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
37409     AssertIntEQ(ERR_get_error_line(&file, &line), 2);
37410     ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
37411     AssertIntEQ(ERR_get_error_line(&file, &line), 3);
37412     ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
37413     AssertIntEQ(ERR_get_error_line(&file, &line), 4);
37414     ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
37415     AssertIntEQ(ERR_get_error_line(&file, &line), 5);
37416     ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
37417     AssertIntEQ(ERR_get_error_line(&file, &line), 6);
37418     ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
37419     AssertIntEQ(ERR_get_error_line(&file, &line), 7);
37420     ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
37421     AssertIntEQ(ERR_get_error_line(&file, &line), 8);
37422     ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
37423     AssertIntEQ(ERR_get_error_line(&file, &line), 9);
37424     ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
37425     AssertIntEQ(ERR_get_error_line(&file, &line), 10);
37426     ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
37427     AssertIntEQ(ERR_get_error_line(&file, &line), 11);
37428     ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
37429     AssertIntEQ(ERR_get_error_line(&file, &line), 12);
37430     ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
37431     AssertIntEQ(ERR_get_error_line(&file, &line), 13);
37432     ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
37433     AssertIntEQ(ERR_get_error_line(&file, &line), 14);
37434     ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
37435     AssertIntEQ(ERR_get_error_line(&file, &line), 15);
37436 
37437 #ifdef WOLFSSL_PYTHON
37438     ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
37439             "this file", 100);
37440     AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
37441             (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
37442     AssertIntEQ(line, 100);
37443     AssertIntEQ(wolfSSL_ERR_peek_error(),
37444             (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
37445     AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
37446 #endif
37447 
37448     /* try reading past end of error queue */
37449     file = NULL;
37450     AssertIntEQ(ERR_get_error_line(&file, &line), 0);
37451     AssertNull(file);
37452     AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
37453 
37454     PEMerr(4,4);
37455     AssertIntEQ(ERR_get_error(), 4);
37456     /* Empty and free up all error nodes */
37457     ERR_clear_error();
37458 
37459     /* Verify all nodes are cleared */
37460     ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
37461     ERR_clear_error();
37462     AssertIntEQ(ERR_get_error_line(&file, &line), 0);
37463 
37464     printf(resultFmt, passed);
37465     #endif
37466 }
37467 
37468 
37469 #ifndef NO_BIO
37470 
37471 static void test_wolfSSL_ERR_print_errors(void)
37472 {
37473     #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
37474         defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
37475     BIO* bio;
37476     char buf[1024];
37477 
37478     printf(testingFmt, "wolfSSL_ERR_print_errors()");
37479 
37480 
37481     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37482     ERR_clear_error(); /* clear out any error nodes */
37483     ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
37484     /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
37485     ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
37486 
37487     ERR_print_errors(bio);
37488     AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
37489     AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
37490                 buf, 55), 0);
37491     AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
37492     AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
37493                 buf, 56), 0);
37494     AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
37495     AssertIntEQ(buf[0], '\0');
37496     AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
37497 
37498     BIO_free(bio);
37499     printf(resultFmt, passed);
37500     #endif
37501 }
37502 
37503 #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
37504     defined(DEBUG_WOLFSSL)
37505 static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
37506 {
37507     wolfSSL_BIO_write((BIO*)u, str, (int)len);
37508     return 0;
37509 }
37510 #endif
37511 
37512 static void test_wolfSSL_ERR_print_errors_cb(void)
37513 {
37514     #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
37515         defined(DEBUG_WOLFSSL)
37516     BIO* bio;
37517     char buf[1024];
37518 
37519     printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
37520 
37521     AssertNotNull(bio = BIO_new(BIO_s_mem()));
37522     ERR_clear_error(); /* clear out any error nodes */
37523     ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
37524     ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
37525 
37526     ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
37527     AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
37528     AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
37529                 buf, 53), 0);
37530     AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
37531                 buf + 53, 55), 0);
37532     AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
37533 
37534     BIO_free(bio);
37535     printf(resultFmt, passed);
37536     #endif
37537 }
37538 /*
37539  * Testing WOLFSSL_ERROR_MSG
37540  */
37541 static int test_WOLFSSL_ERROR_MSG (void)
37542 {
37543     int ret = 0;
37544 #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
37545     defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
37546     const char* msg = TEST_STRING;
37547 
37548     printf(testingFmt, "WOLFSSL_ERROR_MSG()");
37549 
37550     WOLFSSL_ERROR_MSG(msg);
37551 
37552     printf(resultFmt, ret == 0 ? passed : failed);
37553 
37554 #endif
37555     return ret;
37556 }/*End test_WOLFSSL_ERROR_MSG*/
37557 /*
37558  * Testing wc_ERR_remove_state
37559  */
37560 static int test_wc_ERR_remove_state (void)
37561 {
37562     int ret = 0;
37563 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
37564 
37565     printf(testingFmt, "wc_ERR_remove_state()");
37566 
37567     wc_ERR_remove_state();
37568 
37569     printf(resultFmt, ret == 0 ? passed : failed);
37570 
37571 #endif
37572     return ret;
37573 }/*End test_wc_ERR_remove_state*/
37574 /*
37575  * Testing wc_ERR_print_errors_fp
37576  */
37577 static int test_wc_ERR_print_errors_fp (void)
37578 {
37579     int ret = 0;
37580 #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
37581     (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
37582     long sz;
37583 
37584     printf(testingFmt, "wc_ERR_print_errors_fp()");
37585 
37586     WOLFSSL_ERROR(BAD_FUNC_ARG);
37587     XFILE fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
37588     wc_ERR_print_errors_fp(fp);
37589 #if defined(DEBUG_WOLFSSL)
37590     AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
37591     sz = XFTELL(fp);
37592     #ifdef NO_ERROR_QUEUE
37593     /* File should be empty when NO_ERROR_QUEUE is defined */
37594     if (sz != 0) {
37595         ret = BAD_FUNC_ARG;
37596     }
37597     #else
37598     if (sz == 0) {
37599         ret = BAD_FUNC_ARG;
37600     }
37601     #endif
37602 #endif
37603     printf(resultFmt, ret == 0 ? passed : failed);
37604     XFCLOSE(fp);
37605     (void)sz;
37606 #endif
37607     return ret;
37608 }/*End test_wc_ERR_print_errors_fp*/
37609 #ifdef DEBUG_WOLFSSL
37610 static void Logging_cb(const int logLevel, const char *const logMessage)
37611 {
37612     (void)logLevel;
37613     (void)logMessage;
37614 }
37615 #endif
37616 /*
37617  * Testing wolfSSL_GetLoggingCb
37618  */
37619 static int test_wolfSSL_GetLoggingCb (void)
37620 {
37621     int ret = 0;
37622     printf(testingFmt, "wolfSSL_GetLoggingCb()");
37623 #ifdef DEBUG_WOLFSSL
37624 
37625     /* Testing without wolfSSL_SetLoggingCb() */
37626     if (ret == 0) {
37627         if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
37628             ret = 0;
37629         }
37630         if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
37631             ret = -1;
37632         }
37633     }
37634     /* Testing with wolfSSL_SetLoggingCb() */
37635     if (ret == 0) {
37636         ret = wolfSSL_SetLoggingCb(Logging_cb);
37637         if (ret == 0){
37638             if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
37639                 ret = -1;
37640             }
37641             if (ret == 0) {
37642                 if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
37643                     ret = 0;
37644                 }
37645             }
37646 
37647             /* reset logging callback */
37648             wolfSSL_SetLoggingCb(NULL);
37649         }
37650     }
37651 #endif
37652     if (ret == 0) {
37653         if (wolfSSL_GetLoggingCb() != NULL) {
37654             ret = -1;
37655         }
37656     }
37657 
37658     printf(resultFmt, ret == 0 ? passed : failed);
37659 
37660     return ret;
37661 }/*End test_wolfSSL_GetLoggingCb*/
37662 
37663 #endif /* !NO_BIO */
37664 
37665 #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
37666     defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
37667     defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
37668 static void test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
37669 {
37670     static const unsigned char key[] = "simple test key";
37671     HMAC_CTX* hmac;
37672     ENGINE* e = NULL;
37673     unsigned char hash[WC_MAX_DIGEST_SIZE];
37674     unsigned int len;
37675 
37676     AssertNotNull(hmac = HMAC_CTX_new());
37677     HMAC_CTX_init(hmac);
37678     AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
37679                 SSL_SUCCESS);
37680 
37681     /* re-using test key as data to hash */
37682     AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
37683     AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
37684     AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
37685     AssertIntEQ(len, md_len);
37686     AssertIntEQ(HMAC_size(hmac), md_len);
37687     AssertStrEQ(HMAC_CTX_get_md(hmac), md);
37688 
37689     HMAC_cleanup(hmac);
37690     HMAC_CTX_free(hmac);
37691 
37692     len = 0;
37693     AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
37694     AssertIntEQ(len, md_len);
37695 }
37696 #endif
37697 
37698 static void test_wolfSSL_HMAC(void)
37699 {
37700 #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
37701     defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
37702     defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
37703     printf(testingFmt, "wolfSSL_HMAC()");
37704 
37705     #ifndef NO_SHA256
37706         test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
37707     #endif
37708     #ifdef WOLFSSL_SHA224
37709         test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
37710     #endif
37711     #ifdef WOLFSSL_SHA384
37712         test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
37713     #endif
37714     #ifdef WOLFSSL_SHA512
37715         test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
37716     #endif
37717     #ifdef WOLFSSL_SHA3
37718         #ifndef WOLFSSL_NOSHA3_224
37719             test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
37720         #endif
37721         #ifndef WOLFSSL_NOSHA3_256
37722             test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
37723         #endif
37724         #ifndef WOLFSSL_NOSHA3_384
37725             test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
37726         #endif
37727         #ifndef WOLFSSL_NOSHA3_512
37728             test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
37729         #endif
37730     #endif
37731 
37732     printf(resultFmt, passed);
37733 #endif
37734 }
37735 
37736 static void test_wolfSSL_CMAC(void)
37737 {
37738 #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
37739     defined(WOLFSSL_AES_DIRECT)
37740     int i;
37741     byte key[AES_128_KEY_SIZE];
37742     CMAC_CTX* cmacCtx = NULL;
37743     byte out[AES_BLOCK_SIZE];
37744     size_t outLen = AES_BLOCK_SIZE;
37745 
37746     printf(testingFmt, "test_wolfSSL_CMAC()");
37747 
37748     for (i=0; i < AES_128_KEY_SIZE; ++i) {
37749         key[i] = i;
37750     }
37751     AssertNotNull(cmacCtx = CMAC_CTX_new());
37752     /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
37753     AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
37754     AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
37755         NULL), SSL_SUCCESS);
37756     /* re-using test key as data to hash */
37757     AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
37758     AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
37759     AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
37760     AssertIntEQ(outLen, AES_BLOCK_SIZE);
37761     CMAC_CTX_free(cmacCtx);
37762 
37763     printf(resultFmt, passed);
37764 #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
37765 }
37766 
37767 
37768 static void test_wolfSSL_OBJ(void)
37769 {
37770 /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
37771  * mode
37772  */
37773 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
37774     !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
37775     defined(WOLFSSL_CERT_GEN)
37776     ASN1_OBJECT *obj = NULL;
37777     ASN1_OBJECT *obj2 = NULL;
37778     char buf[50];
37779 
37780     XFILE fp;
37781     X509 *x509 = NULL;
37782     X509_NAME *x509Name;
37783     X509_NAME_ENTRY *x509NameEntry;
37784     ASN1_OBJECT *asn1Name = NULL;
37785     int numNames;
37786     BIO *bio = NULL;
37787     int nid;
37788     int i, j;
37789     const char *f[] = {
37790         #ifndef NO_RSA
37791         "./certs/ca-cert.der",
37792         #endif
37793         #ifdef HAVE_ECC
37794         "./certs/ca-ecc-cert.der",
37795         "./certs/ca-ecc384-cert.der",
37796         #endif
37797         NULL};
37798     ASN1_OBJECT *field_name_obj = NULL;
37799     int lastpos = -1;
37800     int tmp = -1;
37801     ASN1_STRING *asn1 = NULL;
37802     unsigned char *buf_dyn = NULL;
37803 
37804     printf(testingFmt, "wolfSSL_OBJ()");
37805 
37806     AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
37807     AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
37808     AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
37809     AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
37810     AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
37811     ASN1_OBJECT_free(obj);
37812 
37813     AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
37814     AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
37815     AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
37816 #ifdef WOLFSSL_CERT_EXT
37817     AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
37818 #endif
37819     AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
37820     AssertNotNull(obj2 = OBJ_dup(obj));
37821     AssertIntEQ(OBJ_cmp(obj, obj2), 0);
37822     ASN1_OBJECT_free(obj);
37823     ASN1_OBJECT_free(obj2);
37824 
37825     for (i = 0; f[i] != NULL; i++)
37826     {
37827         AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
37828         AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
37829         XFCLOSE(fp);
37830         AssertNotNull(x509Name = X509_get_issuer_name(x509));
37831         AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
37832 
37833         /* Get the Common Name by using OBJ_txt2obj */
37834         AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
37835         do
37836         {
37837             lastpos = tmp;
37838             tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
37839         } while (tmp > -1);
37840         AssertIntNE(lastpos, -1);
37841         ASN1_OBJECT_free(field_name_obj);
37842         AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
37843         AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
37844         AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
37845         /*
37846          * All Common Names should be www.wolfssl.com
37847          * This makes testing easier as we can test for the expected value.
37848          */
37849         AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
37850         OPENSSL_free(buf_dyn);
37851         bio = BIO_new(BIO_s_mem());
37852         AssertTrue(bio != NULL);
37853         for (j = 0; j < numNames; j++)
37854         {
37855             AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
37856             AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
37857             AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
37858         }
37859         BIO_free(bio);
37860         X509_free(x509);
37861 
37862     }
37863 
37864 #ifdef HAVE_PKCS12
37865     {
37866         PKCS12 *p12;
37867         int boolRet;
37868         EVP_PKEY *pkey = NULL;
37869         const char *p12_f[] = {
37870             #if !defined(NO_DES3) && !defined(NO_RSA)
37871             "./certs/test-servercert.p12",
37872             #endif
37873             NULL};
37874 
37875         for (i = 0; p12_f[i] != NULL; i++)
37876         {
37877             AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
37878             AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
37879             XFCLOSE(fp);
37880             AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
37881                                                &pkey, &x509, NULL)) > 0);
37882             wc_PKCS12_free(p12);
37883             EVP_PKEY_free(pkey);
37884             x509Name = X509_get_issuer_name(x509);
37885             AssertNotNull(x509Name);
37886             AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
37887             AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
37888             for (j = 0; j < numNames; j++)
37889             {
37890                 AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
37891                 AssertNotNull(asn1Name =
37892                         X509_NAME_ENTRY_get_object(x509NameEntry));
37893                 AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
37894             }
37895             BIO_free(bio);
37896             X509_free(x509);
37897         }
37898     }
37899 #endif /* HAVE_PKCS12 */
37900 
37901     printf(resultFmt, passed);
37902 #endif
37903 }
37904 
37905 static void test_wolfSSL_i2a_ASN1_OBJECT(void)
37906 {
37907 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
37908     ASN1_OBJECT *obj = NULL;
37909     BIO *bio = NULL;
37910 
37911     AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
37912     AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
37913 
37914     AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
37915     AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
37916 
37917     AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
37918 
37919     BIO_free(bio);
37920     ASN1_OBJECT_free(obj);
37921 #endif
37922 }
37923 
37924 static void test_wolfSSL_OBJ_cmp(void)
37925 {
37926 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
37927     ASN1_OBJECT *obj = NULL;
37928     ASN1_OBJECT *obj2 = NULL;
37929 
37930     printf(testingFmt, "wolfSSL_OBJ_cmp()");
37931 
37932     AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
37933     AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
37934 
37935     AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
37936     AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
37937     AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
37938     AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
37939     AssertIntEQ(OBJ_cmp(obj, obj), 0);
37940     AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
37941 
37942     ASN1_OBJECT_free(obj);
37943     ASN1_OBJECT_free(obj2);
37944 
37945     printf(resultFmt, passed);
37946 #endif
37947 }
37948 
37949 static void test_wolfSSL_OBJ_txt2nid(void)
37950 {
37951 #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
37952     int i;
37953     static const struct {
37954         const char* sn;
37955         const char* ln;
37956         const char* oid;
37957         int nid;
37958     } testVals[] = {
37959         { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
37960         { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
37961                                                              NID_id_on_dnsSRV },
37962         { "msUPN", "Microsoft User Principal Name",
37963                                          "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
37964         { NULL, NULL, NULL, NID_undef }
37965     };
37966 
37967     printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
37968 
37969     /* Invalid cases */
37970     AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
37971     AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
37972 
37973     /* Valid cases */
37974     for (i = 0; testVals[i].sn != NULL; i++) {
37975         AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
37976         AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
37977         AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
37978     }
37979 
37980     printf(resultFmt, passed);
37981 #endif
37982 }
37983 
37984 static void test_wolfSSL_OBJ_txt2obj(void)
37985 {
37986 #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
37987         defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
37988     int i;
37989     char buf[50];
37990     ASN1_OBJECT* obj;
37991     static const struct {
37992         const char* oidStr;
37993         const char* sn;
37994         const char* ln;
37995     } objs_list[] = {
37996     #if defined(WOLFSSL_APACHE_HTTPD)
37997         { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
37998         { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
37999     #endif
38000         { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
38001         { NULL, NULL, NULL }
38002     };
38003 
38004     printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
38005 
38006     AssertNull(obj = OBJ_txt2obj("Bad name", 0));
38007     AssertNull(obj = OBJ_txt2obj(NULL, 0));
38008 
38009     for (i = 0; objs_list[i].oidStr != NULL; i++) {
38010         /* Test numerical value of oid (oidStr) */
38011         AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
38012         /* Convert object back to text to confirm oid is correct */
38013         wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
38014         AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
38015         ASN1_OBJECT_free(obj);
38016         XMEMSET(buf, 0, sizeof(buf));
38017 
38018         /* Test short name (sn) */
38019         AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
38020         AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
38021         /* Convert object back to text to confirm oid is correct */
38022         wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
38023         AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
38024         ASN1_OBJECT_free(obj);
38025         XMEMSET(buf, 0, sizeof(buf));
38026 
38027         /* Test long name (ln) - should fail when no_name = 1 */
38028         AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
38029         AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
38030         /* Convert object back to text to confirm oid is correct */
38031         wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
38032         AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
38033         ASN1_OBJECT_free(obj);
38034         XMEMSET(buf, 0, sizeof(buf));
38035     }
38036 
38037     printf(resultFmt, passed);
38038 
38039 #endif
38040 }
38041 
38042 static void test_wolfSSL_i2t_ASN1_OBJECT(void)
38043 {
38044 #if defined(OPENSSL_EXTRA) && \
38045     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
38046 
38047     char buf[50] = {0};
38048     ASN1_OBJECT* obj;
38049     const char* oid = "2.5.29.19";
38050     const char* ln  = "X509v3 Basic Constraints";
38051 
38052     printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
38053 
38054     obj = NULL;
38055     AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
38056     AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
38057     AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
38058 
38059     AssertNotNull(obj = OBJ_txt2obj(oid, 0));
38060     XMEMSET(buf, 0, sizeof(buf));
38061     AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
38062     AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
38063     ASN1_OBJECT_free(obj);
38064 
38065     printf(resultFmt, passed);
38066 #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
38067 }
38068 
38069 static void test_wolfSSL_PEM_write_bio_X509(void)
38070 {
38071 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
38072     defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
38073     /* This test contains the hard coded expected
38074      * lengths. Update if necessary */
38075 
38076     BIO* input;
38077     BIO* output;
38078     X509* x509 = NULL;
38079     int expectedLen;
38080 
38081     printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
38082 
38083     AssertNotNull(input = BIO_new_file(
38084             "certs/test/cert-ext-multiple.pem", "rb"));
38085     AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
38086 
38087     AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
38088 
38089     AssertNotNull(PEM_read_bio_X509(input, &x509, NULL, NULL));
38090 
38091     AssertIntEQ(PEM_write_bio_X509(output, x509), WOLFSSL_SUCCESS);
38092 
38093 #ifdef WOLFSSL_ALT_NAMES
38094     /* Here we copy the validity struct from the original */
38095     expectedLen = 2000;
38096 #else
38097     /* Only difference is that we generate the validity in generalized
38098      * time. Generating UTCTime vs Generalized time should be fixed in
38099      * the future */
38100     expectedLen = 2004;
38101 #endif
38102     AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
38103 
38104     /* Reset output buffer */
38105     BIO_free(output);
38106     AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
38107 
38108     /* Test forcing the AKID to be generated just from KeyIdentifier */
38109     if (x509->authKeyIdSrc != NULL) {
38110         XMEMMOVE(x509->authKeyIdSrc, x509->authKeyId, x509->authKeyIdSz);
38111         x509->authKeyId = x509->authKeyIdSrc;
38112         x509->authKeyIdSrc = NULL;
38113         x509->authKeyIdSrcSz = 0;
38114     }
38115 
38116     AssertIntEQ(PEM_write_bio_X509(output, x509), WOLFSSL_SUCCESS);
38117 
38118     /* Check that we generate a smaller output since the AKID will
38119      * only contain the KeyIdentifier without any additional
38120      * information */
38121 
38122 #ifdef WOLFSSL_ALT_NAMES
38123     /* Here we copy the validity struct from the original */
38124     expectedLen = 1688;
38125 #else
38126     /* UTCTime vs Generalized time */
38127     expectedLen = 1692;
38128 #endif
38129     AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
38130 
38131     X509_free(x509);
38132     BIO_free(input);
38133     BIO_free(output);
38134 
38135     printf(resultFmt, passed);
38136 #endif
38137 }
38138 
38139 static void test_wolfSSL_X509_NAME_ENTRY(void)
38140 {
38141 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
38142     !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
38143     X509*      x509;
38144 #ifndef NO_BIO
38145     BIO*       bio;
38146 #endif
38147     X509_NAME* nm;
38148     X509_NAME_ENTRY* entry;
38149     unsigned char cn[] = "another name to add";
38150 
38151     printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
38152 
38153     AssertNotNull(x509 =
38154             wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
38155 #ifndef NO_BIO
38156     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38157     AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
38158 #endif
38159 
38160 #ifdef WOLFSSL_CERT_REQ
38161     {
38162         X509_REQ* req;
38163 #ifndef NO_BIO
38164         BIO*      bReq;
38165 #endif
38166 
38167         AssertNotNull(req =
38168             wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
38169 #ifndef NO_BIO
38170         AssertNotNull(bReq = BIO_new(BIO_s_mem()));
38171         AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
38172 
38173         BIO_free(bReq);
38174 #endif
38175         X509_free(req);
38176     }
38177 #endif
38178 
38179     AssertNotNull(nm = X509_get_subject_name(x509));
38180 
38181     /* Test add entry */
38182     AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
38183                 0x0c, cn, (int)sizeof(cn)));
38184     AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
38185 
38186 #ifdef WOLFSSL_CERT_EXT
38187     AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
38188                                            (byte*)"support@wolfssl.com", 19, -1,
38189                                            1), WOLFSSL_SUCCESS);
38190 #endif
38191     X509_NAME_ENTRY_free(entry);
38192 
38193 #ifdef WOLFSSL_CERT_REQ
38194     {
38195         unsigned char srv_pkcs9p[] = "Server";
38196         char* subject;
38197         AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
38198             MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
38199 
38200         subject = X509_NAME_oneline(nm, 0, 0);
38201     #ifdef DEBUG_WOLFSSL
38202         printf("\n\t%s\n", subject);
38203     #endif
38204         XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
38205     }
38206 #endif
38207 
38208     /* Test add entry by text */
38209     AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
38210                 0x0c, cn, (int)sizeof(cn)));
38211     #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
38212     || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
38213     AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
38214                 V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
38215     #endif
38216     AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
38217     X509_NAME_ENTRY_free(entry);
38218 
38219     /* Test add entry by NID */
38220     AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
38221                                        cn, -1, -1, 0), SSL_SUCCESS);
38222 
38223 #ifndef NO_BIO
38224     BIO_free(bio);
38225 #endif
38226     X509_free(x509); /* free's nm */
38227 
38228     printf(resultFmt, passed);
38229 #endif
38230 }
38231 
38232 
38233 static void test_wolfSSL_X509_set_name(void)
38234 {
38235 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
38236     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
38237     X509* x509;
38238     X509_NAME* name;
38239 
38240     printf(testingFmt, "wolfSSL_X509_set_name()");
38241 
38242     AssertNotNull(name = X509_NAME_new());
38243     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
38244                                            (byte*)"wolfssl.com", 11, 0, 1),
38245                 WOLFSSL_SUCCESS);
38246     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
38247                                            (byte*)"support@wolfssl.com", 19, -1,
38248                                            1), WOLFSSL_SUCCESS);
38249     AssertNotNull(x509 = X509_new());
38250 
38251     AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
38252     AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
38253     AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
38254     AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
38255 
38256     AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
38257     AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
38258     AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
38259     AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
38260 
38261     X509_free(x509);
38262     X509_NAME_free(name);
38263 
38264     printf(resultFmt, passed);
38265 #endif /* OPENSSL_ALL && !NO_CERTS */
38266 }
38267 
38268 static void test_wolfSSL_X509_set_notAfter(void)
38269 {
38270 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
38271     && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
38272     !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
38273     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
38274     !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
38275     /* Generalized time will overflow time_t if not long */
38276 
38277     X509* x;
38278     BIO*  bio;
38279     ASN1_TIME *asn_time, *time_check;
38280     const int year = 365*24*60*60;
38281     const int day  = 24*60*60;
38282     const int hour = 60*60;
38283     const int mini = 60;
38284     int offset_day;
38285     unsigned char buf[25];
38286     time_t t;
38287 
38288     printf(testingFmt, "wolfSSL_X509_set_notAfter()");
38289     /*
38290      * Setup asn_time. APACHE HTTPD uses time(NULL)
38291      */
38292     t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
38293     offset_day = 7;
38294     /*
38295      * Free these.
38296      */
38297     asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
38298     AssertNotNull(asn_time);
38299     AssertNotNull(x = X509_new());
38300     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38301     /*
38302      * Tests
38303      */
38304     AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
38305     /* time_check is simply (ANS1_TIME*)x->notAfter */
38306     AssertNotNull(time_check = X509_get_notAfter(x));
38307     /* ANS1_TIME_check validates by checking if argument can be parsed */
38308     AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
38309     /* Convert to human readable format and compare to intended date */
38310     AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
38311     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
38312     AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
38313     /*
38314      * Cleanup
38315      */
38316     XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
38317     X509_free(x);
38318     BIO_free(bio);
38319     printf(resultFmt, passed);
38320 #endif
38321 }
38322 
38323 static void test_wolfSSL_X509_set_notBefore(void)
38324 {
38325 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
38326     && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
38327     !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
38328     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
38329 
38330     X509* x;
38331     BIO*  bio;
38332     ASN1_TIME *asn_time, *time_check;
38333     const int year = 365*24*60*60;
38334     const int day  = 24*60*60;
38335     const int hour = 60*60;
38336     const int mini = 60;
38337     int offset_day;
38338     unsigned char buf[25];
38339     time_t t;
38340 
38341     printf(testingFmt, "wolfSSL_X509_set_notBefore()");
38342     /*
38343      * Setup asn_time. APACHE HTTPD uses time(NULL)
38344      */
38345     t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
38346     offset_day = 7;
38347 
38348     /*
38349      * Free these.
38350      */
38351     asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
38352     AssertNotNull(asn_time);
38353     AssertNotNull(x = X509_new());
38354     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38355     AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
38356 
38357     /*
38358      * Main Tests
38359      */
38360     AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
38361     /* time_check == (ANS1_TIME*)x->notBefore */
38362     AssertNotNull(time_check = X509_get_notBefore(x));
38363     /* ANS1_TIME_check validates by checking if argument can be parsed */
38364     AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
38365     /* Convert to human readable format and compare to intended date */
38366     AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
38367     AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
38368     AssertIntEQ(XMEMCMP(buf, "May  8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
38369     /*
38370      * Cleanup
38371      */
38372     XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
38373     X509_free(x);
38374     BIO_free(bio);
38375     printf(resultFmt, passed);
38376 #endif
38377 }
38378 
38379 static void test_wolfSSL_X509_set_version(void)
38380 {
38381 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
38382     !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
38383     X509* x509;
38384     long v = 2L;
38385     long maxInt = INT_MAX;
38386 
38387     AssertNotNull(x509 = X509_new());
38388     /* These should pass. */
38389     AssertTrue(wolfSSL_X509_set_version(x509, v));
38390     AssertIntEQ(v, wolfSSL_X509_get_version(x509));
38391     /* Fail Case: When v(long) is greater than x509->version(int). */
38392     v = maxInt+1;
38393     AssertFalse(wolfSSL_X509_set_version(x509, v));
38394     /* Cleanup */
38395     X509_free(x509);
38396     printf(resultFmt, passed);
38397 #endif
38398 }
38399 
38400 #ifndef NO_BIO
38401 
38402 static void test_wolfSSL_BIO_gets(void)
38403 {
38404     #if defined(OPENSSL_EXTRA)
38405     BIO* bio;
38406     BIO* bio2;
38407     char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
38408     char emp[] = "";
38409     char bio_buffer[20];
38410     int bufferSz = 20;
38411 
38412     printf(testingFmt, "wolfSSL_BIO_gets()");
38413 
38414     /* try with bad args */
38415     AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
38416 
38417     /* try with real msg */
38418     AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
38419     XMEMSET(bio_buffer, 0, bufferSz);
38420     AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
38421     AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
38422     AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
38423     AssertFalse(bio2 != BIO_next(bio));
38424 
38425     /* make buffer filled with no terminating characters */
38426     XMEMSET(bio_buffer, 1, bufferSz);
38427 
38428     /* BIO_gets reads a line of data */
38429     AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
38430     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
38431     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
38432     AssertStrEQ(bio_buffer, "hello wolfSSL\n");
38433     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
38434     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
38435     AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
38436 
38437     /* check not null terminated string */
38438     BIO_free(bio);
38439     msg[0] = 0x33;
38440     msg[1] = 0x33;
38441     msg[2] = 0x33;
38442     AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
38443     AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
38444     AssertIntEQ(bio_buffer[0], msg[0]);
38445     AssertIntEQ(bio_buffer[1], msg[1]);
38446     AssertIntNE(bio_buffer[2], msg[2]);
38447 
38448     BIO_free(bio);
38449     msg[3]    = 0x33;
38450     bio_buffer[3] = 0x33;
38451     AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
38452     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
38453     AssertIntEQ(bio_buffer[0], msg[0]);
38454     AssertIntEQ(bio_buffer[1], msg[1]);
38455     AssertIntEQ(bio_buffer[2], msg[2]);
38456     AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
38457 
38458     /* check reading an empty string */
38459     BIO_free(bio);
38460     AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
38461     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
38462     AssertStrEQ(emp, bio_buffer);
38463     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
38464 
38465     /* check error cases */
38466     BIO_free(bio);
38467     AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
38468     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38469     AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
38470 
38471 #if !defined(NO_FILESYSTEM)
38472     {
38473         BIO*  f_bio;
38474         XFILE f;
38475         AssertNotNull(f_bio = BIO_new(BIO_s_file()));
38476         AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
38477 
38478         f = XFOPEN(svrCertFile, "rb");
38479         AssertTrue((f != XBADFILE));
38480         AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
38481         AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
38482 
38483         BIO_free(f_bio);
38484     }
38485 #endif /* NO_FILESYSTEM */
38486 
38487     BIO_free(bio);
38488     BIO_free(bio2);
38489 
38490     /* try with type BIO */
38491     XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
38492             sizeof(msg));
38493     AssertNotNull(bio = BIO_new(BIO_s_bio()));
38494     AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
38495     AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
38496 
38497     AssertIntEQ(BIO_set_write_buf_size(bio, 10),           SSL_SUCCESS);
38498     AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
38499     AssertIntEQ(BIO_make_bio_pair(bio, bio2),              SSL_SUCCESS);
38500 
38501     AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
38502     AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
38503     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
38504     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
38505     AssertStrEQ(bio_buffer, "hello wolfSSL\n");
38506     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
38507     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
38508     AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
38509 
38510     BIO_free(bio);
38511     BIO_free(bio2);
38512 
38513     /* check reading an empty string */
38514     AssertNotNull(bio = BIO_new(BIO_s_bio()));
38515     AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
38516     AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
38517     AssertStrEQ(emp, bio_buffer);
38518 
38519     BIO_free(bio);
38520 
38521     printf(resultFmt, passed);
38522     #endif
38523 }
38524 
38525 
38526 static void test_wolfSSL_BIO_puts(void)
38527 {
38528     #if defined(OPENSSL_EXTRA)
38529     BIO* bio;
38530     char input[] = "hello\0world\n.....ok\n\0";
38531     char output[128];
38532 
38533     printf(testingFmt, "wolfSSL_BIO_puts()");
38534 
38535     XMEMSET(output, 0, sizeof(output));
38536     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38537     AssertIntEQ(BIO_puts(bio, input), 5);
38538     AssertIntEQ(BIO_pending(bio), 5);
38539     AssertIntEQ(BIO_puts(bio, input + 6), 14);
38540     AssertIntEQ(BIO_pending(bio), 19);
38541     AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
38542     AssertStrEQ(output, "helloworld\n");
38543     AssertIntEQ(BIO_pending(bio), 8);
38544     AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
38545     AssertStrEQ(output, ".....ok\n");
38546     AssertIntEQ(BIO_pending(bio), 0);
38547     AssertIntEQ(BIO_puts(bio, ""), -1);
38548 
38549     BIO_free(bio);
38550     printf(resultFmt, passed);
38551     #endif
38552 }
38553 
38554 #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
38555     !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
38556     defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
38557 static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
38558 {
38559     (void)ssl;
38560     (void)buf;
38561     (void)sz;
38562     (void)ctx;
38563     return WOLFSSL_CBIO_ERR_WANT_READ;
38564 }
38565 #endif
38566 
38567 static void test_wolfSSL_BIO_should_retry(void)
38568 {
38569 #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
38570     !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
38571     defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
38572     tcp_ready ready;
38573     func_args server_args;
38574     THREAD_TYPE serverThread;
38575     SOCKET_T sockfd = 0;
38576     WOLFSSL_CTX* ctx;
38577     WOLFSSL*     ssl;
38578     char msg[64] = "hello wolfssl!";
38579     char reply[1024];
38580     int  msgSz = (int)XSTRLEN(msg);
38581     int  ret;
38582     BIO* bio;
38583 
38584     printf(testingFmt, "wolfSSL_BIO_should_retry()");
38585 
38586     XMEMSET(&server_args, 0, sizeof(func_args));
38587 #ifdef WOLFSSL_TIRTOS
38588     fdOpenSession(Task_self());
38589 #endif
38590 
38591     StartTCP();
38592     InitTcpReady(&ready);
38593 
38594 #if defined(USE_WINDOWS_API)
38595     /* use RNG to get random port if using windows */
38596     ready.port = GetRandomPort();
38597 #endif
38598 
38599     server_args.signal = &ready;
38600     start_thread(test_server_nofail, &server_args, &serverThread);
38601     wait_tcp_ready(&server_args);
38602 
38603 
38604     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
38605 #ifdef OPENSSL_COMPATIBLE_DEFAULTS
38606     AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
38607 #endif
38608     AssertIntEQ(WOLFSSL_SUCCESS,
38609             wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
38610     AssertIntEQ(WOLFSSL_SUCCESS,
38611           wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
38612     AssertIntEQ(WOLFSSL_SUCCESS,
38613             wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
38614     tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
38615 
38616     /* force retry */
38617     ssl = wolfSSL_new(ctx);
38618     AssertNotNull(ssl);
38619     AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
38620     wolfSSL_SSLSetIORecv(ssl, forceWantRead);
38621 
38622     AssertNotNull(bio = BIO_new(BIO_f_ssl()));
38623     BIO_set_ssl(bio, ssl, BIO_CLOSE);
38624 
38625     AssertIntLE(BIO_write(bio, msg, msgSz), 0);
38626     AssertIntNE(BIO_should_retry(bio), 0);
38627 
38628 
38629     /* now perform successful connection */
38630     wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
38631     AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
38632     BIO_read(bio, reply, sizeof(reply));
38633     ret = wolfSSL_get_error(ssl, -1);
38634     if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
38635         AssertIntNE(BIO_should_retry(bio), 0);
38636     }
38637     else {
38638         AssertIntEQ(BIO_should_retry(bio), 0);
38639     }
38640     AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
38641                 XSTRLEN("I hear you fa shizzle!")), 0);
38642     BIO_free(bio);
38643     wolfSSL_CTX_free(ctx);
38644 
38645     join_thread(serverThread);
38646     FreeTcpReady(&ready);
38647 
38648 #ifdef WOLFSSL_TIRTOS
38649     fdOpenSession(Task_self());
38650 #endif
38651 
38652     printf(resultFmt, passed);
38653 #endif
38654 }
38655 
38656 static void test_wolfSSL_BIO_connect(void)
38657 {
38658 #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
38659     tcp_ready ready;
38660     func_args server_args;
38661     THREAD_TYPE serverThread;
38662     BIO *tcpBio;
38663     BIO *sslBio;
38664     SSL_CTX* ctx;
38665     SSL *ssl;
38666     SSL *sslPtr;
38667     char msg[] = "hello wolfssl!";
38668     char reply[30];
38669     char buff[10] = {0};
38670 
38671     printf(testingFmt, "wolfSSL_BIO_new_connect()");
38672 
38673     /* Setup server */
38674     XMEMSET(&server_args, 0, sizeof(func_args));
38675     StartTCP();
38676     InitTcpReady(&ready);
38677 #if defined(USE_WINDOWS_API)
38678     /* use RNG to get random port if using windows */
38679     ready.port = GetRandomPort();
38680 #endif
38681     server_args.signal = &ready;
38682     start_thread(test_server_nofail, &server_args, &serverThread);
38683     wait_tcp_ready(&server_args);
38684     AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
38685 
38686     /* Start the test proper */
38687     /* Setup the TCP BIO */
38688     AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
38689     AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
38690     /* Setup the SSL object */
38691     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
38692     AssertIntEQ(WOLFSSL_SUCCESS,
38693             wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
38694     AssertIntEQ(WOLFSSL_SUCCESS,
38695           wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
38696     AssertIntEQ(WOLFSSL_SUCCESS,
38697             wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
38698     AssertNotNull(ssl = SSL_new(ctx));
38699     SSL_set_connect_state(ssl);
38700     /* Setup the SSL BIO */
38701     AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
38702     AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
38703     /* Verify that BIO_get_ssl works. */
38704     AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
38705     AssertPtrEq(ssl, sslPtr);
38706     /* Link BIO's so that sslBio uses tcpBio for IO */
38707     AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
38708     /* Do TCP connect */
38709     AssertIntEQ(BIO_do_connect(sslBio), 1);
38710     /* Do TLS handshake */
38711     AssertIntEQ(BIO_do_handshake(sslBio), 1);
38712     /* Test writing */
38713     AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
38714     /* Expect length of default wolfSSL reply */
38715     AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
38716 
38717     /* Clean it all up */
38718     BIO_free_all(sslBio);
38719     /* Server clean up */
38720     join_thread(serverThread);
38721     FreeTcpReady(&ready);
38722 
38723     /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
38724      * after. */
38725     XMEMSET(&server_args, 0, sizeof(func_args));
38726     StartTCP();
38727     InitTcpReady(&ready);
38728 #if defined(USE_WINDOWS_API)
38729     /* use RNG to get random port if using windows */
38730     ready.port = GetRandomPort();
38731 #endif
38732     server_args.signal = &ready;
38733     start_thread(test_server_nofail, &server_args, &serverThread);
38734     wait_tcp_ready(&server_args);
38735     AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
38736 
38737     AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
38738     AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
38739     AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
38740     AssertIntEQ(BIO_do_connect(sslBio), 1);
38741     AssertIntEQ(BIO_do_handshake(sslBio), 1);
38742     AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
38743     AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
38744     /* Attempt to close the TLS connection gracefully. */
38745     BIO_ssl_shutdown(sslBio);
38746 
38747     BIO_free_all(sslBio);
38748     join_thread(serverThread);
38749     FreeTcpReady(&ready);
38750 
38751     SSL_CTX_free(ctx);
38752 
38753 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
38754     wc_ecc_fp_free();  /* free per thread cache */
38755 #endif
38756 
38757     printf(resultFmt, passed);
38758 #endif
38759 }
38760 
38761 #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
38762 static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
38763 {
38764     BIO* clientBio;
38765     SSL* sslClient;
38766     SSL_CTX* ctx;
38767     char connectAddr[20]; /* IP + port */;
38768 
38769     (void)args;
38770 
38771     AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
38772     AssertNotNull(clientBio = BIO_new_connect(connectAddr));
38773     AssertIntEQ(BIO_do_connect(clientBio), 1);
38774     AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
38775     AssertNotNull(sslClient = SSL_new(ctx));
38776     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
38777     SSL_set_bio(sslClient, clientBio, clientBio);
38778     AssertIntEQ(SSL_connect(sslClient), 1);
38779 
38780     SSL_free(sslClient);
38781     SSL_CTX_free(ctx);
38782 
38783 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
38784     wc_ecc_fp_free();  /* free per thread cache */
38785 #endif
38786 
38787     return 0;
38788 }
38789 #endif
38790 
38791 static void test_wolfSSL_BIO_accept(void)
38792 {
38793 #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
38794 
38795     BIO* serverBindBio;
38796     BIO* serverAcceptBio;
38797     SSL* sslServer;
38798     SSL_CTX* ctx;
38799     func_args args;
38800     THREAD_TYPE thread;
38801     char port[10]; /* 10 bytes should be enough to store the string
38802                     * representation of the port */
38803 
38804     printf(testingFmt, "wolfSSL_BIO_new_accept()");
38805 
38806     AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
38807     AssertNotNull(serverBindBio = BIO_new_accept(port));
38808 
38809     /* First BIO_do_accept binds the port */
38810     AssertIntEQ(BIO_do_accept(serverBindBio), 1);
38811 
38812     XMEMSET(&args, 0, sizeof(func_args));
38813     start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
38814 
38815     AssertIntEQ(BIO_do_accept(serverBindBio), 1);
38816     /* Let's plug it into SSL to test */
38817     AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
38818     AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
38819     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
38820     AssertNotNull(sslServer = SSL_new(ctx));
38821     AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
38822     SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
38823     AssertIntEQ(SSL_accept(sslServer), 1);
38824 
38825     join_thread(thread);
38826 
38827     BIO_free(serverBindBio);
38828     SSL_free(sslServer);
38829     SSL_CTX_free(ctx);
38830 
38831 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
38832     wc_ecc_fp_free();  /* free per thread cache */
38833 #endif
38834 
38835     printf(resultFmt, passed);
38836 #endif
38837 }
38838 
38839 static void test_wolfSSL_BIO_write(void)
38840 {
38841     #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
38842     BIO* bio;
38843     BIO* bio64;
38844     BIO* ptr;
38845     int  sz;
38846     char msg[] = "conversion test";
38847     char out[40];
38848     char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
38849     void* bufPtr = NULL;
38850     BUF_MEM* buf = NULL;
38851 
38852     printf(testingFmt, "wolfSSL_BIO_write()");
38853 
38854     AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
38855     AssertNotNull(bio   = BIO_push(bio64, BIO_new(BIO_s_mem())));
38856 
38857     /* now should convert to base64 then write to memory */
38858     AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
38859     BIO_flush(bio);
38860 
38861     /* test BIO chain */
38862     AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
38863     AssertNotNull(buf);
38864     AssertIntEQ(buf->length, 25);
38865     AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
38866     AssertPtrEq(buf->data, bufPtr);
38867 
38868     AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
38869     sz = sizeof(out);
38870     XMEMSET(out, 0, sz);
38871     AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
38872     AssertIntEQ(XMEMCMP(out, expected, sz), 0);
38873 
38874     /* write then read should return the same message */
38875     AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
38876     sz = sizeof(out);
38877     XMEMSET(out, 0, sz);
38878     AssertIntEQ(BIO_read(bio, out, sz), 16);
38879     AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
38880 
38881     /* now try encoding with no line ending */
38882     BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
38883     #ifdef HAVE_EX_DATA
38884     BIO_set_ex_data(bio64, 0, (void*) "data");
38885     AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
38886     #endif
38887     AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
38888     BIO_flush(bio);
38889     sz = sizeof(out);
38890     XMEMSET(out, 0, sz);
38891     AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
38892     AssertIntEQ(XMEMCMP(out, expected, sz), 0);
38893 
38894     BIO_free_all(bio); /* frees bio64 also */
38895 
38896     /* test with more than one bio64 in list */
38897     AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
38898     AssertNotNull(bio   = BIO_push(BIO_new(BIO_f_base64()), bio64));
38899     AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
38900 
38901     /* now should convert to base64 when stored and then decode with read */
38902     AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
38903     BIO_flush(bio);
38904     sz = sizeof(out);
38905     XMEMSET(out, 0, sz);
38906     AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
38907     AssertIntEQ(XMEMCMP(out, msg, sz), 0);
38908     BIO_clear_flags(bio64, ~0);
38909     BIO_set_retry_read(bio);
38910     BIO_free_all(bio); /* frees bio64s also */
38911 
38912     printf(resultFmt, passed);
38913     #endif
38914 }
38915 
38916 
38917 static void test_wolfSSL_BIO_printf(void)
38918 {
38919     #if defined(OPENSSL_ALL)
38920     BIO* bio;
38921     int  sz = 7;
38922     char msg[] = "TLS 1.3 for the world";
38923     char out[60];
38924     char expected[] = "TLS 1.3 for the world : sz = 7";
38925 
38926     printf(testingFmt, "wolfSSL_BIO_printf()");
38927 
38928     XMEMSET(out, 0, sizeof(out));
38929     AssertNotNull(bio = BIO_new(BIO_s_mem()));
38930     AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
38931     AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
38932     AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
38933     AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
38934     BIO_free(bio);
38935 
38936     printf(resultFmt, passed);
38937     #endif
38938 }
38939 
38940 static void test_wolfSSL_BIO_f_md(void)
38941 {
38942     #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
38943     BIO *bio, *mem;
38944     char msg[] = "message to hash";
38945     char out[60];
38946     EVP_MD_CTX* ctx;
38947     const unsigned char testKey[] =
38948     {
38949         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
38950         0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
38951         0x0b, 0x0b, 0x0b, 0x0b
38952     };
38953     const char testData[] = "Hi There";
38954     const unsigned char testResult[] =
38955     {
38956         0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
38957         0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
38958         0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
38959         0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
38960     };
38961     const unsigned char expectedHash[] =
38962     {
38963        0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
38964        0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
38965        0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
38966        0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
38967     };
38968     const unsigned char emptyHash[] =
38969     {
38970         0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
38971         0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
38972         0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
38973         0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
38974     };
38975     unsigned char check[sizeof(testResult) + 1];
38976     size_t checkSz = -1;
38977     EVP_PKEY* key;
38978 
38979     printf(testingFmt, "wolfSSL_BIO_f_md()");
38980 
38981     XMEMSET(out, 0, sizeof(out));
38982     AssertNotNull(bio = BIO_new(BIO_f_md()));
38983     AssertNotNull(mem = BIO_new(BIO_s_mem()));
38984 
38985     AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
38986     AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
38987 
38988     /* should not be able to write/read yet since just digest wrapper and no
38989      * data is passing through the bio */
38990     AssertIntEQ(BIO_write(bio, msg, 0), 0);
38991     AssertIntEQ(BIO_pending(bio), 0);
38992     AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
38993     AssertIntEQ(BIO_gets(bio, out, 3), 0);
38994     AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
38995     AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
38996     BIO_reset(bio);
38997 
38998     /* append BIO mem to bio in order to read/write */
38999     AssertNotNull(bio = BIO_push(bio, mem));
39000 
39001     XMEMSET(out, 0, sizeof(out));
39002     AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
39003     AssertIntEQ(BIO_pending(bio), 16);
39004 
39005     /* this just reads the message and does not hash it (gets calls final) */
39006     AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
39007     AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
39008 
39009     /* create a message digest using BIO */
39010     XMEMSET(out, 0, sizeof(out));
39011     AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
39012     AssertIntEQ(BIO_pending(mem), 16);
39013     AssertIntEQ(BIO_pending(bio), 16);
39014     AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
39015     AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
39016     BIO_free(bio);
39017     BIO_free(mem);
39018 
39019     /* test with HMAC */
39020     XMEMSET(out, 0, sizeof(out));
39021     AssertNotNull(bio = BIO_new(BIO_f_md()));
39022     AssertNotNull(mem = BIO_new(BIO_s_mem()));
39023     BIO_get_md_ctx(bio, &ctx);
39024     AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
39025                                              testKey, (int)sizeof(testKey)));
39026     EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
39027     AssertNotNull(bio = BIO_push(bio, mem));
39028     BIO_write(bio, testData, (int)strlen(testData));
39029     EVP_DigestSignFinal(ctx, NULL, &checkSz);
39030     EVP_DigestSignFinal(ctx, check, &checkSz);
39031 
39032     AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
39033 
39034     EVP_PKEY_free(key);
39035     BIO_free(bio);
39036     BIO_free(mem);
39037 
39038     printf(resultFmt, passed);
39039     #endif
39040 }
39041 
39042 static void test_wolfSSL_BIO_up_ref(void)
39043 {
39044 #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
39045     BIO* bio;
39046     printf(testingFmt, "wolfSSL_BIO_up_ref()");
39047 
39048     AssertNotNull(bio = BIO_new(BIO_f_md()));
39049     AssertIntEQ(BIO_up_ref(NULL), 0);
39050     AssertIntEQ(BIO_up_ref(bio), 1);
39051     BIO_free(bio);
39052     AssertIntEQ(BIO_up_ref(bio), 1);
39053     BIO_free(bio);
39054     BIO_free(bio);
39055 
39056     printf(resultFmt, "passed");
39057 #endif
39058 }
39059 
39060 #endif /* !NO_BIO */
39061 
39062 #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
39063 
39064 /* test that the callback arg is correct */
39065 static int certCbArg = 0;
39066 
39067 static int clientCertCb(WOLFSSL* ssl, void* arg)
39068 {
39069     if (ssl == NULL || arg != &certCbArg)
39070         return 0;
39071     if (wolfSSL_use_certificate_file(ssl, cliCertFile,
39072             WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
39073         return 0;
39074     if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
39075             WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
39076         return 0;
39077     return 1;
39078 }
39079 
39080 static void clientCertSetupCb(WOLFSSL_CTX* ctx)
39081 {
39082     SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
39083 }
39084 
39085 /**
39086  * This is only done because test_client_nofail has no way to stop
39087  * certificate and key loading
39088  */
39089 static void clientCertClearCb(WOLFSSL* ssl)
39090 {
39091     /* Clear the loaded certs to force the callbacks to set them up */
39092     SSL_certs_clear(ssl);
39093 }
39094 
39095 static int serverCertCb(WOLFSSL* ssl, void* arg)
39096 {
39097     if (ssl == NULL || arg != &certCbArg)
39098         return 0;
39099     if (wolfSSL_use_certificate_file(ssl, svrCertFile,
39100             WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
39101         return 0;
39102     if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
39103             WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
39104         return 0;
39105     return 1;
39106 }
39107 
39108 static void serverCertSetupCb(WOLFSSL_CTX* ctx)
39109 {
39110     SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
39111 }
39112 
39113 /**
39114  * This is only done because test_server_nofail has no way to stop
39115  * certificate and key loading
39116  */
39117 static void serverCertClearCb(WOLFSSL* ssl)
39118 {
39119     /* Clear the loaded certs to force the callbacks to set them up */
39120     SSL_certs_clear(ssl);
39121 }
39122 
39123 #endif
39124 
39125 static void test_wolfSSL_cert_cb(void)
39126 {
39127 #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
39128 
39129     callback_functions func_cb_client;
39130     callback_functions func_cb_server;
39131     tcp_ready ready;
39132     func_args client_args;
39133     func_args server_args;
39134     THREAD_TYPE serverThread;
39135 
39136     XMEMSET(&client_args, 0, sizeof(func_args));
39137     XMEMSET(&server_args, 0, sizeof(func_args));
39138     XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
39139     XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
39140 
39141 #ifdef WOLFSSL_TIRTOS
39142     fdOpenSession(Task_self());
39143 #endif
39144 
39145     StartTCP();
39146     InitTcpReady(&ready);
39147 
39148 #if defined(USE_WINDOWS_API)
39149     /* use RNG to get random port if using windows */
39150     ready.port = GetRandomPort();
39151 #endif
39152 
39153     server_args.signal = &ready;
39154     client_args.signal = &ready;
39155     client_args.callbacks = &func_cb_client;
39156     server_args.callbacks = &func_cb_server;
39157     func_cb_client.ctx_ready = clientCertSetupCb;
39158     func_cb_client.ssl_ready = clientCertClearCb;
39159     func_cb_server.ctx_ready = serverCertSetupCb;
39160     func_cb_server.ssl_ready = serverCertClearCb;
39161 
39162     start_thread(test_server_nofail, &server_args, &serverThread);
39163     wait_tcp_ready(&server_args);
39164     test_client_nofail(&client_args, NULL);
39165     join_thread(serverThread);
39166 
39167     AssertTrue(client_args.return_code);
39168     AssertTrue(server_args.return_code);
39169 
39170     FreeTcpReady(&ready);
39171 
39172 #ifdef WOLFSSL_TIRTOS
39173     fdOpenSession(Task_self());
39174 #endif
39175 
39176 #endif
39177 }
39178 
39179 static void test_wolfSSL_SESSION(void)
39180 {
39181 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
39182     !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
39183     !defined(NO_SESSION_CACHE)
39184 
39185     WOLFSSL*     ssl;
39186     WOLFSSL_CTX* ctx;
39187     WOLFSSL_SESSION* sess;
39188     WOLFSSL_SESSION* sess_copy;
39189 #ifdef OPENSSL_EXTRA
39190     unsigned char* sessDer = NULL;
39191     unsigned char* ptr     = NULL;
39192     const unsigned char context[] = "user app context";
39193     unsigned int contextSz = (unsigned int)sizeof(context);
39194     int sz;
39195 #endif
39196     int ret, err, sockfd;
39197     tcp_ready ready;
39198     func_args server_args;
39199     THREAD_TYPE serverThread;
39200     char msg[80];
39201     const char* sendGET = "GET";
39202 
39203     printf(testingFmt, "wolfSSL_SESSION()");
39204     /* TLS v1.3 requires session tickets */
39205     /* CHACHA and POLY1305 required for myTicketEncCb */
39206 #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
39207     !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
39208             defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
39209     AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
39210 #else
39211     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
39212 #endif
39213 
39214     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
39215         WOLFSSL_FILETYPE_PEM));
39216     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
39217         WOLFSSL_FILETYPE_PEM));
39218     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
39219         WOLFSSL_SUCCESS);
39220 #ifdef WOLFSSL_ENCRYPTED_KEYS
39221     wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
39222 #endif
39223 #ifdef HAVE_SESSION_TICKET
39224     /* Use session tickets, for ticket tests below */
39225     AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
39226 #endif
39227 
39228     XMEMSET(&server_args, 0, sizeof(func_args));
39229 #ifdef WOLFSSL_TIRTOS
39230     fdOpenSession(Task_self());
39231 #endif
39232 
39233     StartTCP();
39234     InitTcpReady(&ready);
39235 
39236 #if defined(USE_WINDOWS_API)
39237     /* use RNG to get random port if using windows */
39238     ready.port = GetRandomPort();
39239 #endif
39240 
39241     server_args.signal = &ready;
39242     start_thread(test_server_nofail, &server_args, &serverThread);
39243     wait_tcp_ready(&server_args);
39244 
39245     /* client connection */
39246     ssl = wolfSSL_new(ctx);
39247     tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
39248     AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
39249 
39250     #ifdef WOLFSSL_ASYNC_CRYPT
39251     err = 0; /* Reset error */
39252     #endif
39253     do {
39254     #ifdef WOLFSSL_ASYNC_CRYPT
39255         if (err == WC_PENDING_E) {
39256             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
39257             if (ret < 0) { break; } else if (ret == 0) { continue; }
39258         }
39259     #endif
39260         ret = wolfSSL_connect(ssl);
39261         err = wolfSSL_get_error(ssl, 0);
39262     } while (err == WC_PENDING_E);
39263     AssertIntEQ(ret, WOLFSSL_SUCCESS);
39264 
39265     #ifdef WOLFSSL_ASYNC_CRYPT
39266     err = 0; /* Reset error */
39267     #endif
39268     do {
39269     #ifdef WOLFSSL_ASYNC_CRYPT
39270         if (err == WC_PENDING_E) {
39271             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
39272             if (ret < 0) { break; } else if (ret == 0) { continue; }
39273         }
39274     #endif
39275         ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
39276         err = wolfSSL_get_error(ssl, 0);
39277     } while (err == WC_PENDING_E);
39278     AssertIntEQ(ret, (int)XSTRLEN(sendGET));
39279 
39280     #ifdef WOLFSSL_ASYNC_CRYPT
39281     err = 0; /* Reset error */
39282     #endif
39283     do {
39284     #ifdef WOLFSSL_ASYNC_CRYPT
39285         if (err == WC_PENDING_E) {
39286             ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
39287             if (ret < 0) { break; } else if (ret == 0) { continue; }
39288         }
39289     #endif
39290         ret = wolfSSL_read(ssl, msg, sizeof(msg));
39291         err = wolfSSL_get_error(ssl, 0);
39292     } while (err == WC_PENDING_E);
39293     AssertIntEQ(ret, 23);
39294 
39295     AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
39296     AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
39297     AssertPtrEq(sess, sess_copy); /* they should be the same pointer */
39298     wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
39299     wolfSSL_SESSION_free(sess);      sess = NULL; /* free session ref */
39300 
39301     sess = wolfSSL_get_session(ssl);
39302 
39303 #ifdef OPENSSL_EXTRA
39304     AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
39305     AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
39306 
39307     AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
39308     AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
39309     #ifdef HAVE_SESSION_TICKET
39310     AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
39311     AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
39312                 SESSION_TICKET_HINT_DEFAULT);
39313     #else
39314     AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
39315     #endif
39316 #endif /* OPENSSL_EXTRA */
39317 
39318     wolfSSL_shutdown(ssl);
39319     wolfSSL_free(ssl);
39320 
39321     join_thread(serverThread);
39322 
39323     FreeTcpReady(&ready);
39324 
39325 #ifdef WOLFSSL_TIRTOS
39326     fdOpenSession(Task_self());
39327 #endif
39328 
39329 #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
39330     {
39331         X509 *x509;
39332         char buf[30];
39333         int  bufSz;
39334 
39335         AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
39336         AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
39337                     X509_get_subject_name(x509), NID_organizationalUnitName,
39338                     buf, sizeof(buf))), 0);
39339         AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
39340         if (bufSz == 7) {
39341             AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
39342         }
39343         if (bufSz == 16) {
39344             AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
39345         }
39346     }
39347 #endif
39348 
39349 #ifdef HAVE_EXT_CACHE
39350     AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
39351     wolfSSL_SESSION_free(sess_copy);
39352     sess_copy = NULL;
39353 #endif
39354 
39355 #ifdef OPENSSL_EXTRA
39356     /* get session from DER and update the timeout */
39357     AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
39358     AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
39359     wolfSSL_SESSION_free(sess);
39360     sess = NULL;
39361     ptr = sessDer;
39362     AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
39363     AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
39364                 (const unsigned char**)&ptr, sz));
39365     XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
39366     sessDer = NULL;
39367 
39368     AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
39369     AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
39370 #endif
39371 
39372     /* successful set session test */
39373     AssertNotNull(ssl = wolfSSL_new(ctx));
39374     AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
39375 
39376 #ifdef HAVE_SESSION_TICKET
39377     /* Test set/get session ticket */
39378     {
39379         const char* ticket = "This is a session ticket";
39380         char buf[64] = {0};
39381         word32 bufSz = (word32)sizeof(buf);
39382 
39383         AssertIntEQ(SSL_SUCCESS,
39384             wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
39385                 (word32)XSTRLEN(ticket)));
39386         AssertIntEQ(SSL_SUCCESS,
39387             wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
39388         AssertStrEQ(ticket, buf);
39389     }
39390 #endif
39391 
39392 #ifdef OPENSSL_EXTRA
39393     /* session timeout case */
39394     /* make the session to be expired */
39395     AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
39396     XSLEEP_MS(1200);
39397 
39398     /* SSL_set_session should reject specified session but return success
39399      * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
39400      */
39401 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
39402     AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
39403 #else
39404     AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
39405 #endif
39406     AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
39407 
39408     /* fail case with miss match session context IDs (use compatibility API) */
39409     AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
39410             SSL_SUCCESS);
39411     AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
39412     wolfSSL_free(ssl);
39413 
39414     AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
39415             SSL_FAILURE);
39416     AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
39417             SSL_SUCCESS);
39418     AssertNotNull(ssl = wolfSSL_new(ctx));
39419     AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
39420 #endif /* OPENSSL_EXTRA */
39421 
39422     wolfSSL_free(ssl);
39423     wolfSSL_SESSION_free(sess);
39424     wolfSSL_CTX_free(ctx);
39425     printf(resultFmt, passed);
39426 #endif
39427 }
39428 
39429 static void test_wolfSSL_ticket_keys(void)
39430 {
39431 #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
39432     !defined(NO_WOLFSSL_SERVER)
39433     WOLFSSL_CTX* ctx;
39434     byte keys[WOLFSSL_TICKET_KEYS_SZ];
39435 
39436     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
39437 
39438     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
39439                 WOLFSSL_FAILURE);
39440     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
39441                 WOLFSSL_FAILURE);
39442     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
39443                 WOLFSSL_FAILURE);
39444     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
39445                 WOLFSSL_FAILURE);
39446     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
39447                 WOLFSSL_FAILURE);
39448     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
39449                 WOLFSSL_FAILURE);
39450     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
39451                 WOLFSSL_FAILURE);
39452 
39453     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
39454                 WOLFSSL_FAILURE);
39455     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
39456                 WOLFSSL_FAILURE);
39457     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
39458                 WOLFSSL_FAILURE);
39459     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
39460                 WOLFSSL_FAILURE);
39461     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
39462                 WOLFSSL_FAILURE);
39463     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
39464                 WOLFSSL_FAILURE);
39465     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
39466                 WOLFSSL_FAILURE);
39467 
39468     AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
39469                 WOLFSSL_SUCCESS);
39470     AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
39471                 WOLFSSL_SUCCESS);
39472 
39473     wolfSSL_CTX_free(ctx);
39474 #endif
39475 }
39476 
39477 #ifndef NO_BIO
39478 
39479 static void test_wolfSSL_d2i_PUBKEY(void)
39480 {
39481     #if defined(OPENSSL_EXTRA)
39482     BIO*  bio;
39483     EVP_PKEY* pkey;
39484 
39485     printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
39486 
39487     AssertNotNull(bio = BIO_new(BIO_s_mem()));
39488     AssertNull(d2i_PUBKEY_bio(NULL, NULL));
39489 
39490 #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
39491     /* RSA PUBKEY test */
39492     AssertIntGT(BIO_write(bio, client_keypub_der_2048,
39493                 sizeof_client_keypub_der_2048), 0);
39494     AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
39495     EVP_PKEY_free(pkey);
39496 #endif
39497 
39498 #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
39499     /* ECC PUBKEY test */
39500     AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
39501                 sizeof_ecc_clikeypub_der_256), 0);
39502     AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
39503     EVP_PKEY_free(pkey);
39504 #endif
39505 
39506 #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
39507     /* DSA PUBKEY test */
39508     AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
39509                 sizeof_dsa_pub_key_der_2048), 0);
39510     AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
39511     EVP_PKEY_free(pkey);
39512 #endif
39513 
39514 #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
39515 defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
39516 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
39517             (HAVE_FIPS_VERSION > 2))
39518     /* DH PUBKEY test */
39519     AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
39520                 sizeof_dh_pub_key_der_2048), 0);
39521     AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
39522     EVP_PKEY_free(pkey);
39523 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
39524 #endif /*  USE_CERT_BUFFERS_2048 && !NO_DH &&  && OPENSSL_EXTRA */
39525 
39526     BIO_free(bio);
39527 
39528     (void)pkey;
39529     printf(resultFmt, passed);
39530     #endif
39531 }
39532 
39533 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
39534 static void test_wolfSSL_d2i_PrivateKeys_bio(void)
39535 {
39536     BIO*      bio = NULL;
39537     EVP_PKEY* pkey  = NULL;
39538 #ifndef NO_RSA
39539 #endif
39540     WOLFSSL_CTX* ctx;
39541 
39542 #if defined(WOLFSSL_KEY_GEN)
39543     unsigned char buff[4096];
39544     unsigned char* bufPtr = buff;
39545 #endif
39546 
39547     printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
39548 
39549     /* test creating new EVP_PKEY with bad arg */
39550     AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
39551 
39552     /* test loading RSA key using BIO */
39553 #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
39554     {
39555         XFILE file;
39556         const char* fname = "./certs/server-key.der";
39557         size_t sz;
39558         byte* buf;
39559 
39560         file = XFOPEN(fname, "rb");
39561         AssertTrue((file != XBADFILE));
39562         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
39563         sz = XFTELL(file);
39564         XREWIND(file);
39565         AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
39566         AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
39567         XFCLOSE(file);
39568 
39569         /* Test using BIO new mem and loading DER private key */
39570         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
39571         AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
39572         XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
39573         BIO_free(bio);
39574         bio = NULL;
39575         EVP_PKEY_free(pkey);
39576         pkey  = NULL;
39577     }
39578 #endif
39579 
39580     /* test loading ECC key using BIO */
39581 #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
39582     {
39583         XFILE file;
39584         const char* fname = "./certs/ecc-key.der";
39585         size_t sz;
39586         byte* buf;
39587 
39588         file = XFOPEN(fname, "rb");
39589         AssertTrue((file != XBADFILE));
39590         AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
39591         sz = XFTELL(file);
39592         XREWIND(file);
39593         AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
39594         AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
39595         XFCLOSE(file);
39596 
39597         /* Test using BIO new mem and loading DER private key */
39598         AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
39599         AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
39600         XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
39601         BIO_free(bio);
39602         bio = NULL;
39603         EVP_PKEY_free(pkey);
39604         pkey = NULL;
39605     }
39606 #endif
39607 
39608     AssertNotNull(bio = BIO_new(BIO_s_mem()));
39609 #ifndef NO_WOLFSSL_SERVER
39610     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
39611 #else
39612     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
39613 #endif
39614 
39615 #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
39616     !defined(NO_RSA) && !defined(HAVE_USER_RSA)
39617     {
39618         RSA* rsa = NULL;
39619         /* Tests bad parameters */
39620         AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
39621 
39622         /* RSA not set yet, expecting to fail*/
39623         AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
39624 
39625 #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
39626         /* set RSA using bio*/
39627         AssertIntGT(BIO_write(bio, client_key_der_2048,
39628                     sizeof_client_key_der_2048), 0);
39629         AssertNotNull(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
39630 
39631         AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
39632 
39633         /*i2d RSAprivate key tests */
39634         AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
39635         AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
39636         AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
39637                                                sizeof_client_key_der_2048);
39638         bufPtr = NULL;
39639         AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
39640                                                sizeof_client_key_der_2048);
39641         AssertNotNull(bufPtr);
39642         XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
39643 #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
39644         RSA_free(rsa);
39645     }
39646 #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
39647     SSL_CTX_free(ctx);
39648     ctx = NULL;
39649     BIO_free(bio);
39650     bio = NULL;
39651     printf(resultFmt, passed);
39652 }
39653 #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
39654 
39655 #endif /* !NO_BIO */
39656 
39657 
39658 static void test_wolfSSL_sk_GENERAL_NAME(void)
39659 {
39660 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
39661     !defined(NO_RSA)
39662     X509* x509;
39663     GENERAL_NAME* gn;
39664     unsigned char buf[4096];
39665     const unsigned char* bufPt;
39666     int bytes, i;
39667     XFILE f;
39668     STACK_OF(GENERAL_NAME)* sk;
39669 
39670     printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
39671 
39672     f = XFOPEN(cliCertDerFileExt, "rb");
39673     AssertTrue((f != XBADFILE));
39674     AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
39675     XFCLOSE(f);
39676 
39677     bufPt = buf;
39678     AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
39679 
39680     AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
39681                 NID_subject_alt_name, NULL, NULL));
39682 
39683     AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
39684     for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
39685         AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
39686 
39687         switch (gn->type) {
39688         case GEN_DNS:
39689             printf("found type GEN_DNS\n");
39690             break;
39691         case GEN_EMAIL:
39692             printf("found type GEN_EMAIL\n");
39693             break;
39694         case GEN_URI:
39695             printf("found type GEN_URI\n");
39696             break;
39697         }
39698     }
39699     X509_free(x509);
39700     sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
39701 
39702     printf(resultFmt, passed);
39703 #endif
39704 }
39705 
39706 static void test_wolfSSL_GENERAL_NAME_print(void)
39707 {
39708 #if defined(OPENSSL_ALL) && !defined(NO_BIO)
39709 
39710     X509* x509;
39711     GENERAL_NAME* gn;
39712     unsigned char buf[4096];
39713     const unsigned char* bufPt;
39714     int bytes;
39715     XFILE f;
39716     STACK_OF(GENERAL_NAME)* sk;
39717     BIO* out;
39718     unsigned char outbuf[128];
39719 
39720     X509_EXTENSION* ext;
39721     AUTHORITY_INFO_ACCESS* aia;
39722     ACCESS_DESCRIPTION* ad;
39723 
39724     const unsigned char v4Addr[] = {192,168,53,1};
39725     const unsigned char v6Addr[] =
39726                             {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
39727                              0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
39728     const unsigned char email[]  =
39729                              {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
39730                               'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
39731 
39732     const char* dnsStr   = "DNS:example.com";
39733     const char* uriStr   = "URI:http://127.0.0.1:22220";
39734     const char* v4addStr = "IP Address:192.168.53.1";
39735     const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
39736     const char* emailStr = "email:info@wolfssl.com";
39737     const char* othrStr  = "othername:<unsupported>";
39738     const char* x400Str  = "X400Name:<unsupported>";
39739     const char* ediStr   = "EdiPartyName:<unsupported>";
39740 
39741 
39742     printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
39743 
39744     /* BIO to output */
39745     AssertNotNull(out = BIO_new(BIO_s_mem()));
39746 
39747     /* test for NULL param */
39748     gn = NULL;
39749 
39750     AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
39751     AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
39752     AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
39753 
39754 
39755     /* test for GEN_DNS */
39756     f = XFOPEN(cliCertDerFileExt, "rb");
39757     AssertTrue((f != XBADFILE));
39758     AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
39759     XFCLOSE(f);
39760 
39761     bufPt = buf;
39762     AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
39763     AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
39764                 NID_subject_alt_name, NULL, NULL));
39765 
39766     AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
39767     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39768 
39769     XMEMSET(outbuf,0,sizeof(outbuf));
39770     BIO_read(out, outbuf, sizeof(outbuf));
39771     AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
39772 
39773     sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
39774     X509_free(x509);
39775 
39776     /* test for GEN_URI */
39777 
39778     f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
39779     AssertTrue((f != XBADFILE));
39780     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
39781     XFCLOSE(f);
39782 
39783     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
39784     aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
39785     AssertNotNull(aia);
39786     ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
39787 
39788     gn = ad->location;
39789     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39790 
39791     XMEMSET(outbuf,0,sizeof(outbuf));
39792     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39793     AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
39794 
39795     wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
39796     aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
39797     AssertNotNull(aia);
39798     AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
39799     X509_free(x509);
39800 
39801     /* test for GEN_IPADD */
39802 
39803     /* ip v4 address */
39804     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39805     gn->type = GEN_IPADD;
39806     gn->d.iPAddress->length = sizeof(v4Addr);
39807     AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
39808                                                         sizeof(v4Addr)), 1);
39809 
39810     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39811     XMEMSET(outbuf,0,sizeof(outbuf));
39812     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39813     AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
39814 
39815     GENERAL_NAME_free(gn);
39816 
39817     /* ip v6 address */
39818 
39819     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39820     gn->type = GEN_IPADD;
39821     gn->d.iPAddress->length = sizeof(v6Addr);
39822     AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
39823                                                         sizeof(v6Addr)), 1);
39824 
39825     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39826     XMEMSET(outbuf,0,sizeof(outbuf));
39827     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39828     AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
39829 
39830     GENERAL_NAME_free(gn);
39831 
39832     /* test for GEN_EMAIL */
39833 
39834     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39835     gn->type = GEN_EMAIL;
39836     gn->d.rfc822Name->length = sizeof(email);
39837     AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
39838                                                         sizeof(email)), 1);
39839 
39840     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39841     XMEMSET(outbuf,0,sizeof(outbuf));
39842     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39843     AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
39844 
39845     GENERAL_NAME_free(gn);
39846 
39847     /* test for  GEN_OTHERNAME */
39848 
39849     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39850     gn->type = GEN_OTHERNAME;
39851 
39852     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39853     XMEMSET(outbuf,0,sizeof(outbuf));
39854     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39855     AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
39856 
39857     GENERAL_NAME_free(gn);
39858 
39859     /* test for  GEN_X400 */
39860 
39861     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39862     gn->type = GEN_X400;
39863 
39864     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39865     XMEMSET(outbuf,0,sizeof(outbuf));
39866     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39867     AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
39868 
39869     GENERAL_NAME_free(gn);
39870 
39871     /* test for GEN_EDIPARTY */
39872 
39873     AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
39874     gn->type = GEN_EDIPARTY;
39875 
39876     AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
39877     XMEMSET(outbuf,0,sizeof(outbuf));
39878     AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
39879     AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
39880 
39881     GENERAL_NAME_free(gn);
39882 
39883     BIO_free(out);
39884 
39885     printf(resultFmt, passed);
39886 #endif /* OPENSSL_ALL */
39887 }
39888 
39889 static void test_wolfSSL_sk_DIST_POINT(void)
39890 {
39891 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
39892     !defined(NO_RSA)
39893     X509* x509;
39894     unsigned char buf[4096];
39895     const unsigned char* bufPt;
39896     int bytes, i, j;
39897     XFILE f;
39898     DIST_POINT* dp;
39899     GENERAL_NAME* gn;
39900     ASN1_IA5STRING* uri;
39901     STACK_OF(DIST_POINT)* dps;
39902     STACK_OF(GENERAL_NAME)* gns;
39903     const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
39904 
39905     printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
39906 
39907     f = XFOPEN(cliCertDerCrlDistPoint, "rb");
39908     AssertTrue((f != XBADFILE));
39909     AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
39910     XFCLOSE(f);
39911 
39912     bufPt = buf;
39913     AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
39914 
39915     AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
39916                 NID_crl_distribution_points, NULL, NULL));
39917 
39918     AssertIntEQ(sk_DIST_POINT_num(dps), 1);
39919     for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
39920         AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
39921 
39922         gns = dp->distpoint->name.fullname;
39923         AssertNotNull(gns);
39924         AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
39925 
39926         for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
39927             gn = sk_GENERAL_NAME_value(gns, j);
39928             AssertIntEQ(gn->type, GEN_URI);
39929             AssertNotNull(uri = gn->d.uniformResourceIdentifier);
39930             AssertNotNull(uri->data);
39931             AssertIntGT(uri->length, 0);
39932         }
39933     }
39934 
39935     X509_free(x509);
39936     CRL_DIST_POINTS_free(dps);
39937 
39938     printf(resultFmt, passed);
39939 #endif
39940 }
39941 
39942 static void test_wolfSSL_MD4(void)
39943 {
39944 #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
39945     MD4_CTX md4;
39946     unsigned char out[16]; /* MD4_DIGEST_SIZE */
39947     const char* msg  = "12345678901234567890123456789012345678901234567890123456"
39948                        "789012345678901234567890";
39949     const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
39950                        "\xcc\x05\x36";
39951     int msgSz        = (int)XSTRLEN(msg);
39952 
39953     printf(testingFmt, "wolfSSL_MD4()");
39954 
39955     XMEMSET(out, 0, sizeof(out));
39956     MD4_Init(&md4);
39957     MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
39958     MD4_Final(out, &md4);
39959     AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
39960 
39961     printf(resultFmt, passed);
39962 #endif
39963 }
39964 
39965 
39966 static void test_wolfSSL_RSA(void)
39967 {
39968 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
39969     defined(WOLFSSL_KEY_GEN)
39970     RSA* rsa;
39971     const BIGNUM *n;
39972     const BIGNUM *e;
39973     const BIGNUM *d;
39974     const BIGNUM *p;
39975     const BIGNUM *q;
39976     const BIGNUM *dmp1;
39977     const BIGNUM *dmq1;
39978     const BIGNUM *iqmp;
39979 
39980     printf(testingFmt, "wolfSSL_RSA()");
39981 
39982     AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
39983     AssertIntEQ(RSA_size(rsa), 256);
39984 
39985 #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(HAVE_FAST_RSA) && \
39986     (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
39987     (HAVE_FIPS_VERSION >= 2))) && !defined(HAVE_SELFTEST) && \
39988     !defined(HAVE_INTEL_QA) && !defined(WOLFSSL_NO_RSA_KEY_CHECK)
39989     AssertIntEQ(RSA_check_key(rsa), WOLFSSL_SUCCESS);
39990 #endif
39991 
39992     /* sanity check */
39993     AssertIntEQ(RSA_bits(NULL), 0);
39994 
39995     /* key */
39996     AssertIntEQ(RSA_bits(rsa), 2048);
39997     RSA_get0_key(rsa, &n, &e, &d);
39998     AssertPtrEq(rsa->n, n);
39999     AssertPtrEq(rsa->e, e);
40000     AssertPtrEq(rsa->d, d);
40001     AssertNotNull(n = BN_new());
40002     AssertNotNull(e = BN_new());
40003     AssertNotNull(d = BN_new());
40004     AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
40005     AssertPtrEq(rsa->n, n);
40006     AssertPtrEq(rsa->e, e);
40007     AssertPtrEq(rsa->d, d);
40008 
40009     /* crt_params */
40010     RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
40011     AssertPtrEq(rsa->dmp1, dmp1);
40012     AssertPtrEq(rsa->dmq1, dmq1);
40013     AssertPtrEq(rsa->iqmp, iqmp);
40014     AssertNotNull(dmp1 = BN_new());
40015     AssertNotNull(dmq1 = BN_new());
40016     AssertNotNull(iqmp = BN_new());
40017     AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1, (BIGNUM*)iqmp), 1);
40018     AssertPtrEq(rsa->dmp1, dmp1);
40019     AssertPtrEq(rsa->dmq1, dmq1);
40020     AssertPtrEq(rsa->iqmp, iqmp);
40021 
40022     /* factors */
40023     RSA_get0_factors(rsa, &p, &q);
40024     AssertPtrEq(rsa->p, p);
40025     AssertPtrEq(rsa->q, q);
40026     AssertNotNull(p = BN_new());
40027     AssertNotNull(q = BN_new());
40028     AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
40029     AssertPtrEq(rsa->p, p);
40030     AssertPtrEq(rsa->q, q);
40031 
40032     AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
40033     AssertIntEQ(RSA_bits(rsa), 21);
40034     RSA_free(rsa);
40035 
40036 #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
40037     AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
40038     AssertIntEQ(RSA_size(rsa), 384);
40039     AssertIntEQ(RSA_bits(rsa), 3072);
40040     RSA_free(rsa);
40041 #endif
40042 
40043     /* remove for now with odd key size until adjusting rsa key size check with
40044        wc_MakeRsaKey()
40045     AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
40046     RSA_free(rsa);
40047     */
40048 
40049     AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
40050     AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
40051     AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
40052     AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
40053 
40054 
40055 #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
40056     {
40057         byte buff[FOURK_BUF];
40058         byte der[FOURK_BUF];
40059         const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
40060 
40061         XFILE f;
40062         int bytes;
40063 
40064         /* test loading encrypted RSA private pem w/o password */
40065         f = XFOPEN(PrivKeyPemFile, "rb");
40066         AssertTrue((f != XBADFILE));
40067         bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
40068         XFCLOSE(f);
40069         XMEMSET(der, 0, sizeof(der));
40070         /* test that error value is returned with no password */
40071         AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
40072     }
40073 #endif
40074 
40075     printf(resultFmt, passed);
40076 #endif
40077 }
40078 
40079 static void test_wolfSSL_RSA_DER(void)
40080 {
40081 #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
40082     !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
40083 
40084     RSA *rsa;
40085     int i;
40086     unsigned char *buff = NULL;
40087 
40088     struct tbl_s
40089     {
40090         const unsigned char *der;
40091         int sz;
40092     } tbl[] = {
40093 
40094 #ifdef USE_CERT_BUFFERS_1024
40095         {client_key_der_1024, sizeof_client_key_der_1024},
40096         {server_key_der_1024, sizeof_server_key_der_1024},
40097 #endif
40098 #ifdef USE_CERT_BUFFERS_2048
40099         {client_key_der_2048, sizeof_client_key_der_2048},
40100         {server_key_der_2048, sizeof_server_key_der_2048},
40101 #endif
40102         {NULL, 0}
40103     };
40104 
40105     /* Public Key DER */
40106     struct tbl_s pub[] = {
40107 #ifdef USE_CERT_BUFFERS_1024
40108         {client_keypub_der_1024, sizeof_client_keypub_der_1024},
40109 #endif
40110 #ifdef USE_CERT_BUFFERS_2048
40111         {client_keypub_der_2048, sizeof_client_keypub_der_2048},
40112 #endif
40113         {NULL, 0}
40114     };
40115 
40116     printf(testingFmt, "test_wolfSSL_RSA_DER()");
40117 
40118     for (i = 0; tbl[i].der != NULL; i++)
40119     {
40120         AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
40121         AssertNotNull(rsa);
40122         RSA_free(rsa);
40123     }
40124     for (i = 0; tbl[i].der != NULL; i++)
40125     {
40126         AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
40127         AssertNotNull(rsa);
40128         RSA_free(rsa);
40129     }
40130 
40131     for (i = 0; pub[i].der != NULL; i++)
40132     {
40133         AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
40134         AssertNotNull(rsa);
40135         AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
40136         buff = NULL;
40137         AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
40138         AssertNotNull(buff);
40139         AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
40140         XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
40141         RSA_free(rsa);
40142     }
40143 
40144     printf(resultFmt, passed);
40145 
40146 #endif
40147 }
40148 
40149 static void test_wolfSSL_RSA_get0_key(void)
40150 {
40151 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
40152     RSA *rsa = NULL;
40153     const BIGNUM* n = NULL;
40154     const BIGNUM* e = NULL;
40155     const BIGNUM* d = NULL;
40156 
40157     const unsigned char* der;
40158     int derSz;
40159 
40160 #ifdef USE_CERT_BUFFERS_1024
40161     der = client_key_der_1024;
40162     derSz = sizeof_client_key_der_1024;
40163 #elif defined(USE_CERT_BUFFERS_2048)
40164     der = client_key_der_2048;
40165     derSz = sizeof_client_key_der_2048;
40166 #else
40167     der = NULL;
40168     derSz = 0;
40169 #endif
40170 
40171     printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
40172 
40173     if (der != NULL) {
40174         RSA_get0_key(NULL, NULL, NULL, NULL);
40175         RSA_get0_key(rsa, NULL, NULL, NULL);
40176         RSA_get0_key(NULL, &n, &e, &d);
40177         AssertNull(n);
40178         AssertNull(e);
40179         AssertNull(d);
40180 
40181         AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
40182         AssertNotNull(rsa);
40183 
40184         RSA_get0_key(rsa, NULL, NULL, NULL);
40185         RSA_get0_key(rsa, &n, NULL, NULL);
40186         AssertNotNull(n);
40187         RSA_get0_key(rsa, NULL, &e, NULL);
40188         AssertNotNull(e);
40189         RSA_get0_key(rsa, NULL, NULL, &d);
40190         AssertNotNull(d);
40191         RSA_get0_key(rsa, &n, &e, &d);
40192         AssertNotNull(n);
40193         AssertNotNull(e);
40194         AssertNotNull(d);
40195 
40196         RSA_free(rsa);
40197     }
40198     printf(resultFmt, passed);
40199 #endif
40200 }
40201 
40202 static void test_wolfSSL_RSA_meth(void)
40203 {
40204 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
40205     RSA *rsa;
40206     RSA_METHOD *rsa_meth;
40207 
40208     printf(testingFmt, "test_wolfSSL_RSA_meth");
40209 
40210 #ifdef WOLFSSL_KEY_GEN
40211     AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
40212     RSA_free(rsa);
40213 #else
40214     AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
40215 #endif
40216 
40217     AssertNotNull(rsa_meth =
40218             RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
40219 
40220 #ifndef NO_WOLFSSL_STUB
40221     AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
40222     AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
40223     AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
40224     AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
40225     AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
40226     AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
40227     AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
40228 #endif
40229 
40230     AssertNotNull(rsa = RSA_new());
40231     AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
40232     AssertPtrEq(RSA_get_method(rsa), rsa_meth);
40233     AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
40234     RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
40235     AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
40236     AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC | RSA_METHOD_FLAG_NO_CHECK);
40237     RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
40238     AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
40239     AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
40240 
40241     /* rsa_meth is freed here */
40242     RSA_free(rsa);
40243 
40244     printf(resultFmt, passed);
40245 #endif
40246 }
40247 
40248 static void test_wolfSSL_verify_mode(void)
40249 {
40250 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
40251     WOLFSSL*     ssl;
40252     WOLFSSL_CTX* ctx;
40253 
40254     printf(testingFmt, "test_wolfSSL_verify()");
40255     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
40256 
40257     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
40258     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
40259     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
40260 
40261     AssertNotNull(ssl = SSL_new(ctx));
40262     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
40263     SSL_free(ssl);
40264 
40265     SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
40266     AssertNotNull(ssl = SSL_new(ctx));
40267     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
40268     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
40269 
40270     wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
40271     AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
40272     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
40273 
40274     SSL_free(ssl);
40275 
40276     wolfSSL_CTX_set_verify(ctx,
40277                   WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
40278     AssertNotNull(ssl = SSL_new(ctx));
40279     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
40280     AssertIntEQ(SSL_get_verify_mode(ssl),
40281                 WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
40282 
40283     wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
40284     AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
40285                 WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
40286     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
40287 
40288     wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
40289     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
40290 
40291     wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
40292     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
40293 
40294     wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
40295     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
40296 
40297 #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
40298     wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
40299     AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
40300 #endif
40301 
40302     AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
40303                 WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
40304 
40305     SSL_free(ssl);
40306 
40307     SSL_CTX_free(ctx);
40308     printf(resultFmt, passed);
40309 #endif
40310 }
40311 
40312 
40313 static void test_wolfSSL_verify_depth(void)
40314 {
40315 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
40316     WOLFSSL*     ssl;
40317     WOLFSSL_CTX* ctx;
40318     long         depth;
40319 
40320     printf(testingFmt, "test_wolfSSL_verify_depth()");
40321     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
40322 
40323     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
40324     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
40325     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
40326 
40327     AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
40328     AssertNotNull(ssl = SSL_new(ctx));
40329     AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
40330     SSL_free(ssl);
40331 
40332     SSL_CTX_set_verify_depth(ctx, -1);
40333     AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
40334 
40335     SSL_CTX_set_verify_depth(ctx, 2);
40336     AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
40337     AssertNotNull(ssl = SSL_new(ctx));
40338     AssertIntEQ(2, SSL_get_verify_depth(ssl));
40339 
40340     SSL_free(ssl);
40341     SSL_CTX_free(ctx);
40342     printf(resultFmt, passed);
40343 #endif
40344 }
40345 
40346 #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
40347 /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
40348  * buffer of 64 bytes.
40349  *
40350  * returns the size of the digest buffer on success and a negative value on
40351  * failure.
40352  */
40353 static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
40354 {
40355     HMAC_CTX ctx1;
40356     HMAC_CTX ctx2;
40357 
40358     unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
40359                           "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
40360     unsigned char long_key[] =
40361         "0123456789012345678901234567890123456789"
40362         "0123456789012345678901234567890123456789"
40363         "0123456789012345678901234567890123456789"
40364         "0123456789012345678901234567890123456789";
40365 
40366     unsigned char msg[] = "message to hash";
40367     unsigned int  digestSz = 64;
40368     int keySz = sizeof(key);
40369     int long_keySz = sizeof(long_key);
40370     int msgSz = sizeof(msg);
40371 
40372     unsigned char digest2[64];
40373     unsigned int digestSz2 = 64;
40374 
40375     HMAC_CTX_init(&ctx1);
40376 
40377     AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
40378     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40379     AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
40380 
40381     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40382     AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
40383     HMAC_CTX_cleanup(&ctx1);
40384 
40385     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40386     AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
40387     HMAC_CTX_cleanup(&ctx2);
40388 
40389     AssertIntEQ(digestSz, digestSz2);
40390     AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
40391 
40392     /* test HMAC_Init with NULL key */
40393 
40394     /* init after copy */
40395     printf("test HMAC_Init with NULL key (0)\n");
40396     HMAC_CTX_init(&ctx1);
40397     AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
40398     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40399     AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
40400 
40401     AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
40402     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40403     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40404     AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
40405     HMAC_CTX_cleanup(&ctx1);
40406 
40407     AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
40408     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40409     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40410     AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
40411     HMAC_CTX_cleanup(&ctx2);
40412 
40413     AssertIntEQ(digestSz, digestSz2);
40414     AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
40415 
40416     /* long key */
40417     printf("test HMAC_Init with NULL key (1)\n");
40418     HMAC_CTX_init(&ctx1);
40419     AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
40420     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40421     AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
40422 
40423     AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
40424     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40425     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40426     AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
40427     HMAC_CTX_cleanup(&ctx1);
40428 
40429     AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
40430     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40431     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40432     AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
40433     HMAC_CTX_cleanup(&ctx2);
40434 
40435     AssertIntEQ(digestSz, digestSz2);
40436     AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
40437 
40438     /* init before copy */
40439     printf("test HMAC_Init with NULL key (2)\n");
40440     HMAC_CTX_init(&ctx1);
40441     AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
40442     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40443     AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
40444     AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
40445 
40446     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40447     AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
40448     AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
40449     HMAC_CTX_cleanup(&ctx1);
40450 
40451     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40452     AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
40453     AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
40454     HMAC_CTX_cleanup(&ctx2);
40455 
40456     AssertIntEQ(digestSz, digestSz2);
40457     AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
40458 
40459     return digestSz;
40460 }
40461 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
40462 
40463 static void test_wolfSSL_HMAC_CTX(void)
40464 {
40465 #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
40466     unsigned char digest[64];
40467     int digestSz;
40468 
40469     printf(testingFmt, "wolfSSL_HMAC_CTX()");
40470 
40471     #ifndef NO_SHA
40472     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
40473     AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
40474                           "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
40475     #endif /* !NO_SHA */
40476     #ifdef WOLFSSL_SHA224
40477     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
40478     AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
40479                           "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
40480                           "\x02\x0E", digest, digestSz), 0);
40481 
40482     #endif /* WOLFSSL_SHA224 */
40483     #ifndef NO_SHA256
40484     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
40485     AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
40486                           "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
40487                           "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
40488 
40489     #endif /* !NO_SHA256 */
40490 
40491     #ifdef WOLFSSL_SHA384
40492     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
40493     AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
40494                           "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
40495                           "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
40496                           "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
40497                           digest, digestSz), 0);
40498     #endif /* WOLFSSL_SHA384 */
40499     #ifdef WOLFSSL_SHA512
40500     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
40501     AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
40502                           "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
40503                           "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
40504                           "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
40505                           "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
40506                           digest, digestSz), 0);
40507     #endif /* WOLFSSL_SHA512 */
40508 
40509     #ifndef NO_MD5
40510     AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
40511     AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
40512                           "\xE4\x98\xDD", digest, digestSz), 0);
40513     #endif /* !NO_MD5 */
40514 
40515     printf(resultFmt, passed);
40516 #endif
40517 }
40518 
40519 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
40520 static void sslMsgCb(int w, int version, int type, const void* buf,
40521         size_t sz, SSL* ssl, void* arg)
40522 {
40523     int i;
40524     unsigned char* pt = (unsigned char*)buf;
40525 
40526     printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
40527             (int)sz, version, type);
40528     for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
40529     printf("\n");
40530     (void)ssl;
40531     (void)arg;
40532 }
40533 #endif /* OPENSSL_EXTRA */
40534 
40535 static void test_wolfSSL_msg_callback(void)
40536 {
40537 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
40538     WOLFSSL*     ssl;
40539     WOLFSSL_CTX* ctx;
40540 
40541     printf(testingFmt, "wolfSSL_msg_callback()");
40542     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
40543 
40544     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
40545                 SSL_FILETYPE_PEM));
40546     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
40547                 SSL_FILETYPE_PEM));
40548     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
40549                 SSL_SUCCESS);
40550 
40551     AssertNotNull(ssl = SSL_new(ctx));
40552     AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
40553     AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
40554     AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
40555 
40556     SSL_free(ssl);
40557     SSL_CTX_free(ctx);
40558 
40559     printf(resultFmt, passed);
40560 #endif
40561 }
40562 
40563 static void test_wolfSSL_SHA(void)
40564 {
40565 #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
40566     printf(testingFmt, "wolfSSL_SHA()");
40567 
40568     #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES)
40569     {
40570         const unsigned char in[] = "abc";
40571         unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
40572                                     "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
40573         unsigned char out[WC_SHA_DIGEST_SIZE];
40574 
40575         XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
40576         AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
40577         AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
40578 
40579         /* SHA interface test */
40580         XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
40581 
40582         AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
40583         AssertNotNull(SHA(in, 0, out));
40584         AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
40585         AssertNotNull(SHA(NULL, 0, out));
40586         AssertNotNull(SHA(NULL, 0, NULL));
40587 
40588         AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
40589         AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
40590     }
40591     #endif
40592 
40593     #if !defined(NO_SHA256)
40594     {
40595         const unsigned char in[] = "abc";
40596         unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
40597             "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
40598             "\x15\xAD";
40599         unsigned char out[WC_SHA256_DIGEST_SIZE];
40600 
40601         XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
40602 #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
40603         AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
40604 #else
40605         AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
40606 #endif
40607         AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
40608     }
40609     #endif
40610 
40611     #if defined(WOLFSSL_SHA384)
40612     {
40613         const unsigned char in[] = "abc";
40614         unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
40615             "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
40616             "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
40617             "\xc8\x25\xa7";
40618         unsigned char out[WC_SHA384_DIGEST_SIZE];
40619 
40620         XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
40621 #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
40622         AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
40623 #else
40624         AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
40625 #endif
40626         AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
40627     }
40628     #endif
40629 
40630     #if defined(WOLFSSL_SHA512)
40631     {
40632         const unsigned char in[] = "abc";
40633         unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
40634            "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
40635             "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
40636             "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
40637             "\xa5\x4c\xa4\x9f";
40638         unsigned char out[WC_SHA512_DIGEST_SIZE];
40639 
40640         XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
40641 #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
40642         AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
40643 #else
40644         AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
40645 #endif
40646         AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
40647     }
40648     #endif
40649 
40650     printf(resultFmt, passed);
40651 #endif
40652 }
40653 
40654 static void test_wolfSSL_DH_1536_prime(void)
40655 {
40656 #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
40657     BIGNUM* bn;
40658     unsigned char bits[200];
40659     int sz = 192; /* known binary size */
40660     const byte expected[] = {
40661         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
40662         0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
40663         0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
40664         0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
40665         0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
40666         0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
40667         0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
40668         0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
40669         0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
40670         0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
40671         0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
40672         0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
40673         0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
40674         0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
40675         0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
40676         0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
40677         0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
40678         0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
40679         0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
40680         0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
40681         0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
40682         0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
40683         0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
40684         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
40685     };
40686 
40687     printf(testingFmt, "wolfSSL_DH_1536_prime()");
40688     bn = get_rfc3526_prime_1536(NULL);
40689     AssertNotNull(bn);
40690     AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
40691     AssertIntEQ(0, XMEMCMP(expected, bits, sz));
40692 
40693     BN_free(bn);
40694     printf(resultFmt, passed);
40695 #endif
40696 }
40697 
40698 static void test_wolfSSL_PEM_write_DHparams(void)
40699 {
40700 #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
40701     !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
40702     DH* dh;
40703     BIO* bio;
40704     XFILE fp;
40705     byte pem[2048];
40706     int  pemSz;
40707     const char expected[] =
40708 "-----BEGIN DH PARAMETERS-----\n\
40709 MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
40710 v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
40711 nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
40712 joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
40713 wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
40714 tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
40715 -----END DH PARAMETERS-----\n";
40716     printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
40717 
40718     AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
40719     AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
40720     XFCLOSE(fp);
40721 
40722     AssertNotNull(bio = BIO_new(BIO_s_mem()));
40723     AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
40724     AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
40725     BIO_free(bio);
40726 
40727     AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
40728     AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
40729     AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
40730     XFCLOSE(fp);
40731     DH_free(dh);
40732 
40733     /* check results */
40734     XMEMSET(pem, 0, sizeof(pem));
40735     AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
40736     AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
40737     AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
40738     XFCLOSE(fp);
40739 
40740     printf(resultFmt, passed);
40741 #endif
40742 }
40743 
40744 /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
40745 #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
40746     (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
40747 static void binary_dump(void *ptr, int size)
40748 {
40749     #ifdef WOLFSSL_EVP_PRINT
40750     int i = 0;
40751     unsigned char *p = (unsigned char *) ptr;
40752 
40753     printf("{");
40754     while((p != NULL) && (i < size)) {
40755             if((i % 8) == 0) {
40756                     printf("\n");
40757                     printf("    ");
40758             }
40759             printf("0x%02x, ", p[i]);
40760             i++;
40761     }
40762     printf("\n};\n");
40763     #else
40764     (void) ptr;
40765     (void) size;
40766     #endif
40767 }
40768 
40769 static int last_val = 0x0f;
40770 
40771 static int check_result(unsigned char *data, int len)
40772 {
40773     int i;
40774 
40775     for( ; len; ) {
40776             last_val = (last_val + 1) % 16;
40777             for(i = 0; i < 16; len--, i++, data++)
40778                     if(*data != last_val) {
40779                             return -1;
40780                     }
40781     }
40782     return 0;
40783 }
40784 
40785 static int r_offset;
40786 static int w_offset;
40787 
40788 static void init_offset(void)
40789 {
40790     r_offset = 0;
40791     w_offset = 0;
40792 }
40793 static void get_record(unsigned char *data, unsigned char *buf, int len)
40794 {
40795     XMEMCPY(buf, data+r_offset, len);
40796     r_offset += len;
40797 }
40798 
40799 static void set_record(unsigned char *data, unsigned char *buf, int len)
40800 {
40801     XMEMCPY(data+w_offset, buf, len);
40802     w_offset += len;
40803 }
40804 
40805 static void set_plain(unsigned char *plain, int rec)
40806 {
40807     int i, j;
40808     unsigned char *p = plain;
40809 
40810     #define BLOCKSZ 16
40811 
40812     for(i=0; i<(rec/BLOCKSZ); i++){
40813         for(j=0; j<BLOCKSZ; j++)
40814             *p++ = (i % 16);
40815     }
40816 }
40817 #endif
40818 
40819 static int test_wolfSSL_EVP_Cipher_extra(void)
40820 {
40821 
40822 #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
40823     (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
40824 
40825     /* aes128-cbc, keylen=16, ivlen=16 */
40826     byte aes128_cbc_key[] = {
40827         0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
40828         0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
40829     };
40830 
40831     byte aes128_cbc_iv[] = {
40832         0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
40833         0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
40834     };
40835 
40836     /* teset data size table */
40837     int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
40838     int test_drive2[] = {8, 3, 8, 512, 0};
40839     int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
40840 
40841     int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
40842     int test_drive_len[100];
40843 
40844     int ret = 0;
40845     EVP_CIPHER_CTX *evp = NULL;
40846 
40847     int ilen = 0;
40848     int klen = 0;
40849     int i, j;
40850 
40851     const EVP_CIPHER *type;
40852     byte *iv;
40853     byte *key;
40854     int ivlen;
40855     int keylen;
40856 
40857     #define RECORDS 16
40858     #define BUFFSZ  512
40859     byte plain [BUFFSZ * RECORDS];
40860     byte cipher[BUFFSZ * RECORDS];
40861 
40862     byte inb[BUFFSZ];
40863     byte outb[BUFFSZ+16];
40864     int outl, inl;
40865 
40866     iv = aes128_cbc_iv;
40867     ivlen = sizeof(aes128_cbc_iv);
40868     key = aes128_cbc_key;
40869     keylen = sizeof(aes128_cbc_key);
40870     type = EVP_aes_128_cbc();
40871 
40872     set_plain(plain, BUFFSZ * RECORDS);
40873 
40874     SSL_library_init();
40875 
40876     AssertNotNull(evp = EVP_CIPHER_CTX_new());
40877     AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
40878 
40879     klen = EVP_CIPHER_CTX_key_length(evp);
40880     if (klen > 0 && keylen != klen) {
40881         AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
40882     }
40883     ilen = EVP_CIPHER_CTX_iv_length(evp);
40884     if (ilen > 0 && ivlen != ilen) {
40885         AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
40886     }
40887 
40888     AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
40889 
40890     for (j = 0; j<RECORDS; j++)
40891     {
40892         inl = BUFFSZ;
40893         get_record(plain, inb, inl);
40894         AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
40895         set_record(cipher, outb, outl);
40896     }
40897 
40898     for (i = 0; test_drive[i]; i++) {
40899 
40900     AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
40901         init_offset();
40902         test_drive_len[i] = 0;
40903 
40904         for (j = 0; test_drive[i][j]; j++)
40905         {
40906             inl = test_drive[i][j];
40907             test_drive_len[i] += inl;
40908 
40909             get_record(plain, inb, inl);
40910         AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
40911             /* output to cipher buffer, so that following Dec test can detect
40912                if any error */
40913             set_record(cipher, outb, outl);
40914         }
40915 
40916         EVP_CipherFinal(evp, outb, &outl);
40917 
40918         if(outl > 0)
40919             set_record(cipher, outb, outl);
40920     }
40921 
40922     for (i = 0; test_drive[i]; i++) {
40923 
40924             last_val = 0x0f;
40925 
40926         AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
40927 
40928         init_offset();
40929 
40930         for (j = 0; test_drive[i][j]; j++){
40931             inl = test_drive[i][j];
40932             get_record(cipher, inb, inl);
40933 
40934             AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
40935 
40936             binary_dump(outb, outl);
40937             AssertIntEQ((ret = check_result(outb, outl)), 0);
40938             AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
40939         }
40940 
40941         ret = EVP_CipherFinal(evp, outb, &outl);
40942         binary_dump(outb, outl);
40943 
40944         ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
40945                  (((test_drive_len[i] % 16) == 0) && (ret == 1));
40946         AssertTrue(ret);
40947     }
40948 
40949     EVP_CIPHER_CTX_free(evp);
40950 
40951 #endif /* test_EVP_Cipher */
40952 
40953     return 0;
40954 }
40955 
40956 static void test_wolfSSL_PEM_read_DHparams(void)
40957 {
40958 #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
40959     !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
40960     DH* dh;
40961     XFILE fp;
40962     unsigned char derOut[300];
40963     unsigned char* derOutBuf = derOut;
40964     int derOutSz = 0;
40965 
40966     unsigned char derExpected[300];
40967     int derExpectedSz = 0;
40968 
40969     printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
40970 
40971     XMEMSET(derOut, 0, sizeof(derOut));
40972     XMEMSET(derExpected, 0, sizeof(derExpected));
40973 
40974     /* open DH param file, read into DH struct */
40975     AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
40976 
40977     /* bad args */
40978     AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
40979     AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
40980 
40981     /* good args */
40982     AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
40983     XFCLOSE(fp);
40984 
40985     /* read in certs/dh2048.der for comparison against exported params */
40986     fp = XFOPEN("./certs/dh2048.der", "rb");
40987     AssertTrue(fp != XBADFILE);
40988     derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
40989     XFCLOSE(fp);
40990 
40991     /* export DH back to DER and compare */
40992     derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
40993     AssertIntEQ(derOutSz, derExpectedSz);
40994     AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
40995 
40996     /* Test parsing with X9.42 header */
40997     fp = XFOPEN("./certs/x942dh2048.pem", "rb");
40998     AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
40999     XFCLOSE(fp);
41000 
41001     DH_free(dh);
41002     printf(resultFmt, passed);
41003 #endif
41004 }
41005 
41006 static void test_wolfSSL_AES_ecb_encrypt(void)
41007 {
41008 #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
41009     AES_KEY aes;
41010     const byte msg[] =
41011     {
41012       0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
41013       0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
41014     };
41015 
41016     const byte verify[] =
41017     {
41018         0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
41019         0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
41020     };
41021 
41022     const byte key[] =
41023     {
41024       0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
41025       0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
41026       0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
41027       0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
41028     };
41029 
41030 
41031     byte out[AES_BLOCK_SIZE];
41032 
41033     printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
41034 
41035     AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
41036     XMEMSET(out, 0, AES_BLOCK_SIZE);
41037     AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
41038     AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
41039 
41040 #ifdef HAVE_AES_DECRYPT
41041     AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
41042     XMEMSET(out, 0, AES_BLOCK_SIZE);
41043     AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
41044     AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
41045 #endif
41046 
41047     /* test bad arguments */
41048     AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
41049     AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
41050     AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
41051 
41052     printf(resultFmt, passed);
41053 #endif
41054 }
41055 
41056 static void test_wolfSSL_MD5(void)
41057 {
41058 #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
41059     byte input1[] = "";
41060     byte input2[] = "message digest";
41061     byte hash[WC_MD5_DIGEST_SIZE];
41062     unsigned char output1[] =
41063         "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
41064     unsigned char output2[] =
41065         "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
41066     WOLFSSL_MD5_CTX md5;
41067 
41068     printf(testingFmt, "wolfSSL_MD5()");
41069 
41070     XMEMSET(&md5, 0, sizeof(md5));
41071 
41072     /* Test cases for illegal parameters */
41073     AssertIntEQ(MD5_Init(NULL), 0);
41074     AssertIntEQ(MD5_Init(&md5), 1);
41075     AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
41076     AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
41077     AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
41078     AssertIntEQ(MD5_Final(NULL, &md5), 0);
41079     AssertIntEQ(MD5_Final(hash, NULL), 0);
41080     AssertIntEQ(MD5_Final(NULL, NULL), 0);
41081 
41082     /* Init MD5 CTX */
41083     AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
41084     AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
41085                                         XSTRLEN((const char*)&input1)), 1);
41086     AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
41087     AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
41088 
41089     /* Init MD5 CTX */
41090     AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
41091     AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
41092                                     (int)XSTRLEN((const char*)input2)), 1);
41093     AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
41094     AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
41095 #if !defined(NO_OLD_NAMES) && \
41096   (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
41097     AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
41098     AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
41099     AssertPtrNE(MD5(input1, 1, NULL), NULL);
41100     AssertPtrNE(MD5(NULL, 0, NULL), NULL);
41101 
41102     AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
41103     AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
41104 
41105     AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
41106     AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
41107     {
41108         byte data[] = "Data to be hashed.";
41109         XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
41110 
41111         AssertNotNull(MD5(data, sizeof(data), NULL));
41112         AssertNotNull(MD5(data, sizeof(data), hash));
41113         AssertNotNull(MD5(NULL, 0, hash));
41114         AssertNull(MD5(NULL, sizeof(data), hash));
41115     }
41116 #endif
41117 
41118     printf(resultFmt, passed);
41119 #endif
41120 }
41121 
41122 static void test_wolfSSL_MD5_Transform(void)
41123 {
41124 #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
41125     byte input1[] = "";
41126     byte input2[] = "abc";
41127     byte local[WC_MD5_BLOCK_SIZE];
41128     word32 sLen = 0;
41129 #ifdef BIG_ENDIAN_ORDER
41130     unsigned char output1[] =
41131         "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
41132     unsigned char output2[] =
41133         "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
41134 #else
41135     unsigned char output1[] =
41136         "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
41137     unsigned char output2[] =
41138         "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
41139 #endif
41140 
41141     MD5_CTX md5;
41142 
41143     printf(testingFmt, "wolfSSL_MD5_Transform()");
41144 
41145     XMEMSET(&md5, 0, sizeof(md5));
41146     XMEMSET(&local, 0, sizeof(local));
41147 
41148     /* sanity check */
41149     AssertIntEQ(MD5_Transform(NULL, NULL), 0);
41150     AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
41151     AssertIntEQ(MD5_Transform(&md5, NULL), 0);
41152     AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
41153     AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
41154     AssertIntEQ(wc_Md5Transform((wc_Md5*)&md5, NULL), BAD_FUNC_ARG);
41155 
41156     /* Init MD5 CTX */
41157     AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
41158     /* Do Transform*/
41159     sLen = (word32)XSTRLEN((char*)input1);
41160     XMEMCPY(local, input1, sLen);
41161     AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
41162 
41163     AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output1,
41164                                                     WC_MD5_DIGEST_SIZE), 0);
41165 
41166     /* Init MD5 CTX */
41167     AssertIntEQ(MD5_Init(&md5), 1);
41168     sLen = (word32)XSTRLEN((char*)input2);
41169     XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
41170     XMEMCPY(local, input2, sLen);
41171     AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
41172     AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output2,
41173                                                     WC_MD5_DIGEST_SIZE), 0);
41174 
41175     printf(resultFmt, passed);
41176 #endif
41177 }
41178 
41179 static void test_wolfSSL_SHA224(void)
41180 {
41181 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
41182     !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
41183     (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
41184     unsigned char input[] =
41185         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
41186     unsigned char output[] =
41187          "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
41188          "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
41189     size_t inLen;
41190     byte hash[WC_SHA224_DIGEST_SIZE];
41191 
41192     printf(testingFmt, "wolfSSL_SHA224()");
41193     inLen  = XSTRLEN((char*)input);
41194 
41195     XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
41196 
41197     AssertNull(SHA224(NULL, inLen, hash));
41198     AssertNotNull(SHA224(input, 0, hash));
41199     AssertNotNull(SHA224(input, inLen, NULL));
41200     AssertNotNull(SHA224(NULL, 0, hash));
41201     AssertNotNull(SHA224(NULL, 0, NULL));
41202 
41203     AssertNotNull(SHA224(input, inLen, hash));
41204     AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
41205 
41206     printf(resultFmt, passed);
41207 #endif
41208 }
41209 static void test_wolfSSL_SHA_Transform(void)
41210 {
41211 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
41212 #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
41213         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
41214     byte input1[] = "";
41215     byte input2[] = "abc";
41216     byte local[WC_SHA_BLOCK_SIZE];
41217     word32 sLen = 0;
41218 #ifdef BIG_ENDIAN_ORDER
41219     unsigned char output1[] =
41220         "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
41221         "\x09\xf7\x3a\x93";
41222     unsigned char output2[] =
41223         "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
41224         "\x7c\x6e\x08\xb8";
41225 #else
41226     unsigned char output1[] =
41227         "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
41228         "\x93\x3a\xf7\x09";
41229     unsigned char output2[] =
41230         "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
41231         "\xb8\x08\x6e\x7c";
41232 #endif
41233 
41234     SHA_CTX sha;
41235     SHA_CTX sha1;
41236 
41237     printf(testingFmt, "wolfSSL_SHA_Transform()");
41238 
41239     XMEMSET(&sha, 0, sizeof(sha));
41240     XMEMSET(&local, 0, sizeof(local));
41241 
41242     /* sanity check */
41243     AssertIntEQ(SHA_Transform(NULL, NULL), 0);
41244     AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
41245     AssertIntEQ(SHA_Transform(&sha, NULL), 0);
41246     AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
41247     AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
41248     AssertIntEQ(SHA1_Transform(&sha, NULL), 0);
41249     AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
41250     AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
41251     AssertIntEQ(wc_ShaTransform((wc_Sha*)&sha, NULL), BAD_FUNC_ARG);
41252 
41253     /* Init SHA CTX */
41254     AssertIntEQ(SHA_Init(&sha), 1);
41255     /* Do Transform*/
41256     sLen = (word32)XSTRLEN((char*)input1);
41257     XMEMCPY(local, input1, sLen);
41258     AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
41259     AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output1,
41260                                                         WC_SHA_DIGEST_SIZE), 0);
41261     AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
41262 
41263     /* Init SHA CTX */
41264     AssertIntEQ(SHA_Init(&sha), 1);
41265     sLen = (word32)XSTRLEN((char*)input2);
41266     XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
41267     XMEMCPY(local, input2, sLen);
41268     AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
41269     AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output2,
41270                                                         WC_SHA_DIGEST_SIZE), 0);
41271     AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
41272 
41273     /* SHA1 */
41274     XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
41275     /* Init SHA CTX */
41276     AssertIntEQ(SHA1_Init(&sha1), 1);
41277     /* Do Transform*/
41278     sLen = (word32)XSTRLEN((char*)input1);
41279     XMEMCPY(local, input1, sLen);
41280     AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
41281     AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output1,
41282                                                         WC_SHA_DIGEST_SIZE), 0);
41283     AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
41284 
41285     /* Init SHA CTX */
41286     AssertIntEQ(SHA1_Init(&sha1), 1);
41287     sLen = (word32)XSTRLEN((char*)input2);
41288     XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
41289     XMEMCPY(local, input2, sLen);
41290     AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
41291     AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output2,
41292                                                         WC_SHA_DIGEST_SIZE), 0);
41293     AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
41294 
41295     printf(resultFmt, passed);
41296 #endif
41297 #endif
41298 }
41299 
41300 static void test_wolfSSL_SHA256_Transform(void)
41301 {
41302 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
41303 #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
41304         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
41305         !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
41306     byte input1[] = "";
41307     byte input2[] = "abc";
41308     byte local[WC_SHA256_BLOCK_SIZE];
41309     word32 sLen = 0;
41310 #ifdef BIG_ENDIAN_ORDER
41311     unsigned char output1[] =
41312         "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
41313         "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
41314     unsigned char output2[] =
41315         "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
41316         "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
41317 #else
41318     unsigned char output1[] =
41319         "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
41320         "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
41321     unsigned char output2[] =
41322         "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
41323         "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
41324 #endif
41325     SHA256_CTX sha256;
41326 
41327     printf(testingFmt, "wolfSSL_SHA256_Transform()");
41328 
41329     XMEMSET(&sha256, 0, sizeof(sha256));
41330     XMEMSET(&local, 0, sizeof(local));
41331 
41332     /* sanity check */
41333     AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
41334     AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
41335     AssertIntEQ(SHA256_Transform(&sha256, NULL), 0);
41336     AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
41337     AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
41338     AssertIntEQ(wc_Sha256Transform((wc_Sha256*)&sha256, NULL), BAD_FUNC_ARG);
41339 
41340     /* Init SHA256 CTX */
41341     AssertIntEQ(SHA256_Init(&sha256), 1);
41342     /* Do Transform*/
41343     sLen = (word32)XSTRLEN((char*)input1);
41344     XMEMCPY(local, input1, sLen);
41345     AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
41346     AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output1,
41347                                                     WC_SHA256_DIGEST_SIZE), 0);
41348     AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
41349 
41350     /* Init SHA256 CTX */
41351     AssertIntEQ(SHA256_Init(&sha256), 1);
41352     sLen = (word32)XSTRLEN((char*)input2);
41353     XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
41354     XMEMCPY(local, input2, sLen);
41355     AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
41356     AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output2,
41357                                                     WC_SHA256_DIGEST_SIZE), 0);
41358     AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
41359 
41360     printf(resultFmt, passed);
41361 #endif
41362 #endif
41363 }
41364 
41365 static void test_wolfSSL_SHA256(void)
41366 {
41367 #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
41368     defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
41369     unsigned char input[] =
41370         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
41371     unsigned char output[] =
41372         "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
41373         "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
41374         "\x06\xC1";
41375     size_t inLen;
41376     byte hash[WC_SHA256_DIGEST_SIZE];
41377 
41378     printf(testingFmt, "wolfSSL_SHA256()");
41379     inLen  = XSTRLEN((char*)input);
41380 
41381     XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
41382     AssertNotNull(SHA256(input, inLen, hash));
41383     AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
41384 
41385     printf(resultFmt, passed);
41386 #endif
41387 }
41388 
41389 static void test_wolfSSL_SHA512_Transform(void)
41390 {
41391 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
41392 #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
41393         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
41394     byte input1[] = "";
41395     byte input2[] = "abc";
41396     byte local[WC_SHA512_BLOCK_SIZE];
41397     word32 sLen = 0;
41398 #ifdef BIG_ENDIAN_ORDER
41399     unsigned char output1[] =
41400         "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
41401         "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
41402         "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
41403         "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
41404    unsigned char output2[] =
41405         "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
41406         "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
41407         "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
41408         "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
41409 #else
41410     unsigned char output1[] =
41411         "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
41412         "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
41413         "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
41414         "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
41415    unsigned char output2[] =
41416         "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
41417         "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
41418         "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
41419         "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
41420 #endif
41421     SHA512_CTX sha512;
41422 
41423     printf(testingFmt, "wolfSSL_SHA512_Transform()");
41424 
41425     XMEMSET(&sha512, 0, sizeof(sha512));
41426     XMEMSET(&local, 0, sizeof(local));
41427 
41428     /* sanity check */
41429     AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
41430     AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
41431     AssertIntEQ(SHA512_Transform(&sha512, NULL), 0);
41432     AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
41433     AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
41434     AssertIntEQ(wc_Sha512Transform((wc_Sha512*)&sha512, NULL), BAD_FUNC_ARG);
41435 
41436     /* Init SHA512 CTX */
41437     AssertIntEQ(wolfSSL_SHA512_Init(&sha512), 1);
41438 
41439     /* Do Transform*/
41440     sLen = (word32)XSTRLEN((char*)input1);
41441     XMEMCPY(local, input1, sLen);
41442     AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
41443     AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output1,
41444                                                     WC_SHA512_DIGEST_SIZE), 0);
41445     AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
41446 
41447     /* Init SHA512 CTX */
41448     AssertIntEQ(SHA512_Init(&sha512), 1);
41449     sLen = (word32)XSTRLEN((char*)input2);
41450     XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
41451     XMEMCPY(local, input2, sLen);
41452     AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
41453     AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output2,
41454                                                     WC_SHA512_DIGEST_SIZE), 0);
41455     AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
41456 
41457     (void)input1;
41458     printf(resultFmt, passed);
41459 #endif
41460 #endif
41461 }
41462 
41463 static void test_wolfSSL_X509_get_serialNumber(void)
41464 {
41465 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
41466     ASN1_INTEGER* a;
41467     BIGNUM* bn;
41468     X509*   x509;
41469     char *serialHex;
41470     byte serial[3];
41471     int  serialSz;
41472 
41473     printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
41474 
41475     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
41476                                                       SSL_FILETYPE_PEM));
41477     AssertNotNull(a = X509_get_serialNumber(x509));
41478 
41479     /* check on value of ASN1 Integer */
41480     AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
41481 
41482 
41483     /* test setting serial number and then retrieving it */
41484     AssertNotNull(a = ASN1_INTEGER_new());
41485     ASN1_INTEGER_set(a, 3);
41486     AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
41487     serialSz = sizeof(serial);
41488     AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
41489             WOLFSSL_SUCCESS);
41490     AssertIntEQ(serialSz, 1);
41491     AssertIntEQ(serial[0], 3);
41492     ASN1_INTEGER_free(a);
41493 
41494     /* test setting serial number with 0's in it */
41495     serial[0] = 0x01;
41496     serial[1] = 0x00;
41497     serial[2] = 0x02;
41498 
41499     AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
41500     a->data[0] = ASN_INTEGER;
41501     a->data[1] = sizeof(serial);
41502     XMEMCPY(&a->data[2], serial, sizeof(serial));
41503     a->length = sizeof(serial) + 2;
41504     AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
41505 
41506     XMEMSET(serial, 0, sizeof(serial));
41507     serialSz = sizeof(serial);
41508     AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
41509             WOLFSSL_SUCCESS);
41510     AssertIntEQ(serialSz, 3);
41511     AssertIntEQ(serial[0], 0x01);
41512     AssertIntEQ(serial[1], 0x00);
41513     AssertIntEQ(serial[2], 0x02);
41514     ASN1_INTEGER_free(a);
41515 
41516     X509_free(x509); /* free's a */
41517 
41518     AssertNotNull(serialHex = BN_bn2hex(bn));
41519 #ifndef WC_DISABLE_RADIX_ZERO_PAD
41520     AssertStrEQ(serialHex, "01");
41521 #else
41522     AssertStrEQ(serialHex, "1");
41523 #endif
41524     OPENSSL_free(serialHex);
41525 
41526     AssertIntEQ(BN_get_word(bn), 1);
41527 
41528     BN_free(bn);
41529 
41530     /* hard test free'ing with dynamic buffer to make sure there is no leaks */
41531     a = ASN1_INTEGER_new();
41532     if (a) {
41533         AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
41534                     DYNAMIC_TYPE_OPENSSL));
41535         a->isDynamic = 1;
41536         ASN1_INTEGER_free(a);
41537     }
41538 
41539     printf(resultFmt, passed);
41540 #endif
41541 }
41542 
41543 
41544 static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
41545 #if defined(OPENSSL_EXTRA)
41546     printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
41547 
41548     AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
41549 
41550     AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
41551 
41552     AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
41553 
41554     printf(resultFmt, passed);
41555 #endif
41556 }
41557 
41558 static void test_wolfSSL_OPENSSL_hexstr2buf(void)
41559 {
41560 #if defined(OPENSSL_EXTRA)
41561     #define MAX_HEXSTR_BUFSZ 9
41562     #define NUM_CASES        5
41563     struct Output {
41564         const unsigned char buffer[MAX_HEXSTR_BUFSZ];
41565         long ret;
41566     };
41567     int i;
41568     int j;
41569 
41570     const char* inputs[NUM_CASES] = {
41571         "aabcd1357e",
41572         "01:12:23:34:a5:b6:c7:d8:e9",
41573         ":01:02",
41574         "012",
41575         ":ab:ac:d"
41576     };
41577     struct Output expectedOutputs[NUM_CASES] = {
41578         {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
41579         {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
41580         {{0x01, 0x02}, 2},
41581         {{0x00}, 0},
41582         {{0x00}, 0}
41583     };
41584     long len = 0;
41585     unsigned char* returnedBuf = NULL;
41586 
41587     printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
41588 
41589     for (i = 0; i < NUM_CASES; ++i) {
41590         returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
41591 
41592         if (returnedBuf == NULL) {
41593             AssertIntEQ(expectedOutputs[i].ret, 0);
41594             continue;
41595         }
41596 
41597         AssertIntEQ(expectedOutputs[i].ret, len);
41598 
41599         for (j = 0; j < len; ++j) {
41600             AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
41601         }
41602         OPENSSL_free(returnedBuf);
41603     }
41604 
41605     printf(resultFmt, passed);
41606 #endif
41607 }
41608 
41609 static void test_wolfSSL_ASN1_STRING_print_ex(void){
41610 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
41611 #ifndef NO_BIO
41612     ASN1_STRING* asn_str;
41613     const char data[] = "Hello wolfSSL!";
41614     ASN1_STRING* esc_str;
41615     const char esc_data[] = "a+;<>";
41616     BIO *bio;
41617     unsigned long flags;
41618     int p_len;
41619     unsigned char rbuf[255];
41620 
41621     printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
41622 
41623     /* setup */
41624     XMEMSET(rbuf, 0, 255);
41625     bio = BIO_new(BIO_s_mem());
41626     BIO_set_write_buf_size(bio,255);
41627 
41628     asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
41629     ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
41630     esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
41631     ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
41632 
41633     /* no flags */
41634     XMEMSET(rbuf, 0, 255);
41635     flags = 0;
41636     p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
41637     AssertIntEQ(p_len, 15);
41638     BIO_read(bio, (void*)rbuf, 15);
41639     AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
41640 
41641     /* RFC2253 Escape */
41642     XMEMSET(rbuf, 0, 255);
41643     flags = ASN1_STRFLGS_ESC_2253;
41644     p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
41645     AssertIntEQ(p_len, 9);
41646     BIO_read(bio, (void*)rbuf, 9);
41647     AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
41648 
41649     /* Show type */
41650     XMEMSET(rbuf, 0, 255);
41651     flags = ASN1_STRFLGS_SHOW_TYPE;
41652     p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
41653     AssertIntEQ(p_len, 28);
41654     BIO_read(bio, (void*)rbuf, 28);
41655     AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
41656 
41657     /* Dump All */
41658     XMEMSET(rbuf, 0, 255);
41659     flags = ASN1_STRFLGS_DUMP_ALL;
41660     p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
41661     AssertIntEQ(p_len, 31);
41662     BIO_read(bio, (void*)rbuf, 31);
41663     AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
41664 
41665     /* Dump Der */
41666     XMEMSET(rbuf, 0, 255);
41667     flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
41668     p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
41669     AssertIntEQ(p_len, 35);
41670     BIO_read(bio, (void*)rbuf, 35);
41671     AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
41672 
41673     /* Dump All + Show type */
41674     XMEMSET(rbuf, 0, 255);
41675     flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
41676     p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
41677     AssertIntEQ(p_len, 44);
41678     BIO_read(bio, (void*)rbuf, 44);
41679     AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
41680 
41681     BIO_free(bio);
41682     ASN1_STRING_free(asn_str);
41683     ASN1_STRING_free(esc_str);
41684 
41685     printf(resultFmt, passed);
41686 #endif /* !NO_BIO */
41687 #endif
41688 }
41689 
41690 static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
41691 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
41692     WOLFSSL_ASN1_TIME *t;
41693     WOLFSSL_ASN1_TIME *out;
41694     WOLFSSL_ASN1_TIME *gtime;
41695     int tlen = 0;
41696     unsigned char *data;
41697 
41698     printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
41699 
41700     /* UTC Time test */
41701     AssertNotNull(t = wolfSSL_ASN1_TIME_new());
41702     XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
41703     AssertNotNull(out = wolfSSL_ASN1_TIME_new());
41704     t->type = ASN_UTC_TIME;
41705     t->length = ASN_UTC_TIME_SIZE;
41706     XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
41707 
41708     tlen = wolfSSL_ASN1_TIME_get_length(t);
41709     AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
41710     data = wolfSSL_ASN1_TIME_get_data(t);
41711     AssertStrEQ((char*)data, "050727123456Z");
41712     gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
41713     AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
41714     AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
41715     AssertStrEQ((char*)gtime->data, "20050727123456Z");
41716 
41717     /* Generalized Time test */
41718     XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
41719     XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
41720     XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
41721     t->type = ASN_GENERALIZED_TIME;
41722     t->length = ASN_GENERALIZED_TIME_SIZE;
41723     XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
41724 
41725     tlen = wolfSSL_ASN1_TIME_get_length(t);
41726     AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
41727     data = wolfSSL_ASN1_TIME_get_data(t);
41728     AssertStrEQ((char*)data, "20050727123456Z");
41729     gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
41730     AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
41731     AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
41732     AssertStrEQ((char*)gtime->data, "20050727123456Z");
41733     XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
41734 
41735     /* Null parameter test */
41736     XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
41737     gtime = NULL;
41738     out = NULL;
41739     t->type = ASN_UTC_TIME;
41740     t->length = ASN_UTC_TIME_SIZE;
41741     XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
41742     AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
41743     AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
41744     AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
41745     AssertStrEQ((char*)gtime->data, "20050727123456Z");
41746 
41747     XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
41748     XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
41749     printf(resultFmt, passed);
41750 #endif
41751 }
41752 
41753 static void test_wolfSSL_X509_CA_num(void){
41754 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
41755     defined(HAVE_ECC) && !defined(NO_RSA)
41756     WOLFSSL_X509_STORE *store;
41757     WOLFSSL_X509 *x509_1, *x509_2;
41758     int ca_num = 0;
41759 
41760     printf(testingFmt, "wolfSSL_X509_CA_num()");
41761 
41762     store = wolfSSL_X509_STORE_new();
41763     x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
41764     wolfSSL_X509_STORE_add_cert(store, x509_1);
41765     ca_num = wolfSSL_X509_CA_num(store);
41766     AssertIntEQ(ca_num, 1);
41767 
41768     x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
41769     wolfSSL_X509_STORE_add_cert(store, x509_2);
41770     ca_num = wolfSSL_X509_CA_num(store);
41771     AssertIntEQ(ca_num, 2);
41772 
41773     wolfSSL_X509_free(x509_1);
41774     wolfSSL_X509_free(x509_2);
41775     wolfSSL_X509_STORE_free(store);
41776     printf(resultFmt, passed);
41777 #endif
41778 }
41779 
41780 static void test_wolfSSL_X509_check_ca(void){
41781 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
41782     WOLFSSL_X509 *x509;
41783 
41784     printf(testingFmt, "wolfSSL_X509_check_ca()");
41785 
41786     x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
41787     AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
41788     wolfSSL_X509_free(x509);
41789 
41790     printf(resultFmt, passed);
41791 #endif
41792 }
41793 
41794 static void test_wolfSSL_X509_check_ip_asc(void){
41795 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
41796     WOLFSSL_X509 *x509;
41797 
41798     printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
41799 
41800     x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
41801 #if 0
41802     /* TODO: add cert gen for testing positive case */
41803     AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
41804 #endif
41805     AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
41806     AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
41807     wolfSSL_X509_free(x509);
41808 
41809     printf(resultFmt, passed);
41810 #endif
41811 }
41812 
41813 static void test_wolfSSL_DC_cert(void)
41814 {
41815 #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
41816     int      ret;
41817     Cert     cert;
41818     CertName name;
41819     RsaKey   key;
41820     WC_RNG   rng;
41821     byte     der[FOURK_BUF];
41822     word32   idx;
41823     const byte mySerial[8] = {1,2,3,4,5,6,7,8};
41824 
41825 #ifdef OPENSSL_EXTRA
41826     const unsigned char* pt;
41827     int                  certSz;
41828     X509*                x509;
41829     X509_NAME*           x509name;
41830     X509_NAME_ENTRY*     entry;
41831     ASN1_STRING*         entryValue;
41832 #endif
41833 
41834     printf(testingFmt, "wolfSSL Certs with DC");
41835 
41836     XMEMSET(&name, 0, sizeof(CertName));
41837 
41838     /* set up cert name */
41839     XMEMCPY(name.country, "US", sizeof("US"));
41840     name.countryEnc = CTC_PRINTABLE;
41841     XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
41842     name.stateEnc = CTC_UTF8;
41843     XMEMCPY(name.locality, "Portland", sizeof("Portland"));
41844     name.localityEnc = CTC_UTF8;
41845     XMEMCPY(name.sur, "Test", sizeof("Test"));
41846     name.surEnc = CTC_UTF8;
41847     XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
41848     name.orgEnc = CTC_UTF8;
41849     XMEMCPY(name.unit, "Development", sizeof("Development"));
41850     name.unitEnc = CTC_UTF8;
41851     XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
41852     name.commonNameEnc = CTC_UTF8;
41853     XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
41854     name.serialDevEnc = CTC_PRINTABLE;
41855 #ifdef WOLFSSL_MULTI_ATTRIB
41856     #if CTC_MAX_ATTRIB > 2
41857     {
41858         NameAttrib* n;
41859         n = &name.name[0];
41860         n->id   = ASN_DOMAIN_COMPONENT;
41861         n->type = CTC_UTF8;
41862         n->sz   = sizeof("com");
41863         XMEMCPY(n->value, "com", sizeof("com"));
41864 
41865         n = &name.name[1];
41866         n->id   = ASN_DOMAIN_COMPONENT;
41867         n->type = CTC_UTF8;
41868         n->sz   = sizeof("wolfssl");
41869         XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
41870     }
41871     #endif
41872 #endif /* WOLFSSL_MULTI_ATTRIB */
41873 
41874     AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
41875 #ifndef HAVE_FIPS
41876     AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
41877 #else
41878     AssertIntEQ(wc_InitRng(&rng), 0);
41879 #endif
41880 
41881     /* load test RSA key */
41882     idx = 0;
41883 #if defined(USE_CERT_BUFFERS_1024)
41884     AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
41885                 sizeof_server_key_der_1024), 0);
41886 #elif defined(USE_CERT_BUFFERS_2048)
41887     AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
41888                 sizeof_server_key_der_2048), 0);
41889 #else
41890     /* error case, no RSA key loaded, happens later */
41891     (void)idx;
41892 #endif
41893 
41894     XMEMSET(&cert, 0 , sizeof(Cert));
41895     AssertIntEQ(wc_InitCert(&cert), 0);
41896 
41897     XMEMCPY(&cert.subject, &name, sizeof(CertName));
41898     XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
41899     cert.serialSz = (int)sizeof(mySerial);
41900     cert.isCA     = 1;
41901 #ifndef NO_SHA256
41902     cert.sigType = CTC_SHA256wRSA;
41903 #else
41904     cert.sigType = CTC_SHAwRSA;
41905 #endif
41906 
41907     /* add SKID from the Public Key */
41908     AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
41909 
41910     /* add AKID from the Public Key */
41911     AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
41912 
41913     ret = 0;
41914     do {
41915 #if defined(WOLFSSL_ASYNC_CRYPT)
41916         ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
41917 #endif
41918         if (ret >= 0) {
41919             ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
41920         }
41921     } while (ret == WC_PENDING_E);
41922     AssertIntGT(ret, 0);
41923 
41924 #ifdef OPENSSL_EXTRA
41925     /* der holds a certificate with DC's now check X509 parsing of it */
41926     certSz = ret;
41927     pt = der;
41928     AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
41929     AssertNotNull(x509name = X509_get_subject_name(x509));
41930 #ifdef WOLFSSL_MULTI_ATTRIB
41931     AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
41932                     -1)), 5);
41933     AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
41934                     idx)), 6);
41935     AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
41936                     idx)), -1);
41937 #endif /* WOLFSSL_MULTI_ATTRIB */
41938 
41939     /* compare DN at index 0 */
41940     AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
41941     AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
41942     AssertIntEQ(ASN1_STRING_length(entryValue), 2);
41943     AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
41944 
41945 #ifdef WOLFSSL_MULTI_ATTRIB
41946     /* get first and second DC and compare result */
41947     AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
41948                     -1)), 5);
41949     AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
41950     AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
41951     AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
41952 
41953     AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
41954                    idx)), 6);
41955     AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
41956     AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
41957     AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
41958 #endif /* WOLFSSL_MULTI_ATTRIB */
41959 
41960     /* try invalid index locations for regression test and sanity check */
41961     AssertNull(entry = X509_NAME_get_entry(x509name, 11));
41962     AssertNull(entry = X509_NAME_get_entry(x509name, 20));
41963 
41964     X509_free(x509);
41965 #endif /* OPENSSL_EXTRA */
41966 
41967     wc_FreeRsaKey(&key);
41968     wc_FreeRng(&rng);
41969     printf(resultFmt, passed);
41970 #endif
41971 }
41972 
41973 
41974 static void test_wolfSSL_X509_get_version(void){
41975 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
41976     WOLFSSL_X509 *x509;
41977 
41978     printf(testingFmt, "wolfSSL_X509_get_version()");
41979 
41980     x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
41981     AssertNotNull(x509);
41982     AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
41983     wolfSSL_X509_free(x509);
41984 
41985     printf(resultFmt, passed);
41986 #endif
41987 }
41988 
41989 static void test_wolfSSL_DES_ncbc(void){
41990 #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
41991     const_DES_cblock myDes;
41992     DES_cblock iv = {1};
41993     DES_key_schedule key = {0};
41994     unsigned char msg[] = "hello wolfssl";
41995     unsigned char out[DES_BLOCK_SIZE * 2] = {0};
41996     unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
41997 
41998     unsigned char exp[]  = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
41999     unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
42000 
42001     printf(testingFmt, "wolfSSL_DES_ncbc()");
42002 
42003     /* partial block test */
42004     DES_set_key(&key, &myDes);
42005     DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
42006     AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
42007     AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
42008 
42009     DES_set_key(&key, &myDes);
42010     XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
42011     *((byte*)&iv) = 1;
42012     DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
42013     AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
42014     AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
42015 
42016     /* full block test */
42017     DES_set_key(&key, &myDes);
42018     XMEMSET(pln, 0, DES_BLOCK_SIZE);
42019     XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
42020     *((byte*)&iv) = 1;
42021     DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
42022     AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
42023     AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
42024 
42025     DES_set_key(&key, &myDes);
42026     XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
42027     *((byte*)&iv) = 1;
42028     DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
42029     AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
42030     AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
42031 
42032     printf(resultFmt, passed);
42033 #endif
42034 }
42035 
42036 static void test_wolfSSL_AES_cbc_encrypt(void)
42037 {
42038 #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
42039     AES_KEY aes;
42040     AES_KEY* aesN = NULL;
42041     size_t len = 0;
42042     size_t lenB = 0;
42043     int keySz0 = 0;
42044     int keySzN = -1;
42045     byte out[AES_BLOCK_SIZE] = {0};
42046     byte* outN = NULL;
42047 
42048     /* Test vectors retrieved from:
42049      *   <begin URL>
42050      *       https://csrc.nist.gov/
42051      *       CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
42052      *       documents/aes/KAT_AES.zip
42053      *   </end URL>
42054      */
42055     const byte* pt128N  = NULL;
42056     byte* key128N       = NULL;
42057     byte* iv128N        = NULL;
42058     byte iv128tmp[AES_BLOCK_SIZE] = {0};
42059 
42060     const byte pt128[]  = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
42061                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
42062 
42063     const byte ct128[]  = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
42064                             0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
42065 
42066     const byte iv128[]  = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
42067                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
42068 
42069     byte key128[]       = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
42070                             0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
42071 
42072 
42073     len = sizeof(pt128);
42074 
42075     #define STRESS_T(a, b, c, d, e, f, g, h, i) \
42076             wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
42077             AssertIntNE(XMEMCMP(b, g, h), i)
42078 
42079     #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
42080 
42081     printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
42082     STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
42083     STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
42084 
42085     wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, 1);
42086     AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
42087     wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, 1);
42088     AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
42089 
42090     STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
42091     printf(resultFmt, "Stress Tests: passed");
42092 
42093     printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
42094     AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
42095     AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
42096     AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
42097     AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
42098     printf(resultFmt, "Stress Tests: passed");
42099 
42100     printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
42101     AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
42102     AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
42103     AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
42104     AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
42105     printf(resultFmt, "Stress Tests: passed");
42106 
42107   #ifdef WOLFSSL_AES_128
42108 
42109     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
42110     XMEMSET(out, 0, AES_BLOCK_SIZE);
42111     RESET_IV(iv128tmp, iv128);
42112 
42113     AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
42114     wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, 1);
42115     AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
42116     printf(resultFmt, "passed");
42117 
42118     #ifdef HAVE_AES_DECRYPT
42119 
42120     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
42121     XMEMSET(out, 0, AES_BLOCK_SIZE);
42122     RESET_IV(iv128tmp, iv128);
42123     len = sizeof(ct128);
42124 
42125     AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
42126     wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, 0);
42127     AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
42128     printf(resultFmt, "passed");
42129 
42130     #endif
42131 
42132   #endif /* WOLFSSL_AES_128 */
42133   #ifdef WOLFSSL_AES_192
42134     /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
42135      * Appendix F.2.3  */
42136 
42137     byte iv192tmp[AES_BLOCK_SIZE] = {0};
42138 
42139     const byte pt192[]  = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
42140                             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
42141 
42142     const byte ct192[]  = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
42143                             0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
42144 
42145     const byte iv192[]  = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
42146                             0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
42147 
42148     byte key192[]       = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
42149                             0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
42150                             0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
42151 
42152     len = sizeof(pt192);
42153 
42154     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
42155     XMEMSET(out, 0, AES_BLOCK_SIZE);
42156     RESET_IV(iv192tmp, iv192);
42157 
42158     AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
42159     wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, 1);
42160     AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
42161     printf(resultFmt, "passed");
42162 
42163     #ifdef HAVE_AES_DECRYPT
42164 
42165     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
42166     len = sizeof(ct192);
42167     RESET_IV(iv192tmp, iv192);
42168     XMEMSET(out, 0, AES_BLOCK_SIZE);
42169 
42170     AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
42171     wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, 0);
42172     AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
42173     printf(resultFmt, "passed");
42174 
42175     #endif
42176   #endif /* WOLFSSL_AES_192 */
42177   #ifdef WOLFSSL_AES_256
42178     /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
42179      * Appendix F.2.5  */
42180     byte iv256tmp[AES_BLOCK_SIZE] = {0};
42181 
42182     const byte pt256[]  = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
42183                             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
42184 
42185     const byte ct256[]  = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
42186                             0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
42187 
42188     const byte iv256[]  = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
42189                             0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
42190 
42191     byte key256[]       = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
42192                             0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
42193                             0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
42194                             0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
42195 
42196 
42197     len = sizeof(pt256);
42198 
42199     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
42200     XMEMSET(out, 0, AES_BLOCK_SIZE);
42201     RESET_IV(iv256tmp, iv256);
42202 
42203     AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
42204     wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, 1);
42205     AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
42206     printf(resultFmt, "passed");
42207 
42208     #ifdef HAVE_AES_DECRYPT
42209 
42210     printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
42211     len = sizeof(ct256);
42212     RESET_IV(iv256tmp, iv256);
42213     XMEMSET(out, 0, AES_BLOCK_SIZE);
42214 
42215     AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
42216     wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, 0);
42217     AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
42218     printf(resultFmt, "passed");
42219 
42220     #endif
42221 
42222     #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
42223     byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
42224     byte wrapPlain[sizeof(key256)] = { 0 };
42225     byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
42226     printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
42227     AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
42228     AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
42229             15), WOLFSSL_FAILURE);
42230     AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
42231             sizeof(key256)), sizeof(wrapCipher));
42232     printf(resultFmt, "passed");
42233     printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
42234     AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
42235     AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
42236             23), WOLFSSL_FAILURE);
42237     AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
42238             sizeof(wrapCipher)), sizeof(wrapPlain));
42239     AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
42240     printf(resultFmt, "passed");
42241     XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
42242     XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
42243     printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
42244     AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
42245     AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
42246             sizeof(key256)), sizeof(wrapCipher));
42247     printf(resultFmt, "passed");
42248     printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
42249     AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
42250     AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
42251             sizeof(wrapCipher)), sizeof(wrapPlain));
42252     AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
42253     printf(resultFmt, "passed");
42254     #endif /* HAVE_AES_KEYWRAP */
42255   #endif /* WOLFSSL_AES_256 */
42256 #endif
42257 }
42258 
42259 static void test_wolfSSL_CRYPTO_cts128(void)
42260 {
42261 #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
42262     && defined(HAVE_CTS)
42263     byte tmp[64]; /* Largest vector size */
42264     /* Test vectors taken form RFC3962 Appendix B */
42265     const testVector vects[] = {
42266         {
42267             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42268             "\x20",
42269             "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
42270             "\x97",
42271             17, 17
42272         },
42273         {
42274             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42275             "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
42276             "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
42277             "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
42278             31, 31
42279         },
42280         {
42281             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42282             "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
42283             "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
42284             "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
42285             32, 32
42286         },
42287         {
42288             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42289             "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
42290             "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
42291             "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
42292             "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
42293             "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
42294             47, 47
42295         },
42296         {
42297             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42298             "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
42299             "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
42300             "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
42301             "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
42302             "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
42303             48, 48
42304         },
42305         {
42306             "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
42307             "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
42308             "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
42309             "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
42310             "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
42311             "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
42312             "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
42313             "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
42314             64, 64
42315         }
42316     };
42317     byte keyBytes[AES_128_KEY_SIZE] = {
42318         0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
42319         0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
42320     };
42321     size_t i;
42322     XMEMSET(tmp, 0, sizeof(tmp));
42323     for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
42324         AES_KEY encKey;
42325         AES_KEY decKey;
42326         byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
42327         XMEMSET(iv, 0, sizeof(iv));
42328         AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
42329         AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
42330         AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
42331                 tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
42332                 vects[i].outLen);
42333         AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
42334         XMEMSET(iv, 0, sizeof(iv));
42335         AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
42336                 tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
42337                 vects[i].inLen);
42338         AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
42339     }
42340 #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
42341 }
42342 
42343 #if defined(OPENSSL_ALL)
42344 #if !defined(NO_ASN)
42345 static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
42346 {
42347 #if !defined(NO_RSA)
42348     WOLFSSL_X509* x509;
42349     WOLFSSL_X509_NAME* subject;
42350     WOLFSSL_X509_NAME_ENTRY* e;
42351     WOLFSSL_ASN1_STRING* a;
42352     FILE* file;
42353     int idx = 0;
42354     char targetOutput[16] = "www.wolfssl.com";
42355     unsigned char* actual_output;
42356     int len = 0;
42357     int result = 0;
42358 
42359     AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
42360     AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
42361     fclose(file);
42362 
42363     printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
42364     AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
42365     AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
42366                     NID_commonName, -1)), 5);
42367     AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
42368     AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
42369     AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
42370     result = strncmp((const char*)actual_output, targetOutput, len);
42371     AssertIntEQ(result, 0);
42372     printf(resultFmt, result == 0 ? passed : failed);
42373 
42374     printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
42375     AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
42376             WOLFSSL_FATAL_ERROR);
42377     printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
42378 
42379     printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
42380     AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
42381             WOLFSSL_FATAL_ERROR);
42382     printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
42383 
42384     printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
42385     AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
42386             WOLFSSL_FATAL_ERROR);
42387     printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
42388 
42389     wolfSSL_X509_free(x509);
42390     XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
42391 #endif
42392 }
42393 
42394 static void test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
42395 {
42396     ASN1_STRING* asn1str_test;
42397     ASN1_STRING* asn1str_answer;
42398     /* Each character is encoded using 4 bytes */
42399     char input[] = {
42400             0, 0, 0, 'T',
42401             0, 0, 0, 'e',
42402             0, 0, 0, 's',
42403             0, 0, 0, 't',
42404     };
42405     char output[] = "Test";
42406 
42407     printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
42408 
42409     AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
42410     AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
42411     AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
42412 
42413     AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
42414     AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
42415 
42416     AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
42417 
42418     ASN1_STRING_free(asn1str_test);
42419     ASN1_STRING_free(asn1str_answer);
42420 
42421     printf(resultFmt, "passed");
42422 }
42423 #endif /* !defined(NO_ASN) */
42424 
42425 static void test_wolfSSL_sk_CIPHER_description(void)
42426 {
42427 #if !defined(NO_RSA)
42428     const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
42429     int i,j,k;
42430     int numCiphers = 0;
42431     const SSL_METHOD *method = NULL;
42432     const SSL_CIPHER *cipher = NULL;
42433     STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
42434     SSL_CTX *ctx = NULL;
42435     SSL *ssl = NULL;
42436     char buf[256];
42437     char test_str[9] = "0000000";
42438     const char badStr[] = "unknown";
42439     const char certPath[] = "./certs/client-cert.pem";
42440     XMEMSET(buf, 0, sizeof(buf));
42441 
42442     printf(testingFmt, "wolfSSL_sk_CIPHER_description");
42443 
42444     AssertNotNull(method = TLSv1_2_client_method());
42445     AssertNotNull(ctx = SSL_CTX_new(method));
42446 
42447     SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
42448     SSL_CTX_set_verify_depth(ctx, 4);
42449 
42450     SSL_CTX_set_options(ctx, flags);
42451     AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
42452                 WOLFSSL_SUCCESS);
42453 
42454     AssertNotNull(ssl = SSL_new(ctx));
42455     /* SSL_get_ciphers returns a stack of all configured ciphers
42456      * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
42457      */
42458     AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
42459 
42460     /* loop through the amount of supportedCiphers */
42461     numCiphers = sk_num(supportedCiphers);
42462     for (i = 0; i < numCiphers; ++i) {
42463 
42464         /* sk_value increments "sk->data.cipher->cipherOffset".
42465          * wolfSSL_sk_CIPHER_description sets the description for
42466          * the cipher based on the provided offset.
42467          */
42468 
42469         if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
42470             SSL_CIPHER_description(cipher, buf, sizeof(buf));
42471         }
42472 
42473         /* Search cipher description string for "unknown" descriptor */
42474         for (j = 0; j < (int)XSTRLEN(buf); j++) {
42475             k = 0;
42476             while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
42477                 test_str[k] = badStr[k];
42478                 j++;
42479                 k++;
42480             }
42481         }
42482         /* Fail if test_str == badStr == "unknown" */
42483         AssertStrNE(test_str,badStr);
42484     }
42485     SSL_free(ssl);
42486     SSL_CTX_free(ctx);
42487 
42488     printf(resultFmt, passed);
42489 #endif
42490 }
42491 
42492 static void test_wolfSSL_get_ciphers_compat(void)
42493 {
42494 #if !defined(NO_RSA)
42495     const SSL_METHOD *method = NULL;
42496     const char certPath[] = "./certs/client-cert.pem";
42497     STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
42498     SSL_CTX *ctx = NULL;
42499     WOLFSSL *ssl = NULL;
42500     const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
42501 
42502     printf(testingFmt, "wolfSSL_get_ciphers_compat");
42503     method = SSLv23_client_method();
42504     AssertNotNull(method);
42505     ctx = SSL_CTX_new(method);
42506     AssertNotNull(ctx);
42507 
42508     SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
42509     SSL_CTX_set_verify_depth(ctx, 4);
42510 
42511     SSL_CTX_set_options(ctx, flags);
42512     AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
42513                 WOLFSSL_SUCCESS);
42514 
42515     AssertNotNull(ssl = SSL_new(ctx));
42516 
42517     /* Test Bad NULL input */
42518     AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
42519     /* Test for Good input */
42520     AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
42521     /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
42522      * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
42523 
42524     SSL_free(ssl);
42525     SSL_CTX_free(ctx);
42526 
42527     printf(resultFmt, passed);
42528 #endif
42529 }
42530 
42531 static void test_wolfSSL_X509_PUBKEY_get(void)
42532 {
42533     WOLFSSL_X509_PUBKEY pubkey;
42534     WOLFSSL_X509_PUBKEY* key;
42535     WOLFSSL_EVP_PKEY evpkey ;
42536     WOLFSSL_EVP_PKEY* evpPkey;
42537     WOLFSSL_EVP_PKEY* retEvpPkey;
42538 
42539     XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
42540     XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
42541 
42542     key = &pubkey;
42543     evpPkey = &evpkey;
42544 
42545     evpPkey->type = WOLFSSL_SUCCESS;
42546     key->pkey = evpPkey;
42547 
42548     printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
42549     AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
42550     AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
42551 
42552     AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
42553 
42554     key->pkey = NULL;
42555     AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
42556     printf(resultFmt,retEvpPkey == NULL ? passed : failed);
42557 }
42558 
42559 static void test_wolfSSL_d2i_DHparams(void)
42560 {
42561 #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
42562 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
42563     FILE* f = NULL;
42564     unsigned char buf[4096];
42565     const unsigned char* pt = buf;
42566 #ifdef HAVE_FFDHE_2048
42567     const char* params1 = "./certs/dh2048.der";
42568 #endif
42569 #ifdef HAVE_FFDHE_3072
42570     const char* params2 = "./certs/dh3072.der";
42571 #endif
42572     long len = 0;
42573     WOLFSSL_DH* dh = NULL;
42574     XMEMSET(buf, 0, sizeof(buf));
42575 
42576     /* Test 2048 bit parameters */
42577 #ifdef HAVE_FFDHE_2048
42578     printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
42579     f = XFOPEN(params1, "rb");
42580     AssertTrue(f != XBADFILE);
42581     len = (long)XFREAD(buf, 1, sizeof(buf), f);
42582     XFCLOSE(f);
42583 
42584     /* Valid case */
42585     AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
42586     AssertNotNull(dh->p);
42587     AssertNotNull(dh->g);
42588     AssertTrue(pt != buf);
42589 #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
42590     AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
42591 #endif
42592     AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
42593 
42594     /* Invalid cases */
42595     AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
42596     AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
42597     AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
42598 
42599     DH_free(dh);
42600     printf(resultFmt, passed);
42601 
42602     *buf = 0;
42603     pt = buf;
42604 #endif /* HAVE_FFDHE_2048 */
42605 
42606     /* Test 3072 bit parameters */
42607 #ifdef HAVE_FFDHE_3072
42608     printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
42609     f = XFOPEN(params2, "rb");
42610     AssertTrue(f != XBADFILE);
42611     len = (long)XFREAD(buf, 1, sizeof(buf), f);
42612     XFCLOSE(f);
42613 
42614     /* Valid case */
42615     AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
42616     AssertNotNull(dh->p);
42617     AssertNotNull(dh->g);
42618     AssertTrue(pt != buf);
42619     AssertIntEQ(DH_generate_key(dh), 1);
42620 
42621     /* Invalid cases */
42622     AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
42623     AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
42624 
42625     DH_free(dh);
42626     printf(resultFmt, passed);
42627 #endif /* HAVE_FFDHE_3072 */
42628 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
42629 #endif /* !NO_DH */
42630 }
42631 
42632 static void test_wolfSSL_i2d_DHparams(void)
42633 {
42634 #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
42635 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
42636     FILE* f;
42637     unsigned char buf[4096];
42638     const unsigned char* pt = buf;
42639     unsigned char* pt2 = buf;
42640 #ifdef HAVE_FFDHE_2048
42641     const char* params1 = "./certs/dh2048.der";
42642 #endif
42643 #ifdef HAVE_FFDHE_3072
42644     const char* params2 = "./certs/dh3072.der";
42645 #endif
42646     long len;
42647     WOLFSSL_DH* dh;
42648 
42649     /* Test 2048 bit parameters */
42650 #ifdef HAVE_FFDHE_2048
42651     printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
42652     f = XFOPEN(params1, "rb");
42653     AssertTrue(f != XBADFILE);
42654     len = (long)XFREAD(buf, 1, sizeof(buf), f);
42655     XFCLOSE(f);
42656 
42657     /* Valid case */
42658     AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
42659     AssertTrue(pt != buf);
42660     AssertIntEQ(DH_generate_key(dh), 1);
42661     AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
42662 
42663     /* Invalid case */
42664     AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
42665 
42666     /* Return length only */
42667     AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
42668 
42669     DH_free(dh);
42670     printf(resultFmt, passed);
42671 
42672     *buf = 0;
42673     pt = buf;
42674     pt2 = buf;
42675 #endif
42676 
42677     /* Test 3072 bit parameters */
42678 #ifdef HAVE_FFDHE_3072
42679     printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
42680     f = XFOPEN(params2, "rb");
42681     AssertTrue(f != XBADFILE);
42682     len = (long)XFREAD(buf, 1, sizeof(buf), f);
42683     XFCLOSE(f);
42684 
42685     /* Valid case */
42686     AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
42687     AssertTrue(pt != buf);
42688     AssertIntEQ(DH_generate_key(dh), 1);
42689     AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
42690 
42691     /* Invalid case */
42692     AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
42693 
42694     /* Return length only */
42695     AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
42696 
42697     DH_free(dh);
42698     printf(resultFmt, passed);
42699 #endif
42700 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
42701 #endif
42702 }
42703 
42704 static void test_wolfSSL_EC_KEY_dup(void)
42705 {
42706 #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
42707     defined(OPENSSL_EXTRA_X509_SMALL))
42708 
42709     WOLFSSL_EC_KEY* ecKey;
42710     WOLFSSL_EC_KEY* dupKey;
42711     ecc_key* srcKey;
42712     ecc_key* destKey;
42713 
42714     printf(testingFmt, "wolfSSL_EC_KEY_dup()");
42715 
42716     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
42717     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
42718 
42719     /* Valid cases */
42720     AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42721     AssertIntEQ(EC_KEY_check_key(dupKey), 1);
42722 
42723     /* Compare pubkey */
42724     srcKey = (ecc_key*)ecKey->internal;
42725     destKey = (ecc_key*)dupKey->internal;
42726     AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
42727 
42728     /* compare EC_GROUP */
42729     AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
42730 
42731     /* compare EC_POINT */
42732     AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
42733                 dupKey->pub_key, NULL), MP_EQ);
42734 
42735     /* compare BIGNUM */
42736     AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
42737     wolfSSL_EC_KEY_free(dupKey);
42738 
42739     /* Invalid cases */
42740     /* NULL key */
42741     AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
42742     /* NULL ecc_key */
42743     wc_ecc_free((ecc_key*)ecKey->internal);
42744     XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
42745     ecKey->internal = NULL; /* Set ecc_key to NULL */
42746     AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42747     wolfSSL_EC_KEY_free(ecKey);
42748     wolfSSL_EC_KEY_free(dupKey);
42749 
42750     /* NULL Group */
42751     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
42752     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
42753     wolfSSL_EC_GROUP_free(ecKey->group);
42754     ecKey->group = NULL; /* Set group to NULL */
42755     AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42756     wolfSSL_EC_KEY_free(ecKey);
42757     wolfSSL_EC_KEY_free(dupKey);
42758 
42759     /* NULL public key */
42760     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
42761     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
42762     wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
42763     ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
42764     AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42765 
42766     wolfSSL_EC_POINT_free(ecKey->pub_key);
42767     ecKey->pub_key = NULL; /* Set pub_key to NULL */
42768     AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42769     wolfSSL_EC_KEY_free(ecKey);
42770     wolfSSL_EC_KEY_free(dupKey);
42771 
42772     /* NULL private key */
42773     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
42774     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
42775 
42776     wolfSSL_BN_free(ecKey->priv_key);
42777     ecKey->priv_key = NULL; /* Set priv_key to NULL */
42778     AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
42779 
42780     wolfSSL_EC_KEY_free(ecKey);
42781     wolfSSL_EC_KEY_free(dupKey);
42782 
42783     printf(resultFmt, passed);
42784 #endif
42785 }
42786 
42787 static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
42788 {
42789 #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
42790     DSA       *dsa  = NULL;
42791     DSA       *setDsa  = NULL;
42792     EVP_PKEY  *pkey = NULL;
42793     EVP_PKEY  *set1Pkey = NULL;
42794 
42795     SHA_CTX sha;
42796     byte    signature[DSA_SIG_SIZE];
42797     byte    hash[WC_SHA_DIGEST_SIZE];
42798     word32  bytes;
42799     int     answer;
42800 #ifdef USE_CERT_BUFFERS_1024
42801     const unsigned char* dsaKeyDer = dsa_key_der_1024;
42802     int dsaKeySz  = sizeof_dsa_key_der_1024;
42803     byte    tmp[ONEK_BUF];
42804     XMEMSET(tmp, 0, sizeof(tmp));
42805     XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
42806     bytes = dsaKeySz;
42807 #elif defined(USE_CERT_BUFFERS_2048)
42808     const unsigned char* dsaKeyDer = dsa_key_der_2048;
42809     int dsaKeySz  = sizeof_dsa_key_der_2048;
42810     byte    tmp[TWOK_BUF];
42811     XMEMSET(tmp, 0, sizeof(tmp));
42812     XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
42813     bytes = dsaKeySz;
42814 #else
42815     byte    tmp[TWOK_BUF];
42816     const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
42817     int dsaKeySz;
42818     XMEMSET(tmp, 0, sizeof(tmp));
42819     XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
42820     if (fp == XBADFILE) {
42821         return WOLFSSL_BAD_FILE;
42822     }
42823     dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
42824     XFCLOSE(fp);
42825 #endif /* END USE_CERT_BUFFERS_1024 */
42826 
42827     printf(testingFmt,
42828            "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
42829 
42830     /* Create hash to later Sign and Verify */
42831     AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
42832     AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
42833     AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
42834 
42835     /* Initialize pkey with der format dsa key */
42836     AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
42837                 &dsaKeyDer ,(long)dsaKeySz));
42838 
42839     /* Test wolfSSL_EVP_PKEY_get1_DSA */
42840     /* Should Fail: NULL argument */
42841     AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
42842     AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
42843     /* Should Pass: Initialized pkey argument */
42844     AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
42845     AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
42846 
42847 #ifdef USE_CERT_BUFFERS_1024
42848     AssertIntEQ(DSA_bits(dsa), 1024);
42849 #else
42850     AssertIntEQ(DSA_bits(dsa), 2048);
42851 #endif
42852 
42853     /* Sign */
42854     AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
42855     /* Verify. */
42856     AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
42857                 WOLFSSL_SUCCESS);
42858 
42859     /* Test wolfSSL_EVP_PKEY_set1_DSA */
42860     /* Should Fail: set1Pkey not initialized */
42861     AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
42862 
42863     /* Initialize set1Pkey */
42864     set1Pkey = EVP_PKEY_new();
42865 
42866     /* Should Fail Verify: setDsa not initialized from set1Pkey */
42867     AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
42868                 WOLFSSL_SUCCESS);
42869 
42870     /* Should Pass: set dsa into set1Pkey */
42871     AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
42872     printf(resultFmt, passed);
42873 
42874     DSA_free(dsa);
42875     DSA_free(setDsa);
42876     EVP_PKEY_free(pkey);
42877     EVP_PKEY_free(set1Pkey);
42878 #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
42879 } /* END test_EVP_PKEY_set1_get1_DSA */
42880 
42881 static void test_wolfSSL_DSA_SIG(void)
42882 {
42883 #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
42884     !defined(HAVE_FIPS)
42885     DSA          *dsa  = NULL;
42886     DSA          *dsa2 = NULL;
42887     DSA_SIG      *sig  = NULL;
42888     const BIGNUM *p    = NULL;
42889     const BIGNUM *q    = NULL;
42890     const BIGNUM *g    = NULL;
42891     const BIGNUM *pub  = NULL;
42892     const BIGNUM *priv = NULL;
42893     const byte digest[WC_SHA_DIGEST_SIZE] = {0};
42894 
42895     printf(testingFmt, "wolfSSL_DSA_SIG");
42896 
42897     AssertNotNull(dsa = DSA_generate_parameters(2048,
42898             NULL, 0, NULL, NULL, NULL, NULL));
42899     DSA_free(dsa);
42900     AssertNotNull(dsa = DSA_new());
42901     AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
42902             NULL, 0, NULL, NULL, NULL), 1);
42903     AssertIntEQ(DSA_generate_key(dsa), 1);
42904     DSA_get0_pqg(dsa, &p, &q, &g);
42905     DSA_get0_key(dsa, &pub, &priv);
42906     AssertNotNull(p    = BN_dup(p));
42907     AssertNotNull(q    = BN_dup(q));
42908     AssertNotNull(g    = BN_dup(g));
42909     AssertNotNull(pub  = BN_dup(pub));
42910     AssertNotNull(priv = BN_dup(priv));
42911 
42912     AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
42913     AssertNotNull(dsa2 = DSA_new());
42914     AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
42915     AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
42916     AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
42917 
42918     printf(resultFmt, passed);
42919 
42920     DSA_free(dsa);
42921     DSA_free(dsa2);
42922     DSA_SIG_free(sig);
42923 #endif
42924 }
42925 
42926 static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
42927 {
42928 #ifdef HAVE_ECC
42929     WOLFSSL_EC_KEY  *ecKey  = NULL;
42930     WOLFSSL_EC_KEY  *ecGet1  = NULL;
42931     EVP_PKEY  *pkey = NULL;
42932 
42933     printf(testingFmt,
42934            "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
42935     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
42936     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
42937 
42938     /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
42939     AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
42940     AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
42941     /* Should fail since ecKey is empty */
42942     AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
42943     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
42944     AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
42945 
42946     /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
42947     AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
42948     AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
42949 
42950     wolfSSL_EC_KEY_free(ecKey);
42951     wolfSSL_EC_KEY_free(ecGet1);
42952     EVP_PKEY_free(pkey);
42953 
42954     /* PASSED */
42955     printf(resultFmt, passed);
42956 #endif /* HAVE_ECC */
42957 } /* END test_EVP_PKEY_set1_get1_EC_KEY */
42958 
42959 static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
42960 {
42961 #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
42962 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
42963 #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
42964     DH       *dh    = NULL;
42965     DH       *setDh = NULL;
42966     EVP_PKEY *pkey  = NULL;
42967 
42968     FILE* f = NULL;
42969     unsigned char buf[4096];
42970     const unsigned char* pt = buf;
42971     const char* dh2048 = "./certs/dh2048.der";
42972     long len = 0;
42973     int code = -1;
42974 
42975     printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
42976 
42977     XMEMSET(buf, 0, sizeof(buf));
42978 
42979     f = XFOPEN(dh2048, "rb");
42980     AssertTrue(f != XBADFILE);
42981     len = (long)XFREAD(buf, 1, sizeof(buf), f);
42982     XFCLOSE(f);
42983 
42984     /* Load dh2048.der into DH with internal format */
42985     AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
42986 
42987     AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
42988     AssertIntEQ(code, 0);
42989     code = -1;
42990 
42991     pkey = wolfSSL_EVP_PKEY_new();
42992 
42993     /* Set DH into PKEY */
42994     AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
42995 
42996     /* Get DH from PKEY */
42997     AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
42998 
42999     AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
43000     AssertIntEQ(code, 0);
43001 
43002     EVP_PKEY_free(pkey);
43003     DH_free(setDh);
43004     DH_free(dh);
43005     printf(resultFmt, passed);
43006 #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
43007 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
43008 #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
43009 } /* END test_EVP_PKEY_set1_get1_DH */
43010 
43011 static void test_wolfSSL_CTX_ctrl(void)
43012 {
43013 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
43014     !defined(NO_FILESYSTEM) && !defined(NO_RSA)
43015     char caFile[] = "./certs/client-ca.pem";
43016     char clientFile[] = "./certs/client-cert.pem";
43017     SSL_CTX* ctx;
43018     X509* x509 = NULL;
43019 #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
43020     byte buf[6000];
43021     char file[] = "./certs/dsaparams.pem";
43022     XFILE f;
43023     int  bytes;
43024     BIO* bio;
43025     DSA* dsa;
43026     DH*  dh;
43027 #endif
43028 #ifdef HAVE_ECC
43029     WOLFSSL_EC_KEY* ecKey;
43030 #endif
43031     printf(testingFmt, "wolfSSL_CTX_ctrl");
43032 
43033     AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
43034 
43035     x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
43036     AssertNotNull(x509);
43037     AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
43038 
43039     x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
43040     AssertNotNull(x509);
43041 
43042 #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
43043     /* Initialize DH */
43044     f = XFOPEN(file, "rb");
43045     AssertTrue((f != XBADFILE));
43046     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
43047     XFCLOSE(f);
43048 
43049     bio = BIO_new_mem_buf((void*)buf, bytes);
43050     AssertNotNull(bio);
43051 
43052     dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
43053     AssertNotNull(dsa);
43054 
43055     dh = wolfSSL_DSA_dup_DH(dsa);
43056     AssertNotNull(dh);
43057 #endif
43058 #ifdef HAVE_ECC
43059     /* Initialize WOLFSSL_EC_KEY */
43060     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
43061     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
43062 #endif
43063 
43064 #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
43065     /* additional test of getting EVP_PKEY key size from X509
43066      * Do not run with user RSA because wolfSSL_RSA_size is not currently
43067      * allowed with user RSA */
43068     {
43069         EVP_PKEY* pkey;
43070 #if defined(HAVE_ECC)
43071         X509* ecX509;
43072 #endif /* HAVE_ECC */
43073 
43074         AssertNotNull(pkey = X509_get_pubkey(x509));
43075         /* current RSA key is 2048 bit (256 bytes) */
43076         AssertIntEQ(EVP_PKEY_size(pkey), 256);
43077 
43078         EVP_PKEY_free(pkey);
43079 
43080 #if defined(HAVE_ECC)
43081 #if defined(USE_CERT_BUFFERS_256)
43082         AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
43083                       cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
43084                       SSL_FILETYPE_ASN1));
43085 #else
43086         AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
43087                       cliEccCertFile, SSL_FILETYPE_PEM));
43088 #endif
43089         AssertNotNull(pkey = X509_get_pubkey(ecX509));
43090         /* current ECC key is 256 bit (32 bytes) */
43091         AssertIntEQ(EVP_PKEY_size(pkey), 32);
43092 
43093         X509_free(ecX509);
43094         EVP_PKEY_free(pkey);
43095 #endif /* HAVE_ECC */
43096     }
43097 #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
43098 
43099     /* Tests should fail with passed in NULL pointer */
43100     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
43101                 SSL_FAILURE);
43102 #if !defined(NO_DH) && !defined(NO_DSA)
43103     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
43104                 SSL_FAILURE);
43105 #endif
43106 #ifdef HAVE_ECC
43107     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
43108                 SSL_FAILURE);
43109 #endif
43110 
43111     /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
43112      * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
43113      */
43114     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
43115                 SSL_SUCCESS);
43116 
43117     /* Test with SSL_CTRL_OPTIONS
43118      * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
43119      */
43120     AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
43121                == SSL_OP_NO_TLSv1);
43122     AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
43123 
43124     /* Test with SSL_CTRL_SET_TMP_DH
43125      * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
43126      */
43127 #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
43128     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
43129                 SSL_SUCCESS);
43130 #endif
43131 
43132     /* Test with SSL_CTRL_SET_TMP_ECDH
43133      * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
43134      */
43135 #ifdef HAVE_ECC
43136     AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
43137                 SSL_SUCCESS);
43138 #endif
43139 
43140 #ifdef WOLFSSL_ENCRYPTED_KEYS
43141     AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
43142     AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
43143 #endif
43144 
43145      /* Test for min/max proto */
43146      #ifndef WOLFSSL_NO_TLS12
43147      AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
43148                                            0, NULL), SSL_SUCCESS);
43149      AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
43150                                            TLS1_2_VERSION, NULL), SSL_SUCCESS);
43151      AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
43152 
43153      #endif
43154      #ifdef WOLFSSL_TLS13
43155      AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
43156                                            0, NULL), SSL_SUCCESS);
43157 
43158      AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
43159                                            TLS1_3_VERSION, NULL), SSL_SUCCESS);
43160      AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
43161      #ifndef WOLFSSL_NO_TLS12
43162      AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
43163                                            TLS1_2_VERSION, NULL), SSL_SUCCESS);
43164      AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
43165      #endif
43166      #endif
43167     /* Cleanup and Pass */
43168 #if !defined(NO_DH) && !defined(NO_DSA)
43169 #ifndef NO_BIO
43170     BIO_free(bio);
43171     DSA_free(dsa);
43172     DH_free(dh);
43173 #endif
43174 #endif
43175 #ifdef HAVE_ECC
43176     wolfSSL_EC_KEY_free(ecKey);
43177 #endif
43178     SSL_CTX_free(ctx);
43179     printf(resultFmt, passed);
43180 #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
43181           !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
43182 }
43183 
43184 static void test_wolfSSL_DH_check(void)
43185 {
43186 #if !defined(NO_DH) && !defined(NO_DSA)
43187 #ifndef NO_BIO
43188     byte buf[6000];
43189     char file[] = "./certs/dsaparams.pem";
43190     XFILE f;
43191     int  bytes;
43192     BIO* bio;
43193     DSA* dsa;
43194     DH*  dh = NULL;
43195     WOLFSSL_BIGNUM* pTmp = NULL;
43196     WOLFSSL_BIGNUM* gTmp = NULL;
43197     int codes = -1;
43198 
43199     printf(testingFmt, "wolfSSL_DH_check");
43200 
43201     /* Initialize DH */
43202     f = XFOPEN(file, "rb");
43203     AssertTrue((f != XBADFILE));
43204     bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
43205     XFCLOSE(f);
43206 
43207     bio = BIO_new_mem_buf((void*)buf, bytes);
43208     AssertNotNull(bio);
43209 
43210     dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
43211     AssertNotNull(dsa);
43212 
43213     dh = wolfSSL_DSA_dup_DH(dsa);
43214     AssertNotNull(dh);
43215 
43216     /* Test assumed to be valid dh.
43217      * Should return WOLFSSL_SUCCESS
43218      * codes should be 0
43219      * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
43220      */
43221     AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
43222     AssertIntEQ(codes, 0);
43223 
43224     /* Test NULL dh: expected BAD_FUNC_ARG */
43225     AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
43226 
43227     /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
43228     pTmp = dh->p;
43229     dh->p  = NULL;
43230     AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
43231     AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
43232     /* set dh->p back to normal so it wont fail on next tests */
43233     dh->p = pTmp;
43234     pTmp = NULL;
43235 
43236     /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
43237     gTmp = dh->g;
43238     dh->g = NULL;
43239     AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
43240     AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
43241     dh->g = gTmp;
43242     gTmp = NULL;
43243 
43244     /* Cleanup and Pass Test */
43245     BIO_free(bio);
43246     DSA_free(dsa);
43247     DH_free(dh);
43248     printf(resultFmt, passed);
43249 #endif
43250 #endif /* !NO_DH  && !NO_DSA */
43251 }
43252 
43253 static void test_wolfSSL_EVP_PKEY_assign(void)
43254 {
43255 #if defined(OPENSSL_ALL)
43256     int type;
43257     WOLFSSL_EVP_PKEY* pkey;
43258 #ifndef NO_RSA
43259     WOLFSSL_RSA* rsa;
43260 #endif
43261 #ifndef NO_DSA
43262     WOLFSSL_DSA* dsa;
43263 #endif
43264 #ifdef HAVE_ECC
43265     WOLFSSL_EC_KEY* ecKey;
43266 #endif
43267 
43268     (void)pkey;
43269 
43270     printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
43271 #ifndef NO_RSA
43272     type = EVP_PKEY_RSA;
43273     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43274     AssertNotNull(rsa = wolfSSL_RSA_new());
43275     AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa),  WOLFSSL_FAILURE);
43276     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
43277     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa),    WOLFSSL_FAILURE);
43278     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa),  WOLFSSL_SUCCESS);
43279     wolfSSL_EVP_PKEY_free(pkey);
43280 #endif /* NO_RSA */
43281 
43282 #ifndef NO_DSA
43283     type = EVP_PKEY_DSA;
43284     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43285     AssertNotNull(dsa = wolfSSL_DSA_new());
43286     AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa),  WOLFSSL_FAILURE);
43287     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
43288     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa),    WOLFSSL_FAILURE);
43289     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa),  WOLFSSL_SUCCESS);
43290     wolfSSL_EVP_PKEY_free(pkey);
43291 #endif /* NO_DSA */
43292 
43293 #ifdef HAVE_ECC
43294     type = EVP_PKEY_EC;
43295     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43296     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
43297     AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
43298     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL),  WOLFSSL_FAILURE);
43299     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey),   WOLFSSL_FAILURE);
43300     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey),   WOLFSSL_FAILURE);
43301     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
43302     AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
43303     wolfSSL_EVP_PKEY_free(pkey);
43304 #endif /* HAVE_ECC */
43305 
43306     (void)type;
43307 
43308     printf(resultFmt, passed);
43309 #endif /* OPENSSL_ALL */
43310 }
43311 static void test_wolfSSL_EVP_PKEY_base_id(void)
43312 {
43313 #if defined(OPENSSL_ALL)
43314     WOLFSSL_EVP_PKEY* pkey;
43315 
43316     printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
43317 
43318     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43319 
43320     AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
43321 
43322     AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
43323 
43324     EVP_PKEY_free(pkey);
43325 
43326     printf(resultFmt, passed);
43327 #endif
43328 }
43329 static void test_wolfSSL_EVP_PKEY_id(void)
43330 {
43331 #if defined(OPENSSL_ALL)
43332     WOLFSSL_EVP_PKEY* pkey;
43333 
43334     printf(testingFmt, "wolfSSL_EVP_PKEY_id");
43335 
43336     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43337 
43338     AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
43339 
43340     AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
43341 
43342     EVP_PKEY_free(pkey);
43343 
43344     printf(resultFmt, passed);
43345 #endif
43346 }
43347 
43348 static void test_wolfSSL_EVP_PKEY_paramgen(void)
43349 {
43350 #if defined(OPENSSL_ALL) && \
43351     !defined(NO_ECC_SECP) && \
43352     /* This last bit is taken from ecc.c. It is the condition that
43353      * defines ECC256 */ \
43354     ((!defined(NO_ECC256)  || defined(HAVE_ALL_CURVES)) && \
43355             ECC_MIN_KEY_SZ <= 256)
43356     EVP_PKEY_CTX*   ctx;
43357     EVP_PKEY*       pkey = NULL;
43358 
43359     printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
43360 
43361     AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
43362     AssertIntEQ(EVP_PKEY_paramgen_init(ctx), 1);
43363     AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_X9_62_prime256v1), 1);
43364     AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE), 1);
43365     AssertIntEQ(EVP_PKEY_keygen_init(ctx), 1);
43366     AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), 1);
43367 
43368     EVP_PKEY_CTX_free(ctx);
43369     EVP_PKEY_free(pkey);
43370 
43371     printf(resultFmt, passed);
43372 #endif
43373 }
43374 
43375 static void test_wolfSSL_EVP_PKEY_keygen(void)
43376 {
43377 #if defined(OPENSSL_ALL)
43378     WOLFSSL_EVP_PKEY*   pkey;
43379     EVP_PKEY_CTX        *ctx;
43380 
43381     printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
43382 
43383     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43384     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
43385 
43386     /* Bad cases */
43387     AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
43388     AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
43389     AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
43390 
43391     /* Good case */
43392     AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
43393 
43394 
43395     EVP_PKEY_CTX_free(ctx);
43396     EVP_PKEY_free(pkey);
43397 
43398     printf(resultFmt, passed);
43399 #endif
43400 }
43401 static void test_wolfSSL_EVP_PKEY_keygen_init(void)
43402 {
43403 #if defined(OPENSSL_ALL)
43404     WOLFSSL_EVP_PKEY*   pkey;
43405     EVP_PKEY_CTX        *ctx;
43406 
43407     printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
43408 
43409     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43410     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
43411 
43412     AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
43413 
43414 
43415     EVP_PKEY_CTX_free(ctx);
43416     EVP_PKEY_free(pkey);
43417 
43418     printf(resultFmt, passed);
43419 #endif
43420 }
43421 static void test_wolfSSL_EVP_PKEY_missing_parameters(void)
43422 {
43423 #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
43424     WOLFSSL_EVP_PKEY* pkey;
43425 
43426     printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
43427 
43428     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43429 
43430     AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
43431 
43432     EVP_PKEY_free(pkey);
43433 
43434     printf(resultFmt, passed);
43435 #endif
43436 }
43437 static void test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
43438 {
43439 #if defined(OPENSSL_ALL)
43440     WOLFSSL_EVP_PKEY*   pkey;
43441     EVP_PKEY_CTX        *ctx;
43442     int                 bits = 2048;
43443 
43444 
43445     printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
43446 
43447     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
43448     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
43449 
43450     AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
43451                 WOLFSSL_SUCCESS);
43452 
43453 
43454     EVP_PKEY_CTX_free(ctx);
43455     EVP_PKEY_free(pkey);
43456 
43457     printf(resultFmt, passed);
43458 #endif
43459 }
43460 
43461 static void test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
43462 {
43463 #if defined(OPENSSL_ALL)
43464 
43465     /* This is large enough to be used for all key sizes */
43466     byte key[AES_256_KEY_SIZE] = {0};
43467     byte iv[AES_BLOCK_SIZE] = {0};
43468     int i, enumlen;
43469     EVP_CIPHER_CTX *ctx;
43470     const EVP_CIPHER *init;
43471 
43472     int enumArray[] = {
43473 
43474     #ifdef HAVE_AES_CBC
43475          NID_aes_128_cbc,
43476     #endif
43477     #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
43478         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
43479     #ifdef HAVE_AESGCM
43480          NID_aes_128_gcm,
43481     #endif
43482     #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
43483     #ifdef WOLFSSL_AES_COUNTER
43484          NID_aes_128_ctr,
43485     #endif
43486     #ifndef NO_DES3
43487          NID_des_cbc,
43488          NID_des_ede3_cbc,
43489     #endif
43490     #ifdef HAVE_IDEA
43491          NID_idea_cbc,
43492     #endif
43493     };
43494     int iv_lengths[] = {
43495 
43496     #ifdef HAVE_AES_CBC
43497          AES_BLOCK_SIZE,
43498     #endif
43499     #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
43500         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
43501     #ifdef HAVE_AESGCM
43502          GCM_NONCE_MID_SZ,
43503     #endif
43504     #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
43505     #ifdef WOLFSSL_AES_COUNTER
43506          AES_BLOCK_SIZE,
43507     #endif
43508     #ifndef NO_DES3
43509          DES_BLOCK_SIZE,
43510          DES_BLOCK_SIZE,
43511     #endif
43512     #ifdef HAVE_IDEA
43513          IDEA_BLOCK_SIZE,
43514     #endif
43515     };
43516 
43517 
43518     printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
43519     enumlen = (sizeof(enumArray)/sizeof(int));
43520     for(i = 0; i < enumlen; i++)
43521     {
43522         ctx = EVP_CIPHER_CTX_new();
43523         init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
43524 
43525         wolfSSL_EVP_CIPHER_CTX_init(ctx);
43526         AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43527 
43528         AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
43529 
43530         EVP_CIPHER_CTX_free(ctx);
43531     }
43532 
43533     printf(resultFmt, passed);
43534 #endif
43535 }
43536 static void test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
43537 {
43538 #if defined(OPENSSL_ALL) && !defined(NO_DES3)
43539     byte key[AES_256_KEY_SIZE] = {0};
43540     byte iv[AES_BLOCK_SIZE] = {0};
43541     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
43542     const EVP_CIPHER *init = EVP_des_ede3_cbc();
43543 
43544     printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
43545 
43546 
43547     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43548     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43549 
43550     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
43551 
43552     EVP_CIPHER_CTX_free(ctx);
43553     printf(resultFmt, passed);
43554 #endif
43555 }
43556 static void test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
43557 {
43558 #if defined(OPENSSL_ALL) && !defined(NO_DES3)
43559     byte key[AES_256_KEY_SIZE] = {0};
43560     byte iv[AES_BLOCK_SIZE] = {0};
43561     int keylen;
43562     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
43563     const EVP_CIPHER *init = EVP_des_ede3_cbc();
43564 
43565     printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
43566 
43567 
43568 
43569     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43570     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43571 
43572     keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
43573 
43574     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
43575                  WOLFSSL_SUCCESS);
43576 
43577     EVP_CIPHER_CTX_free(ctx);
43578     printf(resultFmt, passed);
43579 #endif
43580 }
43581 static void test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
43582 {
43583 #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(NO_DES3)
43584     byte key[DES3_KEY_SIZE] = {0};
43585     byte iv[DES_BLOCK_SIZE] = {0};
43586     int ivLen, keyLen;
43587     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
43588     const EVP_CIPHER *init = EVP_des_ede3_cbc();
43589 
43590     printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
43591 
43592     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43593     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43594 
43595     ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
43596     keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
43597 
43598     /* Bad cases */
43599     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
43600     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
43601     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
43602     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
43603     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
43604 
43605     /* Good case */
43606     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
43607 
43608 
43609     EVP_CIPHER_CTX_free(ctx);
43610     printf(resultFmt, passed);
43611 #endif
43612 }
43613 static void test_wolfSSL_EVP_PKEY_CTX_new_id(void)
43614 {
43615 #if defined(OPENSSL_ALL)
43616     WOLFSSL_ENGINE* e = NULL;
43617     int id = 0;
43618     EVP_PKEY_CTX *ctx;
43619 
43620     printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
43621 
43622     AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
43623 
43624     EVP_PKEY_CTX_free(ctx);
43625 
43626     printf(resultFmt, passed);
43627 #endif
43628 }
43629 static void test_wolfSSL_EVP_rc4(void)
43630 {
43631 #if defined(OPENSSL_ALL) && !defined(NO_RC4)
43632 
43633     printf(testingFmt, "wolfSSL_EVP_rc4");
43634 
43635     AssertNotNull(wolfSSL_EVP_rc4());
43636 
43637     printf(resultFmt, passed);
43638 #endif
43639 }
43640 static void test_wolfSSL_EVP_enc_null(void)
43641 {
43642 #if defined(OPENSSL_ALL)
43643 
43644     printf(testingFmt, "wolfSSL_EVP_enc_null");
43645 
43646     AssertNotNull(wolfSSL_EVP_enc_null());
43647 
43648     printf(resultFmt, passed);
43649 #endif
43650 }
43651 static void test_wolfSSL_EVP_rc2_cbc(void)
43652 {
43653 #if defined(OPENSSL_ALL) && defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
43654 
43655     printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
43656 
43657     AssertNull(wolfSSL_EVP_rc2_cbc());
43658 
43659     printf(resultFmt, passed);
43660 #endif
43661 }
43662 static void test_wolfSSL_EVP_mdc2(void)
43663 {
43664 #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
43665 
43666     printf(testingFmt, "wolfSSL_EVP_mdc2");
43667 
43668     AssertNull(wolfSSL_EVP_mdc2());
43669 
43670     printf(resultFmt, passed);
43671 #endif
43672 }
43673 static void test_wolfSSL_EVP_md4(void)
43674 {
43675 #if defined(OPENSSL_ALL) && !defined(NO_MD4)
43676 
43677     printf(testingFmt, "wolfSSL_EVP_md4");
43678 
43679     AssertNotNull(wolfSSL_EVP_md4());
43680 
43681     printf(resultFmt, passed);
43682 #endif
43683 }
43684 static void test_wolfSSL_EVP_aes_256_gcm(void)
43685 {
43686 #if defined(OPENSSL_ALL)
43687 
43688     printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
43689 
43690     AssertNotNull(wolfSSL_EVP_aes_256_gcm());
43691 
43692     printf(resultFmt, passed);
43693 #endif
43694 }
43695 static void test_wolfSSL_EVP_aes_192_gcm(void)
43696 {
43697 #if defined(OPENSSL_ALL)
43698 
43699     printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
43700 
43701     AssertNotNull(wolfSSL_EVP_aes_192_gcm());
43702 
43703     printf(resultFmt, passed);
43704 #endif
43705 }
43706 static void test_wolfSSL_EVP_ripemd160(void)
43707 {
43708 #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
43709 
43710     printf(testingFmt, "wolfSSL_EVP_ripemd160");
43711 
43712     AssertNull(wolfSSL_EVP_ripemd160());
43713 
43714     printf(resultFmt, passed);
43715 #endif
43716 }
43717 static void test_wolfSSL_EVP_get_digestbynid(void)
43718 {
43719 #if defined(OPENSSL_ALL)
43720 
43721     printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
43722 
43723 #ifndef NO_MD5
43724     AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
43725 #endif
43726     AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
43727     AssertNull(wolfSSL_EVP_get_digestbynid(0));
43728 
43729     printf(resultFmt, passed);
43730 #endif
43731 }
43732 static void test_wolfSSL_EVP_MD_nid(void)
43733 {
43734 #if defined(OPENSSL_ALL)
43735 
43736     printf(testingFmt, "wolfSSL_EVP_MD_nid");
43737 
43738 #ifndef NO_MD5
43739     AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
43740 #endif
43741 #ifndef NO_SHA
43742     AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
43743 #endif
43744 #ifndef NO_SHA256
43745     AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
43746 #endif
43747     AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
43748 
43749     printf(resultFmt, passed);
43750 #endif
43751 }
43752 static void test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
43753 {
43754 #if defined(HAVE_ECC) && defined(OPENSSL_ALL)
43755     WOLFSSL_EVP_PKEY*   pkey;
43756 
43757     printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
43758 
43759     AssertNotNull(pkey = EVP_PKEY_new());
43760     AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
43761     EVP_PKEY_free(pkey);
43762 
43763     printf(resultFmt, passed);
43764 #endif
43765 }
43766 static void test_wolfSSL_EVP_X_STATE(void)
43767 {
43768 #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
43769 
43770     byte key[DES3_KEY_SIZE] = {0};
43771     byte iv[DES_IV_SIZE] = {0};
43772     EVP_CIPHER_CTX *ctx;
43773     const EVP_CIPHER *init;
43774 
43775     printf(testingFmt, "wolfSSL_EVP_X_STATE");
43776 
43777     /* Bad test cases */
43778     ctx = EVP_CIPHER_CTX_new();
43779     init = EVP_des_ede3_cbc();
43780 
43781     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43782     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43783 
43784     AssertNull(wolfSSL_EVP_X_STATE(NULL));
43785     AssertNull(wolfSSL_EVP_X_STATE(ctx));
43786     EVP_CIPHER_CTX_free(ctx);
43787 
43788     /* Good test case */
43789     ctx = EVP_CIPHER_CTX_new();
43790     init = wolfSSL_EVP_rc4();
43791 
43792     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43793     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43794 
43795     AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
43796     EVP_CIPHER_CTX_free(ctx);
43797 
43798 
43799     printf(resultFmt, passed);
43800 #endif
43801 }
43802 static void test_wolfSSL_EVP_X_STATE_LEN(void)
43803 {
43804 #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
43805 
43806     byte key[DES3_KEY_SIZE] = {0};
43807     byte iv[DES_IV_SIZE] = {0};
43808     EVP_CIPHER_CTX *ctx;
43809     const EVP_CIPHER *init;
43810 
43811     printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
43812 
43813     /* Bad test cases */
43814     ctx = EVP_CIPHER_CTX_new();
43815     init = EVP_des_ede3_cbc();
43816 
43817     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43818     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43819 
43820     AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
43821     AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
43822     EVP_CIPHER_CTX_free(ctx);
43823 
43824     /* Good test case */
43825     ctx = EVP_CIPHER_CTX_new();
43826     init = wolfSSL_EVP_rc4();
43827 
43828     wolfSSL_EVP_CIPHER_CTX_init(ctx);
43829     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
43830 
43831     AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
43832     EVP_CIPHER_CTX_free(ctx);
43833 
43834 
43835 
43836     printf(resultFmt, passed);
43837 #endif
43838 }
43839 
43840 static void test_wolfSSL_EVP_CIPHER_block_size(void)
43841 {
43842 #if defined(OPENSSL_ALL)
43843 
43844 #ifdef HAVE_AES_CBC
43845     #ifdef WOLFSSL_AES_128
43846     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
43847     #endif
43848     #ifdef WOLFSSL_AES_192
43849     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
43850     #endif
43851     #ifdef WOLFSSL_AES_256
43852     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
43853     #endif
43854 #endif
43855 
43856 #ifdef HAVE_AES_GCM
43857     #ifdef WOLFSSL_AES_128
43858     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
43859     #endif
43860     #ifdef WOLFSSL_AES_192
43861     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
43862     #endif
43863     #ifdef WOLFSSL_AES_256
43864     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
43865     #endif
43866 #endif
43867 
43868 #ifdef WOLFSSL_AES_COUNTER
43869     #ifdef WOLFSSL_AES_128
43870     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
43871     #endif
43872     #ifdef WOLFSSL_AES_192
43873     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
43874     #endif
43875     #ifdef WOLFSSL_AES_256
43876     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
43877     #endif
43878 #endif
43879 
43880 #ifdef HAVE_AES_ECB
43881     #ifdef WOLFSSL_AES_128
43882     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
43883     #endif
43884     #ifdef WOLFSSL_AES_192
43885     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
43886     #endif
43887     #ifdef WOLFSSL_AES_256
43888     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
43889     #endif
43890 #endif
43891 
43892 #ifdef WOLFSSL_AES_OFB
43893     #ifdef WOLFSSL_AES_128
43894     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
43895     #endif
43896     #ifdef WOLFSSL_AES_192
43897     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
43898     #endif
43899     #ifdef WOLFSSL_AES_256
43900     AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
43901     #endif
43902 #endif
43903 
43904 #ifndef NO_RC4
43905     AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
43906 #endif
43907 
43908 #endif /* OPENSSL_ALL */
43909 }
43910 
43911 static void test_wolfSSL_EVP_CIPHER_iv_length(void)
43912 {
43913 #if defined(OPENSSL_ALL)
43914     int i, enumlen;
43915 
43916 
43917     int enumArray[] = {
43918     #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
43919     #ifdef WOLFSSL_AES_128
43920         NID_aes_128_cbc,
43921     #endif
43922     #ifdef WOLFSSL_AES_192
43923         NID_aes_192_cbc,
43924     #endif
43925     #ifdef WOLFSSL_AES_256
43926         NID_aes_256_cbc,
43927     #endif
43928     #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
43929     #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
43930         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
43931     #ifdef HAVE_AESGCM
43932         #ifdef WOLFSSL_AES_128
43933             NID_aes_128_gcm,
43934         #endif
43935         #ifdef WOLFSSL_AES_192
43936             NID_aes_192_gcm,
43937         #endif
43938         #ifdef WOLFSSL_AES_256
43939             NID_aes_256_gcm,
43940         #endif
43941     #endif /* HAVE_AESGCM */
43942     #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
43943     #ifdef WOLFSSL_AES_COUNTER
43944     #ifdef WOLFSSL_AES_128
43945          NID_aes_128_ctr,
43946     #endif
43947     #ifdef WOLFSSL_AES_192
43948         NID_aes_192_ctr,
43949     #endif
43950     #ifdef WOLFSSL_AES_256
43951         NID_aes_256_ctr,
43952     #endif
43953     #endif
43954     #ifndef NO_DES3
43955          NID_des_cbc,
43956          NID_des_ede3_cbc,
43957     #endif
43958     #ifdef HAVE_IDEA
43959          NID_idea_cbc,
43960     #endif
43961     };
43962 
43963     int iv_lengths[] = {
43964     #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
43965     #ifdef WOLFSSL_AES_128
43966             AES_BLOCK_SIZE,
43967     #endif
43968     #ifdef WOLFSSL_AES_192
43969             AES_BLOCK_SIZE,
43970     #endif
43971     #ifdef WOLFSSL_AES_256
43972             AES_BLOCK_SIZE,
43973     #endif
43974     #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
43975     #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
43976         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
43977     #ifdef HAVE_AESGCM
43978         #ifdef WOLFSSL_AES_128
43979             GCM_NONCE_MID_SZ,
43980         #endif
43981         #ifdef WOLFSSL_AES_192
43982             GCM_NONCE_MID_SZ,
43983         #endif
43984         #ifdef WOLFSSL_AES_256
43985             GCM_NONCE_MID_SZ,
43986         #endif
43987     #endif /* HAVE_AESGCM */
43988     #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
43989     #ifdef WOLFSSL_AES_COUNTER
43990     #ifdef WOLFSSL_AES_128
43991             AES_BLOCK_SIZE,
43992     #endif
43993     #ifdef WOLFSSL_AES_192
43994             AES_BLOCK_SIZE,
43995     #endif
43996     #ifdef WOLFSSL_AES_256
43997             AES_BLOCK_SIZE,
43998     #endif
43999     #endif
44000     #ifndef NO_DES3
44001             DES_BLOCK_SIZE,
44002             DES_BLOCK_SIZE,
44003     #endif
44004     #ifdef HAVE_IDEA
44005             IDEA_BLOCK_SIZE,
44006     #endif
44007     };
44008 
44009     printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
44010     enumlen = (sizeof(enumArray)/sizeof(int));
44011     for(i = 0; i < enumlen; i++)
44012     {
44013         const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
44014         AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
44015     }
44016 
44017     printf(resultFmt, passed);
44018 #endif
44019 }
44020 static void test_wolfSSL_EVP_SignInit_ex(void)
44021 {
44022 #if defined(OPENSSL_ALL)
44023     WOLFSSL_EVP_MD_CTX  mdCtx;
44024     WOLFSSL_ENGINE*     e = 0;
44025     const               EVP_MD* md;
44026                         md = "SHA256";
44027 
44028     printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
44029 
44030     wolfSSL_EVP_MD_CTX_init(&mdCtx);
44031     AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
44032 
44033     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
44034 
44035     printf(resultFmt, passed);
44036 #endif
44037 }
44038 static void test_wolfSSL_EVP_DigestFinal_ex(void)
44039 {
44040 #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
44041     WOLFSSL_EVP_MD_CTX  mdCtx;
44042     unsigned int        s = 0;
44043     unsigned char       md[WC_SHA256_DIGEST_SIZE];
44044     unsigned char       md2[WC_SHA256_DIGEST_SIZE];
44045 
44046 
44047     printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
44048 
44049 
44050     /* Bad Case */
44051 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
44052 
44053     wolfSSL_EVP_MD_CTX_init(&mdCtx);
44054     AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
44055     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
44056 
44057 #else
44058 
44059     wolfSSL_EVP_MD_CTX_init(&mdCtx);
44060     AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
44061     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
44062 
44063 #endif
44064 
44065     /* Good Case */
44066     wolfSSL_EVP_MD_CTX_init(&mdCtx);
44067     AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
44068     AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
44069     AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
44070 
44071 
44072     printf(resultFmt, passed);
44073 #endif
44074 }
44075 static void test_wolfSSL_EVP_PKEY_assign_DH(void)
44076 {
44077 #if defined(OPENSSL_ALL) && !defined(NO_DH) && \
44078  !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
44079     FILE*                   f = NULL;
44080     unsigned char           buf[4096];
44081     const unsigned char*    pt = buf;
44082     const char*             params1 = "./certs/dh2048.der";
44083     long                    len = 0;
44084     WOLFSSL_DH*             dh = NULL;
44085     WOLFSSL_EVP_PKEY*       pkey;
44086     XMEMSET(buf, 0, sizeof(buf));
44087 
44088 
44089     f = XFOPEN(params1, "rb");
44090     AssertTrue(f != XBADFILE);
44091     len = (long)XFREAD(buf, 1, sizeof(buf), f);
44092     XFCLOSE(f);
44093 
44094 
44095     printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
44096 
44097     AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
44098     AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
44099 
44100     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
44101 
44102     /* Bad cases */
44103     AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
44104     AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
44105     AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
44106 
44107     /* Good case */
44108     AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
44109 
44110 
44111     EVP_PKEY_free(pkey);
44112     printf(resultFmt, passed);
44113 #endif
44114 }
44115 
44116 static void test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
44117 {
44118 #if defined(OPENSSL_EXTRA)
44119     EVP_PKEY*       pkey;
44120     EVP_PKEY_CTX*   ctx;
44121 
44122     printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
44123 
44124     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
44125     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
44126 
44127     #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
44128         /* void */
44129         EVP_PKEY_CTX_free(ctx);
44130         AssertTrue(1);
44131     #else
44132         /* int */
44133         AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
44134     #endif
44135 
44136     EVP_PKEY_free(pkey);
44137     printf(resultFmt, passed);
44138 #endif
44139 }
44140 static void test_wolfSSL_EVP_PKEY_param_check(void)
44141 {
44142 #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
44143 #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
44144 
44145     DH       *dh    = NULL;
44146     DH       *setDh = NULL;
44147     EVP_PKEY *pkey  = NULL;
44148     EVP_PKEY_CTX*   ctx = NULL;
44149 
44150     FILE* f = NULL;
44151     unsigned char buf[512];
44152     const unsigned char* pt = buf;
44153     const char* dh2048 = "./certs/dh2048.der";
44154     long len = 0;
44155     int code = -1;
44156 
44157     printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
44158 
44159     XMEMSET(buf, 0, sizeof(buf));
44160 
44161     f = XFOPEN(dh2048, "rb");
44162     AssertTrue(f != XBADFILE);
44163     len = (long)XFREAD(buf, 1, sizeof(buf), f);
44164     XFCLOSE(f);
44165 
44166     /* Load dh2048.der into DH with internal format */
44167     AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
44168     AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
44169     AssertIntEQ(code, 0);
44170     code = -1;
44171 
44172     pkey = wolfSSL_EVP_PKEY_new();
44173     /* Set DH into PKEY */
44174     AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
44175     /* create ctx from pkey */
44176     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
44177     AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
44178 
44179     /*                    */
44180     /* TO DO invlaid case */
44181     /*                    */
44182 
44183     EVP_PKEY_CTX_free(ctx);
44184     EVP_PKEY_free(pkey);
44185     DH_free(setDh);
44186     DH_free(dh);
44187 
44188     printf(resultFmt, passed);
44189 #endif
44190 #endif
44191 }
44192 static void test_wolfSSL_EVP_BytesToKey(void)
44193 {
44194 #if defined(OPENSSL_ALL) && !defined(NO_AES) && defined(HAVE_AES_CBC)
44195     byte                key[AES_BLOCK_SIZE] = {0};
44196     byte                iv[AES_BLOCK_SIZE] = {0};
44197     int                 sz = 5;
44198     int                 count = 0;
44199     const               EVP_MD* md;
44200                         md = "SHA256";
44201     const EVP_CIPHER    *type;
44202     const unsigned char *salt = (unsigned char *)"salt1234";
44203     const byte data[] = {
44204         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
44205         0x72,0x6c,0x64
44206     };
44207 
44208     type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
44209 
44210     printf(testingFmt, "EVP_BytesToKey");
44211 
44212     /* Bad cases */
44213     AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
44214                  0);
44215     AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
44216                 16);
44217     md = "2";
44218     AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
44219                  WOLFSSL_FAILURE);
44220 
44221     /* Good case */
44222     md = "SHA256";
44223     AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
44224                  16);
44225 
44226     printf(resultFmt, passed);
44227 #endif
44228 }
44229 static void test_IncCtr(void)
44230 {
44231 #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(HAVE_FIPS)
44232     byte key[AES_128_KEY_SIZE] = {0};
44233     byte iv[GCM_NONCE_MID_SZ] = {0};
44234     int type = EVP_CTRL_GCM_IV_GEN;
44235     int arg = 0;
44236     void *ptr = NULL;
44237 
44238     printf(testingFmt, "IncCtr");
44239 
44240     EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
44241     const EVP_CIPHER *init = EVP_aes_128_gcm();
44242 
44243     AssertNotNull(ctx);
44244     wolfSSL_EVP_CIPHER_CTX_init(ctx);
44245     AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
44246 
44247     AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr), WOLFSSL_SUCCESS);
44248 
44249     EVP_CIPHER_CTX_free(ctx);
44250     printf(resultFmt, passed);
44251 #endif
44252 }
44253 static void test_wolfSSL_OBJ_ln(void)
44254 {
44255     const int nid_set[] = {
44256             NID_commonName,
44257             NID_serialNumber,
44258             NID_countryName,
44259             NID_localityName,
44260             NID_stateOrProvinceName,
44261             NID_organizationName,
44262             NID_organizationalUnitName,
44263             NID_domainComponent,
44264             NID_businessCategory,
44265             NID_jurisdictionCountryName,
44266             NID_jurisdictionStateOrProvinceName,
44267             NID_emailAddress
44268     };
44269     const char* ln_set[] = {
44270             "commonName",
44271             "serialNumber",
44272             "countryName",
44273             "localityName",
44274             "stateOrProvinceName",
44275             "organizationName",
44276             "organizationalUnitName",
44277             "domainComponent",
44278             "businessCategory",
44279             "jurisdictionCountryName",
44280             "jurisdictionStateOrProvinceName",
44281             "emailAddress",
44282     };
44283     size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
44284 
44285     printf(testingFmt, "wolfSSL_OBJ_ln");
44286 
44287     AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
44288 
44289 #ifdef HAVE_ECC
44290 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
44291     {
44292         size_t nCurves = 27;
44293         EC_builtin_curve r[nCurves];
44294         nCurves = EC_get_builtin_curves(r,nCurves);
44295 
44296         for (i = 0; i < nCurves; i++) {
44297             /* skip ECC_CURVE_INVALID */
44298             if (r[i].nid != ECC_CURVE_INVALID) {
44299                 AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
44300                 AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
44301             }
44302         }
44303     }
44304 #endif
44305 #endif
44306 
44307     for (i = 0; i < maxIdx; i++) {
44308         AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
44309         AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
44310     }
44311 
44312     printf(resultFmt, passed);
44313 }
44314 
44315 static void test_wolfSSL_OBJ_sn(void)
44316 {
44317     int i = 0, maxIdx = 7;
44318     const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
44319                            NID_stateOrProvinceName,NID_organizationName,
44320                            NID_organizationalUnitName,NID_emailAddress};
44321     const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
44322     const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
44323                                 WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
44324                                 WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
44325                                 WOLFSSL_EMAIL_ADDR};
44326 
44327     printf(testingFmt, "wolfSSL_OBJ_sn");
44328 
44329     AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
44330     for (i = 0; i < maxIdx; i++) {
44331         AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
44332         AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
44333     }
44334 
44335     printf(resultFmt, passed);
44336 }
44337 
44338 #if !defined(NO_BIO)
44339 static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
44340 {
44341     return lh_strhash(s[3]);
44342 }
44343 
44344 static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
44345 {
44346     return XSTRCMP(a[3], b[3]);
44347 }
44348 #endif
44349 
44350 static void test_wolfSSL_TXT_DB(void)
44351 {
44352 #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
44353     BIO *bio;
44354     TXT_DB *db = NULL;
44355     const int columns = 6;
44356     const char *fields[6] = {
44357         "V",
44358         "320926161116Z",
44359         "",
44360         "12BD",
44361         "unknown",
44362         "/CN=rsa doe",
44363     };
44364     char** fields_copy;
44365 
44366     printf(testingFmt, "wolfSSL_TXT_DB");
44367 
44368     /* Test read */
44369     AssertNotNull(bio = BIO_new(BIO_s_file()));
44370     AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
44371     AssertNotNull(db = TXT_DB_read(bio, columns));
44372     AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
44373             DYNAMIC_TYPE_OPENSSL));
44374     XMEMCPY(fields_copy, fields, sizeof(fields));
44375     AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
44376     BIO_free(bio);
44377 
44378     /* Test write */
44379     AssertNotNull(bio = BIO_new(BIO_s_mem()));
44380     AssertIntEQ(TXT_DB_write(bio, db), 1484);
44381     BIO_free(bio);
44382 
44383     /* Test index */
44384     AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
44385             (wolf_sk_compare_cb)TXT_DB_cmp), 1);
44386     AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
44387     fields[3] = "12DA";
44388     AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
44389     fields[3] = "FFFF";
44390     AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
44391     fields[3] = "";
44392     AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
44393 
44394     TXT_DB_free(db);
44395 
44396     printf(resultFmt, passed);
44397 #endif
44398 }
44399 
44400 static void test_wolfSSL_NCONF(void)
44401 {
44402 #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
44403     const char* confFile = "./tests/NCONF_test.cnf";
44404     CONF* conf = NULL;
44405     long eline = 0;
44406     long num = 0;
44407 
44408     printf(testingFmt, "wolfSSL_NCONF");
44409 
44410     AssertNotNull(conf = NCONF_new(NULL));
44411 
44412     AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
44413     AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
44414     AssertIntEQ(num, 1234);
44415     AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
44416     AssertIntEQ(num, 4321);
44417     AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
44418     AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
44419             "./test-dir/file1");
44420     AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
44421             "./test-dir/file1:./test-dir/file2:./section1:file2");
44422 
44423     NCONF_free(conf);
44424 
44425     printf(resultFmt, passed);
44426 #endif
44427 }
44428 #endif /* OPENSSL_ALL */
44429 
44430 static void test_wolfSSL_EC_KEY_set_group(void)
44431 {
44432 #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
44433     defined(OPENSSL_EXTRA)
44434     EC_KEY   *key    = NULL;
44435     EC_GROUP *group  = NULL;
44436     const EC_GROUP *group2 = NULL;
44437 
44438     printf(testingFmt, "wolfSSL_EC_KEY_dup()");
44439 
44440     AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
44441     AssertNotNull(key = EC_KEY_new());
44442     AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
44443     AssertNotNull(group2 = EC_KEY_get0_group(key));
44444     AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
44445 
44446     EC_GROUP_free(group);
44447     EC_KEY_free(key);
44448 
44449     printf(resultFmt, passed);
44450 #endif
44451 }
44452 
44453 static void test_wolfSSL_X509V3_EXT_get(void) {
44454 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44455     FILE* f;
44456     int numOfExt =0;
44457     int extNid = 0;
44458     int i = 0;
44459     WOLFSSL_X509* x509;
44460     WOLFSSL_X509_EXTENSION* ext;
44461     const WOLFSSL_v3_ext_method* method;
44462 
44463     AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
44464     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
44465     fclose(f);
44466 
44467     printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
44468     AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
44469     for (i = 0; i < numOfExt; i++) {
44470         AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44471         AssertIntNE((extNid = ext->obj->nid), NID_undef);
44472         AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
44473         AssertIntEQ(method->ext_nid, extNid);
44474     }
44475     printf(resultFmt, "passed");
44476 
44477     printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
44478     AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
44479     printf(resultFmt, "passed");
44480 
44481     wolfSSL_X509_free(x509);
44482 #endif
44483 }
44484 
44485 static void test_wolfSSL_X509V3_EXT_nconf(void)
44486 {
44487 #if defined (OPENSSL_ALL)
44488     const char *ext_names[] = {
44489         "subjectKeyIdentifier",
44490         "authorityKeyIdentifier",
44491         "subjectAltName",
44492         "keyUsage",
44493     };
44494     size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
44495     int ext_nids[] = {
44496         NID_subject_key_identifier,
44497         NID_authority_key_identifier,
44498         NID_subject_alt_name,
44499         NID_key_usage,
44500     };
44501     size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
44502     const char *ext_values[] = {
44503         "hash",
44504         "hash",
44505         "DNS:example.com, IP:127.0.0.1",
44506         "digitalSignature,keyEncipherment,dataEncipherment",
44507     };
44508     size_t i;
44509 
44510     printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
44511 
44512     for (i = 0; i < ext_names_count; i++) {
44513         X509_EXTENSION* ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
44514                 ext_values[i]);
44515         AssertNotNull(ext);
44516         X509_EXTENSION_free(ext);
44517     }
44518 
44519     for (i = 0; i < ext_nids_count; i++) {
44520         X509_EXTENSION* ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
44521                 ext_values[i]);
44522         AssertNotNull(ext);
44523         X509_EXTENSION_free(ext);
44524     }
44525 
44526     printf(resultFmt, "passed");
44527 #endif
44528 }
44529 
44530 static void test_wolfSSL_X509V3_EXT(void) {
44531 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44532     FILE* f;
44533     int numOfExt = 0, nid = 0, i = 0, expected, actual;
44534     char* str;
44535     unsigned char* data;
44536     const WOLFSSL_v3_ext_method* method;
44537     WOLFSSL_X509* x509;
44538     WOLFSSL_X509_EXTENSION* ext;
44539     WOLFSSL_X509_EXTENSION* ext2;
44540     WOLFSSL_ASN1_OBJECT *obj, *adObj;
44541     WOLFSSL_ASN1_STRING* asn1str;
44542     WOLFSSL_AUTHORITY_KEYID* aKeyId;
44543     WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
44544     WOLFSSL_BASIC_CONSTRAINTS* bc;
44545     WOLFSSL_ACCESS_DESCRIPTION* ad;
44546     WOLFSSL_GENERAL_NAME* gn;
44547 
44548     printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
44549 
44550     /* Check NULL argument */
44551     AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
44552 
44553     /* Using OCSP cert with X509V3 extensions */
44554     AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
44555     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
44556     fclose(f);
44557 
44558     AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
44559 
44560     /* Basic Constraints */
44561     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44562     AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
44563     AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
44564     AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
44565 
44566     AssertIntEQ(bc->ca, 1);
44567     AssertNull(bc->pathlen);
44568     wolfSSL_BASIC_CONSTRAINTS_free(bc);
44569     i++;
44570 
44571     /* Subject Key Identifier */
44572     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44573     AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
44574     AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
44575 
44576     AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
44577     AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
44578                                                 asn1str));
44579     X509_EXTENSION_free(ext2);
44580     AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
44581     AssertNotNull(method->i2s);
44582     AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
44583     wolfSSL_ASN1_STRING_free(asn1str);
44584     actual = strcmp(str,
44585                  "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
44586     AssertIntEQ(actual, 0);
44587     XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
44588     i++;
44589 
44590     /* Authority Key Identifier */
44591     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44592     AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
44593     AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
44594 
44595     AssertNotNull(aKeyId =
44596                          (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
44597     AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
44598     AssertNotNull(asn1str = aKeyId->keyid);
44599     AssertNotNull(str =
44600               wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
44601     actual = strcmp(str,
44602                  "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
44603     AssertIntEQ(actual, 0);
44604     XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
44605     wolfSSL_AUTHORITY_KEYID_free(aKeyId);
44606     i++;
44607 
44608     /* Key Usage */
44609     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44610     AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
44611     AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
44612 
44613     AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
44614     #if defined(WOLFSSL_QT)
44615     AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
44616     #else
44617     AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
44618     #endif
44619     expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
44620 #ifdef BIG_ENDIAN_ORDER
44621     actual = data[1];
44622 #else
44623     actual = data[0];
44624 #endif
44625     AssertIntEQ(actual, expected);
44626     wolfSSL_ASN1_STRING_free(asn1str);
44627 #if 1
44628     i++;
44629 
44630     /* Authority Info Access */
44631     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
44632     AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
44633     AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
44634     AssertNotNull(aia =
44635                    (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
44636 #if defined(WOLFSSL_QT)
44637     AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
44638 #else
44639     AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
44640 #endif
44641     /* URI entry is an ACCESS_DESCRIPTION type */
44642 #if defined(WOLFSSL_QT)
44643     AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
44644 #else
44645     AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
44646 #endif
44647     AssertNotNull(adObj = ad->method);
44648     /* Make sure nid is OCSP */
44649     AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
44650 
44651     /* GENERAL_NAME stores URI as an ASN1_STRING */
44652     AssertNotNull(gn = ad->location);
44653     AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
44654     AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
44655     AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
44656     #if defined(WOLFSSL_QT)
44657     str = (char*)ASN1_STRING_get0_data(asn1str);
44658     #else
44659     str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
44660     #endif
44661     actual = strcmp(str, "http://127.0.0.1:22220");
44662     AssertIntEQ(actual, 0);
44663 
44664     wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
44665 #else
44666     (void) aia; (void) ad; (void) adObj; (void) gn;
44667 #endif
44668     wolfSSL_X509_free(x509);
44669     printf(resultFmt, "passed");
44670 #endif
44671 }
44672 
44673 static void test_wolfSSL_X509_get_extension_flags(void)
44674 {
44675 #ifdef OPENSSL_ALL
44676     XFILE f;
44677     X509* x509;
44678     unsigned int extFlags;
44679     unsigned int keyUsageFlags;
44680     unsigned int extKeyUsageFlags;
44681 
44682     printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
44683 
44684     /* client-int-cert.pem has the following extension flags. */
44685     extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
44686     /* and the following key usage flags. */
44687     keyUsageFlags = KU_DIGITAL_SIGNATURE
44688                   | KU_NON_REPUDIATION
44689                   | KU_KEY_ENCIPHERMENT;
44690     /* and the following extended key usage flags. */
44691     extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
44692 
44693     f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
44694     AssertTrue(f != XBADFILE);
44695     AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
44696     XFCLOSE(f);
44697     AssertIntEQ(X509_get_extension_flags(x509), extFlags);
44698     AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
44699     AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
44700     X509_free(x509);
44701 
44702     /* client-cert-ext.pem has the following extension flags. */
44703     extFlags = EXFLAG_KUSAGE;
44704     /* and the following key usage flags. */
44705     keyUsageFlags = KU_DIGITAL_SIGNATURE
44706                   | KU_KEY_CERT_SIGN
44707                   | KU_CRL_SIGN;
44708 
44709     AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
44710     AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
44711     XFCLOSE(f);
44712     AssertIntEQ(X509_get_extension_flags(x509), extFlags);
44713     AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
44714     X509_free(x509);
44715 
44716     printf(resultFmt, passed);
44717 #endif /* OPENSSL_ALL */
44718 }
44719 
44720 static void test_wolfSSL_X509_get_ext(void){
44721 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44722     int ret = 0;
44723     FILE* f;
44724     WOLFSSL_X509* x509;
44725     WOLFSSL_X509_EXTENSION* foundExtension;
44726 
44727     AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
44728     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
44729     fclose(f);
44730     AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
44731 
44732     printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
44733     AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
44734     printf(resultFmt, "passed");
44735 
44736     printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
44737     AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
44738     AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
44739     printf(resultFmt, "passed");
44740 
44741     printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
44742     AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
44743     AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
44744     printf(resultFmt, "passed");
44745 
44746     printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
44747     AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
44748     printf(resultFmt, "passed");
44749 
44750     wolfSSL_X509_free(x509);
44751 #endif
44752 }
44753 
44754 static void test_wolfSSL_X509_get_ext_by_NID(void)
44755 {
44756 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
44757     int rc;
44758     FILE* f;
44759     WOLFSSL_X509* x509;
44760 
44761     AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
44762     AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
44763     fclose(f);
44764 
44765     rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
44766     AssertIntGE(rc, 0);
44767 
44768     /* Start search from last location (should fail) */
44769     rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
44770     AssertIntGE(rc, -1);
44771 
44772     rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
44773     AssertIntGE(rc, -1);
44774 
44775     rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
44776     AssertIntEQ(rc, -1);
44777 
44778     rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
44779     AssertIntEQ(rc, -1);
44780 
44781     wolfSSL_X509_free(x509);
44782 #endif
44783 }
44784 
44785 static void test_wolfSSL_X509_get_ext_subj_alt_name(void)
44786 {
44787 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
44788     int rc;
44789     XFILE f;
44790     WOLFSSL_X509* x509;
44791     WOLFSSL_X509_EXTENSION* ext;
44792     WOLFSSL_ASN1_STRING* sanString;
44793     byte* sanDer;
44794 
44795     const byte expectedDer[] = {
44796         0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
44797         0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
44798 
44799     printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
44800 
44801     f = XFOPEN("./certs/server-cert.pem", "rb");
44802     AssertTrue(f != XBADFILE);
44803     AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
44804     fclose(f);
44805 
44806     rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
44807     AssertIntNE(rc, -1);
44808     AssertNotNull(ext = X509_get_ext(x509, rc));
44809     AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
44810     AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
44811     AssertNotNull(sanDer = ASN1_STRING_data(sanString));
44812     AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
44813 
44814     X509_free(x509);
44815 
44816     printf(resultFmt, passed);
44817 #endif
44818 }
44819 
44820 static void test_wolfSSL_X509_EXTENSION_new(void)
44821 {
44822 #if defined (OPENSSL_ALL)
44823     WOLFSSL_X509_EXTENSION* ext;
44824 
44825     AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
44826     AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
44827     ext->obj->nid = WOLFSSL_SUCCESS;
44828     AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
44829 
44830     wolfSSL_X509_EXTENSION_free(ext);
44831 #endif
44832 }
44833 
44834 static void test_wolfSSL_X509_EXTENSION_get_object(void)
44835 {
44836 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44837     WOLFSSL_X509* x509;
44838     WOLFSSL_X509_EXTENSION* ext;
44839     WOLFSSL_ASN1_OBJECT* o;
44840     FILE* file;
44841     int nid = 0;
44842 
44843     AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
44844     AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
44845     fclose(file);
44846 
44847     printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
44848     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
44849     AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
44850     AssertIntEQ(o->nid, 128);
44851     nid = o->nid;
44852     printf(resultFmt, nid == 128 ? passed : failed);
44853 
44854     printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
44855     AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
44856     printf(resultFmt, passed);
44857 
44858     wolfSSL_X509_free(x509);
44859 #endif
44860 }
44861 
44862 static void test_wolfSSL_X509_EXTENSION_get_data(void)
44863 {
44864 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44865     WOLFSSL_X509* x509;
44866     WOLFSSL_X509_EXTENSION* ext;
44867     WOLFSSL_ASN1_STRING* str;
44868     FILE* file;
44869 
44870     printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
44871 
44872     AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
44873     AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
44874     fclose(file);
44875     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
44876 
44877     AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
44878     printf(resultFmt, passed);
44879 
44880     wolfSSL_X509_free(x509);
44881 #endif
44882 }
44883 
44884 static void test_wolfSSL_X509_EXTENSION_get_critical(void)
44885 {
44886 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
44887     WOLFSSL_X509* x509;
44888     WOLFSSL_X509_EXTENSION* ext;
44889     FILE* file;
44890     int crit;
44891 
44892     printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
44893 
44894     AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
44895     AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
44896     fclose(file);
44897     AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
44898 
44899     crit = wolfSSL_X509_EXTENSION_get_critical(ext);
44900     AssertIntEQ(crit, 0);
44901     printf(resultFmt, passed);
44902 
44903     wolfSSL_X509_free(x509);
44904 #endif
44905 }
44906 
44907 static void test_wolfSSL_X509V3_EXT_print(void)
44908 {
44909 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
44910     !defined(NO_RSA)
44911     printf(testingFmt, "wolfSSL_X509V3_EXT_print");
44912 
44913     {
44914         FILE* f;
44915         WOLFSSL_X509* x509;
44916         X509_EXTENSION * ext = NULL;
44917         int loc;
44918         BIO *bio = NULL;
44919 
44920         AssertNotNull(f = fopen(svrCertFile, "rb"));
44921         AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
44922         fclose(f);
44923 
44924         AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
44925 
44926         loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
44927         AssertIntGT(loc, -1);
44928         AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
44929         AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
44930 
44931         loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
44932         AssertIntGT(loc, -1);
44933         AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
44934         AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
44935 
44936         loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
44937         AssertIntGT(loc, -1);
44938         AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
44939         AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
44940 
44941         wolfSSL_BIO_free(bio);
44942         wolfSSL_X509_free(x509);
44943     }
44944 
44945     {
44946         X509 *x509;
44947         BIO *bio;
44948         X509_EXTENSION *ext;
44949         unsigned int i;
44950         unsigned int idx;
44951         /* Some NIDs to test with */
44952         int nids[] = {
44953                 /* NID_key_usage, currently X509_get_ext returns this as a bit
44954                  * string, which messes up X509V3_EXT_print */
44955                 /* NID_ext_key_usage, */
44956                 NID_subject_alt_name,
44957         };
44958         int* n;
44959 
44960         AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
44961 
44962         AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
44963             WOLFSSL_FILETYPE_PEM));
44964 
44965         printf("\nPrinting extension values:\n");
44966 
44967         for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
44968             /* X509_get_ext_by_NID should return 3 for now. If that changes then
44969              * update the index */
44970             AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
44971             AssertNotNull(ext = X509_get_ext(x509, idx));
44972             AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
44973             printf("\n");
44974         }
44975 
44976         BIO_free(bio);
44977         X509_free(x509);
44978     }
44979     printf(resultFmt, passed);
44980 #endif
44981 }
44982 
44983 static void test_wolfSSL_X509_cmp(void)
44984 {
44985 #if defined(OPENSSL_ALL) && !defined(NO_RSA)
44986     FILE* file1;
44987     FILE* file2;
44988     WOLFSSL_X509* cert1;
44989     WOLFSSL_X509* cert2;
44990     int ret = 0;
44991 
44992     AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
44993     AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
44994 
44995     AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
44996     AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
44997     fclose(file1);
44998     fclose(file2);
44999 
45000     printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
45001     ret = wolfSSL_X509_cmp(cert1, cert1);
45002     AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
45003     printf(resultFmt, ret == 0 ? passed : failed);
45004 
45005     printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
45006     ret = wolfSSL_X509_cmp(cert1, cert2);
45007     AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
45008     printf(resultFmt, ret == -1 ? passed : failed);
45009 
45010     printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
45011     ret = wolfSSL_X509_cmp(NULL, cert2);
45012     AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
45013     printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
45014 
45015     printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
45016     ret = wolfSSL_X509_cmp(cert1, NULL);
45017     AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
45018     printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
45019 
45020     printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
45021     ret = wolfSSL_X509_cmp(NULL, NULL);
45022     AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
45023     printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
45024 
45025     wolfSSL_X509_free(cert1);
45026     wolfSSL_X509_free(cert2);
45027 #endif
45028 }
45029 
45030 static void test_wolfSSL_PKEY_up_ref(void)
45031 {
45032 #if defined(OPENSSL_ALL)
45033     EVP_PKEY* pkey;
45034     printf(testingFmt, "wolfSSL_PKEY_up_ref()");
45035 
45036     pkey = EVP_PKEY_new();
45037     AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
45038     AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
45039     EVP_PKEY_free(pkey);
45040     AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
45041     EVP_PKEY_free(pkey);
45042     EVP_PKEY_free(pkey);
45043 
45044     printf(resultFmt, "passed");
45045 #endif
45046 }
45047 
45048 static void test_wolfSSL_d2i_and_i2d_PublicKey(void)
45049 {
45050 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
45051     EVP_PKEY* pkey;
45052     const unsigned char* p;
45053     unsigned char* der = NULL;
45054     int derLen;
45055 
45056     printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
45057 
45058     p = client_keypub_der_2048;
45059     /* Check that key can be successfully decoded. */
45060     AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
45061         sizeof_client_keypub_der_2048));
45062     /* Check that key can be successfully encoded. */
45063     AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
45064     /* Ensure that the encoded version matches the original. */
45065     AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
45066     AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
45067 
45068     XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
45069     EVP_PKEY_free(pkey);
45070 
45071     printf(resultFmt, passed);
45072 #endif
45073 }
45074 
45075 static void test_wolfSSL_d2i_and_i2d_DSAparams(void)
45076 {
45077 #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
45078     DSA* dsa;
45079     char file[] = "./certs/dsaparams.der";
45080     XFILE f;
45081     int derInLen;
45082     byte* derIn;
45083     int derOutLen;
45084     byte* derOut = NULL;
45085 
45086     printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
45087 
45088     f = XFOPEN(file, "rb");
45089     AssertTrue(f != XBADFILE);
45090     AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
45091     derInLen = (int)XFTELL(f);
45092     XREWIND(f);
45093     AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
45094         DYNAMIC_TYPE_TMP_BUFFER));
45095     AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
45096     XFCLOSE(f);
45097 
45098     /* Check that params can be successfully decoded. */
45099     AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
45100     /* Check that params can be successfully encoded. */
45101     AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
45102     /* Ensure that the encoded version matches the original. */
45103     AssertIntEQ(derInLen, derOutLen);
45104     AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
45105 
45106     XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
45107     XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
45108     DSA_free(dsa);
45109 
45110     printf(resultFmt, passed);
45111 #endif
45112 }
45113 
45114 static void test_wolfSSL_i2d_PrivateKey(void)
45115 {
45116 #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
45117 
45118     printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
45119 #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
45120     {
45121         EVP_PKEY* pkey;
45122         const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
45123         unsigned char buf[FOURK_BUF];
45124         unsigned char* pt = NULL;
45125         int bufSz;
45126 
45127         AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
45128                     (long)sizeof_server_key_der_2048));
45129         AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
45130         pt = buf;
45131         AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
45132         AssertIntNE((pt - buf), 0);
45133         AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
45134         EVP_PKEY_free(pkey);
45135     }
45136 #endif
45137 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
45138     {
45139         EVP_PKEY* pkey;
45140         const unsigned char* client_key =
45141             (const unsigned char*)ecc_clikey_der_256;
45142         unsigned char buf[FOURK_BUF];
45143         unsigned char* pt = NULL;
45144         int bufSz;
45145 
45146         AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
45147                                                    sizeof_ecc_clikey_der_256)));
45148         AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
45149         pt = buf;
45150         AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
45151         AssertIntNE((pt - buf), 0);
45152         AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
45153         EVP_PKEY_free(pkey);
45154     }
45155 #endif
45156 
45157     printf(resultFmt, "passed");
45158 #endif
45159 }
45160 
45161 static void test_wolfSSL_OCSP_id_get0_info(void)
45162 {
45163 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
45164     !defined(NO_FILESYSTEM) && !defined(NO_RSA)
45165     X509* cert;
45166     X509* issuer;
45167     OCSP_CERTID* id;
45168     OCSP_CERTID* id2;
45169 
45170     ASN1_STRING* name = NULL;
45171     ASN1_OBJECT* pmd  = NULL;
45172     ASN1_STRING* keyHash = NULL;
45173     ASN1_INTEGER* serial = NULL;
45174     ASN1_INTEGER* x509Int;
45175 
45176     printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
45177 
45178     AssertNotNull(cert =
45179             wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
45180     AssertNotNull(issuer =
45181             wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
45182 
45183     id = OCSP_cert_to_id(NULL, cert, issuer);
45184     AssertNotNull(id);
45185     id2 = OCSP_cert_to_id(NULL, cert, issuer);
45186     AssertNotNull(id2);
45187 
45188     AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
45189     AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
45190 
45191     /* name, pmd, keyHash not supported yet, expect failure if not NULL */
45192     AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
45193     AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
45194     AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
45195 
45196     AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
45197     AssertNotNull(serial);
45198 
45199     /* compare serial number to one in cert, should be equal */
45200     x509Int = X509_get_serialNumber(cert);
45201     AssertNotNull(x509Int);
45202     AssertIntEQ(x509Int->length, serial->length);
45203     AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
45204 
45205     /* test OCSP_id_cmp */
45206     AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
45207     AssertIntNE(OCSP_id_cmp(id, NULL), 0);
45208     AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
45209     AssertIntEQ(OCSP_id_cmp(id, id2), 0);
45210     id->issuerHash[0] = ~id->issuerHash[0];
45211     AssertIntNE(OCSP_id_cmp(id, id2), 0);
45212 
45213     OCSP_CERTID_free(id);
45214     OCSP_CERTID_free(id2);
45215     X509_free(cert); /* free's x509Int */
45216     X509_free(issuer);
45217 
45218     printf(resultFmt, "passed");
45219 #endif
45220 }
45221 
45222 static void test_wolfSSL_i2d_OCSP_CERTID(void)
45223 {
45224 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
45225     WOLFSSL_OCSP_CERTID certId;
45226     byte* targetBuffer;
45227     byte* beginTargetBuffer;
45228     /* OCSP CertID bytes taken from PCAP */
45229     byte rawCertId[] = {
45230         0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
45231         0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
45232         0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
45233         0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
45234         0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
45235         0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
45236         0xcf, 0xbc, 0x91
45237     };
45238     int ret, i;
45239 
45240     printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
45241 
45242     XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
45243     certId.rawCertId = rawCertId;
45244     certId.rawCertIdSize = sizeof(rawCertId);
45245     targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
45246 
45247     beginTargetBuffer = targetBuffer;
45248     ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
45249     /* If target buffer is not null, function increments targetBuffer to point
45250        just past the end of the encoded data. */
45251     AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
45252     /* Function returns the size of the encoded data. */
45253     AssertIntEQ(ret, sizeof(rawCertId));
45254     for (i = 0; i < ret; ++i)
45255     {
45256         AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
45257     }
45258 
45259     XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
45260     targetBuffer = NULL;
45261     ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
45262     /* If target buffer is null, function allocates memory for a buffer and
45263        copies the encoded data into it. targetBuffer then points to the start of
45264        this newly allocate buffer. */
45265     AssertIntEQ(ret, sizeof(rawCertId));
45266     for (i = 0; i < ret; ++i)
45267     {
45268         AssertIntEQ(targetBuffer[i], rawCertId[i]);
45269     }
45270 
45271     XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
45272 
45273     printf(resultFmt, passed);
45274 #endif
45275 }
45276 
45277 static void test_wolfSSL_OCSP_id_cmp(void)
45278 {
45279 #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
45280     OCSP_CERTID id1;
45281     OCSP_CERTID id2;
45282     printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
45283 
45284     XMEMSET(&id1, 0, sizeof(id1));
45285     XMEMSET(&id2, 0, sizeof(id2));
45286     AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
45287 
45288     printf(resultFmt, passed);
45289 #endif
45290 }
45291 
45292 static void test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
45293 {
45294 #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
45295     WOLFSSL_OCSP_SINGLERESP single;
45296     const WOLFSSL_OCSP_CERTID* certId;
45297 
45298     XMEMSET(&single, 0, sizeof(single));
45299     certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
45300 
45301     printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
45302 
45303     AssertPtrEq(&single, certId);
45304 
45305     printf(resultFmt, passed);
45306 #endif
45307 }
45308 
45309 static void test_wolfSSL_OCSP_single_get0_status(void)
45310 {
45311 #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
45312     WOLFSSL_OCSP_SINGLERESP single;
45313     CertStatus certStatus;
45314     WOLFSSL_ASN1_TIME* thisDate;
45315     WOLFSSL_ASN1_TIME* nextDate;
45316     int ret, i;
45317 
45318     printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
45319 
45320     XMEMSET(&single,     0, sizeof(WOLFSSL_OCSP_SINGLERESP));
45321     XMEMSET(&certStatus, 0, sizeof(CertStatus));
45322 
45323     /* Fill the date fields with some dummy data. */
45324     for (i = 0; i < CTC_DATE_SIZE; ++i) {
45325         certStatus.thisDateParsed.data[i] = i;
45326         certStatus.nextDateParsed.data[i] = i;
45327     }
45328     certStatus.status = CERT_GOOD;
45329     single.status = &certStatus;
45330 
45331     ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
45332                                           &nextDate);
45333     AssertIntEQ(ret, CERT_GOOD);
45334     AssertPtrEq(thisDate, &certStatus.thisDateParsed);
45335     AssertPtrEq(nextDate, &certStatus.nextDateParsed);
45336 
45337     printf(resultFmt, passed);
45338 #endif
45339 }
45340 
45341 static void test_wolfSSL_OCSP_resp_count(void)
45342 {
45343 #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
45344     WOLFSSL_OCSP_BASICRESP basicResp;
45345     WOLFSSL_OCSP_SINGLERESP singleRespOne;
45346     WOLFSSL_OCSP_SINGLERESP singleRespTwo;
45347     int count;
45348 
45349     printf(testingFmt, "wolfSSL_OCSP_resp_count()");
45350 
45351     XMEMSET(&basicResp,     0, sizeof(WOLFSSL_OCSP_BASICRESP));
45352     XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
45353     XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
45354 
45355     count = wolfSSL_OCSP_resp_count(&basicResp);
45356     AssertIntEQ(count, 0);
45357 
45358     basicResp.single = &singleRespOne;
45359     count = wolfSSL_OCSP_resp_count(&basicResp);
45360     AssertIntEQ(count, 1);
45361 
45362     singleRespOne.next = &singleRespTwo;
45363     count = wolfSSL_OCSP_resp_count(&basicResp);
45364     AssertIntEQ(count, 2);
45365 
45366     printf(resultFmt, passed);
45367 #endif
45368 }
45369 
45370 static void test_wolfSSL_OCSP_resp_get0(void)
45371 {
45372 #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
45373     WOLFSSL_OCSP_BASICRESP basicResp;
45374     WOLFSSL_OCSP_SINGLERESP singleRespOne;
45375     WOLFSSL_OCSP_SINGLERESP singleRespTwo;
45376     WOLFSSL_OCSP_SINGLERESP* ret;
45377 
45378     printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
45379 
45380     XMEMSET(&basicResp,     0, sizeof(WOLFSSL_OCSP_BASICRESP));
45381     XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
45382     XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
45383 
45384     basicResp.single = &singleRespOne;
45385     singleRespOne.next = &singleRespTwo;
45386 
45387     ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
45388     AssertPtrEq(ret, &singleRespOne);
45389 
45390     ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
45391     AssertPtrEq(ret, &singleRespTwo);
45392 
45393     printf(resultFmt, passed);
45394 #endif
45395 }
45396 
45397 static void test_wolfSSL_EVP_PKEY_derive(void)
45398 {
45399 #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
45400 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
45401 #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
45402 
45403     printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
45404     EVP_PKEY_CTX *ctx;
45405     unsigned char *skey;
45406     size_t skeylen;
45407     EVP_PKEY *pkey, *peerkey;
45408     const unsigned char* key;
45409 
45410 #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
45411     /* DH */
45412     key = dh_key_der_2048;
45413     AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
45414                                          sizeof_dh_key_der_2048)));
45415     AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
45416     key = dh_key_der_2048;
45417     AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
45418                                             sizeof_dh_key_der_2048)));
45419     AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
45420     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
45421     AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
45422     AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
45423     AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
45424     AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
45425     AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
45426 
45427     EVP_PKEY_CTX_free(ctx);
45428     EVP_PKEY_free(peerkey);
45429     EVP_PKEY_free(pkey);
45430     XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
45431 #endif
45432 
45433 #ifdef HAVE_ECC
45434     /* ECDH */
45435     key = ecc_clikey_der_256;
45436     AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
45437                                          sizeof_ecc_clikey_der_256)));
45438     key = ecc_clikeypub_der_256;
45439     AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
45440                                         sizeof_ecc_clikeypub_der_256)));
45441     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
45442     AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
45443     AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
45444     AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
45445     AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
45446     AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
45447 
45448     EVP_PKEY_CTX_free(ctx);
45449     EVP_PKEY_free(peerkey);
45450     EVP_PKEY_free(pkey);
45451     XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
45452 #endif /* HAVE_ECC */
45453 
45454     printf(resultFmt, "passed");
45455 #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
45456 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
45457 #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
45458 }
45459 
45460 static void test_wolfSSL_EVP_PBE_scrypt(void)
45461 {
45462 #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
45463     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
45464 #if !defined(NO_PWDBASED) &&  !defined(NO_SHA256)
45465 
45466     int ret;
45467 
45468     const char  pwd[]    = {'p','a','s','s','w','o','r','d'};
45469     int         pwdlen   = sizeof(pwd);
45470     const byte  salt[]   = {'N','a','C','l'};
45471     int         saltlen  = sizeof(salt);
45472     byte        key[80];
45473     word64      numOvr32 = (word64)INT32_MAX + 1;
45474 
45475     /* expected derived key for N:16, r:1, p:1 */
45476     const byte expectedKey[] = {
45477         0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
45478         0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
45479         0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
45480         0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
45481         0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
45482         0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
45483         0xE7, 0xE9, 0xC0, 0x9A};
45484 
45485     printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
45486 
45487     /*                                               N  r  p  mx key keylen */
45488     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
45489     AssertIntEQ(ret, 0); /* N must be greater than 1 */
45490 
45491     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
45492     AssertIntEQ(ret, 0); /* N must be power of 2 */
45493 
45494     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
45495     AssertIntEQ(ret, 0); /* r must be greater than 0 */
45496 
45497     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
45498     AssertIntEQ(ret, 0); /* p must be greater than 0 */
45499 
45500     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
45501     AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
45502 
45503     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
45504     AssertIntEQ(ret, 0); /* r must be smaller than 9 */
45505 
45506     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
45507     AssertIntEQ(ret, 1); /* should succeed if key is NULL  */
45508 
45509     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
45510     AssertIntEQ(ret, 1); /* should succeed */
45511 
45512     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
45513                                                                     key, 64);
45514     AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
45515 
45516     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
45517                                                                     key, 64);
45518     AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
45519 
45520     ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
45521     AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
45522 
45523     ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
45524     AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
45525 
45526     ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
45527     AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
45528 
45529     ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
45530     AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
45531 
45532     ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
45533     AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
45534 
45535     ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
45536     AssertIntEQ(ret, 1);
45537 
45538     ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
45539     AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
45540 
45541     printf(resultFmt, "passed");
45542 
45543 #endif /* !NO_PWDBASED && !NO_SHA256 */
45544 #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
45545 }
45546 
45547 #ifndef NO_RSA
45548 static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
45549 {
45550 #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
45551 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
45552     RSA *rsa;
45553     const unsigned char *derBuf = client_key_der_2048;
45554     unsigned char em[256] = {0}; /* len = 2048/8 */
45555     /* Random data simulating a hash */
45556     const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
45557         0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
45558         0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
45559         0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
45560     };
45561 
45562     AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
45563     AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -1), 1);
45564     AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
45565 
45566     RSA_free(rsa);
45567 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
45568 #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
45569 }
45570 #endif
45571 
45572 static void test_wolfSSL_RSA_sign_sha3(void)
45573 {
45574 #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
45575 #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
45576     RSA *rsa;
45577     const unsigned char *derBuf = client_key_der_2048;
45578     unsigned char sigRet[256] = {0};
45579     unsigned int sigLen = sizeof(sigRet);
45580     /* Random data simulating a hash */
45581     const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
45582         0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
45583         0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
45584         0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
45585     };
45586 
45587     printf(testingFmt, "wolfSSL_RSA_sign_sha3");
45588 
45589     AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
45590     AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
45591                             &sigLen, rsa), 1);
45592 
45593     RSA_free(rsa);
45594 
45595     printf(resultFmt, passed);
45596 #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
45597 #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
45598 }
45599 
45600 static void test_wolfSSL_EC_get_builtin_curves(void)
45601 {
45602 #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
45603 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
45604     EC_builtin_curve* curves = NULL;
45605     size_t crv_len = 0;
45606     size_t i = 0;
45607 
45608     printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
45609 
45610     AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
45611     AssertNotNull(curves = (EC_builtin_curve*)
45612             XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
45613                     DYNAMIC_TYPE_TMP_BUFFER));
45614     AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
45615 
45616     for (i = 0; i < crv_len; i++)
45617     {
45618         if (curves[i].comment != NULL)
45619             AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
45620     }
45621 
45622     XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
45623     printf(resultFmt, passed);
45624 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
45625 #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
45626 }
45627 
45628 static void test_no_op_functions(void)
45629 {
45630     #if defined(OPENSSL_EXTRA)
45631     printf(testingFmt, "no_op_functions()");
45632 
45633     /* this makes sure wolfSSL can compile and run these no-op functions */
45634     SSL_load_error_strings();
45635     ENGINE_load_builtin_engines();
45636     OpenSSL_add_all_ciphers();
45637     AssertIntEQ(CRYPTO_malloc_init(), 0);
45638 
45639     printf(resultFmt, passed);
45640     #endif
45641 }
45642 
45643 static void test_wolfSSL_CRYPTO_memcmp(void)
45644 {
45645 #ifdef OPENSSL_EXTRA
45646     char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
45647                "implementation of TLS/SSL for embedded devices to the cloud.";
45648     char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
45649                "implementation of TLS/SSL for embedded devices to the cloud.";
45650     char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
45651                "implementation of TLS/SSL for embedded devices to the cloud!";
45652 
45653     AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
45654     AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
45655 #endif
45656 }
45657 
45658 /*----------------------------------------------------------------------------*
45659  | wolfCrypt ASN
45660  *----------------------------------------------------------------------------*/
45661 
45662 static void test_wc_CreateEncryptedPKCS8Key(void)
45663 {
45664 #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
45665  && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
45666     WC_RNG rng;
45667     byte* encKey = NULL;
45668     word32 encKeySz = 0;
45669     word32 decKeySz = 0;
45670     const char password[] = "Lorem ipsum dolor sit amet";
45671     word32 passwordSz = (word32)XSTRLEN(password);
45672     word32 tradIdx = 0;
45673 
45674     printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
45675 
45676     AssertIntEQ(wc_InitRng(&rng), 0);
45677     /* Call with NULL for out buffer to get necessary length. */
45678     AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
45679         sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
45680         PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
45681         LENGTH_ONLY_E);
45682     AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
45683         DYNAMIC_TYPE_TMP_BUFFER));
45684     /* Call with the allocated out buffer. */
45685     AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
45686         sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
45687         PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
45688         0);
45689     /* Decrypt the encrypted PKCS8 key we just made. */
45690     AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
45691         passwordSz)), 0);
45692     /* encKey now holds the decrypted key (decrypted in place). */
45693     AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
45694     /* Check that the decrypted key matches the key prior to encryption. */
45695     AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
45696         sizeof_server_key_der_2048), 0);
45697 
45698     if (encKey != NULL)
45699         XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
45700     wc_FreeRng(&rng);
45701 
45702     printf(resultFmt, passed);
45703 #endif
45704 }
45705 
45706 static void test_wc_GetPkcs8TraditionalOffset(void)
45707 {
45708 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
45709     int length, derSz;
45710     word32 inOutIdx;
45711     const char* path = "./certs/server-keyPkcs8.der";
45712     XFILE file;
45713     byte der[2048];
45714 
45715     printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
45716 
45717     file = XFOPEN(path, "rb");
45718     AssertTrue(file != XBADFILE);
45719     derSz = (int)XFREAD(der, 1, sizeof(der), file);
45720     XFCLOSE(file);
45721 
45722     /* valid case */
45723     inOutIdx = 0;
45724     length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
45725     AssertIntGT(length, 0);
45726 
45727     /* inOutIdx > sz */
45728     inOutIdx = 4000;
45729     length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
45730     AssertIntEQ(length, BAD_FUNC_ARG);
45731 
45732     /* null input */
45733     inOutIdx = 0;
45734     length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
45735     AssertIntEQ(length, BAD_FUNC_ARG);
45736 
45737     /* invalid input, fill buffer with 1's */
45738     XMEMSET(der, 1, sizeof(der));
45739     inOutIdx = 0;
45740     length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
45741     AssertIntEQ(length, ASN_PARSE_E);
45742 
45743     printf(resultFmt, passed);
45744 #endif /* NO_ASN */
45745 }
45746 
45747 static void test_wc_SetSubjectRaw(void)
45748 {
45749 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45750     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
45751     const char* joiCertFile = "./certs/test/cert-ext-joi.der";
45752     WOLFSSL_X509* x509;
45753     int peerCertSz;
45754     const byte* peerCertBuf;
45755     Cert forgedCert;
45756 
45757     printf(testingFmt, "test_wc_SetSubjectRaw()");
45758 
45759     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
45760 
45761     AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
45762 
45763     AssertIntEQ(0, wc_InitCert(&forgedCert));
45764 
45765     AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
45766 
45767     wolfSSL_FreeX509(x509);
45768 
45769     printf(resultFmt, passed);
45770 #endif
45771 }
45772 
45773 static void test_wc_GetSubjectRaw(void)
45774 {
45775 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45776     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
45777     Cert cert;
45778     byte *subjectRaw;
45779 
45780     printf(testingFmt, "test_wc_GetSubjectRaw()");
45781 
45782     AssertIntEQ(0, wc_InitCert(&cert));
45783     AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
45784 
45785     printf(resultFmt, passed);
45786 #endif
45787 }
45788 
45789 static void test_wc_SetIssuerRaw(void)
45790 {
45791 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45792     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
45793     const char* joiCertFile = "./certs/test/cert-ext-joi.der";
45794     WOLFSSL_X509* x509;
45795     int peerCertSz;
45796     const byte* peerCertBuf;
45797     Cert forgedCert;
45798 
45799     printf(testingFmt, "test_wc_SetIssuerRaw()");
45800 
45801     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
45802 
45803     AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
45804 
45805     AssertIntEQ(0, wc_InitCert(&forgedCert));
45806 
45807     AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
45808 
45809     wolfSSL_FreeX509(x509);
45810 
45811     printf(resultFmt, passed);
45812 #endif
45813 }
45814 
45815 static void test_wc_SetIssueBuffer(void)
45816 {
45817 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45818     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
45819     const char* joiCertFile = "./certs/test/cert-ext-joi.der";
45820     WOLFSSL_X509* x509;
45821     int peerCertSz;
45822     const byte* peerCertBuf;
45823     Cert forgedCert;
45824 
45825     printf(testingFmt, "test_wc_SetIssuerBuffer()");
45826 
45827     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
45828 
45829     AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
45830 
45831     AssertIntEQ(0, wc_InitCert(&forgedCert));
45832 
45833     AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
45834 
45835     wolfSSL_FreeX509(x509);
45836 
45837     printf(resultFmt, passed);
45838 #endif
45839 }
45840 
45841 /*
45842  * Testing wc_SetSubjectKeyId
45843  */
45844 static void test_wc_SetSubjectKeyId(void)
45845 {
45846 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45847     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
45848     Cert cert;
45849     const char* file = "certs/ecc-client-keyPub.pem";
45850 
45851     printf(testingFmt, "wc_SetSubjectKeyId()");
45852 
45853     AssertIntEQ(0, wc_InitCert(&cert));
45854     AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
45855 
45856     AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
45857     AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
45858 
45859     printf(resultFmt, passed);
45860 #endif
45861 } /* END test_wc_SetSubjectKeyId */
45862 
45863 /*
45864  * Testing wc_SetSubject
45865  */
45866 static void test_wc_SetSubject(void)
45867 {
45868 #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
45869     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
45870     Cert cert;
45871     const char* file = "./certs/ca-ecc-cert.pem";
45872 
45873     printf(testingFmt, "wc_SetSubject()");
45874 
45875     AssertIntEQ(0, wc_InitCert(&cert));
45876     AssertIntEQ(0, wc_SetSubject(&cert, file));
45877 
45878     AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
45879     AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
45880 
45881     printf(resultFmt, passed);
45882 #endif
45883 } /* END test_wc_SetSubject */
45884 
45885 
45886 static void test_CheckCertSignature(void)
45887 {
45888 #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
45889     WOLFSSL_CERT_MANAGER* cm = NULL;
45890 #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
45891     FILE* fp;
45892     byte  cert[4096];
45893     int   certSz;
45894 #endif
45895 
45896     AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
45897     AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
45898     AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
45899 
45900 #ifndef NO_RSA
45901 #ifdef USE_CERT_BUFFERS_1024
45902     AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
45903                 sizeof_server_cert_der_1024, NULL, cm));
45904     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
45905                 ca_cert_der_1024, sizeof_ca_cert_der_1024,
45906                 WOLFSSL_FILETYPE_ASN1));
45907     AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
45908                 sizeof_server_cert_der_1024, NULL, cm));
45909 #elif defined(USE_CERT_BUFFERS_2048)
45910     AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
45911                 sizeof_server_cert_der_2048, NULL, cm));
45912     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
45913                 ca_cert_der_2048, sizeof_ca_cert_der_2048,
45914                 WOLFSSL_FILETYPE_ASN1));
45915     AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
45916                 sizeof_server_cert_der_2048, NULL, cm));
45917 #endif
45918 #endif
45919 
45920 #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
45921     AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
45922                 sizeof_serv_ecc_der_256, NULL, cm));
45923     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
45924                 ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
45925                 WOLFSSL_FILETYPE_ASN1));
45926     AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
45927                 NULL, cm));
45928 #endif
45929 
45930 #if !defined(NO_FILESYSTEM)
45931     wolfSSL_CertManagerFree(cm);
45932     AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
45933 #ifndef NO_RSA
45934     AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
45935     AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
45936     XFCLOSE(fp);
45937     AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
45938     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
45939                 "./certs/ca-cert.pem", NULL));
45940     AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
45941 #endif
45942 #ifdef HAVE_ECC
45943     AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
45944     AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
45945     XFCLOSE(fp);
45946     AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
45947     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
45948                 "./certs/ca-ecc-cert.pem", NULL));
45949     AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
45950 #endif
45951 #endif
45952 
45953 #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
45954     (void)fp;
45955     (void)cert;
45956     (void)certSz;
45957 #endif
45958 
45959     wolfSSL_CertManagerFree(cm);
45960 #endif
45961 }
45962 
45963 static void test_wc_ParseCert(void)
45964 {
45965 #if !defined(NO_CERTS) && !defined(NO_RSA)
45966     DecodedCert decodedCert;
45967     const byte* rawCert = client_cert_der_2048;
45968     const int rawCertSize = sizeof_client_cert_der_2048;
45969 
45970     printf(testingFmt, "wc_ParseCert");
45971 
45972     wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
45973     AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
45974     wc_FreeDecodedCert(&decodedCert);
45975 
45976     printf(resultFmt, passed);
45977 #endif
45978 }
45979 
45980 /*----------------------------------------------------------------------------*
45981  | wolfCrypt ECC
45982  *----------------------------------------------------------------------------*/
45983 
45984 static void test_wc_ecc_get_curve_size_from_name(void)
45985 {
45986 #ifdef HAVE_ECC
45987     int ret;
45988 
45989     printf(testingFmt, "wc_ecc_get_curve_size_from_name");
45990 
45991     #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
45992         ret = wc_ecc_get_curve_size_from_name("SECP256R1");
45993         AssertIntEQ(ret, 32);
45994     #endif
45995 
45996     /* invalid case */
45997     ret = wc_ecc_get_curve_size_from_name("BADCURVE");
45998     AssertIntEQ(ret, -1);
45999 
46000     /* NULL input */
46001     ret = wc_ecc_get_curve_size_from_name(NULL);
46002     AssertIntEQ(ret, BAD_FUNC_ARG);
46003 
46004     printf(resultFmt, passed);
46005 #endif /* HAVE_ECC */
46006 }
46007 
46008 static void test_wc_ecc_get_curve_id_from_name(void)
46009 {
46010 #ifdef HAVE_ECC
46011     int id;
46012 
46013     printf(testingFmt, "wc_ecc_get_curve_id_from_name");
46014 
46015     #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
46016         id = wc_ecc_get_curve_id_from_name("SECP256R1");
46017         AssertIntEQ(id, ECC_SECP256R1);
46018     #endif
46019 
46020     /* invalid case */
46021     id = wc_ecc_get_curve_id_from_name("BADCURVE");
46022     AssertIntEQ(id, -1);
46023 
46024     /* NULL input */
46025     id = wc_ecc_get_curve_id_from_name(NULL);
46026     AssertIntEQ(id, BAD_FUNC_ARG);
46027 
46028     printf(resultFmt, passed);
46029 #endif /* HAVE_ECC */
46030 }
46031 
46032 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
46033     !defined(HAVE_SELFTEST) && \
46034     !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
46035 
46036 static void test_wc_ecc_get_curve_id_from_dp_params(void)
46037 {
46038     int id;
46039 #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
46040     int curve_id;
46041     ecc_key* key;
46042     const ecc_set_type* params;
46043     int ret;
46044 #endif
46045     WOLFSSL_EC_KEY *ecKey = NULL;
46046 
46047     printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
46048 
46049     #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
46050         id = wc_ecc_get_curve_id_from_name("SECP256R1");
46051         AssertIntEQ(id, ECC_SECP256R1);
46052 
46053         ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
46054         AssertNotNull(ecKey);
46055 
46056         ret = EC_KEY_generate_key(ecKey);
46057 
46058         if (ret == 0) {
46059             /* normal test */
46060             key = (ecc_key*)ecKey->internal;
46061             params = key->dp;
46062 
46063             curve_id = wc_ecc_get_curve_id_from_dp_params(params);
46064             AssertIntEQ(curve_id, id);
46065         }
46066     #endif
46067     /* invalid case, NULL input*/
46068 
46069     id = wc_ecc_get_curve_id_from_dp_params(NULL);
46070     AssertIntEQ(id, BAD_FUNC_ARG);
46071     wolfSSL_EC_KEY_free(ecKey);
46072 
46073     printf(resultFmt, passed);
46074 }
46075 #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
46076 
46077 static void test_wc_ecc_get_curve_id_from_params(void)
46078 {
46079 #ifdef HAVE_ECC
46080     int id;
46081 
46082     const byte prime[] =
46083     {
46084         0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
46085         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
46086         0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
46087         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
46088     };
46089 
46090     const byte primeInvalid[] =
46091     {
46092         0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
46093         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
46094         0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
46095         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
46096     };
46097 
46098     const byte Af[] =
46099     {
46100         0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
46101         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
46102         0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
46103         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
46104     };
46105 
46106     const byte Bf[] =
46107     {
46108         0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
46109         0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
46110         0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
46111         0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
46112     };
46113 
46114     const byte order[] =
46115     {
46116         0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
46117         0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
46118         0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
46119         0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
46120     };
46121 
46122     const byte Gx[] =
46123     {
46124         0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
46125         0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
46126         0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
46127         0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
46128     };
46129 
46130     const byte Gy[] =
46131     {
46132         0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
46133         0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
46134         0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
46135         0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
46136     };
46137 
46138     int cofactor = 1;
46139     int fieldSize = 256;
46140 
46141     printf(testingFmt, "wc_ecc_get_curve_id_from_params");
46142 
46143     #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
46144         id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
46145                 Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
46146                 Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
46147         AssertIntEQ(id, ECC_SECP256R1);
46148     #endif
46149 
46150     /* invalid case, fieldSize = 0 */
46151     id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
46152             Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
46153             Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
46154     AssertIntEQ(id, ECC_CURVE_INVALID);
46155 
46156     /* invalid case, NULL prime */
46157     id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
46158             Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
46159             Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
46160     AssertIntEQ(id, BAD_FUNC_ARG);
46161 
46162     /* invalid case, invalid prime */
46163     id = wc_ecc_get_curve_id_from_params(fieldSize,
46164             primeInvalid, sizeof(primeInvalid),
46165             Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
46166             Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
46167     AssertIntEQ(id, ECC_CURVE_INVALID);
46168 
46169     printf(resultFmt, passed);
46170 #endif
46171 }
46172 static void test_wolfSSL_EVP_PKEY_encrypt(void)
46173 {
46174 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
46175     !defined(HAVE_FAST_RSA)
46176     WOLFSSL_RSA* rsa = NULL;
46177     WOLFSSL_EVP_PKEY* pkey = NULL;
46178     WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
46179     const char* in = "What is easy to do is easy not to do.";
46180     size_t inlen = XSTRLEN(in);
46181     size_t outEncLen = 0;
46182     byte*  outEnc = NULL;
46183     byte*  outDec = NULL;
46184     size_t outDecLen = 0;
46185     size_t rsaKeySz = 2048/8;  /* Bytes */
46186 #ifdef WC_RSA_NO_PADDING
46187     byte*  inTmp = NULL;
46188     byte*  outEncTmp = NULL;
46189     byte*  outDecTmp = NULL;
46190 #endif
46191     printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
46192 
46193     AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46194     XMEMSET(outEnc, 0, rsaKeySz);
46195     AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46196     XMEMSET(outDec, 0, rsaKeySz);
46197 
46198     AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
46199     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
46200     AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
46201     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
46202     AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
46203     AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
46204                 WOLFSSL_SUCCESS);
46205 
46206     /* Test pkey references count is decremented. pkey shouldn't be destroyed
46207      since ctx uses it.*/
46208     AssertIntEQ(pkey->references, 2);
46209     EVP_PKEY_free(pkey);
46210     AssertIntEQ(pkey->references, 1);
46211 
46212     /* Encrypt data */
46213     /* Check that we can get the required output buffer length by passing in a
46214      * NULL output buffer. */
46215     AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
46216                             (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
46217     AssertIntEQ(rsaKeySz, outEncLen);
46218     /* Now do the actual encryption. */
46219     AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
46220                             (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
46221 
46222     /* Decrypt data */
46223     AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
46224     /* Check that we can get the required output buffer length by passing in a
46225      * NULL output buffer. */
46226     AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
46227                                  WOLFSSL_SUCCESS);
46228     AssertIntEQ(rsaKeySz, outDecLen);
46229     /* Now do the actual decryption. */
46230     AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
46231                                  WOLFSSL_SUCCESS);
46232 
46233     AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
46234 
46235 #ifdef WC_RSA_NO_PADDING
46236     /* The input length must be the same size as the RSA key.*/
46237     AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46238     XMEMSET(inTmp, 9, rsaKeySz);
46239     AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46240     XMEMSET(outEncTmp, 0, rsaKeySz);
46241     AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46242     XMEMSET(outDecTmp, 0, rsaKeySz);
46243     AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
46244     AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
46245                                              WOLFSSL_SUCCESS);
46246     AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
46247                                  WOLFSSL_SUCCESS);
46248     AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
46249     AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
46250                                  WOLFSSL_SUCCESS);
46251     AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
46252 #endif
46253     EVP_PKEY_CTX_free(ctx);
46254     XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46255     XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46256 #ifdef WC_RSA_NO_PADDING
46257     XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46258     XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46259     XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46260 #endif
46261     printf(resultFmt, passed);
46262 #endif
46263 }
46264 static void test_wolfSSL_EVP_PKEY_sign(void)
46265 {
46266 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
46267     !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
46268 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
46269     WOLFSSL_RSA* rsa = NULL;
46270     WOLFSSL_EVP_PKEY* pkey = NULL;
46271     WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
46272     const char* in = "What is easy to do is easy not to do.";
46273     size_t inlen = XSTRLEN(in);
46274     byte hash[SHA256_DIGEST_LENGTH] = {0};
46275     SHA256_CTX c;
46276     byte*  sig = NULL;
46277     byte*  sigVerify = NULL;
46278     size_t siglen = 0;
46279     size_t rsaKeySz = 2048/8;  /* Bytes */
46280 
46281     printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
46282     sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46283     AssertNotNull(sig);
46284     XMEMSET(sig, 0, rsaKeySz);
46285     AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
46286     XMEMSET(sigVerify, 0, rsaKeySz);
46287 
46288     /* Generate hash */
46289     SHA256_Init(&c);
46290     SHA256_Update(&c, in, inlen);
46291     SHA256_Final(hash, &c);
46292 #ifdef WOLFSSL_SMALL_STACK_CACHE
46293     /* workaround for small stack cache case */
46294     wc_Sha256Free((wc_Sha256*)&c);
46295 #endif
46296 
46297     AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
46298     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
46299     AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
46300     AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
46301     AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
46302     AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
46303                 WOLFSSL_SUCCESS);
46304 
46305     /* Sign data */
46306     AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
46307                               WOLFSSL_SUCCESS);
46308     /* Verify signature.
46309        EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
46310     AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
46311                              rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
46312 
46313     AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
46314     /* error cases */
46315 
46316     AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
46317     ctx->pkey->type = EVP_PKEY_RSA;
46318     AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
46319     AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
46320                               WOLFSSL_SUCCESS);
46321     AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
46322                               WOLFSSL_SUCCESS);
46323 
46324     EVP_PKEY_free(pkey);
46325     EVP_PKEY_CTX_free(ctx);
46326     XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46327     XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
46328 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
46329     printf(resultFmt, passed);
46330 #endif
46331 }
46332 
46333 static void test_EVP_PKEY_rsa(void)
46334 {
46335 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
46336     WOLFSSL_RSA* rsa;
46337     WOLFSSL_EVP_PKEY* pkey;
46338 
46339     AssertNotNull(rsa = wolfSSL_RSA_new());
46340     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
46341     AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
46342     AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
46343     AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
46344     AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
46345     wolfSSL_EVP_PKEY_free(pkey);
46346 
46347     printf(resultFmt, passed);
46348 
46349 #endif
46350 }
46351 
46352 static void test_EVP_PKEY_ec(void)
46353 {
46354 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
46355 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
46356     WOLFSSL_EC_KEY* ecKey;
46357     WOLFSSL_EVP_PKEY* pkey;
46358 
46359     AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
46360     AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
46361     AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
46362     AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
46363     /* Should fail since ecKey is empty */
46364     AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
46365     AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
46366     AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
46367     wolfSSL_EVP_PKEY_free(pkey);
46368 
46369     printf(resultFmt, passed);
46370 #endif
46371 #endif
46372 }
46373 
46374 static void test_EVP_PKEY_cmp(void)
46375 {
46376 #if defined(OPENSSL_EXTRA)
46377     EVP_PKEY *a, *b;
46378     const unsigned char *in;
46379 
46380     printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
46381 #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
46382     in = client_key_der_2048;
46383     AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
46384         &in, (long)sizeof_client_key_der_2048));
46385     in = client_key_der_2048;
46386     AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
46387         &in, (long)sizeof_client_key_der_2048));
46388 
46389     /* Test success case RSA */
46390 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
46391     AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
46392 #else
46393     AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
46394 #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
46395 
46396     EVP_PKEY_free(b);
46397     EVP_PKEY_free(a);
46398 #endif
46399 
46400 #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
46401     in = ecc_clikey_der_256;
46402     AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
46403         &in, (long)sizeof_ecc_clikey_der_256));
46404     in = ecc_clikey_der_256;
46405     AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
46406         &in, (long)sizeof_ecc_clikey_der_256));
46407 
46408     /* Test success case ECC */
46409 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
46410     AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
46411 #else
46412     AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
46413 #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
46414 
46415     EVP_PKEY_free(b);
46416     EVP_PKEY_free(a);
46417 #endif
46418 
46419     /* Test failure cases */
46420 #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
46421      defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
46422 
46423     in = client_key_der_2048;
46424     AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
46425         &in, (long)sizeof_client_key_der_2048));
46426     in = ecc_clikey_der_256;
46427     AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
46428         &in, (long)sizeof_ecc_clikey_der_256));
46429 
46430 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
46431     AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
46432 #else
46433     AssertIntNE(EVP_PKEY_cmp(a, b), 0);
46434 #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
46435     EVP_PKEY_free(b);
46436     EVP_PKEY_free(a);
46437 #endif
46438 
46439     /* invalid or empty failure cases */
46440     a = EVP_PKEY_new();
46441     b = EVP_PKEY_new();
46442 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
46443     AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
46444     AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
46445     AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
46446     AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
46447 #else
46448     AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
46449     AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
46450     AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
46451     AssertIntNE(EVP_PKEY_cmp(a, b), 0);
46452 #endif
46453     EVP_PKEY_free(b);
46454     EVP_PKEY_free(a);
46455 
46456     (void)in;
46457 
46458     printf(resultFmt, passed);
46459 #endif
46460 }
46461 
46462 static void test_ERR_load_crypto_strings(void)
46463 {
46464 #if defined(OPENSSL_ALL)
46465     ERR_load_crypto_strings();
46466 
46467     printf(resultFmt, passed);
46468 #endif
46469 }
46470 
46471 #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
46472 static void free_x509(X509* x)
46473 {
46474     AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
46475 }
46476 #endif
46477 
46478 static void test_sk_X509(void)
46479 {
46480 #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
46481     STACK_OF(X509)* s;
46482 
46483     AssertNotNull(s = sk_X509_new());
46484     AssertIntEQ(sk_X509_num(s), 0);
46485     sk_X509_pop_free(s, NULL);
46486 
46487     AssertNotNull(s = sk_X509_new_null());
46488     AssertIntEQ(sk_X509_num(s), 0);
46489     sk_X509_pop_free(s, NULL);
46490 
46491     AssertNotNull(s = sk_X509_new());
46492     sk_X509_push(s, (X509*)1);
46493     AssertIntEQ(sk_X509_num(s), 1);
46494     AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
46495     sk_X509_push(s, (X509*)2);
46496     AssertIntEQ(sk_X509_num(s), 2);
46497     AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
46498     AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
46499     sk_X509_push(s, (X509*)2);
46500     sk_X509_pop_free(s, free_x509);
46501 
46502     printf(resultFmt, passed);
46503 #endif
46504 }
46505 
46506 static void test_sk_X509_CRL(void)
46507 {
46508 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
46509     X509_CRL* crl;
46510     XFILE fp;
46511     STACK_OF(X509_CRL)* s;
46512 
46513     printf(testingFmt, "test_sk_X509_CRL");
46514 
46515     fp = XFOPEN("./certs/crl/crl.pem", "rb");
46516     AssertTrue((fp != XBADFILE));
46517     AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
46518     XFCLOSE(fp);
46519 
46520     AssertNotNull(s = sk_X509_CRL_new());
46521     AssertIntEQ(sk_X509_CRL_num(s), 0);
46522     AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
46523     AssertIntEQ(sk_X509_CRL_num(s), 1);
46524     AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
46525     sk_X509_CRL_free(s);
46526 
46527     printf(resultFmt, passed);
46528 #endif
46529 }
46530 
46531 static void test_X509_get_signature_nid(void)
46532 {
46533 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
46534     X509*   x509;
46535 
46536     AssertIntEQ(X509_get_signature_nid(NULL), 0);
46537     AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
46538                                                              SSL_FILETYPE_PEM));
46539     AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
46540     X509_free(x509);
46541 
46542     printf(resultFmt, passed);
46543 #endif
46544 }
46545 
46546 static void test_X509_REQ(void)
46547 {
46548 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
46549     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
46550     X509_NAME* name;
46551 #if !defined(NO_RSA) || defined(HAVE_ECC)
46552     X509_REQ* req;
46553     EVP_PKEY* priv;
46554     EVP_PKEY* pub;
46555     unsigned char* der = NULL;
46556     int len;
46557 #endif
46558 #ifndef NO_RSA
46559     EVP_MD_CTX *mctx = NULL;
46560     EVP_PKEY_CTX *pkctx = NULL;
46561     #ifdef USE_CERT_BUFFERS_1024
46562     const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
46563     const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
46564     #elif defined(USE_CERT_BUFFERS_2048)
46565     const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
46566     const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
46567     #endif
46568 #endif
46569 #ifdef HAVE_ECC
46570     const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
46571     const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
46572 #endif
46573 
46574     AssertNotNull(name = X509_NAME_new());
46575     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
46576                                            (byte*)"wolfssl.com", 11, 0, 1),
46577                 WOLFSSL_SUCCESS);
46578     AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
46579                                            (byte*)"support@wolfssl.com", 19, -1,
46580                                            1), WOLFSSL_SUCCESS);
46581 
46582 #ifndef NO_RSA
46583     AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
46584                                         (long)sizeof_client_key_der_2048));
46585     AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
46586                                    (long)sizeof_client_keypub_der_2048));
46587     AssertNotNull(req = X509_REQ_new());
46588     AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
46589     AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
46590     AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
46591     AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
46592     AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
46593     AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
46594     AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
46595     AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
46596     AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
46597     AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
46598     len = i2d_X509_REQ(req, &der);
46599     DEBUG_WRITE_DER(der, len, "req.der");
46600 #ifdef USE_CERT_BUFFERS_1024
46601     AssertIntEQ(len, 381);
46602 #else
46603     AssertIntEQ(len, 643);
46604 #endif
46605     XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
46606     der = NULL;
46607 
46608     mctx = EVP_MD_CTX_new();
46609     AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
46610     AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
46611 
46612     EVP_MD_CTX_free(mctx);
46613     X509_REQ_free(NULL);
46614     X509_REQ_free(req);
46615     EVP_PKEY_free(pub);
46616     EVP_PKEY_free(priv);
46617 #endif
46618 #ifdef HAVE_ECC
46619     AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
46620                                                     sizeof_ecc_clikey_der_256));
46621     AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
46622                                                  sizeof_ecc_clikeypub_der_256));
46623     AssertNotNull(req = X509_REQ_new());
46624     AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
46625     AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
46626     AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
46627     /* Signature is random and may be shorter or longer. */
46628     AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
46629     AssertIntLE(len, 253);
46630     XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
46631     X509_REQ_free(req);
46632     EVP_PKEY_free(pub);
46633     EVP_PKEY_free(priv);
46634 
46635 #ifdef FP_ECC
46636     wc_ecc_fp_free();
46637 #endif
46638 #endif /* HAVE_ECC */
46639 
46640     X509_NAME_free(name);
46641 
46642     printf(resultFmt, passed);
46643 #endif
46644 }
46645 
46646 static void test_wolfssl_PKCS7(void)
46647 {
46648 #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
46649     PKCS7* pkcs7;
46650     byte   data[FOURK_BUF];
46651     word32 len = sizeof(data);
46652     const byte*  p = data;
46653     byte   content[] = "Test data to encode.";
46654 #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
46655     BIO*   bio;
46656     byte   key[sizeof(client_key_der_2048)];
46657     word32 keySz = (word32)sizeof(key);
46658     byte*  out = NULL;
46659 #endif
46660 
46661     AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
46662                                              (word32)sizeof(content),
46663                                              0, 0)), 0);
46664 
46665     AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
46666     AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
46667     AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
46668     AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
46669                                               PKCS7_NOVERIFY), WOLFSSL_FAILURE);
46670     PKCS7_free(pkcs7);
46671 
46672     /* fail case, without PKCS7_NOVERIFY */
46673     p = data;
46674     AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
46675     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
46676                                                            0), WOLFSSL_FAILURE);
46677     PKCS7_free(pkcs7);
46678 
46679     /* success case, with PKCS7_NOVERIFY */
46680     p = data;
46681     AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
46682     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
46683                                               PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
46684 
46685 #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
46686     /* test i2d */
46687     XMEMCPY(key, client_key_der_2048, keySz);
46688     pkcs7->privateKey = key;
46689     pkcs7->privateKeySz = (word32)sizeof(key);
46690     pkcs7->encryptOID = RSAk;
46691     pkcs7->hashOID = SHAh;
46692     AssertNotNull(bio = BIO_new(BIO_s_mem()));
46693     AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
46694     AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
46695     XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
46696     BIO_free(bio);
46697 #endif
46698 
46699     PKCS7_free(NULL);
46700     PKCS7_free(pkcs7);
46701 
46702     printf(resultFmt, passed);
46703 #endif
46704 }
46705 
46706 static void test_wolfSSL_PKCS7_SIGNED_new(void)
46707 {
46708 #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
46709     PKCS7_SIGNED* pkcs7;
46710 
46711     printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
46712 
46713     pkcs7 = PKCS7_SIGNED_new();
46714     AssertNotNull(pkcs7);
46715     AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
46716 
46717     PKCS7_SIGNED_free(pkcs7);
46718     printf(resultFmt, passed);
46719 #endif
46720 }
46721 
46722 #ifndef NO_BIO
46723 static void test_wolfSSL_PEM_write_bio_PKCS7(void)
46724 {
46725 #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
46726     PKCS7* pkcs7 = NULL;
46727     BIO* bio = NULL;
46728     const byte* cert_buf = NULL;
46729     int ret = 0;
46730     WC_RNG rng;
46731     const byte data[] = { /* Hello World */
46732         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
46733         0x72,0x6c,0x64
46734     };
46735 #ifndef NO_RSA
46736     #if defined(USE_CERT_BUFFERS_2048)
46737         byte        key[sizeof(client_key_der_2048)];
46738         byte        cert[sizeof(client_cert_der_2048)];
46739         word32      keySz = (word32)sizeof(key);
46740         word32      certSz = (word32)sizeof(cert);
46741         XMEMSET(key, 0, keySz);
46742         XMEMSET(cert, 0, certSz);
46743         XMEMCPY(key, client_key_der_2048, keySz);
46744         XMEMCPY(cert, client_cert_der_2048, certSz);
46745     #elif defined(USE_CERT_BUFFERS_1024)
46746         byte        key[sizeof_client_key_der_1024];
46747         byte        cert[sizeof(sizeof_client_cert_der_1024)];
46748         word32      keySz = (word32)sizeof(key);
46749         word32      certSz = (word32)sizeof(cert);
46750         XMEMSET(key, 0, keySz);
46751         XMEMSET(cert, 0, certSz);
46752         XMEMCPY(key, client_key_der_1024, keySz);
46753         XMEMCPY(cert, client_cert_der_1024, certSz);
46754     #else
46755         unsigned char   cert[ONEK_BUF];
46756         unsigned char   key[ONEK_BUF];
46757         XFILE           fp;
46758         int             certSz;
46759         int             keySz;
46760 
46761         fp = XFOPEN("./certs/1024/client-cert.der", "rb");
46762         AssertTrue((fp != XBADFILE));
46763         certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
46764         XFCLOSE(fp);
46765 
46766         fp = XFOPEN("./certs/1024/client-key.der", "rb");
46767         AssertTrue(fp != XBADFILE);
46768         keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
46769         XFCLOSE(fp);
46770     #endif
46771 #elif defined(HAVE_ECC)
46772     #if defined(USE_CERT_BUFFERS_256)
46773         unsigned char    cert[sizeof(cliecc_cert_der_256)];
46774         unsigned char    key[sizeof(ecc_clikey_der_256)];
46775         int              certSz = (int)sizeof(cert);
46776         int              keySz = (int)sizeof(key);
46777         XMEMSET(cert, 0, certSz);
46778         XMEMSET(key, 0, keySz);
46779         XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
46780         XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
46781     #else
46782         unsigned char   cert[ONEK_BUF];
46783         unsigned char   key[ONEK_BUF];
46784         XFILE           fp;
46785         int             certSz, keySz;
46786 
46787         fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
46788         AssertTrue(fp != XBADFILE);
46789         certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
46790         XFCLOSE(fp);
46791 
46792         fp = XFOPEN("./certs/client-ecc-key.der", "rb");
46793         AssertTrue(fp != XBADFILE);
46794         keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
46795         XFCLOSE(fp);
46796     #endif
46797 #else
46798     #error PKCS7 requires ECC or RSA
46799 #endif
46800     printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
46801 
46802     AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
46803     /* initialize with DER encoded cert */
46804     AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
46805 
46806     /* init rng */
46807     AssertIntEQ(wc_InitRng(&rng), 0);
46808 
46809     pkcs7->rng = &rng;
46810     pkcs7->content   = (byte*)data; /* not used for ex */
46811     pkcs7->contentSz = (word32)sizeof(data);
46812     pkcs7->contentOID = SIGNED_DATA;
46813     pkcs7->privateKey = key;
46814     pkcs7->privateKeySz = (word32)sizeof(key);
46815     pkcs7->encryptOID = RSAk;
46816     pkcs7->hashOID = SHAh;
46817     pkcs7->signedAttribs   = NULL;
46818     pkcs7->signedAttribsSz = 0;
46819 
46820 #ifndef NO_BIO
46821     AssertNotNull(bio = BIO_new(BIO_s_mem()));
46822     /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
46823     AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
46824 
46825     /* Read PKCS#7 PEM from BIO */
46826     ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
46827     AssertIntGE(ret, 0);
46828 
46829     BIO_free(bio);
46830 #endif
46831     wc_PKCS7_Free(pkcs7);
46832     wc_FreeRng(&rng);
46833 
46834     printf(resultFmt, passed);
46835 
46836 #endif
46837 }
46838 
46839 #ifdef HAVE_SMIME
46840 static void test_wolfSSL_SMIME_read_PKCS7(void)
46841 {
46842 #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
46843     !defined(NO_RSA)
46844     PKCS7* pkcs7 = NULL;
46845     BIO* bio = NULL;
46846     BIO* bcont = NULL;
46847     XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
46848 
46849     printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
46850 
46851     bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
46852     AssertNotNull(bio);
46853     AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
46854     pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
46855     AssertNotNull(pkcs7);
46856     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
46857     XFCLOSE(smimeTestFile);
46858     if (bcont) BIO_free(bcont);
46859     wolfSSL_PKCS7_free(pkcs7);
46860 
46861     smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
46862     AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
46863     pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
46864     AssertNotNull(pkcs7);
46865     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
46866     XFCLOSE(smimeTestFile);
46867     if (bcont) BIO_free(bcont);
46868     wolfSSL_PKCS7_free(pkcs7);
46869 
46870     smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
46871     AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
46872     pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
46873     AssertNull(pkcs7);
46874     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_FAILURE);
46875     XFCLOSE(smimeTestFile);
46876     if (bcont) BIO_free(bcont);
46877     wolfSSL_PKCS7_free(pkcs7);
46878 
46879     smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
46880     AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
46881     pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
46882     AssertNotNull(pkcs7);
46883     AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
46884     BIO_free(bio);
46885     if (bcont) BIO_free(bcont);
46886     wolfSSL_PKCS7_free(pkcs7);
46887 
46888     printf(resultFmt, passed);
46889 #endif
46890 }
46891 #endif /* HAVE_SMIME*/
46892 #endif /* !NO_BIO */
46893 
46894 /*----------------------------------------------------------------------------*
46895  | Certificate Failure Checks
46896  *----------------------------------------------------------------------------*/
46897 #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
46898                            !defined(WOLFSSL_NO_CLIENT_AUTH))
46899     /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
46900     static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
46901         int type)
46902     {
46903         int ret;
46904         WOLFSSL_CERT_MANAGER* cm = NULL;
46905 
46906         switch (type) {
46907             case TESTING_RSA:
46908             #ifdef NO_RSA
46909                 printf("RSA disabled, skipping test\n");
46910                 return ASN_SIG_CONFIRM_E;
46911             #else
46912                 break;
46913             #endif
46914             case TESTING_ECC:
46915             #ifndef HAVE_ECC
46916                 printf("ECC disabled, skipping test\n");
46917                 return ASN_SIG_CONFIRM_E;
46918             #else
46919                 break;
46920             #endif
46921             default:
46922                 printf("Bad function argument\n");
46923                 return BAD_FUNC_ARG;
46924         }
46925         cm = wolfSSL_CertManagerNew();
46926         if (cm == NULL) {
46927             printf("wolfSSL_CertManagerNew failed\n");
46928             return -1;
46929         }
46930 
46931     #ifndef NO_FILESYSTEM
46932         ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
46933         if (ret != WOLFSSL_SUCCESS) {
46934             printf("wolfSSL_CertManagerLoadCA failed\n");
46935             wolfSSL_CertManagerFree(cm);
46936             return ret;
46937         }
46938     #else
46939         (void)ca;
46940     #endif
46941 
46942         ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
46943         /* Let AssertIntEQ handle return code */
46944 
46945         wolfSSL_CertManagerFree(cm);
46946 
46947         return ret;
46948     }
46949 
46950     static int test_RsaSigFailure_cm(void)
46951     {
46952         int ret = 0;
46953         const char* ca_cert = "./certs/ca-cert.pem";
46954         const char* server_cert = "./certs/server-cert.der";
46955         byte* cert_buf = NULL;
46956         size_t cert_sz = 0;
46957 
46958         ret = load_file(server_cert, &cert_buf, &cert_sz);
46959         if (ret == 0) {
46960             /* corrupt DER - invert last byte, which is signature */
46961             cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
46962 
46963             /* test bad cert */
46964             ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
46965         }
46966 
46967         printf("Signature failure test: RSA: Ret %d\n", ret);
46968 
46969         if (cert_buf)
46970             free(cert_buf);
46971 
46972         return ret;
46973     }
46974 
46975     static int test_EccSigFailure_cm(void)
46976     {
46977         int ret = 0;
46978         /* self-signed ECC cert, so use server cert as CA */
46979         const char* ca_cert = "./certs/ca-ecc-cert.pem";
46980         const char* server_cert = "./certs/server-ecc.der";
46981         byte* cert_buf = NULL;
46982         size_t cert_sz = 0;
46983 
46984         ret = load_file(server_cert, &cert_buf, &cert_sz);
46985         if (ret == 0) {
46986             /* corrupt DER - invert last byte, which is signature */
46987             cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
46988 
46989             /* test bad cert */
46990             ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
46991         }
46992 
46993         printf("Signature failure test: ECC: Ret %d\n", ret);
46994 
46995         if (cert_buf)
46996             free(cert_buf);
46997 
46998 #ifdef FP_ECC
46999     wc_ecc_fp_free();
47000 #endif
47001         return ret;
47002     }
47003 
47004 #endif /* NO_CERTS */
47005 
47006 #ifdef WOLFSSL_TLS13
47007 #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
47008 #ifdef WC_SHA384_DIGEST_SIZE
47009     static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
47010 #else
47011     static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
47012 #endif
47013 #endif
47014 #ifdef WOLFSSL_EARLY_DATA
47015 static const char earlyData[] = "Early Data";
47016 static       char earlyDataBuffer[1];
47017 #endif
47018 
47019 static int test_tls13_apis(void)
47020 {
47021     int          ret = 0;
47022 #ifndef WOLFSSL_NO_TLS12
47023 #ifndef NO_WOLFSSL_CLIENT
47024     WOLFSSL_CTX* clientTls12Ctx;
47025     WOLFSSL*     clientTls12Ssl;
47026 #endif
47027 #ifndef NO_WOLFSSL_SERVER
47028     WOLFSSL_CTX* serverTls12Ctx;
47029     WOLFSSL*     serverTls12Ssl;
47030 #endif
47031 #endif
47032 #ifndef NO_WOLFSSL_CLIENT
47033     WOLFSSL_CTX* clientCtx;
47034     WOLFSSL*     clientSsl;
47035 #endif
47036 #ifndef NO_WOLFSSL_SERVER
47037     WOLFSSL_CTX* serverCtx;
47038     WOLFSSL*     serverSsl;
47039 #ifndef NO_CERTS
47040     const char*  ourCert = svrCertFile;
47041     const char*  ourKey  = svrKeyFile;
47042 #endif
47043 #endif
47044     int          required;
47045 #ifdef WOLFSSL_EARLY_DATA
47046     int          outSz;
47047 #endif
47048 #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
47049     int          groups[2] = { WOLFSSL_ECC_SECP256R1,
47050 #ifdef HAVE_PQC
47051                                WOLFSSL_SABER_LEVEL3
47052 #else
47053                                WOLFSSL_ECC_SECP256R1
47054 #endif
47055                              };
47056     int          bad_groups[2] = { 0xDEAD, 0xBEEF };
47057     int          numGroups = 2;
47058 #endif
47059 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
47060     char         groupList[] =
47061 #ifndef NO_ECC_SECP
47062 #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
47063             "P-521:"
47064 #endif
47065 #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
47066             "P-384:"
47067 #endif
47068 #if (!defined(NO_ECC256)  || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
47069             "P-256"
47070 #ifdef HAVE_PQC
47071             ":P256_SABER_LEVEL1"
47072 #endif
47073 #endif
47074 #ifdef HAVE_PQC
47075             ":KYBER_LEVEL1"
47076 #endif
47077             "";
47078 #endif /* !defined(NO_ECC_SECP) */
47079 #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
47080 
47081 #ifndef WOLFSSL_NO_TLS12
47082 #ifndef NO_WOLFSSL_CLIENT
47083     clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
47084     clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
47085 #endif
47086 #ifndef NO_WOLFSSL_SERVER
47087     serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
47088 #ifndef NO_CERTS
47089     wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
47090     wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
47091 #endif
47092     serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
47093 #endif
47094 #endif
47095 
47096 #ifndef NO_WOLFSSL_CLIENT
47097     clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
47098     clientSsl = wolfSSL_new(clientCtx);
47099 #endif
47100 #ifndef NO_WOLFSSL_SERVER
47101     serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
47102 #ifndef NO_CERTS
47103     wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
47104     wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
47105 #endif
47106     serverSsl = wolfSSL_new(serverCtx);
47107 #endif
47108 
47109 #ifdef WOLFSSL_SEND_HRR_COOKIE
47110     AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
47111 #ifndef NO_WOLFSSL_CLIENT
47112     AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
47113 #endif
47114 #ifndef NO_WOLFSSL_SERVER
47115 #ifndef WOLFSSL_NO_TLS12
47116     AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
47117 #endif
47118 
47119     AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
47120     AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
47121                 WOLFSSL_SUCCESS);
47122 #endif
47123 #endif
47124 
47125 #ifdef HAVE_SUPPORTED_CURVES
47126 #ifdef HAVE_ECC
47127     AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
47128 #ifndef NO_WOLFSSL_SERVER
47129     AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
47130                 WOLFSSL_SUCCESS);
47131 #endif
47132 #ifndef NO_WOLFSSL_CLIENT
47133 #ifndef WOLFSSL_NO_TLS12
47134     AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
47135                 WOLFSSL_SUCCESS);
47136 #endif
47137     AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
47138                 WOLFSSL_SUCCESS);
47139 #endif
47140 #elif defined(HAVE_CURVE25519)
47141     AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
47142 #ifndef NO_WOLFSSL_SERVER
47143     AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
47144                 WOLFSSL_SUCCESS);
47145 #endif
47146 #ifndef NO_WOLFSSL_CLIENT
47147 #ifndef WOLFSSL_NO_TLS12
47148     AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
47149                 WOLFSSL_SUCCESS);
47150 #endif
47151     AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
47152                 WOLFSSL_SUCCESS);
47153 #endif
47154 #elif defined(HAVE_CURVE448)
47155     AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
47156 #ifndef NO_WOLFSSL_SERVER
47157     AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
47158                 WOLFSSL_SUCCESS);
47159 #endif
47160 #ifndef NO_WOLFSSL_CLIENT
47161 #ifndef WOLFSSL_NO_TLS12
47162     AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
47163                 WOLFSSL_SUCCESS);
47164 #endif
47165     AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
47166                 WOLFSSL_SUCCESS);
47167 #endif
47168 #else
47169     AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
47170 #ifndef NO_WOLFSSL_CLIENT
47171 #ifndef WOLFSSL_NO_TLS12
47172     AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
47173                 NOT_COMPILED_IN);
47174 #endif
47175     AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
47176                 NOT_COMPILED_IN);
47177 #endif
47178 #endif
47179 
47180 #if defined(HAVE_PQC)
47181     AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
47182 #ifndef NO_WOLFSSL_SERVER
47183     AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
47184                 WOLFSSL_SUCCESS);
47185 #endif
47186 #ifndef NO_WOLFSSL_CLIENT
47187 #ifndef WOLFSSL_NO_TLS12
47188     AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
47189                 BAD_FUNC_ARG);
47190 #endif
47191     AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
47192                 WOLFSSL_SUCCESS);
47193 #endif
47194 #endif
47195 
47196     AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
47197 #ifndef NO_WOLFSSL_SERVER
47198     AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
47199 #endif
47200 #ifndef NO_WOLFSSL_CLIENT
47201 #ifndef WOLFSSL_NO_TLS12
47202     AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
47203 #endif
47204     AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
47205 #endif
47206 #endif /* HAVE_SUPPORTED_CURVES */
47207 
47208     AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
47209 #ifndef NO_WOLFSSL_CLIENT
47210     AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
47211 #endif
47212 #ifndef NO_WOLFSSL_SERVER
47213 #ifndef WOLFSSL_NO_TLS12
47214     AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
47215 #endif
47216     AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
47217 #endif
47218 
47219     AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
47220 #ifndef NO_WOLFSSL_CLIENT
47221     AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
47222 #endif
47223 #ifndef NO_WOLFSSL_SERVER
47224 #ifndef WOLFSSL_NO_TLS12
47225     AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
47226 #endif
47227     AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
47228 #endif
47229 
47230     AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
47231 #ifndef NO_WOLFSSL_CLIENT
47232 #ifndef WOLFSSL_NO_TLS12
47233     AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
47234 #endif
47235     AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
47236 #endif
47237 #ifndef NO_WOLFSSL_SERVER
47238     AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
47239 #endif
47240 
47241     AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
47242 #ifndef NO_WOLFSSL_CLIENT
47243 #ifndef WOLFSSL_NO_TLS12
47244     AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
47245 #endif
47246     AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
47247 #endif
47248 #ifndef NO_WOLFSSL_SERVER
47249     AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
47250 #endif
47251 
47252     AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
47253 #ifndef NO_WOLFSSL_CLIENT
47254 #ifndef WOLFSSL_NO_TLS12
47255     AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
47256 #endif
47257     AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
47258 #endif
47259 #ifndef NO_WOLFSSL_SERVER
47260     AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
47261 #endif
47262 
47263     AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
47264     AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
47265 #ifndef NO_WOLFSSL_CLIENT
47266 #ifndef WOLFSSL_NO_TLS12
47267     AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
47268                 BAD_FUNC_ARG);
47269 #endif
47270     AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
47271 #endif
47272 #ifndef NO_WOLFSSL_SERVER
47273     AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
47274 #endif
47275 
47276 #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
47277     AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
47278 #ifndef NO_WOLFSSL_SERVER
47279     AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
47280 #endif
47281 #ifndef NO_WOLFSSL_CLIENT
47282 #ifndef WOLFSSL_NO_TLS12
47283     AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
47284                 BAD_FUNC_ARG);
47285 #endif
47286     AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
47287 #endif
47288 
47289     AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
47290 #ifndef NO_WOLFSSL_SERVER
47291     AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
47292 #endif
47293 #ifndef NO_WOLFSSL_CLIENT
47294 #ifndef WOLFSSL_NO_TLS12
47295     AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
47296                 BAD_FUNC_ARG);
47297 #endif
47298     AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
47299 #endif
47300 
47301     AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
47302 #ifndef NO_WOLFSSL_CLIENT
47303     AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
47304 #endif
47305 #ifndef NO_WOLFSSL_SERVER
47306 #ifndef WOLFSSL_NO_TLS12
47307     AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
47308                 BAD_FUNC_ARG);
47309 #endif
47310     AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
47311 #endif
47312 #endif
47313 
47314 #ifdef HAVE_ECC
47315 #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
47316     AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
47317 #ifndef NO_WOLFSSL_SERVER
47318     AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
47319 #endif
47320 #ifndef NO_WOLFSSL_CLIENT
47321 #ifndef WOLFSSL_NO_TLS12
47322     AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
47323 #endif
47324     AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
47325 #endif
47326 #endif
47327 
47328 #ifdef HAVE_SUPPORTED_CURVES
47329     AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
47330 #ifndef NO_WOLFSSL_CLIENT
47331     AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
47332 #endif
47333     AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
47334 #ifndef NO_WOLFSSL_CLIENT
47335 #ifndef WOLFSSL_NO_TLS12
47336     AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
47337                 BAD_FUNC_ARG);
47338 #endif
47339     AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
47340                                        WOLFSSL_MAX_GROUP_COUNT + 1),
47341                 BAD_FUNC_ARG);
47342     AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
47343                 WOLFSSL_SUCCESS);
47344     AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
47345                 BAD_FUNC_ARG);
47346 #endif
47347 #ifndef NO_WOLFSSL_SERVER
47348     AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
47349                 WOLFSSL_SUCCESS);
47350     AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
47351                 BAD_FUNC_ARG);
47352 #endif
47353 
47354     AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
47355 #ifndef NO_WOLFSSL_CLIENT
47356     AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
47357 #endif
47358     AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
47359 #ifndef NO_WOLFSSL_CLIENT
47360 #ifndef WOLFSSL_NO_TLS12
47361     AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
47362                 BAD_FUNC_ARG);
47363 #endif
47364     AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
47365                                    WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
47366     AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
47367                 WOLFSSL_SUCCESS);
47368     AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
47369                 BAD_FUNC_ARG);
47370 #endif
47371 #ifndef NO_WOLFSSL_SERVER
47372     AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
47373                 WOLFSSL_SUCCESS);
47374     AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
47375                 BAD_FUNC_ARG);
47376 #endif
47377 
47378 #ifdef OPENSSL_EXTRA
47379     AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
47380 #ifndef NO_WOLFSSL_CLIENT
47381     AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
47382 #endif
47383     AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
47384 #ifndef NO_WOLFSSL_CLIENT
47385 #ifndef WOLFSSL_NO_TLS12
47386     AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
47387                 WOLFSSL_FAILURE);
47388 #endif
47389     AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
47390                 WOLFSSL_SUCCESS);
47391 #endif
47392 #ifndef NO_WOLFSSL_SERVER
47393     AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
47394                 WOLFSSL_SUCCESS);
47395 #endif
47396 
47397     AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
47398 #ifndef NO_WOLFSSL_CLIENT
47399     AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
47400 #endif
47401     AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
47402 #ifndef NO_WOLFSSL_CLIENT
47403 #ifndef WOLFSSL_NO_TLS12
47404     AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
47405                 WOLFSSL_FAILURE);
47406 #endif
47407     AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
47408                 WOLFSSL_SUCCESS);
47409 #endif
47410 #ifndef NO_WOLFSSL_SERVER
47411     AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
47412                 WOLFSSL_SUCCESS);
47413 #endif
47414 #endif /* OPENSSL_EXTRA */
47415 #endif /* HAVE_SUPPORTED_CURVES */
47416 #endif /* HAVE_ECC */
47417 
47418 #ifdef WOLFSSL_EARLY_DATA
47419 #ifndef OPENSSL_EXTRA
47420     AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
47421     AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
47422 #else
47423     AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
47424     AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
47425 #endif
47426 #ifndef NO_WOLFSSL_CLIENT
47427 #ifndef OPENSSL_EXTRA
47428     AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
47429     AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
47430 #else
47431     AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
47432     AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
47433 #endif
47434 #endif
47435 #ifndef NO_WOLFSSL_SERVER
47436 #ifndef WOLFSSL_NO_TLS12
47437 #ifndef OPENSSL_EXTRA
47438     AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
47439                 BAD_FUNC_ARG);
47440     AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
47441 #else
47442     AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
47443                 BAD_FUNC_ARG);
47444     AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
47445 #endif
47446 #endif
47447 #ifndef OPENSSL_EXTRA
47448     AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
47449     AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
47450 #else
47451     AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
47452     AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
47453 #endif
47454 #endif
47455 
47456 #ifndef OPENSSL_EXTRA
47457     AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
47458     AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
47459 #else
47460     AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
47461     AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
47462 #endif
47463 #ifndef NO_WOLFSSL_CLIENT
47464 #ifndef OPENSSL_EXTRA
47465     AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
47466     AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
47467 #else
47468     AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
47469     AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
47470 #endif
47471 #endif
47472 #ifndef NO_WOLFSSL_SERVER
47473 #ifndef WOLFSSL_NO_TLS12
47474 #ifndef OPENSSL_EXTRA
47475     AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
47476     AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
47477 #else
47478     AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
47479     AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
47480 #endif
47481 #endif
47482 #ifndef OPENSSL_EXTRA
47483     AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
47484     AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
47485 #else
47486     AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
47487     AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
47488 #endif
47489 #endif
47490 
47491 
47492     AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
47493                                          &outSz), BAD_FUNC_ARG);
47494 #ifndef NO_WOLFSSL_CLIENT
47495     AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
47496                                          &outSz), BAD_FUNC_ARG);
47497     AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
47498                 BAD_FUNC_ARG);
47499     AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
47500                                          sizeof(earlyData), NULL),
47501                 BAD_FUNC_ARG);
47502 #endif
47503 #ifndef NO_WOLFSSL_SERVER
47504     AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
47505                                          sizeof(earlyData), &outSz),
47506                 SIDE_ERROR);
47507 #endif
47508 #ifndef NO_WOLFSSL_CLIENT
47509 #ifndef WOLFSSL_NO_TLS12
47510     AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
47511                                          sizeof(earlyData), &outSz),
47512                 BAD_FUNC_ARG);
47513 #endif
47514     AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
47515                                          sizeof(earlyData), &outSz),
47516                 WOLFSSL_FATAL_ERROR);
47517 #endif
47518 
47519     AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
47520                                         sizeof(earlyDataBuffer), &outSz),
47521                 BAD_FUNC_ARG);
47522 #ifndef NO_WOLFSSL_SERVER
47523     AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
47524                                         sizeof(earlyDataBuffer), &outSz),
47525                 BAD_FUNC_ARG);
47526     AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
47527                 BAD_FUNC_ARG);
47528     AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
47529                                         sizeof(earlyDataBuffer), NULL),
47530                 BAD_FUNC_ARG);
47531 #endif
47532 #ifndef NO_WOLFSSL_CLIENT
47533     AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
47534                                         sizeof(earlyDataBuffer), &outSz),
47535                 SIDE_ERROR);
47536 #endif
47537 #ifndef NO_WOLFSSL_SERVER
47538 #ifndef WOLFSSL_NO_TLS12
47539     AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
47540                                         sizeof(earlyDataBuffer), &outSz),
47541                 BAD_FUNC_ARG);
47542 #endif
47543     AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
47544                                         sizeof(earlyDataBuffer), &outSz),
47545                 WOLFSSL_FATAL_ERROR);
47546 #endif
47547 #endif
47548 
47549 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
47550     AssertIntLT(SSL_get_early_data_status(NULL), 0);
47551 #endif
47552 
47553 
47554 #ifndef NO_WOLFSSL_SERVER
47555     wolfSSL_free(serverSsl);
47556     wolfSSL_CTX_free(serverCtx);
47557 #endif
47558 #ifndef NO_WOLFSSL_CLIENT
47559     wolfSSL_free(clientSsl);
47560     wolfSSL_CTX_free(clientCtx);
47561 #endif
47562 
47563 #ifndef WOLFSSL_NO_TLS12
47564 #ifndef NO_WOLFSSL_SERVER
47565     wolfSSL_free(serverTls12Ssl);
47566     wolfSSL_CTX_free(serverTls12Ctx);
47567 #endif
47568 #ifndef NO_WOLFSSL_CLIENT
47569     wolfSSL_free(clientTls12Ssl);
47570     wolfSSL_CTX_free(clientTls12Ctx);
47571 #endif
47572 #endif
47573 
47574     return ret;
47575 }
47576 
47577 #endif
47578 
47579 #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
47580     !defined(NO_OLD_TLS))
47581 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
47582         !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
47583         !defined(NO_AES) && defined(HAVE_AES_CBC) && \
47584          defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
47585 static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
47586         const unsigned char* priv, unsigned int privSz,
47587         const unsigned char* pubKeyDer, unsigned int pubKeySz,
47588         unsigned char* out, unsigned int* outlen,
47589         void* ctx)
47590 {
47591     int result;
47592     /* Test fail when context associated with WOLFSSL is NULL */
47593     if (ctx == NULL) {
47594         return -1;
47595     }
47596 
47597     (void)ssl;
47598     /* return 0 on success */
47599     PRIVATE_KEY_UNLOCK();
47600     result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
47601     PRIVATE_KEY_LOCK();
47602     return result;
47603 }
47604 
47605 static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
47606     wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
47607 #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
47608     AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
47609             WOLFSSL_SUCCESS);
47610 #endif
47611 #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
47612     AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
47613             WOLFSSL_SUCCESS);
47614 #endif
47615 }
47616 
47617 static void test_dh_ssl_setup(WOLFSSL* ssl)
47618 {
47619     static int dh_test_ctx = 1;
47620     int ret;
47621 
47622     wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
47623     AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
47624     ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
47625     if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
47626         AssertIntEQ(ret, WOLFSSL_SUCCESS);
47627     }
47628 }
47629 
47630 static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
47631 {
47632     int ret;
47633 
47634     wolfSSL_SetDhAgreeCtx(ssl, NULL);
47635     AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
47636     ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
47637     if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
47638         AssertIntEQ(ret, WOLFSSL_SUCCESS);
47639     }
47640 }
47641 #endif
47642 
47643 static void test_DhCallbacks(void)
47644 {
47645 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
47646         !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
47647         !defined(NO_AES) && defined(HAVE_AES_CBC) && \
47648          defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
47649     WOLFSSL_CTX *ctx;
47650     WOLFSSL     *ssl;
47651     tcp_ready   ready;
47652     func_args   server_args;
47653     func_args   client_args;
47654     THREAD_TYPE serverThread;
47655     callback_functions func_cb_client;
47656     callback_functions func_cb_server;
47657     int  test;
47658 
47659     printf(testingFmt, "test_DhCallbacks");
47660 
47661 #ifndef NO_WOLFSSL_CLIENT
47662     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
47663 #else
47664     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
47665 #endif
47666 
47667     AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
47668 
47669     wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
47670 
47671     /* load client ca cert */
47672     AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
47673             WOLFSSL_SUCCESS);
47674 
47675     /* test with NULL arguments */
47676     wolfSSL_SetDhAgreeCtx(NULL, &test);
47677     AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
47678 
47679     /* test success case */
47680     test = 1;
47681     AssertNotNull(ssl = wolfSSL_new(ctx));
47682     wolfSSL_SetDhAgreeCtx(ssl, &test);
47683     AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
47684 
47685     wolfSSL_free(ssl);
47686     wolfSSL_CTX_free(ctx);
47687 
47688     /* test a connection where callback is used */
47689 #ifdef WOLFSSL_TIRTOS
47690     fdOpenSession(Task_self());
47691 #endif
47692     XMEMSET(&server_args, 0, sizeof(func_args));
47693     XMEMSET(&client_args, 0, sizeof(func_args));
47694     XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
47695     XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
47696 
47697     StartTCP();
47698     InitTcpReady(&ready);
47699 
47700 #if defined(USE_WINDOWS_API)
47701     /* use RNG to get random port if using windows */
47702     ready.port = GetRandomPort();
47703 #endif
47704 
47705     server_args.signal = &ready;
47706     client_args.signal = &ready;
47707     server_args.return_code = TEST_FAIL;
47708     client_args.return_code = TEST_FAIL;
47709 
47710     /* set callbacks to use DH functions */
47711     func_cb_client.ctx_ready = &test_dh_ctx_setup;
47712     func_cb_client.ssl_ready = &test_dh_ssl_setup;
47713 #ifndef WOLFSSL_NO_TLS12
47714     func_cb_client.method = wolfTLSv1_2_client_method;
47715 #else
47716     func_cb_client.method = wolfTLSv1_3_client_method;
47717 #endif
47718     client_args.callbacks = &func_cb_client;
47719 
47720     func_cb_server.ctx_ready = &test_dh_ctx_setup;
47721     func_cb_server.ssl_ready = &test_dh_ssl_setup;
47722 #ifndef WOLFSSL_NO_TLS12
47723     func_cb_server.method = wolfTLSv1_2_server_method;
47724 #else
47725     func_cb_server.method = wolfTLSv1_3_server_method;
47726 #endif
47727     server_args.callbacks = &func_cb_server;
47728 
47729     start_thread(test_server_nofail, &server_args, &serverThread);
47730     wait_tcp_ready(&server_args);
47731     test_client_nofail(&client_args, NULL);
47732     join_thread(serverThread);
47733 
47734     AssertTrue(client_args.return_code);
47735     AssertTrue(server_args.return_code);
47736 
47737     FreeTcpReady(&ready);
47738 
47739 #ifdef WOLFSSL_TIRTOS
47740     fdOpenSession(Task_self());
47741 #endif
47742 
47743     /* now set user ctx to not be 1 so that the callback returns fail case */
47744 #ifdef WOLFSSL_TIRTOS
47745     fdOpenSession(Task_self());
47746 #endif
47747     XMEMSET(&server_args, 0, sizeof(func_args));
47748     XMEMSET(&client_args, 0, sizeof(func_args));
47749     XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
47750     XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
47751 
47752     StartTCP();
47753     InitTcpReady(&ready);
47754 
47755 #if defined(USE_WINDOWS_API)
47756     /* use RNG to get random port if using windows */
47757     ready.port = GetRandomPort();
47758 #endif
47759 
47760     server_args.signal = &ready;
47761     client_args.signal = &ready;
47762     server_args.return_code = TEST_FAIL;
47763     client_args.return_code = TEST_FAIL;
47764 
47765     /* set callbacks to use DH functions */
47766     func_cb_client.ctx_ready = &test_dh_ctx_setup;
47767     func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
47768 #ifndef WOLFSSL_NO_TLS12
47769     func_cb_client.method = wolfTLSv1_2_client_method;
47770 #else
47771     func_cb_client.method = wolfTLSv1_3_client_method;
47772 #endif
47773     client_args.callbacks = &func_cb_client;
47774 
47775     func_cb_server.ctx_ready = &test_dh_ctx_setup;
47776     func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
47777 #ifndef WOLFSSL_NO_TLS12
47778     func_cb_server.method = wolfTLSv1_2_server_method;
47779 #else
47780     func_cb_server.method = wolfTLSv1_3_server_method;
47781 #endif
47782     server_args.callbacks = &func_cb_server;
47783 
47784     start_thread(test_server_nofail, &server_args, &serverThread);
47785     wait_tcp_ready(&server_args);
47786     test_client_nofail(&client_args, NULL);
47787     join_thread(serverThread);
47788 
47789     AssertIntEQ(client_args.return_code, TEST_FAIL);
47790     AssertIntEQ(server_args.return_code, TEST_FAIL);
47791 
47792     FreeTcpReady(&ready);
47793 
47794 #ifdef WOLFSSL_TIRTOS
47795     fdOpenSession(Task_self());
47796 #endif
47797 
47798     printf(resultFmt, passed);
47799 #endif
47800 }
47801 #endif /* HAVE_PK_CALLBACKS */
47802 
47803 #ifdef HAVE_HASHDRBG
47804 
47805 #ifdef TEST_RESEED_INTERVAL
47806 static int test_wc_RNG_GenerateBlock_Reseed(void)
47807 {
47808     int i, ret;
47809     WC_RNG rng;
47810     byte key[32];
47811 
47812     ret = wc_InitRng(&rng);
47813 
47814     if (ret == 0) {
47815         for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
47816             ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
47817             if (ret != 0) {
47818                 break;
47819             }
47820         }
47821     }
47822 
47823     wc_FreeRng(&rng);
47824 
47825     return ret;
47826 }
47827 #endif /* TEST_RESEED_INTERVAL */
47828 
47829 static int test_wc_RNG_GenerateBlock(void)
47830 {
47831     int i, ret;
47832     WC_RNG rng;
47833     byte key[32];
47834 
47835     ret = wc_InitRng(&rng);
47836 
47837     if (ret == 0) {
47838         for(i = 0; i < 10; i++) {
47839             ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
47840             if (ret != 0) {
47841                 break;
47842             }
47843         }
47844     }
47845 
47846     wc_FreeRng(&rng);
47847 
47848     (void)rng; /* for WC_NO_RNG case */
47849     (void)key;
47850 
47851     return ret;
47852 }
47853 #endif
47854 /*
47855  * Testing get_rand_digit
47856  */
47857 static int test_get_rand_digit (void)
47858 {
47859     int ret = 0;
47860 #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
47861 
47862     WC_RNG      rng;
47863     mp_digit    d;
47864 
47865     printf(testingFmt, "get_rand_digit()");
47866 
47867     ret = wc_InitRng(&rng);
47868 
47869     if (ret == 0) {
47870         ret = get_rand_digit(&rng, &d);
47871     }
47872     if (ret == 0) {
47873         ret = get_rand_digit(NULL, NULL);
47874         if (ret == BAD_FUNC_ARG) {
47875             ret = 0;
47876         }
47877     }
47878     if (ret == 0) {
47879         ret = get_rand_digit(NULL, &d);
47880         if (ret == BAD_FUNC_ARG) {
47881             ret = 0;
47882         }
47883     }
47884     if (ret == 0) {
47885         ret = get_rand_digit(&rng, NULL);
47886         if (ret == BAD_FUNC_ARG) {
47887             ret = 0;
47888         }
47889     }
47890 
47891     if (ret == 0) {
47892         ret = wc_FreeRng(&rng);
47893     }
47894 
47895     printf(resultFmt, ret == 0 ? passed : failed);
47896 #endif
47897     return ret;
47898 
47899 }/* End test_get_rand_digit*/
47900 /*
47901  * Testing get_digit_count
47902  */
47903 static int test_get_digit_count (void)
47904 {
47905     int ret = 0;
47906 #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
47907     mp_int a;
47908 
47909     printf(testingFmt, "get_digit_count()");
47910 
47911     if (mp_init(&a) != MP_OKAY) {
47912         ret = -1;
47913     }
47914     if (ret == 0) {
47915         ret = get_digit_count(NULL);
47916     }
47917     if (ret == 0) {
47918         ret = get_digit_count(&a);
47919     }
47920 
47921     printf(resultFmt, ret == 0 ? passed : failed);
47922     mp_clear(&a);
47923 #endif
47924     return ret;
47925 
47926 }/* End test_get_digit_count*/
47927 /*
47928  * Testing mp_cond_copy
47929  */
47930 static int test_mp_cond_copy (void)
47931 {
47932     int     ret = 0;
47933 #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
47934     defined(WOLFSSL_PUBLIC_MP)
47935     mp_int  a;
47936     mp_int  b;
47937     int     copy = 0;
47938 
47939     printf(testingFmt, "mp_cond_copy()");
47940 
47941     if (mp_init(&a) != MP_OKAY) {
47942         ret = -1;
47943     }
47944     if (ret == 0) {
47945         if (mp_init(&b) != MP_OKAY) {
47946             ret = -1;
47947         }
47948     }
47949     if (ret == 0) {
47950         ret = mp_cond_copy(NULL, copy, NULL);
47951         if (ret == BAD_FUNC_ARG) {
47952             ret = 0;
47953         }
47954     }
47955     if (ret == 0) {
47956         ret = mp_cond_copy(NULL, copy, &b);
47957         if (ret == BAD_FUNC_ARG) {
47958             ret = 0;
47959         }
47960     }
47961     if (ret == 0) {
47962         ret = mp_cond_copy(&a, copy, NULL);
47963         if (ret == BAD_FUNC_ARG) {
47964             ret = 0;
47965         }
47966     }
47967     if (ret == 0) {
47968         ret = mp_cond_copy(&a, copy, &b);
47969     }
47970 
47971     printf(resultFmt, ret == 0 ? passed : failed);
47972     mp_clear(&a);
47973     mp_clear(&b);
47974 #endif
47975     return ret;
47976 
47977 }/* End test_mp_cond_copy*/
47978 /*
47979  * Testing mp_rand
47980  */
47981 static int test_mp_rand (void)
47982 {
47983     int ret = 0;
47984 #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
47985     mp_int  a;
47986     int     digits = 1;
47987     WC_RNG  rng;
47988 
47989     printf(testingFmt, "mp_rand()");
47990 
47991     if (mp_init(&a) != MP_OKAY) {
47992         ret = -1;
47993     }
47994     if (ret == 0) {
47995         ret = wc_InitRng(&rng);
47996     }
47997 
47998     if (ret == 0) {
47999         ret = mp_rand(&a, digits, NULL);
48000         if (ret == MISSING_RNG_E) {
48001             ret = 0;
48002         }
48003     }
48004     if (ret == 0) {
48005         ret = mp_rand(NULL, digits, &rng);
48006         if (ret == BAD_FUNC_ARG) {
48007             ret = 0;
48008         }
48009     }
48010     if (ret == 0) {
48011         ret = mp_rand(&a, 0, &rng);
48012         if (ret == BAD_FUNC_ARG) {
48013             ret = 0;
48014         }
48015     }
48016     if (ret == 0) {
48017         ret = mp_rand(&a, digits, &rng);
48018     }
48019 
48020     printf(resultFmt, ret == 0 ? passed : failed);
48021     mp_clear(&a);
48022     wc_FreeRng(&rng);
48023 #endif
48024     return ret;
48025 }/* End test_mp_rand*/
48026 /*
48027  * Testing get_digit
48028  */
48029 static int test_get_digit (void)
48030 {
48031     int     ret = 0;
48032 #if defined(WOLFSSL_PUBLIC_MP)
48033     mp_int  a;
48034     int     n = 0;
48035 
48036     printf(testingFmt, "get_digit()");
48037 
48038     if (mp_init(&a) != MP_OKAY) {
48039         ret = -1;
48040     }
48041     if (ret == 0) {
48042         if (get_digit(NULL, n) != 0) { /* Should not hit this */
48043             ret = -1;
48044         }
48045     }
48046     if (ret == 0) {
48047         if (get_digit(NULL, n) == 0) { /* Should hit this */
48048             ret = 0;
48049         }
48050     }
48051     if (ret == 0) {
48052         if (get_digit(&a, n) != 0) { /* Should not hit this */
48053             ret = -1;
48054         }
48055     }
48056     if (ret == 0) {
48057         if (get_digit(&a, n) == 0) { /* Should hit this */
48058             ret = 0;
48059         }
48060     }
48061 
48062 
48063     printf(resultFmt, ret == 0 ? passed : failed);
48064     mp_clear(&a);
48065 #endif
48066     return ret;
48067 }/* End test_get_digit*/
48068 /*
48069  * Testing wc_export_int
48070  */
48071 static int test_wc_export_int(void)
48072 {
48073     int         ret = 0;
48074 #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
48075     defined(WOLFSSL_PUBLIC_MP)
48076     mp_int      mp;
48077     byte        buf[32];
48078     word32      keySz = (word32)sizeof(buf);
48079     word32      len = (word32)sizeof(buf);
48080 
48081     printf(testingFmt, "wc_export_int()");
48082 
48083     if (mp_init(&mp) != MP_OKAY) {
48084         ret = -1;
48085     }
48086     if (ret == 0) {
48087         ret = mp_set(&mp, 1234);
48088     }
48089     if (ret == 0) {
48090         ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
48091         if (ret == BAD_FUNC_ARG) {
48092             ret = 0;
48093         }
48094     }
48095     if (ret == 0) {
48096         len = sizeof(buf)-1;
48097         ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
48098         if (ret == BUFFER_E) {
48099             ret = 0;
48100         }
48101     }
48102     if (ret == 0) {
48103         len = sizeof(buf);
48104         ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
48105     }
48106     if (ret == 0) {
48107         len = 4; /* test input too small */
48108         ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
48109         if (ret == BUFFER_E) {
48110             ret = 0;
48111         }
48112     }
48113     if (ret == 0) {
48114         len = sizeof(buf);
48115         ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
48116         /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
48117         if (ret == 0 && len != 5) {
48118             ret = BAD_FUNC_ARG;
48119         }
48120     }
48121 
48122     printf(resultFmt, ret == 0 ? passed : failed);
48123     mp_clear(&mp);
48124 #endif
48125     return ret;
48126 
48127 }/* End test_wc_export_int*/
48128 static int test_wc_InitRngNonce(void)
48129 {
48130     int     ret=0;
48131 #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
48132    (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
48133     WC_RNG  rng;
48134     byte    nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
48135                       "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
48136     word32  nonceSz = sizeof(nonce);
48137 
48138 
48139     printf(testingFmt, "wc_InitRngNonce()");
48140 
48141     if (ret == 0){
48142         ret = wc_InitRngNonce(&rng, nonce, nonceSz);
48143     }
48144 
48145     wc_FreeRng(&rng);
48146 
48147     printf(resultFmt, ret == 0 ? passed : failed);
48148 #endif
48149     return ret;
48150 }/* End test_wc_InitRngNonce*/
48151 /*
48152  * Testing wc_InitRngNonce_ex
48153  */
48154 static int test_wc_InitRngNonce_ex(void)
48155 {
48156     int     ret=0;
48157 #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
48158    (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
48159     WC_RNG  rng;
48160     byte    nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
48161                       "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
48162     word32  nonceSz = sizeof(nonce);
48163 
48164     printf(testingFmt, "wc_InitRngNonce_ex()");
48165 
48166     if (ret == 0){
48167         ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
48168     }
48169 
48170     wc_FreeRng(&rng);
48171 
48172     printf(resultFmt, ret == 0 ? passed : failed);
48173 #endif
48174     return ret;
48175 }/*End test_wc_InitRngNonce_ex*/
48176 
48177 
48178 
48179 static void test_wolfSSL_X509_CRL(void)
48180 {
48181 #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
48182 
48183     X509_CRL *crl;
48184     char pem[][100] = {
48185         "./certs/crl/crl.pem",
48186         "./certs/crl/crl2.pem",
48187         "./certs/crl/caEccCrl.pem",
48188         "./certs/crl/eccCliCRL.pem",
48189         "./certs/crl/eccSrvCRL.pem",
48190         ""
48191     };
48192 #ifndef NO_BIO
48193     BIO *bio;
48194 #endif
48195 
48196 #ifdef HAVE_TEST_d2i_X509_CRL_fp
48197     char der[][100] = {
48198         "./certs/crl/crl.der",
48199         "./certs/crl/crl2.der",
48200         ""};
48201 #endif
48202 
48203     XFILE fp;
48204     int i;
48205 
48206     printf(testingFmt, "test_wolfSSL_X509_CRL");
48207 
48208     for (i = 0; pem[i][0] != '\0'; i++)
48209     {
48210         fp = XFOPEN(pem[i], "rb");
48211         AssertTrue((fp != XBADFILE));
48212         AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
48213         AssertNotNull(crl);
48214         X509_CRL_free(crl);
48215         XFCLOSE(fp);
48216         fp = XFOPEN(pem[i], "rb");
48217         AssertTrue((fp != XBADFILE));
48218         AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
48219         AssertNotNull(crl);
48220         X509_CRL_free(crl);
48221         XFCLOSE(fp);
48222     }
48223 
48224 #ifndef NO_BIO
48225     for (i = 0; pem[i][0] != '\0'; i++)
48226     {
48227         AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
48228         AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
48229         X509_CRL_free(crl);
48230         BIO_free(bio);
48231     }
48232 #endif
48233 
48234 #ifdef HAVE_TEST_d2i_X509_CRL_fp
48235     for(i = 0; der[i][0] != '\0'; i++){
48236         fp = XFOPEN(der[i], "rb");
48237         AssertTrue((fp != XBADFILE));
48238         AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
48239         AssertNotNull(crl);
48240         X509_CRL_free(crl);
48241         XFCLOSE(fp);
48242         fp = XFOPEN(der[i], "rb");
48243         AssertTrue((fp != XBADFILE));
48244         AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
48245         AssertNotNull(crl);
48246         X509_CRL_free(crl);
48247         XFCLOSE(fp);
48248     }
48249 #endif
48250 
48251     printf(resultFmt, passed);
48252 #endif
48253         return;
48254 }
48255 
48256 static void test_wolfSSL_X509_load_crl_file(void)
48257 {
48258 #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
48259     !defined(NO_RSA) && !defined(NO_BIO)
48260     int i;
48261     char pem[][100] = {
48262         "./certs/crl/crl.pem",
48263         "./certs/crl/crl2.pem",
48264         "./certs/crl/caEccCrl.pem",
48265         "./certs/crl/eccCliCRL.pem",
48266         "./certs/crl/eccSrvCRL.pem",
48267         ""
48268     };
48269     char der[][100] = {
48270         "./certs/crl/crl.der",
48271         "./certs/crl/crl2.der",
48272         ""
48273     };
48274     WOLFSSL_X509_STORE*  store;
48275     WOLFSSL_X509_LOOKUP* lookup;
48276 
48277     printf(testingFmt, "wolfSSL_X509_load_crl_file");
48278 
48279     AssertNotNull(store = wolfSSL_X509_STORE_new());
48280     AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
48281 
48282     AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
48283                                               X509_FILETYPE_PEM), 1);
48284     AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
48285                                               X509_FILETYPE_PEM), 1);
48286     if (store) {
48287         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
48288                     WOLFSSL_FILETYPE_PEM), 1);
48289         /* since store hasn't yet known the revoked cert*/
48290         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
48291                     WOLFSSL_FILETYPE_PEM), 1);
48292     }
48293 
48294     for (i = 0; pem[i][0] != '\0'; i++)
48295     {
48296         AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
48297     }
48298 
48299     if (store) {
48300         /* since store knows crl list */
48301         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
48302                    WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
48303     }
48304     /* once feeing store */
48305     X509_STORE_free(store);
48306     store = NULL;
48307 
48308     AssertNotNull(store = wolfSSL_X509_STORE_new());
48309     AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
48310 
48311     AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
48312                                               X509_FILETYPE_PEM), 1);
48313     AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
48314                                               X509_FILETYPE_PEM), 1);
48315     if (store) {
48316         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
48317                     WOLFSSL_FILETYPE_PEM), 1);
48318         /* since store hasn't yet known the revoked cert*/
48319         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
48320                     WOLFSSL_FILETYPE_PEM), 1);
48321     }
48322 
48323     for (i = 0; der[i][0] != '\0'; i++)
48324     {
48325         AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
48326     }
48327 
48328     if (store) {
48329         /* since store knows crl list */
48330         AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
48331                    WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
48332     }
48333 
48334     /* test for incorrect parameter */
48335     AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
48336     AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
48337     AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
48338 
48339     X509_STORE_free(store);
48340     store = NULL;
48341 
48342     printf(resultFmt, passed);
48343 #endif
48344 }
48345 
48346 static void test_wolfSSL_d2i_X509_REQ(void)
48347 {
48348 #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
48349     (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA))
48350     /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
48351      * generated by libest
48352      * ./certs/csr.attr.der contains sample attributes
48353      * ./certs/csr.ext.der contains sample extensions */
48354     const char* csrFile = "./certs/csr.signed.der";
48355     const char* csrPopFile = "./certs/csr.attr.der";
48356     const char* csrExtFile = "./certs/csr.ext.der";
48357     /* ./certs/csr.dsa.pem is generated using
48358      * openssl req -newkey dsa:certs/dsaparams.pem \
48359      *     -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
48360      *     -outform PEM
48361      * with the passphrase "wolfSSL"
48362      */
48363 #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
48364     const char* csrDsaFile = "./certs/csr.dsa.pem";
48365     XFILE f;
48366 #endif
48367     BIO* bio = NULL;
48368     X509* req = NULL;
48369     EVP_PKEY *pub_key = NULL;
48370 
48371     {
48372         AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
48373         AssertNotNull(d2i_X509_REQ_bio(bio, &req));
48374 
48375         /*
48376          * Extract the public key from the CSR
48377          */
48378         AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
48379 
48380         /*
48381          * Verify the signature in the CSR
48382          */
48383         AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
48384 
48385         X509_free(req);
48386         BIO_free(bio);
48387         EVP_PKEY_free(pub_key);
48388     }
48389     {
48390 #ifdef OPENSSL_ALL
48391         X509_ATTRIBUTE* attr;
48392         ASN1_TYPE *at;
48393 #endif
48394         AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
48395         AssertNotNull(d2i_X509_REQ_bio(bio, &req));
48396 
48397         /*
48398          * Extract the public key from the CSR
48399          */
48400         AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
48401 
48402         /*
48403          * Verify the signature in the CSR
48404          */
48405         AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
48406 
48407 #ifdef OPENSSL_ALL
48408         /*
48409          * Obtain the challenge password from the CSR
48410          */
48411         AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
48412                 NID_pkcs9_challengePassword);
48413         AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
48414         AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
48415         AssertNotNull(at->value.asn1_string);
48416         AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
48417         AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
48418 #endif
48419 
48420         X509_free(req);
48421         BIO_free(bio);
48422         EVP_PKEY_free(pub_key);
48423     }
48424     {
48425 #ifdef OPENSSL_ALL
48426         X509_ATTRIBUTE* attr;
48427         ASN1_TYPE *at;
48428         STACK_OF(X509_EXTENSION) *exts = NULL;
48429 #endif
48430         AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
48431         /* This CSR contains an Extension Request attribute so
48432          * we test extension parsing in a CSR attribute here. */
48433         AssertNotNull(d2i_X509_REQ_bio(bio, &req));
48434 
48435         /*
48436          * Extract the public key from the CSR
48437          */
48438         AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
48439 
48440         /*
48441          * Verify the signature in the CSR
48442          */
48443         AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
48444 
48445 #ifdef OPENSSL_ALL
48446         AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
48447         AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
48448         sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
48449         /*
48450          * Obtain the challenge password from the CSR
48451          */
48452         AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
48453                 NID_pkcs9_challengePassword);
48454         AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
48455         AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
48456         AssertNotNull(at->value.asn1_string);
48457         AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
48458         AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
48459         AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
48460 #endif
48461 
48462         X509_free(req);
48463         BIO_free(bio);
48464         EVP_PKEY_free(pub_key);
48465     }
48466 #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
48467     {
48468         AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
48469         AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
48470 
48471         /*
48472          * Extract the public key from the CSR
48473          */
48474         AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
48475 
48476         /*
48477          * Verify the signature in the CSR
48478          */
48479         AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
48480 
48481         X509_free(req);
48482         BIO_free(bio);
48483 
48484         /* Run the same test, but with a file pointer instead of a BIO.
48485          * (PEM_read_X509_REQ)*/
48486         AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
48487         AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
48488         AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
48489 
48490         X509_free(req);
48491         EVP_PKEY_free(pub_key);
48492     }
48493 #endif /* !NO_DSA && !HAVE_SELFTEST */
48494 #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
48495 }
48496 
48497 static void test_wolfSSL_PEM_read_X509(void)
48498 {
48499 #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
48500     !defined(NO_RSA)
48501     X509 *x509 = NULL;
48502     XFILE fp;
48503 
48504     printf(testingFmt, "wolfSSL_PEM_read_X509");
48505     fp = XFOPEN(svrCertFile, "rb");
48506     AssertTrue((fp != XBADFILE));
48507     AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
48508     X509_free(x509);
48509     XFCLOSE(fp);
48510 
48511     printf(resultFmt, passed);
48512 #endif
48513 }
48514 
48515 static void test_wolfSSL_PEM_read(void)
48516 {
48517 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
48518     const char* filename = "./certs/server-keyEnc.pem";
48519     XFILE fp;
48520     char* name = NULL;
48521     char* header = NULL;
48522     byte* data = NULL;
48523     long len;
48524     EVP_CIPHER_INFO cipher;
48525     WOLFSSL_BIO* bio;
48526     byte* fileData;
48527     size_t fileDataSz;
48528     byte* out;
48529 
48530     printf(testingFmt, "wolfSSL_PEM_read");
48531     fp = XFOPEN(filename, "rb");
48532     AssertTrue((fp != XBADFILE));
48533 
48534     /* Fail cases. */
48535     AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
48536     AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
48537     AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
48538     AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
48539 
48540     AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
48541 
48542     AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
48543     AssertIntGT(XSTRLEN(header), 0);
48544     AssertIntGT(len, 0);
48545 
48546     AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
48547     AssertIntGT((fileDataSz = XFTELL(fp)), 0);
48548     AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
48549     AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
48550                                                       DYNAMIC_TYPE_TMP_BUFFER));
48551     AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
48552     XFCLOSE(fp);
48553 
48554     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
48555 
48556     /* Fail cases. */
48557     AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
48558     AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
48559     AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
48560     AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
48561 
48562     AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
48563     AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
48564     AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
48565 
48566     /* Fail cases. */
48567     AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
48568     AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
48569     AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
48570 
48571 #ifndef NO_DES3
48572     AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
48573 #endif
48574 
48575     /* Fail cases. */
48576     AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
48577                               (void*)"yassl123"), WOLFSSL_FAILURE);
48578     AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
48579                               (void*)"yassl123"), WOLFSSL_FAILURE);
48580     AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
48581                               (void*)"yassl123"), WOLFSSL_FAILURE);
48582 
48583 #if !defined(NO_DES3) && !defined(NO_MD5)
48584     AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
48585                               (void*)"yassl123"), WOLFSSL_SUCCESS);
48586 #endif
48587 
48588     BIO_free(bio);
48589     XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48590     XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48591     XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48592     XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48593 
48594     name = NULL;
48595     header = NULL;
48596     data = NULL;
48597     fp = XFOPEN(svrKeyFile, "rb");
48598     AssertTrue((fp != XBADFILE));
48599     AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
48600     AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
48601     AssertIntEQ(XSTRLEN(header), 0);
48602     AssertIntGT(len, 0);
48603 
48604     AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
48605     AssertIntGT((fileDataSz = XFTELL(fp)), 0);
48606     AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
48607     AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
48608                                                       DYNAMIC_TYPE_TMP_BUFFER));
48609     AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
48610     XFCLOSE(fp);
48611 
48612     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
48613     AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
48614     AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
48615     AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
48616 
48617     BIO_free(bio);
48618     XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48619     XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48620     XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48621     XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
48622 
48623     printf(resultFmt, passed);
48624 #endif
48625 }
48626 
48627 static void test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
48628 {
48629 #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
48630     !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
48631     const byte iv[12] = { 0 };
48632     const byte key[16] = { 0 };
48633     const byte cleartext[16] = { 0 };
48634     const byte aad[] = {
48635         0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
48636         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
48637         0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
48638         0x4f
48639     };
48640     byte out1Part[16];
48641     byte outTag1Part[16];
48642     byte out2Part[16];
48643     byte outTag2Part[16];
48644     byte decryptBuf[16];
48645     int len;
48646     int tlen;
48647     EVP_CIPHER_CTX* ctx = NULL;
48648 
48649     printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
48650 
48651     /* ENCRYPT */
48652     /* Send AAD and data in 1 part */
48653     AssertNotNull(ctx = EVP_CIPHER_CTX_new());
48654     tlen = 0;
48655     AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
48656                 1);
48657     AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
48658     AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
48659     AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
48660                                   sizeof(cleartext)), 1);
48661     tlen += len;
48662     AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
48663     tlen += len;
48664     AssertIntEQ(tlen, sizeof(cleartext));
48665     AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
48666                                     outTag1Part), 1);
48667     EVP_CIPHER_CTX_free(ctx);
48668 
48669     /* DECRYPT */
48670     /* Send AAD and data in 1 part */
48671     AssertNotNull(ctx = EVP_CIPHER_CTX_new());
48672     tlen = 0;
48673     AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
48674                 1);
48675     AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
48676     AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
48677     AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
48678                                   sizeof(cleartext)), 1);
48679     tlen += len;
48680     AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
48681                                     outTag1Part), 1);
48682     AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
48683     tlen += len;
48684     AssertIntEQ(tlen, sizeof(cleartext));
48685     EVP_CIPHER_CTX_free(ctx);
48686 
48687     AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
48688 
48689     /* ENCRYPT */
48690     /* Send AAD and data in 2 parts */
48691     AssertNotNull(ctx = EVP_CIPHER_CTX_new());
48692     tlen = 0;
48693     AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
48694                 1);
48695     AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
48696     AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
48697     AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
48698                 1);
48699     AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
48700     tlen += len;
48701     AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
48702                                   sizeof(cleartext) - 1), 1);
48703     tlen += len;
48704     AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
48705     tlen += len;
48706     AssertIntEQ(tlen, sizeof(cleartext));
48707     AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
48708                                     outTag2Part), 1);
48709 
48710     AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
48711     AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
48712     EVP_CIPHER_CTX_free(ctx);
48713 
48714     /* DECRYPT */
48715     /* Send AAD and data in 2 parts */
48716     AssertNotNull(ctx = EVP_CIPHER_CTX_new());
48717     tlen = 0;
48718     AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
48719                 1);
48720     AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
48721     AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
48722     AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
48723                 1);
48724     AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
48725     tlen += len;
48726     AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
48727                                   sizeof(cleartext) - 1), 1);
48728     tlen += len;
48729     AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
48730                                     outTag1Part), 1);
48731     AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
48732     tlen += len;
48733     AssertIntEQ(tlen, sizeof(cleartext));
48734 
48735     AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
48736 
48737     /* Test AAD re-use */
48738     EVP_CIPHER_CTX_free(ctx);
48739 
48740     printf(resultFmt, passed);
48741 #endif
48742 }
48743 
48744 #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
48745     !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
48746 static void test_wolfssl_EVP_aes_gcm_zeroLen(void)
48747 {
48748     /* Zero length plain text */
48749 
48750     byte key[] = {
48751         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
48752         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
48753         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
48754         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
48755     }; /* align */
48756     byte iv[]  = {
48757         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
48758     }; /* align */
48759     byte plaintxt[1];
48760     int ivSz  = 12;
48761     int plaintxtSz = 0;
48762     unsigned char tag[16];
48763     unsigned char tag_kat[] =
48764         {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
48765         0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
48766 
48767     byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
48768     byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
48769     int ciphertxtSz = 0;
48770     int decryptedtxtSz = 0;
48771     int len = 0;
48772 
48773     EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
48774     EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
48775 
48776     AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
48777     AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
48778     AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
48779                                      plaintxtSz));
48780     AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
48781     ciphertxtSz += len;
48782     AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
48783     AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
48784 
48785     AssertIntEQ(0, ciphertxtSz);
48786     AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
48787 
48788     EVP_CIPHER_CTX_init(de);
48789     AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
48790     AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
48791     AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
48792     decryptedtxtSz = len;
48793     AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
48794     AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
48795     decryptedtxtSz += len;
48796     AssertIntEQ(0, decryptedtxtSz);
48797 
48798     EVP_CIPHER_CTX_free(en);
48799     EVP_CIPHER_CTX_free(de);
48800 }
48801 #endif
48802 
48803 static void test_wolfssl_EVP_aes_gcm(void)
48804 {
48805 #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
48806     !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
48807 
48808     /* A 256 bit key, AES_128 will use the first 128 bit*/
48809     byte *key = (byte*)"01234567890123456789012345678901";
48810     /* A 128 bit IV */
48811     byte *iv = (byte*)"0123456789012345";
48812     int ivSz = AES_BLOCK_SIZE;
48813     /* Message to be encrypted */
48814     byte *plaintxt = (byte*)"for things to change you have to change";
48815     /* Additional non-confidential data */
48816     byte *aad = (byte*)"Don't spend major time on minor things.";
48817 
48818     unsigned char tag[AES_BLOCK_SIZE] = {0};
48819     int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
48820     int aadSz = (int)XSTRLEN((char*)aad);
48821     byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
48822     byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
48823     int ciphertxtSz = 0;
48824     int decryptedtxtSz = 0;
48825     int len = 0;
48826     int i = 0;
48827     EVP_CIPHER_CTX en[2];
48828     EVP_CIPHER_CTX de[2];
48829 
48830     printf(testingFmt, "wolfssl_EVP_aes_gcm");
48831 
48832     for (i = 0; i < 2; i++) {
48833 
48834         EVP_CIPHER_CTX_init(&en[i]);
48835 
48836         if (i == 0) {
48837             /* Default uses 96-bits IV length */
48838 #ifdef WOLFSSL_AES_128
48839             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
48840 #elif defined(WOLFSSL_AES_192)
48841             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
48842 #elif defined(WOLFSSL_AES_256)
48843             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
48844 #endif
48845         }
48846         else {
48847 #ifdef WOLFSSL_AES_128
48848             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
48849 #elif defined(WOLFSSL_AES_192)
48850             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
48851 #elif defined(WOLFSSL_AES_256)
48852             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
48853 #endif
48854              /* non-default must to set the IV length first */
48855             AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
48856             AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
48857         }
48858         AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
48859         AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
48860         ciphertxtSz = len;
48861         AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
48862         ciphertxtSz += len;
48863         AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
48864         AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
48865 
48866         EVP_CIPHER_CTX_init(&de[i]);
48867         if (i == 0) {
48868             /* Default uses 96-bits IV length */
48869 #ifdef WOLFSSL_AES_128
48870             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
48871 #elif defined(WOLFSSL_AES_192)
48872             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
48873 #elif defined(WOLFSSL_AES_256)
48874             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
48875 #endif
48876         }
48877         else {
48878 #ifdef WOLFSSL_AES_128
48879             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
48880 #elif defined(WOLFSSL_AES_192)
48881             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
48882 #elif defined(WOLFSSL_AES_256)
48883             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
48884 #endif
48885             /* non-default must to set the IV length first */
48886             AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
48887             AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
48888 
48889         }
48890         AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
48891         AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
48892         decryptedtxtSz = len;
48893         AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
48894         AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
48895         decryptedtxtSz += len;
48896         AssertIntEQ(ciphertxtSz, decryptedtxtSz);
48897         AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
48898 
48899         /* modify tag*/
48900         tag[AES_BLOCK_SIZE-1]+=0xBB;
48901         AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
48902         AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
48903         /* fail due to wrong tag */
48904         AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
48905         AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
48906         AssertIntEQ(0, len);
48907         AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
48908     }
48909 
48910     test_wolfssl_EVP_aes_gcm_zeroLen();
48911 
48912     printf(resultFmt, passed);
48913 
48914 #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
48915 }
48916 
48917 #ifndef NO_BIO
48918 static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
48919 {
48920 #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
48921     BIO* bio;
48922     X509_INFO* info;
48923     STACK_OF(X509_INFO)* sk;
48924     char* subject;
48925     char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
48926     char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
48927 
48928     printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
48929     AssertNotNull(bio = BIO_new(BIO_s_file()));
48930     AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
48931     AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
48932     AssertIntEQ(sk_X509_INFO_num(sk), 2);
48933 
48934     /* using dereference to maintain testing for Apache port*/
48935     AssertNotNull(info = sk_X509_INFO_pop(sk));
48936     AssertNotNull(subject =
48937             X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
48938 
48939     AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
48940     XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
48941     X509_INFO_free(info);
48942 
48943     AssertNotNull(info = sk_X509_INFO_pop(sk));
48944     AssertNotNull(subject =
48945             X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
48946 
48947     AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
48948     XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
48949     X509_INFO_free(info);
48950     AssertNull(info = sk_X509_INFO_pop(sk));
48951 
48952     sk_X509_INFO_pop_free(sk, X509_INFO_free);
48953     BIO_free(bio);
48954     printf(resultFmt, passed);
48955 #endif
48956 }
48957 #endif /* !NO_BIO */
48958 
48959 static void test_wolfSSL_X509_NAME_ENTRY_get_object(void)
48960 {
48961 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
48962     X509 *x509;
48963     X509_NAME* name;
48964     int idx = 0;
48965     X509_NAME_ENTRY *ne;
48966     ASN1_OBJECT *object = NULL;
48967 
48968     printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
48969     x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
48970     AssertNotNull(x509);
48971     name = X509_get_subject_name(x509);
48972     idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
48973     AssertIntGE(idx, 0);
48974 
48975     ne = X509_NAME_get_entry(name, idx);
48976     AssertNotNull(ne);
48977     AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
48978 
48979     X509_free(x509);
48980 
48981     printf(resultFmt, passed);
48982 #endif
48983 }
48984 
48985 static void test_wolfSSL_ASN1_INTEGER_get_set(void)
48986 {
48987 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
48988     ASN1_INTEGER *a;
48989     long val;
48990     int ret;
48991 
48992     printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
48993 
48994     a = ASN1_INTEGER_new();
48995     val = 0;
48996     ret = ASN1_INTEGER_set(NULL, val);
48997     AssertIntEQ(ret, 0);
48998     ASN1_INTEGER_free(a);
48999 
49000     /* 0 */
49001     a = ASN1_INTEGER_new();
49002     val = 0;
49003     ret = ASN1_INTEGER_set(a, val);
49004     AssertIntEQ(ret, 1);
49005     AssertIntEQ(ASN1_INTEGER_get(a), val);
49006     ASN1_INTEGER_free(a);
49007 
49008     /* 40 */
49009     a = ASN1_INTEGER_new();
49010     val = 40;
49011     ret = ASN1_INTEGER_set(a, val);
49012     AssertIntEQ(ret, 1);
49013     AssertIntEQ(ASN1_INTEGER_get(a), val);
49014     ASN1_INTEGER_free(a);
49015 
49016     /* -40 */
49017     a = ASN1_INTEGER_new();
49018     val = -40;
49019     ret = ASN1_INTEGER_set(a, val);
49020     AssertIntEQ(ret, 1);
49021     AssertIntEQ(ASN1_INTEGER_get(a), val);
49022     ASN1_INTEGER_free(a);
49023 
49024     /* 128 */
49025     a = ASN1_INTEGER_new();
49026     val = 128;
49027     ret = ASN1_INTEGER_set(a, val);
49028     AssertIntEQ(ret, 1);
49029     AssertIntEQ(ASN1_INTEGER_get(a), val);
49030     ASN1_INTEGER_free(a);
49031 
49032     /* -128 */
49033     a = ASN1_INTEGER_new();
49034     val = -128;
49035     ret = ASN1_INTEGER_set(a, val);
49036     AssertIntEQ(ret, 1);
49037     AssertIntEQ(ASN1_INTEGER_get(a), val);
49038     ASN1_INTEGER_free(a);
49039 
49040     /* 200 */
49041     a = ASN1_INTEGER_new();
49042     val = 200;
49043     ret = ASN1_INTEGER_set(a, val);
49044     AssertIntEQ(ret, 1);
49045     AssertIntEQ(ASN1_INTEGER_get(a), val);
49046     ASN1_INTEGER_free(a);
49047 
49048     /* int max (2147483647) */
49049     a = ASN1_INTEGER_new();
49050     val = 2147483647;
49051     ret = ASN1_INTEGER_set(a, val);
49052     AssertIntEQ(ret, 1);
49053     AssertIntEQ(ASN1_INTEGER_get(a), val);
49054     ASN1_INTEGER_free(a);
49055 
49056     /* int min (-2147483648) */
49057     a = ASN1_INTEGER_new();
49058     val = -2147483647 - 1;
49059     ret = ASN1_INTEGER_set(a, val);
49060     AssertIntEQ(ret, 1);
49061     AssertIntEQ(ASN1_INTEGER_get(a), val);
49062     ASN1_INTEGER_free(a);
49063 
49064     printf(resultFmt, passed);
49065 #endif
49066 }
49067 
49068 static void test_wolfSSL_X509_STORE_get1_certs(void)
49069 {
49070 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
49071     !defined(NO_FILESYSTEM) && !defined(NO_RSA)
49072     X509_STORE_CTX *storeCtx;
49073     X509_STORE *store;
49074     X509 *caX509;
49075     X509 *svrX509;
49076     X509_NAME *subject;
49077     WOLF_STACK_OF(WOLFSSL_X509) *certs;
49078 
49079     printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
49080 
49081     AssertNotNull(caX509 =
49082         X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
49083     AssertNotNull((svrX509 =
49084         wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
49085     AssertNotNull(storeCtx = X509_STORE_CTX_new());
49086     AssertNotNull(store = X509_STORE_new());
49087     AssertNotNull(subject = X509_get_subject_name(caX509));
49088 
49089     /* Errors */
49090     AssertNull(X509_STORE_get1_certs(storeCtx, subject));
49091     AssertNull(X509_STORE_get1_certs(NULL, subject));
49092     AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
49093 
49094     AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
49095     AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
49096 
49097     /* Should find the cert */
49098     AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
49099     AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
49100 
49101     sk_X509_pop_free(certs, NULL);
49102 
49103     /* Should not find the cert */
49104     AssertNotNull(subject = X509_get_subject_name(svrX509));
49105     AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
49106     AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
49107 
49108     sk_X509_pop_free(certs, NULL);
49109 
49110     X509_STORE_free(store);
49111     X509_STORE_CTX_free(storeCtx);
49112     X509_free(svrX509);
49113     X509_free(caX509);
49114 
49115     printf(resultFmt, passed);
49116 #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
49117 }
49118 
49119 /* Testing code used in dpp.c in hostap */
49120 #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
49121 typedef struct {
49122     /* AlgorithmIdentifier ecPublicKey with optional parameters present
49123      * as an OID identifying the curve */
49124     X509_ALGOR *alg;
49125     /* Compressed format public key per ANSI X9.63 */
49126     ASN1_BIT_STRING *pub_key;
49127 } DPP_BOOTSTRAPPING_KEY;
49128 
49129 ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
49130     ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
49131     ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
49132 } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
49133 
49134 IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
49135 #endif
49136 
49137 static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
49138 {
49139     /* Testing code used in dpp.c in hostap */
49140 #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
49141 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
49142     EC_KEY *eckey;
49143     EVP_PKEY *key;
49144     size_t len;
49145     unsigned char *der = NULL;
49146     DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
49147     const unsigned char *in = ecc_clikey_der_256;
49148     const EC_GROUP *group;
49149     const EC_POINT *point;
49150     int nid;
49151 
49152     AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
49153 
49154     AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
49155                                        (long)sizeof_ecc_clikey_der_256));
49156     AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
49157     AssertNotNull(group = EC_KEY_get0_group(eckey));
49158     AssertNotNull(point = EC_KEY_get0_public_key(eckey));
49159     nid = EC_GROUP_get_curve_name(group);
49160 
49161     AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
49162                                 V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
49163 #ifdef HAVE_COMP_KEY
49164     AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
49165                                           NULL, 0, NULL)), 0);
49166 #else
49167     AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
49168                                           NULL, 0, NULL)), 0);
49169 #endif
49170     AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
49171 #ifdef HAVE_COMP_KEY
49172     AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
49173                                    der, len, NULL), len);
49174 #else
49175     AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
49176                                    der, len, NULL), len);
49177 #endif
49178     bootstrap->pub_key->data = der;
49179     bootstrap->pub_key->length = (int)len;
49180     /* Not actually used */
49181     bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
49182     bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
49183 
49184     der = NULL;
49185     AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
49186 
49187     XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
49188     EVP_PKEY_free(key);
49189     EC_KEY_free(eckey);
49190     DPP_BOOTSTRAPPING_KEY_free(bootstrap);
49191 #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
49192 #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
49193 }
49194 
49195 static void test_wolfSSL_i2c_ASN1_INTEGER(void)
49196 {
49197 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
49198     ASN1_INTEGER *a;
49199     unsigned char *pp,*tpp;
49200     int ret;
49201 
49202     printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
49203 
49204     a = wolfSSL_ASN1_INTEGER_new();
49205 
49206     /* 40 */
49207     a->intData[0] = ASN_INTEGER;
49208     a->intData[1] = 1;
49209     a->intData[2] = 40;
49210     ret = i2c_ASN1_INTEGER(a, NULL);
49211     AssertIntEQ(ret, 1);
49212     AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
49213                 DYNAMIC_TYPE_TMP_BUFFER));
49214     tpp = pp;
49215     XMEMSET(pp, 0, ret + 1);
49216     i2c_ASN1_INTEGER(a, &pp);
49217     pp--;
49218     AssertIntEQ(*pp, 40);
49219     XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49220 
49221     /* 128 */
49222     a->intData[0] = ASN_INTEGER;
49223     a->intData[1] = 1;
49224     a->intData[2] = 128;
49225     ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
49226     AssertIntEQ(ret, 2);
49227     AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
49228                 DYNAMIC_TYPE_TMP_BUFFER));
49229     tpp = pp;
49230     XMEMSET(pp, 0, ret + 1);
49231     wolfSSL_i2c_ASN1_INTEGER(a, &pp);
49232     pp--;
49233     AssertIntEQ(*(pp--), 128);
49234     AssertIntEQ(*pp, 0);
49235     XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49236 
49237     /* -40 */
49238     a->intData[0] = ASN_INTEGER;
49239     a->intData[1] = 1;
49240     a->intData[2] = 40;
49241     a->negative = 1;
49242     ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
49243     AssertIntEQ(ret, 1);
49244     AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
49245                 DYNAMIC_TYPE_TMP_BUFFER));
49246     tpp = pp;
49247     XMEMSET(pp, 0, ret + 1);
49248     wolfSSL_i2c_ASN1_INTEGER(a, &pp);
49249     pp--;
49250     AssertIntEQ(*pp, 216);
49251     XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49252 
49253     /* -128 */
49254     a->intData[0] = ASN_INTEGER;
49255     a->intData[1] = 1;
49256     a->intData[2] = 128;
49257     a->negative = 1;
49258     ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
49259     AssertIntEQ(ret, 1);
49260     AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
49261                 DYNAMIC_TYPE_TMP_BUFFER));
49262     tpp = pp;
49263     XMEMSET(pp, 0, ret + 1);
49264     wolfSSL_i2c_ASN1_INTEGER(a, &pp);
49265     pp--;
49266     AssertIntEQ(*pp, 128);
49267     XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49268 
49269     /* -200 */
49270     a->intData[0] = ASN_INTEGER;
49271     a->intData[1] = 1;
49272     a->intData[2] = 200;
49273     a->negative = 1;
49274     ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
49275     AssertIntEQ(ret, 2);
49276     AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
49277             DYNAMIC_TYPE_TMP_BUFFER));
49278     tpp = pp;
49279     XMEMSET(pp, 0, ret + 1);
49280     wolfSSL_i2c_ASN1_INTEGER(a, &pp);
49281     pp--;
49282     AssertIntEQ(*(pp--), 56);
49283     AssertIntEQ(*pp, 255);
49284 
49285     XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
49286     wolfSSL_ASN1_INTEGER_free(a);
49287 
49288     printf(resultFmt, passed);
49289 #endif /* OPENSSL_EXTRA && !NO_ASN */
49290 }
49291 
49292 #ifndef NO_INLINE
49293 #define WOLFSSL_MISC_INCLUDED
49294 #include <wolfcrypt/src/misc.c>
49295 #else
49296 #include <wolfssl/wolfcrypt/misc.h>
49297 #endif
49298 
49299 static int test_ForceZero(void)
49300 {
49301     unsigned char data[32];
49302     unsigned int i, j, len;
49303 
49304     /* Test case with 0 length */
49305     ForceZero(data, 0);
49306 
49307     /* Test ForceZero */
49308     for (i = 0; i < sizeof(data); i++) {
49309         for (len = 1; len < sizeof(data) - i; len++) {
49310             for (j = 0; j < sizeof(data); j++)
49311                 data[j] = j + 1;
49312 
49313             ForceZero(data + i, len);
49314 
49315             for (j = 0; j < sizeof(data); j++) {
49316                 if (j < i || j >= i + len) {
49317                     if (data[j] == 0x00)
49318                         return -10200;
49319                 }
49320                 else if (data[j] != 0x00)
49321                     return -10201;
49322             }
49323         }
49324     }
49325 
49326     return 0;
49327 }
49328 
49329 #ifndef NO_BIO
49330 
49331 static void test_wolfSSL_X509_print(void)
49332 {
49333 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
49334    !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
49335     X509 *x509;
49336     BIO *bio;
49337 #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
49338     const X509_ALGOR *cert_sig_alg;
49339 #endif
49340 
49341     printf(testingFmt, "wolfSSL_X509_print");
49342     x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
49343     AssertNotNull(x509);
49344 
49345     /* print to memory */
49346     AssertNotNull(bio = BIO_new(BIO_s_mem()));
49347     AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
49348 
49349 #if defined(WOLFSSL_QT)
49350     AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
49351 #else
49352     AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
49353 #endif
49354     BIO_free(bio);
49355 
49356     AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
49357 
49358 #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
49359     /* Print signature */
49360     AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
49361     AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
49362 #endif
49363 
49364     /* print to stdout */
49365 #if !defined(NO_WOLFSSL_DIR)
49366     AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
49367 #endif
49368     /* print again */
49369     AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
49370 
49371     X509_free(x509);
49372     BIO_free(bio);
49373     printf(resultFmt, passed);
49374 #endif
49375 }
49376 
49377 static void test_wolfSSL_RSA_print(void)
49378 {
49379 #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
49380    !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
49381    !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
49382     BIO *bio;
49383     WOLFSSL_RSA* rsa = NULL;
49384     printf(testingFmt, "wolfSSL_RSA_print");
49385 
49386     AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
49387     AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
49388     AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
49389 
49390     BIO_free(bio);
49391     RSA_free(rsa);
49392     printf(resultFmt, passed);
49393 #endif
49394 }
49395 
49396 static void test_wolfSSL_BIO_get_len(void)
49397 {
49398 #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
49399     BIO *bio = NULL;
49400     const char txt[] = "Some example text to push to the BIO.";
49401     printf(testingFmt, "wolfSSL_BIO_get_len");
49402 
49403     AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
49404 
49405     AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
49406 
49407     AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
49408     AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
49409     BIO_free(bio);
49410 
49411     AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
49412     AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
49413     BIO_free(bio);
49414 
49415     printf(resultFmt, passed);
49416 #endif
49417 }
49418 
49419 static void test_wolfSSL_ASN1_STRING_print(void){
49420 #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
49421     ASN1_STRING* asnStr = NULL;
49422     const char HELLO_DATA[]= \
49423                       {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
49424     #define MAX_UNPRINTABLE_CHAR 32
49425     #define MAX_BUF 255
49426     unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
49427     unsigned char expected[sizeof(unprintableData)+1];
49428     unsigned char rbuf[MAX_BUF];
49429 
49430     BIO *bio;
49431     int p_len, i;
49432 
49433     printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
49434 
49435     /* setup */
49436 
49437     for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
49438         unprintableData[i]  = HELLO_DATA[i];
49439         expected[i]         = HELLO_DATA[i];
49440     }
49441 
49442     for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
49443         unprintableData[sizeof(HELLO_DATA)+i] = i;
49444 
49445         if (i == (int)'\n' || i == (int)'\r')
49446             expected[sizeof(HELLO_DATA)+i] = i;
49447         else
49448             expected[sizeof(HELLO_DATA)+i] = '.';
49449     }
49450 
49451     unprintableData[sizeof(unprintableData)-1] = '\0';
49452     expected[sizeof(expected)-1] = '\0';
49453 
49454     XMEMSET(rbuf, 0, MAX_BUF);
49455     bio = BIO_new(BIO_s_mem());
49456     BIO_set_write_buf_size(bio, MAX_BUF);
49457 
49458     asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
49459     ASN1_STRING_set(asnStr,(const void*)unprintableData,
49460             (int)sizeof(unprintableData));
49461     /* test */
49462     p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
49463     AssertIntEQ(p_len, 46);
49464     BIO_read(bio, (void*)rbuf, 46);
49465 
49466     AssertStrEQ((char*)rbuf, (const char*)expected);
49467 
49468     BIO_free(bio);
49469     ASN1_STRING_free(asnStr);
49470 
49471     printf(resultFmt, passed);
49472 #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
49473 }
49474 
49475 #endif /* !NO_BIO */
49476 
49477 static void test_wolfSSL_ASN1_get_object(void)
49478 {
49479 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
49480     const unsigned char* derBuf = cliecc_cert_der_256;
49481     int len = sizeof_cliecc_cert_der_256;
49482     long asnLen = 0;
49483     int tag = 0, cls = 0;
49484     ASN1_OBJECT *a;
49485 
49486     printf(testingFmt, "wolfSSL_ASN1_get_object()");
49487 
49488     /* Read a couple TLV triplets and make sure they match the expected values */
49489 
49490     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
49491     AssertIntEQ(asnLen, 862);
49492     AssertIntEQ(tag, 0x10);
49493     AssertIntEQ(cls, 0);
49494 
49495     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
49496             len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
49497     AssertIntEQ(asnLen, 772);
49498     AssertIntEQ(tag, 0x10);
49499     AssertIntEQ(cls, 0);
49500 
49501     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
49502             len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
49503     AssertIntEQ(asnLen, 3);
49504     AssertIntEQ(tag, 0);
49505     AssertIntEQ(cls, 0x80);
49506 
49507     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
49508             len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
49509     AssertIntEQ(asnLen, 1);
49510     AssertIntEQ(tag, 0x2);
49511     AssertIntEQ(cls, 0);
49512     derBuf += asnLen;
49513 
49514     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
49515             len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
49516     AssertIntEQ(asnLen, 20);
49517     AssertIntEQ(tag, 0x2);
49518     AssertIntEQ(cls, 0);
49519     derBuf += asnLen;
49520 
49521     AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
49522             len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
49523     AssertIntEQ(asnLen, 10);
49524     AssertIntEQ(tag, 0x10);
49525     AssertIntEQ(cls, 0);
49526 
49527     /* Read an ASN OBJECT */
49528     AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
49529     ASN1_OBJECT_free(a);
49530 
49531     printf(resultFmt, passed);
49532 #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
49533 }
49534 
49535 static void test_wolfSSL_RSA_verify(void)
49536 {
49537 #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
49538     !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
49539 #ifndef NO_BIO
49540     XFILE fp;
49541     RSA *pKey, *pubKey;
49542     X509 *cert;
49543     const char *text = "Hello wolfSSL !";
49544     unsigned char hash[SHA256_DIGEST_LENGTH];
49545     unsigned char signature[2048/8];
49546     unsigned int signatureLength;
49547     byte *buf;
49548     BIO *bio;
49549     SHA256_CTX c;
49550     EVP_PKEY *evpPkey, *evpPubkey;
49551     size_t sz;
49552 
49553     printf(testingFmt, "wolfSSL_RSA_verify");
49554 
49555     /* generate hash */
49556     SHA256_Init(&c);
49557     SHA256_Update(&c, text, strlen(text));
49558     SHA256_Final(hash, &c);
49559 #ifdef WOLFSSL_SMALL_STACK_CACHE
49560     /* workaround for small stack cache case */
49561     wc_Sha256Free((wc_Sha256*)&c);
49562 #endif
49563 
49564     /* read privete key file */
49565     fp = XFOPEN(svrKeyFile, "rb");
49566     AssertTrue((fp != XBADFILE));
49567     XFSEEK(fp, 0, XSEEK_END);
49568     sz = XFTELL(fp);
49569     XREWIND(fp);
49570     AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
49571     AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
49572     XFCLOSE(fp);
49573 
49574     /* read private key and sign hash data */
49575     AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
49576     AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
49577     AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
49578     AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
49579                             signature, &signatureLength, pKey), SSL_SUCCESS);
49580 
49581     /* read public key and verify signed data */
49582     fp = XFOPEN(svrCertFile,"rb");
49583     AssertTrue((fp != XBADFILE));
49584     cert = PEM_read_X509(fp, 0, 0, 0 );
49585     XFCLOSE(fp);
49586     evpPubkey = X509_get_pubkey(cert);
49587     pubKey = EVP_PKEY_get1_RSA(evpPubkey);
49588     AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
49589                                 signatureLength, pubKey), SSL_SUCCESS);
49590 
49591     RSA_free(pKey);
49592     EVP_PKEY_free(evpPkey);
49593     RSA_free(pubKey);
49594     EVP_PKEY_free(evpPubkey);
49595     X509_free(cert);
49596     BIO_free(bio);
49597     XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
49598     printf(resultFmt, passed);
49599 #endif
49600 #endif
49601 }
49602 
49603 
49604 #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
49605     defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
49606 static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
49607 {
49608     X509* x509 = NULL;
49609     BIGNUM* serial_number = NULL;
49610     X509_NAME* name = NULL;
49611     time_t epoch_off = 0;
49612     ASN1_INTEGER* asn1_serial_number;
49613     long not_before, not_after;
49614 
49615     AssertNotNull(x509 = X509_new());
49616 
49617     AssertIntNE(X509_set_pubkey(x509, pkey), 0);
49618 
49619     AssertNotNull(serial_number = BN_new());
49620     AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
49621     AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
49622     AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
49623 
49624     /* version 3 */
49625     AssertIntNE(X509_set_version(x509, 2L), 0);
49626 
49627     AssertNotNull(name = X509_NAME_new());
49628 
49629     AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
49630         (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
49631 
49632     AssertIntNE(X509_set_subject_name(x509, name), 0);
49633     AssertIntNE(X509_set_issuer_name(x509, name), 0);
49634 
49635     not_before = (long)XTIME(NULL);
49636     not_after = not_before + (365 * 24 * 60 * 60);
49637     AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
49638     AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
49639 
49640     AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
49641 
49642     BN_free(serial_number);
49643     X509_NAME_free(name);
49644     X509_free(x509);
49645 }
49646 #endif
49647 
49648 static void test_openssl_generate_key_and_cert(void)
49649 {
49650 #if defined(OPENSSL_EXTRA)
49651 #if !defined(NO_RSA)
49652     {
49653         EVP_PKEY* pkey = EVP_PKEY_new();
49654         int key_length = 2048;
49655         BIGNUM* exponent = BN_new();
49656         RSA* rsa = RSA_new();
49657 
49658         AssertNotNull(pkey);
49659         AssertNotNull(exponent);
49660         AssertNotNull(rsa);
49661 
49662         AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
49663     #ifndef WOLFSSL_KEY_GEN
49664         AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), WOLFSSL_FAILURE);
49665 
49666         #if defined(USE_CERT_BUFFERS_1024)
49667         AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
49668             sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
49669         key_length = 1024;
49670         #elif defined(USE_CERT_BUFFERS_2048)
49671         AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
49672             sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
49673         #else
49674         RSA_free(rsa);
49675         rsa = NULL;
49676         #endif
49677     #else
49678         AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
49679     #endif
49680 
49681         if (rsa) {
49682             AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
49683 
49684             BN_free(exponent);
49685 
49686         #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
49687                 defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
49688             test_openssl_make_self_signed_certificate(pkey);
49689         #endif
49690         }
49691 
49692         EVP_PKEY_free(pkey);
49693     }
49694 #endif /* !NO_RSA */
49695 
49696 #ifdef HAVE_ECC
49697     {
49698         EVP_PKEY* pkey = EVP_PKEY_new();
49699         EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
49700 
49701         AssertNotNull(pkey);
49702         AssertNotNull(ec_key);
49703 
49704     #ifndef NO_WOLFSSL_STUB
49705         EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
49706     #endif
49707 
49708         AssertIntNE(EC_KEY_generate_key(ec_key), 0);
49709         AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
49710 
49711     #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
49712             defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
49713         test_openssl_make_self_signed_certificate(pkey);
49714     #endif
49715 
49716         EVP_PKEY_free(pkey);
49717     }
49718 #endif /* HAVE_ECC */
49719 #endif /* OPENSSL_EXTRA */
49720 }
49721 
49722 static void test_stubs_are_stubs(void)
49723 {
49724 #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
49725     WOLFSSL_CTX* ctx = NULL;
49726     WOLFSSL_CTX* ctxN = NULL;
49727   #ifndef NO_WOLFSSL_CLIENT
49728     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
49729     AssertNotNull(ctx);
49730   #elif !defined(NO_WOLFSSL_SERVER)
49731     ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
49732     AssertNotNull(ctx);
49733   #else
49734     return;
49735   #endif
49736 
49737     #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
49738                      AssertIntEQ((int) x(z), 0)
49739     /* test logic, all stubs return same result regardless of ctx being NULL
49740      * as there are no sanity checks, it's just a stub! If at some
49741      * point a stub is not a stub it should begin to return BAD_FUNC_ARG
49742      * if invalid inputs are supplied. Test calling both
49743      * with and without valid inputs, if a stub functionality remains unchanged.
49744      */
49745     CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
49746     CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
49747     CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
49748     CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
49749     CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
49750     CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
49751     CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
49752     CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
49753     CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
49754     CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
49755     CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
49756     wolfSSL_CTX_free(ctx);
49757     ctx = NULL;
49758 #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
49759 }
49760 
49761 
49762 static void test_CONF_modules_xxx(void)
49763 {
49764 #if defined(OPENSSL_EXTRA)
49765     CONF_modules_free();
49766     AssertTrue(1);   /* to confirm previous call gives no harm */
49767 
49768     CONF_modules_unload(0);
49769     AssertTrue(1);
49770 
49771     CONF_modules_unload(1);
49772     AssertTrue(1);
49773 
49774     CONF_modules_unload(-1);
49775     AssertTrue(1);
49776 
49777 #endif /* OPENSSL_EXTRA */
49778 }
49779 static void test_CRYPTO_set_dynlock_xxx(void)
49780 {
49781 #if defined(OPENSSL_EXTRA)
49782     printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
49783 
49784     CRYPTO_set_dynlock_create_callback(
49785         (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
49786 
49787     CRYPTO_set_dynlock_create_callback(
49788         (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
49789 
49790     CRYPTO_set_dynlock_destroy_callback(
49791         (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
49792 
49793     CRYPTO_set_dynlock_destroy_callback(
49794         (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
49795 
49796     CRYPTO_set_dynlock_lock_callback(
49797         (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
49798 
49799     CRYPTO_set_dynlock_lock_callback(
49800         (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
49801 
49802     AssertTrue(1);   /* to confirm previous call gives no harm */
49803     printf(resultFmt, passed);
49804 #endif /* OPENSSL_EXTRA */
49805 }
49806 static void test_CRYPTO_THREADID_xxx(void)
49807 {
49808 #if defined(OPENSSL_EXTRA)
49809     printf(testingFmt, "CRYPTO_THREADID_xxx()");
49810 
49811     CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
49812     CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
49813     AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
49814     printf(resultFmt, passed);
49815 #endif /* OPENSSL_EXTRA */
49816 }
49817 static void test_ENGINE_cleanup(void)
49818 {
49819 #if defined(OPENSSL_EXTRA)
49820     printf(testingFmt, "ENGINE_cleanup()");
49821     ENGINE_cleanup();
49822     AssertTrue(1);   /* to confirm previous call gives no harm */
49823     printf(resultFmt, passed);
49824 #endif /* OPENSSL_EXTRA */
49825 }
49826 
49827 static void test_wolfSSL_CTX_LoadCRL(void)
49828 {
49829 #ifdef HAVE_CRL
49830     WOLFSSL_CTX* ctx = NULL;
49831     const char* badPath = "dummypath";
49832     const char* validPath = "./certs/crl";
49833     int derType = WOLFSSL_FILETYPE_ASN1;
49834     int pemType = WOLFSSL_FILETYPE_PEM;
49835     int monitor = WOLFSSL_CRL_MONITOR;
49836 
49837     #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
49838                                                 BAD_FUNC_ARG)
49839     #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
49840                                                 WOLFSSL_SUCCESS)
49841 
49842     FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
49843 
49844   #ifndef NO_WOLFSSL_CLIENT
49845     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
49846   #elif !defined(NO_WOLFSSL_SERVER)
49847     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
49848   #else
49849     return;
49850   #endif
49851 
49852     SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
49853     SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
49854     SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
49855 
49856     wolfSSL_CTX_free(ctx);
49857     ctx = NULL;
49858 #endif
49859 }
49860 
49861 static void test_SetTmpEC_DHE_Sz(void)
49862 {
49863 #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
49864     WOLFSSL_CTX *ctx;
49865     WOLFSSL *ssl;
49866     ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
49867     AssertNotNull(ctx);
49868     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
49869     ssl = wolfSSL_new(ctx);
49870     AssertNotNull(ssl);
49871     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
49872 
49873     wolfSSL_free(ssl);
49874     wolfSSL_CTX_free(ctx);
49875 #endif
49876 }
49877 
49878 static void test_wolfSSL_CTX_get0_privatekey(void)
49879 {
49880 #ifdef OPENSSL_ALL
49881     WOLFSSL_CTX* ctx = NULL;
49882 
49883     printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
49884 
49885 #ifndef NO_RSA
49886     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
49887     AssertNull(SSL_CTX_get0_privatekey(ctx));
49888     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
49889                                                 WOLFSSL_FILETYPE_PEM));
49890     AssertNull(SSL_CTX_get0_privatekey(ctx));
49891     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
49892                                                WOLFSSL_FILETYPE_PEM));
49893     AssertNotNull(SSL_CTX_get0_privatekey(ctx));
49894     wolfSSL_CTX_free(ctx);
49895 #endif
49896 #ifdef HAVE_ECC
49897     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
49898     AssertNull(SSL_CTX_get0_privatekey(ctx));
49899     AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
49900                                                 WOLFSSL_FILETYPE_PEM));
49901     AssertNull(SSL_CTX_get0_privatekey(ctx));
49902     AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
49903                                                WOLFSSL_FILETYPE_PEM));
49904     AssertNotNull(SSL_CTX_get0_privatekey(ctx));
49905     wolfSSL_CTX_free(ctx);
49906 #endif
49907 
49908     printf(resultFmt, passed);
49909 #endif
49910 }
49911 
49912 static void test_wolfSSL_dtls_set_mtu(void)
49913 {
49914 #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
49915                                                            defined(WOLFSSL_DTLS)
49916     WOLFSSL_CTX* ctx = NULL;
49917     WOLFSSL*     ssl = NULL;
49918     const char* testCertFile;
49919     const char* testKeyFile;
49920 
49921     AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
49922 #ifndef NO_RSA
49923         testCertFile = svrCertFile;
49924         testKeyFile = svrKeyFile;
49925 #elif defined(HAVE_ECC)
49926         testCertFile = eccCertFile;
49927         testKeyFile = eccKeyFile;
49928 #endif
49929     if  (testCertFile != NULL && testKeyFile != NULL) {
49930         AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
49931                                                     WOLFSSL_FILETYPE_PEM));
49932         AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
49933                                                    WOLFSSL_FILETYPE_PEM));
49934     }
49935     AssertNotNull(ssl = wolfSSL_new(ctx));
49936 
49937     AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
49938     AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
49939     AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
49940     AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
49941     AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
49942     AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
49943     AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
49944 
49945     wolfSSL_free(ssl);
49946     wolfSSL_CTX_free(ctx);
49947 
49948     printf(testingFmt, "wolfSSL_dtls_set_mtu()");
49949     printf(resultFmt, passed);
49950 #endif
49951 }
49952 
49953 #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
49954     !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
49955     !defined(WOLFSSL_NO_CLIENT_AUTH))
49956 static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
49957 {
49958     int ret;
49959 
49960     if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
49961         printf("loading cert %s failed\n", certA);
49962         printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
49963         return -1;
49964     }
49965 
49966     return 0;
49967 }
49968 
49969 static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
49970 {
49971     int ret;
49972     if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
49973                                                          != WOLFSSL_SUCCESS) {
49974         printf("could not verify the cert: %s\n", certA);
49975         printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
49976         return -1;
49977     } else {
49978         printf("successfully verified: %s\n", certA);
49979     }
49980 
49981     return 0;
49982 }
49983 #define LOAD_ONE_CA(a, b, c, d)                         \
49984                     do {                                \
49985                         a = load_ca_into_cm(c, d);      \
49986                         if (a != 0)                     \
49987                             return b;                   \
49988                         else                            \
49989                             b--;                        \
49990                     } while(0)
49991 
49992 #define VERIFY_ONE_CERT(a, b, c, d)                     \
49993                     do {                                \
49994                         a = verify_cert_with_cm(c, d);  \
49995                         if (a != 0)                     \
49996                             return b;                   \
49997                         else                            \
49998                             b--;                        \
49999                     } while(0)
50000 
50001 static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
50002 {
50003     int ret;
50004     int i = -1;
50005     /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
50006     char chainGArr[9][50] = {"certs/ca-cert.pem",
50007                              "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
50008                              "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
50009                              "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
50010                              "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
50011                              "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
50012                              "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
50013                              "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
50014                              "certs/test-pathlen/chainG-entity.pem"};
50015 
50016     LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
50017     LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
50018     LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
50019     LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
50020     LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
50021     LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
50022     LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
50023     LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
50024     VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
50025     VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
50026     VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
50027     VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
50028     VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
50029     VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
50030     VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
50031     VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
50032 
50033 /* test validating the entity twice, should have no effect on pathLen since
50034  * entity/leaf cert */
50035     VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
50036 
50037     return ret;
50038 }
50039 
50040 static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
50041 {
50042     int ret;
50043     int i = -1;
50044     /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
50045      * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
50046      * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
50047      * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
50048      * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
50049      * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
50050      */
50051     char chainHArr[6][50] = {"certs/ca-cert.pem",
50052                              "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
50053                              "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
50054                              "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
50055                              "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
50056                              "certs/test-pathlen/chainH-entity.pem"};
50057 
50058     LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
50059     LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
50060     LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
50061     LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
50062     LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
50063     VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
50064     VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
50065     VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
50066     VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
50067     VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
50068 
50069     return ret;
50070 }
50071 
50072 static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
50073 {
50074     int ret;
50075     int i = -1;
50076     /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
50077      * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
50078      * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
50079      * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
50080      * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
50081      */
50082     char chainIArr[5][50] = {"certs/ca-cert.pem",
50083                              "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
50084                              "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
50085                              "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
50086                              "certs/test-pathlen/chainI-entity.pem"};
50087 
50088     LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
50089     LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
50090     LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
50091     LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
50092     VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
50093     VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
50094     VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
50095     VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
50096 
50097     return ret;
50098 }
50099 
50100 static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
50101 {
50102     int ret;
50103     int i = -1;
50104     /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
50105      * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
50106      * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
50107      * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
50108      * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
50109      */
50110     char chainJArr[6][50] = {"certs/ca-cert.pem",
50111                              "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
50112                              "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
50113                              "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
50114                              "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
50115                              "certs/test-pathlen/chainJ-entity.pem"};
50116 
50117     LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
50118     LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
50119     LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
50120     LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
50121     LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
50122     VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
50123     VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
50124     VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
50125     VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
50126     VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
50127 
50128     return ret;
50129 }
50130 
50131 static int test_various_pathlen_chains(void)
50132 {
50133     int ret;
50134     WOLFSSL_CERT_MANAGER* cm;
50135 
50136     /* Test chain G (large chain with varying pathLens) */
50137     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50138         printf("cert manager new failed\n");
50139         return -1;
50140     }
50141 
50142     AssertIntEQ(test_chainG(cm), 0);
50143 
50144     ret = wolfSSL_CertManagerUnloadCAs(cm);
50145     if (ret != WOLFSSL_SUCCESS)
50146         return -1;
50147     wolfSSL_CertManagerFree(cm);
50148     /* end test chain G */
50149 
50150     /* Test chain H (5 chain with same pathLens) */
50151     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50152         printf("cert manager new failed\n");
50153         return -1;
50154     }
50155     AssertIntLT(test_chainH(cm), 0);
50156 
50157     wolfSSL_CertManagerUnloadCAs(cm);
50158     wolfSSL_CertManagerFree(cm);
50159     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50160         printf("cert manager new failed\n");
50161         return -1;
50162     }
50163 
50164     ret = wolfSSL_CertManagerUnloadCAs(cm);
50165     if (ret != WOLFSSL_SUCCESS)
50166         return -1;
50167     wolfSSL_CertManagerFree(cm);
50168     /* end test chain H */
50169 
50170     /* Test chain I (only first ICA has pathLen set and it's set to 2,
50171      * followed by 2 ICA's, should pass) */
50172     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50173         printf("cert manager new failed\n");
50174         return -1;
50175     }
50176     AssertIntEQ(test_chainI(cm), 0);
50177 
50178     wolfSSL_CertManagerUnloadCAs(cm);
50179     wolfSSL_CertManagerFree(cm);
50180     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50181         printf("cert manager new failed\n");
50182         return -1;
50183     }
50184 
50185     ret = wolfSSL_CertManagerUnloadCAs(cm);
50186     if (ret != WOLFSSL_SUCCESS)
50187         return -1;
50188     wolfSSL_CertManagerFree(cm);
50189 
50190     /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
50191      * this time followed by 3 ICA's, should fail */
50192     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50193         printf("cert manager new failed\n");
50194         return -1;
50195     }
50196     AssertIntLT(test_chainJ(cm), 0);
50197 
50198     wolfSSL_CertManagerUnloadCAs(cm);
50199     wolfSSL_CertManagerFree(cm);
50200     if ((cm = wolfSSL_CertManagerNew()) == NULL) {
50201         printf("cert manager new failed\n");
50202         return -1;
50203     }
50204 
50205     ret = wolfSSL_CertManagerUnloadCAs(cm);
50206     wolfSSL_CertManagerFree(cm);
50207 
50208     return ret;
50209 }
50210 #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
50211 
50212 #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
50213 static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
50214 {
50215     byte ekm[100] = {0};
50216 
50217     (void)ctx;
50218 
50219     /* Succes Cases */
50220     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50221             "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
50222     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50223             "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
50224     /* Use some random context */
50225     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50226             "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
50227     /* Failure cases */
50228     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50229             "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
50230     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50231             "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
50232     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50233             "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
50234     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50235             "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
50236     AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
50237             "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
50238     return 0;
50239 }
50240 
50241 static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
50242 {
50243     wolfSSL_KeepArrays(ssl);
50244 }
50245 
50246 static void test_export_keying_material(void)
50247 {
50248 #ifndef SINGLE_THREADED
50249     tcp_ready ready;
50250     callback_functions clientCb;
50251     func_args client_args;
50252     func_args server_args;
50253     THREAD_TYPE serverThread;
50254 
50255     XMEMSET(&client_args, 0, sizeof(func_args));
50256     XMEMSET(&server_args, 0, sizeof(func_args));
50257     XMEMSET(&clientCb, 0, sizeof(callback_functions));
50258 #ifdef WOLFSSL_TIRTOS
50259     fdOpenSession(Task_self());
50260 #endif
50261 
50262     StartTCP();
50263     InitTcpReady(&ready);
50264 
50265 #if defined(USE_WINDOWS_API)
50266     /* use RNG to get random port if using windows */
50267     ready.port = GetRandomPort();
50268 #endif
50269 
50270     server_args.signal = &ready;
50271     client_args.signal = &ready;
50272     clientCb.ssl_ready = test_export_keying_material_ssl_cb;
50273     client_args.callbacks = &clientCb;
50274 
50275     start_thread(test_server_nofail, &server_args, &serverThread);
50276     wait_tcp_ready(&server_args);
50277     test_client_nofail(&client_args, test_export_keying_material_cb);
50278     join_thread(serverThread);
50279 
50280     AssertTrue(client_args.return_code);
50281     AssertTrue(server_args.return_code);
50282 
50283     FreeTcpReady(&ready);
50284 
50285 #ifdef WOLFSSL_TIRTOS
50286     fdOpenSession(Task_self());
50287 #endif
50288 #endif /* !SINGLE_THREADED */
50289 }
50290 #endif /* HAVE_KEYING_MATERIAL */
50291 
50292 static int test_wolfSSL_THREADID_hash(void)
50293 {
50294     int ret = 0;
50295     unsigned long res;
50296 #if defined(OPENSSL_EXTRA)
50297     CRYPTO_THREADID id;
50298     printf(testingFmt, "wolfSSL_THREADID_hash");
50299     CRYPTO_THREADID_current(NULL);
50300     AssertTrue(1);
50301     res = CRYPTO_THREADID_hash(NULL);
50302     AssertTrue( res == 0UL);
50303     XMEMSET(&id, 0, sizeof(id));
50304     res = CRYPTO_THREADID_hash(&id);
50305     AssertTrue( res == 0UL);
50306     printf(resultFmt, passed);
50307 #endif /* OPENSSL_EXTRA */
50308     (void)res;
50309     return ret;
50310 }
50311 static int test_wolfSSL_CTX_set_ecdh_auto(void)
50312 {
50313     int ret = 0;
50314     WOLFSSL_CTX* ctx = NULL;
50315 #if defined(OPENSSL_EXTRA)
50316     printf(testingFmt, "SSL_CTX_set_ecdh_auto");
50317     AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
50318     AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
50319     AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
50320     AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
50321     printf(resultFmt, passed);
50322 #endif /* OPENSSL_EXTRA */
50323     (void)ctx;
50324     return ret;
50325 }
50326 
50327 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
50328 static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
50329 {
50330     callback_functions* callbacks = NULL;
50331     WOLFSSL_CTX* ctx = NULL;
50332     WOLFSSL*     ssl = NULL;
50333     SOCKET_T     sfd = 0;
50334     SOCKET_T     cfd = 0;
50335     word16       port;
50336     char msg[] = "I hear you fa shizzle!";
50337     int  len   = (int) XSTRLEN(msg);
50338     char input[1024];
50339     int  ret, err;
50340 
50341     if (!args)
50342         return 0;
50343 
50344     ((func_args*)args)->return_code = TEST_FAIL;
50345 
50346     callbacks   = ((func_args*)args)->callbacks;
50347     ctx         = wolfSSL_CTX_new(callbacks->method());
50348 
50349 #if defined(USE_WINDOWS_API)
50350     port = ((func_args*)args)->signal->port;
50351 #else
50352     /* Let tcp_listen assign port */
50353     port = 0;
50354 #endif
50355 
50356 #ifdef WOLFSSL_TIRTOS
50357     fdOpenSession(Task_self());
50358 #endif
50359 
50360     AssertIntEQ(WOLFSSL_SUCCESS,
50361         wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
50362 
50363     AssertIntEQ(WOLFSSL_SUCCESS,
50364         wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
50365                                         WOLFSSL_FILETYPE_PEM));
50366 
50367     AssertIntEQ(WOLFSSL_SUCCESS,
50368         wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
50369                                         WOLFSSL_FILETYPE_PEM));
50370 
50371     if (callbacks->ctx_ready)
50372         callbacks->ctx_ready(ctx);
50373 
50374     ssl = wolfSSL_new(ctx);
50375     AssertNotNull(ssl);
50376 
50377     /* listen and accept */
50378     tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
50379     CloseSocket(sfd);
50380 
50381     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
50382 
50383     if (callbacks->ssl_ready)
50384         callbacks->ssl_ready(ssl);
50385 
50386     do {
50387         err = 0; /* Reset error */
50388         ret = wolfSSL_accept(ssl);
50389         if (ret != WOLFSSL_SUCCESS) {
50390             err = wolfSSL_get_error(ssl, 0);
50391         }
50392     } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
50393 
50394     if (ret != WOLFSSL_SUCCESS) {
50395         wolfSSL_free(ssl);
50396         wolfSSL_CTX_free(ctx);
50397         CloseSocket(cfd);
50398         ((func_args*)args)->return_code = TEST_FAIL;
50399         return 0;
50400     }
50401 
50402     /* read and write data */
50403     XMEMSET( input, 0, sizeof(input));
50404 
50405     while (1) {
50406         ret = wolfSSL_read(ssl, input, sizeof(input));
50407         if (ret > 0) {
50408             break;
50409         }
50410         else {
50411             err = wolfSSL_get_error(ssl,ret);
50412             if (err == WOLFSSL_ERROR_WANT_READ) {
50413                 continue;
50414             }
50415             break;
50416         }
50417     }
50418 
50419     if (err == WOLFSSL_ERROR_ZERO_RETURN) {
50420         do {
50421             ret = wolfSSL_write(ssl, msg, len);
50422             if (ret > 0) {
50423                 break;
50424             }
50425         } while (ret < 0);
50426     }
50427 
50428     /* bidirectional shutdown */
50429     while ((ret = wolfSSL_shutdown(ssl)) != WOLFSSL_SUCCESS) {
50430         continue;
50431     }
50432 
50433     /* wait for the peer to disconnect the tcp connection */
50434     do {
50435         ret = wolfSSL_read(ssl, input, sizeof(input));
50436         err = wolfSSL_get_error(ssl, ret);
50437     } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
50438 
50439     /* detect TCP disconnect */
50440     AssertIntLE(ret,WOLFSSL_FAILURE);
50441     AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
50442 
50443     ((func_args*)args)->return_code = TEST_SUCCESS;
50444 
50445     wolfSSL_free(ssl);
50446     wolfSSL_CTX_free(ctx);
50447     CloseSocket(cfd);
50448 
50449     return 0;
50450 }
50451 static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
50452 {
50453     callback_functions* callbacks = NULL;
50454     WOLFSSL_CTX* ctx = NULL;
50455     WOLFSSL*     ssl = NULL;
50456     SOCKET_T     sfd = 0;
50457     char msg[] = "hello wolfssl server!";
50458     int  len   = (int) XSTRLEN(msg);
50459     char input[1024];
50460     int  idx;
50461     int  ret, err;
50462 
50463     if (!args)
50464         return 0;
50465 
50466     ((func_args*)args)->return_code = TEST_FAIL;
50467     callbacks   = ((func_args*)args)->callbacks;
50468     ctx         = wolfSSL_CTX_new(callbacks->method());
50469 
50470 #ifdef WOLFSSL_TIRTOS
50471     fdOpenSession(Task_self());
50472 #endif
50473 
50474     AssertIntEQ(WOLFSSL_SUCCESS,
50475                 wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
50476 
50477     AssertIntEQ(WOLFSSL_SUCCESS,
50478                 wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
50479                 WOLFSSL_FILETYPE_PEM));
50480 
50481     AssertIntEQ(WOLFSSL_SUCCESS,
50482                 wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
50483                 WOLFSSL_FILETYPE_PEM));
50484 
50485     AssertNotNull((ssl = wolfSSL_new(ctx)));
50486 
50487     tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
50488 
50489     AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
50490 
50491     do {
50492         err = 0; /* Reset error */
50493         ret = wolfSSL_connect(ssl);
50494         if (ret != WOLFSSL_SUCCESS) {
50495             err = wolfSSL_get_error(ssl, 0);
50496         }
50497     } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
50498 
50499     ret = wolfSSL_write(ssl, msg, len);
50500 
50501     if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
50502         input[idx] = 0;
50503     }
50504 
50505     ret = wolfSSL_shutdown(ssl);
50506     if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
50507         ret = wolfSSL_shutdown(ssl);
50508     }
50509     AssertIntEQ(ret, WOLFSSL_SUCCESS);
50510 
50511     ((func_args*)args)->return_code = TEST_SUCCESS;
50512 
50513     wolfSSL_free(ssl);
50514     wolfSSL_CTX_free(ctx);
50515     CloseSocket(sfd);
50516     return 0;
50517 }
50518 #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL */
50519 
50520 /* This test is to check wolfSSL_read behaves as same as
50521  * openSSL when it is called after SSL_shutdown completes.
50522  */
50523 static int test_wolfSSL_read_detect_TCP_disconnect(void)
50524 {
50525     int ret = 0;
50526 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
50527     tcp_ready ready;
50528     func_args client_args;
50529     func_args server_args;
50530     THREAD_TYPE serverThread;
50531     THREAD_TYPE clientThread;
50532     callback_functions server_cbf;
50533     callback_functions client_cbf;
50534 
50535 
50536     printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
50537 
50538 #ifdef WOLFSSL_TIRTOS
50539     fdOpenSession(Task_self());
50540 #endif
50541     StartTCP();
50542     InitTcpReady(&ready);
50543 
50544 #if defined(USE_WINDOWS_API)
50545     /* use RNG to get random port if using windows */
50546     ready.port = GetRandomPort();
50547 #endif
50548 
50549     XMEMSET(&client_args, 0, sizeof(func_args));
50550     XMEMSET(&server_args, 0, sizeof(func_args));
50551 
50552     XMEMSET(&server_cbf, 0, sizeof(callback_functions));
50553     XMEMSET(&client_cbf, 0, sizeof(callback_functions));
50554 
50555     server_cbf.method = wolfTLSv1_2_server_method;
50556     client_cbf.method = wolfTLSv1_2_client_method;
50557 
50558     server_args.callbacks = &server_cbf;
50559     client_args.callbacks = &client_cbf;
50560 
50561     server_args.signal = &ready;
50562     client_args.signal = &ready;
50563 
50564     start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
50565 
50566     wait_tcp_ready(&server_args);
50567 
50568     start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
50569 
50570     join_thread(clientThread);
50571     join_thread(serverThread);
50572 
50573     AssertTrue(client_args.return_code);
50574     AssertTrue(server_args.return_code);
50575 
50576     FreeTcpReady(&ready);
50577 
50578     printf(resultFmt, passed);
50579 #endif
50580     return ret;
50581 }
50582 static void test_wolfSSL_CTX_get_min_proto_version(void)
50583 {
50584 #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
50585     WOLFSSL_CTX *ctx;
50586 
50587     (void)ctx;
50588 
50589     printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
50590 
50591     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
50592     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
50593     #ifdef WOLFSSL_ALLOW_SSLV3
50594         AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
50595     #else
50596         AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
50597     #endif
50598     wolfSSL_CTX_free(ctx);
50599 
50600     #ifdef WOLFSSL_ALLOW_TLSV10
50601         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
50602     #else
50603         AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
50604     #endif
50605     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
50606     #ifdef WOLFSSL_ALLOW_TLSV10
50607         AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
50608     #else
50609         AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
50610     #endif
50611     wolfSSL_CTX_free(ctx);
50612 
50613     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
50614     AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
50615     #ifndef NO_OLD_TLS
50616         AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
50617     #else
50618         AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
50619     #endif
50620     wolfSSL_CTX_free(ctx);
50621 
50622     #ifndef WOLFSSL_NO_TLS12
50623         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
50624         AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
50625         AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
50626         wolfSSL_CTX_free(ctx);
50627     #endif
50628 
50629     #ifdef WOLFSSL_TLS13
50630         AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
50631         AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
50632         AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
50633         wolfSSL_CTX_free(ctx);
50634     #endif
50635 
50636     printf(resultFmt, passed);
50637 #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
50638 }
50639 
50640 static void test_wolfSSL_security_level(void)
50641 {
50642 #if defined(OPENSSL_EXTRA)
50643     printf(testingFmt, "test_wolfSSL_security_level()");
50644 
50645     SSL_CTX *ctx;
50646 
50647     #ifdef WOLFSSL_TLS13
50648         #ifdef NO_WOLFSSL_SERVER
50649             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
50650         #else
50651             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
50652         #endif
50653         SSL_CTX_set_security_level(ctx, 1);
50654         AssertTrue(1);
50655 
50656         AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
50657 
50658         SSL_CTX_free(ctx);
50659     #else
50660         (void)ctx;
50661     #endif
50662 
50663     printf(resultFmt, passed);
50664 #endif
50665 }
50666 
50667 static void test_wolfSSL_SSL_in_init(void)
50668 {
50669 #if defined(OPENSSL_ALL) && !defined(NO_BIO)
50670     printf(testingFmt, "test_wolfSSL_SSL_in_init()");
50671 
50672     SSL_CTX* ctx;
50673     SSL*     ssl;
50674     const char* testCertFile;
50675     const char* testKeyFile;
50676 
50677     #ifdef WOLFSSL_TLS13
50678         #ifdef NO_WOLFSSL_SERVER
50679             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
50680         #else
50681             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
50682         #endif
50683 #ifndef NO_RSA
50684         testCertFile = svrCertFile;
50685         testKeyFile = svrKeyFile;
50686 #elif defined(HAVE_ECC)
50687         testCertFile = eccCertFile;
50688         testKeyFile = eccKeyFile;
50689 #else
50690         testCertFile = NULL;
50691         testKeyFile = NULL;
50692 #endif
50693         if  (testCertFile != NULL && testKeyFile != NULL) {
50694             AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
50695                                                     SSL_FILETYPE_PEM));
50696             AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
50697                                                     SSL_FILETYPE_PEM));
50698         }
50699 
50700         ssl = SSL_new(ctx);
50701         AssertNotNull(ssl);
50702         AssertIntEQ(SSL_in_init(ssl), 1);
50703 
50704         SSL_CTX_free(ctx);
50705         SSL_free(ssl);
50706     #else
50707         (void)ctx;
50708         (void)ssl;
50709         (void)testCertFile;
50710         (void)testKeyFile;
50711     #endif
50712 
50713     printf(resultFmt, passed);
50714 #endif
50715 }
50716 
50717 static void test_wolfSSL_EC_curve(void)
50718 {
50719 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
50720     printf(testingFmt, "test_wolfSSL_EC_curve()");
50721     int nid = NID_secp160k1;
50722     const char* nid_name;
50723 
50724     AssertNotNull(nid_name = EC_curve_nid2nist(nid));
50725     AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
50726 
50727     AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
50728 
50729     printf(resultFmt, passed);
50730 #endif
50731 }
50732 
50733 static void test_wolfSSL_CTX_set_timeout(void)
50734 {
50735 #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
50736     int timeout;
50737     (void)timeout;
50738     printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
50739 
50740     WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
50741     AssertNotNull(ctx);
50742 
50743 #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
50744     /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
50745      * wolfSSL_CTX_set_timeout returns previous timeout value on success.
50746      */
50747     AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
50748     /* giving 0 as timeout value sets default timeout */
50749     timeout = wolfSSL_CTX_set_timeout(ctx, 0);
50750     AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
50751     AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
50752 
50753 #else
50754 
50755     AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
50756     AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
50757     AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
50758 
50759 #endif
50760     wolfSSL_CTX_free(ctx);
50761 
50762     printf(resultFmt, passed);
50763 #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
50764 }
50765 
50766 static void test_wolfSSL_OpenSSL_version(void)
50767 {
50768 #if defined(OPENSSL_EXTRA)
50769     printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
50770     const char* ver;
50771 
50772     #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
50773     AssertNotNull(ver = OpenSSL_version(0));
50774     #else
50775     AssertNotNull(ver = OpenSSL_version());
50776     #endif
50777     AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
50778             XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
50779     printf(resultFmt, passed);
50780 #endif
50781 }
50782 
50783 static void test_CONF_CTX_CMDLINE(void)
50784 {
50785 #if defined(OPENSSL_ALL)
50786     printf(testingFmt, "test_CONF_CTX_CMDLINE");
50787 
50788     SSL_CTX* ctx = NULL;
50789     SSL_CONF_CTX* cctx = NULL;
50790 
50791     AssertNotNull(cctx = SSL_CONF_CTX_new());
50792 
50793     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
50794     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
50795     AssertTrue(1);
50796 
50797     /* set flags */
50798     AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
50799                                             WOLFSSL_CONF_FLAG_CMDLINE);
50800     AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
50801                    WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
50802     /* cmd invalid command */
50803     AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
50804     AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
50805     AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
50806     AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
50807     AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
50808 
50809     /* cmd Certificate and Private Key*/
50810     {
50811     #if !defined(NO_CERTS) && !defined(NO_RSA)
50812         const char*  ourCert = svrCertFile;
50813         const char*  ourKey  = svrKeyFile;
50814 
50815         AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
50816         AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
50817                                                             WOLFSSL_SUCCESS);
50818         AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
50819         AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
50820         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50821     #endif
50822     }
50823 
50824     /* cmd curves */
50825     {
50826     #if defined(HAVE_ECC)
50827         const char* curve = "secp256r1";
50828 
50829         AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
50830         AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
50831         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50832     #endif
50833     }
50834 
50835     /* cmd CipherString */
50836     {
50837         char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
50838 
50839         AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
50840         AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
50841         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50842     }
50843 
50844     /* cmd DH parameter */
50845     {
50846     #if !defined(NO_DH) && !defined(NO_BIO)
50847         const char* ourdhcert = "./certs/dh2048.pem";
50848 
50849         AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
50850                                                         -3);
50851         AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
50852                                                         WOLFSSL_SUCCESS);
50853         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50854 
50855     #endif
50856     }
50857     SSL_CTX_free(ctx);
50858     SSL_CONF_CTX_free(cctx);
50859 
50860     printf(resultFmt, passed);
50861 #endif /* OPENSSL_EXTRA */
50862 }
50863 
50864 static void test_CONF_CTX_FILE(void)
50865 {
50866 #if defined(OPENSSL_ALL)
50867     printf(testingFmt, "test_CONF_CTX_FILE");
50868 
50869     SSL_CTX* ctx = NULL;
50870     SSL_CONF_CTX* cctx = NULL;
50871 
50872     AssertNotNull(cctx = SSL_CONF_CTX_new());
50873     AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
50874     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
50875     AssertTrue(1);
50876 
50877     /* set flags */
50878     AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
50879                                             WOLFSSL_CONF_FLAG_FILE);
50880     AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
50881                     WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
50882     /* sanity check */
50883     AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
50884     AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
50885     AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
50886     AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
50887     AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
50888 
50889     /* cmd Certificate and Private Key*/
50890     {
50891     #if !defined(NO_CERTS) && !defined(NO_RSA)
50892         const char*  ourCert = svrCertFile;
50893         const char*  ourKey  = svrKeyFile;
50894 
50895         AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
50896         AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
50897 
50898         AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
50899                                                             WOLFSSL_SUCCESS);
50900         AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
50901         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50902     #endif
50903     }
50904 
50905     /* cmd curves */
50906     {
50907     #if defined(HAVE_ECC)
50908         const char* curve = "secp256r1";
50909 
50910         AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
50911         AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
50912         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50913     #endif
50914     }
50915 
50916     /* cmd CipherString */
50917     {
50918         char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
50919 
50920         AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
50921         AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
50922         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50923     }
50924 
50925     /* cmd DH parameter */
50926     {
50927     #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
50928         const char* ourdhcert = "./certs/dh3072.pem";
50929 
50930         AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
50931         AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
50932                                                         WOLFSSL_SUCCESS);
50933         AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
50934 
50935     #endif
50936     }
50937     SSL_CTX_free(ctx);
50938     SSL_CONF_CTX_free(cctx);
50939 
50940     printf(resultFmt, passed);
50941 #endif /* OPENSSL_EXTRA */
50942 }
50943 
50944 static void test_wolfSSL_CRYPTO_get_ex_new_index(void)
50945 {
50946 #ifdef HAVE_EX_DATA
50947     int idx1,idx2;
50948 
50949     printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
50950 
50951     /* test for unsupported flass index */
50952     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION,
50953                                          0,NULL, NULL, NULL, NULL ), -1);
50954     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE,
50955                                          0,NULL, NULL, NULL, NULL ), -1);
50956     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
50957                                          0,NULL, NULL, NULL, NULL ), -1);
50958     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH,
50959                                          0,NULL, NULL, NULL, NULL ), -1);
50960     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA,
50961                                          0,NULL, NULL, NULL, NULL ), -1);
50962     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY,
50963                                          0,NULL, NULL, NULL, NULL ), -1);
50964     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA,
50965                                          0,NULL, NULL, NULL, NULL ), -1);
50966     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE,
50967                                          0,NULL, NULL, NULL, NULL ), -1);
50968     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI,
50969                                          0,NULL, NULL, NULL, NULL ), -1);
50970     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO,
50971                                          0,NULL, NULL, NULL, NULL ), -1);
50972     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
50973                                          0,NULL, NULL, NULL, NULL ), -1);
50974     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
50975                                          0,NULL, NULL, NULL, NULL ), -1);
50976     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG,
50977                                          0,NULL, NULL, NULL, NULL ), -1);
50978     AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
50979 
50980     /* test for supported class index */
50981     idx1 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
50982                                          0,NULL, NULL, NULL, NULL );
50983     idx2 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
50984                                          0,NULL, NULL, NULL, NULL );
50985     AssertIntNE(idx1, -1);
50986     AssertIntNE(idx2, -1);
50987     AssertIntNE(idx1, idx2);
50988 
50989     idx1 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
50990                                          0,NULL, NULL, NULL, NULL );
50991     idx2 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
50992                                          0,NULL, NULL, NULL, NULL );
50993     AssertIntNE(idx1, -1);
50994     AssertIntNE(idx2, -1);
50995     AssertIntNE(idx1, idx2);
50996 
50997     idx1 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
50998                                          0,NULL, NULL, NULL, NULL );
50999     idx2 = wolfSSL_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
51000                                          0,NULL, NULL, NULL, NULL );
51001     AssertIntNE(idx1, -1);
51002     AssertIntNE(idx2, -1);
51003     AssertIntNE(idx1, idx2);
51004 
51005     printf(resultFmt, "passed");
51006 #endif /* HAVE_EX_DATA */
51007 }
51008 
51009 static void test_wolfSSL_set_psk_use_session_callback(void)
51010 {
51011 #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
51012     printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
51013     SSL_CTX* ctx;
51014     SSL*     ssl;
51015     const char* testCertFile;
51016     const char* testKeyFile;
51017 
51018      #ifdef WOLFSSL_TLS13
51019         #ifdef NO_WOLFSSL_SERVER
51020             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
51021         #else
51022             AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
51023         #endif
51024 #ifndef NO_RSA
51025         testCertFile = svrCertFile;
51026         testKeyFile = svrKeyFile;
51027 #elif defined(HAVE_ECC)
51028         testCertFile = eccCertFile;
51029         testKeyFile = eccKeyFile;
51030 #else
51031         testCertFile = NULL;
51032         testKeyFile = NULL;
51033 #endif
51034         if  (testCertFile != NULL && testKeyFile != NULL) {
51035             AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
51036                                                     SSL_FILETYPE_PEM));
51037             AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
51038                                                     SSL_FILETYPE_PEM));
51039         }
51040 
51041         ssl = SSL_new(ctx);
51042         AssertNotNull(ssl);
51043 
51044         SSL_set_psk_use_session_callback(ssl,
51045                             my_psk_use_session_cb);
51046         AssertTrue(1);
51047 
51048         SSL_CTX_free(ctx);
51049         SSL_free(ssl);
51050     #else
51051         (void)ctx;
51052         (void)ssl;
51053         (void)testCertFile;
51054         (void)testKeyFile;
51055     #endif
51056     printf(resultFmt, passed);
51057 #endif
51058 }
51059 
51060 static void test_wolfSSL_DH(void)
51061 {
51062 #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
51063     DH *dh = NULL;
51064     BIGNUM* p;
51065     BIGNUM* q;
51066     BIGNUM* g;
51067     BIGNUM* pub;
51068     BIGNUM* priv;
51069     (void)dh;
51070     (void)p;
51071     (void)q;
51072     (void)g;
51073     (void)pub;
51074     (void)priv;
51075 
51076 #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
51077 #if !defined(HAVE_FIPS) || \
51078         (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
51079     FILE* f = NULL;
51080     unsigned char buf[268];
51081     const unsigned char* pt = buf;
51082     long len = 0;
51083 
51084     dh = NULL;
51085     XMEMSET(buf, 0, sizeof(buf));
51086     /* Test 2048 bit parameters */
51087     f = XFOPEN("./certs/dh2048.der", "rb");
51088     AssertTrue(f != XBADFILE);
51089     len = (long)XFREAD(buf, 1, sizeof(buf), f);
51090     XFCLOSE(f);
51091 
51092     AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
51093     AssertNotNull(dh->p);
51094     AssertNotNull(dh->g);
51095     AssertTrue(pt != buf);
51096     AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
51097 
51098     DH_get0_pqg(dh, (const BIGNUM**)&p,
51099                     (const BIGNUM**)&q,
51100                     (const BIGNUM**) &g);
51101     AssertPtrEq(p, dh->p);
51102     AssertPtrEq(q, dh->q);
51103     AssertPtrEq(g, dh->g);
51104     DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
51105     AssertPtrEq(pub, dh->pub_key);
51106     AssertPtrEq(priv, dh->priv_key);
51107     AssertNotNull(pub = BN_new());
51108     AssertNotNull(priv = BN_new());
51109     AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
51110     AssertPtrEq(pub, dh->pub_key);
51111     AssertPtrEq(priv, dh->priv_key);
51112 
51113     DH_free(dh);
51114 
51115     AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
51116     DH_free(dh);
51117 #endif
51118 #endif
51119     printf(testingFmt, "test_wolfSSL_DH");
51120 
51121     dh =  wolfSSL_DH_new();
51122     AssertNotNull(dh);
51123 
51124     /* invalid parameters test */
51125     DH_get0_pqg(NULL, (const BIGNUM**)&p,
51126                       (const BIGNUM**)&q,
51127                       (const BIGNUM**)&g);
51128 
51129     DH_get0_pqg(dh, NULL,
51130                     (const BIGNUM**)&q,
51131                     (const BIGNUM**)&g);
51132 
51133     DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
51134 
51135     DH_get0_pqg(dh, NULL, NULL, NULL);
51136     AssertTrue(1);
51137 
51138     DH_get0_pqg(dh, (const BIGNUM**)&p,
51139                     (const BIGNUM**)&q,
51140                     (const BIGNUM**)&g);
51141 
51142     AssertPtrEq(p, NULL);
51143     AssertPtrEq(q, NULL);
51144     AssertPtrEq(g, NULL);
51145     DH_free(dh);
51146     printf(resultFmt, passed);
51147 #endif /* OPENSSL_EXTRA && !NO_DH */
51148 }
51149 
51150 static void test_wolfSSL_ERR_strings(void)
51151 {
51152     const char* err1 = "unsupported cipher suite";
51153     const char* err2 = "wolfSSL PEM routines";
51154     const char* err  = NULL;
51155 
51156     (void)err;
51157     (void)err1;
51158     (void)err2;
51159 #if !defined(NO_ERROR_STRINGS)
51160     printf(testingFmt, "test_wolfSSL_ERR_strings");
51161 
51162 #if defined(OPENSSL_EXTRA)
51163     err = ERR_reason_error_string(UNSUPPORTED_SUITE);
51164     AssertTrue(err != NULL);
51165     AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
51166 
51167     err = ERR_func_error_string(UNSUPPORTED_SUITE);
51168     AssertTrue(err != NULL);
51169     AssertIntEQ((*err == '\0'), 1);
51170 
51171     err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
51172     AssertTrue(err != NULL);
51173     AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
51174 #else
51175     err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
51176     AssertTrue(err != NULL);
51177     AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
51178 
51179     err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
51180     AssertTrue(err != NULL);
51181     AssertIntEQ((*err == '\0'), 1);
51182 
51183     /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
51184     err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
51185     AssertTrue(err != NULL);
51186     AssertIntEQ((*err == '\0'), 1);
51187 #endif
51188     printf(resultFmt, passed);
51189 #endif
51190 }
51191 static void test_wolfSSL_EVP_shake128(void)
51192 {
51193 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
51194                                             defined(WOLFSSL_SHAKE128)
51195     printf(testingFmt, "test_wolfSSL_EVP_shake128");
51196     const EVP_MD* md = NULL;
51197     md = EVP_shake128();
51198     AssertTrue(md != NULL);
51199     AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
51200     printf(resultFmt, passed);
51201 #endif
51202 }
51203 
51204 static void test_wolfSSL_EVP_shake256(void)
51205 {
51206 #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
51207                                             defined(WOLFSSL_SHAKE256)
51208     printf(testingFmt, "test_wolfSSL_EVP_shake256");
51209     const EVP_MD* md = NULL;
51210     md = EVP_shake256();
51211     AssertTrue(md != NULL);
51212     AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
51213     printf(resultFmt, passed);
51214 #endif
51215 }
51216 
51217 static void test_EVP_blake2(void)
51218 {
51219 #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
51220     printf(testingFmt, "test_EVP_blake2");
51221 
51222     const EVP_MD* md = NULL;
51223     (void)md;
51224 
51225 #if defined(HAVE_BLAKE2)
51226     md = EVP_blake2b512();
51227     AssertTrue(md != NULL);
51228     AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
51229 #endif
51230 
51231 #if defined(HAVE_BLAKE2S)
51232     md = EVP_blake2s256();
51233     AssertTrue(md != NULL);
51234     AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
51235 #endif
51236     printf(resultFmt, passed);
51237 #endif
51238 }
51239 
51240 #if defined(OPENSSL_EXTRA)
51241 static void list_md_fn(const EVP_MD* m, const char* from,
51242                        const char* to, void* arg)
51243 {
51244     const char* mn;
51245     BIO *bio;
51246 
51247     (void) from;
51248     (void) to;
51249     (void) arg;
51250     (void) mn;
51251     (void) bio;
51252 
51253     if (!m) {
51254         /* alias */
51255         AssertNull(m);
51256         AssertNotNull(to);
51257     }
51258     else {
51259         AssertNotNull(m);
51260         AssertNull(to);
51261     }
51262 
51263     AssertNotNull(from);
51264 
51265 #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
51266     mn = EVP_get_digestbyname(from);
51267     /* print to stdout */
51268     AssertNotNull(arg);
51269 
51270     bio = BIO_new(BIO_s_file());
51271     BIO_set_fp(bio, arg, BIO_NOCLOSE);
51272     BIO_printf(bio, "Use %s message digest algorithm\n", mn);
51273     BIO_free(bio);
51274 #endif
51275 }
51276 #endif
51277 
51278 static void test_EVP_MD_do_all(void)
51279 {
51280 #if defined(OPENSSL_EXTRA)
51281     printf(testingFmt, "test_EVP_MD_do_all");
51282 
51283     EVP_MD_do_all(NULL, stdout);
51284     /* to confirm previous call gives no harm */
51285     AssertTrue(1);
51286 
51287 
51288     EVP_MD_do_all(list_md_fn, stdout);
51289     /* to confirm previous call gives no harm */
51290     AssertTrue(1);
51291 
51292     printf(resultFmt, passed);
51293 #endif
51294 }
51295 
51296 #if defined(OPENSSL_EXTRA)
51297 static void obj_name_t(const OBJ_NAME* nm, void* arg)
51298 {
51299     (void)arg;
51300     (void)nm;
51301 
51302     AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
51303 
51304 #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
51305     /* print to stdout */
51306     AssertNotNull(arg);
51307 
51308     bio = BIO_new(BIO_s_file());
51309     BIO_set_fp(bio, arg, BIO_NOCLOSE);
51310     BIO_printf(bio, "%s\n", mn);
51311     BIO_free(bio);
51312 #endif
51313 }
51314 
51315 #endif
51316 static void test_OBJ_NAME_do_all(void)
51317 {
51318 #if defined(OPENSSL_EXTRA)
51319     printf(testingFmt, "test_OBJ_NAME_do_all");
51320 
51321     OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
51322     /* to confirm previous call gives no harm */
51323     AssertTrue(1);
51324 
51325     OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
51326     /* to confirm previous call gives no harm */
51327     AssertTrue(1);
51328 
51329     OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
51330     AssertTrue(1);
51331     OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
51332     AssertTrue(1);
51333     OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
51334     AssertTrue(1);
51335     OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
51336     AssertTrue(1);
51337     OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
51338     AssertTrue(1);
51339     OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
51340     AssertTrue(1);
51341     OBJ_NAME_do_all(-1, obj_name_t, stdout);
51342     AssertTrue(1);
51343 
51344     printf(resultFmt, passed);
51345 #endif
51346 }
51347 
51348 static void test_SSL_CIPHER_get_xxx(void)
51349 {
51350 #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
51351        !defined(NO_FILESYSTEM)
51352 
51353     printf(testingFmt, "test_SSL_CIPHER_get_xxx");
51354 
51355     const SSL_CIPHER* cipher = NULL;
51356     STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
51357     int i, numCiphers = 0;
51358     SSL_CTX* ctx = NULL;
51359     SSL*     ssl = NULL;
51360     const char* testCertFile;
51361     const char* testKeyFile;
51362     char buf[256] = {0};
51363 
51364     const char* cipher_id = NULL;
51365     int   expect_nid1 = NID_undef;
51366     int   expect_nid2 = NID_undef;
51367     int   expect_nid3 = NID_undef;
51368     int   expect_nid4 = NID_undef;
51369     int   expect_nid5 = 0;
51370 
51371     const char* cipher_id2 = NULL;
51372     int   expect_nid21 = NID_undef;
51373     int   expect_nid22 = NID_undef;
51374     int   expect_nid23 = NID_undef;
51375     int   expect_nid24 = NID_undef;
51376     int   expect_nid25 = 0;
51377 
51378     (void)cipher;
51379     (void)supportedCiphers;
51380     (void)i;
51381     (void)numCiphers;
51382     (void)ctx;
51383     (void)ssl;
51384     (void)testCertFile;
51385     (void)testKeyFile;
51386 
51387 #if defined(WOLFSSL_TLS13)
51388     cipher_id = "TLS13-AES128-GCM-SHA256";
51389     expect_nid1 = NID_auth_rsa;
51390     expect_nid2 = NID_aes_128_gcm;
51391     expect_nid3 = NID_sha256;
51392     expect_nid4 = NID_kx_any;
51393     expect_nid5 = 1;
51394 
51395     #if !defined(WOLFSSL_NO_TLS12)
51396     cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
51397     expect_nid21 = NID_auth_rsa;
51398     expect_nid22 = NID_aes_256_gcm;
51399     expect_nid23 = NID_sha384;
51400     expect_nid24 = NID_kx_ecdhe;
51401     expect_nid25 = 1;
51402     #endif
51403 #endif
51404 
51405     #ifdef NO_WOLFSSL_SERVER
51406         AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
51407     #else
51408         AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
51409     #endif
51410 
51411     if (cipher_id) {
51412     #ifndef NO_RSA
51413         testCertFile = svrCertFile;
51414         testKeyFile = svrKeyFile;
51415     #elif defined(HAVE_ECC)
51416         testCertFile = eccCertFile;
51417         testKeyFile = eccKeyFile;
51418     #else
51419         testCertFile = NULL;
51420         testKeyFile = NULL;
51421     #endif
51422         if  (testCertFile != NULL && testKeyFile != NULL) {
51423             AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
51424                                                     SSL_FILETYPE_PEM));
51425             AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
51426                                                     SSL_FILETYPE_PEM));
51427         }
51428 
51429         ssl = SSL_new(ctx);
51430         AssertNotNull(ssl);
51431         AssertIntEQ(SSL_in_init(ssl), 1);
51432 
51433         supportedCiphers = SSL_get_ciphers(ssl);
51434         numCiphers = sk_num(supportedCiphers);
51435 
51436         for (i = 0; i < numCiphers; ++i) {
51437 
51438             if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
51439                 SSL_CIPHER_description(cipher, buf, sizeof(buf));
51440             }
51441 
51442             if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
51443                 break;
51444             }
51445         }
51446         /* test case for */
51447         if (i != numCiphers) {
51448             AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
51449             AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
51450             AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
51451             AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
51452             AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
51453         }
51454 
51455         if (cipher_id2) {
51456 
51457             for (i = 0; i < numCiphers; ++i) {
51458 
51459                 if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
51460                     SSL_CIPHER_description(cipher, buf, sizeof(buf));
51461                 }
51462 
51463                 if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
51464                     break;
51465                 }
51466             }
51467             /* test case for */
51468             if (i != numCiphers) {
51469                 AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
51470                 AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
51471                 AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
51472                 AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
51473                 AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
51474             }
51475         }
51476     }
51477 
51478     if (ctx)
51479         SSL_CTX_free(ctx);
51480     if(ssl)
51481         SSL_free(ssl);
51482 
51483     printf(resultFmt, passed);
51484 #endif
51485 }
51486 
51487 #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
51488 
51489 static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
51490     int* keyFormat)
51491 {
51492     int ret;
51493     byte* key_buf = NULL;
51494     size_t key_sz = 0;
51495     EncryptedInfo encInfo;
51496 
51497     XMEMSET(&encInfo, 0, sizeof(encInfo));
51498 
51499     ret = load_file(privKeyFile, &key_buf, &key_sz);
51500     if (ret == 0) {
51501         ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
51502             NULL, &encInfo, keyFormat);
51503     }
51504 
51505     if (key_buf != NULL) {
51506         free(key_buf); key_buf = NULL;
51507     }
51508     (void)encInfo; /* not used in this test */
51509 
51510 #ifdef DEBUG_WOLFSSL
51511     printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
51512         (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
51513         (*pDer)->length);
51514 #endif
51515 
51516     return ret;
51517 }
51518 static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
51519 {
51520     int ret = CRYPTOCB_UNAVAILABLE;
51521     const char* privKeyFile = (const char*)ctx;
51522     DerBuffer* pDer = NULL;
51523     int keyFormat = 0;
51524 
51525     if (info->algo_type == WC_ALGO_TYPE_PK) {
51526     #ifdef DEBUG_WOLFSSL
51527         printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
51528     #endif
51529 
51530     #ifndef NO_RSA
51531         if (info->pk.type == WC_PK_TYPE_RSA) {
51532             switch (info->pk.rsa.type) {
51533                 case RSA_PUBLIC_ENCRYPT:
51534                 case RSA_PUBLIC_DECRYPT:
51535                     /* perform software based RSA public op */
51536                     ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
51537                     break;
51538                 case RSA_PRIVATE_ENCRYPT:
51539                 case RSA_PRIVATE_DECRYPT:
51540                 {
51541                     RsaKey key;
51542 
51543                     /* perform software based RSA private op */
51544                 #ifdef DEBUG_WOLFSSL
51545                     printf("test_CryptoCb_Func: RSA Priv\n");
51546                 #endif
51547 
51548                     ret = load_pem_key_file_as_der(privKeyFile, &pDer,
51549                         &keyFormat);
51550                     if (ret != 0) {
51551                         return ret;
51552                     }
51553                     ret = wc_InitRsaKey(&key, NULL);
51554                     if (ret == 0) {
51555                         word32 keyIdx = 0;
51556                         /* load RSA private key and perform private transform */
51557                         ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
51558                             &key, pDer->length);
51559                         if (ret == 0) {
51560                             ret = wc_RsaFunction(
51561                                 info->pk.rsa.in, info->pk.rsa.inLen,
51562                                 info->pk.rsa.out, info->pk.rsa.outLen,
51563                                 info->pk.rsa.type, &key, info->pk.rsa.rng);
51564                         }
51565                         else {
51566                             /* if decode fails, then fall-back to software based crypto */
51567                             printf("test_CryptoCb_Func: RSA private key decode "
51568                                 "failed %d, falling back to software\n", ret);
51569                             ret = CRYPTOCB_UNAVAILABLE;
51570                         }
51571                         wc_FreeRsaKey(&key);
51572                     }
51573                     wc_FreeDer(&pDer); pDer = NULL;
51574                     break;
51575                 }
51576             }
51577         #ifdef DEBUG_WOLFSSL
51578             printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
51579                 info->pk.rsa.type, ret, *info->pk.rsa.outLen);
51580         #endif
51581         }
51582     #endif /* !NO_RSA */
51583     #ifdef HAVE_ECC
51584         if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
51585             /* mark this key as ephemeral */
51586             if (info->pk.eckg.key != NULL) {
51587                 XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
51588                     sizeof(info->pk.eckg.key->label));
51589                 info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
51590             }
51591         }
51592         else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
51593             ecc_key key;
51594 
51595             /* perform software based ECC sign */
51596         #ifdef DEBUG_WOLFSSL
51597             printf("test_CryptoCb_Func: ECC Sign\n");
51598         #endif
51599 
51600             if (info->pk.eccsign.key != NULL &&
51601                 XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
51602                 /* this is an empheral key */
51603             #ifdef DEBUG_WOLFSSL
51604                 printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
51605             #endif
51606                 return CRYPTOCB_UNAVAILABLE;
51607             }
51608 
51609             ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
51610             if (ret != 0) {
51611                 return ret;
51612             }
51613 
51614             ret = wc_ecc_init(&key);
51615             if (ret == 0) {
51616                 word32 keyIdx = 0;
51617                 /* load ECC private key and perform private transform */
51618                 ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
51619                     &key, pDer->length);
51620                 if (ret == 0) {
51621                     ret = wc_ecc_sign_hash(
51622                         info->pk.eccsign.in, info->pk.eccsign.inlen,
51623                         info->pk.eccsign.out, info->pk.eccsign.outlen,
51624                         info->pk.eccsign.rng, &key);
51625                 }
51626                 else {
51627                     /* if decode fails, then fall-back to software based crypto */
51628                     printf("test_CryptoCb_Func: ECC private key decode "
51629                         "failed %d, falling back to software\n", ret);
51630                     ret = CRYPTOCB_UNAVAILABLE;
51631                 }
51632                 wc_ecc_free(&key);
51633             }
51634             wc_FreeDer(&pDer); pDer = NULL;
51635 
51636         #ifdef DEBUG_WOLFSSL
51637             printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
51638                 ret, *info->pk.eccsign.outlen);
51639         #endif
51640         }
51641     #endif /* HAVE_ECC */
51642     #ifdef HAVE_ED25519
51643         if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
51644             ed25519_key key;
51645 
51646             /* perform software based ED25519 sign */
51647         #ifdef DEBUG_WOLFSSL
51648             printf("test_CryptoCb_Func: ED25519 Sign\n");
51649         #endif
51650 
51651             ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
51652             if (ret != 0) {
51653                 return ret;
51654             }
51655             ret = wc_ed25519_init(&key);
51656             if (ret == 0) {
51657                 word32 keyIdx = 0;
51658                 /* load ED25519 private key and perform private transform */
51659                 ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
51660                     &key, pDer->length);
51661                 if (ret == 0) {
51662                     /* calculate public key */
51663                     ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
51664                     if (ret == 0) {
51665                         key.pubKeySet = 1;
51666                         ret = wc_ed25519_sign_msg_ex(
51667                             info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
51668                             info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
51669                             &key, info->pk.ed25519sign.type,
51670                             info->pk.ed25519sign.context,
51671                             info->pk.ed25519sign.contextLen);
51672                     }
51673                 }
51674                 else {
51675                     /* if decode fails, then fall-back to software based crypto */
51676                     printf("test_CryptoCb_Func: ED25519 private key decode "
51677                         "failed %d, falling back to software\n", ret);
51678                     ret = CRYPTOCB_UNAVAILABLE;
51679                 }
51680                 wc_ed25519_free(&key);
51681             }
51682             wc_FreeDer(&pDer); pDer = NULL;
51683 
51684         #ifdef DEBUG_WOLFSSL
51685             printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
51686                 ret, *info->pk.ed25519sign.outLen);
51687         #endif
51688         }
51689     #endif /* HAVE_ED25519 */
51690     }
51691     (void)thisDevId;
51692     (void)keyFormat;
51693 
51694     return ret;
51695 }
51696 
51697 /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
51698 static void test_wc_CryptoCb_TLS(int tlsVer,
51699     const char* cliCaPemFile, const char* cliCertPemFile,
51700     const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
51701     const char* svrCaPemFile, const char* svrCertPemFile,
51702     const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
51703 {
51704     callback_functions client_cbf;
51705     callback_functions server_cbf;
51706 
51707     XMEMSET(&client_cbf, 0, sizeof(client_cbf));
51708     XMEMSET(&server_cbf, 0, sizeof(server_cbf));
51709 
51710     if (tlsVer == WOLFSSL_TLSV1_3) {
51711     #ifdef WOLFSSL_TLS13
51712         server_cbf.method = wolfTLSv1_3_server_method;
51713         client_cbf.method = wolfTLSv1_3_client_method;
51714     #endif
51715     }
51716     else if (tlsVer == WOLFSSL_TLSV1_2) {
51717     #ifndef WOLFSSL_NO_TLS12
51718         server_cbf.method = wolfTLSv1_2_server_method;
51719         client_cbf.method = wolfTLSv1_2_client_method;
51720     #endif
51721     }
51722     else if (tlsVer == WOLFSSL_TLSV1_1) {
51723     #ifndef NO_OLD_TLS
51724         server_cbf.method = wolfTLSv1_1_server_method;
51725         client_cbf.method = wolfTLSv1_1_client_method;
51726     #endif
51727     }
51728     else if (tlsVer == WOLFSSL_TLSV1) {
51729     #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
51730         server_cbf.method = wolfTLSv1_server_method;
51731         client_cbf.method = wolfTLSv1_client_method;
51732     #endif
51733     }
51734     else if (tlsVer == WOLFSSL_SSLV3) {
51735     #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
51736          defined(WOLFSSL_STATIC_RSA)
51737         server_cbf.method = wolfSSLv3_server_method;
51738         client_cbf.method = wolfSSLv3_client_method;
51739     #endif
51740     }
51741     else if (tlsVer == WOLFSSL_DTLSV1_2) {
51742     #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
51743         server_cbf.method = wolfDTLSv1_2_server_method;
51744         client_cbf.method = wolfDTLSv1_2_client_method;
51745     #endif
51746     }
51747     else if (tlsVer == WOLFSSL_DTLSV1) {
51748     #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
51749         server_cbf.method = wolfDTLSv1_server_method;
51750         client_cbf.method = wolfDTLSv1_client_method;
51751     #endif
51752     }
51753 
51754     if (server_cbf.method == NULL) {
51755         /* not enabled */
51756         return;
51757     }
51758 
51759     /* Setup the keys for the TLS test */
51760     client_cbf.certPemFile = cliCertPemFile;
51761     client_cbf.keyPemFile = cliPubKeyPemFile;
51762     client_cbf.caPemFile = cliCaPemFile;
51763 
51764     server_cbf.certPemFile = svrCertPemFile;
51765     server_cbf.keyPemFile = svrPubKeyPemFile;
51766     server_cbf.caPemFile = svrCaPemFile;
51767 
51768     /* Setup a crypto callback with pointer to private key file for testing */
51769     client_cbf.devId = 1;
51770     wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
51771         (void*)cliPrivKeyPemFile);
51772     server_cbf.devId = 2;
51773     wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
51774         (void*)svrPrivKeyPemFile);
51775 
51776     /* Perform TLS server and client test */
51777     /* First test is at WOLFSSL_CTX level */
51778     test_wolfSSL_client_server(&client_cbf, &server_cbf);
51779     /* Check for success */
51780     AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
51781     AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
51782 
51783     /* Second test is a WOLFSSL object level */
51784     client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
51785     test_wolfSSL_client_server(&client_cbf, &server_cbf);
51786 
51787     /* Check for success */
51788     AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
51789     AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
51790 
51791     /* Un register the devId's */
51792     wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
51793     client_cbf.devId = INVALID_DEVID;
51794     wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
51795     server_cbf.devId = INVALID_DEVID;
51796 }
51797 #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
51798 
51799 static void test_wc_CryptoCb(void)
51800 {
51801 #ifdef WOLF_CRYPTO_CB
51802     /* TODO: Add crypto callback API tests */
51803 
51804 #ifdef HAVE_IO_TESTS_DEPENDENCIES
51805     #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
51806     int tlsVer;
51807     #endif
51808 
51809     #ifndef NO_RSA
51810     for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
51811         test_wc_CryptoCb_TLS(tlsVer,
51812             svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
51813             cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
51814     }
51815     #endif
51816     #ifdef HAVE_ECC
51817     for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
51818         test_wc_CryptoCb_TLS(tlsVer,
51819             caEccCertFile,  cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
51820             cliEccCertFile, eccCertFile,    eccKeyFile,    eccKeyPubFile);
51821     }
51822     #endif
51823     #ifdef HAVE_ED25519
51824     for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
51825         if (tlsVer == WOLFSSL_DTLSV1) continue;
51826         test_wc_CryptoCb_TLS(tlsVer,
51827             caEdCertFile,  cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
51828             cliEdCertFile, edCertFile,    edKeyFile,    edKeyPubFile);
51829     }
51830     #endif
51831 #endif /* HAVE_IO_TESTS_DEPENDENCIES */
51832 #endif /* WOLF_CRYPTO_CB */
51833 }
51834 
51835 #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
51836 
51837 /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
51838 static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
51839     const char* cliCaPemFile, const char* cliCertPemFile,
51840     const char* cliPrivKeyPemFile,
51841     const char* svrCaPemFile, const char* svrCertPemFile,
51842     const char* svrPrivKeyPemFile,
51843     byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
51844 {
51845     callback_functions client_cbf;
51846     callback_functions server_cbf;
51847 
51848     XMEMSET(&client_cbf, 0, sizeof(client_cbf));
51849     XMEMSET(&server_cbf, 0, sizeof(server_cbf));
51850 
51851     if (tlsVer == WOLFSSL_TLSV1_3) {
51852     #ifdef WOLFSSL_TLS13
51853         server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
51854         client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
51855     #endif
51856     }
51857     else if (tlsVer == WOLFSSL_TLSV1_2) {
51858     #ifndef WOLFSSL_NO_TLS12
51859         server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
51860         client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
51861     #endif
51862     }
51863     else if (tlsVer == WOLFSSL_TLSV1_1) {
51864     #ifndef NO_OLD_TLS
51865         server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
51866         client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
51867     #endif
51868     }
51869     else if (tlsVer == WOLFSSL_TLSV1) {
51870     #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
51871         server_cbf.method_ex = wolfTLSv1_server_method_ex;
51872         client_cbf.method_ex = wolfTLSv1_client_method_ex;
51873     #endif
51874     }
51875     else if (tlsVer == WOLFSSL_SSLV3) {
51876     #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
51877          defined(WOLFSSL_STATIC_RSA)
51878         server_cbf.method_ex = wolfSSLv3_server_method_ex;
51879         client_cbf.method_ex = wolfSSLv3_client_method_ex;
51880     #endif
51881     }
51882     else if (tlsVer == WOLFSSL_DTLSV1_2) {
51883     #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
51884         server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
51885         client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
51886     #endif
51887     }
51888     else if (tlsVer == WOLFSSL_DTLSV1) {
51889     #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
51890         server_cbf.method_ex = wolfDTLSv1_server_method_ex;
51891         client_cbf.method_ex = wolfDTLSv1_client_method_ex;
51892     #endif
51893     }
51894 
51895     if (server_cbf.method_ex == NULL) {
51896         /* not enabled */
51897         return;
51898     }
51899 
51900     /* Setup the keys for the TLS test */
51901     client_cbf.certPemFile = cliCertPemFile;
51902     client_cbf.keyPemFile = cliPrivKeyPemFile;
51903     client_cbf.caPemFile = cliCaPemFile;
51904 
51905     server_cbf.certPemFile = svrCertPemFile;
51906     server_cbf.keyPemFile = svrPrivKeyPemFile;
51907     server_cbf.caPemFile = svrCaPemFile;
51908 
51909     client_cbf.mem = cliMem;
51910     client_cbf.memSz = cliMemSz;
51911     server_cbf.mem = svrMem;
51912     server_cbf.memSz = svrMemSz;
51913 
51914     client_cbf.devId = INVALID_DEVID;
51915     server_cbf.devId = INVALID_DEVID;
51916 
51917     /* Perform TLS server and client test */
51918     /* First test is at WOLFSSL_CTX level */
51919     test_wolfSSL_client_server(&client_cbf, &server_cbf);
51920     /* Check for success */
51921     AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
51922     AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
51923 
51924     /* Second test is a WOLFSSL object level */
51925     client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
51926     test_wolfSSL_client_server(&client_cbf, &server_cbf);
51927 
51928     /* Check for success */
51929     AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
51930     AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
51931 }
51932 #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
51933 
51934 #ifdef WOLFSSL_STATIC_MEMORY
51935 #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
51936         defined(SESSION_CERTS)
51937     #ifdef OPENSSL_EXTRA
51938     #define TEST_TLS_STATIC_MEMSZ (400000)
51939     #else
51940     #define TEST_TLS_STATIC_MEMSZ (320000)
51941     #endif
51942 #else
51943     #define TEST_TLS_STATIC_MEMSZ (80000)
51944 #endif
51945 
51946 static void test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
51947 {
51948     WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
51949     WOLFSSL_MEM_STATS mem_stats;
51950     WOLFSSL_MEM_CONN_STATS ssl_stats;
51951 
51952 #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
51953     AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
51954         WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
51955     AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
51956         WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
51957 #endif
51958 
51959     AssertNotNull((ssl1 = wolfSSL_new(ctx)));
51960     AssertNotNull((ssl2 = wolfSSL_new(ctx)));
51961     /* this should fail because kMaxCtxClients == 2 */
51962     AssertNull((ssl3 = wolfSSL_new(ctx)));
51963 
51964     if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
51965     #ifdef DEBUG_WOLFSSL
51966         wolfSSL_PrintStatsConn(&ssl_stats);
51967     #endif
51968         (void)ssl_stats;
51969     }
51970 
51971     /* display collected statistics */
51972     if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
51973     #ifdef DEBUG_WOLFSSL
51974         wolfSSL_PrintStats(&mem_stats);
51975     #endif
51976         (void)mem_stats;
51977     }
51978 
51979     wolfSSL_free(ssl1);
51980     wolfSSL_free(ssl2);
51981 }
51982 #endif /* WOLFSSL_STATIC_MEMORY */
51983 
51984 static void test_wolfSSL_CTX_StaticMemory(void)
51985 {
51986 #ifdef WOLFSSL_STATIC_MEMORY
51987     wolfSSL_method_func method_func;
51988     WOLFSSL_CTX* ctx;
51989     const int kMaxCtxClients = 2;
51990     #ifdef HAVE_IO_TESTS_DEPENDENCIES
51991     #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
51992     int tlsVer;
51993     byte cliMem[TEST_TLS_STATIC_MEMSZ];
51994     #endif
51995     #endif
51996     byte svrMem[TEST_TLS_STATIC_MEMSZ];
51997 
51998     printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
51999 
52000 #ifndef NO_WOLFSSL_SERVER
52001     #ifndef WOLFSSL_NO_TLS12
52002         method_func = wolfTLSv1_2_server_method_ex;
52003     #else
52004         method_func = wolfTLSv1_3_server_method_ex;
52005     #endif
52006 #else
52007     #ifndef WOLFSSL_NO_TLS12
52008         method_func = wolfTLSv1_2_client_method_ex;
52009     #else
52010         method_func = wolfTLSv1_3_client_method_ex;
52011     #endif
52012 #endif
52013 
52014     /* Test creating CTX directly from static memory pool */
52015     ctx = NULL;
52016     AssertIntEQ(wolfSSL_CTX_load_static_memory(
52017             &ctx, method_func, svrMem, sizeof(svrMem),
52018             0, kMaxCtxClients), WOLFSSL_SUCCESS);
52019     test_wolfSSL_CTX_StaticMemory_SSL(ctx);
52020     wolfSSL_CTX_free(ctx);
52021     ctx = NULL;
52022 
52023     /* Test for heap allocated CTX, then assigning static pool to it */
52024     AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
52025     AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
52026         NULL, svrMem, sizeof(svrMem),
52027         0, kMaxCtxClients), WOLFSSL_SUCCESS);
52028     test_wolfSSL_CTX_StaticMemory_SSL(ctx);
52029     wolfSSL_CTX_free(ctx);
52030 
52031     /* TLS Level Tests using static memory */
52032 #ifdef HAVE_IO_TESTS_DEPENDENCIES
52033     #ifndef NO_RSA
52034     for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
52035         test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
52036             svrCertFile, cliCertFile, cliKeyFile,
52037             cliCertFile, svrCertFile, svrKeyFile,
52038             cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
52039     }
52040     #endif
52041     #ifdef HAVE_ECC
52042     for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
52043         test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
52044             caEccCertFile,  cliEccCertFile, cliEccKeyFile,
52045             cliEccCertFile, eccCertFile,    eccKeyFile,
52046             cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
52047     }
52048     #endif
52049     #ifdef HAVE_ED25519
52050     for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
52051         if (tlsVer == WOLFSSL_DTLSV1) continue;
52052         test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
52053             caEdCertFile,  cliEdCertFile, cliEdKeyFile,
52054             cliEdCertFile, edCertFile,    edKeyFile,
52055             cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
52056     }
52057     #endif
52058 #endif /* HAVE_IO_TESTS_DEPENDENCIES */
52059 
52060     printf(resultFmt, passed);
52061 #endif
52062 }
52063 
52064 
52065 
52066 /*----------------------------------------------------------------------------*
52067  | Main
52068  *----------------------------------------------------------------------------*/
52069 
52070 void ApiTest(void)
52071 {
52072     printf("\n-----------------Porting tests------------------\n");
52073     AssertTrue(test_fileAccess());
52074 
52075     printf(" Begin API Tests\n");
52076     AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
52077     /* wolfcrypt initialization tests */
52078     test_wolfSSL_Method_Allocators();
52079 #ifndef NO_WOLFSSL_SERVER
52080     test_wolfSSL_CTX_new(wolfSSLv23_server_method());
52081 #endif
52082 #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
52083 (!defined(NO_RSA) || defined(HAVE_ECC))
52084     test_for_double_Free();
52085 #endif
52086 #ifdef HAVE_IO_TESTS_DEPENDENCIES
52087     test_wolfSSL_get_finished();
52088 #endif
52089     test_SSL_CIPHER_get_xxx();
52090     test_wolfSSL_ERR_strings();
52091     test_wolfSSL_EVP_shake128();
52092     test_wolfSSL_EVP_shake256();
52093     test_EVP_blake2();
52094     test_EVP_MD_do_all();
52095     test_OBJ_NAME_do_all();
52096     test_wolfSSL_CTX_use_certificate_file();
52097     AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
52098     test_wolfSSL_CTX_use_PrivateKey_file();
52099     test_wolfSSL_CTX_load_verify_locations();
52100     test_wolfSSL_CertManagerCheckOCSPResponse();
52101     test_wolfSSL_CertManagerLoadCABuffer();
52102     test_wolfSSL_CertManagerGetCerts();
52103     test_wolfSSL_CertManagerSetVerify();
52104     test_wolfSSL_CertManagerNameConstraint();
52105     test_wolfSSL_CertManagerNameConstraint2();
52106     test_wolfSSL_CertManagerNameConstraint3();
52107     test_wolfSSL_CertManagerNameConstraint4();
52108     test_wolfSSL_CertManagerNameConstraint5();
52109     test_wolfSSL_CertManagerCRL();
52110     test_wolfSSL_CTX_load_verify_locations_ex();
52111     test_wolfSSL_CTX_load_verify_buffer_ex();
52112     test_wolfSSL_CTX_load_verify_chain_buffer_format();
52113     test_wolfSSL_CTX_add1_chain_cert();
52114     test_wolfSSL_CTX_use_certificate_chain_file_format();
52115     test_wolfSSL_CTX_trust_peer_cert();
52116     test_wolfSSL_CTX_SetTmpDH_file();
52117     test_wolfSSL_CTX_SetTmpDH_buffer();
52118     test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
52119     test_wolfSSL_CTX_der_load_verify_locations();
52120     test_wolfSSL_CTX_enable_disable();
52121     test_wolfSSL_CTX_ticket_API();
52122     test_server_wolfSSL_new();
52123     test_client_wolfSSL_new();
52124     test_wolfSSL_SetTmpDH_file();
52125     test_wolfSSL_SetTmpDH_buffer();
52126     test_wolfSSL_SetMinMaxDhKey_Sz();
52127     test_SetTmpEC_DHE_Sz();
52128     test_wolfSSL_CTX_get0_privatekey();
52129     test_wolfSSL_dtls_set_mtu();
52130 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
52131     defined(HAVE_IO_TESTS_DEPENDENCIES)
52132     test_wolfSSL_read_write();
52133 #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
52134     test_wolfSSL_reuse_WOLFSSLobj();
52135 #endif
52136     test_wolfSSL_CTX_verifyDepth_ServerClient();
52137     test_wolfSSL_dtls_export();
52138     test_wolfSSL_tls_export();
52139 #endif
52140     AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
52141     AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
52142 
52143     /* TLS extensions tests */
52144 #ifdef HAVE_IO_TESTS_DEPENDENCIES
52145     test_wolfSSL_UseSNI();
52146 #endif
52147     test_wolfSSL_UseTrustedCA();
52148     test_wolfSSL_UseMaxFragment();
52149     test_wolfSSL_UseTruncatedHMAC();
52150     test_wolfSSL_UseSupportedCurve();
52151     test_wolfSSL_UseALPN();
52152     test_wolfSSL_DisableExtendedMasterSecret();
52153     test_wolfSSL_wolfSSL_UseSecureRenegotiation();
52154 
52155     /* X509 tests */
52156     test_wolfSSL_X509_NAME_get_entry();
52157     test_wolfSSL_PKCS12();
52158     test_wolfSSL_no_password_cb();
52159     test_wolfSSL_PKCS8();
52160     test_wolfSSL_PKCS8_ED25519();
52161     test_wolfSSL_PKCS8_ED448();
52162     test_wolfSSL_PKCS5();
52163     test_wolfSSL_URI();
52164     test_wolfSSL_TBS();
52165     test_wolfSSL_X509_verify();
52166     test_wolfSSL_X509_TLS_version();
52167 
52168     test_wc_PemToDer();
52169     test_wc_AllocDer();
52170     test_wc_CertPemToDer();
52171     test_wc_PubKeyPemToDer();
52172     test_wc_PemPubKeyToDer();
52173     test_wc_GetPubKeyDerFromCert();
52174 
52175     /*OCSP Stapling. */
52176     AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
52177     AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
52178 
52179     /* Multicast */
52180     test_wolfSSL_mcast();
52181 
52182     /* compatibility tests */
52183     test_wolfSSL_lhash();
52184     test_wolfSSL_X509_NAME();
52185     test_wolfSSL_X509_NAME_hash();
52186 #ifndef NO_BIO
52187     test_wolfSSL_X509_INFO_multiple_info();
52188     test_wolfSSL_X509_INFO();
52189 #endif
52190     test_wolfSSL_X509_subject_name_hash();
52191     test_wolfSSL_X509_issuer_name_hash();
52192     test_wolfSSL_X509_check_host();
52193     test_wolfSSL_X509_check_email();
52194     test_wolfSSL_DES();
52195     test_wolfSSL_certs();
52196     test_wolfSSL_X509_check_private_key();
52197     test_wolfSSL_ASN1_TIME_print();
52198     test_wolfSSL_ASN1_UTCTIME_print();
52199     test_wolfSSL_ASN1_TIME_diff();
52200     test_wolfSSL_ASN1_GENERALIZEDTIME_free();
52201     test_wolfSSL_private_keys();
52202     test_wolfSSL_PEM_read_PrivateKey();
52203     test_wolfSSL_PEM_PrivateKey();
52204 #ifndef NO_BIO
52205     test_wolfSSL_PEM_bio_RSAKey();
52206     test_wolfSSL_PEM_bio_DSAKey();
52207     test_wolfSSL_PEM_bio_ECKey();
52208     test_wolfSSL_PEM_RSAPrivateKey();
52209     test_wolfSSL_PEM_PUBKEY();
52210 #endif
52211     test_DSA_do_sign_verify();
52212     test_wolfSSL_tmp_dh();
52213     test_wolfSSL_ctrl();
52214     test_wolfSSL_EVP_MD_size();
52215     test_wolfSSL_EVP_MD_pkey_type();
52216     test_wolfSSL_EVP_Digest();
52217     test_wolfSSL_EVP_Digest_all();
52218     test_wolfSSL_EVP_PKEY_new_mac_key();
52219     test_wolfSSL_EVP_MD_hmac_signing();
52220     test_wolfSSL_EVP_MD_rsa_signing();
52221     test_wolfSSL_EVP_MD_ecc_signing();
52222     test_wolfSSL_EVP_PKEY_print_public();
52223     test_wolfSSL_EVP_ENCODE_CTX_new();
52224     test_wolfSSL_EVP_ENCODE_CTX_free();
52225     test_wolfSSL_EVP_EncodeInit();
52226     test_wolfSSL_EVP_EncodeUpdate();
52227     test_wolfSSL_EVP_EncodeFinal();
52228     test_wolfSSL_EVP_DecodeInit();
52229     test_wolfSSL_EVP_DecodeUpdate();
52230     test_wolfSSL_EVP_DecodeFinal();
52231     test_wolfSSL_CTX_add_extra_chain_cert();
52232 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
52233     test_wolfSSL_ERR_peek_last_error_line();
52234 #endif
52235 #ifndef NO_BIO
52236     test_wolfSSL_ERR_print_errors_cb();
52237     AssertFalse(test_wolfSSL_GetLoggingCb());
52238     AssertFalse(test_WOLFSSL_ERROR_MSG());
52239     AssertFalse(test_wc_ERR_remove_state());
52240     AssertFalse(test_wc_ERR_print_errors_fp());
52241 #endif
52242     test_wolfSSL_set_options();
52243     test_wolfSSL_sk_SSL_CIPHER();
52244     test_wolfSSL_set1_curves_list();
52245     test_wolfSSL_set1_sigalgs_list();
52246     test_wolfSSL_PKCS7_certs();
52247     test_wolfSSL_X509_STORE_CTX();
52248     test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup();
52249     test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
52250     test_wolfSSL_msgCb();
52251     test_wolfSSL_either_side();
52252     test_wolfSSL_DTLS_either_side();
52253     test_generate_cookie();
52254     test_wolfSSL_X509_STORE_set_flags();
52255     test_wolfSSL_X509_LOOKUP_load_file();
52256     test_wolfSSL_X509_Name_canon();
52257     test_wolfSSL_X509_LOOKUP_ctrl_file();
52258     test_wolfSSL_X509_LOOKUP_ctrl_hash_dir();
52259     test_wolfSSL_X509_NID();
52260     test_wolfSSL_X509_STORE_CTX_set_time();
52261     test_wolfSSL_get0_param();
52262     test_wolfSSL_X509_VERIFY_PARAM_set1_host();
52263     test_wolfSSL_X509_VERIFY_PARAM_set1_ip();
52264     test_wolfSSL_X509_STORE_CTX_get0_store();
52265     test_wolfSSL_X509_STORE();
52266     test_wolfSSL_X509_STORE_load_locations();
52267     test_X509_STORE_get0_objects();
52268     test_wolfSSL_X509_load_crl_file();
52269     test_wolfSSL_BN();
52270     test_wolfSSL_CTX_get0_set1_param();
52271 #ifndef NO_BIO
52272     test_wolfSSL_PEM_read_bio();
52273     test_wolfSSL_BIO();
52274 #endif
52275     test_wolfSSL_ASN1_STRING();
52276     test_wolfSSL_ASN1_BIT_STRING();
52277     test_wolfSSL_a2i_ASN1_INTEGER();
52278     test_wolfSSL_a2i_IPADDRESS();
52279     test_wolfSSL_X509();
52280     test_wolfSSL_X509_VERIFY_PARAM();
52281     test_wolfSSL_X509_sign();
52282     test_wolfSSL_X509_sign2();
52283     test_wolfSSL_X509_get0_tbs_sigalg();
52284     test_wolfSSL_X509_ALGOR_get0();
52285     test_wolfSSL_X509_get_X509_PUBKEY();
52286     test_wolfSSL_X509_PUBKEY_RSA();
52287     test_wolfSSL_X509_PUBKEY_EC();
52288     test_wolfSSL_X509_PUBKEY_DSA();
52289     test_wolfSSL_RAND();
52290     test_wolfSSL_BUF();
52291     test_wolfSSL_set_tlsext_status_type();
52292     test_wolfSSL_ASN1_TIME_adj();
52293     test_wolfSSL_ASN1_TIME_to_tm();
52294     test_wolfSSL_X509_cmp_time();
52295     test_wolfSSL_X509_time_adj();
52296     test_wolfSSL_CTX_set_client_CA_list();
52297     test_wolfSSL_CTX_add_client_CA();
52298     test_wolfSSL_CTX_set_srp_username();
52299     test_wolfSSL_CTX_set_srp_password();
52300     test_wolfSSL_CTX_set_keylog_callback();
52301     test_wolfSSL_CTX_get_keylog_callback();
52302     test_wolfSSL_Tls12_Key_Logging_test();
52303     test_wolfSSL_Tls13_Key_Logging_test();
52304     test_wolfSSL_Tls13_postauth();
52305     test_wolfSSL_CTX_set_ecdh_auto();
52306     test_wolfSSL_set_minmax_proto_version();
52307     test_wolfSSL_THREADID_hash();
52308     test_wolfSSL_RAND_set_rand_method();
52309     test_wolfSSL_RAND_bytes();
52310     test_wolfSSL_BN_rand();
52311     test_wolfSSL_pseudo_rand();
52312     test_wolfSSL_PKCS8_Compat();
52313     test_wolfSSL_PKCS8_d2i();
52314     test_error_queue_per_thread();
52315     test_wolfSSL_ERR_put_error();
52316 #ifndef NO_BIO
52317     test_wolfSSL_ERR_print_errors();
52318 #endif
52319     test_wolfSSL_HMAC();
52320     test_wolfSSL_CMAC();
52321     test_wolfSSL_OBJ();
52322     test_wolfSSL_i2a_ASN1_OBJECT();
52323     test_wolfSSL_OBJ_cmp();
52324     test_wolfSSL_OBJ_txt2nid();
52325     test_wolfSSL_OBJ_txt2obj();
52326     test_wolfSSL_i2t_ASN1_OBJECT();
52327     test_wolfSSL_PEM_write_bio_X509();
52328     test_wolfSSL_X509_NAME_ENTRY();
52329     test_wolfSSL_X509_set_name();
52330     test_wolfSSL_X509_set_notAfter();
52331     test_wolfSSL_X509_set_notBefore();
52332     test_wolfSSL_X509_set_version();
52333 #ifndef NO_BIO
52334     test_wolfSSL_BIO_gets();
52335     test_wolfSSL_BIO_puts();
52336     test_wolfSSL_BIO_should_retry();
52337     test_wolfSSL_d2i_PUBKEY();
52338     test_wolfSSL_BIO_write();
52339     test_wolfSSL_BIO_connect();
52340     test_wolfSSL_BIO_accept();
52341     test_wolfSSL_BIO_printf();
52342     test_wolfSSL_BIO_f_md();
52343     test_wolfSSL_BIO_up_ref();
52344 #endif
52345     test_wolfSSL_cert_cb();
52346     test_wolfSSL_SESSION();
52347     test_wolfSSL_ticket_keys();
52348     test_wolfSSL_DES_ecb_encrypt();
52349     test_wolfSSL_sk_GENERAL_NAME();
52350     test_wolfSSL_GENERAL_NAME_print();
52351     test_wolfSSL_sk_DIST_POINT();
52352     test_wolfSSL_MD4();
52353     test_wolfSSL_RSA();
52354     test_wolfSSL_RSA_DER();
52355     test_wolfSSL_RSA_get0_key();
52356     test_wolfSSL_RSA_meth();
52357     test_wolfSSL_verify_mode();
52358     test_wolfSSL_verify_depth();
52359     test_wolfSSL_HMAC_CTX();
52360     test_wolfSSL_msg_callback();
52361     test_wolfSSL_SHA();
52362     test_wolfSSL_DH_1536_prime();
52363     test_wolfSSL_PEM_write_DHparams();
52364     test_wolfSSL_PEM_read_DHparams();
52365     test_wolfSSL_AES_ecb_encrypt();
52366     test_wolfSSL_MD5();
52367     test_wolfSSL_MD5_Transform();
52368     test_wolfSSL_SHA_Transform();
52369     test_wolfSSL_SHA256();
52370     test_wolfSSL_SHA256_Transform();
52371     test_wolfSSL_SHA224();
52372     test_wolfSSL_SHA512_Transform();
52373     test_wolfSSL_X509_get_serialNumber();
52374     test_wolfSSL_X509_CRL();
52375     test_wolfSSL_d2i_X509_REQ();
52376     test_wolfSSL_PEM_read_X509();
52377     test_wolfSSL_PEM_read();
52378 #ifndef NO_BIO
52379     test_wolfSSL_PEM_X509_INFO_read_bio();
52380     test_wolfSSL_PEM_read_bio_ECPKParameters();
52381 #endif
52382     test_wolfSSL_X509_STORE_get1_certs();
52383     test_wolfSSL_X509_NAME_ENTRY_get_object();
52384     test_wolfSSL_OpenSSL_add_all_algorithms();
52385     test_wolfSSL_OPENSSL_hexstr2buf();
52386     test_wolfSSL_ASN1_STRING_print_ex();
52387     test_wolfSSL_ASN1_TIME_to_generalizedtime();
52388     test_wolfSSL_ASN1_INTEGER_get_set();
52389     test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
52390     test_wolfSSL_i2c_ASN1_INTEGER();
52391     test_wolfSSL_X509_check_ca();
52392     test_wolfSSL_X509_check_ip_asc();
52393     test_wolfSSL_DC_cert();
52394     test_wolfSSL_DES_ncbc();
52395     test_wolfSSL_AES_cbc_encrypt();
52396     test_wolfSSL_CRYPTO_cts128();
52397     test_wolfssl_EVP_aes_gcm_AAD_2_parts();
52398     test_wolfssl_EVP_aes_gcm();
52399     test_wolfSSL_PKEY_up_ref();
52400     test_wolfSSL_EVP_Cipher_extra();
52401     test_wolfSSL_d2i_and_i2d_PublicKey();
52402     test_wolfSSL_d2i_and_i2d_DSAparams();
52403     test_wolfSSL_i2d_PrivateKey();
52404     test_wolfSSL_OCSP_id_get0_info();
52405     test_wolfSSL_i2d_OCSP_CERTID();
52406     test_wolfSSL_OCSP_id_cmp();
52407     test_wolfSSL_OCSP_SINGLERESP_get0_id();
52408     test_wolfSSL_OCSP_single_get0_status();
52409     test_wolfSSL_OCSP_resp_count();
52410     test_wolfSSL_OCSP_resp_get0();
52411     test_wolfSSL_EVP_PKEY_derive();
52412     test_wolfSSL_EVP_PBE_scrypt();
52413 #ifndef NO_RSA
52414     test_wolfSSL_RSA_padding_add_PKCS1_PSS();
52415 #endif
52416     test_wolfSSL_RSA_sign_sha3();
52417 
52418     test_CONF_modules_xxx();
52419     test_CRYPTO_set_dynlock_xxx();
52420     test_CRYPTO_THREADID_xxx();
52421     test_ENGINE_cleanup();
52422     test_wolfSSL_EC_KEY_set_group();
52423 #if defined(OPENSSL_ALL)
52424     test_wolfSSL_X509_PUBKEY_get();
52425     test_wolfSSL_sk_CIPHER_description();
52426     test_wolfSSL_get_ciphers_compat();
52427     test_wolfSSL_d2i_DHparams();
52428     test_wolfSSL_i2d_DHparams();
52429     test_wolfSSL_ASN1_STRING_to_UTF8();
52430     test_wolfSSL_ASN1_UNIVERSALSTRING_to_string();
52431     test_wolfSSL_EC_KEY_dup();
52432     test_wolfSSL_EVP_PKEY_set1_get1_DSA();
52433     test_wolfSSL_DSA_SIG();
52434     test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
52435     test_wolfSSL_EVP_PKEY_set1_get1_DH();
52436     test_wolfSSL_CTX_ctrl();
52437     test_wolfSSL_DH_check();
52438     test_wolfSSL_EVP_PKEY_assign();
52439     test_wolfSSL_EVP_PKEY_base_id();
52440     test_wolfSSL_EVP_PKEY_id();
52441     test_wolfSSL_EVP_PKEY_paramgen();
52442     test_wolfSSL_EVP_PKEY_keygen();
52443     test_wolfSSL_EVP_PKEY_keygen_init();
52444     test_wolfSSL_EVP_PKEY_missing_parameters();
52445     test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits();
52446     test_wolfSSL_EVP_CIPHER_CTX_iv_length();
52447     test_wolfSSL_EVP_CIPHER_CTX_key_length();
52448     test_wolfSSL_EVP_CIPHER_CTX_set_key_length();
52449     test_wolfSSL_EVP_CIPHER_CTX_set_iv();
52450     test_wolfSSL_EVP_PKEY_CTX_new_id();
52451     test_wolfSSL_EVP_rc4();
52452     test_wolfSSL_EVP_enc_null();
52453     test_wolfSSL_EVP_rc2_cbc();
52454     test_wolfSSL_EVP_mdc2();
52455     test_wolfSSL_EVP_md4();
52456     test_wolfSSL_EVP_aes_256_gcm();
52457     test_wolfSSL_EVP_aes_192_gcm();
52458     test_wolfSSL_EVP_ripemd160();
52459     test_wolfSSL_EVP_get_digestbynid();
52460     test_wolfSSL_EVP_MD_nid();
52461     test_wolfSSL_EVP_PKEY_get0_EC_KEY();
52462     test_wolfSSL_EVP_X_STATE();
52463     test_wolfSSL_EVP_X_STATE_LEN();
52464     test_wolfSSL_EVP_CIPHER_block_size();
52465     test_wolfSSL_EVP_CIPHER_iv_length();
52466     test_wolfSSL_EVP_SignInit_ex();
52467     test_wolfSSL_EVP_DigestFinal_ex();
52468     test_wolfSSL_EVP_PKEY_assign_DH();
52469     test_wolfSSL_EVP_BytesToKey();
52470     test_wolfSSL_EVP_PKEY_param_check();
52471     test_wolfSSL_QT_EVP_PKEY_CTX_free();
52472     test_IncCtr();
52473     test_wolfSSL_OBJ_ln();
52474     test_wolfSSL_OBJ_sn();
52475     test_wolfSSL_TXT_DB();
52476     test_wolfSSL_NCONF();
52477 
52478 #endif /* OPENSSL_ALL */
52479 
52480 #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
52481     AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
52482 #ifndef NO_BIO
52483     test_wolfSSL_d2i_PrivateKeys_bio();
52484 #endif
52485 #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
52486 
52487     test_wolfSSL_X509_CA_num();
52488     test_wolfSSL_X509_get_version();
52489 #ifndef NO_BIO
52490     test_wolfSSL_X509_print();
52491     test_wolfSSL_BIO_get_len();
52492 #endif
52493     test_wolfSSL_RSA_verify();
52494     test_wolfSSL_X509V3_EXT_get();
52495     test_wolfSSL_X509V3_EXT_nconf();
52496     test_wolfSSL_X509V3_EXT();
52497     test_wolfSSL_X509_get_extension_flags();
52498     test_wolfSSL_X509_get_ext();
52499     test_wolfSSL_X509_get_ext_by_NID();
52500     test_wolfSSL_X509_get_ext_subj_alt_name();
52501     test_wolfSSL_X509_get_ext_count();
52502     test_wolfSSL_X509_EXTENSION_new();
52503     test_wolfSSL_X509_EXTENSION_get_object();
52504     test_wolfSSL_X509_EXTENSION_get_data();
52505     test_wolfSSL_X509_EXTENSION_get_critical();
52506     test_wolfSSL_X509V3_EXT_print();
52507     test_wolfSSL_X509_cmp();
52508 #ifndef NO_BIO
52509     test_wolfSSL_RSA_print();
52510     test_wolfSSL_ASN1_STRING_print();
52511 #endif
52512     test_wolfSSL_ASN1_get_object();
52513     test_openssl_generate_key_and_cert();
52514 
52515     test_wolfSSL_EC_get_builtin_curves();
52516 
52517     test_wolfSSL_CRYPTO_memcmp();
52518     test_wolfSSL_read_detect_TCP_disconnect();
52519     /* test the no op functions for compatibility */
52520     test_no_op_functions();
52521 
52522     /* OpenSSL EVP_PKEY API tests */
52523     test_EVP_PKEY_rsa();
52524     test_wolfSSL_EVP_PKEY_encrypt();
52525     test_wolfSSL_EVP_PKEY_sign();
52526     test_EVP_PKEY_ec();
52527     test_EVP_PKEY_cmp();
52528     /* OpenSSL error API tests */
52529     test_ERR_load_crypto_strings();
52530     /* OpenSSL sk_X509 API test */
52531     test_sk_X509();
52532     /* OpenSSL sk_X509_CRL API test */
52533     test_sk_X509_CRL();
52534     /* OpenSSL X509 API test */
52535     test_X509_get_signature_nid();
52536     /* OpenSSL X509 REQ API test */
52537     test_X509_REQ();
52538     /* OpenSSL PKCS7 API test */
52539     test_wolfssl_PKCS7();
52540     test_wolfSSL_PKCS7_SIGNED_new();
52541 #ifndef NO_BIO
52542     test_wolfSSL_PEM_write_bio_PKCS7();
52543 #ifdef HAVE_SMIME
52544     test_wolfSSL_SMIME_read_PKCS7();
52545 #endif
52546 #endif
52547 
52548     /* wolfCrypt ASN tests */
52549     test_wc_CreateEncryptedPKCS8Key();
52550     test_wc_GetPkcs8TraditionalOffset();
52551     test_wc_SetSubjectRaw();
52552     test_wc_GetSubjectRaw();
52553     test_wc_SetIssuerRaw();
52554     test_wc_SetIssueBuffer();
52555     test_wc_SetSubjectKeyId();
52556     test_wc_SetSubject();
52557     test_CheckCertSignature();
52558     test_wc_ParseCert();
52559 
52560     /* wolfCrypt ECC tests */
52561     test_wc_ecc_get_curve_size_from_name();
52562     test_wc_ecc_get_curve_id_from_name();
52563     test_wc_ecc_get_curve_id_from_params();
52564 #ifdef WOLFSSL_TLS13
52565     /* TLS v1.3 API tests */
52566     test_tls13_apis();
52567 #endif
52568 
52569 #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
52570                            !defined(WOLFSSL_NO_CLIENT_AUTH))
52571     /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
52572     /* Bad certificate signature tests */
52573     AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
52574     AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
52575 #endif /* NO_CERTS */
52576 
52577 #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
52578     !defined(NO_OLD_TLS))
52579     /* public key callback tests */
52580     test_DhCallbacks();
52581 #endif
52582 
52583 
52584 #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
52585     test_export_keying_material();
52586 #endif /* HAVE_KEYING_MATERIAL */
52587 
52588     test_wolfSSL_CTX_get_min_proto_version();
52589 
52590     test_wolfSSL_security_level();
52591     test_wolfSSL_SSL_in_init();
52592     test_wolfSSL_EC_curve();
52593     test_wolfSSL_CTX_set_timeout();
52594     test_wolfSSL_OpenSSL_version();
52595     test_wolfSSL_set_psk_use_session_callback();
52596 
52597     test_CONF_CTX_FILE();
52598     test_CONF_CTX_CMDLINE();
52599     test_wolfSSL_CRYPTO_get_ex_new_index();
52600     test_wolfSSL_DH();
52601 
52602     /*wolfcrypt */
52603     printf("\n-----------------wolfcrypt unit tests------------------\n");
52604     AssertFalse(test_wolfCrypt_Init());
52605     AssertFalse(test_wc_InitMd5());
52606     AssertFalse(test_wc_Md5Update());
52607     AssertFalse(test_wc_Md5Final());
52608     AssertFalse(test_wc_InitSha());
52609     AssertFalse(test_wc_ShaUpdate());
52610     AssertFalse(test_wc_ShaFinal());
52611     AssertFalse(test_wc_InitSha256());
52612     AssertFalse(test_wc_Sha256Update());
52613     AssertFalse(test_wc_Sha256Final());
52614     AssertFalse(test_wc_Sha256FinalRaw());
52615     AssertFalse(test_wc_Sha256GetFlags());
52616     AssertFalse(test_wc_Sha256Free());
52617     AssertFalse(test_wc_Sha256GetHash());
52618     AssertFalse(test_wc_Sha256Copy());
52619     AssertFalse(test_wc_InitSha512());
52620     AssertFalse(test_wc_Sha512Update());
52621     AssertFalse(test_wc_Sha512Final());
52622     AssertFalse(test_wc_Sha512GetFlags());
52623     AssertFalse(test_wc_Sha512FinalRaw());
52624     AssertFalse(test_wc_Sha512Free());
52625     AssertFalse(test_wc_Sha512GetHash());
52626     AssertFalse(test_wc_Sha512Copy());
52627 
52628     AssertFalse(test_wc_InitSha512_224());
52629     AssertFalse(test_wc_Sha512_224Update());
52630     AssertFalse(test_wc_Sha512_224Final());
52631     AssertFalse(test_wc_Sha512_224GetFlags());
52632     AssertFalse(test_wc_Sha512_224FinalRaw());
52633     AssertFalse(test_wc_Sha512_224Free());
52634     AssertFalse(test_wc_Sha512_224GetHash());
52635     AssertFalse(test_wc_Sha512_224Copy());
52636     AssertFalse(test_wc_InitSha512_256());
52637     AssertFalse(test_wc_Sha512_256Update());
52638     AssertFalse(test_wc_Sha512_256Final());
52639     AssertFalse(test_wc_Sha512_256GetFlags());
52640     AssertFalse(test_wc_Sha512_256FinalRaw());
52641     AssertFalse(test_wc_Sha512_256Free());
52642     AssertFalse(test_wc_Sha512_256GetHash());
52643     AssertFalse(test_wc_Sha512_256Copy());
52644 
52645     AssertFalse(test_wc_InitSha384());
52646     AssertFalse(test_wc_Sha384Update());
52647     AssertFalse(test_wc_Sha384Final());
52648     AssertFalse(test_wc_Sha384GetFlags());
52649     AssertFalse(test_wc_Sha384FinalRaw());
52650     AssertFalse(test_wc_Sha384Free());
52651     AssertFalse(test_wc_Sha384GetHash());
52652     AssertFalse(test_wc_Sha384Copy());
52653     AssertFalse(test_wc_InitSha224());
52654     AssertFalse(test_wc_Sha224Update());
52655     AssertFalse(test_wc_Sha224Final());
52656     AssertFalse(test_wc_Sha224SetFlags());
52657     AssertFalse(test_wc_Sha224GetFlags());
52658     AssertFalse(test_wc_Sha224Free());
52659     AssertFalse(test_wc_Sha224GetHash());
52660     AssertFalse(test_wc_Sha224Copy());
52661     AssertFalse(test_wc_InitBlake2b());
52662     AssertFalse(test_wc_InitBlake2b_WithKey());
52663     AssertFalse(test_wc_InitBlake2s_WithKey());
52664     AssertFalse(test_wc_InitRipeMd());
52665     AssertFalse(test_wc_RipeMdUpdate());
52666     AssertFalse(test_wc_RipeMdFinal());
52667 
52668     AssertIntEQ(test_wc_InitSha3(), 0);
52669     AssertIntEQ(testing_wc_Sha3_Update(), 0);
52670     AssertIntEQ(test_wc_Sha3_224_Final(), 0);
52671     AssertIntEQ(test_wc_Sha3_256_Final(), 0);
52672     AssertIntEQ(test_wc_Sha3_384_Final(), 0);
52673     AssertIntEQ(test_wc_Sha3_512_Final(), 0);
52674     AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
52675     AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
52676     AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
52677     AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
52678     AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
52679     AssertIntEQ(test_wc_InitShake256(), 0);
52680     AssertIntEQ(testing_wc_Shake256_Update(), 0);
52681     AssertIntEQ(test_wc_Shake256_Final(), 0);
52682     AssertIntEQ(test_wc_Shake256_Copy(), 0);
52683     AssertIntEQ(test_wc_Shake256Hash(), 0);
52684 
52685     AssertFalse(test_wc_Md5HmacSetKey());
52686     AssertFalse(test_wc_Md5HmacUpdate());
52687     AssertFalse(test_wc_Md5HmacFinal());
52688     AssertFalse(test_wc_ShaHmacSetKey());
52689     AssertFalse(test_wc_ShaHmacUpdate());
52690     AssertFalse(test_wc_ShaHmacFinal());
52691     AssertFalse(test_wc_Sha224HmacSetKey());
52692     AssertFalse(test_wc_Sha224HmacUpdate());
52693     AssertFalse(test_wc_Sha224HmacFinal());
52694     AssertFalse(test_wc_Sha256HmacSetKey());
52695     AssertFalse(test_wc_Sha256HmacUpdate());
52696     AssertFalse(test_wc_Sha256HmacFinal());
52697     AssertFalse(test_wc_Sha384HmacSetKey());
52698     AssertFalse(test_wc_Sha384HmacUpdate());
52699     AssertFalse(test_wc_Sha384HmacFinal());
52700 
52701     AssertIntEQ(test_wc_HashInit(), 0);
52702     AssertIntEQ(test_wc_HashSetFlags(), 0);
52703     AssertIntEQ(test_wc_HashGetFlags(), 0);
52704 
52705     AssertIntEQ(test_wc_InitCmac(), 0);
52706     AssertIntEQ(test_wc_CmacUpdate(), 0);
52707     AssertIntEQ(test_wc_CmacFinal(), 0);
52708     AssertIntEQ(test_wc_AesCmacGenerate(), 0);
52709     AssertIntEQ(test_wc_AesGcmStream(), 0);
52710 
52711     AssertIntEQ(test_wc_Des3_SetIV(), 0);
52712     AssertIntEQ(test_wc_Des3_SetKey(), 0);
52713     AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
52714     AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
52715     AssertIntEQ(test_wc_Des3_EcbEncrypt(), 0);
52716     AssertIntEQ(test_wc_IdeaSetKey(), 0);
52717     AssertIntEQ(test_wc_IdeaSetIV(), 0);
52718     AssertIntEQ(test_wc_IdeaCipher(), 0);
52719     AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
52720     AssertIntEQ(test_wc_Chacha_SetKey(), 0);
52721     AssertIntEQ(test_wc_Chacha_Process(), 0);
52722     AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
52723     AssertIntEQ(test_wc_Poly1305SetKey(), 0);
52724 
52725     AssertIntEQ(test_wc_CamelliaSetKey(), 0);
52726     AssertIntEQ(test_wc_CamelliaSetIV(), 0);
52727     AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
52728     AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
52729 
52730 
52731     AssertIntEQ(test_wc_RabbitSetKey(), 0);
52732     AssertIntEQ(test_wc_RabbitProcess(), 0);
52733 
52734     AssertIntEQ(test_wc_Arc4SetKey(), 0);
52735     AssertIntEQ(test_wc_Arc4Process(), 0);
52736 
52737     AssertIntEQ(test_wc_Rc2SetKey(), 0);
52738     AssertIntEQ(test_wc_Rc2SetIV(), 0);
52739     AssertIntEQ(test_wc_Rc2EcbEncryptDecrypt(), 0);
52740     AssertIntEQ(test_wc_Rc2CbcEncryptDecrypt(), 0);
52741 
52742     AssertIntEQ(test_wc_AesSetKey(), 0);
52743     AssertIntEQ(test_wc_AesSetIV(), 0);
52744     AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
52745     AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
52746     AssertIntEQ(test_wc_AesGcmSetKey(), 0);
52747     AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
52748     AssertIntEQ(test_wc_GmacSetKey(), 0);
52749     AssertIntEQ(test_wc_GmacUpdate(), 0);
52750     AssertIntEQ(test_wc_InitRsaKey(), 0);
52751     AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
52752     AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
52753     AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
52754     AssertIntEQ(test_wc_MakeRsaKey(), 0);
52755     AssertIntEQ(test_wc_SetKeyUsage (), 0);
52756 
52757     AssertIntEQ(test_wc_CheckProbablePrime (), 0);
52758     AssertIntEQ(test_wc_RsaPSS_Verify (), 0);
52759     AssertIntEQ(test_wc_RsaPSS_VerifyCheck (), 0);
52760     AssertIntEQ(test_wc_RsaPSS_VerifyCheckInline (), 0);
52761 
52762     AssertIntEQ(test_wc_SetMutexCb(), 0);
52763     AssertIntEQ(test_wc_LockMutex_ex(), 0);
52764 
52765     AssertIntEQ(test_wc_RsaKeyToDer(), 0);
52766     AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
52767     AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
52768     AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
52769     AssertIntEQ(test_wc_RsaEncryptSize(), 0);
52770     AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
52771     AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
52772     AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
52773     AssertIntEQ(test_wc_AesCcmSetKey(), 0);
52774     AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
52775     AssertIntEQ(test_wc_Hc128_SetKey(), 0);
52776     AssertIntEQ(test_wc_Hc128_Process(), 0);
52777     AssertIntEQ(test_wc_InitDsaKey(), 0);
52778     AssertIntEQ(test_wc_DsaSignVerify(), 0);
52779     AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
52780     AssertIntEQ(test_wc_MakeDsaKey(), 0);
52781     AssertIntEQ(test_wc_DsaKeyToDer(), 0);
52782     AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
52783     AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
52784     AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
52785     AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
52786     AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
52787     AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
52788     AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
52789     wolfCrypt_Cleanup();
52790 
52791 #ifdef OPENSSL_EXTRA
52792     /*wolfSSL_EVP_get_cipherbynid test*/
52793     test_wolfSSL_EVP_get_cipherbynid();
52794     test_wolfSSL_EVP_CIPHER_CTX();
52795 #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
52796     test_wolfSSL_EC();
52797 #endif
52798     test_wolfSSL_ECDSA_SIG();
52799     test_ECDSA_size_sign();
52800     test_ED25519();
52801     test_ED448();
52802     test_EC_i2d();
52803 #endif
52804 #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
52805     !defined(HAVE_SELFTEST) && \
52806     !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
52807     test_wc_ecc_get_curve_id_from_dp_params();
52808 #endif
52809 
52810 #ifdef HAVE_HASHDRBG
52811     #ifdef TEST_RESEED_INTERVAL
52812     AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
52813     #endif
52814     AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
52815 
52816 #endif
52817     AssertIntEQ(test_get_rand_digit(), 0);
52818     AssertIntEQ(test_get_digit_count(), 0);
52819     AssertIntEQ(test_mp_cond_copy(), 0);
52820     AssertIntEQ(test_mp_rand(), 0);
52821     AssertIntEQ(test_get_digit(), 0);
52822     AssertIntEQ(test_wc_export_int(), 0);
52823     AssertIntEQ(test_wc_InitRngNonce(), 0);
52824     AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
52825     AssertIntEQ(test_wc_ed25519_make_key(), 0);
52826     AssertIntEQ(test_wc_ed25519_init(), 0);
52827     AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
52828     AssertIntEQ(test_wc_ed25519_import_public(), 0);
52829     AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
52830     AssertIntEQ(test_wc_ed25519_export(), 0);
52831     AssertIntEQ(test_wc_ed25519_size(), 0);
52832     AssertIntEQ(test_wc_ed25519_exportKey(), 0);
52833     AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
52834     AssertIntEQ(test_wc_curve25519_init(), 0);
52835     AssertIntEQ(test_wc_curve25519_size(), 0);
52836     AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
52837     AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
52838     AssertIntEQ(test_wc_curve25519_size (), 0);
52839     AssertIntEQ(test_wc_curve25519_make_key (), 0);
52840     AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
52841     AssertIntEQ(test_wc_curve25519_make_pub (), 0);
52842     AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
52843     AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
52844     AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
52845     AssertIntEQ(test_wc_curve25519_import_private (), 0);
52846     AssertIntEQ(test_wc_ed448_make_key(), 0);
52847     AssertIntEQ(test_wc_ed448_init(), 0);
52848     AssertIntEQ(test_wc_ed448_sign_msg(), 0);
52849     AssertIntEQ(test_wc_ed448_import_public(), 0);
52850     AssertIntEQ(test_wc_ed448_import_private_key(), 0);
52851     AssertIntEQ(test_wc_ed448_export(), 0);
52852     AssertIntEQ(test_wc_ed448_size(), 0);
52853     AssertIntEQ(test_wc_ed448_exportKey(), 0);
52854     AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
52855     AssertIntEQ(test_wc_curve448_make_key (), 0);
52856     AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
52857     AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
52858     AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
52859     AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
52860     AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
52861     AssertIntEQ(test_wc_curve448_import_private (), 0);
52862     AssertIntEQ(test_wc_curve448_init(), 0);
52863     AssertIntEQ(test_wc_curve448_size (), 0);
52864     AssertIntEQ(test_wc_ecc_make_key(), 0);
52865     AssertIntEQ(test_wc_ecc_init(), 0);
52866     AssertIntEQ(test_wc_ecc_check_key(), 0);
52867     AssertIntEQ(test_wc_ecc_get_generator(), 0);
52868     AssertIntEQ(test_wc_ecc_size(), 0);
52869     test_wc_ecc_params();
52870     AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
52871     PRIVATE_KEY_UNLOCK();
52872     AssertIntEQ(test_wc_ecc_shared_secret(), 0);
52873     AssertIntEQ(test_wc_ecc_export_x963(), 0);
52874     PRIVATE_KEY_LOCK();
52875     AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
52876     AssertIntEQ(test_wc_ecc_import_x963(), 0);
52877     AssertIntEQ(ecc_import_private_key(), 0);
52878     AssertIntEQ(test_wc_ecc_export_private_only(), 0);
52879     AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
52880     AssertIntEQ(test_wc_ecc_import_raw(), 0);
52881     AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
52882     AssertIntEQ(test_wc_ecc_sig_size(), 0);
52883     AssertIntEQ(test_wc_ecc_ctx_new(), 0);
52884     AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
52885     AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
52886     AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
52887     AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
52888     AssertIntEQ(test_wc_ecc_del_point(), 0);
52889     AssertIntEQ(test_wc_ecc_pointFns(), 0);
52890     AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
52891     AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
52892     AssertIntEQ(test_wc_ecc_mulmod(), 0);
52893     AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
52894     AssertIntEQ(test_wc_ecc_get_curve_id_from_oid(), 0);
52895     AssertIntEQ(test_wc_ecc_sig_size_calc(), 0);
52896 
52897 
52898     AssertIntEQ(test_ToTraditional(), 0);
52899     AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
52900     AssertIntEQ(test_wc_DhPublicKeyDecode(), 0);
52901     AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
52902     AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
52903     AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
52904     AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
52905     AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
52906     AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
52907     AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
52908 
52909     test_wc_PKCS7_New();
52910     test_wc_PKCS7_Init();
52911     test_wc_PKCS7_InitWithCert();
52912     test_wc_PKCS7_EncodeData();
52913     test_wc_PKCS7_EncodeSignedData();
52914     test_wc_PKCS7_EncodeSignedData_ex();
52915     test_wc_PKCS7_VerifySignedData();
52916     test_wc_PKCS7_EncodeDecodeEnvelopedData();
52917     test_wc_PKCS7_EncodeEncryptedData();
52918     test_wc_PKCS7_Degenerate();
52919     test_wc_PKCS7_BER();
52920     test_PKCS7_signed_enveloped();
52921     test_wc_PKCS7_NoDefaultSignedAttribs();
52922     test_wc_PKCS7_SetOriEncryptCtx();
52923     test_wc_PKCS7_SetOriDecryptCtx();
52924     test_wc_PKCS7_DecodeCompressedData();
52925 
52926     test_wc_i2d_PKCS12();
52927 
52928     test_wolfSSL_CTX_LoadCRL();
52929 
52930     test_wc_CryptoCb();
52931     test_wolfSSL_CTX_StaticMemory();
52932 
52933     AssertIntEQ(test_ForceZero(), 0);
52934 
52935     AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
52936 
52937 #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
52938     !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
52939                            !defined(WOLFSSL_NO_CLIENT_AUTH))
52940     AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
52941 #endif
52942 
52943     /* If at some point a stub get implemented this test should fail indicating
52944      * a need to implement a new test case
52945      */
52946     test_stubs_are_stubs();
52947 #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
52948                       && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
52949     wc_ecc_fp_free();  /* free per thread cache */
52950 #endif
52951     wolfSSL_Cleanup();
52952 
52953     (void)devId;
52954 
52955     printf(" End API Tests\n");
52956 
52957 }
52958